1 //===------------------------- cxa_exception.cpp --------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 // 9 // This file implements the "Exception Handling APIs" 10 // http://mentorembedded.github.io/cxx-abi/abi-eh.html 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "cxxabi.h" 15 16 #include <exception> // for std::terminate 17 #include <cstring> // for memset 18 #include "cxa_exception.hpp" 19 #include "cxa_handlers.hpp" 20 #include "fallback_malloc.h" 21 #include "include/atomic_support.h" 22 23 #if __has_feature(address_sanitizer) 24 extern "C" void __asan_handle_no_return(void); 25 #endif 26 27 // +---------------------------+-----------------------------+---------------+ 28 // | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | 29 // +---------------------------+-----------------------------+---------------+ 30 // ^ 31 // | 32 // +-------------------------------------------------------+ 33 // | 34 // +---------------------------+-----------------------------+ 35 // | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 | 36 // +---------------------------+-----------------------------+ 37 38 namespace __cxxabiv1 { 39 40 // Utility routines 41 static 42 inline 43 __cxa_exception* 44 cxa_exception_from_thrown_object(void* thrown_object) 45 { 46 return static_cast<__cxa_exception*>(thrown_object) - 1; 47 } 48 49 // Note: This is never called when exception_header is masquerading as a 50 // __cxa_dependent_exception. 51 static 52 inline 53 void* 54 thrown_object_from_cxa_exception(__cxa_exception* exception_header) 55 { 56 return static_cast<void*>(exception_header + 1); 57 } 58 59 // Get the exception object from the unwind pointer. 60 // Relies on the structure layout, where the unwind pointer is right in 61 // front of the user's exception object 62 static 63 inline 64 __cxa_exception* 65 cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) 66 { 67 return cxa_exception_from_thrown_object(unwind_exception + 1 ); 68 } 69 70 // Round s up to next multiple of a. 71 static inline 72 size_t aligned_allocation_size(size_t s, size_t a) { 73 return (s + a - 1) & ~(a - 1); 74 } 75 76 static inline 77 size_t cxa_exception_size_from_exception_thrown_size(size_t size) { 78 return aligned_allocation_size(size + sizeof (__cxa_exception), 79 alignof(__cxa_exception)); 80 } 81 82 static void setExceptionClass(_Unwind_Exception* unwind_exception) { 83 unwind_exception->exception_class = kOurExceptionClass; 84 } 85 86 static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) { 87 unwind_exception->exception_class = kOurDependentExceptionClass; 88 } 89 90 // Is it one of ours? 91 static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) { 92 return (unwind_exception->exception_class & get_vendor_and_language) == 93 (kOurExceptionClass & get_vendor_and_language); 94 } 95 96 static bool isDependentException(_Unwind_Exception* unwind_exception) { 97 return (unwind_exception->exception_class & 0xFF) == 0x01; 98 } 99 100 // This does not need to be atomic 101 static inline int incrementHandlerCount(__cxa_exception *exception) { 102 return ++exception->handlerCount; 103 } 104 105 // This does not need to be atomic 106 static inline int decrementHandlerCount(__cxa_exception *exception) { 107 return --exception->handlerCount; 108 } 109 110 /* 111 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler 112 stored in exc is called. Otherwise the exceptionDestructor stored in 113 exc is called, and then the memory for the exception is deallocated. 114 115 This is never called for a __cxa_dependent_exception. 116 */ 117 static 118 void 119 exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) 120 { 121 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception); 122 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) 123 std::__terminate(exception_header->terminateHandler); 124 // Just in case there exists a dependent exception that is pointing to this, 125 // check the reference count and only destroy this if that count goes to zero. 126 __cxa_decrement_exception_refcount(unwind_exception + 1); 127 } 128 129 static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) { 130 // Section 2.5.3 says: 131 // * For purposes of this ABI, several things are considered exception handlers: 132 // ** A terminate() call due to a throw. 133 // and 134 // * Upon entry, Following initialization of the catch parameter, 135 // a handler must call: 136 // * void *__cxa_begin_catch(void *exceptionObject ); 137 (void) __cxa_begin_catch(&exception_header->unwindHeader); 138 std::__terminate(exception_header->terminateHandler); 139 } 140 141 // Return the offset of the __cxa_exception header from the start of the 142 // allocated buffer. If __cxa_exception's alignment is smaller than the maximum 143 // useful alignment for the target machine, padding has to be inserted before 144 // the header to ensure the thrown object that follows the header is 145 // sufficiently aligned. This happens if _Unwind_exception isn't double-word 146 // aligned (on Darwin, for example). 147 static size_t get_cxa_exception_offset() { 148 struct S { 149 } __attribute__((aligned)); 150 151 // Compute the maximum alignment for the target machine. 152 constexpr size_t alignment = std::alignment_of<S>::value; 153 constexpr size_t excp_size = sizeof(__cxa_exception); 154 constexpr size_t aligned_size = 155 (excp_size + alignment - 1) / alignment * alignment; 156 constexpr size_t offset = aligned_size - excp_size; 157 static_assert((offset == 0 || 158 std::alignment_of<_Unwind_Exception>::value < alignment), 159 "offset is non-zero only if _Unwind_Exception isn't aligned"); 160 return offset; 161 } 162 163 extern "C" { 164 165 // Allocate a __cxa_exception object, and zero-fill it. 166 // Reserve "thrown_size" bytes on the end for the user's exception 167 // object. Zero-fill the object. If memory can't be allocated, call 168 // std::terminate. Return a pointer to the memory to be used for the 169 // user's exception object. 170 void *__cxa_allocate_exception(size_t thrown_size) throw() { 171 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size); 172 173 // Allocate extra space before the __cxa_exception header to ensure the 174 // start of the thrown object is sufficiently aligned. 175 size_t header_offset = get_cxa_exception_offset(); 176 char *raw_buffer = 177 (char *)__aligned_malloc_with_fallback(header_offset + actual_size); 178 if (NULL == raw_buffer) 179 std::terminate(); 180 __cxa_exception *exception_header = 181 static_cast<__cxa_exception *>((void *)(raw_buffer + header_offset)); 182 std::memset(exception_header, 0, actual_size); 183 return thrown_object_from_cxa_exception(exception_header); 184 } 185 186 187 // Free a __cxa_exception object allocated with __cxa_allocate_exception. 188 void __cxa_free_exception(void *thrown_object) throw() { 189 // Compute the size of the padding before the header. 190 size_t header_offset = get_cxa_exception_offset(); 191 char *raw_buffer = 192 ((char *)cxa_exception_from_thrown_object(thrown_object)) - header_offset; 193 __aligned_free_with_fallback((void *)raw_buffer); 194 } 195 196 197 // This function shall allocate a __cxa_dependent_exception and 198 // return a pointer to it. (Really to the object, not past its' end). 199 // Otherwise, it will work like __cxa_allocate_exception. 200 void * __cxa_allocate_dependent_exception () { 201 size_t actual_size = sizeof(__cxa_dependent_exception); 202 void *ptr = __aligned_malloc_with_fallback(actual_size); 203 if (NULL == ptr) 204 std::terminate(); 205 std::memset(ptr, 0, actual_size); 206 return ptr; 207 } 208 209 210 // This function shall free a dependent_exception. 211 // It does not affect the reference count of the primary exception. 212 void __cxa_free_dependent_exception (void * dependent_exception) { 213 __aligned_free_with_fallback(dependent_exception); 214 } 215 216 217 // 2.4.3 Throwing the Exception Object 218 /* 219 After constructing the exception object with the throw argument value, 220 the generated code calls the __cxa_throw runtime library routine. This 221 routine never returns. 222 223 The __cxa_throw routine will do the following: 224 225 * Obtain the __cxa_exception header from the thrown exception object address, 226 which can be computed as follows: 227 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1); 228 * Save the current unexpected_handler and terminate_handler in the __cxa_exception header. 229 * Save the tinfo and dest arguments in the __cxa_exception header. 230 * Set the exception_class field in the unwind header. This is a 64-bit value 231 representing the ASCII string "XXXXC++\0", where "XXXX" is a 232 vendor-dependent string. That is, for implementations conforming to this 233 ABI, the low-order 4 bytes of this 64-bit value will be "C++\0". 234 * Increment the uncaught_exception flag. 235 * Call _Unwind_RaiseException in the system unwind library, Its argument is the 236 pointer to the thrown exception, which __cxa_throw itself received as an argument. 237 __Unwind_RaiseException begins the process of stack unwinding, described 238 in Section 2.5. In special cases, such as an inability to find a 239 handler, _Unwind_RaiseException may return. In that case, __cxa_throw 240 will call terminate, assuming that there was no handler for the 241 exception. 242 */ 243 void 244 __cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) { 245 __cxa_eh_globals *globals = __cxa_get_globals(); 246 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 247 248 exception_header->unexpectedHandler = std::get_unexpected(); 249 exception_header->terminateHandler = std::get_terminate(); 250 exception_header->exceptionType = tinfo; 251 exception_header->exceptionDestructor = dest; 252 setExceptionClass(&exception_header->unwindHeader); 253 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety. 254 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local 255 256 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func; 257 258 #if __has_feature(address_sanitizer) 259 // Inform the ASan runtime that now might be a good time to clean stuff up. 260 __asan_handle_no_return(); 261 #endif 262 263 #ifdef __USING_SJLJ_EXCEPTIONS__ 264 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); 265 #else 266 _Unwind_RaiseException(&exception_header->unwindHeader); 267 #endif 268 // This only happens when there is no handler, or some unexpected unwinding 269 // error happens. 270 failed_throw(exception_header); 271 } 272 273 274 // 2.5.3 Exception Handlers 275 /* 276 The adjusted pointer is computed by the personality routine during phase 1 277 and saved in the exception header (either __cxa_exception or 278 __cxa_dependent_exception). 279 280 Requires: exception is native 281 */ 282 void *__cxa_get_exception_ptr(void *unwind_exception) throw() { 283 #if defined(_LIBCXXABI_ARM_EHABI) 284 return reinterpret_cast<void*>( 285 static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]); 286 #else 287 return cxa_exception_from_exception_unwind_exception( 288 static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr; 289 #endif 290 } 291 292 #if defined(_LIBCXXABI_ARM_EHABI) 293 /* 294 The routine to be called before the cleanup. This will save __cxa_exception in 295 __cxa_eh_globals, so that __cxa_end_cleanup() can recover later. 296 */ 297 bool __cxa_begin_cleanup(void *unwind_arg) throw() { 298 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg); 299 __cxa_eh_globals* globals = __cxa_get_globals(); 300 __cxa_exception* exception_header = 301 cxa_exception_from_exception_unwind_exception(unwind_exception); 302 303 if (isOurExceptionClass(unwind_exception)) 304 { 305 if (0 == exception_header->propagationCount) 306 { 307 exception_header->nextPropagatingException = globals->propagatingExceptions; 308 globals->propagatingExceptions = exception_header; 309 } 310 ++exception_header->propagationCount; 311 } 312 else 313 { 314 // If the propagatingExceptions stack is not empty, since we can't 315 // chain the foreign exception, terminate it. 316 if (NULL != globals->propagatingExceptions) 317 std::terminate(); 318 globals->propagatingExceptions = exception_header; 319 } 320 return true; 321 } 322 323 /* 324 The routine to be called after the cleanup has been performed. It will get the 325 propagating __cxa_exception from __cxa_eh_globals, and continue the stack 326 unwinding with _Unwind_Resume. 327 328 According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any 329 register, thus we have to write this function in assembly so that we can save 330 {r1, r2, r3}. We don't have to save r0 because it is the return value and the 331 first argument to _Unwind_Resume(). In addition, we are saving r4 in order to 332 align the stack to 16 bytes, even though it is a callee-save register. 333 */ 334 __attribute__((used)) static _Unwind_Exception * 335 __cxa_end_cleanup_impl() 336 { 337 __cxa_eh_globals* globals = __cxa_get_globals(); 338 __cxa_exception* exception_header = globals->propagatingExceptions; 339 if (NULL == exception_header) 340 { 341 // It seems that __cxa_begin_cleanup() is not called properly. 342 // We have no choice but terminate the program now. 343 std::terminate(); 344 } 345 346 if (isOurExceptionClass(&exception_header->unwindHeader)) 347 { 348 --exception_header->propagationCount; 349 if (0 == exception_header->propagationCount) 350 { 351 globals->propagatingExceptions = exception_header->nextPropagatingException; 352 exception_header->nextPropagatingException = NULL; 353 } 354 } 355 else 356 { 357 globals->propagatingExceptions = NULL; 358 } 359 return &exception_header->unwindHeader; 360 } 361 362 asm ( 363 " .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n" 364 " .globl __cxa_end_cleanup\n" 365 " .type __cxa_end_cleanup,%function\n" 366 "__cxa_end_cleanup:\n" 367 " push {r1, r2, r3, r4}\n" 368 " bl __cxa_end_cleanup_impl\n" 369 " pop {r1, r2, r3, r4}\n" 370 " bl _Unwind_Resume\n" 371 " bl abort\n" 372 " .popsection" 373 ); 374 #endif // defined(_LIBCXXABI_ARM_EHABI) 375 376 /* 377 This routine can catch foreign or native exceptions. If native, the exception 378 can be a primary or dependent variety. This routine may remain blissfully 379 ignorant of whether the native exception is primary or dependent. 380 381 If the exception is native: 382 * Increment's the exception's handler count. 383 * Push the exception on the stack of currently-caught exceptions if it is not 384 already there (from a rethrow). 385 * Decrements the uncaught_exception count. 386 * Returns the adjusted pointer to the exception object, which is stored in 387 the __cxa_exception by the personality routine. 388 389 If the exception is foreign, this means it did not originate from one of throw 390 routines. The foreign exception does not necessarily have a __cxa_exception 391 header. However we can catch it here with a catch (...), or with a call 392 to terminate or unexpected during unwinding. 393 * Do not try to increment the exception's handler count, we don't know where 394 it is. 395 * Push the exception on the stack of currently-caught exceptions only if the 396 stack is empty. The foreign exception has no way to link to the current 397 top of stack. If the stack is not empty, call terminate. Even with an 398 empty stack, this is hacked in by pushing a pointer to an imaginary 399 __cxa_exception block in front of the foreign exception. It would be better 400 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it 401 doesn't. It has a stack of __cxa_exception (which has a next* in it). 402 * Do not decrement the uncaught_exception count because we didn't increment it 403 in __cxa_throw (or one of our rethrow functions). 404 * If we haven't terminated, assume the exception object is just past the 405 _Unwind_Exception and return a pointer to that. 406 */ 407 void* 408 __cxa_begin_catch(void* unwind_arg) throw() 409 { 410 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg); 411 bool native_exception = isOurExceptionClass(unwind_exception); 412 __cxa_eh_globals* globals = __cxa_get_globals(); 413 // exception_header is a hackish offset from a foreign exception, but it 414 // works as long as we're careful not to try to access any __cxa_exception 415 // parts. 416 __cxa_exception* exception_header = 417 cxa_exception_from_exception_unwind_exception 418 ( 419 static_cast<_Unwind_Exception*>(unwind_exception) 420 ); 421 if (native_exception) 422 { 423 // Increment the handler count, removing the flag about being rethrown 424 exception_header->handlerCount = exception_header->handlerCount < 0 ? 425 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1; 426 // place the exception on the top of the stack if it's not already 427 // there by a previous rethrow 428 if (exception_header != globals->caughtExceptions) 429 { 430 exception_header->nextException = globals->caughtExceptions; 431 globals->caughtExceptions = exception_header; 432 } 433 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local 434 #if defined(_LIBCXXABI_ARM_EHABI) 435 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]); 436 #else 437 return exception_header->adjustedPtr; 438 #endif 439 } 440 // Else this is a foreign exception 441 // If the caughtExceptions stack is not empty, terminate 442 if (globals->caughtExceptions != 0) 443 std::terminate(); 444 // Push the foreign exception on to the stack 445 globals->caughtExceptions = exception_header; 446 return unwind_exception + 1; 447 } 448 449 450 /* 451 Upon exit for any reason, a handler must call: 452 void __cxa_end_catch (); 453 454 This routine can be called for either a native or foreign exception. 455 For a native exception: 456 * Locates the most recently caught exception and decrements its handler count. 457 * Removes the exception from the caught exception stack, if the handler count goes to zero. 458 * If the handler count goes down to zero, and the exception was not re-thrown 459 by throw, it locates the primary exception (which may be the same as the one 460 it's handling) and decrements its reference count. If that reference count 461 goes to zero, the function destroys the exception. In any case, if the current 462 exception is a dependent exception, it destroys that. 463 464 For a foreign exception: 465 * If it has been rethrown, there is nothing to do. 466 * Otherwise delete the exception and pop the catch stack to empty. 467 */ 468 void __cxa_end_catch() { 469 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception), 470 "sizeof(__cxa_exception) must be equal to " 471 "sizeof(__cxa_dependent_exception)"); 472 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) == 473 __builtin_offsetof(__cxa_dependent_exception, 474 primaryException), 475 "the layout of __cxa_exception must match the layout of " 476 "__cxa_dependent_exception"); 477 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) == 478 __builtin_offsetof(__cxa_dependent_exception, handlerCount), 479 "the layout of __cxa_exception must match the layout of " 480 "__cxa_dependent_exception"); 481 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch 482 __cxa_exception* exception_header = globals->caughtExceptions; 483 // If we've rethrown a foreign exception, then globals->caughtExceptions 484 // will have been made an empty stack by __cxa_rethrow() and there is 485 // nothing more to be done. Do nothing! 486 if (NULL != exception_header) 487 { 488 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader); 489 if (native_exception) 490 { 491 // This is a native exception 492 if (exception_header->handlerCount < 0) 493 { 494 // The exception has been rethrown by __cxa_rethrow, so don't delete it 495 if (0 == incrementHandlerCount(exception_header)) 496 { 497 // Remove from the chain of uncaught exceptions 498 globals->caughtExceptions = exception_header->nextException; 499 // but don't destroy 500 } 501 // Keep handlerCount negative in case there are nested catch's 502 // that need to be told that this exception is rethrown. Don't 503 // erase this rethrow flag until the exception is recaught. 504 } 505 else 506 { 507 // The native exception has not been rethrown 508 if (0 == decrementHandlerCount(exception_header)) 509 { 510 // Remove from the chain of uncaught exceptions 511 globals->caughtExceptions = exception_header->nextException; 512 // Destroy this exception, being careful to distinguish 513 // between dependent and primary exceptions 514 if (isDependentException(&exception_header->unwindHeader)) 515 { 516 // Reset exception_header to primaryException and deallocate the dependent exception 517 __cxa_dependent_exception* dep_exception_header = 518 reinterpret_cast<__cxa_dependent_exception*>(exception_header); 519 exception_header = 520 cxa_exception_from_thrown_object(dep_exception_header->primaryException); 521 __cxa_free_dependent_exception(dep_exception_header); 522 } 523 // Destroy the primary exception only if its referenceCount goes to 0 524 // (this decrement must be atomic) 525 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header)); 526 } 527 } 528 } 529 else 530 { 531 // The foreign exception has not been rethrown. Pop the stack 532 // and delete it. If there are nested catch's and they try 533 // to touch a foreign exception in any way, that is undefined 534 // behavior. They likely can't since the only way to catch 535 // a foreign exception is with catch (...)! 536 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader); 537 globals->caughtExceptions = 0; 538 } 539 } 540 } 541 542 // Note: exception_header may be masquerading as a __cxa_dependent_exception 543 // and that's ok. exceptionType is there too. 544 // However watch out for foreign exceptions. Return null for them. 545 std::type_info *__cxa_current_exception_type() { 546 // get the current exception 547 __cxa_eh_globals *globals = __cxa_get_globals_fast(); 548 if (NULL == globals) 549 return NULL; // If there have never been any exceptions, there are none now. 550 __cxa_exception *exception_header = globals->caughtExceptions; 551 if (NULL == exception_header) 552 return NULL; // No current exception 553 if (!isOurExceptionClass(&exception_header->unwindHeader)) 554 return NULL; 555 return exception_header->exceptionType; 556 } 557 558 // 2.5.4 Rethrowing Exceptions 559 /* This routine can rethrow native or foreign exceptions. 560 If the exception is native: 561 * marks the exception object on top of the caughtExceptions stack 562 (in an implementation-defined way) as being rethrown. 563 * If the caughtExceptions stack is empty, it calls terminate() 564 (see [C++FDIS] [except.throw], 15.1.8). 565 * It then calls _Unwind_RaiseException which should not return 566 (terminate if it does). 567 Note: exception_header may be masquerading as a __cxa_dependent_exception 568 and that's ok. 569 */ 570 void __cxa_rethrow() { 571 __cxa_eh_globals* globals = __cxa_get_globals(); 572 __cxa_exception* exception_header = globals->caughtExceptions; 573 if (NULL == exception_header) 574 std::terminate(); // throw; called outside of a exception handler 575 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader); 576 if (native_exception) 577 { 578 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch) 579 exception_header->handlerCount = -exception_header->handlerCount; 580 globals->uncaughtExceptions += 1; 581 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary 582 } 583 else // this is a foreign exception 584 { 585 // The only way to communicate to __cxa_end_catch that we've rethrown 586 // a foreign exception, so don't delete us, is to pop the stack here 587 // which must be empty afterwards. Then __cxa_end_catch will do 588 // nothing 589 globals->caughtExceptions = 0; 590 } 591 #ifdef __USING_SJLJ_EXCEPTIONS__ 592 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); 593 #else 594 _Unwind_RaiseException(&exception_header->unwindHeader); 595 #endif 596 597 // If we get here, some kind of unwinding error has occurred. 598 // There is some weird code generation bug happening with 599 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn) 600 // If we call failed_throw here. Turns up with -O2 or higher, and -Os. 601 __cxa_begin_catch(&exception_header->unwindHeader); 602 if (native_exception) 603 std::__terminate(exception_header->terminateHandler); 604 // Foreign exception: can't get exception_header->terminateHandler 605 std::terminate(); 606 } 607 608 /* 609 If thrown_object is not null, atomically increment the referenceCount field 610 of the __cxa_exception header associated with the thrown object referred to 611 by thrown_object. 612 613 Requires: If thrown_object is not NULL, it is a native exception. 614 */ 615 void 616 __cxa_increment_exception_refcount(void *thrown_object) throw() { 617 if (thrown_object != NULL ) 618 { 619 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 620 std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(1)); 621 } 622 } 623 624 /* 625 If thrown_object is not null, atomically decrement the referenceCount field 626 of the __cxa_exception header associated with the thrown object referred to 627 by thrown_object. If the referenceCount drops to zero, destroy and 628 deallocate the exception. 629 630 Requires: If thrown_object is not NULL, it is a native exception. 631 */ 632 _LIBCXXABI_NO_CFI 633 void __cxa_decrement_exception_refcount(void *thrown_object) throw() { 634 if (thrown_object != NULL ) 635 { 636 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 637 if (std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(-1)) == 0) 638 { 639 if (NULL != exception_header->exceptionDestructor) 640 exception_header->exceptionDestructor(thrown_object); 641 __cxa_free_exception(thrown_object); 642 } 643 } 644 } 645 646 /* 647 Returns a pointer to the thrown object (if any) at the top of the 648 caughtExceptions stack. Atomically increment the exception's referenceCount. 649 If there is no such thrown object or if the thrown object is foreign, 650 returns null. 651 652 We can use __cxa_get_globals_fast here to get the globals because if there have 653 been no exceptions thrown, ever, on this thread, we can return NULL without 654 the need to allocate the exception-handling globals. 655 */ 656 void *__cxa_current_primary_exception() throw() { 657 // get the current exception 658 __cxa_eh_globals* globals = __cxa_get_globals_fast(); 659 if (NULL == globals) 660 return NULL; // If there are no globals, there is no exception 661 __cxa_exception* exception_header = globals->caughtExceptions; 662 if (NULL == exception_header) 663 return NULL; // No current exception 664 if (!isOurExceptionClass(&exception_header->unwindHeader)) 665 return NULL; // Can't capture a foreign exception (no way to refcount it) 666 if (isDependentException(&exception_header->unwindHeader)) { 667 __cxa_dependent_exception* dep_exception_header = 668 reinterpret_cast<__cxa_dependent_exception*>(exception_header); 669 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException); 670 } 671 void* thrown_object = thrown_object_from_cxa_exception(exception_header); 672 __cxa_increment_exception_refcount(thrown_object); 673 return thrown_object; 674 } 675 676 /* 677 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler 678 stored in exc is called. Otherwise the referenceCount stored in the 679 primary exception is decremented, destroying the primary if necessary. 680 Finally the dependent exception is destroyed. 681 */ 682 static 683 void 684 dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) 685 { 686 __cxa_dependent_exception* dep_exception_header = 687 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1; 688 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) 689 std::__terminate(dep_exception_header->terminateHandler); 690 __cxa_decrement_exception_refcount(dep_exception_header->primaryException); 691 __cxa_free_dependent_exception(dep_exception_header); 692 } 693 694 /* 695 If thrown_object is not null, allocate, initialize and throw a dependent 696 exception. 697 */ 698 void 699 __cxa_rethrow_primary_exception(void* thrown_object) 700 { 701 if ( thrown_object != NULL ) 702 { 703 // thrown_object guaranteed to be native because 704 // __cxa_current_primary_exception returns NULL for foreign exceptions 705 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 706 __cxa_dependent_exception* dep_exception_header = 707 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception()); 708 dep_exception_header->primaryException = thrown_object; 709 __cxa_increment_exception_refcount(thrown_object); 710 dep_exception_header->exceptionType = exception_header->exceptionType; 711 dep_exception_header->unexpectedHandler = std::get_unexpected(); 712 dep_exception_header->terminateHandler = std::get_terminate(); 713 setDependentExceptionClass(&dep_exception_header->unwindHeader); 714 __cxa_get_globals()->uncaughtExceptions += 1; 715 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup; 716 #ifdef __USING_SJLJ_EXCEPTIONS__ 717 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader); 718 #else 719 _Unwind_RaiseException(&dep_exception_header->unwindHeader); 720 #endif 721 // Some sort of unwinding error. Note that terminate is a handler. 722 __cxa_begin_catch(&dep_exception_header->unwindHeader); 723 } 724 // If we return client will call terminate() 725 } 726 727 bool 728 __cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; } 729 730 unsigned int 731 __cxa_uncaught_exceptions() throw() 732 { 733 // This does not report foreign exceptions in flight 734 __cxa_eh_globals* globals = __cxa_get_globals_fast(); 735 if (globals == 0) 736 return 0; 737 return globals->uncaughtExceptions; 738 } 739 740 } // extern "C" 741 742 } // abi 743