Skip to contentSkip to conversation

tools/nolibc: add sigaction()

RFC In review Benjamin Berg 3 participants 6 messages Jul 1, 2025 – Jul 7, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 tools/nolibc: add sigaction()

From: Benjamin Berg <[email protected]>

In preparation to add tests that use it.

Note that some architectures do not have a usable linux/signal.h include file. However, in those cases we can use asm-generic/signal.h instead.

---

Another attempt at signal handling for nolibc which should actually be working. Some trickery is needed to get the right definition, but I feel it is sufficiently clean this way.

Submitting this as RFC mostly because I do not yet have a proper patch to add a test that uses the feature.

Benjamin

---
 tools/include/nolibc/arch-aarch64.h          |  3 +
 tools/include/nolibc/arch-arm.h              |  7 ++
 tools/include/nolibc/arch-i386.h             | 13 +++
 tools/include/nolibc/arch-loongarch.h        |  3 +
 tools/include/nolibc/arch-m68k.h             | 10 ++
 tools/include/nolibc/arch-mips.h             |  3 +
 tools/include/nolibc/arch-powerpc.h          |  8 ++
 tools/include/nolibc/arch-riscv.h            |  3 +
 tools/include/nolibc/arch-s390.h             |  8 +-
 tools/include/nolibc/arch-sparc.h            | 43 +++++++++
 tools/include/nolibc/arch-x86_64.h           | 10 ++
 tools/include/nolibc/signal.h                | 97 ++++++++++++++++++++
 tools/include/nolibc/sys.h                   |  2 +-
 tools/include/nolibc/time.h                  |  3 +-
 tools/testing/selftests/nolibc/nolibc-test.c | 52 +++++++++++
 15 files changed, 261 insertions(+), 4 deletions(-)
tools/include/nolibc/arch-aarch64.h +3 -0
index 937a348da42e..736aae6dbd47 100644
@@ -10,6 +10,9 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for AARCH64 :
  *   - registers are 64-bit
  *   - stack is 16-byte aligned
tools/include/nolibc/arch-arm.h +7 -0
index 1f66e7e5a444..1faf6c2dbeb8 100644
@@ -10,6 +10,13 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for ARM in ARM or Thumb modes :
  *   - registers are 32-bit
  *   - stack is 8-byte aligned
tools/include/nolibc/arch-i386.h +13 -0
index 7c9b38e96418..fbec7490a92c 100644
@@ -10,6 +10,19 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Restorer must be set on i386 */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for i386 :
  *   - mostly similar to x86_64
  *   - registers are 32-bit
tools/include/nolibc/arch-loongarch.h +3 -0
index 5511705303ea..68d60d04ef59 100644
@@ -10,6 +10,9 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for LoongArch :
  *   - stack is 16-byte aligned
  *   - syscall number is passed in a7
tools/include/nolibc/arch-m68k.h +10 -0
index 6dac1845f298..981b4cc55a69 100644
@@ -13,6 +13,16 @@
 #include "compiler.h"
 #include "crt.h"
 
+/*
+ * Needed to get the correct struct sigaction definition. m68k does not use
+ * sa_restorer, but it is included in the structure.
+ */
+#define SA_RESTORER	0x04000000
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 #define _NOLIBC_SYSCALL_CLOBBERLIST "memory"
 
 #define my_syscall0(num)                                                      \
tools/include/nolibc/arch-mips.h +3 -0
index 753a8ed2cf69..a8837452e744 100644
@@ -14,6 +14,9 @@
 #error Unsupported MIPS ABI
 #endif
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for MIPS ABI O32 :
  *   - WARNING! there's always a delayed slot!
  *   - WARNING again, the syntax is different, registers take a '$' and numbers
tools/include/nolibc/arch-powerpc.h +8 -0
index 204564bbcd32..c846a7ddcf3c 100644
@@ -10,6 +10,14 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for PowerPC :
  *   - stack is 16-byte aligned
  *   - syscall number is passed in r0
tools/include/nolibc/arch-riscv.h +3 -0
index 885383a86c38..709e6a262d9a 100644
@@ -10,6 +10,9 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for RISCV :
  *   - stack is 16-byte aligned
  *   - syscall number is passed in a7
tools/include/nolibc/arch-s390.h +7 -1
index df4c3cc713ac..0dccb6d1ad64 100644
@@ -5,13 +5,19 @@
 
 #ifndef _NOLIBC_ARCH_S390_H
 #define _NOLIBC_ARCH_S390_H
-#include <linux/signal.h>
 #include <linux/unistd.h>
 
 #include "compiler.h"
 #include "crt.h"
 #include "std.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for s390:
  *   - registers are 64-bit
  *   - syscall number is passed in r1
tools/include/nolibc/arch-sparc.h +43 -0

53 lines not on this page · raw message

tools/include/nolibc/arch-x86_64.h +10 -0
index 67305e24dbef..9f13a2205876 100644
@@ -10,6 +10,16 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Restorer must be set on i386 */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for x86_64 :
  *   - registers are 64-bit
  *   - syscall number is passed in rax
tools/include/nolibc/signal.h +97 -0

107 lines not on this page · raw message

tools/include/nolibc/sys.h +1 -1
index 9556c69a6ae1..fb9a1ccf2440 100644
@@ -14,7 +14,6 @@
 
 /* system includes */
 #include <linux/unistd.h>
