blob: 96868f5aff2d876d397052ca5a5674835e410142 [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 {
Marshall Clow3c54f1b2011-08-09 15:09:41 +000037static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0
38static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1
39
Marshall Clowe0252022011-07-20 15:04:39 +000040// Utility routines
Howard Hinnant7f476b42012-01-22 19:14:27 +000041static
42inline
43__cxa_exception*
44cxa_exception_from_thrown_object(void* thrown_object) throw()
45{
46 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000047}
Marshall Clowe0252022011-07-20 15:04:39 +000048
Howard Hinnant7f476b42012-01-22 19:14:27 +000049// Note: This is never called when exception_header is masquerading as a
50// __cxa_dependent_exception.
51static
52inline
53void*
54thrown_object_from_cxa_exception(__cxa_exception* exception_header) throw()
55{
56 return static_cast<void*>(exception_header + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000057}
Marshall Clowe0252022011-07-20 15:04:39 +000058
Marshall Clow3c54f1b2011-08-09 15:09:41 +000059// Get the exception object from the unwind pointer.
60// Relies on the structure layout, where the unwind pointer is right in
61// front of the user's exception object
Howard Hinnant7f476b42012-01-22 19:14:27 +000062static
63inline
64__cxa_exception*
65cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) throw()
66{
67 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clowfb69a8b2011-08-15 18:06:47 +000068}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000069
Howard Hinnant7f476b42012-01-22 19:14:27 +000070static
71inline
72size_t
73cxa_exception_size_from_exception_thrown_size(size_t size) throw()
74{
75 return size + sizeof (__cxa_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000076}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000077
Howard Hinnant7f476b42012-01-22 19:14:27 +000078static void setExceptionClass(_Unwind_Exception* unwind_exception) throw() {
79 unwind_exception->exception_class = kOurExceptionClass;
80}
81
82static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) throw() {
83 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000084}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000085
86// Is it one of ours?
Howard Hinnant7f476b42012-01-22 19:14:27 +000087static bool isOurExceptionClass(_Unwind_Exception* unwind_exception) throw() {
88 return(unwind_exception->exception_class == kOurExceptionClass)||
89 (unwind_exception->exception_class == kOurDependentExceptionClass);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000090}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000091
Howard Hinnant7f476b42012-01-22 19:14:27 +000092static bool isDependentException(_Unwind_Exception* unwind_exception) throw() {
93 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000094}
95
Howard Hinnanta6baba12011-12-08 19:35:18 +000096// This does not need to be atomic
97static inline int incrementHandlerCount(__cxa_exception *exception) throw() {
98 return ++exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000099}
100
Howard Hinnanta6baba12011-12-08 19:35:18 +0000101// This does not need to be atomic
102static inline int decrementHandlerCount(__cxa_exception *exception) throw() {
103 return --exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000104}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000105
Marshall Clowe0252022011-07-20 15:04:39 +0000106#include "fallback_malloc.cpp"
107
108// Allocate some memory from _somewhere_
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000109static void *do_malloc(size_t size) throw() {
110 void *ptr = std::malloc(size);
111 if (NULL == ptr) // if malloc fails, fall back to emergency stash
112 ptr = fallback_malloc(size);
Marshall Clowe0252022011-07-20 15:04:39 +0000113 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000114}
Marshall Clowe0252022011-07-20 15:04:39 +0000115
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000116static void do_free(void *ptr) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000117 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000118}
Marshall Clowe0252022011-07-20 15:04:39 +0000119
Howard Hinnantb80931e2011-12-07 21:16:40 +0000120/*
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000121 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
122 stored in exc is called. Otherwise the exceptionDestructor stored in
123 exc is called, and then the memory for the exception is deallocated.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000124
125 This is never called for a __cxa_dependent_exception.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000126*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000127static
128void
129exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
130{
131 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000132 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000133 std::__terminate(exception_header->terminateHandler);
134
135 void * thrown_object = thrown_object_from_cxa_exception(exception_header);
136 if (NULL != exception_header->exceptionDestructor)
137 exception_header->exceptionDestructor(thrown_object);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000138 __cxa_free_exception(thrown_object);
139}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000140
Howard Hinnant7f476b42012-01-22 19:14:27 +0000141static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) throw() {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000142// Section 2.5.3 says:
143// * For purposes of this ABI, several things are considered exception handlers:
144// ** A terminate() call due to a throw.
145// and
146// * Upon entry, Following initialization of the catch parameter,
147// a handler must call:
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000148// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnant7f476b42012-01-22 19:14:27 +0000149 (void) __cxa_begin_catch(&exception_header->unwindHeader);
150 std::__terminate(exception_header->terminateHandler);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000151}
Marshall Clowe0252022011-07-20 15:04:39 +0000152
153extern "C" {
154
155// Allocate a __cxa_exception object, and zero-fill it.
156// Reserve "thrown_size" bytes on the end for the user's exception
157// object. Zero-fill the object. If memory can't be allocated, call
158// std::terminate. Return a pointer to the memory to be used for the
159// user's exception object.
160void * __cxa_allocate_exception (size_t thrown_size) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000161 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
162 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size));
163 if (NULL == exception_header)
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000164 std::terminate();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000165 std::memset(exception_header, 0, actual_size);
166 return thrown_object_from_cxa_exception(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000167}
Marshall Clowe0252022011-07-20 15:04:39 +0000168
169
170// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000171void __cxa_free_exception (void * thrown_object) throw() {
172 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000173}
Marshall Clowe0252022011-07-20 15:04:39 +0000174
175
176// This function shall allocate a __cxa_dependent_exception and
177// return a pointer to it. (Really to the object, not past its' end).
178// Otherwise, it will work like __cxa_allocate_exception.
179void * __cxa_allocate_dependent_exception () throw() {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000180 size_t actual_size = sizeof(__cxa_dependent_exception);
181 void *ptr = do_malloc(actual_size);
182 if (NULL == ptr)
183 std::terminate();
184 std::memset(ptr, 0, actual_size);
Marshall Clowe0252022011-07-20 15:04:39 +0000185 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000186}
Marshall Clowe0252022011-07-20 15:04:39 +0000187
188
189// This function shall free a dependent_exception.
190// It does not affect the reference count of the primary exception.
191void __cxa_free_dependent_exception (void * dependent_exception) throw() {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000192 do_free(dependent_exception);
193}
Marshall Clowe0252022011-07-20 15:04:39 +0000194
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000195
196// 2.4.3 Throwing the Exception Object
197/*
198After constructing the exception object with the throw argument value,
199the generated code calls the __cxa_throw runtime library routine. This
200routine never returns.
201
202The __cxa_throw routine will do the following:
203
204* Obtain the __cxa_exception header from the thrown exception object address,
205which can be computed as follows:
206 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
207* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
208* Save the tinfo and dest arguments in the __cxa_exception header.
209* Set the exception_class field in the unwind header. This is a 64-bit value
210representing the ASCII string "XXXXC++\0", where "XXXX" is a
211vendor-dependent string. That is, for implementations conforming to this
212ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
213* Increment the uncaught_exception flag.
214* Call _Unwind_RaiseException in the system unwind library, Its argument is the
215pointer to the thrown exception, which __cxa_throw itself received as an argument.
216__Unwind_RaiseException begins the process of stack unwinding, described
217in Section 2.5. In special cases, such as an inability to find a
218handler, _Unwind_RaiseException may return. In that case, __cxa_throw
219will call terminate, assuming that there was no handler for the
220exception.
221*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000222LIBCXXABI_NORETURN
223void
224__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
225{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000226 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000227 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000228
Howard Hinnant7f476b42012-01-22 19:14:27 +0000229 exception_header->unexpectedHandler = std::get_unexpected();
230 exception_header->terminateHandler = std::get_terminate();
231 exception_header->exceptionType = tinfo;
232 exception_header->exceptionDestructor = dest;
233 setExceptionClass(&exception_header->unwindHeader);
234 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000235 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
236
Howard Hinnant7f476b42012-01-22 19:14:27 +0000237 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000238#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000239 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000240#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000241 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000242#endif
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000243// If we get here, some kind of unwinding error has occurred.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000244 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000245}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000246
247
248// 2.5.3 Exception Handlers
Howard Hinnant7f476b42012-01-22 19:14:27 +0000249/*
250The adjusted pointer is computed by the personality routine during phase 1
251 and saved in the exception header (either __cxa_exception or
252 __cxa_dependent_exception).
253*/
254void*
255__cxa_get_exception_ptr(void* unwind_exception) throw()
256{
257 return cxa_exception_from_exception_unwind_exception
258 (
259 static_cast<_Unwind_Exception*>(unwind_exception)
260 )->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000261}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000262
263
264/*
265This routine:
266* Increment's the exception's handler count.
267* Places the exception on the stack of currently-caught exceptions if it is not
268 already there, linking the exception to the previous top of the stack.
269* Decrements the uncaught_exception count.
270* Returns the adjusted pointer to the exception object.
271*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000272void*
273__cxa_begin_catch(void* unwind_exception) throw()
274{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000275 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000276 __cxa_exception* exception_header =
277 cxa_exception_from_exception_unwind_exception
278 (
279 static_cast<_Unwind_Exception*>(unwind_exception)
280 );
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000281
Howard Hinnanta6baba12011-12-08 19:35:18 +0000282// TODO: Handle foreign exceptions? How?
283
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000284// Increment the handler count, removing the flag about being rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000285 exception_header->handlerCount = exception_header->handlerCount < 0 ?
286 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000287
288// place the exception on the top of the stack if it's not there.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000289 if (exception_header != globals->caughtExceptions) {
290 exception_header->nextException = globals->caughtExceptions;
291 globals->caughtExceptions = exception_header;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000292 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000293
294 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnant7f476b42012-01-22 19:14:27 +0000295 return exception_header->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000296}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000297
298
299/*
300Upon exit for any reason, a handler must call:
301 void __cxa_end_catch ();
302
303This routine:
304* Locates the most recently caught exception and decrements its handler count.
305* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnanta6baba12011-12-08 19:35:18 +0000306* If the handler count goes down to zero, and the exception was not re-thrown
307 by throw, it locates the primary exception (which may be the same as the one
308 it's handling) and decrements its reference count. If that reference count
309 goes to zero, the function destroys the exception. In any case, if the current
310 exception is a dependent exception, it destroys that.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000311*/
312void __cxa_end_catch() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000313 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
314 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnanta6baba12011-12-08 19:35:18 +0000315 __cxa_eh_globals *globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
Howard Hinnant7f476b42012-01-22 19:14:27 +0000316 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000317
Howard Hinnant7f476b42012-01-22 19:14:27 +0000318 if (NULL != exception_header) {
Howard Hinnant939daa72011-12-12 19:11:42 +0000319 // TODO: Handle foreign exceptions? How?
Howard Hinnant7f476b42012-01-22 19:14:27 +0000320 if (exception_header->handlerCount < 0) {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000321 // The exception has been rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000322 if (0 == incrementHandlerCount(exception_header)) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000323 // Remove from the chain of uncaught exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000324 globals->caughtExceptions = exception_header->nextException;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000325 // but don't destroy
326 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000327 }
Howard Hinnanta6baba12011-12-08 19:35:18 +0000328 else { // The exception has not been rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000329 if (0 == decrementHandlerCount(exception_header)) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000330 // Remove from the chain of uncaught exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000331 globals->caughtExceptions = exception_header->nextException;
332 if (isDependentException(&exception_header->unwindHeader)) {
333 // Reset exception_header to primaryException and deallocate the dependent exception
334 __cxa_dependent_exception* dep_exception_header =
335 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
336 exception_header =
337 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
338 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000339 }
340 // Destroy the primary exception only if its referenceCount goes to 0
341 // (this decrement must be atomic)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000342 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000343 }
344 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000345 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000346}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000347
Howard Hinnant7f476b42012-01-22 19:14:27 +0000348// Note: exception_header may be masquerading as a __cxa_dependent_exception
349// and that's ok. exceptionType is there too.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000350std::type_info * __cxa_current_exception_type() {
351// get the current exception
Marshall Clow143cfb02011-12-22 15:45:05 +0000352 __cxa_eh_globals *globals = __cxa_get_globals_fast();
353 if (NULL == globals)
354 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000355 __cxa_exception *exception_header = globals->caughtExceptions;
356 if (NULL == exception_header)
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000357 return NULL; // No current exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000358 return exception_header->exceptionType;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000359}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000360
361// 2.5.4 Rethrowing Exceptions
362/* This routine
363* marks the exception object on top of the caughtExceptions stack
364 (in an implementation-defined way) as being rethrown.
365* If the caughtExceptions stack is empty, it calls terminate()
366 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant939daa72011-12-12 19:11:42 +0000367* It then calls _Unwind_Resume_or_Rethrow which should not return
368 (terminate if it does).
Howard Hinnant7f476b42012-01-22 19:14:27 +0000369 Note: exception_header may be masquerading as a __cxa_dependent_exception
370 and that's ok.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000371*/
372extern LIBCXXABI_NORETURN void __cxa_rethrow() {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000373 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000374 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000375
Howard Hinnant7f476b42012-01-22 19:14:27 +0000376 if (NULL == exception_header) // there's no current exception!
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000377 std::terminate ();
378
Howard Hinnant939daa72011-12-12 19:11:42 +0000379// TODO: Handle foreign exceptions? How?
380
381// Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000382 exception_header->handlerCount = -exception_header->handlerCount;
Howard Hinnant939daa72011-12-12 19:11:42 +0000383 globals->uncaughtExceptions += 1;
384// __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000385
386#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000387 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000388#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000389 (void) _Unwind_Resume_or_Rethrow (&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000390#endif
391
392// If we get here, some kind of unwinding error has occurred.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000393 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000394}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000395
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000396/*
397 If p is not null, atomically increment the referenceCount field of the
398 __cxa_exception header associated with the thrown object referred to by p.
399*/
400void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000401__cxa_increment_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000402{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000403 if (thrown_object != NULL )
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000404 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000405 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
406 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000407 }
408}
409
410/*
411 If p is not null, atomically decrement the referenceCount field of the
412 __cxa_exception header associated with the thrown object referred to by p.
413 If the referenceCount drops to zero, destroy and deallocate the exception.
414*/
415void
416__cxa_decrement_exception_refcount(void* thrown_object) throw()
417{
418 if (thrown_object != NULL )
419 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000420 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
421 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000422 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000423 if (NULL != exception_header->exceptionDestructor)
424 exception_header->exceptionDestructor(thrown_object);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000425 __cxa_free_exception(thrown_object);
426 }
427 }
428}
429
430/*
431 Returns a pointer to the thrown object (if any) at the top of the
432 caughtExceptions stack. Atommically increment the exception's referenceCount.
433 If there is no such thrown object, returns null.
Marshall Clow15997562012-01-04 22:18:10 +0000434
435 We can use __cxa_get_globals_fast here to get the globals because if there have
436 been no exceptions thrown, ever, on this thread, we can return NULL without
437 the need to allocate the exception-handling globals.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000438*/
439void*
440__cxa_current_primary_exception() throw()
441{
442// get the current exception
Marshall Clowe80ed7d2012-01-03 23:26:09 +0000443 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow57908522012-01-03 23:10:20 +0000444 if (NULL == globals)
Marshall Clow8989e4e2012-01-04 14:56:09 +0000445 return NULL; // If there are no globals, there is no exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000446 __cxa_exception* exception_header = globals->caughtExceptions;
447 if (NULL == exception_header)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000448 return NULL; // No current exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000449 if (isDependentException(&exception_header->unwindHeader)) {
450 __cxa_dependent_exception* dep_exception_header =
451 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
452 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000453 }
Howard Hinnant7f476b42012-01-22 19:14:27 +0000454 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000455 __cxa_increment_exception_refcount(thrown_object);
456 return thrown_object;
457}
458
459/*
460 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
461 stored in exc is called. Otherwise the referenceCount stored in the
462 primary exception is decremented, destroying the primary if necessary.
463 Finally the dependent exception is destroyed.
464*/
465static
466void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000467dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000468{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000469 __cxa_dependent_exception* dep_exception_header =
470 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000471 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000472 std::__terminate(dep_exception_header->terminateHandler);
473 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
474 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000475}
476
477/*
478 If thrown_object is not null, allocate, initialize and thow a dependent
479 exception.
480*/
481void
482__cxa_rethrow_primary_exception(void* thrown_object)
483{
484 if ( thrown_object != NULL )
485 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000486 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
487 __cxa_dependent_exception* dep_exception_header =
488 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
489 dep_exception_header->primaryException = thrown_object;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000490 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnant7f476b42012-01-22 19:14:27 +0000491 dep_exception_header->exceptionType = exception_header->exceptionType;
492 dep_exception_header->unexpectedHandler = std::get_unexpected();
493 dep_exception_header->terminateHandler = std::get_terminate();
494 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantb9bf50b2011-12-21 23:48:05 +0000495 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000496 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000497#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000498 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000499#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000500 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000501#endif
502 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000503 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000504 }
505 // If we return client will call terminate()
506}
507
Marshall Clowe0252022011-07-20 15:04:39 +0000508} // extern "C"
509
510} // abi