1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // PLEASE READ BEFORE CHANGING THIS FILE!
6 //
7 // This file implements the support code for the out of bounds signal handler.
8 // Nothing in here actually runs in the signal handler, but the code here
9 // manipulates data structures used by the signal handler so we still need to be
10 // careful. In order to minimize this risk, here are some rules to follow.
11 //
12 // 1. Avoid introducing new external dependencies. The files in src/trap-handler
13 // should be as self-contained as possible to make it easy to audit the code.
14 //
15 // 2. Any changes must be reviewed by someone from the crash reporting
16 // or security team. Se OWNERS for suggested reviewers.
17 //
18 // For more information, see https://goo.gl/yMeyUY.
19 //
20 // For the code that runs in the signal handler itself, see handler-inside.cc.
21
22 #include <signal.h>
23
24 #include <cstdio>
25
26 #include "src/trap-handler/handler-inside-posix.h"
27 #include "src/trap-handler/trap-handler-internal.h"
28
29 namespace v8 {
30 namespace internal {
31 namespace trap_handler {
32
33 #if V8_TRAP_HANDLER_SUPPORTED
34 namespace {
35 struct sigaction g_old_handler;
36
37 // When using the default signal handler, we save the old one to restore in case
38 // V8 chooses not to handle the signal.
39 bool g_is_default_signal_handler_registered;
40
41 } // namespace
42
RegisterDefaultTrapHandler()43 bool RegisterDefaultTrapHandler() {
44 TH_CHECK(!g_is_default_signal_handler_registered);
45
46 struct sigaction action;
47 action.sa_sigaction = HandleSignal;
48 action.sa_flags = SA_SIGINFO;
49 sigemptyset(&action.sa_mask);
50 // {sigaction} installs a new custom segfault handler. On success, it returns
51 // 0. If we get a nonzero value, we report an error to the caller by returning
52 // false.
53 if (sigaction(kOobSignal, &action, &g_old_handler) != 0) {
54 return false;
55 }
56
57 // Sanitizers often prevent us from installing our own signal handler. Attempt
58 // to detect this and if so, refuse to enable trap handling.
59 //
60 // TODO(chromium:830894): Remove this once all bots support custom signal
61 // handlers.
62 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
63 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
64 defined(UNDEFINED_SANITIZER)
65 struct sigaction installed_handler;
66 TH_CHECK(sigaction(kOobSignal, NULL, &installed_handler) == 0);
67 // If the installed handler does not point to HandleSignal, then
68 // allow_user_segv_handler is 0.
69 if (installed_handler.sa_sigaction != HandleSignal) {
70 printf(
71 "WARNING: sanitizers are preventing signal handler installation. "
72 "Trap handlers are disabled.\n");
73 return false;
74 }
75 #endif
76
77 g_is_default_signal_handler_registered = true;
78 return true;
79 }
80
RemoveTrapHandler()81 void RemoveTrapHandler() {
82 if (g_is_default_signal_handler_registered) {
83 if (sigaction(kOobSignal, &g_old_handler, nullptr) == 0) {
84 g_is_default_signal_handler_registered = false;
85 }
86 }
87 }
88 #endif // V8_TRAP_HANDLER_SUPPORTED
89
90 } // namespace trap_handler
91 } // namespace internal
92 } // namespace v8
93