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