blob: c43777c2fc4ff126dfa7bc9aa31fb38f618c8822 [file] [log] [blame]
Marshall Clow47c29eb2011-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
Howard Hinnant6c33e762013-06-17 18:10:34 +000018#include <cstring> // for memset
Marshall Clow47c29eb2011-07-20 15:04:39 +000019#include <pthread.h>
20
21#include "cxa_exception.hpp"
Howard Hinnante8fcf832011-12-07 21:16:40 +000022#include "cxa_handlers.hpp"
Marshall Clow47c29eb2011-07-20 15:04:39 +000023
Howard Hinnanta36fb302012-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
Marshall Clow47c29eb2011-07-20 15:04:39 +000035namespace __cxxabiv1 {
Howard Hinnant2a70c102012-01-24 18:15:20 +000036
Howard Hinnant02406852012-02-02 20:47:28 +000037#pragma GCC visibility push(default)
38
Marshall Clow47c29eb2011-07-20 15:04:39 +000039// Utility routines
Howard Hinnanta36fb302012-01-22 19:14:27 +000040static
41inline
42__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000043cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnanta36fb302012-01-22 19:14:27 +000044{
45 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clow7ac31262011-08-15 18:06:47 +000046}
Marshall Clow47c29eb2011-07-20 15:04:39 +000047
Howard Hinnanta36fb302012-01-22 19:14:27 +000048// Note: This is never called when exception_header is masquerading as a
49// __cxa_dependent_exception.
50static
51inline
52void*
Howard Hinnant7da9b962012-01-30 16:07:00 +000053thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnanta36fb302012-01-22 19:14:27 +000054{
55 return static_cast<void*>(exception_header + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +000056}
Marshall Clow47c29eb2011-07-20 15:04:39 +000057
Marshall Clow7c4652f2011-08-09 15:09:41 +000058// Get the exception object from the unwind pointer.
59// Relies on the structure layout, where the unwind pointer is right in
60// front of the user's exception object
Howard Hinnanta36fb302012-01-22 19:14:27 +000061static
62inline
63__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000064cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnanta36fb302012-01-22 19:14:27 +000065{
66 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clow7ac31262011-08-15 18:06:47 +000067}
Marshall Clow7c4652f2011-08-09 15:09:41 +000068
Howard Hinnanta36fb302012-01-22 19:14:27 +000069static
70inline
71size_t
Howard Hinnant7da9b962012-01-30 16:07:00 +000072cxa_exception_size_from_exception_thrown_size(size_t size)
Howard Hinnanta36fb302012-01-22 19:14:27 +000073{
74 return size + sizeof (__cxa_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +000075}
Marshall Clow7c4652f2011-08-09 15:09:41 +000076
Howard Hinnant7da9b962012-01-30 16:07:00 +000077static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000078 unwind_exception->exception_class = kOurExceptionClass;
79}
80
Howard Hinnant7da9b962012-01-30 16:07:00 +000081static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000082 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clow7ac31262011-08-15 18:06:47 +000083}
Marshall Clow7c4652f2011-08-09 15:09:41 +000084
85// Is it one of ours?
Howard Hinnant7da9b962012-01-30 16:07:00 +000086static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant7ab185e2012-02-01 18:15:15 +000087 return (unwind_exception->exception_class & get_vendor_and_language) ==
88 (kOurExceptionClass & get_vendor_and_language);
Marshall Clow7ac31262011-08-15 18:06:47 +000089}
Marshall Clow7c4652f2011-08-09 15:09:41 +000090
Howard Hinnant7da9b962012-01-30 16:07:00 +000091static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000092 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clow7ac31262011-08-15 18:06:47 +000093}
94
Howard Hinnant6eb54ad2011-12-08 19:35:18 +000095// This does not need to be atomic
Howard Hinnant7da9b962012-01-30 16:07:00 +000096static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +000097 return ++exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +000098}
99
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000100// This does not need to be atomic
Howard Hinnant7da9b962012-01-30 16:07:00 +0000101static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000102 return --exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +0000103}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000104
Howard Hinnant60a17882012-01-24 21:41:27 +0000105#include "fallback_malloc.ipp"
Marshall Clow47c29eb2011-07-20 15:04:39 +0000106
107// Allocate some memory from _somewhere_
Howard Hinnant7da9b962012-01-30 16:07:00 +0000108static void *do_malloc(size_t size) {
Marshall Clow7ac31262011-08-15 18:06:47 +0000109 void *ptr = std::malloc(size);
110 if (NULL == ptr) // if malloc fails, fall back to emergency stash
111 ptr = fallback_malloc(size);
Marshall Clow47c29eb2011-07-20 15:04:39 +0000112 return ptr;
Marshall Clow7ac31262011-08-15 18:06:47 +0000113}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000114
Howard Hinnant7da9b962012-01-30 16:07:00 +0000115static void do_free(void *ptr) {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000116 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clow7ac31262011-08-15 18:06:47 +0000117}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000118
Howard Hinnante8fcf832011-12-07 21:16:40 +0000119/*
Marshall Clow7c4652f2011-08-09 15:09:41 +0000120 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
121 stored in exc is called. Otherwise the exceptionDestructor stored in
122 exc is called, and then the memory for the exception is deallocated.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000123
124 This is never called for a __cxa_dependent_exception.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000125*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000126static
127void
128exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
129{
130 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +0000131 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000132 std::__terminate(exception_header->terminateHandler);
Howard Hinnant8dd7a482012-02-01 18:44:21 +0000133 // Just in case there exists a dependent exception that is pointing to this,
134 // check the reference count and only destroy this if that count goes to zero.
135 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +0000136}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000137
Howard Hinnant7da9b962012-01-30 16:07:00 +0000138static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow7c4652f2011-08-09 15:09:41 +0000139// Section 2.5.3 says:
140// * For purposes of this ABI, several things are considered exception handlers:
141// ** A terminate() call due to a throw.
142// and
143// * Upon entry, Following initialization of the catch parameter,
144// a handler must call:
Marshall Clow7ac31262011-08-15 18:06:47 +0000145// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnanta36fb302012-01-22 19:14:27 +0000146 (void) __cxa_begin_catch(&exception_header->unwindHeader);
147 std::__terminate(exception_header->terminateHandler);
Marshall Clow7ac31262011-08-15 18:06:47 +0000148}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000149
150extern "C" {
151
152// Allocate a __cxa_exception object, and zero-fill it.
153// Reserve "thrown_size" bytes on the end for the user's exception
154// object. Zero-fill the object. If memory can't be allocated, call
155// std::terminate. Return a pointer to the memory to be used for the
156// user's exception object.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000157void * __cxa_allocate_exception (size_t thrown_size) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000158 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
159 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size));
160 if (NULL == exception_header)
Marshall Clow7ac31262011-08-15 18:06:47 +0000161 std::terminate();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000162 std::memset(exception_header, 0, actual_size);
163 return thrown_object_from_cxa_exception(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000164}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000165
166
167// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000168void __cxa_free_exception (void * thrown_object) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000169 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clow7ac31262011-08-15 18:06:47 +0000170}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000171
172
173// This function shall allocate a __cxa_dependent_exception and
174// return a pointer to it. (Really to the object, not past its' end).
175// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000176void * __cxa_allocate_dependent_exception () {
Marshall Clow7ac31262011-08-15 18:06:47 +0000177 size_t actual_size = sizeof(__cxa_dependent_exception);
178 void *ptr = do_malloc(actual_size);
179 if (NULL == ptr)
180 std::terminate();
181 std::memset(ptr, 0, actual_size);
Marshall Clow47c29eb2011-07-20 15:04:39 +0000182 return ptr;
Marshall Clow7ac31262011-08-15 18:06:47 +0000183}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000184
185
186// This function shall free a dependent_exception.
187// It does not affect the reference count of the primary exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000188void __cxa_free_dependent_exception (void * dependent_exception) {
Marshall Clow7ac31262011-08-15 18:06:47 +0000189 do_free(dependent_exception);
190}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000191
Marshall Clow7c4652f2011-08-09 15:09:41 +0000192
193// 2.4.3 Throwing the Exception Object
194/*
195After constructing the exception object with the throw argument value,
196the generated code calls the __cxa_throw runtime library routine. This
197routine never returns.
198
199The __cxa_throw routine will do the following:
200
201* Obtain the __cxa_exception header from the thrown exception object address,
202which can be computed as follows:
203 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
204* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
205* Save the tinfo and dest arguments in the __cxa_exception header.
206* Set the exception_class field in the unwind header. This is a 64-bit value
207representing the ASCII string "XXXXC++\0", where "XXXX" is a
208vendor-dependent string. That is, for implementations conforming to this
209ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
210* Increment the uncaught_exception flag.
211* Call _Unwind_RaiseException in the system unwind library, Its argument is the
212pointer to the thrown exception, which __cxa_throw itself received as an argument.
213__Unwind_RaiseException begins the process of stack unwinding, described
214in Section 2.5. In special cases, such as an inability to find a
215handler, _Unwind_RaiseException may return. In that case, __cxa_throw
216will call terminate, assuming that there was no handler for the
217exception.
218*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000219LIBCXXABI_NORETURN
220void
221__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
222{
Marshall Clow7ac31262011-08-15 18:06:47 +0000223 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000224 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000225
Howard Hinnanta36fb302012-01-22 19:14:27 +0000226 exception_header->unexpectedHandler = std::get_unexpected();
227 exception_header->terminateHandler = std::get_terminate();
228 exception_header->exceptionType = tinfo;
229 exception_header->exceptionDestructor = dest;
230 setExceptionClass(&exception_header->unwindHeader);
231 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000232 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
233
Howard Hinnanta36fb302012-01-22 19:14:27 +0000234 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000235#if __arm__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000236 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000237#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000238 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000239#endif
Howard Hinnant978bfde2012-01-28 00:34:46 +0000240 // This only happens when there is no handler, or some unexpected unwinding
241 // error happens.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000242 failed_throw(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000243}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000244
245
246// 2.5.3 Exception Handlers
Howard Hinnanta36fb302012-01-22 19:14:27 +0000247/*
248The adjusted pointer is computed by the personality routine during phase 1
249 and saved in the exception header (either __cxa_exception or
250 __cxa_dependent_exception).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000251
252 Requires: exception is native
Howard Hinnanta36fb302012-01-22 19:14:27 +0000253*/
254void*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000255__cxa_get_exception_ptr(void* unwind_exception) throw()
Howard Hinnanta36fb302012-01-22 19:14:27 +0000256{
257 return cxa_exception_from_exception_unwind_exception
258 (
259 static_cast<_Unwind_Exception*>(unwind_exception)
260 )->adjustedPtr;
Marshall Clow7ac31262011-08-15 18:06:47 +0000261}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000262
Marshall Clow7c4652f2011-08-09 15:09:41 +0000263/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000264This routine can catch foreign or native exceptions. If native, the exception
265can be a primary or dependent variety. This routine may remain blissfully
266ignorant of whether the native exception is primary or dependent.
267
268If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000269* Increment's the exception's handler count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000270* Push the exception on the stack of currently-caught exceptions if it is not
271 already there (from a rethrow).
Marshall Clow7c4652f2011-08-09 15:09:41 +0000272* Decrements the uncaught_exception count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000273* Returns the adjusted pointer to the exception object, which is stored in
274 the __cxa_exception by the personality routine.
275
276If the exception is foreign, this means it did not originate from one of throw
277routines. The foreign exception does not necessarily have a __cxa_exception
278header. However we can catch it here with a catch (...), or with a call
279to terminate or unexpected during unwinding.
280* Do not try to increment the exception's handler count, we don't know where
281 it is.
282* Push the exception on the stack of currently-caught exceptions only if the
283 stack is empty. The foreign exception has no way to link to the current
284 top of stack. If the stack is not empty, call terminate. Even with an
285 empty stack, this is hacked in by pushing a pointer to an imaginary
286 __cxa_exception block in front of the foreign exception. It would be better
287 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
288 doesn't. It has a stack of __cxa_exception (which has a next* in it).
289* Do not decrement the uncaught_exception count because we didn't increment it
290 in __cxa_throw (or one of our rethrow functions).
291* If we haven't terminated, assume the exception object is just past the
292 _Unwind_Exception and return a pointer to that.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000293*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000294void*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000295__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnanta36fb302012-01-22 19:14:27 +0000296{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000297 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
298 bool native_exception = isOurExceptionClass(unwind_exception);
299 __cxa_eh_globals* globals = __cxa_get_globals();
300 // exception_header is a hackish offset from a foreign exception, but it
301 // works as long as we're careful not to try to access any __cxa_exception
302 // parts.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000303 __cxa_exception* exception_header =
304 cxa_exception_from_exception_unwind_exception
305 (
306 static_cast<_Unwind_Exception*>(unwind_exception)
307 );
Howard Hinnant7da9b962012-01-30 16:07:00 +0000308 if (native_exception)
309 {
310 // Increment the handler count, removing the flag about being rethrown
311 exception_header->handlerCount = exception_header->handlerCount < 0 ?
312 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
313 // place the exception on the top of the stack if it's not already
314 // there by a previous rethrow
315 if (exception_header != globals->caughtExceptions)
316 {
317 exception_header->nextException = globals->caughtExceptions;
318 globals->caughtExceptions = exception_header;
319 }
320 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnant7da9b962012-01-30 16:07:00 +0000321 return exception_header->adjustedPtr;
Marshall Clow7ac31262011-08-15 18:06:47 +0000322 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000323 // Else this is a foreign exception
324 // If the caughtExceptions stack is not empty, terminate
325 if (globals->caughtExceptions != 0)
326 std::terminate();
327 // Push the foreign exception on to the stack
328 globals->caughtExceptions = exception_header;
329 return unwind_exception + 1;
Marshall Clow7ac31262011-08-15 18:06:47 +0000330}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000331
332
333/*
334Upon exit for any reason, a handler must call:
335 void __cxa_end_catch ();
336
Howard Hinnant7da9b962012-01-30 16:07:00 +0000337This routine can be called for either a native or foreign exception.
338For a native exception:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000339* Locates the most recently caught exception and decrements its handler count.
340* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000341* If the handler count goes down to zero, and the exception was not re-thrown
342 by throw, it locates the primary exception (which may be the same as the one
343 it's handling) and decrements its reference count. If that reference count
344 goes to zero, the function destroys the exception. In any case, if the current
345 exception is a dependent exception, it destroys that.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000346
347For a foreign exception:
348* If it has been rethrown, there is nothing to do.
349* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000350*/
Howard Hinnant7da9b962012-01-30 16:07:00 +0000351void __cxa_end_catch()
352{
Howard Hinnanta36fb302012-01-22 19:14:27 +0000353 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
354 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnant7da9b962012-01-30 16:07:00 +0000355 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
356 __cxa_exception* exception_header = globals->caughtExceptions;
357 // If we've rethrown a foreign exception, then globals->caughtExceptions
358 // will have been made an empty stack by __cxa_rethrow() and there is
359 // nothing more to be done. Do nothing!
360 if (NULL != exception_header)
361 {
362 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
363 if (native_exception)
364 {
365 // This is a native exception
366 if (exception_header->handlerCount < 0)
367 {
368 // The exception has been rethrown by __cxa_rethrow, so don't delete it
369 if (0 == incrementHandlerCount(exception_header))
370 {
371 // Remove from the chain of uncaught exceptions
372 globals->caughtExceptions = exception_header->nextException;
373 // but don't destroy
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000374 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000375 // Keep handlerCount negative in case there are nested catch's
376 // that need to be told that this exception is rethrown. Don't
377 // erase this rethrow flag until the exception is recaught.
378 }
379 else
380 {
381 // The native exception has not been rethrown
382 if (0 == decrementHandlerCount(exception_header))
383 {
384 // Remove from the chain of uncaught exceptions
385 globals->caughtExceptions = exception_header->nextException;
386 // Destroy this exception, being careful to distinguish
387 // between dependent and primary exceptions
388 if (isDependentException(&exception_header->unwindHeader))
389 {
390 // Reset exception_header to primaryException and deallocate the dependent exception
391 __cxa_dependent_exception* dep_exception_header =
392 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
393 exception_header =
394 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
395 __cxa_free_dependent_exception(dep_exception_header);
396 }
397 // Destroy the primary exception only if its referenceCount goes to 0
398 // (this decrement must be atomic)
399 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
400 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000401 }
402 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000403 else
404 {
405 // The foreign exception has not been rethrown. Pop the stack
406 // and delete it. If there are nested catch's and they try
407 // to touch a foreign exception in any way, that is undefined
408 // behavior. They likely can't since the only way to catch
409 // a foreign exception is with catch (...)!
410 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
411 globals->caughtExceptions = 0;
412 }
Marshall Clow7c4652f2011-08-09 15:09:41 +0000413 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000414}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000415
Howard Hinnanta36fb302012-01-22 19:14:27 +0000416// Note: exception_header may be masquerading as a __cxa_dependent_exception
417// and that's ok. exceptionType is there too.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000418// However watch out for foreign exceptions. Return null for them.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000419std::type_info * __cxa_current_exception_type() {
420// get the current exception
Marshall Clow64e00972011-12-22 15:45:05 +0000421 __cxa_eh_globals *globals = __cxa_get_globals_fast();
422 if (NULL == globals)
423 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000424 __cxa_exception *exception_header = globals->caughtExceptions;
425 if (NULL == exception_header)
Marshall Clow7c4652f2011-08-09 15:09:41 +0000426 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000427 if (!isOurExceptionClass(&exception_header->unwindHeader))
428 return NULL;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000429 return exception_header->exceptionType;
Marshall Clow7ac31262011-08-15 18:06:47 +0000430}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000431
432// 2.5.4 Rethrowing Exceptions
Howard Hinnant7da9b962012-01-30 16:07:00 +0000433/* This routine can rethrow native or foreign exceptions.
434If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000435* marks the exception object on top of the caughtExceptions stack
436 (in an implementation-defined way) as being rethrown.
437* If the caughtExceptions stack is empty, it calls terminate()
438 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000439* It then calls _Unwind_RaiseException which should not return
Howard Hinnantd4028fe2011-12-12 19:11:42 +0000440 (terminate if it does).
Howard Hinnanta36fb302012-01-22 19:14:27 +0000441 Note: exception_header may be masquerading as a __cxa_dependent_exception
442 and that's ok.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000443*/
Howard Hinnant7da9b962012-01-30 16:07:00 +0000444LIBCXXABI_NORETURN
445void
446__cxa_rethrow()
447{
448 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnant7da9b962012-01-30 16:07:00 +0000449 __cxa_exception* exception_header = globals->caughtExceptions;
450 if (NULL == exception_header)
451 std::terminate(); // throw; called outside of a exception handler
452 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
453 if (native_exception)
454 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000455 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
456 exception_header->handlerCount = -exception_header->handlerCount;
457 globals->uncaughtExceptions += 1;
458 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
459 }
460 else // this is a foreign exception
461 {
462 // The only way to communicate to __cxa_end_catch that we've rethrown
463 // a foreign exception, so don't delete us, is to pop the stack here
464 // which must be empty afterwards. Then __cxa_end_catch will do
465 // nothing
466 globals->caughtExceptions = 0;
467 }
Marshall Clow7c4652f2011-08-09 15:09:41 +0000468#if __arm__
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000469 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000470#else
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000471 _Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000472#endif
473
Howard Hinnant7da9b962012-01-30 16:07:00 +0000474 // If we get here, some kind of unwinding error has occurred.
475 // There is some weird code generation bug happening with
476 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
477 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
478 __cxa_begin_catch(&exception_header->unwindHeader);
479 if (native_exception)
480 std::__terminate(exception_header->terminateHandler);
481 // Foreign exception: can't get exception_header->terminateHandler
482 std::terminate();
Marshall Clow7ac31262011-08-15 18:06:47 +0000483}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000484
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000485/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000486 If thrown_object is not null, atomically increment the referenceCount field
487 of the __cxa_exception header associated with the thrown object referred to
488 by thrown_object.
489
490 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000491*/
492void
Howard Hinnant7da9b962012-01-30 16:07:00 +0000493__cxa_increment_exception_refcount(void* thrown_object) throw()
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000494{
Howard Hinnanta36fb302012-01-22 19:14:27 +0000495 if (thrown_object != NULL )
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000496 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000497 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
498 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000499 }
500}
501
502/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000503 If thrown_object is not null, atomically decrement the referenceCount field
504 of the __cxa_exception header associated with the thrown object referred to
505 by thrown_object. If the referenceCount drops to zero, destroy and
506 deallocate the exception.
507
508 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000509*/
510void
Howard Hinnant7da9b962012-01-30 16:07:00 +0000511__cxa_decrement_exception_refcount(void* thrown_object) throw()
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000512{
513 if (thrown_object != NULL )
514 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000515 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
516 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000517 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000518 if (NULL != exception_header->exceptionDestructor)
519 exception_header->exceptionDestructor(thrown_object);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000520 __cxa_free_exception(thrown_object);
521 }
522 }
523}
524
525/*
526 Returns a pointer to the thrown object (if any) at the top of the
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000527 caughtExceptions stack. Atomically increment the exception's referenceCount.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000528 If there is no such thrown object or if the thrown object is foreign,
529 returns null.
Marshall Clow0acbd982012-01-04 22:18:10 +0000530
531 We can use __cxa_get_globals_fast here to get the globals because if there have
532 been no exceptions thrown, ever, on this thread, we can return NULL without
533 the need to allocate the exception-handling globals.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000534*/
535void*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000536__cxa_current_primary_exception() throw()
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000537{
538// get the current exception
Marshall Clowaeed3a92012-01-03 23:26:09 +0000539 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow62edc012012-01-03 23:10:20 +0000540 if (NULL == globals)
Marshall Clow2bce81b2012-01-04 14:56:09 +0000541 return NULL; // If there are no globals, there is no exception
Howard Hinnanta36fb302012-01-22 19:14:27 +0000542 __cxa_exception* exception_header = globals->caughtExceptions;
543 if (NULL == exception_header)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000544 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000545 if (!isOurExceptionClass(&exception_header->unwindHeader))
546 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000547 if (isDependentException(&exception_header->unwindHeader)) {
548 __cxa_dependent_exception* dep_exception_header =
549 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
550 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000551 }
Howard Hinnanta36fb302012-01-22 19:14:27 +0000552 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000553 __cxa_increment_exception_refcount(thrown_object);
554 return thrown_object;
555}
556
557/*
558 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
559 stored in exc is called. Otherwise the referenceCount stored in the
560 primary exception is decremented, destroying the primary if necessary.
561 Finally the dependent exception is destroyed.
562*/
563static
564void
Howard Hinnanta36fb302012-01-22 19:14:27 +0000565dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000566{
Howard Hinnanta36fb302012-01-22 19:14:27 +0000567 __cxa_dependent_exception* dep_exception_header =
568 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000569 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000570 std::__terminate(dep_exception_header->terminateHandler);
571 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
572 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000573}
574
575/*
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000576 If thrown_object is not null, allocate, initialize and throw a dependent
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000577 exception.
578*/
579void
580__cxa_rethrow_primary_exception(void* thrown_object)
581{
582 if ( thrown_object != NULL )
583 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000584 // thrown_object guaranteed to be native because
585 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnanta36fb302012-01-22 19:14:27 +0000586 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
587 __cxa_dependent_exception* dep_exception_header =
588 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
589 dep_exception_header->primaryException = thrown_object;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000590 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnanta36fb302012-01-22 19:14:27 +0000591 dep_exception_header->exceptionType = exception_header->exceptionType;
592 dep_exception_header->unexpectedHandler = std::get_unexpected();
593 dep_exception_header->terminateHandler = std::get_terminate();
594 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantdd47ca42011-12-21 23:48:05 +0000595 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000596 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000597#if __arm__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000598 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000599#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000600 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000601#endif
602 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000603 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000604 }
605 // If we return client will call terminate()
606}
607
Howard Hinnantca6514d2012-01-24 00:01:31 +0000608bool
Howard Hinnant7da9b962012-01-30 16:07:00 +0000609__cxa_uncaught_exception() throw()
Howard Hinnantca6514d2012-01-24 00:01:31 +0000610{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000611 // This does not report foreign exceptions in flight
Howard Hinnantca6514d2012-01-24 00:01:31 +0000612 __cxa_eh_globals* globals = __cxa_get_globals_fast();
613 if (globals == 0)
614 return false;
615 return globals->uncaughtExceptions != 0;
616}
617
Marshall Clow47c29eb2011-07-20 15:04:39 +0000618} // extern "C"
619
Howard Hinnant02406852012-02-02 20:47:28 +0000620#pragma GCC visibility pop
621
Marshall Clow47c29eb2011-07-20 15:04:39 +0000622} // abi