1 /*
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 
17 #include <errno.h>
18 #include <signal.h>
19 #include <sys/syscall.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <chrono>
24 #include <thread>
25 
26 #include <android-base/macros.h>
27 #include <android-base/threads.h>
28 
29 #include <gtest/gtest.h>
30 
31 #include "SignalUtils.h"
32 #include "utils.h"
33 
34 using namespace std::chrono_literals;
35 
SIGNAL_MIN()36 static int SIGNAL_MIN() {
37   return 1; // Signals start at 1 (SIGHUP), not 0.
38 }
39 
40 template <typename SigSetT>
SIGNAL_MAX(SigSetT * set)41 static int SIGNAL_MAX(SigSetT* set) {
42   return sizeof(*set) * 8;
43 }
44 
45 template <typename SigSetT>
TestSigSet1(int (fn)(SigSetT *))46 static void TestSigSet1(int (fn)(SigSetT*)) {
47   // nullptr sigset_t*/sigset64_t*.
48   SigSetT* set_ptr = nullptr;
49   errno = 0;
50   ASSERT_EQ(-1, fn(set_ptr));
51   ASSERT_EQ(EINVAL, errno);
52 
53   // Non-nullptr.
54   SigSetT set = {};
55   errno = 0;
56   ASSERT_EQ(0, fn(&set));
57   ASSERT_EQ(0, errno);
58 }
59 
60 template <typename SigSetT>
TestSigSet2(int (fn)(SigSetT *,int))61 static void TestSigSet2(int (fn)(SigSetT*, int)) {
62   // nullptr sigset_t*/sigset64_t*.
63   SigSetT* set_ptr = nullptr;
64   errno = 0;
65   ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
66   ASSERT_EQ(EINVAL, errno);
67 
68   SigSetT set = {};
69 
70   // 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;
77   ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1));
78   ASSERT_EQ(EINVAL, errno);
79 
80   // Good signal numbers, low and high ends of range.
81   errno = 0;
82   ASSERT_EQ(0, fn(&set, SIGNAL_MIN()));
83   ASSERT_EQ(0, errno);
84   ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set)));
85   ASSERT_EQ(0, errno);
86 }
87 
TEST(signal,sigaddset_invalid)88 TEST(signal, sigaddset_invalid) {
89   TestSigSet2(sigaddset);
90 }
91 
TEST(signal,sigaddset64_invalid)92 TEST(signal, sigaddset64_invalid) {
93 #if defined(__BIONIC__)
94   TestSigSet2(sigaddset64);
95 #endif
96 }
97 
TEST(signal,sigdelset_invalid)98 TEST(signal, sigdelset_invalid) {
99   TestSigSet2(sigdelset);
100 }
101 
TEST(signal,sigdelset64_invalid)102 TEST(signal, sigdelset64_invalid) {
103 #if defined(__BIONIC__)
104   TestSigSet2(sigdelset64);
105 #endif
106 }
107 
TEST(signal,sigemptyset_invalid)108 TEST(signal, sigemptyset_invalid) {
109   TestSigSet1(sigemptyset);
110 }
111 
TEST(signal,sigemptyset64_invalid)112 TEST(signal, sigemptyset64_invalid) {
113 #if defined(__BIONIC__)
114   TestSigSet1(sigemptyset64);
115 #endif
116 }
117 
TEST(signal,sigfillset_invalid)118 TEST(signal, sigfillset_invalid) {
119   TestSigSet1(sigfillset);
120 }
121 
TEST(signal,sigfillset64_invalid)122 TEST(signal, sigfillset64_invalid) {
123 #if defined(__BIONIC__)
124   TestSigSet1(sigfillset64);
125 #endif
126 }
127 
TEST(signal,sigismember_invalid)128 TEST(signal, sigismember_invalid) {
129   TestSigSet2(sigismember);
130 }
131 
TEST(signal,sigismember64_invalid)132 TEST(signal, sigismember64_invalid) {
133 #if defined(__BIONIC__)
134   TestSigSet2(sigismember64);
135 #endif
136 }
137 
TEST(signal,raise_invalid)138 TEST(signal, raise_invalid) {
139   errno = 0;
140   ASSERT_EQ(-1, raise(-1));
141   ASSERT_EQ(EINVAL, errno);
142 }
143 
raise_in_signal_handler_helper(int signal_number)144 static 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 
TEST(signal,raise_in_signal_handler)152 TEST(signal, raise_in_signal_handler) {
153   ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
154   raise(SIGALRM);
155 }
156 
157 static int g_sigsuspend_signal_handler_call_count = 0;
158 
TEST(signal,sigsuspend_sigpending)159 TEST(signal, sigsuspend_sigpending) {
160   SignalMaskRestorer smr;
161 
162   // Block SIGALRM.
163   sigset_t just_SIGALRM;
164   sigemptyset(&just_SIGALRM);
165   sigaddset(&just_SIGALRM, SIGALRM);
166   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
167 
168   ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; });
169 
170   // There should be no pending signals.
171   sigset_t pending;
172   sigemptyset(&pending);
173   ASSERT_EQ(0, sigpending(&pending));
174   for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
175     EXPECT_FALSE(sigismember(&pending, i)) << i;
176   }
177 
178   // Raise SIGALRM and check our signal handler wasn't called.
179   raise(SIGALRM);
180   ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count);
181 
182   // We should now have a pending SIGALRM but nothing else.
183   sigemptyset(&pending);
184   ASSERT_EQ(0, sigpending(&pending));
185   for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
186     EXPECT_EQ((i == SIGALRM), sigismember(&pending, i));
187   }
188 
189   // 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.
196   ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count);
197 }
198 
199 static int g_sigsuspend64_signal_handler_call_count = 0;
200 
TEST(signal,sigsuspend64_sigpending64)201 TEST(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);
239 }
240 
241 template <typename SigActionT, typename SigSetT>
TestSigAction(int (sigaction_fn)(int,const SigActionT *,SigActionT *),int (sigaddset_fn)(SigSetT *,int),int sig)242 static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*),
243                           int (sigaddset_fn)(SigSetT*, int),
244                           int sig) {
245   // 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
247   // define SA_RESTORER, but luckily it's the same value everywhere.
248   static const unsigned sa_restorer = 0x4000000;
249 
250   // See what's currently set for this signal.
251   SigActionT original_sa = {};
252   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);
255   ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
256 #ifdef SA_RESTORER
257   ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer));
258 #endif
259 
260   // Set a traditional sa_handler signal handler.
261   auto no_op_signal_handler = [](int) {};
262   SigActionT sa = {};
263   sigaddset_fn(&sa.sa_mask, sig);
264   sa.sa_flags = SA_ONSTACK;
265   sa.sa_handler = no_op_signal_handler;
266   ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
267 
268   // Check that we can read it back.
269   sa = {};
270   ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
271   ASSERT_TRUE(sa.sa_handler == no_op_signal_handler);
272   ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
273   ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
274 #ifdef SA_RESTORER
275   ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
276 #endif
277 
278   // Set a new-style sa_sigaction signal handler.
279   auto no_op_sigaction = [](int, siginfo_t*, void*) {};
280   sa = {};
281   sigaddset_fn(&sa.sa_mask, sig);
282   sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
283   sa.sa_sigaction = no_op_sigaction;
284   ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
285 
286   // Check that we can read it back.
287   sa = {};
288   ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
289   ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction);
290   ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
291   ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
292 #ifdef SA_RESTORER
293   ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
294 #endif
295 
296   // Put everything back how it was.
297   ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr));
298 }
299 
TEST(signal,sigaction)300 TEST(signal, sigaction) {
301   TestSigAction(sigaction, sigaddset, SIGALRM);
302 }
303 
TEST(signal,sigaction64_SIGRTMIN)304 TEST(signal, sigaction64_SIGRTMIN) {
305   TestSigAction(sigaction64, sigaddset64, SIGRTMIN);
306 }
307 
ClearSignalMask()308 static void ClearSignalMask() {
309   uint64_t sigset = 0;
310   SignalSetAdd(&sigset, __SIGRTMIN);
311   if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
312     abort();
313   }
314 }
315 
FillSignalMask()316 static void FillSignalMask() {
317   uint64_t sigset = ~0ULL;
318   for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) {
319     SignalSetDel(&sigset, signo);
320   }
321   if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
322     abort();
323   }
324 }
325 
GetSignalMask()326 static 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 
TestSignalMaskFiltered(uint64_t sigset)334 static void TestSignalMaskFiltered(uint64_t sigset) {
335 #if defined(__BIONIC__)
336   for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) {
337     bool signal_blocked = sigset & (1ULL << (signo - 1));
338     if (signo == __SIGRTMIN) {
339       // TIMER_SIGNAL must be blocked.
340       EXPECT_EQ(true, signal_blocked) << "signal " << signo;
341     } else {
342       // The other reserved signals must not be blocked.
343       EXPECT_EQ(false, signal_blocked) << "signal " << signo;
344     }
345   }
346 #else
347   UNUSED(sigset);
348 #endif
349 }
350 
TestSignalMaskFunction(std::function<void ()> fn)351 static void TestSignalMaskFunction(std::function<void()> fn) {
352   ClearSignalMask();
353   fn();
354   TestSignalMaskFiltered(GetSignalMask());
355 }
356 
TEST(signal,sigaction_filter)357 TEST(signal, sigaction_filter) {
358   ClearSignalMask();
359   static uint64_t sigset;
360   struct sigaction sa = {};
361   sa.sa_handler = [](int) { sigset = GetSignalMask(); };
362   sa.sa_flags = SA_ONSTACK | SA_NODEFER;
363   sigfillset(&sa.sa_mask);
364   sigaction(SIGUSR1, &sa, nullptr);
365   raise(SIGUSR1);
366 
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);
375 }
376 
TEST(signal,sigaction64_filter)377 TEST(signal, sigaction64_filter) {
378   ClearSignalMask();
379   static uint64_t sigset;
380   struct sigaction64 sa = {};
381   sa.sa_handler = [](int) { sigset = GetSignalMask(); };
382   sa.sa_flags = SA_ONSTACK | SA_NODEFER;
383   sigfillset64(&sa.sa_mask);
384   sigaction64(SIGUSR1, &sa, nullptr);
385   raise(SIGUSR1);
386 
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);
394 }
395 
TEST(signal,sigprocmask_setmask_filter)396 TEST(signal, sigprocmask_setmask_filter) {
397   TestSignalMaskFunction([]() {
398     ClearSignalMask();
399     sigset_t sigset_libc;
400     sigfillset(&sigset_libc);
401     ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr));
402   });
403 }
404 
TEST(signal,sigprocmask64_setmask_filter)405 TEST(signal, sigprocmask64_setmask_filter) {
406   TestSignalMaskFunction([]() {
407     ClearSignalMask();
408     sigset64_t sigset_libc;
409     sigfillset64(&sigset_libc);
410     ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr));
411   });
412 }
413 
TEST(signal,pthread_sigmask_setmask_filter)414 TEST(signal, pthread_sigmask_setmask_filter) {
415   TestSignalMaskFunction([]() {
416     ClearSignalMask();
417     sigset_t sigset_libc;
418     sigfillset(&sigset_libc);
419     ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr));
420   });
421 }
422 
TEST(signal,pthread_sigmask64_setmask_filter)423 TEST(signal, pthread_sigmask64_setmask_filter) {
424   TestSignalMaskFunction([]() {
425     ClearSignalMask();
426     sigset64_t sigset_libc;
427     sigfillset64(&sigset_libc);
428     ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr));
429   });
430 }
431 
TEST(signal,sigprocmask_block_filter)432 TEST(signal, sigprocmask_block_filter) {
433   TestSignalMaskFunction([]() {
434     ClearSignalMask();
435     sigset_t sigset_libc;
436     sigfillset(&sigset_libc);
437     ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr));
438   });
439 }
440 
TEST(signal,sigprocmask64_block_filter)441 TEST(signal, sigprocmask64_block_filter) {
442   TestSignalMaskFunction([]() {
443     ClearSignalMask();
444     sigset64_t sigset_libc;
445     sigfillset64(&sigset_libc);
446     ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr));
447   });
448 }
449 
TEST(signal,pthread_sigmask_block_filter)450 TEST(signal, pthread_sigmask_block_filter) {
451   TestSignalMaskFunction([]() {
452     ClearSignalMask();
453     sigset_t sigset_libc;
454     sigfillset(&sigset_libc);
455     ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr));
456   });
457 }
458 
TEST(signal,pthread_sigmask64_block_filter)459 TEST(signal, pthread_sigmask64_block_filter) {
460   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 
TEST(signal,sigprocmask_unblock_filter)468 TEST(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 
TEST(signal,sigprocmask64_unblock_filter)477 TEST(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 
TEST(signal,pthread_sigmask_unblock_filter)486 TEST(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 
TEST(signal,pthread_sigmask64_unblock_filter)495 TEST(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   });
502 }
503 
504 // glibc filters out signals via sigfillset, not the actual underlying functions.
TEST(signal,sigset_filter)505 TEST(signal, sigset_filter) {
506 #if defined(__BIONIC__)
507   TestSignalMaskFunction([]() {
508     for (int i = 1; i <= 64; ++i) {
509       sigset(i, SIG_HOLD);
510     }
511   });
512 #endif
513 }
514 
TEST(signal,sighold_filter)515 TEST(signal, sighold_filter) {
516 #if defined(__BIONIC__)
517   TestSignalMaskFunction([]() {
518     for (int i = 1; i <= 64; ++i) {
519       sighold(i);
520     }
521   });
522 #endif
523 }
524 
525 #if defined(__BIONIC__)
526 // Not exposed via headers, but the symbols are available if you declare them yourself.
527 extern "C" int sigblock(int);
528 extern "C" int sigsetmask(int);
529 #endif
530 
TEST(signal,sigblock_filter)531 TEST(signal, sigblock_filter) {
532 #if defined(__BIONIC__)
533   TestSignalMaskFunction([]() {
534     sigblock(~0U);
535   });
536 #endif
537 }
538 
TEST(signal,sigsetmask_filter)539 TEST(signal, sigsetmask_filter) {
540 #if defined(__BIONIC__)
541   TestSignalMaskFunction([]() {
542     sigsetmask(~0U);
543   });
544 #endif
545 }
546 
TEST(signal,sys_signame)547 TEST(signal, sys_signame) {
548 #if defined(__BIONIC__)
549   ASSERT_TRUE(sys_signame[0] == nullptr);
550   ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
551 #else
552   GTEST_SKIP() << "glibc doesn't have sys_signame";
553 #endif
554 }
555 
TEST(signal,sys_siglist)556 TEST(signal, sys_siglist) {
557   ASSERT_TRUE(sys_siglist[0] == nullptr);
558   ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
559 }
560 
TEST(signal,limits)561 TEST(signal, limits) {
562   // These come from the kernel.
563   ASSERT_EQ(32, __SIGRTMIN);
564   ASSERT_EQ(64, __SIGRTMAX);
565 
566   // We reserve a non-zero number at the bottom for ourselves.
567   ASSERT_GT(SIGRTMIN, __SIGRTMIN);
568 
569   // We don't currently reserve any at the top.
570   ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
571 }
572 
573 static int g_sigqueue_signal_handler_call_count = 0;
574 
SigqueueSignalHandler(int signum,siginfo_t * info,void *)575 static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) {
576   ASSERT_EQ(SIGALRM, signum);
577   ASSERT_EQ(SIGALRM, info->si_signo);
578   ASSERT_EQ(SI_QUEUE, info->si_code);
579   ASSERT_EQ(1, info->si_value.sival_int);
580   ++g_sigqueue_signal_handler_call_count;
581 }
582 
TEST(signal,sigqueue)583 TEST(signal, sigqueue) {
584   ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
585   sigval_t sigval = {.sival_int = 1};
586   errno = 0;
587   ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
588   ASSERT_EQ(0, errno);
589   ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
590 }
591 
TEST(signal,pthread_sigqueue_self)592 TEST(signal, pthread_sigqueue_self) {
593   ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
594   sigval_t sigval = {.sival_int = 1};
595   errno = 0;
596   ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval));
597   ASSERT_EQ(0, errno);
598   ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
599 }
600 
TEST(signal,pthread_sigqueue_other)601 TEST(signal, pthread_sigqueue_other) {
602   ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
603   sigval_t sigval = {.sival_int = 1};
604 
605   sigset_t mask;
606   sigfillset(&mask);
607   pthread_sigmask(SIG_SETMASK, &mask, nullptr);
608   pthread_t thread;
609   int rc = pthread_create(&thread, nullptr,
610                           [](void*) -> void* {
611                             sigset_t mask;
612                             sigemptyset(&mask);
613                             sigsuspend(&mask);
614                             return nullptr;
615                           },
616                           nullptr);
617   ASSERT_EQ(0, rc);
618 
619   errno = 0;
620   ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval));
621   ASSERT_EQ(0, errno);
622   pthread_join(thread, nullptr);
623   ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
624 }
625 
TEST(signal,sigwait_SIGALRM)626 TEST(signal, sigwait_SIGALRM) {
627   SignalMaskRestorer smr;
628 
629   // Block SIGALRM.
630   sigset_t just_SIGALRM;
631   sigemptyset(&just_SIGALRM);
632   sigaddset(&just_SIGALRM, SIGALRM);
633   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
634 
635   // Raise SIGALRM.
636   sigval_t sigval = {.sival_int = 1};
637   ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
638 
639   // Get pending SIGALRM.
640   int sig;
641   ASSERT_EQ(0, sigwait(&just_SIGALRM, &sig));
642   ASSERT_EQ(SIGALRM, sig);
643 }
644 
TEST(signal,sigwait64_SIGRTMIN)645 TEST(signal, sigwait64_SIGRTMIN) {
646   SignalMaskRestorer smr;
647 
648   // Block SIGRTMIN.
649   sigset64_t just_SIGRTMIN;
650   sigemptyset64(&just_SIGRTMIN);
651   sigaddset64(&just_SIGRTMIN, SIGRTMIN);
652   ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
653 
654   // Raise SIGRTMIN.
655   sigval_t sigval = {.sival_int = 1};
656   ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
657 
658   // Get pending SIGRTMIN.
659   int sig;
660   ASSERT_EQ(0, sigwait64(&just_SIGRTMIN, &sig));
661   ASSERT_EQ(SIGRTMIN, sig);
662 }
663 
TEST(signal,sigwaitinfo)664 TEST(signal, sigwaitinfo) {
665   SignalMaskRestorer smr;
666 
667   // Block SIGALRM.
668   sigset_t just_SIGALRM;
669   sigemptyset(&just_SIGALRM);
670   sigaddset(&just_SIGALRM, SIGALRM);
671   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
672 
673   // Raise SIGALRM.
674   sigval_t sigval = {.sival_int = 1};
675   ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
676 
677   // Get pending SIGALRM.
678   siginfo_t info;
679   errno = 0;
680   ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info));
681   ASSERT_EQ(0, errno);
682   ASSERT_EQ(SIGALRM, info.si_signo);
683   ASSERT_EQ(1, info.si_value.sival_int);
684 }
685 
TEST(signal,sigwaitinfo64_SIGRTMIN)686 TEST(signal, sigwaitinfo64_SIGRTMIN) {
687   SignalMaskRestorer smr;
688 
689   // Block SIGRTMIN.
690   sigset64_t just_SIGRTMIN;
691   sigemptyset64(&just_SIGRTMIN);
692   sigaddset64(&just_SIGRTMIN, SIGRTMIN);
693   ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
694 
695   // Raise SIGRTMIN.
696   sigval_t sigval = {.sival_int = 1};
697   ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
698 
699   // Get pending SIGRTMIN.
700   siginfo_t info;
701   errno = 0;
702   ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info));
703   ASSERT_EQ(0, errno);
704   ASSERT_EQ(SIGRTMIN, info.si_signo);
705   ASSERT_EQ(1, info.si_value.sival_int);
706 }
707 
TEST(signal,sigtimedwait)708 TEST(signal, sigtimedwait) {
709   SignalMaskRestorer smr;
710 
711   // Block SIGALRM.
712   sigset_t just_SIGALRM;
713   sigemptyset(&just_SIGALRM);
714   sigaddset(&just_SIGALRM, SIGALRM);
715   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
716 
717   // Raise SIGALRM.
718   sigval_t sigval = { .sival_int = 1 };
719   ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
720 
721   // Get pending SIGALRM.
722   siginfo_t info;
723   timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
724   errno = 0;
725   ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout));
726   ASSERT_EQ(0, errno);
727 }
728 
TEST(signal,sigtimedwait64_SIGRTMIN)729 TEST(signal, sigtimedwait64_SIGRTMIN) {
730   SignalMaskRestorer smr;
731 
732   // Block SIGRTMIN.
733   sigset64_t just_SIGRTMIN;
734   sigemptyset64(&just_SIGRTMIN);
735   sigaddset64(&just_SIGRTMIN, SIGRTMIN);
736   ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
737 
738   // Raise SIGALRM.
739   sigval_t sigval = { .sival_int = 1 };
740   ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
741 
742   // Get pending SIGALRM.
743   siginfo_t info;
744   timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
745   errno = 0;
746   ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout));
747   ASSERT_EQ(0, errno);
748 }
749 
TEST(signal,sigtimedwait_timeout)750 TEST(signal, sigtimedwait_timeout) {
751   // Block SIGALRM.
752   sigset_t just_SIGALRM;
753   sigemptyset(&just_SIGALRM);
754   sigaddset(&just_SIGALRM, SIGALRM);
755   sigset_t original_set;
756   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
757 
758   // Wait timeout.
759   auto t0 = std::chrono::steady_clock::now();
760   siginfo_t info;
761   timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
762   errno = 0;
763   ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
764   ASSERT_EQ(EAGAIN, errno);
765   auto t1 = std::chrono::steady_clock::now();
766   ASSERT_GE(t1-t0, 1000000ns);
767 
768   ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr));
769 }
770 
771 #if defined(__BIONIC__)
TEST(signal,rt_tgsigqueueinfo)772 TEST(signal, rt_tgsigqueueinfo) {
773   // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self.
774   // If this fails, your kernel needs commit 66dd34a to be backported.
775   static constexpr char error_msg[] =
776     "\nPlease ensure that the following kernel patch has been applied:\n"
777     "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n";
778   static siginfo received;
779 
780   struct sigaction handler = {};
781   handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; };
782   handler.sa_flags = SA_SIGINFO;
783 
784   ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr));
785 
786   siginfo sent;
787   memset(&sent, 0, sizeof(sent));
788 
789   sent.si_code = SI_TKILL;
790   ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
791     << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
792   ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
793                                             << sent.si_code << ", received " << received.si_code
794                                             << error_msg;
795 
796   sent.si_code = SI_USER;
797   ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
798     << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
799   ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
800                                             << sent.si_code << ", received " << received.si_code
801                                             << error_msg;
802 }
803 #endif
804 
TEST(signal,sigset_size)805 TEST(signal, sigset_size) {
806   // The setjmp implementations for ARM, AArch64, x86, and x86_64 assume that sigset_t can fit in a
807   // long. This is true because ARM and x86 have broken rt signal support, and AArch64 and x86_64
808   // both have a SIGRTMAX defined as 64.
809 #if defined(__arm__) || defined(__aarch64__) || defined(__i386__) || defined(__x86_64__)
810 #if defined(__BIONIC__)
811   static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long");
812 #endif
813   static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals");
814 #endif
815 }
816 
TEST(signal,sigignore_EINVAL)817 TEST(signal, sigignore_EINVAL) {
818   errno = 0;
819   ASSERT_EQ(-1, sigignore(99999));
820   ASSERT_EQ(EINVAL, errno);
821 }
822 
TEST(signal,sigignore)823 TEST(signal, sigignore) {
824   errno = 0;
825   EXPECT_EQ(-1, sigignore(SIGKILL));
826   EXPECT_EQ(errno, EINVAL);
827 
828   errno = 0;
829   EXPECT_EQ(-1, sigignore(SIGSTOP));
830   EXPECT_EQ(errno, EINVAL);
831 
832   ScopedSignalHandler sigalrm{SIGALRM};
833   ASSERT_EQ(0, sigignore(SIGALRM));
834 
835   struct sigaction sa;
836   ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa));
837   EXPECT_EQ(SIG_IGN, sa.sa_handler);
838 }
839 
TEST(signal,sighold_EINVAL)840 TEST(signal, sighold_EINVAL) {
841   errno = 0;
842   ASSERT_EQ(-1, sighold(99999));
843   ASSERT_EQ(EINVAL, errno);
844 }
845 
TEST(signal,sigpause_EINVAL)846 TEST(signal, sigpause_EINVAL) {
847   errno = 0;
848   ASSERT_EQ(-1, sigpause(99999));
849   ASSERT_EQ(EINVAL, errno);
850 }
851 
TEST(signal,sigrelse_EINVAL)852 TEST(signal, sigrelse_EINVAL) {
853   errno = 0;
854   ASSERT_EQ(-1, sigpause(99999));
855   ASSERT_EQ(EINVAL, errno);
856 }
857 
TestSigholdSigpauseSigrelse(int sig)858 static void TestSigholdSigpauseSigrelse(int sig) {
859   static int signal_handler_call_count = 0;
860   ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }};
861   SignalMaskRestorer mask_restorer;
862   sigset_t set;
863 
864   // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ...
865   ASSERT_EQ(0, sighold(sig));
866   ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
867   EXPECT_TRUE(sigismember(&set, sig));
868 
869   // ... preventing our SIGALRM/SIGRTMIN handler from running ...
870   raise(sig);
871   ASSERT_EQ(0, signal_handler_call_count);
872   // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it.
873   ASSERT_EQ(-1, sigpause(sig));
874   ASSERT_EQ(EINTR, errno);
875   ASSERT_EQ(1, signal_handler_call_count);
876 
877   if (sig >= SIGRTMIN && sizeof(void*) == 8) {
878     // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN.
879     ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
880     EXPECT_TRUE(sigismember(&set, sig));
881 
882     // Whereas sigrelse(SIGALRM/SIGRTMIN) should.
883     ASSERT_EQ(0, sigrelse(sig));
884     ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
885     EXPECT_FALSE(sigismember(&set, sig));
886   } else {
887     // sigismember won't work for SIGRTMIN on LP32.
888   }
889 }
890 
TEST(signal,sighold_sigpause_sigrelse)891 TEST(signal, sighold_sigpause_sigrelse) {
892   TestSigholdSigpauseSigrelse(SIGALRM);
893 }
894 
TEST(signal,sighold_sigpause_sigrelse_RT)895 TEST(signal, sighold_sigpause_sigrelse_RT) {
896   TestSigholdSigpauseSigrelse(SIGRTMIN);
897 }
898 
TEST(signal,sigset_EINVAL)899 TEST(signal, sigset_EINVAL) {
900   errno = 0;
901   ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL));
902   ASSERT_EQ(EINVAL, errno);
903 }
904 
TEST(signal,sigset_RT)905 TEST(signal, sigset_RT) {
906   static int signal_handler_call_count = 0;
907   auto signal_handler = [](int) { signal_handler_call_count++; };
908   ScopedSignalHandler ssh{SIGRTMIN, signal_handler};
909   SignalMaskRestorer mask_restorer;
910 
911   ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD));
912 #if defined(__LP64__)
913   sigset_t set;
914   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
915   ASSERT_TRUE(sigismember(&set, SIGRTMIN));
916 #endif
917 
918   ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler));
919   ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler));
920   ASSERT_EQ(0, signal_handler_call_count);
921   raise(SIGRTMIN);
922   ASSERT_EQ(1, signal_handler_call_count);
923 }
924 
TEST(signal,sigset)925 TEST(signal, sigset) {
926   static int signal_handler_call_count = 0;
927   auto signal_handler = [](int) { signal_handler_call_count++; };
928   ScopedSignalHandler ssh{SIGALRM, signal_handler};
929   SignalMaskRestorer mask_restorer;
930 
931   ASSERT_EQ(0, signal_handler_call_count);
932   raise(SIGALRM);
933   ASSERT_EQ(1, signal_handler_call_count);
934 
935   // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD.
936   sigset_t set;
937   sigemptyset(&set);
938   sigaddset(&set, SIGALRM);
939   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr));
940 
941   sigemptyset(&set);
942   ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler));
943   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
944   EXPECT_FALSE(sigismember(&set, SIGALRM));
945 
946   ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN));
947   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
948   EXPECT_FALSE(sigismember(&set, SIGALRM));
949 
950   ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL));
951   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
952   EXPECT_FALSE(sigismember(&set, SIGALRM));
953 
954   ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD));
955   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
956   EXPECT_TRUE(sigismember(&set, SIGALRM));
957 }
958 
TEST(signal,killpg_EINVAL)959 TEST(signal, killpg_EINVAL) {
960   // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0
961   // and passes 0 through to kill(2).
962   errno = 0;
963   ASSERT_EQ(-1, killpg(-1, SIGKILL));
964   ASSERT_EQ(EINVAL, errno);
965 }
966