//===------------------------- cxa_exception.cpp --------------------------===//1//2// The LLVM Compiler Infrastructure3//4// This file is dual licensed under the MIT and the University of Illinois Open5// Source Licenses. See LICENSE.TXT for details.6//7//8// This file implements the "Exception Handling APIs"9// http://mentorembedded.github.io/cxx-abi/abi-eh.html10//11//===----------------------------------------------------------------------===//1213#include "cxxabi.h"1415#include <exception> // for std::terminate16#include <cstring> // for memset17#include "cxa_exception.hpp"18#include "cxa_handlers.hpp"19#include "fallback_malloc.h"20#include "include/atomic_support.h"2122#if __has_feature(address_sanitizer)23extern "C" void __asan_handle_no_return(void);24#endif2526// +---------------------------+-----------------------------+---------------+27// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |28// +---------------------------+-----------------------------+---------------+29// ^30// |31// +-------------------------------------------------------+32// |33// +---------------------------+-----------------------------+34// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |35// +---------------------------+-----------------------------+3637namespace __cxxabiv1 {3839// Utility routines40static41inline42__cxa_exception*43cxa_exception_from_thrown_object(void* thrown_object)44{45return static_cast<__cxa_exception*>(thrown_object) - 1;46}4748// Note: This is never called when exception_header is masquerading as a49// __cxa_dependent_exception.50static51inline52void*53thrown_object_from_cxa_exception(__cxa_exception* exception_header)54{55return static_cast<void*>(exception_header + 1);56}5758// Get the exception object from the unwind pointer.59// Relies on the structure layout, where the unwind pointer is right in60// front of the user's exception object61static62inline63__cxa_exception*64cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)65{66return cxa_exception_from_thrown_object(unwind_exception + 1 );67}6869// Round s up to next multiple of a.70static inline71size_t aligned_allocation_size(size_t s, size_t a) {72return (s + a - 1) & ~(a - 1);73}7475static inline76size_t cxa_exception_size_from_exception_thrown_size(size_t size) {77return aligned_allocation_size(size + sizeof (__cxa_exception),78alignof(__cxa_exception));79}8081void __setExceptionClass(_Unwind_Exception* unwind_exception, uint64_t newValue) {82::memcpy(&unwind_exception->exception_class, &newValue, sizeof(newValue));83}848586static void setOurExceptionClass(_Unwind_Exception* unwind_exception) {87__setExceptionClass(unwind_exception, kOurExceptionClass);88}8990static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {91__setExceptionClass(unwind_exception, kOurDependentExceptionClass);92}9394// Is it one of ours?95uint64_t __getExceptionClass(const _Unwind_Exception* unwind_exception) {96// On x86 and some ARM unwinders, unwind_exception->exception_class is97// a uint64_t. On other ARM unwinders, it is a char[8]98// See: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0038b/IHI0038B_ehabi.pdf99// So we just copy it into a uint64_t to be sure.100uint64_t exClass;101::memcpy(&exClass, &unwind_exception->exception_class, sizeof(exClass));102return exClass;103}104105bool __isOurExceptionClass(const _Unwind_Exception* unwind_exception) {106return (__getExceptionClass(unwind_exception) & get_vendor_and_language) ==107(kOurExceptionClass & get_vendor_and_language);108}109110static bool isDependentException(_Unwind_Exception* unwind_exception) {111return (__getExceptionClass(unwind_exception) & 0xFF) == 0x01;112}113114// This does not need to be atomic115static inline int incrementHandlerCount(__cxa_exception *exception) {116return ++exception->handlerCount;117}118119// This does not need to be atomic120static inline int decrementHandlerCount(__cxa_exception *exception) {121return --exception->handlerCount;122}123124/*125If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler126stored in exc is called. Otherwise the exceptionDestructor stored in127exc is called, and then the memory for the exception is deallocated.128129This is never called for a __cxa_dependent_exception.130*/131static132void133exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)134{135__cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);136if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)137std::__terminate(exception_header->terminateHandler);138// Just in case there exists a dependent exception that is pointing to this,139// check the reference count and only destroy this if that count goes to zero.140__cxa_decrement_exception_refcount(unwind_exception + 1);141}142143static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {144// Section 2.5.3 says:145// * For purposes of this ABI, several things are considered exception handlers:146// ** A terminate() call due to a throw.147// and148// * Upon entry, Following initialization of the catch parameter,149// a handler must call:150// * void *__cxa_begin_catch(void *exceptionObject );151(void) __cxa_begin_catch(&exception_header->unwindHeader);152std::__terminate(exception_header->terminateHandler);153}154155// Return the offset of the __cxa_exception header from the start of the156// allocated buffer. If __cxa_exception's alignment is smaller than the maximum157// useful alignment for the target machine, padding has to be inserted before158// the header to ensure the thrown object that follows the header is159// sufficiently aligned. This happens if _Unwind_exception isn't double-word160// aligned (on Darwin, for example).161static size_t get_cxa_exception_offset() {162struct S {163} __attribute__((aligned));164165// Compute the maximum alignment for the target machine.166constexpr size_t alignment = std::alignment_of<S>::value;167constexpr size_t excp_size = sizeof(__cxa_exception);168constexpr size_t aligned_size =169(excp_size + alignment - 1) / alignment * alignment;170constexpr size_t offset = aligned_size - excp_size;171static_assert((offset == 0 ||172std::alignment_of<_Unwind_Exception>::value < alignment),173"offset is non-zero only if _Unwind_Exception isn't aligned");174return offset;175}176177extern "C" {178179// Allocate a __cxa_exception object, and zero-fill it.180// Reserve "thrown_size" bytes on the end for the user's exception181// object. Zero-fill the object. If memory can't be allocated, call182// std::terminate. Return a pointer to the memory to be used for the183// user's exception object.184void *__cxa_allocate_exception(size_t thrown_size) throw() {185size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);186187// Allocate extra space before the __cxa_exception header to ensure the188// start of the thrown object is sufficiently aligned.189size_t header_offset = get_cxa_exception_offset();190char *raw_buffer =191(char *)__aligned_malloc_with_fallback(header_offset + actual_size);192if (NULL == raw_buffer)193std::terminate();194__cxa_exception *exception_header =195static_cast<__cxa_exception *>((void *)(raw_buffer + header_offset));196std::memset(exception_header, 0, actual_size);197return thrown_object_from_cxa_exception(exception_header);198}199200201// Free a __cxa_exception object allocated with __cxa_allocate_exception.202void __cxa_free_exception(void *thrown_object) throw() {203// Compute the size of the padding before the header.204size_t header_offset = get_cxa_exception_offset();205char *raw_buffer =206((char *)cxa_exception_from_thrown_object(thrown_object)) - header_offset;207__aligned_free_with_fallback((void *)raw_buffer);208}209210211// This function shall allocate a __cxa_dependent_exception and212// return a pointer to it. (Really to the object, not past its' end).213// Otherwise, it will work like __cxa_allocate_exception.214void * __cxa_allocate_dependent_exception () {215size_t actual_size = sizeof(__cxa_dependent_exception);216void *ptr = __aligned_malloc_with_fallback(actual_size);217if (NULL == ptr)218std::terminate();219std::memset(ptr, 0, actual_size);220return ptr;221}222223224// This function shall free a dependent_exception.225// It does not affect the reference count of the primary exception.226void __cxa_free_dependent_exception (void * dependent_exception) {227__aligned_free_with_fallback(dependent_exception);228}229230231// 2.4.3 Throwing the Exception Object232/*233After constructing the exception object with the throw argument value,234the generated code calls the __cxa_throw runtime library routine. This235routine never returns.236237The __cxa_throw routine will do the following:238239* Obtain the __cxa_exception header from the thrown exception object address,240which can be computed as follows:241__cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);242* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.243* Save the tinfo and dest arguments in the __cxa_exception header.244* Set the exception_class field in the unwind header. This is a 64-bit value245representing the ASCII string "XXXXC++\0", where "XXXX" is a246vendor-dependent string. That is, for implementations conforming to this247ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".248* Increment the uncaught_exception flag.249* Call _Unwind_RaiseException in the system unwind library, Its argument is the250pointer to the thrown exception, which __cxa_throw itself received as an argument.251__Unwind_RaiseException begins the process of stack unwinding, described252in Section 2.5. In special cases, such as an inability to find a253handler, _Unwind_RaiseException may return. In that case, __cxa_throw254will call terminate, assuming that there was no handler for the255exception.256*/257void258__cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {259__cxa_eh_globals *globals = __cxa_get_globals();260__cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);261262exception_header->unexpectedHandler = std::get_unexpected();263exception_header->terminateHandler = std::get_terminate();264exception_header->exceptionType = tinfo;265exception_header->exceptionDestructor = dest;266setOurExceptionClass(&exception_header->unwindHeader);267exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.268globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local269270exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;271272#if __has_feature(address_sanitizer)273// Inform the ASan runtime that now might be a good time to clean stuff up.274__asan_handle_no_return();275#endif276277#ifdef __USING_SJLJ_EXCEPTIONS__278_Unwind_SjLj_RaiseException(&exception_header->unwindHeader);279#else280_Unwind_RaiseException(&exception_header->unwindHeader);281#endif282// This only happens when there is no handler, or some unexpected unwinding283// error happens.284failed_throw(exception_header);285}286287288// 2.5.3 Exception Handlers289/*290The adjusted pointer is computed by the personality routine during phase 1291and saved in the exception header (either __cxa_exception or292__cxa_dependent_exception).293294Requires: exception is native295*/296void *__cxa_get_exception_ptr(void *unwind_exception) throw() {297#if defined(_LIBCXXABI_ARM_EHABI)298return reinterpret_cast<void*>(299static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]);300#else301return cxa_exception_from_exception_unwind_exception(302static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr;303#endif304}305306#if defined(_LIBCXXABI_ARM_EHABI)307/*308The routine to be called before the cleanup. This will save __cxa_exception in309__cxa_eh_globals, so that __cxa_end_cleanup() can recover later.310*/311bool __cxa_begin_cleanup(void *unwind_arg) throw() {312_Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);313__cxa_eh_globals* globals = __cxa_get_globals();314__cxa_exception* exception_header =315cxa_exception_from_exception_unwind_exception(unwind_exception);316317if (__isOurExceptionClass(unwind_exception))318{319if (0 == exception_header->propagationCount)320{321exception_header->nextPropagatingException = globals->propagatingExceptions;322globals->propagatingExceptions = exception_header;323}324++exception_header->propagationCount;325}326else327{328// If the propagatingExceptions stack is not empty, since we can't329// chain the foreign exception, terminate it.330if (NULL != globals->propagatingExceptions)331std::terminate();332globals->propagatingExceptions = exception_header;333}334return true;335}336337/*338The routine to be called after the cleanup has been performed. It will get the339propagating __cxa_exception from __cxa_eh_globals, and continue the stack340unwinding with _Unwind_Resume.341342According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any343register, thus we have to write this function in assembly so that we can save344{r1, r2, r3}. We don't have to save r0 because it is the return value and the345first argument to _Unwind_Resume(). In addition, we are saving r4 in order to346align the stack to 16 bytes, even though it is a callee-save register.347*/348__attribute__((used)) static _Unwind_Exception *349__cxa_end_cleanup_impl()350{351__cxa_eh_globals* globals = __cxa_get_globals();352__cxa_exception* exception_header = globals->propagatingExceptions;353if (NULL == exception_header)354{355// It seems that __cxa_begin_cleanup() is not called properly.356// We have no choice but terminate the program now.357std::terminate();358}359360if (__isOurExceptionClass(&exception_header->unwindHeader))361{362--exception_header->propagationCount;363if (0 == exception_header->propagationCount)364{365globals->propagatingExceptions = exception_header->nextPropagatingException;366exception_header->nextPropagatingException = NULL;367}368}369else370{371globals->propagatingExceptions = NULL;372}373return &exception_header->unwindHeader;374}375376asm (377" .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"378" .globl __cxa_end_cleanup\n"379" .type __cxa_end_cleanup,%function\n"380"__cxa_end_cleanup:\n"381" push {r1, r2, r3, r4}\n"382" bl __cxa_end_cleanup_impl\n"383" pop {r1, r2, r3, r4}\n"384" bl _Unwind_Resume\n"385" bl abort\n"386" .popsection"387);388#endif // defined(_LIBCXXABI_ARM_EHABI)389390/*391This routine can catch foreign or native exceptions. If native, the exception392can be a primary or dependent variety. This routine may remain blissfully393ignorant of whether the native exception is primary or dependent.394395If the exception is native:396* Increment's the exception's handler count.397* Push the exception on the stack of currently-caught exceptions if it is not398already there (from a rethrow).399* Decrements the uncaught_exception count.400* Returns the adjusted pointer to the exception object, which is stored in401the __cxa_exception by the personality routine.402403If the exception is foreign, this means it did not originate from one of throw404routines. The foreign exception does not necessarily have a __cxa_exception405header. However we can catch it here with a catch (...), or with a call406to terminate or unexpected during unwinding.407* Do not try to increment the exception's handler count, we don't know where408it is.409* Push the exception on the stack of currently-caught exceptions only if the410stack is empty. The foreign exception has no way to link to the current411top of stack. If the stack is not empty, call terminate. Even with an412empty stack, this is hacked in by pushing a pointer to an imaginary413__cxa_exception block in front of the foreign exception. It would be better414if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it415doesn't. It has a stack of __cxa_exception (which has a next* in it).416* Do not decrement the uncaught_exception count because we didn't increment it417in __cxa_throw (or one of our rethrow functions).418* If we haven't terminated, assume the exception object is just past the419_Unwind_Exception and return a pointer to that.420*/421void*422__cxa_begin_catch(void* unwind_arg) throw()423{424_Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);425bool native_exception = __isOurExceptionClass(unwind_exception);426__cxa_eh_globals* globals = __cxa_get_globals();427// exception_header is a hackish offset from a foreign exception, but it428// works as long as we're careful not to try to access any __cxa_exception429// parts.430__cxa_exception* exception_header =431cxa_exception_from_exception_unwind_exception432(433static_cast<_Unwind_Exception*>(unwind_exception)434);435if (native_exception)436{437// Increment the handler count, removing the flag about being rethrown438exception_header->handlerCount = exception_header->handlerCount < 0 ?439-exception_header->handlerCount + 1 : exception_header->handlerCount + 1;440// place the exception on the top of the stack if it's not already441// there by a previous rethrow442if (exception_header != globals->caughtExceptions)443{444exception_header->nextException = globals->caughtExceptions;445globals->caughtExceptions = exception_header;446}447globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local448#if defined(_LIBCXXABI_ARM_EHABI)449return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);450#else451return exception_header->adjustedPtr;452#endif453}454// Else this is a foreign exception455// If the caughtExceptions stack is not empty, terminate456if (globals->caughtExceptions != 0)457std::terminate();458// Push the foreign exception on to the stack459globals->caughtExceptions = exception_header;460return unwind_exception + 1;461}462463464/*465Upon exit for any reason, a handler must call:466void __cxa_end_catch ();467468This routine can be called for either a native or foreign exception.469For a native exception:470* Locates the most recently caught exception and decrements its handler count.471* Removes the exception from the caught exception stack, if the handler count goes to zero.472* If the handler count goes down to zero, and the exception was not re-thrown473by throw, it locates the primary exception (which may be the same as the one474it's handling) and decrements its reference count. If that reference count475goes to zero, the function destroys the exception. In any case, if the current476exception is a dependent exception, it destroys that.477478For a foreign exception:479* If it has been rethrown, there is nothing to do.480* Otherwise delete the exception and pop the catch stack to empty.481*/482void __cxa_end_catch() {483static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),484"sizeof(__cxa_exception) must be equal to "485"sizeof(__cxa_dependent_exception)");486static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==487__builtin_offsetof(__cxa_dependent_exception,488primaryException),489"the layout of __cxa_exception must match the layout of "490"__cxa_dependent_exception");491static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==492__builtin_offsetof(__cxa_dependent_exception, handlerCount),493"the layout of __cxa_exception must match the layout of "494"__cxa_dependent_exception");495__cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch496__cxa_exception* exception_header = globals->caughtExceptions;497// If we've rethrown a foreign exception, then globals->caughtExceptions498// will have been made an empty stack by __cxa_rethrow() and there is499// nothing more to be done. Do nothing!500if (NULL != exception_header)501{502bool native_exception = __isOurExceptionClass(&exception_header->unwindHeader);503if (native_exception)504{505// This is a native exception506if (exception_header->handlerCount < 0)507{508// The exception has been rethrown by __cxa_rethrow, so don't delete it509if (0 == incrementHandlerCount(exception_header))510{511// Remove from the chain of uncaught exceptions512globals->caughtExceptions = exception_header->nextException;513// but don't destroy514}515// Keep handlerCount negative in case there are nested catch's516// that need to be told that this exception is rethrown. Don't517// erase this rethrow flag until the exception is recaught.518}519else520{521// The native exception has not been rethrown522if (0 == decrementHandlerCount(exception_header))523{524// Remove from the chain of uncaught exceptions525globals->caughtExceptions = exception_header->nextException;526// Destroy this exception, being careful to distinguish527// between dependent and primary exceptions528if (isDependentException(&exception_header->unwindHeader))529{530// Reset exception_header to primaryException and deallocate the dependent exception531__cxa_dependent_exception* dep_exception_header =532reinterpret_cast<__cxa_dependent_exception*>(exception_header);533exception_header =534cxa_exception_from_thrown_object(dep_exception_header->primaryException);535__cxa_free_dependent_exception(dep_exception_header);536}537// Destroy the primary exception only if its referenceCount goes to 0538// (this decrement must be atomic)539__cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));540}541}542}543else544{545// The foreign exception has not been rethrown. Pop the stack546// and delete it. If there are nested catch's and they try547// to touch a foreign exception in any way, that is undefined548// behavior. They likely can't since the only way to catch549// a foreign exception is with catch (...)!550_Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);551globals->caughtExceptions = 0;552}553}554}555556// Note: exception_header may be masquerading as a __cxa_dependent_exception557// and that's ok. exceptionType is there too.558// However watch out for foreign exceptions. Return null for them.559std::type_info *__cxa_current_exception_type() {560// get the current exception561__cxa_eh_globals *globals = __cxa_get_globals_fast();562if (NULL == globals)563return NULL; // If there have never been any exceptions, there are none now.564__cxa_exception *exception_header = globals->caughtExceptions;565if (NULL == exception_header)566return NULL; // No current exception567if (!__isOurExceptionClass(&exception_header->unwindHeader))568return NULL;569return exception_header->exceptionType;570}571572// 2.5.4 Rethrowing Exceptions573/* This routine can rethrow native or foreign exceptions.574If the exception is native:575* marks the exception object on top of the caughtExceptions stack576(in an implementation-defined way) as being rethrown.577* If the caughtExceptions stack is empty, it calls terminate()578(see [C++FDIS] [except.throw], 15.1.8).579* It then calls _Unwind_RaiseException which should not return580(terminate if it does).581Note: exception_header may be masquerading as a __cxa_dependent_exception582and that's ok.583*/584void __cxa_rethrow() {585__cxa_eh_globals* globals = __cxa_get_globals();586__cxa_exception* exception_header = globals->caughtExceptions;587if (NULL == exception_header)588std::terminate(); // throw; called outside of a exception handler589bool native_exception = __isOurExceptionClass(&exception_header->unwindHeader);590if (native_exception)591{592// Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)593exception_header->handlerCount = -exception_header->handlerCount;594globals->uncaughtExceptions += 1;595// __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary596}597else // this is a foreign exception598{599// The only way to communicate to __cxa_end_catch that we've rethrown600// a foreign exception, so don't delete us, is to pop the stack here601// which must be empty afterwards. Then __cxa_end_catch will do602// nothing603globals->caughtExceptions = 0;604}605#ifdef __USING_SJLJ_EXCEPTIONS__606_Unwind_SjLj_RaiseException(&exception_header->unwindHeader);607#else608_Unwind_RaiseException(&exception_header->unwindHeader);609#endif610611// If we get here, some kind of unwinding error has occurred.612// There is some weird code generation bug happening with613// Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)614// If we call failed_throw here. Turns up with -O2 or higher, and -Os.615__cxa_begin_catch(&exception_header->unwindHeader);616if (native_exception)617std::__terminate(exception_header->terminateHandler);618// Foreign exception: can't get exception_header->terminateHandler619std::terminate();620}621622/*623If thrown_object is not null, atomically increment the referenceCount field624of the __cxa_exception header associated with the thrown object referred to625by thrown_object.626627Requires: If thrown_object is not NULL, it is a native exception.628*/629void630__cxa_increment_exception_refcount(void *thrown_object) throw() {631if (thrown_object != NULL )632{633__cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);634std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(1));635}636}637638/*639If thrown_object is not null, atomically decrement the referenceCount field640of the __cxa_exception header associated with the thrown object referred to641by thrown_object. If the referenceCount drops to zero, destroy and642deallocate the exception.643644Requires: If thrown_object is not NULL, it is a native exception.645*/646_LIBCXXABI_NO_CFI647void __cxa_decrement_exception_refcount(void *thrown_object) throw() {648if (thrown_object != NULL )649{650__cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);651if (std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(-1)) == 0)652{653if (NULL != exception_header->exceptionDestructor)654exception_header->exceptionDestructor(thrown_object);655__cxa_free_exception(thrown_object);656}657}658}659660/*661Returns a pointer to the thrown object (if any) at the top of the662caughtExceptions stack. Atomically increment the exception's referenceCount.663If there is no such thrown object or if the thrown object is foreign,664returns null.665666We can use __cxa_get_globals_fast here to get the globals because if there have667been no exceptions thrown, ever, on this thread, we can return NULL without668the need to allocate the exception-handling globals.669*/670void *__cxa_current_primary_exception() throw() {671// get the current exception672__cxa_eh_globals* globals = __cxa_get_globals_fast();673if (NULL == globals)674return NULL; // If there are no globals, there is no exception675__cxa_exception* exception_header = globals->caughtExceptions;676if (NULL == exception_header)677return NULL; // No current exception678if (!__isOurExceptionClass(&exception_header->unwindHeader))679return NULL; // Can't capture a foreign exception (no way to refcount it)680if (isDependentException(&exception_header->unwindHeader)) {681__cxa_dependent_exception* dep_exception_header =682reinterpret_cast<__cxa_dependent_exception*>(exception_header);683exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);684}685void* thrown_object = thrown_object_from_cxa_exception(exception_header);686__cxa_increment_exception_refcount(thrown_object);687return thrown_object;688}689690/*691If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler692stored in exc is called. Otherwise the referenceCount stored in the693primary exception is decremented, destroying the primary if necessary.694Finally the dependent exception is destroyed.695*/696static697void698dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)699{700__cxa_dependent_exception* dep_exception_header =701reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;702if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)703std::__terminate(dep_exception_header->terminateHandler);704__cxa_decrement_exception_refcount(dep_exception_header->primaryException);705__cxa_free_dependent_exception(dep_exception_header);706}707708/*709If thrown_object is not null, allocate, initialize and throw a dependent710exception.711*/712void713__cxa_rethrow_primary_exception(void* thrown_object)714{715if ( thrown_object != NULL )716{717// thrown_object guaranteed to be native because718// __cxa_current_primary_exception returns NULL for foreign exceptions719__cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);720__cxa_dependent_exception* dep_exception_header =721static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());722dep_exception_header->primaryException = thrown_object;723__cxa_increment_exception_refcount(thrown_object);724dep_exception_header->exceptionType = exception_header->exceptionType;725dep_exception_header->unexpectedHandler = std::get_unexpected();726dep_exception_header->terminateHandler = std::get_terminate();727setDependentExceptionClass(&dep_exception_header->unwindHeader);728__cxa_get_globals()->uncaughtExceptions += 1;729dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;730#ifdef __USING_SJLJ_EXCEPTIONS__731_Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);732#else733_Unwind_RaiseException(&dep_exception_header->unwindHeader);734#endif735// Some sort of unwinding error. Note that terminate is a handler.736__cxa_begin_catch(&dep_exception_header->unwindHeader);737}738// If we return client will call terminate()739}740741bool742__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }743744unsigned int745__cxa_uncaught_exceptions() throw()746{747// This does not report foreign exceptions in flight748__cxa_eh_globals* globals = __cxa_get_globals_fast();749if (globals == 0)750return 0;751return globals->uncaughtExceptions;752}753754void __cxa_call_terminate(void* unwind_arg) throw() {755__cxa_begin_catch(unwind_arg);756std::terminate();757}758759} // extern "C"760761} // abi762763764