mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0
@ 2026-09-23 20:56 Danish Khateeb
  2026-09-23 20:56 ` [PATCH 1/2] " Danish Khateeb
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Danish Khateeb @ 2026-09-23 20:56 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh
  Cc: Shuah Khan, linux-kselftest, linux-kernel, 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 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
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] tools/nolibc: fix WIFSIGNALED() for status 0
  2026-09-23 20:56 [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0 Danish Khateeb
@ 2026-09-23 20:56 ` Danish Khateeb
  2026-09-23 20:56 ` [PATCH 2/2] selftests/nolibc: add tests for the wait status macros Danish Khateeb
  2026-09-24  8:41 ` [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0 Thomas Weißschuh
  2 siblings, 0 replies; 4+ messages in thread
From: Danish Khateeb @ 2026-09-23 20:56 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh
  Cc: Shuah Khan, linux-kselftest, linux-kernel, Danish Khateeb, stable

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.

Fixes: 8c934d4822c7 ("tools/nolibc: add helpers for wait() signal exits")
Cc: stable@vger.kernel.org # v6.4+
Assisted-by: LLM
Signed-off-by: Danish Khateeb <danishkhateeb03@gmail.com>
---
 tools/include/nolibc/types.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h
index 8f3cb18df7f1..4373bd8a5b5f 100644
--- a/tools/include/nolibc/types.h
+++ b/tools/include/nolibc/types.h
@@ -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
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] selftests/nolibc: add tests for the wait status macros
  2026-09-23 20:56 [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0 Danish Khateeb
  2026-09-23 20:56 ` [PATCH 1/2] " Danish Khateeb
@ 2026-09-23 20:56 ` Danish Khateeb
  2026-09-24  8:41 ` [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0 Thomas Weißschuh
  2 siblings, 0 replies; 4+ messages in thread
From: Danish Khateeb @ 2026-09-23 20:56 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh
  Cc: Shuah Khan, linux-kselftest, linux-kernel, Danish Khateeb

Until 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.

Assisted-by: LLM
Signed-off-by: Danish Khateeb <danishkhateeb03@gmail.com>
---
 tools/testing/selftests/nolibc/nolibc-test.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index f62183a46205..8985aa1ba96a 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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 */
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0
  2026-09-23 20:56 [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0 Danish Khateeb
  2026-09-23 20:56 ` [PATCH 1/2] " Danish Khateeb
  2026-09-23 20:56 ` [PATCH 2/2] selftests/nolibc: add tests for the wait status macros Danish Khateeb
@ 2026-09-24  8:41 ` Thomas Weißschuh
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2026-09-24  8:41 UTC (permalink / raw)
  To: Danish Khateeb; +Cc: Willy Tarreau, Shuah Khan, linux-kselftest, linux-kernel

On 2026-09-23 15:56:20-0500, Danish Khateeb wrote:
> 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

Applied without the Cc stable, as we don't do that in the nolibc
subsystem.

Thanks!

>  tools/include/nolibc/types.h                 | 2 +-
>  tools/testing/selftests/nolibc/nolibc-test.c | 8 ++++++++
>  2 files changed, 9 insertions(+), 1 deletion(-)
> 
> 
> base-commit: f2212892b6a0c2adab438783e9fa8093c90786d7
> -- 
> 2.55.0
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-24  8:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-23 20:56 [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0 Danish Khateeb
2026-09-23 20:56 ` [PATCH 1/2] " Danish Khateeb
2026-09-23 20:56 ` [PATCH 2/2] selftests/nolibc: add tests for the wait status macros Danish Khateeb
2026-09-24  8:41 ` [PATCH 0/2] tools/nolibc: fix WIFSIGNALED() for status 0 Thomas Weißschuh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®