cxa_exception.cpp revision 0240685ed6756ee0eed7c76e37eb2abdc01dcf82
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://www.codesourcery.com/public/cxx-abi/abi-eh.html 11// 12//===----------------------------------------------------------------------===// 13 14#include "cxxabi.h" 15 16#include <exception> // for std::terminate 17#include <cstdlib> // for malloc, free 18#include <string> // for memset 19#include <pthread.h> 20 21#include "cxa_exception.hpp" 22#include "cxa_handlers.hpp" 23 24// +---------------------------+-----------------------------+---------------+ 25// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | 26// +---------------------------+-----------------------------+---------------+ 27// ^ 28// | 29// +-------------------------------------------------------+ 30// | 31// +---------------------------+-----------------------------+ 32// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 | 33// +---------------------------+-----------------------------+ 34 35namespace __cxxabiv1 { 36 37#pragma GCC visibility push(default) 38 39// Utility routines 40static 41inline 42__cxa_exception* 43cxa_exception_from_thrown_object(void* thrown_object) 44{ 45 return static_cast<__cxa_exception*>(thrown_object) - 1; 46} 47 48// Note: This is never called when exception_header is masquerading as a 49// __cxa_dependent_exception. 50static 51inline 52void* 53thrown_object_from_cxa_exception(__cxa_exception* exception_header) 54{ 55 return static_cast<void*>(exception_header + 1); 56} 57 58// Get the exception object from the unwind pointer. 59// Relies on the structure layout, where the unwind pointer is right in 60// front of the user's exception object 61static 62inline 63__cxa_exception* 64cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) 65{ 66 return cxa_exception_from_thrown_object(unwind_exception + 1 ); 67} 68 69static 70inline 71size_t 72cxa_exception_size_from_exception_thrown_size(size_t size) 73{ 74 return size + sizeof (__cxa_exception); 75} 76 77static void setExceptionClass(_Unwind_Exception* unwind_exception) { 78 unwind_exception->exception_class = kOurExceptionClass; 79} 80 81static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) { 82 unwind_exception->exception_class = kOurDependentExceptionClass; 83} 84 85// Is it one of ours? 86static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) { 87 return (unwind_exception->exception_class & get_vendor_and_language) == 88 (kOurExceptionClass & get_vendor_and_language); 89} 90 91static bool isDependentException(_Unwind_Exception* unwind_exception) { 92 return (unwind_exception->exception_class & 0xFF) == 0x01; 93} 94 95// This does not need to be atomic 96static inline int incrementHandlerCount(__cxa_exception *exception) { 97 return ++exception->handlerCount; 98} 99 100// This does not need to be atomic 101static inline int decrementHandlerCount(__cxa_exception *exception) { 102 return --exception->handlerCount; 103} 104 105#include "fallback_malloc.ipp" 106 107// Allocate some memory from _somewhere_ 108static void *do_malloc(size_t size) { 109 void *ptr = std::malloc(size); 110 if (NULL == ptr) // if malloc fails, fall back to emergency stash 111 ptr = fallback_malloc(size); 112 return ptr; 113} 114 115static void do_free(void *ptr) { 116 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr); 117} 118 119/* 120 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler 121 stored in exc is called. Otherwise the exceptionDestructor stored in 122 exc is called, and then the memory for the exception is deallocated. 123 124 This is never called for a __cxa_dependent_exception. 125*/ 126static 127void 128exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) 129{ 130 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception); 131 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) 132 std::__terminate(exception_header->terminateHandler); 133 // Just in case there exists a dependent exception that is pointing to this, 134 // check the reference count and only destroy this if that count goes to zero. 135 __cxa_decrement_exception_refcount(unwind_exception + 1); 136} 137 138static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) { 139// Section 2.5.3 says: 140// * For purposes of this ABI, several things are considered exception handlers: 141// ** A terminate() call due to a throw. 142// and 143// * Upon entry, Following initialization of the catch parameter, 144// a handler must call: 145// * void *__cxa_begin_catch(void *exceptionObject ); 146 (void) __cxa_begin_catch(&exception_header->unwindHeader); 147 std::__terminate(exception_header->terminateHandler); 148} 149 150extern "C" { 151 152// Allocate a __cxa_exception object, and zero-fill it. 153// Reserve "thrown_size" bytes on the end for the user's exception 154// object. Zero-fill the object. If memory can't be allocated, call 155// std::terminate. Return a pointer to the memory to be used for the 156// user's exception object. 157void * __cxa_allocate_exception (size_t thrown_size) throw() { 158 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size); 159 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size)); 160 if (NULL == exception_header) 161 std::terminate(); 162 std::memset(exception_header, 0, actual_size); 163 return thrown_object_from_cxa_exception(exception_header); 164} 165 166 167// Free a __cxa_exception object allocated with __cxa_allocate_exception. 168void __cxa_free_exception (void * thrown_object) throw() { 169 do_free(cxa_exception_from_thrown_object(thrown_object)); 170} 171 172 173// This function shall allocate a __cxa_dependent_exception and 174// return a pointer to it. (Really to the object, not past its' end). 175// Otherwise, it will work like __cxa_allocate_exception. 176void * __cxa_allocate_dependent_exception () { 177 size_t actual_size = sizeof(__cxa_dependent_exception); 178 void *ptr = do_malloc(actual_size); 179 if (NULL == ptr) 180 std::terminate(); 181 std::memset(ptr, 0, actual_size); 182 return ptr; 183} 184 185 186// This function shall free a dependent_exception. 187// It does not affect the reference count of the primary exception. 188void __cxa_free_dependent_exception (void * dependent_exception) { 189 do_free(dependent_exception); 190} 191 192 193// 2.4.3 Throwing the Exception Object 194/* 195After constructing the exception object with the throw argument value, 196the generated code calls the __cxa_throw runtime library routine. This 197routine never returns. 198 199The __cxa_throw routine will do the following: 200 201* Obtain the __cxa_exception header from the thrown exception object address, 202which can be computed as follows: 203 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1); 204* Save the current unexpected_handler and terminate_handler in the __cxa_exception header. 205* Save the tinfo and dest arguments in the __cxa_exception header. 206* Set the exception_class field in the unwind header. This is a 64-bit value 207representing the ASCII string "XXXXC++\0", where "XXXX" is a 208vendor-dependent string. That is, for implementations conforming to this 209ABI, the low-order 4 bytes of this 64-bit value will be "C++\0". 210* Increment the uncaught_exception flag. 211* Call _Unwind_RaiseException in the system unwind library, Its argument is the 212pointer to the thrown exception, which __cxa_throw itself received as an argument. 213__Unwind_RaiseException begins the process of stack unwinding, described 214in Section 2.5. In special cases, such as an inability to find a 215handler, _Unwind_RaiseException may return. In that case, __cxa_throw 216will call terminate, assuming that there was no handler for the 217exception. 218*/ 219LIBCXXABI_NORETURN 220void 221__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*)) 222{ 223 __cxa_eh_globals *globals = __cxa_get_globals(); 224 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 225 226 exception_header->unexpectedHandler = std::get_unexpected(); 227 exception_header->terminateHandler = std::get_terminate(); 228 exception_header->exceptionType = tinfo; 229 exception_header->exceptionDestructor = dest; 230 setExceptionClass(&exception_header->unwindHeader); 231 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety. 232 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local 233 234 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func; 235#if __arm__ 236 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); 237#else 238 _Unwind_RaiseException(&exception_header->unwindHeader); 239#endif 240 // This only happens when there is no handler, or some unexpected unwinding 241 // error happens. 242 failed_throw(exception_header); 243} 244 245 246// 2.5.3 Exception Handlers 247/* 248The adjusted pointer is computed by the personality routine during phase 1 249 and saved in the exception header (either __cxa_exception or 250 __cxa_dependent_exception). 251 252 Requires: exception is native 253*/ 254void* 255__cxa_get_exception_ptr(void* unwind_exception) throw() 256{ 257 return cxa_exception_from_exception_unwind_exception 258 ( 259 static_cast<_Unwind_Exception*>(unwind_exception) 260 )->adjustedPtr; 261} 262 263/* 264This routine can catch foreign or native exceptions. If native, the exception 265can be a primary or dependent variety. This routine may remain blissfully 266ignorant of whether the native exception is primary or dependent. 267 268If the exception is native: 269* Increment's the exception's handler count. 270* Push the exception on the stack of currently-caught exceptions if it is not 271 already there (from a rethrow). 272* Decrements the uncaught_exception count. 273* Returns the adjusted pointer to the exception object, which is stored in 274 the __cxa_exception by the personality routine. 275 276If the exception is foreign, this means it did not originate from one of throw 277routines. The foreign exception does not necessarily have a __cxa_exception 278header. However we can catch it here with a catch (...), or with a call 279to terminate or unexpected during unwinding. 280* Do not try to increment the exception's handler count, we don't know where 281 it is. 282* Push the exception on the stack of currently-caught exceptions only if the 283 stack is empty. The foreign exception has no way to link to the current 284 top of stack. If the stack is not empty, call terminate. Even with an 285 empty stack, this is hacked in by pushing a pointer to an imaginary 286 __cxa_exception block in front of the foreign exception. It would be better 287 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it 288 doesn't. It has a stack of __cxa_exception (which has a next* in it). 289* Do not decrement the uncaught_exception count because we didn't increment it 290 in __cxa_throw (or one of our rethrow functions). 291* If we haven't terminated, assume the exception object is just past the 292 _Unwind_Exception and return a pointer to that. 293*/ 294void* 295__cxa_begin_catch(void* unwind_arg) throw() 296{ 297 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg); 298 bool native_exception = isOurExceptionClass(unwind_exception); 299 __cxa_eh_globals* globals = __cxa_get_globals(); 300 // exception_header is a hackish offset from a foreign exception, but it 301 // works as long as we're careful not to try to access any __cxa_exception 302 // parts. 303 __cxa_exception* exception_header = 304 cxa_exception_from_exception_unwind_exception 305 ( 306 static_cast<_Unwind_Exception*>(unwind_exception) 307 ); 308 if (native_exception) 309 { 310 // Increment the handler count, removing the flag about being rethrown 311 exception_header->handlerCount = exception_header->handlerCount < 0 ? 312 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1; 313 // place the exception on the top of the stack if it's not already 314 // there by a previous rethrow 315 if (exception_header != globals->caughtExceptions) 316 { 317 exception_header->nextException = globals->caughtExceptions; 318 globals->caughtExceptions = exception_header; 319 } 320 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local 321 return exception_header->adjustedPtr; 322 } 323 // Else this is a foreign exception 324 // If the caughtExceptions stack is not empty, terminate 325 if (globals->caughtExceptions != 0) 326 std::terminate(); 327 // Push the foreign exception on to the stack 328 globals->caughtExceptions = exception_header; 329 return unwind_exception + 1; 330} 331 332 333/* 334Upon exit for any reason, a handler must call: 335 void __cxa_end_catch (); 336 337This routine can be called for either a native or foreign exception. 338For a native exception: 339* Locates the most recently caught exception and decrements its handler count. 340* Removes the exception from the caught exception stack, if the handler count goes to zero. 341* If the handler count goes down to zero, and the exception was not re-thrown 342 by throw, it locates the primary exception (which may be the same as the one 343 it's handling) and decrements its reference count. If that reference count 344 goes to zero, the function destroys the exception. In any case, if the current 345 exception is a dependent exception, it destroys that. 346 347For a foreign exception: 348* If it has been rethrown, there is nothing to do. 349* Otherwise delete the exception and pop the catch stack to empty. 350*/ 351void __cxa_end_catch() 352{ 353 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception), 354 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)"); 355 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch 356 __cxa_exception* exception_header = globals->caughtExceptions; 357 // If we've rethrown a foreign exception, then globals->caughtExceptions 358 // will have been made an empty stack by __cxa_rethrow() and there is 359 // nothing more to be done. Do nothing! 360 if (NULL != exception_header) 361 { 362 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader); 363 if (native_exception) 364 { 365 // This is a native exception 366 if (exception_header->handlerCount < 0) 367 { 368 // The exception has been rethrown by __cxa_rethrow, so don't delete it 369 if (0 == incrementHandlerCount(exception_header)) 370 { 371 // Remove from the chain of uncaught exceptions 372 globals->caughtExceptions = exception_header->nextException; 373 // but don't destroy 374 } 375 // Keep handlerCount negative in case there are nested catch's 376 // that need to be told that this exception is rethrown. Don't 377 // erase this rethrow flag until the exception is recaught. 378 } 379 else 380 { 381 // The native exception has not been rethrown 382 if (0 == decrementHandlerCount(exception_header)) 383 { 384 // Remove from the chain of uncaught exceptions 385 globals->caughtExceptions = exception_header->nextException; 386 // Destroy this exception, being careful to distinguish 387 // between dependent and primary exceptions 388 if (isDependentException(&exception_header->unwindHeader)) 389 { 390 // Reset exception_header to primaryException and deallocate the dependent exception 391 __cxa_dependent_exception* dep_exception_header = 392 reinterpret_cast<__cxa_dependent_exception*>(exception_header); 393 exception_header = 394 cxa_exception_from_thrown_object(dep_exception_header->primaryException); 395 __cxa_free_dependent_exception(dep_exception_header); 396 } 397 // Destroy the primary exception only if its referenceCount goes to 0 398 // (this decrement must be atomic) 399 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header)); 400 } 401 } 402 } 403 else 404 { 405 // The foreign exception has not been rethrown. Pop the stack 406 // and delete it. If there are nested catch's and they try 407 // to touch a foreign exception in any way, that is undefined 408 // behavior. They likely can't since the only way to catch 409 // a foreign exception is with catch (...)! 410 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader); 411 globals->caughtExceptions = 0; 412 } 413 } 414} 415 416// Note: exception_header may be masquerading as a __cxa_dependent_exception 417// and that's ok. exceptionType is there too. 418// However watch out for foreign exceptions. Return null for them. 419std::type_info * __cxa_current_exception_type() { 420// get the current exception 421 __cxa_eh_globals *globals = __cxa_get_globals_fast(); 422 if (NULL == globals) 423 return NULL; // If there have never been any exceptions, there are none now. 424 __cxa_exception *exception_header = globals->caughtExceptions; 425 if (NULL == exception_header) 426 return NULL; // No current exception 427 if (!isOurExceptionClass(&exception_header->unwindHeader)) 428 return NULL; 429 return exception_header->exceptionType; 430} 431 432// 2.5.4 Rethrowing Exceptions 433/* This routine can rethrow native or foreign exceptions. 434If the exception is native: 435* marks the exception object on top of the caughtExceptions stack 436 (in an implementation-defined way) as being rethrown. 437* If the caughtExceptions stack is empty, it calls terminate() 438 (see [C++FDIS] [except.throw], 15.1.8). 439* It then calls _Unwind_RaiseException which should not return 440 (terminate if it does). 441 Note: exception_header may be masquerading as a __cxa_dependent_exception 442 and that's ok. 443*/ 444LIBCXXABI_NORETURN 445void 446__cxa_rethrow() 447{ 448 __cxa_eh_globals* globals = __cxa_get_globals(); 449 __cxa_exception* exception_header = globals->caughtExceptions; 450 if (NULL == exception_header) 451 std::terminate(); // throw; called outside of a exception handler 452 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader); 453 if (native_exception) 454 { 455 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch) 456 exception_header->handlerCount = -exception_header->handlerCount; 457 globals->uncaughtExceptions += 1; 458 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary 459 } 460 else // this is a foreign exception 461 { 462 // The only way to communicate to __cxa_end_catch that we've rethrown 463 // a foreign exception, so don't delete us, is to pop the stack here 464 // which must be empty afterwards. Then __cxa_end_catch will do 465 // nothing 466 globals->caughtExceptions = 0; 467 } 468#if __arm__ 469 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader); 470#else 471 (void)_Unwind_RaiseException(&exception_header->unwindHeader); 472#endif 473 474 // If we get here, some kind of unwinding error has occurred. 475 // There is some weird code generation bug happening with 476 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn) 477 // If we call failed_throw here. Turns up with -O2 or higher, and -Os. 478 __cxa_begin_catch(&exception_header->unwindHeader); 479 if (native_exception) 480 std::__terminate(exception_header->terminateHandler); 481 // Foreign exception: can't get exception_header->terminateHandler 482 std::terminate(); 483} 484 485/* 486 If thrown_object is not null, atomically increment the referenceCount field 487 of the __cxa_exception header associated with the thrown object referred to 488 by thrown_object. 489 490 Requires: If thrown_object is not NULL, it is a native exception. 491*/ 492void 493__cxa_increment_exception_refcount(void* thrown_object) throw() 494{ 495 if (thrown_object != NULL ) 496 { 497 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 498 __sync_add_and_fetch(&exception_header->referenceCount, 1); 499 } 500} 501 502/* 503 If thrown_object is not null, atomically decrement the referenceCount field 504 of the __cxa_exception header associated with the thrown object referred to 505 by thrown_object. If the referenceCount drops to zero, destroy and 506 deallocate the exception. 507 508 Requires: If thrown_object is not NULL, it is a native exception. 509*/ 510void 511__cxa_decrement_exception_refcount(void* thrown_object) throw() 512{ 513 if (thrown_object != NULL ) 514 { 515 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 516 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0) 517 { 518 if (NULL != exception_header->exceptionDestructor) 519 exception_header->exceptionDestructor(thrown_object); 520 __cxa_free_exception(thrown_object); 521 } 522 } 523} 524 525/* 526 Returns a pointer to the thrown object (if any) at the top of the 527 caughtExceptions stack. Atommically increment the exception's referenceCount. 528 If there is no such thrown object or if the thrown object is foreign, 529 returns null. 530 531 We can use __cxa_get_globals_fast here to get the globals because if there have 532 been no exceptions thrown, ever, on this thread, we can return NULL without 533 the need to allocate the exception-handling globals. 534*/ 535void* 536__cxa_current_primary_exception() throw() 537{ 538// get the current exception 539 __cxa_eh_globals* globals = __cxa_get_globals_fast(); 540 if (NULL == globals) 541 return NULL; // If there are no globals, there is no exception 542 __cxa_exception* exception_header = globals->caughtExceptions; 543 if (NULL == exception_header) 544 return NULL; // No current exception 545 if (!isOurExceptionClass(&exception_header->unwindHeader)) 546 return NULL; // Can't capture a foreign exception (no way to refcount it) 547 if (isDependentException(&exception_header->unwindHeader)) { 548 __cxa_dependent_exception* dep_exception_header = 549 reinterpret_cast<__cxa_dependent_exception*>(exception_header); 550 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException); 551 } 552 void* thrown_object = thrown_object_from_cxa_exception(exception_header); 553 __cxa_increment_exception_refcount(thrown_object); 554 return thrown_object; 555} 556 557/* 558 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler 559 stored in exc is called. Otherwise the referenceCount stored in the 560 primary exception is decremented, destroying the primary if necessary. 561 Finally the dependent exception is destroyed. 562*/ 563static 564void 565dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) 566{ 567 __cxa_dependent_exception* dep_exception_header = 568 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1; 569 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) 570 std::__terminate(dep_exception_header->terminateHandler); 571 __cxa_decrement_exception_refcount(dep_exception_header->primaryException); 572 __cxa_free_dependent_exception(dep_exception_header); 573} 574 575/* 576 If thrown_object is not null, allocate, initialize and thow a dependent 577 exception. 578*/ 579void 580__cxa_rethrow_primary_exception(void* thrown_object) 581{ 582 if ( thrown_object != NULL ) 583 { 584 // thrown_object guaranteed to be native because 585 // __cxa_current_primary_exception returns NULL for foreign exceptions 586 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); 587 __cxa_dependent_exception* dep_exception_header = 588 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception()); 589 dep_exception_header->primaryException = thrown_object; 590 __cxa_increment_exception_refcount(thrown_object); 591 dep_exception_header->exceptionType = exception_header->exceptionType; 592 dep_exception_header->unexpectedHandler = std::get_unexpected(); 593 dep_exception_header->terminateHandler = std::get_terminate(); 594 setDependentExceptionClass(&dep_exception_header->unwindHeader); 595 __cxa_get_globals()->uncaughtExceptions += 1; 596 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup; 597#if __arm__ 598 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader); 599#else 600 _Unwind_RaiseException(&dep_exception_header->unwindHeader); 601#endif 602 // Some sort of unwinding error. Note that terminate is a handler. 603 __cxa_begin_catch(&dep_exception_header->unwindHeader); 604 } 605 // If we return client will call terminate() 606} 607 608bool 609__cxa_uncaught_exception() throw() 610{ 611 // This does not report foreign exceptions in flight 612 __cxa_eh_globals* globals = __cxa_get_globals_fast(); 613 if (globals == 0) 614 return false; 615 return globals->uncaughtExceptions != 0; 616} 617 618} // extern "C" 619 620#pragma GCC visibility pop 621 622} // abi 623