From cd79d834bfa130b51f8cb67d8afca9ba538c867a Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Tue, 21 Nov 2023 01:51:50 +0200 Subject: [PATCH] trace: Add TEST_FAIL_TAG macro to allow more narrow matching The tag is inserted as the first item in the stack trace, making it trivial to match against it from the test. Signed-off-by: Benjamin Berg --- src/utils/os.h | 6 ++++-- src/utils/os_unix.c | 25 +++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/utils/os.h b/src/utils/os.h index 83d5ad99b..07abf7a9a 100644 --- a/src/utils/os.h +++ b/src/utils/os.h @@ -667,12 +667,14 @@ int os_exec(const char *program, const char *arg, int wait_completion); #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) -#define TEST_FAIL() testing_test_fail(false) -int testing_test_fail(bool is_alloc); +#define TEST_FAIL() testing_test_fail(NULL, false) +#define TEST_FAIL_TAG(tag) testing_test_fail(tag, false) +int testing_test_fail(const char *tag, bool is_alloc); int testing_set_fail_pattern(bool is_alloc, char *patterns); int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen); #else #define TEST_FAIL() 0 +#define TEST_FAIL_TAG(tag) 0 static inline int testing_set_fail_pattern(bool is_alloc, char *patterns) { return -1; diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c index 60b88f817..d103ae832 100644 --- a/src/utils/os_unix.c +++ b/src/utils/os_unix.c @@ -545,10 +545,10 @@ struct wpa_trace_test_fail { char pattern[256]; } wpa_trace_test_fail[5][2]; -int testing_test_fail(bool is_alloc) +int testing_test_fail(const char *tag, bool is_alloc) { const char *ignore_list[] = { - __func__, "os_malloc", "os_zalloc", "os_calloc", "os_realloc", + "os_malloc", "os_zalloc", "os_calloc", "os_realloc", "os_realloc_array", "os_strdup", "os_memdup" }; const char *func[WPA_TRACE_LEN]; @@ -568,9 +568,22 @@ int testing_test_fail(bool is_alloc) res = wpa_trace_calling_func(func, WPA_TRACE_LEN); i = 0; - /* Skip this function as well as allocation helpers */ - for (j = 0; j < ARRAY_SIZE(ignore_list) && i < res; j++) { - if (os_strcmp(func[i], ignore_list[j]) == 0) + if (is_alloc) { + /* Skip our own stack frame */ + i++; + + /* Skip allocation helpers */ + for (j = 0; j < ARRAY_SIZE(ignore_list) && i < res; j++) { + if (os_strcmp(func[i], ignore_list[j]) == 0) + i++; + } + } else { + /* Not allocation, we might have a tag, if so, replace our + * own stack frame with the tag, otherwise skip it. + */ + if (tag) + func[0] = tag; + else i++; } @@ -702,7 +715,7 @@ void * os_malloc(size_t size) { struct os_alloc_trace *a; - if (testing_test_fail(true)) + if (testing_test_fail(NULL, true)) return NULL; a = malloc(sizeof(*a) + size);