Line data Source code
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 "src/trap-handler/handler-inside-posix.h"
25 : #include "src/trap-handler/trap-handler-internal.h"
26 :
27 : namespace v8 {
28 : namespace internal {
29 : namespace trap_handler {
30 :
31 : #if V8_TRAP_HANDLER_SUPPORTED
32 : namespace {
33 : struct sigaction g_old_handler;
34 :
35 : // When using the default signal handler, we save the old one to restore in case
36 : // V8 chooses not to handle the signal.
37 : bool g_is_default_signal_handler_registered;
38 :
39 : } // namespace
40 :
41 57107 : bool RegisterDefaultTrapHandler() {
42 57107 : CHECK(!g_is_default_signal_handler_registered);
43 :
44 : struct sigaction action;
45 57107 : action.sa_sigaction = HandleSignal;
46 57107 : action.sa_flags = SA_SIGINFO;
47 57107 : sigemptyset(&action.sa_mask);
48 : // {sigaction} installs a new custom segfault handler. On success, it returns
49 : // 0. If we get a nonzero value, we report an error to the caller by returning
50 : // false.
51 57107 : if (sigaction(kOobSignal, &action, &g_old_handler) != 0) {
52 : return false;
53 : }
54 :
55 : // Sanitizers often prevent us from installing our own signal handler. Attempt
56 : // to detect this and if so, refuse to enable trap handling.
57 : //
58 : // TODO(chromium:830894): Remove this once all bots support custom signal
59 : // handlers.
60 : #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
61 : defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
62 : defined(UNDEFINED_SANITIZER)
63 : struct sigaction installed_handler;
64 : CHECK_EQ(sigaction(kOobSignal, NULL, &installed_handler), 0);
65 : // If the installed handler does not point to HandleSignal, then
66 : // allow_user_segv_handler is 0.
67 : if (installed_handler.sa_sigaction != HandleSignal) {
68 : printf(
69 : "WARNING: sanitizers are preventing signal handler installation. "
70 : "Trap handlers are disabled.\n");
71 : return false;
72 : }
73 : #endif
74 :
75 57107 : g_is_default_signal_handler_registered = true;
76 57107 : return true;
77 : }
78 :
79 18 : void RemoveTrapHandler() {
80 18 : if (g_is_default_signal_handler_registered) {
81 7 : if (sigaction(kOobSignal, &g_old_handler, nullptr) == 0) {
82 7 : g_is_default_signal_handler_registered = false;
83 : }
84 : }
85 18 : }
86 : #endif // V8_TRAP_HANDLER_SUPPORTED
87 :
88 : } // namespace trap_handler
89 : } // namespace internal
90 : } // namespace v8
|