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