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