[libc-commits] [libc] bcbe075 - [libc] Reland 90cd08c8381cf01921b915a56c7347bed1bf7a11 with test fix. (#238934)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 4 12:46:09 PST 2026
Author: Alexey Samsonov
Date: 2026-11-04T12:46:03-08:00
New Revision: bcbe075193bfc20b86adb4bc89ff630d33a4dbcc
URL: https://github.com/llvm/llvm-project/commit/bcbe075193bfc20b86adb4bc89ff630d33a4dbcc
DIFF: https://github.com/llvm/llvm-project/commit/bcbe075193bfc20b86adb4bc89ff630d33a4dbcc.diff
LOG: [libc] Reland 90cd08c8381cf01921b915a56c7347bed1bf7a11 with test fix. (#238934)
Reland 90cd08c8381cf01921b915a56c7347bed1bf7a11 - Implement sigpending
Fix the test by verifying that SIGUSR1 is indeed blocked and is in
pending state.
But restoring the original mask caused this signal to be instantly
delivered
to the process, potentially crashing it.
Fix it by installing `SIG_IGN` handler before restore the original mask.
Added:
libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h
libc/src/signal/linux/sigpending.cpp
libc/src/signal/sigpending.h
libc/test/src/signal/sigpending_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/signal.yaml
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/signal/CMakeLists.txt
libc/src/signal/linux/CMakeLists.txt
libc/test/src/signal/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 81d842dd1d6ae..aa408d7dbbea4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1414,6 +1414,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigfillset
libc.src.signal.sigismember
libc.src.signal.signal
+ libc.src.signal.sigpending
libc.src.signal.sigprocmask
libc.src.signal.sigsuspend
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index d7129e8b362ff..10e6fb74c7feb 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1600,6 +1600,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigfillset
libc.src.signal.sigismember
libc.src.signal.signal
+ libc.src.signal.sigpending
libc.src.signal.sigprocmask
libc.src.signal.sigsuspend
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 87c407dbe2584..ee3bb9d6eefa4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1626,6 +1626,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigfillset
libc.src.signal.sigismember
libc.src.signal.signal
+ libc.src.signal.sigpending
libc.src.signal.sigprocmask
libc.src.signal.sigsuspend
diff --git a/libc/include/signal.yaml b/libc/include/signal.yaml
index f5d4a08b0374c..47fb110b19f3c 100644
--- a/libc/include/signal.yaml
+++ b/libc/include/signal.yaml
@@ -126,6 +126,12 @@ functions:
arguments:
- type: int
- type: sighandler_t
+ - name: sigpending
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: sigset_t *
- name: sigprocmask
standards:
- posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 97034933a3abf..6c594331cfae3 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -693,6 +693,19 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ rt_sigpending
+ HDRS
+ rt_sigpending.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.hdr.types.sigset_t
+ libc.include.sys_syscall
+)
+
add_header_library(
rt_sigsuspend
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h
new file mode 100644
index 0000000000000..e55c75679f3ce
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for rt_sigpending.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGPENDING_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGPENDING_H
+
+#include "hdr/types/sigset_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> rt_sigpending(sigset_t *set) {
+ return syscall_checked<int>(SYS_rt_sigpending, set, sizeof(sigset_t));
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGPENDING_H
diff --git a/libc/src/signal/CMakeLists.txt b/libc/src/signal/CMakeLists.txt
index 209fec27d72a8..7e68f76b473ac 100644
--- a/libc/src/signal/CMakeLists.txt
+++ b/libc/src/signal/CMakeLists.txt
@@ -30,6 +30,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.sigaction
)
+add_entrypoint_object(
+ sigpending
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.sigpending
+)
+
add_entrypoint_object(
sigaltstack
ALIAS
diff --git a/libc/src/signal/linux/CMakeLists.txt b/libc/src/signal/linux/CMakeLists.txt
index edd4b96ca1d0d..cd7247d97f287 100644
--- a/libc/src/signal/linux/CMakeLists.txt
+++ b/libc/src/signal/linux/CMakeLists.txt
@@ -111,6 +111,18 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ sigpending
+ SRCS
+ sigpending.cpp
+ HDRS
+ ../sigpending.h
+ DEPENDS
+ libc.hdr.types.sigset_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.rt_sigpending
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
sigsuspend
SRCS
diff --git a/libc/src/signal/linux/sigpending.cpp b/libc/src/signal/linux/sigpending.cpp
new file mode 100644
index 0000000000000..2f34ac933664e
--- /dev/null
+++ b/libc/src/signal/linux/sigpending.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of sigpending.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigpending.h"
+
+#include "hdr/types/sigset_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sigpending, (sigset_t * set)) {
+ auto result = linux_syscalls::rt_sigpending(set);
+ if (!result.has_value()) {
+ libc_errno = result.error();
+ return -1;
+ }
+ return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/signal/sigpending.h b/libc/src/signal/sigpending.h
new file mode 100644
index 0000000000000..e06ae04b4d15c
--- /dev/null
+++ b/libc/src/signal/sigpending.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for sigpending function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SIGNAL_SIGPENDING_H
+#define LLVM_LIBC_SRC_SIGNAL_SIGPENDING_H
+
+#include "hdr/types/sigset_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int sigpending(sigset_t *set);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SIGNAL_SIGPENDING_H
diff --git a/libc/test/src/signal/CMakeLists.txt b/libc/test/src/signal/CMakeLists.txt
index ea6a0cd6219b2..6e6dba4e3b833 100644
--- a/libc/test/src/signal/CMakeLists.txt
+++ b/libc/test/src/signal/CMakeLists.txt
@@ -69,6 +69,25 @@ add_libc_test(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ sigpending_test
+ SUITE
+ libc-signal-tests
+ SRCS
+ sigpending_test.cpp
+ DEPENDS
+ libc.hdr.signal_macros
+ libc.hdr.types.sigset_t
+ libc.src.signal.raise
+ libc.src.signal.sigaddset
+ libc.src.signal.sigemptyset
+ libc.src.signal.sigismember
+ libc.src.signal.signal
+ libc.src.signal.sigpending
+ libc.src.signal.sigprocmask
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_test(
sigsuspend_test
SUITE
diff --git a/libc/test/src/signal/sigpending_test.cpp b/libc/test/src/signal/sigpending_test.cpp
new file mode 100644
index 0000000000000..25d20f6e51b95
--- /dev/null
+++ b/libc/test/src/signal/sigpending_test.cpp
@@ -0,0 +1,59 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for sigpending.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigpending.h"
+
+#include "hdr/signal_macros.h"
+#include "hdr/types/sigset_t.h"
+#include "src/signal/raise.h"
+#include "src/signal/sigaddset.h"
+#include "src/signal/sigemptyset.h"
+#include "src/signal/sigismember.h"
+#include "src/signal/signal.h"
+#include "src/signal/sigprocmask.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcSigpendingTest, NoBlockedSignals) {
+ sigset_t pending;
+ ASSERT_THAT(LIBC_NAMESPACE::sigpending(&pending), Succeeds(0));
+}
+
+TEST(LlvmLibcSigpendingTest, BlockAndRaise) {
+ // Block SIGUSR1.
+ sigset_t blockset;
+ ASSERT_EQ(LIBC_NAMESPACE::sigemptyset(&blockset), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&blockset, SIGUSR1), 0);
+
+ sigset_t oldset;
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, &blockset, &oldset), 0);
+
+ // Raise SIGUSR1; it should become pending since it is blocked.
+ ASSERT_EQ(LIBC_NAMESPACE::raise(SIGUSR1), 0);
+
+ // Check that SIGUSR1 is now pending.
+ sigset_t pending;
+ ASSERT_THAT(LIBC_NAMESPACE::sigpending(&pending), Succeeds(0));
+ ASSERT_EQ(LIBC_NAMESPACE::sigismember(&pending, SIGUSR1), 1);
+
+ // Ignore SIGUSR1 so that when sigprocmask() below restores the original
+ // mask and sends a pending SIGUSR1 signal it will not crash the process.
+ auto old_handler = LIBC_NAMESPACE::signal(SIGUSR1, SIG_IGN);
+ ASSERT_NE(old_handler, SIG_ERR);
+
+ // Restore the old signal mask and handler.
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &oldset, nullptr), 0);
+ ASSERT_NE(LIBC_NAMESPACE::signal(SIGUSR1, old_handler), SIG_ERR);
+}
More information about the libc-commits
mailing list