blob: df10b28a468d66cd6d0b8e4bf198843b5e2d4278 [file] [log] [blame]
Marshall Clowe0252022011-07-20 15:04:39 +00001//===------------------------- 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 Hinnantb80931e2011-12-07 21:16:40 +000022#include "cxa_handlers.hpp"
Marshall Clowe0252022011-07-20 15:04:39 +000023
Howard Hinnant7f476b42012-01-22 19:14:27 +000024// +---------------------------+-----------------------------+---------------+
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 Clowe0252022011-07-20 15:04:39 +000036namespace __cxxabiv1 {
Howard Hinnantb94f2252012-01-24 18:15:20 +000037
Marshall Clowe0252022011-07-20 15:04:39 +000038// Utility routines
Howard Hinnant7f476b42012-01-22 19:14:27 +000039static
40inline
41__cxa_exception*
42cxa_exception_from_thrown_object(void* thrown_object) throw()
43{
44 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000045}
Marshall Clowe0252022011-07-20 15:04:39 +000046
Howard Hinnant7f476b42012-01-22 19:14:27 +000047// Note: This is never called when exception_header is masquerading as a
48// __cxa_dependent_exception.
49static
50inline
51void*
52thrown_object_from_cxa_exception(__cxa_exception* exception_header) throw()
53{
54 return static_cast<void*>(exception_header + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000055}
Marshall Clowe0252022011-07-20 15:04:39 +000056
Marshall Clow3c54f1b2011-08-09 15:09:41 +000057// 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 Hinnant7f476b42012-01-22 19:14:27 +000060static
61inline
62__cxa_exception*
63cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) throw()
64{
65 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clowfb69a8b2011-08-15 18:06:47 +000066}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000067
Howard Hinnant7f476b42012-01-22 19:14:27 +000068static
69inline
70size_t
71cxa_exception_size_from_exception_thrown_size(size_t size) throw()
72{
73 return size + sizeof (__cxa_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000074}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000075
Howard Hinnant7f476b42012-01-22 19:14:27 +000076static void setExceptionClass(_Unwind_Exception* unwind_exception) throw() {
77 unwind_exception->exception_class = kOurExceptionClass;
78}
79
80static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) throw() {
81 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000082}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000083
84// Is it one of ours?
Howard Hinnant7f476b42012-01-22 19:14:27 +000085static bool isOurExceptionClass(_Unwind_Exception* unwind_exception) throw() {
86 return(unwind_exception->exception_class == kOurExceptionClass)||
87 (unwind_exception->exception_class == kOurDependentExceptionClass);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000088}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000089
Howard Hinnant7f476b42012-01-22 19:14:27 +000090static bool isDependentException(_Unwind_Exception* unwind_exception) throw() {
91 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000092}
93
Howard Hinnanta6baba12011-12-08 19:35:18 +000094// This does not need to be atomic
95static inline int incrementHandlerCount(__cxa_exception *exception) throw() {
96 return ++exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000097}
98
Howard Hinnanta6baba12011-12-08 19:35:18 +000099// This does not need to be atomic
100static inline int decrementHandlerCount(__cxa_exception *exception) throw() {
101 return --exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000102}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000103
Howard Hinnantd5c461f2012-01-24 21:02:21 +0000104// 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
114namespace {
115
116static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER;
117
118class mutexor {
119public:
120 mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); }
121 ~mutexor () { pthread_mutex_unlock ( mtx_ ); }
122private:
123 mutexor ( const mutexor &rhs );
124 mutexor & operator = ( const mutexor &rhs );
125 pthread_mutex_t *mtx_;
126 };
127
128
129#define HEAP_SIZE 512
130char heap [ HEAP_SIZE ];
131
132typedef unsigned short heap_offset;
133typedef unsigned short heap_size;
134
135struct heap_node {
136 heap_offset next_node; // offset into heap
137 heap_size len; // size in units of "sizeof(heap_node)"
138};
139
140static const heap_node *list_end = (heap_node *) ( &heap [ HEAP_SIZE ] ); // one past the end of the heap
141static heap_node *freelist = NULL;
142
143heap_node *node_from_offset ( const heap_offset offset ) throw()
144 { return (heap_node *) ( heap + ( offset * sizeof (heap_node))); }
145
146heap_offset offset_from_node ( const heap_node *ptr ) throw()
147 { return (((char *) ptr ) - heap) / sizeof (heap_node); }
148
149void 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
156size_t alloc_size (size_t len) throw()
157 { return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; }
158
159bool is_fallback_ptr ( void *ptr ) throw()
160 { return ptr >= heap && ptr < ( heap + HEAP_SIZE ); }
161
162void *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
197heap_node *after ( struct heap_node *p ) throw() { return p + p->len; }
198
199void 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
248size_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 Clowe0252022011-07-20 15:04:39 +0000265
266// Allocate some memory from _somewhere_
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000267static 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 Clowe0252022011-07-20 15:04:39 +0000271 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000272}
Marshall Clowe0252022011-07-20 15:04:39 +0000273
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000274static void do_free(void *ptr) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000275 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000276}
Marshall Clowe0252022011-07-20 15:04:39 +0000277
Howard Hinnantb80931e2011-12-07 21:16:40 +0000278/*
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000279 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 Hinnant7f476b42012-01-22 19:14:27 +0000282
283 This is never called for a __cxa_dependent_exception.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000284*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000285static
286void
287exception_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 Clowfb69a8b2011-08-15 18:06:47 +0000290 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000291 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 Clowfb69a8b2011-08-15 18:06:47 +0000296 __cxa_free_exception(thrown_object);
297}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000298
Howard Hinnant7f476b42012-01-22 19:14:27 +0000299static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) throw() {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000300// 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 Clowfb69a8b2011-08-15 18:06:47 +0000306// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnant7f476b42012-01-22 19:14:27 +0000307 (void) __cxa_begin_catch(&exception_header->unwindHeader);
308 std::__terminate(exception_header->terminateHandler);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000309}
Marshall Clowe0252022011-07-20 15:04:39 +0000310
311extern "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.
318void * __cxa_allocate_exception (size_t thrown_size) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000319 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 Clowfb69a8b2011-08-15 18:06:47 +0000322 std::terminate();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000323 std::memset(exception_header, 0, actual_size);
324 return thrown_object_from_cxa_exception(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000325}
Marshall Clowe0252022011-07-20 15:04:39 +0000326
327
328// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000329void __cxa_free_exception (void * thrown_object) throw() {
330 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000331}
Marshall Clowe0252022011-07-20 15:04:39 +0000332
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.
337void * __cxa_allocate_dependent_exception () throw() {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000338 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 Clowe0252022011-07-20 15:04:39 +0000343 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000344}
Marshall Clowe0252022011-07-20 15:04:39 +0000345
346
347// This function shall free a dependent_exception.
348// It does not affect the reference count of the primary exception.
349void __cxa_free_dependent_exception (void * dependent_exception) throw() {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000350 do_free(dependent_exception);
351}
Marshall Clowe0252022011-07-20 15:04:39 +0000352
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000353
354// 2.4.3 Throwing the Exception Object
355/*
356After constructing the exception object with the throw argument value,
357the generated code calls the __cxa_throw runtime library routine. This
358routine never returns.
359
360The __cxa_throw routine will do the following:
361
362* Obtain the __cxa_exception header from the thrown exception object address,
363which 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
368representing the ASCII string "XXXXC++\0", where "XXXX" is a
369vendor-dependent string. That is, for implementations conforming to this
370ABI, 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
373pointer to the thrown exception, which __cxa_throw itself received as an argument.
374__Unwind_RaiseException begins the process of stack unwinding, described
375in Section 2.5. In special cases, such as an inability to find a
376handler, _Unwind_RaiseException may return. In that case, __cxa_throw
377will call terminate, assuming that there was no handler for the
378exception.
379*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000380LIBCXXABI_NORETURN
381void
382__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
383{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000384 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000385 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000386
Howard Hinnant7f476b42012-01-22 19:14:27 +0000387 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 Clow3c54f1b2011-08-09 15:09:41 +0000393 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
394
Howard Hinnant7f476b42012-01-22 19:14:27 +0000395 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000396#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000397 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000398#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000399 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000400#endif
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000401// If we get here, some kind of unwinding error has occurred.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000402 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000403}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000404
405
406// 2.5.3 Exception Handlers
Howard Hinnant7f476b42012-01-22 19:14:27 +0000407/*
408The 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*/
412void*
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 Clowfb69a8b2011-08-15 18:06:47 +0000419}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000420
421
422/*
423This 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 Hinnant7f476b42012-01-22 19:14:27 +0000430void*
431__cxa_begin_catch(void* unwind_exception) throw()
432{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000433 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000434 __cxa_exception* exception_header =
435 cxa_exception_from_exception_unwind_exception
436 (
437 static_cast<_Unwind_Exception*>(unwind_exception)
438 );
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000439
Howard Hinnanta6baba12011-12-08 19:35:18 +0000440// TODO: Handle foreign exceptions? How?
441
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000442// Increment the handler count, removing the flag about being rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000443 exception_header->handlerCount = exception_header->handlerCount < 0 ?
444 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000445
446// place the exception on the top of the stack if it's not there.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000447 if (exception_header != globals->caughtExceptions) {
448 exception_header->nextException = globals->caughtExceptions;
449 globals->caughtExceptions = exception_header;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000450 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000451
452 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnant7f476b42012-01-22 19:14:27 +0000453 return exception_header->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000454}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000455
456
457/*
458Upon exit for any reason, a handler must call:
459 void __cxa_end_catch ();
460
461This 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 Hinnanta6baba12011-12-08 19:35:18 +0000464* 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 Clow3c54f1b2011-08-09 15:09:41 +0000469*/
470void __cxa_end_catch() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000471 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
472 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnanta6baba12011-12-08 19:35:18 +0000473 __cxa_eh_globals *globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
Howard Hinnant7f476b42012-01-22 19:14:27 +0000474 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000475
Howard Hinnant7f476b42012-01-22 19:14:27 +0000476 if (NULL != exception_header) {
Howard Hinnant939daa72011-12-12 19:11:42 +0000477 // TODO: Handle foreign exceptions? How?
Howard Hinnant7f476b42012-01-22 19:14:27 +0000478 if (exception_header->handlerCount < 0) {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000479 // The exception has been rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000480 if (0 == incrementHandlerCount(exception_header)) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000481 // Remove from the chain of uncaught exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000482 globals->caughtExceptions = exception_header->nextException;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000483 // but don't destroy
484 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000485 }
Howard Hinnanta6baba12011-12-08 19:35:18 +0000486 else { // The exception has not been rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000487 if (0 == decrementHandlerCount(exception_header)) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000488 // Remove from the chain of uncaught exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000489 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 Hinnanta6baba12011-12-08 19:35:18 +0000497 }
498 // Destroy the primary exception only if its referenceCount goes to 0
499 // (this decrement must be atomic)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000500 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000501 }
502 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000503 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000504}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000505
Howard Hinnant7f476b42012-01-22 19:14:27 +0000506// Note: exception_header may be masquerading as a __cxa_dependent_exception
507// and that's ok. exceptionType is there too.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000508std::type_info * __cxa_current_exception_type() {
509// get the current exception
Marshall Clow143cfb02011-12-22 15:45:05 +0000510 __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 Hinnant7f476b42012-01-22 19:14:27 +0000513 __cxa_exception *exception_header = globals->caughtExceptions;
514 if (NULL == exception_header)
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000515 return NULL; // No current exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000516 return exception_header->exceptionType;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000517}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000518
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 Hinnant939daa72011-12-12 19:11:42 +0000525* It then calls _Unwind_Resume_or_Rethrow which should not return
526 (terminate if it does).
Howard Hinnant7f476b42012-01-22 19:14:27 +0000527 Note: exception_header may be masquerading as a __cxa_dependent_exception
528 and that's ok.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000529*/
530extern LIBCXXABI_NORETURN void __cxa_rethrow() {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000531 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000532 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000533
Howard Hinnant7f476b42012-01-22 19:14:27 +0000534 if (NULL == exception_header) // there's no current exception!
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000535 std::terminate ();
536
Howard Hinnant939daa72011-12-12 19:11:42 +0000537// TODO: Handle foreign exceptions? How?
538
539// Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000540 exception_header->handlerCount = -exception_header->handlerCount;
Howard Hinnant939daa72011-12-12 19:11:42 +0000541 globals->uncaughtExceptions += 1;
542// __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000543
544#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000545 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000546#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000547 (void) _Unwind_Resume_or_Rethrow (&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000548#endif
549
550// If we get here, some kind of unwinding error has occurred.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000551 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000552}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000553
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000554/*
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*/
558void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000559__cxa_increment_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000560{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000561 if (thrown_object != NULL )
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000562 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000563 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
564 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000565 }
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*/
573void
574__cxa_decrement_exception_refcount(void* thrown_object) throw()
575{
576 if (thrown_object != NULL )
577 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000578 __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 Hinnantf7b47f72011-12-21 23:32:11 +0000580 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000581 if (NULL != exception_header->exceptionDestructor)
582 exception_header->exceptionDestructor(thrown_object);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000583 __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 Clow15997562012-01-04 22:18:10 +0000592
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 Hinnantf7b47f72011-12-21 23:32:11 +0000596*/
597void*
598__cxa_current_primary_exception() throw()
599{
600// get the current exception
Marshall Clowe80ed7d2012-01-03 23:26:09 +0000601 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow57908522012-01-03 23:10:20 +0000602 if (NULL == globals)
Marshall Clow8989e4e2012-01-04 14:56:09 +0000603 return NULL; // If there are no globals, there is no exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000604 __cxa_exception* exception_header = globals->caughtExceptions;
605 if (NULL == exception_header)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000606 return NULL; // No current exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000607 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 Hinnantf7b47f72011-12-21 23:32:11 +0000611 }
Howard Hinnant7f476b42012-01-22 19:14:27 +0000612 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000613 __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*/
623static
624void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000625dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000626{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000627 __cxa_dependent_exception* dep_exception_header =
628 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000629 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000630 std::__terminate(dep_exception_header->terminateHandler);
631 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
632 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000633}
634
635/*
636 If thrown_object is not null, allocate, initialize and thow a dependent
637 exception.
638*/
639void
640__cxa_rethrow_primary_exception(void* thrown_object)
641{
642 if ( thrown_object != NULL )
643 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000644 __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 Hinnantf7b47f72011-12-21 23:32:11 +0000648 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnant7f476b42012-01-22 19:14:27 +0000649 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 Hinnantb9bf50b2011-12-21 23:48:05 +0000653 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000654 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000655#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000656 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000657#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000658 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000659#endif
660 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000661 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000662 }
663 // If we return client will call terminate()
664}
665
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000666bool
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 Clowe0252022011-07-20 15:04:39 +0000675} // extern "C"
676
677} // abi