-#include <linux/signal.h>  /* for SIGCHLD */
 #include <linux/termios.h>
 #include <linux/mman.h>
 #include <linux/fs.h>
@@ -23,6 +22,7 @@
 #include <linux/auxvec.h>
 #include <linux/fcntl.h> /* for O_* and AT_* */
 #include <linux/stat.h>  /* for statx() */
+/* signal definitions are included by arch.h */
 
 #include "errno.h"
 #include "stdarg.h"
tools/include/nolibc/time.h +1 -2
index fc387940d51f..70ef31d4117f 100644
@@ -14,9 +14,8 @@
 #include "arch.h"
 #include "types.h"
 #include "sys.h"
-
-#include <linux/signal.h>
 #include <linux/time.h>
+/* signal definitions are included by arch.h */
 
 static __inline__
 void __nolibc_timespec_user_to_kernel(const struct timespec *ts, struct __kernel_timespec *kts)
tools/testing/selftests/nolibc/nolibc-test.c +52 -0

64 lines not on this page · raw message

Signature · 1 line
2.50.0
#1 lore raw

Hi Benjamin,

Thanks for your patch!

Quoted 1 line · Benjamin Berg

From: Benjamin Berg <[email protected]>

Please send the next revisions to the nolibc maintainers from the MAINTAINERS file. Willy and my @weissschuh.net address.

Quoted 1 line

In preparation to add tests that use it.

Here "tests" is not clear about its meaning.

Quoted 2 lines

Note that some architectures do not have a usable linux/signal.h include file. However, in those cases we can use asm-generic/signal.h instead.

This should explain what "unusable" means.

Quoted 10 lines

---

Another attempt at signal handling for nolibc which should actually be working. Some trickery is needed to get the right definition, but I feel it is sufficiently clean this way.

Submitting this as RFC mostly because I do not yet have a proper patch to add a test that uses the feature.

We do pick up new features in nolibc without them having in-kernel users. So if you want to get this in already you can drop the RFC state.

Quoted 3 lines

Benjamin

---
 tools/include/nolibc/arch-aarch64.h          |  3 +

In nolibc/for-next, arch-aarch64.h is now called arch-arm64.h.

https://git.kernel.org/pub/scm/linux/kernel/git/nolibc/linux-nolibc.git/log/?h=for-next

Quoted 2 lines
 tools/include/nolibc/arch-arm.h              |  7 ++
 tools/include/nolibc/arch-i386.h             | 13 +++

... and arch-i386.h was renamed to arch-x86.h

Quoted 8 lines
 tools/include/nolibc/arch-loongarch.h        |  3 +
 tools/include/nolibc/arch-m68k.h             | 10 ++
 tools/include/nolibc/arch-mips.h             |  3 +
 tools/include/nolibc/arch-powerpc.h          |  8 ++
 tools/include/nolibc/arch-riscv.h            |  3 +
 tools/include/nolibc/arch-s390.h             |  8 +-
 tools/include/nolibc/arch-sparc.h            | 43 +++++++++
 tools/include/nolibc/arch-x86_64.h           | 10 ++

same for arch-x86_64.h, Unlucky timing.

Quoted 5 lines
 tools/include/nolibc/signal.h                | 97 ++++++++++++++++++++
 tools/include/nolibc/sys.h                   |  2 +-
 tools/include/nolibc/time.h                  |  3 +-
 tools/testing/selftests/nolibc/nolibc-test.c | 52 +++++++++++
 15 files changed, 261 insertions(+), 4 deletions(-)

<snip>

Quoted 8 lines
tools/include/nolibc/arch-i386.h +2 -0
@@ -10,6 +10,19 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000

tools/include/nolibc/arch-i386.h line 24

Quoted 10 lines
tools/include/nolibc/arch-i386.h +10 -0
+
+/* Restorer must be set on i386 */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>

This doesn't work if the user already has <linux/signal.h> included for some other reason. The symbol names will conflict.

Can we do something like this:

#include <linux/signal.h>

/* lifted from asm-generic/signal.h */

struct __nolibc_sigaction {
	__sighandler_t sa_handler;
	unsigned long sa_flags;
#ifdef SA_RESTORER
	__sigrestore_t sa_restorer;
#endif
	__nolibc_sigset_t sa_mask;
};

int sys_rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)

{
	struct __nolibc_sigaction nolibc_act, nolibc_oldact;
	int ret;

	/* Convert whatever gunk we got from linux/signal.h to what the
	 * kernel actually expects. If it is the same structure,
	 * hopefully the compiler manages to optimize it away.
	 * See __nolibc_user_timespec_to_kernel() et al.
	 * (Comment not meant to be copied verbatim)
	 */
	__nolibc_sigaction_user_to_kernel(act, &nolibc_act);

#if defined(_NOLIBC_ARCH_NEEDS_SA_RESTORER)
...
#endif

	ret = my_syscall4(__NR_rt_sigaction, signum, &nolibc_act, &nolibc_oldact, sizeof(nolibc_act->sa_mask));

	__nolibc_sigaction_kernel_to_user(&nolibc_oldact, oldact);

	return ret;
}

Or am I missing something?

Quoted 4 lines
tools/include/nolibc/arch-i386.h +1 -0
+
 /* Syscalls for i386 :
  *   - mostly similar to x86_64
  *   - registers are 32-bit

<snip>

tools/include/nolibc/arch-sparc.h line 20

Quoted 12 lines
tools/include/nolibc/arch-sparc.h +6 -0
@@ -12,6 +12,19 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* The includes are sane, if one sets __WANT_POSIX1B_SIGNALS__ */
+#define __WANT_POSIX1B_SIGNALS__
+#include <linux/signal.h>

