blob: d9bfa787a5fe2fdb73f1fcf9940a31edc5fec41e [file] [log] [blame]
Elliott Hughesda73f652012-11-30 16:40:55 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Josh Gao61cf3f32016-03-08 15:27:15 -080017#include <errno.h>
Elliott Hughesafe58ad2014-09-04 13:54:42 -070018#include <signal.h>
Tamas Zsoldos3bfa4bc2025-04-29 08:27:50 -070019#include <sys/auxv.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070020#include <sys/cdefs.h>
Josh Gao61cf3f32016-03-08 15:27:15 -080021#include <sys/syscall.h>
22#include <sys/types.h>
23#include <unistd.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080024
Elliott Hughesca3f8e42019-10-28 15:59:38 -070025#include <chrono>
Elliott Hughes5905d6f2018-01-30 15:09:51 -080026#include <thread>
27
Josh Gaobaf20fc2018-10-08 17:28:07 -070028#include <android-base/macros.h>
Elliott Hughesf2ee3db2025-05-06 14:59:21 -070029#include <android-base/test_utils.h>
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010030#include <android-base/threads.h>
31
Elliott Hughesafe58ad2014-09-04 13:54:42 -070032#include <gtest/gtest.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080033
Elliott Hughes71ba5892018-02-07 12:44:45 -080034#include "SignalUtils.h"
Tamas Petz99032782025-05-14 14:03:43 +020035#include "sme_utils.h"
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010036#include "utils.h"
Christopher Ferris13613132013-10-28 15:24:04 -070037
Elliott Hughesca3f8e42019-10-28 15:59:38 -070038using namespace std::chrono_literals;
39
Colin Cross23b986c2022-10-19 15:03:59 -070040#if defined(ANDROID_HOST_MUSL)
41// Musl doesn't export __SIGRTMIN and __SIGRTMAX, #define
42// them here.
43#define __SIGRTMIN 32
44#define __SIGRTMAX 64
45#endif
46
Elliott Hughes5905d6f2018-01-30 15:09:51 -080047static int SIGNAL_MIN() {
Elliott Hughes40d105c2013-10-16 12:53:58 -070048 return 1; // Signals start at 1 (SIGHUP), not 0.
49}
50
Elliott Hughes5905d6f2018-01-30 15:09:51 -080051template <typename SigSetT>
52static int SIGNAL_MAX(SigSetT* set) {
53 return sizeof(*set) * 8;
Elliott Hughes40d105c2013-10-16 12:53:58 -070054}
55
Elliott Hughes5905d6f2018-01-30 15:09:51 -080056template <typename SigSetT>
57static void TestSigSet1(int (fn)(SigSetT*)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070058 // nullptr sigset_t*/sigset64_t*.
59 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080060 errno = 0;
61 ASSERT_EQ(-1, fn(set_ptr));
Elliott Hughes95646e62023-09-21 14:11:19 -070062 ASSERT_ERRNO(EINVAL);
Elliott Hughesda73f652012-11-30 16:40:55 -080063
Yi Kong32bc0fc2018-08-02 17:31:13 -070064 // Non-nullptr.
Elliott Hughes5905d6f2018-01-30 15:09:51 -080065 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080066 errno = 0;
67 ASSERT_EQ(0, fn(&set));
Elliott Hughes95646e62023-09-21 14:11:19 -070068 ASSERT_ERRNO(0);
Elliott Hughesda73f652012-11-30 16:40:55 -080069}
70
Elliott Hughes5905d6f2018-01-30 15:09:51 -080071template <typename SigSetT>
72static void TestSigSet2(int (fn)(SigSetT*, int)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070073 // nullptr sigset_t*/sigset64_t*.
74 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080075 errno = 0;
76 ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
Elliott Hughes95646e62023-09-21 14:11:19 -070077 ASSERT_ERRNO(EINVAL);
Elliott Hughesda73f652012-11-30 16:40:55 -080078
Elliott Hughes5905d6f2018-01-30 15:09:51 -080079 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080080
Elliott Hughesda73f652012-11-30 16:40:55 -080081 // Bad signal number: too small.
82 errno = 0;
83 ASSERT_EQ(-1, fn(&set, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -070084 ASSERT_ERRNO(EINVAL);
Elliott Hughesda73f652012-11-30 16:40:55 -080085
86 // Bad signal number: too high.
87 errno = 0;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080088 ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1));
Elliott Hughes95646e62023-09-21 14:11:19 -070089 ASSERT_ERRNO(EINVAL);
Elliott Hughesda73f652012-11-30 16:40:55 -080090
91 // Good signal numbers, low and high ends of range.
92 errno = 0;
Elliott Hughes40d105c2013-10-16 12:53:58 -070093 ASSERT_EQ(0, fn(&set, SIGNAL_MIN()));
Elliott Hughes95646e62023-09-21 14:11:19 -070094 ASSERT_ERRNO(0);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080095 ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set)));
Elliott Hughes95646e62023-09-21 14:11:19 -070096 ASSERT_ERRNO(0);
Elliott Hughesda73f652012-11-30 16:40:55 -080097}
98
Elliott Hughesda73f652012-11-30 16:40:55 -080099TEST(signal, sigaddset_invalid) {
100 TestSigSet2(sigaddset);
101}
102
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800103TEST(signal, sigaddset64_invalid) {
104#if defined(__BIONIC__)
105 TestSigSet2(sigaddset64);
106#endif
107}
108
Elliott Hughesda73f652012-11-30 16:40:55 -0800109TEST(signal, sigdelset_invalid) {
110 TestSigSet2(sigdelset);
111}
112
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800113TEST(signal, sigdelset64_invalid) {
114#if defined(__BIONIC__)
115 TestSigSet2(sigdelset64);
116#endif
117}
118
Elliott Hughesda73f652012-11-30 16:40:55 -0800119TEST(signal, sigemptyset_invalid) {
120 TestSigSet1(sigemptyset);
121}
122
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800123TEST(signal, sigemptyset64_invalid) {
124#if defined(__BIONIC__)
125 TestSigSet1(sigemptyset64);
126#endif
127}
128
Elliott Hughesda73f652012-11-30 16:40:55 -0800129TEST(signal, sigfillset_invalid) {
130 TestSigSet1(sigfillset);
131}
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800132
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800133TEST(signal, sigfillset64_invalid) {
134#if defined(__BIONIC__)
135 TestSigSet1(sigfillset64);
136#endif
137}
138
139TEST(signal, sigismember_invalid) {
140 TestSigSet2(sigismember);
141}
142
143TEST(signal, sigismember64_invalid) {
144#if defined(__BIONIC__)
145 TestSigSet2(sigismember64);
146#endif
147}
148
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800149TEST(signal, raise_invalid) {
150 errno = 0;
151 ASSERT_EQ(-1, raise(-1));
Elliott Hughes95646e62023-09-21 14:11:19 -0700152 ASSERT_ERRNO(EINVAL);
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800153}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800154
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800155static void raise_in_signal_handler_helper(int signal_number) {
156 ASSERT_EQ(SIGALRM, signal_number);
157 static int count = 0;
158 if (++count == 1) {
159 raise(SIGALRM);
160 }
161}
162
163TEST(signal, raise_in_signal_handler) {
164 ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
165 raise(SIGALRM);
166}
167
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800168static int g_sigsuspend_signal_handler_call_count = 0;
169
Elliott Hughes40d105c2013-10-16 12:53:58 -0700170TEST(signal, sigsuspend_sigpending) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800171 SignalMaskRestorer smr;
172
Elliott Hughes1f5af922013-10-15 18:01:56 -0700173 // Block SIGALRM.
174 sigset_t just_SIGALRM;
175 sigemptyset(&just_SIGALRM);
176 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800177 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Elliott Hughes1f5af922013-10-15 18:01:56 -0700178
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800179 ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; });
Elliott Hughes11952072013-10-24 15:15:14 -0700180
Elliott Hughes40d105c2013-10-16 12:53:58 -0700181 // There should be no pending signals.
182 sigset_t pending;
183 sigemptyset(&pending);
184 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800185 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700186 EXPECT_FALSE(sigismember(&pending, i)) << i;
187 }
188
Elliott Hughes1f5af922013-10-15 18:01:56 -0700189 // Raise SIGALRM and check our signal handler wasn't called.
190 raise(SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800191 ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700192
Elliott Hughes40d105c2013-10-16 12:53:58 -0700193 // We should now have a pending SIGALRM but nothing else.
194 sigemptyset(&pending);
195 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800196 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700197 EXPECT_EQ((i == SIGALRM), sigismember(&pending, i));
198 }
199
Elliott Hughes1f5af922013-10-15 18:01:56 -0700200 // Use sigsuspend to block everything except SIGALRM...
201 sigset_t not_SIGALRM;
202 sigfillset(&not_SIGALRM);
203 sigdelset(&not_SIGALRM, SIGALRM);
204 ASSERT_EQ(-1, sigsuspend(&not_SIGALRM));
Elliott Hughes95646e62023-09-21 14:11:19 -0700205 ASSERT_ERRNO(EINTR);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700206 // ...and check that we now receive our pending SIGALRM.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800207 ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count);
208}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700209
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800210static int g_sigsuspend64_signal_handler_call_count = 0;
211
212TEST(signal, sigsuspend64_sigpending64) {
213 SignalMaskRestorer smr;
214
215 // Block SIGRTMIN.
216 sigset64_t just_SIGRTMIN;
217 sigemptyset64(&just_SIGRTMIN);
218 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
219 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
220
221 ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; });
222
223 // There should be no pending signals.
224 sigset64_t pending;
225 sigemptyset64(&pending);
226 ASSERT_EQ(0, sigpending64(&pending));
227 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
228 EXPECT_FALSE(sigismember64(&pending, i)) << i;
229 }
230
231 // Raise SIGRTMIN and check our signal handler wasn't called.
232 raise(SIGRTMIN);
233 ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count);
234
235 // We should now have a pending SIGRTMIN but nothing else.
236 sigemptyset64(&pending);
237 ASSERT_EQ(0, sigpending64(&pending));
238 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
239 EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i));
240 }
241
242 // Use sigsuspend64 to block everything except SIGRTMIN...
243 sigset64_t not_SIGRTMIN;
244 sigfillset64(&not_SIGRTMIN);
245 sigdelset64(&not_SIGRTMIN, SIGRTMIN);
246 ASSERT_EQ(-1, sigsuspend64(&not_SIGRTMIN));
Elliott Hughes95646e62023-09-21 14:11:19 -0700247 ASSERT_ERRNO(EINTR);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800248 // ...and check that we now receive our pending SIGRTMIN.
249 ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700250}
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700251
Elliott Hughes3e235912018-02-01 14:21:51 -0800252template <typename SigActionT, typename SigSetT>
253static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*),
254 int (sigaddset_fn)(SigSetT*, int),
255 int sig) {
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700256 // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm,
257 // arm64, x86, and x86-64. The version of glibc we're using also doesn't
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800258 // define SA_RESTORER, but luckily it's the same value everywhere.
Elliott Hughesaa13e832014-09-04 15:43:10 -0700259 static const unsigned sa_restorer = 0x4000000;
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700260
Elliott Hughes3e235912018-02-01 14:21:51 -0800261 // See what's currently set for this signal.
262 SigActionT original_sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700263 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &original_sa));
264 ASSERT_TRUE(original_sa.sa_handler == nullptr);
265 ASSERT_TRUE(original_sa.sa_sigaction == nullptr);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700266 ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100267#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100268 ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100269#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700270
271 // Set a traditional sa_handler signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800272 auto no_op_signal_handler = [](int) {};
273 SigActionT sa = {};
274 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700275 sa.sa_flags = SA_ONSTACK;
Elliott Hughes3e235912018-02-01 14:21:51 -0800276 sa.sa_handler = no_op_signal_handler;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700277 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700278
279 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800280 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700281 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800282 ASSERT_TRUE(sa.sa_handler == no_op_signal_handler);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700283 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700284 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100285#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100286 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100287#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700288
289 // Set a new-style sa_sigaction signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800290 auto no_op_sigaction = [](int, siginfo_t*, void*) {};
291 sa = {};
292 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700293 sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
Elliott Hughes3e235912018-02-01 14:21:51 -0800294 sa.sa_sigaction = no_op_sigaction;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700295 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700296
297 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800298 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700299 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800300 ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700301 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700302 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100303#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100304 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100305#endif
Elliott Hughes11952072013-10-24 15:15:14 -0700306
307 // Put everything back how it was.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700308 ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr));
Elliott Hughes3e235912018-02-01 14:21:51 -0800309}
310
311TEST(signal, sigaction) {
312 TestSigAction(sigaction, sigaddset, SIGALRM);
313}
314
315TEST(signal, sigaction64_SIGRTMIN) {
316 TestSigAction(sigaction64, sigaddset64, SIGRTMIN);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700317}
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800318
Josh Gao6fcba932018-02-09 13:38:32 -0800319static void ClearSignalMask() {
320 uint64_t sigset = 0;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700321 SignalSetAdd(&sigset, __SIGRTMIN);
322 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
323 abort();
324 }
325}
326
327static void FillSignalMask() {
328 uint64_t sigset = ~0ULL;
329 for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) {
330 SignalSetDel(&sigset, signo);
331 }
Josh Gao6fcba932018-02-09 13:38:32 -0800332 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
333 abort();
334 }
335}
336
337static uint64_t GetSignalMask() {
338 uint64_t sigset;
339 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, nullptr, &sigset, sizeof(sigset)) != 0) {
340 abort();
341 }
342 return sigset;
343}
344
Josh Gaobaf20fc2018-10-08 17:28:07 -0700345static void TestSignalMaskFiltered(uint64_t sigset) {
346#if defined(__BIONIC__)
347 for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) {
Josh Gao6fcba932018-02-09 13:38:32 -0800348 bool signal_blocked = sigset & (1ULL << (signo - 1));
Josh Gaobaf20fc2018-10-08 17:28:07 -0700349 if (signo == __SIGRTMIN) {
350 // TIMER_SIGNAL must be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800351 EXPECT_EQ(true, signal_blocked) << "signal " << signo;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700352 } else {
353 // The other reserved signals must not be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800354 EXPECT_EQ(false, signal_blocked) << "signal " << signo;
Josh Gao6fcba932018-02-09 13:38:32 -0800355 }
356 }
Josh Gaobaf20fc2018-10-08 17:28:07 -0700357#else
358 UNUSED(sigset);
359#endif
Josh Gao6fcba932018-02-09 13:38:32 -0800360}
361
Josh Gaobaf20fc2018-10-08 17:28:07 -0700362static void TestSignalMaskFunction(std::function<void()> fn) {
Josh Gao6fcba932018-02-09 13:38:32 -0800363 ClearSignalMask();
364 fn();
Josh Gaobaf20fc2018-10-08 17:28:07 -0700365 TestSignalMaskFiltered(GetSignalMask());
Josh Gao6fcba932018-02-09 13:38:32 -0800366}
367
368TEST(signal, sigaction_filter) {
369 ClearSignalMask();
370 static uint64_t sigset;
371 struct sigaction sa = {};
372 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800373 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800374 sigfillset(&sa.sa_mask);
375 sigaction(SIGUSR1, &sa, nullptr);
376 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800377
378 // On LP32, struct sigaction::sa_mask is only 32-bits wide.
379 unsigned long expected_sigset = ~0UL;
380
381 // SIGKILL and SIGSTOP are always blocked.
382 expected_sigset &= ~(1UL << (SIGKILL - 1));
383 expected_sigset &= ~(1UL << (SIGSTOP - 1));
384
385 ASSERT_EQ(static_cast<uint64_t>(expected_sigset), sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800386}
387
388TEST(signal, sigaction64_filter) {
389 ClearSignalMask();
390 static uint64_t sigset;
391 struct sigaction64 sa = {};
392 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800393 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800394 sigfillset64(&sa.sa_mask);
395 sigaction64(SIGUSR1, &sa, nullptr);
396 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800397
398 uint64_t expected_sigset = ~0ULL;
399
400 // SIGKILL and SIGSTOP are always blocked.
401 expected_sigset &= ~(1ULL << (SIGKILL - 1));
402 expected_sigset &= ~(1ULL << (SIGSTOP - 1));
403
404 ASSERT_EQ(expected_sigset, sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800405}
406
407TEST(signal, sigprocmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700408 TestSignalMaskFunction([]() {
409 ClearSignalMask();
410 sigset_t sigset_libc;
411 sigfillset(&sigset_libc);
412 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr));
413 });
Josh Gao6fcba932018-02-09 13:38:32 -0800414}
415
416TEST(signal, sigprocmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700417 TestSignalMaskFunction([]() {
418 ClearSignalMask();
419 sigset64_t sigset_libc;
420 sigfillset64(&sigset_libc);
421 ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr));
422 });
Josh Gao6fcba932018-02-09 13:38:32 -0800423}
424
425TEST(signal, pthread_sigmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700426 TestSignalMaskFunction([]() {
427 ClearSignalMask();
428 sigset_t sigset_libc;
429 sigfillset(&sigset_libc);
430 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr));
431 });
Josh Gao6fcba932018-02-09 13:38:32 -0800432}
433
434TEST(signal, pthread_sigmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700435 TestSignalMaskFunction([]() {
436 ClearSignalMask();
437 sigset64_t sigset_libc;
438 sigfillset64(&sigset_libc);
439 ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr));
440 });
Josh Gao6fcba932018-02-09 13:38:32 -0800441}
442
443TEST(signal, sigprocmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700444 TestSignalMaskFunction([]() {
445 ClearSignalMask();
446 sigset_t sigset_libc;
447 sigfillset(&sigset_libc);
448 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr));
449 });
Josh Gao6fcba932018-02-09 13:38:32 -0800450}
451
452TEST(signal, sigprocmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700453 TestSignalMaskFunction([]() {
454 ClearSignalMask();
455 sigset64_t sigset_libc;
456 sigfillset64(&sigset_libc);
457 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr));
458 });
Josh Gao6fcba932018-02-09 13:38:32 -0800459}
460
461TEST(signal, pthread_sigmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700462 TestSignalMaskFunction([]() {
463 ClearSignalMask();
464 sigset_t sigset_libc;
465 sigfillset(&sigset_libc);
466 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr));
467 });
Josh Gao6fcba932018-02-09 13:38:32 -0800468}
469
470TEST(signal, pthread_sigmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700471 TestSignalMaskFunction([]() {
472 ClearSignalMask();
473 sigset64_t sigset_libc;
474 sigfillset64(&sigset_libc);
475 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &sigset_libc, nullptr));
476 });
477}
478
479TEST(signal, sigprocmask_unblock_filter) {
480 TestSignalMaskFunction([]() {
481 FillSignalMask();
482 sigset_t sigset_libc;
483 sigfillset(&sigset_libc);
484 ASSERT_EQ(0, sigprocmask(SIG_UNBLOCK, &sigset_libc, nullptr));
485 });
486}
487
488TEST(signal, sigprocmask64_unblock_filter) {
489 TestSignalMaskFunction([]() {
490 FillSignalMask();
491 sigset64_t sigset_libc;
492 sigfillset64(&sigset_libc);
493 ASSERT_EQ(0, sigprocmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
494 });
495}
496
497TEST(signal, pthread_sigmask_unblock_filter) {
498 TestSignalMaskFunction([]() {
499 FillSignalMask();
500 sigset_t sigset_libc;
501 sigfillset(&sigset_libc);
502 ASSERT_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigset_libc, nullptr));
503 });
504}
505
506TEST(signal, pthread_sigmask64_unblock_filter) {
507 TestSignalMaskFunction([]() {
508 FillSignalMask();
509 sigset64_t sigset_libc;
510 sigfillset64(&sigset_libc);
511 ASSERT_EQ(0, pthread_sigmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
512 });
Josh Gao6fcba932018-02-09 13:38:32 -0800513}
514
515// glibc filters out signals via sigfillset, not the actual underlying functions.
516TEST(signal, sigset_filter) {
517#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700518 TestSignalMaskFunction([]() {
519 for (int i = 1; i <= 64; ++i) {
520 sigset(i, SIG_HOLD);
521 }
522 });
Josh Gao6fcba932018-02-09 13:38:32 -0800523#endif
524}
525
526TEST(signal, sighold_filter) {
527#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700528 TestSignalMaskFunction([]() {
529 for (int i = 1; i <= 64; ++i) {
530 sighold(i);
531 }
532 });
Josh Gao6fcba932018-02-09 13:38:32 -0800533#endif
534}
535
Elliott Hughes215baed2023-07-17 17:15:01 -0700536#if defined(__BIONIC__) && !defined(__riscv)
Josh Gao6fcba932018-02-09 13:38:32 -0800537// Not exposed via headers, but the symbols are available if you declare them yourself.
538extern "C" int sigblock(int);
539extern "C" int sigsetmask(int);
Elliott Hughes215baed2023-07-17 17:15:01 -0700540#define HAVE_SIGBLOCK_SIGSETMASK
Josh Gao6fcba932018-02-09 13:38:32 -0800541#endif
542
543TEST(signal, sigblock_filter) {
Elliott Hughes215baed2023-07-17 17:15:01 -0700544#if defined(HAVE_SIGBLOCK_SIGSETMASK)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700545 TestSignalMaskFunction([]() {
546 sigblock(~0U);
547 });
Josh Gao6fcba932018-02-09 13:38:32 -0800548#endif
549}
550
551TEST(signal, sigsetmask_filter) {
Elliott Hughes215baed2023-07-17 17:15:01 -0700552#if defined(HAVE_SIGBLOCK_SIGSETMASK)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700553 TestSignalMaskFunction([]() {
554 sigsetmask(~0U);
555 });
Josh Gao6fcba932018-02-09 13:38:32 -0800556#endif
557}
558
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800559TEST(signal, sys_signame) {
Elliott Hughes671e2362014-02-12 19:04:27 -0800560#if defined(__BIONIC__)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700561 ASSERT_TRUE(sys_signame[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800562 ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
563#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800564 GTEST_SKIP() << "glibc doesn't have sys_signame";
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800565#endif
566}
567
568TEST(signal, sys_siglist) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700569#if !defined(ANDROID_HOST_MUSL)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700570 ASSERT_TRUE(sys_siglist[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800571 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
Colin Cross7da20342021-07-28 11:18:11 -0700572#else
573 GTEST_SKIP() << "musl doesn't have sys_siglist";
574#endif
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800575}
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700576
577TEST(signal, limits) {
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800578 // These come from the kernel.
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700579 ASSERT_EQ(32, __SIGRTMIN);
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800580 ASSERT_EQ(64, __SIGRTMAX);
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700581
582 // We reserve a non-zero number at the bottom for ourselves.
583 ASSERT_GT(SIGRTMIN, __SIGRTMIN);
584
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700585 // We don't currently reserve any at the top.
586 ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
587}
Yabin Cui63481602014-12-01 17:41:04 -0800588
589static int g_sigqueue_signal_handler_call_count = 0;
590
591static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) {
592 ASSERT_EQ(SIGALRM, signum);
593 ASSERT_EQ(SIGALRM, info->si_signo);
594 ASSERT_EQ(SI_QUEUE, info->si_code);
595 ASSERT_EQ(1, info->si_value.sival_int);
596 ++g_sigqueue_signal_handler_call_count;
597}
598
599TEST(signal, sigqueue) {
600 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700601 sigval sigval = {.sival_int = 1};
Yabin Cui63481602014-12-01 17:41:04 -0800602 errno = 0;
603 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
Elliott Hughes95646e62023-09-21 14:11:19 -0700604 ASSERT_ERRNO(0);
Yabin Cui63481602014-12-01 17:41:04 -0800605 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
606}
607
Josh Gao726b63f2018-08-27 16:00:58 -0700608TEST(signal, pthread_sigqueue_self) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700609#if !defined(ANDROID_HOST_MUSL)
Josh Gao726b63f2018-08-27 16:00:58 -0700610 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700611 sigval sigval = {.sival_int = 1};
Josh Gao726b63f2018-08-27 16:00:58 -0700612 errno = 0;
613 ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval));
Elliott Hughes95646e62023-09-21 14:11:19 -0700614 ASSERT_ERRNO(0);
Josh Gao726b63f2018-08-27 16:00:58 -0700615 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
Colin Cross7da20342021-07-28 11:18:11 -0700616#else
617 GTEST_SKIP() << "musl doesn't have pthread_sigqueue";
618#endif
Josh Gao726b63f2018-08-27 16:00:58 -0700619}
620
621TEST(signal, pthread_sigqueue_other) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700622#if !defined(ANDROID_HOST_MUSL)
Josh Gao726b63f2018-08-27 16:00:58 -0700623 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700624 sigval sigval = {.sival_int = 1};
Josh Gao726b63f2018-08-27 16:00:58 -0700625
626 sigset_t mask;
627 sigfillset(&mask);
628 pthread_sigmask(SIG_SETMASK, &mask, nullptr);
629 pthread_t thread;
630 int rc = pthread_create(&thread, nullptr,
631 [](void*) -> void* {
632 sigset_t mask;
633 sigemptyset(&mask);
634 sigsuspend(&mask);
635 return nullptr;
636 },
637 nullptr);
638 ASSERT_EQ(0, rc);
639
640 errno = 0;
641 ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval));
Elliott Hughes95646e62023-09-21 14:11:19 -0700642 ASSERT_ERRNO(0);
Josh Gao726b63f2018-08-27 16:00:58 -0700643 pthread_join(thread, nullptr);
644 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
Colin Cross7da20342021-07-28 11:18:11 -0700645#else
646 GTEST_SKIP() << "musl doesn't have pthread_sigqueue";
647#endif
Josh Gao726b63f2018-08-27 16:00:58 -0700648}
649
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700650TEST(signal, sigwait_SIGALRM) {
651 SignalMaskRestorer smr;
652
653 // Block SIGALRM.
654 sigset_t just_SIGALRM;
655 sigemptyset(&just_SIGALRM);
656 sigaddset(&just_SIGALRM, SIGALRM);
657 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
658
659 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700660 sigval sigval = {.sival_int = 1};
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700661 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
662
663 // Get pending SIGALRM.
664 int sig;
665 ASSERT_EQ(0, sigwait(&just_SIGALRM, &sig));
666 ASSERT_EQ(SIGALRM, sig);
667}
668
669TEST(signal, sigwait64_SIGRTMIN) {
670 SignalMaskRestorer smr;
671
672 // Block SIGRTMIN.
673 sigset64_t just_SIGRTMIN;
674 sigemptyset64(&just_SIGRTMIN);
675 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
676 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
677
678 // Raise SIGRTMIN.
Colin Cross7da20342021-07-28 11:18:11 -0700679 sigval sigval = {.sival_int = 1};
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700680 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
681
682 // Get pending SIGRTMIN.
683 int sig;
684 ASSERT_EQ(0, sigwait64(&just_SIGRTMIN, &sig));
685 ASSERT_EQ(SIGRTMIN, sig);
686}
687
Yabin Cui63481602014-12-01 17:41:04 -0800688TEST(signal, sigwaitinfo) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800689 SignalMaskRestorer smr;
690
Yabin Cui63481602014-12-01 17:41:04 -0800691 // Block SIGALRM.
692 sigset_t just_SIGALRM;
693 sigemptyset(&just_SIGALRM);
694 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800695 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800696
697 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700698 sigval sigval = {.sival_int = 1};
Yabin Cui63481602014-12-01 17:41:04 -0800699 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
700
701 // Get pending SIGALRM.
702 siginfo_t info;
703 errno = 0;
704 ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info));
Elliott Hughes95646e62023-09-21 14:11:19 -0700705 ASSERT_ERRNO(0);
Yabin Cui63481602014-12-01 17:41:04 -0800706 ASSERT_EQ(SIGALRM, info.si_signo);
707 ASSERT_EQ(1, info.si_value.sival_int);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800708}
Yabin Cui63481602014-12-01 17:41:04 -0800709
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800710TEST(signal, sigwaitinfo64_SIGRTMIN) {
711 SignalMaskRestorer smr;
712
713 // Block SIGRTMIN.
714 sigset64_t just_SIGRTMIN;
715 sigemptyset64(&just_SIGRTMIN);
716 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
717 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
718
719 // Raise SIGRTMIN.
Colin Cross7da20342021-07-28 11:18:11 -0700720 sigval sigval = {.sival_int = 1};
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800721 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
722
723 // Get pending SIGRTMIN.
724 siginfo_t info;
725 errno = 0;
726 ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info));
Elliott Hughes95646e62023-09-21 14:11:19 -0700727 ASSERT_ERRNO(0);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800728 ASSERT_EQ(SIGRTMIN, info.si_signo);
729 ASSERT_EQ(1, info.si_value.sival_int);
Yabin Cui63481602014-12-01 17:41:04 -0800730}
731
732TEST(signal, sigtimedwait) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800733 SignalMaskRestorer smr;
734
Yabin Cui63481602014-12-01 17:41:04 -0800735 // Block SIGALRM.
736 sigset_t just_SIGALRM;
737 sigemptyset(&just_SIGALRM);
738 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800739 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800740
741 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700742 sigval sigval = { .sival_int = 1 };
Yabin Cui63481602014-12-01 17:41:04 -0800743 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
744
745 // Get pending SIGALRM.
746 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800747 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
Yabin Cui63481602014-12-01 17:41:04 -0800748 errno = 0;
749 ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout));
Elliott Hughes95646e62023-09-21 14:11:19 -0700750 ASSERT_ERRNO(0);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800751}
Yabin Cui63481602014-12-01 17:41:04 -0800752
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800753TEST(signal, sigtimedwait64_SIGRTMIN) {
754 SignalMaskRestorer smr;
755
756 // Block SIGRTMIN.
757 sigset64_t just_SIGRTMIN;
758 sigemptyset64(&just_SIGRTMIN);
759 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
760 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
761
762 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700763 sigval sigval = { .sival_int = 1 };
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800764 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
765
766 // Get pending SIGALRM.
767 siginfo_t info;
768 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
769 errno = 0;
770 ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout));
Elliott Hughes95646e62023-09-21 14:11:19 -0700771 ASSERT_ERRNO(0);
Yabin Cui63481602014-12-01 17:41:04 -0800772}
773
Yabin Cui63481602014-12-01 17:41:04 -0800774TEST(signal, sigtimedwait_timeout) {
775 // Block SIGALRM.
776 sigset_t just_SIGALRM;
777 sigemptyset(&just_SIGALRM);
778 sigaddset(&just_SIGALRM, SIGALRM);
779 sigset_t original_set;
780 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
781
782 // Wait timeout.
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700783 auto t0 = std::chrono::steady_clock::now();
Yabin Cui63481602014-12-01 17:41:04 -0800784 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800785 timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
Yabin Cui63481602014-12-01 17:41:04 -0800786 errno = 0;
787 ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
Elliott Hughes95646e62023-09-21 14:11:19 -0700788 ASSERT_ERRNO(EAGAIN);
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700789 auto t1 = std::chrono::steady_clock::now();
790 ASSERT_GE(t1-t0, 1000000ns);
Yabin Cui63481602014-12-01 17:41:04 -0800791
Yi Kong32bc0fc2018-08-02 17:31:13 -0700792 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800793}
Josh Gao61cf3f32016-03-08 15:27:15 -0800794
795#if defined(__BIONIC__)
796TEST(signal, rt_tgsigqueueinfo) {
797 // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self.
798 // If this fails, your kernel needs commit 66dd34a to be backported.
799 static constexpr char error_msg[] =
800 "\nPlease ensure that the following kernel patch has been applied:\n"
801 "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n";
802 static siginfo received;
803
Elliott Hughes3e235912018-02-01 14:21:51 -0800804 struct sigaction handler = {};
Josh Gao61cf3f32016-03-08 15:27:15 -0800805 handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; };
806 handler.sa_flags = SA_SIGINFO;
807
808 ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr));
809
Christopher Ferris2a391882024-12-19 13:44:35 -0800810 siginfo sent = {.si_code = SI_TKILL};
Josh Gao61cf3f32016-03-08 15:27:15 -0800811 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
812 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
813 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
814 << sent.si_code << ", received " << received.si_code
815 << error_msg;
816
817 sent.si_code = SI_USER;
818 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
819 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
820 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
821 << sent.si_code << ", received " << received.si_code
822 << error_msg;
823}
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800824#endif
Josh Gaoc244fcb2016-03-29 14:34:03 -0700825
Josh Gaoc244fcb2016-03-29 14:34:03 -0700826TEST(signal, sigset_size) {
Elliott Hughesfc035032022-11-11 22:52:04 +0000827 // The setjmp implementations assume that sigset_t can fit in a long.
828 // This is true because the 32-bit ABIs have broken rt signal support,
829 // but the 64-bit ABIs both have a SIGRTMAX defined as 64.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800830#if defined(__BIONIC__)
Josh Gaoc244fcb2016-03-29 14:34:03 -0700831 static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long");
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800832#endif
833 static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals");
Josh Gaoc244fcb2016-03-29 14:34:03 -0700834}
835
Greg Hackmann5375bf62016-02-29 12:35:33 -0800836TEST(signal, sigignore_EINVAL) {
837 errno = 0;
838 ASSERT_EQ(-1, sigignore(99999));
Elliott Hughes95646e62023-09-21 14:11:19 -0700839 ASSERT_ERRNO(EINVAL);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800840}
841
842TEST(signal, sigignore) {
843 errno = 0;
844 EXPECT_EQ(-1, sigignore(SIGKILL));
Elliott Hughes95646e62023-09-21 14:11:19 -0700845 EXPECT_ERRNO(EINVAL);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800846
847 errno = 0;
848 EXPECT_EQ(-1, sigignore(SIGSTOP));
Elliott Hughes95646e62023-09-21 14:11:19 -0700849 EXPECT_ERRNO(EINVAL);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800850
851 ScopedSignalHandler sigalrm{SIGALRM};
852 ASSERT_EQ(0, sigignore(SIGALRM));
853
854 struct sigaction sa;
855 ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa));
856 EXPECT_EQ(SIG_IGN, sa.sa_handler);
857}
858
859TEST(signal, sighold_EINVAL) {
860 errno = 0;
861 ASSERT_EQ(-1, sighold(99999));
Elliott Hughes95646e62023-09-21 14:11:19 -0700862 ASSERT_ERRNO(EINVAL);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800863}
864
865TEST(signal, sigpause_EINVAL) {
866 errno = 0;
867 ASSERT_EQ(-1, sigpause(99999));
Elliott Hughes95646e62023-09-21 14:11:19 -0700868 ASSERT_ERRNO(EINVAL);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800869}
870
871TEST(signal, sigrelse_EINVAL) {
872 errno = 0;
873 ASSERT_EQ(-1, sigpause(99999));
Elliott Hughes95646e62023-09-21 14:11:19 -0700874 ASSERT_ERRNO(EINVAL);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800875}
876
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800877static void TestSigholdSigpauseSigrelse(int sig) {
878 static int signal_handler_call_count = 0;
879 ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }};
880 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800881 sigset_t set;
882
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800883 // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ...
884 ASSERT_EQ(0, sighold(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700885 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800886 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800887
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800888 // ... preventing our SIGALRM/SIGRTMIN handler from running ...
889 raise(sig);
890 ASSERT_EQ(0, signal_handler_call_count);
891 // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it.
892 ASSERT_EQ(-1, sigpause(sig));
Elliott Hughes95646e62023-09-21 14:11:19 -0700893 ASSERT_ERRNO(EINTR);
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800894 ASSERT_EQ(1, signal_handler_call_count);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800895
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800896 if (sig >= SIGRTMIN && sizeof(void*) == 8) {
897 // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700898 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800899 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800900
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800901 // Whereas sigrelse(SIGALRM/SIGRTMIN) should.
902 ASSERT_EQ(0, sigrelse(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700903 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800904 EXPECT_FALSE(sigismember(&set, sig));
905 } else {
906 // sigismember won't work for SIGRTMIN on LP32.
907 }
908}
909
910TEST(signal, sighold_sigpause_sigrelse) {
911 TestSigholdSigpauseSigrelse(SIGALRM);
912}
913
914TEST(signal, sighold_sigpause_sigrelse_RT) {
915 TestSigholdSigpauseSigrelse(SIGRTMIN);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800916}
917
918TEST(signal, sigset_EINVAL) {
919 errno = 0;
920 ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL));
Elliott Hughes95646e62023-09-21 14:11:19 -0700921 ASSERT_ERRNO(EINVAL);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800922}
923
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800924TEST(signal, sigset_RT) {
925 static int signal_handler_call_count = 0;
926 auto signal_handler = [](int) { signal_handler_call_count++; };
927 ScopedSignalHandler ssh{SIGRTMIN, signal_handler};
928 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800929
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800930 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD));
931#if defined(__LP64__)
Greg Hackmann5375bf62016-02-29 12:35:33 -0800932 sigset_t set;
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800933 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
934 ASSERT_TRUE(sigismember(&set, SIGRTMIN));
935#endif
936
937 ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler));
938 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler));
939 ASSERT_EQ(0, signal_handler_call_count);
940 raise(SIGRTMIN);
941 ASSERT_EQ(1, signal_handler_call_count);
942}
943
944TEST(signal, sigset) {
945 static int signal_handler_call_count = 0;
946 auto signal_handler = [](int) { signal_handler_call_count++; };
947 ScopedSignalHandler ssh{SIGALRM, signal_handler};
948 SignalMaskRestorer mask_restorer;
949
950 ASSERT_EQ(0, signal_handler_call_count);
951 raise(SIGALRM);
952 ASSERT_EQ(1, signal_handler_call_count);
953
954 // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD.
955 sigset_t set;
956 sigemptyset(&set);
957 sigaddset(&set, SIGALRM);
958 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr));
959
960 sigemptyset(&set);
961 ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800962 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
963 EXPECT_FALSE(sigismember(&set, SIGALRM));
964
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800965 ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800966 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
967 EXPECT_FALSE(sigismember(&set, SIGALRM));
968
969 ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL));
970 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
971 EXPECT_FALSE(sigismember(&set, SIGALRM));
972
973 ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD));
974 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
975 EXPECT_TRUE(sigismember(&set, SIGALRM));
976}
Elliott Hughes7532b322017-07-11 15:00:17 -0700977
978TEST(signal, killpg_EINVAL) {
979 // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0
980 // and passes 0 through to kill(2).
981 errno = 0;
982 ASSERT_EQ(-1, killpg(-1, SIGKILL));
Elliott Hughes95646e62023-09-21 14:11:19 -0700983 ASSERT_ERRNO(EINVAL);
Elliott Hughes7532b322017-07-11 15:00:17 -0700984}
Elliott Hughes55c1e372024-08-08 19:37:14 +0000985
986TEST(signal, sig2str) {
987#if defined(__BIONIC__)
988 char str[SIG2STR_MAX];
989
990 // A regular signal.
991 ASSERT_EQ(0, sig2str(SIGHUP, str));
992 ASSERT_STREQ("HUP", str);
993
994 // A real-time signal.
995 ASSERT_EQ(0, sig2str(SIGRTMIN + 4, str));
996 ASSERT_STREQ("RTMIN+4", str);
997 ASSERT_EQ(0, sig2str(SIGRTMAX - 4, str));
998 ASSERT_STREQ("RTMAX-4", str);
999 // Special cases.
1000 ASSERT_EQ(0, sig2str(SIGRTMAX, str));
1001 ASSERT_STREQ("RTMAX", str);
1002 ASSERT_EQ(0, sig2str(SIGRTMIN, str));
1003 ASSERT_STREQ("RTMIN", str);
1004 // One of the signals the C library keeps to itself.
1005 ASSERT_EQ(-1, sig2str(32, str)); // __SIGRTMIN
1006
1007 // Errors.
1008 ASSERT_EQ(-1, sig2str(-1, str)); // Too small.
1009 ASSERT_EQ(-1, sig2str(0, str)); // Still too small.
1010 ASSERT_EQ(-1, sig2str(1234, str)); // Too large.
1011#else
1012 GTEST_SKIP() << "our old glibc doesn't have sig2str";
1013#endif
1014}
1015
1016TEST(signal, str2sig) {
1017#if defined(__BIONIC__)
1018 int sig;
1019
1020 // A regular signal, by number.
1021 sig = -1;
1022 ASSERT_EQ(0, str2sig("9", &sig));
1023 ASSERT_EQ(SIGKILL, sig);
1024
1025 // A regular signal, by name.
1026 sig = -1;
1027 ASSERT_EQ(0, str2sig("HUP", &sig));
1028 ASSERT_EQ(SIGHUP, sig);
1029
1030 // A real-time signal, by number.
1031 sig = -1;
1032 ASSERT_EQ(0, str2sig("64", &sig));
1033 ASSERT_EQ(SIGRTMAX, sig);
1034
1035 // A real-time signal, by name and offset.
1036 sig = -1;
1037 ASSERT_EQ(0, str2sig("RTMAX-4", &sig));
1038 ASSERT_EQ(SIGRTMAX - 4, sig);
1039 sig = -1;
1040 ASSERT_EQ(0, str2sig("RTMIN+4", &sig));
1041 ASSERT_EQ(SIGRTMIN + 4, sig);
1042 // Unspecified by POSIX, but we try to be reasonable.
1043 sig = -1;
1044 ASSERT_EQ(0, str2sig("RTMAX-0", &sig));
1045 ASSERT_EQ(SIGRTMAX, sig);
1046 sig = -1;
1047 ASSERT_EQ(0, str2sig("RTMIN+0", &sig));
1048 ASSERT_EQ(SIGRTMIN, sig);
1049 // One of the signals the C library keeps to itself, numerically.
1050 ASSERT_EQ(-1, str2sig("32", &sig)); // __SIGRTMIN
1051
1052 // Special cases.
1053 sig = -1;
1054 ASSERT_EQ(0, str2sig("RTMAX", &sig));
1055 ASSERT_EQ(SIGRTMAX, sig);
1056 sig = -1;
1057 ASSERT_EQ(0, str2sig("RTMIN", &sig));
1058 ASSERT_EQ(SIGRTMIN, sig);
1059
1060 // Errors.
1061 ASSERT_EQ(-1, str2sig("SIGHUP", &sig)); // No "SIG" prefix allowed.
1062 ASSERT_EQ(-1, str2sig("-1", &sig)); // Too small.
1063 ASSERT_EQ(-1, str2sig("0", &sig)); // Still too small.
1064 ASSERT_EQ(-1, str2sig("1234", &sig)); // Too large.
1065 ASSERT_EQ(-1, str2sig("RTMAX-666", &sig)); // Offset too small.
1066 ASSERT_EQ(-1, str2sig("RTMIN+666", &sig)); // Offset too large.
1067 ASSERT_EQ(-1, str2sig("RTMAX-+1", &sig)); // Silly.
1068 ASSERT_EQ(-1, str2sig("RTMIN+-1", &sig)); // Silly.
1069 ASSERT_EQ(-1, str2sig("HUPs", &sig)); // Trailing junk.
1070 ASSERT_EQ(-1, str2sig("2b", &sig)); // Trailing junk.
1071 ASSERT_EQ(-1, str2sig("RTMIN+2b", &sig)); // Trailing junk.
1072#else
1073 GTEST_SKIP() << "our old glibc doesn't have str2sig";
1074#endif
1075}
Tamas Zsoldos3bfa4bc2025-04-29 08:27:50 -07001076
1077#if defined(__aarch64__)
Tamas Petz99032782025-05-14 14:03:43 +02001078static void raises_sigusr1() {
Tamas Zsoldos3bfa4bc2025-04-29 08:27:50 -07001079 raise(SIGUSR1);
1080}
1081
1082TEST(signal, sme_tpidr2_clear) {
1083 // When using SME, on entering a signal handler the kernel should clear TPIDR2_EL0, but this was
1084 // not always correctly done. This tests checks if the kernel correctly clears it or not.
Tamas Petz99032782025-05-14 14:03:43 +02001085 if (!sme_is_enabled()) {
Tamas Zsoldos3bfa4bc2025-04-29 08:27:50 -07001086 GTEST_SKIP() << "SME is not enabled on device.";
1087 }
1088
1089 static uint64_t tpidr2 = 0;
1090 struct sigaction handler = {};
1091 handler.sa_sigaction = [](int, siginfo_t*, void*) {
Tamas Petz99032782025-05-14 14:03:43 +02001092 tpidr2 = sme_tpidr2_el0();
1093 sme_set_tpidr2_el0(0UL);
Tamas Zsoldos3bfa4bc2025-04-29 08:27:50 -07001094 };
1095 handler.sa_flags = SA_SIGINFO;
1096
1097 ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr));
1098
Tamas Petz99032782025-05-14 14:03:43 +02001099 sme_dormant_caller(&raises_sigusr1);
Tamas Zsoldos3bfa4bc2025-04-29 08:27:50 -07001100
1101 ASSERT_EQ(0x0UL, tpidr2)
1102 << "Broken kernel! TPIDR2_EL0 was not null in the signal handler! "
1103 << "Please make sure the following patch has been applied to the kernel: "
1104 << "https://lore.kernel.org/linux-arm-kernel/[email protected]/";
1105}
1106#endif
Elliott Hughesf2ee3db2025-05-06 14:59:21 -07001107
1108TEST(signal, psignal) {
1109 CapturedStderr cap;
1110 psignal(SIGINT, "a b c");
1111 ASSERT_EQ(cap.str(), "a b c: Interrupt\n");
1112}
1113
1114TEST(signal, psignal_null) {
1115 CapturedStderr cap;
1116 psignal(SIGINT, nullptr);
1117 ASSERT_EQ(cap.str(), "Interrupt\n");
1118}
1119
1120TEST(signal, psignal_empty) {
1121 CapturedStderr cap;
1122 psignal(SIGINT, "");
1123 ASSERT_EQ(cap.str(), "Interrupt\n");
1124}
1125
1126TEST(signal, psiginfo) {
1127 CapturedStderr cap;
1128 siginfo_t si{.si_signo = SIGINT};
1129 psiginfo(&si, "a b c");
1130 ASSERT_EQ(cap.str(), "a b c: Interrupt\n");
1131}
1132
1133TEST(signal, psiginfo_null) {
1134 CapturedStderr cap;
1135 siginfo_t si{.si_signo = SIGINT};
1136 psiginfo(&si, nullptr);
1137 ASSERT_EQ(cap.str(), "Interrupt\n");
1138}
1139
1140TEST(signal, psiginfo_empty) {
1141 CapturedStderr cap;
1142 siginfo_t si{.si_signo = SIGINT};
1143 psiginfo(&si, "");
1144 ASSERT_EQ(cap.str(), "Interrupt\n");
1145}