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 | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 24 | #include <stdio.h> |
| 25 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 26 | // +---------------------------+-----------------------------+---------------+ |
| 27 | // | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | |
| 28 | // +---------------------------+-----------------------------+---------------+ |
| 29 | // ^ |
| 30 | // | |
| 31 | // +-------------------------------------------------------+ |
| 32 | // | |
| 33 | // +---------------------------+-----------------------------+ |
| 34 | // | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 | |
| 35 | // +---------------------------+-----------------------------+ |
| 36 | |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 37 | namespace __cxxabiv1 { |
Howard Hinnant | b94f225 | 2012-01-24 18:15:20 +0000 | [diff] [blame] | 38 | |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 39 | // Utility routines |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 40 | static |
| 41 | inline |
| 42 | __cxa_exception* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 43 | cxa_exception_from_thrown_object(void* thrown_object) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 44 | { |
| 45 | return static_cast<__cxa_exception*>(thrown_object) - 1; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 46 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 47 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 48 | // Note: This is never called when exception_header is masquerading as a |
| 49 | // __cxa_dependent_exception. |
| 50 | static |
| 51 | inline |
| 52 | void* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 53 | thrown_object_from_cxa_exception(__cxa_exception* exception_header) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 54 | { |
| 55 | return static_cast<void*>(exception_header + 1); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 56 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 57 | |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 58 | // Get the exception object from the unwind pointer. |
| 59 | // Relies on the structure layout, where the unwind pointer is right in |
| 60 | // front of the user's exception object |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 61 | static |
| 62 | inline |
| 63 | __cxa_exception* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 64 | cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 65 | { |
| 66 | return cxa_exception_from_thrown_object(unwind_exception + 1 ); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 67 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 68 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 69 | static |
| 70 | inline |
| 71 | size_t |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 72 | cxa_exception_size_from_exception_thrown_size(size_t size) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 73 | { |
| 74 | return size + sizeof (__cxa_exception); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 75 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 76 | |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 77 | static void setExceptionClass(_Unwind_Exception* unwind_exception) { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 78 | unwind_exception->exception_class = kOurExceptionClass; |
| 79 | } |
| 80 | |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 81 | static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 82 | unwind_exception->exception_class = kOurDependentExceptionClass; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 83 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 84 | |
| 85 | // Is it one of ours? |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 86 | static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) { |
| 87 | return (unwind_exception->exception_class & get_language) == |
| 88 | (kOurExceptionClass & get_language); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 89 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 90 | |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 91 | static bool isDependentException(_Unwind_Exception* unwind_exception) { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 92 | return (unwind_exception->exception_class & 0xFF) == 0x01; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 95 | // This does not need to be atomic |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 96 | static inline int incrementHandlerCount(__cxa_exception *exception) { |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 97 | return ++exception->handlerCount; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 100 | // This does not need to be atomic |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 101 | static inline int decrementHandlerCount(__cxa_exception *exception) { |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 102 | return --exception->handlerCount; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 103 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 104 | |
Howard Hinnant | f46a3f8 | 2012-01-24 21:41:27 +0000 | [diff] [blame] | 105 | #include "fallback_malloc.ipp" |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 106 | |
| 107 | // Allocate some memory from _somewhere_ |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 108 | static void *do_malloc(size_t size) { |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 109 | void *ptr = std::malloc(size); |
| 110 | if (NULL == ptr) // if malloc fails, fall back to emergency stash |
| 111 | ptr = fallback_malloc(size); |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 112 | return ptr; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 113 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 114 | |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 115 | static void do_free(void *ptr) { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 116 | is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 117 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 118 | |
Howard Hinnant | b80931e | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 119 | /* |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 120 | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
| 121 | stored in exc is called. Otherwise the exceptionDestructor stored in |
| 122 | exc is called, and then the memory for the exception is deallocated. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 123 | |
| 124 | This is never called for a __cxa_dependent_exception. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 125 | */ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 126 | static |
| 127 | void |
| 128 | exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) |
| 129 | { |
| 130 | __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 131 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 132 | std::__terminate(exception_header->terminateHandler); |
| 133 | |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 134 | // TODO: Shouldn't this check the reference count first? |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 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 | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 141 | static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) { |
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. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 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 | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 171 | void __cxa_free_exception (void * thrown_object) throw() { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 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. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 179 | void * __cxa_allocate_dependent_exception () { |
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. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 191 | void __cxa_free_dependent_exception (void * dependent_exception) { |
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 |
Howard Hinnant | cbe4c6d | 2012-01-28 00:34:46 +0000 | [diff] [blame] | 243 | // This only happens when there is no handler, or some unexpected unwinding |
| 244 | // error happens. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 245 | failed_throw(exception_header); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 246 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 247 | |
| 248 | |
| 249 | // 2.5.3 Exception Handlers |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 250 | /* |
| 251 | The adjusted pointer is computed by the personality routine during phase 1 |
| 252 | and saved in the exception header (either __cxa_exception or |
| 253 | __cxa_dependent_exception). |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 254 | |
| 255 | Requires: exception is native |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 256 | */ |
| 257 | void* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 258 | __cxa_get_exception_ptr(void* unwind_exception) throw() |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 259 | { |
| 260 | return cxa_exception_from_exception_unwind_exception |
| 261 | ( |
| 262 | static_cast<_Unwind_Exception*>(unwind_exception) |
| 263 | )->adjustedPtr; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 264 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 265 | |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 266 | /* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 267 | This routine can catch foreign or native exceptions. If native, the exception |
| 268 | can be a primary or dependent variety. This routine may remain blissfully |
| 269 | ignorant of whether the native exception is primary or dependent. |
| 270 | |
| 271 | If the exception is native: |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 272 | * Increment's the exception's handler count. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 273 | * Push the exception on the stack of currently-caught exceptions if it is not |
| 274 | already there (from a rethrow). |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 275 | * Decrements the uncaught_exception count. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 276 | * Returns the adjusted pointer to the exception object, which is stored in |
| 277 | the __cxa_exception by the personality routine. |
| 278 | |
| 279 | If the exception is foreign, this means it did not originate from one of throw |
| 280 | routines. The foreign exception does not necessarily have a __cxa_exception |
| 281 | header. However we can catch it here with a catch (...), or with a call |
| 282 | to terminate or unexpected during unwinding. |
| 283 | * Do not try to increment the exception's handler count, we don't know where |
| 284 | it is. |
| 285 | * Push the exception on the stack of currently-caught exceptions only if the |
| 286 | stack is empty. The foreign exception has no way to link to the current |
| 287 | top of stack. If the stack is not empty, call terminate. Even with an |
| 288 | empty stack, this is hacked in by pushing a pointer to an imaginary |
| 289 | __cxa_exception block in front of the foreign exception. It would be better |
| 290 | if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it |
| 291 | doesn't. It has a stack of __cxa_exception (which has a next* in it). |
| 292 | * Do not decrement the uncaught_exception count because we didn't increment it |
| 293 | in __cxa_throw (or one of our rethrow functions). |
| 294 | * If we haven't terminated, assume the exception object is just past the |
| 295 | _Unwind_Exception and return a pointer to that. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 296 | */ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 297 | void* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 298 | __cxa_begin_catch(void* unwind_arg) throw() |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 299 | { |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 300 | printf("entering __cxa_begin_catch\n"); |
| 301 | _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg); |
| 302 | bool native_exception = isOurExceptionClass(unwind_exception); |
| 303 | __cxa_eh_globals* globals = __cxa_get_globals(); |
| 304 | // exception_header is a hackish offset from a foreign exception, but it |
| 305 | // works as long as we're careful not to try to access any __cxa_exception |
| 306 | // parts. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 307 | __cxa_exception* exception_header = |
| 308 | cxa_exception_from_exception_unwind_exception |
| 309 | ( |
| 310 | static_cast<_Unwind_Exception*>(unwind_exception) |
| 311 | ); |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 312 | if (native_exception) |
| 313 | { |
| 314 | // Increment the handler count, removing the flag about being rethrown |
| 315 | exception_header->handlerCount = exception_header->handlerCount < 0 ? |
| 316 | -exception_header->handlerCount + 1 : exception_header->handlerCount + 1; |
| 317 | // place the exception on the top of the stack if it's not already |
| 318 | // there by a previous rethrow |
| 319 | if (exception_header != globals->caughtExceptions) |
| 320 | { |
| 321 | exception_header->nextException = globals->caughtExceptions; |
| 322 | globals->caughtExceptions = exception_header; |
| 323 | } |
| 324 | globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local |
| 325 | printf("leaving __cxa_begin_catch\n"); |
| 326 | return exception_header->adjustedPtr; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 327 | } |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 328 | // Else this is a foreign exception |
| 329 | // If the caughtExceptions stack is not empty, terminate |
| 330 | if (globals->caughtExceptions != 0) |
| 331 | std::terminate(); |
| 332 | // Push the foreign exception on to the stack |
| 333 | globals->caughtExceptions = exception_header; |
| 334 | return unwind_exception + 1; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 335 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | /* |
| 339 | Upon exit for any reason, a handler must call: |
| 340 | void __cxa_end_catch (); |
| 341 | |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 342 | This routine can be called for either a native or foreign exception. |
| 343 | For a native exception: |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 344 | * Locates the most recently caught exception and decrements its handler count. |
| 345 | * 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] | 346 | * If the handler count goes down to zero, and the exception was not re-thrown |
| 347 | by throw, it locates the primary exception (which may be the same as the one |
| 348 | it's handling) and decrements its reference count. If that reference count |
| 349 | goes to zero, the function destroys the exception. In any case, if the current |
| 350 | exception is a dependent exception, it destroys that. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 351 | |
| 352 | For a foreign exception: |
| 353 | * If it has been rethrown, there is nothing to do. |
| 354 | * Otherwise delete the exception and pop the catch stack to empty. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 355 | */ |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 356 | void __cxa_end_catch() |
| 357 | { |
| 358 | printf("entering __cxa_end_catch\n"); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 359 | static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception), |
| 360 | "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)"); |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 361 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch |
| 362 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 363 | // If we've rethrown a foreign exception, then globals->caughtExceptions |
| 364 | // will have been made an empty stack by __cxa_rethrow() and there is |
| 365 | // nothing more to be done. Do nothing! |
| 366 | if (NULL != exception_header) |
| 367 | { |
| 368 | bool native_exception = isOurExceptionClass(&exception_header->unwindHeader); |
| 369 | if (native_exception) |
| 370 | { |
| 371 | // This is a native exception |
| 372 | if (exception_header->handlerCount < 0) |
| 373 | { |
| 374 | // The exception has been rethrown by __cxa_rethrow, so don't delete it |
| 375 | if (0 == incrementHandlerCount(exception_header)) |
| 376 | { |
| 377 | // Remove from the chain of uncaught exceptions |
| 378 | globals->caughtExceptions = exception_header->nextException; |
| 379 | // but don't destroy |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 380 | } |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 381 | // Keep handlerCount negative in case there are nested catch's |
| 382 | // that need to be told that this exception is rethrown. Don't |
| 383 | // erase this rethrow flag until the exception is recaught. |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | // The native exception has not been rethrown |
| 388 | if (0 == decrementHandlerCount(exception_header)) |
| 389 | { |
| 390 | // Remove from the chain of uncaught exceptions |
| 391 | globals->caughtExceptions = exception_header->nextException; |
| 392 | // Destroy this exception, being careful to distinguish |
| 393 | // between dependent and primary exceptions |
| 394 | if (isDependentException(&exception_header->unwindHeader)) |
| 395 | { |
| 396 | // Reset exception_header to primaryException and deallocate the dependent exception |
| 397 | __cxa_dependent_exception* dep_exception_header = |
| 398 | reinterpret_cast<__cxa_dependent_exception*>(exception_header); |
| 399 | exception_header = |
| 400 | cxa_exception_from_thrown_object(dep_exception_header->primaryException); |
| 401 | __cxa_free_dependent_exception(dep_exception_header); |
| 402 | } |
| 403 | // Destroy the primary exception only if its referenceCount goes to 0 |
| 404 | // (this decrement must be atomic) |
| 405 | __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header)); |
| 406 | } |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 409 | else |
| 410 | { |
| 411 | // The foreign exception has not been rethrown. Pop the stack |
| 412 | // and delete it. If there are nested catch's and they try |
| 413 | // to touch a foreign exception in any way, that is undefined |
| 414 | // behavior. They likely can't since the only way to catch |
| 415 | // a foreign exception is with catch (...)! |
| 416 | _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader); |
| 417 | globals->caughtExceptions = 0; |
| 418 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 419 | } |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 420 | printf("leaving __cxa_end_catch\n"); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 421 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 422 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 423 | // Note: exception_header may be masquerading as a __cxa_dependent_exception |
| 424 | // and that's ok. exceptionType is there too. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 425 | // However watch out for foreign exceptions. Return null for them. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 426 | std::type_info * __cxa_current_exception_type() { |
| 427 | // get the current exception |
Marshall Clow | 143cfb0 | 2011-12-22 15:45:05 +0000 | [diff] [blame] | 428 | __cxa_eh_globals *globals = __cxa_get_globals_fast(); |
| 429 | if (NULL == globals) |
| 430 | 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] | 431 | __cxa_exception *exception_header = globals->caughtExceptions; |
| 432 | if (NULL == exception_header) |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 433 | return NULL; // No current exception |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 434 | if (!isOurExceptionClass(&exception_header->unwindHeader)) |
| 435 | return NULL; |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 436 | return exception_header->exceptionType; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 437 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 438 | |
| 439 | // 2.5.4 Rethrowing Exceptions |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 440 | /* This routine can rethrow native or foreign exceptions. |
| 441 | If the exception is native: |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 442 | * marks the exception object on top of the caughtExceptions stack |
| 443 | (in an implementation-defined way) as being rethrown. |
| 444 | * If the caughtExceptions stack is empty, it calls terminate() |
| 445 | (see [C++FDIS] [except.throw], 15.1.8). |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 446 | * It then calls _Unwind_RaiseException which should not return |
Howard Hinnant | 939daa7 | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 447 | (terminate if it does). |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 448 | Note: exception_header may be masquerading as a __cxa_dependent_exception |
| 449 | and that's ok. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 450 | */ |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 451 | LIBCXXABI_NORETURN |
| 452 | void |
| 453 | __cxa_rethrow() |
| 454 | { |
| 455 | __cxa_eh_globals* globals = __cxa_get_globals(); |
| 456 | //printf("entering __cxa_rethrow\n"); |
| 457 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 458 | if (NULL == exception_header) |
| 459 | std::terminate(); // throw; called outside of a exception handler |
| 460 | bool native_exception = isOurExceptionClass(&exception_header->unwindHeader); |
| 461 | if (native_exception) |
| 462 | { |
| 463 | //printf("__cxa_rethrow native branch\n"); |
| 464 | // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch) |
| 465 | exception_header->handlerCount = -exception_header->handlerCount; |
| 466 | globals->uncaughtExceptions += 1; |
| 467 | // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary |
| 468 | } |
| 469 | else // this is a foreign exception |
| 470 | { |
| 471 | // The only way to communicate to __cxa_end_catch that we've rethrown |
| 472 | // a foreign exception, so don't delete us, is to pop the stack here |
| 473 | // which must be empty afterwards. Then __cxa_end_catch will do |
| 474 | // nothing |
| 475 | globals->caughtExceptions = 0; |
| 476 | } |
| 477 | //printf("leaving __cxa_rethrow, private_1 = %lu\n", exception_header->unwindHeader.private_1); |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 478 | #if __arm__ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 479 | (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader); |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 480 | #else |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 481 | (void)_Unwind_RaiseException(&exception_header->unwindHeader); |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 482 | #endif |
| 483 | |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 484 | // If we get here, some kind of unwinding error has occurred. |
| 485 | // There is some weird code generation bug happening with |
| 486 | // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn) |
| 487 | // If we call failed_throw here. Turns up with -O2 or higher, and -Os. |
| 488 | __cxa_begin_catch(&exception_header->unwindHeader); |
| 489 | if (native_exception) |
| 490 | std::__terminate(exception_header->terminateHandler); |
| 491 | // Foreign exception: can't get exception_header->terminateHandler |
| 492 | std::terminate(); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 493 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 494 | |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 495 | /* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 496 | If thrown_object is not null, atomically increment the referenceCount field |
| 497 | of the __cxa_exception header associated with the thrown object referred to |
| 498 | by thrown_object. |
| 499 | |
| 500 | Requires: If thrown_object is not NULL, it is a native exception. |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 501 | */ |
| 502 | void |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 503 | __cxa_increment_exception_refcount(void* thrown_object) throw() |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 504 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 505 | if (thrown_object != NULL ) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 506 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 507 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
| 508 | __sync_add_and_fetch(&exception_header->referenceCount, 1); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | /* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 513 | If thrown_object is not null, atomically decrement the referenceCount field |
| 514 | of the __cxa_exception header associated with the thrown object referred to |
| 515 | by thrown_object. If the referenceCount drops to zero, destroy and |
| 516 | deallocate the exception. |
| 517 | |
| 518 | Requires: If thrown_object is not NULL, it is a native exception. |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 519 | */ |
| 520 | void |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 521 | __cxa_decrement_exception_refcount(void* thrown_object) throw() |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 522 | { |
| 523 | if (thrown_object != NULL ) |
| 524 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 525 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
| 526 | if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 527 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 528 | if (NULL != exception_header->exceptionDestructor) |
| 529 | exception_header->exceptionDestructor(thrown_object); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 530 | __cxa_free_exception(thrown_object); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | Returns a pointer to the thrown object (if any) at the top of the |
| 537 | caughtExceptions stack. Atommically increment the exception's referenceCount. |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 538 | If there is no such thrown object or if the thrown object is foreign, |
| 539 | returns null. |
Marshall Clow | 1599756 | 2012-01-04 22:18:10 +0000 | [diff] [blame] | 540 | |
| 541 | We can use __cxa_get_globals_fast here to get the globals because if there have |
| 542 | been no exceptions thrown, ever, on this thread, we can return NULL without |
| 543 | the need to allocate the exception-handling globals. |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 544 | */ |
| 545 | void* |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 546 | __cxa_current_primary_exception() throw() |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 547 | { |
| 548 | // get the current exception |
Marshall Clow | e80ed7d | 2012-01-03 23:26:09 +0000 | [diff] [blame] | 549 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
Marshall Clow | 5790852 | 2012-01-03 23:10:20 +0000 | [diff] [blame] | 550 | if (NULL == globals) |
Marshall Clow | 8989e4e | 2012-01-04 14:56:09 +0000 | [diff] [blame] | 551 | return NULL; // If there are no globals, there is no exception |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 552 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 553 | if (NULL == exception_header) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 554 | return NULL; // No current exception |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 555 | if (!isOurExceptionClass(&exception_header->unwindHeader)) |
| 556 | return NULL; // Can't capture a foreign exception (no way to refcount it) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 557 | if (isDependentException(&exception_header->unwindHeader)) { |
| 558 | __cxa_dependent_exception* dep_exception_header = |
| 559 | reinterpret_cast<__cxa_dependent_exception*>(exception_header); |
| 560 | exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 561 | } |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 562 | void* thrown_object = thrown_object_from_cxa_exception(exception_header); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 563 | __cxa_increment_exception_refcount(thrown_object); |
| 564 | return thrown_object; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
| 569 | stored in exc is called. Otherwise the referenceCount stored in the |
| 570 | primary exception is decremented, destroying the primary if necessary. |
| 571 | Finally the dependent exception is destroyed. |
| 572 | */ |
| 573 | static |
| 574 | void |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 575 | dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 576 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 577 | __cxa_dependent_exception* dep_exception_header = |
| 578 | reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1; |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 579 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 580 | std::__terminate(dep_exception_header->terminateHandler); |
| 581 | __cxa_decrement_exception_refcount(dep_exception_header->primaryException); |
| 582 | __cxa_free_dependent_exception(dep_exception_header); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* |
| 586 | If thrown_object is not null, allocate, initialize and thow a dependent |
| 587 | exception. |
| 588 | */ |
| 589 | void |
| 590 | __cxa_rethrow_primary_exception(void* thrown_object) |
| 591 | { |
| 592 | if ( thrown_object != NULL ) |
| 593 | { |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 594 | // thrown_object guaranteed to be native because |
| 595 | // __cxa_current_primary_exception returns NULL for foreign exceptions |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 596 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
| 597 | __cxa_dependent_exception* dep_exception_header = |
| 598 | static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception()); |
| 599 | dep_exception_header->primaryException = thrown_object; |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 600 | __cxa_increment_exception_refcount(thrown_object); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 601 | dep_exception_header->exceptionType = exception_header->exceptionType; |
| 602 | dep_exception_header->unexpectedHandler = std::get_unexpected(); |
| 603 | dep_exception_header->terminateHandler = std::get_terminate(); |
| 604 | setDependentExceptionClass(&dep_exception_header->unwindHeader); |
Howard Hinnant | b9bf50b | 2011-12-21 23:48:05 +0000 | [diff] [blame] | 605 | __cxa_get_globals()->uncaughtExceptions += 1; |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 606 | dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup; |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 607 | #if __arm__ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 608 | _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 609 | #else |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 610 | _Unwind_RaiseException(&dep_exception_header->unwindHeader); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 611 | #endif |
| 612 | // Some sort of unwinding error. Note that terminate is a handler. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 613 | __cxa_begin_catch(&dep_exception_header->unwindHeader); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 614 | } |
| 615 | // If we return client will call terminate() |
| 616 | } |
| 617 | |
Howard Hinnant | f710f1e | 2012-01-24 00:01:31 +0000 | [diff] [blame] | 618 | bool |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 619 | __cxa_uncaught_exception() throw() |
Howard Hinnant | f710f1e | 2012-01-24 00:01:31 +0000 | [diff] [blame] | 620 | { |
Howard Hinnant | b50cda7 | 2012-01-30 16:07:00 +0000 | [diff] [blame^] | 621 | // This does not report foreign exceptions in flight |
Howard Hinnant | f710f1e | 2012-01-24 00:01:31 +0000 | [diff] [blame] | 622 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 623 | if (globals == 0) |
| 624 | return false; |
| 625 | return globals->uncaughtExceptions != 0; |
| 626 | } |
| 627 | |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 628 | } // extern "C" |
| 629 | |
| 630 | } // abi |