[dpdk-dev] [PATCH v13 2/4] DPDK code adapts to ASan
David Marchand
david.marchand at redhat.com
Fri Oct 29 11:23:27 CEST 2021
On Wed, Oct 20, 2021 at 9:47 AM <zhihongx.peng at intel.com> wrote:
>
> From: Zhihong Peng <zhihongx.peng at intel.com>
>
> DPDK ASan functionality is currently only supported on Linux x86_64.
> If want to support on other platforms, need to define ASAN_SHADOW_OFFSET
> value according to google ASan document, and configure meson file
> (config/meson.build).
>
> Signed-off-by: Xueqin Lin <xueqin.lin at intel.com>
> Signed-off-by: Zhihong Peng <zhihongx.peng at intel.com>
> Acked-by: Anatoly Burakov <anatoly.burakov at intel.com>
Suggests following title/commitlog:
"""
mem: instrument allocator for ASan
This patch adds necessary hooks in the memory allocator for ASan.
This feature is currently available in DPDK only on Linux x86_64.
If other OS/architectures want to support it, ASAN_SHADOW_OFFSET must be
defined and RTE_MALLOC_ASAN must be set accordingly in meson.
"""
> ---
> v7: Split doc and code into two.
> v8: No change.
> v9: Modify the definition of RTE_MALLOC_ASAN.
> v10:Modify the definition of RTE_MALLOC_ASAN.
> v11:No change.
> v12:No change.
> v13:Modify the document.
> ---
> config/meson.build | 4 +
> doc/guides/prog_guide/asan.rst | 58 +++++++++-
> lib/eal/common/malloc_elem.c | 26 ++++-
> lib/eal/common/malloc_elem.h | 194 ++++++++++++++++++++++++++++++++-
> lib/eal/common/malloc_heap.c | 12 ++
> lib/eal/common/rte_malloc.c | 9 +-
> 6 files changed, 296 insertions(+), 7 deletions(-)
>
> diff --git a/config/meson.build b/config/meson.build
> index f02b0e9c6d..bf751583bd 100644
> --- a/config/meson.build
> +++ b/config/meson.build
> @@ -425,6 +425,10 @@ if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address
> add_project_link_arguments('-lasan', language: 'c')
> dpdk_extra_ldflags += '-lasan'
> endif
> +
> + if is_linux and arch_subdir == 'x86'
Missing a check on "and dpdk_conf.get('RTE_ARCH_64')" for i386 build.
> + dpdk_conf.set10('RTE_MALLOC_ASAN', true)
> + endif
> endif
>
> if get_option('default_library') == 'both'
> diff --git a/doc/guides/prog_guide/asan.rst b/doc/guides/prog_guide/asan.rst
> index 6888fc9a87..02591ca68a 100644
> --- a/doc/guides/prog_guide/asan.rst
> +++ b/doc/guides/prog_guide/asan.rst
> @@ -13,6 +13,58 @@ printing out detailed debug information whenever an error is detected.
>
> AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+).
I'll move the examples after the explanations on how to enable ASan.
>
> +DPDK ASan functionality is currently only supported on Linux x86_64.
> +If want to support on other platforms, need to define ASAN_SHADOW_OFFSET
> +value according to google ASan document, and configure meson file
> +(config/meson.build).
DPDK ASan functionnality is vague.
Suggests rewording:
"""
ASan is aware of DPDK memory allocations, thanks to added instrumentation.
This is only enabled on x86_64 at the moment.
Other architectures may have to define ASAN_SHADOW_OFFSET.
"""
> +
> +Example heap-buffer-overflow error
> +----------------------------------
> +
> +Add below unit test code in examples/helloworld/main.c::
> +
> + Add code to helloworld:
> + char *p = rte_zmalloc(NULL, 9, 0);
> + if (!p) {
> + printf("rte_zmalloc error.");
> + return -1;
> + }
> + p[9] = 'a';
> +
> +Above 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::
> +
> + ==369953==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7fb17f465809 at pc 0x5652e6707b84 bp 0x7ffea70eea20 sp 0x7ffea70eea10 WRITE of size 1 at 0x7fb17f465809 thread T0
> + #0 0x5652e6707b83 in main ../examples/helloworld/main.c:47
> + #1 0x7fb94953c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
> + #2 0x5652e67079bd in _start (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-helloworld+0x8329bd)
> +
> + Address 0x7fb17f465809 is a wild pointer.
> + SUMMARY: AddressSanitizer: heap-buffer-overflow ../examples/helloworld/main.c:47 in main
I'll move the note update here:
"""
Note::
- Some of the features of ASan (for example, 'Display memory
application location, currently
displayed as a wild pointer') are not currently supported with
DPDK allocations.
"""
> +
> +Example use-after-free error
> +----------------------------
> +
> +Add below unit test code in examples/helloworld/main.c::
> +
> + Add code to helloworld:
> + char *p = rte_zmalloc(NULL, 9, 0);
> + if (!p) {
> + printf("rte_zmalloc error.");
> + return -1;
> + }
> + rte_free(p);
> + *p = 'a';
> +
> +Above 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::
> +
> + ==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
> + #0 0x564308a39b88 in main ../examples/helloworld/main.c:48
> + #1 0x7fd0079c60b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
> + #2 0x564308a399bd in _start (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-helloworld+0x8329bd)
> +
> + Address 0x7fc83f465800 is a wild pointer.
> + SUMMARY: AddressSanitizer: heap-use-after-free ../examples/helloworld/main.c:48 in main
> +
> Add following meson build commands to enable ASan in the meson build system:
>
> * gcc::
> @@ -25,6 +77,8 @@ Add following meson build commands to enable ASan in the meson build system:
>
> .. Note::
>
> - a) If compile with gcc in centos, libasan needs to be installed separately.
> - b) If the program is tested using cmdline, you may need to execute the
> + a) Some of the features of ASan (for example, 'Display memory application location, currently
> + displayed as a wild pointer') are not currently supported by DPDK's implementation.
> + b) If compile with gcc in centos, libasan needs to be installed separately.
> + c) If the program is tested using cmdline, you may need to execute the
> "stty echo" command when an error occurs.
--
David Marchand
More information about the dev
mailing list