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