v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
handler-outside-posix.cc
Go to the documentation of this file.
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
28
29namespace v8 {
30namespace internal {
31namespace trap_handler {
32
33#if V8_TRAP_HANDLER_SUPPORTED
34namespace {
35struct 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.
39bool g_is_default_signal_handler_registered;
40
41} // namespace
42
44 TH_CHECK(!g_is_default_signal_handler_registered);
45
46 struct sigaction action;
47 action.sa_sigaction = HandleSignal;
48 // Use SA_ONSTACK so that iff an alternate signal stack was registered via
49 // sigaltstack, that one is used for handling the signal instead of the
50 // default stack. This can be useful if for example the stack pointer is
51 // corrupted or a stack overflow is triggered as that may cause the trap
52 // handler to crash if it runs on the default stack. We assume that other
53 // parts, e.g. Asan or the v8 sandbox testing infrastructure, will register
54 // the alternate stack if necessary.
55 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
56 sigemptyset(&action.sa_mask);
57 // {sigaction} installs a new custom segfault handler. On success, it returns
58 // 0. If we get a nonzero value, we report an error to the caller by returning
59 // false.
60 if (sigaction(kOobSignal, &action, &g_old_handler) != 0) {
61 return false;
62 }
63
64// Sanitizers often prevent us from installing our own signal handler. Attempt
65// to detect this and if so, refuse to enable trap handling.
66//
67// TODO(chromium:830894): Remove this once all bots support custom signal
68// handlers.
69#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
70 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
71 defined(UNDEFINED_SANITIZER)
72 struct sigaction installed_handler;
73 TH_CHECK(sigaction(kOobSignal, NULL, &installed_handler) == 0);
74 // If the installed handler does not point to HandleSignal, then
75 // allow_user_segv_handler is 0.
76 if (installed_handler.sa_sigaction != HandleSignal) {
77 printf(
78 "WARNING: sanitizers are preventing signal handler installation. "
79 "Trap handlers are disabled.\n");
80 return false;
81 }
82#endif
83
84 g_is_default_signal_handler_registered = true;
85 return true;
86}
87
88void RemoveTrapHandler() {
89 if (g_is_default_signal_handler_registered) {
90 if (sigaction(kOobSignal, &g_old_handler, nullptr) == 0) {
91 g_is_default_signal_handler_registered = false;
92 }
93 }
94}
95#endif // V8_TRAP_HANDLER_SUPPORTED
96
97} // namespace trap_handler
98} // namespace internal
99} // namespace v8
void HandleSignal(int signum, siginfo_t *info, void *context)
#define TH_CHECK(condition)