V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
handler-inside-win.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 trap handler for
8 // WebAssembly. Exception 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 // trap 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 an exception
23 // handler context. Some additional code is used both inside and outside the
24 // trap handler. This code can be found in handler-shared.cc.
25 
26 #include "src/trap-handler/handler-inside-win.h"
27 
28 #include <windows.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 bool TryHandleWasmTrap(EXCEPTION_POINTERS* exception) {
38  // Ensure the faulting thread was actually running Wasm code.
39  if (!IsThreadInWasm()) {
40  return false;
41  }
42 
43  // Clear g_thread_in_wasm_code, primarily to protect against nested faults.
44  g_thread_in_wasm_code = false;
45 
46  const EXCEPTION_RECORD* record = exception->ExceptionRecord;
47 
48  if (record->ExceptionCode != EXCEPTION_ACCESS_VIOLATION) {
49  return false;
50  }
51 
52  uintptr_t fault_addr = reinterpret_cast<uintptr_t>(record->ExceptionAddress);
53  uintptr_t landing_pad = 0;
54 
55  if (TryFindLandingPad(fault_addr, &landing_pad)) {
56  exception->ContextRecord->Rip = landing_pad;
57  // We will return to wasm code, so restore the g_thread_in_wasm_code flag.
58  g_thread_in_wasm_code = true;
59  return true;
60  }
61 
62  // If we get here, it's not a recoverable wasm fault, so we go to the next
63  // handler. Leave the g_thread_in_wasm_code flag unset since we do not return
64  // to wasm code.
65  return false;
66 }
67 
68 LONG HandleWasmTrap(EXCEPTION_POINTERS* exception) {
69  if (TryHandleWasmTrap(exception)) {
70  return EXCEPTION_CONTINUE_EXECUTION;
71  }
72  return EXCEPTION_CONTINUE_SEARCH;
73 }
74 
75 } // namespace trap_handler
76 } // namespace internal
77 } // namespace v8
Definition: libplatform.h:13