Line data Source code
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 :
30 : struct ProtectedInstructionData {
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 :
43 : /// Adds the handler data to the place where the trap handler will find it.
44 : ///
45 : /// This returns a number that can be used to identify the handler data to
46 : /// ReleaseHandlerData, or -1 on failure.
47 : int V8_EXPORT_PRIVATE RegisterHandlerData(
48 : Address base, size_t size, size_t num_protected_instructions,
49 : const ProtectedInstructionData* protected_instructions);
50 :
51 : /// Removes the data from the master list and frees any memory, if necessary.
52 : /// TODO(mtrofin): We can switch to using size_t for index and not need
53 : /// kInvalidIndex.
54 : void V8_EXPORT_PRIVATE 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 22041272 : 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 : V8_NOINLINE V8_EXPORT_PRIVATE int* GetThreadInWasmThreadLocalAddress();
83 :
84 : // On Windows, asan installs its own exception handler which maps shadow
85 : // memory. Since our exception handler may be executed before the asan exception
86 : // handler, we have to make sure that asan shadow memory is not accessed here.
87 2602918 : DISABLE_ASAN inline bool IsThreadInWasm() { return g_thread_in_wasm_code; }
88 :
89 : inline void SetThreadInWasm() {
90 9808152 : if (IsTrapHandlerEnabled()) {
91 : DCHECK(!IsThreadInWasm());
92 9808088 : g_thread_in_wasm_code = true;
93 : }
94 : }
95 :
96 : inline void ClearThreadInWasm() {
97 9959655 : if (IsTrapHandlerEnabled()) {
98 : DCHECK(IsThreadInWasm());
99 9959591 : g_thread_in_wasm_code = false;
100 : }
101 : }
102 :
103 : bool RegisterDefaultTrapHandler();
104 : V8_EXPORT_PRIVATE void RemoveTrapHandler();
105 :
106 : size_t GetRecoveredTrapCount();
107 :
108 : } // namespace trap_handler
109 : } // namespace internal
110 : } // namespace v8
111 :
112 : #endif // V8_TRAP_HANDLER_TRAP_HANDLER_H_
|