Skip to content

Commit 72d7cb5

Browse files
KCNyushuahkh
authored andcommitted
selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN
This patch addresses an issue in the selftests/harness where an assertion within FIXTURE_TEARDOWN could trigger an infinite loop. The problem arises because the teardown procedure is meant to execute once, but the presence of failing assertions (ASSERT_EQ(0, 1)) leads to repeated attempts to execute teardown due to the long jump mechanism used by the harness for handling assertions. To resolve this, the patch ensures that the teardown process runs only once, regardless of assertion outcomes, preventing the infinite loop and allowing tests to fail. A simple test demo(test.c): #include "kselftest_harness.h" FIXTURE(f) { int fd; }; FIXTURE_SETUP(f) { self->fd = 0; } FIXTURE_TEARDOWN(f) { TH_LOG("TEARDOWN"); ASSERT_EQ(0, 1); self->fd = -1; } TEST_F(f, open_close) { ASSERT_NE(self->fd, 1); } TEST_HARNESS_MAIN will always output the following output due to a dead loop until timeout: # test.c:15:open_close:TEARDOWN # test.c:16:open_close:Expected 0 (0) == 1 (1) # test.c:15:open_close:TEARDOWN # test.c:16:open_close:Expected 0 (0) == 1 (1) ... But here's what we should and expect to get: TAP version 13 1..1 # Starting 1 tests from 2 test cases. # RUN f.open_close ... # test.c:15:open_close:TEARDOWN # test.c:16:open_close:Expected 0 (0) == 1 (1) # open_close: Test terminated by assertion # FAIL f.open_close not ok 1 f.open_close # FAILED: 0 / 1 tests passed. # Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0 also this is related to the issue mentioned in this patch https://patchwork.kernel.org/project/linux-kselftest/patch/[email protected]/ Signed-off-by: Shengyu Li <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 1a4ea83 commit 72d7cb5

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

tools/testing/selftests/kselftest_harness.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
FIXTURE_DATA(fixture_name) self; \
384384
pid_t child = 1; \
385385
int status = 0; \
386+
bool jmp = false; \
386387
memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
387388
if (setjmp(_metadata->env) == 0) { \
388389
/* Use the same _metadata. */ \
@@ -399,8 +400,10 @@
399400
_metadata->exit_code = KSFT_FAIL; \
400401
} \
401402
} \
403+
else \
404+
jmp = true; \
402405
if (child == 0) { \
403-
if (_metadata->setup_completed && !_metadata->teardown_parent) \
406+
if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \
404407
fixture_name##_teardown(_metadata, &self, variant->data); \
405408
_exit(0); \
406409
} \

0 commit comments

Comments
 (0)