V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
handler-inside-posix.cc
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 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 "src/trap-handler/handler-inside-posix.h"
27 
28 #include <signal.h>
29 
30 #ifdef V8_OS_LINUX
31 #include <ucontext.h>
32 #elif V8_OS_MACOSX
33 #include <sys/ucontext.h>
34 #endif
35 
36 #include <stddef.h>
37 #include <stdlib.h>
38 
39 #include "src/trap-handler/trap-handler-internal.h"
40 #include "src/trap-handler/trap-handler.h"
41 
42 namespace v8 {
43 namespace internal {
44 namespace trap_handler {
45 
46 bool IsKernelGeneratedSignal(siginfo_t* info) {
47  // On macOS, only `info->si_code > 0` is relevant, because macOS leaves
48  // si_code at its default of 0 for signals that don’t originate in hardware.
49  // The other conditions are only relevant for Linux.
50  return info->si_code > 0 && info->si_code != SI_USER &&
51  info->si_code != SI_QUEUE && info->si_code != SI_TIMER &&
52  info->si_code != SI_ASYNCIO && info->si_code != SI_MESGQ;
53 }
54 
56  public:
57  explicit SigUnmaskStack(sigset_t sigs) {
58  // TODO(eholk): consider using linux-syscall-support for calling this
59  // syscall.
60  pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_);
61  }
62 
63  ~SigUnmaskStack() { pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); }
64 
65  private:
66  sigset_t old_mask_;
67 
68  // We'd normally use DISALLOW_COPY_AND_ASSIGN, but we're avoiding a dependency
69  // on base/macros.h
70  SigUnmaskStack(const SigUnmaskStack&) = delete;
71  void operator=(const SigUnmaskStack&) = delete;
72 };
73 
74 bool TryHandleSignal(int signum, siginfo_t* info, void* context) {
75  // Bail out early in case we got called for the wrong kind of signal.
76 
77  if (signum != kOobSignal) {
78  return false;
79  }
80 
81  // Make sure the signal was generated by the kernel and not some other source.
82  if (!IsKernelGeneratedSignal(info)) {
83  return false;
84  }
85 
86  // Ensure the faulting thread was actually running Wasm code.
87  if (!IsThreadInWasm()) {
88  return false;
89  }
90 
91  // Clear g_thread_in_wasm_code, primarily to protect against nested faults.
92  g_thread_in_wasm_code = false;
93 
94  // Begin signal mask scope. We need to be sure to restore the signal mask
95  // before we restore the g_thread_in_wasm_code flag.
96  {
97  // Unmask the signal so that if this signal handler crashes, the crash will
98  // be handled by the crash reporter. Otherwise, the process might be killed
99  // with the crash going unreported.
100  sigset_t sigs;
101  // Fortunately, sigemptyset and sigaddset are async-signal-safe according to
102  // the POSIX standard.
103  sigemptyset(&sigs);
104  sigaddset(&sigs, SIGSEGV);
105  SigUnmaskStack unmask(sigs);
106 
107  ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
108 #if V8_OS_LINUX
109  auto* context_rip = &uc->uc_mcontext.gregs[REG_RIP];
110 #elif V8_OS_MACOSX
111  auto* context_rip = &uc->uc_mcontext->__ss.__rip;
112 #else
113 #error Unsupported platform
114 #endif
115  uintptr_t fault_addr = *context_rip;
116  uintptr_t landing_pad = 0;
117  if (TryFindLandingPad(fault_addr, &landing_pad)) {
118  // Tell the caller to return to the landing pad.
119  *context_rip = landing_pad;
120  // We will return to wasm code, so restore the g_thread_in_wasm_code flag.
121  g_thread_in_wasm_code = true;
122  return true;
123  }
124  } // end signal mask scope
125 
126  // If we get here, it's not a recoverable wasm fault, so we go to the next
127  // handler. Leave the g_thread_in_wasm_code flag unset since we do not return
128  // to wasm code.
129  return false;
130 }
131 
132 void HandleSignal(int signum, siginfo_t* info, void* context) {
133  if (!TryHandleSignal(signum, info, context)) {
134  // Since V8 didn't handle this signal, we want to re-raise the same signal.
135  // For kernel-generated SEGV signals, we do this by restoring the original
136  // SEGV handler and then returning. The fault will happen again and the
137  // usual SEGV handling will happen.
138  //
139  // We handle user-generated signals by calling raise() instead. This is for
140  // completeness. We should never actually see one of these, but just in
141  // case, we do the right thing.
142  RemoveTrapHandler();
143  if (!IsKernelGeneratedSignal(info)) {
144  raise(signum);
145  }
146  }
147  // TryHandleSignal modifies context to change where we return to.
148 }
149 
150 } // namespace trap_handler
151 } // namespace internal
152 } // namespace v8
Definition: libplatform.h:13