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