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