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 |
| 18 | #include <string> // for memset |
| 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 | |
| 24 | namespace __cxxabiv1 { |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 25 | static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0 |
| 26 | static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1 |
| 27 | |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 28 | // Utility routines |
Howard Hinnant | e8fcf83 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 29 | static inline __cxa_exception *exception_from_thrown_object(void *p) throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 30 | return ((__cxa_exception *) p) - 1; |
| 31 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 32 | |
Howard Hinnant | e8fcf83 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 33 | static inline void * thrown_object_from_exception(void *p) throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 34 | return (void *) (((__cxa_exception *) p) + 1 ); |
| 35 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 36 | |
Howard Hinnant | e8fcf83 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 37 | static inline size_t object_size_from_exception_size(size_t size) throw() { |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 38 | return size + sizeof (__cxa_exception); |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 39 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 40 | |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 41 | // Get the exception object from the unwind pointer. |
| 42 | // Relies on the structure layout, where the unwind pointer is right in |
| 43 | // front of the user's exception object |
| 44 | static __cxa_exception * |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 45 | exception_from_exception_object(void *ptr) throw() { |
| 46 | _Unwind_Exception *p = reinterpret_cast<_Unwind_Exception *>(ptr); |
| 47 | return exception_from_thrown_object(p + 1 ); |
| 48 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 49 | |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 50 | static void setExceptionClass(_Unwind_Exception *unwind) throw() { |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 51 | unwind->exception_class = kOurExceptionClass; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 52 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 53 | |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 54 | static void setDependentExceptionClass(_Unwind_Exception *unwind) throw() { |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 55 | unwind->exception_class = kOurDependentExceptionClass; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 56 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 57 | |
| 58 | // Is it one of ours? |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 59 | static bool isOurExceptionClass(_Unwind_Exception *unwind) throw() { |
| 60 | return(unwind->exception_class == kOurExceptionClass)|| |
| 61 | (unwind->exception_class == kOurDependentExceptionClass); |
| 62 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 63 | |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 64 | static bool isDependentException(_Unwind_Exception *unwind) throw() { |
| 65 | return (unwind->exception_class & 0xFF) == 0x01; |
| 66 | } |
| 67 | |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 68 | // This does not need to be atomic |
| 69 | static inline int incrementHandlerCount(__cxa_exception *exception) throw() { |
| 70 | return ++exception->handlerCount; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 73 | // This does not need to be atomic |
| 74 | static inline int decrementHandlerCount(__cxa_exception *exception) throw() { |
| 75 | return --exception->handlerCount; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 76 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 77 | |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 78 | #include "fallback_malloc.cpp" |
| 79 | |
| 80 | // Allocate some memory from _somewhere_ |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 81 | static void *do_malloc(size_t size) throw() { |
| 82 | void *ptr = std::malloc(size); |
| 83 | if (NULL == ptr) // if malloc fails, fall back to emergency stash |
| 84 | ptr = fallback_malloc(size); |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 85 | return ptr; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 86 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 87 | |
| 88 | // Didn't know you could "return <expression>" from a void function, did you? |
| 89 | // Well, you can, if the type of the expression is "void" also. |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 90 | static void do_free(void *ptr) throw() { |
| 91 | return is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr); |
| 92 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 93 | |
Howard Hinnant | e8fcf83 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 94 | /* |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 95 | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
| 96 | stored in exc is called. Otherwise the exceptionDestructor stored in |
| 97 | exc is called, and then the memory for the exception is deallocated. |
| 98 | */ |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 99 | static void exception_cleanup_func(_Unwind_Reason_Code reason, struct _Unwind_Exception* exc) { |
| 100 | __cxa_exception *exception = exception_from_exception_object(exc); |
| 101 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
Howard Hinnant | e8fcf83 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 102 | std::__terminate(exception->terminateHandler); |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 103 | |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 104 | void * thrown_object = thrown_object_from_exception(exception); |
| 105 | if (NULL != exception->exceptionDestructor) |
| 106 | exception->exceptionDestructor(thrown_object); |
| 107 | __cxa_free_exception(thrown_object); |
| 108 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 109 | |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 110 | static LIBCXXABI_NORETURN void failed_throw(__cxa_exception *exception) throw() { |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 111 | // Section 2.5.3 says: |
| 112 | // * For purposes of this ABI, several things are considered exception handlers: |
| 113 | // ** A terminate() call due to a throw. |
| 114 | // and |
| 115 | // * Upon entry, Following initialization of the catch parameter, |
| 116 | // a handler must call: |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 117 | // * void *__cxa_begin_catch(void *exceptionObject ); |
| 118 | (void) __cxa_begin_catch(&exception->unwindHeader); |
Howard Hinnant | e8fcf83 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 119 | std::__terminate(exception->terminateHandler); |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 120 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 121 | |
| 122 | extern "C" { |
| 123 | |
| 124 | // Allocate a __cxa_exception object, and zero-fill it. |
| 125 | // Reserve "thrown_size" bytes on the end for the user's exception |
| 126 | // object. Zero-fill the object. If memory can't be allocated, call |
| 127 | // std::terminate. Return a pointer to the memory to be used for the |
| 128 | // user's exception object. |
| 129 | void * __cxa_allocate_exception (size_t thrown_size) throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 130 | size_t actual_size = object_size_from_exception_size(thrown_size); |
| 131 | void *ptr = do_malloc(actual_size); |
| 132 | if (NULL == ptr) |
| 133 | std::terminate(); |
| 134 | std::memset(ptr, 0, actual_size); |
| 135 | return thrown_object_from_exception(ptr); |
| 136 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | // Free a __cxa_exception object allocated with __cxa_allocate_exception. |
| 140 | void __cxa_free_exception (void * thrown_exception) throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 141 | do_free(exception_from_thrown_object(thrown_exception)); |
| 142 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | // This function shall allocate a __cxa_dependent_exception and |
| 146 | // return a pointer to it. (Really to the object, not past its' end). |
| 147 | // Otherwise, it will work like __cxa_allocate_exception. |
| 148 | void * __cxa_allocate_dependent_exception () throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 149 | size_t actual_size = sizeof(__cxa_dependent_exception); |
| 150 | void *ptr = do_malloc(actual_size); |
| 151 | if (NULL == ptr) |
| 152 | std::terminate(); |
| 153 | std::memset(ptr, 0, actual_size); |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 154 | return ptr; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 155 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | // This function shall free a dependent_exception. |
| 159 | // It does not affect the reference count of the primary exception. |
| 160 | void __cxa_free_dependent_exception (void * dependent_exception) throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 161 | do_free(dependent_exception); |
| 162 | } |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 163 | |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 164 | |
| 165 | // 2.4.3 Throwing the Exception Object |
| 166 | /* |
| 167 | After constructing the exception object with the throw argument value, |
| 168 | the generated code calls the __cxa_throw runtime library routine. This |
| 169 | routine never returns. |
| 170 | |
| 171 | The __cxa_throw routine will do the following: |
| 172 | |
| 173 | * Obtain the __cxa_exception header from the thrown exception object address, |
| 174 | which can be computed as follows: |
| 175 | __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1); |
| 176 | * Save the current unexpected_handler and terminate_handler in the __cxa_exception header. |
| 177 | * Save the tinfo and dest arguments in the __cxa_exception header. |
| 178 | * Set the exception_class field in the unwind header. This is a 64-bit value |
| 179 | representing the ASCII string "XXXXC++\0", where "XXXX" is a |
| 180 | vendor-dependent string. That is, for implementations conforming to this |
| 181 | ABI, the low-order 4 bytes of this 64-bit value will be "C++\0". |
| 182 | * Increment the uncaught_exception flag. |
| 183 | * Call _Unwind_RaiseException in the system unwind library, Its argument is the |
| 184 | pointer to the thrown exception, which __cxa_throw itself received as an argument. |
| 185 | __Unwind_RaiseException begins the process of stack unwinding, described |
| 186 | in Section 2.5. In special cases, such as an inability to find a |
| 187 | handler, _Unwind_RaiseException may return. In that case, __cxa_throw |
| 188 | will call terminate, assuming that there was no handler for the |
| 189 | exception. |
| 190 | */ |
| 191 | LIBCXXABI_NORETURN void |
| 192 | __cxa_throw(void * thrown_exception, std::type_info * tinfo, void (*dest)(void *)) { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 193 | __cxa_eh_globals *globals = __cxa_get_globals(); |
| 194 | __cxa_exception *exception = exception_from_thrown_object(thrown_exception); |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 195 | |
Howard Hinnant | fb271ad | 2011-12-06 19:02:03 +0000 | [diff] [blame] | 196 | exception->unexpectedHandler = std::get_unexpected(); |
| 197 | exception->terminateHandler = std::get_terminate(); |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 198 | exception->exceptionType = tinfo; |
| 199 | exception->exceptionDestructor = dest; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 200 | setExceptionClass(&exception->unwindHeader); |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 201 | exception->referenceCount = 1; // This is a newly allocated exception, no need for thread safety. |
| 202 | globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local |
| 203 | |
| 204 | exception->unwindHeader.exception_cleanup = exception_cleanup_func; |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 205 | #if __arm__ |
| 206 | _Unwind_SjLj_RaiseException(&exception->unwindHeader); |
| 207 | #else |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 208 | _Unwind_RaiseException(&exception->unwindHeader); |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 209 | #endif |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 210 | // If we get here, some kind of unwinding error has occurred. |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 211 | failed_throw(exception); |
| 212 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 213 | |
| 214 | |
| 215 | // 2.5.3 Exception Handlers |
| 216 | extern void * __cxa_get_exception_ptr(void * exceptionObject) throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 217 | return exception_from_exception_object(exceptionObject); |
| 218 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | /* |
| 222 | This routine: |
| 223 | * Increment's the exception's handler count. |
| 224 | * Places the exception on the stack of currently-caught exceptions if it is not |
| 225 | already there, linking the exception to the previous top of the stack. |
| 226 | * Decrements the uncaught_exception count. |
| 227 | * Returns the adjusted pointer to the exception object. |
| 228 | */ |
| 229 | void * __cxa_begin_catch(void * exceptionObject) throw() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 230 | __cxa_eh_globals *globals = __cxa_get_globals(); |
| 231 | __cxa_exception *exception = exception_from_exception_object(exceptionObject); |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 232 | |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 233 | // TODO: Handle foreign exceptions? How? |
| 234 | |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 235 | // Increment the handler count, removing the flag about being rethrown |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 236 | exception->handlerCount = exception->handlerCount < 0 ? |
| 237 | -exception->handlerCount + 1 : exception->handlerCount + 1; |
| 238 | |
| 239 | // place the exception on the top of the stack if it's not there. |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 240 | if (exception != globals->caughtExceptions) { |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 241 | exception->nextException = globals->caughtExceptions; |
| 242 | globals->caughtExceptions = exception; |
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 | globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 246 | return thrown_object_from_exception(exception); |
| 247 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | /* |
| 251 | Upon exit for any reason, a handler must call: |
| 252 | void __cxa_end_catch (); |
| 253 | |
| 254 | This routine: |
| 255 | * Locates the most recently caught exception and decrements its handler count. |
| 256 | * 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] | 257 | * If the handler count goes down to zero, and the exception was not re-thrown |
| 258 | by throw, it locates the primary exception (which may be the same as the one |
| 259 | it's handling) and decrements its reference count. If that reference count |
| 260 | goes to zero, the function destroys the exception. In any case, if the current |
| 261 | exception is a dependent exception, it destroys that. |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 262 | */ |
| 263 | void __cxa_end_catch() { |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 264 | __cxa_eh_globals *globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 265 | __cxa_exception *current_exception = globals->caughtExceptions; |
| 266 | |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 267 | if (NULL != current_exception) { |
Howard Hinnant | d4028fe | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 268 | // TODO: Handle foreign exceptions? How? |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 269 | if (current_exception->handlerCount < 0) { |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 270 | // The exception has been rethrown |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 271 | if (0 == incrementHandlerCount(current_exception)) { |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 272 | // Remove from the chain of uncaught exceptions |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 273 | globals->caughtExceptions = current_exception->nextException; |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 274 | // but don't destroy |
| 275 | } |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 276 | } |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 277 | else { // The exception has not been rethrown |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 278 | if (0 == decrementHandlerCount(current_exception)) { |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 279 | // Remove from the chain of uncaught exceptions |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 280 | globals->caughtExceptions = current_exception->nextException; |
Howard Hinnant | 6eb54ad | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 281 | if (isDependentException(¤t_exception->unwindHeader)) { |
| 282 | // Reset current_exception to primaryException and deallocate the dependent exception |
| 283 | __cxa_dependent_exception* deh = |
| 284 | reinterpret_cast<__cxa_dependent_exception*>(current_exception + 1) - 1; |
| 285 | current_exception = static_cast<__cxa_exception*>(deh->primaryException) - 1; |
| 286 | __cxa_free_dependent_exception(deh); |
| 287 | } |
| 288 | // Destroy the primary exception only if its referenceCount goes to 0 |
| 289 | // (this decrement must be atomic) |
Howard Hinnant | 1b0aed9 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 290 | __cxa_decrement_exception_refcount(thrown_object_from_exception(current_exception)); |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 293 | } |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 294 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 295 | |
| 296 | |
| 297 | std::type_info * __cxa_current_exception_type() { |
| 298 | // get the current exception |
Marshall Clow | 64e0097 | 2011-12-22 15:45:05 +0000 | [diff] [blame] | 299 | __cxa_eh_globals *globals = __cxa_get_globals_fast(); |
| 300 | if (NULL == globals) |
| 301 | return NULL; // If there have never been any exceptions, there are none now. |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 302 | __cxa_exception *current_exception = globals->caughtExceptions; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 303 | if (NULL == current_exception) |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 304 | return NULL; // No current exception |
Howard Hinnant | b5fea43 | 2011-12-12 18:16:10 +0000 | [diff] [blame] | 305 | if (isDependentException(¤t_exception->unwindHeader)) { |
| 306 | __cxa_dependent_exception* deh = |
| 307 | reinterpret_cast<__cxa_dependent_exception*>(current_exception + 1) - 1; |
| 308 | current_exception = static_cast<__cxa_exception*>(deh->primaryException) - 1; |
| 309 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 310 | return current_exception->exceptionType; |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 311 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 312 | |
| 313 | // 2.5.4 Rethrowing Exceptions |
| 314 | /* This routine |
| 315 | * marks the exception object on top of the caughtExceptions stack |
| 316 | (in an implementation-defined way) as being rethrown. |
| 317 | * If the caughtExceptions stack is empty, it calls terminate() |
| 318 | (see [C++FDIS] [except.throw], 15.1.8). |
Howard Hinnant | d4028fe | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 319 | * It then calls _Unwind_Resume_or_Rethrow which should not return |
| 320 | (terminate if it does). |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 321 | */ |
| 322 | extern LIBCXXABI_NORETURN void __cxa_rethrow() { |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 323 | __cxa_eh_globals *globals = __cxa_get_globals(); |
Howard Hinnant | d4028fe | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 324 | __cxa_exception *exception = globals->caughtExceptions; |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 325 | |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 326 | if (NULL == exception) // there's no current exception! |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 327 | std::terminate (); |
| 328 | |
Howard Hinnant | d4028fe | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 329 | // TODO: Handle foreign exceptions? How? |
| 330 | |
| 331 | // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch) |
| 332 | exception->handlerCount = -exception->handlerCount; |
| 333 | globals->uncaughtExceptions += 1; |
| 334 | // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 335 | |
| 336 | #if __arm__ |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 337 | (void) _Unwind_SjLj_Resume_or_Rethrow(&exception->unwindHeader); |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 338 | #else |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 339 | (void) _Unwind_Resume_or_Rethrow (&exception->unwindHeader); |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 340 | #endif |
| 341 | |
| 342 | // If we get here, some kind of unwinding error has occurred. |
Marshall Clow | 7ac3126 | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 343 | failed_throw(exception); |
| 344 | } |
Marshall Clow | 7c4652f | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 345 | |
Howard Hinnant | 1b0aed9 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 346 | /* |
| 347 | If p is not null, atomically increment the referenceCount field of the |
| 348 | __cxa_exception header associated with the thrown object referred to by p. |
| 349 | */ |
| 350 | void |
| 351 | __cxa_increment_exception_refcount(void* p) throw() |
| 352 | { |
| 353 | if (p != NULL ) |
| 354 | { |
| 355 | __cxa_exception* header = exception_from_thrown_object(p); |
| 356 | __sync_add_and_fetch(&header->referenceCount, 1); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | If p is not null, atomically decrement the referenceCount field of the |
| 362 | __cxa_exception header associated with the thrown object referred to by p. |
| 363 | If the referenceCount drops to zero, destroy and deallocate the exception. |
| 364 | */ |
| 365 | void |
| 366 | __cxa_decrement_exception_refcount(void* thrown_object) throw() |
| 367 | { |
| 368 | if (thrown_object != NULL ) |
| 369 | { |
| 370 | __cxa_exception* header = exception_from_thrown_object(thrown_object); |
| 371 | if (__sync_sub_and_fetch(&header->referenceCount, size_t(1)) == 0) |
| 372 | { |
| 373 | if (NULL != header->exceptionDestructor) |
| 374 | header->exceptionDestructor(thrown_object); |
| 375 | __cxa_free_exception(thrown_object); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | Returns a pointer to the thrown object (if any) at the top of the |
| 382 | caughtExceptions stack. Atommically increment the exception's referenceCount. |
| 383 | If there is no such thrown object, returns null. |
| 384 | */ |
| 385 | void* |
| 386 | __cxa_current_primary_exception() throw() |
| 387 | { |
| 388 | // get the current exception |
| 389 | __cxa_eh_globals* globals = __cxa_get_globals(); |
Marshall Clow | 62edc01 | 2012-01-03 23:10:20 +0000 | [diff] [blame^] | 390 | if (NULL == globals) |
| 391 | return NULL; // Never has been an exception |
Howard Hinnant | 1b0aed9 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 392 | __cxa_exception* current_exception = globals->caughtExceptions; |
| 393 | if (NULL == current_exception) |
| 394 | return NULL; // No current exception |
| 395 | if (isDependentException(¤t_exception->unwindHeader)) { |
| 396 | __cxa_dependent_exception* deh = |
| 397 | reinterpret_cast<__cxa_dependent_exception*>(current_exception + 1) - 1; |
| 398 | current_exception = static_cast<__cxa_exception*>(deh->primaryException) - 1; |
| 399 | } |
| 400 | void* thrown_object = thrown_object_from_exception(current_exception); |
| 401 | __cxa_increment_exception_refcount(thrown_object); |
| 402 | return thrown_object; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
| 407 | stored in exc is called. Otherwise the referenceCount stored in the |
| 408 | primary exception is decremented, destroying the primary if necessary. |
| 409 | Finally the dependent exception is destroyed. |
| 410 | */ |
| 411 | static |
| 412 | void |
| 413 | dependent_exception_cleanup(_Unwind_Reason_Code reason, struct _Unwind_Exception* exc) |
| 414 | { |
| 415 | __cxa_dependent_exception* deh = |
| 416 | reinterpret_cast<__cxa_dependent_exception*>(exc + 1) - 1; |
| 417 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
| 418 | std::__terminate(deh->terminateHandler); |
| 419 | __cxa_decrement_exception_refcount(deh->primaryException); |
| 420 | __cxa_free_dependent_exception(deh); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | If thrown_object is not null, allocate, initialize and thow a dependent |
| 425 | exception. |
| 426 | */ |
| 427 | void |
| 428 | __cxa_rethrow_primary_exception(void* thrown_object) |
| 429 | { |
| 430 | if ( thrown_object != NULL ) |
| 431 | { |
| 432 | __cxa_exception* header = exception_from_thrown_object(thrown_object); |
| 433 | __cxa_dependent_exception* deh = |
| 434 | (__cxa_dependent_exception*)__cxa_allocate_dependent_exception(); |
| 435 | deh->primaryException = thrown_object; |
| 436 | __cxa_increment_exception_refcount(thrown_object); |
| 437 | deh->exceptionType = header->exceptionType; |
| 438 | deh->unexpectedHandler = std::get_unexpected(); |
| 439 | deh->terminateHandler = std::get_terminate(); |
| 440 | setDependentExceptionClass(&deh->unwindHeader); |
Howard Hinnant | dd47ca4 | 2011-12-21 23:48:05 +0000 | [diff] [blame] | 441 | __cxa_get_globals()->uncaughtExceptions += 1; |
Howard Hinnant | 1b0aed9 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 442 | deh->unwindHeader.exception_cleanup = dependent_exception_cleanup; |
| 443 | #if __arm__ |
| 444 | _Unwind_SjLj_RaiseException(&deh->unwindHeader); |
| 445 | #else |
| 446 | _Unwind_RaiseException(&deh->unwindHeader); |
| 447 | #endif |
| 448 | // Some sort of unwinding error. Note that terminate is a handler. |
| 449 | __cxa_begin_catch(&deh->unwindHeader); |
| 450 | } |
| 451 | // If we return client will call terminate() |
| 452 | } |
| 453 | |
Howard Hinnant | 57d64cd | 2011-12-22 16:00:06 +0000 | [diff] [blame] | 454 | _Unwind_Reason_Code |
| 455 | __gxx_personality_v0(int version, _Unwind_Action actions, uint64_t exceptionClass, |
| 456 | _Unwind_Exception* exceptionObject, _Unwind_Context* context) |
| 457 | { |
| 458 | } |
| 459 | |
Marshall Clow | 47c29eb | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 460 | } // extern "C" |
| 461 | |
| 462 | } // abi |