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