123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdint.h>
- #include <new>
- #include "pthread_compat.h"
- #include "typeinfo.h"
- #include "dwarf_eh.h"
- #include "atomic.h"
- #include "cxxabi.h"
- #include "thread_data.h"
- #include "se_cdefs.h"
- SGX_ACCESS_VERSION(tstdcxx, 1)
- using namespace ABI_NAMESPACE;
- static void saveLandingPad(struct _Unwind_Context *context,
- struct _Unwind_Exception *ucb,
- struct __cxa_exception *ex,
- int selector,
- dw_eh_ptr_t landingPad)
- {
- #ifdef __arm__
-
- ucb->barrier_cache.sp = _Unwind_GetGR(context, 13);
- ucb->barrier_cache.bitpattern[1] = (uint32_t)selector;
- ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad;
- #endif
-
-
- if (ex)
- {
- ex->handlerSwitchValue = selector;
- ex->catchTemp = landingPad;
- }
- }
- static int loadLandingPad(struct _Unwind_Context *context,
- struct _Unwind_Exception *ucb,
- struct __cxa_exception *ex,
- unsigned long *selector,
- dw_eh_ptr_t *landingPad)
- {
- #ifdef __arm__
- *selector = ucb->barrier_cache.bitpattern[1];
- *landingPad = (dw_eh_ptr_t)ucb->barrier_cache.bitpattern[3];
- return 1;
- #else
- if (ex)
- {
- *selector = ex->handlerSwitchValue;
- *landingPad = (dw_eh_ptr_t)ex->catchTemp;
- return 0;
- }
- return 0;
- #endif
- }
- static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex,
- struct _Unwind_Context *context)
- {
- #ifdef __arm__
- if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; }
- #endif
- return _URC_CONTINUE_UNWIND;
- }
- extern "C" void __cxa_free_exception(void *thrown_exception);
- extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
- extern "C" void* __dynamic_cast(const void *sub,
- const __class_type_info *src,
- const __class_type_info *dst,
- ptrdiff_t src2dst_offset);
- typedef enum
- {
-
- handler_none,
-
- handler_cleanup,
-
- handler_catch
- } handler_type;
- struct __cxa_thread_info
- {
-
- terminate_handler terminateHandler;
-
- unexpected_handler unexpectedHandler;
-
- int emergencyBuffersHeld;
-
- _Unwind_Exception *currentCleanup;
-
- __cxa_eh_globals globals;
- };
- struct __cxa_dependent_exception
- {
- #if __LP64__
- void *primaryException;
- #endif
- std::type_info *exceptionType;
- void (*exceptionDestructor) (void *);
- unexpected_handler unexpectedHandler;
- terminate_handler terminateHandler;
- __cxa_exception *nextException;
- int handlerCount;
- #ifdef __arm__
- _Unwind_Exception *nextCleanup;
- int cleanupCount;
- #endif
- int handlerSwitchValue;
- const char *actionRecord;
- const char *languageSpecificData;
- void *catchTemp;
- void *adjustedPtr;
- #if !__LP64__
- void *primaryException;
- #endif
- _Unwind_Exception unwindHeader;
- };
- namespace std
- {
- void unexpected();
- #if 0
- class exception
- {
- public:
- virtual ~exception() throw();
- virtual const char* what() const throw();
- };
- #endif
- }
- extern "C" std::type_info *__cxa_current_exception_type();
- static const uint64_t exception_class =
- EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
- static const uint64_t dependent_exception_class =
- EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01');
- static const uint32_t abi_exception_class =
- GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
- static bool isCXXException(uint64_t cls)
- {
- return (cls == exception_class) || (cls == dependent_exception_class);
- }
- static bool isDependentException(uint64_t cls)
- {
- return cls == dependent_exception_class;
- }
- static __cxa_exception *exceptionFromPointer(void *ex)
- {
- return (__cxa_exception*)((char*)ex -
- offsetof(struct __cxa_exception, unwindHeader));
- }
- static __cxa_exception *realExceptionFromException(__cxa_exception *ex)
- {
- if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; }
- return ((__cxa_exception*)(((__cxa_dependent_exception*)ex)->primaryException))-1;
- }
- namespace std
- {
-
-
- void terminate(void);
- }
- using namespace ABI_NAMESPACE;
- static terminate_handler terminateHandler = abort;
- static unexpected_handler unexpectedHandler = std::terminate;
- static void exception_cleanup(_Unwind_Reason_Code reason,
- struct _Unwind_Exception *ex)
- {
- __cxa_free_exception((void*)ex);
- }
- static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
- struct _Unwind_Exception *ex)
- {
- __cxa_free_dependent_exception((void*)ex);
- }
- static void free_exception_list(__cxa_exception *ex)
- {
- if (0 != ex->nextException)
- {
- free_exception_list(ex->nextException);
- }
-
-
- __cxa_free_exception(ex+1);
- }
- static __cxa_thread_info* __get_thread_info_ptr()
- {
- thread_data_t* td = get_thread_data();
- return (__cxa_thread_info *)td->cxx_thread_info;
- }
- static __cxa_thread_info *thread_info()
- {
- return __get_thread_info_ptr();
- }
- static __cxa_thread_info *thread_info_fast()
- {
- return __get_thread_info_ptr();
- }
- extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
- {
- return &(thread_info()->globals);
- }
- extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
- {
- return &(thread_info_fast()->globals);
- }
- static char emergency_buffer[16384];
- static bool buffer_allocated[16];
- static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
- static volatile bool buffer_available = true;
- static char *emergency_malloc(size_t size)
- {
- if (size > 1024) { return 0; }
- __cxa_thread_info *info = thread_info();
-
- if (info->emergencyBuffersHeld > 3) { return 0; }
- pthread_mutex_lock(&emergency_malloc_lock);
- int buffer = -1;
- while (buffer < 0)
- {
-
-
-
-
- void *m = calloc(1, size);
- if (0 != m)
- {
- pthread_mutex_unlock(&emergency_malloc_lock);
- return (char*)m;
- }
- for (int i=0 ; i<16 ; i++)
- {
- if (!buffer_allocated[i])
- {
- buffer = i;
- buffer_allocated[i] = true;
- break;
- }
- }
-
- if (buffer < 0)
- {
-
- buffer_available = false;
- pthread_mutex_unlock(&emergency_malloc_lock);
- while (!buffer_available) continue;
-
- pthread_mutex_lock(&emergency_malloc_lock);
- }
- }
- pthread_mutex_unlock(&emergency_malloc_lock);
- info->emergencyBuffersHeld++;
- return emergency_buffer + (1024 * buffer);
- }
- static void emergency_malloc_free(char *ptr)
- {
- int buffer = -1;
-
- for (int i=0 ; i<16 ; i++)
- {
- if (ptr == (void*)(emergency_buffer + (1024 * i)))
- {
- buffer = i;
- break;
- }
- }
- assert(buffer >= 0 &&
- "Trying to free something that is not an emergency buffer!");
-
-
-
- memset((void*)ptr, 0, 1024);
- pthread_mutex_lock(&emergency_malloc_lock);
-
-
-
- if (buffer != -1)
- {
- buffer_allocated[buffer] = false;
- buffer_available = true;
- }
- pthread_mutex_unlock(&emergency_malloc_lock);
- }
- static char *alloc_or_die(size_t size)
- {
- char *buffer = (char*)calloc(1, size);
-
-
- if (0 == buffer)
- {
- buffer = emergency_malloc(size);
-
-
- if (0 == buffer)
- {
-
- std::terminate();
- }
- }
- return buffer;
- }
- static void free_exception(char *e)
- {
-
-
- if ((e >= emergency_buffer) &&
- (e < (emergency_buffer + sizeof(emergency_buffer))))
- {
- emergency_malloc_free(e);
- }
- else
- {
- free(e);
- }
- }
- extern "C" void *__cxa_allocate_exception(size_t thrown_size)
- {
- size_t size = thrown_size + sizeof(__cxa_exception);
- char *buffer = alloc_or_die(size);
- return buffer+sizeof(__cxa_exception);
- }
- extern "C" void *__cxa_allocate_dependent_exception(void)
- {
- size_t size = sizeof(__cxa_dependent_exception);
- char *buffer = alloc_or_die(size);
- return buffer+sizeof(__cxa_dependent_exception);
- }
- extern "C" void __cxa_free_exception(void *thrown_exception)
- {
- __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
-
- if (0 != ex->exceptionDestructor)
- {
- try
- {
- ex->exceptionDestructor(thrown_exception);
- }
- catch(...)
- {
-
- std::terminate();
- }
- }
- free_exception((char*)ex);
- }
- static void releaseException(__cxa_exception *exception)
- {
- if (isDependentException(exception->unwindHeader.exception_class))
- {
- __cxa_free_dependent_exception(exception+1);
- return;
- }
- if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
- {
-
-
-
- __cxa_free_exception(exception+1);
- }
- }
- void __cxa_free_dependent_exception(void *thrown_exception)
- {
- __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)thrown_exception) - 1;
- assert(isDependentException(ex->unwindHeader.exception_class));
- if (ex->primaryException)
- {
- releaseException(realExceptionFromException((__cxa_exception*)ex));
- }
- free_exception((char*)ex);
- }
- static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
- {
- #if 0
- Dl_info myinfo;
- int mylookup =
- dladdr((void*)(uintptr_t)__cxa_current_exception_type, &myinfo);
- void *ip = (void*)_Unwind_GetIP(context);
- Dl_info info;
- if (dladdr(ip, &info) != 0)
- {
- if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
- {
- printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
- }
- }
- #endif
- return _URC_CONTINUE_UNWIND;
- }
- static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
- {
- switch (err)
- {
- default: break;
- case _URC_FATAL_PHASE1_ERROR:
-
- break;
- #ifndef __arm__
- case _URC_FATAL_PHASE2_ERROR:
-
- break;
- #endif
- case _URC_END_OF_STACK:
-
-
- thrown_exception = realExceptionFromException(thrown_exception);
- static const __class_type_info *e_ti =
- static_cast<const __class_type_info*>(&typeid(std::exception));
- const __class_type_info *throw_ti =
- dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType);
- if (throw_ti)
- {
- std::exception *e =
- (std::exception*)e_ti->cast_to((void*)(thrown_exception+1),
- throw_ti);
- if (e)
- {
-
- }
- }
- #if 0
- size_t bufferSize = 128;
- char *demangled = (char*)malloc(bufferSize);
- const char *mangled = thrown_exception->exceptionType->name();
- int status;
- demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
-
-
- if (status == 0) { free(demangled); }
- #endif
-
-
- _Unwind_Backtrace(trace, 0);
- break;
- }
- std::terminate();
- }
- static void throw_exception(__cxa_exception *ex)
- {
- __cxa_thread_info *info = thread_info();
- ex->unexpectedHandler = info->unexpectedHandler;
- if (0 == ex->unexpectedHandler)
- {
- ex->unexpectedHandler = unexpectedHandler;
- }
- ex->terminateHandler = info->terminateHandler;
- if (0 == ex->terminateHandler)
- {
- ex->terminateHandler = terminateHandler;
- }
- info->globals.uncaughtExceptions++;
- _Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
-
-
-
- report_failure(err, ex);
- }
- extern "C" void __cxa_throw(void *thrown_exception,
- std::type_info *tinfo,
- void(*dest)(void*))
- {
- __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
- ex->referenceCount = 1;
- ex->exceptionType = tinfo;
-
- ex->exceptionDestructor = dest;
-
- ex->unwindHeader.exception_class = exception_class;
- ex->unwindHeader.exception_cleanup = exception_cleanup;
- throw_exception(ex);
- }
- extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
- {
- if (NULL == thrown_exception) { return; }
- __cxa_exception *original = exceptionFromPointer(thrown_exception);
- __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)__cxa_allocate_dependent_exception())-1;
- ex->primaryException = thrown_exception;
- __cxa_increment_exception_refcount(thrown_exception);
- ex->exceptionType = original->exceptionType;
- ex->unwindHeader.exception_class = dependent_exception_class;
- ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
- throw_exception((__cxa_exception*)ex);
- }
- extern "C" void *__cxa_current_primary_exception(void)
- {
- __cxa_eh_globals* globals = __cxa_get_globals();
- __cxa_exception *ex = globals->caughtExceptions;
- if (0 == ex) { return NULL; }
- ex = realExceptionFromException(ex);
- __sync_fetch_and_add(&ex->referenceCount, 1);
- return ex + 1;
- }
- extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
- {
- if (NULL == thrown_exception) { return; }
- __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
- if (isDependentException(ex->unwindHeader.exception_class)) { return; }
- __sync_fetch_and_add(&ex->referenceCount, 1);
- }
- extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
- {
- if (NULL == thrown_exception) { return; }
- __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
- releaseException(ex);
- }
- extern "C" void __cxa_rethrow()
- {
- __cxa_eh_globals *globals = __cxa_get_globals();
-
-
-
-
-
- __cxa_exception *ex = globals->caughtExceptions;
- if (0 == ex)
- {
-
-
- std::terminate();
- }
- assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
- globals->uncaughtExceptions++;
-
-
-
-
-
-
- ex->handlerCount = -ex->handlerCount;
-
-
-
-
- _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
- report_failure(err, ex);
- }
- static std::type_info *get_type_info_entry(_Unwind_Context *context,
- dwarf_eh_lsda *lsda,
- int filter)
- {
-
- dw_eh_ptr_t record = lsda->type_table -
- dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
-
- dw_eh_ptr_t start = record;
-
- int64_t offset = read_value(lsda->type_table_encoding, &record);
-
- if (offset == 0) { return 0; }
-
- return (std::type_info*)resolve_indirect_value(context,
- lsda->type_table_encoding, offset, start);
- }
- static bool check_type_signature(__cxa_exception *ex,
- const std::type_info *type,
- void *&adjustedPtr)
- {
- void *exception_ptr = (void*)(ex+1);
- const std::type_info *ex_type = NULL;
- if (ex)
- {
- ex_type = ex->exceptionType;
- bool is_ptr = ex_type->__is_pointer_p();
- if (is_ptr)
- {
- exception_ptr = *(void**)exception_ptr;
- }
- }
-
-
-
-
- if (0 == type)
- {
- if (ex)
- {
- adjustedPtr = exception_ptr;
- }
- return true;
- }
- if (0 == ex) { return false; }
-
- if (*type == *ex_type)
- {
- adjustedPtr = exception_ptr;
- return true;
- }
- if (type->__do_catch(ex_type, &exception_ptr, 1))
- {
- adjustedPtr = exception_ptr;
- return true;
- }
- return false;
- }
- static handler_type check_action_record(_Unwind_Context *context,
- dwarf_eh_lsda *lsda,
- dw_eh_ptr_t action_record,
- __cxa_exception *ex,
- unsigned long *selector,
- void *&adjustedPtr)
- {
- if (!action_record) { return handler_cleanup; }
- handler_type found = handler_none;
- while (action_record)
- {
- int filter = read_sleb128(&action_record);
- dw_eh_ptr_t action_record_offset_base = action_record;
- int displacement = read_sleb128(&action_record);
- action_record = displacement ?
- action_record_offset_base + displacement : 0;
-
-
- if (filter > 0 && 0 != ex)
- {
- std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
- if (check_type_signature(ex, handler_type, adjustedPtr))
- {
- *selector = filter;
- return handler_catch;
- }
- }
- else if (filter < 0 && 0 != ex)
- {
- bool matched = false;
- if (found == handler_none) *selector = filter;
- #ifdef __arm__
- filter++;
- std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
- while (handler_type)
- {
- if (check_type_signature(ex, handler_type, adjustedPtr))
- {
- matched = true;
- break;
- }
- handler_type = get_type_info_entry(context, lsda, filter--);
- }
- #else
- unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1);
- while (*type_index)
- {
- std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
-
-
-
- if (check_type_signature(ex, handler_type, adjustedPtr))
- {
- matched = true;
- break;
- }
- }
- #endif
- if (matched) { continue; }
-
-
-
- return handler_catch;
- }
- else if (filter == 0)
- {
- *selector = filter;
- found = handler_cleanup;
- }
- }
- return found;
- }
- static void pushCleanupException(_Unwind_Exception *exceptionObject,
- __cxa_exception *ex)
- {
- #ifdef __arm__
- __cxa_thread_info *info = thread_info_fast();
- if (ex)
- {
- ex->cleanupCount++;
- if (ex->cleanupCount > 1)
- {
- assert(exceptionObject == info->currentCleanup);
- return;
- }
- ex->nextCleanup = info->currentCleanup;
- }
- info->currentCleanup = exceptionObject;
- #endif
- }
- extern "C"
- BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
-
-
-
- if (1 != version)
- {
- return _URC_FATAL_PHASE1_ERROR;
- }
- __cxa_exception *ex = 0;
- __cxa_exception *realEx = 0;
-
-
-
- bool foreignException = !isCXXException(exceptionClass);
-
- if (!foreignException)
- {
- ex = exceptionFromPointer(exceptionObject);
- realEx = realExceptionFromException(ex);
- }
- unsigned char *lsda_addr =
- (unsigned char*)_Unwind_GetLanguageSpecificData(context);
-
- if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
-
- dwarf_eh_action action = {0};
- unsigned long selector = 0;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (actions & _UA_SEARCH_PHASE)
- {
- struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
- if (!dwarf_eh_find_callsite(context, &lsda, &action))
- {
-
-
-
-
-
- return _URC_HANDLER_FOUND;
- }
- handler_type found_handler = check_action_record(context, &lsda,
- action.action_record, realEx, &selector, ex->adjustedPtr);
-
-
- if (found_handler == handler_catch)
- {
-
-
- if (ex)
- {
- saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
- ex->languageSpecificData = (const char*)lsda_addr;
- ex->actionRecord = (const char*)action.action_record;
-
- }
- return _URC_HANDLER_FOUND;
- }
- return continueUnwinding(exceptionObject, context);
- }
-
-
-
-
- if (!(actions & _UA_HANDLER_FRAME))
- {
-
- struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
- dwarf_eh_find_callsite(context, &lsda, &action);
- if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
- handler_type found_handler = check_action_record(context, &lsda,
- action.action_record, realEx, &selector, ex->adjustedPtr);
-
- if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
- pushCleanupException(exceptionObject, ex);
- }
- else if (foreignException)
- {
- struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
- dwarf_eh_find_callsite(context, &lsda, &action);
- check_action_record(context, &lsda, action.action_record, realEx,
- &selector, ex->adjustedPtr);
- }
- else if (ex->catchTemp == 0)
- {
-
- std::terminate();
- }
- else
- {
-
- loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
- ex->catchTemp = 0;
- ex->handlerSwitchValue = 0;
- }
- _Unwind_SetIP(context, (unsigned long)action.landing_pad);
- _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
- (unsigned long)exceptionObject);
- _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
- return _URC_INSTALL_CONTEXT;
- }
- #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
- extern "C" void *__cxa_begin_catch(void *e) throw()
- #else
- extern "C" void *__cxa_begin_catch(void *e)
- #endif
- {
-
- __cxa_eh_globals *globals = __cxa_get_globals();
- globals->uncaughtExceptions--;
- _Unwind_Exception *exceptionObject = (_Unwind_Exception*)e;
- if (isCXXException(exceptionObject->exception_class))
- {
- __cxa_exception *ex = exceptionFromPointer(exceptionObject);
- if (ex->handlerCount == 0)
- {
-
-
-
- ex->nextException = globals->caughtExceptions;
- globals->caughtExceptions = ex;
- }
- if (ex->handlerCount < 0)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ex->handlerCount = -ex->handlerCount + 1;
- }
- else
- {
- ex->handlerCount++;
- }
-
- return ex->adjustedPtr;
- }
-
-
- return ((char*)exceptionObject + sizeof(_Unwind_Exception));
- }
- extern "C" void __cxa_end_catch()
- {
-
-
- __cxa_eh_globals *globals = __cxa_get_globals_fast();
- __cxa_exception *ex = globals->caughtExceptions;
- assert(0 != ex && "Ending catch when no exception is on the stack!");
- bool deleteException = true;
- if (ex->handlerCount < 0)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ex->handlerCount++;
- deleteException = false;
- }
- else
- {
- ex->handlerCount--;
- }
- if (ex->handlerCount == 0)
- {
- globals->caughtExceptions = ex->nextException;
- if (deleteException)
- {
- releaseException(ex);
- }
- }
- }
- extern "C" std::type_info *__cxa_current_exception_type()
- {
- __cxa_eh_globals *globals = __cxa_get_globals();
- __cxa_exception *ex = globals->caughtExceptions;
- return ex ? ex->exceptionType : 0;
- }
- extern "C" void __cxa_call_unexpected(void*exception)
- {
- _Unwind_Exception *exceptionObject = (_Unwind_Exception*)exception;
- if (exceptionObject->exception_class == exception_class)
- {
- __cxa_exception *ex = exceptionFromPointer(exceptionObject);
- if (ex->unexpectedHandler)
- {
- ex->unexpectedHandler();
-
- abort();
- }
- }
- std::unexpected();
-
- abort();
- }
- extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
- {
- return exceptionFromPointer(exceptionObject)->adjustedPtr;
- }
- static bool thread_local_handlers = false;
- namespace pathscale
- {
-
- void set_use_thread_local_handlers(bool flag) throw()
- {
- thread_local_handlers = flag;
- }
-
- unexpected_handler set_unexpected(unexpected_handler f) throw()
- {
- static __cxa_thread_info *info = thread_info();
- unexpected_handler old = info->unexpectedHandler;
- info->unexpectedHandler = f;
- return old;
- }
-
- terminate_handler set_terminate(terminate_handler f) throw()
- {
- static __cxa_thread_info *info = thread_info();
- terminate_handler old = info->terminateHandler;
- info->terminateHandler = f;
- return old;
- }
- }
- namespace std
- {
-
- unexpected_handler set_unexpected(unexpected_handler f) throw()
- {
- if (thread_local_handlers) { return pathscale::set_unexpected(f); }
- return ATOMIC_SWAP(&unexpectedHandler, f);
- }
-
- terminate_handler set_terminate(terminate_handler f) throw()
- {
- if (thread_local_handlers) { return pathscale::set_terminate(f); }
- return ATOMIC_SWAP(&terminateHandler, f);
- }
-
- void terminate()
- {
- #define force_abort(term_handler) do { \
- try { term_handler (); abort(); } \
- catch (...) { abort(); } \
- } while (0)
- static __cxa_thread_info *info = thread_info_fast();
- if (0 != info && 0 != info->terminateHandler)
- {
- force_abort(info->terminateHandler);
-
-
- }
- force_abort(terminateHandler);
- #undef force_abort
- }
-
- void unexpected()
- {
- static __cxa_thread_info *info = thread_info_fast();
- if (0 != info && 0 != info->unexpectedHandler)
- {
- info->unexpectedHandler();
-
-
- abort();
- }
- unexpectedHandler();
- }
-
- bool uncaught_exception() throw()
- {
- __cxa_thread_info *info = thread_info();
- return info->globals.uncaughtExceptions != 0;
- }
-
- unexpected_handler get_unexpected() throw()
- {
- __cxa_thread_info *info = thread_info();
- if (info->unexpectedHandler)
- {
- return info->unexpectedHandler;
- }
- return ATOMIC_LOAD(&unexpectedHandler);
- }
-
- terminate_handler get_terminate() throw()
- {
- __cxa_thread_info *info = thread_info();
- if (info->terminateHandler)
- {
- return info->terminateHandler;
- }
- return ATOMIC_LOAD(&terminateHandler);
- }
- }
- #ifdef __arm__
- extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
- {
- __cxa_thread_info *info = thread_info_fast();
- _Unwind_Exception *exceptionObject = info->currentCleanup;
- if (isCXXException(exceptionObject->exception_class))
- {
- __cxa_exception *ex = exceptionFromPointer(exceptionObject);
- ex->cleanupCount--;
- if (ex->cleanupCount == 0)
- {
- info->currentCleanup = ex->nextCleanup;
- ex->nextCleanup = 0;
- }
- }
- else
- {
- info->currentCleanup = 0;
- }
- return exceptionObject;
- }
- asm (
- ".pushsection .text.__cxa_end_cleanup \n"
- ".global __cxa_end_cleanup \n"
- ".type __cxa_end_cleanup, \"function\" \n"
- "__cxa_end_cleanup: \n"
- " push {r1, r2, r3, r4} \n"
- " bl __cxa_get_cleanup \n"
- " push {r1, r2, r3, r4} \n"
- " b _Unwind_Resume \n"
- " bl abort \n"
- ".popsection \n"
- );
- #endif
|