16e029025SZhihong Peng.. SPDX-License-Identifier: BSD-3-Clause 26e029025SZhihong Peng Copyright(c) 2021 Intel Corporation 36e029025SZhihong Peng 46e029025SZhihong PengRunning AddressSanitizer 56e029025SZhihong Peng======================== 66e029025SZhihong Peng 76e029025SZhihong Peng`AddressSanitizer 86e029025SZhihong Peng<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) 96e029025SZhihong Pengis a widely-used debugging tool to detect memory access errors. 106e029025SZhihong PengIt helps to detect issues like use-after-free, various kinds of buffer 116e029025SZhihong Pengoverruns in C/C++ programs, and other similar errors, as well as 126e029025SZhihong Pengprinting out detailed debug information whenever an error is detected. 136e029025SZhihong Peng 146e029025SZhihong PengAddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). 156e029025SZhihong Peng 166e029025SZhihong PengEnabling ASan is done by passing the -Db_sanitize=address option to the meson build system, 176e029025SZhihong Pengsee :ref:`linux_gsg_compiling_dpdk` for details. 186e029025SZhihong Peng 196e029025SZhihong PengThe way ASan is integrated with clang requires to allow undefined symbols when linking code. 206e029025SZhihong PengTo do this, the -Db_lundef=false option must be added. 216e029025SZhihong Peng 226e029025SZhihong PengAdditionally, passing -Dbuildtype=debug option might help getting more readable ASan reports. 236e029025SZhihong Peng 246e029025SZhihong PengExample:: 256e029025SZhihong Peng 266e029025SZhihong Peng - gcc: meson setup -Db_sanitize=address <build_dir> 276e029025SZhihong Peng - clang: meson setup -Db_sanitize=address -Db_lundef=false <build_dir> 286e029025SZhihong Peng 296e029025SZhihong Peng.. Note:: 306e029025SZhihong Peng 316e029025SZhihong Peng - The libasan package must be installed when compiling with gcc in Centos/RHEL. 326e029025SZhihong Peng - If the program is tested using cmdline, you may need to execute the 336e029025SZhihong Peng "stty echo" command when an error occurs. 346cc51b12SZhihong Peng 35*f2a66612SDavid ChristensenASan is aware of DPDK memory allocations, thanks to added instrumentation, and 36*f2a66612SDavid Christensenis enabled on all 64 bit architectures for Linux. Other architectures may have 37*f2a66612SDavid Christensento define ASAN_SHADOW_OFFSET. 386cc51b12SZhihong Peng 396cc51b12SZhihong PengExample heap-buffer-overflow error 406cc51b12SZhihong Peng---------------------------------- 416cc51b12SZhihong Peng 426cc51b12SZhihong PengAdd below unit test code in examples/helloworld/main.c:: 436cc51b12SZhihong Peng 446cc51b12SZhihong Peng Add code to helloworld: 456cc51b12SZhihong Peng char *p = rte_zmalloc(NULL, 9, 0); 466cc51b12SZhihong Peng if (!p) { 476cc51b12SZhihong Peng printf("rte_zmalloc error.\n"); 486cc51b12SZhihong Peng return -1; 496cc51b12SZhihong Peng } 506cc51b12SZhihong Peng p[9] = 'a'; 516cc51b12SZhihong Peng 526cc51b12SZhihong PengAbove code will result in heap-buffer-overflow error if ASan is enabled, because apply 9 bytes of memory but access the tenth byte, detailed error log as below:: 536cc51b12SZhihong Peng 546cc51b12SZhihong Peng ==369953==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7fb17f465809 at pc 0x5652e6707b84 bp 0x7ffea70eea20 sp 0x7ffea70eea10 WRITE of size 1 at 0x7fb17f465809 thread T0 556cc51b12SZhihong Peng #0 0x5652e6707b83 in main ../examples/helloworld/main.c:47 566cc51b12SZhihong Peng #1 0x7fb94953c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) 576cc51b12SZhihong Peng #2 0x5652e67079bd in _start (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-helloworld+0x8329bd) 586cc51b12SZhihong Peng 596cc51b12SZhihong Peng Address 0x7fb17f465809 is a wild pointer. 606cc51b12SZhihong Peng SUMMARY: AddressSanitizer: heap-buffer-overflow ../examples/helloworld/main.c:47 in main 616cc51b12SZhihong Peng 626cc51b12SZhihong PengNote:: 636cc51b12SZhihong Peng 646cc51b12SZhihong Peng - Some of the features of ASan (for example, 'Display memory application location, currently 656cc51b12SZhihong Peng displayed as a wild pointer') are not currently supported with DPDK allocations. 666cc51b12SZhihong Peng 676cc51b12SZhihong PengExample use-after-free error 686cc51b12SZhihong Peng---------------------------- 696cc51b12SZhihong Peng 706cc51b12SZhihong PengAdd below unit test code in examples/helloworld/main.c:: 716cc51b12SZhihong Peng 726cc51b12SZhihong Peng Add code to helloworld: 736cc51b12SZhihong Peng char *p = rte_zmalloc(NULL, 9, 0); 746cc51b12SZhihong Peng if (!p) { 756cc51b12SZhihong Peng printf("rte_zmalloc error.\n"); 766cc51b12SZhihong Peng return -1; 776cc51b12SZhihong Peng } 786cc51b12SZhihong Peng rte_free(p); 796cc51b12SZhihong Peng *p = 'a'; 806cc51b12SZhihong Peng 816cc51b12SZhihong PengAbove code will result in use-after-free error if ASan is enabled, because apply 9 bytes of memory but access the first byte after release, detailed error log as below:: 826cc51b12SZhihong Peng 836cc51b12SZhihong Peng ==417048==ERROR: AddressSanitizer: heap-use-after-free on address 0x7fc83f465800 at pc 0x564308a39b89 bp 0x7ffc8c85bf50 sp 0x7ffc8c85bf40 WRITE of size 1 at 0x7fc83f465800 thread T0 846cc51b12SZhihong Peng #0 0x564308a39b88 in main ../examples/helloworld/main.c:48 856cc51b12SZhihong Peng #1 0x7fd0079c60b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) 866cc51b12SZhihong Peng #2 0x564308a399bd in _start (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-helloworld+0x8329bd) 876cc51b12SZhihong Peng 886cc51b12SZhihong Peng Address 0x7fc83f465800 is a wild pointer. 896cc51b12SZhihong Peng SUMMARY: AddressSanitizer: heap-use-after-free ../examples/helloworld/main.c:48 in main 90