1 //===----------------------------------------------------------------------===// 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_datarel: 300 assert((base != 0) && "DW_EH_PE_datarel is invalid with a base of 0"); 301 if (result) 302 result += base; 303 break; 304 case DW_EH_PE_textrel: 305 case DW_EH_PE_funcrel: 306 case DW_EH_PE_aligned: 307 default: 308 // not supported 309 abort(); 310 break; 311 } 312 // then apply indirection 313 if (result && (encoding & DW_EH_PE_indirect)) 314 result = *((uintptr_t*)result); 315 *data = p; 316 return result; 317 } 318 319 static 320 void 321 call_terminate(bool native_exception, _Unwind_Exception* unwind_exception) 322 { 323 __cxa_begin_catch(unwind_exception); 324 if (native_exception) 325 { 326 // Use the stored terminate_handler if possible 327 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 328 std::__terminate(exception_header->terminateHandler); 329 } 330 std::terminate(); 331 } 332 333 #if defined(_LIBCXXABI_ARM_EHABI) 334 static const void* read_target2_value(const void* ptr) 335 { 336 uintptr_t offset = *reinterpret_cast<const uintptr_t*>(ptr); 337 if (!offset) 338 return 0; 339 // "ARM EABI provides a TARGET2 relocation to describe these typeinfo 340 // pointers. The reason being it allows their precise semantics to be 341 // deferred to the linker. For bare-metal they turn into absolute 342 // relocations. For linux they turn into GOT-REL relocations." 343 // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html 344 #if defined(LIBCXXABI_BAREMETAL) 345 return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) + 346 offset); 347 #else 348 return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) + 349 offset); 350 #endif 351 } 352 353 static const __shim_type_info* 354 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo, 355 uint8_t ttypeEncoding, bool native_exception, 356 _Unwind_Exception* unwind_exception, uintptr_t /*base*/ = 0) 357 { 358 if (classInfo == 0) 359 { 360 // this should not happen. Indicates corrupted eh_table. 361 call_terminate(native_exception, unwind_exception); 362 } 363 364 assert(((ttypeEncoding == DW_EH_PE_absptr) || // LLVM or GCC 4.6 365 (ttypeEncoding == DW_EH_PE_pcrel) || // GCC 4.7 baremetal 366 (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) && // GCC 4.7 linux 367 "Unexpected TTypeEncoding"); 368 (void)ttypeEncoding; 369 370 const uint8_t* ttypePtr = classInfo - ttypeIndex * sizeof(uintptr_t); 371 return reinterpret_cast<const __shim_type_info *>( 372 read_target2_value(ttypePtr)); 373 } 374 #else // !defined(_LIBCXXABI_ARM_EHABI) 375 static 376 const __shim_type_info* 377 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo, 378 uint8_t ttypeEncoding, bool native_exception, 379 _Unwind_Exception* unwind_exception, uintptr_t base = 0) 380 { 381 if (classInfo == 0) 382 { 383 // this should not happen. Indicates corrupted eh_table. 384 call_terminate(native_exception, unwind_exception); 385 } 386 switch (ttypeEncoding & 0x0F) 387 { 388 case DW_EH_PE_absptr: 389 ttypeIndex *= sizeof(void*); 390 break; 391 case DW_EH_PE_udata2: 392 case DW_EH_PE_sdata2: 393 ttypeIndex *= 2; 394 break; 395 case DW_EH_PE_udata4: 396 case DW_EH_PE_sdata4: 397 ttypeIndex *= 4; 398 break; 399 case DW_EH_PE_udata8: 400 case DW_EH_PE_sdata8: 401 ttypeIndex *= 8; 402 break; 403 default: 404 // this should not happen. Indicates corrupted eh_table. 405 call_terminate(native_exception, unwind_exception); 406 } 407 classInfo -= ttypeIndex; 408 return (const __shim_type_info*)readEncodedPointer(&classInfo, 409 ttypeEncoding, base); 410 } 411 #endif // !defined(_LIBCXXABI_ARM_EHABI) 412 413 /* 414 This is checking a thrown exception type, excpType, against a possibly empty 415 list of catchType's which make up an exception spec. 416 417 An exception spec acts like a catch handler, but in reverse. This "catch 418 handler" will catch an excpType if and only if none of the catchType's in 419 the list will catch a excpType. If any catchType in the list can catch an 420 excpType, then this exception spec does not catch the excpType. 421 */ 422 #if defined(_LIBCXXABI_ARM_EHABI) 423 static 424 bool 425 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo, 426 uint8_t ttypeEncoding, const __shim_type_info* excpType, 427 void* adjustedPtr, _Unwind_Exception* unwind_exception, 428 uintptr_t /*base*/ = 0) 429 { 430 if (classInfo == 0) 431 { 432 // this should not happen. Indicates corrupted eh_table. 433 call_terminate(false, unwind_exception); 434 } 435 436 assert(((ttypeEncoding == DW_EH_PE_absptr) || // LLVM or GCC 4.6 437 (ttypeEncoding == DW_EH_PE_pcrel) || // GCC 4.7 baremetal 438 (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) && // GCC 4.7 linux 439 "Unexpected TTypeEncoding"); 440 (void)ttypeEncoding; 441 442 // specIndex is negative of 1-based byte offset into classInfo; 443 specIndex = -specIndex; 444 --specIndex; 445 const void** temp = reinterpret_cast<const void**>( 446 reinterpret_cast<uintptr_t>(classInfo) + 447 static_cast<uintptr_t>(specIndex) * sizeof(uintptr_t)); 448 // If any type in the spec list can catch excpType, return false, else return true 449 // adjustments to adjustedPtr are ignored. 450 while (true) 451 { 452 // ARM EHABI exception specification table (filter table) consists of 453 // several pointers which will directly point to the type info object 454 // (instead of ttypeIndex). The table will be terminated with 0. 455 const void** ttypePtr = temp++; 456 if (*ttypePtr == 0) 457 break; 458 // We can get the __shim_type_info simply by performing a 459 // R_ARM_TARGET2 relocation, and cast the result to __shim_type_info. 460 const __shim_type_info* catchType = 461 static_cast<const __shim_type_info*>(read_target2_value(ttypePtr)); 462 void* tempPtr = adjustedPtr; 463 if (catchType->can_catch(excpType, tempPtr)) 464 return false; 465 } 466 return true; 467 } 468 #else 469 static 470 bool 471 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo, 472 uint8_t ttypeEncoding, const __shim_type_info* excpType, 473 void* adjustedPtr, _Unwind_Exception* unwind_exception, 474 uintptr_t base = 0) 475 { 476 if (classInfo == 0) 477 { 478 // this should not happen. Indicates corrupted eh_table. 479 call_terminate(false, unwind_exception); 480 } 481 // specIndex is negative of 1-based byte offset into classInfo; 482 specIndex = -specIndex; 483 --specIndex; 484 const uint8_t* temp = classInfo + specIndex; 485 // If any type in the spec list can catch excpType, return false, else return true 486 // adjustments to adjustedPtr are ignored. 487 while (true) 488 { 489 uint64_t ttypeIndex = readULEB128(&temp); 490 if (ttypeIndex == 0) 491 break; 492 const __shim_type_info* catchType = get_shim_type_info(ttypeIndex, 493 classInfo, 494 ttypeEncoding, 495 true, 496 unwind_exception, 497 base); 498 void* tempPtr = adjustedPtr; 499 if (catchType->can_catch(excpType, tempPtr)) 500 return false; 501 } 502 return true; 503 } 504 #endif 505 506 static 507 void* 508 get_thrown_object_ptr(_Unwind_Exception* unwind_exception) 509 { 510 // Even for foreign exceptions, the exception object is *probably* at unwind_exception + 1 511 // Regardless, this library is prohibited from touching a foreign exception 512 void* adjustedPtr = unwind_exception + 1; 513 if (__getExceptionClass(unwind_exception) == kOurDependentExceptionClass) 514 adjustedPtr = ((__cxa_dependent_exception*)adjustedPtr - 1)->primaryException; 515 return adjustedPtr; 516 } 517 518 namespace 519 { 520 521 struct scan_results 522 { 523 int64_t ttypeIndex; // > 0 catch handler, < 0 exception spec handler, == 0 a cleanup 524 const uint8_t* actionRecord; // Currently unused. Retained to ease future maintenance. 525 const uint8_t* languageSpecificData; // Needed only for __cxa_call_unexpected 526 uintptr_t landingPad; // null -> nothing found, else something found 527 void* adjustedPtr; // Used in cxa_exception.cpp 528 _Unwind_Reason_Code reason; // One of _URC_FATAL_PHASE1_ERROR, 529 // _URC_FATAL_PHASE2_ERROR, 530 // _URC_CONTINUE_UNWIND, 531 // _URC_HANDLER_FOUND 532 }; 533 534 } // unnamed namespace 535 536 static 537 void 538 set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context, 539 const scan_results& results) 540 { 541 #if defined(__USING_SJLJ_EXCEPTIONS__) 542 #define __builtin_eh_return_data_regno(regno) regno 543 #elif defined(__ibmxl__) 544 // IBM xlclang++ compiler does not support __builtin_eh_return_data_regno. 545 #define __builtin_eh_return_data_regno(regno) regno + 3 546 #endif 547 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0), 548 reinterpret_cast<uintptr_t>(unwind_exception)); 549 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 550 static_cast<uintptr_t>(results.ttypeIndex)); 551 _Unwind_SetIP(context, results.landingPad); 552 } 553 554 /* 555 There are 3 types of scans needed: 556 557 1. Scan for handler with native or foreign exception. If handler found, 558 save state and return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND. 559 May also report an error on invalid input. 560 May terminate for invalid exception table. 561 _UA_SEARCH_PHASE 562 563 2. Scan for handler with foreign exception. Must return _URC_HANDLER_FOUND, 564 or call terminate. 565 _UA_CLEANUP_PHASE && _UA_HANDLER_FRAME && !native_exception 566 567 3. Scan for cleanups. If a handler is found and this isn't forced unwind, 568 then terminate, otherwise ignore the handler and keep looking for cleanup. 569 If a cleanup is found, return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND. 570 May also report an error on invalid input. 571 May terminate for invalid exception table. 572 _UA_CLEANUP_PHASE && !_UA_HANDLER_FRAME 573 */ 574 575 static void scan_eh_tab(scan_results &results, _Unwind_Action actions, 576 bool native_exception, 577 _Unwind_Exception *unwind_exception, 578 _Unwind_Context *context) { 579 // Initialize results to found nothing but an error 580 results.ttypeIndex = 0; 581 results.actionRecord = 0; 582 results.languageSpecificData = 0; 583 results.landingPad = 0; 584 results.adjustedPtr = 0; 585 results.reason = _URC_FATAL_PHASE1_ERROR; 586 // Check for consistent actions 587 if (actions & _UA_SEARCH_PHASE) 588 { 589 // Do Phase 1 590 if (actions & (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME | _UA_FORCE_UNWIND)) 591 { 592 // None of these flags should be set during Phase 1 593 // Client error 594 results.reason = _URC_FATAL_PHASE1_ERROR; 595 return; 596 } 597 } 598 else if (actions & _UA_CLEANUP_PHASE) 599 { 600 if ((actions & _UA_HANDLER_FRAME) && (actions & _UA_FORCE_UNWIND)) 601 { 602 // _UA_HANDLER_FRAME should only be set if phase 1 found a handler. 603 // If _UA_FORCE_UNWIND is set, phase 1 shouldn't have happened. 604 // Client error 605 results.reason = _URC_FATAL_PHASE2_ERROR; 606 return; 607 } 608 } 609 else // Neither _UA_SEARCH_PHASE nor _UA_CLEANUP_PHASE is set 610 { 611 // One of these should be set. 612 // Client error 613 results.reason = _URC_FATAL_PHASE1_ERROR; 614 return; 615 } 616 // Start scan by getting exception table address 617 const uint8_t *lsda = (const uint8_t *)_Unwind_GetLanguageSpecificData(context); 618 if (lsda == 0) 619 { 620 // There is no exception table 621 results.reason = _URC_CONTINUE_UNWIND; 622 return; 623 } 624 results.languageSpecificData = lsda; 625 #if defined(_AIX) 626 uintptr_t base = _Unwind_GetDataRelBase(context); 627 #else 628 uintptr_t base = 0; 629 #endif 630 // Get the current instruction pointer and offset it before next 631 // instruction in the current frame which threw the exception. 632 uintptr_t ip = _Unwind_GetIP(context) - 1; 633 // Get beginning current frame's code (as defined by the 634 // emitted dwarf code) 635 uintptr_t funcStart = _Unwind_GetRegionStart(context); 636 #ifdef __USING_SJLJ_EXCEPTIONS__ 637 if (ip == uintptr_t(-1)) 638 { 639 // no action 640 results.reason = _URC_CONTINUE_UNWIND; 641 return; 642 } 643 else if (ip == 0) 644 call_terminate(native_exception, unwind_exception); 645 // ip is 1-based index into call site table 646 #else // !__USING_SJLJ_EXCEPTIONS__ 647 uintptr_t ipOffset = ip - funcStart; 648 #endif // !defined(_USING_SLJL_EXCEPTIONS__) 649 const uint8_t* classInfo = NULL; 650 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding 651 // dwarf emission 652 // Parse LSDA header. 653 uint8_t lpStartEncoding = *lsda++; 654 const uint8_t* lpStart = 655 (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding, base); 656 if (lpStart == 0) 657 lpStart = (const uint8_t*)funcStart; 658 uint8_t ttypeEncoding = *lsda++; 659 if (ttypeEncoding != DW_EH_PE_omit) 660 { 661 // Calculate type info locations in emitted dwarf code which 662 // were flagged by type info arguments to llvm.eh.selector 663 // intrinsic 664 uintptr_t classInfoOffset = readULEB128(&lsda); 665 classInfo = lsda + classInfoOffset; 666 } 667 // Walk call-site table looking for range that 668 // includes current PC. 669 uint8_t callSiteEncoding = *lsda++; 670 #ifdef __USING_SJLJ_EXCEPTIONS__ 671 (void)callSiteEncoding; // When using SjLj exceptions, callSiteEncoding is never used 672 #endif 673 uint32_t callSiteTableLength = static_cast<uint32_t>(readULEB128(&lsda)); 674 const uint8_t* callSiteTableStart = lsda; 675 const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength; 676 const uint8_t* actionTableStart = callSiteTableEnd; 677 const uint8_t* callSitePtr = callSiteTableStart; 678 while (callSitePtr < callSiteTableEnd) 679 { 680 // There is one entry per call site. 681 #ifndef __USING_SJLJ_EXCEPTIONS__ 682 // The call sites are non-overlapping in [start, start+length) 683 // The call sites are ordered in increasing value of start 684 uintptr_t start = readEncodedPointer(&callSitePtr, callSiteEncoding); 685 uintptr_t length = readEncodedPointer(&callSitePtr, callSiteEncoding); 686 uintptr_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding); 687 uintptr_t actionEntry = readULEB128(&callSitePtr); 688 if ((start <= ipOffset) && (ipOffset < (start + length))) 689 #else // __USING_SJLJ_EXCEPTIONS__ 690 // ip is 1-based index into this table 691 uintptr_t landingPad = readULEB128(&callSitePtr); 692 uintptr_t actionEntry = readULEB128(&callSitePtr); 693 if (--ip == 0) 694 #endif // __USING_SJLJ_EXCEPTIONS__ 695 { 696 // Found the call site containing ip. 697 #ifndef __USING_SJLJ_EXCEPTIONS__ 698 if (landingPad == 0) 699 { 700 // No handler here 701 results.reason = _URC_CONTINUE_UNWIND; 702 return; 703 } 704 landingPad = (uintptr_t)lpStart + landingPad; 705 #else // __USING_SJLJ_EXCEPTIONS__ 706 ++landingPad; 707 #endif // __USING_SJLJ_EXCEPTIONS__ 708 results.landingPad = landingPad; 709 if (actionEntry == 0) 710 { 711 // Found a cleanup 712 results.reason = actions & _UA_SEARCH_PHASE 713 ? _URC_CONTINUE_UNWIND 714 : _URC_HANDLER_FOUND; 715 return; 716 } 717 // Convert 1-based byte offset into 718 const uint8_t* action = actionTableStart + (actionEntry - 1); 719 bool hasCleanup = false; 720 // Scan action entries until you find a matching handler, cleanup, or the end of action list 721 while (true) 722 { 723 const uint8_t* actionRecord = action; 724 int64_t ttypeIndex = readSLEB128(&action); 725 if (ttypeIndex > 0) 726 { 727 // Found a catch, does it actually catch? 728 // First check for catch (...) 729 const __shim_type_info* catchType = 730 get_shim_type_info(static_cast<uint64_t>(ttypeIndex), 731 classInfo, ttypeEncoding, 732 native_exception, unwind_exception, 733 base); 734 if (catchType == 0) 735 { 736 // Found catch (...) catches everything, including 737 // foreign exceptions. This is search phase, cleanup 738 // phase with foreign exception, or forced unwinding. 739 assert(actions & (_UA_SEARCH_PHASE | _UA_HANDLER_FRAME | 740 _UA_FORCE_UNWIND)); 741 results.ttypeIndex = ttypeIndex; 742 results.actionRecord = actionRecord; 743 results.adjustedPtr = 744 get_thrown_object_ptr(unwind_exception); 745 results.reason = _URC_HANDLER_FOUND; 746 return; 747 } 748 // Else this is a catch (T) clause and will never 749 // catch a foreign exception 750 else if (native_exception) 751 { 752 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 753 void* adjustedPtr = get_thrown_object_ptr(unwind_exception); 754 const __shim_type_info* excpType = 755 static_cast<const __shim_type_info*>(exception_header->exceptionType); 756 if (adjustedPtr == 0 || excpType == 0) 757 { 758 // Something very bad happened 759 call_terminate(native_exception, unwind_exception); 760 } 761 if (catchType->can_catch(excpType, adjustedPtr)) 762 { 763 // Found a matching handler. This is either search 764 // phase or forced unwinding. 765 assert(actions & 766 (_UA_SEARCH_PHASE | _UA_FORCE_UNWIND)); 767 results.ttypeIndex = ttypeIndex; 768 results.actionRecord = actionRecord; 769 results.adjustedPtr = adjustedPtr; 770 results.reason = _URC_HANDLER_FOUND; 771 return; 772 } 773 } 774 // Scan next action ... 775 } 776 else if (ttypeIndex < 0) 777 { 778 // Found an exception specification. 779 if (actions & _UA_FORCE_UNWIND) { 780 // Skip if forced unwinding. 781 } else if (native_exception) { 782 // Does the exception spec catch this native exception? 783 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 784 void* adjustedPtr = get_thrown_object_ptr(unwind_exception); 785 const __shim_type_info* excpType = 786 static_cast<const __shim_type_info*>(exception_header->exceptionType); 787 if (adjustedPtr == 0 || excpType == 0) 788 { 789 // Something very bad happened 790 call_terminate(native_exception, unwind_exception); 791 } 792 if (exception_spec_can_catch(ttypeIndex, classInfo, 793 ttypeEncoding, excpType, 794 adjustedPtr, 795 unwind_exception, base)) 796 { 797 // Native exception caught by exception 798 // specification. 799 assert(actions & _UA_SEARCH_PHASE); 800 results.ttypeIndex = ttypeIndex; 801 results.actionRecord = actionRecord; 802 results.adjustedPtr = adjustedPtr; 803 results.reason = _URC_HANDLER_FOUND; 804 return; 805 } 806 } else { 807 // foreign exception caught by exception spec 808 results.ttypeIndex = ttypeIndex; 809 results.actionRecord = actionRecord; 810 results.adjustedPtr = 811 get_thrown_object_ptr(unwind_exception); 812 results.reason = _URC_HANDLER_FOUND; 813 return; 814 } 815 // Scan next action ... 816 } else { 817 hasCleanup = true; 818 } 819 const uint8_t* temp = action; 820 int64_t actionOffset = readSLEB128(&temp); 821 if (actionOffset == 0) 822 { 823 // End of action list. If this is phase 2 and we have found 824 // a cleanup (ttypeIndex=0), return _URC_HANDLER_FOUND; 825 // otherwise return _URC_CONTINUE_UNWIND. 826 results.reason = hasCleanup && actions & _UA_CLEANUP_PHASE 827 ? _URC_HANDLER_FOUND 828 : _URC_CONTINUE_UNWIND; 829 return; 830 } 831 // Go to next action 832 action += actionOffset; 833 } // there is no break out of this loop, only return 834 } 835 #ifndef __USING_SJLJ_EXCEPTIONS__ 836 else if (ipOffset < start) 837 { 838 // There is no call site for this ip 839 // Something bad has happened. We should never get here. 840 // Possible stack corruption. 841 call_terminate(native_exception, unwind_exception); 842 } 843 #endif // !__USING_SJLJ_EXCEPTIONS__ 844 } // there might be some tricky cases which break out of this loop 845 846 // It is possible that no eh table entry specify how to handle 847 // this exception. By spec, terminate it immediately. 848 call_terminate(native_exception, unwind_exception); 849 } 850 851 // public API 852 853 /* 854 The personality function branches on actions like so: 855 856 _UA_SEARCH_PHASE 857 858 If _UA_CLEANUP_PHASE or _UA_HANDLER_FRAME or _UA_FORCE_UNWIND there's 859 an error from above, return _URC_FATAL_PHASE1_ERROR. 860 861 Scan for anything that could stop unwinding: 862 863 1. A catch clause that will catch this exception 864 (will never catch foreign). 865 2. A catch (...) (will always catch foreign). 866 3. An exception spec that will catch this exception 867 (will always catch foreign). 868 If a handler is found 869 If not foreign 870 Save state in header 871 return _URC_HANDLER_FOUND 872 Else a handler not found 873 return _URC_CONTINUE_UNWIND 874 875 _UA_CLEANUP_PHASE 876 877 If _UA_HANDLER_FRAME 878 If _UA_FORCE_UNWIND 879 How did this happen? return _URC_FATAL_PHASE2_ERROR 880 If foreign 881 Do _UA_SEARCH_PHASE to recover state 882 else 883 Recover state from header 884 Transfer control to landing pad. return _URC_INSTALL_CONTEXT 885 886 Else 887 888 This branch handles both normal C++ non-catching handlers (cleanups) 889 and forced unwinding. 890 Scan for anything that can not stop unwinding: 891 892 1. A cleanup. 893 894 If a cleanup is found 895 transfer control to it. return _URC_INSTALL_CONTEXT 896 Else a cleanup is not found: return _URC_CONTINUE_UNWIND 897 */ 898 899 #if !defined(_LIBCXXABI_ARM_EHABI) 900 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) 901 static _Unwind_Reason_Code __gxx_personality_imp 902 #else 903 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code 904 #ifdef __USING_SJLJ_EXCEPTIONS__ 905 __gxx_personality_sj0 906 #else 907 __gxx_personality_v0 908 #endif 909 #endif 910 (int version, _Unwind_Action actions, uint64_t exceptionClass, 911 _Unwind_Exception* unwind_exception, _Unwind_Context* context) 912 { 913 if (version != 1 || unwind_exception == 0 || context == 0) 914 return _URC_FATAL_PHASE1_ERROR; 915 916 bool native_exception = (exceptionClass & get_vendor_and_language) == 917 (kOurExceptionClass & get_vendor_and_language); 918 scan_results results; 919 // Process a catch handler for a native exception first. 920 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME) && 921 native_exception) { 922 // Reload the results from the phase 1 cache. 923 __cxa_exception* exception_header = 924 (__cxa_exception*)(unwind_exception + 1) - 1; 925 results.ttypeIndex = exception_header->handlerSwitchValue; 926 results.actionRecord = exception_header->actionRecord; 927 results.languageSpecificData = exception_header->languageSpecificData; 928 results.landingPad = 929 reinterpret_cast<uintptr_t>(exception_header->catchTemp); 930 results.adjustedPtr = exception_header->adjustedPtr; 931 932 // Jump to the handler. 933 set_registers(unwind_exception, context, results); 934 // Cache base for calculating the address of ttype in 935 // __cxa_call_unexpected. 936 if (results.ttypeIndex < 0) { 937 #if defined(_AIX) 938 exception_header->catchTemp = (void *)_Unwind_GetDataRelBase(context); 939 #else 940 exception_header->catchTemp = 0; 941 #endif 942 } 943 return _URC_INSTALL_CONTEXT; 944 } 945 946 // In other cases we need to scan LSDA. 947 scan_eh_tab(results, actions, native_exception, unwind_exception, context); 948 if (results.reason == _URC_CONTINUE_UNWIND || 949 results.reason == _URC_FATAL_PHASE1_ERROR) 950 return results.reason; 951 952 if (actions & _UA_SEARCH_PHASE) 953 { 954 // Phase 1 search: All we're looking for in phase 1 is a handler that 955 // halts unwinding 956 assert(results.reason == _URC_HANDLER_FOUND); 957 if (native_exception) { 958 // For a native exception, cache the LSDA result. 959 __cxa_exception* exc = (__cxa_exception*)(unwind_exception + 1) - 1; 960 exc->handlerSwitchValue = static_cast<int>(results.ttypeIndex); 961 exc->actionRecord = results.actionRecord; 962 exc->languageSpecificData = results.languageSpecificData; 963 exc->catchTemp = reinterpret_cast<void*>(results.landingPad); 964 exc->adjustedPtr = results.adjustedPtr; 965 } 966 return _URC_HANDLER_FOUND; 967 } 968 969 assert(actions & _UA_CLEANUP_PHASE); 970 assert(results.reason == _URC_HANDLER_FOUND); 971 set_registers(unwind_exception, context, results); 972 // Cache base for calculating the address of ttype in __cxa_call_unexpected. 973 if (results.ttypeIndex < 0) { 974 __cxa_exception* exception_header = 975 (__cxa_exception*)(unwind_exception + 1) - 1; 976 #if defined(_AIX) 977 exception_header->catchTemp = (void *)_Unwind_GetDataRelBase(context); 978 #else 979 exception_header->catchTemp = 0; 980 #endif 981 } 982 return _URC_INSTALL_CONTEXT; 983 } 984 985 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) 986 extern "C" _LIBCXXABI_FUNC_VIS EXCEPTION_DISPOSITION 987 __gxx_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame, 988 PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp) 989 { 990 return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, ms_disp, 991 __gxx_personality_imp); 992 } 993 #endif 994 995 #else 996 997 extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*, 998 _Unwind_Context*); 999 1000 // Helper function to unwind one frame. 1001 // ARM EHABI 7.3 and 7.4: If the personality function returns _URC_CONTINUE_UNWIND, the 1002 // personality routine should update the virtual register set (VRS) according to the 1003 // corresponding frame unwinding instructions (ARM EHABI 9.3.) 1004 static _Unwind_Reason_Code continue_unwind(_Unwind_Exception* unwind_exception, 1005 _Unwind_Context* context) 1006 { 1007 switch (__gnu_unwind_frame(unwind_exception, context)) { 1008 case _URC_OK: 1009 return _URC_CONTINUE_UNWIND; 1010 case _URC_END_OF_STACK: 1011 return _URC_END_OF_STACK; 1012 default: 1013 return _URC_FAILURE; 1014 } 1015 } 1016 1017 // ARM register names 1018 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER) 1019 static const uint32_t REG_UCB = 12; // Register to save _Unwind_Control_Block 1020 #endif 1021 static const uint32_t REG_SP = 13; 1022 1023 static void save_results_to_barrier_cache(_Unwind_Exception* unwind_exception, 1024 const scan_results& results) 1025 { 1026 unwind_exception->barrier_cache.bitpattern[0] = (uint32_t)results.adjustedPtr; 1027 unwind_exception->barrier_cache.bitpattern[1] = (uint32_t)results.actionRecord; 1028 unwind_exception->barrier_cache.bitpattern[2] = (uint32_t)results.languageSpecificData; 1029 unwind_exception->barrier_cache.bitpattern[3] = (uint32_t)results.landingPad; 1030 unwind_exception->barrier_cache.bitpattern[4] = (uint32_t)results.ttypeIndex; 1031 } 1032 1033 static void load_results_from_barrier_cache(scan_results& results, 1034 const _Unwind_Exception* unwind_exception) 1035 { 1036 results.adjustedPtr = (void*)unwind_exception->barrier_cache.bitpattern[0]; 1037 results.actionRecord = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[1]; 1038 results.languageSpecificData = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2]; 1039 results.landingPad = (uintptr_t)unwind_exception->barrier_cache.bitpattern[3]; 1040 results.ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4]; 1041 } 1042 1043 extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code 1044 __gxx_personality_v0(_Unwind_State state, 1045 _Unwind_Exception* unwind_exception, 1046 _Unwind_Context* context) 1047 { 1048 if (unwind_exception == 0 || context == 0) 1049 return _URC_FATAL_PHASE1_ERROR; 1050 1051 bool native_exception = __isOurExceptionClass(unwind_exception); 1052 1053 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER) 1054 // Copy the address of _Unwind_Control_Block to r12 so that 1055 // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can 1056 // return correct address. 1057 _Unwind_SetGR(context, REG_UCB, reinterpret_cast<uint32_t>(unwind_exception)); 1058 #endif 1059 1060 // Check the undocumented force unwinding behavior 1061 bool is_force_unwinding = state & _US_FORCE_UNWIND; 1062 state &= ~_US_FORCE_UNWIND; 1063 1064 scan_results results; 1065 switch (state) { 1066 case _US_VIRTUAL_UNWIND_FRAME: 1067 if (is_force_unwinding) 1068 return continue_unwind(unwind_exception, context); 1069 1070 // Phase 1 search: All we're looking for in phase 1 is a handler that halts unwinding 1071 scan_eh_tab(results, _UA_SEARCH_PHASE, native_exception, unwind_exception, context); 1072 if (results.reason == _URC_HANDLER_FOUND) 1073 { 1074 unwind_exception->barrier_cache.sp = _Unwind_GetGR(context, REG_SP); 1075 if (native_exception) 1076 save_results_to_barrier_cache(unwind_exception, results); 1077 return _URC_HANDLER_FOUND; 1078 } 1079 // Did not find the catch handler 1080 if (results.reason == _URC_CONTINUE_UNWIND) 1081 return continue_unwind(unwind_exception, context); 1082 return results.reason; 1083 1084 case _US_UNWIND_FRAME_STARTING: 1085 // TODO: Support force unwinding in the phase 2 search. 1086 // NOTE: In order to call the cleanup functions, _Unwind_ForcedUnwind() 1087 // will call this personality function with (_US_FORCE_UNWIND | 1088 // _US_UNWIND_FRAME_STARTING). 1089 1090 // Phase 2 search 1091 if (unwind_exception->barrier_cache.sp == _Unwind_GetGR(context, REG_SP)) 1092 { 1093 // Found a catching handler in phase 1 1094 if (native_exception) 1095 { 1096 // Load the result from the native exception barrier cache. 1097 load_results_from_barrier_cache(results, unwind_exception); 1098 results.reason = _URC_HANDLER_FOUND; 1099 } 1100 else 1101 { 1102 // Search for the catching handler again for the foreign exception. 1103 scan_eh_tab(results, static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME), 1104 native_exception, unwind_exception, context); 1105 if (results.reason != _URC_HANDLER_FOUND) // phase1 search should guarantee to find one 1106 call_terminate(native_exception, unwind_exception); 1107 } 1108 1109 // Install the context for the catching handler 1110 set_registers(unwind_exception, context, results); 1111 return _URC_INSTALL_CONTEXT; 1112 } 1113 1114 // Either we didn't do a phase 1 search (due to forced unwinding), or 1115 // phase 1 reported no catching-handlers. 1116 // Search for a (non-catching) cleanup 1117 if (is_force_unwinding) 1118 scan_eh_tab( 1119 results, 1120 static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_FORCE_UNWIND), 1121 native_exception, unwind_exception, context); 1122 else 1123 scan_eh_tab(results, _UA_CLEANUP_PHASE, native_exception, 1124 unwind_exception, context); 1125 if (results.reason == _URC_HANDLER_FOUND) 1126 { 1127 // Found a non-catching handler 1128 1129 // ARM EHABI 8.4.2: Before we can jump to the cleanup handler, we have to setup some 1130 // internal data structures, so that __cxa_end_cleanup() can get unwind_exception from 1131 // __cxa_get_globals(). 1132 __cxa_begin_cleanup(unwind_exception); 1133 1134 // Install the context for the cleanup handler 1135 set_registers(unwind_exception, context, results); 1136 return _URC_INSTALL_CONTEXT; 1137 } 1138 1139 // Did not find any handler 1140 if (results.reason == _URC_CONTINUE_UNWIND) 1141 return continue_unwind(unwind_exception, context); 1142 return results.reason; 1143 1144 case _US_UNWIND_FRAME_RESUME: 1145 return continue_unwind(unwind_exception, context); 1146 } 1147 1148 // We were called improperly: neither a phase 1 or phase 2 search 1149 return _URC_FATAL_PHASE1_ERROR; 1150 } 1151 #endif 1152 1153 1154 __attribute__((noreturn)) 1155 _LIBCXXABI_FUNC_VIS void 1156 __cxa_call_unexpected(void* arg) 1157 { 1158 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(arg); 1159 if (unwind_exception == 0) 1160 call_terminate(false, unwind_exception); 1161 __cxa_begin_catch(unwind_exception); 1162 bool native_old_exception = __isOurExceptionClass(unwind_exception); 1163 std::unexpected_handler u_handler; 1164 std::terminate_handler t_handler; 1165 __cxa_exception* old_exception_header = 0; 1166 int64_t ttypeIndex; 1167 const uint8_t* lsda; 1168 uintptr_t base = 0; 1169 1170 if (native_old_exception) 1171 { 1172 old_exception_header = (__cxa_exception*)(unwind_exception+1) - 1; 1173 t_handler = old_exception_header->terminateHandler; 1174 u_handler = old_exception_header->unexpectedHandler; 1175 // If std::__unexpected(u_handler) rethrows the same exception, 1176 // these values get overwritten by the rethrow. So save them now: 1177 #if defined(_LIBCXXABI_ARM_EHABI) 1178 ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4]; 1179 lsda = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2]; 1180 #else 1181 ttypeIndex = old_exception_header->handlerSwitchValue; 1182 lsda = old_exception_header->languageSpecificData; 1183 base = (uintptr_t)old_exception_header->catchTemp; 1184 #endif 1185 } 1186 else 1187 { 1188 t_handler = std::get_terminate(); 1189 u_handler = std::get_unexpected(); 1190 } 1191 try 1192 { 1193 std::__unexpected(u_handler); 1194 } 1195 catch (...) 1196 { 1197 // If the old exception is foreign, then all we can do is terminate. 1198 // We have no way to recover the needed old exception spec. There's 1199 // no way to pass that information here. And the personality routine 1200 // can't call us directly and do anything but terminate() if we throw 1201 // from here. 1202 if (native_old_exception) 1203 { 1204 // Have: 1205 // old_exception_header->languageSpecificData 1206 // old_exception_header->actionRecord 1207 // old_exception_header->catchTemp, base for calculating ttype 1208 // Need 1209 // const uint8_t* classInfo 1210 // uint8_t ttypeEncoding 1211 uint8_t lpStartEncoding = *lsda++; 1212 const uint8_t* lpStart = 1213 (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding, base); 1214 (void)lpStart; // purposefully unused. Just needed to increment lsda. 1215 uint8_t ttypeEncoding = *lsda++; 1216 if (ttypeEncoding == DW_EH_PE_omit) 1217 std::__terminate(t_handler); 1218 uintptr_t classInfoOffset = readULEB128(&lsda); 1219 const uint8_t* classInfo = lsda + classInfoOffset; 1220 // Is this new exception catchable by the exception spec at ttypeIndex? 1221 // The answer is obviously yes if the new and old exceptions are the same exception 1222 // If no 1223 // throw; 1224 __cxa_eh_globals* globals = __cxa_get_globals_fast(); 1225 __cxa_exception* new_exception_header = globals->caughtExceptions; 1226 if (new_exception_header == 0) 1227 // This shouldn't be able to happen! 1228 std::__terminate(t_handler); 1229 bool native_new_exception = __isOurExceptionClass(&new_exception_header->unwindHeader); 1230 void* adjustedPtr; 1231 if (native_new_exception && (new_exception_header != old_exception_header)) 1232 { 1233 const __shim_type_info* excpType = 1234 static_cast<const __shim_type_info*>(new_exception_header->exceptionType); 1235 adjustedPtr = 1236 __getExceptionClass(&new_exception_header->unwindHeader) == kOurDependentExceptionClass ? 1237 ((__cxa_dependent_exception*)new_exception_header)->primaryException : 1238 new_exception_header + 1; 1239 if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding, 1240 excpType, adjustedPtr, 1241 unwind_exception, base)) 1242 { 1243 // We need to __cxa_end_catch, but for the old exception, 1244 // not the new one. This is a little tricky ... 1245 // Disguise new_exception_header as a rethrown exception, but 1246 // don't actually rethrow it. This means you can temporarily 1247 // end the catch clause enclosing new_exception_header without 1248 // __cxa_end_catch destroying new_exception_header. 1249 new_exception_header->handlerCount = -new_exception_header->handlerCount; 1250 globals->uncaughtExceptions += 1; 1251 // Call __cxa_end_catch for new_exception_header 1252 __cxa_end_catch(); 1253 // Call __cxa_end_catch for old_exception_header 1254 __cxa_end_catch(); 1255 // Renter this catch clause with new_exception_header 1256 __cxa_begin_catch(&new_exception_header->unwindHeader); 1257 // Rethrow new_exception_header 1258 throw; 1259 } 1260 } 1261 // Will a std::bad_exception be catchable by the exception spec at 1262 // ttypeIndex? 1263 // If no 1264 // throw std::bad_exception(); 1265 const __shim_type_info* excpType = 1266 static_cast<const __shim_type_info*>(&typeid(std::bad_exception)); 1267 std::bad_exception be; 1268 adjustedPtr = &be; 1269 if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding, 1270 excpType, adjustedPtr, 1271 unwind_exception, base)) 1272 { 1273 // We need to __cxa_end_catch for both the old exception and the 1274 // new exception. Technically we should do it in that order. 1275 // But it is expedient to do it in the opposite order: 1276 // Call __cxa_end_catch for new_exception_header 1277 __cxa_end_catch(); 1278 // Throw std::bad_exception will __cxa_end_catch for 1279 // old_exception_header 1280 throw be; 1281 } 1282 } 1283 } 1284 std::__terminate(t_handler); 1285 } 1286 1287 #if defined(_AIX) 1288 // Personality routine for EH using the range table. Make it an alias of 1289 // __gxx_personality_v0(). 1290 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code __xlcxx_personality_v1( 1291 int version, _Unwind_Action actions, uint64_t exceptionClass, 1292 _Unwind_Exception* unwind_exception, _Unwind_Context* context) 1293 __attribute__((__alias__("__gxx_personality_v0"))); 1294 #endif 1295 1296 } // extern "C" 1297 1298 } // __cxxabiv1 1299