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

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

Issue 2371833007: [wasm] Initial signal handler (Closed)
Patch Set: Handler signal handler registration failure 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.
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
Mark Seaborn 2017/02/09 16:44:36 Linking to the design doc (the Google Doc) could b
Eric Holk 2017/02/15 02:02:45 I don't know if we have an official place, but I m
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.
Mark Mentovai 2017/02/09 17:39:41 Point to the this directory’s OWNERS file as a rem
Eric Holk 2017/02/15 02:02:45 Done.
19
20 // This file contains all of the code that actually runs in a signal handler
21 // context.
22
23 #include <signal.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <atomic>
30
31 #include "src/flags.h"
32 #include "src/trap-handler/trap-handler-internal.h"
33 #include "src/trap-handler/trap-handler.h"
34
35 #define TRACE(...)
36
37 namespace v8 {
38 namespace internal {
39 namespace trap_handler {
40
41 MetadataLock::MetadataLock() {
Mark Seaborn 2017/02/09 16:44:36 MetadataLock is the only code that runs both insid
Eric Holk 2017/02/15 02:02:45 How does handler-{inside,outside,shared}.cc sound?
Mark Seaborn 2017/02/17 21:41:12 Sounds good.
42 if (g_thread_in_wasm_code) {
43 abort();
44 }
45
46 while (spinlock_.test_and_set(std::memory_order::memory_order_acquire)) {
47 }
48 }
49
50 MetadataLock::~MetadataLock() {
51 if (g_thread_in_wasm_code) {
52 abort();
53 }
54
55 spinlock_.clear(std::memory_order::memory_order_release);
56 }
57
58 namespace {
59
60 #if V8_TRAP_HANDLER_SUPPORTED
61 class SigUnmaskStack {
62 public:
63 explicit SigUnmaskStack(sigset_t sigs) {
64 // TODO(eholk): consider using linux-syscall-support instead of
65 // pthread_sigmask.
66 pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_);
67 }
68
69 ~SigUnmaskStack() { pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); }
70
71 private:
72 sigset_t old_mask_;
73
74 SigUnmaskStack(const SigUnmaskStack&) = delete;
75 void operator=(const SigUnmaskStack&) = delete;
76 };
77 #endif
78 } // namespace
79
80 #if V8_OS_LINUX
81 bool TryHandleSignal(int signum, siginfo_t* info, ucontext_t* context) {
82 #if V8_TRAP_HANDLER_SUPPORTED
83 // Bail out early in case we got called for the wrong kind of signal.
84 if (signum != SIGSEGV && signum != SIGBUS) {
Mark Mentovai 2017/02/09 17:39:41 We decided on just SIGSEGV here and on line 112.
Eric Holk 2017/02/15 02:02:45 Done.
85 return false;
86 }
87
88 // Make sure the signal was generated by the kernel and not some other source.
89 if (info->si_code <= 0 || info->si_code == SI_USER ||
90 info->si_code == SI_QUEUE || info->si_code == SI_TIMER ||
91 info->si_code == SI_ASYNCIO || info->si_code == SI_MESGQ) {
92 return false;
93 }
94
95 // Ensure the faulting thread was actually running Wasm code.
96 if (!g_thread_in_wasm_code) {
97 TRACE("signal handler: Thread not running WASM code; crashing\n");
98 return false;
99 }
100
101 // Clear g_thread_in_wasm_code, primarily to protect against nested faults.
102 g_thread_in_wasm_code = false;
103
104 // Unmask the signal so that if this signal handler crashes, the crash will be
105 // handled by the crash reporter. Otherwise, the process might be killed with
106 // the crash going unreported.
107 sigset_t sigs;
108 // Fortunately, sigemptyset and sigaddset are async-signal-safe according to
109 // the POSIX standard.
110 sigemptyset(&sigs);
111 sigaddset(&sigs, SIGSEGV);
112 sigaddset(&sigs, SIGBUS);
113 SigUnmaskStack unmask(sigs);
114
115 uintptr_t fault_addr = context->uc_mcontext.gregs[REG_RIP];
116
117 TRACE("Handling fault at %p\n", reinterpret_cast<void*>(fault_addr));
118
119 // TODO(eholk): broad code range check
120
121 MetadataLock lock_holder;
Mark Mentovai 2017/02/09 17:39:41 Add a comment here explaining how you know that th
Eric Holk 2017/02/15 02:02:45 Done.
122
123 for (size_t i = 0; i < gNumCodeObjects; ++i) {
124 const CodeProtectionInfo* data = gCodeObjects[i];
125 if (data == nullptr) {
126 continue;
127 }
128 const uintptr_t base = reinterpret_cast<uintptr_t>(data->base);
129
130 TRACE(" Checking range base=%p, length = %zu\n",
131 reinterpret_cast<void*>(base), data->size);
132
133 if (fault_addr >= base && fault_addr < base + data->size) {
134 // Hurray, we found the code object. Check for protected addresses.
135 const ptrdiff_t offset = fault_addr - base;
136
137 TRACE("Range found, offset is %td\n", offset);
138
139 for (unsigned i = 0; i < data->num_protected_instructions; ++i) {
140 TRACE(" Checking instr offset %d, landing offset %d\n",
141 data->instructions[i].instr_offset,
142 data->instructions[i].landing_offset);
143 if (data->instructions[i].instr_offset == offset) {
144 // Hurray again, we found the actual instruction. Tell the caller to
145 // return to the landing pad.
146 context->uc_mcontext.gregs[REG_RIP] =
147 data->instructions[i].landing_offset + base;
148 return true;
149 }
150 }
151 }
152 }
153
154 // If we get here, it's not a recoverable wasm fault, so we go to the next
155 // handler.
156 g_thread_in_wasm_code = true;
157 return false;
158 #else
159 abort();
Mark Mentovai 2017/02/09 17:39:41 I don’t see how this #else abort() is ever reachab
Eric Holk 2017/02/15 02:02:45 It shouldn't ever be reachable. I'll see if I can
160 #endif // V8_TRAP_HANDLER_SUPPORTED
161 }
162 #endif // V8_OS_LINUX
163
164 namespace {
165
166 #if V8_TRAP_HANDLER_SUPPORTED
167 void HandleSignal(int signum, siginfo_t* info, void* context) {
168 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
169
170 if (!TryHandleSignal(signum, info, uc)) {
171 abort();
Mark Mentovai 2017/02/09 17:39:41 Is this really how you want things to go down, as
Eric Holk 2017/02/15 02:02:45 We want to preserve the signal and context. Done.
172 }
173 // TryHandleSignal modifies context to change where we return to.
174 }
175 #endif
176 } // namespace
177
178 bool RegisterDefaultSignalHandler() {
179 #if V8_TRAP_HANDLER_SUPPORTED
180 struct sigaction action;
181 struct sigaction old_action;
182 action.sa_sigaction = HandleSignal;
183 action.sa_flags = SA_SIGINFO;
184 sigemptyset(&action.sa_mask);
185 if (sigaction(SIGSEGV, &action, &old_action) != 0) {
Mark Mentovai 2017/02/09 17:39:41 You can give nullptr instead of old_action, since
Eric Holk 2017/02/15 02:02:45 Done.
186 return false;
187 }
188
189 return true;
190 #else
191 return false;
192 #endif
193 }
194
195 } // namespace trap_handler
196 } // namespace internal
197 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698