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