blob: b6b915384587ac864a3acf06dfc9f1355d4d089d [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
Marshall Clowe0252022011-07-20 15:04:39 +000035namespace __cxxabiv1 {
Howard Hinnantb94f2252012-01-24 18:15:20 +000036
Marshall Clowe0252022011-07-20 15:04:39 +000037// Utility routines
Howard Hinnant7f476b42012-01-22 19:14:27 +000038static
39inline
40__cxa_exception*
Howard Hinnantb50cda72012-01-30 16:07:00 +000041cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnant7f476b42012-01-22 19:14:27 +000042{
43 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000044}
Marshall Clowe0252022011-07-20 15:04:39 +000045
Howard Hinnant7f476b42012-01-22 19:14:27 +000046// Note: This is never called when exception_header is masquerading as a
47// __cxa_dependent_exception.
48static
49inline
50void*
Howard Hinnantb50cda72012-01-30 16:07:00 +000051thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnant7f476b42012-01-22 19:14:27 +000052{
53 return static_cast<void*>(exception_header + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000054}
Marshall Clowe0252022011-07-20 15:04:39 +000055
Marshall Clow3c54f1b2011-08-09 15:09:41 +000056// Get the exception object from the unwind pointer.
57// Relies on the structure layout, where the unwind pointer is right in
58// front of the user's exception object
Howard Hinnant7f476b42012-01-22 19:14:27 +000059static
60inline
61__cxa_exception*
Howard Hinnantb50cda72012-01-30 16:07:00 +000062cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnant7f476b42012-01-22 19:14:27 +000063{
64 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clowfb69a8b2011-08-15 18:06:47 +000065}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000066
Howard Hinnant7f476b42012-01-22 19:14:27 +000067static
68inline
69size_t
Howard Hinnantb50cda72012-01-30 16:07:00 +000070cxa_exception_size_from_exception_thrown_size(size_t size)
Howard Hinnant7f476b42012-01-22 19:14:27 +000071{
72 return size + sizeof (__cxa_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000073}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000074
Howard Hinnantb50cda72012-01-30 16:07:00 +000075static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000076 unwind_exception->exception_class = kOurExceptionClass;
77}
78
Howard Hinnantb50cda72012-01-30 16:07:00 +000079static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000080 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000081}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000082
83// Is it one of ours?
Howard Hinnantb50cda72012-01-30 16:07:00 +000084static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant1f5bb2c2012-02-01 18:15:15 +000085 return (unwind_exception->exception_class & get_vendor_and_language) ==
86 (kOurExceptionClass & get_vendor_and_language);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000087}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000088
Howard Hinnantb50cda72012-01-30 16:07:00 +000089static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000090 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000091}
92
Howard Hinnanta6baba12011-12-08 19:35:18 +000093// This does not need to be atomic
Howard Hinnantb50cda72012-01-30 16:07:00 +000094static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnanta6baba12011-12-08 19:35:18 +000095 return ++exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000096}
97
Howard Hinnanta6baba12011-12-08 19:35:18 +000098// This does not need to be atomic
Howard Hinnantb50cda72012-01-30 16:07:00 +000099static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000100 return --exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000101}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000102
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000103#include "fallback_malloc.ipp"
Marshall Clowe0252022011-07-20 15:04:39 +0000104
105// Allocate some memory from _somewhere_
Howard Hinnantb50cda72012-01-30 16:07:00 +0000106static void *do_malloc(size_t size) {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000107 void *ptr = std::malloc(size);
108 if (NULL == ptr) // if malloc fails, fall back to emergency stash
109 ptr = fallback_malloc(size);
Marshall Clowe0252022011-07-20 15:04:39 +0000110 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000111}
Marshall Clowe0252022011-07-20 15:04:39 +0000112
Howard Hinnantb50cda72012-01-30 16:07:00 +0000113static void do_free(void *ptr) {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000114 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000115}
Marshall Clowe0252022011-07-20 15:04:39 +0000116
Howard Hinnantb80931e2011-12-07 21:16:40 +0000117/*
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000118 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
119 stored in exc is called. Otherwise the exceptionDestructor stored in
120 exc is called, and then the memory for the exception is deallocated.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000121
122 This is never called for a __cxa_dependent_exception.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000123*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000124static
125void
126exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
127{
128 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000129 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000130 std::__terminate(exception_header->terminateHandler);
131
Howard Hinnantb50cda72012-01-30 16:07:00 +0000132 // TODO: Shouldn't this check the reference count first?
Howard Hinnant7f476b42012-01-22 19:14:27 +0000133 void * thrown_object = thrown_object_from_cxa_exception(exception_header);
134 if (NULL != exception_header->exceptionDestructor)
135 exception_header->exceptionDestructor(thrown_object);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000136 __cxa_free_exception(thrown_object);
137}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000138
Howard Hinnantb50cda72012-01-30 16:07:00 +0000139static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000140// Section 2.5.3 says:
141// * For purposes of this ABI, several things are considered exception handlers:
142// ** A terminate() call due to a throw.
143// and
144// * Upon entry, Following initialization of the catch parameter,
145// a handler must call:
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000146// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnant7f476b42012-01-22 19:14:27 +0000147 (void) __cxa_begin_catch(&exception_header->unwindHeader);
148 std::__terminate(exception_header->terminateHandler);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000149}
Marshall Clowe0252022011-07-20 15:04:39 +0000150
151extern "C" {
152
153// Allocate a __cxa_exception object, and zero-fill it.
154// Reserve "thrown_size" bytes on the end for the user's exception
155// object. Zero-fill the object. If memory can't be allocated, call
156// std::terminate. Return a pointer to the memory to be used for the
157// user's exception object.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000158void * __cxa_allocate_exception (size_t thrown_size) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000159 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
160 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size));
161 if (NULL == exception_header)
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000162 std::terminate();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000163 std::memset(exception_header, 0, actual_size);
164 return thrown_object_from_cxa_exception(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000165}
Marshall Clowe0252022011-07-20 15:04:39 +0000166
167
168// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000169void __cxa_free_exception (void * thrown_object) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000170 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000171}
Marshall Clowe0252022011-07-20 15:04:39 +0000172
173
174// This function shall allocate a __cxa_dependent_exception and
175// return a pointer to it. (Really to the object, not past its' end).
176// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000177void * __cxa_allocate_dependent_exception () {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000178 size_t actual_size = sizeof(__cxa_dependent_exception);
179 void *ptr = do_malloc(actual_size);
180 if (NULL == ptr)
181 std::terminate();
182 std::memset(ptr, 0, actual_size);
Marshall Clowe0252022011-07-20 15:04:39 +0000183 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000184}
Marshall Clowe0252022011-07-20 15:04:39 +0000185
186
187// This function shall free a dependent_exception.
188// It does not affect the reference count of the primary exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000189void __cxa_free_dependent_exception (void * dependent_exception) {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000190 do_free(dependent_exception);
191}
Marshall Clowe0252022011-07-20 15:04:39 +0000192
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000193
194// 2.4.3 Throwing the Exception Object
195/*
196After constructing the exception object with the throw argument value,
197the generated code calls the __cxa_throw runtime library routine. This
198routine never returns.
199
200The __cxa_throw routine will do the following:
201
202* Obtain the __cxa_exception header from the thrown exception object address,
203which can be computed as follows:
204 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
205* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
206* Save the tinfo and dest arguments in the __cxa_exception header.
207* Set the exception_class field in the unwind header. This is a 64-bit value
208representing the ASCII string "XXXXC++\0", where "XXXX" is a
209vendor-dependent string. That is, for implementations conforming to this
210ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
211* Increment the uncaught_exception flag.
212* Call _Unwind_RaiseException in the system unwind library, Its argument is the
213pointer to the thrown exception, which __cxa_throw itself received as an argument.
214__Unwind_RaiseException begins the process of stack unwinding, described
215in Section 2.5. In special cases, such as an inability to find a
216handler, _Unwind_RaiseException may return. In that case, __cxa_throw
217will call terminate, assuming that there was no handler for the
218exception.
219*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000220LIBCXXABI_NORETURN
221void
222__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
223{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000224 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000225 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000226
Howard Hinnant7f476b42012-01-22 19:14:27 +0000227 exception_header->unexpectedHandler = std::get_unexpected();
228 exception_header->terminateHandler = std::get_terminate();
229 exception_header->exceptionType = tinfo;
230 exception_header->exceptionDestructor = dest;
231 setExceptionClass(&exception_header->unwindHeader);
232 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000233 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
234
Howard Hinnant7f476b42012-01-22 19:14:27 +0000235 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000236#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000237 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000238#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000239 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000240#endif
Howard Hinnantcbe4c6d2012-01-28 00:34:46 +0000241 // This only happens when there is no handler, or some unexpected unwinding
242 // error happens.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000243 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000244}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000245
246
247// 2.5.3 Exception Handlers
Howard Hinnant7f476b42012-01-22 19:14:27 +0000248/*
249The adjusted pointer is computed by the personality routine during phase 1
250 and saved in the exception header (either __cxa_exception or
251 __cxa_dependent_exception).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000252
253 Requires: exception is native
Howard Hinnant7f476b42012-01-22 19:14:27 +0000254*/
255void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000256__cxa_get_exception_ptr(void* unwind_exception) throw()
Howard Hinnant7f476b42012-01-22 19:14:27 +0000257{
258 return cxa_exception_from_exception_unwind_exception
259 (
260 static_cast<_Unwind_Exception*>(unwind_exception)
261 )->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000262}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000263
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000264/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000265This routine can catch foreign or native exceptions. If native, the exception
266can be a primary or dependent variety. This routine may remain blissfully
267ignorant of whether the native exception is primary or dependent.
268
269If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000270* Increment's the exception's handler count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000271* Push the exception on the stack of currently-caught exceptions if it is not
272 already there (from a rethrow).
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000273* Decrements the uncaught_exception count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000274* Returns the adjusted pointer to the exception object, which is stored in
275 the __cxa_exception by the personality routine.
276
277If the exception is foreign, this means it did not originate from one of throw
278routines. The foreign exception does not necessarily have a __cxa_exception
279header. However we can catch it here with a catch (...), or with a call
280to terminate or unexpected during unwinding.
281* Do not try to increment the exception's handler count, we don't know where
282 it is.
283* Push the exception on the stack of currently-caught exceptions only if the
284 stack is empty. The foreign exception has no way to link to the current
285 top of stack. If the stack is not empty, call terminate. Even with an
286 empty stack, this is hacked in by pushing a pointer to an imaginary
287 __cxa_exception block in front of the foreign exception. It would be better
288 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
289 doesn't. It has a stack of __cxa_exception (which has a next* in it).
290* Do not decrement the uncaught_exception count because we didn't increment it
291 in __cxa_throw (or one of our rethrow functions).
292* If we haven't terminated, assume the exception object is just past the
293 _Unwind_Exception and return a pointer to that.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000294*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000295void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000296__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnant7f476b42012-01-22 19:14:27 +0000297{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000298 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
299 bool native_exception = isOurExceptionClass(unwind_exception);
300 __cxa_eh_globals* globals = __cxa_get_globals();
301 // exception_header is a hackish offset from a foreign exception, but it
302 // works as long as we're careful not to try to access any __cxa_exception
303 // parts.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000304 __cxa_exception* exception_header =
305 cxa_exception_from_exception_unwind_exception
306 (
307 static_cast<_Unwind_Exception*>(unwind_exception)
308 );
Howard Hinnantb50cda72012-01-30 16:07:00 +0000309 if (native_exception)
310 {
311 // Increment the handler count, removing the flag about being rethrown
312 exception_header->handlerCount = exception_header->handlerCount < 0 ?
313 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
314 // place the exception on the top of the stack if it's not already
315 // there by a previous rethrow
316 if (exception_header != globals->caughtExceptions)
317 {
318 exception_header->nextException = globals->caughtExceptions;
319 globals->caughtExceptions = exception_header;
320 }
321 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnantb50cda72012-01-30 16:07:00 +0000322 return exception_header->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000323 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000324 // Else this is a foreign exception
325 // If the caughtExceptions stack is not empty, terminate
326 if (globals->caughtExceptions != 0)
327 std::terminate();
328 // Push the foreign exception on to the stack
329 globals->caughtExceptions = exception_header;
330 return unwind_exception + 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000331}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000332
333
334/*
335Upon exit for any reason, a handler must call:
336 void __cxa_end_catch ();
337
Howard Hinnantb50cda72012-01-30 16:07:00 +0000338This routine can be called for either a native or foreign exception.
339For a native exception:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000340* Locates the most recently caught exception and decrements its handler count.
341* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnanta6baba12011-12-08 19:35:18 +0000342* If the handler count goes down to zero, and the exception was not re-thrown
343 by throw, it locates the primary exception (which may be the same as the one
344 it's handling) and decrements its reference count. If that reference count
345 goes to zero, the function destroys the exception. In any case, if the current
346 exception is a dependent exception, it destroys that.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000347
348For a foreign exception:
349* If it has been rethrown, there is nothing to do.
350* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000351*/
Howard Hinnantb50cda72012-01-30 16:07:00 +0000352void __cxa_end_catch()
353{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000354 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
355 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnantb50cda72012-01-30 16:07:00 +0000356 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
357 __cxa_exception* exception_header = globals->caughtExceptions;
358 // If we've rethrown a foreign exception, then globals->caughtExceptions
359 // will have been made an empty stack by __cxa_rethrow() and there is
360 // nothing more to be done. Do nothing!
361 if (NULL != exception_header)
362 {
363 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
364 if (native_exception)
365 {
366 // This is a native exception
367 if (exception_header->handlerCount < 0)
368 {
369 // The exception has been rethrown by __cxa_rethrow, so don't delete it
370 if (0 == incrementHandlerCount(exception_header))
371 {
372 // Remove from the chain of uncaught exceptions
373 globals->caughtExceptions = exception_header->nextException;
374 // but don't destroy
Howard Hinnanta6baba12011-12-08 19:35:18 +0000375 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000376 // Keep handlerCount negative in case there are nested catch's
377 // that need to be told that this exception is rethrown. Don't
378 // erase this rethrow flag until the exception is recaught.
379 }
380 else
381 {
382 // The native exception has not been rethrown
383 if (0 == decrementHandlerCount(exception_header))
384 {
385 // Remove from the chain of uncaught exceptions
386 globals->caughtExceptions = exception_header->nextException;
387 // Destroy this exception, being careful to distinguish
388 // between dependent and primary exceptions
389 if (isDependentException(&exception_header->unwindHeader))
390 {
391 // Reset exception_header to primaryException and deallocate the dependent exception
392 __cxa_dependent_exception* dep_exception_header =
393 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
394 exception_header =
395 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
396 __cxa_free_dependent_exception(dep_exception_header);
397 }
398 // Destroy the primary exception only if its referenceCount goes to 0
399 // (this decrement must be atomic)
400 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
401 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000402 }
403 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000404 else
405 {
406 // The foreign exception has not been rethrown. Pop the stack
407 // and delete it. If there are nested catch's and they try
408 // to touch a foreign exception in any way, that is undefined
409 // behavior. They likely can't since the only way to catch
410 // a foreign exception is with catch (...)!
411 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
412 globals->caughtExceptions = 0;
413 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000414 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000415}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000416
Howard Hinnant7f476b42012-01-22 19:14:27 +0000417// Note: exception_header may be masquerading as a __cxa_dependent_exception
418// and that's ok. exceptionType is there too.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000419// However watch out for foreign exceptions. Return null for them.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000420std::type_info * __cxa_current_exception_type() {
421// get the current exception
Marshall Clow143cfb02011-12-22 15:45:05 +0000422 __cxa_eh_globals *globals = __cxa_get_globals_fast();
423 if (NULL == globals)
424 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000425 __cxa_exception *exception_header = globals->caughtExceptions;
426 if (NULL == exception_header)
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000427 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000428 if (!isOurExceptionClass(&exception_header->unwindHeader))
429 return NULL;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000430 return exception_header->exceptionType;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000431}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000432
433// 2.5.4 Rethrowing Exceptions
Howard Hinnantb50cda72012-01-30 16:07:00 +0000434/* This routine can rethrow native or foreign exceptions.
435If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000436* marks the exception object on top of the caughtExceptions stack
437 (in an implementation-defined way) as being rethrown.
438* If the caughtExceptions stack is empty, it calls terminate()
439 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000440* It then calls _Unwind_RaiseException which should not return
Howard Hinnant939daa72011-12-12 19:11:42 +0000441 (terminate if it does).
Howard Hinnant7f476b42012-01-22 19:14:27 +0000442 Note: exception_header may be masquerading as a __cxa_dependent_exception
443 and that's ok.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000444*/
Howard Hinnantb50cda72012-01-30 16:07:00 +0000445LIBCXXABI_NORETURN
446void
447__cxa_rethrow()
448{
449 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnantb50cda72012-01-30 16:07:00 +0000450 __cxa_exception* exception_header = globals->caughtExceptions;
451 if (NULL == exception_header)
452 std::terminate(); // throw; called outside of a exception handler
453 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
454 if (native_exception)
455 {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000456 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
457 exception_header->handlerCount = -exception_header->handlerCount;
458 globals->uncaughtExceptions += 1;
459 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
460 }
461 else // this is a foreign exception
462 {
463 // The only way to communicate to __cxa_end_catch that we've rethrown
464 // a foreign exception, so don't delete us, is to pop the stack here
465 // which must be empty afterwards. Then __cxa_end_catch will do
466 // nothing
467 globals->caughtExceptions = 0;
468 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000469#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000470 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000471#else
Howard Hinnantb50cda72012-01-30 16:07:00 +0000472 (void)_Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000473#endif
474
Howard Hinnantb50cda72012-01-30 16:07:00 +0000475 // If we get here, some kind of unwinding error has occurred.
476 // There is some weird code generation bug happening with
477 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
478 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
479 __cxa_begin_catch(&exception_header->unwindHeader);
480 if (native_exception)
481 std::__terminate(exception_header->terminateHandler);
482 // Foreign exception: can't get exception_header->terminateHandler
483 std::terminate();
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000484}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000485
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000486/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000487 If thrown_object is not null, atomically increment the referenceCount field
488 of the __cxa_exception header associated with the thrown object referred to
489 by thrown_object.
490
491 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000492*/
493void
Howard Hinnantb50cda72012-01-30 16:07:00 +0000494__cxa_increment_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000495{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000496 if (thrown_object != NULL )
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000497 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000498 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
499 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000500 }
501}
502
503/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000504 If thrown_object is not null, atomically decrement the referenceCount field
505 of the __cxa_exception header associated with the thrown object referred to
506 by thrown_object. If the referenceCount drops to zero, destroy and
507 deallocate the exception.
508
509 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000510*/
511void
Howard Hinnantb50cda72012-01-30 16:07:00 +0000512__cxa_decrement_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000513{
514 if (thrown_object != NULL )
515 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000516 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
517 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000518 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000519 if (NULL != exception_header->exceptionDestructor)
520 exception_header->exceptionDestructor(thrown_object);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000521 __cxa_free_exception(thrown_object);
522 }
523 }
524}
525
526/*
527 Returns a pointer to the thrown object (if any) at the top of the
528 caughtExceptions stack. Atommically increment the exception's referenceCount.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000529 If there is no such thrown object or if the thrown object is foreign,
530 returns null.
Marshall Clow15997562012-01-04 22:18:10 +0000531
532 We can use __cxa_get_globals_fast here to get the globals because if there have
533 been no exceptions thrown, ever, on this thread, we can return NULL without
534 the need to allocate the exception-handling globals.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000535*/
536void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000537__cxa_current_primary_exception() throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000538{
539// get the current exception
Marshall Clowe80ed7d2012-01-03 23:26:09 +0000540 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow57908522012-01-03 23:10:20 +0000541 if (NULL == globals)
Marshall Clow8989e4e2012-01-04 14:56:09 +0000542 return NULL; // If there are no globals, there is no exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000543 __cxa_exception* exception_header = globals->caughtExceptions;
544 if (NULL == exception_header)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000545 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000546 if (!isOurExceptionClass(&exception_header->unwindHeader))
547 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000548 if (isDependentException(&exception_header->unwindHeader)) {
549 __cxa_dependent_exception* dep_exception_header =
550 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
551 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000552 }
Howard Hinnant7f476b42012-01-22 19:14:27 +0000553 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000554 __cxa_increment_exception_refcount(thrown_object);
555 return thrown_object;
556}
557
558/*
559 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
560 stored in exc is called. Otherwise the referenceCount stored in the
561 primary exception is decremented, destroying the primary if necessary.
562 Finally the dependent exception is destroyed.
563*/
564static
565void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000566dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000567{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000568 __cxa_dependent_exception* dep_exception_header =
569 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000570 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000571 std::__terminate(dep_exception_header->terminateHandler);
572 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
573 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000574}
575
576/*
577 If thrown_object is not null, allocate, initialize and thow a dependent
578 exception.
579*/
580void
581__cxa_rethrow_primary_exception(void* thrown_object)
582{
583 if ( thrown_object != NULL )
584 {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000585 // thrown_object guaranteed to be native because
586 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000587 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
588 __cxa_dependent_exception* dep_exception_header =
589 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
590 dep_exception_header->primaryException = thrown_object;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000591 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnant7f476b42012-01-22 19:14:27 +0000592 dep_exception_header->exceptionType = exception_header->exceptionType;
593 dep_exception_header->unexpectedHandler = std::get_unexpected();
594 dep_exception_header->terminateHandler = std::get_terminate();
595 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantb9bf50b2011-12-21 23:48:05 +0000596 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000597 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000598#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000599 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000600#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000601 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000602#endif
603 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000604 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000605 }
606 // If we return client will call terminate()
607}
608
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000609bool
Howard Hinnantb50cda72012-01-30 16:07:00 +0000610__cxa_uncaught_exception() throw()
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000611{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000612 // This does not report foreign exceptions in flight
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000613 __cxa_eh_globals* globals = __cxa_get_globals_fast();
614 if (globals == 0)
615 return false;
616 return globals->uncaughtExceptions != 0;
617}
618
Marshall Clowe0252022011-07-20 15:04:39 +0000619} // extern "C"
620
621} // abi