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. 34*6cc51b12SZhihong Peng 35*6cc51b12SZhihong PengASan is aware of DPDK memory allocations, thanks to added instrumentation. 36*6cc51b12SZhihong PengThis is only enabled on x86_64 at the moment. 37*6cc51b12SZhihong PengOther architectures may have to define ASAN_SHADOW_OFFSET. 38*6cc51b12SZhihong Peng 39*6cc51b12SZhihong PengExample heap-buffer-overflow error 40*6cc51b12SZhihong Peng---------------------------------- 41*6cc51b12SZhihong Peng 42*6cc51b12SZhihong PengAdd below unit test code in examples/helloworld/main.c:: 43*6cc51b12SZhihong Peng 44*6cc51b12SZhihong Peng Add code to helloworld: 45*6cc51b12SZhihong Peng char *p = rte_zmalloc(NULL, 9, 0); 46*6cc51b12SZhihong Peng if (!p) { 47*6cc51b12SZhihong Peng printf("rte_zmalloc error.\n"); 48*6cc51b12SZhihong Peng return -1; 49*6cc51b12SZhihong Peng } 50*6cc51b12SZhihong Peng p[9] = 'a'; 51*6cc51b12SZhihong Peng 52*6cc51b12SZhihong 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:: 53*6cc51b12SZhihong Peng 54*6cc51b12SZhihong 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 55*6cc51b12SZhihong Peng #0 0x5652e6707b83 in main ../examples/helloworld/main.c:47 56*6cc51b12SZhihong Peng #1 0x7fb94953c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) 57*6cc51b12SZhihong Peng #2 0x5652e67079bd in _start (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-helloworld+0x8329bd) 58*6cc51b12SZhihong Peng 59*6cc51b12SZhihong Peng Address 0x7fb17f465809 is a wild pointer. 60*6cc51b12SZhihong Peng SUMMARY: AddressSanitizer: heap-buffer-overflow ../examples/helloworld/main.c:47 in main 61*6cc51b12SZhihong Peng 62*6cc51b12SZhihong PengNote:: 63*6cc51b12SZhihong Peng 64*6cc51b12SZhihong Peng - Some of the features of ASan (for example, 'Display memory application location, currently 65*6cc51b12SZhihong Peng displayed as a wild pointer') are not currently supported with DPDK allocations. 66*6cc51b12SZhihong Peng 67*6cc51b12SZhihong PengExample use-after-free error 68*6cc51b12SZhihong Peng---------------------------- 69*6cc51b12SZhihong Peng 70*6cc51b12SZhihong PengAdd below unit test code in examples/helloworld/main.c:: 71*6cc51b12SZhihong Peng 72*6cc51b12SZhihong Peng Add code to helloworld: 73*6cc51b12SZhihong Peng char *p = rte_zmalloc(NULL, 9, 0); 74*6cc51b12SZhihong Peng if (!p) { 75*6cc51b12SZhihong Peng printf("rte_zmalloc error.\n"); 76*6cc51b12SZhihong Peng return -1; 77*6cc51b12SZhihong Peng } 78*6cc51b12SZhihong Peng rte_free(p); 79*6cc51b12SZhihong Peng *p = 'a'; 80*6cc51b12SZhihong Peng 81*6cc51b12SZhihong 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:: 82*6cc51b12SZhihong Peng 83*6cc51b12SZhihong 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 84*6cc51b12SZhihong Peng #0 0x564308a39b88 in main ../examples/helloworld/main.c:48 85*6cc51b12SZhihong Peng #1 0x7fd0079c60b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) 86*6cc51b12SZhihong Peng #2 0x564308a399bd in _start (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-helloworld+0x8329bd) 87*6cc51b12SZhihong Peng 88*6cc51b12SZhihong Peng Address 0x7fc83f465800 is a wild pointer. 89*6cc51b12SZhihong Peng SUMMARY: AddressSanitizer: heap-use-after-free ../examples/helloworld/main.c:48 in main 90