Skip to content

Commit 906dbc1

Browse files
committed
selftests: harness: Move teardown conditional into test metadata
To get rid of setjmp()/longjmp(), the teardown logic needs to be usable from __bail(). To access the atomic teardown conditional from there, move it into the test metadata. This also allows the removal of "setup_completed". Signed-off-by: Thomas Weißschuh <[email protected]> Acked-by: Shuah Khan <[email protected]> Link: https://lore.kernel.org/r/20250505-nolibc-kselftest-harness-v4-9-ee4dd5257135@linutronix.de Signed-off-by: Thomas Weißschuh <[email protected]>
1 parent fb25e99 commit 906dbc1

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

tools/testing/selftests/kselftest_harness.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@
411411
pid_t child = 1; \
412412
int status = 0; \
413413
/* Makes sure there is only one teardown, even when child forks again. */ \
414-
bool *teardown = mmap(NULL, sizeof(*teardown), \
414+
_metadata->no_teardown = mmap(NULL, sizeof(*_metadata->no_teardown), \
415415
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); \
416-
*teardown = false; \
416+
*_metadata->no_teardown = true; \
417417
if (sizeof(*self) > 0) { \
418418
if (fixture_name##_teardown_parent) { \
419419
self = mmap(NULL, sizeof(*self), PROT_READ | PROT_WRITE, \
@@ -431,23 +431,24 @@
431431
/* Let setup failure terminate early. */ \
432432
if (_metadata->exit_code) \
433433
_exit(0); \
434-
_metadata->setup_completed = true; \
434+
*_metadata->no_teardown = false; \
435435
fixture_name##_##test_name(_metadata, self, variant->data); \
436436
} else if (child < 0 || child != waitpid(child, &status, 0)) { \
437437
ksft_print_msg("ERROR SPAWNING TEST GRANDCHILD\n"); \
438438
_metadata->exit_code = KSFT_FAIL; \
439439
} \
440440
} \
441441
if (child == 0) { \
442-
if (_metadata->setup_completed && !fixture_name##_teardown_parent && \
443-
!__atomic_test_and_set(teardown, __ATOMIC_RELAXED)) \
442+
if (!fixture_name##_teardown_parent && \
443+
!__atomic_test_and_set(_metadata->no_teardown, __ATOMIC_RELAXED)) \
444444
fixture_name##_teardown(_metadata, self, variant->data); \
445445
_exit(0); \
446446
} \
447-
if (_metadata->setup_completed && fixture_name##_teardown_parent && \
448-
!__atomic_test_and_set(teardown, __ATOMIC_RELAXED)) \
447+
if (fixture_name##_teardown_parent && \
448+
!__atomic_test_and_set(_metadata->no_teardown, __ATOMIC_RELAXED)) \
449449
fixture_name##_teardown(_metadata, self, variant->data); \
450-
munmap(teardown, sizeof(*teardown)); \
450+
munmap(_metadata->no_teardown, sizeof(*_metadata->no_teardown)); \
451+
_metadata->no_teardown = NULL; \
451452
if (self && fixture_name##_teardown_parent) \
452453
munmap(self, sizeof(*self)); \
453454
if (WIFEXITED(status)) { \
@@ -916,7 +917,7 @@ struct __test_metadata {
916917
int trigger; /* extra handler after the evaluation */
917918
int timeout; /* seconds to wait for test timeout */
918919
bool aborted; /* stopped test due to failed ASSERT */
919-
bool setup_completed; /* did setup finish? */
920+
bool *no_teardown; /* fixture needs teardown */
920921
jmp_buf env; /* for exiting out of test early */
921922
struct __test_results *results;
922923
struct __test_metadata *prev, *next;
@@ -1195,7 +1196,7 @@ static void __run_test(struct __fixture_metadata *f,
11951196
t->exit_code = KSFT_PASS;
11961197
t->trigger = 0;
11971198
t->aborted = false;
1198-
t->setup_completed = false;
1199+
t->no_teardown = NULL;
11991200
memset(t->env, 0, sizeof(t->env));
12001201
memset(t->results->reason, 0, sizeof(t->results->reason));
12011202

0 commit comments

Comments
 (0)