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