Enable AddressSanitizer feature on DPDK
PATCH 1/1 Enable AddressSanitizer feature on DPDK
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.
--- app/test/meson.build | 2 + app/test/test_asan_heap_buffer_overflow.c | 30 ++++ app/test/test_asan_use_after_free.c | 31 ++++ doc/guides/prog_guide/asan.rst | 130 ++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 +++- lib/eal/common/malloc_elem.h | 172 +++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/meson.build | 4 + lib/eal/common/rte_malloc.c | 9 +- lib/pipeline/rte_swx_pipeline.c | 4 +- 11 files changed, 416 insertions(+), 5 deletions(-) create mode 100644 app/test/test_asan_heap_buffer_overflow.c create mode 100644 app/test/test_asan_use_after_free.c create mode 100644 doc/guides/prog_guide/asan.rst
app/test/meson.build +2 -0
@@ -150,6 +150,8 @@ test_sources = files( 'test_trace_perf.c', 'test_version.c', 'virtual_pmd.c', + 'test_asan_heap_buffer_overflow.c', + 'test_asan_use_after_free.c', ) test_deps = [
app/test/test_asan_heap_buffer_overflow.c new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) <2021> Intel Corporation +*/ + +#include <stdint.h> +#include <stdio.h> +#include <stdarg.h> +#include <errno.h> +#include <string.h> +#include <rte_per_lcore.h> +#include <rte_errno.h> +#include <rte_string_fns.h> +#include <rte_malloc.h> + +#include "test.h" + +static int +asan_heap_buffer_overflow (void ) +{ +char *p =rte_zmalloc (NULL ,9 ,0 ); +if (!p) { +printf ("rte_zmalloc error." ); +return -1 ; + } + p[9 ] ='a' ; + +return 0 ; +} + +REGISTER_TEST_COMMAND (asan_heap_buffer_overflow_autotest, asan_heap_buffer_overflow);
app/test/test_asan_use_after_free.c new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) <2021> Intel Corporation +*/ + +#include <stdint.h> +#include <stdio.h> +#include <stdarg.h> +#include <errno.h> +#include <string.h> +#include <rte_per_lcore.h> +#include <rte_errno.h> +#include <rte_string_fns.h> +#include <rte_malloc.h> + +#include "test.h" + +static int +asan_use_after_free (void ) +{ +char *p =rte_zmalloc (NULL ,9 ,0 ); +if (!p) { +printf ("rte_zmalloc error." ); +return -1 ; + } +rte_free (p); + *p ='a' ; + +return 0 ; +} + +REGISTER_TEST_COMMAND (asan_use_after_free_autotest, asan_use_after_free);
doc/guides/prog_guide/asan.rst new file +130 -0
130 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +171 -1
190 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/meson.build +4 -0
@@ -5,6 +5,10 @@ includes += include_directories('.') cflags += [ '-DABI_VERSION="@0@"'.format(abi_version) ] +if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif + if is_windows sources += files( 'eal_common_bus.c',
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *
@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
lib/eal/common/meson.build includes += include_directories('.') · line 11
Quoted 5 lines · On Fri, 10 Sep 2021 02:01:47 +0000 [email protected] wrote:
lib/eal/common/meson.build +4
-0+if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif +
This looks great, but can we make it just do-the-right-thing and get rid of the nerd knobs (i.e no meson configure).
The address sanitizer already has a way to detect if enabled.
GCC uses:__SANITIZE_ADDRESS__
Clang uses:
#if defined(__has_feature)
# if __has_feature(address_sanitizer)
Quoted 23 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, September 10, 2021 10:48 AM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [PATCH] Enable AddressSanitizer feature on DPDKlib/eal/common/meson.build includes += include_directories('.') · line 11
Quoted 5 lines · On Fri, 10 Sep 2021 02:01:47 +0000 [email protected] wrote:
lib/eal/common/meson.build +4
-0+if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif +This looks great, but can we make it just do-the-right-thing and get rid of the nerd knobs (i.e no meson configure).
Quoted 8 lines
The address sanitizer already has a way to detect if enabled.
GCC uses:
__SANITIZE_ADDRESS__Clang uses:
#ifdefined(__has_feature)
# if__has_feature(address_sanitizer)
Tried this method you said. It can run successfully. Because gcc and clang have different Methods for determining whether to turn on the asan function, so if you judge the two methods in the code, it feels not simple to judge in meson.
Quoted 26 lines · ZhihongX
Quoted 26 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, September 10, 2021 10:48 AM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [PATCH] Enable AddressSanitizer feature on DPDKlib/eal/common/meson.build includes += include_directories('.') · line 11
Quoted 5 lines · On Fri, 10 Sep 2021 02:01:47 +0000 [email protected] wrote:
lib/eal/common/meson.build +4
-0+if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif +This looks great, but can we make it just do-the-right-thing and get rid of the nerd knobs (i.e no meson configure).
The address sanitizer already has a way to detect if enabled.
GCC uses:
__SANITIZE_ADDRESS__
Quoted 7 lines
Quoted 3 lines
Clang uses:
#ifdefined(__has_feature)
# if__has_feature(address_sanitizer)Tried this method you said. It can run successfully. Because gcc and clang have different Methods for determining whether to turn on the asan function, so if you judge the two methods in the code, it feels not simple to judge in meson.
There is already compiler specific #ifdef's why not do this contained in one header file?
The point is DPDK is trying to get away from having configuration settings if at all possible. Configuration creates dependency nightmares and also leaves many code paths as never tested.
Quoted 16 lines · Stephen Hemminger
Quoted 13 lines · ZhihongX
Quoted 13 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, September 10, 2021 10:48 AM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [PATCH] Enable AddressSanitizer feature on DPDKQuoted 1 line · On Fri, 10 Sep 2021 02:01:47 +0000 [email protected] wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
lib/eal/common/meson.build includes += include_directories('.') · line 11
Quoted 4 lines
lib/eal/common/meson.build +4
-0+if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif +This looks great, but can we make it just do-the-right-thing and get rid of the nerd knobs (i.e no meson configure).
There are no new meson options being added here. Turning on/off address sanitizing is a built-in meson option that is there already.
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
The address sanitizer already has a way to detect if enabled.
GCC uses:
__SANITIZE_ADDRESS__Quoted 7 lines
Quoted 3 lines
Clang uses:
#ifdefined(__has_feature)
# if__has_feature(address_sanitizer)Tried this method you said. It can run successfully. Because gcc and clang have different Methods for determining whether to turn on the asan function, so if you judge the two methods in the code, it feels not simple to judge in meson.
Quoted 5 lines
There is already compiler specific #ifdef's why not do this contained in one header file?
The point is DPDK is trying to get away from having configuration settings if at all possible. Configuration creates dependency nightmares and also leaves many code paths as never tested.
Not sure I follow your point here. We need some macro to easily tell if we are running with address sanitization enabled or not, so as to avoid having the multi-compiler detection rules all over the place. The only question is where it's better to have this in a header file or a meson.build file. Given your objection and the fact that the meson.build code above looks a little awkward, I'd suggest putting the conditional checks in malloc_elem.h.
Is something like the following what you had in mind?
#ifdef __SANITIZE_ADDRESS__ #define RTE_MALLOC_ASAN #elif defined(__has_feature) && __has_feature(address_sanitizer) #define RTE_MALLOC_ASAN #endif
/Bruce
Quoted 48 lines · On Mon, 13 Sep 2021 16:22:13 +0100 Bruce Richardson…
Quoted 16 lines · Stephen Hemminger
Quoted 13 lines · ZhihongX
Quoted 13 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, September 10, 2021 10:48 AM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [PATCH] Enable AddressSanitizer feature on DPDKQuoted 1 line · On Fri, 10 Sep 2021 02:01:47 +0000 [email protected] wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
lib/eal/common/meson.build includes += include_directories('.') · line 11
Quoted 4 lines
lib/eal/common/meson.build +4
-0+if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif +This looks great, but can we make it just do-the-right-thing and get rid of the nerd knobs (i.e no meson configure).
There are no new meson options being added here. Turning on/off address sanitizing is a built-in meson option that is there already.
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
The address sanitizer already has a way to detect if enabled.
GCC uses:
__SANITIZE_ADDRESS__Quoted 7 lines
Quoted 3 lines
Clang uses:
#ifdefined(__has_feature)
# if__has_feature(address_sanitizer)Tried this method you said. It can run successfully. Because gcc and clang have different Methods for determining whether to turn on the asan function, so if you judge the two methods in the code, it feels not simple to judge in meson.
Quoted 5 lines
There is already compiler specific #ifdef's why not do this contained in one header file?
The point is DPDK is trying to get away from having configuration settings if at all possible. Configuration creates dependency nightmares and also leaves many code paths as never tested.
Quoted 6 lines
Not sure I follow your point here. We need some macro to easily tell if we are running with address sanitization enabled or not, so as to avoid having the multi-compiler detection rules all over the place. The only question is where it's better to have this in a header file or a meson.build file. Given your objection and the fact that the meson.build code above looks a little awkward, I'd suggest putting the conditional checks in
malloc_elem.h.
NVM working of meson as commn base seems like good option.
Quoted 67 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Monday, September 13, 2021 11:46 PM
To: Richardson, Bruce <[email protected]>
Cc: Peng, ZhihongX <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] Enable AddressSanitizer feature on DPDKQuoted 26 lines · On Mon, 13 Sep 2021 16:22:13 +0100 Bruce Richardson…
Quoted 16 lines · Stephen Hemminger
Quoted 13 lines · ZhihongX
Quoted 13 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, September 10, 2021 10:48 AM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [PATCH] Enable AddressSanitizer feature on DPDKQuoted 1 line · On Fri, 10 Sep 2021 02:01:47 +0000 [email protected] wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
lib/eal/common/meson.build includes += include_directories('.') · line 11
Quoted 4 lines
lib/eal/common/meson.build +4
-0+if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif +This looks great, but can we make it just do-the-right-thing and get rid of the nerd knobs (i.e no meson configure).
Quoted 15 lines
There are no new meson options being added here. Turning on/off address sanitizing is a built-in meson option that is there already.
Quoted 12 lines
Quoted 12 lines
Quoted 8 lines
The address sanitizer already has a way to detect if enabled.
GCC uses:
__SANITIZE_ADDRESS__Clang uses:
#ifdefined(__has_feature)
# if__has_feature(address_sanitizer)Tried this method you said. It can run successfully. Because gcc and clang have different Methods for determining whether to turn on the asan function, so if you judge the two methods in the code, it
feels not simple to judge in meson.
Quoted 2 lines
Quoted 2 lines
There is already compiler specific #ifdef's why not do this contained in
one header file?
Quoted 9 lines
Quoted 4 lines
The point is DPDK is trying to get away from having configuration settings if at all possible. Configuration creates dependency nightmares and also leaves many code paths as never tested.
Not sure I follow your point here. We need some macro to easily tell if we are running with address sanitization enabled or not, so as to avoid having the multi-compiler detection rules all over the place. The only question is where it's better to have this in a header file or a
Quoted 6 lines
meson.build file.
Quoted 2 lines
Given your objection and the fact that the meson.build code above looks a little awkward, I'd suggest putting the conditional checks in
malloc_elem.h.NVM working of meson as commn base seems like good option.
+1, good option for the tool enable, thanks a lot for your guide and positive feedback.
We will update them in V2 and send V2 soon.
High appreciate that you can ACK this feature patch if no other objection, thanks.
Quoted 68 lines
-----Original Message-----
From: Richardson, Bruce <[email protected]>
Sent: Monday, September 13, 2021 11:22 PM
To: Stephen Hemminger <[email protected]>
Cc: Peng, ZhihongX <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] Enable AddressSanitizer feature on DPDKQuoted 15 lines · Stephen Hemminger
Quoted 12 lines · ZhihongX
Quoted 12 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, September 10, 2021 10:48 AM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [PATCH] Enable AddressSanitizer feature on DPDKQuoted 1 line · On Fri, 10 Sep 2021 02:01:47 +0000 [email protected] wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
lib/eal/common/meson.build includes += include_directories('.') · line 11
Quoted 4 lines
lib/eal/common/meson.build +4
-0+if get_option('b_sanitize').startswith('address') + cflags += '-DRTE_MALLOC_ASAN' +endif +This looks great, but can we make it just do-the-right-thing and get rid of the nerd knobs (i.e no meson configure).
There are no new meson options being added here. Turning on/off address sanitizing is a built-in meson option that is there already.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
The address sanitizer already has a way to detect if enabled.
GCC uses:
__SANITIZE_ADDRESS__Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Clang uses:
#ifdefined(__has_feature)
# if__has_feature(address_sanitizer)Tried this method you said. It can run successfully. Because gcc and clang have different Methods for determining whether to turn on the asan function, so if you judge the two methods in the code, it feels not
simple to judge in meson.
Quoted 2 lines
There is already compiler specific #ifdef's why not do this contained in one
header file?
Quoted 4 lines
The point is DPDK is trying to get away from having configuration settings if at all possible. Configuration creates dependency nightmares and also leaves many code paths as never tested.
Not sure I follow your point here. We need some macro to easily tell if we are running with address sanitization enabled or not, so as to avoid having the multi-compiler detection rules all over the place. The only question is where it's better to have this in a header file or a meson.build file. Given your objection and the fact that the meson.build code above looks a little awkward, I'd suggest putting the conditional checks in
malloc_elem.h.Is something like the following what you had in mind?
Quoted 7 lines
#ifdef __SANITIZE_ADDRESS__ #define RTE_MALLOC_ASAN #elif defined(__has_feature) && __has_feature(address_sanitizer) #define RTE_MALLOC_ASAN #endif/Bruce
Agree, we will update it in V2 version and send it soon.
Thanks Bruce for your review and guide. If no other objections, need your help to ACK this feature patch.
Quoted 25 lines
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......
Quoted 7 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.
On initial inspection, it appears ASAN functionality doesn't work with DPDK on PPC architecture. I tested the patch with several compiler versions (gcc 8.3.1 from RHEL 8.3 through gcc 11.2.1 from the IBM Advanced Toolchain 15.0) and observed the following error when running testpmd with ASAN enabled:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==49246==ERROR: AddressSanitizer: SEGV on unknown address 0x0000a0077bd0 (pc 0x000010b4eca4 bp 0x7fffffffe150 sp 0x7fffffffe150 T0) ==49246==The signal is caused by a UNKNOWN memory access.
#0 0x10b4eca4 in asan_set_shadow ../lib/eal/common/malloc_elem.h:120
#1 0x10b4ed68 in asan_set_zone ../lib/eal/common/malloc_elem.h:135
#2 0x10b4ee90 in asan_clear_split_alloczone
../lib/eal/common/malloc_elem.h:162
#3 0x10b51f84 in malloc_elem_alloc ../lib/eal/common/malloc_elem.c:477...
Can you incorporate an exception for PPC architecture with this patch while I look into the problem further?
Dave
Quoted 59 lines
-----Original Message-----
From: David Christensen <[email protected]>
Sent: Saturday, September 11, 2021 1:59 AM
To: Peng, ZhihongX <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]
Cc: [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] Enable AddressSanitizer feature on DPDKQuoted 24 lines
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......Quoted 7 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.On initial inspection, it appears ASAN functionality doesn't work with DPDK on PPC architecture. I tested the patch with several compiler versions (gcc 8.3.1 from RHEL 8.3 through gcc 11.2.1 from the IBM Advanced Toolchain 15.0) and observed the following error when running testpmd with ASAN enabled:
AddressSanitizer:DEADLYSIGNAL
==========================================================
=======
==49246==ERROR: AddressSanitizer: SEGV on unknown address0x0000a0077bd0(pc0x000010b4eca4bp0x7fffffffe150sp0x7fffffffe150T0) ==49246==The signal is caused by a UNKNOWN memory access.#0 0x10b4eca4 in asan_set_shadow ../lib/eal/common/malloc_elem.h:120 #1 0x10b4ed68 in asan_set_zone ../lib/eal/common/malloc_elem.h:135 #2 0x10b4ee90 in asan_clear_split_alloczone ../lib/eal/common/malloc_elem.h:162 #3 0x10b51f84 in malloc_elem_alloc ../lib/eal/common/malloc_elem.c:477
Quoted 6 lines
...
Can you incorporate an exception for PPC architecture with this patch while I look into the problem further?
Dave
We do not have a ppc platform, so there is no adaptation. doc/guides/prog_guide/asan.rst has stated that we currently only support Linux x86_64. You can adapt according to the following documents, the main work is to modify the base address according to the platform. Documents:
https://github.com/google/sanitizers/wiki/AddressSanitizer
https://github.com/llvm/llvm-project/tree/main/compiler-rt
Quoted 29 lines
Quoted 29 lines
Quoted 7 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.On initial inspection, it appears ASAN functionality doesn't work with DPDK on PPC architecture. I tested the patch with several compiler versions (gcc 8.3.1 from RHEL 8.3 through gcc 11.2.1 from the IBM Advanced Toolchain 15.0) and observed the following error when running testpmd with ASAN enabled:
AddressSanitizer:DEADLYSIGNAL
==========================================================
=======
==49246==ERROR: AddressSanitizer: SEGV on unknown address0x0000a0077bd0(pc0x000010b4eca4bp0x7fffffffe150sp0x7fffffffe150T0) ==49246==The signal is caused by a UNKNOWN memory access.#0 0x10b4eca4 in asan_set_shadow ../lib/eal/common/malloc_elem.h:120 #1 0x10b4ed68 in asan_set_zone ../lib/eal/common/malloc_elem.h:135 #2 0x10b4ee90 in asan_clear_split_alloczone ../lib/eal/common/malloc_elem.h:162 #3 0x10b51f84 in malloc_elem_alloc ../lib/eal/common/malloc_elem.c:477...
Can you incorporate an exception for PPC architecture with this patch while I look into the problem further?
Quoted 8 lines
Quoted 1 line
Dave
We do not have a ppc platform, so there is no adaptation.
doc/guides/prog_guide/asan.rsthas stated that we currently only support Linuxx86_64. You can adapt according to the following documents, the main work is to modify the base address according to the platform. Documents:
https://github.com/google/sanitizers/wiki/AddressSanitizer
https://github.com/llvm/llvm-project/tree/main/compiler-rt
Understand you don't have such a platform. I looked into it and suggest the following change in lib/eal/common/malloc_elem.h:
#define ASAN_SHADOW_GRAIN_SIZE 8
#define ASAN_SHADOW_SCALE 3
#ifdef RTE_ARCH_PPC_64
#define ASAN_SHADOW_OFFSET 0x020000000000
#else
#define ASAN_SHADOW_OFFSET 0x00007fff8000
#endif
#define ASAN_MEM_FREE_FLAG 0xfd
#define ASAN_MEM_REDZONE_FLAG 0xfa
#define ASAN_MEM_TO_SHADOW(mem) (((mem) >> ASAN_SHADOW_SCALE) + ASAN_SHADOW_OFFSET)
This resolves the segmentation error I receive.
Dave
P.S. FYI, here's the ASAN mapping I observe on x86 vs. POWER:
x86 results:
-----------------------------------------------------------------------------------
ASAN_OPTIONS=verbosity=1 ./a.out ==141271==AddressSanitizer: libc interceptors initialized
|| `[0x1000_7fff_8000, 0x7fff_ffff_ffff]` || HighMem || || `[0x0200_8fff_7000, 0x1000_7fff_7fff]` || HighShadow || || `[0x0000_8fff_7000, 0x0200_8fff_6fff]` || ShadowGap || || `[0x0000_7fff_8000, 0x0000_8fff_6fff]` || LowShadow || || `[0x0000_0000_0000, 0x0000_7fff_7fff]` || LowMem ||
MemToShadow(shadow): 0x0000_8fff_7000 0x0000_91ff_6dff 0x0040_91ff_6e00 0x0200_8fff_6fff redzone=16
max_redzone=2048
quarantine_size_mb=256M
thread_local_quarantine_size_kb=1024K
malloc_context_size=30SHADOW_SCALE: 3SHADOW_GRANULARITY: 8SHADOW_OFFSET: 0x7fff_8000
POWER results:
-----------------------------------------------------------------------------------
ASAN_OPTIONS=verbosity=1 ./a.out
...
==93284==AddressSanitizer: libc interceptors initialized
|| `[0x1200_0000_0000, 0x7fff_ffff_ffff]` || HighMem || || `[0x0440_0000_0000, 0x11ff_ffff_ffff]` || HighShadow || || `[0x0240_0000_0000, 0x043f_ffff_ffff]` || ShadowGap || || `[0x0200_0000_0000, 0x023f_ffff_ffff]` || LowShadow || || `[0x0000_0000_0000, 0x01ff_ffff_ffff]` || LowMem ||
MemToShadow(shadow): 0x0240_0000_0000 0x0247_ffff_ffff 0x0288_0000_0000 0x043f_ffff_ffff redzone=16
max_redzone=2048
quarantine_size_mb=256M
thread_local_quarantine_size_kb=1024K
malloc_context_size=30SHADOW_SCALE: 3SHADOW_GRANULARITY: 8SHADOW_OFFSET: 0x200_0000_0000
Quoted 69 lines
-----Original Message-----
From: David Christensen <[email protected]>
Sent: Saturday, September 18, 2021 4:51 AM
To: Peng, ZhihongX <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]
Cc: [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] Enable AddressSanitizer feature on DPDKQuoted 8 lines
Quoted 8 lines
Quoted 7 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.Quoted 5 lines
Quoted 5 lines
On initial inspection, it appears ASAN functionality doesn't work with DPDK on PPC architecture. I tested the patch with several compiler versions (gcc
8.3.1 from RHEL 8.3 through gcc 11.2.1 from the IBM Advanced Toolchain 15.0) and observed the following error when running testpmdwith ASAN enabled:
Quoted 3 lines
Quoted 3 lines
AddressSanitizer:DEADLYSIGNAL
==========================================================
Quoted 5 lines
Quoted 5 lines
=======
==49246==ERROR: AddressSanitizer: SEGV on unknown address0x0000a0077bd0(pc0x000010b4eca4bp0x7fffffffe150sp0x7fffffffe150T0) ==49246==The signal is caused by a UNKNOWN memory access.#0 0x10b4eca4 in
asan_set_shadow../lib/eal/common/malloc_elem.h:120Quoted 1 line
Quoted 1 line
#1 0x10b4ed68 in
asan_set_zone../lib/eal/common/malloc_elem.h:135Quoted 6 lines
Quoted 6 lines
#2 0x10b4ee90 in asan_clear_split_alloczone ../lib/eal/common/malloc_elem.h:162 #3 0x10b51f84 in malloc_elem_alloc
../lib/eal/common/malloc_elem.c:477
...Quoted 8 lines
Quoted 4 lines
Can you incorporate an exception for PPC architecture with this patch while I look into the problem further?
Dave
We do not have a ppc platform, so there is no adaptation.
doc/guides/prog_guide/asan.rsthas stated that we currently only support Linuxx86_64. You can adapt according to the following documents,the main work is to modify the base address according to the platform.
Quoted 3 lines
Documents:
https://github.com/google/sanitizers/wiki/AddressSanitizer
https://github.com/llvm/llvm-project/tree/main/compiler-rtUnderstand you don't have such a platform. I looked into it and suggest the following change in
lib/eal/common/malloc_elem.h:#define
ASAN_SHADOW_GRAIN_SIZE8
#defineASAN_SHADOW_SCALE3
#ifdefRTE_ARCH_PPC_64
#defineASAN_SHADOW_OFFSET0x020000000000#else #defineASAN_SHADOW_OFFSET0x00007fff8000#endif
#defineASAN_MEM_FREE_FLAG0xfd
#defineASAN_MEM_REDZONE_FLAG0xfa
#defineASAN_MEM_TO_SHADOW(mem)(((mem) >>ASAN_SHADOW_SCALE) +ASAN_SHADOW_OFFSET)
Quoted 4 lines
This resolves the segmentation error I receive.
Dave
Great, good information for dpdk asan tool. Because we can't do many tests, so when this patch is merged into the main line, you can submit the ppc architecture patch.
Quoted 33 lines
P.S. FYI, here's the ASAN mapping I observe on x86 vs. POWER:x86 results:
-----------------------------------------------------------------------------------
ASAN_OPTIONS=verbosity=1 ./a.out ==141271==AddressSanitizer: libc interceptors initialized|| `[0x1000_7fff_8000, 0x7fff_ffff_ffff]` || HighMem || || `[0x0200_8fff_7000, 0x1000_7fff_7fff]` || HighShadow || || `[0x0000_8fff_7000, 0x0200_8fff_6fff]` || ShadowGap || || `[0x0000_7fff_8000, 0x0000_8fff_6fff]` || LowShadow || || `[0x0000_0000_0000, 0x0000_7fff_7fff]` || LowMem ||MemToShadow(shadow): 0x0000_8fff_7000 0x0000_91ff_6dff 0x0040_91ff_6e00
0x0200_8fff_6fff
redzone=16
max_redzone=2048
quarantine_size_mb=256M thread_local_quarantine_size_kb=1024K malloc_context_size=30SHADOW_SCALE: 3SHADOW_GRANULARITY: 8SHADOW_OFFSET: 0x7fff_8000POWER results:
-----------------------------------------------------------------------------------
ASAN_OPTIONS=verbosity=1 ./a.out
...
==93284==AddressSanitizer: libc interceptors initialized|| `[0x1200_0000_0000, 0x7fff_ffff_ffff]` || HighMem || || `[0x0440_0000_0000, 0x11ff_ffff_ffff]` || HighShadow || || `[0x0240_0000_0000, 0x043f_ffff_ffff]` || ShadowGap || || `[0x0200_0000_0000, 0x023f_ffff_ffff]` || LowShadow || || `[0x0000_0000_0000, 0x01ff_ffff_ffff]` || LowMem ||
Quoted 11 lines
MemToShadow(shadow): 0x0240_0000_0000 0x0247_ffff_ffff 0x0288_0000_0000
0x043f_ffff_ffff
redzone=16
max_redzone=2048
quarantine_size_mb=256M thread_local_quarantine_size_kb=1024K malloc_context_size=30SHADOW_SCALE: 3SHADOW_GRANULARITY: 8SHADOW_OFFSET: 0x200_0000_0000
Peng, ZhihongX
Quoted 69 lines · ZhihongX
Quoted 69 lines
-----Original Message-----
From: David Christensen <[email protected]>
Sent: Saturday, September 18, 2021 4:51 AM
To: Peng, ZhihongX <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]
Cc: [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] Enable AddressSanitizer feature on DPDKQuoted 8 lines
Quoted 8 lines
Quoted 7 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.Quoted 5 lines
Quoted 5 lines
On initial inspection, it appears ASAN functionality doesn't work with DPDK on PPC architecture. I tested the patch with several compiler versions (gcc
8.3.1 from RHEL 8.3 through gcc 11.2.1 from the IBM Advanced Toolchain 15.0) and observed the following error when running testpmdwith ASAN enabled:
Quoted 3 lines
Quoted 3 lines
AddressSanitizer:DEADLYSIGNAL
==========================================================
Quoted 5 lines
Quoted 5 lines
=======
==49246==ERROR: AddressSanitizer: SEGV on unknown address0x0000a0077bd0(pc0x000010b4eca4bp0x7fffffffe150sp0x7fffffffe150T0) ==49246==The signal is caused by a UNKNOWN memory access.#0 0x10b4eca4 in
asan_set_shadow../lib/eal/common/malloc_elem.h:120Quoted 1 line
Quoted 1 line
#1 0x10b4ed68 in
asan_set_zone../lib/eal/common/malloc_elem.h:135Quoted 6 lines
Quoted 6 lines
#2 0x10b4ee90 in asan_clear_split_alloczone ../lib/eal/common/malloc_elem.h:162 #3 0x10b51f84 in malloc_elem_alloc
../lib/eal/common/malloc_elem.c:477
...Quoted 8 lines
Quoted 4 lines
Can you incorporate an exception for PPC architecture with this patch while I look into the problem further?
Dave
We do not have a ppc platform, so there is no adaptation.
doc/guides/prog_guide/asan.rsthas stated that we currently only support Linuxx86_64. You can adapt according to the following documents,the main work is to modify the base address according to the platform.
Quoted 3 lines
Documents:
https://github.com/google/sanitizers/wiki/AddressSanitizer
https://github.com/llvm/llvm-project/tree/main/compiler-rtUnderstand you don't have such a platform. I looked into it and suggest the following change in
lib/eal/common/malloc_elem.h:#define
ASAN_SHADOW_GRAIN_SIZE8
#defineASAN_SHADOW_SCALE3
#ifdefRTE_ARCH_PPC_64
#defineASAN_SHADOW_OFFSET0x020000000000#else #defineASAN_SHADOW_OFFSET0x00007fff8000#endif
#defineASAN_MEM_FREE_FLAG0xfd
#defineASAN_MEM_REDZONE_FLAG0xfa
#defineASAN_MEM_TO_SHADOW(mem)(((mem) >>ASAN_SHADOW_SCALE) +ASAN_SHADOW_OFFSET)
Quoted 8 lines
Quoted 4 lines
This resolves the segmentation error I receive.
Dave
Great, good information for dpdk asan tool. Because we can't do many tests, so when this patch is merged into the main line, you can submit the ppc architecture patch.
If your argument is that this is x86 only then please ensure it can't be enabled on non-x86 platforms such as ARM and PPC. I can then easily submit a follow-on patch to enable for PPC.
As the patch currently stands it enables ASAN on a non-tested platform and provides an unexpected error for some users when it can easily be avoided. I'd advise not accepting the patch as currently presented.
Dave
Quoted 32 lines · David Christensen
Quoted 23 lines
Quoted 23 lines
Quoted 3 lines
We do not have a ppc platform, so there is no adaptation.
doc/guides/prog_guide/asan.rsthas stated that we currently only support Linuxx86_64. You can adapt according to the following documents,the main work is to modify the base address according to the platform.
Quoted 3 lines
Documents:
https://github.com/google/sanitizers/wiki/AddressSanitizer
https://github.com/llvm/llvm-project/tree/main/compiler-rtUnderstand you don't have such a platform. I looked into it and suggest the following change in
lib/eal/common/malloc_elem.h:#define
ASAN_SHADOW_GRAIN_SIZE8
#defineASAN_SHADOW_SCALE3
#ifdefRTE_ARCH_PPC_64
#defineASAN_SHADOW_OFFSET0x020000000000#else #defineASAN_SHADOW_OFFSET0x00007fff8000#endif
#defineASAN_MEM_FREE_FLAG0xfd
#defineASAN_MEM_REDZONE_FLAG0xfa
#defineASAN_MEM_TO_SHADOW(mem)(((mem) >>ASAN_SHADOW_SCALE) +ASAN_SHADOW_OFFSET)Quoted 8 lines
Quoted 4 lines
This resolves the segmentation error I receive.
Dave
Great, good information for dpdk asan tool. Because we can't do many tests, so when this patch is merged into the main line, you can submit the ppc architecture patch.
Quoted 7 lines
If your argument is that this is x86 only then please ensure it can't be enabled on non-x86 platforms such as ARM and PPC. I can then easily submit a follow-on patch to enable for PPC.
As the patch currently stands it enables ASAN on a non-tested platform and provides an unexpected error for some users when it can easily be avoided. I'd advise not accepting the patch as currently presented.
Please make sure only x86_64 gets this code enabled. I'll wait for a new revision, thanks.
Signature · 1 line
David Marchand
Quoted 39 lines · David Marchand
Quoted 36 lines · David Christensen
Quoted 23 lines
Quoted 23 lines
Quoted 3 lines
We do not have a ppc platform, so there is no adaptation.
doc/guides/prog_guide/asan.rsthas stated that we currently only support Linuxx86_64. You can adapt according to the following documents,the main work is to modify the base address according to the platform.
Quoted 3 lines
Documents:
https://github.com/google/sanitizers/wiki/AddressSanitizer
https://github.com/llvm/llvm-project/tree/main/compiler-rtUnderstand you don't have such a platform. I looked into it and suggest the following change in
lib/eal/common/malloc_elem.h:#define
ASAN_SHADOW_GRAIN_SIZE8
#defineASAN_SHADOW_SCALE3
#ifdefRTE_ARCH_PPC_64
#defineASAN_SHADOW_OFFSET0x020000000000#else #defineASAN_SHADOW_OFFSET0x00007fff8000#endif
#defineASAN_MEM_FREE_FLAG0xfd
#defineASAN_MEM_REDZONE_FLAG0xfa
#defineASAN_MEM_TO_SHADOW(mem)(((mem) >>ASAN_SHADOW_SCALE) +ASAN_SHADOW_OFFSET)Quoted 8 lines
Quoted 4 lines
This resolves the segmentation error I receive.
Dave
Great, good information for dpdk asan tool. Because we can't do many tests, so when this patch is merged into the main line, you can submit the ppc architecture patch.
If your argument is that this is x86 only then please ensure it can't be enabled on non-x86 platforms such as ARM and PPC. I can then easily submit a follow-on patch to enable for PPC.
Quoted 5 lines
Quoted 3 lines
As the patch currently stands it enables ASAN on a non-tested platform and provides an unexpected error for some users when it can easily be avoided. I'd advise not accepting the patch as currently presented.
Please make sure only
x86_64gets this code enabled.
I think, we need to opt out only the PPC. According
https://developer.android.com/ndk/guides/asan, Arm64 and x86 are supported.
I suggest resping by opting out PPC, I can test the next version and confirm the behavior on arm64 and give Ack.
Quoted 6 lines
I'll wait for a new revision, thanks.
--
David Marchand
Quoted 49 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Tuesday, September 21, 2021 4:30 PM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; David Christensen <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] Enable AddressSanitizer feature on DPDKQuoted 37 lines · David Christensen
Quoted 24 lines
Quoted 24 lines
Quoted 4 lines
We do not have a ppc platform, so there is no adaptation.
doc/guides/prog_guide/asan.rsthas stated that we currently only support Linuxx86_64. You can adapt according to the following documents,the main work is to modify the base address according to the platform.
Quoted 3 lines
Documents:
https://github.com/google/sanitizers/wiki/AddressSanitizer
https://github.com/llvm/llvm-project/tree/main/compiler-rtUnderstand you don't have such a platform. I looked into it and suggest the following change in
lib/eal/common/malloc_elem.h:#define
ASAN_SHADOW_GRAIN_SIZE8
#defineASAN_SHADOW_SCALE3
#ifdefRTE_ARCH_PPC_64
#defineASAN_SHADOW_OFFSET0x020000000000#else #defineASAN_SHADOW_OFFSET0x00007fff8000#endif
#defineASAN_MEM_FREE_FLAG0xfd
#defineASAN_MEM_REDZONE_FLAG0xfa
#defineASAN_MEM_TO_SHADOW(mem)(((mem) >>ASAN_SHADOW_SCALE) +ASAN_SHADOW_OFFSET)Quoted 8 lines
Quoted 4 lines
This resolves the segmentation error I receive.
Dave
Great, good information for dpdk asan tool. Because we can't do many tests, so when this patch is merged into the main line, you can submit the ppc architecture patch.
If your argument is that this is x86 only then please ensure it can't be enabled on non-x86 platforms such as ARM and PPC. I can then easily submit a follow-on patch to enable for PPC.
Quoted 6 lines
Quoted 3 lines
As the patch currently stands it enables ASAN on a non-tested platform and provides an unexpected error for some users when it can easily be avoided. I'd advise not accepting the patch as currently presented.
Please make sure only
x86_64gets this code enabled. I'll wait for a new revision, thanks.
V4 will enable platform macro switch.
Quoted 3 lines
--
David Marchand
PATCH v2 1/1 Enable AddressSanitizer feature on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASAN function currently only supports on Linux x86_64.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.
--- app/test/meson.build | 2 + app/test/test_asan_heap_buffer_overflow.c | 32 ++++ app/test/test_asan_use_after_free.c | 33 ++++ doc/guides/prog_guide/asan.rst | 130 +++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 ++- lib/eal/common/malloc_elem.h | 184 +++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- lib/pipeline/rte_swx_pipeline.c | 4 +- 10 files changed, 426 insertions(+), 7 deletions(-) create mode 100644 app/test/test_asan_heap_buffer_overflow.c create mode 100644 app/test/test_asan_use_after_free.c create mode 100644 doc/guides/prog_guide/asan.rst
app/test/meson.build +2 -0
@@ -150,6 +150,8 @@ test_sources = files( 'test_trace_perf.c', 'test_version.c', 'virtual_pmd.c', + 'test_asan_heap_buffer_overflow.c', + 'test_asan_use_after_free.c', ) test_deps = [
app/test/test_asan_heap_buffer_overflow.c new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) <2021> Intel Corporation +*/ + +#include <stdint.h> +#include <stdio.h> +#include <stdarg.h> +#include <errno.h> +#include <string.h> +#include <rte_per_lcore.h> +#include <rte_errno.h> +#include <rte_string_fns.h> +#include <rte_malloc.h> + +#include "test.h" + +static int +asan_heap_buffer_overflow (void ) +{ +uint32_t malloc_size =9 ; + +char *p =rte_zmalloc (NULL , malloc_size,0 ); +if (!p) { +printf ("rte_zmalloc error." ); +return -1 ; + } + p[9 ] ='a' ; + +return 0 ; +} + +REGISTER_TEST_COMMAND (asan_heap_buffer_overflow_autotest, asan_heap_buffer_overflow);
app/test/test_asan_use_after_free.c new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) <2021> Intel Corporation +*/ + +#include <stdint.h> +#include <stdio.h> +#include <stdarg.h> +#include <errno.h> +#include <string.h> +#include <rte_per_lcore.h> +#include <rte_errno.h> +#include <rte_string_fns.h> +#include <rte_malloc.h> + +#include "test.h" + +static int +asan_use_after_free (void ) +{ +uint32_t malloc_size =9 ; + +char *p =rte_zmalloc (NULL , malloc_size,0 ); +if (!p) { +printf ("rte_zmalloc error." ); +return -1 ; + } +rte_free (p); + *p ='a' ; + +return 0 ; +} + +REGISTER_TEST_COMMAND (asan_use_after_free_autotest, asan_use_after_free);
doc/guides/prog_guide/asan.rst new file +130 -0
130 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +181 -3
213 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *
@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
Quoted 20 lines · On Thu, Sep 16, 2021 at 3:47 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASAN function currently only supports on Linuxx86_64.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......
Quoted 7 lines
Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......
Unfortunately, this won't build with some (too smart) compilers. Can you look at the CI report?
Thanks.
Quoted 10 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>
Signature · 1 line
David Marchand
Quoted 39 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Friday, September 17, 2021 4:23 PM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v2] Enable AddressSanitizer feature on DPDKQuoted 19 lines · On Thu, Sep 16, 2021 at 3:47 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASAN function currently only supports on Linuxx86_64.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Quoted 7 lines
Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......
Quoted 3 lines
Unfortunately, this won't build with some (too smart) compilers. Can you look at the CI report?
Thanks.
I am trying to solve this issue.If there is no good solution, this test case will be removed. Thanks.
Quoted 17 lines
Quoted 10 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.Quoted 2 lines
Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>--
David Marchand
Quoted 4 lines · ZhihongX
Quoted 4 lines
Quoted 4 lines
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASAN function currently only supports on Linuxx86_64.
We are using ASAN on Arm64 without problems, don't know why the patch shouldn't work there as well.
Quoted 13 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday, September 17, 2021 10:59 PM
To: Peng, ZhihongX <[email protected]>
Cc: David Marchand <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v2] Enable AddressSanitizer feature on DPDKOn Fri, 17 Sep 2021 09:12:07 +0000
"Peng, ZhihongX" <[email protected]> wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorit
hm), enable heap-buffer-overflow and use-after-free functions on dpdk.
DPDK ASAN function currently only supports on Linuxx86_64.We are using ASAN on Arm64 without problems, don't know why the patch shouldn't work there as well.
Google asan cannot support DPDK heap memory detection. This patch adapts to google asan so that it can check DPDK heap memory bugs. For each platform, it is necessary to adapt the base address and do a lot of testing. We don't have an arm platform here, so we didn't adapt it.
Peng, ZhihongX
PATCH v3 1/1 Enable AddressSanitizer feature on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASAN function currently only supports on Linux x86_64.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": Display code information when coredump occurs in the program.
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.
--- doc/guides/prog_guide/asan.rst | 130 ++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 184 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 +++ lib/eal/common/rte_malloc.c | 9 +- lib/pipeline/rte_swx_pipeline.c | 4 +- 7 files changed, 359 insertions(+), 7 deletions(-) create mode 100644 doc/guides/prog_guide/asan.rst
doc/guides/prog_guide/asan.rst new file +130 -0
@@ -0,0 +1,130 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(Asan) +========================================================== + +AddressSanitizer (ASan) is a google memory error detect +standard tool. It could help to detect use-after-free and +{heap,stack,global}-buffer overflow bugs in C/C++ programs, +print detailed error information when error happens, large +improve debug efficiency. + +By referring to its implementation algorithm +(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm), +enabled heap-buffer-overflow and use-after-free functions on dpdk. +DPDK ASAN function currently only supports on Linux x86_64. + +AddressSanitizer is a part of LLVM(3.1+)and GCC(4.8+). + +Example heap-buffer-overflow error +---------------------------------- + +Following error was reported when Asan was enabled:: + + Applied 9 bytes of memory, but accessed the 10th byte of memory, + so heap-buffer-overflow appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0); + if (!p) { + printf("rte_zmalloc error."); + return -1; + } + p[9] = 'a'; + +The error log:: + + ==49433==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7f773fafa249 at pc 0x5556b13bdae4 bp 0x7ffeb4965e40 sp 0x7ffeb4965e30 WRITE of size 1 at 0x7f773fafa249 thread T0 + #0 0x5556b13bdae3 in asan_heap_buffer_overflow ../app/test/test_asan_heap_buffer_overflow.c:25 + #1 0x5556b043e9d4 in cmd_autotest_parsed ../app/test/commands.c:71 + #2 0x5556b1cdd4b0 in cmdline_parse ../lib/cmdline/cmdline_parse.c:290 + #3 0x5556b1cd8987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x5556b1ce477a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x5556b1cd923e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x5556b1cd9769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x5556b045f53b in main ../app/test/test.c:234 + #8 0x7f7f1eba90b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) + #9 0x5556b043e70d in _start (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-test+0x7ce70d) + + Address 0x7f773fafa249 is a wild pointer. + SUMMARY: AddressSanitizer: heap-buffer-overflow ../app/test/test_asan_heap_buffer_overflow.c:25 in asan_heap_buffer_overflow + Shadow bytes around the buggy address: + 0x0fef67f573f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57400: fa fa 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + =>0x0fef67f57440: 00 00 00 00 00 00 fa fa 00[01]fa 00 00 00 00 00 + 0x0fef67f57450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57460: 00 00 00 00 00 00 fa fa 00 00 00 00 00 00 00 00 + 0x0fef67f57470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +Example use-after-free error +---------------------------- + +Following error was reported when Asan was enabled:: + + Applied for 9 bytes of memory, and accessed the first byte after + released, so heap-use-after-free appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0); + if (!p) { + printf("rte_zmalloc error."); + return -1; + } + rte_free(p); + *p = 'a'; + +The error log:: + + ==49478==ERROR: AddressSanitizer: heap-use-after-free on address 0x7fe2ffafa240 at pc 0x56409b084bc8 bp 0x7ffef62c57d0 sp 0x7ffef62c57c0 WRITE of size 1 at 0x7fe2ffafa240 thread T0 + #0 0x56409b084bc7 in asan_use_after_free ../app/test/test_asan_use_after_free.c:26 + #1 0x56409a1059d4 in cmd_autotest_parsed ../app/test/commands.c:71 + #2 0x56409b9a44b0 in cmdline_parse ../lib/cmdline/cmdline_parse.c:290 + #3 0x56409b99f987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x56409b9ab77a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x56409b9a023e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x56409b9a0769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x56409a12653b in main ../app/test/test.c:234 + #8 0x7feafafc20b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) + #9 0x56409a10570d in _start (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-test+0x7ce70d) + + Address 0x7fe2ffafa240 is a wild pointer. + SUMMARY: AddressSanitizer: heap-use-after-free ../app/test/test_asan_use_after_free.c:26 in asan_use_after_free + Shadow bytes around the buggy address: + 0x0ffcdff573f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57400: fa fa 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + =>0x0ffcdff57440: 00 00 00 00 00 00 00 00[fd]fd fd fd fd fd fd fd + 0x0ffcdff57450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57460: 00 00 00 00 00 00 fa fa 00 00 00 00 00 00 00 00 + 0x0ffcdff57470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable Asan in meson build system, use following meson build command: + +Example usage:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) The issue of asan wild pointer is that dpdk asan tool is not fully adapted to google asan. + For example: Address 0x7fe2ffafa240 is a wild pointer. + b) Centos8 needs to install libasan separately. + c) If the program uses cmdline, when a memory bug occurs, need to execute the "stty echo" command.
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +181 -3
213 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *
@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
Quoted 2 lines · On Sat, Sep 18, 2021 at 9:51 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
- The title is too vague.
I am not sure what the best title is, but my current idea is:
mem: instrument allocator with ASan
- This is a nice feature that must be announced in the release notes.
- How should we spell it?
Asan ?
ASAN ?
ASan ?
Please update devtools/words-case.txt and fix inconsistencies in this patch.
Quoted 11 lines
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASAN function currently only supports on Linuxx86_64.
If you don't intend to update other arches, at least explain in the commitlog what should be done: so that other arches know what to do to add support.
Quoted 14 lines
Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......
Quoted 3 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address
ASAN is triggered by -Db_sanitize=address, it is the only *needed* option afaiu.
Quoted 2 lines
"-Dbuildtype=debug": Display code information when coredump occurs in the program.
In ASan context, there is no coredump.
ASan displays a backtrace which is easier to read when debug symbols are available.
You can suggest building with debug, but this is *not needed*.
Quoted 2 lines
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.
This is an issue with meson and clang. Tweaking b_lundef is needed with clang, gcc looks fine. But still, on RHEL with gcc, I need to install libasan.
Maybe we can add libasan at a requirement at project level, did you try it?
Quoted 11 lines
Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>--- doc/guides/prog_guide/asan.rst | 130 ++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 184 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 +++ lib/eal/common/rte_malloc.c | 9 +- lib/pipeline/rte_swx_pipeline.c | 4 +-
This change on pipeline has no explanation, and looks out of place wrt to current change.
Quoted 127 lines
7 files changed, 359 insertions(+), 7 deletions(-) create mode 100644 doc/guides/prog_guide/asan.rstdoc/guides/prog_guide/asan.rst new file +118
-0@@ -0,0 +1,130 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(Asan) +========================================================== + +AddressSanitizer (ASan) is a google memory error detect +standard tool. It could help to detect use-after-free and +{heap,stack,global}-buffer overflow bugs in C/C++ programs, +print detailed error information when error happens, large +improve debug efficiency. + +By referring to its implementation algorithm +(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm), +enabled heap-buffer-overflow and use-after-free functions on dpdk. +DPDK ASAN function currently only supports on Linux x86_64. + +AddressSanitizer is a part of LLVM(3.1+)and GCC(4.8+). + +Example heap-buffer-overflow error +---------------------------------- + +Following error was reported when Asan was enabled:: + + Applied 9 bytes of memory, but accessed the 10th byte of memory, + so heap-buffer-overflow appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0); + if (!p) { + printf("rte_zmalloc error."); + return -1; + } + p[9] = 'a'; + +The error log:: + + ==49433==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7f773fafa249 at pc 0x5556b13bdae4 bp 0x7ffeb4965e40 sp 0x7ffeb4965e30 WRITE of size 1 at 0x7f773fafa249 thread T0 + #0 0x5556b13bdae3 in asan_heap_buffer_overflow ../app/test/test_asan_heap_buffer_overflow.c:25 + #1 0x5556b043e9d4 in cmd_autotest_parsed ../app/test/commands.c:71 + #2 0x5556b1cdd4b0 in cmdline_parse ../lib/cmdline/cmdline_parse.c:290 + #3 0x5556b1cd8987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x5556b1ce477a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x5556b1cd923e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x5556b1cd9769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x5556b045f53b in main ../app/test/test.c:234 + #8 0x7f7f1eba90b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) + #9 0x5556b043e70d in _start (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-test+0x7ce70d) + + Address 0x7f773fafa249 is a wild pointer. + SUMMARY: AddressSanitizer: heap-buffer-overflow ../app/test/test_asan_heap_buffer_overflow.c:25 in asan_heap_buffer_overflow + Shadow bytes around the buggy address: + 0x0fef67f573f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57400: fa fa 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + =>0x0fef67f57440: 00 00 00 00 00 00 fa fa 00[01]fa 00 00 00 00 00 + 0x0fef67f57450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57460: 00 00 00 00 00 00 fa fa 00 00 00 00 00 00 00 00 + 0x0fef67f57470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +Example use-after-free error +---------------------------- + +Following error was reported when Asan was enabled:: + + Applied for 9 bytes of memory, and accessed the first byte after + released, so heap-use-after-free appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0); + if (!p) { + printf("rte_zmalloc error."); + return -1; + } + rte_free(p); + *p = 'a'; + +The error log:: + + ==49478==ERROR: AddressSanitizer: heap-use-after-free on address 0x7fe2ffafa240 at pc 0x56409b084bc8 bp 0x7ffef62c57d0 sp 0x7ffef62c57c0 WRITE of size 1 at 0x7fe2ffafa240 thread T0 + #0 0x56409b084bc7 in asan_use_after_free ../app/test/test_asan_use_after_free.c:26 + #1 0x56409a1059d4 in cmd_autotest_parsed ../app/test/commands.c:71 + #2 0x56409b9a44b0 in cmdline_parse ../lib/cmdline/cmdline_parse.c:290 + #3 0x56409b99f987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x56409b9ab77a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x56409b9a023e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x56409b9a0769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x56409a12653b in main ../app/test/test.c:234 + #8 0x7feafafc20b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) + #9 0x56409a10570d in _start (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-test+0x7ce70d) + + Address 0x7fe2ffafa240 is a wild pointer. + SUMMARY: AddressSanitizer: heap-use-after-free ../app/test/test_asan_use_after_free.c:26 in asan_use_after_free + Shadow bytes around the buggy address: + 0x0ffcdff573f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57400: fa fa 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + =>0x0ffcdff57440: 00 00 00 00 00 00 00 00[fd]fd fd fd fd fd fd fd + 0x0ffcdff57450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57460: 00 00 00 00 00 00 fa fa 00 00 00 00 00 00 00 00 + 0x0ffcdff57470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable Asan in meson build system, use following meson build command:
doc/guides/prog_guide/asan.rst line 128
Quoted 10 lines
doc/guides/prog_guide/asan.rst new file +10
-0+ +Example usage:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) The issue of asan wild pointer is that dpdk asan tool is not fully adapted to google asan. + For example: Address 0x7fe2ffafa240 is a wild pointer.
I can't understand what the "wild pointer" means in this context. This comment belongs to the traces in the section before.
doc/guides/prog_guide/asan.rst line 129
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ b) Centos8 needs to install libasan separately.
See my previous comment on b_lundef.
doc/guides/prog_guide/asan.rst line 130
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ c) If the program uses cmdline, when a memory bug occurs, need to execute the "stty echo" command.
Yes, this is annoying when executing failing unit tests.
That is something to handle better in the cmdline library, maybe in the future.
Like "wild pointer", I don't think this comment belongs here.
Quoted 125 lines
doc/guides/prog_guide/index.rst +1
-0@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asanlib/eal/common/malloc_elem.c +25
-1@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */ @@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }lib/eal/common/malloc_elem.h +8
-0@@ -7,6 +7,14 @@#include <stdbool.h> +#ifdef __SANITIZE_ADDRESS__ +#define RTE_MALLOC_ASAN +#elif defined(__has_feature) +# if __has_feature (address_sanitizer) +#define RTE_MALLOC_ASAN +# endif +#endif +#define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)/* dummy definition of struct so we can use pointers to it in malloc_elem struct */
lib/eal/common/malloc_elem.h struct malloc_elem { · line 49
Quoted 6 lines
lib/eal/common/malloc_elem.h +3
-0@@ -36,10 +44,20 @@ struct malloc_elem {uint64_t header_cookie;/* Cookie marking start of data */ /* trailer cookie at start + size */ #endif +#ifdef RTE_MALLOC_ASAN +size_t user_size; +uint64_t asan_cookie[2 ];/*must be next to header_cookie*/
Fix coding style for comment please.
Quoted 33 lines
lib/eal/common/malloc_elem.h +11
-3+#endif } __rte_cache_aligned; +static const unsigned int MALLOC_ELEM_HEADER_LEN =sizeof (struct malloc_elem ); +#ifndef RTE_MALLOC_DEBUG -static const unsigned MALLOC_ELEM_TRAILER_LEN =0 ; +#ifdef RTE_MALLOC_ASAN +static const unsigned int MALLOC_ELEM_TRAILER_LEN =RTE_CACHE_LINE_SIZE ; +#else +static const unsigned int MALLOC_ELEM_TRAILER_LEN ; +#endif /* dummy function - just check if pointer is non-null */ static inline int@@ -55,7 +73,7 @@ set_trailer(struct malloc_elem *elem __rte_unused){ }#else -static const unsigned MALLOC_ELEM_TRAILER_LEN =RTE_CACHE_LINE_SIZE ; +static const unsigned int MALLOC_ELEM_TRAILER_LEN =RTE_CACHE_LINE_SIZE ;#define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL/**< Header cookie. */ #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL/**< Trailer cookie.*/ @@ -90,9 +108,169 @@ malloc_elem_cookies_ok(const struct malloc_elem *elem)#endif -static const unsigned MALLOC_ELEM_HEADER_LEN =sizeof (struct malloc_elem );#define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN) +#ifdef RTE_MALLOC_ASAN +
lib/eal/common/malloc_elem.h malloc_elem_cookies_ok(const struct malloc_elem *elem) · line 124
Quoted 10 lines
lib/eal/common/malloc_elem.h +10
-0+#define ASAN_SHADOW_GRAIN_SIZE 8 +#define ASAN_MEM_FREE_FLAG 0xfd +#define ASAN_MEM_REDZONE_FLAG 0xfa +#define ASAN_MEM_TO_SHADOW (mem) (((mem) >> 3) + 0x00007fff8000) + +#if defined(__clang__) +__attribute__((no_sanitize("address", "hwaddress"))) +#else +__attribute__((no_sanitize_address)) +#endif
This attribute is only used here, I am ok with leaving this as is. If later it is needed elsewhere, we'll have to define a new attribute wrapper in rte_common.h.
lib/eal/common/malloc_elem.h malloc_elem_cookies_ok(const struct malloc_elem *elem) · line 136
Quoted 12 lines
lib/eal/common/malloc_elem.h +12
-0+static inline void +asan_set_shadow (void *addr,char val) +{ + *(char *)addr = val; +} + +static inline void +asan_set_zone (void *ptr,size_t len,uint32_t val) +{ +size_t offset; +char *shadow; +size_t zone_len = len /ASAN_SHADOW_GRAIN_SIZE ;
+ size_t i;
+ <empty line> to separate declarations from code.
lib/eal/common/malloc_elem.h malloc_elem_cookies_ok(const struct malloc_elem *elem) · line 140
Quoted 4 lines
lib/eal/common/malloc_elem.h +4
-0+if (len %ASAN_SHADOW_GRAIN_SIZE !=0 ) + zone_len +=1 ; + +for (size_t i =0 ; i < zone_len; i++) {
+ for (i = 0; i < zone_len; i++) {
That's to fix build issue:
In file included from ../lib/eal/common/malloc_mp.c:16:0:../lib/eal/common/malloc_elem.h: In function ‘asan_set_zone’:../lib/eal/common/malloc_elem.h:140:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
for (size_t i = 0; i < zone_len; i++) {
^../lib/eal/common/malloc_elem.h:140:2: note: use option -std=c99 or -std=gnu99 to compile your code
Quoted 260 lines
lib/eal/common/malloc_elem.h +133
-0+ offset = i *ASAN_SHADOW_GRAIN_SIZE ; + shadow = (char *)ASAN_MEM_TO_SHADOW (((int64_t )ptr + offset)); + asan_set_shadow(shadow, val); + } +} + +/* +* When the memory is released, the release mark is +* set in the corresponding range of the shadow area. +*/ +static inline void +asan_set_freezone (void *ptr,size_t size) +{ +asan_set_zone (ptr, size,ASAN_MEM_FREE_FLAG ); +} + +/* +* When the memory is allocated, memory state must set as accessible. +*/ +static inline void +asan_clear_alloczone (struct malloc_elem *elem) +{ +asan_set_zone ((void *)elem, elem->size,0x0 ); +} + +static inline void +asan_clear_split_alloczone (struct malloc_elem *elem) +{ +void *ptr =RTE_PTR_SUB (elem,MALLOC_ELEM_TRAILER_LEN ); +asan_set_zone (ptr,MALLOC_ELEM_OVERHEAD ,0x0 ); +} + +/* +* When the memory is allocated, the memory boundary is +* marked in the corresponding range of the shadow area. +*/ +static inline void +asan_set_redzone (struct malloc_elem *elem,size_t user_size) +{ +uint64_t ptr; +char *shadow; +if (elem !=NULL ) { +if (elem->state !=ELEM_PAD ) + elem =RTE_PTR_ADD (elem, elem->pad); + + elem->user_size = user_size; + +/* Set mark before the start of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN ) + -ASAN_SHADOW_GRAIN_SIZE ; + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +asan_set_shadow (shadow,ASAN_MEM_REDZONE_FLAG ); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + -ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,ASAN_MEM_REDZONE_FLAG ); + +/* Set mark after the end of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + + elem->user_size); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +uint32_t val = (ptr %ASAN_SHADOW_GRAIN_SIZE ); + val = (val ==0 ) ?ASAN_MEM_REDZONE_FLAG : val; +asan_set_shadow (shadow, val); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + +ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,ASAN_MEM_REDZONE_FLAG ); + } +} + +/* +* When the memory is released, the mark of the memory boundary +* in the corresponding range of the shadow area is cleared. +*/ +static inline void +asan_clear_redzone (struct malloc_elem *elem) +{ +uint64_t ptr; +char *shadow; +if (elem !=NULL ) { + elem =RTE_PTR_ADD (elem, elem->pad); + +/* Clear mark before the start of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN ) + -ASAN_SHADOW_GRAIN_SIZE ; + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +asan_set_shadow (shadow,0x00 ); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + -ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,0x00 ); + +/* Clear mark after the end of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + + elem->user_size); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +asan_set_shadow (shadow,0x00 ); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + +ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,0x00 ); + } +} + +static inline size_t +old_malloc_size (struct malloc_elem *elem) +{ +if (elem->state !=ELEM_PAD ) + elem =RTE_PTR_ADD (elem, elem->pad); + +return elem->user_size; +} +#else +static inline void +asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { } + +static inline void +asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { } + +static inline void +asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { } + +static inline void +asan_set_redzone(struct malloc_elem *elem __rte_unused, + size_t user_size __rte_unused) { } + +static inline void +asan_clear_redzone(struct malloc_elem *elem __rte_unused) { } + +static inline size_t +old_malloc_size (struct malloc_elem *elem) +{ +return elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +} +#endif +/* * Given a pointer to the start of a memory block returned by malloc, get * the actual malloc_elem header for that block. lib/eal/common/malloc_heap.c +12
-0@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }lib/eal/common/rte_malloc.c +8
-1@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok, @@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);lib/pipeline/rte_swx_pipeline.c +2
-3@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data * --2.25.1
Signature · 1 line
David Marchand
Quoted 10 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, September 20, 2021 6:04 PM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3] Enable AddressSanitizer feature on DPDK
Quoted 7 lines
Quoted 2 lines · On Sat, Sep 18, 2021 at 9:51 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
- The title is too vague.
I am not sure what the best title is, but my current idea is:
mem: instrument allocator with Asan
Do you think this name is accurate: Enable AddressSanitizer for memory detector on DPDK
Quoted 9 lines
- This is a nice feature that must be announced in the release notes.
- How should we spell it?
Asan ?
ASAN ?
ASan ?
Use ASan, google's spelling:
https://github.com/google/sanitizers/wiki/AddressSanitizer
Quoted 2 lines
Please update
devtools/words-case.txtand fix inconsistencies in this patch.
V4 will update this document.
Quoted 6 lines
Quoted 6 lines
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
Quoted 7 lines
Quoted 4 lines
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASAN function currently only supports on Linuxx86_64.If you don't intend to update other arches, at least explain in the commitlog what should be done: so that other arches know what to do to add support.
V4 will add instructions.
Quoted 23 lines
Quoted 15 lines
Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......Quoted 3 lines
If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=addressASAN is triggered by -Db_sanitize=address, it is the only *needed* option afaiu.
Quoted 11 lines
Quoted 2 lines
"-Dbuildtype=debug": Display code information when coredump occurs in the program.
In ASan context, there is no coredump.
ASan displays a backtrace which is easier to read when debug symbols are available.
You can suggest building with debug, but this is *not needed*.Quoted 2 lines
"-Db_lundef=false": It is enabled by default, and needs to be disabled when using asan.
Dbuildtype and Db_lundef will update the instructions.
Quoted 5 lines
This is an issue with meson and clang. Tweaking
b_lundefis needed with clang, gcc looks fine. But still, on RHEL with gcc, I need to install libasan.Maybe we can add libasan at a requirement at project level, did you try it?
libasan is installed by default on the ubuntu system. Because libasan and the compiler are matched, so it is best to manually install the matching library.
Quoted 9 lines
Quoted 9 lines
Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>--- doc/guides/prog_guide/asan.rst | 130 ++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 184
Quoted 7 lines
+++++++++++++++++++++++++++++++-
Quoted 3 lines
lib/eal/common/malloc_heap.c | 12 +++ lib/eal/common/rte_malloc.c | 9 +- lib/pipeline/rte_swx_pipeline.c | 4 +-This change on pipeline has no explanation, and looks out of place wrt to current change.
This is to solve the gcc compilation problem, I will separate this patch.
Quoted 166 lines
Quoted 13 lines
7 files changed, 359 insertions(+), 7 deletions(-) create mode100644
doc/guides/prog_guide/asan.rst(quoted diff) +0
-0
b/doc/guides/prog_guide/asan.rstnew file mode 100644 index 0000000000..a0589d9b8adoc/guides/prog_guide/asan.rst line 4
Quoted 4 lines
doc/guides/prog_guide/asan.rst new file +4
-0@@ -0,0 +1,130 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(Asan)+=========================================================
=Quoted 24 lines
doc/guides/prog_guide/asan.rst new file +24
-0+ +AddressSanitizer (ASan) is a google memory error detect standard +tool. It could help to detect use-after-free and +{heap,stack,global}-buffer overflow bugs in C/C++ programs, print +detailed error information when error happens, large improve debug +efficiency. + +By referring to its implementation algorithm +(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm) +, enabled heap-buffer-overflow and use-after-free functions on dpdk. +DPDK ASAN function currently only supports on Linux x86_64. + +AddressSanitizer is a part of LLVM(3.1+)and GCC(4.8+). + +Example heap-buffer-overflow error +---------------------------------- + +Following error was reported when Asan was enabled:: + + Applied 9 bytes of memory, but accessed the 10th byte of memory, + so heap-buffer-overflow appeared. + +Below code results in this error:: +doc/guides/prog_guide/asan.rst
Quoted 10 lines
doc/guides/prog_guide/asan.rst new file +10
-0+ char *p = rte_zmalloc(NULL, 9, 0); + if (!p) { + printf("rte_zmalloc error."); + return -1; + } + p[9] = 'a'; + +The error log:: + + ==49433==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x7f773fafa249at pc0x5556b13bdae4bp0x7ffeb4965e40sp0x7ffeb4965e30WRITE of size 1 at0x7f773fafa249thread T0doc/guides/prog_guide/asan.rst line 39
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ #0 0x5556b13bdae3 in
asan_heap_buffer_overflow../app/test/test_asan_heap_buffer_overflow. c:25doc/guides/prog_guide/asan.rst line 40
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ #1 0x5556b043e9d4 in
cmd_autotest_parsed../app/test/commands.c:71doc/guides/prog_guide/asan.rst line 41
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ #2 0x5556b1cdd4b0 in
cmdline_parse../lib/cmdline/cmdline_parse.c:290doc/guides/prog_guide/asan.rst
Quoted 6 lines
doc/guides/prog_guide/asan.rst new file +6
-0+ #3 0x5556b1cd8987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x5556b1ce477a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x5556b1cd923e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x5556b1cd9769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x5556b045f53b in main ../app/test/test.c:234 + #8 0x7f7f1eba90b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/asan.rst line 49
Quoted 2 lines
doc/guides/prog_guide/asan.rst new file +2
-0+ #9 0x5556b043e70d in _start + (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-
test+0x7ce70doc/guides/prog_guide/asan.rst
Quoted 4 lines
doc/guides/prog_guide/asan.rst new file +4
-0+ d) + + Address 0x7f773fafa249 is a wild pointer. + SUMMARY: AddressSanitizer: heap-buffer-overflow
../app/test/test_asan_heap_buffer_overflow.c:25inasan_heap_buffer_overflowQuoted 23 lines
doc/guides/prog_guide/asan.rst new file +23
-0+ Shadow bytes around the buggy address: + 0x0fef67f573f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57400: fa fa 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + =>0x0fef67f57440: 00 00 00 00 00 00 fa fa 00[01]fa 00 00 00 00 00 + 0x0fef67f57450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57460: 00 00 00 00 00 00 fa fa 00 00 00 00 00 00 00 00 + 0x0fef67f57470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0fef67f57480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +Example use-after-free error +---------------------------- + +Following error was reported when Asan was enabled:: + + Applied for 9 bytes of memory, and accessed the first byte after + released, so heap-use-after-free appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0);doc/guides/prog_guide/asan.rst
Quoted 10 lines
doc/guides/prog_guide/asan.rst new file +10
-0+ if (!p) { + printf("rte_zmalloc error."); + return -1; + } + rte_free(p); + *p = 'a'; + +The error log:: + + ==49478==ERROR: AddressSanitizer: heap-use-after-free on address
0x7fe2ffafa240at pc0x56409b084bc8bp0x7ffef62c57d0sp0x7ffef62c57c0WRITE of size 1 at0x7fe2ffafa240thread T0doc/guides/prog_guide/asan.rst line 87
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ #0 0x56409b084bc7 in
asan_use_after_free../app/test/test_asan_use_after_free.c:26doc/guides/prog_guide/asan.rst line 88
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ #1 0x56409a1059d4 in
cmd_autotest_parsed../app/test/commands.c:71doc/guides/prog_guide/asan.rst line 89
Quoted 1 line
doc/guides/prog_guide/asan.rst new file +1
-0+ #2 0x56409b9a44b0 in
cmdline_parse../lib/cmdline/cmdline_parse.c:290doc/guides/prog_guide/asan.rst line 91
Quoted 2 lines
doc/guides/prog_guide/asan.rst new file +2
-0+ #3 0x56409b99f987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x56409b9ab77a in
rdline_char_in../lib/cmdline/cmdline_rdline.c:421doc/guides/prog_guide/asan.rst
Quoted 4 lines
doc/guides/prog_guide/asan.rst new file +4
-0+ #5 0x56409b9a023e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x56409b9a0769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x56409a12653b in main ../app/test/test.c:234 + #8 0x7feafafc20b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/asan.rst line 97
Quoted 2 lines
doc/guides/prog_guide/asan.rst new file +2
-0+ #9 0x56409a10570d in _start + (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-
test+0x7ce70doc/guides/prog_guide/asan.rst
Quoted 4 lines
doc/guides/prog_guide/asan.rst new file +4
-0+ d) + + Address 0x7fe2ffafa240 is a wild pointer. + SUMMARY: AddressSanitizer: heap-use-after-free
../app/test/test_asan_use_after_free.c:26inasan_use_after_freeQuoted 10 lines
doc/guides/prog_guide/asan.rst new file +10
-0+ Shadow bytes around the buggy address: + 0x0ffcdff573f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57400: fa fa 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + =>0x0ffcdff57440: 00 00 00 00 00 00 00 00[fd]fd fd fd fd fd fd fd + 0x0ffcdff57450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57460: 00 00 00 00 00 00 fa fa 00 00 00 00 00 00 00 00 + 0x0ffcdff57470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00doc/guides/prog_guide/asan.rst
Quoted 10 lines
doc/guides/prog_guide/asan.rst new file +10
-0+ 0x0ffcdff57480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 0x0ffcdff57490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable Asan in meson build system, use following meson buildcommand:
doc/guides/prog_guide/asan.rst
Quoted 9 lines
doc/guides/prog_guide/asan.rst new file +9
-0+ +Example usage:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) The issue of asan wild pointer is that dpdk asan tool is not fullyadapted to google asan.
Quoted 4 lines
Quoted 1 line
+ For example: Address
0x7fe2ffafa240is a wild pointer.I can't understand what the "wild pointer" means in this context. This comment belongs to the traces in the section before.
This is because we only set up the red zone and did not construct the complete data structure required by libasan. As a result, libasan could not find the location of the memory request, so "wild pointer" was reported. The current information has been able to troubleshoot errors, so this issue is a lower priority.
Quoted 6 lines
Quoted 2 lines
+ b) Centos8 needs to install libasan separately.
See my previous comment on
b_lundef.
Quoted 7 lines
Quoted 1 line
+ c) If the program uses cmdline, when a memory bug occurs, need to
execute the "stty echo" command.
Yes, this is annoying when executing failing unit tests.
That is something to handle better in the cmdline library, maybe in the future.Like "wild pointer", I don't think this comment belongs here.
cmdline cannot solve this problem temporarily, so this problem must be explained, but I don’t know which position is better.
Quoted 138 lines
lib/eal/common/malloc_elem.c malloc_elem_alloc(struct malloc_elem *elem,
Quoted 16 lines
(quoted diff) +0
-0
b/doc/guides/prog_guide/index.rstindex2dce507f46..df8a4b93e1100644doc/guides/prog_guide/index.rst +1
-0@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan(quoted diff) +0
-0
b/lib/eal/common/malloc_elem.cindexc2c9461f1d..bdd20a162e100644lib/eal/common/malloc_elem.c +0
-0@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem,
size_tsize, unsigned align,lib/eal/common/malloc_elem.c malloc_elem_alloc(struct malloc_elem *elem,
Quoted 10 lines
lib/eal/common/malloc_elem.c +2
-0struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem,
size_tsize, unsigned align,lib/eal/common/malloc_elem.c malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
Quoted 6 lines
lib/eal/common/malloc_elem.c +2
-0elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */
lib/eal/common/malloc_elem.c malloc_elem_alloc(struct malloc_elem *elem,
Quoted 3 lines
lib/eal/common/malloc_elem.c +0
-1if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */ @@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_tsize, unsigned align,
lib/eal/common/malloc_elem.c malloc_elem_hide_region(struct malloc_elem
Quoted 19 lines
return new_elem; }lib/eal/common/malloc_elem.c +6
-0+asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem*elem, void *start,
size_tlen)lib/eal/common/malloc_elem.c malloc_elem_hide_region(struct malloc_elem
Quoted 10 lines
lib/eal/common/malloc_elem.c +2
-0if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem*elem, void *start,
size_tlen)lib/eal/common/malloc_elem.c malloc_elem_hide_region(struct malloc_elem
Quoted 10 lines
lib/eal/common/malloc_elem.c +2
-0if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem*elem, void *start,
size_tlen)lib/eal/common/malloc_elem.c malloc_elem_resize(struct malloc_elem *elem,
Quoted 9 lines
lib/eal/common/malloc_elem.c +2
-0} } +asan_clear_alloczone (elem); +remove_elem (elem); }@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem,
size_tsize)Quoted 11 lines
lib/eal/common/malloc_elem.c +3
-1const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ Quoted 3 lines
if (!elem->next || elem->next->state != ELEM_FREE || @@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size) /* now we have a big block together. Lets cut it down a bit, bysplitting */
Quoted 20 lines
struct
malloc_elem*split_pt=RTE_PTR_ADD(elem, new_size);split_pt= RTE_PTR_ALIGN_CEIL(split_pt,RTE_CACHE_LINE_SIZE);lib/eal/common/malloc_elem.c +6
-0+ +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }(quoted diff) +0
-0
b/lib/eal/common/malloc_elem.hindexa1e5f7f02c..01a739f2ea100644lib/eal/common/malloc_elem.h line 13
Quoted 7 lines
lib/eal/common/malloc_elem.h +4
-0@@ -7,6 +7,14 @@#include <stdbool.h> +#ifdef __SANITIZE_ADDRESS__ +#define RTE_MALLOC_ASAN +#elif defined(__has_feature) +# if __has_feature(address_sanitizer) #define RTE_MALLOC_ASAN #endif
Quoted 4 lines
lib/eal/common/malloc_elem.h +2
-0+#endif +#define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
Quoted 10 lines
lib/eal/common/malloc_elem.h struct malloc_elem { · line 49
Quoted 8 lines
lib/eal/common/malloc_elem.h +0
-0/* dummy definition of struct so we can use pointers to it in
malloc_elemstruct */ @@ -36,10 +44,20 @@ structmalloc_elem{
uint64_theader_cookie; /* Cookie marking start of data */
/* trailer cookie at start +
size */ #endiflib/eal/common/malloc_elem.h +3
-0+#ifdef RTE_MALLOC_ASAN +size_t user_size; +uint64_t asan_cookie[2 ];/*must be next to header_cookie*/ Fix coding style for comment please.
V4 will be fixed.
Quoted 71 lines
Quoted 19 lines
lib/eal/common/malloc_elem.h +8
-1+#endif } __rte_cache_aligned; +static const unsigned int MALLOC_ELEM_HEADER_LEN =sizeof (struct +malloc_elem ); +#ifndef RTE_MALLOC_DEBUG -static const unsigned MALLOC_ELEM_TRAILER_LEN =0 ; +#ifdef RTE_MALLOC_ASAN +static const unsigned int MALLOC_ELEM_TRAILER_LEN = +RTE_CACHE_LINE_SIZE ;#else static const unsigned int +MALLOC_ELEM_TRAILER_LEN ;#endif /* dummy function - just check if pointer is non-null */ staticinline int @@ -55,7 +73,7 @@
set_trailer(struct malloc_elem *elem __rte_unused){ }Quoted 2 lines
#else
-static const unsignedMALLOC_ELEM_TRAILER_LEN=
RTE_CACHE_LINE_SIZE;lib/eal/common/malloc_elem.h
Quoted 4 lines
lib/eal/common/malloc_elem.h +2
-0+static const unsigned int MALLOC_ELEM_TRAILER_LEN = +RTE_CACHE_LINE_SIZE ;#define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL/**< Header cookie. */
Quoted 8 lines
#define
MALLOC_TRAILER_COOKIE0xadd2e55badbadbadULL/**< Trailer
cookie.*/ @@ -90,9 +108,169 @@malloc_elem_cookies_ok(const struct malloc_elem *elem)#endif
-static const unsigned
MALLOC_ELEM_HEADER_LEN=sizeof(struct malloc_elem); #defineMALLOC_ELEM_OVERHEAD(
MALLOC_ELEM_HEADER_LEN+lib/eal/common/malloc_elem.h malloc_elem_cookies_ok(const struct malloc_elem *elem)
Quoted 8 lines
MALLOC_ELEM_TRAILER_LEN)lib/eal/common/malloc_elem.h +6
-0+#ifdef RTE_MALLOC_ASAN + +#define ASAN_SHADOW_GRAIN_SIZE 8 +#define ASAN_MEM_FREE_FLAG 0xfd +#define ASAN_MEM_REDZONE_FLAG 0xfa +#define ASAN_MEM_TO_SHADOW (mem) (((mem) >> 3) +
0x00007fff8000)lib/eal/common/malloc_elem.h malloc_elem_cookies_ok(const struct malloc_elem *elem) · line 124
Quoted 5 lines
lib/eal/common/malloc_elem.h +5
-0+ +#if defined(__clang__) +__attribute__((no_sanitize("address", "hwaddress"))) #else +__attribute__((no_sanitize_address)) +#endif This attribute is only used here, I am ok with leaving this as is. If later it is needed elsewhere, we'll have to define a new attribute wrapper in
rte_common.h.lib/eal/common/malloc_elem.h malloc_elem_cookies_ok(const struct malloc_elem *elem) · line 136
Quoted 10 lines
lib/eal/common/malloc_elem.h +10
-0+static inline void +asan_set_shadow (void *addr,char val) { + *(char *)addr = val; +} + +static inline void +asan_set_zone (void *ptr,size_t len,uint32_t val) { +size_t offset; +char *shadow; +size_t zone_len = len /ASAN_SHADOW_GRAIN_SIZE ;+
size_ti;
+ <empty line> to separate declarations from code.
Quoted 9 lines
lib/eal/common/malloc_elem.h malloc_elem_cookies_ok(const struct malloc_elem *elem) · line 140
Quoted 4 lines
lib/eal/common/malloc_elem.h +4
-0+if (len %ASAN_SHADOW_GRAIN_SIZE !=0 ) + zone_len +=1 ; + +for (size_t i =0 ; i < zone_len; i++) {+ for (i = 0; i <
zone_len; i++) {That's to fix build issue:
V4 will be fixed.
Quoted 292 lines
In file included from
../lib/eal/common/malloc_mp.c:16:0:../lib/eal/common/malloc_elem.h: In function ‘asan_set_zone’:../lib/eal/common/malloc_elem.h:140:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
for (size_ti = 0; i <zone_len; i++) {
^../lib/eal/common/malloc_elem.h:140:2: note: use option -std=c99 or -std=gnu99 to compile your codelib/eal/common/malloc_elem.h
Quoted 2 lines
lib/eal/common/malloc_elem.h +2
-0+ offset = i *ASAN_SHADOW_GRAIN_SIZE ; + shadow = (char *)ASAN_MEM_TO_SHADOW(((int64_t)ptr +offset));
Review
Quoted 133 lines
lib/eal/common/malloc_elem.h +129
-0+ asan_set_shadow(shadow, val); + } +} + +/* +* When the memory is released, the release mark is +* set in the corresponding range of the shadow area. +*/ +static inline void +asan_set_freezone (void *ptr,size_t size) { +asan_set_zone (ptr, size,ASAN_MEM_FREE_FLAG ); } + +/* +* When the memory is allocated, memory state must set as accessible. +*/ +static inline void +asan_clear_alloczone (struct malloc_elem *elem) +{ +asan_set_zone ((void *)elem, elem->size,0x0 ); +} + +static inline void +asan_clear_split_alloczone (struct malloc_elem *elem) +{ +void *ptr =RTE_PTR_SUB (elem,MALLOC_ELEM_TRAILER_LEN ); +asan_set_zone (ptr,MALLOC_ELEM_OVERHEAD ,0x0 ); +} + +/* +* When the memory is allocated, the memory boundary is +* marked in the corresponding range of the shadow area. +*/ +static inline void +asan_set_redzone (struct malloc_elem *elem,size_t user_size) +{ +uint64_t ptr; +char *shadow; +if (elem !=NULL ) { +if (elem->state !=ELEM_PAD ) + elem =RTE_PTR_ADD (elem, elem->pad); + + elem->user_size = user_size; + +/* Set mark before the start of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN ) + -ASAN_SHADOW_GRAIN_SIZE ; + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +asan_set_shadow (shadow,ASAN_MEM_REDZONE_FLAG ); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + -ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,ASAN_MEM_REDZONE_FLAG ); + +/* Set mark after the end of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + + elem->user_size); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +uint32_t val = (ptr %ASAN_SHADOW_GRAIN_SIZE ); + val = (val ==0 ) ?ASAN_MEM_REDZONE_FLAG : val; +asan_set_shadow (shadow, val); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + +ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,ASAN_MEM_REDZONE_FLAG ); + } +} + +/* +* When the memory is released, the mark of the memory boundary +* in the corresponding range of the shadow area is cleared. +*/ +static inline void +asan_clear_redzone (struct malloc_elem *elem) +{ +uint64_t ptr; +char *shadow; +if (elem !=NULL ) { + elem =RTE_PTR_ADD (elem, elem->pad); + +/* Clear mark before the start of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN ) + -ASAN_SHADOW_GRAIN_SIZE ; + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +asan_set_shadow (shadow,0x00 ); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + -ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,0x00 ); + +/* Clear mark after the end of the allocated memory */ + ptr = (uint64_t )RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + + elem->user_size); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr); +asan_set_shadow (shadow,0x00 ); + shadow = (char *)ASAN_MEM_TO_SHADOW (ptr + +ASAN_SHADOW_GRAIN_SIZE ); +asan_set_shadow (shadow,0x00 ); + } +} + +static inline size_t +old_malloc_size (struct malloc_elem *elem) +{ +if (elem->state !=ELEM_PAD ) + elem =RTE_PTR_ADD (elem, elem->pad); + +return elem->user_size; +} +#else +static inline void +asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { } + +static inline void +asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { } + +static inline void +asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { } + +static inline void +asan_set_redzone(struct malloc_elem *elem __rte_unused, + size_t user_size __rte_unused) { } + +static inline void +asan_clear_redzone(struct malloc_elem *elem __rte_unused) { } + +static inline size_t +old_malloc_size (struct malloc_elem *elem) +{ +return elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +} +#endif +/* * Given a pointer to the start of a memory block returned by malloc, get * the actual malloc_elem header for that block. (quoted diff) +0
-0
b/lib/eal/common/malloc_heap.clib/eal/common/malloc_heap.c heap_alloc(struct malloc_heap *heap, const char
Quoted 4 lines
index
ee400f38ec..775d6789df100644lib/eal/common/malloc_heap.c +0
-0@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char*type
__rte_unused,size_tsize,lib/eal/common/malloc_heap.c heap_alloc(struct malloc_heap *heap, const char
Quoted 8 lines
lib/eal/common/malloc_heap.c +1
-0unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char*type
__rte_unused,size_tsize,lib/eal/common/malloc_heap.c heap_alloc_biggest(struct malloc_heap *heap,
Quoted 9 lines
lib/eal/common/malloc_heap.c +2
-0/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap,const char *type
__rte_unused,Quoted 9 lines
lib/eal/common/malloc_heap.c +2
-0/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);lib/eal/common/malloc_heap.c malloc_heap_free(struct malloc_elem *elem) · line 851
Quoted 6 lines
lib/eal/common/malloc_heap.c +2
-0@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */
lib/eal/common/malloc_heap.c malloc_heap_free(struct malloc_elem *elem) · line 858
Quoted 7 lines
lib/eal/common/malloc_heap.c +1
-0heap = elem->heap; msl = elem->msl;@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); + void *asan_ptr = RTE_PTR_ADD(elem, MALLOC_ELEM_HEADER_LEN +
elem->pad);lib/eal/common/malloc_heap.c malloc_heap_free(struct malloc_elem *elem) · line 859
Quoted 1 line
lib/eal/common/malloc_heap.c +1
-0+ size_t asan_data_len = elem->size - MALLOC_ELEM_OVERHEAD -
elem->pad;lib/eal/common/rte_malloc.c rte_calloc(const char *type, size_t num, size_t size,
Quoted 18 lines
lib/eal/common/malloc_heap.c +3
-0+/* mark element as free */ elem->state =ELEM_FREE ;@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }lib/eal/common/rte_malloc.c +0
-0@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size,unsigned align)
lib/eal/common/rte_malloc.c rte_realloc_socket(void *ptr, size_t size, unsigned
Quoted 9 lines
lib/eal/common/rte_malloc.c +2
-0void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsignedint align, int socket)
lib/eal/common/rte_malloc.c rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)
Quoted 6 lines
lib/eal/common/rte_malloc.c +2
-0return NULL ; } + user_size = size; + size = RTE_CACHE_LINE_ROUNDUP(size), align =
RTE_CACHE_LINE_ROUNDUP(align);lib/eal/common/rte_malloc.c rte_realloc_socket(void *ptr, size_t size, unsigned
Quoted 3 lines
lib/eal/common/rte_malloc.c +0
-0/* check requested socket id and alignment matches first, and if ok, @@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsignedint align, int socket)
lib/eal/common/rte_malloc.c rte_realloc_socket(void *ptr, size_t size, unsigned
Quoted 10 lines
lib/eal/common/rte_malloc.c +3
-0RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsignedint align, int socket)
lib/eal/common/rte_malloc.c rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)
Quoted 4 lines
lib/eal/common/rte_malloc.c +0
-1if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ - const size_t old_size = elem->size - elem->pad -
MALLOC_ELEM_OVERHEAD;Review
Quoted 5 lines
lib/eal/common/rte_malloc.c +1
-0+const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);(quoted diff) +0
-0
b/lib/pipeline/rte_swx_pipeline.cQuoted 22 lines
index
8eb978a30c..aaa0107d02100644lib/pipeline/rte_swx_pipeline.c +2
-3@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data * --Quoted 2 lines
2.25.1
--
David Marchand
PATCH v4 1/2 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASan function currently only supports on Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
Note:
a) The issue of ASan wild pointer is that dpdk ASan tool is not fully adapted to google ASan. For example: Address 0x7fe2ffafa240 is a wild pointer.
b) Centos needs to install libasan separately.
c) If the program uses DPDK cmdline, when a memory bug occurs, need to execute the "stty echo" command.
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 130 ++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 190 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 7 files changed, 364 insertions(+), 5 deletions(-) create mode 100644 doc/guides/prog_guide/ASan.rst
devtools/words-case.txt +1 -0
@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASan
doc/guides/prog_guide/ASan.rst new file +130 -0
130 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + ASan
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +187 -3
219 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v4 2/2 lib/pipeline: Fix gcc compilation error using ASan
From: Zhihong Peng <[email protected]>
After adding ASan, the gcc compilation check will be stricter. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *
@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v5 1/2 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASan function currently only supports on Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
Note:
a) The issue of ASan wild pointer is that dpdk ASan tool is not fully adapted to google ASan. For example: Address 0x7fe2ffafa240 is a wild pointer.
b) Centos needs to install libasan separately.
c) If the program uses DPDK cmdline, when a memory bug occurs, need to execute the "stty echo" command.
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 109 ++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 190 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 7 files changed, 343 insertions(+), 5 deletions(-) create mode 100644 doc/guides/prog_guide/ASan.rst
devtools/words-case.txt +1 -0
@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASan
doc/guides/prog_guide/ASan.rst new file +109 -0
109 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + ASan
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +187 -3
219 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v5 2/2 Fix gcc compilation error using ASan
From: Zhihong Peng <[email protected]>
After adding ASan, the gcc compilation check will be stricter. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *
@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v5 1/2 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
By referring to its implementation algorithm
(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm),
enable heap-buffer-overflow and use-after-free functions on dpdk. DPDK ASan function currently only supports on Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 107 ++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 190 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 7 files changed, 341 insertions(+), 5 deletions(-) create mode 100644 doc/guides/prog_guide/ASan.rst
devtools/words-case.txt +1 -0
@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASan
doc/guides/prog_guide/ASan.rst new file +107 -0
107 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + ASan
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +187 -3
219 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v5 2/2 lib/pipeline: Fix gcc compilation error using ASan
From: Zhihong Peng <[email protected]>
After adding ASan, the gcc compilation check will be stricter. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -6340,7 +6340,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline struct meter *
@@ -8025,7 +8025,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v6 1/2 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 108 +++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + examples/helloworld/main.c | 5 + lib/eal/common/malloc_elem.c | 26 +++- lib/eal/common/malloc_elem.h | 204 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 8 files changed, 361 insertions(+), 5 deletions(-) create mode 100644 doc/guides/prog_guide/ASan.rst
devtools/words-case.txt +1 -0
@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASan
doc/guides/prog_guide/ASan.rst new file +108 -0
108 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + ASan
examples/helloworld/main.c +5 -0
@@ -14,6 +14,7 @@#include <rte_per_lcore.h> #include <rte_lcore.h> #include <rte_debug.h> +#include <rte_malloc.h> /* Launch a function on lcore. 8< */ static int
@@ -38,6 +39,10 @@ main(int argc, char **argv)rte_panic ("Cannot init EAL\n" );/* >8 End of initialization of Environment Abstraction Layer */ +char *p = (char *)rte_malloc (NULL ,9 ,0 ); +rte_free (p); + p[5 ] ='a' ; +/* Launches the function on each lcore. 8< */ RTE_LCORE_FOREACH_WORKER (lcore_id) {/* Simpler equivalent. 8< */
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +201 -3
233 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v6 2/2 lib/pipeline: Fix gcc compilation error using ASan
From: Zhihong Peng <[email protected]>
After adding ASan, the gcc compilation check will be stricter. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
Re: [dpdk-dev] [dpdk-stable] [PATCH v6 2/2] lib/pipeline: Fix gcc compilation error using ASan
Quoted 2 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
Commit titles don't start with lib/.
Quoted 3 lines
After adding ASan, the gcc compilation check will be stricter. "Control reaches end of non-void function" error occurs here.
Fwiw, I could not pinpoint the right version where this warning appears. I can't see it with gcc v4.8.5 (rhel7), but I get it with gcc 11.2.1 (fc34). Do you know which versions are affected? Just asking for info.
Quoted 2 lines
Fixes f38913b7fb8e (pipeline: add meter array to SWX)
Should be formatted as:
Please use a git alias as suggested in the contribution guide.
Quoted 22 lines
Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/pipeline/rte_swx_pipeline.c +1
-1@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
lib/pipeline/rte_swx_pipeline.c instr_translate(struct rte_swx_pipeline *p, · line 5943
Quoted 8 lines
lib/pipeline/rte_swx_pipeline.c +1
-1@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
There are two other functions (instr_table_translate, and instr_extern_translate) which have the same pattern in this file. Odd that the compiler is not reporting them.
Signature · 1 line
David Marchand
Re: [dpdk-dev] [dpdk-stable] [PATCH v6 2/2] lib/pipeline: Fix gcc compilation error using ASan
Quoted 11 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, September 30, 2021 4:30 PM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; dpdk stable <[email protected]>
Subject: Re: [dpdk-stable] [PATCH v6 2/2] lib/pipeline: Fix gcc compilation error using ASan
Quoted 5 lines
Quoted 2 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
Commit titles don't start with
lib/.
The v9 version will be fixed.
Quoted 11 lines
Quoted 4 lines
After adding ASan, the gcc compilation check will be stricter. "Control reaches end of non-void function" error occurs here.
Fwiw, I could not pinpoint the right version where this warning appears. I can't see it with gcc v4.8.5 (rhel7), but I get it with gcc 11.2.1 (fc34). Do you know which versions are affected? Just asking for info.
Quoted 1 line
Quoted 6 lines
Quoted 1 line
Fixes f38913b7fb8e (pipeline: add meter array to SWX)Should be formatted as:
Fixes f38913b7fb8e ("pipeline: add meter array to SWX")Please use a git alias as suggested in the contribution guide.
The v9 version will be fixed.
Quoted 30 lines
Quoted 30 lines
Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)(quoted diff) +0
-0
b/lib/pipeline/rte_swx_pipeline.cindex1cd09a4b44..0acd6c6752100644lib/pipeline/rte_swx_pipeline.c +2
-2@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; }
Quoted 5 lines
lib/pipeline/rte_swx_pipeline.c instr_translate(struct rte_swx_pipeline *p, · line 5943
Quoted 1 line
lib/pipeline/rte_swx_pipeline.c +0
-0static struct instruction_data *There are two other functions (
instr_table_translate, andinstr_extern_translate) which have the same pattern in this file. Odd that the compiler is not reporting them.
The lowest version we tried is gcc version 7.4.0, the highest gcc version 9.3.0, these versions will report errors. As for why some do not report errors, it may be gcc's own problem, we just let the compilation pass.
Quoted 2 lines
--
David Marchand
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
In reply to [email protected]Hello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my fault.
Quoted 9 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
Quoted 6 lines
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
This patch mixes how to use ASan and instrumenting the DPDK mem allocator.
I would split this patch in two.
The first patch can add the documentation on enabling/using ASan and describe the known issues on enabling it.
I'd find it better (from a user pov) if we hide all those details about b_lundef and installation of libasan on Centos.
Something like (only quickly tested):
config/meson.build +10 -0
@@ -411,6 +411,33 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
Bruce, do you see an issue with this approach?
Then a second patch adds the rte_malloc instrumentation, with a check at configuration time.
endif
add_project_link_arguments('-lasan', language: 'c') dpdk_extra_ldflags += '-lasan'
+ if arch_subdir == 'x86'
+ asan_check_code = '''
+#ifdef __SANITIZE_ADDRESS__
+#define RTE_MALLOC_ASAN
+#elif defined(__has_feature)
+# if __has_feature(address_sanitizer)
+#define RTE_MALLOC_ASAN
+# endif
+#endif
+
+#ifndef RTE_MALLOC_ASAN
+#error ASan not available.
+#endif
+'''
+ if cc.compiles(asan_check_code)
+ dpdk_conf.set10('RTE_MALLOC_ASAN', true)
+ endif
+ endif
endif
if get_option('default_library') == 'both'
Few more comments:
Quoted 46 lines
DPDK ASan functionality is currently only supported Linux
x86_64. Support other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 108 +++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + examples/helloworld/main.c | 5 + lib/eal/common/malloc_elem.c | 26 +++- lib/eal/common/malloc_elem.h | 204 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 8 files changed, 361 insertions(+), 5 deletions(-) create mode 100644 doc/guides/prog_guide/ASan.rstdevtools/words-case.txt +0
-0
devtools/words-case.txt VXLAN · line 89
Quoted 4 lines
devtools/words-case.txt +1
-0@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASan
Alphabetical order please.
doc/guides/prog_guide/ASan.rst
Quoted 1 line
doc/guides/prog_guide/ASan.rst +0
-0
Filenames are lowercase in the doc.
Quoted 13 lines
new file mode 100644
index 0000000000..7145a3b1a1doc/guides/prog_guide/ASan.rst new file +8
-0@@ -0,0 +1,108 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(ASan) +========================================================== + +AddressSanitizer (ASan) is a google memory error detect +standard tool. It could help to detect use-after-free and
doc/guides/prog_guide/ASan.rst line 18
Quoted 10 lines
doc/guides/prog_guide/ASan.rst new file +10
-0+{heap,stack,global}-buffer overflow bugs in C/C++ programs, +print detailed error information when error happens, large +improve debug efficiency. + +By referring to its implementation algorithm +(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm), +enabled heap-buffer-overflow and use-after-free functions on DPDK. +DPDK ASan function currently only supports on Linux x86_64. + +AddressSanitizer is a part of LLVM(3.1+)and GCC(4.8+).
missing spaces around ().
Quoted 12 lines
doc/guides/prog_guide/ASan.rst new file +12
-0+ +Example heap-buffer-overflow error +---------------------------------- + +Following error was reported when ASan was enabled:: + + Applied 9 bytes of memory, but accessed the 10th byte of memory, + so heap-buffer-overflow appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0);
doc/guides/prog_guide/ASan.rst line 40
Quoted 10 lines
doc/guides/prog_guide/ASan.rst new file +10
-0+ if (!p) { + printf("rte_zmalloc error."); + return -1; + } + p[9] = 'a'; + +The error log:: + + ==49433==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7f773fafa249 at pc 0x5556b13bdae4 bp 0x7ffeb4965e40 sp 0x7ffeb4965e30 WRITE of size 1 at 0x7f773fafa249 thread T0 + #0 0x5556b13bdae3 in asan_heap_buffer_overflow ../app/test/test_asan_heap_buffer_overflow.c:25
Please update this example since the unit test has been removed.
Quoted 25 lines
doc/guides/prog_guide/ASan.rst new file +25
-0+ #1 0x5556b043e9d4 in cmd_autotest_parsed ../app/test/commands.c:71 + #2 0x5556b1cdd4b0 in cmdline_parse ../lib/cmdline/cmdline_parse.c:290 + #3 0x5556b1cd8987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x5556b1ce477a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x5556b1cd923e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x5556b1cd9769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x5556b045f53b in main ../app/test/test.c:234 + #8 0x7f7f1eba90b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) + #9 0x5556b043e70d in _start (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-test+0x7ce70d) + + Address 0x7f773fafa249 is a wild pointer. + SUMMARY: AddressSanitizer: heap-buffer-overflow ../app/test/test_asan_heap_buffer_overflow.c:25 in asan_heap_buffer_overflow + +Example use-after-free error +---------------------------- + +Following error was reported when ASan was enabled:: + + Applied for 9 bytes of memory, and accessed the first byte after + released, so heap-use-after-free appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0); + if (!p) {
doc/guides/prog_guide/ASan.rst line 75
Quoted 10 lines
doc/guides/prog_guide/ASan.rst new file +10
-0+ printf("rte_zmalloc error."); + return -1; + } + rte_free(p); + *p = 'a'; + +The error log:: + + ==49478==ERROR: AddressSanitizer: heap-use-after-free on address 0x7fe2ffafa240 at pc 0x56409b084bc8 bp 0x7ffef62c57d0 sp 0x7ffef62c57c0 WRITE of size 1 at 0x7fe2ffafa240 thread T0 + #0 0x56409b084bc7 in asan_use_after_free ../app/test/test_asan_use_after_free.c:26
Idem.
Quoted 12 lines
doc/guides/prog_guide/ASan.rst new file +12
-0+ #1 0x56409a1059d4 in cmd_autotest_parsed ../app/test/commands.c:71 + #2 0x56409b9a44b0 in cmdline_parse ../lib/cmdline/cmdline_parse.c:290 + #3 0x56409b99f987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x56409b9ab77a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x56409b9a023e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x56409b9a0769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x56409a12653b in main ../app/test/test.c:234 + #8 0x7feafafc20b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) + #9 0x56409a10570d in _start (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-test+0x7ce70d) + + Address 0x7fe2ffafa240 is a wild pointer. + SUMMARY: AddressSanitizer: heap-use-after-free ../app/test/test_asan_use_after_free.c:26 in asan_use_after_free
Signature · 1 line
David Marchand
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
Quoted 37 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, September 30, 2021 4:20 PM
To: Peng, ZhihongX <[email protected]>; Richardson, Bruce <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKHello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my fault.
Quoted 8 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
Quoted 6 lines
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.This patch mixes how to use ASan and instrumenting the DPDK mem allocator.
Quoted 7 lines
I would split this patch in two.
The first patch can add the documentation on enabling/using ASan and describe the known issues on enabling it.
I'd find it better (from a user pov) if we hide all those details aboutb_lundefand installation of libasan on Centos.
V7 version will be modified.
Quoted 25 lines
Something like (only quickly tested):
config/meson.build index renamed from config/meson.build +0
-0
4cdf589e20..7d8b71da79100644config/meson.build +10
-0@@ -411,6 +411,33 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
Quoted 1 line
Bruce, do you see an issue with this approach?
It seems clearer to get the ASan switch in the c code.
Quoted 75 lines
Then a second patch adds the
rte_mallocinstrumentation, with a check at configuration time.endif
add_project_link_arguments('-lasan', language: 'c')dpdk_extra_ldflags+= '-lasan'config/meson.build +18
-0+ if arch_subdir == 'x86' + asan_check_code = ''' +#ifdef __SANITIZE_ADDRESS__ +#define RTE_MALLOC_ASAN +#elif defined(__has_feature) +# if __has_feature(address_sanitizer) +#define RTE_MALLOC_ASAN +# endif +#endif + +#ifndef RTE_MALLOC_ASAN +#error ASan not available. +#endif +''' + if cc.compiles(asan_check_code) + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif + endif endif if get_option('default_library') == 'both'Few more comments:
Quoted 29 lines
DPDK ASan functionality is currently only supported Linux
x86_64. Support other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>Quoted 7 lines
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 108 +++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + examples/helloworld/main.c | 5 + lib/eal/common/malloc_elem.c | 26 +++- lib/eal/common/malloc_elem.h | 204+++++++++++++++++++++++++++++++-
Quoted 5 lines
lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 8 files changed, 361 insertions(+), 5 deletions(-) create mode100644
doc/guides/prog_guide/ASan.rst
Quoted 12 lines
devtools/words-case.txt VXLAN · line 89
Quoted 9 lines
devtools/words-case.txt index renamed from devtools/words-case.txt +0
-0
0bbad48626..3655596d47100644devtools/words-case.txt +1
-0@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASanAlphabetical order please.
V7 version will be modified.
Quoted 4 lines
doc/guides/prog_guide/ASan.rst
Quoted 2 lines
doc/guides/prog_guide/ASan.rst +0
-0Filenames are lowercase in the doc.
V7 version will be modified.
Quoted 13 lines
doc/guides/prog_guide/ASan.rst line 4
Quoted 11 lines
new file mode 100644
index 0000000000..7145a3b1a1doc/guides/prog_guide/ASan.rst new file +4
-0@@ -0,0 +1,108 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(ASan)+=========================================================
=
Quoted 15 lines
doc/guides/prog_guide/ASan.rst line 17
Quoted 13 lines
doc/guides/prog_guide/ASan.rst new file +13
-0+ +AddressSanitizer (ASan) is a google memory error detect standard +tool. It could help to detect use-after-free and +{heap,stack,global}-buffer overflow bugs in C/C++ programs, print +detailed error information when error happens, large improve debug +efficiency. + +By referring to its implementation algorithm +(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm) +, enabled heap-buffer-overflow and use-after-free functions on DPDK. +DPDK ASan function currently only supports on Linux x86_64. + +AddressSanitizer is a part of LLVM(3.1+)and GCC(4.8+).missing spaces around ().
V7 version will be modified.
Quoted 24 lines
Quoted 12 lines
doc/guides/prog_guide/ASan.rst new file +11
-0+ +Example heap-buffer-overflow error +---------------------------------- + +Following error was reported when ASan was enabled:: + + Applied 9 bytes of memory, but accessed the 10th byte of memory, + so heap-buffer-overflow appeared. + +Below code results in this error:: +doc/guides/prog_guide/ASan.rst line 38
Quoted 10 lines
doc/guides/prog_guide/ASan.rst new file +10
-0+ char *p = rte_zmalloc(NULL, 9, 0); + if (!p) { + printf("rte_zmalloc error."); + return -1; + } + p[9] = 'a'; + +The error log:: + + ==49433==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x7f773fafa249at pc0x5556b13bdae4bp0x7ffeb4965e40sp0x7ffeb4965e30WRITE of size 1 at0x7f773fafa249thread T0
Quoted 4 lines
doc/guides/prog_guide/ASan.rst line 40
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #0 0x5556b13bdae3 in asan_heap_buffer_overflow + ../app/test/test_asan_heap_buffer_overflow.c:25Please update this example since the unit test has been removed.
V7 version will be modified.
Quoted 75 lines
doc/guides/prog_guide/ASan.rst line 41
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +1
-0+ #1 0x5556b043e9d4 in
cmd_autotest_parsed../app/test/commands.c:71doc/guides/prog_guide/ASan.rst line 42
Quoted 1 line
doc/guides/prog_guide/ASan.rst new file +1
-0+ #2 0x5556b1cdd4b0 in
cmdline_parse../lib/cmdline/cmdline_parse.c:290doc/guides/prog_guide/ASan.rst line 48
Quoted 6 lines
doc/guides/prog_guide/ASan.rst new file +6
-0+ #3 0x5556b1cd8987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x5556b1ce477a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x5556b1cd923e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x5556b1cd9769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x5556b045f53b in main ../app/test/test.c:234 + #8 0x7f7f1eba90b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/ASan.rst line 50
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #9 0x5556b043e70d in _start + (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-
test+0x7ce70Quoted 18 lines
doc/guides/prog_guide/ASan.rst new file +18
-0+ d) + + Address 0x7f773fafa249 is a wild pointer. + SUMMARY: AddressSanitizer: heap-buffer-overflow + ../app/test/test_asan_heap_buffer_overflow.c:25 in + asan_heap_buffer_overflow + +Example use-after-free error +---------------------------- + +Following error was reported when ASan was enabled:: + + Applied for 9 bytes of memory, and accessed the first byte after + released, so heap-use-after-free appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0);doc/guides/prog_guide/ASan.rst line 78
Quoted 10 lines
doc/guides/prog_guide/ASan.rst new file +10
-0+ if (!p) { + printf("rte_zmalloc error."); + return -1; + } + rte_free(p); + *p = 'a'; + +The error log:: + + ==49478==ERROR: AddressSanitizer: heap-use-after-free on address
0x7fe2ffafa240at pc0x56409b084bc8bp0x7ffef62c57d0sp0x7ffef62c57c0WRITE of size 1 at0x7fe2ffafa240thread T0doc/guides/prog_guide/ASan.rst line 80
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #0 0x56409b084bc7 in asan_use_after_free + ../app/test/test_asan_use_after_free.c:26Idem.
doc/guides/prog_guide/ASan.rst line 81
Quoted 1 line
doc/guides/prog_guide/ASan.rst new file +1
-0+ #1 0x56409a1059d4 in
cmd_autotest_parsed../app/test/commands.c:71doc/guides/prog_guide/ASan.rst line 82
Quoted 1 line
doc/guides/prog_guide/ASan.rst new file +1
-0+ #2 0x56409b9a44b0 in
cmdline_parse../lib/cmdline/cmdline_parse.c:290doc/guides/prog_guide/ASan.rst line 84
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #3 0x56409b99f987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x56409b9ab77a in
rdline_char_in../lib/cmdline/cmdline_rdline.c:421doc/guides/prog_guide/ASan.rst line 88
Quoted 4 lines
doc/guides/prog_guide/ASan.rst new file +4
-0+ #5 0x56409b9a023e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x56409b9a0769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x56409a12653b in main ../app/test/test.c:234 + #8 0x7feafafc20b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/ASan.rst line 90
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #9 0x56409a10570d in _start + (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-
test+0x7ce70doc/guides/prog_guide/ASan.rst line 95
Quoted 5 lines
doc/guides/prog_guide/ASan.rst new file +5
-0+ d) + + Address 0x7fe2ffafa240 is a wild pointer. + SUMMARY: AddressSanitizer: heap-use-after-free + ../app/test/test_asan_use_after_free.c:26 in asan_use_after_free--
David Marchand
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
Quoted 18 lines · ZhihongX
Quoted 18 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, September 30, 2021 4:20 PM
To: Peng, ZhihongX <[email protected]>; Richardson, Bruce <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKHello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my fault.
[snip]
Quoted 2 lines
Quoted 1 line
V7 version will be modified.
Ok, let's forget about *v6 bis* and go with v7...
Quoted 26 lines
Quoted 26 lines
Something like (only quickly tested):
config/meson.build index renamed from config/meson.build +0
-0
4cdf589e20..7d8b71da79100644config/meson.build +10
-0@@ -411,6 +411,33 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
Quoted 3 lines
Quoted 1 line
Bruce, do you see an issue with this approach?
It seems clearer to get the ASan switch in the c code.
This is a feature the developper asked for at configuration time. We have other condition to fulfill to get ASan linking correctly (wrt lundef workaround and presence of libasan on Centos/RHEL 7).
It's not a matter of being clearer (which I fail to see how it is), it's a matter of putting the check at the right place.
Signature · 1 line
David Marchand
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
Quoted 71 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Friday, October 8, 2021 4:30 PM
To: Peng, ZhihongX <[email protected]>; Richardson, Bruce <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKQuoted 18 lines · ZhihongX
Quoted 18 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, September 30, 2021 4:20 PM
To: Peng, ZhihongX <[email protected]>; Richardson, Bruce <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKHello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my fault.
[snip]
Quoted 2 lines
Quoted 1 line
V7 version will be modified.
Ok, let's forget about *v6 bis* and go with v7...
Quoted 26 lines
Quoted 26 lines
Something like (only quickly tested):
config/meson.build index renamed from config/meson.build +0
-0
4cdf589e20..7d8b71da79100644config/meson.build +10
-0@@ -411,6 +411,33 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.Quoted 3 lines
Quoted 1 line
Bruce, do you see an issue with this approach?
It seems clearer to get the ASan switch in the c code.
Quoted 6 lines
This is a feature the developper asked for at configuration time. We have other condition to fulfill to get ASan linking correctly (wrt lundef workaround and presence of libasan on Centos/RHEL 7).
It's not a matter of being clearer (which I fail to see how it is), it's a matter of putting the check at the right place.
The v9 version will be fixed.
Quoted 3 lines
--
David Marchand
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
In reply to David MarchandQuoted 52 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, September 30, 2021 4:20 PM
To: Peng, ZhihongX <[email protected]>; Richardson, Bruce <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKHello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my fault.
Quoted 8 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
Quoted 6 lines
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.This patch mixes how to use ASan and instrumenting the DPDK mem allocator.
I would split this patch in two.
The first patch can add the documentation on enabling/using ASan and describe the known issues on enabling it.
I'd find it better (from a user pov) if we hide all those details aboutb_lundefand installation of libasan on Centos.Something like (only quickly tested):
config/meson.build index renamed from config/meson.build +0
-0
4cdf589e20..7d8b71da79100644config/meson.build +0
-0@@ -411,6 +411,33 @@ if get_option('b_lto') endif
config/meson.build if get_option('b_lto') · line 421
Quoted 10 lines
config/meson.build +8
-0endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan'
No need to add code:add_project_link_arguments('-lasan', language: 'c') dpdk_extra_ldflags += '-lasan'
The app compiled by clang will fail to run
Quoted 22 lines
config/meson.build +2
-0+endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.Bruce, do you see an issue with this approach?
Then a second patch adds the
rte_mallocinstrumentation, with a check at configuration time.endif
add_project_link_arguments('-lasan', language: 'c')dpdk_extra_ldflags+= '-lasan'config/meson.build +6
-0+ if arch_subdir == 'x86' + asan_check_code = ''' +#ifdef __SANITIZE_ADDRESS__ +#define RTE_MALLOC_ASAN +#elif defined(__has_feature) +# if __has_feature(address_sanitizer)
config/meson.build
Quoted 10 lines
config/meson.build +10
-0+#define RTE_MALLOC_ASAN +# endif +#endif + +#ifndef RTE_MALLOC_ASAN +#error ASan not available. +#endif +''' + if cc.compiles(asan_check_code) + dpdk_conf.set10('RTE_MALLOC_ASAN', true)
dpdk_conf.set10('RTE_MALLOC_ASAN', true) is not executed
config/meson.build if get_option('b_lto')
Quoted 3 lines
config/meson.build +2
-0+ endif + endif endif
Set the macro directly:dpdk_conf.set10('RTE_MALLOC_ASAN', true)
All code:
if get_option('b_sanitize') == 'address'
asan_dep = cc.find_library('asan', required: true)
if (not cc.links('int main(int argc, char *argv[]) { return 0; }',
dependencies: asan_dep))
error('broken dependency, "libasan"')
endif
if arch_subdir == 'x86'
dpdk_conf.set10('RTE_MALLOC_ASAN', true)
endifendif
Quoted 199 lines
if
get_option('default_library')== 'both'Few more comments:
Quoted 29 lines
DPDK ASan functionality is currently only supported Linux
x86_64. Support other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>Quoted 7 lines
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 108 +++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + examples/helloworld/main.c | 5 + lib/eal/common/malloc_elem.c | 26 +++- lib/eal/common/malloc_elem.h | 204+++++++++++++++++++++++++++++++-
Quoted 10 lines
lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 8 files changed, 361 insertions(+), 5 deletions(-) create mode100644
doc/guides/prog_guide/ASan.rstdevtools/words-case.txt index renamed from devtools/words-case.txt +0
-0
0bbad48626..3655596d47100644devtools/words-case.txt VXLAN · line 89
Quoted 4 lines
devtools/words-case.txt +1
-0@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASanAlphabetical order please.
doc/guides/prog_guide/ASan.rst
Quoted 2 lines
doc/guides/prog_guide/ASan.rst +0
-0Filenames are lowercase in the doc.
doc/guides/prog_guide/ASan.rst line 4
Quoted 10 lines
new file mode 100644
index 0000000000..7145a3b1a1doc/guides/prog_guide/ASan.rst new file +4
-0@@ -0,0 +1,108 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(ASan)+=========================================================
=doc/guides/prog_guide/ASan.rst line 17
Quoted 13 lines
doc/guides/prog_guide/ASan.rst new file +13
-0+ +AddressSanitizer (ASan) is a google memory error detect standard +tool. It could help to detect use-after-free and +{heap,stack,global}-buffer overflow bugs in C/C++ programs, print +detailed error information when error happens, large improve debug +efficiency. + +By referring to its implementation algorithm +(https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm) +, enabled heap-buffer-overflow and use-after-free functions on DPDK. +DPDK ASan function currently only supports on Linux x86_64. + +AddressSanitizer is a part of LLVM(3.1+)and GCC(4.8+).missing spaces around ().
Quoted 11 lines
doc/guides/prog_guide/ASan.rst new file +11
-0+ +Example heap-buffer-overflow error +---------------------------------- + +Following error was reported when ASan was enabled:: + + Applied 9 bytes of memory, but accessed the 10th byte of memory, + so heap-buffer-overflow appeared. + +Below code results in this error:: +doc/guides/prog_guide/ASan.rst line 38
Quoted 10 lines
doc/guides/prog_guide/ASan.rst new file +10
-0+ char *p = rte_zmalloc(NULL, 9, 0); + if (!p) { + printf("rte_zmalloc error."); + return -1; + } + p[9] = 'a'; + +The error log:: + + ==49433==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x7f773fafa249at pc0x5556b13bdae4bp0x7ffeb4965e40sp0x7ffeb4965e30WRITE of size 1 at0x7f773fafa249thread T0doc/guides/prog_guide/ASan.rst line 40
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #0 0x5556b13bdae3 in asan_heap_buffer_overflow + ../app/test/test_asan_heap_buffer_overflow.c:25Please update this example since the unit test has been removed.
doc/guides/prog_guide/ASan.rst line 41
Quoted 1 line
doc/guides/prog_guide/ASan.rst new file +1
-0+ #1 0x5556b043e9d4 in
cmd_autotest_parsed../app/test/commands.c:71doc/guides/prog_guide/ASan.rst line 42
Quoted 1 line
doc/guides/prog_guide/ASan.rst new file +1
-0+ #2 0x5556b1cdd4b0 in
cmdline_parse../lib/cmdline/cmdline_parse.c:290doc/guides/prog_guide/ASan.rst line 48
Quoted 6 lines
doc/guides/prog_guide/ASan.rst new file +6
-0+ #3 0x5556b1cd8987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x5556b1ce477a in rdline_char_in ../lib/cmdline/cmdline_rdline.c:421 + #5 0x5556b1cd923e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x5556b1cd9769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x5556b045f53b in main ../app/test/test.c:234 + #8 0x7f7f1eba90b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/ASan.rst line 50
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #9 0x5556b043e70d in _start + (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-
test+0x7ce70Quoted 18 lines
doc/guides/prog_guide/ASan.rst new file +18
-0+ d) + + Address 0x7f773fafa249 is a wild pointer. + SUMMARY: AddressSanitizer: heap-buffer-overflow + ../app/test/test_asan_heap_buffer_overflow.c:25 in + asan_heap_buffer_overflow + +Example use-after-free error +---------------------------- + +Following error was reported when ASan was enabled:: + + Applied for 9 bytes of memory, and accessed the first byte after + released, so heap-use-after-free appeared. + +Below code results in this error:: + + char *p = rte_zmalloc(NULL, 9, 0);doc/guides/prog_guide/ASan.rst line 78
Quoted 10 lines
doc/guides/prog_guide/ASan.rst new file +10
-0+ if (!p) { + printf("rte_zmalloc error."); + return -1; + } + rte_free(p); + *p = 'a'; + +The error log:: + + ==49478==ERROR: AddressSanitizer: heap-use-after-free on address
0x7fe2ffafa240at pc0x56409b084bc8bp0x7ffef62c57d0sp0x7ffef62c57c0WRITE of size 1 at0x7fe2ffafa240thread T0doc/guides/prog_guide/ASan.rst line 80
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #0 0x56409b084bc7 in asan_use_after_free + ../app/test/test_asan_use_after_free.c:26Idem.
doc/guides/prog_guide/ASan.rst line 81
Quoted 1 line
doc/guides/prog_guide/ASan.rst new file +1
-0+ #1 0x56409a1059d4 in
cmd_autotest_parsed../app/test/commands.c:71doc/guides/prog_guide/ASan.rst line 82
Quoted 1 line
doc/guides/prog_guide/ASan.rst new file +1
-0+ #2 0x56409b9a44b0 in
cmdline_parse../lib/cmdline/cmdline_parse.c:290doc/guides/prog_guide/ASan.rst line 84
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #3 0x56409b99f987 in cmdline_valid_buffer ../lib/cmdline/cmdline.c:26 + #4 0x56409b9ab77a in
rdline_char_in../lib/cmdline/cmdline_rdline.c:421doc/guides/prog_guide/ASan.rst line 88
Quoted 4 lines
doc/guides/prog_guide/ASan.rst new file +4
-0+ #5 0x56409b9a023e in cmdline_in ../lib/cmdline/cmdline.c:149 + #6 0x56409b9a0769 in cmdline_interact ../lib/cmdline/cmdline.c:223 + #7 0x56409a12653b in main ../app/test/test.c:234 + #8 0x7feafafc20b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/ASan.rst line 90
Quoted 2 lines
doc/guides/prog_guide/ASan.rst new file +2
-0+ #9 0x56409a10570d in _start + (/home/pzh/yyy/x86_64-native-linuxapp-gcc/app/test/dpdk-
test+0x7ce70doc/guides/prog_guide/ASan.rst line 95
Quoted 5 lines
doc/guides/prog_guide/ASan.rst new file +5
-0+ d) + + Address 0x7fe2ffafa240 is a wild pointer. + SUMMARY: AddressSanitizer: heap-use-after-free + ../app/test/test_asan_use_after_free.c:26 in asan_use_after_free--
David Marchand
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
In reply to David MarchandQuoted 56 lines · David Marchand
Hello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my fault.
Quoted 9 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
Quoted 6 lines
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.This patch mixes how to use ASan and instrumenting the DPDK mem allocator.
I would split this patch in two.
The first patch can add the documentation on enabling/using ASan and describe the known issues on enabling it.
I'd find it better (from a user pov) if we hide all those details aboutb_lundefand installation of libasan on Centos.Something like (only quickly tested):
config/meson.build +10
-0@@ -411,6 +411,33 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
Quoted 2 lines
Bruce, do you see an issue with this approach?
Apologies for delayed reply on this.
No issue with this approach on my end, seems reasonable. Just watch out that b_sanitize can have "address,undefined" as a possible value, so if we want to support that, we can't just check directly for the literal string "address"
Quoted 17 lines
Then a second patch adds the
rte_mallocinstrumentation, with a check at configuration time.endif
add_project_link_arguments('-lasan', language: 'c')dpdk_extra_ldflags+= '-lasan'config/meson.build +9
-0+ if arch_subdir == 'x86' + asan_check_code = ''' +#ifdef __SANITIZE_ADDRESS__ +#define RTE_MALLOC_ASAN +#elif defined(__has_feature) +# if __has_feature(address_sanitizer) +#define RTE_MALLOC_ASAN +# endif +#endif
config/meson.build
Quoted 10 lines
config/meson.build +9
-0+ +#ifndef RTE_MALLOC_ASAN +#error ASan not available. +#endif +''' + if cc.compiles(asan_check_code) + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif + endif endif
Apologies, but I haven't been tracking this set in much detail. Do we really need this second configuration check? Should it, or could it, be merged into the check above for the asan library presence?
/Bruce
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
Quoted 95 lines
-----Original Message-----
From: Richardson, Bruce <[email protected]>
Sent: Wednesday, October 13, 2021 4:00 PM
To: David Marchand <[email protected]>
Cc: Peng, ZhihongX <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKQuoted 3 lines · David Marchand
Hello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my
fault.
Quoted 12 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
Quoted 9 lines
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
Quoted 8 lines
Quoted 6 lines
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.This patch mixes how to use ASan and instrumenting the DPDK mem
allocator.
Quoted 33 lines
I would split this patch in two.
The first patch can add the documentation on enabling/using ASan and describe the known issues on enabling it.
I'd find it better (from a user pov) if we hide all those details aboutb_lundefand installation of libasan on Centos.Something like (only quickly tested):
config/meson.build index renamed from config/meson.build +0
-0
4cdf589e20..7d8b71da79100644config/meson.build +10
-0@@ -411,6 +411,33 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.Quoted 2 lines
Bruce, do you see an issue with this approach?
Apologies for delayed reply on this.
No issue with this approach on my end, seems reasonable. Just watch out that
b_sanitizecan have "address,undefined" as a possible value, so if we want to support that, we can't just check directly for the literal string "address"config/meson.build
Quoted 13 lines
Then a second patch adds the
rte_mallocinstrumentation, with a check at configuration time.endif
add_project_link_arguments('-lasan', language: 'c')dpdk_extra_ldflags+= '-lasan'config/meson.build +6
-0+ if arch_subdir == 'x86' + asan_check_code = ''' +#ifdef __SANITIZE_ADDRESS__ +#define RTE_MALLOC_ASAN +#elif defined(__has_feature) +# if __has_feature(address_sanitizer) #define RTE_MALLOC_ASAN #endif
Quoted 17 lines
config/meson.build
Quoted 12 lines
config/meson.build +10
-0+#endif + +#ifndef RTE_MALLOC_ASAN +#error ASan not available. +#endif +''' + if cc.compiles(asan_check_code) + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif + endif endifApologies, but I haven't been tracking this set in much detail. Do we really need this second configuration check? Should it, or could it, be merged into the check above for the asan library presence?
All code:
if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'undefined'
if cc.get_id() == 'gcc'
asan_dep = cc.find_library('asan', required: true)
if (not cc.links('int main(int argc, char *argv[]) { return 0; }',
dependencies: asan_dep))
error('broken dependency, "libasan"')
endif
endif
if exec_env == 'linux' and arch_subdir == 'x86'
dpdk_conf.set10('RTE_MALLOC_ASAN', true)
endifendif
Bruce, is this code correct?
Thanks!
Quoted 1 line
/Bruce
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
Quoted 141 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Peng, ZhihongX
Sent: Thursday, October 14, 2021 2:34 PM
To: Richardson, Bruce <[email protected]>; David Marchand <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKQuoted 12 lines
-----Original Message-----
From: Richardson, Bruce <[email protected]>
Sent: Wednesday, October 13, 2021 4:00 PM
To: David Marchand <[email protected]>
Cc: Peng, ZhihongX <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDKQuoted 24 lines
Quoted 4 lines · David Marchand
Hello,
I see v6 is superseded in pw, I have been cleaning my queue... maybe my
fault.
Quoted 18 lines · On Thu, Sep 30, 2021 at 7:37 AM <[email protected]> wrote:
Quoted 15 lines
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`
(ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error isdetected.
Quoted 60 lines
Quoted 2 lines
This patch mixes how to use ASan and instrumenting the DPDK mem
allocator.
Quoted 33 lines
I would split this patch in two.
The first patch can add the documentation on enabling/using ASan and describe the known issues on enabling it.
I'd find it better (from a user pov) if we hide all those details aboutb_lundefand installation of libasan on Centos.Something like (only quickly tested):
config/meson.build index renamed from config/meson.build +0
-0
4cdf589e20..7d8b71da79100644config/meson.build +10
-0@@ -411,6 +411,33 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.Quoted 2 lines
Bruce, do you see an issue with this approach?
Apologies for delayed reply on this.
No issue with this approach on my end, seems reasonable. Just watch out that
b_sanitizecan have "address,undefined" as a possible value, so if we want to support that, we can't just check directly for the literal string "address"config/meson.build
Quoted 13 lines
Then a second patch adds the
rte_mallocinstrumentation, with a check at configuration time.endif
add_project_link_arguments('-lasan', language: 'c')dpdk_extra_ldflags+= '-lasan'config/meson.build +6
-0+ if arch_subdir == 'x86' + asan_check_code = ''' +#ifdef __SANITIZE_ADDRESS__ +#define RTE_MALLOC_ASAN +#elif defined(__has_feature) +# if __has_feature(address_sanitizer) #define RTE_MALLOC_ASAN #endif
Quoted 17 lines
config/meson.build
Quoted 12 lines
config/meson.build +10
-0+#endif + +#ifndef RTE_MALLOC_ASAN +#error ASan not available. +#endif +''' + if cc.compiles(asan_check_code) + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif + endif endifApologies, but I haven't been tracking this set in much detail. Do we really need this second configuration check? Should it, or could it, be merged into the check above for the asan library presence?
All code:
ifget_option('b_sanitize')== 'address' orget_option('b_sanitize')== 'undefined'if cc.get_id() == 'gcc' asan_dep = cc.find_library('asan', required: true) if (not cc.links('int main(int argc, char *argv[]) { return 0; }', dependencies: asan_dep)) error('broken dependency, "libasan"') endif endif if exec_env == 'linux' and arch_subdir == 'x86' dpdk_conf.set10('RTE_MALLOC_ASAN', true) endif
Quoted 2 lines
endif
Modify:
if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'undefined'
if cc.get_id() == 'gcc'
asan_dep = cc.find_library('asan', required: true)
if (not cc.links('int main(int argc, char *argv[]) { return 0; }',
dependencies: asan_dep))
error('broken dependency, "libasan"')
endif
endif
if get_option('b_sanitize') == 'address'
if exec_env == 'linux' and arch_subdir == 'x86'
dpdk_conf.set10('RTE_MALLOC_ASAN', true)
endif
endifendif
Quoted 3 lines
Bruce, is this code correct?
Thanks!Quoted 1 line
/Bruce
PATCH v6 1/2 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 112 ++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 +++- lib/eal/common/malloc_elem.h | 204 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 7 files changed, 360 insertions(+), 5 deletions(-) create mode 100644 doc/guides/prog_guide/ASan.rst
devtools/words-case.txt +1 -0
@@ -86,3 +86,4 @@ VXLAN Windows XDP XOR +ASan
doc/guides/prog_guide/ASan.rst new file +112 -0
112 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + ASan
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +201 -3
233 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v6 2/2 lib/pipeline: Fix compilation error with gcc ASan
From: Zhihong Peng <[email protected]>
After adding ASan, the gcc compilation check will be stricter. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
In reply to [email protected]Quoted 41 lines · On 30-Sep-21 1:59 PM, [email protected] wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.DPDK ASan functionality is currently only supported Linux
x86_64. Support other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
Quoted 3 lines
Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
Signature · 2 lines
Thanks, Anatoly
Re: [dpdk-dev] [PATCH v6 1/2] Enable ASan for memory detector on DPDK
In reply to [email protected]Quoted 44 lines · On Thu, Sep 30, 2021 at 3:09 PM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.DPDK ASan functionality is currently only supported Linux
x86_64. Support other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document.Here is an example of heap-buffer-overflow bug:
...... char *p = rte_zmalloc(NULL, 7, 0); p[7] = 'a'; ......Here is an example of use-after-free bug:
...... char *p = rte_zmalloc(NULL, 7, 0); rte_free(p); *p = 'a'; ......If you want to use this feature,
you need to add below compilation options when compiling code:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>
Quoted 11 lines
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/ASan.rst | 112 ++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + lib/eal/common/malloc_elem.c | 26 +++- lib/eal/common/malloc_elem.h | 204 +++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 7 files changed, 360 insertions(+), 5 deletions(-) create mode 100644 doc/guides/prog_guide/ASan.rst
I suppose this makes it a v7 since I can see differences when comparing with "previous" v6 series.
Please pay attention to versioning and add a changelog when submitting a new revision.
Plus, I had comments on v6:
https://inbox.dpdk.org/dev/CAJFAV8yzYJtwpnx+jsaB+X7q7POT86uKC3RS-FB9t7p=kTyGDw@mail.gmail.com/
https://inbox.dpdk.org/dev/CAJFAV8w8Zj5xP+giZtYCUz=4ekuFRDW5Niys9uM6xvAW0kteQg@mail.gmail.com/
Thanks.
Signature · 1 line
David Marchand
PATCH v7 1/3 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 100 ++++++++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + 3 files changed, 102 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +100 -0
100 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
Signature · 1 line
2.25.1
PATCH v7 2/3 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
--- lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 204 ++++++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 +++ lib/eal/common/rte_malloc.c | 9 +- 4 files changed, 246 insertions(+), 5 deletions(-)
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +201 -3
233 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v7 3/3 lib/pipeline: Fix compilation error with gcc ASan
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v8 1/3 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
AddressSanitizer (ASan) is a google memory error detect standard tool. It could help to detect use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs, print detailed error information when error happens, large improve debug efficiency.
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
--- devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 100 ++++++++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + 3 files changed, 102 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +100 -0
100 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
Signature · 1 line
2.25.1
PATCH v8 2/3 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document.
--- lib/eal/common/malloc_elem.c | 26 ++++- lib/eal/common/malloc_elem.h | 204 ++++++++++++++++++++++++++++++++++- lib/eal/common/malloc_heap.c | 12 +++ lib/eal/common/rte_malloc.c | 9 +- 4 files changed, 246 insertions(+), 5 deletions(-)
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +201 -3
233 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v8 3/3 lib/pipeline: Fix compilation error with gcc ASan
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v9 1/3 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
---
v7: 1) Split doc and code into two.
2) Modify asan.rst doc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
--- config/meson.build | 10 +++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 97 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 +++ 5 files changed, 118 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +10 -0
@@ -411,6 +411,16 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +97 -0
97 lines not on this page · raw message
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -141,6 +141,15 @@ New Features * Added tests to validate packets hard expiry. * Added tests to verify tunnel header verification in IPsec inbound. +* **Enable ASan for memory detector on DPDK.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v9 2/3 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
---
v7: Split doc and code into two.
v8: No change.
v9: Modify the definition of RTE_MALLOC_ASAN.
--- config/meson.build | 2 + 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 +- 5 files changed, 238 insertions(+), 5 deletions(-)
config/meson.build +2 -0
@@ -419,6 +419,8 @@ if get_option('b_sanitize') == 'address' error('broken dependency, "libasan"') endif endif + + dpdk_conf.set10('RTE_MALLOC_ASAN', true) endif if get_option('default_library') == 'both'
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v9 2/3] DPDK code adapts to ASan
Quoted 27 lines · On Tue, Oct 12, 2021 at 11:54 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux
x86_64. Support other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document, and configure meson (config/meson.build).Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v7: Split doc and code into two.
v8: No change.
v9: Modify the definition ofRTE_MALLOC_ASAN.--- config/meson.build | 2 + 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 +- 5 files changed, 238 insertions(+), 5 deletions(-)config/meson.build +0
-0
config/meson.build if get_option('b_sanitize') == 'address' · line 423
Quoted 5 lines
config/meson.build +2
-0@@ -419,6 +419,8 @@ if get_option('b_sanitize') == 'address' error('broken dependency, "libasan"') endif endif + + dpdk_conf.set10('RTE_MALLOC_ASAN', true)
The test on architecture == x86_64 is missing.
ARM compilation with ASan is broken.
$ ./devtools/test-meson-builds.sh
...
...
ninja: Entering directory `/home/dmarchan/builds/build-arm64-bluefield'
[0/1] Regenerating build files.
The Meson build system
Version: 0.59.0
Source dir: /home/dmarchan/dpdk
Build dir: /home/dmarchan/builds/build-arm64-bluefield
...
[2/2784] Compiling C object lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o
FAILED: lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o
aarch64-linux-gnu-gcc -Ilib/librte_eal.a.p -Ilib -I../../dpdk/lib -I.
-I../../dpdk -Iconfig -I../../dpdk/config -Ilib/eal/include
-I../../dpdk/lib/eal/include -Ilib/eal/linux/include
-I../../dpdk/lib/eal/linux/include -Ilib/eal/arm/include
-I../../dpdk/lib/eal/arm/include -Ilib/eal/common
-I../../dpdk/lib/eal/common -Ilib/eal -I../../dpdk/lib/eal
-Ilib/kvargs -I../../dpdk/lib/kvargs -Ilib/metrics
-I../../dpdk/lib/metrics -Ilib/telemetry -I../../dpdk/lib/telemetry
-fdiagnostics-color=always -fsanitize=address -fno-omit-frame-pointer
-D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Werror -O2 -g -include
rte_config.h -Wextra -Wcast-qual -Wdeprecated -Wformat
-Wformat-nonliteral -Wformat-security -Wmissing-declarations
-Wmissing-prototypes -Wnested-externs -Wold-style-definition
-Wpointer-arith -Wsign-compare -Wstrict-prototypes -Wundef
-Wwrite-strings -Wno-address-of-packed-member -Wno-packed-not-aligned
-Wno-missing-field-initializers -Wno-zero-length-bounds -D_GNU_SOURCE
-fPIC -mcpu=cortex-a72 -DALLOW_EXPERIMENTAL_API -DALLOW_INTERNAL_API
-Wno-format-truncation '-DABI_VERSION="22.0"'
-DRTE_LIBEAL_USE_GETENTROPY -DRTE_LOG_DEFAULT_LOGTYPE=lib.eal -MD -MQ
lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o -MF
lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o.d -o
lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o -c
../../dpdk/lib/eal/common/eal_common_memzone.c
In file included from ../../dpdk/lib/eal/include/rte_log.h:25,
from ../../dpdk/lib/eal/common/eal_common_memzone.c:14:
../../dpdk/lib/eal/common/malloc_elem.h: In function ‘asan_set_zone’:
../../dpdk/lib/eal/common/malloc_elem.h:118:35: error:
‘ASAN_SHADOW_OFFSET’ undeclared (first use in this function); did you
mean ‘ASAN_SHADOW_SCALE’?
118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
| ^~~~~~~~~~~~~~~~~~
../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of
macro ‘RTE_PTR_ADD’
250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
| ^
../../dpdk/lib/eal/common/malloc_elem.h:142:12: note: in expansion of
macro ‘ASAN_MEM_TO_SHADOW’
142 | shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
| ^~~~~~~~~~~~~~~~~~
../../dpdk/lib/eal/common/malloc_elem.h:118:35: note: each undeclared
identifier is reported only once for each function it appears in
118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
| ^~~~~~~~~~~~~~~~~~
../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of
macro ‘RTE_PTR_ADD’
250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
| ^
../../dpdk/lib/eal/common/malloc_elem.h:142:12: note: in expansion of
macro ‘ASAN_MEM_TO_SHADOW’
142 | shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
| ^~~~~~~~~~~~~~~~~~
../../dpdk/lib/eal/common/malloc_elem.h: In function ‘asan_set_redzone’:
../../dpdk/lib/eal/common/malloc_elem.h:118:35: error:
‘ASAN_SHADOW_OFFSET’ undeclared (first use in this function); did you
mean ‘ASAN_SHADOW_SCALE’?
118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
| ^~~~~~~~~~~~~~~~~~
../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of
macro ‘RTE_PTR_ADD’
250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
| ^
../../dpdk/lib/eal/common/malloc_elem.h:196:18: note: in expansion of
macro ‘ASAN_MEM_TO_SHADOW’
196 | front_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
| ^~~~~~~~~~~~~~~~~~
../../dpdk/lib/eal/common/malloc_elem.h: In function ‘asan_clear_redzone’:
../../dpdk/lib/eal/common/malloc_elem.h:118:35: error:
‘ASAN_SHADOW_OFFSET’ undeclared (first use in this function); did you
mean ‘ASAN_SHADOW_SCALE’?
118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
| ^~~~~~~~~~~~~~~~~~
../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of
macro ‘RTE_PTR_ADD’
250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
| ^
../../dpdk/lib/eal/common/malloc_elem.h:235:17: note: in expansion of
macro ‘ASAN_MEM_TO_SHADOW’
235 | head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
| ^~~~~~~~~~~~~~~~~~
[31/2784] Compiling C object lib/librte_eal.a.p/eal_linux_eal_interrupts.c.o
ninja: build stopped: subcommand failed.Signature · 7 lines
David Marchand -- David Marchand
Re: [dpdk-dev] [PATCH v9 2/3] DPDK code adapts to ASan
Quoted 135 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, October 14, 2021 12:45 AM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>; Richardson, Bruce <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [PATCH v9 2/3] DPDK code adapts to ASanQuoted 10 lines · On Tue, Oct 12, 2021 at 11:54 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux
x86_64. Support other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document, and configure meson (config/meson.build).Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>Quoted 8 lines
---
v7: Split doc and code into two.
v8: No change.
v9: Modify the definition ofRTE_MALLOC_ASAN.--- config/meson.build | 2 + lib/eal/common/malloc_elem.c | 26 ++++-
lib/eal/common/malloc_elem.h| 194++++++++++++++++++++++++++++++++++-
Quoted 9 lines
lib/eal/common/malloc_heap.c | 12 +++ lib/eal/common/rte_malloc.c | 9 +- 5 files changed, 238 insertions(+), 5 deletions(-)config/meson.build index renamed from config/meson.build +0
-0
5170b79fed..1ae9390e68100644config/meson.build if get_option('b_sanitize') == 'address' · line 423
Quoted 5 lines
config/meson.build +2
-0@@ -419,6 +419,8 @@ if get_option('b_sanitize') == 'address' error('broken dependency, "libasan"') endif endif + + dpdk_conf.set10('RTE_MALLOC_ASAN', true)The test on architecture ==
x86_64is missing.
ARM compilation with ASan is broken.$ ./devtools/test-meson-builds.sh ... ... ninja: Entering directory `/home/dmarchan/builds/build-arm64-bluefield' [0/1] Regenerating build files. The Meson build system Version: 0.59.0 Source dir: /home/dmarchan/dpdk Build dir: /home/dmarchan/builds/build-arm64-bluefield ... [2/2784] Compiling C object lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o FAILED: lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o aarch64-linux-gnu-gcc -Ilib/librte_eal.a.p -Ilib -I../../dpdk/lib -I. -I../../dpdk -Iconfig -I../../dpdk/config -Ilib/eal/include - I../../dpdk/lib/eal/include -Ilib/eal/linux/include - I../../dpdk/lib/eal/linux/include -Ilib/eal/arm/include - I../../dpdk/lib/eal/arm/include -Ilib/eal/common - I../../dpdk/lib/eal/common -Ilib/eal -I../../dpdk/lib/eal -Ilib/kvargs - I../../dpdk/lib/kvargs -Ilib/metrics -I../../dpdk/lib/metrics -Ilib/telemetry - I../../dpdk/lib/telemetry -fdiagnostics-color=always -fsanitize=address -fno- omit-frame-pointer -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Werror -O2 -g -include rte_config.h -Wextra -Wcast-qual -Wdeprecated -Wformat -Wformat- nonliteral -Wformat-security -Wmissing-declarations -Wmissing-prototypes - Wnested-externs -Wold-style-definition -Wpointer-arith -Wsign-compare - Wstrict-prototypes -Wundef -Wwrite-strings -Wno-address-of-packed- member -Wno-packed-not-aligned -Wno-missing-field-initializers -Wno- zero-length-bounds -D_GNU_SOURCE -fPIC -mcpu=cortex-a72 - DALLOW_EXPERIMENTAL_API -DALLOW_INTERNAL_API -Wno-format- truncation '-DABI_VERSION="22.0"' -DRTE_LIBEAL_USE_GETENTROPY -DRTE_LOG_DEFAULT_LOGTYPE=lib.eal - MD -MQ lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o -MF lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o.d -o lib/librte_eal.a.p/eal_common_eal_common_memzone.c.o - c ../../dpdk/lib/eal/common/eal_common_memzone.c In file included from ../../dpdk/lib/eal/include/rte_log.h:25, from ../../dpdk/lib/eal/common/eal_common_memzone.c:14: ../../dpdk/lib/eal/common/malloc_elem.h: In function ‘asan_set_zone’: ../../dpdk/lib/eal/common/malloc_elem.h:118:35: error: ‘ASAN_SHADOW_OFFSET’ undeclared (first use in this function); did you mean ‘ASAN_SHADOW_SCALE’? 118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET) | ^~~~~~~~~~~~~~~~~~ ../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of macro ‘RTE_PTR_ADD’ 250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x))) | ^ ../../dpdk/lib/eal/common/malloc_elem.h:142:12: note: in expansion of macro ‘ASAN_MEM_TO_SHADOW’ 142 | shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset); | ^~~~~~~~~~~~~~~~~~ ../../dpdk/lib/eal/common/malloc_elem.h:118:35: note: each undeclared identifier is reported only once for each function it appears in 118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET) | ^~~~~~~~~~~~~~~~~~ ../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of macro ‘RTE_PTR_ADD’ 250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x))) | ^ ../../dpdk/lib/eal/common/malloc_elem.h:142:12: note: in expansion of macro ‘ASAN_MEM_TO_SHADOW’ 142 | shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset); | ^~~~~~~~~~~~~~~~~~ ../../dpdk/lib/eal/common/malloc_elem.h: In function ‘asan_set_redzone’: ../../dpdk/lib/eal/common/malloc_elem.h:118:35: error: ‘ASAN_SHADOW_OFFSET’ undeclared (first use in this function); did you mean ‘ASAN_SHADOW_SCALE’? 118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET) | ^~~~~~~~~~~~~~~~~~ ../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of macro ‘RTE_PTR_ADD’ 250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x))) | ^ ../../dpdk/lib/eal/common/malloc_elem.h:196:18: note: in expansion of macro ‘ASAN_MEM_TO_SHADOW’ 196 | front_shadow = ASAN_MEM_TO_SHADOW(head_redzone); | ^~~~~~~~~~~~~~~~~~ ../../dpdk/lib/eal/common/malloc_elem.h: In function ‘asan_clear_redzone’: ../../dpdk/lib/eal/common/malloc_elem.h:118:35: error: ‘ASAN_SHADOW_OFFSET’ undeclared (first use in this function); did you mean ‘ASAN_SHADOW_SCALE’? 118 | RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET) | ^~~~~~~~~~~~~~~~~~ ../../dpdk/lib/eal/include/rte_common.h:250:58: note: in definition of macro ‘RTE_PTR_ADD’
Quoted 8 lines
250 | #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
| ^
../../dpdk/lib/eal/common/malloc_elem.h:235:17: note: in expansion of
macro ‘ASAN_MEM_TO_SHADOW’
235 | head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
| ^~~~~~~~~~~~~~~~~~
[31/2784] Compiling C object lib/librte_eal.a.p/eal_linux_eal_interrupts.c.o
ninja: build stopped: subcommand failed.The v10 version will be fixed.
All meson code:
if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined'
if cc.get_id() == 'gcc'
asan_dep = cc.find_library('asan', required: true)
if (not cc.links('int main(int argc, char *argv[]) { return 0; }',
dependencies: asan_dep))
error('broken dependency, "libasan"')
endif
add_project_link_arguments('-lasan', language: 'c')
dpdk_extra_ldflags += '-lasan'
endif
if exec_env == 'linux' and arch_subdir == 'x86'
dpdk_conf.set10('RTE_MALLOC_ASAN', true)
endifendif
Quoted 10 lines
--
David Marchand--
David Marchand
PATCH v9 3/3 pipeline: Fix compilation error with gcc ASan
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
---
v7: no change
v8: no change
v9: Modify the submit log
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v10 1/4 Enable ASan for memory detector on DPDK
From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
--- config/meson.build | 16 ++++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 35 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 +++++++ 5 files changed, 62 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +16 -0
@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined' + if is_windows + error('asan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +35 -0
@@ -0,0 +1,35 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(ASan) +========================================================== + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable ASan in meson build system, use following meson build command: + +Example usage:: + + gcc : meson build -Dbuildtype=debug -Db_sanitize=address + ninja -C build + clang: meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) DPDK test has been completed in ubuntu18.04/ubuntu20.04/redhat8.3. To compile with gcc in + centos, libasan needs to be installed separately. + b) If the program uses cmdline, when a memory bug occurs, need to execute the "stty echo" command.
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -159,6 +159,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan for memory detector on DPDK.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v10 2/4 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......--- config/meson.build | 4 + doc/guides/prog_guide/asan.rst | 68 +++++++++++- 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, 306 insertions(+), 7 deletions(-)
config/meson.build +4 -0
@@ -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' + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'
doc/guides/prog_guide/asan.rst +66 -2
78 lines not on this page · raw message
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v10 3/4 pipeline: Fix compilation error with gcc ASan
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v10 4/4 performance-thread: Fix cross compilation failed
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
--- examples/performance-thread/common/lthread.c | 2 +- examples/performance-thread/common/lthread_cond.c | 4 ++-- examples/performance-thread/common/lthread_mutex.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
examples/performance-thread/common/lthread.c +1 -1
@@ -463,6 +463,6 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); +strncpy (lt->funcname, f,sizeof (lt->funcname) -1 ); lt->funcname[sizeof (lt->funcname)-1 ] =0 ; }
examples/performance-thread/common/lthread_cond.c +2 -2
@@ -57,9 +57,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +strncpy (c->name,"no name" ,sizeof (c->name) -1 );else -strncpy (c->name, name,sizeof (c->name)); +strncpy (c->name, name,sizeof (c->name) -1 ); c->name[sizeof (c->name)-1 ] =0 ; c->root_sched =THIS_SCHED ;
examples/performance-thread/common/lthread_mutex.c +2 -2
@@ -52,9 +52,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +strncpy (m->name,"no name" ,sizeof (m->name) -1 );else -strncpy (m->name, name,sizeof (m->name)); +strncpy (m->name, name,sizeof (m->name) -1 ); m->name[sizeof (m->name)-1 ] =0 ; m->root_sched =THIS_SCHED ;
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v10 1/4] Enable ASan for memory detector on DPDK
In reply to [email protected]Quoted 12 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Friday, October 15, 2021 3:28 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>
Subject: [PATCH v10 1/4] Enable ASan for memory detector on DPDK
Quoted 3 lines
From: Zhihong Peng <[email protected]>
`AddressSanitizer
Good work on this. It is a good initiative. Some comments on the docs below.
Quoted 6 lines
(quoted diff) +0
-0
b/doc/guides/prog_guide/asan.rstnew file mode 100644 index 0000000000..b732288af3
doc/guides/prog_guide/asan.rst line 8
Quoted 8 lines
doc/guides/prog_guide/asan.rst new file +8
-0@@ -0,0 +1,35 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(ASan) +========================================================== + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is
There are a number of minor doc and grammar issues in the rst for this patch. Rather than going through each one here is a rework of the asan.rst with these issues fixed:
.. SPDX-License-Identifier: BSD-3-Clause
Copyright(c) 2021 Intel Corporation
Running Address Sanitizer
==========================================================
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a widely-used debugging tool for detecting memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns and similar errors, as well as printing out detailed debug information whenever an error is detected.
To enable ASan in the meson build system, use following meson build command:
Example usage:
* gcc::
meson build -Dbuildtype=debug -Db_sanitize=address ninja -C build
* clang::
meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address ninja -C build
AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+).
.. Note::
a) To compile with gcc in Centos, libasan needs to be installed separately.
b) If the program being tested uses cmdline you will need to execute the "stty echo" command when a error occurs.
doc/guides/prog_guide/index.rst Programmer's Guide · line 74
Quoted 9 lines
(quoted diff) +0
-0
b/doc/guides/prog_guide/index.rstindex2dce507f46..df8a4b93e1100644doc/guides/prog_guide/index.rst +1
-0@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
The asan file should go before "glossary" and after "profile_app".
doc/guides/rel_notes/release_21_11.rst New Features · line 162
Quoted 10 lines
doc/guides/rel_notes/release_21_11.rst +1
-0@@ -159,6 +159,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan for memory detector on DPDK.**
Change to "**Enable ASan Address Sanitization.**"
doc/guides/rel_notes/release_21_11.rst New Features · line 165
Quoted 3 lines
doc/guides/rel_notes/release_21_11.rst +3
-0+ + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan)
This text should match the edited text above.
John
Re: [dpdk-dev] [PATCH v10 1/4] Enable ASan for memory detector on DPDK
Quoted 53 lines
-----Original Message-----
From: Mcnamara, John <[email protected]>
Sent: Tuesday, October 19, 2021 5:03 PM
To: Peng, ZhihongX <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>
Subject: RE: [PATCH v10 1/4] Enable ASan for memory detector on DPDKQuoted 12 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Friday, October 15, 2021 3:28 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>
Subject: [PATCH v10 1/4] Enable ASan for memory detector on DPDKQuoted 3 lines
From: Zhihong Peng <[email protected]>
`AddressSanitizer
Good work on this. It is a good initiative. Some comments on the docs below.
doc/guides/prog_guide/asan.rst line 4
Quoted 11 lines
(quoted diff) +0
-0
b/doc/guides/prog_guide/asan.rstnew file mode 100644 index 0000000000..b732288af3doc/guides/prog_guide/asan.rst new file +4
-0@@ -0,0 +1,35 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(ASan)+=========================================================
=doc/guides/prog_guide/asan.rst
Quoted 4 lines
doc/guides/prog_guide/asan.rst new file +4
-0+ +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) +isThere are a number of minor doc and grammar issues in the rst for this patch. Rather than going through each one here is a rework of the
asan.rstwith these issues fixed:
Quoted 3 lines
.. SPDX-License-Identifier: BSD-3-Clause
Copyright(c) 2021 Intel Corporation
The v11 version will be fixed.
Quoted 5 lines
Running Address Sanitizer
==========================================================`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a
The v11 version will be fixed.
Quoted 15 lines
widely-used debugging tool for detecting memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns and similar errors, as well as printing out detailed debug information whenever an error is detected.
To enable ASan in the meson build system, use following meson build command:
Example usage:
* gcc::
meson build -Dbuildtype=debug -Db_sanitize=address ninja -C build
Quoted 6 lines
* clang::
meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address ninja -C build
AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+).
The v11 version will be fixed.
Quoted 3 lines
.. Note::
a) To compile with gcc in Centos, libasan needs to be installed separately.
The v11 version will be fixed.
Quoted 2 lines
b) If the program being tested uses cmdline you will need to execute the "stty echo" command when a error occurs.
The v11 version will be fixed.
Quoted 42 lines
Quoted 10 lines
(quoted diff) +0
-0
b/doc/guides/prog_guide/index.rstindex2dce507f46..df8a4b93e1100644doc/guides/prog_guide/index.rst Programmer's Guide · line 74
Quoted 4 lines
doc/guides/prog_guide/index.rst +1
-0@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asanThe asan file should go before "glossary" and after "
profile_app".doc/guides/rel_notes/release_21_11.rst New Features · line 162
Quoted 10 lines
doc/guides/rel_notes/release_21_11.rst +1
-0@@ -159,6 +159,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan for memory detector on DPDK.**Change to "**Enable ASan Address Sanitization.**"
doc/guides/rel_notes/release_21_11.rst New Features · line 166
Quoted 4 lines
doc/guides/rel_notes/release_21_11.rst +4
-0+ + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>` + (ASan)This text should match the edited text above.
John
PATCH v10 1/4 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
---
v7: 1) Split doc and code into two.
2) Modify asan.rst doc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
v10:1) Split doc and code into two.
2) Meson supports asan.
--- config/meson.build | 16 ++++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 35 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 +++++++ 5 files changed, 62 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +16 -0
@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined' + if is_windows + error('ASan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +35 -0
@@ -0,0 +1,35 @@ +.. Copyright (c) <2021>, Intel Corporation + All rights reserved. + +Memory error detect standard tool - AddressSanitizer(ASan) +========================================================== + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable ASan in meson build system, use following meson build command: + +Example usage:: + + gcc : meson build -Dbuildtype=debug -Db_sanitize=address + ninja -C build + clang: meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) DPDK test has been completed in ubuntu18.04/ubuntu20.04/redhat8.3. To compile with gcc in + centos, libasan needs to be installed separately. + b) If the program uses cmdline, when a memory bug occurs, need to execute the "stty echo" command.
doc/guides/prog_guide/index.rst +1 -0
@@ -71,3 +71,4 @@ Programmer's Guide lto profile_app glossary + asan
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -159,6 +159,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan for memory detector on DPDK.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v10 2/4 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......---
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.
--- config/meson.build | 4 + doc/guides/prog_guide/asan.rst | 68 +++++++++++- 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, 306 insertions(+), 7 deletions(-)
config/meson.build +4 -0
@@ -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' + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'
doc/guides/prog_guide/asan.rst +66 -2
78 lines not on this page · raw message
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v10 3/4 pipeline: Fix compilation error with gcc ASan
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
---
v7: no change
v8: no change
v9: Modify the submit log
v10:no change
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASan
Quoted 56 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Friday, October 15, 2021 4:11 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>; [email protected]
Subject: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanFrom: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
Fixes f38913b7fb8e ("pipeline: add meter array to SWX")Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v7: no change
v8: no change
v9: Modify the submit log
v10:no change--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/pipeline/rte_swx_pipeline.c +2
-3@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data * --
Quoted 1 line
2.25.1
NACK.
This is a false issue, no bug is here. CHECK(0, EINVAL) translates to an unconditional return -EINVAL.
Does this tool work correctly when macros are present? Maybe the tool should parse the preprocessed C code as opposed to initial C code?
Regards,
Cristian
Re: [dpdk-dev] [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASan
Quoted 71 lines
-----Original Message-----
From: Dumitrescu, Cristian <[email protected]>
Sent: Monday, October 18, 2021 8:22 PM
To: Peng, ZhihongX <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; [email protected]
Subject: RE: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanQuoted 54 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Friday, October 15, 2021 4:11 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>; [email protected]
Subject: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanFrom: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
Fixes f38913b7fb8e ("pipeline: add meter array to SWX")Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v7: no change
v8: no change
v9: Modify the submit log
v10:no change--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)(quoted diff) +0
-0
b/lib/pipeline/rte_swx_pipeline.cindex1cd09a4b44..0acd6c6752100644lib/pipeline/rte_swx_pipeline.c +2
-3@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data * --Quoted 1 line
2.25.1
NACK.
Quoted 4 lines
This is a false issue, no bug is here.
CHECK(0, EINVAL)translates to an unconditional return-EINVAL.
Does this tool work correctly when macros are present? Maybe the tool should parse the preprocessed C code as opposed to initial C code?
Yes, this is not a bug, it just solves the problem that cannot be passed after adding the asan compiler option.
Only part of the macro reports errors, which may be caused by the tool itself, but this tool is part of gcc and clang, so we still have to make the code not report errors.
Quoted 2 lines
Regards,
Cristian
Re: [dpdk-dev] [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASan
Quoted 90 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Monday, October 18, 2021 1:55 PM
To: Dumitrescu, Cristian <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; [email protected]
Subject: RE: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanQuoted 44 lines
-----Original Message-----
From: Dumitrescu, Cristian <[email protected]>
Sent: Monday, October 18, 2021 8:22 PM
To: Peng, ZhihongX <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; [email protected]
Subject: RE: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanQuoted 31 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Friday, October 15, 2021 4:11 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>; [email protected]
Subject: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanFrom: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
Fixes f38913b7fb8e ("pipeline: add meter array to SWX")Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v7: no change
v8: no change
v9: Modify the submit log
v10:no change--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 5 lines
lib/pipeline/rte_swx_pipeline.c instr_meter_translate(struct rte_swx_pipeline
Quoted 5 lines
(quoted diff) +0
-0
b/lib/pipeline/rte_swx_pipeline.cindex1cd09a4b44..0acd6c6752100644lib/pipeline/rte_swx_pipeline.c +0
-0@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline*p,
Quoted 22 lines
Quoted 18 lines
lib/pipeline/rte_swx_pipeline.c +2
-3return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data * --Quoted 1 line
2.25.1
NACK.
Quoted 4 lines
This is a false issue, no bug is here.
CHECK(0, EINVAL)translates to an unconditional return-EINVAL.
Does this tool work correctly when macros are present? Maybe the tool should parse the preprocessed C code as opposed to initial C code?
Quoted 7 lines
Yes, this is not a bug, it just solves the problem that cannot be passed after adding the asan compiler option.
Only part of the macro reports errors, which may be caused by the tool itself, but this tool is part of gcc and clang, so we still have to make the code not report errors.Quoted 2 lines
Regards,
Cristian
Hi Zhihong,
If this is not a bug in the pipeline library, why then does your patch has fix in the tile, has the Fixes label and CC-es [email protected]? Please remove and rephrase accordingly.
I agree this is not a bug, and based on your statements I understand this is a sort of issue or limitation with the tool . I would prefer we fix the tool rather than fixing correct code in order to please the tool. This is likely not going to be an isolated case, but a recurring issue.
Hence, I am reluctantly OK to ack this patch in order to allow the tool in (after the "fix" claim is removed), hopefully the tool will prove its benefit to DPDK.
Regards,
Cristian
Re: [dpdk-dev] [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASan
Quoted 122 lines
-----Original Message-----
From: Dumitrescu, Cristian <[email protected]>
Sent: Tuesday, October 19, 2021 7:26 PM
To: Peng, ZhihongX <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; [email protected]
Subject: RE: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanQuoted 7 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Monday, October 18, 2021 1:55 PM
To: Dumitrescu, Cristian <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected];Mcnamara,
Quoted 87 lines
John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; [email protected]
Subject: RE: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanQuoted 47 lines
-----Original Message-----
From: Dumitrescu, Cristian <[email protected]>
Sent: Monday, October 18, 2021 8:22 PM
To: Peng, ZhihongX <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; [email protected]
Subject: RE: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanQuoted 32 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Friday, October 15, 2021 4:11 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>; [email protected]
Subject: [PATCH v10 3/4] pipeline: Fix compilation error with gcc ASanFrom: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
Fixes f38913b7fb8e ("pipeline: add meter array to SWX")Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v7: no change
v8: no change
v9: Modify the submit log
v10:no change--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 7 lines
Quoted 7 lines
(quoted diff) +0
-0
b/lib/pipeline/rte_swx_pipeline.cindex1cd09a4b44..0acd6c6752100644lib/pipeline/rte_swx_pipeline.c +0
-0@@ -4642,7 +4642,7 @@ instr_meter_translate(struct
rte_swx_pipeline*p,
Quoted 22 lines
Quoted 18 lines
lib/pipeline/rte_swx_pipeline.c +2
-3return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data * --Quoted 1 line
2.25.1
NACK.
Quoted 4 lines
This is a false issue, no bug is here.
CHECK(0, EINVAL)translates to an unconditional return-EINVAL.
Does this tool work correctly when macros are present? Maybe the tool should parse the preprocessed C code as opposed to initial C code?Quoted 7 lines
Yes, this is not a bug, it just solves the problem that cannot be passed after adding the asan compiler option.
Only part of the macro reports errors, which may be caused by the tool itself, but this tool is part of gcc and clang, so we still have to make the code not report errors.Quoted 2 lines
Regards,
CristianHi Zhihong,
If this is not a bug in the pipeline library, why then does your patch has fix in the tile, has the Fixes label and CC-es [email protected]? Please remove and rephrase accordingly.
Quoted 7 lines
I agree this is not a bug, and based on your statements I understand this is a sort of issue or limitation with the tool . I would prefer we fix the tool rather than fixing correct code in order to please the tool. This is likely not going to be an isolated case, but a recurring issue.
Hence, I am reluctantly OK to ack this patch in order to allow the tool in (after the "fix" claim is removed), hopefully the tool will prove its benefit to DPDK.
ok, I will delete it.
Quoted 2 lines
Regards,
Cristian
PATCH v10 4/4 performance-thread: Fix cross compilation failed
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
--- examples/performance-thread/common/lthread.c | 2 +- examples/performance-thread/common/lthread_cond.c | 4 ++-- examples/performance-thread/common/lthread_mutex.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
examples/performance-thread/common/lthread.c +1 -1
@@ -463,6 +463,6 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); +strncpy (lt->funcname, f,sizeof (lt->funcname) -1 ); lt->funcname[sizeof (lt->funcname)-1 ] =0 ; }
examples/performance-thread/common/lthread_cond.c +2 -2
@@ -57,9 +57,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +strncpy (c->name,"no name" ,sizeof (c->name) -1 );else -strncpy (c->name, name,sizeof (c->name)); +strncpy (c->name, name,sizeof (c->name) -1 ); c->name[sizeof (c->name)-1 ] =0 ; c->root_sched =THIS_SCHED ;
examples/performance-thread/common/lthread_mutex.c +2 -2
@@ -52,9 +52,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +strncpy (m->name,"no name" ,sizeof (m->name) -1 );else -strncpy (m->name, name,sizeof (m->name)); +strncpy (m->name, name,sizeof (m->name) -1 ); m->name[sizeof (m->name)-1 ] =0 ; m->root_sched =THIS_SCHED ;
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v10 4/4] performance-thread: Fix cross compilation failed
Quoted 75 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Friday, October 15, 2021 11:11 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>; [email protected]
Subject: [PATCH v10 4/4] performance-thread: Fix cross compilation failedFrom: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
Fixes 116819b9ed0d ("examples/performance-thread: add lthreadsubsystem")
Cc: [email protected]Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>--- examples/performance-thread/common/lthread.c | 2 +- examples/performance-thread/common/lthread_cond.c | 4 ++-- examples/performance-thread/common/lthread_mutex.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)examples/performance-thread/common/lthread.c +1
-1@@ -463,6 +463,6 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); +strncpy (lt->funcname, f,sizeof (lt->funcname) -1 ); lt->funcname[sizeof(lt->funcname)-1] = 0; } diff --git
a/examples/performance-thread/common/lthread_cond.cb/examples/performance-thread/common/lthread_cond.cindexcdcc7a7b5a..6ec8bc7e82100644examples/performance-thread/common/lthread_cond.c +2
-2@@ -57,9 +57,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +strncpy (c->name,"no name" ,sizeof (c->name) -1 );else -strncpy (c->name, name,sizeof (c->name)); +strncpy (c->name, name,sizeof (c->name) -1 ); c->name[sizeof (c->name)-1 ] =0 ;examples/performance-thread/common/lthread_cond.c +0
-0c->root_sched =THIS_SCHED ;examples/performance-thread/common/lthread_mutex.c +2
-2@@ -52,9 +52,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +strncpy (m->name,"no name" ,sizeof (m->name) -1 );else -strncpy (m->name, name,sizeof (m->name)); +strncpy (m->name, name,sizeof (m->name) -1 ); m->name[sizeof (m->name)-1 ] =0 ;
Quoted 3 lines
m->root_sched=THIS_SCHED;
--
2.25.1
Hi, John
Can you give me an ack?
Thanks!
PATCH v11 1/4 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
---
v7: 1) Split doc and code into two.
2) Modify asan.rst doc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
v10:1) Split doc and code into two.
2) Meson supports asan.
v11:Modify the document.
--- config/meson.build | 16 +++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 40 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 ++++++ 5 files changed, 67 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +16 -0
@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined' + if is_windows + error('ASan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +40 -0
@@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation + +Running Address Sanitizer +========================= + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable ASan in meson build system, use following meson build command: + +Example usage:: + +* gcc:: + + meson build -Dbuildtype=debug -Db_sanitize=address + ninja -C build + +* clang:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) To compile with gcc in centos, libasan needs to be installed separately. + b) If the program being tested uses cmdline you will need to execute the + "stty echo" command when a error occurs.
doc/guides/prog_guide/index.rst +1 -0
@@ -72,3 +72,4 @@ Programmer's Guide lto profile_app glossary + asan
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -167,6 +167,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan for memory detector on DPDK.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v11 2/4 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......---
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.
--- config/meson.build | 4 + doc/guides/prog_guide/asan.rst | 68 +++++++++++- 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, 306 insertions(+), 7 deletions(-)
config/meson.build +4 -0
@@ -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' + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'
doc/guides/prog_guide/asan.rst +66 -2
78 lines not on this page · raw message
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v11 3/4 pipeline: Fix compilation error with gcc ASan
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
---
v7: no change
v8: no change
v9: Modify the submit log
v10:no change
v11:no change
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v11 4/4 performance-thread: Fix cross compilation failed
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
---
v11: Use rte_strlcpy to replace strncpy.
--- examples/performance-thread/common/lthread.c | 4 ++-- examples/performance-thread/common/lthread_cond.c | 6 +++--- examples/performance-thread/common/lthread_mutex.c | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-)
examples/performance-thread/common/lthread.c +2 -2
@@ -20,6 +20,7 @@#include <sys/mman.h> #include <rte_log.h> +#include <rte_string_fns.h> #include <ctx.h> #include <stack.h>
@@ -463,6 +464,5 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); - lt->funcname[sizeof (lt->funcname)-1 ] =0 ; +rte_strlcpy (lt->funcname, f,sizeof (lt->funcname)); }
examples/performance-thread/common/lthread_cond.c +3 -3
@@ -20,6 +20,7 @@#include <rte_log.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_diag_api.h"
@@ -57,10 +58,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +rte_strlcpy (c->name,"no name" ,sizeof (c->name));else -strncpy (c->name, name,sizeof (c->name)); - c->name[sizeof (c->name)-1 ] =0 ; +rte_strlcpy (c->name, name,sizeof (c->name)); c->root_sched =THIS_SCHED ;
examples/performance-thread/common/lthread_mutex.c +3 -3
@@ -19,6 +19,7 @@#include <rte_log.h> #include <rte_spinlock.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_int.h"
@@ -52,10 +53,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +rte_strlcpy (m->name,"no name" ,sizeof (m->name));else -strncpy (m->name, name,sizeof (m->name)); - m->name[sizeof (m->name)-1 ] =0 ; +rte_strlcpy (m->name, name,sizeof (m->name)); m->root_sched =THIS_SCHED ; m->owner =NULL ;
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v11 4/4] performance-thread: Fix cross compilation failed
Quoted 5 lines · On Tue, Oct 19, 2021 at 06:12:07PM +0800, [email protected] wrote:
From: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
Quoted 8 lines
Fixes 116819b9ed0d ("examples/performance-thread: add lthread subsystem")Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v11: Userte_strlcpyto replace strncpy.
---
+1 to using strlcpy, but it should be "strlcpy" rather than "rte_strlcpy" which should be used. Analysis tools are likely to be familiar with strlcpy but won't know the DPDK-specific version of it.
With that change to remove the rte_ prefix
/Bruce
PATCH v12 1/4 Enable ASan for memory detector on DPDK
In reply to [email protected]From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
---
v7: 1) Split doc and code into two.
2) Modify asan.rst doc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
v10:1) Split doc and code into two.
2) Meson supports asan.
v11:Modify the document.
v12:No change.
--- config/meson.build | 16 +++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 40 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 ++++++ 5 files changed, 67 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +16 -0
@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined' + if is_windows + error('ASan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +40 -0
@@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation + +Running Address Sanitizer +========================= + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable ASan in meson build system, use following meson build command: + +Example usage:: + +* gcc:: + + meson build -Dbuildtype=debug -Db_sanitize=address + ninja -C build + +* clang:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) To compile with gcc in centos, libasan needs to be installed separately. + b) If the program being tested uses cmdline you will need to execute the + "stty echo" command when a error occurs.
doc/guides/prog_guide/index.rst +1 -0
@@ -72,3 +72,4 @@ Programmer's Guide lto profile_app glossary + asan
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -167,6 +167,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan for memory detector on DPDK.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>` (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v12 2/4 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......---
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.
--- config/meson.build | 4 + doc/guides/prog_guide/asan.rst | 68 +++++++++++- 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, 306 insertions(+), 7 deletions(-)
config/meson.build +4 -0
@@ -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' + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'
doc/guides/prog_guide/asan.rst +66 -2
78 lines not on this page · raw message
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v12 3/4 pipeline: Fix compilation error with gcc ASan
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc will check code more stricter when ASan enabled. "Control reaches end of non-void function" error occurs here.
---
v7: no change.
v8: no change.
v9: Modify the submit log.
v10:no change.
v11:no change.
v12:Modify the commit log.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v12 4/4 performance-thread: Fix cross compilation failed
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
---
v11: Use rte_strlcpy to replace strncpy.
v12: Delete rte_strlcpy's rte_.
--- examples/performance-thread/common/lthread.c | 4 ++-- examples/performance-thread/common/lthread_cond.c | 6 +++--- examples/performance-thread/common/lthread_mutex.c | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-)
examples/performance-thread/common/lthread.c +2 -2
@@ -20,6 +20,7 @@#include <sys/mman.h> #include <rte_log.h> +#include <rte_string_fns.h> #include <ctx.h> #include <stack.h>
@@ -463,6 +464,5 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); - lt->funcname[sizeof (lt->funcname)-1 ] =0 ; +strlcpy (lt->funcname, f,sizeof (lt->funcname)); }
examples/performance-thread/common/lthread_cond.c +3 -3
@@ -20,6 +20,7 @@#include <rte_log.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_diag_api.h"
@@ -57,10 +58,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +strlcpy (c->name,"no name" ,sizeof (c->name));else -strncpy (c->name, name,sizeof (c->name)); - c->name[sizeof (c->name)-1 ] =0 ; +strlcpy (c->name, name,sizeof (c->name)); c->root_sched =THIS_SCHED ;
examples/performance-thread/common/lthread_mutex.c +3 -3
@@ -19,6 +19,7 @@#include <rte_log.h> #include <rte_spinlock.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_int.h"
@@ -52,10 +53,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +strlcpy (m->name,"no name" ,sizeof (m->name));else -strncpy (m->name, name,sizeof (m->name)); - m->name[sizeof (m->name)-1 ] =0 ; +strlcpy (m->name, name,sizeof (m->name)); m->root_sched =THIS_SCHED ; m->owner =NULL ;
Signature · 1 line
2.25.1
PATCH v12 1/4 Enable ASan Address Sanitization
In reply to [email protected]From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
---
v7: 1) Split doc and code into two.
2) Modify asan.rst doc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
v10:1) Split doc and code into two.
2) Meson supports asan.
v11:Modify the document.
v12:No change.
--- config/meson.build | 16 +++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 40 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 ++++++ 5 files changed, 67 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +16 -0
@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined' + if is_windows + error('ASan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +40 -0
@@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation + +Running Address Sanitizer +========================= + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable ASan in meson build system, use following meson build command: + +Example usage:: + +* gcc:: + + meson build -Dbuildtype=debug -Db_sanitize=address + ninja -C build + +* clang:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) To compile with gcc in centos, libasan needs to be installed separately. + b) If the program being tested uses cmdline you will need to execute the + "stty echo" command when a error occurs.
doc/guides/prog_guide/index.rst +1 -0
@@ -71,4 +71,5 @@ Programmer's Guide writing_efficient_code lto profile_app + asan glossary
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -167,6 +167,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan Address Sanitization.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v12 2/4 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......---
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.
--- config/meson.build | 4 + doc/guides/prog_guide/asan.rst | 68 +++++++++++- 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, 306 insertions(+), 7 deletions(-)
config/meson.build +4 -0
@@ -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' + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'
doc/guides/prog_guide/asan.rst +66 -2
78 lines not on this page · raw message
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v12 3/4 Code changes to avoid the ASan error
In reply to [email protected]From: Zhihong Peng <[email protected]>
Code changes to avoid the following ASan error:
"Control reaches end of non-void function".
---
v7: no change.
v8: no change.
v9: Modify the submit log.
v10:no change.
v11:no change.
v12:Modify the commit log.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v12 3/4] Code changes to avoid the ASan error
Quoted 58 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Tuesday, October 19, 2021 2:59 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>; Richardson, Bruce <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>
Subject: [PATCH v12 3/4] Code changes to avoid the ASan errorFrom: Zhihong Peng <[email protected]>
Code changes to avoid the following ASan error:
"Control reaches end of non-void function".Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v7: no change.
v8: no change.
v9: Modify the submit log.
v10:no change.
v11:no change.
v12:Modify the commit log.--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/pipeline/rte_swx_pipeline.c +2
-3@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data * --
Quoted 1 line
2.25.1
This patch changes code working correctly just to please the tool. We should look at ways to fix the tooll, as this is likely going to be a recurring issue.
Hopefully this tool is to provide value to DPDK users.
Reluctantly,
PATCH v12 4/4 performance-thread: Fix cross compilation failed
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
---
v11: Use rte_strlcpy to replace strncpy.
v12: Delete rte_strlcpy's rte_.
--- examples/performance-thread/common/lthread.c | 4 ++-- examples/performance-thread/common/lthread_cond.c | 6 +++--- examples/performance-thread/common/lthread_mutex.c | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-)
examples/performance-thread/common/lthread.c +2 -2
@@ -20,6 +20,7 @@#include <sys/mman.h> #include <rte_log.h> +#include <rte_string_fns.h> #include <ctx.h> #include <stack.h>
@@ -463,6 +464,5 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); - lt->funcname[sizeof (lt->funcname)-1 ] =0 ; +strlcpy (lt->funcname, f,sizeof (lt->funcname)); }
examples/performance-thread/common/lthread_cond.c +3 -3
@@ -20,6 +20,7 @@#include <rte_log.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_diag_api.h"
@@ -57,10 +58,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +strlcpy (c->name,"no name" ,sizeof (c->name));else -strncpy (c->name, name,sizeof (c->name)); - c->name[sizeof (c->name)-1 ] =0 ; +strlcpy (c->name, name,sizeof (c->name)); c->root_sched =THIS_SCHED ;
examples/performance-thread/common/lthread_mutex.c +3 -3
@@ -19,6 +19,7 @@#include <rte_log.h> #include <rte_spinlock.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_int.h"
@@ -52,10 +53,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +strlcpy (m->name,"no name" ,sizeof (m->name));else -strncpy (m->name, name,sizeof (m->name)); - m->name[sizeof (m->name)-1 ] =0 ; +strlcpy (m->name, name,sizeof (m->name)); m->root_sched =THIS_SCHED ; m->owner =NULL ;
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v12 1/4] Enable ASan Address Sanitization
In reply to [email protected]Hi, David
The v12 version has been submitted, and patch 3 and 4 has been acked.
V10 information:
I have compiled passed on the x86/arm/ppc platforms, directory targets as below :
build-arm64-bluefield build-arm64-host-clang build-clang-shared build-gcc-shared build-ppc64le-power8
build-arm64-dpaa build-arm64-octeontx2 build-clang-static build-gcc-static build-x86-generic
We do not support the windows platform due to standard google document(https://github.com/google/sanitizers/wiki/AddressSanitizer) also not support this.
We also sent our cross-compilation log to you in an other email. Passed to run unit test for dpdk-testpmd simple on x86 platform. What else is blocking the process ?
What's any action we should do for the code merge? High appreciate for your check and feedback.
Quoted 171 lines
-----Original Message-----
From: Peng, ZhihongX <[email protected]>
Sent: Tuesday, October 19, 2021 9:59 PM
To: [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>; Richardson, Bruce <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>; Peng, ZhihongX <[email protected]>
Subject: [PATCH v12 1/4] Enable ASan Address SanitizationFrom: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a widely-used debugging tool to detect memory access errors.
It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>---
v7: 1) Split doc and code into two.
2) Modifyasan.rstdoc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
v10:1) Split doc and code into two.
2) Meson supports asan.
v11:Modify the document.
v12:No change.--- config/meson.build | 16 +++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 40 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 ++++++ 5 files changed, 67 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rstconfig/meson.build index renamed from config/meson.build +0
-0
4cdf589e20..f02b0e9c6d100644config/meson.build +1
-0@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') =='address,undefined'
config/meson.build +15
-0+ if is_windows + error('ASan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.devtools/words-case.txt index renamed from devtools/words-case.txt +0
-0
0bbad48626..ada6910fa0100644devtools/words-case.txt +1
-0@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB(quoted diff) +0
-0
b/doc/guides/prog_guide/asan.rstnew file mode 100644 index 0000000000..969676ebe8doc/guides/prog_guide/asan.rst new file +22
-0@@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation + +Running Address Sanitizer +========================= + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable ASan in meson build system, use following meson buildcommand:
doc/guides/prog_guide/asan.rst new file +18
-0+ +Example usage:: + +* gcc:: + + meson build -Dbuildtype=debug -Db_sanitize=address + ninja -C build + +* clang:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) To compile with gcc in centos, libasan needs to be installed separately. + b) If the program being tested uses cmdline you will need to execute the + "stty echo" command when a error occurs.(quoted diff) +0
-0
b/doc/guides/prog_guide/index.rstindex89af28dacb..b95c460b19100644doc/guides/prog_guide/index.rst +1
-0@@ -71,4 +71,5 @@ Programmer's Guide writing_efficient_code lto profile_app + asan glossarydoc/guides/rel_notes/release_21_11.rst +9
-1@@ -167,6 +167,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan Address Sanitization.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items ------------- --2.25.1
Mcnamara, John
Acked-byRe: [dpdk-dev] [PATCH v12 1/4] Enable ASan Address Sanitization
Quoted 12 lines
Quoted 10 lines
(quoted diff) +0
-0
b/doc/guides/prog_guide/asan.rstnew file mode 100644 index 0000000000..969676ebe8doc/guides/prog_guide/asan.rst new file +3
-0@@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation +
Quoted 8 lines
doc/guides/prog_guide/asan.rst line 20
Quoted 8 lines
...
doc/guides/prog_guide/asan.rst new file +6
-0+ +Usage +----- + +meson build +^^^^^^^^^^^
These 2 additional header levels aren't needed. If you do another version you can leave them out. Apart from that:
Re: [dpdk-dev] [PATCH v12 1/4] Enable ASan Address Sanitization
Quoted 26 lines
-----Original Message-----
From: Mcnamara, John <[email protected]>
Sent: Tuesday, October 19, 2021 11:18 PM
To: Peng, ZhihongX <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Richardson, Bruce <[email protected]>
Cc: [email protected]; Lin, Xueqin <[email protected]>
Subject: RE: [PATCH v12 1/4] Enable ASan Address SanitizationQuoted 12 lines
Quoted 12 lines
(quoted diff) +0
-0
b/doc/guides/prog_guide/asan.rstnew file mode 100644 index 0000000000..969676ebe8doc/guides/prog_guide/asan.rst new file +3
-0@@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation +
Quoted 9 lines
Quoted 6 lines
doc/guides/prog_guide/asan.rst line 20
Quoted 6 lines
doc/guides/prog_guide/asan.rst new file +6
-0+ +Usage +----- + +meson build +^^^^^^^^^^^These 2 additional header levels aren't needed. If you do another version you can leave them out. Apart from that:
I will fix it in v13.
Quoted 1 line
Acked-by John McNamara <[email protected]>
Re: [dpdk-dev] [PATCH v12 1/4] Enable ASan Address Sanitization
In reply to Peng, ZhihongXQuoted 8 lines · ZhihongX
The v12 version has been submitted, and patch 3 and 4 has been acked.
V10 information:
I have compiled passed on the x86/arm/ppc platforms, directory targets as below :
build-arm64-bluefield build-arm64-host-clang build-clang-shared build-gcc-shared build-ppc64le-power8
build-arm64-dpaa build-arm64-octeontx2 build-clang-static build-gcc-static build-x86-generic
Quoted 5 lines
We do not support the windows platform due to standard google document(https://github.com/google/sanitizers/wiki/AddressSanitizer) also not support this.
We also sent our cross-compilation log to you in an other email. Passed to run unit test for dpdk-testpmd simple on x86 platform. What else is blocking the process ?
What's any action we should do for the code merge? High appreciate for your check and feedback.
This series has seen so many revisions (often numbered the same, and while I was writting this reply, here is a new revision) in the last days that I put it at the end of my queue simply to wait for a stable state.
By the way, simply pinging me to get your patches merged is easy, but if you care about the dpdk project, there are other series that need reviews/checks when merging, you are welcome to review and test patches.
Signature · 1 line
David Marchand
Re: [dpdk-dev] [PATCH v12 1/4] Enable ASan Address Sanitization
Quoted 34 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Tuesday, October 19, 2021 11:21 PM
To: Peng, ZhihongX <[email protected]>
Cc: Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; [email protected]; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>; Richardson, Bruce <[email protected]>; [email protected]; Lin, Xueqin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v12 1/4] Enable ASan Address SanitizationQuoted 5 lines · ZhihongX
The v12 version has been submitted, and patch 3 and 4 has been acked.
V10 information:
I have compiled passed on the x86/arm/ppc platforms, directory targets as
below :
Quoted 1 line
build-arm64-bluefield build-arm64-host-clang build-clang-shared build-
gcc-shared build-ppc64le-power8
Quoted 1 line
build-arm64-dpaa build-arm64-octeontx2 build-clang-static build-gcc-
static build-x86-generic
Quoted 5 lines
We do not support the windows platform due to standard google document(https://github.com/google/sanitizers/wiki/AddressSanitizer) also not support this.
We also sent our cross-compilation log to you in an other email. Passed torun unit test for dpdk-testpmd simple on x86 platform.
Quoted 2 lines
What else is blocking the process ?
What's any action we should do for the code merge? High appreciate foryour check and feedback.
Quoted 7 lines
This series has seen so many revisions (often numbered the same, and while I was writting this reply, here is a new revision) in the last days that I put it at the end of my queue simply to wait for a stable state.
By the way, simply pinging me to get your patches merged is easy, but if you care about the dpdk project, there are other series that need reviews/checks when merging, you are welcome to review and test patches.
Really sorry to bring you so many trouble, apart from ASan implementation patch, also have other error build, doc... patches, recently receive some comments from other domain experts and maintainers. We are active to deal with them and have so many revisions. For the same numbers, it is our fault, sorry for that, and thanks a lot for your and reviewers' great support. Today we will send v13 version, and all the fix will be in this version.
We also care about the quality, make sure to run cross-compilation build and unit test passed on our platforms then send the patch.
Hope that everything is well and could meet the expectation, thanks a lot.
Quoted 4 lines
--
David Marchand
PATCH v12 1/4 Enable ASan AddressSanitizer
In reply to [email protected]From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a widely-used debugging tool to detect memory access errors. It helps detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
---
v7: 1) Split doc and code into two.
2) Modify asan.rst doc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
v10:1) Split doc and code into two.
2) Meson supports asan.
v11:Modify the document.
v12:No change.
--- config/meson.build | 16 +++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 40 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 ++++++ 5 files changed, 67 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +16 -0
@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined' + if is_windows + error('ASan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +40 -0
@@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation + +Running Address Sanitizer +========================= + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Usage +----- + +meson build +^^^^^^^^^^^ + +To enable ASan in meson build system, use following meson build command: + +Example usage:: + +* gcc:: + + meson build -Dbuildtype=debug -Db_sanitize=address + ninja -C build + +* clang:: + + meson build -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + ninja -C build + +.. Note:: + + a) To compile with gcc in centos, libasan needs to be installed separately. + b) If the program being tested uses cmdline you will need to execute the + "stty echo" command when a error occurs.
doc/guides/prog_guide/index.rst +1 -0
@@ -71,4 +71,5 @@ Programmer's Guide writing_efficient_code lto profile_app + asan glossary
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -167,6 +167,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan AddressSanitizer.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v12 2/4 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported Linux x86_64. Support other platforms, need to define ASAN_SHADOW_OFFSET value according to google ASan document, and configure meson (config/meson.build).
Here is an example of heap-buffer-overflow bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
p[7] = 'a';
......Here is an example of use-after-free bug:
......
char *p = rte_zmalloc(NULL, 7, 0);
rte_free(p);
*p = 'a';
......---
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.
--- config/meson.build | 4 + doc/guides/prog_guide/asan.rst | 68 +++++++++++- 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, 306 insertions(+), 7 deletions(-)
config/meson.build +4 -0
@@ -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' + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'
doc/guides/prog_guide/asan.rst +66 -2
78 lines not on this page · raw message
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
PATCH v12 3/4 Code changes to avoid the ASan error
In reply to [email protected]From: Zhihong Peng <[email protected]>
Code changes to avoid the following ASan error:
"Control reaches end of non-void function".
---
v7: no change.
v8: no change.
v9: Modify the submit log.
v10:no change.
v11:no change.
v12:Modify the commit log.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v12 4/4 performance-thread: Fix cross compilation failed
In reply to [email protected]From: Zhihong Peng <[email protected]>
The gcc(arm-linux-gcc) will check code more stricter when ASan enabled. "strncpy specified bound XX equals destination size" error occurs here.
---
v11: Use rte_strlcpy to replace strncpy.
v12: Delete rte_strlcpy's rte_.
--- examples/performance-thread/common/lthread.c | 4 ++-- examples/performance-thread/common/lthread_cond.c | 6 +++--- examples/performance-thread/common/lthread_mutex.c | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-)
examples/performance-thread/common/lthread.c +2 -2
@@ -20,6 +20,7 @@#include <sys/mman.h> #include <rte_log.h> +#include <rte_string_fns.h> #include <ctx.h> #include <stack.h>
@@ -463,6 +464,5 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); - lt->funcname[sizeof (lt->funcname)-1 ] =0 ; +strlcpy (lt->funcname, f,sizeof (lt->funcname)); }
examples/performance-thread/common/lthread_cond.c +3 -3
@@ -20,6 +20,7 @@#include <rte_log.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_diag_api.h"
@@ -57,10 +58,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +strlcpy (c->name,"no name" ,sizeof (c->name));else -strncpy (c->name, name,sizeof (c->name)); - c->name[sizeof (c->name)-1 ] =0 ; +strlcpy (c->name, name,sizeof (c->name)); c->root_sched =THIS_SCHED ;
examples/performance-thread/common/lthread_mutex.c +3 -3
@@ -19,6 +19,7 @@#include <rte_log.h> #include <rte_spinlock.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_int.h"
@@ -52,10 +53,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +strlcpy (m->name,"no name" ,sizeof (m->name));else -strncpy (m->name, name,sizeof (m->name)); - m->name[sizeof (m->name)-1 ] =0 ; +strlcpy (m->name, name,sizeof (m->name)); m->root_sched =THIS_SCHED ; m->owner =NULL ;
Signature · 1 line
2.25.1
PATCH v13 1/4 enable ASan AddressSanitizer
From: Zhihong Peng <[email protected]>
`AddressSanitizer
<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) is a widely-used debugging tool to detect memory access errors. It helps to detect issues like use-after-free, various kinds of buffer overruns in C/C++ programs, and other similar errors, as well as printing out detailed debug information whenever an error is detected.
We can enable ASan by adding below compilation options:
-Dbuildtype=debug -Db_lundef=false -Db_sanitize=address "-Dbuildtype=debug": This is a non-essential option. When this option is added, if a memory error occurs, ASan can clearly show where the code is wrong.
"-Db_lundef=false": When use clang to compile DPDK, this option must be added.
---
v7: 1) Split doc and code into two.
2) Modify asan.rst doc
v8: No change.
v9: 1) Add the check of libasan library.
2) Add release notes.
v10:1) Split doc and code into two.
2) Meson supports asan.
v11:Modify the document.
v12:No change.
v13:Modify the document.
--- config/meson.build | 16 ++++++++++++++ devtools/words-case.txt | 1 + doc/guides/prog_guide/asan.rst | 30 ++++++++++++++++++++++++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/rel_notes/release_21_11.rst | 9 ++++++++ 5 files changed, 57 insertions(+) create mode 100644 doc/guides/prog_guide/asan.rst
config/meson.build +16 -0
@@ -411,6 +411,22 @@ if get_option('b_lto') endif endif +if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined' + if is_windows + error('ASan is not supported on windows') + endif + + if cc.get_id() == 'gcc' + asan_dep = cc.find_library('asan', required: true) + if (not cc.links('int main(int argc, char *argv[]) { return 0; }', + dependencies: asan_dep)) + error('broken dependency, "libasan"') + endif + add_project_link_arguments('-lasan', language: 'c') + dpdk_extra_ldflags += '-lasan' + endif +endif + if get_option('default_library') == 'both' error( ''' Unsupported value "both" for "default_library" option.
devtools/words-case.txt +1 -0
@@ -5,6 +5,7 @@ API Arm armv7 armv8 +ASan BAR CRC DCB
doc/guides/prog_guide/asan.rst new file +30 -0
@@ -0,0 +1,30 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2021 Intel Corporation + +Running AddressSanitizer +======================== + +`AddressSanitizer +<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) +is a widely-used debugging tool to detect memory access errors. +It helps to detect issues like use-after-free, various kinds of buffer +overruns in C/C++ programs, and other similar errors, as well as +printing out detailed debug information whenever an error is detected. + +AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+). + +Add following meson build commands to enable ASan in the meson build system: + +* gcc:: + + -Dbuildtype=debug -Db_sanitize=address + +* clang:: + + -Dbuildtype=debug -Db_lundef=false -Db_sanitize=address + +.. 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 + "stty echo" command when an error occurs.
doc/guides/prog_guide/index.rst +1 -0
@@ -71,4 +71,5 @@ Programmer's Guide writing_efficient_code lto profile_app + asan glossary
doc/guides/rel_notes/release_21_11.rst +9 -0
@@ -173,6 +173,15 @@ New Features * Added tests to verify tunnel header verification in IPsec inbound. * Added tests to verify inner checksum. +* **Enable ASan AddressSanitizer.** + + `AddressSanitizer + <https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ (ASan) + is a widely-used debugging tool to detect memory access errors. + It helps to detect issues like use-after-free, various kinds of buffer + overruns in C/C++ programs, and other similar errors, as well as + printing out detailed debug information whenever an error is detected. + Removed Items -------------
Signature · 1 line
2.25.1
PATCH v13 2/4 DPDK code adapts to ASan
From: Zhihong Peng <[email protected]>
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).
---
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(-)
config/meson.build +4 -0
@@ -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' + dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'
doc/guides/prog_guide/asan.rst +56 -2
68 lines not on this page · raw message
lib/eal/common/malloc_elem.c +25 -1
@@ -446,6 +446,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,struct malloc_elem *new_free_elem =RTE_PTR_ADD (new_elem, size +MALLOC_ELEM_OVERHEAD ); +asan_clear_split_alloczone (new_free_elem); +split_elem (elem, new_free_elem);malloc_elem_free_list_insert (new_free_elem);
@@ -458,6 +460,8 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align, elem->state =ELEM_BUSY ; elem->pad = old_elem_size; +asan_clear_alloczone (elem); +/* put a dummy header in padding, to point to real element header */ if (elem->pad >0 ) {/* pad will be at least 64-bytes, as everything * is cache-line aligned */
@@ -470,12 +474,18 @@ malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,return new_elem; } +asan_clear_split_alloczone (new_elem); +/* we are going to split the element in two. The original element * remains free, and the new element is the one allocated. * Re-insert original element, in case its new size makes it * belong on a different list. */ +split_elem (elem, new_elem); + +asan_clear_alloczone (new_elem); + new_elem->state =ELEM_BUSY ;malloc_elem_free_list_insert (elem);
@@ -601,6 +611,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (next &&next_elem_is_adjacent (elem)) { len_after =RTE_PTR_DIFF (next, hide_end);if (len_after >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_end); +/* split after */ split_elem (elem, hide_end);
@@ -615,6 +627,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)if (prev &&prev_elem_is_adjacent (elem)) { len_before =RTE_PTR_DIFF (hide_start, elem);if (len_before >=MALLOC_ELEM_OVERHEAD +MIN_DATA_SIZE ) { +asan_clear_split_alloczone (hide_start); +/* split before */ split_elem (elem, hide_start);
@@ -628,6 +642,8 @@ malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len) } } +asan_clear_alloczone (elem); +remove_elem (elem); }
@@ -641,8 +657,10 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)const size_t new_size = size + elem->pad +MALLOC_ELEM_OVERHEAD ;/* if we request a smaller size, then always return ok */ -if (elem->size >= new_size) +if (elem->size >= new_size) { +asan_clear_alloczone (elem);return 0 ; + }/* check if there is a next element, it's free and adjacent */ if (!elem->next || elem->next->state != ELEM_FREE ||
@@ -661,9 +679,15 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size)/* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt =RTE_PTR_ADD (elem, new_size); split_pt =RTE_PTR_ALIGN_CEIL (split_pt,RTE_CACHE_LINE_SIZE ); + +asan_clear_split_alloczone (split_pt); +split_elem (elem, split_pt);malloc_elem_free_list_insert (split_pt); } + +asan_clear_alloczone (elem); +return 0 ; }
lib/eal/common/malloc_elem.h +191 -3
217 lines not on this page · raw message
lib/eal/common/malloc_heap.c +12 -0
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size, unsigned int flags, size_t align, size_t bound, bool contig) { struct malloc_elem *elem; +size_t user_size = size; size =RTE_CACHE_LINE_ROUNDUP (size); align =RTE_CACHE_LINE_ROUNDUP (align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, user_size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,/* increase heap's count of allocated elements */ heap->alloc_count++; + +asan_set_redzone (elem, size); }return elem ==NULL ?NULL : (void *)(&elem[1 ]);
@@ -841,6 +846,8 @@ malloc_heap_free(struct malloc_elem *elem)if (!malloc_elem_cookies_ok (elem) || elem->state !=ELEM_BUSY )return -1 ; +asan_clear_redzone (elem); +/* elem may be merged with previous element, so keep heap address */ heap = elem->heap; msl = elem->msl;
@@ -848,6 +855,9 @@ malloc_heap_free(struct malloc_elem *elem)rte_spinlock_lock (&(heap->lock)); +void *asan_ptr =RTE_PTR_ADD (elem,MALLOC_ELEM_HEADER_LEN + elem->pad); +size_t asan_data_len = elem->size -MALLOC_ELEM_OVERHEAD - elem->pad; +/* mark element as free */ elem->state =ELEM_FREE ;
@@ -1001,6 +1011,8 @@ malloc_heap_free(struct malloc_elem *elem)rte_mcfg_mem_write_unlock (); free_unlock: +asan_set_freezone (asan_ptr, asan_data_len); +rte_spinlock_unlock (&(heap->lock));return ret; }
lib/eal/common/rte_malloc.c +8 -1
@@ -162,6 +162,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align)void *rte_realloc_socket (void *ptr,size_t size,unsigned int align,int socket) { +size_t user_size; +if (ptr ==NULL )return rte_malloc_socket (NULL , size, align, socket);
@@ -171,6 +173,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)return NULL ; } + user_size = size; + size =RTE_CACHE_LINE_ROUNDUP (size), align =RTE_CACHE_LINE_ROUNDUP (align);/* check requested socket id and alignment matches first, and if ok,
@@ -181,6 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) RTE_PTR_ALIGN(ptr, align) == ptr && malloc_heap_resize(elem, size) == 0) {rte_eal_trace_mem_realloc (size, align, socket, ptr); + +asan_set_redzone (elem, user_size); +return ptr; }
@@ -192,7 +199,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)if (new_ptr ==NULL )return NULL ;/* elem: |pad|data_elem|data|trailer| */ -const size_t old_size = elem->size - elem->pad -MALLOC_ELEM_OVERHEAD ; +const size_t old_size =old_malloc_size (elem);rte_memcpy (new_ptr, ptr, old_size < size ? old_size : size);rte_free (ptr);
Signature · 1 line
2.25.1
Re: [dpdk-dev] [PATCH v13 2/4] DPDK code adapts to ASan
Quoted 11 lines · On Wed, Oct 20, 2021 at 9:47 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported on Linux
x86_64. If want to support on other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document, and configure meson file (config/meson.build).Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>Acked-by Anatoly Burakov <[email protected]>
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. """
Quoted 22 lines
---
v7: Split doc and code into two.
v8: No change.
v9: Modify the definition ofRTE_MALLOC_ASAN.
v10:Modify the definition ofRTE_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(-)config/meson.build +0
-0
config/meson.build if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address · line 429
Quoted 5 lines
config/meson.build +2
-0@@ -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.
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever an error is detected. · line 14
Quoted 12 lines
config/meson.build +2
-0+ dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'doc/guides/prog_guide/asan.rst +0
-0@@ -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.
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever an error is detected. · line 19
Quoted 5 lines
doc/guides/prog_guide/asan.rst +4
-0+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.
"""
Quoted 13 lines
doc/guides/prog_guide/asan.rst +13
-0+ +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';
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever an error is detected. · line 42
Quoted 10 lines
doc/guides/prog_guide/asan.rst +10
-0+ +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.
"""
Quoted 39 lines
doc/guides/prog_guide/asan.rst +29
-2+ +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.
Signature · 1 line
David Marchand
Re: [dpdk-dev] [PATCH v13 2/4] DPDK code adapts to ASan
Quoted 141 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Friday, October 29, 2021 5:23 PM
To: Peng, ZhihongX <[email protected]>
Cc: Thomas Monjalon <[email protected]>; Burakov, Anatoly <[email protected]>; Ananyev, Konstantin <[email protected]>; Stephen Hemminger <[email protected]>; Dumitrescu, Cristian <[email protected]>; Mcnamara, John <[email protected]>; Richardson, Bruce <[email protected]>; dev <[email protected]>; Lin, Xueqin <[email protected]>
Subject: Re: [PATCH v13 2/4] DPDK code adapts to ASanQuoted 11 lines · On Wed, Oct 20, 2021 at 9:47 AM <[email protected]> wrote:
From: Zhihong Peng <[email protected]>
DPDK ASan functionality is currently only supported on Linux
x86_64. If want to support on other platforms, need to defineASAN_SHADOW_OFFSETvalue according to google ASan document, and configure meson file (config/meson.build).Signed-off-by Xueqin Lin <[email protected]>Signed-off-by Zhihong Peng <[email protected]>Acked-by Anatoly Burakov <[email protected]>Suggests following title/commitlog:
"""
mem: instrument allocator for ASanThis 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_OFFSETmust be defined andRTE_MALLOC_ASANmust be set accordingly in meson. """Quoted 8 lines
---
v7: Split doc and code into two.
v8: No change.
v9: Modify the definition ofRTE_MALLOC_ASAN.
v10:Modify the definition ofRTE_MALLOC_ASAN.
v11:No change.
v12:No change.
v13:Modify the document.Quoted 5 lines
--- 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++++++++++++++++++++++++++++++++-
config/meson.build if get_option('b_sanitize') == 'address' or
Quoted 9 lines
lib/eal/common/malloc_heap.c | 12 ++ lib/eal/common/rte_malloc.c | 9 +- 6 files changed, 296 insertions(+), 7 deletions(-)config/meson.build index renamed from config/meson.build +0
-0
f02b0e9c6d..bf751583bd100644config/meson.build +0
-0@@ -425,6 +425,10 @@ if get_option('b_sanitize') == 'address' or
get_option('b_sanitize')== 'addressconfig/meson.build if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address · line 429
Quoted 5 lines
config/meson.build +2
-0add_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.doc/guides/prog_guide/asan.rst printing out detailed debug information whenever
Quoted 10 lines
config/meson.build +2
-0+ dpdk_conf.set10('RTE_MALLOC_ASAN', true) + endif endif if get_option('default_library') == 'both'(quoted diff) +0
-0
b/doc/guides/prog_guide/asan.rstindex6888fc9a87..02591ca68a100644doc/guides/prog_guide/asan.rst +0
-0@@ -13,6 +13,58 @@ printing out detailed debug information wheneveran error is detected.
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever an error is detected. · line 14
Quoted 2 lines
doc/guides/prog_guide/asan.rst +0
-0AddressSanitizer is a part of LLVM (3.1+) and GCC (4.8+).I'll move the examples after the explanations on how to enable ASan.
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 17
Quoted 5 lines
doc/guides/prog_guide/asan.rst +4
-0+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 onx86_64at the moment.
Other architectures may have to defineASAN_SHADOW_OFFSET.
"""Quoted 5 lines
doc/guides/prog_guide/asan.rst +5
-0+ +Example heap-buffer-overflow error +---------------------------------- + +Add below unit test code in examples/helloworld/main.c::doc/guides/prog_guide/asan.rst printing out detailed debug information whenever an error is detected.
Quoted 10 lines
doc/guides/prog_guide/asan.rst +10
-0+ + 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::
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 34
Quoted 2 lines
doc/guides/prog_guide/asan.rst +2
-0+ + ==369953==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x7fb17f465809at pc0x5652e6707b84bp0x7ffea70eea20sp0x7ffea70eea10WRITE of size 1 at0x7fb17f465809thread T0doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 36
Quoted 2 lines
doc/guides/prog_guide/asan.rst +2
-0+ #0 0x5652e6707b83 in main ../examples/helloworld/main.c:47 + #1 0x7fb94953c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 38
Quoted 2 lines
doc/guides/prog_guide/asan.rst +2
-0+ #2 0x5652e67079bd in _start + (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-hellow
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 43
Quoted 5 lines
doc/guides/prog_guide/asan.rst +5
-0+ orld+0x8329bd) + + Address 0x7fb17f465809 is a wild pointer. + SUMMARY: AddressSanitizer: heap-buffer-overflow + ../examples/helloworld/main.c:47 in mainI'll move the note update here:
"""
Note::
Quoted 6 lines
- 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."""
I agree with the above changes.
Quoted 57 lines
Quoted 6 lines
doc/guides/prog_guide/asan.rst +6
-0+ +Example use-after-free error +---------------------------- + +Add below unit test code in examples/helloworld/main.c:: +doc/guides/prog_guide/asan.rst printing out detailed debug information whenever an error is detected.
Quoted 10 lines
doc/guides/prog_guide/asan.rst +10
-0+ 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, becauseapply 9 bytes of memory but access the first byte after release, detailed error log as below::
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 61
Quoted 2 lines
doc/guides/prog_guide/asan.rst +2
-0+ + ==417048==ERROR: AddressSanitizer: heap-use-after-free on address
0x7fc83f465800at pc0x564308a39b89bp0x7ffc8c85bf50sp0x7ffc8c85bf40WRITE of size 1 at0x7fc83f465800thread T0doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 63
Quoted 2 lines
doc/guides/prog_guide/asan.rst +2
-0+ #0 0x564308a39b88 in main ../examples/helloworld/main.c:48 + #1 0x7fd0079c60b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever · line 65
Quoted 2 lines
doc/guides/prog_guide/asan.rst +2
-0+ #2 0x564308a399bd in _start + (/home/pzh/asan_test/x86_64-native-linuxapp-gcc/examples/dpdk-hellow
doc/guides/prog_guide/asan.rst printing out detailed debug information whenever
Quoted 7 lines
doc/guides/prog_guide/asan.rst +6
-0+ orld+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 buildsystem:
doc/guides/prog_guide/asan.rst Add following meson build commands to enable ASan
Quoted 3 lines
* gcc::
doc/guides/prog_guide/asan.rst +0
-0@@ -25,6 +77,8 @@ Add following meson build commands to enable ASanin the meson build system:
doc/guides/prog_guide/asan.rst Add following meson build commands to enable ASan in the meson build system:
Quoted 6 lines
doc/guides/prog_guide/asan.rst +1
-2.. 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 memoryapplication location, currently
doc/guides/prog_guide/asan.rst Add following meson build commands to enable ASan · line 80
Quoted 1 line
doc/guides/prog_guide/asan.rst +1
-0+ displayed as a wild pointer') are not currently supported by DPDK'simplementation.
doc/guides/prog_guide/asan.rst Add following meson build commands to enable ASan in the meson build system: · line 84
Quoted 4 lines
doc/guides/prog_guide/asan.rst +3
-0+ 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
PATCH v13 3/4 code changes to avoid the ASan error
In reply to [email protected]From: Zhihong Peng <[email protected]>
Code changes to avoid the following ASan error:
"Control reaches end of non-void function".
---
v7: no change.
v8: no change.
v9: Modify the submit log.
v10:no change.
v11:no change.
v12:Modify the commit log.
v13:Modify the commit log.
--- lib/pipeline/rte_swx_pipeline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/pipeline/rte_swx_pipeline.c +2 -2
@@ -4642,7 +4642,7 @@ instr_meter_translate(struct rte_swx_pipeline *p,return 0 ; } -CHECK (0 ,EINVAL ); +return -EINVAL ; } static inline void
@@ -5937,7 +5937,7 @@ instr_translate(struct rte_swx_pipeline *p, instr, data); -CHECK (0 ,EINVAL ); +return -EINVAL ; } static struct instruction_data *
Signature · 1 line
2.25.1
PATCH v13 4/4 performance-thread: avoid cross compilation fail
In reply to [email protected]From: Zhihong Peng <[email protected]>
Code changes to avoid the following ASan error:
"strncpy specified bound XX equals destination size".
---
v11: Use rte_strlcpy to replace strncpy.
v12: Delete rte_strlcpy's rte_.
v13: Modify the commit log.
--- examples/performance-thread/common/lthread.c | 4 ++-- examples/performance-thread/common/lthread_cond.c | 6 +++--- examples/performance-thread/common/lthread_mutex.c | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-)
examples/performance-thread/common/lthread.c +2 -2
@@ -20,6 +20,7 @@#include <sys/mman.h> #include <rte_log.h> +#include <rte_string_fns.h> #include <ctx.h> #include <stack.h>
@@ -465,6 +466,5 @@ void lthread_set_funcname(const char *f) {struct lthread *lt =THIS_LTHREAD ; -strncpy (lt->funcname, f,sizeof (lt->funcname)); - lt->funcname[sizeof (lt->funcname)-1 ] =0 ; +strlcpy (lt->funcname, f,sizeof (lt->funcname)); }
examples/performance-thread/common/lthread_cond.c +3 -3
@@ -20,6 +20,7 @@#include <rte_log.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_diag_api.h"
@@ -57,10 +58,9 @@ lthread_cond_init(char *name, struct lthread_cond **cond, }if (name ==NULL ) -strncpy (c->name,"no name" ,sizeof (c->name)); +strlcpy (c->name,"no name" ,sizeof (c->name));else -strncpy (c->name, name,sizeof (c->name)); - c->name[sizeof (c->name)-1 ] =0 ; +strlcpy (c->name, name,sizeof (c->name)); c->root_sched =THIS_SCHED ;
examples/performance-thread/common/lthread_mutex.c +3 -3
@@ -19,6 +19,7 @@#include <rte_log.h> #include <rte_spinlock.h> #include <rte_common.h> +#include <rte_string_fns.h> #include "lthread_api.h" #include "lthread_int.h"
@@ -52,10 +53,9 @@ lthread_mutex_init(char *name, struct lthread_mutex **mutex, }if (name ==NULL ) -strncpy (m->name,"no name" ,sizeof (m->name)); +strlcpy (m->name,"no name" ,sizeof (m->name));else -strncpy (m->name, name,sizeof (m->name)); - m->name[sizeof (m->name)-1 ] =0 ; +strlcpy (m->name, name,sizeof (m->name)); m->root_sched =THIS_SCHED ; m->owner =NULL ;
Signature · 1 line
2.25.1