blob: b8ab51b6292fd631e46427428fb9770c0dad7581 [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
Bilyan Borisovbb661c02016-04-04 16:27:32 +010017#ifdef ART_TARGET_ANDROID
Dave Allisonf4b80bc2014-05-14 15:41:25 -070018#include <android/log.h>
Dave Allison69dfe512014-07-11 17:11:58 +000019#else
20#include <stdarg.h>
21#include <iostream>
22#endif
23
Dave Allisonf4b80bc2014-05-14 15:41:25 -070024#include <dlfcn.h>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070025#include <errno.h>
Josh Gao99875e92017-04-17 15:58:36 -070026#include <pthread.h>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070027#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070030#include <string.h>
Josh Gao85a78cf2017-03-20 16:26:42 -070031
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070032#include <initializer_list>
Josh Gao99875e92017-04-17 15:58:36 -070033#include <mutex>
Josh Gao85a78cf2017-03-20 16:26:42 -070034#include <utility>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070035
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
Josh Gaodd241ae2017-03-20 14:17:23 -070046#include <ucontext.h>
47
Josh Gao85a78cf2017-03-20 16:26:42 -070048// libsigchain provides an interception layer for signal handlers, to allow ART and others to give
49// their signal handlers the first stab at handling signals before passing them on to user code.
50//
51// It implements wrapper functions for signal, sigaction, and sigprocmask, and a handler that
52// forwards signals appropriately.
53//
54// In our handler, we start off with all signals blocked, fetch the original signal mask from the
55// passed in ucontext, and then adjust our signal mask appropriately for the user handler.
56//
57// It's somewhat tricky for us to properly handle some flag cases:
58// SA_NOCLDSTOP and SA_NOCLDWAIT: shouldn't matter, we don't have special handlers for SIGCHLD.
59// SA_NODEFER: unimplemented, we can manually change the signal mask appropriately.
60// ~SA_ONSTACK: always silently enable this
61// SA_RESETHAND: unimplemented, but we can probably do this?
62// ~SA_RESTART: unimplemented, maybe we can reserve an RT signal, register an empty handler that
63// doesn't have SA_RESTART, and raise the signal to avoid restarting syscalls that are
64// expected to be interrupted?
Dave Allisonf4b80bc2014-05-14 15:41:25 -070065
Dave Allisonf4b80bc2014-05-14 15:41:25 -070066static void log(const char* format, ...) {
67 char buf[256];
68 va_list ap;
69 va_start(ap, format);
70 vsnprintf(buf, sizeof(buf), format, ap);
Bilyan Borisovbb661c02016-04-04 16:27:32 +010071#ifdef ART_TARGET_ANDROID
Dave Allisonf4b80bc2014-05-14 15:41:25 -070072 __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf);
Dave Allison69dfe512014-07-11 17:11:58 +000073#else
74 std::cout << buf << "\n";
75#endif
Dave Allisonf4b80bc2014-05-14 15:41:25 -070076 va_end(ap);
77}
78
Josh Gao85a78cf2017-03-20 16:26:42 -070079#define fatal(...) log(__VA_ARGS__); abort()
Josh Gao7600fa92017-03-15 17:40:42 -070080
Josh Gao85a78cf2017-03-20 16:26:42 -070081static int sigorset(sigset_t* dest, sigset_t* left, sigset_t* right) {
82 sigemptyset(dest);
83 for (size_t i = 0; i < sizeof(sigset_t) * CHAR_BIT; ++i) {
84 if (sigismember(left, i) == 1 || sigismember(right, i) == 1) {
85 sigaddset(dest, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -070086 }
Josh Gao85a78cf2017-03-20 16:26:42 -070087 }
88 return 0;
89}
90
91namespace art {
92
93static decltype(&sigaction) linked_sigaction;
94static decltype(&sigprocmask) linked_sigprocmask;
Josh Gao99875e92017-04-17 15:58:36 -070095
Josh Gaofd4d0d32017-04-26 19:09:47 -070096__attribute__((constructor)) static void InitializeSignalChain() {
97 static std::once_flag once;
98 std::call_once(once, []() {
99 void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction");
100 if (linked_sigaction_sym == nullptr) {
101 linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction");
102 if (linked_sigaction_sym == nullptr ||
103 linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) {
104 fatal("Unable to find next sigaction in signal chain");
105 }
106 }
107
108 void* linked_sigprocmask_sym = dlsym(RTLD_NEXT, "sigprocmask");
109 if (linked_sigprocmask_sym == nullptr) {
110 linked_sigprocmask_sym = dlsym(RTLD_DEFAULT, "sigprocmask");
111 if (linked_sigprocmask_sym == nullptr ||
112 linked_sigprocmask_sym == reinterpret_cast<void*>(sigprocmask)) {
113 fatal("Unable to find next sigprocmask in signal chain");
114 }
115 }
116
117 linked_sigaction =
118 reinterpret_cast<decltype(linked_sigaction)>(linked_sigaction_sym);
119 linked_sigprocmask =
120 reinterpret_cast<decltype(linked_sigprocmask)>(linked_sigprocmask_sym);
121 });
122}
123
124
Josh Gao99875e92017-04-17 15:58:36 -0700125static pthread_key_t GetHandlingSignalKey() {
126 static pthread_key_t key;
127 static std::once_flag once;
128 std::call_once(once, []() {
129 int rc = pthread_key_create(&key, nullptr);
130 if (rc != 0) {
131 fatal("failed to create sigchain pthread key: %s", strerror(rc));
132 }
133 });
134 return key;
135}
136
137static bool GetHandlingSignal() {
138 void* result = pthread_getspecific(GetHandlingSignalKey());
139 return reinterpret_cast<uintptr_t>(result);
140}
141
142static void SetHandlingSignal(bool value) {
143 pthread_setspecific(GetHandlingSignalKey(),
144 reinterpret_cast<void*>(static_cast<uintptr_t>(value)));
145}
146
147class ScopedHandlingSignal {
148 public:
149 ScopedHandlingSignal() : original_value_(GetHandlingSignal()) {
150 }
151
152 ~ScopedHandlingSignal() {
153 SetHandlingSignal(original_value_);
154 }
155
156 private:
157 bool original_value_;
158};
Josh Gao85a78cf2017-03-20 16:26:42 -0700159
160class SignalChain {
161 public:
162 SignalChain() : claimed_(false) {
Jin Qian33dca562017-03-18 02:51:37 +0000163 }
164
Josh Gao85a78cf2017-03-20 16:26:42 -0700165 bool IsClaimed() {
166 return claimed_;
167 }
168
169 void Claim(int signo) {
170 if (!claimed_) {
171 Register(signo);
172 claimed_ = true;
Jin Qian33dca562017-03-18 02:51:37 +0000173 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700174 }
175
176 // Register the signal chain with the kernel if needed.
177 void Register(int signo) {
178 struct sigaction handler_action = {};
179 handler_action.sa_sigaction = SignalChain::Handler;
180 handler_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
181 sigfillset(&handler_action.sa_mask);
182 linked_sigaction(signo, &handler_action, &action_);
183 }
184
185 void SetAction(const struct sigaction* action) {
186 action_ = *action;
187 }
188
189 struct sigaction GetAction() {
190 return action_;
191 }
192
Josh Gao6b2018f2017-05-04 13:55:28 -0700193 void AddSpecialHandler(SigchainAction* sa) {
194 for (SigchainAction& slot : special_handlers_) {
195 if (slot.sc_sigaction == nullptr) {
196 slot = *sa;
Josh Gao85a78cf2017-03-20 16:26:42 -0700197 return;
198 }
199 }
200
201 fatal("too many special signal handlers");
202 }
203
Josh Gao6b2018f2017-05-04 13:55:28 -0700204 void RemoveSpecialHandler(bool (*fn)(int, siginfo_t*, void*)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700205 // This isn't thread safe, but it's unlikely to be a real problem.
206 size_t len = sizeof(special_handlers_)/sizeof(*special_handlers_);
207 for (size_t i = 0; i < len; ++i) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700208 if (special_handlers_[i].sc_sigaction == fn) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700209 for (size_t j = i; j < len - 1; ++j) {
210 special_handlers_[j] = special_handlers_[j + 1];
211 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700212 special_handlers_[len - 1].sc_sigaction = nullptr;
Josh Gao85a78cf2017-03-20 16:26:42 -0700213 return;
214 }
215 }
216
217 fatal("failed to find special handler to remove");
218 }
219
220
221 static void Handler(int signo, siginfo_t* siginfo, void*);
222
223 private:
224 bool claimed_;
225 struct sigaction action_;
Josh Gao6b2018f2017-05-04 13:55:28 -0700226 SigchainAction special_handlers_[2];
Josh Gao85a78cf2017-03-20 16:26:42 -0700227};
228
229static SignalChain chains[_NSIG];
230
Josh Gao85a78cf2017-03-20 16:26:42 -0700231void SignalChain::Handler(int signo, siginfo_t* siginfo, void* ucontext_raw) {
232 // Try the special handlers first.
233 // 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 -0700234 if (!GetHandlingSignal()) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700235 for (const auto& handler : chains[signo].special_handlers_) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700236 if (handler.sc_sigaction == nullptr) {
237 break;
238 }
239
240 // The native bridge signal handler might not return.
241 // Avoid setting the thread local flag in this case, since we'll never
242 // get a chance to restore it.
243 bool handler_noreturn = (handler.sc_flags & SIGCHAIN_ALLOW_NORETURN);
244 sigset_t previous_mask;
245 linked_sigprocmask(SIG_SETMASK, &handler.sc_mask, &previous_mask);
246
247 ScopedHandlingSignal restorer;
248 if (!handler_noreturn) {
249 SetHandlingSignal(true);
250 }
251
252 if (handler.sc_sigaction(signo, siginfo, ucontext_raw)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700253 return;
254 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700255
256 linked_sigprocmask(SIG_SETMASK, &previous_mask, nullptr);
Josh Gao85a78cf2017-03-20 16:26:42 -0700257 }
258 }
259
260 // Forward to the user's signal handler.
261 int handler_flags = chains[signo].action_.sa_flags;
262 ucontext_t* ucontext = static_cast<ucontext_t*>(ucontext_raw);
263 sigset_t mask;
264 sigorset(&mask, &ucontext->uc_sigmask, &chains[signo].action_.sa_mask);
Josh Gao04de4fe2017-05-31 20:24:51 -0700265 if (!(handler_flags & SA_NODEFER)) {
266 sigaddset(&mask, signo);
Josh Gao85a78cf2017-03-20 16:26:42 -0700267 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700268 linked_sigprocmask(SIG_SETMASK, &mask, nullptr);
Josh Gao85a78cf2017-03-20 16:26:42 -0700269
270 if ((handler_flags & SA_SIGINFO)) {
271 chains[signo].action_.sa_sigaction(signo, siginfo, ucontext_raw);
Jin Qian33dca562017-03-18 02:51:37 +0000272 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700273 auto handler = chains[signo].action_.sa_handler;
274 if (handler == SIG_IGN) {
275 return;
276 } else if (handler == SIG_DFL) {
Josh Gaofb539a42017-03-20 16:23:41 -0700277 fatal("exiting due to SIG_DFL handler for signal %d", signo);
Jin Qian33dca562017-03-18 02:51:37 +0000278 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700279 handler(signo);
Jin Qian33dca562017-03-18 02:51:37 +0000280 }
281 }
282}
283
Dmitriy Ivanovf57874d2014-10-07 13:43:23 -0700284extern "C" int sigaction(int signal, const struct sigaction* new_action, struct sigaction* old_action) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700285 InitializeSignalChain();
286
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700287 // If this signal has been claimed as a signal chain, record the user's
288 // action but don't pass it on to the kernel.
289 // Note that we check that the signal number is in range here. An out of range signal
290 // number should behave exactly as the libc sigaction.
Josh Gao85a78cf2017-03-20 16:26:42 -0700291 if (signal < 0 || signal >= _NSIG) {
292 errno = EINVAL;
293 return -1;
294 }
295
Josh Gao9d631dd2017-03-23 20:04:58 -0700296 if (chains[signal].IsClaimed()) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700297 struct sigaction saved_action = chains[signal].GetAction();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700298 if (new_action != nullptr) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700299 chains[signal].SetAction(new_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700300 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700301 if (old_action != nullptr) {
Dmitriy Ivanovc01683b2015-01-06 14:55:26 -0800302 *old_action = saved_action;
303 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700304 return 0;
305 }
306
307 // Will only get here if the signal chain has not been claimed. We want
308 // to pass the sigaction on to the kernel via the real sigaction in libc.
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700309 return linked_sigaction(signal, new_action, old_action);
310}
311
Josh Gao85a78cf2017-03-20 16:26:42 -0700312extern "C" sighandler_t signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700313 InitializeSignalChain();
314
Josh Gao85a78cf2017-03-20 16:26:42 -0700315 if (signo < 0 || signo > _NSIG) {
316 errno = EINVAL;
317 return SIG_ERR;
318 }
319
320 struct sigaction sa = {};
Dave Allison91a83662014-08-28 16:12:40 -0700321 sigemptyset(&sa.sa_mask);
322 sa.sa_handler = handler;
Josh Gao85a78cf2017-03-20 16:26:42 -0700323 sa.sa_flags = SA_RESTART | SA_ONSTACK;
Dave Allison91a83662014-08-28 16:12:40 -0700324 sighandler_t oldhandler;
325
326 // If this signal has been claimed as a signal chain, record the user's
327 // action but don't pass it on to the kernel.
Josh Gao85a78cf2017-03-20 16:26:42 -0700328 if (chains[signo].IsClaimed()) {
329 oldhandler = reinterpret_cast<sighandler_t>(chains[signo].GetAction().sa_handler);
330 chains[signo].SetAction(&sa);
Dave Allison91a83662014-08-28 16:12:40 -0700331 return oldhandler;
332 }
333
334 // Will only get here if the signal chain has not been claimed. We want
335 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gao85a78cf2017-03-20 16:26:42 -0700336 if (linked_sigaction(signo, &sa, &sa) == -1) {
Dave Allison91a83662014-08-28 16:12:40 -0700337 return SIG_ERR;
338 }
339
340 return reinterpret_cast<sighandler_t>(sa.sa_handler);
341}
342
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700343#if !defined(__LP64__)
Josh Gao85a78cf2017-03-20 16:26:42 -0700344extern "C" sighandler_t bsd_signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700345 InitializeSignalChain();
346
Josh Gao85a78cf2017-03-20 16:26:42 -0700347 return signal(signo, handler);
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700348}
349#endif
350
Dmitriy Ivanovf57874d2014-10-07 13:43:23 -0700351extern "C" int sigprocmask(int how, const sigset_t* bionic_new_set, sigset_t* bionic_old_set) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700352 InitializeSignalChain();
353
Josh Gao90444552017-03-20 14:58:25 -0700354 // When inside a signal handler, forward directly to the actual sigprocmask.
Josh Gao99875e92017-04-17 15:58:36 -0700355 if (GetHandlingSignal()) {
Josh Gao90444552017-03-20 14:58:25 -0700356 return linked_sigprocmask(how, bionic_new_set, bionic_old_set);
357 }
358
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700359 const sigset_t* new_set_ptr = bionic_new_set;
360 sigset_t tmpset;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700361 if (bionic_new_set != nullptr) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700362 tmpset = *bionic_new_set;
363
364 if (how == SIG_BLOCK) {
365 // Don't allow claimed signals in the mask. If a signal chain has been claimed
366 // we can't allow the user to block that signal.
367 for (int i = 0 ; i < _NSIG; ++i) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700368 if (chains[i].IsClaimed() && sigismember(&tmpset, i)) {
Dmitriy Ivanova3164b92015-04-01 11:08:45 -0700369 sigdelset(&tmpset, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700370 }
371 }
372 }
373 new_set_ptr = &tmpset;
374 }
375
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700376 return linked_sigprocmask(how, new_set_ptr, bionic_old_set);
377}
Dave Allisoncefcea82014-09-16 10:01:01 -0700378
Josh Gao6b2018f2017-05-04 13:55:28 -0700379extern "C" void AddSpecialSignalHandlerFn(int signal, SigchainAction* sa) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700380 InitializeSignalChain();
381
Josh Gao85a78cf2017-03-20 16:26:42 -0700382 if (signal <= 0 || signal >= _NSIG) {
383 fatal("Invalid signal %d", signal);
384 }
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700385
386 // Set the managed_handler.
Josh Gao6b2018f2017-05-04 13:55:28 -0700387 chains[signal].AddSpecialHandler(sa);
Josh Gao85a78cf2017-03-20 16:26:42 -0700388 chains[signal].Claim(signal);
389}
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700390
Josh Gao6b2018f2017-05-04 13:55:28 -0700391extern "C" void RemoveSpecialSignalHandlerFn(int signal, bool (*fn)(int, siginfo_t*, void*)) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700392 InitializeSignalChain();
393
Josh Gao85a78cf2017-03-20 16:26:42 -0700394 if (signal <= 0 || signal >= _NSIG) {
395 fatal("Invalid signal %d", signal);
396 }
397
398 chains[signal].RemoveSpecialHandler(fn);
399}
400
401extern "C" void EnsureFrontOfChain(int signal) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700402 InitializeSignalChain();
403
Josh Gao85a78cf2017-03-20 16:26:42 -0700404 if (signal <= 0 || signal >= _NSIG) {
405 fatal("Invalid signal %d", signal);
406 }
407
408 // Read the current action without looking at the chain, it should be the expected action.
409 struct sigaction current_action;
Josh Gao85a78cf2017-03-20 16:26:42 -0700410 linked_sigaction(signal, nullptr, &current_action);
Josh Gaofd4d0d32017-04-26 19:09:47 -0700411
Josh Gao85a78cf2017-03-20 16:26:42 -0700412 // If the sigactions don't match then we put the current action on the chain and make ourself as
413 // the main action.
414 if (current_action.sa_sigaction != SignalChain::Handler) {
415 log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction);
416 chains[signal].Register(signal);
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700417 }
418}
419
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700420} // namespace art
421