selftests/harness: Handle timeouts cleanly
Subsystems (2)
v2 cover selftests/harness: Handle timeouts cleanly
Repeating patch 2/2's commit log:
When a selftest would timeout before, the program would just fall over and no accounting of failures would be reported (i.e. it would result in an incomplete TAP report). Instead, add an explicit SIGALRM handler to cleanly catch and report the timeout.
Before:
[==========] Running 2 tests from 2 test cases.
[ RUN ] timeout.finish
[ OK ] timeout.finish
[ RUN ] timeout.too_long
Alarm clock
After:
[==========] Running 2 tests from 2 test cases.
[ RUN ] timeout.finish
[ OK ] timeout.finish
[ RUN ] timeout.too_long
timeout.too_long: Test terminated by timeout
[ FAIL ] timeout.too_long
[==========] 1 / 2 tests passed.
[ FAILED ]
Thanks!
-Kees
v2:
- fix typo in subject prefix
v1: https://lore.kernel.org/lkml/[email protected]
Kees Cook (2):
selftests/harness: Move test child waiting logic selftests/harness: Handle timeouts cleanly
tools/testing/selftests/kselftest_harness.h | 144 ++++++++++++++------ 1 file changed, 99 insertions(+), 45 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/2 selftests/harness: Move test child waiting logic
In order to better handle timeout failures, rearrange the child waiting logic into a separate function. This is mostly a copy/paste with an indentation change. To handle pid tracking, a new field is added for the child pid. Also move the alarm() pairing into the function.
--- tools/testing/selftests/kselftest_harness.h | 93 +++++++++++---------- 1 file changed, 49 insertions(+), 44 deletions(-)
tools/testing/selftests/kselftest_harness.h +49 -44
@@ -635,6 +635,7 @@struct __test_metadata {const char *name;void (*fn)(struct __test_metadata *); +pid_t pid;/* pid of test when being run */ int termsig;int passed;int trigger;/* extra handler after the evaluation */
@@ -695,64 +696,68 @@ static inline int __bail(int for_realz, bool no_print, __u8 step)return 0 ; } -void __run_test (struct __test_metadata *t) +void __wait_for_test (struct __test_metadata *t) { -pid_t child_pid;int status; +alarm (t->timeout); +waitpid (t->pid, &status,0 ); +alarm (0 ); + +if (WIFEXITED (status)) { + t->passed = t->termsig ==-1 ? !WEXITSTATUS (status) :0 ; +if (t->termsig !=-1 ) { +fprintf (TH_LOG_STREAM , +"%s: Test exited normally " +"instead of by signal (code: %d)\n" , + t->name, +WEXITSTATUS (status)); + }else if (!t->passed) { +fprintf (TH_LOG_STREAM , +"%s: Test failed at step #%d\n" , + t->name, +WEXITSTATUS (status)); + } + }else if (WIFSIGNALED (status)) { + t->passed =0 ; +if (WTERMSIG (status) ==SIGABRT ) { +fprintf (TH_LOG_STREAM , +"%s: Test terminated by assertion\n" , + t->name); + }else if (WTERMSIG (status) == t->termsig) { + t->passed =1 ; + }else { +fprintf (TH_LOG_STREAM , +"%s: Test terminated unexpectedly " +"by signal %d\n" , + t->name, +WTERMSIG (status)); + } + }else { +fprintf (TH_LOG_STREAM , +"%s: Test ended in some other way [%u]\n" , + t->name, + status); + } +} + +void __run_test (struct __test_metadata *t) +{ t->passed =1 ; t->trigger =0 ;printf ("[ RUN ] %s\n" , t->name); -alarm (t->timeout); - child_pid =fork (); -if (child_pid <0 ) { + t->pid =fork (); +if (t->pid <0 ) {printf ("ERROR SPAWNING TEST CHILD\n" ); t->passed =0 ; - }else if (child_pid ==0 ) { + }else if (t->pid ==0 ) { t->fn (t);/* return the step that failed or 0 */ _exit (t->passed ?0 : t->step); }else { -/* TODO(wad) add timeout support. */ -waitpid (child_pid, &status,0 ); -if (WIFEXITED (status)) { - t->passed = t->termsig ==-1 ? !WEXITSTATUS (status) :0 ; -if (t->termsig !=-1 ) { -fprintf (TH_LOG_STREAM , -"%s: Test exited normally " -"instead of by signal (code: %d)\n" , - t->name, -WEXITSTATUS (status)); - }else if (!t->passed) { -fprintf (TH_LOG_STREAM , -"%s: Test failed at step #%d\n" , - t->name, -WEXITSTATUS (status)); - } - }else if (WIFSIGNALED (status)) { - t->passed =0 ; -if (WTERMSIG (status) ==SIGABRT ) { -fprintf (TH_LOG_STREAM , -"%s: Test terminated by assertion\n" , - t->name); - }else if (WTERMSIG (status) == t->termsig) { - t->passed =1 ; - }else { -fprintf (TH_LOG_STREAM , -"%s: Test terminated unexpectedly " -"by signal %d\n" , - t->name, -WTERMSIG (status)); - } - }else { -fprintf (TH_LOG_STREAM , -"%s: Test ended in some other way [%u]\n" , - t->name, - status); - } +__wait_for_test (t); }printf ("[ %4s ] %s\n" , (t->passed ?"OK" :"FAIL" ), t->name); -alarm (0 ); } static int test_harness_run(int __attribute__((unused)) argc,
Signature · 1 line
2.20.1
shuah
maintainerRe: [PATCH v2 1/2] selftests/harness: Move test child waiting logic
Hi Kees,
Quoted 6 lines · Kees Cook
In order to better handle timeout failures, rearrange the child waiting logic into a separate function. This is mostly a copy/paste with an indentation change. To handle pid tracking, a new field is added for the child pid. Also move the
alarm()pairing into the function.Signed-off-by Kees Cook <[email protected]>
Sorry for the delay. I was on vacation all of last week and totally trying to stay away from checking email.
My commit scripts falied on checkpatch warnings.
A couple of comments below:
Quoted 4 lines
--- tools/testing/selftests/kselftest_harness.h | 93 +++++++++++---------- 1 file changed, 49 insertions(+), 44 deletions(-)Review
Quoted 1 line
(quoted diff) +0
-0
b/tools/testing/selftests/kselftest_harness.h
Quoted 23 lines
index
5336b26506ab..c7b67e379219100644tools/testing/selftests/kselftest_harness.h +4
-2@@ -635,6 +635,7 @@struct __test_metadata {const char *name;void (*fn)(struct __test_metadata *); +pid_t pid;/* pid of test when being run */ int termsig;int passed;int trigger;/* extra handler after the evaluation */ @@ -695,64 +696,68 @@ static inline int __bail(int for_realz, bool no_print, __u8 step)return 0 ; } -void __run_test (struct __test_metadata *t) +void __wait_for_test (struct __test_metadata *t) { -pid_t child_pid;int status; +alarm (t->timeout); +waitpid (t->pid, &status,0 );
tools/testing/selftests/kselftest_harness.h static inline int __bail(int for_realz, bool no_print, __u8 step) · line 714
Quoted 10 lines
tools/testing/selftests/kselftest_harness.h +10
-0+alarm (0 ); + +if (WIFEXITED (status)) { + t->passed = t->termsig ==-1 ? !WEXITSTATUS (status) :0 ; +if (t->termsig !=-1 ) { +fprintf (TH_LOG_STREAM , +"%s: Test exited normally " +"instead of by signal (code: %d)\n" , + t->name, +WEXITSTATUS (status));
This one isn't an error and gets printed to stderr? Except for this one message, all other messages in this routine are indeed errors. It makes sense to print this out to stdout instead.
Quoted 82 lines
tools/testing/selftests/kselftest_harness.h +34
-38+ }else if (!t->passed) { +fprintf (TH_LOG_STREAM , +"%s: Test failed at step #%d\n" , + t->name, +WEXITSTATUS (status)); + } + }else if (WIFSIGNALED (status)) { + t->passed =0 ; +if (WTERMSIG (status) ==SIGABRT ) { +fprintf (TH_LOG_STREAM , +"%s: Test terminated by assertion\n" , + t->name); + }else if (WTERMSIG (status) == t->termsig) { + t->passed =1 ; + }else { +fprintf (TH_LOG_STREAM , +"%s: Test terminated unexpectedly " +"by signal %d\n" , + t->name, +WTERMSIG (status)); + } + }else { +fprintf (TH_LOG_STREAM , +"%s: Test ended in some other way [%u]\n" , + t->name, + status); + } +} + +void __run_test (struct __test_metadata *t) +{ t->passed =1 ; t->trigger =0 ;printf ("[ RUN ] %s\n" , t->name); -alarm (t->timeout); - child_pid =fork (); -if (child_pid <0 ) { + t->pid =fork (); +if (t->pid <0 ) {printf ("ERROR SPAWNING TEST CHILD\n" ); t->passed =0 ; - }else if (child_pid ==0 ) { + }else if (t->pid ==0 ) { t->fn (t);/* return the step that failed or 0 */ _exit (t->passed ?0 : t->step); }else { -/* TODO(wad) add timeout support. */ -waitpid (child_pid, &status,0 ); -if (WIFEXITED (status)) { - t->passed = t->termsig ==-1 ? !WEXITSTATUS (status) :0 ; -if (t->termsig !=-1 ) { -fprintf (TH_LOG_STREAM , -"%s: Test exited normally " -"instead of by signal (code: %d)\n" , - t->name, -WEXITSTATUS (status)); - }else if (!t->passed) { -fprintf (TH_LOG_STREAM , -"%s: Test failed at step #%d\n" , - t->name, -WEXITSTATUS (status)); - } - }else if (WIFSIGNALED (status)) { - t->passed =0 ; -if (WTERMSIG (status) ==SIGABRT ) { -fprintf (TH_LOG_STREAM , -"%s: Test terminated by assertion\n" , - t->name); - }else if (WTERMSIG (status) == t->termsig) { - t->passed =1 ; - }else { -fprintf (TH_LOG_STREAM , -"%s: Test terminated unexpectedly " -"by signal %d\n" , - t->name, -WTERMSIG (status)); - } - }else { - fprintf(TH_LOG_STREAM, - "%s: Test ended in some other way [%u]\n",
tools/testing/selftests/kselftest_harness.h static inline int __bail(int for_realz, bool no_print, __u8 step) · line 763
Quoted 10 lines
tools/testing/selftests/kselftest_harness.h +1
-4- t->name, - status); - } +__wait_for_test (t); }printf ("[ %4s ] %s\n" , (t->passed ?"OK" :"FAIL" ), t->name); -alarm (0 ); } static int test_harness_run(int __attribute__((unused)) argc,
thanks,
-- Shuah
Re: [PATCH v2 1/2] selftests/harness: Move test child waiting logic
Quoted 8 lines · shuah
Hi Kees,
Quoted 5 lines · Kees Cook
In order to better handle timeout failures, rearrange the child waiting logic into a separate function. This is mostly a copy/paste with an indentation change. To handle pid tracking, a new field is added for the child pid. Also move the
alarm()pairing into the function.
Quoted 4 lines
Quoted 1 line
Signed-off-by Kees Cook <[email protected]>Sorry for the delay. I was on vacation all of last week and totally trying to stay away from checking email.
Indeed; that's what vacation is for! :)
Quoted 1 line
My commit scripts falied on checkpatch warnings.
All the macros in this file tend to anger checkpatch. ;)
Quoted 36 lines
A couple of comments below:
Quoted 4 lines
--- tools/testing/selftests/kselftest_harness.h | 93 +++++++++++---------- 1 file changed, 49 insertions(+), 44 deletions(-)Review
Quoted 1 line
(quoted diff) +0
-0
b/tools/testing/selftests/kselftest_harness.hQuoted 27 lines
index
5336b26506ab..c7b67e379219100644tools/testing/selftests/kselftest_harness.h +10
-2@@ -635,6 +635,7 @@struct __test_metadata {const char *name;void (*fn)(struct __test_metadata *); +pid_t pid;/* pid of test when being run */ int termsig;int passed;int trigger;/* extra handler after the evaluation */ @@ -695,64 +696,68 @@ static inline int __bail(int for_realz, bool no_print, __u8 step)return 0 ; } -void __run_test (struct __test_metadata *t) +void __wait_for_test (struct __test_metadata *t) { -pid_t child_pid;int status; +alarm (t->timeout); +waitpid (t->pid, &status,0 ); +alarm (0 ); + +if (WIFEXITED (status)) { + t->passed = t->termsig ==-1 ? !WEXITSTATUS (status) :0 ; +if (t->termsig !=-1 ) { + fprintf(TH_LOG_STREAM,
Quoted 8 lines
tools/testing/selftests/kselftest_harness.h static inline int __bail(int for_realz, bool no_print, __u8 step) · line 714
Quoted 4 lines
tools/testing/selftests/kselftest_harness.h +4
-0+"%s: Test exited normally " +"instead of by signal (code: %d)\n" , + t->name, + WEXITSTATUS(status));This one isn't an error and gets printed to stderr? Except for this one message, all other messages in this routine are indeed errors. It makes sense to print this out to stdout instead.
No, this is an error. A signal was _expected_ in this case, but the program exited without a signal. i.e. the test case failed, and the error is reported to stderr.
(This portion, btw, is just moved from below.)
Quoted 87 lines
Quoted 87 lines
tools/testing/selftests/kselftest_harness.h +35
-41+ }else if (!t->passed) { +fprintf (TH_LOG_STREAM , +"%s: Test failed at step #%d\n" , + t->name, +WEXITSTATUS (status)); + } + }else if (WIFSIGNALED (status)) { + t->passed =0 ; +if (WTERMSIG (status) ==SIGABRT ) { +fprintf (TH_LOG_STREAM , +"%s: Test terminated by assertion\n" , + t->name); + }else if (WTERMSIG (status) == t->termsig) { + t->passed =1 ; + }else { +fprintf (TH_LOG_STREAM , +"%s: Test terminated unexpectedly " +"by signal %d\n" , + t->name, +WTERMSIG (status)); + } + }else { +fprintf (TH_LOG_STREAM , +"%s: Test ended in some other way [%u]\n" , + t->name, + status); + } +} + +void __run_test (struct __test_metadata *t) +{ t->passed =1 ; t->trigger =0 ;printf ("[ RUN ] %s\n" , t->name); -alarm (t->timeout); - child_pid =fork (); -if (child_pid <0 ) { + t->pid =fork (); +if (t->pid <0 ) {printf ("ERROR SPAWNING TEST CHILD\n" ); t->passed =0 ; - }else if (child_pid ==0 ) { + }else if (t->pid ==0 ) { t->fn (t);/* return the step that failed or 0 */ _exit (t->passed ?0 : t->step); }else { -/* TODO(wad) add timeout support. */ -waitpid (child_pid, &status,0 ); -if (WIFEXITED (status)) { - t->passed = t->termsig ==-1 ? !WEXITSTATUS (status) :0 ; -if (t->termsig !=-1 ) { -fprintf (TH_LOG_STREAM , -"%s: Test exited normally " -"instead of by signal (code: %d)\n" , - t->name, -WEXITSTATUS (status)); - }else if (!t->passed) { -fprintf (TH_LOG_STREAM , -"%s: Test failed at step #%d\n" , - t->name, -WEXITSTATUS (status)); - } - }else if (WIFSIGNALED (status)) { - t->passed =0 ; -if (WTERMSIG (status) ==SIGABRT ) { -fprintf (TH_LOG_STREAM , -"%s: Test terminated by assertion\n" , - t->name); - }else if (WTERMSIG (status) == t->termsig) { - t->passed =1 ; - }else { -fprintf (TH_LOG_STREAM , -"%s: Test terminated unexpectedly " -"by signal %d\n" , - t->name, -WTERMSIG (status)); - } - }else { -fprintf (TH_LOG_STREAM , -"%s: Test ended in some other way [%u]\n" , - t->name, - status); - } +__wait_for_test (t); }
Quoted 8 lines
tools/testing/selftests/kselftest_harness.h static inline int __bail(int for_realz, bool no_print, __u8 step)
Quoted 5 lines
tools/testing/selftests/kselftest_harness.h +0
-1printf ("[ %4s ] %s\n" , (t->passed ?"OK" :"FAIL" ), t->name); -alarm (0 ); } static int test_harness_run(int __attribute__((unused)) argc,thanks,
-- Shuah
Hopefully that clears it up. I don't see anything to change for a v2? Did I miss anything?
Thanks!
-Kees
Signature · 1 line
Kees Cook
PATCH v2 2/2 selftests/harness: Handle timeouts cleanly
In reply to Kees CookWhen a selftest would timeout before, the program would just fall over and no accounting of failures would be reported (i.e. it would result in an incomplete TAP report). Instead, add an explicit SIGALRM handler to cleanly catch and report the timeout.
Before:
[==========] Running 2 tests from 2 test cases.
[ RUN ] timeout.finish
[ OK ] timeout.finish
[ RUN ] timeout.too_long
Alarm clock
After:
[==========] Running 2 tests from 2 test cases.
[ RUN ] timeout.finish
[ OK ] timeout.finish
[ RUN ] timeout.too_long
timeout.too_long: Test terminated by timeout
[ FAIL ] timeout.too_long
[==========] 1 / 2 tests passed.
[ FAILED ]
--- tools/testing/selftests/kselftest_harness.h | 53 ++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-)
tools/testing/selftests/kselftest_harness.h +51 -2
@@ -639,7 +639,8 @@ struct __test_metadata {int termsig;int passed;int trigger;/* extra handler after the evaluation */ -int timeout; +int timeout;/* seconds to wait for test timeout */ +bool timed_out;/* did this test timeout instead of exiting? */ __u8 step;bool no_print;/* manual trigger when TH_LOG_STREAM is not available */ struct __test_metadata *prev, *next;
@@ -696,15 +697,63 @@ static inline int __bail(int for_realz, bool no_print, __u8 step)return 0 ; } +struct __test_metadata *__active_test; +static void __timeout_handler (int sig,siginfo_t *info,void *ucontext) +{ +struct __test_metadata *t = __active_test; + +/* Sanity check handler execution environment. */ +if (!t) { +fprintf (TH_LOG_STREAM , +"no active test in SIGARLM handler!?\n" ); +abort (); + } +if (sig !=SIGALRM || sig != info->si_signo) { +fprintf (TH_LOG_STREAM , +"%s: SIGALRM handler caught signal %d!?\n" , + t->name, sig !=SIGALRM ? sig : info->si_signo); +abort (); + } + + t->timed_out = true; +kill (t->pid,SIGKILL ); +} +void __wait_for_test (struct __test_metadata *t) { +struct sigaction action = { + .sa_sigaction = __timeout_handler, + .sa_flags =SA_SIGINFO , + }; +struct sigaction saved_action;int status; +if (sigaction (SIGALRM , &action, &saved_action)) { + t->passed =0 ; +fprintf (TH_LOG_STREAM , +"%s: unable to install SIGARLM handler\n" , + t->name); +return ; + } + __active_test = t; + t->timed_out = false;alarm (t->timeout);waitpid (t->pid, &status,0 );alarm (0 ); +if (sigaction (SIGALRM , &saved_action,NULL )) { + t->passed =0 ; +fprintf (TH_LOG_STREAM , +"%s: unable to uninstall SIGARLM handler\n" , + t->name); +return ; + } + __active_test =NULL ; -if (WIFEXITED (status)) { +if (t->timed_out) { + t->passed =0 ; +fprintf (TH_LOG_STREAM , +"%s: Test terminated by timeout\n" , t->name); + }else if (WIFEXITED (status)) { t->passed = t->termsig ==-1 ? !WEXITSTATUS (status) :0 ;if (t->termsig !=-1 ) { fprintf(TH_LOG_STREAM,
Signature · 1 line
2.20.1