XFAIL_ADD() registers expected failures using constructor order, but the
associated struct __test_metadata pointer may not be initialized yet.
As a result, xfail entries can end up with a NULL test pointer and never
match at runtime, causing expected failures to be reported as FAIL.

Store the test case name in the xfail entry and fall back to name-based
matching when the test pointer is unavailable, while keeping the original
pointer-based matching for compatibility.

Fixes: 2709473c9386 ("selftests: kselftest_harness: support using xfail")
Signed-off-by: Sun Jian <[email protected]>
---
 tools/testing/selftests/kselftest_harness.h | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/kselftest_harness.h 
b/tools/testing/selftests/kselftest_harness.h
index 16a119a4656c..1082024b2d77 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -860,6 +860,7 @@ struct __test_xfail {
        struct __fixture_metadata *fixture;
        struct __fixture_variant_metadata *variant;
        struct __test_metadata *test;
+       const char *case_name;
        struct __test_xfail *prev, *next;
 };
 
@@ -873,19 +874,19 @@ struct __test_xfail {
  * to fail. Tests marked this way will report XPASS / XFAIL return codes,
  * instead of PASS / FAIL,and use respective counters.
  */
-#define XFAIL_ADD(fixture_name, variant_name, test_name) \
+#define XFAIL_ADD(fixture_name, variant_name, tc_name) \
        static struct __test_xfail \
-               _##fixture_name##_##variant_name##_##test_name##_xfail = \
+               _##fixture_name##_##variant_name##_##tc_name##_xfail = \
        { \
                .fixture = &_##fixture_name##_fixture_object, \
                .variant = &_##fixture_name##_##variant_name##_object, \
        }; \
        static void __attribute__((constructor)) \
-               
_register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
+               
_register_##fixture_name##_##variant_name##_##tc_name##_xfail(void) \
        { \
-               _##fixture_name##_##variant_name##_##test_name##_xfail.test = \
-                       _##fixture_name##_##test_name##_object; \
-               
__register_xfail(&_##fixture_name##_##variant_name##_##test_name##_xfail); \
+               _##fixture_name##_##variant_name##_##tc_name##_xfail.case_name 
= \
+                       #tc_name; \
+               
__register_xfail(&_##fixture_name##_##variant_name##_##tc_name##_xfail); \
        }
 
 static struct __fixture_metadata *__fixture_list = &_fixture_global;
@@ -1233,7 +1234,10 @@ static void __run_test(struct __fixture_metadata *f,
 
        /* Check if we're expecting this test to fail */
        for (xfail = variant->xfails; xfail; xfail = xfail->next)
-               if (xfail->test == t)
+               if (xfail->test == t ||
+                   (!xfail->test &&
+                    xfail->case_name && t->name &&
+                    !strcmp(xfail->case_name, t->name)))
                        break;
        if (xfail)
                t->exit_code = __test_passed(t) ? KSFT_XPASS : KSFT_XFAIL;

base-commit: 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd
-- 
2.43.0


Reply via email to