[libc-commits] [libc] [libc][riscv] Fix kernel sigaction layout on RISC-V (PR #244926)
Mikhail R. Gadelha via libc-commits
libc-commits at lists.llvm.org
Sat Nov 21 11:49:39 PST 2026
https://github.com/mikhailramalho created https://github.com/llvm/llvm-project/pull/244926
RISC-V has no SA_RESTORER, so its kernel struct sigaction has no sa_restorer field. KernelSigaction always had one, so the kernel read the restorer pointer as sa_mask and left our sa_mask unset on reads. This blocked SIGUSR1 inside handlers and made sigaction_test loop forever.
Like the kernel, glibc and musl, do not define SA_RESTORER on RISC-V and guard sa_restorer with #ifdef SA_RESTORER. Zero-initialize the structs and add a test that sa_mask round-trips and is blocked in the handler.
Assisted-by: Automated tooling, human reviewed.
>From 1f65be5bc4f359ac5ed995d61182195eb2b6ea00 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Tue, 17 Nov 2026 08:43:34 -0300
Subject: [PATCH] [libc][riscv] Fix kernel sigaction layout on RISC-V
RISC-V has no SA_RESTORER, so its kernel struct sigaction has no
sa_restorer field. KernelSigaction always had one, so the kernel read the
restorer pointer as sa_mask and left our sa_mask unset on reads. This
blocked SIGUSR1 inside handlers and made sigaction_test loop forever.
Like the kernel, glibc and musl, do not define SA_RESTORER on RISC-V and
guard sa_restorer with #ifdef SA_RESTORER. Zero-initialize the structs
and add a test that sa_mask round-trips and is blocked in the handler.
---
.../llvm-libc-macros/linux/signal-macros.h | 6 ++-
libc/src/signal/linux/signal.cpp | 2 +-
libc/src/signal/linux/signal_utils.h | 19 ++++++++--
.../src/__support/OSUtil/linux/vdso_test.cpp | 4 +-
libc/test/src/signal/CMakeLists.txt | 3 ++
libc/test/src/signal/sigaction_test.cpp | 37 +++++++++++++++++++
6 files changed, 64 insertions(+), 7 deletions(-)
diff --git a/libc/include/llvm-libc-macros/linux/signal-macros.h b/libc/include/llvm-libc-macros/linux/signal-macros.h
index 9254c39957d4b3..7a797d039c6ad8 100644
--- a/libc/include/llvm-libc-macros/linux/signal-macros.h
+++ b/libc/include/llvm-libc-macros/linux/signal-macros.h
@@ -72,10 +72,14 @@
#define SA_NOCLDWAIT 0x00000002
#define SA_SIGINFO 0x00000004
#define SA_RESTART 0x10000000
-#define SA_RESTORER 0x04000000
#define SA_ONSTACK 0x08000000
#define SA_NODEFER 0x40000000
#define SA_RESETHAND 0x80000000
+// Like the kernel, do not define SA_RESTORER on RISC-V, whose struct sigaction
+// has no sa_restorer field.
+#if !defined(__riscv)
+#define SA_RESTORER 0x04000000
+#endif
// Signal stack flags
#define SS_ONSTACK 0x1
diff --git a/libc/src/signal/linux/signal.cpp b/libc/src/signal/linux/signal.cpp
index 7c8ea16c6cd2e2..60f13277bde1d8 100644
--- a/libc/src/signal/linux/signal.cpp
+++ b/libc/src/signal/linux/signal.cpp
@@ -19,7 +19,7 @@ using signal_handler = void (*)(int);
LLVM_LIBC_FUNCTION(signal_handler, signal,
(int signum, signal_handler handler)) {
- struct sigaction action, old;
+ struct sigaction action{}, old{};
action.sa_handler = handler;
action.sa_flags = SA_RESTART;
// Errno will already be set so no need to worry about changing errno here.
diff --git a/libc/src/signal/linux/signal_utils.h b/libc/src/signal/linux/signal_utils.h
index 7a78b00d865de6..da360aff21678f 100644
--- a/libc/src/signal/linux/signal_utils.h
+++ b/libc/src/signal/linux/signal_utils.h
@@ -26,7 +26,9 @@
namespace LIBC_NAMESPACE_DECL {
+#ifdef SA_RESTORER
extern "C" void __restore_rt();
+#endif
// The POSIX definition of struct sigaction and the sigaction data structure
// expected by the rt_sigaction syscall differ in their definition. So, we
@@ -35,11 +37,14 @@ extern "C" void __restore_rt();
//
// NOTE: Though the kernel definition does not have a union to include the
// handler taking siginfo_t * argument, one can set sa_handler to sa_sigaction
-// if SA_SIGINFO is set in sa_flags.
+// if SA_SIGINFO is set in sa_flags. Like the kernel, we only have sa_restorer
+// where SA_RESTORER is defined.
struct KernelSigaction {
LIBC_INLINE KernelSigaction &operator=(const struct sigaction &sa) {
sa_flags = sa.sa_flags;
+#ifdef SA_RESTORER
sa_restorer = sa.sa_restorer;
+#endif
sa_mask = sa.sa_mask;
if (sa_flags & SA_SIGINFO) {
sa_sigaction = sa.sa_sigaction;
@@ -50,10 +55,12 @@ struct KernelSigaction {
}
LIBC_INLINE operator struct sigaction() const {
- struct sigaction sa;
+ struct sigaction sa{};
sa.sa_flags = static_cast<int>(sa_flags);
sa.sa_mask = sa_mask;
+#ifdef SA_RESTORER
sa.sa_restorer = sa_restorer;
+#endif
if (sa_flags & SA_SIGINFO)
sa.sa_sigaction = sa_sigaction;
else
@@ -66,7 +73,9 @@ struct KernelSigaction {
void (*sa_sigaction)(int, siginfo_t *, void *);
};
unsigned long sa_flags;
+#ifdef SA_RESTORER
void (*sa_restorer)(void);
+#endif
// Our public definition of sigset_t matches that of the kernel's definition.
// So, we can use the public sigset_t type here.
sigset_t sa_mask;
@@ -154,17 +163,19 @@ class SigAbortGuard {
LIBC_INLINE ErrorOr<int>
unchecked_sigaction(int signal, const struct sigaction *__restrict libc_new,
struct sigaction *__restrict libc_old) {
- vdso::TypedSymbol<vdso::VDSOSym::RTSigReturn> rt_sigreturn;
KernelSigaction kernel_new;
if (libc_new) {
kernel_new = *libc_new;
+#ifdef SA_RESTORER
+ vdso::TypedSymbol<vdso::VDSOSym::RTSigReturn> rt_sigreturn;
if (!(kernel_new.sa_flags & SA_RESTORER)) {
kernel_new.sa_flags |= SA_RESTORER;
kernel_new.sa_restorer = rt_sigreturn ? rt_sigreturn : __restore_rt;
}
+#endif
}
- KernelSigaction kernel_old;
+ KernelSigaction kernel_old{};
int ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_rt_sigaction, signal, libc_new ? &kernel_new : nullptr,
libc_old ? &kernel_old : nullptr, sizeof(sigset_t));
diff --git a/libc/test/src/__support/OSUtil/linux/vdso_test.cpp b/libc/test/src/__support/OSUtil/linux/vdso_test.cpp
index 71892a055daba5..a79ea7384e9190 100644
--- a/libc/test/src/__support/OSUtil/linux/vdso_test.cpp
+++ b/libc/test/src/__support/OSUtil/linux/vdso_test.cpp
@@ -113,11 +113,13 @@ TEST(LlvmLibcOSUtilVDSOTest, RtSigReturn) {
struct sigaction sa{};
struct sigaction old_sa{};
sa.sa_handler = sigprof_handler;
- sa.sa_flags = SA_RESTORER;
vdso::TypedSymbol<vdso::VDSOSym::RTSigReturn> symbol;
if (!symbol)
return;
+#ifdef SA_RESTORER
+ sa.sa_flags = SA_RESTORER;
sa.sa_restorer = symbol;
+#endif
ASSERT_THAT(LIBC_NAMESPACE::sigaction(SIGPROF, &sa, &old_sa), Succeeds());
raise(SIGPROF);
ASSERT_TRUE(flag);
diff --git a/libc/test/src/signal/CMakeLists.txt b/libc/test/src/signal/CMakeLists.txt
index f22a8a8f0a092c..2d68915cb7cc45 100644
--- a/libc/test/src/signal/CMakeLists.txt
+++ b/libc/test/src/signal/CMakeLists.txt
@@ -36,6 +36,9 @@ add_libc_test(
libc.hdr.signal_macros
libc.src.signal.raise
libc.src.signal.sigaction
+ libc.src.signal.sigaddset
+ libc.src.signal.sigemptyset
+ libc.src.signal.sigprocmask
libc.test.UnitTest.ErrnoSetterMatcher
)
diff --git a/libc/test/src/signal/sigaction_test.cpp b/libc/test/src/signal/sigaction_test.cpp
index c26cc4927e70fe..2413816d2c9365 100644
--- a/libc/test/src/signal/sigaction_test.cpp
+++ b/libc/test/src/signal/sigaction_test.cpp
@@ -10,6 +10,9 @@
#include "hdr/signal_macros.h"
#include "src/signal/raise.h"
#include "src/signal/sigaction.h"
+#include "src/signal/sigaddset.h"
+#include "src/signal/sigemptyset.h"
+#include "src/signal/sigprocmask.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
@@ -168,3 +171,37 @@ TEST(LlvmLibcSigaction, Siginfo) {
EXPECT_TRUE(SiginfoTest::received);
EXPECT_EQ(SiginfoTest::signo, SIGUSR1);
}
+
+struct SigmaskTest {
+ inline static sigset_t blocked;
+ static void handler(int) {
+ LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, nullptr, &blocked);
+ }
+};
+
+// Verify that sa_mask reaches the kernel, comes back unchanged, and is blocked
+// along with the signal itself while the handler runs.
+TEST(LlvmLibcSigaction, Sigmask) {
+ struct sigaction action{};
+ action.sa_handler = SigmaskTest::handler;
+ action.sa_flags = SA_RESTART;
+ ASSERT_EQ(LIBC_NAMESPACE::sigemptyset(&action.sa_mask), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&action.sa_mask, SIGUSR2), 0);
+ struct sigaction old_action{};
+ ASSERT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1, &action, &old_action),
+ Succeeds());
+
+ struct sigaction current{};
+ ASSERT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1, nullptr, ¤t),
+ Succeeds());
+ EXPECT_EQ(current.sa_mask.__signals[0], action.sa_mask.__signals[0]);
+ EXPECT_EQ(current.sa_flags & SA_RESTART, SA_RESTART);
+
+ LIBC_NAMESPACE::raise(SIGUSR1);
+ sigset_t expected = action.sa_mask;
+ ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&expected, SIGUSR1), 0);
+ EXPECT_EQ(SigmaskTest::blocked.__signals[0], expected.__signals[0]);
+
+ EXPECT_THAT(LIBC_NAMESPACE::sigaction(SIGUSR1, &old_action, nullptr),
+ Succeeds());
+}
More information about the libc-commits
mailing list