blob: 5bad8568f2d19e188d9907bf9ea035ca27381c04 [file] [log] [blame]
Dave Allisonf4b80bc2014-05-14 15:41:25 -07001/*
2 * Copyright (C) 2014 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
Dave Allisonf4b80bc2014-05-14 15:41:25 -070017#include <dlfcn.h>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070018#include <errno.h>
Josh Gao99875e92017-04-17 15:58:36 -070019#include <pthread.h>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070020#include <signal.h>
21#include <stdio.h>
22#include <stdlib.h>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070023#include <string.h>
Josh Gao85a78cf2017-03-20 16:26:42 -070024
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -080025#if defined(__BIONIC__)
26#include <bionic/macros.h>
27#endif
28
Josh Gaod32d79d2018-02-26 14:29:25 -080029#include <algorithm>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070030#include <initializer_list>
Igor Murashkin5573c372017-11-16 13:34:30 -080031#include <mutex>
Josh Gaod32d79d2018-02-26 14:29:25 -080032#include <type_traits>
Josh Gao85a78cf2017-03-20 16:26:42 -070033#include <utility>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070034
Josh Gaocca6fc02018-03-12 16:37:21 -070035#include "log.h"
Dave Allisoncefcea82014-09-16 10:01:01 -070036#include "sigchain.h"
37
Dave Allison69dfe512014-07-11 17:11:58 +000038#if defined(__APPLE__)
39#define _NSIG NSIG
Dave Allison38680092014-08-29 12:29:34 -070040#define sighandler_t sig_t
Josh Gaodd241ae2017-03-20 14:17:23 -070041
42// Darwin has an #error when ucontext.h is included without _XOPEN_SOURCE defined.
43#define _XOPEN_SOURCE
Dave Allison69dfe512014-07-11 17:11:58 +000044#endif
45
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -080046#define SA_UNSUPPORTED 0x00000400
47#define SA_EXPOSE_TAGBITS 0x00000800
48
Josh Gaodd241ae2017-03-20 14:17:23 -070049#include <ucontext.h>
50
Josh Gao85a78cf2017-03-20 16:26:42 -070051// libsigchain provides an interception layer for signal handlers, to allow ART and others to give
52// their signal handlers the first stab at handling signals before passing them on to user code.
53//
54// It implements wrapper functions for signal, sigaction, and sigprocmask, and a handler that
55// forwards signals appropriately.
56//
57// In our handler, we start off with all signals blocked, fetch the original signal mask from the
58// passed in ucontext, and then adjust our signal mask appropriately for the user handler.
59//
60// It's somewhat tricky for us to properly handle some flag cases:
61// SA_NOCLDSTOP and SA_NOCLDWAIT: shouldn't matter, we don't have special handlers for SIGCHLD.
62// SA_NODEFER: unimplemented, we can manually change the signal mask appropriately.
63// ~SA_ONSTACK: always silently enable this
64// SA_RESETHAND: unimplemented, but we can probably do this?
65// ~SA_RESTART: unimplemented, maybe we can reserve an RT signal, register an empty handler that
66// doesn't have SA_RESTART, and raise the signal to avoid restarting syscalls that are
67// expected to be interrupted?
Dave Allisonf4b80bc2014-05-14 15:41:25 -070068
Vladimir Markobe0d3cf2020-02-12 10:52:22 +000069#if defined(__BIONIC__) && !defined(__LP64__)
Josh Gaod32d79d2018-02-26 14:29:25 -080070static int sigismember(const sigset64_t* sigset, int signum) {
71 return sigismember64(sigset, signum);
72}
73
74static int sigemptyset(sigset64_t* sigset) {
75 return sigemptyset64(sigset);
76}
77
78static int sigaddset(sigset64_t* sigset, int signum) {
79 return sigaddset64(sigset, signum);
80}
81
82static int sigdelset(sigset64_t* sigset, int signum) {
83 return sigdelset64(sigset, signum);
84}
85#endif
86
87template<typename SigsetType>
88static int sigorset(SigsetType* dest, SigsetType* left, SigsetType* right) {
Josh Gao85a78cf2017-03-20 16:26:42 -070089 sigemptyset(dest);
Josh Gaod32d79d2018-02-26 14:29:25 -080090 for (size_t i = 0; i < sizeof(SigsetType) * CHAR_BIT; ++i) {
Josh Gao85a78cf2017-03-20 16:26:42 -070091 if (sigismember(left, i) == 1 || sigismember(right, i) == 1) {
92 sigaddset(dest, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -070093 }
Josh Gao85a78cf2017-03-20 16:26:42 -070094 }
95 return 0;
96}
97
98namespace art {
99
100static decltype(&sigaction) linked_sigaction;
101static decltype(&sigprocmask) linked_sigprocmask;
Josh Gao99875e92017-04-17 15:58:36 -0700102
Josh Gaod32d79d2018-02-26 14:29:25 -0800103#if defined(__BIONIC__)
104static decltype(&sigaction64) linked_sigaction64;
105static decltype(&sigprocmask64) linked_sigprocmask64;
106#endif
107
Josh Gao6e4deea2019-07-18 15:33:16 -0700108template <typename T>
109static void lookup_libc_symbol(T* output, T wrapper, const char* name) {
110#if defined(__BIONIC__)
111 constexpr const char* libc_name = "libc.so";
112#elif defined(__GLIBC__)
113#if __GNU_LIBRARY__ != 6
114#error unsupported glibc version
115#endif
116 constexpr const char* libc_name = "libc.so.6";
Colin Cross073885c2021-09-14 14:35:15 -0700117#elif defined(ANDROID_HOST_MUSL)
118 constexpr const char* libc_name = "libc_musl.so";
Josh Gao6e4deea2019-07-18 15:33:16 -0700119#else
120#error unsupported libc: not bionic or glibc?
121#endif
122
123 static void* libc = []() {
124 void* result = dlopen(libc_name, RTLD_LOCAL | RTLD_LAZY);
125 if (!result) {
126 fatal("failed to dlopen %s: %s", libc_name, dlerror());
127 }
128 return result;
129 }();
130
131 void* sym = dlsym(libc, name); // NOLINT glibc triggers cert-dcl16-c with RTLD_NEXT.
Josh Gaod32d79d2018-02-26 14:29:25 -0800132 if (sym == nullptr) {
133 sym = dlsym(RTLD_DEFAULT, name);
134 if (sym == wrapper || sym == sigaction) {
135 fatal("Unable to find next %s in signal chain", name);
136 }
137 }
138 *output = reinterpret_cast<T>(sym);
139}
140
Josh Gaofd4d0d32017-04-26 19:09:47 -0700141__attribute__((constructor)) static void InitializeSignalChain() {
142 static std::once_flag once;
143 std::call_once(once, []() {
Josh Gao6e4deea2019-07-18 15:33:16 -0700144 lookup_libc_symbol(&linked_sigaction, sigaction, "sigaction");
145 lookup_libc_symbol(&linked_sigprocmask, sigprocmask, "sigprocmask");
Josh Gaofd4d0d32017-04-26 19:09:47 -0700146
Josh Gaod32d79d2018-02-26 14:29:25 -0800147#if defined(__BIONIC__)
Josh Gao6e4deea2019-07-18 15:33:16 -0700148 lookup_libc_symbol(&linked_sigaction64, sigaction64, "sigaction64");
149 lookup_libc_symbol(&linked_sigprocmask64, sigprocmask64, "sigprocmask64");
Josh Gaod32d79d2018-02-26 14:29:25 -0800150#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700151 });
152}
153
Josh Gao99875e92017-04-17 15:58:36 -0700154static pthread_key_t GetHandlingSignalKey() {
155 static pthread_key_t key;
156 static std::once_flag once;
157 std::call_once(once, []() {
158 int rc = pthread_key_create(&key, nullptr);
159 if (rc != 0) {
160 fatal("failed to create sigchain pthread key: %s", strerror(rc));
161 }
162 });
163 return key;
164}
165
166static bool GetHandlingSignal() {
167 void* result = pthread_getspecific(GetHandlingSignalKey());
168 return reinterpret_cast<uintptr_t>(result);
169}
170
171static void SetHandlingSignal(bool value) {
172 pthread_setspecific(GetHandlingSignalKey(),
173 reinterpret_cast<void*>(static_cast<uintptr_t>(value)));
174}
175
176class ScopedHandlingSignal {
177 public:
178 ScopedHandlingSignal() : original_value_(GetHandlingSignal()) {
179 }
180
181 ~ScopedHandlingSignal() {
182 SetHandlingSignal(original_value_);
183 }
184
185 private:
186 bool original_value_;
187};
Josh Gao85a78cf2017-03-20 16:26:42 -0700188
189class SignalChain {
190 public:
191 SignalChain() : claimed_(false) {
Jin Qian33dca562017-03-18 02:51:37 +0000192 }
193
Josh Gao85a78cf2017-03-20 16:26:42 -0700194 bool IsClaimed() {
195 return claimed_;
196 }
197
198 void Claim(int signo) {
199 if (!claimed_) {
200 Register(signo);
201 claimed_ = true;
Jin Qian33dca562017-03-18 02:51:37 +0000202 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700203 }
204
205 // Register the signal chain with the kernel if needed.
206 void Register(int signo) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800207#if defined(__BIONIC__)
208 struct sigaction64 handler_action = {};
209 sigfillset64(&handler_action.sa_mask);
210#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700211 struct sigaction handler_action = {};
Josh Gaod32d79d2018-02-26 14:29:25 -0800212 sigfillset(&handler_action.sa_mask);
213#endif
214
Josh Gao85a78cf2017-03-20 16:26:42 -0700215 handler_action.sa_sigaction = SignalChain::Handler;
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800216 handler_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK |
217 SA_UNSUPPORTED | SA_EXPOSE_TAGBITS;
Josh Gaod32d79d2018-02-26 14:29:25 -0800218
219#if defined(__BIONIC__)
220 linked_sigaction64(signo, &handler_action, &action_);
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800221 linked_sigaction64(signo, nullptr, &handler_action);
Josh Gaod32d79d2018-02-26 14:29:25 -0800222#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700223 linked_sigaction(signo, &handler_action, &action_);
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800224 linked_sigaction(signo, nullptr, &handler_action);
Josh Gaod32d79d2018-02-26 14:29:25 -0800225#endif
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800226
227 // Newer kernels clear unknown flags from sigaction.sa_flags in order to
228 // allow userspace to determine which flag bits are supported. We use this
229 // behavior in turn to implement the same flag bit support detection
230 // protocol regardless of kernel version. Due to the lack of a flag bit
231 // support detection protocol in older kernels we assume support for a base
232 // set of flags that have been supported since at least 2003 [1]. No flags
233 // were introduced since then until the introduction of SA_EXPOSE_TAGBITS
234 // handled below. glibc headers do not define SA_RESTORER so we define it
235 // ourselves.
236 //
237 // TODO(pcc): The new kernel behavior has been implemented in a kernel
238 // patch [2] that has not yet landed. Update the code if necessary once it
239 // lands.
240 //
241 // [1] https://github.com/mpe/linux-fullhistory/commit/c0f806c86fc8b07ad426df023f1a4bb0e53c64f6
242 // [2] https://lore.kernel.org/linux-arm-kernel/cover.1605235762.git.pcc@google.com/
243#if !defined(__BIONIC__)
244#define SA_RESTORER 0x04000000
245#endif
246 kernel_supported_flags_ = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_SIGINFO |
247 SA_ONSTACK | SA_RESTART | SA_NODEFER |
248 SA_RESETHAND | SA_RESTORER;
249
250 // Determine whether the kernel supports SA_EXPOSE_TAGBITS. For newer
251 // kernels we use the flag support detection protocol described above. In
252 // order to allow userspace to distinguish old and new kernels,
253 // SA_UNSUPPORTED has been reserved as an unsupported flag. If the kernel
254 // did not clear it then we know that we have an old kernel that would not
255 // support SA_EXPOSE_TAGBITS anyway.
256 if (!(handler_action.sa_flags & SA_UNSUPPORTED) &&
257 (handler_action.sa_flags & SA_EXPOSE_TAGBITS)) {
258 kernel_supported_flags_ |= SA_EXPOSE_TAGBITS;
259 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700260 }
261
Josh Gaod32d79d2018-02-26 14:29:25 -0800262 template <typename SigactionType>
263 SigactionType GetAction() {
264 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
265 return action_;
266 } else {
267 SigactionType result;
268 result.sa_flags = action_.sa_flags;
269 result.sa_handler = action_.sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100270#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800271 result.sa_restorer = action_.sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100272#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800273 memcpy(&result.sa_mask, &action_.sa_mask,
274 std::min(sizeof(action_.sa_mask), sizeof(result.sa_mask)));
275 return result;
276 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700277 }
278
Josh Gaod32d79d2018-02-26 14:29:25 -0800279 template <typename SigactionType>
280 void SetAction(const SigactionType* new_action) {
281 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
282 action_ = *new_action;
283 } else {
284 action_.sa_flags = new_action->sa_flags;
285 action_.sa_handler = new_action->sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100286#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800287 action_.sa_restorer = new_action->sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100288#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800289 sigemptyset(&action_.sa_mask);
290 memcpy(&action_.sa_mask, &new_action->sa_mask,
291 std::min(sizeof(action_.sa_mask), sizeof(new_action->sa_mask)));
292 }
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800293 action_.sa_flags &= kernel_supported_flags_;
Josh Gao85a78cf2017-03-20 16:26:42 -0700294 }
295
Josh Gao6b2018f2017-05-04 13:55:28 -0700296 void AddSpecialHandler(SigchainAction* sa) {
297 for (SigchainAction& slot : special_handlers_) {
298 if (slot.sc_sigaction == nullptr) {
299 slot = *sa;
Josh Gao85a78cf2017-03-20 16:26:42 -0700300 return;
301 }
302 }
303
304 fatal("too many special signal handlers");
305 }
306
Josh Gao6b2018f2017-05-04 13:55:28 -0700307 void RemoveSpecialHandler(bool (*fn)(int, siginfo_t*, void*)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700308 // This isn't thread safe, but it's unlikely to be a real problem.
309 size_t len = sizeof(special_handlers_)/sizeof(*special_handlers_);
310 for (size_t i = 0; i < len; ++i) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700311 if (special_handlers_[i].sc_sigaction == fn) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700312 for (size_t j = i; j < len - 1; ++j) {
313 special_handlers_[j] = special_handlers_[j + 1];
314 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700315 special_handlers_[len - 1].sc_sigaction = nullptr;
Josh Gao85a78cf2017-03-20 16:26:42 -0700316 return;
317 }
318 }
319
320 fatal("failed to find special handler to remove");
321 }
322
323
324 static void Handler(int signo, siginfo_t* siginfo, void*);
325
326 private:
327 bool claimed_;
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800328 int kernel_supported_flags_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800329#if defined(__BIONIC__)
330 struct sigaction64 action_;
331#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700332 struct sigaction action_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800333#endif
Josh Gao6b2018f2017-05-04 13:55:28 -0700334 SigchainAction special_handlers_[2];
Josh Gao85a78cf2017-03-20 16:26:42 -0700335};
336
Josh Gao07d7a5d2018-02-26 14:12:34 -0800337// _NSIG is 1 greater than the highest valued signal, but signals start from 1.
338// Leave an empty element at index 0 for convenience.
339static SignalChain chains[_NSIG + 1];
Josh Gao85a78cf2017-03-20 16:26:42 -0700340
randy.jeong5bef0222019-05-27 10:29:09 +0900341static bool is_signal_hook_debuggable = false;
342
Josh Gao85a78cf2017-03-20 16:26:42 -0700343void SignalChain::Handler(int signo, siginfo_t* siginfo, void* ucontext_raw) {
344 // Try the special handlers first.
345 // If one of them crashes, we'll reenter this handler and pass that crash onto the user handler.
Josh Gao99875e92017-04-17 15:58:36 -0700346 if (!GetHandlingSignal()) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700347 for (const auto& handler : chains[signo].special_handlers_) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700348 if (handler.sc_sigaction == nullptr) {
349 break;
350 }
351
352 // The native bridge signal handler might not return.
353 // Avoid setting the thread local flag in this case, since we'll never
354 // get a chance to restore it.
355 bool handler_noreturn = (handler.sc_flags & SIGCHAIN_ALLOW_NORETURN);
356 sigset_t previous_mask;
357 linked_sigprocmask(SIG_SETMASK, &handler.sc_mask, &previous_mask);
358
359 ScopedHandlingSignal restorer;
360 if (!handler_noreturn) {
361 SetHandlingSignal(true);
362 }
363
364 if (handler.sc_sigaction(signo, siginfo, ucontext_raw)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700365 return;
366 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700367
368 linked_sigprocmask(SIG_SETMASK, &previous_mask, nullptr);
Josh Gao85a78cf2017-03-20 16:26:42 -0700369 }
370 }
371
372 // Forward to the user's signal handler.
373 int handler_flags = chains[signo].action_.sa_flags;
374 ucontext_t* ucontext = static_cast<ucontext_t*>(ucontext_raw);
Josh Gaod32d79d2018-02-26 14:29:25 -0800375#if defined(__BIONIC__)
376 sigset64_t mask;
377 sigorset(&mask, &ucontext->uc_sigmask64, &chains[signo].action_.sa_mask);
378#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700379 sigset_t mask;
380 sigorset(&mask, &ucontext->uc_sigmask, &chains[signo].action_.sa_mask);
Josh Gaod32d79d2018-02-26 14:29:25 -0800381#endif
Josh Gao04de4fe2017-05-31 20:24:51 -0700382 if (!(handler_flags & SA_NODEFER)) {
383 sigaddset(&mask, signo);
Josh Gao85a78cf2017-03-20 16:26:42 -0700384 }
Josh Gaod32d79d2018-02-26 14:29:25 -0800385
386#if defined(__BIONIC__)
387 linked_sigprocmask64(SIG_SETMASK, &mask, nullptr);
388#else
Josh Gao6b2018f2017-05-04 13:55:28 -0700389 linked_sigprocmask(SIG_SETMASK, &mask, nullptr);
Josh Gaod32d79d2018-02-26 14:29:25 -0800390#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700391
392 if ((handler_flags & SA_SIGINFO)) {
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800393 // If the chained handler is not expecting tag bits in the fault address,
394 // mask them out now.
395#if defined(__BIONIC__)
396 if (!(handler_flags & SA_EXPOSE_TAGBITS) &&
397 (signo == SIGILL || signo == SIGFPE || signo == SIGSEGV ||
398 signo == SIGBUS || signo == SIGTRAP) &&
399 siginfo->si_code > SI_USER && siginfo->si_code < SI_KERNEL &&
400 !(signo == SIGTRAP && siginfo->si_code == TRAP_HWBKPT)) {
401 siginfo->si_addr = untag_address(siginfo->si_addr);
402 }
403#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700404 chains[signo].action_.sa_sigaction(signo, siginfo, ucontext_raw);
Jin Qian33dca562017-03-18 02:51:37 +0000405 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700406 auto handler = chains[signo].action_.sa_handler;
407 if (handler == SIG_IGN) {
408 return;
409 } else if (handler == SIG_DFL) {
sunny.kuo496e8372021-08-18 10:32:48 +0800410 fatal("exiting due to SIG_DFL handler for signal %d, ucontext %p", signo, ucontext);
Jin Qian33dca562017-03-18 02:51:37 +0000411 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700412 handler(signo);
Jin Qian33dca562017-03-18 02:51:37 +0000413 }
414 }
415}
416
Josh Gaod32d79d2018-02-26 14:29:25 -0800417template <typename SigactionType>
418static int __sigaction(int signal, const SigactionType* new_action,
419 SigactionType* old_action,
420 int (*linked)(int, const SigactionType*,
421 SigactionType*)) {
randy.jeong5bef0222019-05-27 10:29:09 +0900422 if (is_signal_hook_debuggable) {
423 return 0;
424 }
425
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700426 // If this signal has been claimed as a signal chain, record the user's
427 // action but don't pass it on to the kernel.
428 // Note that we check that the signal number is in range here. An out of range signal
429 // number should behave exactly as the libc sigaction.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800430 if (signal <= 0 || signal >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700431 errno = EINVAL;
432 return -1;
433 }
434
Josh Gao9d631dd2017-03-23 20:04:58 -0700435 if (chains[signal].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800436 SigactionType saved_action = chains[signal].GetAction<SigactionType>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700437 if (new_action != nullptr) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700438 chains[signal].SetAction(new_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700439 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700440 if (old_action != nullptr) {
Dmitriy Ivanovc01683b2015-01-06 14:55:26 -0800441 *old_action = saved_action;
442 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700443 return 0;
444 }
445
446 // Will only get here if the signal chain has not been claimed. We want
447 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gaod32d79d2018-02-26 14:29:25 -0800448 return linked(signal, new_action, old_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700449}
450
Josh Gaod32d79d2018-02-26 14:29:25 -0800451extern "C" int sigaction(int signal, const struct sigaction* new_action,
452 struct sigaction* old_action) {
453 InitializeSignalChain();
454 return __sigaction(signal, new_action, old_action, linked_sigaction);
455}
456
457#if defined(__BIONIC__)
458extern "C" int sigaction64(int signal, const struct sigaction64* new_action,
459 struct sigaction64* old_action) {
460 InitializeSignalChain();
461 return __sigaction(signal, new_action, old_action, linked_sigaction64);
462}
463#endif
464
Josh Gao85a78cf2017-03-20 16:26:42 -0700465extern "C" sighandler_t signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700466 InitializeSignalChain();
467
Josh Gao07d7a5d2018-02-26 14:12:34 -0800468 if (signo <= 0 || signo >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700469 errno = EINVAL;
470 return SIG_ERR;
471 }
472
473 struct sigaction sa = {};
Dave Allison91a83662014-08-28 16:12:40 -0700474 sigemptyset(&sa.sa_mask);
475 sa.sa_handler = handler;
Josh Gao85a78cf2017-03-20 16:26:42 -0700476 sa.sa_flags = SA_RESTART | SA_ONSTACK;
Dave Allison91a83662014-08-28 16:12:40 -0700477 sighandler_t oldhandler;
478
479 // If this signal has been claimed as a signal chain, record the user's
480 // action but don't pass it on to the kernel.
Josh Gao85a78cf2017-03-20 16:26:42 -0700481 if (chains[signo].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800482 oldhandler = reinterpret_cast<sighandler_t>(
483 chains[signo].GetAction<struct sigaction>().sa_handler);
Josh Gao85a78cf2017-03-20 16:26:42 -0700484 chains[signo].SetAction(&sa);
Dave Allison91a83662014-08-28 16:12:40 -0700485 return oldhandler;
486 }
487
488 // Will only get here if the signal chain has not been claimed. We want
489 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gao85a78cf2017-03-20 16:26:42 -0700490 if (linked_sigaction(signo, &sa, &sa) == -1) {
Dave Allison91a83662014-08-28 16:12:40 -0700491 return SIG_ERR;
492 }
493
494 return reinterpret_cast<sighandler_t>(sa.sa_handler);
495}
496
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700497#if !defined(__LP64__)
Josh Gao85a78cf2017-03-20 16:26:42 -0700498extern "C" sighandler_t bsd_signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700499 InitializeSignalChain();
500
Josh Gao85a78cf2017-03-20 16:26:42 -0700501 return signal(signo, handler);
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700502}
503#endif
504
Josh Gaod32d79d2018-02-26 14:29:25 -0800505template <typename SigsetType>
506int __sigprocmask(int how, const SigsetType* new_set, SigsetType* old_set,
507 int (*linked)(int, const SigsetType*, SigsetType*)) {
Josh Gao90444552017-03-20 14:58:25 -0700508 // When inside a signal handler, forward directly to the actual sigprocmask.
Josh Gao99875e92017-04-17 15:58:36 -0700509 if (GetHandlingSignal()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800510 return linked(how, new_set, old_set);
Josh Gao90444552017-03-20 14:58:25 -0700511 }
512
Josh Gaod32d79d2018-02-26 14:29:25 -0800513 const SigsetType* new_set_ptr = new_set;
514 SigsetType tmpset;
515 if (new_set != nullptr) {
516 tmpset = *new_set;
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700517
Josh Gaof74caac2018-02-26 14:13:52 -0800518 if (how == SIG_BLOCK || how == SIG_SETMASK) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700519 // Don't allow claimed signals in the mask. If a signal chain has been claimed
520 // we can't allow the user to block that signal.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800521 for (int i = 1; i < _NSIG; ++i) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700522 if (chains[i].IsClaimed() && sigismember(&tmpset, i)) {
Dmitriy Ivanova3164b92015-04-01 11:08:45 -0700523 sigdelset(&tmpset, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700524 }
525 }
526 }
527 new_set_ptr = &tmpset;
528 }
529
Josh Gaod32d79d2018-02-26 14:29:25 -0800530 return linked(how, new_set_ptr, old_set);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700531}
Dave Allisoncefcea82014-09-16 10:01:01 -0700532
Josh Gaod32d79d2018-02-26 14:29:25 -0800533extern "C" int sigprocmask(int how, const sigset_t* new_set,
534 sigset_t* old_set) {
535 InitializeSignalChain();
536 return __sigprocmask(how, new_set, old_set, linked_sigprocmask);
537}
538
539#if defined(__BIONIC__)
540extern "C" int sigprocmask64(int how, const sigset64_t* new_set,
541 sigset64_t* old_set) {
542 InitializeSignalChain();
543 return __sigprocmask(how, new_set, old_set, linked_sigprocmask64);
544}
545#endif
546
Josh Gao6b2018f2017-05-04 13:55:28 -0700547extern "C" void AddSpecialSignalHandlerFn(int signal, SigchainAction* sa) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700548 InitializeSignalChain();
549
Josh Gao85a78cf2017-03-20 16:26:42 -0700550 if (signal <= 0 || signal >= _NSIG) {
551 fatal("Invalid signal %d", signal);
552 }
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700553
554 // Set the managed_handler.
Josh Gao6b2018f2017-05-04 13:55:28 -0700555 chains[signal].AddSpecialHandler(sa);
Josh Gao85a78cf2017-03-20 16:26:42 -0700556 chains[signal].Claim(signal);
557}
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700558
Josh Gao6b2018f2017-05-04 13:55:28 -0700559extern "C" void RemoveSpecialSignalHandlerFn(int signal, bool (*fn)(int, siginfo_t*, void*)) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700560 InitializeSignalChain();
561
Josh Gao85a78cf2017-03-20 16:26:42 -0700562 if (signal <= 0 || signal >= _NSIG) {
563 fatal("Invalid signal %d", signal);
564 }
565
566 chains[signal].RemoveSpecialHandler(fn);
567}
568
569extern "C" void EnsureFrontOfChain(int signal) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700570 InitializeSignalChain();
571
Josh Gao85a78cf2017-03-20 16:26:42 -0700572 if (signal <= 0 || signal >= _NSIG) {
573 fatal("Invalid signal %d", signal);
574 }
575
576 // Read the current action without looking at the chain, it should be the expected action.
Josh Gao3bef5272018-09-04 14:33:23 -0700577#if defined(__BIONIC__)
578 struct sigaction64 current_action;
579 linked_sigaction64(signal, nullptr, &current_action);
580#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700581 struct sigaction current_action;
Josh Gao85a78cf2017-03-20 16:26:42 -0700582 linked_sigaction(signal, nullptr, &current_action);
Josh Gao3bef5272018-09-04 14:33:23 -0700583#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700584
Josh Gao85a78cf2017-03-20 16:26:42 -0700585 // If the sigactions don't match then we put the current action on the chain and make ourself as
586 // the main action.
587 if (current_action.sa_sigaction != SignalChain::Handler) {
588 log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction);
589 chains[signal].Register(signal);
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700590 }
591}
592
randy.jeong5bef0222019-05-27 10:29:09 +0900593extern "C" void SkipAddSignalHandler(bool value) {
594 is_signal_hook_debuggable = value;
595}
596
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700597} // namespace art
598