• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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_DIAGNOSTICS_UNWINDING_INFO_WIN64_H_
6 #define V8_DIAGNOSTICS_UNWINDING_INFO_WIN64_H_
7 
8 #include <vector>
9 
10 #include "include/v8-callbacks.h"
11 #include "include/v8config.h"
12 #include "src/common/globals.h"
13 
14 #if defined(V8_OS_WIN64)
15 #include "src/base/win32-headers.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 namespace win64_unwindinfo {
21 
22 #define CRASH_HANDLER_FUNCTION_NAME CrashForExceptionInNonABICompliantCodeRange
23 #define CRASH_HANDLER_FUNCTION_NAME_STRING \
24   "CrashForExceptionInNonABICompliantCodeRange"
25 
26 static const int kOSPageSize = 4096;
27 
28 /**
29  * Returns true if V8 is configured to emit unwinding data for embedded in the
30  * pdata/xdata sections of the executable. Currently, this happens when V8 is
31  * built with "v8_win64_unwinding_info = true".
32  */
33 bool CanEmitUnwindInfoForBuiltins();
34 
35 /**
36  * Returns true if V8 if we can register unwinding data for the whole code range
37  * of an isolate or Wasm module. The first page of the code range is reserved
38  * and writable, to be used to store unwind data, as documented in:
39  * https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64.
40  * In jitless mode V8 does not allocate any executable memory itself so the only
41  * non-abi-compliant code range is in the embedded blob.
42  */
43 bool CanRegisterUnwindInfoForNonABICompliantCodeRange();
44 
45 /**
46  * Registers a custom exception handler for exceptions in V8-generated code.
47  */
48 void SetUnhandledExceptionCallback(
49     v8::UnhandledExceptionCallback unhandled_exception_callback);
50 
51 void RegisterNonABICompliantCodeRange(void* start, size_t size_in_bytes);
52 void UnregisterNonABICompliantCodeRange(void* start);
53 
54 /**
55  * Default count of RUNTIME_FUNCTION needed. For Windows X64, 1 RUNTIME_FUNCTION
56  * covers 4GB range which is sufficient to cover the whole code range of an
57  * isolate or Wasm module. For Windows ARM64, 1 RUNTIME_FUNCTION covers
58  * kMaxFunctionLength bytes so multiple RUNTIME_FUNCTION structs could be needed
59  * to cover the whole code range of an isolate or Wasm module. The extra
60  * RUNTIME_FUNCTIONs are assumed following the first one in the reserved page.
61  */
62 static const uint32_t kDefaultRuntimeFunctionCount = 1;
63 
64 #if defined(V8_OS_WIN_X64)
65 
66 static const int kPushRbpInstructionLength = 1;
67 static const int kMovRbpRspInstructionLength = 3;
68 static const int kRbpPrefixCodes = 2;
69 static const int kRbpPrefixLength =
70     kPushRbpInstructionLength + kMovRbpRspInstructionLength;
71 
72 /**
73  * Returns a vector of bytes that contains the Win X64 unwind data used for all
74  * V8 builtin functions.
75  */
76 std::vector<uint8_t> GetUnwindInfoForBuiltinFunctions();
77 
78 class BuiltinUnwindInfo {
79  public:
BuiltinUnwindInfo()80   BuiltinUnwindInfo() : is_leaf_function_(true) {}
BuiltinUnwindInfo(const std::vector<int> & fp_offsets)81   explicit BuiltinUnwindInfo(const std::vector<int>& fp_offsets)
82       : is_leaf_function_(false), fp_offsets_(fp_offsets) {}
83 
is_leaf_function()84   bool is_leaf_function() const { return is_leaf_function_; }
fp_offsets()85   const std::vector<int>& fp_offsets() const { return fp_offsets_; }
86 
87  private:
88   bool is_leaf_function_;
89   std::vector<int> fp_offsets_;
90 };
91 
92 class XdataEncoder {
93  public:
XdataEncoder(const Assembler & assembler)94   explicit XdataEncoder(const Assembler& assembler)
95       : assembler_(assembler), current_frame_code_offset_(-1) {}
96 
97   void onPushRbp();
98   void onMovRbpRsp();
99 
unwinding_info()100   BuiltinUnwindInfo unwinding_info() const {
101     return BuiltinUnwindInfo(fp_offsets_);
102   }
103 
104  private:
105   const Assembler& assembler_;
106   std::vector<int> fp_offsets_;
107   int current_frame_code_offset_;
108 };
109 
110 #elif defined(V8_OS_WIN_ARM64)
111 
112 /**
113  * Base on below doc, unwind record has 18 bits (unsigned) to encode function
114  * length, besides 2 LSB which are always 0.
115  * https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling#xdata-records
116  */
117 static const int kMaxFunctionLength = ((1 << 18) - 1) << 2;
118 
119 struct FrameOffsets {
120   FrameOffsets();
121   bool IsDefault() const;
122   int fp_to_saved_caller_fp;
123   int fp_to_caller_sp;
124 };
125 
126 /**
127  * Returns a vector of bytes that contains the Win ARM64 unwind data used for
128  * all V8 builtin functions.
129  *
130  * func_len: length in bytes of current function/region to unwind.
131  * fp_adjustment: offset of the saved caller's fp based on fp in current frame.
132  *                this is necessary to encode unwind data for Windows stack
133  *                unwinder to find correct caller's fp.
134  */
135 std::vector<uint8_t> GetUnwindInfoForBuiltinFunction(
136     uint32_t func_len, FrameOffsets fp_adjustment);
137 class BuiltinUnwindInfo {
138  public:
BuiltinUnwindInfo()139   BuiltinUnwindInfo() : is_leaf_function_(true) {}
BuiltinUnwindInfo(const std::vector<int> & fp_offsets,const std::vector<FrameOffsets> & fp_adjustments)140   explicit BuiltinUnwindInfo(const std::vector<int>& fp_offsets,
141                              const std::vector<FrameOffsets>& fp_adjustments)
142       : is_leaf_function_(false),
143         fp_offsets_(fp_offsets),
144         fp_adjustments_(fp_adjustments) {}
145 
fp_adjustments()146   const std::vector<FrameOffsets>& fp_adjustments() const {
147     return fp_adjustments_;
148   }
149 
is_leaf_function()150   bool is_leaf_function() const { return is_leaf_function_; }
fp_offsets()151   const std::vector<int>& fp_offsets() const { return fp_offsets_; }
152 
153  private:
154   bool is_leaf_function_;
155   std::vector<int> fp_offsets_;
156   std::vector<FrameOffsets> fp_adjustments_;
157 };
158 
159 class XdataEncoder {
160  public:
XdataEncoder(const Assembler & assembler)161   explicit XdataEncoder(const Assembler& assembler)
162       : assembler_(assembler), current_frame_code_offset_(-1) {}
163 
164   void onSaveFpLr();
165   void onFramePointerAdjustment(int fp_to_saved_caller_fp, int fp_to_caller_sp);
166 
unwinding_info()167   BuiltinUnwindInfo unwinding_info() const {
168     return BuiltinUnwindInfo(fp_offsets_, fp_adjustments_);
169   }
170 
171  private:
172   const Assembler& assembler_;
173   std::vector<int> fp_offsets_;
174   int current_frame_code_offset_;
175   FrameOffsets current_frame_adjustment_;
176   std::vector<FrameOffsets> fp_adjustments_;
177 };
178 
179 #endif
180 
181 }  // namespace win64_unwindinfo
182 }  // namespace internal
183 }  // namespace v8
184 
185 #endif  // V8_OS_WIN64
186 
187 #endif  // V8_DIAGNOSTICS_UNWINDING_INFO_WIN64_H_
188