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