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