v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
handler-inside-win.cc
Go to the documentation of this file.
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
27
28#include <windows.h>
29
32
33#ifdef V8_TRAP_HANDLER_VIA_SIMULATOR
35#endif
36
37namespace v8 {
38namespace internal {
39namespace trap_handler {
40
41#if V8_TRAP_HANDLER_SUPPORTED
42
43// The below struct needed to access the offset in the Thread Environment Block
44// to see if the thread local storage for the thread has been allocated yet.
45//
46// The ThreadLocalStorage pointer is located 12 pointers into the TEB (i.e. at
47// offset 0x58 for 64-bit platforms, and 0x2c for 32-bit platforms). This is
48// true for x64, x86, ARM, and ARM64 platforms (see the header files in the SDK
49// named ksamd64.inc, ks386.inc, ksarm.h, and ksarm64.h respectively).
50//
51// These offsets are baked into compiled binaries, so can never be changed for
52// backwards compatibility reasons.
53struct TEB {
54 PVOID reserved[11];
55 PVOID thread_local_storage_pointer;
56};
57
58#ifdef V8_TRAP_HANDLER_VIA_SIMULATOR
59// This is the address where we continue on a failed "ProbeMemory". It's defined
60// in "handler-outside-simulator.cc".
61extern char probe_memory_continuation[] asm(
62 "v8_simulator_probe_memory_continuation");
63#endif // V8_TRAP_HANDLER_VIA_SIMULATOR
64
65bool TryHandleWasmTrap(EXCEPTION_POINTERS* exception) {
66 // VectoredExceptionHandlers need extreme caution. Do as little as possible
67 // to determine if the exception should be handled or not. Exceptions can be
68 // thrown very early in a threads life, before the thread has even completed
69 // initializing. As a demonstrative example, there was a bug (#8966) where an
70 // exception would be raised before the thread local copy of the
71 // "__declspec(thread)" variables had been allocated, the handler tried to
72 // access the thread-local "g_thread_in_wasm_code", which would then raise
73 // another exception, and an infinite loop ensued.
74
75 // First ensure this is an exception type of interest
76 if (exception->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION) {
77 return false;
78 }
79
80 // See if thread-local storage for __declspec(thread) variables has been
81 // allocated yet. This pointer is initially null in the TEB until the
82 // loader has completed allocating the memory for thread_local variables
83 // and copy constructed their initial values. (Note: Any functions that
84 // need to run to initialize values may not have run yet, but that is not
85 // the case for any thread_locals used here).
86 TEB* pteb = reinterpret_cast<TEB*>(NtCurrentTeb());
87 if (!pteb->thread_local_storage_pointer) return false;
88
89 // Now safe to run more advanced logic, which may access thread_locals
90 // Ensure the faulting thread was actually running Wasm code.
91 if (!IsThreadInWasm()) return false;
92
93 // Clear g_thread_in_wasm_code, primarily to protect against nested faults.
94 // The only path that resets the flag to true is if we find a landing pad (in
95 // which case this function returns true). Otherwise we leave the flag unset
96 // since we do not return to wasm code.
98
99 const EXCEPTION_RECORD* record = exception->ExceptionRecord;
100
101 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(record->ExceptionAddress);
102
103#ifdef V8_TRAP_HANDLER_VIA_SIMULATOR
104 // Only handle signals triggered by the load in {ProbeMemory}.
105 if (fault_addr != reinterpret_cast<uintptr_t>(&ProbeMemory)) return false;
106
107 // The simulated ip will be in the second parameter register (%rdx).
108 uintptr_t simulated_ip = exception->ContextRecord->Rdx;
109 if (!IsFaultAddressCovered(simulated_ip)) return false;
110
111 exception->ContextRecord->Rax = gLandingPad;
112 // The fault_address that is set in non-simulator builds here is set in the
113 // simulator directly.
114 // Continue at the memory probing continuation.
115 exception->ContextRecord->Rip =
116 reinterpret_cast<uintptr_t>(&probe_memory_continuation);
117#else
118 if (!IsFaultAddressCovered(fault_addr)) return false;
119
121 // Tell the caller to return to the landing pad.
122#if V8_HOST_ARCH_X64
123 exception->ContextRecord->Rip = gLandingPad;
124 exception->ContextRecord->R10 = fault_addr;
125#elif V8_HOST_ARCH_ARM64
126 exception->ContextRecord->Pc = gLandingPad;
127 exception->ContextRecord->X16 = fault_addr;
128#else
129#error Unsupported architecture
130#endif // V8_HOST_ARCH_X64
131#endif // V8_TRAP_HANDLER_VIA_SIMULATOR
132 // We will return to wasm code, so restore the g_thread_in_wasm_code flag.
134 return true;
135}
136
137LONG HandleWasmTrap(EXCEPTION_POINTERS* exception) {
138 if (TryHandleWasmTrap(exception)) {
139 return EXCEPTION_CONTINUE_EXECUTION;
140 }
141 return EXCEPTION_CONTINUE_SEARCH;
142}
143
144#endif
145
146} // namespace trap_handler
147} // namespace internal
148} // namespace v8
DurationRecord record
bool IsFaultAddressCovered(uintptr_t fault_addr)
TH_DISABLE_ASAN bool TryHandleWasmTrap(EXCEPTION_POINTERS *exception)
thread_local int g_thread_in_wasm_code
LONG WINAPI HandleWasmTrap(EXCEPTION_POINTERS *exception)
std::atomic< uintptr_t > gLandingPad
TH_DISABLE_ASAN bool IsThreadInWasm()
#define TH_DCHECK(condition)
long LONG
void * PVOID