blob: 6f482a2b6236ccba6792e7a29c092d6fc5d3e357 [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);
Howard Hinnantbe01d952012-02-01 18:44:21 +0000131 // Just in case there exists a dependent exception that is pointing to this,
132 // check the reference count and only destroy this if that count goes to zero.
133 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000134}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000135
Howard Hinnantb50cda72012-01-30 16:07:00 +0000136static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000137// Section 2.5.3 says:
138// * For purposes of this ABI, several things are considered exception handlers:
139// ** A terminate() call due to a throw.
140// and
141// * Upon entry, Following initialization of the catch parameter,
142// a handler must call:
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000143// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnant7f476b42012-01-22 19:14:27 +0000144 (void) __cxa_begin_catch(&exception_header->unwindHeader);
145 std::__terminate(exception_header->terminateHandler);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000146}
Marshall Clowe0252022011-07-20 15:04:39 +0000147
148extern "C" {
149
150// Allocate a __cxa_exception object, and zero-fill it.
151// Reserve "thrown_size" bytes on the end for the user's exception
152// object. Zero-fill the object. If memory can't be allocated, call
153// std::terminate. Return a pointer to the memory to be used for the
154// user's exception object.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000155void * __cxa_allocate_exception (size_t thrown_size) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000156 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
157 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size));
158 if (NULL == exception_header)
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000159 std::terminate();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000160 std::memset(exception_header, 0, actual_size);
161 return thrown_object_from_cxa_exception(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000162}
Marshall Clowe0252022011-07-20 15:04:39 +0000163
164
165// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000166void __cxa_free_exception (void * thrown_object) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000167 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000168}
Marshall Clowe0252022011-07-20 15:04:39 +0000169
170
171// This function shall allocate a __cxa_dependent_exception and
172// return a pointer to it. (Really to the object, not past its' end).
173// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000174void * __cxa_allocate_dependent_exception () {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000175 size_t actual_size = sizeof(__cxa_dependent_exception);
176 void *ptr = do_malloc(actual_size);
177 if (NULL == ptr)
178 std::terminate();
179 std::memset(ptr, 0, actual_size);
Marshall Clowe0252022011-07-20 15:04:39 +0000180 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000181}
Marshall Clowe0252022011-07-20 15:04:39 +0000182
183
184// This function shall free a dependent_exception.
185// It does not affect the reference count of the primary exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000186void __cxa_free_dependent_exception (void * dependent_exception) {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000187 do_free(dependent_exception);
188}
Marshall Clowe0252022011-07-20 15:04:39 +0000189
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000190
191// 2.4.3 Throwing the Exception Object
192/*
193After constructing the exception object with the throw argument value,
194the generated code calls the __cxa_throw runtime library routine. This
195routine never returns.
196
197The __cxa_throw routine will do the following:
198
199* Obtain the __cxa_exception header from the thrown exception object address,
200which can be computed as follows:
201 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
202* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
203* Save the tinfo and dest arguments in the __cxa_exception header.
204* Set the exception_class field in the unwind header. This is a 64-bit value
205representing the ASCII string "XXXXC++\0", where "XXXX" is a
206vendor-dependent string. That is, for implementations conforming to this
207ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
208* Increment the uncaught_exception flag.
209* Call _Unwind_RaiseException in the system unwind library, Its argument is the
210pointer to the thrown exception, which __cxa_throw itself received as an argument.
211__Unwind_RaiseException begins the process of stack unwinding, described
212in Section 2.5. In special cases, such as an inability to find a
213handler, _Unwind_RaiseException may return. In that case, __cxa_throw
214will call terminate, assuming that there was no handler for the
215exception.
216*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000217LIBCXXABI_NORETURN
218void
219__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
220{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000221 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000222 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000223
Howard Hinnant7f476b42012-01-22 19:14:27 +0000224 exception_header->unexpectedHandler = std::get_unexpected();
225 exception_header->terminateHandler = std::get_terminate();
226 exception_header->exceptionType = tinfo;
227 exception_header->exceptionDestructor = dest;
228 setExceptionClass(&exception_header->unwindHeader);
229 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000230 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
231
Howard Hinnant7f476b42012-01-22 19:14:27 +0000232 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000233#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000234 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000235#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000236 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000237#endif
Howard Hinnantcbe4c6d2012-01-28 00:34:46 +0000238 // This only happens when there is no handler, or some unexpected unwinding
239 // error happens.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000240 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000241}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000242
243
244// 2.5.3 Exception Handlers
Howard Hinnant7f476b42012-01-22 19:14:27 +0000245/*
246The adjusted pointer is computed by the personality routine during phase 1
247 and saved in the exception header (either __cxa_exception or
248 __cxa_dependent_exception).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000249
250 Requires: exception is native
Howard Hinnant7f476b42012-01-22 19:14:27 +0000251*/
252void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000253__cxa_get_exception_ptr(void* unwind_exception) throw()
Howard Hinnant7f476b42012-01-22 19:14:27 +0000254{
255 return cxa_exception_from_exception_unwind_exception
256 (
257 static_cast<_Unwind_Exception*>(unwind_exception)
258 )->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000259}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000260
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000261/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000262This routine can catch foreign or native exceptions. If native, the exception
263can be a primary or dependent variety. This routine may remain blissfully
264ignorant of whether the native exception is primary or dependent.
265
266If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000267* Increment's the exception's handler count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000268* Push the exception on the stack of currently-caught exceptions if it is not
269 already there (from a rethrow).
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000270* Decrements the uncaught_exception count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000271* Returns the adjusted pointer to the exception object, which is stored in
272 the __cxa_exception by the personality routine.
273
274If the exception is foreign, this means it did not originate from one of throw
275routines. The foreign exception does not necessarily have a __cxa_exception
276header. However we can catch it here with a catch (...), or with a call
277to terminate or unexpected during unwinding.
278* Do not try to increment the exception's handler count, we don't know where
279 it is.
280* Push the exception on the stack of currently-caught exceptions only if the
281 stack is empty. The foreign exception has no way to link to the current
282 top of stack. If the stack is not empty, call terminate. Even with an
283 empty stack, this is hacked in by pushing a pointer to an imaginary
284 __cxa_exception block in front of the foreign exception. It would be better
285 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
286 doesn't. It has a stack of __cxa_exception (which has a next* in it).
287* Do not decrement the uncaught_exception count because we didn't increment it
288 in __cxa_throw (or one of our rethrow functions).
289* If we haven't terminated, assume the exception object is just past the
290 _Unwind_Exception and return a pointer to that.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000291*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000292void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000293__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnant7f476b42012-01-22 19:14:27 +0000294{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000295 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
296 bool native_exception = isOurExceptionClass(unwind_exception);
297 __cxa_eh_globals* globals = __cxa_get_globals();
298 // exception_header is a hackish offset from a foreign exception, but it
299 // works as long as we're careful not to try to access any __cxa_exception
300 // parts.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000301 __cxa_exception* exception_header =
302 cxa_exception_from_exception_unwind_exception
303 (
304 static_cast<_Unwind_Exception*>(unwind_exception)
305 );
Howard Hinnantb50cda72012-01-30 16:07:00 +0000306 if (native_exception)
307 {
308 // Increment the handler count, removing the flag about being rethrown
309 exception_header->handlerCount = exception_header->handlerCount < 0 ?
310 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
311 // place the exception on the top of the stack if it's not already
312 // there by a previous rethrow
313 if (exception_header != globals->caughtExceptions)
314 {
315 exception_header->nextException = globals->caughtExceptions;
316 globals->caughtExceptions = exception_header;
317 }
318 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnantb50cda72012-01-30 16:07:00 +0000319 return exception_header->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000320 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000321 // Else this is a foreign exception
322 // If the caughtExceptions stack is not empty, terminate
323 if (globals->caughtExceptions != 0)
324 std::terminate();
325 // Push the foreign exception on to the stack
326 globals->caughtExceptions = exception_header;
327 return unwind_exception + 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000328}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000329
330
331/*
332Upon exit for any reason, a handler must call:
333 void __cxa_end_catch ();
334
Howard Hinnantb50cda72012-01-30 16:07:00 +0000335This routine can be called for either a native or foreign exception.
336For a native exception:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000337* Locates the most recently caught exception and decrements its handler count.
338* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnanta6baba12011-12-08 19:35:18 +0000339* If the handler count goes down to zero, and the exception was not re-thrown
340 by throw, it locates the primary exception (which may be the same as the one
341 it's handling) and decrements its reference count. If that reference count
342 goes to zero, the function destroys the exception. In any case, if the current
343 exception is a dependent exception, it destroys that.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000344
345For a foreign exception:
346* If it has been rethrown, there is nothing to do.
347* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000348*/
Howard Hinnantb50cda72012-01-30 16:07:00 +0000349void __cxa_end_catch()
350{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000351 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
352 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnantb50cda72012-01-30 16:07:00 +0000353 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
354 __cxa_exception* exception_header = globals->caughtExceptions;
355 // If we've rethrown a foreign exception, then globals->caughtExceptions
356 // will have been made an empty stack by __cxa_rethrow() and there is
357 // nothing more to be done. Do nothing!
358 if (NULL != exception_header)
359 {
360 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
361 if (native_exception)
362 {
363 // This is a native exception
364 if (exception_header->handlerCount < 0)
365 {
366 // The exception has been rethrown by __cxa_rethrow, so don't delete it
367 if (0 == incrementHandlerCount(exception_header))
368 {
369 // Remove from the chain of uncaught exceptions
370 globals->caughtExceptions = exception_header->nextException;
371 // but don't destroy
Howard Hinnanta6baba12011-12-08 19:35:18 +0000372 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000373 // Keep handlerCount negative in case there are nested catch's
374 // that need to be told that this exception is rethrown. Don't
375 // erase this rethrow flag until the exception is recaught.
376 }
377 else
378 {
379 // The native exception has not been rethrown
380 if (0 == decrementHandlerCount(exception_header))
381 {
382 // Remove from the chain of uncaught exceptions
383 globals->caughtExceptions = exception_header->nextException;
384 // Destroy this exception, being careful to distinguish
385 // between dependent and primary exceptions
386 if (isDependentException(&exception_header->unwindHeader))
387 {
388 // Reset exception_header to primaryException and deallocate the dependent exception
389 __cxa_dependent_exception* dep_exception_header =
390 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
391 exception_header =
392 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
393 __cxa_free_dependent_exception(dep_exception_header);
394 }
395 // Destroy the primary exception only if its referenceCount goes to 0
396 // (this decrement must be atomic)
397 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
398 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000399 }
400 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000401 else
402 {
403 // The foreign exception has not been rethrown. Pop the stack
404 // and delete it. If there are nested catch's and they try
405 // to touch a foreign exception in any way, that is undefined
406 // behavior. They likely can't since the only way to catch
407 // a foreign exception is with catch (...)!
408 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
409 globals->caughtExceptions = 0;
410 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000411 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000412}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000413
Howard Hinnant7f476b42012-01-22 19:14:27 +0000414// Note: exception_header may be masquerading as a __cxa_dependent_exception
415// and that's ok. exceptionType is there too.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000416// However watch out for foreign exceptions. Return null for them.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000417std::type_info * __cxa_current_exception_type() {
418// get the current exception
Marshall Clow143cfb02011-12-22 15:45:05 +0000419 __cxa_eh_globals *globals = __cxa_get_globals_fast();
420 if (NULL == globals)
421 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000422 __cxa_exception *exception_header = globals->caughtExceptions;
423 if (NULL == exception_header)
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000424 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000425 if (!isOurExceptionClass(&exception_header->unwindHeader))
426 return NULL;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000427 return exception_header->exceptionType;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000428}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000429
430// 2.5.4 Rethrowing Exceptions
Howard Hinnantb50cda72012-01-30 16:07:00 +0000431/* This routine can rethrow native or foreign exceptions.
432If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000433* marks the exception object on top of the caughtExceptions stack
434 (in an implementation-defined way) as being rethrown.
435* If the caughtExceptions stack is empty, it calls terminate()
436 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000437* It then calls _Unwind_RaiseException which should not return
Howard Hinnant939daa72011-12-12 19:11:42 +0000438 (terminate if it does).
Howard Hinnant7f476b42012-01-22 19:14:27 +0000439 Note: exception_header may be masquerading as a __cxa_dependent_exception
440 and that's ok.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000441*/
Howard Hinnantb50cda72012-01-30 16:07:00 +0000442LIBCXXABI_NORETURN
443void
444__cxa_rethrow()
445{
446 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnantb50cda72012-01-30 16:07:00 +0000447 __cxa_exception* exception_header = globals->caughtExceptions;
448 if (NULL == exception_header)
449 std::terminate(); // throw; called outside of a exception handler
450 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
451 if (native_exception)
452 {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000453 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
454 exception_header->handlerCount = -exception_header->handlerCount;
455 globals->uncaughtExceptions += 1;
456 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
457 }
458 else // this is a foreign exception
459 {
460 // The only way to communicate to __cxa_end_catch that we've rethrown
461 // a foreign exception, so don't delete us, is to pop the stack here
462 // which must be empty afterwards. Then __cxa_end_catch will do
463 // nothing
464 globals->caughtExceptions = 0;
465 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000466#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000467 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000468#else
Howard Hinnantb50cda72012-01-30 16:07:00 +0000469 (void)_Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000470#endif
471
Howard Hinnantb50cda72012-01-30 16:07:00 +0000472 // If we get here, some kind of unwinding error has occurred.
473 // There is some weird code generation bug happening with
474 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
475 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
476 __cxa_begin_catch(&exception_header->unwindHeader);
477 if (native_exception)
478 std::__terminate(exception_header->terminateHandler);
479 // Foreign exception: can't get exception_header->terminateHandler
480 std::terminate();
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000481}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000482
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000483/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000484 If thrown_object is not null, atomically increment the referenceCount field
485 of the __cxa_exception header associated with the thrown object referred to
486 by thrown_object.
487
488 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000489*/
490void
Howard Hinnantb50cda72012-01-30 16:07:00 +0000491__cxa_increment_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000492{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000493 if (thrown_object != NULL )
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000494 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000495 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
496 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000497 }
498}
499
500/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000501 If thrown_object is not null, atomically decrement the referenceCount field
502 of the __cxa_exception header associated with the thrown object referred to
503 by thrown_object. If the referenceCount drops to zero, destroy and
504 deallocate the exception.
505
506 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000507*/
508void
Howard Hinnantb50cda72012-01-30 16:07:00 +0000509__cxa_decrement_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000510{
511 if (thrown_object != NULL )
512 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000513 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
514 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000515 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000516 if (NULL != exception_header->exceptionDestructor)
517 exception_header->exceptionDestructor(thrown_object);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000518 __cxa_free_exception(thrown_object);
519 }
520 }
521}
522
523/*
524 Returns a pointer to the thrown object (if any) at the top of the
525 caughtExceptions stack. Atommically increment the exception's referenceCount.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000526 If there is no such thrown object or if the thrown object is foreign,
527 returns null.
Marshall Clow15997562012-01-04 22:18:10 +0000528
529 We can use __cxa_get_globals_fast here to get the globals because if there have
530 been no exceptions thrown, ever, on this thread, we can return NULL without
531 the need to allocate the exception-handling globals.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000532*/
533void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000534__cxa_current_primary_exception() throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000535{
536// get the current exception
Marshall Clowe80ed7d2012-01-03 23:26:09 +0000537 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow57908522012-01-03 23:10:20 +0000538 if (NULL == globals)
Marshall Clow8989e4e2012-01-04 14:56:09 +0000539 return NULL; // If there are no globals, there is no exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000540 __cxa_exception* exception_header = globals->caughtExceptions;
541 if (NULL == exception_header)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000542 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000543 if (!isOurExceptionClass(&exception_header->unwindHeader))
544 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000545 if (isDependentException(&exception_header->unwindHeader)) {
546 __cxa_dependent_exception* dep_exception_header =
547 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
548 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000549 }
Howard Hinnant7f476b42012-01-22 19:14:27 +0000550 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000551 __cxa_increment_exception_refcount(thrown_object);
552 return thrown_object;
553}
554
555/*
556 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
557 stored in exc is called. Otherwise the referenceCount stored in the
558 primary exception is decremented, destroying the primary if necessary.
559 Finally the dependent exception is destroyed.
560*/
561static
562void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000563dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000564{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000565 __cxa_dependent_exception* dep_exception_header =
566 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000567 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000568 std::__terminate(dep_exception_header->terminateHandler);
569 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
570 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000571}
572
573/*
574 If thrown_object is not null, allocate, initialize and thow a dependent
575 exception.
576*/
577void
578__cxa_rethrow_primary_exception(void* thrown_object)
579{
580 if ( thrown_object != NULL )
581 {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000582 // thrown_object guaranteed to be native because
583 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000584 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
585 __cxa_dependent_exception* dep_exception_header =
586 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
587 dep_exception_header->primaryException = thrown_object;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000588 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnant7f476b42012-01-22 19:14:27 +0000589 dep_exception_header->exceptionType = exception_header->exceptionType;
590 dep_exception_header->unexpectedHandler = std::get_unexpected();
591 dep_exception_header->terminateHandler = std::get_terminate();
592 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantb9bf50b2011-12-21 23:48:05 +0000593 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000594 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000595#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000596 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000597#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000598 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000599#endif
600 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000601 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000602 }
603 // If we return client will call terminate()
604}
605
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000606bool
Howard Hinnantb50cda72012-01-30 16:07:00 +0000607__cxa_uncaught_exception() throw()
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000608{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000609 // This does not report foreign exceptions in flight
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000610 __cxa_eh_globals* globals = __cxa_get_globals_fast();
611 if (globals == 0)
612 return false;
613 return globals->uncaughtExceptions != 0;
614}
615
Marshall Clowe0252022011-07-20 15:04:39 +0000616} // extern "C"
617
618} // abi