11using namespace ABI_NAMESPACE; 12 13/** 14 * Saves the result of the landing pad that we have found. For ARM, this is 15 * stored in the generic unwind structure, while on other platforms it is 16 * stored in the C++ exception. 17 */ 18static void saveLandingPad(struct _Unwind_Context *context, 19 struct _Unwind_Exception *ucb, 20 struct __cxa_exception *ex, 21 int selector, 22 dw_eh_ptr_t landingPad) 23{ 24#ifdef __arm__ 25 // On ARM, we store the saved exception in the generic part of the structure 26 ucb->barrier_cache.sp = _Unwind_GetGR(context, 13); 27 ucb->barrier_cache.bitpattern[1] = (uint32_t)selector; 28 ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad; 29#endif 30 // Cache the results for the phase 2 unwind, if we found a handler 31 // and this is not a foreign exception. 32 if (ex) 33 { 34 ex->handlerSwitchValue = selector; 35 ex->catchTemp = landingPad; 36 } 37} 38 39/** 40 * Loads the saved landing pad. Returns 1 on success, 0 on failure. 41 */ 42static int loadLandingPad(struct _Unwind_Context *context, 43 struct _Unwind_Exception *ucb, 44 struct __cxa_exception *ex, 45 unsigned long *selector, 46 dw_eh_ptr_t *landingPad) 47{ 48#ifdef __arm__ 49 *selector = ucb->barrier_cache.bitpattern[1]; 50 *landingPad = (dw_eh_ptr_t)ucb->barrier_cache.bitpattern[3]; 51 return 1; 52#else 53 if (ex) 54 { 55 *selector = ex->handlerSwitchValue; 56 *landingPad = (dw_eh_ptr_t)ex->catchTemp; 57 return 0; 58 } 59 return 0; 60#endif 61} 62 63static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex, 64 struct _Unwind_Context *context) 65{ 66#ifdef __arm__ 67 if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; } 68#endif 69 return _URC_CONTINUE_UNWIND; 70} 71 72 73extern "C" void __cxa_free_exception(void *thrown_exception); 74extern "C" void __cxa_free_dependent_exception(void *thrown_exception); 75extern "C" void* __dynamic_cast(const void *sub, 76 const __class_type_info *src, 77 const __class_type_info *dst, 78 ptrdiff_t src2dst_offset); 79 80/** 81 * The type of a handler that has been found. 82 */ 83typedef enum 84{ 85 /** No handler. */ 86 handler_none, 87 /** 88 * A cleanup - the exception will propagate through this frame, but code 89 * must be run when this happens. 90 */ 91 handler_cleanup, 92 /** 93 * A catch statement. The exception will not propagate past this frame 94 * (without an explicit rethrow). 95 */ 96 handler_catch 97} handler_type; 98 99/** 100 * Per-thread info required by the runtime. We store a single structure 101 * pointer in thread-local storage, because this tends to be a scarce resource 102 * and it's impolite to steal all of it and not leave any for the rest of the 103 * program. 104 * 105 * Instances of this structure are allocated lazily - at most one per thread - 106 * and are destroyed on thread termination. 107 */ 108struct __cxa_thread_info 109{ 110 /** The termination handler for this thread. */ 111 terminate_handler terminateHandler; 112 /** The unexpected exception handler for this thread. */ 113 unexpected_handler unexpectedHandler; 114 /** 115 * The number of emergency buffers held by this thread. This is 0 in 116 * normal operation - the emergency buffers are only used when malloc() 117 * fails to return memory for allocating an exception. Threads are not 118 * permitted to hold more than 4 emergency buffers (as per recommendation 119 * in ABI spec [3.3.1]). 120 */ 121 int emergencyBuffersHeld; 122 /** 123 * The exception currently running in a cleanup. 124 */ 125 _Unwind_Exception *currentCleanup; 126 /** 127 * The public part of this structure, accessible from outside of this 128 * module. 129 */ 130 __cxa_eh_globals globals; 131}; 132/** 133 * Dependent exception. This 134 */ 135struct __cxa_dependent_exception 136{ 137#if __LP64__ 138 void *primaryException; 139#endif 140 std::type_info *exceptionType; 141 void (*exceptionDestructor) (void *); 142 unexpected_handler unexpectedHandler; 143 terminate_handler terminateHandler; 144 __cxa_exception *nextException; 145 int handlerCount; 146#ifdef __arm__ 147 _Unwind_Exception *nextCleanup; 148 int cleanupCount; 149#endif 150 int handlerSwitchValue; 151 const char *actionRecord; 152 const char *languageSpecificData; 153 void *catchTemp; 154 void *adjustedPtr; 155#if !__LP64__ 156 void *primaryException; 157#endif 158 _Unwind_Exception unwindHeader; 159}; 160 161 162namespace std 163{ 164 void unexpected(); 165 class exception 166 { 167 public: 168 virtual ~exception() throw(); 169 virtual const char* what() const throw(); 170 }; 171 172} 173 174extern "C" std::type_info *__cxa_current_exception_type(); 175 176/** 177 * Class of exceptions to distinguish between this and other exception types. 178 * 179 * The first four characters are the vendor ID. Currently, we use GNUC, 180 * because we aim for ABI-compatibility with the GNU implementation, and 181 * various checks may test for equality of the class, which is incorrect. 182 */ 183static const uint64_t exception_class = 184 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0'); 185/** 186 * Class used for dependent exceptions. 187 */ 188static const uint64_t dependent_exception_class = 189 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01'); 190/** 191 * The low four bytes of the exception class, indicating that we conform to the 192 * Itanium C++ ABI. This is currently unused, but should be used in the future 193 * if we change our exception class, to allow this library and libsupc++ to be 194 * linked to the same executable and both to interoperate. 195 */ 196static const uint32_t abi_exception_class = 197 GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0'); 198 199static bool isCXXException(uint64_t cls) 200{ 201 return (cls == exception_class) || (cls == dependent_exception_class); 202} 203 204static bool isDependentException(uint64_t cls) 205{ 206 return cls == dependent_exception_class; 207} 208 209static __cxa_exception *exceptionFromPointer(void *ex) 210{ 211 return (__cxa_exception*)((char*)ex - 212 offsetof(struct __cxa_exception, unwindHeader)); 213} 214static __cxa_exception *realExceptionFromException(__cxa_exception *ex) 215{ 216 if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; } 217 return ((__cxa_exception*)(((__cxa_dependent_exception*)ex)->primaryException))-1; 218} 219 220 221namespace std 222{ 223 // Forward declaration of standard library terminate() function used to 224 // abort execution. 225 void terminate(void); 226} 227 228using namespace ABI_NAMESPACE; 229 230 231 232/** The global termination handler. */ 233static terminate_handler terminateHandler = abort; 234/** The global unexpected exception handler. */ 235static unexpected_handler unexpectedHandler = std::terminate; 236 237/** Key used for thread-local data. */ 238static pthread_key_t eh_key; 239 240 241/** 242 * Cleanup function, allowing foreign exception handlers to correctly destroy 243 * this exception if they catch it. 244 */ 245static void exception_cleanup(_Unwind_Reason_Code reason, 246 struct _Unwind_Exception *ex) 247{ 248 __cxa_free_exception((void*)ex); 249} 250static void dependent_exception_cleanup(_Unwind_Reason_Code reason, 251 struct _Unwind_Exception *ex) 252{ 253 254 __cxa_free_dependent_exception((void*)ex); 255} 256 257/** 258 * Recursively walk a list of exceptions and delete them all in post-order. 259 */ 260static void free_exception_list(__cxa_exception *ex) 261{ 262 if (0 != ex->nextException) 263 { 264 free_exception_list(ex->nextException); 265 } 266 // __cxa_free_exception() expects to be passed the thrown object, which 267 // immediately follows the exception, not the exception itself 268 __cxa_free_exception(ex+1); 269} 270 271/** 272 * Cleanup function called when a thread exists to make certain that all of the 273 * per-thread data is deleted. 274 */ 275static void thread_cleanup(void* thread_info) 276{ 277 __cxa_thread_info *info = (__cxa_thread_info*)thread_info; 278 if (info->globals.caughtExceptions) 279 { 280 free_exception_list(info->globals.caughtExceptions); 281 } 282 free(thread_info); 283} 284 285 286/** 287 * Once control used to protect the key creation. 288 */ 289static pthread_once_t once_control = PTHREAD_ONCE_INIT; 290 291/**
| 22using namespace ABI_NAMESPACE; 23 24/** 25 * Saves the result of the landing pad that we have found. For ARM, this is 26 * stored in the generic unwind structure, while on other platforms it is 27 * stored in the C++ exception. 28 */ 29static void saveLandingPad(struct _Unwind_Context *context, 30 struct _Unwind_Exception *ucb, 31 struct __cxa_exception *ex, 32 int selector, 33 dw_eh_ptr_t landingPad) 34{ 35#ifdef __arm__ 36 // On ARM, we store the saved exception in the generic part of the structure 37 ucb->barrier_cache.sp = _Unwind_GetGR(context, 13); 38 ucb->barrier_cache.bitpattern[1] = (uint32_t)selector; 39 ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad; 40#endif 41 // Cache the results for the phase 2 unwind, if we found a handler 42 // and this is not a foreign exception. 43 if (ex) 44 { 45 ex->handlerSwitchValue = selector; 46 ex->catchTemp = landingPad; 47 } 48} 49 50/** 51 * Loads the saved landing pad. Returns 1 on success, 0 on failure. 52 */ 53static int loadLandingPad(struct _Unwind_Context *context, 54 struct _Unwind_Exception *ucb, 55 struct __cxa_exception *ex, 56 unsigned long *selector, 57 dw_eh_ptr_t *landingPad) 58{ 59#ifdef __arm__ 60 *selector = ucb->barrier_cache.bitpattern[1]; 61 *landingPad = (dw_eh_ptr_t)ucb->barrier_cache.bitpattern[3]; 62 return 1; 63#else 64 if (ex) 65 { 66 *selector = ex->handlerSwitchValue; 67 *landingPad = (dw_eh_ptr_t)ex->catchTemp; 68 return 0; 69 } 70 return 0; 71#endif 72} 73 74static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex, 75 struct _Unwind_Context *context) 76{ 77#ifdef __arm__ 78 if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; } 79#endif 80 return _URC_CONTINUE_UNWIND; 81} 82 83 84extern "C" void __cxa_free_exception(void *thrown_exception); 85extern "C" void __cxa_free_dependent_exception(void *thrown_exception); 86extern "C" void* __dynamic_cast(const void *sub, 87 const __class_type_info *src, 88 const __class_type_info *dst, 89 ptrdiff_t src2dst_offset); 90 91/** 92 * The type of a handler that has been found. 93 */ 94typedef enum 95{ 96 /** No handler. */ 97 handler_none, 98 /** 99 * A cleanup - the exception will propagate through this frame, but code 100 * must be run when this happens. 101 */ 102 handler_cleanup, 103 /** 104 * A catch statement. The exception will not propagate past this frame 105 * (without an explicit rethrow). 106 */ 107 handler_catch 108} handler_type; 109 110/** 111 * Per-thread info required by the runtime. We store a single structure 112 * pointer in thread-local storage, because this tends to be a scarce resource 113 * and it's impolite to steal all of it and not leave any for the rest of the 114 * program. 115 * 116 * Instances of this structure are allocated lazily - at most one per thread - 117 * and are destroyed on thread termination. 118 */ 119struct __cxa_thread_info 120{ 121 /** The termination handler for this thread. */ 122 terminate_handler terminateHandler; 123 /** The unexpected exception handler for this thread. */ 124 unexpected_handler unexpectedHandler; 125 /** 126 * The number of emergency buffers held by this thread. This is 0 in 127 * normal operation - the emergency buffers are only used when malloc() 128 * fails to return memory for allocating an exception. Threads are not 129 * permitted to hold more than 4 emergency buffers (as per recommendation 130 * in ABI spec [3.3.1]). 131 */ 132 int emergencyBuffersHeld; 133 /** 134 * The exception currently running in a cleanup. 135 */ 136 _Unwind_Exception *currentCleanup; 137 /** 138 * The public part of this structure, accessible from outside of this 139 * module. 140 */ 141 __cxa_eh_globals globals; 142}; 143/** 144 * Dependent exception. This 145 */ 146struct __cxa_dependent_exception 147{ 148#if __LP64__ 149 void *primaryException; 150#endif 151 std::type_info *exceptionType; 152 void (*exceptionDestructor) (void *); 153 unexpected_handler unexpectedHandler; 154 terminate_handler terminateHandler; 155 __cxa_exception *nextException; 156 int handlerCount; 157#ifdef __arm__ 158 _Unwind_Exception *nextCleanup; 159 int cleanupCount; 160#endif 161 int handlerSwitchValue; 162 const char *actionRecord; 163 const char *languageSpecificData; 164 void *catchTemp; 165 void *adjustedPtr; 166#if !__LP64__ 167 void *primaryException; 168#endif 169 _Unwind_Exception unwindHeader; 170}; 171 172 173namespace std 174{ 175 void unexpected(); 176 class exception 177 { 178 public: 179 virtual ~exception() throw(); 180 virtual const char* what() const throw(); 181 }; 182 183} 184 185extern "C" std::type_info *__cxa_current_exception_type(); 186 187/** 188 * Class of exceptions to distinguish between this and other exception types. 189 * 190 * The first four characters are the vendor ID. Currently, we use GNUC, 191 * because we aim for ABI-compatibility with the GNU implementation, and 192 * various checks may test for equality of the class, which is incorrect. 193 */ 194static const uint64_t exception_class = 195 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0'); 196/** 197 * Class used for dependent exceptions. 198 */ 199static const uint64_t dependent_exception_class = 200 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01'); 201/** 202 * The low four bytes of the exception class, indicating that we conform to the 203 * Itanium C++ ABI. This is currently unused, but should be used in the future 204 * if we change our exception class, to allow this library and libsupc++ to be 205 * linked to the same executable and both to interoperate. 206 */ 207static const uint32_t abi_exception_class = 208 GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0'); 209 210static bool isCXXException(uint64_t cls) 211{ 212 return (cls == exception_class) || (cls == dependent_exception_class); 213} 214 215static bool isDependentException(uint64_t cls) 216{ 217 return cls == dependent_exception_class; 218} 219 220static __cxa_exception *exceptionFromPointer(void *ex) 221{ 222 return (__cxa_exception*)((char*)ex - 223 offsetof(struct __cxa_exception, unwindHeader)); 224} 225static __cxa_exception *realExceptionFromException(__cxa_exception *ex) 226{ 227 if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; } 228 return ((__cxa_exception*)(((__cxa_dependent_exception*)ex)->primaryException))-1; 229} 230 231 232namespace std 233{ 234 // Forward declaration of standard library terminate() function used to 235 // abort execution. 236 void terminate(void); 237} 238 239using namespace ABI_NAMESPACE; 240 241 242 243/** The global termination handler. */ 244static terminate_handler terminateHandler = abort; 245/** The global unexpected exception handler. */ 246static unexpected_handler unexpectedHandler = std::terminate; 247 248/** Key used for thread-local data. */ 249static pthread_key_t eh_key; 250 251 252/** 253 * Cleanup function, allowing foreign exception handlers to correctly destroy 254 * this exception if they catch it. 255 */ 256static void exception_cleanup(_Unwind_Reason_Code reason, 257 struct _Unwind_Exception *ex) 258{ 259 __cxa_free_exception((void*)ex); 260} 261static void dependent_exception_cleanup(_Unwind_Reason_Code reason, 262 struct _Unwind_Exception *ex) 263{ 264 265 __cxa_free_dependent_exception((void*)ex); 266} 267 268/** 269 * Recursively walk a list of exceptions and delete them all in post-order. 270 */ 271static void free_exception_list(__cxa_exception *ex) 272{ 273 if (0 != ex->nextException) 274 { 275 free_exception_list(ex->nextException); 276 } 277 // __cxa_free_exception() expects to be passed the thrown object, which 278 // immediately follows the exception, not the exception itself 279 __cxa_free_exception(ex+1); 280} 281 282/** 283 * Cleanup function called when a thread exists to make certain that all of the 284 * per-thread data is deleted. 285 */ 286static void thread_cleanup(void* thread_info) 287{ 288 __cxa_thread_info *info = (__cxa_thread_info*)thread_info; 289 if (info->globals.caughtExceptions) 290 { 291 free_exception_list(info->globals.caughtExceptions); 292 } 293 free(thread_info); 294} 295 296 297/** 298 * Once control used to protect the key creation. 299 */ 300static pthread_once_t once_control = PTHREAD_ONCE_INIT; 301 302/**
|
441} 442 443static char *alloc_or_die(size_t size) 444{ 445 char *buffer = (char*)calloc(1, size); 446 447 // If calloc() doesn't want to give us any memory, try using an emergency 448 // buffer. 449 if (0 == buffer) 450 { 451 buffer = emergency_malloc(size); 452 // This is only reached if the allocation is greater than 1KB, and 453 // anyone throwing objects that big really should know better. 454 if (0 == buffer) 455 { 456 fprintf(stderr, "Out of memory attempting to allocate exception\n"); 457 std::terminate(); 458 } 459 } 460 return buffer; 461} 462static void free_exception(char *e) 463{ 464 // If this allocation is within the address range of the emergency buffer, 465 // don't call free() because it was not allocated with malloc() 466 if ((e > emergency_buffer) && 467 (e < (emergency_buffer + sizeof(emergency_buffer)))) 468 { 469 emergency_malloc_free(e); 470 } 471 else 472 { 473 free(e); 474 } 475} 476 477/** 478 * Allocates an exception structure. Returns a pointer to the space that can 479 * be used to store an object of thrown_size bytes. This function will use an 480 * emergency buffer if malloc() fails, and may block if there are no such 481 * buffers available. 482 */ 483extern "C" void *__cxa_allocate_exception(size_t thrown_size) 484{ 485 size_t size = thrown_size + sizeof(__cxa_exception); 486 char *buffer = alloc_or_die(size); 487 return buffer+sizeof(__cxa_exception); 488} 489 490extern "C" void *__cxa_allocate_dependent_exception(void) 491{ 492 size_t size = sizeof(__cxa_dependent_exception); 493 char *buffer = alloc_or_die(size); 494 return buffer+sizeof(__cxa_dependent_exception); 495} 496 497/** 498 * __cxa_free_exception() is called when an exception was thrown in between 499 * calling __cxa_allocate_exception() and actually throwing the exception. 500 * This happens when the object's copy constructor throws an exception. 501 * 502 * In this implementation, it is also called by __cxa_end_catch() and during 503 * thread cleanup. 504 */ 505extern "C" void __cxa_free_exception(void *thrown_exception) 506{ 507 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 508 // Free the object that was thrown, calling its destructor 509 if (0 != ex->exceptionDestructor) 510 { 511 try 512 { 513 ex->exceptionDestructor(thrown_exception); 514 } 515 catch(...) 516 { 517 // FIXME: Check that this is really what the spec says to do. 518 std::terminate(); 519 } 520 } 521 522 free_exception((char*)ex); 523} 524 525static void releaseException(__cxa_exception *exception) 526{ 527 if (isDependentException(exception->unwindHeader.exception_class)) 528 { 529 __cxa_free_dependent_exception(exception+1); 530 return; 531 } 532 if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0) 533 { 534 // __cxa_free_exception() expects to be passed the thrown object, 535 // which immediately follows the exception, not the exception 536 // itself 537 __cxa_free_exception(exception+1); 538 } 539} 540 541void __cxa_free_dependent_exception(void *thrown_exception) 542{ 543 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)thrown_exception) - 1; 544 assert(isDependentException(ex->unwindHeader.exception_class)); 545 if (ex->primaryException) 546 { 547 releaseException(realExceptionFromException((__cxa_exception*)ex)); 548 } 549 free_exception((char*)ex); 550} 551 552/** 553 * Callback function used with _Unwind_Backtrace(). 554 * 555 * Prints a stack trace. Used only for debugging help. 556 * 557 * Note: As of FreeBSD 8.1, dladd() still doesn't work properly, so this only 558 * correctly prints function names from public, relocatable, symbols. 559 */ 560static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c) 561{ 562 Dl_info myinfo; 563 int mylookup = 564 dladdr((void*)(uintptr_t)__cxa_current_exception_type, &myinfo); 565 void *ip = (void*)_Unwind_GetIP(context); 566 Dl_info info; 567 if (dladdr(ip, &info) != 0) 568 { 569 if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0) 570 { 571 printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname); 572 } 573 } 574 return _URC_CONTINUE_UNWIND; 575} 576 577/** 578 * Report a failure that occurred when attempting to throw an exception. 579 * 580 * If the failure happened by falling off the end of the stack without finding 581 * a handler, prints a back trace before aborting. 582 */ 583static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception) 584{ 585 switch (err) 586 { 587 default: break; 588 case _URC_FATAL_PHASE1_ERROR: 589 fprintf(stderr, "Fatal error during phase 1 unwinding\n"); 590 break; 591#ifndef __arm__ 592 case _URC_FATAL_PHASE2_ERROR: 593 fprintf(stderr, "Fatal error during phase 2 unwinding\n"); 594 break; 595#endif 596 case _URC_END_OF_STACK: 597 fprintf(stderr, "Terminating due to uncaught exception %p", 598 (void*)thrown_exception); 599 thrown_exception = realExceptionFromException(thrown_exception); 600 static const __class_type_info *e_ti = 601 static_cast<const __class_type_info*>(&typeid(std::exception)); 602 const __class_type_info *throw_ti = 603 dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType); 604 if (throw_ti) 605 { 606 std::exception *e = 607 (std::exception*)e_ti->cast_to((void*)(thrown_exception+1), 608 throw_ti); 609 if (e) 610 { 611 fprintf(stderr, " '%s'", e->what()); 612 } 613 } 614 615 size_t bufferSize = 128; 616 char *demangled = (char*)malloc(bufferSize); 617 const char *mangled = thrown_exception->exceptionType->name(); 618 int status; 619 demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status); 620 fprintf(stderr, " of type %s\n", 621 status == 0 ? (const char*)demangled : mangled); 622 if (status == 0) { free(demangled); } 623 // Print a back trace if no handler is found. 624 // TODO: Make this optional 625 _Unwind_Backtrace(trace, 0); 626 break; 627 } 628 std::terminate(); 629} 630 631static void throw_exception(__cxa_exception *ex) 632{ 633 __cxa_thread_info *info = thread_info(); 634 ex->unexpectedHandler = info->unexpectedHandler; 635 if (0 == ex->unexpectedHandler) 636 { 637 ex->unexpectedHandler = unexpectedHandler; 638 } 639 ex->terminateHandler = info->terminateHandler; 640 if (0 == ex->terminateHandler) 641 { 642 ex->terminateHandler = terminateHandler; 643 } 644 info->globals.uncaughtExceptions++; 645 646 _Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader); 647 // The _Unwind_RaiseException() function should not return, it should 648 // unwind the stack past this function. If it does return, then something 649 // has gone wrong. 650 report_failure(err, ex); 651} 652 653 654/** 655 * ABI function for throwing an exception. Takes the object to be thrown (the 656 * pointer returned by __cxa_allocate_exception()), the type info for the 657 * pointee, and the destructor (if there is one) as arguments. 658 */ 659extern "C" void __cxa_throw(void *thrown_exception, 660 std::type_info *tinfo, 661 void(*dest)(void*)) 662{ 663 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 664 665 ex->referenceCount = 1; 666 ex->exceptionType = tinfo; 667 668 ex->exceptionDestructor = dest; 669 670 ex->unwindHeader.exception_class = exception_class; 671 ex->unwindHeader.exception_cleanup = exception_cleanup; 672 673 throw_exception(ex); 674} 675 676extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception) 677{ 678 if (NULL == thrown_exception) { return; } 679 680 __cxa_exception *original = exceptionFromPointer(thrown_exception); 681 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)__cxa_allocate_dependent_exception())-1; 682 683 ex->primaryException = thrown_exception; 684 __cxa_increment_exception_refcount(thrown_exception); 685 686 ex->exceptionType = original->exceptionType; 687 ex->unwindHeader.exception_class = dependent_exception_class; 688 ex->unwindHeader.exception_cleanup = dependent_exception_cleanup; 689 690 throw_exception((__cxa_exception*)ex); 691} 692 693extern "C" void *__cxa_current_primary_exception(void) 694{ 695 __cxa_eh_globals* globals = __cxa_get_globals(); 696 __cxa_exception *ex = globals->caughtExceptions; 697 698 if (0 == ex) { return NULL; } 699 ex = realExceptionFromException(ex); 700 __sync_fetch_and_add(&ex->referenceCount, 1); 701 return ex + 1; 702} 703 704extern "C" void __cxa_increment_exception_refcount(void* thrown_exception) 705{ 706 if (NULL == thrown_exception) { return; } 707 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 708 if (isDependentException(ex->unwindHeader.exception_class)) { return; } 709 __sync_fetch_and_add(&ex->referenceCount, 1); 710} 711extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception) 712{ 713 if (NULL == thrown_exception) { return; } 714 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 715 releaseException(ex); 716} 717 718/** 719 * ABI function. Rethrows the current exception. Does not remove the 720 * exception from the stack or decrement its handler count - the compiler is 721 * expected to set the landing pad for this function to the end of the catch 722 * block, and then call _Unwind_Resume() to continue unwinding once 723 * __cxa_end_catch() has been called and any cleanup code has been run. 724 */ 725extern "C" void __cxa_rethrow() 726{ 727 __cxa_eh_globals *globals = __cxa_get_globals(); 728 // Note: We don't remove this from the caught list here, because 729 // __cxa_end_catch will be called when we unwind out of the try block. We 730 // could probably make this faster by providing an alternative rethrow 731 // function and ensuring that all cleanup code is run before calling it, so 732 // we can skip the top stack frame when unwinding. 733 __cxa_exception *ex = globals->caughtExceptions; 734 735 if (0 == ex) 736 { 737 fprintf(stderr, 738 "Attempting to rethrow an exception that doesn't exist!\n"); 739 std::terminate(); 740 } 741 742 assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!"); 743 744 // ex->handlerCount will be decremented in __cxa_end_catch in enclosing 745 // catch block 746 747 // Make handler count negative. This will tell __cxa_end_catch that 748 // exception was rethrown and exception object should not be destroyed 749 // when handler count become zero 750 ex->handlerCount = -ex->handlerCount; 751 752 // Continue unwinding the stack with this exception. This should unwind to 753 // the place in the caller where __cxa_end_catch() is called. The caller 754 // will then run cleanup code and bounce the exception back with 755 // _Unwind_Resume(). 756 _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader); 757 report_failure(err, ex); 758} 759 760/** 761 * Returns the type_info object corresponding to the filter. 762 */ 763static std::type_info *get_type_info_entry(_Unwind_Context *context, 764 dwarf_eh_lsda *lsda, 765 int filter) 766{ 767 // Get the address of the record in the table. 768 dw_eh_ptr_t record = lsda->type_table - 769 dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter; 770 //record -= 4; 771 dw_eh_ptr_t start = record; 772 // Read the value, but it's probably an indirect reference... 773 int64_t offset = read_value(lsda->type_table_encoding, &record); 774 775 // (If the entry is 0, don't try to dereference it. That would be bad.) 776 if (offset == 0) { return 0; } 777 778 // ...so we need to resolve it 779 return (std::type_info*)resolve_indirect_value(context, 780 lsda->type_table_encoding, offset, start); 781} 782 783 784 785/** 786 * Checks the type signature found in a handler against the type of the thrown 787 * object. If ex is 0 then it is assumed to be a foreign exception and only 788 * matches cleanups. 789 */ 790static bool check_type_signature(__cxa_exception *ex, 791 const std::type_info *type, 792 void *&adjustedPtr) 793{ 794 // TODO: For compatibility with the GNU implementation, we should move this 795 // out into a __do_catch() virtual function in std::type_info 796 void *exception_ptr = (void*)(ex+1); 797 const std::type_info *ex_type = ex->exceptionType; 798 799 const __pointer_type_info *ptr_type = 800 dynamic_cast<const __pointer_type_info*>(ex_type); 801 if (0 != ptr_type) 802 { 803 exception_ptr = *(void**)exception_ptr; 804 } 805 // Always match a catchall, even with a foreign exception 806 // 807 // Note: A 0 here is a catchall, not a cleanup, so we return true to 808 // indicate that we found a catch. 809 // 810 // TODO: Provide a class for matching against foreign exceptions. This is 811 // already done in libobjc2, allowing C++ exceptions to be boxed as 812 // Objective-C objects. We should do something similar, allowing foreign 813 // exceptions to be wrapped in a C++ exception and delivered. 814 if (0 == type) 815 { 816 if (ex) 817 { 818 adjustedPtr = exception_ptr; 819 } 820 return true; 821 } 822 823 if (0 == ex) { return false; } 824 825 const __pointer_type_info *target_ptr_type = 826 dynamic_cast<const __pointer_type_info*>(type); 827 828 if (0 != ptr_type && 0 != target_ptr_type) 829 { 830 if (ptr_type->__flags & ~target_ptr_type->__flags) 831 { 832 // Handler pointer is less qualified 833 return false; 834 } 835 836 // Special case for void* handler. 837 if(*target_ptr_type->__pointee == typeid(void)) 838 { 839 adjustedPtr = exception_ptr; 840 return true; 841 } 842 843 ex_type = ptr_type->__pointee; 844 type = target_ptr_type->__pointee; 845 } 846 847 // If the types are the same, no casting is needed. 848 if (*type == *ex_type) 849 { 850 adjustedPtr = exception_ptr; 851 return true; 852 } 853 854 const __class_type_info *cls_type = 855 dynamic_cast<const __class_type_info*>(ex_type); 856 const __class_type_info *target_cls_type = 857 dynamic_cast<const __class_type_info*>(type); 858 859 if (0 != cls_type && 860 0 != target_cls_type && 861 cls_type->can_cast_to(target_cls_type)) 862 { 863 adjustedPtr = cls_type->cast_to(exception_ptr, target_cls_type); 864 return true; 865 } 866 return false; 867} 868/** 869 * Checks whether the exception matches the type specifiers in this action 870 * record. If the exception only matches cleanups, then this returns false. 871 * If it matches a catch (including a catchall) then it returns true. 872 * 873 * The selector argument is used to return the selector that is passed in the 874 * second exception register when installing the context. 875 */ 876static handler_type check_action_record(_Unwind_Context *context, 877 dwarf_eh_lsda *lsda, 878 dw_eh_ptr_t action_record, 879 __cxa_exception *ex, 880 unsigned long *selector, 881 void *&adjustedPtr) 882{ 883 if (!action_record) { return handler_cleanup; } 884 handler_type found = handler_none; 885 while (action_record) 886 { 887 int filter = read_sleb128(&action_record); 888 dw_eh_ptr_t action_record_offset_base = action_record; 889 int displacement = read_sleb128(&action_record); 890 action_record = displacement ? 891 action_record_offset_base + displacement : 0; 892 // We only check handler types for C++ exceptions - foreign exceptions 893 // are only allowed for cleanup. 894 if (filter > 0 && 0 != ex) 895 { 896 std::type_info *handler_type = get_type_info_entry(context, lsda, filter); 897 if (check_type_signature(ex, handler_type, adjustedPtr)) 898 { 899 *selector = filter; 900 return handler_catch; 901 } 902 } 903 else if (filter < 0 && 0 != ex) 904 { 905 bool matched = false; 906 *selector = filter; 907#ifdef __arm__ 908 filter++; 909 std::type_info *handler_type = get_type_info_entry(context, lsda, filter--); 910 while (handler_type) 911 { 912 if (check_type_signature(ex, handler_type, adjustedPtr)) 913 { 914 matched = true; 915 break; 916 } 917 handler_type = get_type_info_entry(context, lsda, filter--); 918 } 919#else 920 unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1); 921 while (*type_index) 922 { 923 std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++)); 924 // If the exception spec matches a permitted throw type for 925 // this function, don't report a handler - we are allowed to 926 // propagate this exception out. 927 if (check_type_signature(ex, handler_type, adjustedPtr)) 928 { 929 matched = true; 930 break; 931 } 932 } 933#endif 934 if (matched) { continue; } 935 // If we don't find an allowed exception spec, we need to install 936 // the context for this action. The landing pad will then call the 937 // unexpected exception function. Treat this as a catch 938 return handler_catch; 939 } 940 else if (filter == 0) 941 { 942 *selector = filter; 943 found = handler_cleanup; 944 } 945 } 946 return found; 947} 948 949static void pushCleanupException(_Unwind_Exception *exceptionObject, 950 __cxa_exception *ex) 951{ 952#ifdef __arm__ 953 __cxa_thread_info *info = thread_info_fast(); 954 if (ex) 955 { 956 ex->cleanupCount++; 957 if (ex->cleanupCount > 1) 958 { 959 assert(exceptionObject == info->currentCleanup); 960 return; 961 } 962 ex->nextCleanup = info->currentCleanup; 963 } 964 info->currentCleanup = exceptionObject; 965#endif 966} 967 968/** 969 * The exception personality function. This is referenced in the unwinding 970 * DWARF metadata and is called by the unwind library for each C++ stack frame 971 * containing catch or cleanup code. 972 */ 973extern "C" 974BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) 975 // This personality function is for version 1 of the ABI. If you use it 976 // with a future version of the ABI, it won't know what to do, so it 977 // reports a fatal error and give up before it breaks anything. 978 if (1 != version) 979 { 980 return _URC_FATAL_PHASE1_ERROR; 981 } 982 __cxa_exception *ex = 0; 983 __cxa_exception *realEx = 0; 984 985 // If this exception is throw by something else then we can't make any 986 // assumptions about its layout beyond the fields declared in 987 // _Unwind_Exception. 988 bool foreignException = !isCXXException(exceptionClass); 989 990 // If this isn't a foreign exception, then we have a C++ exception structure 991 if (!foreignException) 992 { 993 ex = exceptionFromPointer(exceptionObject); 994 realEx = realExceptionFromException(ex); 995 } 996 997 unsigned char *lsda_addr = 998 (unsigned char*)_Unwind_GetLanguageSpecificData(context); 999 1000 // No LSDA implies no landing pads - try the next frame 1001 if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); } 1002 1003 // These two variables define how the exception will be handled. 1004 dwarf_eh_action action = {0}; 1005 unsigned long selector = 0; 1006 1007 // During the search phase, we do a complete lookup. If we return 1008 // _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with 1009 // a _UA_HANDLER_FRAME action, telling us to install the handler frame. If 1010 // we return _URC_CONTINUE_UNWIND, we may be called again later with a 1011 // _UA_CLEANUP_PHASE action for this frame. 1012 // 1013 // The point of the two-stage unwind allows us to entirely avoid any stack 1014 // unwinding if there is no handler. If there are just cleanups found, 1015 // then we can just panic call an abort function. 1016 // 1017 // Matching a handler is much more expensive than matching a cleanup, 1018 // because we don't need to bother doing type comparisons (or looking at 1019 // the type table at all) for a cleanup. This means that there is no need 1020 // to cache the result of finding a cleanup, because it's (quite) quick to 1021 // look it up again from the action table. 1022 if (actions & _UA_SEARCH_PHASE) 1023 { 1024 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr); 1025 1026 if (!dwarf_eh_find_callsite(context, &lsda, &action)) 1027 { 1028 // EH range not found. This happens if exception is thrown and not 1029 // caught inside a cleanup (destructor). We should call 1030 // terminate() in this case. The catchTemp (landing pad) field of 1031 // exception object will contain null when personality function is 1032 // called with _UA_HANDLER_FRAME action for phase 2 unwinding. 1033 return _URC_HANDLER_FOUND; 1034 } 1035 1036 handler_type found_handler = check_action_record(context, &lsda, 1037 action.action_record, realEx, &selector, ex->adjustedPtr); 1038 // If there's no action record, we've only found a cleanup, so keep 1039 // searching for something real 1040 if (found_handler == handler_catch) 1041 { 1042 // Cache the results for the phase 2 unwind, if we found a handler 1043 // and this is not a foreign exception. 1044 if (ex) 1045 { 1046 saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad); 1047 ex->languageSpecificData = (const char*)lsda_addr; 1048 ex->actionRecord = (const char*)action.action_record; 1049 // ex->adjustedPtr is set when finding the action record. 1050 } 1051 return _URC_HANDLER_FOUND; 1052 } 1053 return continueUnwinding(exceptionObject, context); 1054 } 1055 1056 1057 // If this is a foreign exception, we didn't have anywhere to cache the 1058 // lookup stuff, so we need to do it again. If this is either a forced 1059 // unwind, a foreign exception, or a cleanup, then we just install the 1060 // context for a cleanup. 1061 if (!(actions & _UA_HANDLER_FRAME)) 1062 { 1063 // cleanup 1064 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr); 1065 dwarf_eh_find_callsite(context, &lsda, &action); 1066 if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); } 1067 handler_type found_handler = check_action_record(context, &lsda, 1068 action.action_record, realEx, &selector, ex->adjustedPtr); 1069 // Ignore handlers this time. 1070 if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); } 1071 pushCleanupException(exceptionObject, ex); 1072 } 1073 else if (foreignException) 1074 { 1075 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr); 1076 dwarf_eh_find_callsite(context, &lsda, &action); 1077 check_action_record(context, &lsda, action.action_record, realEx, 1078 &selector, ex->adjustedPtr); 1079 } 1080 else if (ex->catchTemp == 0) 1081 { 1082 // Uncaught exception in cleanup, calling terminate 1083 std::terminate(); 1084 } 1085 else 1086 { 1087 // Restore the saved info if we saved some last time. 1088 loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad); 1089 ex->catchTemp = 0; 1090 ex->handlerSwitchValue = 0; 1091 } 1092 1093 1094 _Unwind_SetIP(context, (unsigned long)action.landing_pad); 1095 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0), 1096 (unsigned long)exceptionObject); 1097 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector); 1098 1099 return _URC_INSTALL_CONTEXT; 1100} 1101 1102/** 1103 * ABI function called when entering a catch statement. The argument is the 1104 * pointer passed out of the personality function. This is always the start of 1105 * the _Unwind_Exception object. The return value for this function is the 1106 * pointer to the caught exception, which is either the adjusted pointer (for 1107 * C++ exceptions) of the unadjusted pointer (for foreign exceptions). 1108 */ 1109#if __GNUC__ > 3 && __GNUC_MINOR__ > 2 1110extern "C" void *__cxa_begin_catch(void *e) throw() 1111#else 1112extern "C" void *__cxa_begin_catch(void *e) 1113#endif 1114{ 1115 // Decrement the uncaught exceptions count 1116 __cxa_eh_globals *globals = __cxa_get_globals(); 1117 globals->uncaughtExceptions--; 1118 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)e; 1119 1120 if (isCXXException(exceptionObject->exception_class)) 1121 { 1122 __cxa_exception *ex = exceptionFromPointer(exceptionObject); 1123 1124 if (ex->handlerCount == 0) 1125 { 1126 // Add this to the front of the list of exceptions being handled 1127 // and increment its handler count so that it won't be deleted 1128 // prematurely. 1129 ex->nextException = globals->caughtExceptions; 1130 globals->caughtExceptions = ex; 1131 } 1132 1133 if (ex->handlerCount < 0) 1134 { 1135 // Rethrown exception is catched before end of catch block. 1136 // Clear the rethrow flag (make value positive) - we are allowed 1137 // to delete this exception at the end of the catch block, as long 1138 // as it isn't thrown again later. 1139 1140 // Code pattern: 1141 // 1142 // try { 1143 // throw x; 1144 // } 1145 // catch() { 1146 // try { 1147 // throw; 1148 // } 1149 // catch() { 1150 // __cxa_begin_catch() <- we are here 1151 // } 1152 // } 1153 ex->handlerCount = -ex->handlerCount + 1; 1154 } 1155 else 1156 { 1157 ex->handlerCount++; 1158 } 1159 1160 return ex->adjustedPtr; 1161 } 1162 // exceptionObject is the pointer to the _Unwind_Exception within the 1163 // __cxa_exception. The throw object is after this 1164 return ((char*)exceptionObject + sizeof(_Unwind_Exception)); 1165} 1166 1167 1168 1169/** 1170 * ABI function called when exiting a catch block. This will free the current 1171 * exception if it is no longer referenced in other catch blocks. 1172 */ 1173extern "C" void __cxa_end_catch() 1174{ 1175 // We can call the fast version here because the slow version is called in 1176 // __cxa_throw(), which must have been called before we end a catch block 1177 __cxa_eh_globals *globals = __cxa_get_globals_fast(); 1178 __cxa_exception *ex = globals->caughtExceptions; 1179 1180 assert(0 != ex && "Ending catch when no exception is on the stack!"); 1181 1182 bool deleteException = true; 1183 1184 if (ex->handlerCount < 0) 1185 { 1186 // exception was rethrown. Exception should not be deleted even if 1187 // handlerCount become zero. 1188 // Code pattern: 1189 // try { 1190 // throw x; 1191 // } 1192 // catch() { 1193 // { 1194 // throw; 1195 // } 1196 // cleanup { 1197 // __cxa_end_catch(); <- we are here 1198 // } 1199 // } 1200 // 1201 1202 ex->handlerCount++; 1203 deleteException = false; 1204 } 1205 else 1206 { 1207 ex->handlerCount--; 1208 } 1209 1210 if (ex->handlerCount == 0) 1211 { 1212 globals->caughtExceptions = ex->nextException; 1213 if (deleteException) 1214 { 1215 releaseException(ex); 1216 } 1217 } 1218} 1219 1220/** 1221 * ABI function. Returns the type of the current exception. 1222 */ 1223extern "C" std::type_info *__cxa_current_exception_type() 1224{ 1225 __cxa_eh_globals *globals = __cxa_get_globals(); 1226 __cxa_exception *ex = globals->caughtExceptions; 1227 return ex ? ex->exceptionType : 0; 1228} 1229 1230/** 1231 * ABI function, called when an exception specification is violated. 1232 * 1233 * This function does not return. 1234 */ 1235extern "C" void __cxa_call_unexpected(void*exception) 1236{ 1237 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)exception; 1238 if (exceptionObject->exception_class == exception_class) 1239 { 1240 __cxa_exception *ex = exceptionFromPointer(exceptionObject); 1241 if (ex->unexpectedHandler) 1242 { 1243 ex->unexpectedHandler(); 1244 // Should not be reached. 1245 abort(); 1246 } 1247 } 1248 std::unexpected(); 1249 // Should not be reached. 1250 abort(); 1251} 1252 1253/** 1254 * ABI function, returns the adjusted pointer to the exception object. 1255 */ 1256extern "C" void *__cxa_get_exception_ptr(void *exceptionObject) 1257{ 1258 return exceptionFromPointer(exceptionObject)->adjustedPtr; 1259} 1260 1261/** 1262 * As an extension, we provide the ability for the unexpected and terminate 1263 * handlers to be thread-local. We default to the standards-compliant 1264 * behaviour where they are global. 1265 */ 1266static bool thread_local_handlers = false; 1267 1268 1269namespace pathscale 1270{ 1271 /** 1272 * Sets whether unexpected and terminate handlers should be thread-local. 1273 */ 1274 void set_use_thread_local_handlers(bool flag) throw() 1275 { 1276 thread_local_handlers = flag; 1277 } 1278 /** 1279 * Sets a thread-local unexpected handler. 1280 */ 1281 unexpected_handler set_unexpected(unexpected_handler f) throw() 1282 { 1283 static __cxa_thread_info *info = thread_info(); 1284 unexpected_handler old = info->unexpectedHandler; 1285 info->unexpectedHandler = f; 1286 return old; 1287 } 1288 /** 1289 * Sets a thread-local terminate handler. 1290 */ 1291 terminate_handler set_terminate(terminate_handler f) throw() 1292 { 1293 static __cxa_thread_info *info = thread_info(); 1294 terminate_handler old = info->terminateHandler; 1295 info->terminateHandler = f; 1296 return old; 1297 } 1298} 1299 1300namespace std 1301{ 1302 /** 1303 * Sets the function that will be called when an exception specification is 1304 * violated. 1305 */ 1306 unexpected_handler set_unexpected(unexpected_handler f) throw() 1307 { 1308 if (thread_local_handlers) { return pathscale::set_unexpected(f); } 1309 1310 return __sync_lock_test_and_set(&unexpectedHandler, f); 1311 } 1312 /** 1313 * Sets the function that is called to terminate the program. 1314 */ 1315 terminate_handler set_terminate(terminate_handler f) throw() 1316 { 1317 if (thread_local_handlers) { return pathscale::set_terminate(f); } 1318 return __sync_lock_test_and_set(&terminateHandler, f); 1319 } 1320 /** 1321 * Terminates the program, calling a custom terminate implementation if 1322 * required. 1323 */ 1324 void terminate() 1325 { 1326 static __cxa_thread_info *info = thread_info_fast(); 1327 if (0 != info && 0 != info->terminateHandler) 1328 { 1329 info->terminateHandler(); 1330 // Should not be reached - a terminate handler is not expected to 1331 // return. 1332 abort(); 1333 } 1334 terminateHandler(); 1335 } 1336 /** 1337 * Called when an unexpected exception is encountered (i.e. an exception 1338 * violates an exception specification). This calls abort() unless a 1339 * custom handler has been set.. 1340 */ 1341 void unexpected() 1342 { 1343 static __cxa_thread_info *info = thread_info_fast(); 1344 if (0 != info && 0 != info->unexpectedHandler) 1345 { 1346 info->unexpectedHandler(); 1347 // Should not be reached - a terminate handler is not expected to 1348 // return. 1349 abort(); 1350 } 1351 unexpectedHandler(); 1352 } 1353 /** 1354 * Returns whether there are any exceptions currently being thrown that 1355 * have not been caught. This can occur inside a nested catch statement. 1356 */ 1357 bool uncaught_exception() throw() 1358 { 1359 __cxa_thread_info *info = thread_info(); 1360 return info->globals.uncaughtExceptions != 0; 1361 } 1362 /** 1363 * Returns the current unexpected handler. 1364 */ 1365 unexpected_handler get_unexpected() throw() 1366 { 1367 __cxa_thread_info *info = thread_info(); 1368 if (info->unexpectedHandler) 1369 { 1370 return info->unexpectedHandler; 1371 } 1372 return unexpectedHandler; 1373 } 1374 /** 1375 * Returns the current terminate handler. 1376 */ 1377 terminate_handler get_terminate() throw() 1378 { 1379 __cxa_thread_info *info = thread_info(); 1380 if (info->terminateHandler) 1381 { 1382 return info->terminateHandler; 1383 } 1384 return terminateHandler; 1385 } 1386} 1387#ifdef __arm__ 1388extern "C" _Unwind_Exception *__cxa_get_cleanup(void) 1389{ 1390 __cxa_thread_info *info = thread_info_fast(); 1391 _Unwind_Exception *exceptionObject = info->currentCleanup; 1392 if (isCXXException(exceptionObject->exception_class)) 1393 { 1394 __cxa_exception *ex = exceptionFromPointer(exceptionObject); 1395 ex->cleanupCount--; 1396 if (ex->cleanupCount == 0) 1397 { 1398 info->currentCleanup = ex->nextCleanup; 1399 ex->nextCleanup = 0; 1400 } 1401 } 1402 else 1403 { 1404 info->currentCleanup = 0; 1405 } 1406 return exceptionObject; 1407} 1408 1409asm ( 1410".pushsection .text.__cxa_end_cleanup \n" 1411".global __cxa_end_cleanup \n" 1412".type __cxa_end_cleanup, \"function\" \n" 1413"__cxa_end_cleanup: \n" 1414" push {r1, r2, r3, r4} \n" 1415" bl __cxa_get_cleanup \n" 1416" push {r1, r2, r3, r4} \n" 1417" b _Unwind_Resume \n" 1418" bl abort \n" 1419".popsection \n" 1420); 1421#endif
| 503} 504 505static char *alloc_or_die(size_t size) 506{ 507 char *buffer = (char*)calloc(1, size); 508 509 // If calloc() doesn't want to give us any memory, try using an emergency 510 // buffer. 511 if (0 == buffer) 512 { 513 buffer = emergency_malloc(size); 514 // This is only reached if the allocation is greater than 1KB, and 515 // anyone throwing objects that big really should know better. 516 if (0 == buffer) 517 { 518 fprintf(stderr, "Out of memory attempting to allocate exception\n"); 519 std::terminate(); 520 } 521 } 522 return buffer; 523} 524static void free_exception(char *e) 525{ 526 // If this allocation is within the address range of the emergency buffer, 527 // don't call free() because it was not allocated with malloc() 528 if ((e > emergency_buffer) && 529 (e < (emergency_buffer + sizeof(emergency_buffer)))) 530 { 531 emergency_malloc_free(e); 532 } 533 else 534 { 535 free(e); 536 } 537} 538 539/** 540 * Allocates an exception structure. Returns a pointer to the space that can 541 * be used to store an object of thrown_size bytes. This function will use an 542 * emergency buffer if malloc() fails, and may block if there are no such 543 * buffers available. 544 */ 545extern "C" void *__cxa_allocate_exception(size_t thrown_size) 546{ 547 size_t size = thrown_size + sizeof(__cxa_exception); 548 char *buffer = alloc_or_die(size); 549 return buffer+sizeof(__cxa_exception); 550} 551 552extern "C" void *__cxa_allocate_dependent_exception(void) 553{ 554 size_t size = sizeof(__cxa_dependent_exception); 555 char *buffer = alloc_or_die(size); 556 return buffer+sizeof(__cxa_dependent_exception); 557} 558 559/** 560 * __cxa_free_exception() is called when an exception was thrown in between 561 * calling __cxa_allocate_exception() and actually throwing the exception. 562 * This happens when the object's copy constructor throws an exception. 563 * 564 * In this implementation, it is also called by __cxa_end_catch() and during 565 * thread cleanup. 566 */ 567extern "C" void __cxa_free_exception(void *thrown_exception) 568{ 569 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 570 // Free the object that was thrown, calling its destructor 571 if (0 != ex->exceptionDestructor) 572 { 573 try 574 { 575 ex->exceptionDestructor(thrown_exception); 576 } 577 catch(...) 578 { 579 // FIXME: Check that this is really what the spec says to do. 580 std::terminate(); 581 } 582 } 583 584 free_exception((char*)ex); 585} 586 587static void releaseException(__cxa_exception *exception) 588{ 589 if (isDependentException(exception->unwindHeader.exception_class)) 590 { 591 __cxa_free_dependent_exception(exception+1); 592 return; 593 } 594 if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0) 595 { 596 // __cxa_free_exception() expects to be passed the thrown object, 597 // which immediately follows the exception, not the exception 598 // itself 599 __cxa_free_exception(exception+1); 600 } 601} 602 603void __cxa_free_dependent_exception(void *thrown_exception) 604{ 605 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)thrown_exception) - 1; 606 assert(isDependentException(ex->unwindHeader.exception_class)); 607 if (ex->primaryException) 608 { 609 releaseException(realExceptionFromException((__cxa_exception*)ex)); 610 } 611 free_exception((char*)ex); 612} 613 614/** 615 * Callback function used with _Unwind_Backtrace(). 616 * 617 * Prints a stack trace. Used only for debugging help. 618 * 619 * Note: As of FreeBSD 8.1, dladd() still doesn't work properly, so this only 620 * correctly prints function names from public, relocatable, symbols. 621 */ 622static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c) 623{ 624 Dl_info myinfo; 625 int mylookup = 626 dladdr((void*)(uintptr_t)__cxa_current_exception_type, &myinfo); 627 void *ip = (void*)_Unwind_GetIP(context); 628 Dl_info info; 629 if (dladdr(ip, &info) != 0) 630 { 631 if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0) 632 { 633 printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname); 634 } 635 } 636 return _URC_CONTINUE_UNWIND; 637} 638 639/** 640 * Report a failure that occurred when attempting to throw an exception. 641 * 642 * If the failure happened by falling off the end of the stack without finding 643 * a handler, prints a back trace before aborting. 644 */ 645static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception) 646{ 647 switch (err) 648 { 649 default: break; 650 case _URC_FATAL_PHASE1_ERROR: 651 fprintf(stderr, "Fatal error during phase 1 unwinding\n"); 652 break; 653#ifndef __arm__ 654 case _URC_FATAL_PHASE2_ERROR: 655 fprintf(stderr, "Fatal error during phase 2 unwinding\n"); 656 break; 657#endif 658 case _URC_END_OF_STACK: 659 fprintf(stderr, "Terminating due to uncaught exception %p", 660 (void*)thrown_exception); 661 thrown_exception = realExceptionFromException(thrown_exception); 662 static const __class_type_info *e_ti = 663 static_cast<const __class_type_info*>(&typeid(std::exception)); 664 const __class_type_info *throw_ti = 665 dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType); 666 if (throw_ti) 667 { 668 std::exception *e = 669 (std::exception*)e_ti->cast_to((void*)(thrown_exception+1), 670 throw_ti); 671 if (e) 672 { 673 fprintf(stderr, " '%s'", e->what()); 674 } 675 } 676 677 size_t bufferSize = 128; 678 char *demangled = (char*)malloc(bufferSize); 679 const char *mangled = thrown_exception->exceptionType->name(); 680 int status; 681 demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status); 682 fprintf(stderr, " of type %s\n", 683 status == 0 ? (const char*)demangled : mangled); 684 if (status == 0) { free(demangled); } 685 // Print a back trace if no handler is found. 686 // TODO: Make this optional 687 _Unwind_Backtrace(trace, 0); 688 break; 689 } 690 std::terminate(); 691} 692 693static void throw_exception(__cxa_exception *ex) 694{ 695 __cxa_thread_info *info = thread_info(); 696 ex->unexpectedHandler = info->unexpectedHandler; 697 if (0 == ex->unexpectedHandler) 698 { 699 ex->unexpectedHandler = unexpectedHandler; 700 } 701 ex->terminateHandler = info->terminateHandler; 702 if (0 == ex->terminateHandler) 703 { 704 ex->terminateHandler = terminateHandler; 705 } 706 info->globals.uncaughtExceptions++; 707 708 _Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader); 709 // The _Unwind_RaiseException() function should not return, it should 710 // unwind the stack past this function. If it does return, then something 711 // has gone wrong. 712 report_failure(err, ex); 713} 714 715 716/** 717 * ABI function for throwing an exception. Takes the object to be thrown (the 718 * pointer returned by __cxa_allocate_exception()), the type info for the 719 * pointee, and the destructor (if there is one) as arguments. 720 */ 721extern "C" void __cxa_throw(void *thrown_exception, 722 std::type_info *tinfo, 723 void(*dest)(void*)) 724{ 725 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 726 727 ex->referenceCount = 1; 728 ex->exceptionType = tinfo; 729 730 ex->exceptionDestructor = dest; 731 732 ex->unwindHeader.exception_class = exception_class; 733 ex->unwindHeader.exception_cleanup = exception_cleanup; 734 735 throw_exception(ex); 736} 737 738extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception) 739{ 740 if (NULL == thrown_exception) { return; } 741 742 __cxa_exception *original = exceptionFromPointer(thrown_exception); 743 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)__cxa_allocate_dependent_exception())-1; 744 745 ex->primaryException = thrown_exception; 746 __cxa_increment_exception_refcount(thrown_exception); 747 748 ex->exceptionType = original->exceptionType; 749 ex->unwindHeader.exception_class = dependent_exception_class; 750 ex->unwindHeader.exception_cleanup = dependent_exception_cleanup; 751 752 throw_exception((__cxa_exception*)ex); 753} 754 755extern "C" void *__cxa_current_primary_exception(void) 756{ 757 __cxa_eh_globals* globals = __cxa_get_globals(); 758 __cxa_exception *ex = globals->caughtExceptions; 759 760 if (0 == ex) { return NULL; } 761 ex = realExceptionFromException(ex); 762 __sync_fetch_and_add(&ex->referenceCount, 1); 763 return ex + 1; 764} 765 766extern "C" void __cxa_increment_exception_refcount(void* thrown_exception) 767{ 768 if (NULL == thrown_exception) { return; } 769 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 770 if (isDependentException(ex->unwindHeader.exception_class)) { return; } 771 __sync_fetch_and_add(&ex->referenceCount, 1); 772} 773extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception) 774{ 775 if (NULL == thrown_exception) { return; } 776 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; 777 releaseException(ex); 778} 779 780/** 781 * ABI function. Rethrows the current exception. Does not remove the 782 * exception from the stack or decrement its handler count - the compiler is 783 * expected to set the landing pad for this function to the end of the catch 784 * block, and then call _Unwind_Resume() to continue unwinding once 785 * __cxa_end_catch() has been called and any cleanup code has been run. 786 */ 787extern "C" void __cxa_rethrow() 788{ 789 __cxa_eh_globals *globals = __cxa_get_globals(); 790 // Note: We don't remove this from the caught list here, because 791 // __cxa_end_catch will be called when we unwind out of the try block. We 792 // could probably make this faster by providing an alternative rethrow 793 // function and ensuring that all cleanup code is run before calling it, so 794 // we can skip the top stack frame when unwinding. 795 __cxa_exception *ex = globals->caughtExceptions; 796 797 if (0 == ex) 798 { 799 fprintf(stderr, 800 "Attempting to rethrow an exception that doesn't exist!\n"); 801 std::terminate(); 802 } 803 804 assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!"); 805 806 // ex->handlerCount will be decremented in __cxa_end_catch in enclosing 807 // catch block 808 809 // Make handler count negative. This will tell __cxa_end_catch that 810 // exception was rethrown and exception object should not be destroyed 811 // when handler count become zero 812 ex->handlerCount = -ex->handlerCount; 813 814 // Continue unwinding the stack with this exception. This should unwind to 815 // the place in the caller where __cxa_end_catch() is called. The caller 816 // will then run cleanup code and bounce the exception back with 817 // _Unwind_Resume(). 818 _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader); 819 report_failure(err, ex); 820} 821 822/** 823 * Returns the type_info object corresponding to the filter. 824 */ 825static std::type_info *get_type_info_entry(_Unwind_Context *context, 826 dwarf_eh_lsda *lsda, 827 int filter) 828{ 829 // Get the address of the record in the table. 830 dw_eh_ptr_t record = lsda->type_table - 831 dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter; 832 //record -= 4; 833 dw_eh_ptr_t start = record; 834 // Read the value, but it's probably an indirect reference... 835 int64_t offset = read_value(lsda->type_table_encoding, &record); 836 837 // (If the entry is 0, don't try to dereference it. That would be bad.) 838 if (offset == 0) { return 0; } 839 840 // ...so we need to resolve it 841 return (std::type_info*)resolve_indirect_value(context, 842 lsda->type_table_encoding, offset, start); 843} 844 845 846 847/** 848 * Checks the type signature found in a handler against the type of the thrown 849 * object. If ex is 0 then it is assumed to be a foreign exception and only 850 * matches cleanups. 851 */ 852static bool check_type_signature(__cxa_exception *ex, 853 const std::type_info *type, 854 void *&adjustedPtr) 855{ 856 // TODO: For compatibility with the GNU implementation, we should move this 857 // out into a __do_catch() virtual function in std::type_info 858 void *exception_ptr = (void*)(ex+1); 859 const std::type_info *ex_type = ex->exceptionType; 860 861 const __pointer_type_info *ptr_type = 862 dynamic_cast<const __pointer_type_info*>(ex_type); 863 if (0 != ptr_type) 864 { 865 exception_ptr = *(void**)exception_ptr; 866 } 867 // Always match a catchall, even with a foreign exception 868 // 869 // Note: A 0 here is a catchall, not a cleanup, so we return true to 870 // indicate that we found a catch. 871 // 872 // TODO: Provide a class for matching against foreign exceptions. This is 873 // already done in libobjc2, allowing C++ exceptions to be boxed as 874 // Objective-C objects. We should do something similar, allowing foreign 875 // exceptions to be wrapped in a C++ exception and delivered. 876 if (0 == type) 877 { 878 if (ex) 879 { 880 adjustedPtr = exception_ptr; 881 } 882 return true; 883 } 884 885 if (0 == ex) { return false; } 886 887 const __pointer_type_info *target_ptr_type = 888 dynamic_cast<const __pointer_type_info*>(type); 889 890 if (0 != ptr_type && 0 != target_ptr_type) 891 { 892 if (ptr_type->__flags & ~target_ptr_type->__flags) 893 { 894 // Handler pointer is less qualified 895 return false; 896 } 897 898 // Special case for void* handler. 899 if(*target_ptr_type->__pointee == typeid(void)) 900 { 901 adjustedPtr = exception_ptr; 902 return true; 903 } 904 905 ex_type = ptr_type->__pointee; 906 type = target_ptr_type->__pointee; 907 } 908 909 // If the types are the same, no casting is needed. 910 if (*type == *ex_type) 911 { 912 adjustedPtr = exception_ptr; 913 return true; 914 } 915 916 const __class_type_info *cls_type = 917 dynamic_cast<const __class_type_info*>(ex_type); 918 const __class_type_info *target_cls_type = 919 dynamic_cast<const __class_type_info*>(type); 920 921 if (0 != cls_type && 922 0 != target_cls_type && 923 cls_type->can_cast_to(target_cls_type)) 924 { 925 adjustedPtr = cls_type->cast_to(exception_ptr, target_cls_type); 926 return true; 927 } 928 return false; 929} 930/** 931 * Checks whether the exception matches the type specifiers in this action 932 * record. If the exception only matches cleanups, then this returns false. 933 * If it matches a catch (including a catchall) then it returns true. 934 * 935 * The selector argument is used to return the selector that is passed in the 936 * second exception register when installing the context. 937 */ 938static handler_type check_action_record(_Unwind_Context *context, 939 dwarf_eh_lsda *lsda, 940 dw_eh_ptr_t action_record, 941 __cxa_exception *ex, 942 unsigned long *selector, 943 void *&adjustedPtr) 944{ 945 if (!action_record) { return handler_cleanup; } 946 handler_type found = handler_none; 947 while (action_record) 948 { 949 int filter = read_sleb128(&action_record); 950 dw_eh_ptr_t action_record_offset_base = action_record; 951 int displacement = read_sleb128(&action_record); 952 action_record = displacement ? 953 action_record_offset_base + displacement : 0; 954 // We only check handler types for C++ exceptions - foreign exceptions 955 // are only allowed for cleanup. 956 if (filter > 0 && 0 != ex) 957 { 958 std::type_info *handler_type = get_type_info_entry(context, lsda, filter); 959 if (check_type_signature(ex, handler_type, adjustedPtr)) 960 { 961 *selector = filter; 962 return handler_catch; 963 } 964 } 965 else if (filter < 0 && 0 != ex) 966 { 967 bool matched = false; 968 *selector = filter; 969#ifdef __arm__ 970 filter++; 971 std::type_info *handler_type = get_type_info_entry(context, lsda, filter--); 972 while (handler_type) 973 { 974 if (check_type_signature(ex, handler_type, adjustedPtr)) 975 { 976 matched = true; 977 break; 978 } 979 handler_type = get_type_info_entry(context, lsda, filter--); 980 } 981#else 982 unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1); 983 while (*type_index) 984 { 985 std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++)); 986 // If the exception spec matches a permitted throw type for 987 // this function, don't report a handler - we are allowed to 988 // propagate this exception out. 989 if (check_type_signature(ex, handler_type, adjustedPtr)) 990 { 991 matched = true; 992 break; 993 } 994 } 995#endif 996 if (matched) { continue; } 997 // If we don't find an allowed exception spec, we need to install 998 // the context for this action. The landing pad will then call the 999 // unexpected exception function. Treat this as a catch 1000 return handler_catch; 1001 } 1002 else if (filter == 0) 1003 { 1004 *selector = filter; 1005 found = handler_cleanup; 1006 } 1007 } 1008 return found; 1009} 1010 1011static void pushCleanupException(_Unwind_Exception *exceptionObject, 1012 __cxa_exception *ex) 1013{ 1014#ifdef __arm__ 1015 __cxa_thread_info *info = thread_info_fast(); 1016 if (ex) 1017 { 1018 ex->cleanupCount++; 1019 if (ex->cleanupCount > 1) 1020 { 1021 assert(exceptionObject == info->currentCleanup); 1022 return; 1023 } 1024 ex->nextCleanup = info->currentCleanup; 1025 } 1026 info->currentCleanup = exceptionObject; 1027#endif 1028} 1029 1030/** 1031 * The exception personality function. This is referenced in the unwinding 1032 * DWARF metadata and is called by the unwind library for each C++ stack frame 1033 * containing catch or cleanup code. 1034 */ 1035extern "C" 1036BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) 1037 // This personality function is for version 1 of the ABI. If you use it 1038 // with a future version of the ABI, it won't know what to do, so it 1039 // reports a fatal error and give up before it breaks anything. 1040 if (1 != version) 1041 { 1042 return _URC_FATAL_PHASE1_ERROR; 1043 } 1044 __cxa_exception *ex = 0; 1045 __cxa_exception *realEx = 0; 1046 1047 // If this exception is throw by something else then we can't make any 1048 // assumptions about its layout beyond the fields declared in 1049 // _Unwind_Exception. 1050 bool foreignException = !isCXXException(exceptionClass); 1051 1052 // If this isn't a foreign exception, then we have a C++ exception structure 1053 if (!foreignException) 1054 { 1055 ex = exceptionFromPointer(exceptionObject); 1056 realEx = realExceptionFromException(ex); 1057 } 1058 1059 unsigned char *lsda_addr = 1060 (unsigned char*)_Unwind_GetLanguageSpecificData(context); 1061 1062 // No LSDA implies no landing pads - try the next frame 1063 if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); } 1064 1065 // These two variables define how the exception will be handled. 1066 dwarf_eh_action action = {0}; 1067 unsigned long selector = 0; 1068 1069 // During the search phase, we do a complete lookup. If we return 1070 // _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with 1071 // a _UA_HANDLER_FRAME action, telling us to install the handler frame. If 1072 // we return _URC_CONTINUE_UNWIND, we may be called again later with a 1073 // _UA_CLEANUP_PHASE action for this frame. 1074 // 1075 // The point of the two-stage unwind allows us to entirely avoid any stack 1076 // unwinding if there is no handler. If there are just cleanups found, 1077 // then we can just panic call an abort function. 1078 // 1079 // Matching a handler is much more expensive than matching a cleanup, 1080 // because we don't need to bother doing type comparisons (or looking at 1081 // the type table at all) for a cleanup. This means that there is no need 1082 // to cache the result of finding a cleanup, because it's (quite) quick to 1083 // look it up again from the action table. 1084 if (actions & _UA_SEARCH_PHASE) 1085 { 1086 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr); 1087 1088 if (!dwarf_eh_find_callsite(context, &lsda, &action)) 1089 { 1090 // EH range not found. This happens if exception is thrown and not 1091 // caught inside a cleanup (destructor). We should call 1092 // terminate() in this case. The catchTemp (landing pad) field of 1093 // exception object will contain null when personality function is 1094 // called with _UA_HANDLER_FRAME action for phase 2 unwinding. 1095 return _URC_HANDLER_FOUND; 1096 } 1097 1098 handler_type found_handler = check_action_record(context, &lsda, 1099 action.action_record, realEx, &selector, ex->adjustedPtr); 1100 // If there's no action record, we've only found a cleanup, so keep 1101 // searching for something real 1102 if (found_handler == handler_catch) 1103 { 1104 // Cache the results for the phase 2 unwind, if we found a handler 1105 // and this is not a foreign exception. 1106 if (ex) 1107 { 1108 saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad); 1109 ex->languageSpecificData = (const char*)lsda_addr; 1110 ex->actionRecord = (const char*)action.action_record; 1111 // ex->adjustedPtr is set when finding the action record. 1112 } 1113 return _URC_HANDLER_FOUND; 1114 } 1115 return continueUnwinding(exceptionObject, context); 1116 } 1117 1118 1119 // If this is a foreign exception, we didn't have anywhere to cache the 1120 // lookup stuff, so we need to do it again. If this is either a forced 1121 // unwind, a foreign exception, or a cleanup, then we just install the 1122 // context for a cleanup. 1123 if (!(actions & _UA_HANDLER_FRAME)) 1124 { 1125 // cleanup 1126 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr); 1127 dwarf_eh_find_callsite(context, &lsda, &action); 1128 if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); } 1129 handler_type found_handler = check_action_record(context, &lsda, 1130 action.action_record, realEx, &selector, ex->adjustedPtr); 1131 // Ignore handlers this time. 1132 if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); } 1133 pushCleanupException(exceptionObject, ex); 1134 } 1135 else if (foreignException) 1136 { 1137 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr); 1138 dwarf_eh_find_callsite(context, &lsda, &action); 1139 check_action_record(context, &lsda, action.action_record, realEx, 1140 &selector, ex->adjustedPtr); 1141 } 1142 else if (ex->catchTemp == 0) 1143 { 1144 // Uncaught exception in cleanup, calling terminate 1145 std::terminate(); 1146 } 1147 else 1148 { 1149 // Restore the saved info if we saved some last time. 1150 loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad); 1151 ex->catchTemp = 0; 1152 ex->handlerSwitchValue = 0; 1153 } 1154 1155 1156 _Unwind_SetIP(context, (unsigned long)action.landing_pad); 1157 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0), 1158 (unsigned long)exceptionObject); 1159 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector); 1160 1161 return _URC_INSTALL_CONTEXT; 1162} 1163 1164/** 1165 * ABI function called when entering a catch statement. The argument is the 1166 * pointer passed out of the personality function. This is always the start of 1167 * the _Unwind_Exception object. The return value for this function is the 1168 * pointer to the caught exception, which is either the adjusted pointer (for 1169 * C++ exceptions) of the unadjusted pointer (for foreign exceptions). 1170 */ 1171#if __GNUC__ > 3 && __GNUC_MINOR__ > 2 1172extern "C" void *__cxa_begin_catch(void *e) throw() 1173#else 1174extern "C" void *__cxa_begin_catch(void *e) 1175#endif 1176{ 1177 // Decrement the uncaught exceptions count 1178 __cxa_eh_globals *globals = __cxa_get_globals(); 1179 globals->uncaughtExceptions--; 1180 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)e; 1181 1182 if (isCXXException(exceptionObject->exception_class)) 1183 { 1184 __cxa_exception *ex = exceptionFromPointer(exceptionObject); 1185 1186 if (ex->handlerCount == 0) 1187 { 1188 // Add this to the front of the list of exceptions being handled 1189 // and increment its handler count so that it won't be deleted 1190 // prematurely. 1191 ex->nextException = globals->caughtExceptions; 1192 globals->caughtExceptions = ex; 1193 } 1194 1195 if (ex->handlerCount < 0) 1196 { 1197 // Rethrown exception is catched before end of catch block. 1198 // Clear the rethrow flag (make value positive) - we are allowed 1199 // to delete this exception at the end of the catch block, as long 1200 // as it isn't thrown again later. 1201 1202 // Code pattern: 1203 // 1204 // try { 1205 // throw x; 1206 // } 1207 // catch() { 1208 // try { 1209 // throw; 1210 // } 1211 // catch() { 1212 // __cxa_begin_catch() <- we are here 1213 // } 1214 // } 1215 ex->handlerCount = -ex->handlerCount + 1; 1216 } 1217 else 1218 { 1219 ex->handlerCount++; 1220 } 1221 1222 return ex->adjustedPtr; 1223 } 1224 // exceptionObject is the pointer to the _Unwind_Exception within the 1225 // __cxa_exception. The throw object is after this 1226 return ((char*)exceptionObject + sizeof(_Unwind_Exception)); 1227} 1228 1229 1230 1231/** 1232 * ABI function called when exiting a catch block. This will free the current 1233 * exception if it is no longer referenced in other catch blocks. 1234 */ 1235extern "C" void __cxa_end_catch() 1236{ 1237 // We can call the fast version here because the slow version is called in 1238 // __cxa_throw(), which must have been called before we end a catch block 1239 __cxa_eh_globals *globals = __cxa_get_globals_fast(); 1240 __cxa_exception *ex = globals->caughtExceptions; 1241 1242 assert(0 != ex && "Ending catch when no exception is on the stack!"); 1243 1244 bool deleteException = true; 1245 1246 if (ex->handlerCount < 0) 1247 { 1248 // exception was rethrown. Exception should not be deleted even if 1249 // handlerCount become zero. 1250 // Code pattern: 1251 // try { 1252 // throw x; 1253 // } 1254 // catch() { 1255 // { 1256 // throw; 1257 // } 1258 // cleanup { 1259 // __cxa_end_catch(); <- we are here 1260 // } 1261 // } 1262 // 1263 1264 ex->handlerCount++; 1265 deleteException = false; 1266 } 1267 else 1268 { 1269 ex->handlerCount--; 1270 } 1271 1272 if (ex->handlerCount == 0) 1273 { 1274 globals->caughtExceptions = ex->nextException; 1275 if (deleteException) 1276 { 1277 releaseException(ex); 1278 } 1279 } 1280} 1281 1282/** 1283 * ABI function. Returns the type of the current exception. 1284 */ 1285extern "C" std::type_info *__cxa_current_exception_type() 1286{ 1287 __cxa_eh_globals *globals = __cxa_get_globals(); 1288 __cxa_exception *ex = globals->caughtExceptions; 1289 return ex ? ex->exceptionType : 0; 1290} 1291 1292/** 1293 * ABI function, called when an exception specification is violated. 1294 * 1295 * This function does not return. 1296 */ 1297extern "C" void __cxa_call_unexpected(void*exception) 1298{ 1299 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)exception; 1300 if (exceptionObject->exception_class == exception_class) 1301 { 1302 __cxa_exception *ex = exceptionFromPointer(exceptionObject); 1303 if (ex->unexpectedHandler) 1304 { 1305 ex->unexpectedHandler(); 1306 // Should not be reached. 1307 abort(); 1308 } 1309 } 1310 std::unexpected(); 1311 // Should not be reached. 1312 abort(); 1313} 1314 1315/** 1316 * ABI function, returns the adjusted pointer to the exception object. 1317 */ 1318extern "C" void *__cxa_get_exception_ptr(void *exceptionObject) 1319{ 1320 return exceptionFromPointer(exceptionObject)->adjustedPtr; 1321} 1322 1323/** 1324 * As an extension, we provide the ability for the unexpected and terminate 1325 * handlers to be thread-local. We default to the standards-compliant 1326 * behaviour where they are global. 1327 */ 1328static bool thread_local_handlers = false; 1329 1330 1331namespace pathscale 1332{ 1333 /** 1334 * Sets whether unexpected and terminate handlers should be thread-local. 1335 */ 1336 void set_use_thread_local_handlers(bool flag) throw() 1337 { 1338 thread_local_handlers = flag; 1339 } 1340 /** 1341 * Sets a thread-local unexpected handler. 1342 */ 1343 unexpected_handler set_unexpected(unexpected_handler f) throw() 1344 { 1345 static __cxa_thread_info *info = thread_info(); 1346 unexpected_handler old = info->unexpectedHandler; 1347 info->unexpectedHandler = f; 1348 return old; 1349 } 1350 /** 1351 * Sets a thread-local terminate handler. 1352 */ 1353 terminate_handler set_terminate(terminate_handler f) throw() 1354 { 1355 static __cxa_thread_info *info = thread_info(); 1356 terminate_handler old = info->terminateHandler; 1357 info->terminateHandler = f; 1358 return old; 1359 } 1360} 1361 1362namespace std 1363{ 1364 /** 1365 * Sets the function that will be called when an exception specification is 1366 * violated. 1367 */ 1368 unexpected_handler set_unexpected(unexpected_handler f) throw() 1369 { 1370 if (thread_local_handlers) { return pathscale::set_unexpected(f); } 1371 1372 return __sync_lock_test_and_set(&unexpectedHandler, f); 1373 } 1374 /** 1375 * Sets the function that is called to terminate the program. 1376 */ 1377 terminate_handler set_terminate(terminate_handler f) throw() 1378 { 1379 if (thread_local_handlers) { return pathscale::set_terminate(f); } 1380 return __sync_lock_test_and_set(&terminateHandler, f); 1381 } 1382 /** 1383 * Terminates the program, calling a custom terminate implementation if 1384 * required. 1385 */ 1386 void terminate() 1387 { 1388 static __cxa_thread_info *info = thread_info_fast(); 1389 if (0 != info && 0 != info->terminateHandler) 1390 { 1391 info->terminateHandler(); 1392 // Should not be reached - a terminate handler is not expected to 1393 // return. 1394 abort(); 1395 } 1396 terminateHandler(); 1397 } 1398 /** 1399 * Called when an unexpected exception is encountered (i.e. an exception 1400 * violates an exception specification). This calls abort() unless a 1401 * custom handler has been set.. 1402 */ 1403 void unexpected() 1404 { 1405 static __cxa_thread_info *info = thread_info_fast(); 1406 if (0 != info && 0 != info->unexpectedHandler) 1407 { 1408 info->unexpectedHandler(); 1409 // Should not be reached - a terminate handler is not expected to 1410 // return. 1411 abort(); 1412 } 1413 unexpectedHandler(); 1414 } 1415 /** 1416 * Returns whether there are any exceptions currently being thrown that 1417 * have not been caught. This can occur inside a nested catch statement. 1418 */ 1419 bool uncaught_exception() throw() 1420 { 1421 __cxa_thread_info *info = thread_info(); 1422 return info->globals.uncaughtExceptions != 0; 1423 } 1424 /** 1425 * Returns the current unexpected handler. 1426 */ 1427 unexpected_handler get_unexpected() throw() 1428 { 1429 __cxa_thread_info *info = thread_info(); 1430 if (info->unexpectedHandler) 1431 { 1432 return info->unexpectedHandler; 1433 } 1434 return unexpectedHandler; 1435 } 1436 /** 1437 * Returns the current terminate handler. 1438 */ 1439 terminate_handler get_terminate() throw() 1440 { 1441 __cxa_thread_info *info = thread_info(); 1442 if (info->terminateHandler) 1443 { 1444 return info->terminateHandler; 1445 } 1446 return terminateHandler; 1447 } 1448} 1449#ifdef __arm__ 1450extern "C" _Unwind_Exception *__cxa_get_cleanup(void) 1451{ 1452 __cxa_thread_info *info = thread_info_fast(); 1453 _Unwind_Exception *exceptionObject = info->currentCleanup; 1454 if (isCXXException(exceptionObject->exception_class)) 1455 { 1456 __cxa_exception *ex = exceptionFromPointer(exceptionObject); 1457 ex->cleanupCount--; 1458 if (ex->cleanupCount == 0) 1459 { 1460 info->currentCleanup = ex->nextCleanup; 1461 ex->nextCleanup = 0; 1462 } 1463 } 1464 else 1465 { 1466 info->currentCleanup = 0; 1467 } 1468 return exceptionObject; 1469} 1470 1471asm ( 1472".pushsection .text.__cxa_end_cleanup \n" 1473".global __cxa_end_cleanup \n" 1474".type __cxa_end_cleanup, \"function\" \n" 1475"__cxa_end_cleanup: \n" 1476" push {r1, r2, r3, r4} \n" 1477" bl __cxa_get_cleanup \n" 1478" push {r1, r2, r3, r4} \n" 1479" b _Unwind_Resume \n" 1480" bl abort \n" 1481".popsection \n" 1482); 1483#endif
|