This also assumes the user did not already include <linux/signal.h> before including nolibc.

Quoted 11 lines
tools/include/nolibc/arch-sparc.h +7 -0
+
+/*
+ * sparc has ODD_RT_SIGACTION, we always pass our restorer as an argument
+ * to rt_sigaction. The restorer is implemented in this file.
+ */
+#define _NOLIBC_RT_SIGACTION_PASSES_RESTORER
+
 /*
  * Syscalls for SPARC:
  *   - registers are native word size

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 205

Quoted 5 lines
tools/include/nolibc/arch-sparc.h +2 -0
@@ -188,4 +201,34 @@ pid_t sys_fork(void)
 }
 #define sys_fork sys_fork
 
+#define __nolibc_stringify_1(x...)     #x
+#define __nolibc_stringify(x...)       __stringify_1(x)

If we need this, IMO it belongs into compiler.h.

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 211

Quoted 6 lines
tools/include/nolibc/arch-sparc.h +6 -0
+
+/* The compiler insists on adding a SAVE call to the start of every function */
+#define __nolibc_sa_restorer __nolibc_sa_restorer
+void __nolibc_sa_restorer (void);
+#ifdef __arch64__
+__asm__(                                                        \

We are avoiding bare toplevel asm calls.
You could use the same trick as my SuperH _start() function and use asm() inside a function.

https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
tools/include/nolibc/arch-sparc.h +6 -0
+	".section .text\n"                                      \
+	".align  4 \n"                                          \
+	"__nolibc_sa_restorer:\n"                               \
+	"nop\n"                                                 \
+	"nop\n"                                                 \
+	"mov     " __stringify(__NR_rt_sigreturn) ", %g1 \n"    \

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 227

Quoted 10 lines
tools/include/nolibc/arch-sparc.h +10 -0
+	"t       0x6d \n");
+#else
+__asm__(                                                        \
+	".section .text\n"                                      \
+	".align  4 \n"                                          \
+	"__nolibc_sa_restorer:\n"                               \
+	"nop\n"                                                 \
+	"nop\n"                                                 \
+	"mov     " __stringify(__NR_rt_sigreturn) ", %g1 \n"    \
+	"t       0x10 \n"                                       \

Only one line differs. I'd prefer the #ifdef around that.

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 232

Quoted 5 lines
tools/include/nolibc/arch-sparc.h +5 -0
+	);
+#endif
+
+#undef __nolibc_stringify_1(x...)
+#undef __nolibc_stringify

And there is no need to undef it again.

Quoted 4 lines
tools/include/nolibc/arch-sparc.h +1 -0
+
 #endif /* _NOLIBC_ARCH_SPARC_H */
tools/include/nolibc/arch-x86_64.h +0 -0
index 67305e24dbef..9f13a2205876 100644

<snip>

Quoted 12 lines
tools/include/nolibc/signal.h +2 -0
@@ -14,6 +14,8 @@
 #include "arch.h"
 #include "types.h"
 #include "sys.h"
+#include "string.h"
+/* signal definitions are included by arch.h */
 
 /* This one is not marked static as it's needed by libgcc for divide by zero */
 int raise(int signal);

tools/include/nolibc/signal.h int raise(int signal) · line 32

Quoted 8 lines
tools/include/nolibc/signal.h +5 -0
@@ -23,4 +25,99 @@ int raise(int signal)
 	return sys_kill(sys_getpid(), signal);
 }
 
+/*
+ * sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
+ */
+#if defined(_NOLIBC_ARCH_NEEDS_SA_RESTORER) && !defined(__nolibc_sa_restorer)
+static __attribute__((noreturn)) __nolibc_entrypoint __no_stack_protector

__attribute__((noreturn)) is not always available and should be behind __nolibc_has_attribute().
I'm a bit unhappy about reusing the entrypoint machinery, but I guess it's necessary.

tools/include/nolibc/signal.h int raise(int signal) · line 44

