blob: c9dd1d1612617a1688d1fc544e89e9a0574bb6e2 [file] [log] [blame]
Marshall Clowe2dcb752011-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 Hinnant5ec91832011-12-07 21:16:40 +000022#include "cxa_handlers.hpp"
Marshall Clowe2dcb752011-07-20 15:04:39 +000023
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000024// +---------------------------+-----------------------------+---------------+
25// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
26// +---------------------------+-----------------------------+---------------+
27// ^
28// |
29// +-------------------------------------------------------+
30// |
31// +---------------------------+-----------------------------+
32// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
33// +---------------------------+-----------------------------+
34
35
Marshall Clowe2dcb752011-07-20 15:04:39 +000036namespace __cxxabiv1 {
Howard Hinnant6830b2a2012-01-24 18:15:20 +000037
Marshall Clowe2dcb752011-07-20 15:04:39 +000038// Utility routines
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000039static
40inline
41__cxa_exception*
Howard Hinnantabc77062012-01-24 23:42:30 +000042cxa_exception_from_thrown_object(void* thrown_object) noexcept
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000043{
44 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clow9b454bc2011-08-15 18:06:47 +000045}
Marshall Clowe2dcb752011-07-20 15:04:39 +000046
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000047// Note: This is never called when exception_header is masquerading as a
48// __cxa_dependent_exception.
49static
50inline
51void*
Howard Hinnantabc77062012-01-24 23:42:30 +000052thrown_object_from_cxa_exception(__cxa_exception* exception_header) noexcept
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000053{
54 return static_cast<void*>(exception_header + 1);
Marshall Clow9b454bc2011-08-15 18:06:47 +000055}
Marshall Clowe2dcb752011-07-20 15:04:39 +000056
Marshall Clow87694492011-08-09 15:09:41 +000057// Get the exception object from the unwind pointer.
58// Relies on the structure layout, where the unwind pointer is right in
59// front of the user's exception object
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000060static
61inline
62__cxa_exception*
Howard Hinnantabc77062012-01-24 23:42:30 +000063cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) noexcept
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000064{
65 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clow9b454bc2011-08-15 18:06:47 +000066}
Marshall Clow87694492011-08-09 15:09:41 +000067
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000068static
69inline
70size_t
Howard Hinnantabc77062012-01-24 23:42:30 +000071cxa_exception_size_from_exception_thrown_size(size_t size) noexcept
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000072{
73 return size + sizeof (__cxa_exception);
Marshall Clow9b454bc2011-08-15 18:06:47 +000074}
Marshall Clow87694492011-08-09 15:09:41 +000075
Howard Hinnantabc77062012-01-24 23:42:30 +000076static void setExceptionClass(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000077 unwind_exception->exception_class = kOurExceptionClass;
78}
79
Howard Hinnantabc77062012-01-24 23:42:30 +000080static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000081 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clow9b454bc2011-08-15 18:06:47 +000082}
Marshall Clow87694492011-08-09 15:09:41 +000083
84// Is it one of ours?
Howard Hinnantabc77062012-01-24 23:42:30 +000085static bool isOurExceptionClass(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000086 return(unwind_exception->exception_class == kOurExceptionClass)||
87 (unwind_exception->exception_class == kOurDependentExceptionClass);
Marshall Clow9b454bc2011-08-15 18:06:47 +000088}
Marshall Clow87694492011-08-09 15:09:41 +000089
Howard Hinnantabc77062012-01-24 23:42:30 +000090static bool isDependentException(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000091 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clow9b454bc2011-08-15 18:06:47 +000092}
93
Howard Hinnant6ccae152011-12-08 19:35:18 +000094// This does not need to be atomic
Howard Hinnantabc77062012-01-24 23:42:30 +000095static inline int incrementHandlerCount(__cxa_exception *exception) noexcept {
Howard Hinnant6ccae152011-12-08 19:35:18 +000096 return ++exception->handlerCount;
Marshall Clow9b454bc2011-08-15 18:06:47 +000097}
98
Howard Hinnant6ccae152011-12-08 19:35:18 +000099// This does not need to be atomic
Howard Hinnantabc77062012-01-24 23:42:30 +0000100static inline int decrementHandlerCount(__cxa_exception *exception) noexcept {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000101 return --exception->handlerCount;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000102}
Marshall Clow87694492011-08-09 15:09:41 +0000103
Howard Hinnant73ab1862012-01-24 21:41:27 +0000104#include "fallback_malloc.ipp"
Marshall Clowe2dcb752011-07-20 15:04:39 +0000105
106// Allocate some memory from _somewhere_
Howard Hinnantabc77062012-01-24 23:42:30 +0000107static void *do_malloc(size_t size) noexcept {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000108 void *ptr = std::malloc(size);
109 if (NULL == ptr) // if malloc fails, fall back to emergency stash
110 ptr = fallback_malloc(size);
Marshall Clowe2dcb752011-07-20 15:04:39 +0000111 return ptr;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000112}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000113
Howard Hinnantabc77062012-01-24 23:42:30 +0000114static void do_free(void *ptr) noexcept {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000115 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000116}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000117
Howard Hinnant5ec91832011-12-07 21:16:40 +0000118/*
Marshall Clow87694492011-08-09 15:09:41 +0000119 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
120 stored in exc is called. Otherwise the exceptionDestructor stored in
121 exc is called, and then the memory for the exception is deallocated.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000122
123 This is never called for a __cxa_dependent_exception.
Marshall Clow87694492011-08-09 15:09:41 +0000124*/
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000125static
126void
127exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
128{
129 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000130 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000131 std::__terminate(exception_header->terminateHandler);
132
133 void * thrown_object = thrown_object_from_cxa_exception(exception_header);
134 if (NULL != exception_header->exceptionDestructor)
135 exception_header->exceptionDestructor(thrown_object);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000136 __cxa_free_exception(thrown_object);
137}
Marshall Clow87694492011-08-09 15:09:41 +0000138
Howard Hinnantabc77062012-01-24 23:42:30 +0000139static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) noexcept {
Marshall Clow87694492011-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 Clow9b454bc2011-08-15 18:06:47 +0000146// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000147 (void) __cxa_begin_catch(&exception_header->unwindHeader);
148 std::__terminate(exception_header->terminateHandler);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000149}
Marshall Clowe2dcb752011-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 Hinnantabc77062012-01-24 23:42:30 +0000158void * __cxa_allocate_exception (size_t thrown_size) noexcept {
Howard Hinnantafcf7ac2012-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 Clow9b454bc2011-08-15 18:06:47 +0000162 std::terminate();
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000163 std::memset(exception_header, 0, actual_size);
164 return thrown_object_from_cxa_exception(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000165}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000166
167
168// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Howard Hinnantabc77062012-01-24 23:42:30 +0000169void __cxa_free_exception (void * thrown_object) noexcept {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000170 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clow9b454bc2011-08-15 18:06:47 +0000171}
Marshall Clowe2dcb752011-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 Hinnantabc77062012-01-24 23:42:30 +0000177void * __cxa_allocate_dependent_exception () noexcept {
Marshall Clow9b454bc2011-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 Clowe2dcb752011-07-20 15:04:39 +0000183 return ptr;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000184}
Marshall Clowe2dcb752011-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 Hinnantabc77062012-01-24 23:42:30 +0000189void __cxa_free_dependent_exception (void * dependent_exception) noexcept {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000190 do_free(dependent_exception);
191}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000192
Marshall Clow87694492011-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 Hinnantafcf7ac2012-01-22 19:14:27 +0000220LIBCXXABI_NORETURN
221void
222__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
223{
Marshall Clow9b454bc2011-08-15 18:06:47 +0000224 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000225 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000226
Howard Hinnantafcf7ac2012-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 Clow87694492011-08-09 15:09:41 +0000233 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
234
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000235 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Howard Hinnant6ccae152011-12-08 19:35:18 +0000236#if __arm__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000237 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000238#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000239 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000240#endif
Marshall Clow87694492011-08-09 15:09:41 +0000241// If we get here, some kind of unwinding error has occurred.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000242 failed_throw(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000243}
Marshall Clow87694492011-08-09 15:09:41 +0000244
245
246// 2.5.3 Exception Handlers
Howard Hinnantafcf7ac2012-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).
251*/
252void*
Howard Hinnantabc77062012-01-24 23:42:30 +0000253__cxa_get_exception_ptr(void* unwind_exception) noexcept
Howard Hinnantafcf7ac2012-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 Clow9b454bc2011-08-15 18:06:47 +0000259}
Marshall Clow87694492011-08-09 15:09:41 +0000260
261
262/*
263This routine:
264* Increment's the exception's handler count.
265* Places the exception on the stack of currently-caught exceptions if it is not
266 already there, linking the exception to the previous top of the stack.
267* Decrements the uncaught_exception count.
268* Returns the adjusted pointer to the exception object.
269*/
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000270void*
Howard Hinnantabc77062012-01-24 23:42:30 +0000271__cxa_begin_catch(void* unwind_exception) noexcept
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000272{
Marshall Clow9b454bc2011-08-15 18:06:47 +0000273 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000274 __cxa_exception* exception_header =
275 cxa_exception_from_exception_unwind_exception
276 (
277 static_cast<_Unwind_Exception*>(unwind_exception)
278 );
Marshall Clow87694492011-08-09 15:09:41 +0000279
Howard Hinnant6ccae152011-12-08 19:35:18 +0000280// TODO: Handle foreign exceptions? How?
281
Marshall Clow87694492011-08-09 15:09:41 +0000282// Increment the handler count, removing the flag about being rethrown
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000283 exception_header->handlerCount = exception_header->handlerCount < 0 ?
284 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
Marshall Clow87694492011-08-09 15:09:41 +0000285
286// place the exception on the top of the stack if it's not there.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000287 if (exception_header != globals->caughtExceptions) {
288 exception_header->nextException = globals->caughtExceptions;
289 globals->caughtExceptions = exception_header;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000290 }
Marshall Clow87694492011-08-09 15:09:41 +0000291
292 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000293 return exception_header->adjustedPtr;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000294}
Marshall Clow87694492011-08-09 15:09:41 +0000295
296
297/*
298Upon exit for any reason, a handler must call:
299 void __cxa_end_catch ();
300
301This routine:
302* Locates the most recently caught exception and decrements its handler count.
303* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnant6ccae152011-12-08 19:35:18 +0000304* If the handler count goes down to zero, and the exception was not re-thrown
305 by throw, it locates the primary exception (which may be the same as the one
306 it's handling) and decrements its reference count. If that reference count
307 goes to zero, the function destroys the exception. In any case, if the current
308 exception is a dependent exception, it destroys that.
Marshall Clow87694492011-08-09 15:09:41 +0000309*/
310void __cxa_end_catch() {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000311 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
312 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnant6ccae152011-12-08 19:35:18 +0000313 __cxa_eh_globals *globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000314 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow87694492011-08-09 15:09:41 +0000315
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000316 if (NULL != exception_header) {
Howard Hinnant58926c92011-12-12 19:11:42 +0000317 // TODO: Handle foreign exceptions? How?
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000318 if (exception_header->handlerCount < 0) {
Marshall Clow87694492011-08-09 15:09:41 +0000319 // The exception has been rethrown
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000320 if (0 == incrementHandlerCount(exception_header)) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000321 // Remove from the chain of uncaught exceptions
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000322 globals->caughtExceptions = exception_header->nextException;
Howard Hinnant6ccae152011-12-08 19:35:18 +0000323 // but don't destroy
324 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000325 }
Howard Hinnant6ccae152011-12-08 19:35:18 +0000326 else { // The exception has not been rethrown
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000327 if (0 == decrementHandlerCount(exception_header)) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000328 // Remove from the chain of uncaught exceptions
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000329 globals->caughtExceptions = exception_header->nextException;
330 if (isDependentException(&exception_header->unwindHeader)) {
331 // Reset exception_header to primaryException and deallocate the dependent exception
332 __cxa_dependent_exception* dep_exception_header =
333 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
334 exception_header =
335 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
336 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000337 }
338 // Destroy the primary exception only if its referenceCount goes to 0
339 // (this decrement must be atomic)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000340 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
Marshall Clow9b454bc2011-08-15 18:06:47 +0000341 }
342 }
Marshall Clow87694492011-08-09 15:09:41 +0000343 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000344}
Marshall Clow87694492011-08-09 15:09:41 +0000345
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000346// Note: exception_header may be masquerading as a __cxa_dependent_exception
347// and that's ok. exceptionType is there too.
Marshall Clow87694492011-08-09 15:09:41 +0000348std::type_info * __cxa_current_exception_type() {
349// get the current exception
Marshall Clow1de4fc02011-12-22 15:45:05 +0000350 __cxa_eh_globals *globals = __cxa_get_globals_fast();
351 if (NULL == globals)
352 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000353 __cxa_exception *exception_header = globals->caughtExceptions;
354 if (NULL == exception_header)
Marshall Clow87694492011-08-09 15:09:41 +0000355 return NULL; // No current exception
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000356 return exception_header->exceptionType;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000357}
Marshall Clow87694492011-08-09 15:09:41 +0000358
359// 2.5.4 Rethrowing Exceptions
360/* This routine
361* marks the exception object on top of the caughtExceptions stack
362 (in an implementation-defined way) as being rethrown.
363* If the caughtExceptions stack is empty, it calls terminate()
364 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant58926c92011-12-12 19:11:42 +0000365* It then calls _Unwind_Resume_or_Rethrow which should not return
366 (terminate if it does).
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000367 Note: exception_header may be masquerading as a __cxa_dependent_exception
368 and that's ok.
Marshall Clow87694492011-08-09 15:09:41 +0000369*/
370extern LIBCXXABI_NORETURN void __cxa_rethrow() {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000371 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000372 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow87694492011-08-09 15:09:41 +0000373
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000374 if (NULL == exception_header) // there's no current exception!
Marshall Clow87694492011-08-09 15:09:41 +0000375 std::terminate ();
376
Howard Hinnant58926c92011-12-12 19:11:42 +0000377// TODO: Handle foreign exceptions? How?
378
379// Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000380 exception_header->handlerCount = -exception_header->handlerCount;
Howard Hinnant58926c92011-12-12 19:11:42 +0000381 globals->uncaughtExceptions += 1;
382// __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
Marshall Clow87694492011-08-09 15:09:41 +0000383
384#if __arm__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000385 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000386#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000387 (void) _Unwind_Resume_or_Rethrow (&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000388#endif
389
390// If we get here, some kind of unwinding error has occurred.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000391 failed_throw(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000392}
Marshall Clow87694492011-08-09 15:09:41 +0000393
Howard Hinnante04f5162011-12-21 23:32:11 +0000394/*
395 If p is not null, atomically increment the referenceCount field of the
396 __cxa_exception header associated with the thrown object referred to by p.
397*/
398void
Howard Hinnantabc77062012-01-24 23:42:30 +0000399__cxa_increment_exception_refcount(void* thrown_object) noexcept
Howard Hinnante04f5162011-12-21 23:32:11 +0000400{
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000401 if (thrown_object != NULL )
Howard Hinnante04f5162011-12-21 23:32:11 +0000402 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000403 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
404 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnante04f5162011-12-21 23:32:11 +0000405 }
406}
407
408/*
409 If p is not null, atomically decrement the referenceCount field of the
410 __cxa_exception header associated with the thrown object referred to by p.
411 If the referenceCount drops to zero, destroy and deallocate the exception.
412*/
413void
Howard Hinnantabc77062012-01-24 23:42:30 +0000414__cxa_decrement_exception_refcount(void* thrown_object) noexcept
Howard Hinnante04f5162011-12-21 23:32:11 +0000415{
416 if (thrown_object != NULL )
417 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000418 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
419 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnante04f5162011-12-21 23:32:11 +0000420 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000421 if (NULL != exception_header->exceptionDestructor)
422 exception_header->exceptionDestructor(thrown_object);
Howard Hinnante04f5162011-12-21 23:32:11 +0000423 __cxa_free_exception(thrown_object);
424 }
425 }
426}
427
428/*
429 Returns a pointer to the thrown object (if any) at the top of the
430 caughtExceptions stack. Atommically increment the exception's referenceCount.
431 If there is no such thrown object, returns null.
Marshall Clowd2bab352012-01-04 22:18:10 +0000432
433 We can use __cxa_get_globals_fast here to get the globals because if there have
434 been no exceptions thrown, ever, on this thread, we can return NULL without
435 the need to allocate the exception-handling globals.
Howard Hinnante04f5162011-12-21 23:32:11 +0000436*/
437void*
Howard Hinnantabc77062012-01-24 23:42:30 +0000438__cxa_current_primary_exception() noexcept
Howard Hinnante04f5162011-12-21 23:32:11 +0000439{
440// get the current exception
Marshall Clowf83663a2012-01-03 23:26:09 +0000441 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow3e417e72012-01-03 23:10:20 +0000442 if (NULL == globals)
Marshall Clowf3684862012-01-04 14:56:09 +0000443 return NULL; // If there are no globals, there is no exception
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000444 __cxa_exception* exception_header = globals->caughtExceptions;
445 if (NULL == exception_header)
Howard Hinnante04f5162011-12-21 23:32:11 +0000446 return NULL; // No current exception
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000447 if (isDependentException(&exception_header->unwindHeader)) {
448 __cxa_dependent_exception* dep_exception_header =
449 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
450 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnante04f5162011-12-21 23:32:11 +0000451 }
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000452 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnante04f5162011-12-21 23:32:11 +0000453 __cxa_increment_exception_refcount(thrown_object);
454 return thrown_object;
455}
456
457/*
458 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
459 stored in exc is called. Otherwise the referenceCount stored in the
460 primary exception is decremented, destroying the primary if necessary.
461 Finally the dependent exception is destroyed.
462*/
463static
464void
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000465dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnante04f5162011-12-21 23:32:11 +0000466{
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000467 __cxa_dependent_exception* dep_exception_header =
468 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnante04f5162011-12-21 23:32:11 +0000469 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000470 std::__terminate(dep_exception_header->terminateHandler);
471 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
472 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnante04f5162011-12-21 23:32:11 +0000473}
474
475/*
476 If thrown_object is not null, allocate, initialize and thow a dependent
477 exception.
478*/
479void
480__cxa_rethrow_primary_exception(void* thrown_object)
481{
482 if ( thrown_object != NULL )
483 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000484 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
485 __cxa_dependent_exception* dep_exception_header =
486 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
487 dep_exception_header->primaryException = thrown_object;
Howard Hinnante04f5162011-12-21 23:32:11 +0000488 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000489 dep_exception_header->exceptionType = exception_header->exceptionType;
490 dep_exception_header->unexpectedHandler = std::get_unexpected();
491 dep_exception_header->terminateHandler = std::get_terminate();
492 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnant22f28b22011-12-21 23:48:05 +0000493 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000494 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnante04f5162011-12-21 23:32:11 +0000495#if __arm__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000496 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000497#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000498 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000499#endif
500 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000501 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000502 }
503 // If we return client will call terminate()
504}
505
Howard Hinnante33b2f52012-01-24 00:01:31 +0000506bool
Howard Hinnantabc77062012-01-24 23:42:30 +0000507__cxa_uncaught_exception() noexcept
Howard Hinnante33b2f52012-01-24 00:01:31 +0000508{
509 __cxa_eh_globals* globals = __cxa_get_globals_fast();
510 if (globals == 0)
511 return false;
512 return globals->uncaughtExceptions != 0;
513}
514
Marshall Clowe2dcb752011-07-20 15:04:39 +0000515} // extern "C"
516
517} // abi