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 <benjamin.berg@intel.com>
This commit is contained in:
Benjamin Berg 2023-11-21 01:51:50 +02:00 committed by Jouni Malinen
parent a5d5b63d61
commit cd79d834bf
2 changed files with 23 additions and 8 deletions

View file

@ -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) #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
#define TEST_FAIL() testing_test_fail(false) #define TEST_FAIL() testing_test_fail(NULL, false)
int testing_test_fail(bool is_alloc); #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_set_fail_pattern(bool is_alloc, char *patterns);
int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen); int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen);
#else #else
#define TEST_FAIL() 0 #define TEST_FAIL() 0
#define TEST_FAIL_TAG(tag) 0
static inline int testing_set_fail_pattern(bool is_alloc, char *patterns) static inline int testing_set_fail_pattern(bool is_alloc, char *patterns)
{ {
return -1; return -1;

View file

@ -545,10 +545,10 @@ struct wpa_trace_test_fail {
char pattern[256]; char pattern[256];
} wpa_trace_test_fail[5][2]; } 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[] = { 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" "os_realloc_array", "os_strdup", "os_memdup"
}; };
const char *func[WPA_TRACE_LEN]; const char *func[WPA_TRACE_LEN];
@ -568,11 +568,24 @@ int testing_test_fail(bool is_alloc)
res = wpa_trace_calling_func(func, WPA_TRACE_LEN); res = wpa_trace_calling_func(func, WPA_TRACE_LEN);
i = 0; i = 0;
/* Skip this function as well as allocation helpers */ if (is_alloc) {
/* Skip our own stack frame */
i++;
/* Skip allocation helpers */
for (j = 0; j < ARRAY_SIZE(ignore_list) && i < res; j++) { for (j = 0; j < ARRAY_SIZE(ignore_list) && i < res; j++) {
if (os_strcmp(func[i], ignore_list[j]) == 0) if (os_strcmp(func[i], ignore_list[j]) == 0)
i++; 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++;
}
pos = wpa_trace_test_fail[is_alloc][idx].pattern; pos = wpa_trace_test_fail[is_alloc][idx].pattern;
@ -702,7 +715,7 @@ void * os_malloc(size_t size)
{ {
struct os_alloc_trace *a; struct os_alloc_trace *a;
if (testing_test_fail(true)) if (testing_test_fail(NULL, true))
return NULL; return NULL;
a = malloc(sizeof(*a) + size); a = malloc(sizeof(*a) + size);