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

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

Issue 2371833007: [wasm] Initial signal handler (Closed)
Patch Set: Split signal handler into a separate file Created 3 years, 11 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
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.
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 // #define TRACE(...) fprintf(stderr, __VA_ARGS__)
37
38 namespace v8 {
39 namespace internal {
40 namespace trap_handler {
41
42 extern __thread bool gThreadInWasmCode;
jochen (gone - plz use gerrit) 2017/01/02 07:35:08 please use g_thread_in_wasm_code
Eric Holk 2017/01/10 23:10:48 Done.
43
44 extern size_t gNumCodeObjects;
45 extern CodeObjectData **gCodeObjects;
46
47 MetadataLock::MetadataLock() {
48 if (gThreadInWasmCode) {
49 abort();
50 }
51
52 while (spinlock_.test_and_set(std::memory_order::memory_order_acquire)) {
53 }
54 }
55
56 MetadataLock::~MetadataLock() {
57 if (gThreadInWasmCode) {
58 abort();
59 }
60
61 spinlock_.clear(std::memory_order::memory_order_release);
62 }
63
64 namespace {
65
66 class SigUnmaskStack {
67 public:
68 explicit SigUnmaskStack(int signum) {
69 sigset_t sigs;
70 sigemptyset(&sigs);
71 sigaddset(&sigs, signum);
72 pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_);
73 }
74
75 ~SigUnmaskStack() { pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); }
76
77 private:
78 sigset_t old_mask_;
79 };
jochen (gone - plz use gerrit) 2017/01/02 07:35:08 disallow copy& assign
Eric Holk 2017/01/10 23:10:48 Done.
80 } // namespace
81
82 bool MaybeHandleFault(int signum, void *siginfo, void *context) {
83 // Bail out early in case we got called for the wrong kind of signal.
84 if (signum != SIGSEGV && signum != SIGBUS) {
85 return false;
86 }
87
88 // Make sure the signal was generated by the kernel and not some other source.
89 siginfo_t *info = reinterpret_cast<siginfo_t *>(siginfo);
90 if (info->si_code <= 0 || info->si_code == SI_USER ||
91 info->si_code == SI_QUEUE || info->si_code == SI_TIMER ||
92 info->si_code == SI_ASYNCIO || info->si_code == SI_MESGQ) {
93 return false;
94 }
95
96 // Ensure the faulting thread was actually running Wasm code.
97 if (!gThreadInWasmCode) {
98 TRACE("signal handler: Thread not running WASM code; crashing\n");
99 return false;
100 }
101
102 // This isn't Wasm code anymore. At this point we were either crash or throw
103 // an exception, so we will not be running Wasm code again and there is no
104 // need to set the flag again later.
105 gThreadInWasmCode = false;
106
107 // Unmask the signal in case we fault in the signal hander.
108 SigUnmaskStack unmask(signum);
109
110 ucontext_t *uc = reinterpret_cast<ucontext_t *>(context);
111
112 // TODO(eholk): broad code range check
113 intptr_t fault_addr = uc->uc_mcontext.gregs[REG_RIP];
114
115 TRACE("Handling fault at %p\n", reinterpret_cast<void *>(fault_addr));
116
117 MetadataLock _;
jochen (gone - plz use gerrit) 2017/01/02 07:35:08 please use a proper variable name
Eric Holk 2017/01/10 23:10:48 Done.
118
119 for (size_t i = 0; i < gNumCodeObjects; ++i) {
120 const CodeObjectData *data = gCodeObjects[i];
121 if (data == nullptr) {
122 continue;
123 }
124 const intptr_t base = reinterpret_cast<intptr_t>(data->base);
125
126 TRACE(" Checking range base=%p, length = %zu\n",
127 reinterpret_cast<void *>(base), data->size);
128
129 if (fault_addr >= base &&
130 fault_addr < base + static_cast<intptr_t>(data->size)) {
131 // Hurray, we found the code object. Check for protected addresses.
132 const ptrdiff_t offset = fault_addr - base;
133
134 TRACE("Range found, offset is %td\n", offset);
135
136 for (unsigned i = 0; i < data->num_protected_instructions; ++i) {
137 TRACE(" Checking instr offset %d, landing offset %d\n",
138 data->instructions[i].instr_offset,
139 data->instructions[i].landing_offset);
140 if (data->instructions[i].instr_offset == offset) {
141 // Hurray again, we found the actual instruction. Jump to
142 // the landing pad.
143
144 uc->uc_mcontext.gregs[REG_RIP] =
145 data->instructions[i].landing_offset + base;
146 return true;
147 }
148 }
149 }
150 }
151
152 // If we get here, it's not a wasm fault, so we go to the next handler.
153 return false;
154 }
155 } // namespace trap_handler
156 } // namespace internal
157 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698