lib/tst_test.c: Fix tst_brk() handling
PATCH 1/1 lib/tst_test.c: Fix tst_brk() handling
This makes the tst_brk() handling cleaner and saner as instead of propagating the tst_brk() result in a return value an abort flag is introduced into the shared memory.
Now:
- All the processes but the library one that reports the results exit with 0
- tst_brk(TBROK, ...) increments result conters, sets the abort flag. and exit current process
- all other tst_brk() variants will just increments the countes and exits the current process
This makes the tst_brk() behavior well defined so we can now even call tst_brk() with TFAIL and TPASS as well.
And since TBROK is supposed to exit the test immediatelly (i.e. unrecoverable error) we are now properly doing so.
The case that main test pid called TBROK was working correctly before this patch, since send the SIGKILL signal to he process group after we waited for the main test pid. All that was missing is a code that sends a signal to the main test pid in the case that TBROK was triggered by one of it's children and now we properly kill all test processes in that case as well.
--- include/tst_test.h | 26 +++++---- lib/newlib_tests/.gitignore | 8 ++- lib/newlib_tests/test_brk_child.c | 31 +++++++++++ lib/newlib_tests/test_brk_fail.c | 25 +++++++++ lib/newlib_tests/test_brk_parent.c | 28 ++++++++++ lib/newlib_tests/test_brk_pass.c | 25 +++++++++ lib/newlib_tests/test_brk_variant.c | 19 +++++++ lib/newlib_tests/test_fail_variant.c | 20 +++++++ lib/tst_test.c | 81 +++++++++++++++++----------- 9 files changed, 221 insertions(+), 42 deletions(-) create mode 100644 lib/newlib_tests/test_brk_child.c create mode 100644 lib/newlib_tests/test_brk_fail.c create mode 100644 lib/newlib_tests/test_brk_parent.c create mode 100644 lib/newlib_tests/test_brk_pass.c create mode 100644 lib/newlib_tests/test_brk_variant.c create mode 100644 lib/newlib_tests/test_fail_variant.c
include/tst_test.h +15 -11
@@ -98,28 +98,32 @@ void tst_brk_(const char *file, const int lineno, int ttype, __attribute__ ((format (printf, 4, 5)));/** -* tst_brk() - Reports a breakage and exits the test. +* tst_brk() - Reports a breakage and exits the test or test process. * * @ttype: An enum tst_res_type. * @arg_fmt: A printf-like format. * @...: A printf-like parameters. * -* Reports either TBROK or TCONF and exits the test immediately. When called -* all children in the same process group as the main test library process are -* killed. This function, unless in a test cleanup, calls _exit() and does not -* return. +* Reports a single result and exits immediately. The call behaves differently +* based on the ttype parameter. For all ttype results but TBROK the call exits +* the current test process, i.e. increments test result counters and calls +* exit(0). +* +* The TBROK ttype is special that apart from exitting the current test process +* it also tells to the test library to exit immediatelly. When TBROK is +* triggered by any of the test processes the whole process group is killed so +* that there are no processes left after the library process exits. This also +* means that any subsequent test iterations are not executed, e.g. if a test +* runs for all filesystems and tst_brk() with TBROK is called, the test exits +* and does not attempt to continue a test iteration for the next filtesystem. * * When test is in cleanup() function TBROK is converted into TWARN by the test * library and we attempt to carry on with a cleanup even when tst_brk() was * called. This makes it possible to use SAFE_FOO() macros in the test cleanup * without interrupting the cleanup process on a failure. */ -#define tst_brk (ttype, arg_fmt, ...) \ - ({ \ - TST_BRK_SUPPORTS_ONLY_TCONF_TBROK(!((ttype) & \ - (TBROK | TCONF | TFAIL))); \ - tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__);\ - }) +#define tst_brk (ttype, arg_fmt, ...) \ + tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)void tst_printf (const char *const fmt, ...) __attribute__((nonnull (1 ),format (printf,1 ,2 )));
lib/newlib_tests/.gitignore +7 -1
@@ -58,4 +58,10 @@ test_runtime01 test_runtime02 test_children_cleanup tst_res_flags -tst_safe_sscanf +tst_safe_sscanf +test_brk_child +test_brk_fail +test_brk_parent +test_brk_pass +test_brk_variant +test_fail_variant
lib/newlib_tests/test_brk_child.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 Cyril Hrubis <[email protected]> +*/ + +/* +* Test that tst_brk(TFAIL, ...) exits only single test variant. +*/ +#include "tst_test.h" + +static void do_test (void ) +{ +int i; + +for (i =0 ; i <10 ; i++) { +if (!SAFE_FORK ()) { +tst_res (TINFO ,"Suspending child %i" , i); +pause (); + } + } + +if (!SAFE_FORK ()) +tst_brk (TBROK ,"Child triggers TBROK" ); + +pause (); +} + +static struct tst_test test = { + .test_all = do_test, + .forks_child =1 , +};
lib/newlib_tests/test_brk_fail.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2024 Cyril Hrubis <[email protected]> +*/ + +/* +* Test that tst_brk(TFAIL, ...) works properly. +*/ +#include "tst_test.h" + +static void do_test (void ) +{ +int pid =SAFE_FORK (); + +if (pid) +return ; + +tst_brk (TFAIL ,"Test child exitting..." ); +tst_res (TWARN ,"Test child stil alive!" ); +} + +static struct tst_test test = { + .test_all = do_test, + .forks_child =1 , +};
lib/newlib_tests/test_brk_parent.c new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 Cyril Hrubis <[email protected]> +*/ + +/* +* Test that tst_brk(TFAIL, ...) exits only single test variant. +*/ +#include "tst_test.h" + +static void do_test (void ) +{ +int i; + +for (i =0 ; i <10 ; i++) { +if (!SAFE_FORK ()) { +tst_res (TINFO ,"Suspending child %i" , i); +pause (); + } + } + +tst_brk (TBROK ,"Parent triggers TBROK" ); +} + +static struct tst_test test = { + .test_all = do_test, + .forks_child =1 , +};
lib/newlib_tests/test_brk_pass.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2024 Cyril Hrubis <[email protected]> +*/ + +/* +* Test that tst_brk(TPASS, ...) works properly. +*/ +#include "tst_test.h" + +static void do_test (void ) +{ +int pid =SAFE_FORK (); + +if (pid) +return ; + +tst_brk (TPASS ,"Test child exitting..." ); +tst_res (TWARN ,"Test child stil alive!" ); +} + +static struct tst_test test = { + .test_all = do_test, + .forks_child =1 , +};
lib/newlib_tests/test_brk_variant.c new file +19 -0
@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2024 Cyril Hrubis <[email protected]> +*/ + +/* +* Test that tst_brk(TBROK, ...) exits the test immediatelly. +*/ +#include "tst_test.h" + +static void do_test (void ) +{ +tst_brk (TBROK ,"Exitting the test during the first variant" ); +} + +static struct tst_test test = { + .test_all = do_test, + .test_variants =10 , +};
lib/newlib_tests/test_fail_variant.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2024 Cyril Hrubis <[email protected]> +*/ + +/* +* Test that tst_brk(TFAIL, ...) exits only single test variant. +*/ +#include "tst_test.h" + +static void do_test (void ) +{ +tst_brk (TFAIL ,"Failing a test variant" ); +tst_res (TWARN ,"Shouldn't be reached" ); +} + +static struct tst_test test = { + .test_all = do_test, + .test_variants =10 , +};
lib/tst_test.c +51 -30
159 lines not on this page · raw message
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi all,
[ Cc Jan and Li s there can be some hidden problems. ]
Quoted 13 lines
This makes the
tst_brk()handling cleaner and saner as instead of propagating thetst_brk()result in a return value an abort flag is introduced into the shared memory.Now:
- All the processes but the library one that reports the results exit with 0
-tst_brk(TBROK, ...)increments result conters, sets the abort flag. and exit current process
- all othertst_brk()variants will just increments the countes and exits the current process
Quoted 2 lines
This makes the
tst_brk()behavior well defined so we can now even calltst_brk()with TFAIL and TPASS as well.
+1. As a separate effort we can replace with coccinelle tst_res(TFAIL or TPASS) followed by return by tst_brk(TFAIL or TPASS).
Quoted 9 lines
And since TBROK is supposed to exit the test immediatelly (i.e. unrecoverable error) we are now properly doing so.
The case that main test pid called TBROK was working correctly before this patch, since send the SIGKILL signal to he process group after we waited for the main test pid. All that was missing is a code that sends a signal to the main test pid in the case that TBROK was triggered by one of it's children and now we properly kill all test processes in that case as well.
+1
Also thanks for doing tests.
Generally LGTM.
One thing I'm worried is the fact that some shell loader tests core dumped in CI:
https://github.com/pevik/ltp/actions/runs/14242818586/job/39916477770
e.g. these which are supposed to TBROK due broken metadata:shell_loader_invalid_block.sh, shell_loader_no_metadata.sh, shell_loader_wrong_metadata.sh:
Segmentation fault (core dumped)
I also wonder if tst_brk() related doc should be updated.
Isn't there anything which should be updated in doc/old/C-Test-API.asciidoc ? This docs will stay with us for some time, the conversion to kerneldoc takes time, it'd be good to keep it updated (valuable texts will be migrated to kerneldoc).
Maybe parts:
1.8 Doing the test in the child process
1.9 Fork() and Parent-child synchronization
(both code examples and the description).
very nit: please before merge fix typos in both code and commit message:
exitting => exiting
countes|countes => counters
immediatelly => immediately
filtesystem => filesystem
NOTE: test_brk_pass could be added to lib/newlib_tests/runtest.sh. I would also prefer, if we changed tests to behave like testcases/lib/run_tests.sh, i.e. allow to run all tests and check exit code (intermediate step before we compare the test output).
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 7 lines
One thing I'm worried is the fact that some shell loader tests core dumped in CI:
https://github.com/pevik/ltp/actions/runs/14242818586/job/39916477770
e.g. these which are supposed to TBROK due broken metadata:shell_loader_invalid_block.sh,shell_loader_no_metadata.sh,shell_loader_wrong_metadata.sh:Segmentation fault (core dumped)
Ah, that's because if we call tst_brk() before we finished initialization results is not defined. We need at least:
lib/tst_test.c +2 -1
@@ -407,7 +407,8 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* current process. */ if (TTYPE_RESULT (ttype) ==TBROK ) { -tst_atomic_inc (&results->abort_flag); +if (results) +tst_atomic_inc (&results->abort_flag);/* * If TBROK was called from one of the child processes we kill
Quoted 1 line
I also wonder if
tst_brk()related doc should be updated.
I did update the documentation comment in the tst_test.h.
Quoted 9 lines
Isn't there anything which should be updated in doc/old/C-Test-API.asciidoc ? This docs will stay with us for some time, the conversion to kerneldoc takes time, it'd be good to keep it updated (valuable texts will be migrated to kerneldoc).
Maybe parts:
1.8 Doing the test in the child process
1.9Fork()and Parent-child synchronization
(both code examples and the description).
I will have a look.
Quoted 5 lines
very nit: please before merge fix typos in both code and commit message:
exitting => exiting
countes|countes => counters
immediatelly => immediately
filtesystem => filesyste
Uff, will do.
Quoted 4 lines
NOTE:
test_brk_passcould be added tolib/newlib_tests/runtest.sh. I would also prefer, if we changed tests to behave liketestcases/lib/run_tests.sh, i.e. allow to run all tests and check exit code (intermediate step before we compare the test output).
Rewriting the tests is a more complex task that should be done in a separate patchset.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 6 lines
lib/tst_test.c +0
-0
lib/tst_test.c void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, · line 414
Quoted 9 lines
lib/tst_test.c +2
-1@@ -407,7 +407,8 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* current process. */ if (TTYPE_RESULT (ttype) ==TBROK ) { -tst_atomic_inc (&results->abort_flag); +if (results) +tst_atomic_inc (&results->abort_flag);/* * If TBROK was called from one of the child processes we kill
And it's a bit more complex we also need to make sure to exit properly when tst_brk() was called before the library was initialized, so we also need:
lib/tst_test.c +9 -1
@@ -386,6 +386,14 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, va_list va) {print_result (file, lineno, ttype, fmt, va); + +/* +* If tst_brk() is called from some of the C helpers even before the +* library was initialized, just exit. +*/ +if (!lib_pid) +exit (TTYPE_RESULT (ttype)); +update_results (TTYPE_RESULT (ttype));/*
@@ -415,7 +423,7 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* the main test process. That in turn triggers the code that * kills leftover children once the main test process did exit. */ -if (tst_getpid () != main_pid) { +if (main_pid &&tst_getpid () != main_pid) {tst_res (TINFO ,"Child process reported TBROK killing the test" );kill (main_pid,SIGKILL ); }
I will send v2 later on.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 13 lines
Hi!
Quoted 11 lines
lib/tst_test.c +2
-1@@ -407,7 +407,8 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* current process. */ if (TTYPE_RESULT (ttype) ==TBROK ) { -tst_atomic_inc (&results->abort_flag); +if (results) +tst_atomic_inc (&results->abort_flag);
Quoted 6 lines
lib/tst_test.c void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, · line 413
Quoted 2 lines
lib/tst_test.c +0
-0/* * If TBROK was called from one of the child processes we kill And it's a bit more complex we also need to make sure to exit properly when
tst_brk()was called before the library was initialized, so we also
+1
Quoted 7 lines
need:
lib/tst_test.c +0
-0
lib/tst_test.c void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, · line 395
Quoted 10 lines
lib/tst_test.c +7
-0@@ -386,6 +386,14 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, va_list va) {print_result (file, lineno, ttype, fmt, va); + +/* +* If tst_brk() is called from some of the C helpers even before the +* library was initialized, just exit. +*/ +if (!lib_pid) +exit (TTYPE_RESULT (ttype));
Interesting, I never noticed lib_pid :). It's assigned in tst_run_tcases() - library gets initialized there. As that's the very first call in main(), this really applies for helpers in testcases/lib/ which define TST_NO_DEFAULT_MAIN.
I suppose this is the part of the fix for core dumped helpers.
Quoted 15 lines
lib/tst_test.c +2
-1+update_results (TTYPE_RESULT (ttype));/* @@ -415,7 +423,7 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* the main test process. That in turn triggers the code that * kills leftover children once the main test process did exit. */ -if (tst_getpid () != main_pid) { +if (main_pid &&tst_getpid () != main_pid) {tst_res (TINFO ,"Child process reported TBROK killing the test" );kill (main_pid,SIGKILL ); }
Quoted 1 line
I will send v2 later on.
+1
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 62 lines · Petr Vorel
Quoted 14 lines
Hi!
Quoted 11 lines
lib/tst_test.c +2
-1@@ -407,7 +407,8 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* current process. */ if (TTYPE_RESULT (ttype) ==TBROK ) { -tst_atomic_inc (&results->abort_flag); +if (results) +tst_atomic_inc (&results->abort_flag);Quoted 6 lines
lib/tst_test.c void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, · line 413
Quoted 2 lines
lib/tst_test.c +0
-0/* * If TBROK was called from one of the child processes we kill And it's a bit more complex we also need to make sure to exit properly when
tst_brk()was called before the library was initialized, so we also+1
Quoted 7 lines
need:
lib/tst_test.c +0
-0lib/tst_test.c void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, · line 395
Quoted 10 lines
lib/tst_test.c +7
-0@@ -386,6 +386,14 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt, va_list va) {print_result (file, lineno, ttype, fmt, va); + +/* +* If tst_brk() is called from some of the C helpers even before the +* library was initialized, just exit. +*/ +if (!lib_pid) +exit (TTYPE_RESULT (ttype));Interesting, I never noticed
lib_pid:). It's assigned intst_run_tcases()- library gets initialized there. As that's the very first call inmain(), this really applies for helpers in testcases/lib/ which defineTST_NO_DEFAULT_MAIN.I suppose this is the part of the fix for core dumped helpers.
Quoted 13 lines
lib/tst_test.c +2
-1+update_results (TTYPE_RESULT (ttype));/* @@ -415,7 +423,7 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* the main test process. That in turn triggers the code that * kills leftover children once the main test process did exit. */ -if (tst_getpid () != main_pid) { +if (main_pid &&tst_getpid () != main_pid) {tst_res (TINFO ,"Child process reported TBROK killing the test" );kill (main_pid,SIGKILL ); }
Quoted 3 lines
Quoted 1 line
I will send v2 later on.
+1
+1, I wanted to have a look too, and it will be easier to follow having it all in together.
Quoted 7 lines
Kind regards,
Petr--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 5 lines
Interesting, I never noticed
lib_pid:). It's assigned intst_run_tcases()- library gets initialized there. As that's the very first call inmain(), this really applies for helpers in testcases/lib/ which defineTST_NO_DEFAULT_MAIN.I suppose this is the part of the fix for core dumped helpers.
The tst_run_shell.c needs to fill in the tst_test structure first and that is done before the test library was initialized. If we happen to call tst_brk() in that phase we have to make sure that we do not touch the results structure, which wasn't mapped yet and that we exit() with right exit value. After these changes, once the test library has been initialized all processes but the test library has to exit with success and the final exit value is produce by the test library process.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Cyril,
Quoted 27 lines
Hi!
Quoted 7 lines
One thing I'm worried is the fact that some shell loader tests core dumped in CI:
https://github.com/pevik/ltp/actions/runs/14242818586/job/39916477770
e.g. these which are supposed to TBROK due broken metadata:shell_loader_invalid_block.sh,shell_loader_no_metadata.sh,shell_loader_wrong_metadata.sh:Segmentation fault (core dumped)
Ah, that's because if we call
tst_brk()before we finished initialization results is not defined. We need at least:lib/tst_test.c +2
-1@@ -407,7 +407,8 @@ void tst_vbrk_(const char *file, const int lineno, int ttype, const char *fmt,* current process. */ if (TTYPE_RESULT (ttype) ==TBROK ) { -tst_atomic_inc (&results->abort_flag); +if (results) +tst_atomic_inc (&results->abort_flag);/* * If TBROK was called from one of the child processes we kill
Quoted 3 lines
Quoted 1 line
I also wonder if
tst_brk()related doc should be updated.I did update the documentation comment in the
tst_test.h.
I'm sorry to overlook this. The description LGTM.
+ * runs for all filesystems and tst_brk() with TBROK is called, the test exits
+ * and does not attempt to continue a test iteration for the next filesystem.
I guess this part of the doc update is also applicable for the current master, right? Quite important note, indeed TCONF does not quit testing on all_filesystems. Probably not worth to have it as a separate commit (hopefully this change will not get reverted).
Quoted 11 lines
Quoted 9 lines
Isn't there anything which should be updated in doc/old/C-Test-API.asciidoc ? This docs will stay with us for some time, the conversion to kerneldoc takes time, it'd be good to keep it updated (valuable texts will be migrated to kerneldoc).
Maybe parts:
1.8 Doing the test in the child process
1.9Fork()and Parent-child synchronization
(both code examples and the description).I will have a look.
Thanks!
Quoted 7 lines
Quoted 5 lines
very nit: please before merge fix typos in both code and commit message:
exitting => exiting
countes|countes => counters
immediatelly => immediately
filtesystem => filesysteUff, will do.
Thanks. (I have spell checker on patches, thus I see it red.)
Quoted 7 lines
Quoted 4 lines
NOTE:
test_brk_passcould be added tolib/newlib_tests/runtest.sh. I would also prefer, if we changed tests to behave liketestcases/lib/run_tests.sh, i.e. allow to run all tests and check exit code (intermediate step before we compare the test output).Rewriting the tests is a more complex task that should be done in a separate patchset.
Sure, this was more sharing my idea (I planned to write "as a separate effort", I see I forget).
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp