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