2 * Copyright 2010-2011 PathScale, Inc. All rights reserved.
3 * Copyright 2021 David Chisnall. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
16 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #pragma weak pthread_key_create
40 #pragma weak pthread_setspecific
41 #pragma weak pthread_getspecific
42 #pragma weak pthread_once
43 #ifdef LIBCXXRT_WEAK_LOCKS
44 #pragma weak pthread_mutex_lock
45 #define pthread_mutex_lock(mtx) do {\
46 if (pthread_mutex_lock) pthread_mutex_lock(mtx);\
48 #pragma weak pthread_mutex_unlock
49 #define pthread_mutex_unlock(mtx) do {\
50 if (pthread_mutex_unlock) pthread_mutex_unlock(mtx);\
52 #pragma weak pthread_cond_signal
53 #define pthread_cond_signal(cv) do {\
54 if (pthread_cond_signal) pthread_cond_signal(cv);\
56 #pragma weak pthread_cond_wait
57 #define pthread_cond_wait(cv, mtx) do {\
58 if (pthread_cond_wait) pthread_cond_wait(cv, mtx);\
62 using namespace ABI_NAMESPACE;
65 * Saves the result of the landing pad that we have found. For ARM, this is
66 * stored in the generic unwind structure, while on other platforms it is
67 * stored in the C++ exception.
69 static void saveLandingPad(struct _Unwind_Context *context,
70 struct _Unwind_Exception *ucb,
71 struct __cxa_exception *ex,
73 dw_eh_ptr_t landingPad)
75 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
76 // On ARM, we store the saved exception in the generic part of the structure
77 ucb->barrier_cache.sp = _Unwind_GetGR(context, 13);
78 ucb->barrier_cache.bitpattern[1] = static_cast<uint32_t>(selector);
79 ucb->barrier_cache.bitpattern[3] = reinterpret_cast<uint32_t>(landingPad);
81 // Cache the results for the phase 2 unwind, if we found a handler
82 // and this is not a foreign exception.
85 ex->handlerSwitchValue = selector;
86 ex->catchTemp = landingPad;
91 * Loads the saved landing pad. Returns 1 on success, 0 on failure.
93 static int loadLandingPad(struct _Unwind_Context *context,
94 struct _Unwind_Exception *ucb,
95 struct __cxa_exception *ex,
96 unsigned long *selector,
97 dw_eh_ptr_t *landingPad)
99 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
100 *selector = ucb->barrier_cache.bitpattern[1];
101 *landingPad = reinterpret_cast<dw_eh_ptr_t>(ucb->barrier_cache.bitpattern[3]);
106 *selector = ex->handlerSwitchValue;
107 *landingPad = reinterpret_cast<dw_eh_ptr_t>(ex->catchTemp);
114 static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex,
115 struct _Unwind_Context *context)
117 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
118 if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; }
120 return _URC_CONTINUE_UNWIND;
124 extern "C" void __cxa_free_exception(void *thrown_exception) throw();
125 extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
126 extern "C" void* __dynamic_cast(const void *sub,
127 const __class_type_info *src,
128 const __class_type_info *dst,
129 ptrdiff_t src2dst_offset);
132 * The type of a handler that has been found.
139 * A cleanup - the exception will propagate through this frame, but code
140 * must be run when this happens.
144 * A catch statement. The exception will not propagate past this frame
145 * (without an explicit rethrow).
151 * Per-thread info required by the runtime. We store a single structure
152 * pointer in thread-local storage, because this tends to be a scarce resource
153 * and it's impolite to steal all of it and not leave any for the rest of the
156 * Instances of this structure are allocated lazily - at most one per thread -
157 * and are destroyed on thread termination.
159 struct __cxa_thread_info
161 /** The termination handler for this thread. */
162 terminate_handler terminateHandler;
163 /** The unexpected exception handler for this thread. */
164 unexpected_handler unexpectedHandler;
165 #ifndef LIBCXXRT_NO_EMERGENCY_MALLOC
167 * The number of emergency buffers held by this thread. This is 0 in
168 * normal operation - the emergency buffers are only used when malloc()
169 * fails to return memory for allocating an exception. Threads are not
170 * permitted to hold more than 4 emergency buffers (as per recommendation
171 * in ABI spec [3.3.1]).
173 int emergencyBuffersHeld;
176 * The exception currently running in a cleanup.
178 _Unwind_Exception *currentCleanup;
180 * Our state with respect to foreign exceptions. Usually none, set to
181 * caught if we have just caught an exception and rethrown if we are
189 } foreign_exception_state;
191 * The public part of this structure, accessible from outside of this
194 __cxa_eh_globals globals;
197 * Dependent exception. This
199 struct __cxa_dependent_exception
203 void *primaryException;
205 std::type_info *exceptionType;
206 void (*exceptionDestructor) (void *);
207 unexpected_handler unexpectedHandler;
208 terminate_handler terminateHandler;
209 __cxa_exception *nextException;
211 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
212 _Unwind_Exception *nextCleanup;
215 int handlerSwitchValue;
216 const char *actionRecord;
217 const char *languageSpecificData;
221 void *primaryException;
223 _Unwind_Exception unwindHeader;
225 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
226 "__cxa_exception and __cxa_dependent_exception should have the same size");
227 static_assert(offsetof(__cxa_exception, referenceCount) ==
228 offsetof(__cxa_dependent_exception, primaryException),
229 "referenceCount and primaryException should have the same offset");
230 static_assert(offsetof(__cxa_exception, unwindHeader) ==
231 offsetof(__cxa_dependent_exception, unwindHeader),
232 "unwindHeader fields should have the same offset");
233 static_assert(offsetof(__cxa_dependent_exception, unwindHeader) ==
234 offsetof(__cxa_dependent_exception, adjustedPtr) + 8,
235 "there should be no padding before unwindHeader");
244 virtual ~exception() throw();
245 virtual const char* what() const throw();
251 * Class of exceptions to distinguish between this and other exception types.
253 * The first four characters are the vendor ID. Currently, we use GNUC,
254 * because we aim for ABI-compatibility with the GNU implementation, and
255 * various checks may test for equality of the class, which is incorrect.
257 static const uint64_t exception_class =
258 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
260 * Class used for dependent exceptions.
262 static const uint64_t dependent_exception_class =
263 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01');
265 * The low four bytes of the exception class, indicating that we conform to the
266 * Itanium C++ ABI. This is currently unused, but should be used in the future
267 * if we change our exception class, to allow this library and libsupc++ to be
268 * linked to the same executable and both to interoperate.
270 static const uint32_t abi_exception_class =
271 GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
273 static bool isCXXException(uint64_t cls)
275 return (cls == exception_class) || (cls == dependent_exception_class);
278 static bool isDependentException(uint64_t cls)
280 return cls == dependent_exception_class;
283 static __cxa_exception *exceptionFromPointer(void *ex)
285 return reinterpret_cast<__cxa_exception*>(static_cast<char*>(ex) -
286 offsetof(struct __cxa_exception, unwindHeader));
288 static __cxa_exception *realExceptionFromException(__cxa_exception *ex)
290 if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; }
291 return reinterpret_cast<__cxa_exception*>((reinterpret_cast<__cxa_dependent_exception*>(ex))->primaryException)-1;
297 // Forward declaration of standard library terminate() function used to
299 void terminate(void);
302 using namespace ABI_NAMESPACE;
306 /** The global termination handler. */
307 static atomic<terminate_handler> terminateHandler = abort;
308 /** The global unexpected exception handler. */
309 static atomic<unexpected_handler> unexpectedHandler = std::terminate;
311 /** Key used for thread-local data. */
312 static pthread_key_t eh_key;
316 * Cleanup function, allowing foreign exception handlers to correctly destroy
317 * this exception if they catch it.
319 static void exception_cleanup(_Unwind_Reason_Code reason,
320 struct _Unwind_Exception *ex)
323 // [__cxa_exception [_Unwind_Exception]] [exception object]
325 // __cxa_free_exception expects a pointer to the exception object
326 __cxa_free_exception(static_cast<void*>(ex + 1));
328 static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
329 struct _Unwind_Exception *ex)
332 __cxa_free_dependent_exception(static_cast<void*>(ex + 1));
336 * Recursively walk a list of exceptions and delete them all in post-order.
338 static void free_exception_list(__cxa_exception *ex)
340 if (0 != ex->nextException)
342 free_exception_list(ex->nextException);
344 // __cxa_free_exception() expects to be passed the thrown object, which
345 // immediately follows the exception, not the exception itself
346 __cxa_free_exception(ex+1);
350 * Cleanup function called when a thread exists to make certain that all of the
351 * per-thread data is deleted.
353 static void thread_cleanup(void* thread_info)
355 __cxa_thread_info *info = static_cast<__cxa_thread_info*>(thread_info);
356 if (info->globals.caughtExceptions)
358 // If this is a foreign exception, ask it to clean itself up.
359 if (info->foreign_exception_state != __cxa_thread_info::none)
361 _Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(info->globals.caughtExceptions);
362 if (e->exception_cleanup)
363 e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
367 free_exception_list(info->globals.caughtExceptions);
375 * Once control used to protect the key creation.
377 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
380 * We may not be linked against a full pthread implementation. If we're not,
381 * then we need to fake the thread-local storage by storing 'thread-local'
382 * things in a global.
386 * Thread-local storage for a single-threaded program.
388 static __cxa_thread_info singleThreadInfo;
392 static void init_key(void)
394 if ((0 == pthread_key_create) ||
395 (0 == pthread_setspecific) ||
396 (0 == pthread_getspecific))
401 pthread_key_create(&eh_key, thread_cleanup);
402 pthread_setspecific(eh_key, reinterpret_cast<void *>(0x42));
403 fakeTLS = (pthread_getspecific(eh_key) != reinterpret_cast<void *>(0x42));
404 pthread_setspecific(eh_key, 0);
408 * Returns the thread info structure, creating it if it is not already created.
410 static __cxa_thread_info *thread_info()
412 if ((0 == pthread_once) || pthread_once(&once_control, init_key))
416 if (fakeTLS) { return &singleThreadInfo; }
417 __cxa_thread_info *info = static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
420 info = static_cast<__cxa_thread_info*>(calloc(1, sizeof(__cxa_thread_info)));
421 pthread_setspecific(eh_key, info);
426 * Fast version of thread_info(). May fail if thread_info() is not called on
427 * this thread at least once already.
429 static __cxa_thread_info *thread_info_fast()
431 if (fakeTLS) { return &singleThreadInfo; }
432 return static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
435 * ABI function returning the __cxa_eh_globals structure.
437 extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
439 return &(thread_info()->globals);
442 * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
443 * been called at least once by this thread.
445 extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
447 return &(thread_info_fast()->globals);
450 #ifdef LIBCXXRT_NO_EMERGENCY_MALLOC
451 static char *alloc_or_die(size_t size)
453 char *buffer = static_cast<char*>(calloc(1, size));
455 if (buffer == nullptr)
457 fputs("Out of memory attempting to allocate exception\n", stderr);
462 static void free_exception(char *e)
468 * An emergency allocation reserved for when malloc fails. This is treated as
469 * 16 buffers of 1KB each.
471 static char emergency_buffer[16384];
473 * Flag indicating whether each buffer is allocated.
475 static bool buffer_allocated[16];
477 * Lock used to protect emergency allocation.
479 static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
481 * Condition variable used to wait when two threads are both trying to use the
482 * emergency malloc() buffer at once.
484 static pthread_cond_t emergency_malloc_wait = PTHREAD_COND_INITIALIZER;
487 * Allocates size bytes from the emergency allocation mechanism, if possible.
488 * This function will fail if size is over 1KB or if this thread already has 4
489 * emergency buffers. If all emergency buffers are allocated, it will sleep
490 * until one becomes available.
492 static char *emergency_malloc(size_t size)
494 if (size > 1024) { return 0; }
496 __cxa_thread_info *info = thread_info();
497 // Only 4 emergency buffers allowed per thread!
498 if (info->emergencyBuffersHeld > 3) { return 0; }
500 pthread_mutex_lock(&emergency_malloc_lock);
504 // While we were sleeping on the lock, another thread might have free'd
505 // enough memory for us to use, so try the allocation again - no point
506 // using the emergency buffer if there is some real memory that we can
508 void *m = calloc(1, size);
511 pthread_mutex_unlock(&emergency_malloc_lock);
512 return static_cast<char*>(m);
514 for (int i=0 ; i<16 ; i++)
516 if (!buffer_allocated[i])
519 buffer_allocated[i] = true;
523 // If there still isn't a buffer available, then sleep on the condition
524 // variable. This will be signalled when another thread releases one
525 // of the emergency buffers.
528 pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock);
531 pthread_mutex_unlock(&emergency_malloc_lock);
532 info->emergencyBuffersHeld++;
533 return emergency_buffer + (1024 * buffer);
537 * Frees a buffer returned by emergency_malloc().
539 * Note: Neither this nor emergency_malloc() is particularly efficient. This
540 * should not matter, because neither will be called in normal operation - they
541 * are only used when the program runs out of memory, which should not happen
544 static void emergency_malloc_free(char *ptr)
547 // Find the buffer corresponding to this pointer.
548 for (int i=0 ; i<16 ; i++)
550 if (ptr == static_cast<void*>(emergency_buffer + (1024 * i)))
556 assert(buffer >= 0 &&
557 "Trying to free something that is not an emergency buffer!");
558 // emergency_malloc() is expected to return 0-initialized data. We don't
559 // zero the buffer when allocating it, because the static buffers will
560 // begin life containing 0 values.
561 memset(ptr, 0, 1024);
562 // Signal the condition variable to wake up any threads that are blocking
563 // waiting for some space in the emergency buffer
564 pthread_mutex_lock(&emergency_malloc_lock);
565 // In theory, we don't need to do this with the lock held. In practice,
566 // our array of bools will probably be updated using 32-bit or 64-bit
567 // memory operations, so this update may clobber adjacent values.
568 buffer_allocated[buffer] = false;
569 pthread_cond_signal(&emergency_malloc_wait);
570 pthread_mutex_unlock(&emergency_malloc_lock);
573 static char *alloc_or_die(size_t size)
575 char *buffer = static_cast<char*>(calloc(1, size));
577 // If calloc() doesn't want to give us any memory, try using an emergency
581 buffer = emergency_malloc(size);
582 // This is only reached if the allocation is greater than 1KB, and
583 // anyone throwing objects that big really should know better.
586 fprintf(stderr, "Out of memory attempting to allocate exception\n");
592 static void free_exception(char *e)
594 // If this allocation is within the address range of the emergency buffer,
595 // don't call free() because it was not allocated with malloc()
596 if ((e >= emergency_buffer) &&
597 (e < (emergency_buffer + sizeof(emergency_buffer))))
599 emergency_malloc_free(e);
609 * Allocates an exception structure. Returns a pointer to the space that can
610 * be used to store an object of thrown_size bytes. This function will use an
611 * emergency buffer if malloc() fails, and may block if there are no such
614 extern "C" void *__cxa_allocate_exception(size_t thrown_size) throw()
616 size_t size = thrown_size + sizeof(__cxa_exception);
617 char *buffer = alloc_or_die(size);
618 return buffer+sizeof(__cxa_exception);
621 extern "C" void *__cxa_allocate_dependent_exception(void)
623 size_t size = sizeof(__cxa_dependent_exception);
624 char *buffer = alloc_or_die(size);
625 return buffer+sizeof(__cxa_dependent_exception);
629 * __cxa_free_exception() is called when an exception was thrown in between
630 * calling __cxa_allocate_exception() and actually throwing the exception.
631 * This happens when the object's copy constructor throws an exception.
633 * In this implementation, it is also called by __cxa_end_catch() and during
636 extern "C" void __cxa_free_exception(void *thrown_exception) throw()
638 __cxa_exception *ex = reinterpret_cast<__cxa_exception*>(thrown_exception) - 1;
639 // Free the object that was thrown, calling its destructor
640 if (0 != ex->exceptionDestructor)
644 ex->exceptionDestructor(thrown_exception);
648 // FIXME: Check that this is really what the spec says to do.
653 free_exception(reinterpret_cast<char*>(ex));
656 static void releaseException(__cxa_exception *exception)
658 if (isDependentException(exception->unwindHeader.exception_class))
660 __cxa_free_dependent_exception(exception+1);
663 if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
665 // __cxa_free_exception() expects to be passed the thrown object,
666 // which immediately follows the exception, not the exception
668 __cxa_free_exception(exception+1);
672 void __cxa_free_dependent_exception(void *thrown_exception)
674 __cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(thrown_exception) - 1;
675 assert(isDependentException(ex->unwindHeader.exception_class));
676 if (ex->primaryException)
678 releaseException(realExceptionFromException(reinterpret_cast<__cxa_exception*>(ex)));
680 free_exception(reinterpret_cast<char*>(ex));
684 * Callback function used with _Unwind_Backtrace().
686 * Prints a stack trace. Used only for debugging help.
688 * Note: As of FreeBSD 8.1, dladd() still doesn't work properly, so this only
689 * correctly prints function names from public, relocatable, symbols.
691 static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
695 dladdr(reinterpret_cast<void *>(__cxa_current_exception_type), &myinfo);
696 void *ip = reinterpret_cast<void*>(_Unwind_GetIP(context));
698 if (dladdr(ip, &info) != 0)
700 if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
702 printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
705 return _URC_CONTINUE_UNWIND;
709 * Report a failure that occurred when attempting to throw an exception.
711 * If the failure happened by falling off the end of the stack without finding
712 * a handler, prints a back trace before aborting.
714 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
715 extern "C" void *__cxa_begin_catch(void *e) throw();
717 extern "C" void *__cxa_begin_catch(void *e);
719 static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
724 case _URC_FATAL_PHASE1_ERROR:
725 fprintf(stderr, "Fatal error during phase 1 unwinding\n");
727 #if !defined(__arm__) || defined(__ARM_DWARF_EH__)
728 case _URC_FATAL_PHASE2_ERROR:
729 fprintf(stderr, "Fatal error during phase 2 unwinding\n");
732 case _URC_END_OF_STACK:
733 __cxa_begin_catch (&(thrown_exception->unwindHeader));
735 fprintf(stderr, "Terminating due to uncaught exception %p",
736 static_cast<void*>(thrown_exception));
737 thrown_exception = realExceptionFromException(thrown_exception);
738 static const __class_type_info *e_ti =
739 static_cast<const __class_type_info*>(&typeid(std::exception));
740 const __class_type_info *throw_ti =
741 dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType);
745 static_cast<std::exception*>(e_ti->cast_to(static_cast<void*>(thrown_exception+1),
749 fprintf(stderr, " '%s'", e->what());
753 size_t bufferSize = 128;
754 char *demangled = static_cast<char*>(malloc(bufferSize));
755 const char *mangled = thrown_exception->exceptionType->name();
757 demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
758 fprintf(stderr, " of type %s\n",
759 status == 0 ? demangled : mangled);
760 if (status == 0) { free(demangled); }
761 // Print a back trace if no handler is found.
762 // TODO: Make this optional
764 _Unwind_Backtrace(trace, 0);
767 // Just abort. No need to call std::terminate for the second time
774 static void throw_exception(__cxa_exception *ex)
776 __cxa_thread_info *info = thread_info();
777 ex->unexpectedHandler = info->unexpectedHandler;
778 if (0 == ex->unexpectedHandler)
780 ex->unexpectedHandler = unexpectedHandler.load();
782 ex->terminateHandler = info->terminateHandler;
783 if (0 == ex->terminateHandler)
785 ex->terminateHandler = terminateHandler.load();
787 info->globals.uncaughtExceptions++;
789 _Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
790 // The _Unwind_RaiseException() function should not return, it should
791 // unwind the stack past this function. If it does return, then something
793 report_failure(err, ex);
796 extern "C" __cxa_exception *__cxa_init_primary_exception(
797 void *object, std::type_info* tinfo, void (*dest)(void *)) throw() {
798 __cxa_exception *ex = reinterpret_cast<__cxa_exception*>(object) - 1;
800 ex->referenceCount = 0;
801 ex->exceptionType = tinfo;
803 ex->exceptionDestructor = dest;
805 ex->unwindHeader.exception_class = exception_class;
806 ex->unwindHeader.exception_cleanup = exception_cleanup;
813 * ABI function for throwing an exception. Takes the object to be thrown (the
814 * pointer returned by __cxa_allocate_exception()), the type info for the
815 * pointee, and the destructor (if there is one) as arguments.
817 extern "C" void __cxa_throw(void *thrown_exception,
818 std::type_info *tinfo,
821 __cxa_exception *ex = __cxa_init_primary_exception(thrown_exception, tinfo, dest);
822 ex->referenceCount = 1;
827 extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
829 if (NULL == thrown_exception) { return; }
831 __cxa_exception *original = exceptionFromPointer(thrown_exception);
832 __cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception())-1;
834 ex->primaryException = thrown_exception;
835 __cxa_increment_exception_refcount(thrown_exception);
837 ex->exceptionType = original->exceptionType;
838 ex->unwindHeader.exception_class = dependent_exception_class;
839 ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
841 throw_exception(reinterpret_cast<__cxa_exception*>(ex));
844 extern "C" void *__cxa_current_primary_exception(void)
846 __cxa_eh_globals* globals = __cxa_get_globals();
847 __cxa_exception *ex = globals->caughtExceptions;
849 if (0 == ex) { return NULL; }
850 ex = realExceptionFromException(ex);
851 __sync_fetch_and_add(&ex->referenceCount, 1);
855 extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
857 if (NULL == thrown_exception) { return; }
858 __cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
859 if (isDependentException(ex->unwindHeader.exception_class)) { return; }
860 __sync_fetch_and_add(&ex->referenceCount, 1);
862 extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
864 if (NULL == thrown_exception) { return; }
865 __cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
866 releaseException(ex);
870 * ABI function. Rethrows the current exception. Does not remove the
871 * exception from the stack or decrement its handler count - the compiler is
872 * expected to set the landing pad for this function to the end of the catch
873 * block, and then call _Unwind_Resume() to continue unwinding once
874 * __cxa_end_catch() has been called and any cleanup code has been run.
876 extern "C" void __cxa_rethrow()
878 __cxa_thread_info *ti = thread_info();
879 __cxa_eh_globals *globals = &ti->globals;
880 // Note: We don't remove this from the caught list here, because
881 // __cxa_end_catch will be called when we unwind out of the try block. We
882 // could probably make this faster by providing an alternative rethrow
883 // function and ensuring that all cleanup code is run before calling it, so
884 // we can skip the top stack frame when unwinding.
885 __cxa_exception *ex = globals->caughtExceptions;
890 "Attempting to rethrow an exception that doesn't exist!\n");
894 if (ti->foreign_exception_state != __cxa_thread_info::none)
896 ti->foreign_exception_state = __cxa_thread_info::rethrown;
897 _Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ex);
898 _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(e);
899 report_failure(err, ex);
903 assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
905 // `globals->uncaughtExceptions` was decremented by `__cxa_begin_catch`.
906 // It's normally incremented by `throw_exception`, but this path invokes
907 // `_Unwind_Resume_or_Rethrow` directly to rethrow the exception.
908 // This path is only reachable if we're rethrowing a C++ exception -
909 // foreign exceptions don't adjust any of this state.
910 globals->uncaughtExceptions++;
912 // ex->handlerCount will be decremented in __cxa_end_catch in enclosing
915 // Make handler count negative. This will tell __cxa_end_catch that
916 // exception was rethrown and exception object should not be destroyed
917 // when handler count become zero
918 ex->handlerCount = -ex->handlerCount;
920 // Continue unwinding the stack with this exception. This should unwind to
921 // the place in the caller where __cxa_end_catch() is called. The caller
922 // will then run cleanup code and bounce the exception back with
924 _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
925 report_failure(err, ex);
929 * Returns the type_info object corresponding to the filter.
931 static std::type_info *get_type_info_entry(_Unwind_Context *context,
935 // Get the address of the record in the table.
936 dw_eh_ptr_t record = lsda->type_table -
937 dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
939 dw_eh_ptr_t start = record;
940 // Read the value, but it's probably an indirect reference...
941 int64_t offset = read_value(lsda->type_table_encoding, &record);
943 // (If the entry is 0, don't try to dereference it. That would be bad.)
944 if (offset == 0) { return 0; }
946 // ...so we need to resolve it
947 return reinterpret_cast<std::type_info*>(resolve_indirect_value(context,
948 lsda->type_table_encoding, offset, start));
954 * Checks the type signature found in a handler against the type of the thrown
955 * object. If ex is 0 then it is assumed to be a foreign exception and only
958 static bool check_type_signature(__cxa_exception *ex,
959 const std::type_info *type,
962 void *exception_ptr = static_cast<void*>(ex+1);
963 const std::type_info *ex_type = ex ? ex->exceptionType : 0;
965 bool is_ptr = ex ? ex_type->__is_pointer_p() : false;
968 exception_ptr = *static_cast<void**>(exception_ptr);
970 // Always match a catchall, even with a foreign exception
972 // Note: A 0 here is a catchall, not a cleanup, so we return true to
973 // indicate that we found a catch.
978 adjustedPtr = exception_ptr;
983 if (0 == ex) { return false; }
985 // If the types are the same, no casting is needed.
986 if (*type == *ex_type)
988 adjustedPtr = exception_ptr;
993 if (type->__do_catch(ex_type, &exception_ptr, 1))
995 adjustedPtr = exception_ptr;
1002 * Checks whether the exception matches the type specifiers in this action
1003 * record. If the exception only matches cleanups, then this returns false.
1004 * If it matches a catch (including a catchall) then it returns true.
1006 * The selector argument is used to return the selector that is passed in the
1007 * second exception register when installing the context.
1009 static handler_type check_action_record(_Unwind_Context *context,
1010 dwarf_eh_lsda *lsda,
1011 dw_eh_ptr_t action_record,
1012 __cxa_exception *ex,
1013 unsigned long *selector,
1016 if (!action_record) { return handler_cleanup; }
1017 handler_type found = handler_none;
1018 while (action_record)
1020 int filter = read_sleb128(&action_record);
1021 dw_eh_ptr_t action_record_offset_base = action_record;
1022 int displacement = read_sleb128(&action_record);
1023 action_record = displacement ?
1024 action_record_offset_base + displacement : 0;
1025 // We only check handler types for C++ exceptions - foreign exceptions
1026 // are only allowed for cleanups and catchalls.
1029 std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
1030 if (check_type_signature(ex, handler_type, adjustedPtr))
1033 return handler_catch;
1036 else if (filter < 0 && 0 != ex)
1038 bool matched = false;
1040 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
1042 std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
1043 while (handler_type)
1045 if (check_type_signature(ex, handler_type, adjustedPtr))
1050 handler_type = get_type_info_entry(context, lsda, filter--);
1053 unsigned char *type_index = reinterpret_cast<unsigned char*>(lsda->type_table) - filter - 1;
1056 std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
1057 // If the exception spec matches a permitted throw type for
1058 // this function, don't report a handler - we are allowed to
1059 // propagate this exception out.
1060 if (check_type_signature(ex, handler_type, adjustedPtr))
1067 if (matched) { continue; }
1068 // If we don't find an allowed exception spec, we need to install
1069 // the context for this action. The landing pad will then call the
1070 // unexpected exception function. Treat this as a catch
1071 return handler_catch;
1073 else if (filter == 0)
1076 found = handler_cleanup;
1082 static void pushCleanupException(_Unwind_Exception *exceptionObject,
1083 __cxa_exception *ex)
1085 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
1086 __cxa_thread_info *info = thread_info_fast();
1090 if (ex->cleanupCount > 1)
1092 assert(exceptionObject == info->currentCleanup);
1095 ex->nextCleanup = info->currentCleanup;
1097 info->currentCleanup = exceptionObject;
1102 * The exception personality function. This is referenced in the unwinding
1103 * DWARF metadata and is called by the unwind library for each C++ stack frame
1104 * containing catch or cleanup code.
1107 BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
1108 // This personality function is for version 1 of the ABI. If you use it
1109 // with a future version of the ABI, it won't know what to do, so it
1110 // reports a fatal error and give up before it breaks anything.
1113 return _URC_FATAL_PHASE1_ERROR;
1115 __cxa_exception *ex = 0;
1116 __cxa_exception *realEx = 0;
1118 // If this exception is throw by something else then we can't make any
1119 // assumptions about its layout beyond the fields declared in
1120 // _Unwind_Exception.
1121 bool foreignException = !isCXXException(exceptionClass);
1123 // If this isn't a foreign exception, then we have a C++ exception structure
1124 if (!foreignException)
1126 ex = exceptionFromPointer(exceptionObject);
1127 realEx = realExceptionFromException(ex);
1130 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
1131 unsigned char *lsda_addr =
1132 static_cast<unsigned char*>(_Unwind_GetLanguageSpecificData(context));
1134 unsigned char *lsda_addr =
1135 reinterpret_cast<unsigned char*>(static_cast<uintptr_t>(_Unwind_GetLanguageSpecificData(context)));
1138 // No LSDA implies no landing pads - try the next frame
1139 if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
1141 // These two variables define how the exception will be handled.
1142 dwarf_eh_action action = {0};
1143 unsigned long selector = 0;
1145 // During the search phase, we do a complete lookup. If we return
1146 // _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with
1147 // a _UA_HANDLER_FRAME action, telling us to install the handler frame. If
1148 // we return _URC_CONTINUE_UNWIND, we may be called again later with a
1149 // _UA_CLEANUP_PHASE action for this frame.
1151 // The point of the two-stage unwind allows us to entirely avoid any stack
1152 // unwinding if there is no handler. If there are just cleanups found,
1153 // then we can just panic call an abort function.
1155 // Matching a handler is much more expensive than matching a cleanup,
1156 // because we don't need to bother doing type comparisons (or looking at
1157 // the type table at all) for a cleanup. This means that there is no need
1158 // to cache the result of finding a cleanup, because it's (quite) quick to
1159 // look it up again from the action table.
1160 if (actions & _UA_SEARCH_PHASE)
1162 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1164 if (!dwarf_eh_find_callsite(context, &lsda, &action))
1166 // EH range not found. This happens if exception is thrown and not
1167 // caught inside a cleanup (destructor). We should call
1168 // terminate() in this case. The catchTemp (landing pad) field of
1169 // exception object will contain null when personality function is
1170 // called with _UA_HANDLER_FRAME action for phase 2 unwinding.
1171 return _URC_HANDLER_FOUND;
1174 handler_type found_handler = check_action_record(context, &lsda,
1175 action.action_record, realEx, &selector, ex->adjustedPtr);
1176 // If there's no action record, we've only found a cleanup, so keep
1177 // searching for something real
1178 if (found_handler == handler_catch)
1180 // Cache the results for the phase 2 unwind, if we found a handler
1181 // and this is not a foreign exception.
1184 saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
1185 ex->languageSpecificData = reinterpret_cast<const char*>(lsda_addr);
1186 ex->actionRecord = reinterpret_cast<const char*>(action.action_record);
1187 // ex->adjustedPtr is set when finding the action record.
1189 return _URC_HANDLER_FOUND;
1191 return continueUnwinding(exceptionObject, context);
1195 // If this is a foreign exception, we didn't have anywhere to cache the
1196 // lookup stuff, so we need to do it again. If this is either a forced
1197 // unwind, a foreign exception, or a cleanup, then we just install the
1198 // context for a cleanup.
1199 if (!(actions & _UA_HANDLER_FRAME))
1202 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1203 dwarf_eh_find_callsite(context, &lsda, &action);
1204 if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
1205 handler_type found_handler = check_action_record(context, &lsda,
1206 action.action_record, realEx, &selector, ex->adjustedPtr);
1207 // Ignore handlers this time.
1208 if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
1209 pushCleanupException(exceptionObject, ex);
1211 else if (foreignException)
1213 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1214 dwarf_eh_find_callsite(context, &lsda, &action);
1215 check_action_record(context, &lsda, action.action_record, realEx,
1216 &selector, ex->adjustedPtr);
1218 else if (ex->catchTemp == 0)
1220 // Uncaught exception in cleanup, calling terminate
1225 // Restore the saved info if we saved some last time.
1226 loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
1228 ex->handlerSwitchValue = 0;
1232 _Unwind_SetIP(context, reinterpret_cast<unsigned long>(action.landing_pad));
1233 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
1234 reinterpret_cast<unsigned long>(exceptionObject));
1235 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
1237 return _URC_INSTALL_CONTEXT;
1241 * ABI function called when entering a catch statement. The argument is the
1242 * pointer passed out of the personality function. This is always the start of
1243 * the _Unwind_Exception object. The return value for this function is the
1244 * pointer to the caught exception, which is either the adjusted pointer (for
1245 * C++ exceptions) of the unadjusted pointer (for foreign exceptions).
1247 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
1248 extern "C" void *__cxa_begin_catch(void *e) throw()
1250 extern "C" void *__cxa_begin_catch(void *e)
1253 // We can't call the fast version here, because if the first exception that
1254 // we see is a foreign exception then we won't have called it yet.
1255 __cxa_thread_info *ti = thread_info();
1256 __cxa_eh_globals *globals = &ti->globals;
1257 _Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(e);
1259 if (isCXXException(exceptionObject->exception_class))
1261 // Only exceptions thrown with a C++ exception throwing function will
1262 // increment this, so don't decrement it here.
1263 globals->uncaughtExceptions--;
1264 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1266 if (ex->handlerCount == 0)
1268 // Add this to the front of the list of exceptions being handled
1269 // and increment its handler count so that it won't be deleted
1271 ex->nextException = globals->caughtExceptions;
1272 globals->caughtExceptions = ex;
1275 if (ex->handlerCount < 0)
1277 // Rethrown exception is catched before end of catch block.
1278 // Clear the rethrow flag (make value positive) - we are allowed
1279 // to delete this exception at the end of the catch block, as long
1280 // as it isn't thrown again later.
1292 // __cxa_begin_catch() <- we are here
1295 ex->handlerCount = -ex->handlerCount + 1;
1301 ti->foreign_exception_state = __cxa_thread_info::none;
1303 return ex->adjustedPtr;
1307 // If this is a foreign exception, then we need to be able to
1308 // store it. We can't chain foreign exceptions, so we give up
1309 // if there are already some outstanding ones.
1310 if (globals->caughtExceptions != 0)
1314 globals->caughtExceptions = reinterpret_cast<__cxa_exception*>(exceptionObject);
1315 ti->foreign_exception_state = __cxa_thread_info::caught;
1317 // exceptionObject is the pointer to the _Unwind_Exception within the
1318 // __cxa_exception. The throw object is after this
1319 return (reinterpret_cast<char*>(exceptionObject) + sizeof(_Unwind_Exception));
1325 * ABI function called when exiting a catch block. This will free the current
1326 * exception if it is no longer referenced in other catch blocks.
1328 extern "C" void __cxa_end_catch()
1330 // We can call the fast version here because the slow version is called in
1331 // __cxa_throw(), which must have been called before we end a catch block
1332 __cxa_thread_info *ti = thread_info_fast();
1333 __cxa_eh_globals *globals = &ti->globals;
1334 __cxa_exception *ex = globals->caughtExceptions;
1336 assert(0 != ex && "Ending catch when no exception is on the stack!");
1338 if (ti->foreign_exception_state != __cxa_thread_info::none)
1340 if (ti->foreign_exception_state != __cxa_thread_info::rethrown)
1342 _Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ti->globals.caughtExceptions);
1343 if (e->exception_cleanup)
1344 e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
1346 globals->caughtExceptions = 0;
1347 ti->foreign_exception_state = __cxa_thread_info::none;
1351 bool deleteException = true;
1353 if (ex->handlerCount < 0)
1355 // exception was rethrown. Exception should not be deleted even if
1356 // handlerCount become zero.
1366 // __cxa_end_catch(); <- we are here
1372 deleteException = false;
1379 if (ex->handlerCount == 0)
1381 globals->caughtExceptions = ex->nextException;
1382 if (deleteException)
1384 releaseException(ex);
1390 * ABI function. Returns the type of the current exception.
1392 extern "C" std::type_info *__cxa_current_exception_type()
1394 __cxa_eh_globals *globals = __cxa_get_globals();
1395 __cxa_exception *ex = globals->caughtExceptions;
1396 return ex ? ex->exceptionType : 0;
1400 * Cleanup, ensures that `__cxa_end_catch` is called to balance an explicit
1401 * `__cxa_begin_catch` call.
1403 static void end_catch(char *)
1408 * ABI function, called when an exception specification is violated.
1410 * This function does not return.
1412 extern "C" void __cxa_call_unexpected(void*exception)
1414 _Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(exception);
1415 // Wrap the call to the unexpected handler in calls to `__cxa_begin_catch`
1416 // and `__cxa_end_catch` so that we correctly update exception counts if
1417 // the unexpected handler throws an exception.
1418 __cxa_begin_catch(exceptionObject);
1419 __attribute__((cleanup(end_catch)))
1421 if (exceptionObject->exception_class == exception_class)
1423 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1424 if (ex->unexpectedHandler)
1426 ex->unexpectedHandler();
1427 // Should not be reached.
1432 // Should not be reached.
1437 * ABI function, returns the adjusted pointer to the exception object.
1439 extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
1441 return exceptionFromPointer(exceptionObject)->adjustedPtr;
1445 * As an extension, we provide the ability for the unexpected and terminate
1446 * handlers to be thread-local. We default to the standards-compliant
1447 * behaviour where they are global.
1449 static bool thread_local_handlers = false;
1455 * Sets whether unexpected and terminate handlers should be thread-local.
1457 void set_use_thread_local_handlers(bool flag) throw()
1459 thread_local_handlers = flag;
1462 * Sets a thread-local unexpected handler.
1464 unexpected_handler set_unexpected(unexpected_handler f) throw()
1466 static __cxa_thread_info *info = thread_info();
1467 unexpected_handler old = info->unexpectedHandler;
1468 info->unexpectedHandler = f;
1472 * Sets a thread-local terminate handler.
1474 terminate_handler set_terminate(terminate_handler f) throw()
1476 static __cxa_thread_info *info = thread_info();
1477 terminate_handler old = info->terminateHandler;
1478 info->terminateHandler = f;
1486 * Sets the function that will be called when an exception specification is
1489 unexpected_handler set_unexpected(unexpected_handler f) throw()
1491 if (thread_local_handlers) { return pathscale::set_unexpected(f); }
1493 return unexpectedHandler.exchange(f);
1496 * Sets the function that is called to terminate the program.
1498 terminate_handler set_terminate(terminate_handler f) throw()
1500 if (thread_local_handlers) { return pathscale::set_terminate(f); }
1502 return terminateHandler.exchange(f);
1505 * Terminates the program, calling a custom terminate implementation if
1510 static __cxa_thread_info *info = thread_info();
1511 if (0 != info && 0 != info->terminateHandler)
1513 info->terminateHandler();
1514 // Should not be reached - a terminate handler is not expected to
1518 terminateHandler.load()();
1521 * Called when an unexpected exception is encountered (i.e. an exception
1522 * violates an exception specification). This calls abort() unless a
1523 * custom handler has been set..
1527 static __cxa_thread_info *info = thread_info();
1528 if (0 != info && 0 != info->unexpectedHandler)
1530 info->unexpectedHandler();
1531 // Should not be reached - a terminate handler is not expected to
1535 unexpectedHandler.load()();
1538 * Returns whether there are any exceptions currently being thrown that
1539 * have not been caught. This can occur inside a nested catch statement.
1541 bool uncaught_exception() throw()
1543 __cxa_thread_info *info = thread_info();
1544 return info->globals.uncaughtExceptions != 0;
1547 * Returns the number of exceptions currently being thrown that have not
1548 * been caught. This can occur inside a nested catch statement.
1550 int uncaught_exceptions() throw()
1552 __cxa_thread_info *info = thread_info();
1553 return info->globals.uncaughtExceptions;
1556 * Returns the current unexpected handler.
1558 unexpected_handler get_unexpected() throw()
1560 __cxa_thread_info *info = thread_info();
1561 if (info->unexpectedHandler)
1563 return info->unexpectedHandler;
1565 return unexpectedHandler.load();
1568 * Returns the current terminate handler.
1570 terminate_handler get_terminate() throw()
1572 __cxa_thread_info *info = thread_info();
1573 if (info->terminateHandler)
1575 return info->terminateHandler;
1577 return terminateHandler.load();
1580 #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
1581 extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
1583 __cxa_thread_info *info = thread_info_fast();
1584 _Unwind_Exception *exceptionObject = info->currentCleanup;
1585 if (isCXXException(exceptionObject->exception_class))
1587 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1589 if (ex->cleanupCount == 0)
1591 info->currentCleanup = ex->nextCleanup;
1592 ex->nextCleanup = 0;
1597 info->currentCleanup = 0;
1599 return exceptionObject;
1603 ".pushsection .text.__cxa_end_cleanup \n"
1604 ".global __cxa_end_cleanup \n"
1605 ".type __cxa_end_cleanup, \"function\" \n"
1606 "__cxa_end_cleanup: \n"
1607 " push {r1, r2, r3, r4} \n"
1609 " bl __cxa_get_cleanup \n"
1611 " pop {r1, r2, r3, r4} \n"
1612 " b _Unwind_Resume \n"