1 //===--------------------------- Unwind-seh.cpp ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Implements SEH-based Itanium C++ exceptions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "config.h" 15 16 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 17 18 #include <unwind.h> 19 20 #include <stdint.h> 21 #include <stdbool.h> 22 #include <stdlib.h> 23 24 #include <windef.h> 25 #include <excpt.h> 26 #include <winnt.h> 27 #include <ntstatus.h> 28 29 #include "libunwind_ext.h" 30 #include "UnwindCursor.hpp" 31 32 using namespace libunwind; 33 34 #define STATUS_USER_DEFINED (1u << 29) 35 36 #define STATUS_GCC_MAGIC (('G' << 16) | ('C' << 8) | 'C') 37 38 #define MAKE_CUSTOM_STATUS(s, c) \ 39 ((NTSTATUS)(((s) << 30) | STATUS_USER_DEFINED | (c))) 40 #define MAKE_GCC_EXCEPTION(c) \ 41 MAKE_CUSTOM_STATUS(STATUS_SEVERITY_SUCCESS, STATUS_GCC_MAGIC | ((c) << 24)) 42 43 /// SEH exception raised by libunwind when the program calls 44 /// \c _Unwind_RaiseException. 45 #define STATUS_GCC_THROW MAKE_GCC_EXCEPTION(0) // 0x20474343 46 /// SEH exception raised by libunwind to initiate phase 2 of exception 47 /// handling. 48 #define STATUS_GCC_UNWIND MAKE_GCC_EXCEPTION(1) // 0x21474343 49 50 /// Class of foreign exceptions based on unrecognized SEH exceptions. 51 static const uint64_t kSEHExceptionClass = 0x434C4E4753454800; // CLNGSEH\0 52 53 /// Exception cleanup routine used by \c _GCC_specific_handler to 54 /// free foreign exceptions. 55 static void seh_exc_cleanup(_Unwind_Reason_Code urc, _Unwind_Exception *exc) { 56 if (exc->exception_class != kSEHExceptionClass) 57 _LIBUNWIND_ABORT("SEH cleanup called on non-SEH exception"); 58 free(exc); 59 } 60 61 static int _unw_init_seh(unw_cursor_t *cursor, CONTEXT *ctx); 62 static DISPATCHER_CONTEXT *_unw_seh_get_disp_ctx(unw_cursor_t *cursor); 63 static void _unw_seh_set_disp_ctx(unw_cursor_t *cursor, DISPATCHER_CONTEXT *disp); 64 65 /// Common implementation of SEH-style handler functions used by Itanium- 66 /// style frames. Depending on how and why it was called, it may do one of: 67 /// a) Delegate to the given Itanium-style personality function; or 68 /// b) Initiate a collided unwind to halt unwinding. 69 _LIBUNWIND_EXPORT EXCEPTION_DISPOSITION 70 _GCC_specific_handler(PEXCEPTION_RECORD ms_exc, PVOID frame, PCONTEXT ms_ctx, 71 DISPATCHER_CONTEXT *disp, __personality_routine pers) { 72 unw_context_t uc; 73 unw_cursor_t cursor; 74 _Unwind_Exception *exc; 75 _Unwind_Action action; 76 struct _Unwind_Context *ctx = nullptr; 77 _Unwind_Reason_Code urc; 78 uintptr_t retval, target; 79 bool ours = false; 80 81 _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler(%#010x(%x), %p)", ms_exc->ExceptionCode, ms_exc->ExceptionFlags, frame); 82 if (ms_exc->ExceptionCode == STATUS_GCC_UNWIND) { 83 if (IS_TARGET_UNWIND(ms_exc->ExceptionFlags)) { 84 // Set up the upper return value (the lower one and the target PC 85 // were set in the call to RtlUnwindEx()) for the landing pad. 86 #ifdef __x86_64__ 87 disp->ContextRecord->Rdx = ms_exc->ExceptionInformation[3]; 88 #elif defined(__arm__) 89 disp->ContextRecord->R1 = ms_exc->ExceptionInformation[3]; 90 #endif 91 } 92 // This is the collided unwind to the landing pad. Nothing to do. 93 return ExceptionContinueSearch; 94 } 95 96 if (ms_exc->ExceptionCode == STATUS_GCC_THROW) { 97 // This is (probably) a libunwind-controlled exception/unwind. Recover the 98 // parameters which we set below, and pass them to the personality function. 99 ours = true; 100 exc = (_Unwind_Exception *)ms_exc->ExceptionInformation[0]; 101 if (!IS_UNWINDING(ms_exc->ExceptionFlags) && ms_exc->NumberParameters > 1) { 102 ctx = (struct _Unwind_Context *)ms_exc->ExceptionInformation[1]; 103 action = (_Unwind_Action)ms_exc->ExceptionInformation[2]; 104 } 105 } else { 106 // Foreign exception. 107 exc = (_Unwind_Exception *)malloc(sizeof(_Unwind_Exception)); 108 exc->exception_class = kSEHExceptionClass; 109 exc->exception_cleanup = seh_exc_cleanup; 110 memset(exc->private_, 0, sizeof(exc->private_)); 111 } 112 if (!ctx) { 113 _unw_init_seh(&cursor, disp->ContextRecord); 114 _unw_seh_set_disp_ctx(&cursor, disp); 115 unw_set_reg(&cursor, UNW_REG_IP, disp->ControlPc-1); 116 ctx = (struct _Unwind_Context *)&cursor; 117 118 if (!IS_UNWINDING(ms_exc->ExceptionFlags)) { 119 if (ours && ms_exc->NumberParameters > 1) 120 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_FORCE_UNWIND); 121 else 122 action = _UA_SEARCH_PHASE; 123 } else { 124 if (ours && ms_exc->ExceptionInformation[1] == (ULONG_PTR)frame) 125 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME); 126 else 127 action = _UA_CLEANUP_PHASE; 128 } 129 } 130 131 _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler() calling personality function %p(1, %d, %llx, %p, %p)", pers, action, exc->exception_class, exc, ctx); 132 urc = pers(1, action, exc->exception_class, exc, ctx); 133 _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler() personality returned %d", urc); 134 switch (urc) { 135 case _URC_CONTINUE_UNWIND: 136 // If we're in phase 2, and the personality routine said to continue 137 // at the target frame, we're in real trouble. 138 if (action & _UA_HANDLER_FRAME) 139 _LIBUNWIND_ABORT("Personality continued unwind at the target frame!"); 140 return ExceptionContinueSearch; 141 case _URC_HANDLER_FOUND: 142 // If we were called by __libunwind_seh_personality(), indicate that 143 // a handler was found; otherwise, initiate phase 2 by unwinding. 144 if (ours && ms_exc->NumberParameters > 1) 145 return 4 /* ExecptionExecuteHandler in mingw */; 146 // This should never happen in phase 2. 147 if (IS_UNWINDING(ms_exc->ExceptionFlags)) 148 _LIBUNWIND_ABORT("Personality indicated exception handler in phase 2!"); 149 exc->private_[1] = (ULONG_PTR)frame; 150 if (ours) { 151 ms_exc->NumberParameters = 4; 152 ms_exc->ExceptionInformation[1] = (ULONG_PTR)frame; 153 } 154 // FIXME: Indicate target frame in foreign case! 155 // phase 2: the clean up phase 156 RtlUnwindEx(frame, (PVOID)disp->ControlPc, ms_exc, exc, ms_ctx, disp->HistoryTable); 157 _LIBUNWIND_ABORT("RtlUnwindEx() failed"); 158 case _URC_INSTALL_CONTEXT: { 159 // If we were called by __libunwind_seh_personality(), indicate that 160 // a handler was found; otherwise, it's time to initiate a collided 161 // unwind to the target. 162 if (ours && !IS_UNWINDING(ms_exc->ExceptionFlags) && ms_exc->NumberParameters > 1) 163 return 4 /* ExecptionExecuteHandler in mingw */; 164 // This should never happen in phase 1. 165 if (!IS_UNWINDING(ms_exc->ExceptionFlags)) 166 _LIBUNWIND_ABORT("Personality installed context during phase 1!"); 167 #ifdef __x86_64__ 168 exc->private_[2] = disp->TargetIp; 169 unw_get_reg(&cursor, UNW_X86_64_RAX, &retval); 170 unw_get_reg(&cursor, UNW_X86_64_RDX, &exc->private_[3]); 171 #elif defined(__arm__) 172 exc->private_[2] = disp->TargetPc; 173 unw_get_reg(&cursor, UNW_ARM_R0, &retval); 174 unw_get_reg(&cursor, UNW_ARM_R1, &exc->private_[3]); 175 #endif 176 unw_get_reg(&cursor, UNW_REG_IP, &target); 177 ms_exc->ExceptionCode = STATUS_GCC_UNWIND; 178 #ifdef __x86_64__ 179 ms_exc->ExceptionInformation[2] = disp->TargetIp; 180 #elif defined(__arm__) 181 ms_exc->ExceptionInformation[2] = disp->TargetPc; 182 #endif 183 ms_exc->ExceptionInformation[3] = exc->private_[3]; 184 // Give NTRTL some scratch space to keep track of the collided unwind. 185 // Don't use the one that was passed in; we don't want to overwrite the 186 // context in the DISPATCHER_CONTEXT. 187 CONTEXT new_ctx; 188 RtlUnwindEx(frame, (PVOID)target, ms_exc, (PVOID)retval, &new_ctx, disp->HistoryTable); 189 _LIBUNWIND_ABORT("RtlUnwindEx() failed"); 190 } 191 // Anything else indicates a serious problem. 192 default: return ExceptionContinueExecution; 193 } 194 } 195 196 /// Personality function returned by \c unw_get_proc_info() in SEH contexts. 197 /// This is a wrapper that calls the real SEH handler function, which in 198 /// turn (at least, for Itanium-style frames) calls the real Itanium 199 /// personality function (see \c _GCC_specific_handler()). 200 extern "C" _Unwind_Reason_Code 201 __libunwind_seh_personality(int version, _Unwind_Action state, 202 uint64_t klass, _Unwind_Exception *exc, 203 struct _Unwind_Context *context) { 204 EXCEPTION_RECORD ms_exc; 205 bool phase2 = (state & (_UA_SEARCH_PHASE|_UA_CLEANUP_PHASE)) == _UA_CLEANUP_PHASE; 206 ms_exc.ExceptionCode = STATUS_GCC_THROW; 207 ms_exc.ExceptionFlags = 0; 208 ms_exc.NumberParameters = 3; 209 ms_exc.ExceptionInformation[0] = (ULONG_PTR)exc; 210 ms_exc.ExceptionInformation[1] = (ULONG_PTR)context; 211 ms_exc.ExceptionInformation[2] = state; 212 DISPATCHER_CONTEXT *disp_ctx = _unw_seh_get_disp_ctx((unw_cursor_t *)context); 213 EXCEPTION_DISPOSITION ms_act = disp_ctx->LanguageHandler(&ms_exc, 214 (PVOID)disp_ctx->EstablisherFrame, 215 disp_ctx->ContextRecord, 216 disp_ctx); 217 switch (ms_act) { 218 case ExceptionContinueSearch: return _URC_CONTINUE_UNWIND; 219 case 4 /*ExceptionExecuteHandler*/: 220 return phase2 ? _URC_INSTALL_CONTEXT : _URC_HANDLER_FOUND; 221 default: 222 return phase2 ? _URC_FATAL_PHASE2_ERROR : _URC_FATAL_PHASE1_ERROR; 223 } 224 } 225 226 static _Unwind_Reason_Code 227 unwind_phase2_forced(unw_context_t *uc, 228 _Unwind_Exception *exception_object, 229 _Unwind_Stop_Fn stop, void *stop_parameter) { 230 unw_cursor_t cursor2; 231 unw_init_local(&cursor2, uc); 232 233 // Walk each frame until we reach where search phase said to stop 234 while (unw_step(&cursor2) > 0) { 235 236 // Update info about this frame. 237 unw_proc_info_t frameInfo; 238 if (unw_get_proc_info(&cursor2, &frameInfo) != UNW_ESUCCESS) { 239 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): unw_step " 240 "failed => _URC_END_OF_STACK", 241 (void *)exception_object); 242 return _URC_FATAL_PHASE2_ERROR; 243 } 244 245 // When tracing, print state information. 246 if (_LIBUNWIND_TRACING_UNWINDING) { 247 char functionBuf[512]; 248 const char *functionName = functionBuf; 249 unw_word_t offset; 250 if ((unw_get_proc_name(&cursor2, functionBuf, sizeof(functionBuf), 251 &offset) != UNW_ESUCCESS) || 252 (frameInfo.start_ip + offset > frameInfo.end_ip)) 253 functionName = ".anonymous."; 254 _LIBUNWIND_TRACE_UNWINDING( 255 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIx64 256 ", func=%s, lsda=0x%" PRIx64 ", personality=0x%" PRIx64, 257 (void *)exception_object, frameInfo.start_ip, functionName, 258 frameInfo.lsda, frameInfo.handler); 259 } 260 261 // Call stop function at each frame. 262 _Unwind_Action action = 263 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE); 264 _Unwind_Reason_Code stopResult = 265 (*stop)(1, action, exception_object->exception_class, exception_object, 266 (struct _Unwind_Context *)(&cursor2), stop_parameter); 267 _LIBUNWIND_TRACE_UNWINDING( 268 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d", 269 (void *)exception_object, stopResult); 270 if (stopResult != _URC_NO_REASON) { 271 _LIBUNWIND_TRACE_UNWINDING( 272 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function", 273 (void *)exception_object); 274 return _URC_FATAL_PHASE2_ERROR; 275 } 276 277 // If there is a personality routine, tell it we are unwinding. 278 if (frameInfo.handler != 0) { 279 __personality_routine p = 280 (__personality_routine)(intptr_t)(frameInfo.handler); 281 _LIBUNWIND_TRACE_UNWINDING( 282 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p", 283 (void *)exception_object, (void *)(uintptr_t)p); 284 _Unwind_Reason_Code personalityResult = 285 (*p)(1, action, exception_object->exception_class, exception_object, 286 (struct _Unwind_Context *)(&cursor2)); 287 switch (personalityResult) { 288 case _URC_CONTINUE_UNWIND: 289 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " 290 "personality returned " 291 "_URC_CONTINUE_UNWIND", 292 (void *)exception_object); 293 // Destructors called, continue unwinding 294 break; 295 case _URC_INSTALL_CONTEXT: 296 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " 297 "personality returned " 298 "_URC_INSTALL_CONTEXT", 299 (void *)exception_object); 300 // We may get control back if landing pad calls _Unwind_Resume(). 301 unw_resume(&cursor2); 302 break; 303 default: 304 // Personality routine returned an unknown result code. 305 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " 306 "personality returned %d, " 307 "_URC_FATAL_PHASE2_ERROR", 308 (void *)exception_object, personalityResult); 309 return _URC_FATAL_PHASE2_ERROR; 310 } 311 } 312 } 313 314 // Call stop function one last time and tell it we've reached the end 315 // of the stack. 316 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop " 317 "function with _UA_END_OF_STACK", 318 (void *)exception_object); 319 _Unwind_Action lastAction = 320 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK); 321 (*stop)(1, lastAction, exception_object->exception_class, exception_object, 322 (struct _Unwind_Context *)(&cursor2), stop_parameter); 323 324 // Clean up phase did not resume at the frame that the search phase said it 325 // would. 326 return _URC_FATAL_PHASE2_ERROR; 327 } 328 329 /// Called by \c __cxa_throw(). Only returns if there is a fatal error. 330 _LIBUNWIND_EXPORT _Unwind_Reason_Code 331 _Unwind_RaiseException(_Unwind_Exception *exception_object) { 332 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)", 333 (void *)exception_object); 334 335 // Mark that this is a non-forced unwind, so _Unwind_Resume() 336 // can do the right thing. 337 memset(exception_object->private_, 0, sizeof(exception_object->private_)); 338 339 // phase 1: the search phase 340 // We'll let the system do that for us. 341 RaiseException(STATUS_GCC_THROW, 0, 1, (ULONG_PTR *)&exception_object); 342 343 // If we get here, either something went horribly wrong or we reached the 344 // top of the stack. Either way, let libc++abi call std::terminate(). 345 return _URC_END_OF_STACK; 346 } 347 348 /// When \c _Unwind_RaiseException() is in phase2, it hands control 349 /// to the personality function at each frame. The personality 350 /// may force a jump to a landing pad in that function; the landing 351 /// pad code may then call \c _Unwind_Resume() to continue with the 352 /// unwinding. Note: the call to \c _Unwind_Resume() is from compiler 353 /// geneated user code. All other \c _Unwind_* routines are called 354 /// by the C++ runtime \c __cxa_* routines. 355 /// 356 /// Note: re-throwing an exception (as opposed to continuing the unwind) 357 /// is implemented by having the code call \c __cxa_rethrow() which 358 /// in turn calls \c _Unwind_Resume_or_Rethrow(). 359 _LIBUNWIND_EXPORT void 360 _Unwind_Resume(_Unwind_Exception *exception_object) { 361 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object); 362 363 if (exception_object->private_[0] != 0) { 364 unw_context_t uc; 365 366 unw_getcontext(&uc); 367 unwind_phase2_forced(&uc, exception_object, 368 (_Unwind_Stop_Fn) exception_object->private_[0], 369 (void *)exception_object->private_[4]); 370 } else { 371 // Recover the parameters for the unwind from the exception object 372 // so we can start unwinding again. 373 EXCEPTION_RECORD ms_exc; 374 CONTEXT ms_ctx; 375 UNWIND_HISTORY_TABLE hist; 376 377 ms_exc.ExceptionCode = STATUS_GCC_THROW; 378 ms_exc.ExceptionFlags = EXCEPTION_NONCONTINUABLE; 379 ms_exc.NumberParameters = 4; 380 ms_exc.ExceptionInformation[0] = (ULONG_PTR)exception_object; 381 ms_exc.ExceptionInformation[1] = exception_object->private_[1]; 382 ms_exc.ExceptionInformation[2] = exception_object->private_[2]; 383 ms_exc.ExceptionInformation[3] = exception_object->private_[3]; 384 RtlUnwindEx((PVOID)exception_object->private_[1], 385 (PVOID)exception_object->private_[2], &ms_exc, 386 exception_object, &ms_ctx, &hist); 387 } 388 389 // Clients assume _Unwind_Resume() does not return, so all we can do is abort. 390 _LIBUNWIND_ABORT("_Unwind_Resume() can't return"); 391 } 392 393 /// Not used by C++. 394 /// Unwinds stack, calling "stop" function at each frame. 395 /// Could be used to implement \c longjmp(). 396 _LIBUNWIND_EXPORT _Unwind_Reason_Code 397 _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, 398 _Unwind_Stop_Fn stop, void *stop_parameter) { 399 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)", 400 (void *)exception_object, (void *)(uintptr_t)stop); 401 unw_context_t uc; 402 unw_getcontext(&uc); 403 404 // Mark that this is a forced unwind, so _Unwind_Resume() can do 405 // the right thing. 406 exception_object->private_[0] = (uintptr_t) stop; 407 exception_object->private_[4] = (uintptr_t) stop_parameter; 408 409 // do it 410 return unwind_phase2_forced(&uc, exception_object, stop, stop_parameter); 411 } 412 413 /// Called by personality handler during phase 2 to get LSDA for current frame. 414 _LIBUNWIND_EXPORT uintptr_t 415 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { 416 uintptr_t result = (uintptr_t)_unw_seh_get_disp_ctx((unw_cursor_t *)context)->HandlerData; 417 _LIBUNWIND_TRACE_API( 418 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR, 419 (void *)context, result); 420 return result; 421 } 422 423 /// Called by personality handler during phase 2 to find the start of the 424 /// function. 425 _LIBUNWIND_EXPORT uintptr_t 426 _Unwind_GetRegionStart(struct _Unwind_Context *context) { 427 DISPATCHER_CONTEXT *disp = _unw_seh_get_disp_ctx((unw_cursor_t *)context); 428 uintptr_t result = (uintptr_t)disp->FunctionEntry->BeginAddress + disp->ImageBase; 429 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR, 430 (void *)context, result); 431 return result; 432 } 433 434 static int 435 _unw_init_seh(unw_cursor_t *cursor, CONTEXT *context) { 436 #ifdef _LIBUNWIND_TARGET_X86_64 437 new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_x86_64>( 438 context, LocalAddressSpace::sThisAddressSpace); 439 auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor); 440 co->setInfoBasedOnIPRegister(); 441 return UNW_ESUCCESS; 442 #elif defined(_LIBUNWIND_TARGET_ARM) 443 new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_arm>( 444 context, LocalAddressSpace::sThisAddressSpace); 445 auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor); 446 co->setInfoBasedOnIPRegister(); 447 return UNW_ESUCCESS; 448 #else 449 return UNW_EINVAL; 450 #endif 451 } 452 453 static DISPATCHER_CONTEXT * 454 _unw_seh_get_disp_ctx(unw_cursor_t *cursor) { 455 #ifdef _LIBUNWIND_TARGET_X86_64 456 return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->getDispatcherContext(); 457 #elif defined(_LIBUNWIND_TARGET_ARM) 458 return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->getDispatcherContext(); 459 #else 460 return nullptr; 461 #endif 462 } 463 464 static void 465 _unw_seh_set_disp_ctx(unw_cursor_t *cursor, DISPATCHER_CONTEXT *disp) { 466 #ifdef _LIBUNWIND_TARGET_X86_64 467 reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->setDispatcherContext(disp); 468 #elif defined(_LIBUNWIND_TARGET_ARM) 469 reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->setDispatcherContext(disp); 470 #endif 471 } 472 473 #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 474