1 //===------------------------- cxa_exception.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 // This file implements the "Exception Handling APIs" 10 // http://mentorembedded.github.io/cxx-abi/abi-eh.html 11 // http://www.intel.com/design/itanium/downloads/245358.htm 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include <assert.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <typeinfo> 19 20 #include "config.h" 21 #include "cxa_exception.hpp" 22 #include "cxa_handlers.hpp" 23 #include "private_typeinfo.h" 24 #include "unwind.h" 25 26 /* 27 Exception Header Layout: 28 29 +---------------------------+-----------------------------+---------------+ 30 | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | 31 +---------------------------+-----------------------------+---------------+ 32 ^ 33 | 34 +-------------------------------------------------------+ 35 | 36 +---------------------------+-----------------------------+ 37 | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 | 38 +---------------------------+-----------------------------+ 39 40 Exception Handling Table Layout: 41 42 +-----------------+--------+ 43 | lpStartEncoding | (char) | 44 +---------+-------+--------+---------------+-----------------------+ 45 | lpStart | (encoded with lpStartEncoding) | defaults to funcStart | 46 +---------+-----+--------+-----------------+---------------+-------+ 47 | ttypeEncoding | (char) | Encoding of the type_info table | 48 +---------------+-+------+----+----------------------------+----------------+ 49 | classInfoOffset | (ULEB128) | Offset to type_info table, defaults to null | 50 +-----------------++--------+-+----------------------------+----------------+ 51 | callSiteEncoding | (char) | Encoding for Call Site Table | 52 +------------------+--+-----+-----+------------------------+--------------------------+ 53 | callSiteTableLength | (ULEB128) | Call Site Table length, used to find Action table | 54 +---------------------+-----------+---------------------------------------------------+ 55 #ifndef __USING_SJLJ_EXCEPTIONS__ 56 +---------------------+-----------+------------------------------------------------+ 57 | Beginning of Call Site Table The current ip lies within the | 58 | ... (start, length) range of one of these | 59 | call sites. There may be action needed. | 60 | +-------------+---------------------------------+------------------------------+ | 61 | | start | (encoded with callSiteEncoding) | offset relative to funcStart | | 62 | | length | (encoded with callSiteEncoding) | length of code fragment | | 63 | | landingPad | (encoded with callSiteEncoding) | offset relative to lpStart | | 64 | | actionEntry | (ULEB128) | Action Table Index 1-based | | 65 | | | | actionEntry == 0 -> cleanup | | 66 | +-------------+---------------------------------+------------------------------+ | 67 | ... | 68 +----------------------------------------------------------------------------------+ 69 #else // __USING_SJLJ_EXCEPTIONS__ 70 +---------------------+-----------+------------------------------------------------+ 71 | Beginning of Call Site Table The current ip is a 1-based index into | 72 | ... this table. Or it is -1 meaning no | 73 | action is needed. Or it is 0 meaning | 74 | terminate. | 75 | +-------------+---------------------------------+------------------------------+ | 76 | | landingPad | (ULEB128) | offset relative to lpStart | | 77 | | actionEntry | (ULEB128) | Action Table Index 1-based | | 78 | | | | actionEntry == 0 -> cleanup | | 79 | +-------------+---------------------------------+------------------------------+ | 80 | ... | 81 +----------------------------------------------------------------------------------+ 82 #endif // __USING_SJLJ_EXCEPTIONS__ 83 +---------------------------------------------------------------------+ 84 | Beginning of Action Table ttypeIndex == 0 : cleanup | 85 | ... ttypeIndex > 0 : catch | 86 | ttypeIndex < 0 : exception spec | 87 | +--------------+-----------+--------------------------------------+ | 88 | | ttypeIndex | (SLEB128) | Index into type_info Table (1-based) | | 89 | | actionOffset | (SLEB128) | Offset into next Action Table entry | | 90 | +--------------+-----------+--------------------------------------+ | 91 | ... | 92 +---------------------------------------------------------------------+-----------------+ 93 | type_info Table, but classInfoOffset does *not* point here! | 94 | +----------------+------------------------------------------------+-----------------+ | 95 | | Nth type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == N | | 96 | +----------------+------------------------------------------------+-----------------+ | 97 | ... | 98 | +----------------+------------------------------------------------+-----------------+ | 99 | | 1st type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == 1 | | 100 | +----------------+------------------------------------------------+-----------------+ | 101 | +---------------------------------------+-----------+------------------------------+ | 102 | | 1st ttypeIndex for 1st exception spec | (ULEB128) | classInfoOffset points here! | | 103 | | ... | (ULEB128) | | | 104 | | Mth ttypeIndex for 1st exception spec | (ULEB128) | | | 105 | | 0 | (ULEB128) | | | 106 | +---------------------------------------+------------------------------------------+ | 107 | ... | 108 | +---------------------------------------+------------------------------------------+ | 109 | | 0 | (ULEB128) | throw() | | 110 | +---------------------------------------+------------------------------------------+ | 111 | ... | 112 | +---------------------------------------+------------------------------------------+ | 113 | | 1st ttypeIndex for Nth exception spec | (ULEB128) | | | 114 | | ... | (ULEB128) | | | 115 | | Mth ttypeIndex for Nth exception spec | (ULEB128) | | | 116 | | 0 | (ULEB128) | | | 117 | +---------------------------------------+------------------------------------------+ | 118 +---------------------------------------------------------------------------------------+ 119 120 Notes: 121 122 * ttypeIndex in the Action Table, and in the exception spec table, is an index, 123 not a byte count, if positive. It is a negative index offset of 124 classInfoOffset and the sizeof entry depends on ttypeEncoding. 125 But if ttypeIndex is negative, it is a positive 1-based byte offset into the 126 type_info Table. 127 And if ttypeIndex is zero, it refers to a catch (...). 128 129 * landingPad can be 0, this implies there is nothing to be done. 130 131 * landingPad != 0 and actionEntry == 0 implies a cleanup needs to be done 132 @landingPad. 133 134 * A cleanup can also be found under landingPad != 0 and actionEntry != 0 in 135 the Action Table with ttypeIndex == 0. 136 */ 137 138 namespace __cxxabiv1 139 { 140 141 namespace 142 { 143 144 template <class AsType> 145 uintptr_t readPointerHelper(const uint8_t*& p) { 146 AsType value; 147 memcpy(&value, p, sizeof(AsType)); 148 p += sizeof(AsType); 149 return static_cast<uintptr_t>(value); 150 } 151 152 } // end namespace 153 154 extern "C" 155 { 156 157 // private API 158 159 // Heavily borrowed from llvm/examples/ExceptionDemo/ExceptionDemo.cpp 160 161 // DWARF Constants 162 enum 163 { 164 DW_EH_PE_absptr = 0x00, 165 DW_EH_PE_uleb128 = 0x01, 166 DW_EH_PE_udata2 = 0x02, 167 DW_EH_PE_udata4 = 0x03, 168 DW_EH_PE_udata8 = 0x04, 169 DW_EH_PE_sleb128 = 0x09, 170 DW_EH_PE_sdata2 = 0x0A, 171 DW_EH_PE_sdata4 = 0x0B, 172 DW_EH_PE_sdata8 = 0x0C, 173 DW_EH_PE_pcrel = 0x10, 174 DW_EH_PE_textrel = 0x20, 175 DW_EH_PE_datarel = 0x30, 176 DW_EH_PE_funcrel = 0x40, 177 DW_EH_PE_aligned = 0x50, 178 DW_EH_PE_indirect = 0x80, 179 DW_EH_PE_omit = 0xFF 180 }; 181 182 /// Read a uleb128 encoded value and advance pointer 183 /// See Variable Length Data Appendix C in: 184 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink 185 /// @param data reference variable holding memory pointer to decode from 186 /// @returns decoded value 187 static 188 uintptr_t 189 readULEB128(const uint8_t** data) 190 { 191 uintptr_t result = 0; 192 uintptr_t shift = 0; 193 unsigned char byte; 194 const uint8_t *p = *data; 195 do 196 { 197 byte = *p++; 198 result |= static_cast<uintptr_t>(byte & 0x7F) << shift; 199 shift += 7; 200 } while (byte & 0x80); 201 *data = p; 202 return result; 203 } 204 205 /// Read a sleb128 encoded value and advance pointer 206 /// See Variable Length Data Appendix C in: 207 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink 208 /// @param data reference variable holding memory pointer to decode from 209 /// @returns decoded value 210 static 211 intptr_t 212 readSLEB128(const uint8_t** data) 213 { 214 uintptr_t result = 0; 215 uintptr_t shift = 0; 216 unsigned char byte; 217 const uint8_t *p = *data; 218 do 219 { 220 byte = *p++; 221 result |= static_cast<uintptr_t>(byte & 0x7F) << shift; 222 shift += 7; 223 } while (byte & 0x80); 224 *data = p; 225 if ((byte & 0x40) && (shift < (sizeof(result) << 3))) 226 result |= static_cast<uintptr_t>(~0) << shift; 227 return static_cast<intptr_t>(result); 228 } 229 230 /// Read a pointer encoded value and advance pointer 231 /// See Variable Length Data in: 232 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink 233 /// @param data reference variable holding memory pointer to decode from 234 /// @param encoding dwarf encoding type 235 /// @returns decoded value 236 static 237 uintptr_t 238 readEncodedPointer(const uint8_t** data, uint8_t encoding) 239 { 240 uintptr_t result = 0; 241 if (encoding == DW_EH_PE_omit) 242 return result; 243 const uint8_t* p = *data; 244 // first get value 245 switch (encoding & 0x0F) 246 { 247 case DW_EH_PE_absptr: 248 result = readPointerHelper<uintptr_t>(p); 249 break; 250 case DW_EH_PE_uleb128: 251 result = readULEB128(&p); 252 break; 253 case DW_EH_PE_sleb128: 254 result = static_cast<uintptr_t>(readSLEB128(&p)); 255 break; 256 case DW_EH_PE_udata2: 257 result = readPointerHelper<uint16_t>(p); 258 break; 259 case DW_EH_PE_udata4: 260 result = readPointerHelper<uint32_t>(p); 261 break; 262 case DW_EH_PE_udata8: 263 result = readPointerHelper<uint64_t>(p); 264 break; 265 case DW_EH_PE_sdata2: 266 result = readPointerHelper<int16_t>(p); 267 break; 268 case DW_EH_PE_sdata4: 269 result = readPointerHelper<int32_t>(p); 270 break; 271 case DW_EH_PE_sdata8: 272 result = readPointerHelper<int64_t>(p); 273 break; 274 default: 275 // not supported 276 abort(); 277 break; 278 } 279 // then add relative offset 280 switch (encoding & 0x70) 281 { 282 case DW_EH_PE_absptr: 283 // do nothing 284 break; 285 case DW_EH_PE_pcrel: 286 if (result) 287 result += (uintptr_t)(*data); 288 break; 289 case DW_EH_PE_textrel: 290 case DW_EH_PE_datarel: 291 case DW_EH_PE_funcrel: 292 case DW_EH_PE_aligned: 293 default: 294 // not supported 295 abort(); 296 break; 297 } 298 // then apply indirection 299 if (result && (encoding & DW_EH_PE_indirect)) 300 result = *((uintptr_t*)result); 301 *data = p; 302 return result; 303 } 304 305 static 306 void 307 call_terminate(bool native_exception, _Unwind_Exception* unwind_exception) 308 { 309 __cxa_begin_catch(unwind_exception); 310 if (native_exception) 311 { 312 // Use the stored terminate_handler if possible 313 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 314 std::__terminate(exception_header->terminateHandler); 315 } 316 std::terminate(); 317 } 318 319 #if LIBCXXABI_ARM_EHABI 320 static const void* read_target2_value(const void* ptr) 321 { 322 uintptr_t offset = *reinterpret_cast<const uintptr_t*>(ptr); 323 if (!offset) 324 return 0; 325 // "ARM EABI provides a TARGET2 relocation to describe these typeinfo 326 // pointers. The reason being it allows their precise semantics to be 327 // deferred to the linker. For bare-metal they turn into absolute 328 // relocations. For linux they turn into GOT-REL relocations." 329 // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html 330 #if LIBCXXABI_BAREMETAL 331 return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) + 332 offset); 333 #else 334 return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) + 335 offset); 336 #endif 337 } 338 339 static const __shim_type_info* 340 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo, 341 uint8_t ttypeEncoding, bool native_exception, 342 _Unwind_Exception* unwind_exception) 343 { 344 if (classInfo == 0) 345 { 346 // this should not happen. Indicates corrupted eh_table. 347 call_terminate(native_exception, unwind_exception); 348 } 349 350 assert(ttypeEncoding == DW_EH_PE_absptr && "Unexpected TTypeEncoding"); 351 (void)ttypeEncoding; 352 353 const uint8_t* ttypePtr = classInfo - ttypeIndex * sizeof(uintptr_t); 354 return reinterpret_cast<const __shim_type_info *>( 355 read_target2_value(ttypePtr)); 356 } 357 #else // !LIBCXXABI_ARM_EHABI 358 static 359 const __shim_type_info* 360 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo, 361 uint8_t ttypeEncoding, bool native_exception, 362 _Unwind_Exception* unwind_exception) 363 { 364 if (classInfo == 0) 365 { 366 // this should not happen. Indicates corrupted eh_table. 367 call_terminate(native_exception, unwind_exception); 368 } 369 switch (ttypeEncoding & 0x0F) 370 { 371 case DW_EH_PE_absptr: 372 ttypeIndex *= sizeof(void*); 373 break; 374 case DW_EH_PE_udata2: 375 case DW_EH_PE_sdata2: 376 ttypeIndex *= 2; 377 break; 378 case DW_EH_PE_udata4: 379 case DW_EH_PE_sdata4: 380 ttypeIndex *= 4; 381 break; 382 case DW_EH_PE_udata8: 383 case DW_EH_PE_sdata8: 384 ttypeIndex *= 8; 385 break; 386 default: 387 // this should not happen. Indicates corrupted eh_table. 388 call_terminate(native_exception, unwind_exception); 389 } 390 classInfo -= ttypeIndex; 391 return (const __shim_type_info*)readEncodedPointer(&classInfo, ttypeEncoding); 392 } 393 #endif // !LIBCXXABI_ARM_EHABI 394 395 /* 396 This is checking a thrown exception type, excpType, against a possibly empty 397 list of catchType's which make up an exception spec. 398 399 An exception spec acts like a catch handler, but in reverse. This "catch 400 handler" will catch an excpType if and only if none of the catchType's in 401 the list will catch a excpType. If any catchType in the list can catch an 402 excpType, then this exception spec does not catch the excpType. 403 */ 404 #if LIBCXXABI_ARM_EHABI 405 static 406 bool 407 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo, 408 uint8_t ttypeEncoding, const __shim_type_info* excpType, 409 void* adjustedPtr, _Unwind_Exception* unwind_exception) 410 { 411 if (classInfo == 0) 412 { 413 // this should not happen. Indicates corrupted eh_table. 414 call_terminate(false, unwind_exception); 415 } 416 417 assert(ttypeEncoding == DW_EH_PE_absptr && "Unexpected TTypeEncoding"); 418 (void)ttypeEncoding; 419 420 // specIndex is negative of 1-based byte offset into classInfo; 421 specIndex = -specIndex; 422 --specIndex; 423 const void** temp = reinterpret_cast<const void**>( 424 reinterpret_cast<uintptr_t>(classInfo) + 425 static_cast<uintptr_t>(specIndex) * sizeof(uintptr_t)); 426 // If any type in the spec list can catch excpType, return false, else return true 427 // adjustments to adjustedPtr are ignored. 428 while (true) 429 { 430 // ARM EHABI exception specification table (filter table) consists of 431 // several pointers which will directly point to the type info object 432 // (instead of ttypeIndex). The table will be terminated with 0. 433 const void** ttypePtr = temp++; 434 if (*ttypePtr == 0) 435 break; 436 // We can get the __shim_type_info simply by performing a 437 // R_ARM_TARGET2 relocation, and cast the result to __shim_type_info. 438 const __shim_type_info* catchType = 439 static_cast<const __shim_type_info*>(read_target2_value(ttypePtr)); 440 void* tempPtr = adjustedPtr; 441 if (catchType->can_catch(excpType, tempPtr)) 442 return false; 443 } 444 return true; 445 } 446 #else 447 static 448 bool 449 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo, 450 uint8_t ttypeEncoding, const __shim_type_info* excpType, 451 void* adjustedPtr, _Unwind_Exception* unwind_exception) 452 { 453 if (classInfo == 0) 454 { 455 // this should not happen. Indicates corrupted eh_table. 456 call_terminate(false, unwind_exception); 457 } 458 // specIndex is negative of 1-based byte offset into classInfo; 459 specIndex = -specIndex; 460 --specIndex; 461 const uint8_t* temp = classInfo + specIndex; 462 // If any type in the spec list can catch excpType, return false, else return true 463 // adjustments to adjustedPtr are ignored. 464 while (true) 465 { 466 uint64_t ttypeIndex = readULEB128(&temp); 467 if (ttypeIndex == 0) 468 break; 469 const __shim_type_info* catchType = get_shim_type_info(ttypeIndex, 470 classInfo, 471 ttypeEncoding, 472 true, 473 unwind_exception); 474 void* tempPtr = adjustedPtr; 475 if (catchType->can_catch(excpType, tempPtr)) 476 return false; 477 } 478 return true; 479 } 480 #endif 481 482 static 483 void* 484 get_thrown_object_ptr(_Unwind_Exception* unwind_exception) 485 { 486 // Even for foreign exceptions, the exception object is *probably* at unwind_exception + 1 487 // Regardless, this library is prohibited from touching a foreign exception 488 void* adjustedPtr = unwind_exception + 1; 489 if (unwind_exception->exception_class == kOurDependentExceptionClass) 490 adjustedPtr = ((__cxa_dependent_exception*)adjustedPtr - 1)->primaryException; 491 return adjustedPtr; 492 } 493 494 namespace 495 { 496 497 struct scan_results 498 { 499 int64_t ttypeIndex; // > 0 catch handler, < 0 exception spec handler, == 0 a cleanup 500 const uint8_t* actionRecord; // Currently unused. Retained to ease future maintenance. 501 const uint8_t* languageSpecificData; // Needed only for __cxa_call_unexpected 502 uintptr_t landingPad; // null -> nothing found, else something found 503 void* adjustedPtr; // Used in cxa_exception.cpp 504 _Unwind_Reason_Code reason; // One of _URC_FATAL_PHASE1_ERROR, 505 // _URC_FATAL_PHASE2_ERROR, 506 // _URC_CONTINUE_UNWIND, 507 // _URC_HANDLER_FOUND 508 }; 509 510 } // unnamed namespace 511 512 static 513 void 514 set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context, 515 const scan_results& results) 516 { 517 #if defined(__USING_SJLJ_EXCEPTIONS__) 518 #define __builtin_eh_return_data_regno(regno) regno 519 #endif 520 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0), 521 reinterpret_cast<uintptr_t>(unwind_exception)); 522 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 523 static_cast<uintptr_t>(results.ttypeIndex)); 524 _Unwind_SetIP(context, results.landingPad); 525 } 526 527 /* 528 There are 3 types of scans needed: 529 530 1. Scan for handler with native or foreign exception. If handler found, 531 save state and return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND. 532 May also report an error on invalid input. 533 May terminate for invalid exception table. 534 _UA_SEARCH_PHASE 535 536 2. Scan for handler with foreign exception. Must return _URC_HANDLER_FOUND, 537 or call terminate. 538 _UA_CLEANUP_PHASE && _UA_HANDLER_FRAME && !native_exception 539 540 3. Scan for cleanups. If a handler is found and this isn't forced unwind, 541 then terminate, otherwise ignore the handler and keep looking for cleanup. 542 If a cleanup is found, return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND. 543 May also report an error on invalid input. 544 May terminate for invalid exception table. 545 _UA_CLEANUP_PHASE && !_UA_HANDLER_FRAME 546 */ 547 548 static void scan_eh_tab(scan_results &results, _Unwind_Action actions, 549 bool native_exception, 550 _Unwind_Exception *unwind_exception, 551 _Unwind_Context *context) { 552 // Initialize results to found nothing but an error 553 results.ttypeIndex = 0; 554 results.actionRecord = 0; 555 results.languageSpecificData = 0; 556 results.landingPad = 0; 557 results.adjustedPtr = 0; 558 results.reason = _URC_FATAL_PHASE1_ERROR; 559 // Check for consistent actions 560 if (actions & _UA_SEARCH_PHASE) 561 { 562 // Do Phase 1 563 if (actions & (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME | _UA_FORCE_UNWIND)) 564 { 565 // None of these flags should be set during Phase 1 566 // Client error 567 results.reason = _URC_FATAL_PHASE1_ERROR; 568 return; 569 } 570 } 571 else if (actions & _UA_CLEANUP_PHASE) 572 { 573 if ((actions & _UA_HANDLER_FRAME) && (actions & _UA_FORCE_UNWIND)) 574 { 575 // _UA_HANDLER_FRAME should only be set if phase 1 found a handler. 576 // If _UA_FORCE_UNWIND is set, phase 1 shouldn't have happened. 577 // Client error 578 results.reason = _URC_FATAL_PHASE2_ERROR; 579 return; 580 } 581 } 582 else // Neither _UA_SEARCH_PHASE nor _UA_CLEANUP_PHASE is set 583 { 584 // One of these should be set. 585 // Client error 586 results.reason = _URC_FATAL_PHASE1_ERROR; 587 return; 588 } 589 // Start scan by getting exception table address 590 const uint8_t *lsda = (const uint8_t *)_Unwind_GetLanguageSpecificData(context); 591 if (lsda == 0) 592 { 593 // There is no exception table 594 results.reason = _URC_CONTINUE_UNWIND; 595 return; 596 } 597 results.languageSpecificData = lsda; 598 // Get the current instruction pointer and offset it before next 599 // instruction in the current frame which threw the exception. 600 uintptr_t ip = _Unwind_GetIP(context) - 1; 601 // Get beginning current frame's code (as defined by the 602 // emitted dwarf code) 603 uintptr_t funcStart = _Unwind_GetRegionStart(context); 604 #ifdef __USING_SJLJ_EXCEPTIONS__ 605 if (ip == uintptr_t(-1)) 606 { 607 // no action 608 results.reason = _URC_CONTINUE_UNWIND; 609 return; 610 } 611 else if (ip == 0) 612 call_terminate(native_exception, unwind_exception); 613 // ip is 1-based index into call site table 614 #else // !__USING_SJLJ_EXCEPTIONS__ 615 uintptr_t ipOffset = ip - funcStart; 616 #endif // !defined(_USING_SLJL_EXCEPTIONS__) 617 const uint8_t* classInfo = NULL; 618 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding 619 // dwarf emission 620 // Parse LSDA header. 621 uint8_t lpStartEncoding = *lsda++; 622 const uint8_t* lpStart = (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding); 623 if (lpStart == 0) 624 lpStart = (const uint8_t*)funcStart; 625 uint8_t ttypeEncoding = *lsda++; 626 if (ttypeEncoding != DW_EH_PE_omit) 627 { 628 // Calculate type info locations in emitted dwarf code which 629 // were flagged by type info arguments to llvm.eh.selector 630 // intrinsic 631 uintptr_t classInfoOffset = readULEB128(&lsda); 632 classInfo = lsda + classInfoOffset; 633 } 634 // Walk call-site table looking for range that 635 // includes current PC. 636 uint8_t callSiteEncoding = *lsda++; 637 #ifdef __USING_SJLJ_EXCEPTIONS__ 638 (void)callSiteEncoding; // When using SjLj exceptions, callSiteEncoding is never used 639 #endif 640 uint32_t callSiteTableLength = static_cast<uint32_t>(readULEB128(&lsda)); 641 const uint8_t* callSiteTableStart = lsda; 642 const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength; 643 const uint8_t* actionTableStart = callSiteTableEnd; 644 const uint8_t* callSitePtr = callSiteTableStart; 645 while (callSitePtr < callSiteTableEnd) 646 { 647 // There is one entry per call site. 648 #ifndef __USING_SJLJ_EXCEPTIONS__ 649 // The call sites are non-overlapping in [start, start+length) 650 // The call sites are ordered in increasing value of start 651 uintptr_t start = readEncodedPointer(&callSitePtr, callSiteEncoding); 652 uintptr_t length = readEncodedPointer(&callSitePtr, callSiteEncoding); 653 uintptr_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding); 654 uintptr_t actionEntry = readULEB128(&callSitePtr); 655 if ((start <= ipOffset) && (ipOffset < (start + length))) 656 #else // __USING_SJLJ_EXCEPTIONS__ 657 // ip is 1-based index into this table 658 uintptr_t landingPad = readULEB128(&callSitePtr); 659 uintptr_t actionEntry = readULEB128(&callSitePtr); 660 if (--ip == 0) 661 #endif // __USING_SJLJ_EXCEPTIONS__ 662 { 663 // Found the call site containing ip. 664 #ifndef __USING_SJLJ_EXCEPTIONS__ 665 if (landingPad == 0) 666 { 667 // No handler here 668 results.reason = _URC_CONTINUE_UNWIND; 669 return; 670 } 671 landingPad = (uintptr_t)lpStart + landingPad; 672 #else // __USING_SJLJ_EXCEPTIONS__ 673 ++landingPad; 674 #endif // __USING_SJLJ_EXCEPTIONS__ 675 if (actionEntry == 0) 676 { 677 // Found a cleanup 678 // If this is a type 1 or type 2 search, there are no handlers 679 // If this is a type 3 search, you want to install the cleanup. 680 if ((actions & _UA_CLEANUP_PHASE) && !(actions & _UA_HANDLER_FRAME)) 681 { 682 results.ttypeIndex = 0; // Redundant but clarifying 683 results.landingPad = landingPad; 684 results.reason = _URC_HANDLER_FOUND; 685 return; 686 } 687 // No handler here 688 results.reason = _URC_CONTINUE_UNWIND; 689 return; 690 } 691 // Convert 1-based byte offset into 692 const uint8_t* action = actionTableStart + (actionEntry - 1); 693 // Scan action entries until you find a matching handler, cleanup, or the end of action list 694 while (true) 695 { 696 const uint8_t* actionRecord = action; 697 int64_t ttypeIndex = readSLEB128(&action); 698 if (ttypeIndex > 0) 699 { 700 // Found a catch, does it actually catch? 701 // First check for catch (...) 702 const __shim_type_info* catchType = 703 get_shim_type_info(static_cast<uint64_t>(ttypeIndex), 704 classInfo, ttypeEncoding, 705 native_exception, unwind_exception); 706 if (catchType == 0) 707 { 708 // Found catch (...) catches everything, including foreign exceptions 709 // If this is a type 1 search save state and return _URC_HANDLER_FOUND 710 // If this is a type 2 search save state and return _URC_HANDLER_FOUND 711 // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1! 712 // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan 713 if ((actions & _UA_SEARCH_PHASE) || (actions & _UA_HANDLER_FRAME)) 714 { 715 // Save state and return _URC_HANDLER_FOUND 716 results.ttypeIndex = ttypeIndex; 717 results.actionRecord = actionRecord; 718 results.landingPad = landingPad; 719 results.adjustedPtr = get_thrown_object_ptr(unwind_exception); 720 results.reason = _URC_HANDLER_FOUND; 721 return; 722 } 723 else if (!(actions & _UA_FORCE_UNWIND)) 724 { 725 // It looks like the exception table has changed 726 // on us. Likely stack corruption! 727 call_terminate(native_exception, unwind_exception); 728 } 729 } 730 // Else this is a catch (T) clause and will never 731 // catch a foreign exception 732 else if (native_exception) 733 { 734 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 735 void* adjustedPtr = get_thrown_object_ptr(unwind_exception); 736 const __shim_type_info* excpType = 737 static_cast<const __shim_type_info*>(exception_header->exceptionType); 738 if (adjustedPtr == 0 || excpType == 0) 739 { 740 // Something very bad happened 741 call_terminate(native_exception, unwind_exception); 742 } 743 if (catchType->can_catch(excpType, adjustedPtr)) 744 { 745 // Found a matching handler 746 // If this is a type 1 search save state and return _URC_HANDLER_FOUND 747 // If this is a type 3 search and !_UA_FORCE_UNWIND, we should have found this in phase 1! 748 // If this is a type 3 search and _UA_FORCE_UNWIND, ignore handler and continue scan 749 if (actions & _UA_SEARCH_PHASE) 750 { 751 // Save state and return _URC_HANDLER_FOUND 752 results.ttypeIndex = ttypeIndex; 753 results.actionRecord = actionRecord; 754 results.landingPad = landingPad; 755 results.adjustedPtr = adjustedPtr; 756 results.reason = _URC_HANDLER_FOUND; 757 return; 758 } 759 else if (!(actions & _UA_FORCE_UNWIND)) 760 { 761 // It looks like the exception table has changed 762 // on us. Likely stack corruption! 763 call_terminate(native_exception, unwind_exception); 764 } 765 } 766 } 767 // Scan next action ... 768 } 769 else if (ttypeIndex < 0) 770 { 771 // Found an exception spec. If this is a foreign exception, 772 // it is always caught. 773 if (native_exception) 774 { 775 // Does the exception spec catch this native exception? 776 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 777 void* adjustedPtr = get_thrown_object_ptr(unwind_exception); 778 const __shim_type_info* excpType = 779 static_cast<const __shim_type_info*>(exception_header->exceptionType); 780 if (adjustedPtr == 0 || excpType == 0) 781 { 782 // Something very bad happened 783 call_terminate(native_exception, unwind_exception); 784 } 785 if (exception_spec_can_catch(ttypeIndex, classInfo, 786 ttypeEncoding, excpType, 787 adjustedPtr, unwind_exception)) 788 { 789 // native exception caught by exception spec 790 // If this is a type 1 search, save state and return _URC_HANDLER_FOUND 791 // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1! 792 // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan 793 if (actions & _UA_SEARCH_PHASE) 794 { 795 // Save state and return _URC_HANDLER_FOUND 796 results.ttypeIndex = ttypeIndex; 797 results.actionRecord = actionRecord; 798 results.landingPad = landingPad; 799 results.adjustedPtr = adjustedPtr; 800 results.reason = _URC_HANDLER_FOUND; 801 return; 802 } 803 else if (!(actions & _UA_FORCE_UNWIND)) 804 { 805 // It looks like the exception table has changed 806 // on us. Likely stack corruption! 807 call_terminate(native_exception, unwind_exception); 808 } 809 } 810 } 811 else 812 { 813 // foreign exception caught by exception spec 814 // If this is a type 1 search, save state and return _URC_HANDLER_FOUND 815 // If this is a type 2 search, save state and return _URC_HANDLER_FOUND 816 // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1! 817 // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan 818 if ((actions & _UA_SEARCH_PHASE) || (actions & _UA_HANDLER_FRAME)) 819 { 820 // Save state and return _URC_HANDLER_FOUND 821 results.ttypeIndex = ttypeIndex; 822 results.actionRecord = actionRecord; 823 results.landingPad = landingPad; 824 results.adjustedPtr = get_thrown_object_ptr(unwind_exception); 825 results.reason = _URC_HANDLER_FOUND; 826 return; 827 } 828 else if (!(actions & _UA_FORCE_UNWIND)) 829 { 830 // It looks like the exception table has changed 831 // on us. Likely stack corruption! 832 call_terminate(native_exception, unwind_exception); 833 } 834 } 835 // Scan next action ... 836 } 837 else // ttypeIndex == 0 838 { 839 // Found a cleanup 840 // If this is a type 1 search, ignore it and continue scan 841 // If this is a type 2 search, ignore it and continue scan 842 // If this is a type 3 search, save state and return _URC_HANDLER_FOUND 843 if ((actions & _UA_CLEANUP_PHASE) && !(actions & _UA_HANDLER_FRAME)) 844 { 845 // Save state and return _URC_HANDLER_FOUND 846 results.ttypeIndex = ttypeIndex; 847 results.actionRecord = actionRecord; 848 results.landingPad = landingPad; 849 results.adjustedPtr = get_thrown_object_ptr(unwind_exception); 850 results.reason = _URC_HANDLER_FOUND; 851 return; 852 } 853 } 854 const uint8_t* temp = action; 855 int64_t actionOffset = readSLEB128(&temp); 856 if (actionOffset == 0) 857 { 858 // End of action list, no matching handler or cleanup found 859 results.reason = _URC_CONTINUE_UNWIND; 860 return; 861 } 862 // Go to next action 863 action += actionOffset; 864 } // there is no break out of this loop, only return 865 } 866 #ifndef __USING_SJLJ_EXCEPTIONS__ 867 else if (ipOffset < start) 868 { 869 // There is no call site for this ip 870 // Something bad has happened. We should never get here. 871 // Possible stack corruption. 872 call_terminate(native_exception, unwind_exception); 873 } 874 #endif // !__USING_SJLJ_EXCEPTIONS__ 875 } // there might be some tricky cases which break out of this loop 876 877 // It is possible that no eh table entry specify how to handle 878 // this exception. By spec, terminate it immediately. 879 call_terminate(native_exception, unwind_exception); 880 } 881 882 // public API 883 884 /* 885 The personality function branches on actions like so: 886 887 _UA_SEARCH_PHASE 888 889 If _UA_CLEANUP_PHASE or _UA_HANDLER_FRAME or _UA_FORCE_UNWIND there's 890 an error from above, return _URC_FATAL_PHASE1_ERROR. 891 892 Scan for anything that could stop unwinding: 893 894 1. A catch clause that will catch this exception 895 (will never catch foreign). 896 2. A catch (...) (will always catch foreign). 897 3. An exception spec that will catch this exception 898 (will always catch foreign). 899 If a handler is found 900 If not foreign 901 Save state in header 902 return _URC_HANDLER_FOUND 903 Else a handler not found 904 return _URC_CONTINUE_UNWIND 905 906 _UA_CLEANUP_PHASE 907 908 If _UA_HANDLER_FRAME 909 If _UA_FORCE_UNWIND 910 How did this happen? return _URC_FATAL_PHASE2_ERROR 911 If foreign 912 Do _UA_SEARCH_PHASE to recover state 913 else 914 Recover state from header 915 Transfer control to landing pad. return _URC_INSTALL_CONTEXT 916 917 Else 918 919 This branch handles both normal C++ non-catching handlers (cleanups) 920 and forced unwinding. 921 Scan for anything that can not stop unwinding: 922 923 1. A cleanup. 924 925 If a cleanup is found 926 transfer control to it. return _URC_INSTALL_CONTEXT 927 Else a cleanup is not found: return _URC_CONTINUE_UNWIND 928 */ 929 930 #if !LIBCXXABI_ARM_EHABI 931 _Unwind_Reason_Code 932 #ifdef __USING_SJLJ_EXCEPTIONS__ 933 __gxx_personality_sj0 934 #else 935 __gxx_personality_v0 936 #endif 937 (int version, _Unwind_Action actions, uint64_t exceptionClass, 938 _Unwind_Exception* unwind_exception, _Unwind_Context* context) 939 { 940 if (version != 1 || unwind_exception == 0 || context == 0) 941 return _URC_FATAL_PHASE1_ERROR; 942 943 bool native_exception = (exceptionClass & get_vendor_and_language) == 944 (kOurExceptionClass & get_vendor_and_language); 945 scan_results results; 946 if (actions & _UA_SEARCH_PHASE) 947 { 948 // Phase 1 search: All we're looking for in phase 1 is a handler that 949 // halts unwinding 950 scan_eh_tab(results, actions, native_exception, unwind_exception, context); 951 if (results.reason == _URC_HANDLER_FOUND) 952 { 953 // Found one. Can we cache the results somewhere to optimize phase 2? 954 if (native_exception) 955 { 956 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 957 exception_header->handlerSwitchValue = static_cast<int>(results.ttypeIndex); 958 exception_header->actionRecord = results.actionRecord; 959 exception_header->languageSpecificData = results.languageSpecificData; 960 exception_header->catchTemp = reinterpret_cast<void*>(results.landingPad); 961 exception_header->adjustedPtr = results.adjustedPtr; 962 } 963 return _URC_HANDLER_FOUND; 964 } 965 // Did not find a catching-handler. Return the results of the scan 966 // (normally _URC_CONTINUE_UNWIND, but could have been _URC_FATAL_PHASE1_ERROR 967 // if we were called improperly). 968 return results.reason; 969 } 970 if (actions & _UA_CLEANUP_PHASE) 971 { 972 // Phase 2 search: 973 // Did we find a catching handler in phase 1? 974 if (actions & _UA_HANDLER_FRAME) 975 { 976 // Yes, phase 1 said we have a catching handler here. 977 // Did we cache the results of the scan? 978 if (native_exception) 979 { 980 // Yes, reload the results from the cache. 981 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 982 results.ttypeIndex = exception_header->handlerSwitchValue; 983 results.actionRecord = exception_header->actionRecord; 984 results.languageSpecificData = exception_header->languageSpecificData; 985 results.landingPad = reinterpret_cast<uintptr_t>(exception_header->catchTemp); 986 results.adjustedPtr = exception_header->adjustedPtr; 987 } 988 else 989 { 990 // No, do the scan again to reload the results. 991 scan_eh_tab(results, actions, native_exception, unwind_exception, context); 992 // Phase 1 told us we would find a handler. Now in Phase 2 we 993 // didn't find a handler. The eh table should not be changing! 994 if (results.reason != _URC_HANDLER_FOUND) 995 call_terminate(native_exception, unwind_exception); 996 } 997 // Jump to the handler 998 set_registers(unwind_exception, context, results); 999 return _URC_INSTALL_CONTEXT; 1000 } 1001 // Either we didn't do a phase 1 search (due to forced unwinding), or 1002 // phase 1 reported no catching-handlers. 1003 // Search for a (non-catching) cleanup 1004 scan_eh_tab(results, actions, native_exception, unwind_exception, context); 1005 if (results.reason == _URC_HANDLER_FOUND) 1006 { 1007 // Found a non-catching handler. Jump to it: 1008 set_registers(unwind_exception, context, results); 1009 return _URC_INSTALL_CONTEXT; 1010 } 1011 // Did not find a cleanup. Return the results of the scan 1012 // (normally _URC_CONTINUE_UNWIND, but could have been _URC_FATAL_PHASE2_ERROR 1013 // if we were called improperly). 1014 return results.reason; 1015 } 1016 // We were called improperly: neither a phase 1 or phase 2 search 1017 return _URC_FATAL_PHASE1_ERROR; 1018 } 1019 #else 1020 1021 extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*, 1022 _Unwind_Context*); 1023 1024 // Helper function to unwind one frame. 1025 // ARM EHABI 7.3 and 7.4: If the personality function returns _URC_CONTINUE_UNWIND, the 1026 // personality routine should update the virtual register set (VRS) according to the 1027 // corresponding frame unwinding instructions (ARM EHABI 9.3.) 1028 static _Unwind_Reason_Code continue_unwind(_Unwind_Exception* unwind_exception, 1029 _Unwind_Context* context) 1030 { 1031 if (__gnu_unwind_frame(unwind_exception, context) != _URC_OK) 1032 return _URC_FAILURE; 1033 return _URC_CONTINUE_UNWIND; 1034 } 1035 1036 // ARM register names 1037 #if !LIBCXXABI_USE_LLVM_UNWINDER 1038 static const uint32_t REG_UCB = 12; // Register to save _Unwind_Control_Block 1039 #endif 1040 static const uint32_t REG_SP = 13; 1041 1042 static void save_results_to_barrier_cache(_Unwind_Exception* unwind_exception, 1043 const scan_results& results) 1044 { 1045 unwind_exception->barrier_cache.bitpattern[0] = (uint32_t)results.adjustedPtr; 1046 unwind_exception->barrier_cache.bitpattern[1] = (uint32_t)results.actionRecord; 1047 unwind_exception->barrier_cache.bitpattern[2] = (uint32_t)results.languageSpecificData; 1048 unwind_exception->barrier_cache.bitpattern[3] = (uint32_t)results.landingPad; 1049 unwind_exception->barrier_cache.bitpattern[4] = (uint32_t)results.ttypeIndex; 1050 } 1051 1052 static void load_results_from_barrier_cache(scan_results& results, 1053 const _Unwind_Exception* unwind_exception) 1054 { 1055 results.adjustedPtr = (void*)unwind_exception->barrier_cache.bitpattern[0]; 1056 results.actionRecord = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[1]; 1057 results.languageSpecificData = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2]; 1058 results.landingPad = (uintptr_t)unwind_exception->barrier_cache.bitpattern[3]; 1059 results.ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4]; 1060 } 1061 1062 extern "C" _Unwind_Reason_Code 1063 __gxx_personality_v0(_Unwind_State state, 1064 _Unwind_Exception* unwind_exception, 1065 _Unwind_Context* context) 1066 { 1067 if (unwind_exception == 0 || context == 0) 1068 return _URC_FATAL_PHASE1_ERROR; 1069 1070 bool native_exception = (unwind_exception->exception_class & get_vendor_and_language) == 1071 (kOurExceptionClass & get_vendor_and_language); 1072 1073 #if !LIBCXXABI_USE_LLVM_UNWINDER 1074 // Copy the address of _Unwind_Control_Block to r12 so that 1075 // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can 1076 // return correct address. 1077 _Unwind_SetGR(context, REG_UCB, reinterpret_cast<uint32_t>(unwind_exception)); 1078 #endif 1079 1080 // Check the undocumented force unwinding behavior 1081 bool is_force_unwinding = state & _US_FORCE_UNWIND; 1082 state &= ~_US_FORCE_UNWIND; 1083 1084 scan_results results; 1085 switch (state) { 1086 case _US_VIRTUAL_UNWIND_FRAME: 1087 if (is_force_unwinding) 1088 return continue_unwind(unwind_exception, context); 1089 1090 // Phase 1 search: All we're looking for in phase 1 is a handler that halts unwinding 1091 scan_eh_tab(results, _UA_SEARCH_PHASE, native_exception, unwind_exception, context); 1092 if (results.reason == _URC_HANDLER_FOUND) 1093 { 1094 unwind_exception->barrier_cache.sp = _Unwind_GetGR(context, REG_SP); 1095 if (native_exception) 1096 save_results_to_barrier_cache(unwind_exception, results); 1097 return _URC_HANDLER_FOUND; 1098 } 1099 // Did not find the catch handler 1100 if (results.reason == _URC_CONTINUE_UNWIND) 1101 return continue_unwind(unwind_exception, context); 1102 return results.reason; 1103 1104 case _US_UNWIND_FRAME_STARTING: 1105 // TODO: Support force unwinding in the phase 2 search. 1106 // NOTE: In order to call the cleanup functions, _Unwind_ForcedUnwind() 1107 // will call this personality function with (_US_FORCE_UNWIND | 1108 // _US_UNWIND_FRAME_STARTING). 1109 1110 // Phase 2 search 1111 if (unwind_exception->barrier_cache.sp == _Unwind_GetGR(context, REG_SP)) 1112 { 1113 // Found a catching handler in phase 1 1114 if (native_exception) 1115 { 1116 // Load the result from the native exception barrier cache. 1117 load_results_from_barrier_cache(results, unwind_exception); 1118 results.reason = _URC_HANDLER_FOUND; 1119 } 1120 else 1121 { 1122 // Search for the catching handler again for the foreign exception. 1123 scan_eh_tab(results, static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME), 1124 native_exception, unwind_exception, context); 1125 if (results.reason != _URC_HANDLER_FOUND) // phase1 search should guarantee to find one 1126 call_terminate(native_exception, unwind_exception); 1127 } 1128 1129 // Install the context for the catching handler 1130 set_registers(unwind_exception, context, results); 1131 return _URC_INSTALL_CONTEXT; 1132 } 1133 1134 // Either we didn't do a phase 1 search (due to forced unwinding), or 1135 // phase 1 reported no catching-handlers. 1136 // Search for a (non-catching) cleanup 1137 scan_eh_tab(results, _UA_CLEANUP_PHASE, native_exception, unwind_exception, context); 1138 if (results.reason == _URC_HANDLER_FOUND) 1139 { 1140 // Found a non-catching handler 1141 1142 // ARM EHABI 8.4.2: Before we can jump to the cleanup handler, we have to setup some 1143 // internal data structures, so that __cxa_end_cleanup() can get unwind_exception from 1144 // __cxa_get_globals(). 1145 __cxa_begin_cleanup(unwind_exception); 1146 1147 // Install the context for the cleanup handler 1148 set_registers(unwind_exception, context, results); 1149 return _URC_INSTALL_CONTEXT; 1150 } 1151 1152 // Did not find any handler 1153 if (results.reason == _URC_CONTINUE_UNWIND) 1154 return continue_unwind(unwind_exception, context); 1155 return results.reason; 1156 1157 case _US_UNWIND_FRAME_RESUME: 1158 return continue_unwind(unwind_exception, context); 1159 } 1160 1161 // We were called improperly: neither a phase 1 or phase 2 search 1162 return _URC_FATAL_PHASE1_ERROR; 1163 } 1164 #endif 1165 1166 1167 __attribute__((noreturn)) 1168 void 1169 __cxa_call_unexpected(void* arg) 1170 { 1171 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(arg); 1172 if (unwind_exception == 0) 1173 call_terminate(false, unwind_exception); 1174 __cxa_begin_catch(unwind_exception); 1175 bool native_old_exception = 1176 (unwind_exception->exception_class & get_vendor_and_language) == 1177 (kOurExceptionClass & get_vendor_and_language); 1178 std::unexpected_handler u_handler; 1179 std::terminate_handler t_handler; 1180 __cxa_exception* old_exception_header = 0; 1181 int64_t ttypeIndex; 1182 const uint8_t* lsda; 1183 if (native_old_exception) 1184 { 1185 old_exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 1186 t_handler = old_exception_header->terminateHandler; 1187 u_handler = old_exception_header->unexpectedHandler; 1188 // If std::__unexpected(u_handler) rethrows the same exception, 1189 // these values get overwritten by the rethrow. So save them now: 1190 #if LIBCXXABI_ARM_EHABI 1191 ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4]; 1192 lsda = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2]; 1193 #else 1194 ttypeIndex = old_exception_header->handlerSwitchValue; 1195 lsda = old_exception_header->languageSpecificData; 1196 #endif 1197 } 1198 else 1199 { 1200 t_handler = std::get_terminate(); 1201 u_handler = std::get_unexpected(); 1202 } 1203 try 1204 { 1205 std::__unexpected(u_handler); 1206 } 1207 catch (...) 1208 { 1209 // If the old exception is foreign, then all we can do is terminate. 1210 // We have no way to recover the needed old exception spec. There's 1211 // no way to pass that information here. And the personality routine 1212 // can't call us directly and do anything but terminate() if we throw 1213 // from here. 1214 if (native_old_exception) 1215 { 1216 // Have: 1217 // old_exception_header->languageSpecificData 1218 // old_exception_header->actionRecord 1219 // Need 1220 // const uint8_t* classInfo 1221 // uint8_t ttypeEncoding 1222 uint8_t lpStartEncoding = *lsda++; 1223 const uint8_t* lpStart = (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding); 1224 (void)lpStart; // purposefully unused. Just needed to increment lsda. 1225 uint8_t ttypeEncoding = *lsda++; 1226 if (ttypeEncoding == DW_EH_PE_omit) 1227 std::__terminate(t_handler); 1228 uintptr_t classInfoOffset = readULEB128(&lsda); 1229 const uint8_t* classInfo = lsda + classInfoOffset; 1230 // Is this new exception catchable by the exception spec at ttypeIndex? 1231 // The answer is obviously yes if the new and old exceptions are the same exception 1232 // If no 1233 // throw; 1234 __cxa_eh_globals* globals = __cxa_get_globals_fast(); 1235 __cxa_exception* new_exception_header = globals->caughtExceptions; 1236 if (new_exception_header == 0) 1237 // This shouldn't be able to happen! 1238 std::__terminate(t_handler); 1239 bool native_new_exception = 1240 (new_exception_header->unwindHeader.exception_class & get_vendor_and_language) == 1241 (kOurExceptionClass & get_vendor_and_language); 1242 void* adjustedPtr; 1243 if (native_new_exception && (new_exception_header != old_exception_header)) 1244 { 1245 const __shim_type_info* excpType = 1246 static_cast<const __shim_type_info*>(new_exception_header->exceptionType); 1247 adjustedPtr = 1248 new_exception_header->unwindHeader.exception_class == kOurDependentExceptionClass ? 1249 ((__cxa_dependent_exception*)new_exception_header)->primaryException : 1250 new_exception_header + 1; 1251 if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding, 1252 excpType, adjustedPtr, unwind_exception)) 1253 { 1254 // We need to __cxa_end_catch, but for the old exception, 1255 // not the new one. This is a little tricky ... 1256 // Disguise new_exception_header as a rethrown exception, but 1257 // don't actually rethrow it. This means you can temporarily 1258 // end the catch clause enclosing new_exception_header without 1259 // __cxa_end_catch destroying new_exception_header. 1260 new_exception_header->handlerCount = -new_exception_header->handlerCount; 1261 globals->uncaughtExceptions += 1; 1262 // Call __cxa_end_catch for new_exception_header 1263 __cxa_end_catch(); 1264 // Call __cxa_end_catch for old_exception_header 1265 __cxa_end_catch(); 1266 // Renter this catch clause with new_exception_header 1267 __cxa_begin_catch(&new_exception_header->unwindHeader); 1268 // Rethrow new_exception_header 1269 throw; 1270 } 1271 } 1272 // Will a std::bad_exception be catchable by the exception spec at 1273 // ttypeIndex? 1274 // If no 1275 // throw std::bad_exception(); 1276 const __shim_type_info* excpType = 1277 static_cast<const __shim_type_info*>(&typeid(std::bad_exception)); 1278 std::bad_exception be; 1279 adjustedPtr = &be; 1280 if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding, 1281 excpType, adjustedPtr, unwind_exception)) 1282 { 1283 // We need to __cxa_end_catch for both the old exception and the 1284 // new exception. Technically we should do it in that order. 1285 // But it is expedient to do it in the opposite order: 1286 // Call __cxa_end_catch for new_exception_header 1287 __cxa_end_catch(); 1288 // Throw std::bad_exception will __cxa_end_catch for 1289 // old_exception_header 1290 throw be; 1291 } 1292 } 1293 } 1294 std::__terminate(t_handler); 1295 } 1296 1297 } // extern "C" 1298 1299 } // __cxxabiv1 1300