Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 1 | //===------------------------- cxa_exception.cpp --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | // |
| 9 | // This file implements the "Exception Handling APIs" |
| 10 | // http://www.codesourcery.com/public/cxx-abi/abi-eh.html |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "cxxabi.h" |
| 15 | |
| 16 | #include <exception> // for std::terminate |
| 17 | #include <cstdlib> // for malloc, free |
| 18 | #include <string> // for memset |
| 19 | #include <pthread.h> |
| 20 | |
| 21 | #include "cxa_exception.hpp" |
Howard Hinnant | b80931e | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 22 | #include "cxa_handlers.hpp" |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 23 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 24 | // +---------------------------+-----------------------------+---------------+ |
| 25 | // | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | |
| 26 | // +---------------------------+-----------------------------+---------------+ |
| 27 | // ^ |
| 28 | // | |
| 29 | // +-------------------------------------------------------+ |
| 30 | // | |
| 31 | // +---------------------------+-----------------------------+ |
| 32 | // | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 | |
| 33 | // +---------------------------+-----------------------------+ |
| 34 | |
| 35 | |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 36 | namespace __cxxabiv1 { |
Howard Hinnant | b94f225 | 2012-01-24 18:15:20 +0000 | [diff] [blame] | 37 | |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 38 | // Utility routines |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 39 | static |
| 40 | inline |
| 41 | __cxa_exception* |
| 42 | cxa_exception_from_thrown_object(void* thrown_object) throw() |
| 43 | { |
| 44 | return static_cast<__cxa_exception*>(thrown_object) - 1; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 45 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 46 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 47 | // Note: This is never called when exception_header is masquerading as a |
| 48 | // __cxa_dependent_exception. |
| 49 | static |
| 50 | inline |
| 51 | void* |
| 52 | thrown_object_from_cxa_exception(__cxa_exception* exception_header) throw() |
| 53 | { |
| 54 | return static_cast<void*>(exception_header + 1); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 55 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 56 | |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 57 | // Get the exception object from the unwind pointer. |
| 58 | // Relies on the structure layout, where the unwind pointer is right in |
| 59 | // front of the user's exception object |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 60 | static |
| 61 | inline |
| 62 | __cxa_exception* |
| 63 | cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) throw() |
| 64 | { |
| 65 | return cxa_exception_from_thrown_object(unwind_exception + 1 ); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 66 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 67 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 68 | static |
| 69 | inline |
| 70 | size_t |
| 71 | cxa_exception_size_from_exception_thrown_size(size_t size) throw() |
| 72 | { |
| 73 | return size + sizeof (__cxa_exception); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 74 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 75 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 76 | static void setExceptionClass(_Unwind_Exception* unwind_exception) throw() { |
| 77 | unwind_exception->exception_class = kOurExceptionClass; |
| 78 | } |
| 79 | |
| 80 | static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) throw() { |
| 81 | unwind_exception->exception_class = kOurDependentExceptionClass; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 82 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 83 | |
| 84 | // Is it one of ours? |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 85 | static bool isOurExceptionClass(_Unwind_Exception* unwind_exception) throw() { |
| 86 | return(unwind_exception->exception_class == kOurExceptionClass)|| |
| 87 | (unwind_exception->exception_class == kOurDependentExceptionClass); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 88 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 89 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 90 | static bool isDependentException(_Unwind_Exception* unwind_exception) throw() { |
| 91 | return (unwind_exception->exception_class & 0xFF) == 0x01; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 94 | // This does not need to be atomic |
| 95 | static inline int incrementHandlerCount(__cxa_exception *exception) throw() { |
| 96 | return ++exception->handlerCount; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 99 | // This does not need to be atomic |
| 100 | static inline int decrementHandlerCount(__cxa_exception *exception) throw() { |
| 101 | return --exception->handlerCount; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 102 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 103 | |
Howard Hinnant | d5c461f | 2012-01-24 21:02:21 +0000 | [diff] [blame] | 104 | // A small, simple heap manager based (loosely) on |
| 105 | // the startup heap manager from FreeBSD, optimized for space. |
| 106 | // |
| 107 | // Manages a fixed-size memory pool, supports malloc and free only. |
| 108 | // No support for realloc. |
| 109 | // |
| 110 | // Allocates chunks in multiples of four bytes, with a four byte header |
| 111 | // for each chunk. The overhead of each chunk is kept low by keeping pointers |
| 112 | // as two byte offsets within the heap, rather than (4 or 8 byte) pointers. |
| 113 | |
| 114 | namespace { |
| 115 | |
| 116 | static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 117 | |
| 118 | class mutexor { |
| 119 | public: |
| 120 | mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); } |
| 121 | ~mutexor () { pthread_mutex_unlock ( mtx_ ); } |
| 122 | private: |
| 123 | mutexor ( const mutexor &rhs ); |
| 124 | mutexor & operator = ( const mutexor &rhs ); |
| 125 | pthread_mutex_t *mtx_; |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | #define HEAP_SIZE 512 |
| 130 | char heap [ HEAP_SIZE ]; |
| 131 | |
| 132 | typedef unsigned short heap_offset; |
| 133 | typedef unsigned short heap_size; |
| 134 | |
| 135 | struct heap_node { |
| 136 | heap_offset next_node; // offset into heap |
| 137 | heap_size len; // size in units of "sizeof(heap_node)" |
| 138 | }; |
| 139 | |
| 140 | static const heap_node *list_end = (heap_node *) ( &heap [ HEAP_SIZE ] ); // one past the end of the heap |
| 141 | static heap_node *freelist = NULL; |
| 142 | |
| 143 | heap_node *node_from_offset ( const heap_offset offset ) throw() |
| 144 | { return (heap_node *) ( heap + ( offset * sizeof (heap_node))); } |
| 145 | |
| 146 | heap_offset offset_from_node ( const heap_node *ptr ) throw() |
| 147 | { return (((char *) ptr ) - heap) / sizeof (heap_node); } |
| 148 | |
| 149 | void init_heap () throw() { |
| 150 | freelist = (heap_node *) heap; |
| 151 | freelist->next_node = offset_from_node ( list_end ); |
| 152 | freelist->len = HEAP_SIZE / sizeof (heap_node); |
| 153 | } |
| 154 | |
| 155 | // How big a chunk we allocate |
| 156 | size_t alloc_size (size_t len) throw() |
| 157 | { return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; } |
| 158 | |
| 159 | bool is_fallback_ptr ( void *ptr ) throw() |
| 160 | { return ptr >= heap && ptr < ( heap + HEAP_SIZE ); } |
| 161 | |
| 162 | void *fallback_malloc(size_t len) throw() { |
| 163 | heap_node *p, *prev; |
| 164 | const size_t nelems = alloc_size ( len ); |
| 165 | mutexor mtx ( &heap_mutex ); |
| 166 | |
| 167 | if ( NULL == freelist ) |
| 168 | init_heap (); |
| 169 | |
| 170 | // Walk the free list, looking for a "big enough" chunk |
| 171 | for (p = freelist, prev = 0; |
| 172 | p && p != list_end; prev = p, p = node_from_offset ( p->next_node)) { |
| 173 | |
| 174 | if (p->len > nelems) { // chunk is larger, shorten, and return the tail |
| 175 | heap_node *q; |
| 176 | |
| 177 | p->len -= nelems; |
| 178 | q = p + p->len; |
| 179 | q->next_node = 0; |
| 180 | q->len = nelems; |
| 181 | return (void *) (q + 1); |
| 182 | } |
| 183 | |
| 184 | if (p->len == nelems) { // exact size match |
| 185 | if (prev == 0) |
| 186 | freelist = node_from_offset(p->next_node); |
| 187 | else |
| 188 | prev->next_node = p->next_node; |
| 189 | p->next_node = 0; |
| 190 | return (void *) (p + 1); |
| 191 | } |
| 192 | } |
| 193 | return NULL; // couldn't find a spot big enough |
| 194 | } |
| 195 | |
| 196 | // Return the start of the next block |
| 197 | heap_node *after ( struct heap_node *p ) throw() { return p + p->len; } |
| 198 | |
| 199 | void fallback_free (void *ptr) throw() { |
| 200 | struct heap_node *cp = ((struct heap_node *) ptr) - 1; // retrieve the chunk |
| 201 | struct heap_node *p, *prev; |
| 202 | |
| 203 | mutexor mtx ( &heap_mutex ); |
| 204 | |
| 205 | #ifdef DEBUG_FALLBACK_MALLOC |
| 206 | std::cout << "Freeing item at " << offset_from_node ( cp ) << " of size " << cp->len << std::endl; |
| 207 | #endif |
| 208 | |
| 209 | for (p = freelist, prev = 0; |
| 210 | p && p != list_end; prev = p, p = node_from_offset (p->next_node)) { |
| 211 | #ifdef DEBUG_FALLBACK_MALLOC |
| 212 | std::cout << " p, cp, after (p), after(cp) " |
| 213 | << offset_from_node ( p ) << ' ' |
| 214 | << offset_from_node ( cp ) << ' ' |
| 215 | << offset_from_node ( after ( p )) << ' ' |
| 216 | << offset_from_node ( after ( cp )) << std::endl; |
| 217 | #endif |
| 218 | if ( after ( p ) == cp ) { |
| 219 | #ifdef DEBUG_FALLBACK_MALLOC |
| 220 | std::cout << " Appending onto chunk at " << offset_from_node ( p ) << std::endl; |
| 221 | #endif |
| 222 | p->len += cp->len; // make the free heap_node larger |
| 223 | return; |
| 224 | } |
| 225 | else if ( after ( cp ) == p ) { // there's a free heap_node right after |
| 226 | #ifdef DEBUG_FALLBACK_MALLOC |
| 227 | std::cout << " Appending free chunk at " << offset_from_node ( p ) << std::endl; |
| 228 | #endif |
| 229 | cp->len += p->len; |
| 230 | if ( prev == 0 ) { |
| 231 | freelist = cp; |
| 232 | cp->next_node = p->next_node; |
| 233 | } |
| 234 | else |
| 235 | prev->next_node = offset_from_node(cp); |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | // Nothing to merge with, add it to the start of the free list |
| 240 | #ifdef DEBUG_FALLBACK_MALLOC |
| 241 | std::cout << " Making new free list entry " << offset_from_node ( cp ) << std::endl; |
| 242 | #endif |
| 243 | cp->next_node = offset_from_node ( freelist ); |
| 244 | freelist = cp; |
| 245 | } |
| 246 | |
| 247 | #ifdef INSTRUMENT_FALLBACK_MALLOC |
| 248 | size_t print_free_list () { |
| 249 | struct heap_node *p, *prev; |
| 250 | heap_size total_free = 0; |
| 251 | if ( NULL == freelist ) |
| 252 | init_heap (); |
| 253 | |
| 254 | for (p = freelist, prev = 0; |
| 255 | p && p != list_end; prev = p, p = node_from_offset (p->next_node)) { |
| 256 | std::cout << ( prev == 0 ? "" : " ") << "Offset: " << offset_from_node ( p ) |
| 257 | << "\tsize: " << p->len << " Next: " << p->next_node << std::endl; |
| 258 | total_free += p->len; |
| 259 | } |
| 260 | std::cout << "Total Free space: " << total_free << std::endl; |
| 261 | return total_free; |
| 262 | } |
| 263 | #endif |
| 264 | } // end unnamed namespace |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 265 | |
| 266 | // Allocate some memory from _somewhere_ |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 267 | static void *do_malloc(size_t size) throw() { |
| 268 | void *ptr = std::malloc(size); |
| 269 | if (NULL == ptr) // if malloc fails, fall back to emergency stash |
| 270 | ptr = fallback_malloc(size); |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 271 | return ptr; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 272 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 273 | |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 274 | static void do_free(void *ptr) throw() { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 275 | is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 276 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 277 | |
Howard Hinnant | b80931e | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 278 | /* |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 279 | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
| 280 | stored in exc is called. Otherwise the exceptionDestructor stored in |
| 281 | exc is called, and then the memory for the exception is deallocated. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 282 | |
| 283 | This is never called for a __cxa_dependent_exception. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 284 | */ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 285 | static |
| 286 | void |
| 287 | exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) |
| 288 | { |
| 289 | __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 290 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 291 | std::__terminate(exception_header->terminateHandler); |
| 292 | |
| 293 | void * thrown_object = thrown_object_from_cxa_exception(exception_header); |
| 294 | if (NULL != exception_header->exceptionDestructor) |
| 295 | exception_header->exceptionDestructor(thrown_object); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 296 | __cxa_free_exception(thrown_object); |
| 297 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 298 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 299 | static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) throw() { |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 300 | // Section 2.5.3 says: |
| 301 | // * For purposes of this ABI, several things are considered exception handlers: |
| 302 | // ** A terminate() call due to a throw. |
| 303 | // and |
| 304 | // * Upon entry, Following initialization of the catch parameter, |
| 305 | // a handler must call: |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 306 | // * void *__cxa_begin_catch(void *exceptionObject ); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 307 | (void) __cxa_begin_catch(&exception_header->unwindHeader); |
| 308 | std::__terminate(exception_header->terminateHandler); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 309 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 310 | |
| 311 | extern "C" { |
| 312 | |
| 313 | // Allocate a __cxa_exception object, and zero-fill it. |
| 314 | // Reserve "thrown_size" bytes on the end for the user's exception |
| 315 | // object. Zero-fill the object. If memory can't be allocated, call |
| 316 | // std::terminate. Return a pointer to the memory to be used for the |
| 317 | // user's exception object. |
| 318 | void * __cxa_allocate_exception (size_t thrown_size) throw() { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 319 | size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size); |
| 320 | __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size)); |
| 321 | if (NULL == exception_header) |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 322 | std::terminate(); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 323 | std::memset(exception_header, 0, actual_size); |
| 324 | return thrown_object_from_cxa_exception(exception_header); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 325 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 326 | |
| 327 | |
| 328 | // Free a __cxa_exception object allocated with __cxa_allocate_exception. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 329 | void __cxa_free_exception (void * thrown_object) throw() { |
| 330 | do_free(cxa_exception_from_thrown_object(thrown_object)); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 331 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 332 | |
| 333 | |
| 334 | // This function shall allocate a __cxa_dependent_exception and |
| 335 | // return a pointer to it. (Really to the object, not past its' end). |
| 336 | // Otherwise, it will work like __cxa_allocate_exception. |
| 337 | void * __cxa_allocate_dependent_exception () throw() { |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 338 | size_t actual_size = sizeof(__cxa_dependent_exception); |
| 339 | void *ptr = do_malloc(actual_size); |
| 340 | if (NULL == ptr) |
| 341 | std::terminate(); |
| 342 | std::memset(ptr, 0, actual_size); |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 343 | return ptr; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 344 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 345 | |
| 346 | |
| 347 | // This function shall free a dependent_exception. |
| 348 | // It does not affect the reference count of the primary exception. |
| 349 | void __cxa_free_dependent_exception (void * dependent_exception) throw() { |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 350 | do_free(dependent_exception); |
| 351 | } |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 352 | |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 353 | |
| 354 | // 2.4.3 Throwing the Exception Object |
| 355 | /* |
| 356 | After constructing the exception object with the throw argument value, |
| 357 | the generated code calls the __cxa_throw runtime library routine. This |
| 358 | routine never returns. |
| 359 | |
| 360 | The __cxa_throw routine will do the following: |
| 361 | |
| 362 | * Obtain the __cxa_exception header from the thrown exception object address, |
| 363 | which can be computed as follows: |
| 364 | __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1); |
| 365 | * Save the current unexpected_handler and terminate_handler in the __cxa_exception header. |
| 366 | * Save the tinfo and dest arguments in the __cxa_exception header. |
| 367 | * Set the exception_class field in the unwind header. This is a 64-bit value |
| 368 | representing the ASCII string "XXXXC++\0", where "XXXX" is a |
| 369 | vendor-dependent string. That is, for implementations conforming to this |
| 370 | ABI, the low-order 4 bytes of this 64-bit value will be "C++\0". |
| 371 | * Increment the uncaught_exception flag. |
| 372 | * Call _Unwind_RaiseException in the system unwind library, Its argument is the |
| 373 | pointer to the thrown exception, which __cxa_throw itself received as an argument. |
| 374 | __Unwind_RaiseException begins the process of stack unwinding, described |
| 375 | in Section 2.5. In special cases, such as an inability to find a |
| 376 | handler, _Unwind_RaiseException may return. In that case, __cxa_throw |
| 377 | will call terminate, assuming that there was no handler for the |
| 378 | exception. |
| 379 | */ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 380 | LIBCXXABI_NORETURN |
| 381 | void |
| 382 | __cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*)) |
| 383 | { |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 384 | __cxa_eh_globals *globals = __cxa_get_globals(); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 385 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 386 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 387 | exception_header->unexpectedHandler = std::get_unexpected(); |
| 388 | exception_header->terminateHandler = std::get_terminate(); |
| 389 | exception_header->exceptionType = tinfo; |
| 390 | exception_header->exceptionDestructor = dest; |
| 391 | setExceptionClass(&exception_header->unwindHeader); |
| 392 | 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] | 393 | globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local |
| 394 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 395 | exception_header->unwindHeader.exception_cleanup = exception_cleanup_func; |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 396 | #if __arm__ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 397 | _Unwind_SjLj_RaiseException(&exception_header->unwindHeader); |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 398 | #else |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 399 | _Unwind_RaiseException(&exception_header->unwindHeader); |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 400 | #endif |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 401 | // If we get here, some kind of unwinding error has occurred. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 402 | failed_throw(exception_header); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 403 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 404 | |
| 405 | |
| 406 | // 2.5.3 Exception Handlers |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 407 | /* |
| 408 | The adjusted pointer is computed by the personality routine during phase 1 |
| 409 | and saved in the exception header (either __cxa_exception or |
| 410 | __cxa_dependent_exception). |
| 411 | */ |
| 412 | void* |
| 413 | __cxa_get_exception_ptr(void* unwind_exception) throw() |
| 414 | { |
| 415 | return cxa_exception_from_exception_unwind_exception |
| 416 | ( |
| 417 | static_cast<_Unwind_Exception*>(unwind_exception) |
| 418 | )->adjustedPtr; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 419 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 420 | |
| 421 | |
| 422 | /* |
| 423 | This routine: |
| 424 | * Increment's the exception's handler count. |
| 425 | * Places the exception on the stack of currently-caught exceptions if it is not |
| 426 | already there, linking the exception to the previous top of the stack. |
| 427 | * Decrements the uncaught_exception count. |
| 428 | * Returns the adjusted pointer to the exception object. |
| 429 | */ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 430 | void* |
| 431 | __cxa_begin_catch(void* unwind_exception) throw() |
| 432 | { |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 433 | __cxa_eh_globals *globals = __cxa_get_globals(); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 434 | __cxa_exception* exception_header = |
| 435 | cxa_exception_from_exception_unwind_exception |
| 436 | ( |
| 437 | static_cast<_Unwind_Exception*>(unwind_exception) |
| 438 | ); |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 439 | |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 440 | // TODO: Handle foreign exceptions? How? |
| 441 | |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 442 | // Increment the handler count, removing the flag about being rethrown |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 443 | exception_header->handlerCount = exception_header->handlerCount < 0 ? |
| 444 | -exception_header->handlerCount + 1 : exception_header->handlerCount + 1; |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 445 | |
| 446 | // place the exception on the top of the stack if it's not there. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 447 | if (exception_header != globals->caughtExceptions) { |
| 448 | exception_header->nextException = globals->caughtExceptions; |
| 449 | globals->caughtExceptions = exception_header; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 450 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 451 | |
| 452 | globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 453 | return exception_header->adjustedPtr; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 454 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 455 | |
| 456 | |
| 457 | /* |
| 458 | Upon exit for any reason, a handler must call: |
| 459 | void __cxa_end_catch (); |
| 460 | |
| 461 | This routine: |
| 462 | * Locates the most recently caught exception and decrements its handler count. |
| 463 | * 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] | 464 | * If the handler count goes down to zero, and the exception was not re-thrown |
| 465 | by throw, it locates the primary exception (which may be the same as the one |
| 466 | it's handling) and decrements its reference count. If that reference count |
| 467 | goes to zero, the function destroys the exception. In any case, if the current |
| 468 | exception is a dependent exception, it destroys that. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 469 | */ |
| 470 | void __cxa_end_catch() { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 471 | static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception), |
| 472 | "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)"); |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 473 | __cxa_eh_globals *globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 474 | __cxa_exception *exception_header = globals->caughtExceptions; |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 475 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 476 | if (NULL != exception_header) { |
Howard Hinnant | 939daa7 | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 477 | // TODO: Handle foreign exceptions? How? |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 478 | if (exception_header->handlerCount < 0) { |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 479 | // The exception has been rethrown |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 480 | if (0 == incrementHandlerCount(exception_header)) { |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 481 | // Remove from the chain of uncaught exceptions |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 482 | globals->caughtExceptions = exception_header->nextException; |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 483 | // but don't destroy |
| 484 | } |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 485 | } |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 486 | else { // The exception has not been rethrown |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 487 | if (0 == decrementHandlerCount(exception_header)) { |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 488 | // Remove from the chain of uncaught exceptions |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 489 | globals->caughtExceptions = exception_header->nextException; |
| 490 | if (isDependentException(&exception_header->unwindHeader)) { |
| 491 | // Reset exception_header to primaryException and deallocate the dependent exception |
| 492 | __cxa_dependent_exception* dep_exception_header = |
| 493 | reinterpret_cast<__cxa_dependent_exception*>(exception_header); |
| 494 | exception_header = |
| 495 | cxa_exception_from_thrown_object(dep_exception_header->primaryException); |
| 496 | __cxa_free_dependent_exception(dep_exception_header); |
Howard Hinnant | a6baba1 | 2011-12-08 19:35:18 +0000 | [diff] [blame] | 497 | } |
| 498 | // Destroy the primary exception only if its referenceCount goes to 0 |
| 499 | // (this decrement must be atomic) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 500 | __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header)); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 503 | } |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 504 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 505 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 506 | // Note: exception_header may be masquerading as a __cxa_dependent_exception |
| 507 | // and that's ok. exceptionType is there too. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 508 | std::type_info * __cxa_current_exception_type() { |
| 509 | // get the current exception |
Marshall Clow | 143cfb0 | 2011-12-22 15:45:05 +0000 | [diff] [blame] | 510 | __cxa_eh_globals *globals = __cxa_get_globals_fast(); |
| 511 | if (NULL == globals) |
| 512 | 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] | 513 | __cxa_exception *exception_header = globals->caughtExceptions; |
| 514 | if (NULL == exception_header) |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 515 | return NULL; // No current exception |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 516 | return exception_header->exceptionType; |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 517 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 518 | |
| 519 | // 2.5.4 Rethrowing Exceptions |
| 520 | /* This routine |
| 521 | * marks the exception object on top of the caughtExceptions stack |
| 522 | (in an implementation-defined way) as being rethrown. |
| 523 | * If the caughtExceptions stack is empty, it calls terminate() |
| 524 | (see [C++FDIS] [except.throw], 15.1.8). |
Howard Hinnant | 939daa7 | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 525 | * It then calls _Unwind_Resume_or_Rethrow which should not return |
| 526 | (terminate if it does). |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 527 | Note: exception_header may be masquerading as a __cxa_dependent_exception |
| 528 | and that's ok. |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 529 | */ |
| 530 | extern LIBCXXABI_NORETURN void __cxa_rethrow() { |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 531 | __cxa_eh_globals *globals = __cxa_get_globals(); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 532 | __cxa_exception *exception_header = globals->caughtExceptions; |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 533 | |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 534 | if (NULL == exception_header) // there's no current exception! |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 535 | std::terminate (); |
| 536 | |
Howard Hinnant | 939daa7 | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 537 | // TODO: Handle foreign exceptions? How? |
| 538 | |
| 539 | // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 540 | exception_header->handlerCount = -exception_header->handlerCount; |
Howard Hinnant | 939daa7 | 2011-12-12 19:11:42 +0000 | [diff] [blame] | 541 | globals->uncaughtExceptions += 1; |
| 542 | // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 543 | |
| 544 | #if __arm__ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 545 | (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader); |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 546 | #else |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 547 | (void) _Unwind_Resume_or_Rethrow (&exception_header->unwindHeader); |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 548 | #endif |
| 549 | |
| 550 | // If we get here, some kind of unwinding error has occurred. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 551 | failed_throw(exception_header); |
Marshall Clow | fb69a8b | 2011-08-15 18:06:47 +0000 | [diff] [blame] | 552 | } |
Marshall Clow | 3c54f1b | 2011-08-09 15:09:41 +0000 | [diff] [blame] | 553 | |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 554 | /* |
| 555 | If p is not null, atomically increment the referenceCount field of the |
| 556 | __cxa_exception header associated with the thrown object referred to by p. |
| 557 | */ |
| 558 | void |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 559 | __cxa_increment_exception_refcount(void* thrown_object) throw() |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 560 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 561 | if (thrown_object != NULL ) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 562 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 563 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
| 564 | __sync_add_and_fetch(&exception_header->referenceCount, 1); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | If p is not null, atomically decrement the referenceCount field of the |
| 570 | __cxa_exception header associated with the thrown object referred to by p. |
| 571 | If the referenceCount drops to zero, destroy and deallocate the exception. |
| 572 | */ |
| 573 | void |
| 574 | __cxa_decrement_exception_refcount(void* thrown_object) throw() |
| 575 | { |
| 576 | if (thrown_object != NULL ) |
| 577 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 578 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
| 579 | if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 580 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 581 | if (NULL != exception_header->exceptionDestructor) |
| 582 | exception_header->exceptionDestructor(thrown_object); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 583 | __cxa_free_exception(thrown_object); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | Returns a pointer to the thrown object (if any) at the top of the |
| 590 | caughtExceptions stack. Atommically increment the exception's referenceCount. |
| 591 | If there is no such thrown object, returns null. |
Marshall Clow | 1599756 | 2012-01-04 22:18:10 +0000 | [diff] [blame] | 592 | |
| 593 | We can use __cxa_get_globals_fast here to get the globals because if there have |
| 594 | been no exceptions thrown, ever, on this thread, we can return NULL without |
| 595 | the need to allocate the exception-handling globals. |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 596 | */ |
| 597 | void* |
| 598 | __cxa_current_primary_exception() throw() |
| 599 | { |
| 600 | // get the current exception |
Marshall Clow | e80ed7d | 2012-01-03 23:26:09 +0000 | [diff] [blame] | 601 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
Marshall Clow | 5790852 | 2012-01-03 23:10:20 +0000 | [diff] [blame] | 602 | if (NULL == globals) |
Marshall Clow | 8989e4e | 2012-01-04 14:56:09 +0000 | [diff] [blame] | 603 | return NULL; // If there are no globals, there is no exception |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 604 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 605 | if (NULL == exception_header) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 606 | return NULL; // No current exception |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 607 | if (isDependentException(&exception_header->unwindHeader)) { |
| 608 | __cxa_dependent_exception* dep_exception_header = |
| 609 | reinterpret_cast<__cxa_dependent_exception*>(exception_header); |
| 610 | exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 611 | } |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 612 | void* thrown_object = thrown_object_from_cxa_exception(exception_header); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 613 | __cxa_increment_exception_refcount(thrown_object); |
| 614 | return thrown_object; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler |
| 619 | stored in exc is called. Otherwise the referenceCount stored in the |
| 620 | primary exception is decremented, destroying the primary if necessary. |
| 621 | Finally the dependent exception is destroyed. |
| 622 | */ |
| 623 | static |
| 624 | void |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 625 | dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception) |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 626 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 627 | __cxa_dependent_exception* dep_exception_header = |
| 628 | reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1; |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 629 | if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason) |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 630 | std::__terminate(dep_exception_header->terminateHandler); |
| 631 | __cxa_decrement_exception_refcount(dep_exception_header->primaryException); |
| 632 | __cxa_free_dependent_exception(dep_exception_header); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
| 636 | If thrown_object is not null, allocate, initialize and thow a dependent |
| 637 | exception. |
| 638 | */ |
| 639 | void |
| 640 | __cxa_rethrow_primary_exception(void* thrown_object) |
| 641 | { |
| 642 | if ( thrown_object != NULL ) |
| 643 | { |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 644 | __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); |
| 645 | __cxa_dependent_exception* dep_exception_header = |
| 646 | static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception()); |
| 647 | dep_exception_header->primaryException = thrown_object; |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 648 | __cxa_increment_exception_refcount(thrown_object); |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 649 | dep_exception_header->exceptionType = exception_header->exceptionType; |
| 650 | dep_exception_header->unexpectedHandler = std::get_unexpected(); |
| 651 | dep_exception_header->terminateHandler = std::get_terminate(); |
| 652 | setDependentExceptionClass(&dep_exception_header->unwindHeader); |
Howard Hinnant | b9bf50b | 2011-12-21 23:48:05 +0000 | [diff] [blame] | 653 | __cxa_get_globals()->uncaughtExceptions += 1; |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 654 | dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup; |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 655 | #if __arm__ |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 656 | _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 657 | #else |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 658 | _Unwind_RaiseException(&dep_exception_header->unwindHeader); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 659 | #endif |
| 660 | // Some sort of unwinding error. Note that terminate is a handler. |
Howard Hinnant | 7f476b4 | 2012-01-22 19:14:27 +0000 | [diff] [blame] | 661 | __cxa_begin_catch(&dep_exception_header->unwindHeader); |
Howard Hinnant | f7b47f7 | 2011-12-21 23:32:11 +0000 | [diff] [blame] | 662 | } |
| 663 | // If we return client will call terminate() |
| 664 | } |
| 665 | |
Howard Hinnant | f710f1e | 2012-01-24 00:01:31 +0000 | [diff] [blame] | 666 | bool |
| 667 | __cxa_uncaught_exception() throw() |
| 668 | { |
| 669 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 670 | if (globals == 0) |
| 671 | return false; |
| 672 | return globals->uncaughtExceptions != 0; |
| 673 | } |
| 674 | |
Marshall Clow | e025202 | 2011-07-20 15:04:39 +0000 | [diff] [blame] | 675 | } // extern "C" |
| 676 | |
| 677 | } // abi |