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