Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(363)

Side by Side Diff: src/trap-handler/handler-inside.cc

Issue 2371833007: [wasm] Initial signal handler (Closed)
Patch Set: Restore signal mask at the right place Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
ahaas 2017/02/20 09:27:24 2017
Eric Holk 2017/02/23 02:16:56 Done.
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 out of bounds signal handler for
8 // WebAssembly. Signal handlers are notoriously difficult to get
9 // right, and getting it wrong can lead to security
10 // vulnerabilities. In order to minimize this risk, here are some
11 // rules to follow.
12 //
13 // 1. Do not introduce any new external dependencies. This file needs
14 // to be self contained so it is easy to audit everything that a
15 // signal handler might do.
16 //
17 // 2. Any changes must be reviewed by someone from the crash reporting
18 // or security team. See OWNERS for suggested reviewers.
19 //
20 // For more information, see https://goo.gl/yMeyUY.
21 //
22 // This file contains most of the code that actually runs in a signal handler
23 // context. Some additional code is used both inside and outside the signal
24 // handler. This code can be found in handler-shared.cc.
25
26 #include <signal.h>
27 #include <stddef.h>
28 #include <stdlib.h>
29
30 #include "src/trap-handler/trap-handler-internal.h"
31 #include "src/trap-handler/trap-handler.h"
32
33 namespace v8 {
34 namespace internal {
35 namespace trap_handler {
36
37 namespace {
38
39 bool IsKernelGeneratedSignal(siginfo_t* info) {
40 return info->si_code > 0 && info->si_code != SI_USER &&
41 info->si_code != SI_QUEUE && info->si_code != SI_TIMER &&
42 info->si_code != SI_ASYNCIO && info->si_code != SI_MESGQ;
43 }
44
45 #if V8_TRAP_HANDLER_SUPPORTED
46 class SigUnmaskStack {
47 public:
48 explicit SigUnmaskStack(sigset_t sigs) {
49 // TODO(eholk): consider using linux-syscall-support instead of
Mark Seaborn 2017/02/17 21:41:12 Nit: "instead of pthread_sigmask" -> "for calling
Eric Holk 2017/02/23 02:16:56 Done.
50 // pthread_sigmask.
51 pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_);
52 }
53
54 ~SigUnmaskStack() { pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); }
55
56 private:
57 sigset_t old_mask_;
58
59 // We'd normally use DISALLOW_COPY_AND_ASSIGN, but we're avoiding a
Mark Seaborn 2017/02/17 21:41:12 Nit: collapse 2 spaces into 1 after "we're"
Eric Holk 2017/02/23 02:16:56 Done.
60 // dependency on base/macros.h
61 SigUnmaskStack(const SigUnmaskStack&) = delete;
62 void operator=(const SigUnmaskStack&) = delete;
63 };
64 #endif
65 } // namespace
66
67 #if V8_TRAP_HANDLER_SUPPORTED && V8_OS_LINUX
68 bool TryHandleSignal(int signum, siginfo_t* info, ucontext_t* context) {
69 // Bail out early in case we got called for the wrong kind of signal.
70 if (signum != SIGSEGV) {
71 return false;
72 }
73
74 // Make sure the signal was generated by the kernel and not some other source.
75 if (!IsKernelGeneratedSignal(info)) {
76 return false;
77 }
78
79 // Ensure the faulting thread was actually running Wasm code.
80 if (!g_thread_in_wasm_code) {
ahaas 2017/02/20 09:27:24 Use IsThreadInWasm() instead?
Eric Holk 2017/02/23 02:16:56 Done.
81 return false;
82 }
83
84 // Clear g_thread_in_wasm_code, primarily to protect against nested faults.
85 g_thread_in_wasm_code = false;
86
87 // begin signal mask scope. We need to be sure to restore the signal mask
Mark Seaborn 2017/02/17 21:41:12 "begin" -> "Begin"
Eric Holk 2017/02/23 02:16:56 Done.
88 // before we restore the g_thread_in_wasm_code flag.
89 {
90 // Unmask the signal so that if this signal handler crashes, the crash will
91 // be
Mark Seaborn 2017/02/17 21:41:12 Fix comment wrapping. (Does clang-format try to w
Eric Holk 2017/02/23 02:16:56 Done. (Apparently not... Maybe it's cautious about
92 // handled by the crash reporter. Otherwise, the process might be killed
93 // with
94 // the crash going unreported.
95 sigset_t sigs;
96 // Fortunately, sigemptyset and sigaddset are async-signal-safe according to
97 // the POSIX standard.
98 sigemptyset(&sigs);
99 sigaddset(&sigs, SIGSEGV);
100 SigUnmaskStack unmask(sigs);
101
102 uintptr_t fault_addr = context->uc_mcontext.gregs[REG_RIP];
103
104 // TODO(eholk): broad code range check
105
106 // Taking locks in a signal handler is risky because a fault in the signal
107 // handler could lead to a deadlock when attempting to acquire the lock
108 // again.
109 // We guard against this case with g_thread_in_wasm_code. The lock may only
110 // be taken when not executing Wasm code (an assert in MetadataLock's
111 // constructor ensures this). This signal handler will bail out before
112 // trying
113 // to take the lock if g_thread_in_wasm_code is not set.
Mark Seaborn 2017/02/17 21:41:12 Fix comment wrapping
Eric Holk 2017/02/23 02:16:56 Done.
114 MetadataLock lock_holder;
115
116 for (size_t i = 0; i < gNumCodeObjects; ++i) {
117 const CodeProtectionInfo* data = gCodeObjects[i];
118 if (data == nullptr) {
119 continue;
120 }
121 const uintptr_t base = reinterpret_cast<uintptr_t>(data->base);
122
123 if (fault_addr >= base && fault_addr < base + data->size) {
124 // Hurray, we found the code object. Check for protected addresses.
125 const ptrdiff_t offset = fault_addr - base;
126
127 for (unsigned i = 0; i < data->num_protected_instructions; ++i) {
128 if (data->instructions[i].instr_offset == offset) {
129 // Hurray again, we found the actual instruction. Tell the caller to
130 // return to the landing pad.
131 context->uc_mcontext.gregs[REG_RIP] =
132 data->instructions[i].landing_offset + base;
133 return true;
134 }
135 }
136 }
137 }
138 } // end signal mask scope
139
140 // If we get here, it's not a recoverable wasm fault, so we go to the next
141 // handler.
142 g_thread_in_wasm_code = true;
143 return false;
144 }
145 #endif // V8_TRAP_HANDLER_SUPPORTED && V8_OS_LINUX
146
147 #if V8_TRAP_HANDLER_SUPPORTED
148 void HandleSignal(int signum, siginfo_t* info, void* context) {
149 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
150
151 if (!TryHandleSignal(signum, info, uc)) {
152 // Since V8 didn't handle this signal, we want to re-raise the same signal.
153 // For kernel-generated SEGV signals, we do this by restoring the default
154 // SEGV handler and then returning. The fault will happen again and the
155 // usual SEGV handling will happen.
156 //
157 // We handle user-generated signals by calling raise() instead. This is for
158 // completeness. We should never actually see one of these, but just in
159 // case, we do the right thing.
160 struct sigaction action;
161 action.sa_handler = SIG_DFL;
162 sigemptyset(&action.sa_mask);
163 action.sa_flags = 0;
164 sigaction(signum, &action, nullptr);
165 if (!IsKernelGeneratedSignal(info)) {
166 raise(signum);
167 }
168 }
169 // TryHandleSignal modifies context to change where we return to.
170 }
171 #endif
172 } // namespace trap_handler
173 } // namespace internal
174 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698