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_H_
6 : #define V8_TRAP_HANDLER_H_
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 :
20 : namespace v8 {
21 : namespace internal {
22 : namespace trap_handler {
23 :
24 : // TODO(eholk): Support trap handlers on other platforms.
25 : #if V8_TARGET_ARCH_X64 && V8_OS_LINUX && !V8_OS_ANDROID
26 : #define V8_TRAP_HANDLER_SUPPORTED 1
27 : #else
28 : #define V8_TRAP_HANDLER_SUPPORTED 0
29 : #endif
30 :
31 : struct ProtectedInstructionData {
32 : // The offset of this instruction from the start of its code object.
33 : intptr_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 : intptr_t landing_offset;
39 : };
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 7107025 : return FLAG_wasm_trap_handler && V8_TRAP_HANDLER_SUPPORTED;
66 : }
67 :
68 : extern THREAD_LOCAL bool g_thread_in_wasm_code;
69 :
70 2444770 : inline bool IsThreadInWasm() { return g_thread_in_wasm_code; }
71 :
72 3347211 : inline void SetThreadInWasm() {
73 3347776 : if (UseTrapHandler()) {
74 : DCHECK(!IsThreadInWasm());
75 3347248 : g_thread_in_wasm_code = true;
76 : }
77 3347211 : }
78 :
79 3344796 : inline void ClearThreadInWasm() {
80 3345292 : if (UseTrapHandler()) {
81 : DCHECK(IsThreadInWasm());
82 3345058 : g_thread_in_wasm_code = false;
83 : }
84 3344796 : }
85 :
86 : bool RegisterDefaultSignalHandler();
87 :
88 : #if V8_OS_LINUX
89 : bool TryHandleSignal(int signum, siginfo_t* info, ucontext_t* context);
90 : #endif // V8_OS_LINUX
91 :
92 : } // namespace trap_handler
93 : } // namespace internal
94 : } // namespace v8
95 :
96 : #endif // V8_TRAP_HANDLER_H_
|