blob: 08c21b4af8f89b53a7c3d37a0b41671ddb363f5f [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>
Josh Gao61cf3f32016-03-08 15:27:15 -080019#include <sys/syscall.h>
20#include <sys/types.h>
21#include <unistd.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080022
Elliott Hughesca3f8e42019-10-28 15:59:38 -070023#include <chrono>
Elliott Hughes5905d6f2018-01-30 15:09:51 -080024#include <thread>
25
Josh Gaobaf20fc2018-10-08 17:28:07 -070026#include <android-base/macros.h>
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010027#include <android-base/threads.h>
28
Elliott Hughesafe58ad2014-09-04 13:54:42 -070029#include <gtest/gtest.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080030
Elliott Hughes71ba5892018-02-07 12:44:45 -080031#include "SignalUtils.h"
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010032#include "utils.h"
Christopher Ferris13613132013-10-28 15:24:04 -070033
Elliott Hughesca3f8e42019-10-28 15:59:38 -070034using namespace std::chrono_literals;
35
Elliott Hughes5905d6f2018-01-30 15:09:51 -080036static int SIGNAL_MIN() {
Elliott Hughes40d105c2013-10-16 12:53:58 -070037 return 1; // Signals start at 1 (SIGHUP), not 0.
38}
39
Elliott Hughes5905d6f2018-01-30 15:09:51 -080040template <typename SigSetT>
41static int SIGNAL_MAX(SigSetT* set) {
42 return sizeof(*set) * 8;
Elliott Hughes40d105c2013-10-16 12:53:58 -070043}
44
Elliott Hughes5905d6f2018-01-30 15:09:51 -080045template <typename SigSetT>
46static void TestSigSet1(int (fn)(SigSetT*)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070047 // nullptr sigset_t*/sigset64_t*.
48 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080049 errno = 0;
50 ASSERT_EQ(-1, fn(set_ptr));
51 ASSERT_EQ(EINVAL, errno);
52
Yi Kong32bc0fc2018-08-02 17:31:13 -070053 // Non-nullptr.
Elliott Hughes5905d6f2018-01-30 15:09:51 -080054 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080055 errno = 0;
56 ASSERT_EQ(0, fn(&set));
57 ASSERT_EQ(0, errno);
58}
59
Elliott Hughes5905d6f2018-01-30 15:09:51 -080060template <typename SigSetT>
61static void TestSigSet2(int (fn)(SigSetT*, int)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070062 // nullptr sigset_t*/sigset64_t*.
63 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080064 errno = 0;
65 ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
66 ASSERT_EQ(EINVAL, errno);
67
Elliott Hughes5905d6f2018-01-30 15:09:51 -080068 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080069
Elliott Hughesda73f652012-11-30 16:40:55 -080070 // Bad signal number: too small.
71 errno = 0;
72 ASSERT_EQ(-1, fn(&set, 0));
73 ASSERT_EQ(EINVAL, errno);
74
75 // Bad signal number: too high.
76 errno = 0;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080077 ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1));
Elliott Hughesda73f652012-11-30 16:40:55 -080078 ASSERT_EQ(EINVAL, errno);
79
80 // Good signal numbers, low and high ends of range.
81 errno = 0;
Elliott Hughes40d105c2013-10-16 12:53:58 -070082 ASSERT_EQ(0, fn(&set, SIGNAL_MIN()));
Elliott Hughesda73f652012-11-30 16:40:55 -080083 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080084 ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set)));
Elliott Hughesda73f652012-11-30 16:40:55 -080085 ASSERT_EQ(0, errno);
86}
87
Elliott Hughesda73f652012-11-30 16:40:55 -080088TEST(signal, sigaddset_invalid) {
89 TestSigSet2(sigaddset);
90}
91
Elliott Hughes5905d6f2018-01-30 15:09:51 -080092TEST(signal, sigaddset64_invalid) {
93#if defined(__BIONIC__)
94 TestSigSet2(sigaddset64);
95#endif
96}
97
Elliott Hughesda73f652012-11-30 16:40:55 -080098TEST(signal, sigdelset_invalid) {
99 TestSigSet2(sigdelset);
100}
101
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800102TEST(signal, sigdelset64_invalid) {
103#if defined(__BIONIC__)
104 TestSigSet2(sigdelset64);
105#endif
106}
107
Elliott Hughesda73f652012-11-30 16:40:55 -0800108TEST(signal, sigemptyset_invalid) {
109 TestSigSet1(sigemptyset);
110}
111
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800112TEST(signal, sigemptyset64_invalid) {
113#if defined(__BIONIC__)
114 TestSigSet1(sigemptyset64);
115#endif
116}
117
Elliott Hughesda73f652012-11-30 16:40:55 -0800118TEST(signal, sigfillset_invalid) {
119 TestSigSet1(sigfillset);
120}
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800121
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800122TEST(signal, sigfillset64_invalid) {
123#if defined(__BIONIC__)
124 TestSigSet1(sigfillset64);
125#endif
126}
127
128TEST(signal, sigismember_invalid) {
129 TestSigSet2(sigismember);
130}
131
132TEST(signal, sigismember64_invalid) {
133#if defined(__BIONIC__)
134 TestSigSet2(sigismember64);
135#endif
136}
137
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800138TEST(signal, raise_invalid) {
139 errno = 0;
140 ASSERT_EQ(-1, raise(-1));
141 ASSERT_EQ(EINVAL, errno);
142}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800143
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800144static void raise_in_signal_handler_helper(int signal_number) {
145 ASSERT_EQ(SIGALRM, signal_number);
146 static int count = 0;
147 if (++count == 1) {
148 raise(SIGALRM);
149 }
150}
151
152TEST(signal, raise_in_signal_handler) {
153 ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
154 raise(SIGALRM);
155}
156
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800157static int g_sigsuspend_signal_handler_call_count = 0;
158
Elliott Hughes40d105c2013-10-16 12:53:58 -0700159TEST(signal, sigsuspend_sigpending) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800160 SignalMaskRestorer smr;
161
Elliott Hughes1f5af922013-10-15 18:01:56 -0700162 // Block SIGALRM.
163 sigset_t just_SIGALRM;
164 sigemptyset(&just_SIGALRM);
165 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800166 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Elliott Hughes1f5af922013-10-15 18:01:56 -0700167
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800168 ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; });
Elliott Hughes11952072013-10-24 15:15:14 -0700169
Elliott Hughes40d105c2013-10-16 12:53:58 -0700170 // There should be no pending signals.
171 sigset_t pending;
172 sigemptyset(&pending);
173 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800174 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700175 EXPECT_FALSE(sigismember(&pending, i)) << i;
176 }
177
Elliott Hughes1f5af922013-10-15 18:01:56 -0700178 // Raise SIGALRM and check our signal handler wasn't called.
179 raise(SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800180 ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700181
Elliott Hughes40d105c2013-10-16 12:53:58 -0700182 // We should now have a pending SIGALRM but nothing else.
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_EQ((i == SIGALRM), sigismember(&pending, i));
187 }
188
Elliott Hughes1f5af922013-10-15 18:01:56 -0700189 // Use sigsuspend to block everything except SIGALRM...
190 sigset_t not_SIGALRM;
191 sigfillset(&not_SIGALRM);
192 sigdelset(&not_SIGALRM, SIGALRM);
193 ASSERT_EQ(-1, sigsuspend(&not_SIGALRM));
194 ASSERT_EQ(EINTR, errno);
195 // ...and check that we now receive our pending SIGALRM.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800196 ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count);
197}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700198
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800199static int g_sigsuspend64_signal_handler_call_count = 0;
200
201TEST(signal, sigsuspend64_sigpending64) {
202 SignalMaskRestorer smr;
203
204 // Block SIGRTMIN.
205 sigset64_t just_SIGRTMIN;
206 sigemptyset64(&just_SIGRTMIN);
207 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
208 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
209
210 ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; });
211
212 // There should be no pending signals.
213 sigset64_t pending;
214 sigemptyset64(&pending);
215 ASSERT_EQ(0, sigpending64(&pending));
216 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
217 EXPECT_FALSE(sigismember64(&pending, i)) << i;
218 }
219
220 // Raise SIGRTMIN and check our signal handler wasn't called.
221 raise(SIGRTMIN);
222 ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count);
223
224 // We should now have a pending SIGRTMIN but nothing else.
225 sigemptyset64(&pending);
226 ASSERT_EQ(0, sigpending64(&pending));
227 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
228 EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i));
229 }
230
231 // Use sigsuspend64 to block everything except SIGRTMIN...
232 sigset64_t not_SIGRTMIN;
233 sigfillset64(&not_SIGRTMIN);
234 sigdelset64(&not_SIGRTMIN, SIGRTMIN);
235 ASSERT_EQ(-1, sigsuspend64(&not_SIGRTMIN));
236 ASSERT_EQ(EINTR, errno);
237 // ...and check that we now receive our pending SIGRTMIN.
238 ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700239}
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700240
Elliott Hughes3e235912018-02-01 14:21:51 -0800241template <typename SigActionT, typename SigSetT>
242static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*),
243 int (sigaddset_fn)(SigSetT*, int),
244 int sig) {
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700245 // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm,
246 // arm64, x86, and x86-64. The version of glibc we're using also doesn't
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800247 // define SA_RESTORER, but luckily it's the same value everywhere.
Elliott Hughesaa13e832014-09-04 15:43:10 -0700248 static const unsigned sa_restorer = 0x4000000;
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700249
Elliott Hughes3e235912018-02-01 14:21:51 -0800250 // See what's currently set for this signal.
251 SigActionT original_sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700252 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &original_sa));
253 ASSERT_TRUE(original_sa.sa_handler == nullptr);
254 ASSERT_TRUE(original_sa.sa_sigaction == nullptr);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700255 ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100256#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100257 ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100258#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700259
260 // Set a traditional sa_handler signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800261 auto no_op_signal_handler = [](int) {};
262 SigActionT sa = {};
263 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700264 sa.sa_flags = SA_ONSTACK;
Elliott Hughes3e235912018-02-01 14:21:51 -0800265 sa.sa_handler = no_op_signal_handler;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700266 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700267
268 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800269 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700270 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800271 ASSERT_TRUE(sa.sa_handler == no_op_signal_handler);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700272 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700273 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100274#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100275 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100276#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700277
278 // Set a new-style sa_sigaction signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800279 auto no_op_sigaction = [](int, siginfo_t*, void*) {};
280 sa = {};
281 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700282 sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
Elliott Hughes3e235912018-02-01 14:21:51 -0800283 sa.sa_sigaction = no_op_sigaction;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700285
286 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800287 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700288 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800289 ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700290 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700291 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100292#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100293 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100294#endif
Elliott Hughes11952072013-10-24 15:15:14 -0700295
296 // Put everything back how it was.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700297 ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr));
Elliott Hughes3e235912018-02-01 14:21:51 -0800298}
299
300TEST(signal, sigaction) {
301 TestSigAction(sigaction, sigaddset, SIGALRM);
302}
303
304TEST(signal, sigaction64_SIGRTMIN) {
305 TestSigAction(sigaction64, sigaddset64, SIGRTMIN);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700306}
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800307
Josh Gao6fcba932018-02-09 13:38:32 -0800308static void ClearSignalMask() {
309 uint64_t sigset = 0;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700310 SignalSetAdd(&sigset, __SIGRTMIN);
311 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
312 abort();
313 }
314}
315
316static void FillSignalMask() {
317 uint64_t sigset = ~0ULL;
318 for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) {
319 SignalSetDel(&sigset, signo);
320 }
Josh Gao6fcba932018-02-09 13:38:32 -0800321 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
322 abort();
323 }
324}
325
326static uint64_t GetSignalMask() {
327 uint64_t sigset;
328 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, nullptr, &sigset, sizeof(sigset)) != 0) {
329 abort();
330 }
331 return sigset;
332}
333
Josh Gaobaf20fc2018-10-08 17:28:07 -0700334static void TestSignalMaskFiltered(uint64_t sigset) {
335#if defined(__BIONIC__)
336 for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) {
Josh Gao6fcba932018-02-09 13:38:32 -0800337 bool signal_blocked = sigset & (1ULL << (signo - 1));
Josh Gaobaf20fc2018-10-08 17:28:07 -0700338 if (signo == __SIGRTMIN) {
339 // TIMER_SIGNAL must be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800340 EXPECT_EQ(true, signal_blocked) << "signal " << signo;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700341 } else {
342 // The other reserved signals must not be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800343 EXPECT_EQ(false, signal_blocked) << "signal " << signo;
Josh Gao6fcba932018-02-09 13:38:32 -0800344 }
345 }
Josh Gaobaf20fc2018-10-08 17:28:07 -0700346#else
347 UNUSED(sigset);
348#endif
Josh Gao6fcba932018-02-09 13:38:32 -0800349}
350
Josh Gaobaf20fc2018-10-08 17:28:07 -0700351static void TestSignalMaskFunction(std::function<void()> fn) {
Josh Gao6fcba932018-02-09 13:38:32 -0800352 ClearSignalMask();
353 fn();
Josh Gaobaf20fc2018-10-08 17:28:07 -0700354 TestSignalMaskFiltered(GetSignalMask());
Josh Gao6fcba932018-02-09 13:38:32 -0800355}
356
357TEST(signal, sigaction_filter) {
358 ClearSignalMask();
359 static uint64_t sigset;
360 struct sigaction sa = {};
361 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800362 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800363 sigfillset(&sa.sa_mask);
364 sigaction(SIGUSR1, &sa, nullptr);
365 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800366
367 // On LP32, struct sigaction::sa_mask is only 32-bits wide.
368 unsigned long expected_sigset = ~0UL;
369
370 // SIGKILL and SIGSTOP are always blocked.
371 expected_sigset &= ~(1UL << (SIGKILL - 1));
372 expected_sigset &= ~(1UL << (SIGSTOP - 1));
373
374 ASSERT_EQ(static_cast<uint64_t>(expected_sigset), sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800375}
376
377TEST(signal, sigaction64_filter) {
378 ClearSignalMask();
379 static uint64_t sigset;
380 struct sigaction64 sa = {};
381 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800382 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800383 sigfillset64(&sa.sa_mask);
384 sigaction64(SIGUSR1, &sa, nullptr);
385 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800386
387 uint64_t expected_sigset = ~0ULL;
388
389 // SIGKILL and SIGSTOP are always blocked.
390 expected_sigset &= ~(1ULL << (SIGKILL - 1));
391 expected_sigset &= ~(1ULL << (SIGSTOP - 1));
392
393 ASSERT_EQ(expected_sigset, sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800394}
395
396TEST(signal, sigprocmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700397 TestSignalMaskFunction([]() {
398 ClearSignalMask();
399 sigset_t sigset_libc;
400 sigfillset(&sigset_libc);
401 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr));
402 });
Josh Gao6fcba932018-02-09 13:38:32 -0800403}
404
405TEST(signal, sigprocmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700406 TestSignalMaskFunction([]() {
407 ClearSignalMask();
408 sigset64_t sigset_libc;
409 sigfillset64(&sigset_libc);
410 ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr));
411 });
Josh Gao6fcba932018-02-09 13:38:32 -0800412}
413
414TEST(signal, pthread_sigmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700415 TestSignalMaskFunction([]() {
416 ClearSignalMask();
417 sigset_t sigset_libc;
418 sigfillset(&sigset_libc);
419 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr));
420 });
Josh Gao6fcba932018-02-09 13:38:32 -0800421}
422
423TEST(signal, pthread_sigmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700424 TestSignalMaskFunction([]() {
425 ClearSignalMask();
426 sigset64_t sigset_libc;
427 sigfillset64(&sigset_libc);
428 ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr));
429 });
Josh Gao6fcba932018-02-09 13:38:32 -0800430}
431
432TEST(signal, sigprocmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700433 TestSignalMaskFunction([]() {
434 ClearSignalMask();
435 sigset_t sigset_libc;
436 sigfillset(&sigset_libc);
437 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr));
438 });
Josh Gao6fcba932018-02-09 13:38:32 -0800439}
440
441TEST(signal, sigprocmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700442 TestSignalMaskFunction([]() {
443 ClearSignalMask();
444 sigset64_t sigset_libc;
445 sigfillset64(&sigset_libc);
446 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr));
447 });
Josh Gao6fcba932018-02-09 13:38:32 -0800448}
449
450TEST(signal, pthread_sigmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700451 TestSignalMaskFunction([]() {
452 ClearSignalMask();
453 sigset_t sigset_libc;
454 sigfillset(&sigset_libc);
455 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr));
456 });
Josh Gao6fcba932018-02-09 13:38:32 -0800457}
458
459TEST(signal, pthread_sigmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700460 TestSignalMaskFunction([]() {
461 ClearSignalMask();
462 sigset64_t sigset_libc;
463 sigfillset64(&sigset_libc);
464 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &sigset_libc, nullptr));
465 });
466}
467
468TEST(signal, sigprocmask_unblock_filter) {
469 TestSignalMaskFunction([]() {
470 FillSignalMask();
471 sigset_t sigset_libc;
472 sigfillset(&sigset_libc);
473 ASSERT_EQ(0, sigprocmask(SIG_UNBLOCK, &sigset_libc, nullptr));
474 });
475}
476
477TEST(signal, sigprocmask64_unblock_filter) {
478 TestSignalMaskFunction([]() {
479 FillSignalMask();
480 sigset64_t sigset_libc;
481 sigfillset64(&sigset_libc);
482 ASSERT_EQ(0, sigprocmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
483 });
484}
485
486TEST(signal, pthread_sigmask_unblock_filter) {
487 TestSignalMaskFunction([]() {
488 FillSignalMask();
489 sigset_t sigset_libc;
490 sigfillset(&sigset_libc);
491 ASSERT_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigset_libc, nullptr));
492 });
493}
494
495TEST(signal, pthread_sigmask64_unblock_filter) {
496 TestSignalMaskFunction([]() {
497 FillSignalMask();
498 sigset64_t sigset_libc;
499 sigfillset64(&sigset_libc);
500 ASSERT_EQ(0, pthread_sigmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
501 });
Josh Gao6fcba932018-02-09 13:38:32 -0800502}
503
504// glibc filters out signals via sigfillset, not the actual underlying functions.
505TEST(signal, sigset_filter) {
506#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700507 TestSignalMaskFunction([]() {
508 for (int i = 1; i <= 64; ++i) {
509 sigset(i, SIG_HOLD);
510 }
511 });
Josh Gao6fcba932018-02-09 13:38:32 -0800512#endif
513}
514
515TEST(signal, sighold_filter) {
516#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700517 TestSignalMaskFunction([]() {
518 for (int i = 1; i <= 64; ++i) {
519 sighold(i);
520 }
521 });
Josh Gao6fcba932018-02-09 13:38:32 -0800522#endif
523}
524
525#if defined(__BIONIC__)
526// Not exposed via headers, but the symbols are available if you declare them yourself.
527extern "C" int sigblock(int);
528extern "C" int sigsetmask(int);
529#endif
530
531TEST(signal, sigblock_filter) {
532#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700533 TestSignalMaskFunction([]() {
534 sigblock(~0U);
535 });
Josh Gao6fcba932018-02-09 13:38:32 -0800536#endif
537}
538
539TEST(signal, sigsetmask_filter) {
540#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700541 TestSignalMaskFunction([]() {
542 sigsetmask(~0U);
543 });
Josh Gao6fcba932018-02-09 13:38:32 -0800544#endif
545}
546
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800547TEST(signal, sys_signame) {
Elliott Hughes671e2362014-02-12 19:04:27 -0800548#if defined(__BIONIC__)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700549 ASSERT_TRUE(sys_signame[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800550 ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
551#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800552 GTEST_SKIP() << "glibc doesn't have sys_signame";
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800553#endif
554}
555
556TEST(signal, sys_siglist) {
Colin Cross7da20342021-07-28 11:18:11 -0700557#if !defined(MUSL)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700558 ASSERT_TRUE(sys_siglist[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800559 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
Colin Cross7da20342021-07-28 11:18:11 -0700560#else
561 GTEST_SKIP() << "musl doesn't have sys_siglist";
562#endif
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800563}
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700564
565TEST(signal, limits) {
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800566 // These come from the kernel.
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700567 ASSERT_EQ(32, __SIGRTMIN);
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800568 ASSERT_EQ(64, __SIGRTMAX);
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700569
570 // We reserve a non-zero number at the bottom for ourselves.
571 ASSERT_GT(SIGRTMIN, __SIGRTMIN);
572
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700573 // We don't currently reserve any at the top.
574 ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
575}
Yabin Cui63481602014-12-01 17:41:04 -0800576
577static int g_sigqueue_signal_handler_call_count = 0;
578
579static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) {
580 ASSERT_EQ(SIGALRM, signum);
581 ASSERT_EQ(SIGALRM, info->si_signo);
582 ASSERT_EQ(SI_QUEUE, info->si_code);
583 ASSERT_EQ(1, info->si_value.sival_int);
584 ++g_sigqueue_signal_handler_call_count;
585}
586
587TEST(signal, sigqueue) {
588 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700589 sigval sigval = {.sival_int = 1};
Yabin Cui63481602014-12-01 17:41:04 -0800590 errno = 0;
591 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
592 ASSERT_EQ(0, errno);
593 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
594}
595
Josh Gao726b63f2018-08-27 16:00:58 -0700596TEST(signal, pthread_sigqueue_self) {
Colin Cross7da20342021-07-28 11:18:11 -0700597#if !defined(MUSL)
Josh Gao726b63f2018-08-27 16:00:58 -0700598 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700599 sigval sigval = {.sival_int = 1};
Josh Gao726b63f2018-08-27 16:00:58 -0700600 errno = 0;
601 ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval));
602 ASSERT_EQ(0, errno);
603 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
Colin Cross7da20342021-07-28 11:18:11 -0700604#else
605 GTEST_SKIP() << "musl doesn't have pthread_sigqueue";
606#endif
Josh Gao726b63f2018-08-27 16:00:58 -0700607}
608
609TEST(signal, pthread_sigqueue_other) {
Colin Cross7da20342021-07-28 11:18:11 -0700610#if !defined(MUSL)
Josh Gao726b63f2018-08-27 16:00:58 -0700611 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700612 sigval sigval = {.sival_int = 1};
Josh Gao726b63f2018-08-27 16:00:58 -0700613
614 sigset_t mask;
615 sigfillset(&mask);
616 pthread_sigmask(SIG_SETMASK, &mask, nullptr);
617 pthread_t thread;
618 int rc = pthread_create(&thread, nullptr,
619 [](void*) -> void* {
620 sigset_t mask;
621 sigemptyset(&mask);
622 sigsuspend(&mask);
623 return nullptr;
624 },
625 nullptr);
626 ASSERT_EQ(0, rc);
627
628 errno = 0;
629 ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval));
630 ASSERT_EQ(0, errno);
631 pthread_join(thread, nullptr);
632 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
Colin Cross7da20342021-07-28 11:18:11 -0700633#else
634 GTEST_SKIP() << "musl doesn't have pthread_sigqueue";
635#endif
Josh Gao726b63f2018-08-27 16:00:58 -0700636}
637
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700638TEST(signal, sigwait_SIGALRM) {
639 SignalMaskRestorer smr;
640
641 // Block SIGALRM.
642 sigset_t just_SIGALRM;
643 sigemptyset(&just_SIGALRM);
644 sigaddset(&just_SIGALRM, SIGALRM);
645 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
646
647 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700648 sigval sigval = {.sival_int = 1};
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700649 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
650
651 // Get pending SIGALRM.
652 int sig;
653 ASSERT_EQ(0, sigwait(&just_SIGALRM, &sig));
654 ASSERT_EQ(SIGALRM, sig);
655}
656
657TEST(signal, sigwait64_SIGRTMIN) {
658 SignalMaskRestorer smr;
659
660 // Block SIGRTMIN.
661 sigset64_t just_SIGRTMIN;
662 sigemptyset64(&just_SIGRTMIN);
663 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
664 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
665
666 // Raise SIGRTMIN.
Colin Cross7da20342021-07-28 11:18:11 -0700667 sigval sigval = {.sival_int = 1};
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700668 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
669
670 // Get pending SIGRTMIN.
671 int sig;
672 ASSERT_EQ(0, sigwait64(&just_SIGRTMIN, &sig));
673 ASSERT_EQ(SIGRTMIN, sig);
674}
675
Yabin Cui63481602014-12-01 17:41:04 -0800676TEST(signal, sigwaitinfo) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800677 SignalMaskRestorer smr;
678
Yabin Cui63481602014-12-01 17:41:04 -0800679 // Block SIGALRM.
680 sigset_t just_SIGALRM;
681 sigemptyset(&just_SIGALRM);
682 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800683 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800684
685 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700686 sigval sigval = {.sival_int = 1};
Yabin Cui63481602014-12-01 17:41:04 -0800687 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
688
689 // Get pending SIGALRM.
690 siginfo_t info;
691 errno = 0;
692 ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info));
693 ASSERT_EQ(0, errno);
694 ASSERT_EQ(SIGALRM, info.si_signo);
695 ASSERT_EQ(1, info.si_value.sival_int);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800696}
Yabin Cui63481602014-12-01 17:41:04 -0800697
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800698TEST(signal, sigwaitinfo64_SIGRTMIN) {
699 SignalMaskRestorer smr;
700
701 // Block SIGRTMIN.
702 sigset64_t just_SIGRTMIN;
703 sigemptyset64(&just_SIGRTMIN);
704 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
705 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
706
707 // Raise SIGRTMIN.
Colin Cross7da20342021-07-28 11:18:11 -0700708 sigval sigval = {.sival_int = 1};
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800709 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
710
711 // Get pending SIGRTMIN.
712 siginfo_t info;
713 errno = 0;
714 ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info));
715 ASSERT_EQ(0, errno);
716 ASSERT_EQ(SIGRTMIN, info.si_signo);
717 ASSERT_EQ(1, info.si_value.sival_int);
Yabin Cui63481602014-12-01 17:41:04 -0800718}
719
720TEST(signal, sigtimedwait) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800721 SignalMaskRestorer smr;
722
Yabin Cui63481602014-12-01 17:41:04 -0800723 // Block SIGALRM.
724 sigset_t just_SIGALRM;
725 sigemptyset(&just_SIGALRM);
726 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800727 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800728
729 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700730 sigval sigval = { .sival_int = 1 };
Yabin Cui63481602014-12-01 17:41:04 -0800731 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
732
733 // Get pending SIGALRM.
734 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800735 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
Yabin Cui63481602014-12-01 17:41:04 -0800736 errno = 0;
737 ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout));
738 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800739}
Yabin Cui63481602014-12-01 17:41:04 -0800740
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800741TEST(signal, sigtimedwait64_SIGRTMIN) {
742 SignalMaskRestorer smr;
743
744 // Block SIGRTMIN.
745 sigset64_t just_SIGRTMIN;
746 sigemptyset64(&just_SIGRTMIN);
747 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
748 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
749
750 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700751 sigval sigval = { .sival_int = 1 };
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800752 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
753
754 // Get pending SIGALRM.
755 siginfo_t info;
756 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
757 errno = 0;
758 ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout));
759 ASSERT_EQ(0, errno);
Yabin Cui63481602014-12-01 17:41:04 -0800760}
761
Yabin Cui63481602014-12-01 17:41:04 -0800762TEST(signal, sigtimedwait_timeout) {
763 // Block SIGALRM.
764 sigset_t just_SIGALRM;
765 sigemptyset(&just_SIGALRM);
766 sigaddset(&just_SIGALRM, SIGALRM);
767 sigset_t original_set;
768 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
769
770 // Wait timeout.
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700771 auto t0 = std::chrono::steady_clock::now();
Yabin Cui63481602014-12-01 17:41:04 -0800772 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800773 timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
Yabin Cui63481602014-12-01 17:41:04 -0800774 errno = 0;
775 ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
776 ASSERT_EQ(EAGAIN, errno);
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700777 auto t1 = std::chrono::steady_clock::now();
778 ASSERT_GE(t1-t0, 1000000ns);
Yabin Cui63481602014-12-01 17:41:04 -0800779
Yi Kong32bc0fc2018-08-02 17:31:13 -0700780 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800781}
Josh Gao61cf3f32016-03-08 15:27:15 -0800782
783#if defined(__BIONIC__)
784TEST(signal, rt_tgsigqueueinfo) {
785 // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self.
786 // If this fails, your kernel needs commit 66dd34a to be backported.
787 static constexpr char error_msg[] =
788 "\nPlease ensure that the following kernel patch has been applied:\n"
789 "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n";
790 static siginfo received;
791
Elliott Hughes3e235912018-02-01 14:21:51 -0800792 struct sigaction handler = {};
Josh Gao61cf3f32016-03-08 15:27:15 -0800793 handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; };
794 handler.sa_flags = SA_SIGINFO;
795
796 ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr));
797
Josh Gaod7878522016-03-14 18:15:15 -0700798 siginfo sent;
799 memset(&sent, 0, sizeof(sent));
Josh Gao61cf3f32016-03-08 15:27:15 -0800800
801 sent.si_code = SI_TKILL;
802 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
803 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
804 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
805 << sent.si_code << ", received " << received.si_code
806 << error_msg;
807
808 sent.si_code = SI_USER;
809 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
810 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
811 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
812 << sent.si_code << ", received " << received.si_code
813 << error_msg;
814}
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800815#endif
Josh Gaoc244fcb2016-03-29 14:34:03 -0700816
Josh Gaoc244fcb2016-03-29 14:34:03 -0700817TEST(signal, sigset_size) {
Elliott Hughes370e9632021-01-21 14:40:49 -0800818 // The setjmp implementations assume that sigset_t can fit in a
819 // long. This is true because ARM and x86 have broken rt signal support,
820 // and AArch64 and x86_64 both have a SIGRTMAX defined as 64.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800821#if defined(__BIONIC__)
Josh Gaoc244fcb2016-03-29 14:34:03 -0700822 static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long");
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800823#endif
824 static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals");
Josh Gaoc244fcb2016-03-29 14:34:03 -0700825}
826
Greg Hackmann5375bf62016-02-29 12:35:33 -0800827TEST(signal, sigignore_EINVAL) {
828 errno = 0;
829 ASSERT_EQ(-1, sigignore(99999));
830 ASSERT_EQ(EINVAL, errno);
831}
832
833TEST(signal, sigignore) {
834 errno = 0;
835 EXPECT_EQ(-1, sigignore(SIGKILL));
836 EXPECT_EQ(errno, EINVAL);
837
838 errno = 0;
839 EXPECT_EQ(-1, sigignore(SIGSTOP));
840 EXPECT_EQ(errno, EINVAL);
841
842 ScopedSignalHandler sigalrm{SIGALRM};
843 ASSERT_EQ(0, sigignore(SIGALRM));
844
845 struct sigaction sa;
846 ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa));
847 EXPECT_EQ(SIG_IGN, sa.sa_handler);
848}
849
850TEST(signal, sighold_EINVAL) {
851 errno = 0;
852 ASSERT_EQ(-1, sighold(99999));
853 ASSERT_EQ(EINVAL, errno);
854}
855
856TEST(signal, sigpause_EINVAL) {
857 errno = 0;
858 ASSERT_EQ(-1, sigpause(99999));
859 ASSERT_EQ(EINVAL, errno);
860}
861
862TEST(signal, sigrelse_EINVAL) {
863 errno = 0;
864 ASSERT_EQ(-1, sigpause(99999));
865 ASSERT_EQ(EINVAL, errno);
866}
867
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800868static void TestSigholdSigpauseSigrelse(int sig) {
869 static int signal_handler_call_count = 0;
870 ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }};
871 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800872 sigset_t set;
873
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800874 // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ...
875 ASSERT_EQ(0, sighold(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700876 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800877 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800878
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800879 // ... preventing our SIGALRM/SIGRTMIN handler from running ...
880 raise(sig);
881 ASSERT_EQ(0, signal_handler_call_count);
882 // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it.
883 ASSERT_EQ(-1, sigpause(sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800884 ASSERT_EQ(EINTR, errno);
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800885 ASSERT_EQ(1, signal_handler_call_count);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800886
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800887 if (sig >= SIGRTMIN && sizeof(void*) == 8) {
888 // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700889 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800890 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800891
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800892 // Whereas sigrelse(SIGALRM/SIGRTMIN) should.
893 ASSERT_EQ(0, sigrelse(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700894 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800895 EXPECT_FALSE(sigismember(&set, sig));
896 } else {
897 // sigismember won't work for SIGRTMIN on LP32.
898 }
899}
900
901TEST(signal, sighold_sigpause_sigrelse) {
902 TestSigholdSigpauseSigrelse(SIGALRM);
903}
904
905TEST(signal, sighold_sigpause_sigrelse_RT) {
906 TestSigholdSigpauseSigrelse(SIGRTMIN);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800907}
908
909TEST(signal, sigset_EINVAL) {
910 errno = 0;
911 ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL));
912 ASSERT_EQ(EINVAL, errno);
913}
914
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800915TEST(signal, sigset_RT) {
916 static int signal_handler_call_count = 0;
917 auto signal_handler = [](int) { signal_handler_call_count++; };
918 ScopedSignalHandler ssh{SIGRTMIN, signal_handler};
919 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800920
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800921 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD));
922#if defined(__LP64__)
Greg Hackmann5375bf62016-02-29 12:35:33 -0800923 sigset_t set;
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800924 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
925 ASSERT_TRUE(sigismember(&set, SIGRTMIN));
926#endif
927
928 ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler));
929 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler));
930 ASSERT_EQ(0, signal_handler_call_count);
931 raise(SIGRTMIN);
932 ASSERT_EQ(1, signal_handler_call_count);
933}
934
935TEST(signal, sigset) {
936 static int signal_handler_call_count = 0;
937 auto signal_handler = [](int) { signal_handler_call_count++; };
938 ScopedSignalHandler ssh{SIGALRM, signal_handler};
939 SignalMaskRestorer mask_restorer;
940
941 ASSERT_EQ(0, signal_handler_call_count);
942 raise(SIGALRM);
943 ASSERT_EQ(1, signal_handler_call_count);
944
945 // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD.
946 sigset_t set;
947 sigemptyset(&set);
948 sigaddset(&set, SIGALRM);
949 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr));
950
951 sigemptyset(&set);
952 ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800953 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
954 EXPECT_FALSE(sigismember(&set, SIGALRM));
955
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800956 ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800957 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
958 EXPECT_FALSE(sigismember(&set, SIGALRM));
959
960 ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL));
961 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
962 EXPECT_FALSE(sigismember(&set, SIGALRM));
963
964 ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD));
965 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
966 EXPECT_TRUE(sigismember(&set, SIGALRM));
967}
Elliott Hughes7532b322017-07-11 15:00:17 -0700968
969TEST(signal, killpg_EINVAL) {
970 // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0
971 // and passes 0 through to kill(2).
972 errno = 0;
973 ASSERT_EQ(-1, killpg(-1, SIGKILL));
974 ASSERT_EQ(EINVAL, errno);
975}