Quoted 12 lines
tools/include/nolibc/signal.h +12 -0
+void __nolibc_sa_restorer(void)
+{
+	my_syscall0(__NR_rt_sigreturn);
+	__nolibc_entrypoint_epilogue();
+}
+#endif
+
+static __attribute__((unused))
+int sys_rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
+{
+	struct sigaction real_act = *act;
+#if defined(SA_RESTORER) && defined(_NOLIBC_ARCH_NEEDS_SA_RESTORER)

If _NOLIBC_ARCH_NEEDS_SA_RESTORER is set then SA_RESTORER should also be present. SA_RESTORER doesn't need to be checked also.
Are there cases where SA_RESTORER is defined but not needed?

Quoted 5 lines
tools/include/nolibc/signal.h +5 -0
+	if (!(real_act.sa_flags & SA_RESTORER)) {
+		real_act.sa_flags |= SA_RESTORER;
+		real_act.sa_restorer = __nolibc_sa_restorer;
+	}
+#endif

tools/include/nolibc/signal.h int raise(int signal) · line 59

Quoted 10 lines
tools/include/nolibc/signal.h +10 -0
+#ifdef _NOLIBC_ARCH_FORCE_SIG_FLAGS
+	real_act.sa_flags |= _NOLIBC_ARCH_FORCE_SIG_FLAGS;
+#endif
+
+#ifndef _NOLIBC_RT_SIGACTION_PASSES_RESTORER
+	return my_syscall4(__NR_rt_sigaction, signum, &real_act, oldact,
+			   sizeof(act->sa_mask));
+#else
+	return my_syscall5(__NR_rt_sigaction, signum, &real_act, oldact,
+			   __nolibc_sa_restorer, sizeof(act->sa_mask));

This calling convention is specific to SPARC, so I prefer it to be in arch-sparc.h. Alpha also uses 5 arguments for the syscall, but of course in a different order... (I do have nearly done patches for alpha support).

Also if a user specified their custom sa_restorer in 'act', that one should be used, no?

Quoted 28 lines
tools/include/nolibc/signal.h +28 -0
+#endif
+}
+
+static __attribute__((unused))
+int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
+{
+	return __sysret(sys_rt_sigaction(signum, act, oldact));
+}
+
+/*
+ * int sigemptyset(sigset_t *set)
+ */
+static __attribute__((unused))
+int sigemptyset(sigset_t *set)
+{
+	memset(set, 0, sizeof(*set));
+	return 0;
+}
+
+/*
+ * int sigfillset(sigset_t *set)
+ */
+static __attribute__((unused))
+int sigfillset(sigset_t *set)
+{
+	memset(set, 0xff, sizeof(*set));
+	return 0;
+}

tools/include/nolibc/signal.h int raise(int signal) · line 97

Quoted 10 lines
tools/include/nolibc/signal.h +10 -0
+
+/*
+ * int sigaddset(sigset_t *set, int signum)
+ */
+static __attribute__((unused))
+int sigaddset(sigset_t *set, int signum)
+{
+	set->sig[(signum - 1) / (8 * sizeof(set->sig[0]))] |=
+		1UL << ((signum - 1) % (8 * sizeof(set->sig[0])));
+	return 0;

This is documented to return EINVAL for an invalid signum.

Quoted 14 lines
tools/include/nolibc/signal.h +14 -0
+}
+
+/*
+ * int sigdelset(sigset_t *set, int signum)
+ */
+static __attribute__((unused))
+int sigdelset(sigset_t *set, int signum)
+{
+	set->sig[(signum - 1) / (8 * sizeof(set->sig[0]))] &=
+		~(1UL << ((signum - 1) % (8 * sizeof(set->sig[0]))));
+	return 0;
+}
+
+/*

tools/include/nolibc/signal.h int raise(int signal) · line 121

Quoted 10 lines
tools/include/nolibc/signal.h +10 -0
+ * int sigismember(sigset_t *set, int signum)
+ */
+static __attribute__((unused))
+int sigismember(sigset_t *set, int signum)
+{
+	unsigned long res =
+		set->sig[(signum - 1) / (8 * sizeof(set->sig[0]))] &
+			(1UL << ((signum - 1) % (8 * sizeof(set->sig[0]))));
+	return !!res;
+}

These are similar to FD_CLR()/FD_SET() etc, no?
Moving both sets of functions to common inline helpers would be nice.

Quoted 2 lines
tools/include/nolibc/signal.h +1 -0
+
 #endif /* _NOLIBC_SIGNAL_H */

<snip>

tools/testing/selftests/nolibc/nolibc-test.c static int run_protection(int min __attribute__((unused)), · line 1753

Quoted 9 lines
tools/testing/selftests/nolibc/nolibc-test.c +1 -0
index dbe13000fb1a..af66b739ea18 100644
@@ -1750,6 +1750,57 @@ static int run_protection(int min __attribute__((unused)),
 	}
 }
 
+volatile int signal_check;

Technically this should be sig_atomic_t.
Which can be a typedef to int.

