blob: 5bda8b329690e823e72ef64d157e70c719c48c9e [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>
Colin Cross4c5595c2021-08-16 15:51:59 -070019#include <sys/cdefs.h>
Josh Gao61cf3f32016-03-08 15:27:15 -080020#include <sys/syscall.h>
21#include <sys/types.h>
22#include <unistd.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080023
Elliott Hughesca3f8e42019-10-28 15:59:38 -070024#include <chrono>
Elliott Hughes5905d6f2018-01-30 15:09:51 -080025#include <thread>
26
Josh Gaobaf20fc2018-10-08 17:28:07 -070027#include <android-base/macros.h>
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010028#include <android-base/threads.h>
29
Elliott Hughesafe58ad2014-09-04 13:54:42 -070030#include <gtest/gtest.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080031
Elliott Hughes71ba5892018-02-07 12:44:45 -080032#include "SignalUtils.h"
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010033#include "utils.h"
Christopher Ferris13613132013-10-28 15:24:04 -070034
Elliott Hughesca3f8e42019-10-28 15:59:38 -070035using namespace std::chrono_literals;
36
Elliott Hughes5905d6f2018-01-30 15:09:51 -080037static int SIGNAL_MIN() {
Elliott Hughes40d105c2013-10-16 12:53:58 -070038 return 1; // Signals start at 1 (SIGHUP), not 0.
39}
40
Elliott Hughes5905d6f2018-01-30 15:09:51 -080041template <typename SigSetT>
42static int SIGNAL_MAX(SigSetT* set) {
43 return sizeof(*set) * 8;
Elliott Hughes40d105c2013-10-16 12:53:58 -070044}
45
Elliott Hughes5905d6f2018-01-30 15:09:51 -080046template <typename SigSetT>
47static void TestSigSet1(int (fn)(SigSetT*)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070048 // nullptr sigset_t*/sigset64_t*.
49 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080050 errno = 0;
51 ASSERT_EQ(-1, fn(set_ptr));
52 ASSERT_EQ(EINVAL, errno);
53
Yi Kong32bc0fc2018-08-02 17:31:13 -070054 // Non-nullptr.
Elliott Hughes5905d6f2018-01-30 15:09:51 -080055 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080056 errno = 0;
57 ASSERT_EQ(0, fn(&set));
58 ASSERT_EQ(0, errno);
59}
60
Elliott Hughes5905d6f2018-01-30 15:09:51 -080061template <typename SigSetT>
62static void TestSigSet2(int (fn)(SigSetT*, int)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070063 // nullptr sigset_t*/sigset64_t*.
64 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080065 errno = 0;
66 ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
67 ASSERT_EQ(EINVAL, errno);
68
Elliott Hughes5905d6f2018-01-30 15:09:51 -080069 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080070
Elliott Hughesda73f652012-11-30 16:40:55 -080071 // Bad signal number: too small.
72 errno = 0;
73 ASSERT_EQ(-1, fn(&set, 0));
74 ASSERT_EQ(EINVAL, errno);
75
76 // Bad signal number: too high.
77 errno = 0;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080078 ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1));
Elliott Hughesda73f652012-11-30 16:40:55 -080079 ASSERT_EQ(EINVAL, errno);
80
81 // Good signal numbers, low and high ends of range.
82 errno = 0;
Elliott Hughes40d105c2013-10-16 12:53:58 -070083 ASSERT_EQ(0, fn(&set, SIGNAL_MIN()));
Elliott Hughesda73f652012-11-30 16:40:55 -080084 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080085 ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set)));
Elliott Hughesda73f652012-11-30 16:40:55 -080086 ASSERT_EQ(0, errno);
87}
88
Elliott Hughesda73f652012-11-30 16:40:55 -080089TEST(signal, sigaddset_invalid) {
90 TestSigSet2(sigaddset);
91}
92
Elliott Hughes5905d6f2018-01-30 15:09:51 -080093TEST(signal, sigaddset64_invalid) {
94#if defined(__BIONIC__)
95 TestSigSet2(sigaddset64);
96#endif
97}
98
Elliott Hughesda73f652012-11-30 16:40:55 -080099TEST(signal, sigdelset_invalid) {
100 TestSigSet2(sigdelset);
101}
102
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800103TEST(signal, sigdelset64_invalid) {
104#if defined(__BIONIC__)
105 TestSigSet2(sigdelset64);
106#endif
107}
108
Elliott Hughesda73f652012-11-30 16:40:55 -0800109TEST(signal, sigemptyset_invalid) {
110 TestSigSet1(sigemptyset);
111}
112
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800113TEST(signal, sigemptyset64_invalid) {
114#if defined(__BIONIC__)
115 TestSigSet1(sigemptyset64);
116#endif
117}
118
Elliott Hughesda73f652012-11-30 16:40:55 -0800119TEST(signal, sigfillset_invalid) {
120 TestSigSet1(sigfillset);
121}
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800122
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800123TEST(signal, sigfillset64_invalid) {
124#if defined(__BIONIC__)
125 TestSigSet1(sigfillset64);
126#endif
127}
128
129TEST(signal, sigismember_invalid) {
130 TestSigSet2(sigismember);
131}
132
133TEST(signal, sigismember64_invalid) {
134#if defined(__BIONIC__)
135 TestSigSet2(sigismember64);
136#endif
137}
138
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800139TEST(signal, raise_invalid) {
140 errno = 0;
141 ASSERT_EQ(-1, raise(-1));
142 ASSERT_EQ(EINVAL, errno);
143}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800144
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800145static void raise_in_signal_handler_helper(int signal_number) {
146 ASSERT_EQ(SIGALRM, signal_number);
147 static int count = 0;
148 if (++count == 1) {
149 raise(SIGALRM);
150 }
151}
152
153TEST(signal, raise_in_signal_handler) {
154 ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
155 raise(SIGALRM);
156}
157
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800158static int g_sigsuspend_signal_handler_call_count = 0;
159
Elliott Hughes40d105c2013-10-16 12:53:58 -0700160TEST(signal, sigsuspend_sigpending) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800161 SignalMaskRestorer smr;
162
Elliott Hughes1f5af922013-10-15 18:01:56 -0700163 // Block SIGALRM.
164 sigset_t just_SIGALRM;
165 sigemptyset(&just_SIGALRM);
166 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800167 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Elliott Hughes1f5af922013-10-15 18:01:56 -0700168
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800169 ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; });
Elliott Hughes11952072013-10-24 15:15:14 -0700170
Elliott Hughes40d105c2013-10-16 12:53:58 -0700171 // There should be no pending signals.
172 sigset_t pending;
173 sigemptyset(&pending);
174 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800175 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700176 EXPECT_FALSE(sigismember(&pending, i)) << i;
177 }
178
Elliott Hughes1f5af922013-10-15 18:01:56 -0700179 // Raise SIGALRM and check our signal handler wasn't called.
180 raise(SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800181 ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700182
Elliott Hughes40d105c2013-10-16 12:53:58 -0700183 // We should now have a pending SIGALRM but nothing else.
184 sigemptyset(&pending);
185 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800186 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700187 EXPECT_EQ((i == SIGALRM), sigismember(&pending, i));
188 }
189
Elliott Hughes1f5af922013-10-15 18:01:56 -0700190 // Use sigsuspend to block everything except SIGALRM...
191 sigset_t not_SIGALRM;
192 sigfillset(&not_SIGALRM);
193 sigdelset(&not_SIGALRM, SIGALRM);
194 ASSERT_EQ(-1, sigsuspend(&not_SIGALRM));
195 ASSERT_EQ(EINTR, errno);
196 // ...and check that we now receive our pending SIGALRM.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800197 ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count);
198}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700199
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800200static int g_sigsuspend64_signal_handler_call_count = 0;
201
202TEST(signal, sigsuspend64_sigpending64) {
203 SignalMaskRestorer smr;
204
205 // Block SIGRTMIN.
206 sigset64_t just_SIGRTMIN;
207 sigemptyset64(&just_SIGRTMIN);
208 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
209 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
210
211 ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; });
212
213 // There should be no pending signals.
214 sigset64_t pending;
215 sigemptyset64(&pending);
216 ASSERT_EQ(0, sigpending64(&pending));
217 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
218 EXPECT_FALSE(sigismember64(&pending, i)) << i;
219 }
220
221 // Raise SIGRTMIN and check our signal handler wasn't called.
222 raise(SIGRTMIN);
223 ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count);
224
225 // We should now have a pending SIGRTMIN but nothing else.
226 sigemptyset64(&pending);
227 ASSERT_EQ(0, sigpending64(&pending));
228 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
229 EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i));
230 }
231
232 // Use sigsuspend64 to block everything except SIGRTMIN...
233 sigset64_t not_SIGRTMIN;
234 sigfillset64(&not_SIGRTMIN);
235 sigdelset64(&not_SIGRTMIN, SIGRTMIN);
236 ASSERT_EQ(-1, sigsuspend64(&not_SIGRTMIN));
237 ASSERT_EQ(EINTR, errno);
238 // ...and check that we now receive our pending SIGRTMIN.
239 ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700240}
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700241
Elliott Hughes3e235912018-02-01 14:21:51 -0800242template <typename SigActionT, typename SigSetT>
243static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*),
244 int (sigaddset_fn)(SigSetT*, int),
245 int sig) {
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700246 // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm,
247 // arm64, x86, and x86-64. The version of glibc we're using also doesn't
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800248 // define SA_RESTORER, but luckily it's the same value everywhere.
Elliott Hughesaa13e832014-09-04 15:43:10 -0700249 static const unsigned sa_restorer = 0x4000000;
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700250
Elliott Hughes3e235912018-02-01 14:21:51 -0800251 // See what's currently set for this signal.
252 SigActionT original_sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700253 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &original_sa));
254 ASSERT_TRUE(original_sa.sa_handler == nullptr);
255 ASSERT_TRUE(original_sa.sa_sigaction == nullptr);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700256 ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100257#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100258 ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100259#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700260
261 // Set a traditional sa_handler signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800262 auto no_op_signal_handler = [](int) {};
263 SigActionT sa = {};
264 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700265 sa.sa_flags = SA_ONSTACK;
Elliott Hughes3e235912018-02-01 14:21:51 -0800266 sa.sa_handler = no_op_signal_handler;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700267 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700268
269 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800270 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700271 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800272 ASSERT_TRUE(sa.sa_handler == no_op_signal_handler);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700273 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700274 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100275#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100276 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100277#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700278
279 // Set a new-style sa_sigaction signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800280 auto no_op_sigaction = [](int, siginfo_t*, void*) {};
281 sa = {};
282 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700283 sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
Elliott Hughes3e235912018-02-01 14:21:51 -0800284 sa.sa_sigaction = no_op_sigaction;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700285 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700286
287 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800288 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700289 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800290 ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700291 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700292 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100293#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100294 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100295#endif
Elliott Hughes11952072013-10-24 15:15:14 -0700296
297 // Put everything back how it was.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700298 ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr));
Elliott Hughes3e235912018-02-01 14:21:51 -0800299}
300
301TEST(signal, sigaction) {
302 TestSigAction(sigaction, sigaddset, SIGALRM);
303}
304
305TEST(signal, sigaction64_SIGRTMIN) {
306 TestSigAction(sigaction64, sigaddset64, SIGRTMIN);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700307}
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800308
Josh Gao6fcba932018-02-09 13:38:32 -0800309static void ClearSignalMask() {
310 uint64_t sigset = 0;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700311 SignalSetAdd(&sigset, __SIGRTMIN);
312 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
313 abort();
314 }
315}
316
317static void FillSignalMask() {
318 uint64_t sigset = ~0ULL;
319 for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) {
320 SignalSetDel(&sigset, signo);
321 }
Josh Gao6fcba932018-02-09 13:38:32 -0800322 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
323 abort();
324 }
325}
326
327static uint64_t GetSignalMask() {
328 uint64_t sigset;
329 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, nullptr, &sigset, sizeof(sigset)) != 0) {
330 abort();
331 }
332 return sigset;
333}
334
Josh Gaobaf20fc2018-10-08 17:28:07 -0700335static void TestSignalMaskFiltered(uint64_t sigset) {
336#if defined(__BIONIC__)
337 for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) {
Josh Gao6fcba932018-02-09 13:38:32 -0800338 bool signal_blocked = sigset & (1ULL << (signo - 1));
Josh Gaobaf20fc2018-10-08 17:28:07 -0700339 if (signo == __SIGRTMIN) {
340 // TIMER_SIGNAL must be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800341 EXPECT_EQ(true, signal_blocked) << "signal " << signo;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700342 } else {
343 // The other reserved signals must not be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800344 EXPECT_EQ(false, signal_blocked) << "signal " << signo;
Josh Gao6fcba932018-02-09 13:38:32 -0800345 }
346 }
Josh Gaobaf20fc2018-10-08 17:28:07 -0700347#else
348 UNUSED(sigset);
349#endif
Josh Gao6fcba932018-02-09 13:38:32 -0800350}
351
Josh Gaobaf20fc2018-10-08 17:28:07 -0700352static void TestSignalMaskFunction(std::function<void()> fn) {
Josh Gao6fcba932018-02-09 13:38:32 -0800353 ClearSignalMask();
354 fn();
Josh Gaobaf20fc2018-10-08 17:28:07 -0700355 TestSignalMaskFiltered(GetSignalMask());
Josh Gao6fcba932018-02-09 13:38:32 -0800356}
357
358TEST(signal, sigaction_filter) {
359 ClearSignalMask();
360 static uint64_t sigset;
361 struct sigaction sa = {};
362 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800363 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800364 sigfillset(&sa.sa_mask);
365 sigaction(SIGUSR1, &sa, nullptr);
366 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800367
368 // On LP32, struct sigaction::sa_mask is only 32-bits wide.
369 unsigned long expected_sigset = ~0UL;
370
371 // SIGKILL and SIGSTOP are always blocked.
372 expected_sigset &= ~(1UL << (SIGKILL - 1));
373 expected_sigset &= ~(1UL << (SIGSTOP - 1));
374
375 ASSERT_EQ(static_cast<uint64_t>(expected_sigset), sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800376}
377
378TEST(signal, sigaction64_filter) {
379 ClearSignalMask();
380 static uint64_t sigset;
381 struct sigaction64 sa = {};
382 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800383 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800384 sigfillset64(&sa.sa_mask);
385 sigaction64(SIGUSR1, &sa, nullptr);
386 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800387
388 uint64_t expected_sigset = ~0ULL;
389
390 // SIGKILL and SIGSTOP are always blocked.
391 expected_sigset &= ~(1ULL << (SIGKILL - 1));
392 expected_sigset &= ~(1ULL << (SIGSTOP - 1));
393
394 ASSERT_EQ(expected_sigset, sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800395}
396
397TEST(signal, sigprocmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700398 TestSignalMaskFunction([]() {
399 ClearSignalMask();
400 sigset_t sigset_libc;
401 sigfillset(&sigset_libc);
402 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr));
403 });
Josh Gao6fcba932018-02-09 13:38:32 -0800404}
405
406TEST(signal, sigprocmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700407 TestSignalMaskFunction([]() {
408 ClearSignalMask();
409 sigset64_t sigset_libc;
410 sigfillset64(&sigset_libc);
411 ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr));
412 });
Josh Gao6fcba932018-02-09 13:38:32 -0800413}
414
415TEST(signal, pthread_sigmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700416 TestSignalMaskFunction([]() {
417 ClearSignalMask();
418 sigset_t sigset_libc;
419 sigfillset(&sigset_libc);
420 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr));
421 });
Josh Gao6fcba932018-02-09 13:38:32 -0800422}
423
424TEST(signal, pthread_sigmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700425 TestSignalMaskFunction([]() {
426 ClearSignalMask();
427 sigset64_t sigset_libc;
428 sigfillset64(&sigset_libc);
429 ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr));
430 });
Josh Gao6fcba932018-02-09 13:38:32 -0800431}
432
433TEST(signal, sigprocmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700434 TestSignalMaskFunction([]() {
435 ClearSignalMask();
436 sigset_t sigset_libc;
437 sigfillset(&sigset_libc);
438 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr));
439 });
Josh Gao6fcba932018-02-09 13:38:32 -0800440}
441
442TEST(signal, sigprocmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700443 TestSignalMaskFunction([]() {
444 ClearSignalMask();
445 sigset64_t sigset_libc;
446 sigfillset64(&sigset_libc);
447 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr));
448 });
Josh Gao6fcba932018-02-09 13:38:32 -0800449}
450
451TEST(signal, pthread_sigmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700452 TestSignalMaskFunction([]() {
453 ClearSignalMask();
454 sigset_t sigset_libc;
455 sigfillset(&sigset_libc);
456 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr));
457 });
Josh Gao6fcba932018-02-09 13:38:32 -0800458}
459
460TEST(signal, pthread_sigmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700461 TestSignalMaskFunction([]() {
462 ClearSignalMask();
463 sigset64_t sigset_libc;
464 sigfillset64(&sigset_libc);
465 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &sigset_libc, nullptr));
466 });
467}
468
469TEST(signal, sigprocmask_unblock_filter) {
470 TestSignalMaskFunction([]() {
471 FillSignalMask();
472 sigset_t sigset_libc;
473 sigfillset(&sigset_libc);
474 ASSERT_EQ(0, sigprocmask(SIG_UNBLOCK, &sigset_libc, nullptr));
475 });
476}
477
478TEST(signal, sigprocmask64_unblock_filter) {
479 TestSignalMaskFunction([]() {
480 FillSignalMask();
481 sigset64_t sigset_libc;
482 sigfillset64(&sigset_libc);
483 ASSERT_EQ(0, sigprocmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
484 });
485}
486
487TEST(signal, pthread_sigmask_unblock_filter) {
488 TestSignalMaskFunction([]() {
489 FillSignalMask();
490 sigset_t sigset_libc;
491 sigfillset(&sigset_libc);
492 ASSERT_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigset_libc, nullptr));
493 });
494}
495
496TEST(signal, pthread_sigmask64_unblock_filter) {
497 TestSignalMaskFunction([]() {
498 FillSignalMask();
499 sigset64_t sigset_libc;
500 sigfillset64(&sigset_libc);
501 ASSERT_EQ(0, pthread_sigmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
502 });
Josh Gao6fcba932018-02-09 13:38:32 -0800503}
504
505// glibc filters out signals via sigfillset, not the actual underlying functions.
506TEST(signal, sigset_filter) {
507#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700508 TestSignalMaskFunction([]() {
509 for (int i = 1; i <= 64; ++i) {
510 sigset(i, SIG_HOLD);
511 }
512 });
Josh Gao6fcba932018-02-09 13:38:32 -0800513#endif
514}
515
516TEST(signal, sighold_filter) {
517#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700518 TestSignalMaskFunction([]() {
519 for (int i = 1; i <= 64; ++i) {
520 sighold(i);
521 }
522 });
Josh Gao6fcba932018-02-09 13:38:32 -0800523#endif
524}
525
526#if defined(__BIONIC__)
527// Not exposed via headers, but the symbols are available if you declare them yourself.
528extern "C" int sigblock(int);
529extern "C" int sigsetmask(int);
530#endif
531
532TEST(signal, sigblock_filter) {
533#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700534 TestSignalMaskFunction([]() {
535 sigblock(~0U);
536 });
Josh Gao6fcba932018-02-09 13:38:32 -0800537#endif
538}
539
540TEST(signal, sigsetmask_filter) {
541#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700542 TestSignalMaskFunction([]() {
543 sigsetmask(~0U);
544 });
Josh Gao6fcba932018-02-09 13:38:32 -0800545#endif
546}
547
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800548TEST(signal, sys_signame) {
Elliott Hughes671e2362014-02-12 19:04:27 -0800549#if defined(__BIONIC__)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700550 ASSERT_TRUE(sys_signame[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800551 ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
552#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800553 GTEST_SKIP() << "glibc doesn't have sys_signame";
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800554#endif
555}
556
557TEST(signal, sys_siglist) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700558#if !defined(ANDROID_HOST_MUSL)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700559 ASSERT_TRUE(sys_siglist[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800560 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
Colin Cross7da20342021-07-28 11:18:11 -0700561#else
562 GTEST_SKIP() << "musl doesn't have sys_siglist";
563#endif
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800564}
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700565
566TEST(signal, limits) {
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800567 // These come from the kernel.
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700568 ASSERT_EQ(32, __SIGRTMIN);
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800569 ASSERT_EQ(64, __SIGRTMAX);
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700570
571 // We reserve a non-zero number at the bottom for ourselves.
572 ASSERT_GT(SIGRTMIN, __SIGRTMIN);
573
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700574 // We don't currently reserve any at the top.
575 ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
576}
Yabin Cui63481602014-12-01 17:41:04 -0800577
578static int g_sigqueue_signal_handler_call_count = 0;
579
580static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) {
581 ASSERT_EQ(SIGALRM, signum);
582 ASSERT_EQ(SIGALRM, info->si_signo);
583 ASSERT_EQ(SI_QUEUE, info->si_code);
584 ASSERT_EQ(1, info->si_value.sival_int);
585 ++g_sigqueue_signal_handler_call_count;
586}
587
588TEST(signal, sigqueue) {
589 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700590 sigval sigval = {.sival_int = 1};
Yabin Cui63481602014-12-01 17:41:04 -0800591 errno = 0;
592 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
593 ASSERT_EQ(0, errno);
594 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
595}
596
Josh Gao726b63f2018-08-27 16:00:58 -0700597TEST(signal, pthread_sigqueue_self) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700598#if !defined(ANDROID_HOST_MUSL)
Josh Gao726b63f2018-08-27 16:00:58 -0700599 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700600 sigval sigval = {.sival_int = 1};
Josh Gao726b63f2018-08-27 16:00:58 -0700601 errno = 0;
602 ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval));
603 ASSERT_EQ(0, errno);
604 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
Colin Cross7da20342021-07-28 11:18:11 -0700605#else
606 GTEST_SKIP() << "musl doesn't have pthread_sigqueue";
607#endif
Josh Gao726b63f2018-08-27 16:00:58 -0700608}
609
610TEST(signal, pthread_sigqueue_other) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700611#if !defined(ANDROID_HOST_MUSL)
Josh Gao726b63f2018-08-27 16:00:58 -0700612 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
Colin Cross7da20342021-07-28 11:18:11 -0700613 sigval sigval = {.sival_int = 1};
Josh Gao726b63f2018-08-27 16:00:58 -0700614
615 sigset_t mask;
616 sigfillset(&mask);
617 pthread_sigmask(SIG_SETMASK, &mask, nullptr);
618 pthread_t thread;
619 int rc = pthread_create(&thread, nullptr,
620 [](void*) -> void* {
621 sigset_t mask;
622 sigemptyset(&mask);
623 sigsuspend(&mask);
624 return nullptr;
625 },
626 nullptr);
627 ASSERT_EQ(0, rc);
628
629 errno = 0;
630 ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval));
631 ASSERT_EQ(0, errno);
632 pthread_join(thread, nullptr);
633 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
Colin Cross7da20342021-07-28 11:18:11 -0700634#else
635 GTEST_SKIP() << "musl doesn't have pthread_sigqueue";
636#endif
Josh Gao726b63f2018-08-27 16:00:58 -0700637}
638
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700639TEST(signal, sigwait_SIGALRM) {
640 SignalMaskRestorer smr;
641
642 // Block SIGALRM.
643 sigset_t just_SIGALRM;
644 sigemptyset(&just_SIGALRM);
645 sigaddset(&just_SIGALRM, SIGALRM);
646 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
647
648 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700649 sigval sigval = {.sival_int = 1};
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700650 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
651
652 // Get pending SIGALRM.
653 int sig;
654 ASSERT_EQ(0, sigwait(&just_SIGALRM, &sig));
655 ASSERT_EQ(SIGALRM, sig);
656}
657
658TEST(signal, sigwait64_SIGRTMIN) {
659 SignalMaskRestorer smr;
660
661 // Block SIGRTMIN.
662 sigset64_t just_SIGRTMIN;
663 sigemptyset64(&just_SIGRTMIN);
664 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
665 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
666
667 // Raise SIGRTMIN.
Colin Cross7da20342021-07-28 11:18:11 -0700668 sigval sigval = {.sival_int = 1};
Elliott Hughes50fca4d2020-03-20 16:25:54 -0700669 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
670
671 // Get pending SIGRTMIN.
672 int sig;
673 ASSERT_EQ(0, sigwait64(&just_SIGRTMIN, &sig));
674 ASSERT_EQ(SIGRTMIN, sig);
675}
676
Yabin Cui63481602014-12-01 17:41:04 -0800677TEST(signal, sigwaitinfo) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800678 SignalMaskRestorer smr;
679
Yabin Cui63481602014-12-01 17:41:04 -0800680 // Block SIGALRM.
681 sigset_t just_SIGALRM;
682 sigemptyset(&just_SIGALRM);
683 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800684 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800685
686 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700687 sigval sigval = {.sival_int = 1};
Yabin Cui63481602014-12-01 17:41:04 -0800688 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
689
690 // Get pending SIGALRM.
691 siginfo_t info;
692 errno = 0;
693 ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info));
694 ASSERT_EQ(0, errno);
695 ASSERT_EQ(SIGALRM, info.si_signo);
696 ASSERT_EQ(1, info.si_value.sival_int);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800697}
Yabin Cui63481602014-12-01 17:41:04 -0800698
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800699TEST(signal, sigwaitinfo64_SIGRTMIN) {
700 SignalMaskRestorer smr;
701
702 // Block SIGRTMIN.
703 sigset64_t just_SIGRTMIN;
704 sigemptyset64(&just_SIGRTMIN);
705 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
706 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
707
708 // Raise SIGRTMIN.
Colin Cross7da20342021-07-28 11:18:11 -0700709 sigval sigval = {.sival_int = 1};
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800710 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
711
712 // Get pending SIGRTMIN.
713 siginfo_t info;
714 errno = 0;
715 ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info));
716 ASSERT_EQ(0, errno);
717 ASSERT_EQ(SIGRTMIN, info.si_signo);
718 ASSERT_EQ(1, info.si_value.sival_int);
Yabin Cui63481602014-12-01 17:41:04 -0800719}
720
721TEST(signal, sigtimedwait) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800722 SignalMaskRestorer smr;
723
Yabin Cui63481602014-12-01 17:41:04 -0800724 // Block SIGALRM.
725 sigset_t just_SIGALRM;
726 sigemptyset(&just_SIGALRM);
727 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800728 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800729
730 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700731 sigval sigval = { .sival_int = 1 };
Yabin Cui63481602014-12-01 17:41:04 -0800732 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
733
734 // Get pending SIGALRM.
735 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800736 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
Yabin Cui63481602014-12-01 17:41:04 -0800737 errno = 0;
738 ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout));
739 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800740}
Yabin Cui63481602014-12-01 17:41:04 -0800741
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800742TEST(signal, sigtimedwait64_SIGRTMIN) {
743 SignalMaskRestorer smr;
744
745 // Block SIGRTMIN.
746 sigset64_t just_SIGRTMIN;
747 sigemptyset64(&just_SIGRTMIN);
748 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
749 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
750
751 // Raise SIGALRM.
Colin Cross7da20342021-07-28 11:18:11 -0700752 sigval sigval = { .sival_int = 1 };
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800753 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
754
755 // Get pending SIGALRM.
756 siginfo_t info;
757 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
758 errno = 0;
759 ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout));
760 ASSERT_EQ(0, errno);
Yabin Cui63481602014-12-01 17:41:04 -0800761}
762
Yabin Cui63481602014-12-01 17:41:04 -0800763TEST(signal, sigtimedwait_timeout) {
764 // Block SIGALRM.
765 sigset_t just_SIGALRM;
766 sigemptyset(&just_SIGALRM);
767 sigaddset(&just_SIGALRM, SIGALRM);
768 sigset_t original_set;
769 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
770
771 // Wait timeout.
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700772 auto t0 = std::chrono::steady_clock::now();
Yabin Cui63481602014-12-01 17:41:04 -0800773 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800774 timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
Yabin Cui63481602014-12-01 17:41:04 -0800775 errno = 0;
776 ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
777 ASSERT_EQ(EAGAIN, errno);
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700778 auto t1 = std::chrono::steady_clock::now();
779 ASSERT_GE(t1-t0, 1000000ns);
Yabin Cui63481602014-12-01 17:41:04 -0800780
Yi Kong32bc0fc2018-08-02 17:31:13 -0700781 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800782}
Josh Gao61cf3f32016-03-08 15:27:15 -0800783
784#if defined(__BIONIC__)
785TEST(signal, rt_tgsigqueueinfo) {
786 // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self.
787 // If this fails, your kernel needs commit 66dd34a to be backported.
788 static constexpr char error_msg[] =
789 "\nPlease ensure that the following kernel patch has been applied:\n"
790 "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n";
791 static siginfo received;
792
Elliott Hughes3e235912018-02-01 14:21:51 -0800793 struct sigaction handler = {};
Josh Gao61cf3f32016-03-08 15:27:15 -0800794 handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; };
795 handler.sa_flags = SA_SIGINFO;
796
797 ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr));
798
Josh Gaod7878522016-03-14 18:15:15 -0700799 siginfo sent;
800 memset(&sent, 0, sizeof(sent));
Josh Gao61cf3f32016-03-08 15:27:15 -0800801
802 sent.si_code = SI_TKILL;
803 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
804 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
805 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
806 << sent.si_code << ", received " << received.si_code
807 << error_msg;
808
809 sent.si_code = SI_USER;
810 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
811 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
812 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
813 << sent.si_code << ", received " << received.si_code
814 << error_msg;
815}
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800816#endif
Josh Gaoc244fcb2016-03-29 14:34:03 -0700817
Josh Gaoc244fcb2016-03-29 14:34:03 -0700818TEST(signal, sigset_size) {
Elliott Hughes370e9632021-01-21 14:40:49 -0800819 // The setjmp implementations assume that sigset_t can fit in a
820 // long. This is true because ARM and x86 have broken rt signal support,
821 // and AArch64 and x86_64 both have a SIGRTMAX defined as 64.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800822#if defined(__BIONIC__)
Josh Gaoc244fcb2016-03-29 14:34:03 -0700823 static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long");
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800824#endif
825 static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals");
Josh Gaoc244fcb2016-03-29 14:34:03 -0700826}
827
Greg Hackmann5375bf62016-02-29 12:35:33 -0800828TEST(signal, sigignore_EINVAL) {
829 errno = 0;
830 ASSERT_EQ(-1, sigignore(99999));
831 ASSERT_EQ(EINVAL, errno);
832}
833
834TEST(signal, sigignore) {
835 errno = 0;
836 EXPECT_EQ(-1, sigignore(SIGKILL));
837 EXPECT_EQ(errno, EINVAL);
838
839 errno = 0;
840 EXPECT_EQ(-1, sigignore(SIGSTOP));
841 EXPECT_EQ(errno, EINVAL);
842
843 ScopedSignalHandler sigalrm{SIGALRM};
844 ASSERT_EQ(0, sigignore(SIGALRM));
845
846 struct sigaction sa;
847 ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa));
848 EXPECT_EQ(SIG_IGN, sa.sa_handler);
849}
850
851TEST(signal, sighold_EINVAL) {
852 errno = 0;
853 ASSERT_EQ(-1, sighold(99999));
854 ASSERT_EQ(EINVAL, errno);
855}
856
857TEST(signal, sigpause_EINVAL) {
858 errno = 0;
859 ASSERT_EQ(-1, sigpause(99999));
860 ASSERT_EQ(EINVAL, errno);
861}
862
863TEST(signal, sigrelse_EINVAL) {
864 errno = 0;
865 ASSERT_EQ(-1, sigpause(99999));
866 ASSERT_EQ(EINVAL, errno);
867}
868
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800869static void TestSigholdSigpauseSigrelse(int sig) {
870 static int signal_handler_call_count = 0;
871 ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }};
872 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800873 sigset_t set;
874
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800875 // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ...
876 ASSERT_EQ(0, sighold(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700877 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800878 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800879
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800880 // ... preventing our SIGALRM/SIGRTMIN handler from running ...
881 raise(sig);
882 ASSERT_EQ(0, signal_handler_call_count);
883 // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it.
884 ASSERT_EQ(-1, sigpause(sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800885 ASSERT_EQ(EINTR, errno);
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800886 ASSERT_EQ(1, signal_handler_call_count);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800887
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800888 if (sig >= SIGRTMIN && sizeof(void*) == 8) {
889 // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700890 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800891 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800892
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800893 // Whereas sigrelse(SIGALRM/SIGRTMIN) should.
894 ASSERT_EQ(0, sigrelse(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700895 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800896 EXPECT_FALSE(sigismember(&set, sig));
897 } else {
898 // sigismember won't work for SIGRTMIN on LP32.
899 }
900}
901
902TEST(signal, sighold_sigpause_sigrelse) {
903 TestSigholdSigpauseSigrelse(SIGALRM);
904}
905
906TEST(signal, sighold_sigpause_sigrelse_RT) {
907 TestSigholdSigpauseSigrelse(SIGRTMIN);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800908}
909
910TEST(signal, sigset_EINVAL) {
911 errno = 0;
912 ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL));
913 ASSERT_EQ(EINVAL, errno);
914}
915
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800916TEST(signal, sigset_RT) {
917 static int signal_handler_call_count = 0;
918 auto signal_handler = [](int) { signal_handler_call_count++; };
919 ScopedSignalHandler ssh{SIGRTMIN, signal_handler};
920 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800921
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800922 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD));
923#if defined(__LP64__)
Greg Hackmann5375bf62016-02-29 12:35:33 -0800924 sigset_t set;
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800925 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
926 ASSERT_TRUE(sigismember(&set, SIGRTMIN));
927#endif
928
929 ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler));
930 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler));
931 ASSERT_EQ(0, signal_handler_call_count);
932 raise(SIGRTMIN);
933 ASSERT_EQ(1, signal_handler_call_count);
934}
935
936TEST(signal, sigset) {
937 static int signal_handler_call_count = 0;
938 auto signal_handler = [](int) { signal_handler_call_count++; };
939 ScopedSignalHandler ssh{SIGALRM, signal_handler};
940 SignalMaskRestorer mask_restorer;
941
942 ASSERT_EQ(0, signal_handler_call_count);
943 raise(SIGALRM);
944 ASSERT_EQ(1, signal_handler_call_count);
945
946 // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD.
947 sigset_t set;
948 sigemptyset(&set);
949 sigaddset(&set, SIGALRM);
950 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr));
951
952 sigemptyset(&set);
953 ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800954 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
955 EXPECT_FALSE(sigismember(&set, SIGALRM));
956
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800957 ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800958 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
959 EXPECT_FALSE(sigismember(&set, SIGALRM));
960
961 ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL));
962 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
963 EXPECT_FALSE(sigismember(&set, SIGALRM));
964
965 ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD));
966 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
967 EXPECT_TRUE(sigismember(&set, SIGALRM));
968}
Elliott Hughes7532b322017-07-11 15:00:17 -0700969
970TEST(signal, killpg_EINVAL) {
971 // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0
972 // and passes 0 through to kill(2).
973 errno = 0;
974 ASSERT_EQ(-1, killpg(-1, SIGKILL));
975 ASSERT_EQ(EINVAL, errno);
976}