OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_TRAP_HANDLER_H_ | 5 #ifndef V8_TRAP_HANDLER_H_ |
6 #define V8_TRAP_HANDLER_H_ | 6 #define V8_TRAP_HANDLER_H_ |
7 | 7 |
| 8 #include <signal.h> |
| 9 #include <stdint.h> |
| 10 #include <stdlib.h> |
| 11 |
| 12 #include "src/base/build_config.h" |
| 13 #include "src/flags.h" |
| 14 #include "src/globals.h" |
| 15 |
| 16 #if V8_OS_LINUX |
| 17 #include <ucontext.h> |
| 18 #endif |
| 19 |
8 namespace v8 { | 20 namespace v8 { |
9 namespace internal { | 21 namespace internal { |
10 namespace trap_handler { | 22 namespace trap_handler { |
11 | 23 |
| 24 // TODO(eholk): Support trap handlers on other platforms. |
| 25 #if V8_TARGET_ARCH_X64 && V8_OS_LINUX |
| 26 #define V8_TRAP_HANDLER_SUPPORTED 1 |
| 27 #else |
| 28 #define V8_TRAP_HANDLER_SUPPORTED 0 |
| 29 #endif |
| 30 |
12 struct ProtectedInstructionData { | 31 struct ProtectedInstructionData { |
13 // The offset of this instruction from the start of its code object. | 32 // The offset of this instruction from the start of its code object. |
14 intptr_t instr_offset; | 33 intptr_t instr_offset; |
15 | 34 |
16 // The offset of the landing pad from the start of its code object. | 35 // The offset of the landing pad from the start of its code object. |
17 // | 36 // |
18 // TODO(eholk): Using a single landing pad and store parameters here. | 37 // TODO(eholk): Using a single landing pad and store parameters here. |
19 intptr_t landing_offset; | 38 intptr_t landing_offset; |
20 }; | 39 }; |
21 | 40 |
| 41 /// Adjusts the base code pointer. |
| 42 void UpdateHandlerDataCodePointer(int index, void* base); |
| 43 |
| 44 /// Adds the handler data to the place where the signal handler will find it. |
| 45 /// |
| 46 /// This returns a number that can be used to identify the handler data to |
| 47 /// UpdateHandlerDataCodePointer and ReleaseHandlerData, or -1 on failure. |
| 48 int RegisterHandlerData(void* base, size_t size, |
| 49 size_t num_protected_instructions, |
| 50 ProtectedInstructionData* protected_instructions); |
| 51 |
| 52 /// Removes the data from the master list and frees any memory, if necessary. |
| 53 void ReleaseHandlerData(int index); |
| 54 |
| 55 #if V8_OS_WIN |
| 56 #define THREAD_LOCAL __declspec(thread) |
| 57 #elif V8_OS_ANDROID |
| 58 // TODO(eholk): fix this before enabling for trap handlers for Android. |
| 59 #define THREAD_LOCAL |
| 60 #else |
| 61 #define THREAD_LOCAL __thread |
| 62 #endif |
| 63 |
| 64 inline bool UseTrapHandler() { |
| 65 return FLAG_wasm_trap_handler && V8_TRAP_HANDLER_SUPPORTED; |
| 66 } |
| 67 |
| 68 extern THREAD_LOCAL bool g_thread_in_wasm_code; |
| 69 |
| 70 inline bool IsThreadInWasm() { return g_thread_in_wasm_code; } |
| 71 |
| 72 inline void SetThreadInWasm() { |
| 73 if (UseTrapHandler()) { |
| 74 DCHECK(!IsThreadInWasm()); |
| 75 g_thread_in_wasm_code = true; |
| 76 } |
| 77 } |
| 78 inline void ClearThreadInWasm() { |
| 79 if (UseTrapHandler()) { |
| 80 DCHECK(IsThreadInWasm()); |
| 81 g_thread_in_wasm_code = false; |
| 82 } |
| 83 } |
| 84 |
| 85 bool RegisterDefaultSignalHandler(); |
| 86 |
| 87 #if V8_OS_LINUX |
| 88 bool TryHandleSignal(int signum, siginfo_t* info, ucontext_t* context); |
| 89 #endif // V8_OS_LINUX |
| 90 |
22 } // namespace trap_handler | 91 } // namespace trap_handler |
23 } // namespace internal | 92 } // namespace internal |
24 } // namespace v8 | 93 } // namespace v8 |
25 | 94 |
26 #endif // V8_TRAP_HANDLER_H_ | 95 #endif // V8_TRAP_HANDLER_H_ |
OLD | NEW |