[PATCH 36/42] os: Add TEST_FAIL_TAG macro to allow more narrow matching

Andrei Otcheretianski andrei.otcheretianski at intel.com
Mon Nov 20 15:51:50 PST 2023


From: Benjamin Berg <benjamin.berg at intel.com>

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 at intel.com>
---
 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 83d5ad99b2..07abf7a9a0 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 d9664b0f79..da52f44028 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++;
 	}
 
@@ -691,7 +704,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);
-- 
2.38.1




More information about the Hostap mailing list