tools/nolibc: fix WIFSIGNALED() for status 0
Subsystems (3)
cover tools/nolibc: fix WIFSIGNALED() for status 0
nolibc's WIFSIGNALED() is true for status 0, so a child that exits with 0 looks as if it was killed by signal 0. I ran into it in a nolibc init for a QEMU test, which reported every clean exit of a child that way.
Patch 1 fixes the macro. Patch 2 adds nolibc-test cases for the wait status macros, which would have caught it.
Tested on top of nolibc/for-next f2212892b6a0:
- x86_64: without patch 1, the new wifsignaled_exit0 test fails, and the rest of nolibc-test gives the same results with and without it.
- x86_64, i386, and arm and sparc64 under qemu-user: all new tests pass.
- libc-test against glibc: all new tests pass, so the expected values match glibc.
Danish Khateeb (2):
tools/nolibc: fix WIFSIGNALED() for status 0 selftests/nolibc: add tests for the wait status macros
tools/include/nolibc/types.h | 2 +- tools/testing/selftests/nolibc/nolibc-test.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-)
base-commit: f2212892b6a0c2adab438783e9fa8093c90786d7
Signature · 1 line
2.55.0
PATCH 1/2 tools/nolibc: fix WIFSIGNALED() for status 0
WIFSIGNALED() needs the subtraction to wrap around for status 0, so that only 1 to 0xff, a terminating signal with or without the core dump flag, pass the check. But status is an int, and 0 - 1 is -1, which is less than 0xff. A child that exits with 0 is therefore reported as both exited and killed, and a caller that tests WIFSIGNALED() first sees "killed by signal 0".
Make the subtraction unsigned, as musl does in the same macro.
--- tools/include/nolibc/types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/types.h +1 -1
@@ -119,7 +119,7 @@ struct timeval {#define WEXITSTATUS (status) (((status) & 0xff00) >> 8)#define WIFEXITED (status) (((status) & 0x7f) == 0)#define WTERMSIG (status) ((status) & 0x7f) -#define WIFSIGNALED (status) ((status) - 1 < 0xff) +#define WIFSIGNALED (status) ((status) - 1U < 0xff)/* standard exit() codes */ #define EXIT_SUCCESS 0
Signature · 1 line
2.55.0
PATCH 2/2 selftests/nolibc: add tests for the wait status macros
In reply to Danish KhateebUntil the previous commit, WIFSIGNALED() was true for status 0, which is what waitpid() reports for a child that exits with 0. nolibc-test did not notice, because its only use of WIFSIGNALED(), in the abort test, also checks WTERMSIG().
Add tests for WEXITSTATUS(), WIFEXITED(), WIFSIGNALED() and WTERMSIG(), on the statuses the kernel reports for an exit with 0 or 0x12, a kill by SIGKILL, a SIGSEGV with the core dump flag, and a stop by SIGSTOP.
--- tools/testing/selftests/nolibc/nolibc-test.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +8 -0
@@ -2038,6 +2038,14 @@ int run_stdlib(int min, int max)CASE_TEST (htole32);EXPECT_EQ (1 ,htole32 (is_le ?0x01234567 :0x67452301 ),0x01234567 );break ;CASE_TEST (htobe64);EXPECT_EQ (1 ,htobe64 (is_le ?0x0123456789000000 :0x8967452301 ),0x8967452301 );break ;CASE_TEST (htole64);EXPECT_EQ (1 ,htole64 (is_le ?0x0123456789 :0x8967452301000000 ),0x0123456789 );break ; +CASE_TEST (wexitstatus);EXPECT_EQ (1 ,WEXITSTATUS (0x1200 ),0x12 );break ; +CASE_TEST (wifexited_exit0);EXPECT_NZ (1 ,WIFEXITED (0 ));break ; +CASE_TEST (wifexited_signal);EXPECT_ZR (1 ,WIFEXITED (SIGKILL ));break ; +CASE_TEST (wifsignaled_exit0);EXPECT_ZR (1 ,WIFSIGNALED (0 ));break ; +CASE_TEST (wifsignaled_exit);EXPECT_ZR (1 ,WIFSIGNALED (0x1200 ));break ; +CASE_TEST (wifsignaled_signal);EXPECT_NZ (1 ,WIFSIGNALED (SIGKILL ));break ; +CASE_TEST (wifsignaled_stopped);EXPECT_ZR (1 ,WIFSIGNALED ((SIGSTOP <<8 ) |0x7f ));break ; +CASE_TEST (wtermsig_core);EXPECT_EQ (1 ,WTERMSIG (0x80 |SIGSEGV ),SIGSEGV );break ;case __LINE__:return ret;/* must be last */
Signature · 1 line
2.55.0
Thomas Weißschuh
maintainerQuoted 14 lines · Danish Khateeb
nolibc's
WIFSIGNALED()is true for status 0, so a child that exits with 0 looks as if it was killed by signal 0. I ran into it in a nolibc init for a QEMU test, which reported every clean exit of a child that way.Patch 1 fixes the macro. Patch 2 adds nolibc-test cases for the wait status macros, which would have caught it.
Tested on top of nolibc/for-next
f2212892b6a0:
-x86_64: without patch 1, the newwifsignaled_exit0test fails, and the rest of nolibc-test gives the same results with and without it.
-x86_64, i386, and arm and sparc64 under qemu-user: all new tests pass.
- libc-test against glibc: all new tests pass, so the expected values match glibc.
Quoted 3 lines
Danish Khateeb (2):
tools/nolibc: fixWIFSIGNALED()for status 0 selftests/nolibc: add tests for the wait status macros
Applied without the Cc stable, as we don't do that in the nolibc subsystem.
Thanks!
Quoted 9 lines
tools/include/nolibc/types.h | 2 +- tools/testing/selftests/nolibc/nolibc-test.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-)base-commit:
f2212892b6a0c2adab438783e9fa8093c90786d7Signature · 1 line
2.55.0