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