V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
trap-handler.h
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 #ifndef V8_TRAP_HANDLER_TRAP_HANDLER_H_
6 #define V8_TRAP_HANDLER_TRAP_HANDLER_H_
7 
8 #include <stdint.h>
9 #include <stdlib.h>
10 
11 #include "src/base/build_config.h"
12 #include "src/flags.h"
13 #include "src/globals.h"
14 
15 namespace v8 {
16 namespace internal {
17 namespace trap_handler {
18 
19 // TODO(eholk): Support trap handlers on other platforms.
20 #if V8_TARGET_ARCH_X64 && V8_OS_LINUX && !V8_OS_ANDROID
21 #define V8_TRAP_HANDLER_SUPPORTED true
22 #elif V8_TARGET_ARCH_X64 && V8_OS_WIN
23 #define V8_TRAP_HANDLER_SUPPORTED true
24 #elif V8_TARGET_ARCH_X64 && V8_OS_MACOSX
25 #define V8_TRAP_HANDLER_SUPPORTED true
26 #else
27 #define V8_TRAP_HANDLER_SUPPORTED false
28 #endif
29 
31  // The offset of this instruction from the start of its code object.
32  // Wasm code never grows larger than 2GB, so uint32_t is sufficient.
33  uint32_t instr_offset;
34 
35  // The offset of the landing pad from the start of its code object.
36  //
37  // TODO(eholk): Using a single landing pad and store parameters here.
38  uint32_t landing_offset;
39 };
40 
41 const int kInvalidIndex = -1;
42 
47 int RegisterHandlerData(Address base, size_t size,
48  size_t num_protected_instructions,
49  const ProtectedInstructionData* protected_instructions);
50 
54 void ReleaseHandlerData(int index);
55 
56 #if V8_OS_WIN
57 #define THREAD_LOCAL __declspec(thread)
58 #elif V8_OS_ANDROID
59 // TODO(eholk): fix this before enabling for trap handlers for Android.
60 #define THREAD_LOCAL
61 #else
62 #define THREAD_LOCAL __thread
63 #endif
64 
65 extern bool g_is_trap_handler_enabled;
66 // Enables trap handling for WebAssembly bounds checks.
67 //
68 // use_v8_handler indicates that V8 should install its own handler
69 // rather than relying on the embedder to do it.
70 bool EnableTrapHandler(bool use_v8_handler);
71 
72 inline bool IsTrapHandlerEnabled() {
73  DCHECK_IMPLIES(g_is_trap_handler_enabled, V8_TRAP_HANDLER_SUPPORTED);
74  return g_is_trap_handler_enabled;
75 }
76 
77 extern THREAD_LOCAL int g_thread_in_wasm_code;
78 
79 // Return the address of the thread-local {g_thread_in_wasm_code} variable. This
80 // pointer can be accessed and modified as long as the thread calling this
81 // function exists. Only use if from the same thread do avoid race conditions.
82 inline int* GetThreadInWasmThreadLocalAddress() {
83  return &g_thread_in_wasm_code;
84 }
85 
86 // On Windows, asan installs its own exception handler which maps shadow
87 // memory. Since our exception handler may be executed before the asan exception
88 // handler, we have to make sure that asan shadow memory is not accessed here.
89 DISABLE_ASAN inline bool IsThreadInWasm() { return g_thread_in_wasm_code; }
90 
91 inline void SetThreadInWasm() {
92  if (IsTrapHandlerEnabled()) {
93  DCHECK(!IsThreadInWasm());
94  g_thread_in_wasm_code = true;
95  }
96 }
97 
98 inline void ClearThreadInWasm() {
99  if (IsTrapHandlerEnabled()) {
100  DCHECK(IsThreadInWasm());
101  g_thread_in_wasm_code = false;
102  }
103 }
104 
105 bool RegisterDefaultTrapHandler();
106 V8_EXPORT_PRIVATE void RemoveTrapHandler();
107 
108 size_t GetRecoveredTrapCount();
109 
110 } // namespace trap_handler
111 } // namespace internal
112 } // namespace v8
113 
114 #endif // V8_TRAP_HANDLER_TRAP_HANDLER_H_
Definition: libplatform.h:13