Quoted 17 lines
tools/testing/selftests/nolibc/nolibc-test.c +17 -0
+void test_sighandler(int signum)
+{
+	if (signum == SIGUSR1) {
+		kill(getpid(), SIGUSR2);
+		signal_check = 1;
+	} else {
+		signal_check++;
+	}
+}
+
+int run_signal(int min, int max)
+{
+	struct sigaction sa = {
+		.sa_flags = 0,
+		.sa_handler = test_sighandler,
+	};
+	int llen; /* line length */

tools/testing/selftests/nolibc/nolibc-test.c static int run_protection(int min __attribute__((unused)), · line 1781

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+	int ret = 0;
+	int res;
+
+	(void)min;
+	(void)max;
+
+	signal_check = 0;
+
+	sigemptyset(&sa.sa_mask);
+	sigaddset(&sa.sa_mask, SIGUSR2);

It would be good to do some assertions after sigemptyset() and sigaddset() to validate the bitfiddling.

tools/testing/selftests/nolibc/nolibc-test.c static int run_protection(int min __attribute__((unused)), · line 1788

Quoted 7 lines
tools/testing/selftests/nolibc/nolibc-test.c +7 -0
+
+	res = sigaction(SIGUSR1, &sa, NULL);
+	llen = printf("register SIGUSR1: %d", res);
+	EXPECT_EQ(1, 0, res);
+	res = sigaction(SIGUSR2, &sa, NULL);
+	llen = printf("register SIGUSR2: %d", res);
+	EXPECT_EQ(1, 0, res);

Here it would be nice to validate the old action.

Quoted 19 lines
tools/testing/selftests/nolibc/nolibc-test.c +15 -0
+
+	/* Trigger the first signal. */
+	kill(getpid(), SIGUSR1);
+
+	/* If signal_check is 1 or higher, then signal emission worked */
+	llen = printf("signal emission: 1 <= signal_check");
+	EXPECT_GE(1, signal_check, 1);
+
+	/* If it is 2, then signal masking worked */
+	llen = printf("signal masking: 2 == signal_check");
+	EXPECT_EQ(1, signal_check, 2);
+
+	return ret;
+}
+
 /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
 int prepare(void)
 {

tools/testing/selftests/nolibc/nolibc-test.c static const struct test test_names[] = { · line 1869

Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c +1 -0
@@ -1815,6 +1866,7 @@ static const struct test test_names[] = {
 	{ .name = "stdlib",     .func = run_stdlib     },
 	{ .name = "printf",     .func = run_printf     },
 	{ .name = "protection", .func = run_protection },
+	{ .name = "signal",     .func = run_signal },

These 'struct test's are really more test suites.
This testcase can be part of the syscall suite.

Quoted 6 lines
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
 	{ 0 }
 };
 
Signature · 1 line
2.50.0
#2 lore raw

Hi,

Quoted 14 lines · Thomas Weißschuh

[SNIP]

Quoted 13 lines
tools/include/nolibc/arch-i386.h +0 -0
@@ -10,6 +10,19 @@
  #include "compiler.h"
  #include "crt.h"

tools/include/nolibc/arch-i386.h +7 -0
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Restorer must be set on i386 */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
Quoted 8 lines

tools/include/nolibc/arch-i386.h line 24

Quoted 5 lines
tools/include/nolibc/arch-i386.h +5 -0
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>

This doesn't work if the user already has <linux/signal.h> included for some other reason. The symbol names will conflict.

I was thinking this is fine. Such a conflict already exists between the normal glibc <signal.h> and <linux/signal.h>. So there would only be a problem if the user is explicitly not including <signal.h> to then use <linux/signal.h>. I doubt that makes sense.

Benjamin

Quoted 359 lines

Can we do something like this:

#include <linux/signal.h>

/* lifted from asm-generic/signal.h */

struct __nolibc_sigaction {
	__sighandler_t sa_handler;
	unsigned long sa_flags;
#ifdef SA_RESTORER
	__sigrestore_t sa_restorer;
#endif
	__nolibc_sigset_t sa_mask;
};

int sys_rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)

{
	struct __nolibc_sigaction nolibc_act, nolibc_oldact;
	int ret;

	/* Convert whatever gunk we got from linux/signal.h to what the
	 * kernel actually expects. If it is the same structure,
	 * hopefully the compiler manages to optimize it away.
	 * See __nolibc_user_timespec_to_kernel() et al.
	 * (Comment not meant to be copied verbatim)
	 */
	__nolibc_sigaction_user_to_kernel(act, &nolibc_act);

#if defined(_NOLIBC_ARCH_NEEDS_SA_RESTORER)
...
#endif

	ret = my_syscall4(__NR_rt_sigaction, signum, &nolibc_act, &nolibc_oldact, sizeof(nolibc_act->sa_mask));

	__nolibc_sigaction_kernel_to_user(&nolibc_oldact, oldact);

	return ret;
}

Or am I missing something?

Quoted 4 lines

+
 /* Syscalls for i386 :
  *   - mostly similar to x86_64
  *   - registers are 32-bit

<snip>

tools/include/nolibc/arch-sparc.h line 20

Quoted 12 lines
tools/include/nolibc/arch-sparc.h +0 -0
@@ -12,6 +12,19 @@
  #include "compiler.h"
  #include "crt.h"

tools/include/nolibc/arch-sparc.h +6 -0
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* The includes are sane, if one sets __WANT_POSIX1B_SIGNALS__ */
+#define __WANT_POSIX1B_SIGNALS__
+#include <linux/signal.h>

This also assumes the user did not already include <linux/signal.h> before including nolibc.

Quoted 14 lines
tools/include/nolibc/arch-sparc.h +7 -0
+
+/*
+ * sparc has ODD_RT_SIGACTION, we always pass our restorer as an argument
+ * to rt_sigaction. The restorer is implemented in this file.
+ */
+#define _NOLIBC_RT_SIGACTION_PASSES_RESTORER
+

 /*
  * Syscalls for SPARC:
  *   - registers are native word size

tools/include/nolibc/arch-sparc.h +0 -0
@@ -188,4 +201,34 @@ pid_t sys_fork(void)
  }
  #define sys_fork sys_fork

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 205

Quoted 2 lines
tools/include/nolibc/arch-sparc.h +2 -0
+#define __nolibc_stringify_1(x...)     #x
+#define __nolibc_stringify(x...)       __stringify_1(x)

If we need this, IMO it belongs into compiler.h.

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 211

Quoted 6 lines
tools/include/nolibc/arch-sparc.h +6 -0
+
+/* The compiler insists on adding a SAVE call to the start of every function */
+#define __nolibc_sa_restorer __nolibc_sa_restorer
+void __nolibc_sa_restorer (void);
+#ifdef __arch64__
+__asm__(                                                        \

We are avoiding bare toplevel asm calls.
You could use the same trick as my SuperH _start() function and use asm() inside a function.

https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
tools/include/nolibc/arch-sparc.h +6 -0
+	".section .text\n"                                      \
+	".align  4 \n"                                          \
+	"__nolibc_sa_restorer:\n"                               \
+	"nop\n"                                                 \
+	"nop\n"                                                 \
+	"mov     " __stringify(__NR_rt_sigreturn) ", %g1 \n"    \

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 227

Quoted 10 lines
tools/include/nolibc/arch-sparc.h +10 -0
+	"t       0x6d \n");
+#else
+__asm__(                                                        \
+	".section .text\n"                                      \
+	".align  4 \n"                                          \
+	"__nolibc_sa_restorer:\n"                               \
+	"nop\n"                                                 \
+	"nop\n"                                                 \
+	"mov     " __stringify(__NR_rt_sigreturn) ", %g1 \n"    \
+	"t       0x10 \n"                                       \

Only one line differs. I'd prefer the #ifdef around that.

tools/include/nolibc/arch-sparc.h pid_t sys_fork(void) · line 232

Quoted 5 lines
tools/include/nolibc/arch-sparc.h +5 -0
+	);
+#endif
+
+#undef __nolibc_stringify_1(x...)
+#undef __nolibc_stringify

And there is no need to undef it again.

Quoted 4 lines

+
 #endif /* _NOLIBC_ARCH_SPARC_H */

tools/include/nolibc/arch-x86_64.h +0 -0
index 67305e24dbef..9f13a2205876 100644

<snip>

Quoted 15 lines
tools/include/nolibc/signal.h +2 -0
@@ -14,6 +14,8 @@
  #include "arch.h"
  #include "types.h"
  #include "sys.h"
+#include "string.h"
+/* signal definitions are included by arch.h */


 /* This one is not marked static as it's needed by libgcc for divide by zero */ int raise(int signal);

tools/include/nolibc/signal.h +0 -0
@@ -23,4 +25,99 @@ int raise(int signal)
  	return sys_kill(sys_getpid(), signal);
  }

tools/include/nolibc/signal.h int raise(int signal) · line 32

Quoted 5 lines
tools/include/nolibc/signal.h +5 -0
+/*
+ * sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
+ */
+#if defined(_NOLIBC_ARCH_NEEDS_SA_RESTORER) && !defined(__nolibc_sa_restorer)
+static __attribute__((noreturn)) __nolibc_entrypoint __no_stack_protector

__attribute__((noreturn)) is not always available and should be behind __nolibc_has_attribute().
I'm a bit unhappy about reusing the entrypoint machinery, but I guess it's necessary.

tools/include/nolibc/signal.h int raise(int signal) · line 44

Quoted 12 lines
tools/include/nolibc/signal.h +12 -0
+void __nolibc_sa_restorer(void)
+{
+	my_syscall0(__NR_rt_sigreturn);
+	__nolibc_entrypoint_epilogue();
+}
+#endif
+
+static __attribute__((unused))
+int sys_rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
+{
+	struct sigaction real_act = *act;
+#if defined(SA_RESTORER) && defined(_NOLIBC_ARCH_NEEDS_SA_RESTORER)

If _NOLIBC_ARCH_NEEDS_SA_RESTORER is set then SA_RESTORER should also be present. SA_RESTORER doesn't need to be checked also.
Are there cases where SA_RESTORER is defined but not needed?

Quoted 5 lines
tools/include/nolibc/signal.h +5 -0
+	if (!(real_act.sa_flags & SA_RESTORER)) {
+		real_act.sa_flags |= SA_RESTORER;
+		real_act.sa_restorer = __nolibc_sa_restorer;
+	}
+#endif

tools/include/nolibc/signal.h int raise(int signal) · line 59

Quoted 10 lines
tools/include/nolibc/signal.h +10 -0
+#ifdef _NOLIBC_ARCH_FORCE_SIG_FLAGS
+	real_act.sa_flags |= _NOLIBC_ARCH_FORCE_SIG_FLAGS;
+#endif
+
+#ifndef _NOLIBC_RT_SIGACTION_PASSES_RESTORER
+	return my_syscall4(__NR_rt_sigaction, signum, &real_act, oldact,
+			   sizeof(act->sa_mask));
+#else
+	return my_syscall5(__NR_rt_sigaction, signum, &real_act, oldact,
+			   __nolibc_sa_restorer, sizeof(act->sa_mask));

This calling convention is specific to SPARC, so I prefer it to be in arch-sparc.h. Alpha also uses 5 arguments for the syscall, but of course in a different order... (I do have nearly done patches for alpha support).

Also if a user specified their custom sa_restorer in 'act', that one should be used, no?

Quoted 28 lines
tools/include/nolibc/signal.h +28 -0
+#endif
+}
+
+static __attribute__((unused))
+int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
+{
+	return __sysret(sys_rt_sigaction(signum, act, oldact));
+}
+
+/*
+ * int sigemptyset(sigset_t *set)
+ */
+static __attribute__((unused))
+int sigemptyset(sigset_t *set)
+{
+	memset(set, 0, sizeof(*set));
+	return 0;
+}
+
+/*
+ * int sigfillset(sigset_t *set)
+ */
+static __attribute__((unused))
+int sigfillset(sigset_t *set)
+{
+	memset(set, 0xff, sizeof(*set));
+	return 0;
+}

tools/include/nolibc/signal.h int raise(int signal) · line 97

Quoted 10 lines
tools/include/nolibc/signal.h +10 -0
+
+/*
+ * int sigaddset(sigset_t *set, int signum)
+ */
+static __attribute__((unused))
+int sigaddset(sigset_t *set, int signum)
+{
+	set->sig[(signum - 1) / (8 * sizeof(set->sig[0]))] |=
+		1UL << ((signum - 1) % (8 * sizeof(set->sig[0])));
+	return 0;

This is documented to return EINVAL for an invalid signum.

Quoted 14 lines
tools/include/nolibc/signal.h +14 -0
+}
+
+/*
+ * int sigdelset(sigset_t *set, int signum)
+ */
+static __attribute__((unused))
+int sigdelset(sigset_t *set, int signum)
+{
+	set->sig[(signum - 1) / (8 * sizeof(set->sig[0]))] &=
+		~(1UL << ((signum - 1) % (8 * sizeof(set->sig[0]))));
+	return 0;
+}
+
+/*

tools/include/nolibc/signal.h int raise(int signal) · line 121

Quoted 10 lines
tools/include/nolibc/signal.h +10 -0
+ * int sigismember(sigset_t *set, int signum)
+ */
+static __attribute__((unused))
+int sigismember(sigset_t *set, int signum)
+{
+	unsigned long res =
+		set->sig[(signum - 1) / (8 * sizeof(set->sig[0]))] &
+			(1UL << ((signum - 1) % (8 * sizeof(set->sig[0]))));
+	return !!res;
+}

These are similar to FD_CLR()/FD_SET() etc, no?
Moving both sets of functions to common inline helpers would be nice.

Quoted 2 lines

+
 #endif /* _NOLIBC_SIGNAL_H */

<snip>

Quoted 9 lines
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
index dbe13000fb1a..af66b739ea18 100644
@@ -1750,6 +1750,57 @@ static int run_protection(int min __attribute__((unused)),
  	}
  }


+volatile int signal_check;

Technically this should be sig_atomic_t.
Which can be a typedef to int.

Quoted 17 lines
tools/testing/selftests/nolibc/nolibc-test.c +17 -0
+void test_sighandler(int signum)
+{
+	if (signum == SIGUSR1) {
+		kill(getpid(), SIGUSR2);
+		signal_check = 1;
+	} else {
+		signal_check++;
+	}
+}
+
+int run_signal(int min, int max)
+{
+	struct sigaction sa = {
+		.sa_flags = 0,
+		.sa_handler = test_sighandler,
+	};
+	int llen; /* line length */

tools/testing/selftests/nolibc/nolibc-test.c static int run_protection(int min __attribute__((unused)), · line 1781

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+	int ret = 0;
+	int res;
+
+	(void)min;
+	(void)max;
+
+	signal_check = 0;
+
+	sigemptyset(&sa.sa_mask);
+	sigaddset(&sa.sa_mask, SIGUSR2);

It would be good to do some assertions after sigemptyset() and sigaddset() to validate the bitfiddling.

tools/testing/selftests/nolibc/nolibc-test.c static int run_protection(int min __attribute__((unused)), · line 1788

Quoted 7 lines
tools/testing/selftests/nolibc/nolibc-test.c +7 -0
+
+	res = sigaction(SIGUSR1, &sa, NULL);
+	llen = printf("register SIGUSR1: %d", res);
+	EXPECT_EQ(1, 0, res);
+	res = sigaction(SIGUSR2, &sa, NULL);
+	llen = printf("register SIGUSR2: %d", res);
+	EXPECT_EQ(1, 0, res);

Here it would be nice to validate the old action.

Quoted 19 lines
tools/testing/selftests/nolibc/nolibc-test.c +15 -0
+
+	/* Trigger the first signal. */
+	kill(getpid(), SIGUSR1);
+
+	/* If signal_check is 1 or higher, then signal emission worked */
+	llen = printf("signal emission: 1 <= signal_check");
+	EXPECT_GE(1, signal_check, 1);
+
+	/* If it is 2, then signal masking worked */
+	llen = printf("signal masking: 2 == signal_check");
+	EXPECT_EQ(1, signal_check, 2);
+
+	return ret;
+}
+

 /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */ int prepare(void)
 {

tools/testing/selftests/nolibc/nolibc-test.c static const struct test test_names[] = { · line 1869

Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c +1 -0
@@ -1815,6 +1866,7 @@ static const struct test test_names[] = {
  	{ .name = "stdlib",     .func = run_stdlib     },
  	{ .name = "printf",     .func = run_printf     },
  	{ .name = "protection", .func = run_protection },
+	{ .name = "signal",     .func = run_signal },

These 'struct test's are really more test suites.
This testcase can be part of the syscall suite.

Quoted 6 lines
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
  	{ 0 }
  };

Signature · 1 line
2.50.0
#3 lore raw
Quoted 24 lines · Benjamin Berg
Quoted 14 lines · Thomas Weißschuh

[SNIP]

Quoted 13 lines
tools/include/nolibc/arch-i386.h +0 -0
@@ -10,6 +10,19 @@
  #include "compiler.h"
  #include "crt.h"

tools/include/nolibc/arch-i386.h +7 -0
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Restorer must be set on i386 */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
Quoted 8 lines

tools/include/nolibc/arch-i386.h line 24

Quoted 5 lines
tools/include/nolibc/arch-i386.h +5 -0
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>

This doesn't work if the user already has <linux/signal.h> included for some other reason. The symbol names will conflict.

Quoted 2 lines

I was thinking this is fine. Such a conflict already exists between the normal glibc <signal.h> and <linux/signal.h>.

It would be enough to keep compatibility with glibc. But personally I'd like to make it work generally.

Quoted 3 lines

So there would only be a
problem if the user is explicitly not including <signal.h> to then use <linux/signal.h>. I doubt that makes sense.

Technically nolibc is always included as a whole, so any application using it would be prevented from using linux/signal.h.

Maybe Willy has some strong opinions.
Otherwise I'm also fine if we keep this part as is for now. Then if everything else is addressed I'll try to actually implement my proposal on top.

Does this sound reasonable?

Thomas

maintainer
#4 lore raw
Quoted 38 lines · Thomas Weißschuh
Quoted 24 lines · Benjamin Berg
Quoted 14 lines · Thomas Weißschuh

[SNIP]

Quoted 13 lines
tools/include/nolibc/arch-i386.h +0 -0
@@ -10,6 +10,19 @@
  #include "compiler.h"
  #include "crt.h"

tools/include/nolibc/arch-i386.h +7 -0
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Restorer must be set on i386 */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
Quoted 8 lines

tools/include/nolibc/arch-i386.h line 24

Quoted 5 lines
tools/include/nolibc/arch-i386.h +5 -0
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>

This doesn't work if the user already has <linux/signal.h> included for some other reason. The symbol names will conflict.

Quoted 2 lines

I was thinking this is fine. Such a conflict already exists between the normal glibc <signal.h> and <linux/signal.h>.

It would be enough to keep compatibility with glibc. But personally I'd like to make it work generally.

Quoted 3 lines

So there would only be a
problem if the user is explicitly not including <signal.h> to then use <linux/signal.h>. I doubt that makes sense.

Technically nolibc is always included as a whole, so any application using it would be prevented from using linux/signal.h.

Quoted 6 lines

Maybe Willy has some strong opinions.
Otherwise I'm also fine if we keep this part as is for now. Then if everything else is addressed I'll try to actually implement my proposal on top.

Does this sound reasonable?

I think that userland application writers are used to seeing random failures here and there when they start using linux/ without being invited to do so. Of course it's never fun to deal with build failures, but the effort spent overengineering solutions is often not worth being spent when you figure that sometimes the next application you try will blatantly fail again. I'd suggest that we stay on the pragmatic side of things: if we find a solution that works fine for the common case, that may break for rare cases that already break with glibc, I think we're already fine. That doesn't mean we don't need to think about it, but it's not something urgent nor that warrants taking more risks either.

I personally also think that we'll face an increasing amount of build issue reports due to our attempt at being more compatible. Indeed, in the past, one had to #ifdef all includes and pass -include nolibc on the gcc command line. Nowadays by supporting a larger number of functions and include file names, we encourage developers to try to simply switch their libc for nolibc. We do know that the coverage remains low, and reports will come proportionally to the ratio of apparent ease of use to feature coverage. That's expected and we should try to resist feeling bad about it. I don't think we'll ever aim at competing with glibc/musl for example, so a little bit of application adaptation is expected from time to time.

Cheers,
Willy

#5 lore raw
Quoted 46 lines · Willy Tarreau
Quoted 38 lines · Thomas Weißschuh
Quoted 24 lines · Benjamin Berg
Quoted 14 lines · Thomas Weißschuh

[SNIP]

Quoted 13 lines
tools/include/nolibc/arch-i386.h +0 -0
@@ -10,6 +10,19 @@
  #include "compiler.h"
  #include "crt.h"

tools/include/nolibc/arch-i386.h +7 -0
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Restorer must be set on i386 */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Otherwise we would need to use sigreturn instead of rt_sigreturn */
Quoted 8 lines

tools/include/nolibc/arch-i386.h line 24

Quoted 5 lines
tools/include/nolibc/arch-i386.h +5 -0
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>

This doesn't work if the user already has <linux/signal.h> included for some other reason. The symbol names will conflict.

Quoted 2 lines

I was thinking this is fine. Such a conflict already exists between the normal glibc <signal.h> and <linux/signal.h>.

It would be enough to keep compatibility with glibc. But personally I'd like to make it work generally.

Quoted 3 lines

So there would only be a
problem if the user is explicitly not including <signal.h> to then use <linux/signal.h>. I doubt that makes sense.

Technically nolibc is always included as a whole, so any application using it would be prevented from using linux/signal.h.

Quoted 6 lines

Maybe Willy has some strong opinions.
Otherwise I'm also fine if we keep this part as is for now. Then if everything else is addressed I'll try to actually implement my proposal on top.

Does this sound reasonable?

Quoted 10 lines

I think that userland application writers are used to seeing random failures here and there when they start using linux/ without being invited to do so. Of course it's never fun to deal with build failures, but the effort spent overengineering solutions is often not worth being spent when you figure that sometimes the next application you try will blatantly fail again. I'd suggest that we stay on the pragmatic side of things: if we find a solution that works fine for the common case, that may break for rare cases that already break with glibc, I think we're already fine. That doesn't mean we don't need to think about it, but it's not something urgent nor that warrants taking more risks either.

I'm all for keeping it simple. However here I think we disagree about the aproach which would be simpler. As mentioned before let's go with Benjamin's and I'll try to do my thing on top.
Then we can actually compare two working aproaches (if my idea does work).

<snip>