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