blob: 8aa47600bfd90ba515b8219bf12248b0ea6dccea [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";
117#else
118#error unsupported libc: not bionic or glibc?
119#endif
120
121 static void* libc = []() {
122 void* result = dlopen(libc_name, RTLD_LOCAL | RTLD_LAZY);
123 if (!result) {
124 fatal("failed to dlopen %s: %s", libc_name, dlerror());
125 }
126 return result;
127 }();
128
129 void* sym = dlsym(libc, name); // NOLINT glibc triggers cert-dcl16-c with RTLD_NEXT.
Josh Gaod32d79d2018-02-26 14:29:25 -0800130 if (sym == nullptr) {
131 sym = dlsym(RTLD_DEFAULT, name);
132 if (sym == wrapper || sym == sigaction) {
133 fatal("Unable to find next %s in signal chain", name);
134 }
135 }
136 *output = reinterpret_cast<T>(sym);
137}
138
Josh Gaofd4d0d32017-04-26 19:09:47 -0700139__attribute__((constructor)) static void InitializeSignalChain() {
140 static std::once_flag once;
141 std::call_once(once, []() {
Josh Gao6e4deea2019-07-18 15:33:16 -0700142 lookup_libc_symbol(&linked_sigaction, sigaction, "sigaction");
143 lookup_libc_symbol(&linked_sigprocmask, sigprocmask, "sigprocmask");
Josh Gaofd4d0d32017-04-26 19:09:47 -0700144
Josh Gaod32d79d2018-02-26 14:29:25 -0800145#if defined(__BIONIC__)
Josh Gao6e4deea2019-07-18 15:33:16 -0700146 lookup_libc_symbol(&linked_sigaction64, sigaction64, "sigaction64");
147 lookup_libc_symbol(&linked_sigprocmask64, sigprocmask64, "sigprocmask64");
Josh Gaod32d79d2018-02-26 14:29:25 -0800148#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700149 });
150}
151
Josh Gao99875e92017-04-17 15:58:36 -0700152static pthread_key_t GetHandlingSignalKey() {
153 static pthread_key_t key;
154 static std::once_flag once;
155 std::call_once(once, []() {
156 int rc = pthread_key_create(&key, nullptr);
157 if (rc != 0) {
158 fatal("failed to create sigchain pthread key: %s", strerror(rc));
159 }
160 });
161 return key;
162}
163
164static bool GetHandlingSignal() {
165 void* result = pthread_getspecific(GetHandlingSignalKey());
166 return reinterpret_cast<uintptr_t>(result);
167}
168
169static void SetHandlingSignal(bool value) {
170 pthread_setspecific(GetHandlingSignalKey(),
171 reinterpret_cast<void*>(static_cast<uintptr_t>(value)));
172}
173
174class ScopedHandlingSignal {
175 public:
176 ScopedHandlingSignal() : original_value_(GetHandlingSignal()) {
177 }
178
179 ~ScopedHandlingSignal() {
180 SetHandlingSignal(original_value_);
181 }
182
183 private:
184 bool original_value_;
185};
Josh Gao85a78cf2017-03-20 16:26:42 -0700186
187class SignalChain {
188 public:
189 SignalChain() : claimed_(false) {
Jin Qian33dca562017-03-18 02:51:37 +0000190 }
191
Josh Gao85a78cf2017-03-20 16:26:42 -0700192 bool IsClaimed() {
193 return claimed_;
194 }
195
196 void Claim(int signo) {
197 if (!claimed_) {
198 Register(signo);
199 claimed_ = true;
Jin Qian33dca562017-03-18 02:51:37 +0000200 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700201 }
202
203 // Register the signal chain with the kernel if needed.
204 void Register(int signo) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800205#if defined(__BIONIC__)
206 struct sigaction64 handler_action = {};
207 sigfillset64(&handler_action.sa_mask);
208#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700209 struct sigaction handler_action = {};
Josh Gaod32d79d2018-02-26 14:29:25 -0800210 sigfillset(&handler_action.sa_mask);
211#endif
212
Josh Gao85a78cf2017-03-20 16:26:42 -0700213 handler_action.sa_sigaction = SignalChain::Handler;
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800214 handler_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK |
215 SA_UNSUPPORTED | SA_EXPOSE_TAGBITS;
Josh Gaod32d79d2018-02-26 14:29:25 -0800216
217#if defined(__BIONIC__)
218 linked_sigaction64(signo, &handler_action, &action_);
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800219 linked_sigaction64(signo, nullptr, &handler_action);
Josh Gaod32d79d2018-02-26 14:29:25 -0800220#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700221 linked_sigaction(signo, &handler_action, &action_);
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800222 linked_sigaction(signo, nullptr, &handler_action);
Josh Gaod32d79d2018-02-26 14:29:25 -0800223#endif
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800224
225 // Newer kernels clear unknown flags from sigaction.sa_flags in order to
226 // allow userspace to determine which flag bits are supported. We use this
227 // behavior in turn to implement the same flag bit support detection
228 // protocol regardless of kernel version. Due to the lack of a flag bit
229 // support detection protocol in older kernels we assume support for a base
230 // set of flags that have been supported since at least 2003 [1]. No flags
231 // were introduced since then until the introduction of SA_EXPOSE_TAGBITS
232 // handled below. glibc headers do not define SA_RESTORER so we define it
233 // ourselves.
234 //
235 // TODO(pcc): The new kernel behavior has been implemented in a kernel
236 // patch [2] that has not yet landed. Update the code if necessary once it
237 // lands.
238 //
239 // [1] https://github.com/mpe/linux-fullhistory/commit/c0f806c86fc8b07ad426df023f1a4bb0e53c64f6
240 // [2] https://lore.kernel.org/linux-arm-kernel/cover.1605235762.git.pcc@google.com/
241#if !defined(__BIONIC__)
242#define SA_RESTORER 0x04000000
243#endif
244 kernel_supported_flags_ = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_SIGINFO |
245 SA_ONSTACK | SA_RESTART | SA_NODEFER |
246 SA_RESETHAND | SA_RESTORER;
247
248 // Determine whether the kernel supports SA_EXPOSE_TAGBITS. For newer
249 // kernels we use the flag support detection protocol described above. In
250 // order to allow userspace to distinguish old and new kernels,
251 // SA_UNSUPPORTED has been reserved as an unsupported flag. If the kernel
252 // did not clear it then we know that we have an old kernel that would not
253 // support SA_EXPOSE_TAGBITS anyway.
254 if (!(handler_action.sa_flags & SA_UNSUPPORTED) &&
255 (handler_action.sa_flags & SA_EXPOSE_TAGBITS)) {
256 kernel_supported_flags_ |= SA_EXPOSE_TAGBITS;
257 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700258 }
259
Josh Gaod32d79d2018-02-26 14:29:25 -0800260 template <typename SigactionType>
261 SigactionType GetAction() {
262 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
263 return action_;
264 } else {
265 SigactionType result;
266 result.sa_flags = action_.sa_flags;
267 result.sa_handler = action_.sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100268#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800269 result.sa_restorer = action_.sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100270#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800271 memcpy(&result.sa_mask, &action_.sa_mask,
272 std::min(sizeof(action_.sa_mask), sizeof(result.sa_mask)));
273 return result;
274 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700275 }
276
Josh Gaod32d79d2018-02-26 14:29:25 -0800277 template <typename SigactionType>
278 void SetAction(const SigactionType* new_action) {
279 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
280 action_ = *new_action;
281 } else {
282 action_.sa_flags = new_action->sa_flags;
283 action_.sa_handler = new_action->sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100284#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800285 action_.sa_restorer = new_action->sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100286#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800287 sigemptyset(&action_.sa_mask);
288 memcpy(&action_.sa_mask, &new_action->sa_mask,
289 std::min(sizeof(action_.sa_mask), sizeof(new_action->sa_mask)));
290 }
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800291 action_.sa_flags &= kernel_supported_flags_;
Josh Gao85a78cf2017-03-20 16:26:42 -0700292 }
293
Josh Gao6b2018f2017-05-04 13:55:28 -0700294 void AddSpecialHandler(SigchainAction* sa) {
295 for (SigchainAction& slot : special_handlers_) {
296 if (slot.sc_sigaction == nullptr) {
297 slot = *sa;
Josh Gao85a78cf2017-03-20 16:26:42 -0700298 return;
299 }
300 }
301
302 fatal("too many special signal handlers");
303 }
304
Josh Gao6b2018f2017-05-04 13:55:28 -0700305 void RemoveSpecialHandler(bool (*fn)(int, siginfo_t*, void*)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700306 // This isn't thread safe, but it's unlikely to be a real problem.
307 size_t len = sizeof(special_handlers_)/sizeof(*special_handlers_);
308 for (size_t i = 0; i < len; ++i) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700309 if (special_handlers_[i].sc_sigaction == fn) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700310 for (size_t j = i; j < len - 1; ++j) {
311 special_handlers_[j] = special_handlers_[j + 1];
312 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700313 special_handlers_[len - 1].sc_sigaction = nullptr;
Josh Gao85a78cf2017-03-20 16:26:42 -0700314 return;
315 }
316 }
317
318 fatal("failed to find special handler to remove");
319 }
320
321
322 static void Handler(int signo, siginfo_t* siginfo, void*);
323
324 private:
325 bool claimed_;
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800326 int kernel_supported_flags_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800327#if defined(__BIONIC__)
328 struct sigaction64 action_;
329#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700330 struct sigaction action_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800331#endif
Josh Gao6b2018f2017-05-04 13:55:28 -0700332 SigchainAction special_handlers_[2];
Josh Gao85a78cf2017-03-20 16:26:42 -0700333};
334
Josh Gao07d7a5d2018-02-26 14:12:34 -0800335// _NSIG is 1 greater than the highest valued signal, but signals start from 1.
336// Leave an empty element at index 0 for convenience.
337static SignalChain chains[_NSIG + 1];
Josh Gao85a78cf2017-03-20 16:26:42 -0700338
randy.jeong5bef0222019-05-27 10:29:09 +0900339static bool is_signal_hook_debuggable = false;
340
Josh Gao85a78cf2017-03-20 16:26:42 -0700341void SignalChain::Handler(int signo, siginfo_t* siginfo, void* ucontext_raw) {
342 // Try the special handlers first.
343 // 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 -0700344 if (!GetHandlingSignal()) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700345 for (const auto& handler : chains[signo].special_handlers_) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700346 if (handler.sc_sigaction == nullptr) {
347 break;
348 }
349
350 // The native bridge signal handler might not return.
351 // Avoid setting the thread local flag in this case, since we'll never
352 // get a chance to restore it.
353 bool handler_noreturn = (handler.sc_flags & SIGCHAIN_ALLOW_NORETURN);
354 sigset_t previous_mask;
355 linked_sigprocmask(SIG_SETMASK, &handler.sc_mask, &previous_mask);
356
357 ScopedHandlingSignal restorer;
358 if (!handler_noreturn) {
359 SetHandlingSignal(true);
360 }
361
362 if (handler.sc_sigaction(signo, siginfo, ucontext_raw)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700363 return;
364 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700365
366 linked_sigprocmask(SIG_SETMASK, &previous_mask, nullptr);
Josh Gao85a78cf2017-03-20 16:26:42 -0700367 }
368 }
369
370 // Forward to the user's signal handler.
371 int handler_flags = chains[signo].action_.sa_flags;
372 ucontext_t* ucontext = static_cast<ucontext_t*>(ucontext_raw);
Josh Gaod32d79d2018-02-26 14:29:25 -0800373#if defined(__BIONIC__)
374 sigset64_t mask;
375 sigorset(&mask, &ucontext->uc_sigmask64, &chains[signo].action_.sa_mask);
376#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700377 sigset_t mask;
378 sigorset(&mask, &ucontext->uc_sigmask, &chains[signo].action_.sa_mask);
Josh Gaod32d79d2018-02-26 14:29:25 -0800379#endif
Josh Gao04de4fe2017-05-31 20:24:51 -0700380 if (!(handler_flags & SA_NODEFER)) {
381 sigaddset(&mask, signo);
Josh Gao85a78cf2017-03-20 16:26:42 -0700382 }
Josh Gaod32d79d2018-02-26 14:29:25 -0800383
384#if defined(__BIONIC__)
385 linked_sigprocmask64(SIG_SETMASK, &mask, nullptr);
386#else
Josh Gao6b2018f2017-05-04 13:55:28 -0700387 linked_sigprocmask(SIG_SETMASK, &mask, nullptr);
Josh Gaod32d79d2018-02-26 14:29:25 -0800388#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700389
390 if ((handler_flags & SA_SIGINFO)) {
Peter Collingbournecfc6e9d2020-11-13 10:54:13 -0800391 // If the chained handler is not expecting tag bits in the fault address,
392 // mask them out now.
393#if defined(__BIONIC__)
394 if (!(handler_flags & SA_EXPOSE_TAGBITS) &&
395 (signo == SIGILL || signo == SIGFPE || signo == SIGSEGV ||
396 signo == SIGBUS || signo == SIGTRAP) &&
397 siginfo->si_code > SI_USER && siginfo->si_code < SI_KERNEL &&
398 !(signo == SIGTRAP && siginfo->si_code == TRAP_HWBKPT)) {
399 siginfo->si_addr = untag_address(siginfo->si_addr);
400 }
401#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700402 chains[signo].action_.sa_sigaction(signo, siginfo, ucontext_raw);
Jin Qian33dca562017-03-18 02:51:37 +0000403 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700404 auto handler = chains[signo].action_.sa_handler;
405 if (handler == SIG_IGN) {
406 return;
407 } else if (handler == SIG_DFL) {
sunny.kuo8bc28c22021-08-18 10:32:48 +0800408 fatal("exiting due to SIG_DFL handler for signal %d, ucontext %p", signo, ucontext);
Jin Qian33dca562017-03-18 02:51:37 +0000409 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700410 handler(signo);
Jin Qian33dca562017-03-18 02:51:37 +0000411 }
412 }
413}
414
Josh Gaod32d79d2018-02-26 14:29:25 -0800415template <typename SigactionType>
416static int __sigaction(int signal, const SigactionType* new_action,
417 SigactionType* old_action,
418 int (*linked)(int, const SigactionType*,
419 SigactionType*)) {
randy.jeong5bef0222019-05-27 10:29:09 +0900420 if (is_signal_hook_debuggable) {
421 return 0;
422 }
423
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700424 // If this signal has been claimed as a signal chain, record the user's
425 // action but don't pass it on to the kernel.
426 // Note that we check that the signal number is in range here. An out of range signal
427 // number should behave exactly as the libc sigaction.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800428 if (signal <= 0 || signal >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700429 errno = EINVAL;
430 return -1;
431 }
432
Josh Gao9d631dd2017-03-23 20:04:58 -0700433 if (chains[signal].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800434 SigactionType saved_action = chains[signal].GetAction<SigactionType>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700435 if (new_action != nullptr) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700436 chains[signal].SetAction(new_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700437 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700438 if (old_action != nullptr) {
Dmitriy Ivanovc01683b2015-01-06 14:55:26 -0800439 *old_action = saved_action;
440 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700441 return 0;
442 }
443
444 // Will only get here if the signal chain has not been claimed. We want
445 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gaod32d79d2018-02-26 14:29:25 -0800446 return linked(signal, new_action, old_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700447}
448
Josh Gaod32d79d2018-02-26 14:29:25 -0800449extern "C" int sigaction(int signal, const struct sigaction* new_action,
450 struct sigaction* old_action) {
451 InitializeSignalChain();
452 return __sigaction(signal, new_action, old_action, linked_sigaction);
453}
454
455#if defined(__BIONIC__)
456extern "C" int sigaction64(int signal, const struct sigaction64* new_action,
457 struct sigaction64* old_action) {
458 InitializeSignalChain();
459 return __sigaction(signal, new_action, old_action, linked_sigaction64);
460}
461#endif
462
Josh Gao85a78cf2017-03-20 16:26:42 -0700463extern "C" sighandler_t signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700464 InitializeSignalChain();
465
Josh Gao07d7a5d2018-02-26 14:12:34 -0800466 if (signo <= 0 || signo >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700467 errno = EINVAL;
468 return SIG_ERR;
469 }
470
471 struct sigaction sa = {};
Dave Allison91a83662014-08-28 16:12:40 -0700472 sigemptyset(&sa.sa_mask);
473 sa.sa_handler = handler;
Josh Gao85a78cf2017-03-20 16:26:42 -0700474 sa.sa_flags = SA_RESTART | SA_ONSTACK;
Dave Allison91a83662014-08-28 16:12:40 -0700475 sighandler_t oldhandler;
476
477 // If this signal has been claimed as a signal chain, record the user's
478 // action but don't pass it on to the kernel.
Josh Gao85a78cf2017-03-20 16:26:42 -0700479 if (chains[signo].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800480 oldhandler = reinterpret_cast<sighandler_t>(
481 chains[signo].GetAction<struct sigaction>().sa_handler);
Josh Gao85a78cf2017-03-20 16:26:42 -0700482 chains[signo].SetAction(&sa);
Dave Allison91a83662014-08-28 16:12:40 -0700483 return oldhandler;
484 }
485
486 // Will only get here if the signal chain has not been claimed. We want
487 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gao85a78cf2017-03-20 16:26:42 -0700488 if (linked_sigaction(signo, &sa, &sa) == -1) {
Dave Allison91a83662014-08-28 16:12:40 -0700489 return SIG_ERR;
490 }
491
492 return reinterpret_cast<sighandler_t>(sa.sa_handler);
493}
494
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700495#if !defined(__LP64__)
Josh Gao85a78cf2017-03-20 16:26:42 -0700496extern "C" sighandler_t bsd_signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700497 InitializeSignalChain();
498
Josh Gao85a78cf2017-03-20 16:26:42 -0700499 return signal(signo, handler);
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700500}
501#endif
502
Josh Gaod32d79d2018-02-26 14:29:25 -0800503template <typename SigsetType>
504int __sigprocmask(int how, const SigsetType* new_set, SigsetType* old_set,
505 int (*linked)(int, const SigsetType*, SigsetType*)) {
Josh Gao90444552017-03-20 14:58:25 -0700506 // When inside a signal handler, forward directly to the actual sigprocmask.
Josh Gao99875e92017-04-17 15:58:36 -0700507 if (GetHandlingSignal()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800508 return linked(how, new_set, old_set);
Josh Gao90444552017-03-20 14:58:25 -0700509 }
510
Josh Gaod32d79d2018-02-26 14:29:25 -0800511 const SigsetType* new_set_ptr = new_set;
512 SigsetType tmpset;
513 if (new_set != nullptr) {
514 tmpset = *new_set;
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700515
Josh Gaof74caac2018-02-26 14:13:52 -0800516 if (how == SIG_BLOCK || how == SIG_SETMASK) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700517 // Don't allow claimed signals in the mask. If a signal chain has been claimed
518 // we can't allow the user to block that signal.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800519 for (int i = 1; i < _NSIG; ++i) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700520 if (chains[i].IsClaimed() && sigismember(&tmpset, i)) {
Dmitriy Ivanova3164b92015-04-01 11:08:45 -0700521 sigdelset(&tmpset, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700522 }
523 }
524 }
525 new_set_ptr = &tmpset;
526 }
527
Josh Gaod32d79d2018-02-26 14:29:25 -0800528 return linked(how, new_set_ptr, old_set);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700529}
Dave Allisoncefcea82014-09-16 10:01:01 -0700530
Josh Gaod32d79d2018-02-26 14:29:25 -0800531extern "C" int sigprocmask(int how, const sigset_t* new_set,
532 sigset_t* old_set) {
533 InitializeSignalChain();
534 return __sigprocmask(how, new_set, old_set, linked_sigprocmask);
535}
536
537#if defined(__BIONIC__)
538extern "C" int sigprocmask64(int how, const sigset64_t* new_set,
539 sigset64_t* old_set) {
540 InitializeSignalChain();
541 return __sigprocmask(how, new_set, old_set, linked_sigprocmask64);
542}
543#endif
544
Josh Gao6b2018f2017-05-04 13:55:28 -0700545extern "C" void AddSpecialSignalHandlerFn(int signal, SigchainAction* sa) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700546 InitializeSignalChain();
547
Josh Gao85a78cf2017-03-20 16:26:42 -0700548 if (signal <= 0 || signal >= _NSIG) {
549 fatal("Invalid signal %d", signal);
550 }
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700551
552 // Set the managed_handler.
Josh Gao6b2018f2017-05-04 13:55:28 -0700553 chains[signal].AddSpecialHandler(sa);
Josh Gao85a78cf2017-03-20 16:26:42 -0700554 chains[signal].Claim(signal);
555}
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700556
Josh Gao6b2018f2017-05-04 13:55:28 -0700557extern "C" void RemoveSpecialSignalHandlerFn(int signal, bool (*fn)(int, siginfo_t*, void*)) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700558 InitializeSignalChain();
559
Josh Gao85a78cf2017-03-20 16:26:42 -0700560 if (signal <= 0 || signal >= _NSIG) {
561 fatal("Invalid signal %d", signal);
562 }
563
564 chains[signal].RemoveSpecialHandler(fn);
565}
566
567extern "C" void EnsureFrontOfChain(int signal) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700568 InitializeSignalChain();
569
Josh Gao85a78cf2017-03-20 16:26:42 -0700570 if (signal <= 0 || signal >= _NSIG) {
571 fatal("Invalid signal %d", signal);
572 }
573
574 // Read the current action without looking at the chain, it should be the expected action.
Josh Gao3bef5272018-09-04 14:33:23 -0700575#if defined(__BIONIC__)
576 struct sigaction64 current_action;
577 linked_sigaction64(signal, nullptr, &current_action);
578#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700579 struct sigaction current_action;
Josh Gao85a78cf2017-03-20 16:26:42 -0700580 linked_sigaction(signal, nullptr, &current_action);
Josh Gao3bef5272018-09-04 14:33:23 -0700581#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700582
Josh Gao85a78cf2017-03-20 16:26:42 -0700583 // If the sigactions don't match then we put the current action on the chain and make ourself as
584 // the main action.
585 if (current_action.sa_sigaction != SignalChain::Handler) {
586 log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction);
587 chains[signal].Register(signal);
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700588 }
589}
590
randy.jeong5bef0222019-05-27 10:29:09 +0900591extern "C" void SkipAddSignalHandler(bool value) {
592 is_signal_hook_debuggable = value;
593}
594
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700595} // namespace art
596