blob: 3f516c5ef838ab360a3f2cf27c9916d16ced8e6f [file] [log] [blame]
Marshall Clowe0252022011-07-20 15:04:39 +00001//===------------------------- cxa_exception.cpp --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//
9// This file implements the "Exception Handling APIs"
10// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
11//
12//===----------------------------------------------------------------------===//
13
14#include "cxxabi.h"
15
16#include <exception> // for std::terminate
17#include <cstdlib> // for malloc, free
18#include <string> // for memset
19#include <pthread.h>
20
21#include "cxa_exception.hpp"
Howard Hinnantb80931e2011-12-07 21:16:40 +000022#include "cxa_handlers.hpp"
Marshall Clowe0252022011-07-20 15:04:39 +000023
Howard Hinnant7f476b42012-01-22 19:14:27 +000024// +---------------------------+-----------------------------+---------------+
25// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
26// +---------------------------+-----------------------------+---------------+
27// ^
28// |
29// +-------------------------------------------------------+
30// |
31// +---------------------------+-----------------------------+
32// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
33// +---------------------------+-----------------------------+
34
35
Marshall Clowe0252022011-07-20 15:04:39 +000036namespace __cxxabiv1 {
Howard Hinnantb94f2252012-01-24 18:15:20 +000037
Marshall Clowe0252022011-07-20 15:04:39 +000038// Utility routines
Howard Hinnant7f476b42012-01-22 19:14:27 +000039static
40inline
41__cxa_exception*
Howard Hinnant41df9c82012-01-24 23:42:30 +000042cxa_exception_from_thrown_object(void* thrown_object) noexcept
Howard Hinnant7f476b42012-01-22 19:14:27 +000043{
44 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000045}
Marshall Clowe0252022011-07-20 15:04:39 +000046
Howard Hinnant7f476b42012-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 Hinnant41df9c82012-01-24 23:42:30 +000052thrown_object_from_cxa_exception(__cxa_exception* exception_header) noexcept
Howard Hinnant7f476b42012-01-22 19:14:27 +000053{
54 return static_cast<void*>(exception_header + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000055}
Marshall Clowe0252022011-07-20 15:04:39 +000056
Marshall Clow3c54f1b2011-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 Hinnant7f476b42012-01-22 19:14:27 +000060static
61inline
62__cxa_exception*
Howard Hinnant41df9c82012-01-24 23:42:30 +000063cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception) noexcept
Howard Hinnant7f476b42012-01-22 19:14:27 +000064{
65 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clowfb69a8b2011-08-15 18:06:47 +000066}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000067
Howard Hinnant7f476b42012-01-22 19:14:27 +000068static
69inline
70size_t
Howard Hinnant41df9c82012-01-24 23:42:30 +000071cxa_exception_size_from_exception_thrown_size(size_t size) noexcept
Howard Hinnant7f476b42012-01-22 19:14:27 +000072{
73 return size + sizeof (__cxa_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000074}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000075
Howard Hinnant41df9c82012-01-24 23:42:30 +000076static void setExceptionClass(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnant7f476b42012-01-22 19:14:27 +000077 unwind_exception->exception_class = kOurExceptionClass;
78}
79
Howard Hinnant41df9c82012-01-24 23:42:30 +000080static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnant7f476b42012-01-22 19:14:27 +000081 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000082}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000083
84// Is it one of ours?
Howard Hinnant41df9c82012-01-24 23:42:30 +000085static bool isOurExceptionClass(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnant7f476b42012-01-22 19:14:27 +000086 return(unwind_exception->exception_class == kOurExceptionClass)||
87 (unwind_exception->exception_class == kOurDependentExceptionClass);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000088}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000089
Howard Hinnant41df9c82012-01-24 23:42:30 +000090static bool isDependentException(_Unwind_Exception* unwind_exception) noexcept {
Howard Hinnant7f476b42012-01-22 19:14:27 +000091 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000092}
93
Howard Hinnanta6baba12011-12-08 19:35:18 +000094// This does not need to be atomic
Howard Hinnant41df9c82012-01-24 23:42:30 +000095static inline int incrementHandlerCount(__cxa_exception *exception) noexcept {
Howard Hinnanta6baba12011-12-08 19:35:18 +000096 return ++exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000097}
98
Howard Hinnanta6baba12011-12-08 19:35:18 +000099// This does not need to be atomic
Howard Hinnant41df9c82012-01-24 23:42:30 +0000100static inline int decrementHandlerCount(__cxa_exception *exception) noexcept {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000101 return --exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000102}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000103
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000104#include "fallback_malloc.ipp"
Marshall Clowe0252022011-07-20 15:04:39 +0000105
106// Allocate some memory from _somewhere_
Howard Hinnant41df9c82012-01-24 23:42:30 +0000107static void *do_malloc(size_t size) noexcept {
Marshall Clowfb69a8b2011-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 Clowe0252022011-07-20 15:04:39 +0000111 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000112}
Marshall Clowe0252022011-07-20 15:04:39 +0000113
Howard Hinnant41df9c82012-01-24 23:42:30 +0000114static void do_free(void *ptr) noexcept {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000115 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000116}
Marshall Clowe0252022011-07-20 15:04:39 +0000117
Howard Hinnantb80931e2011-12-07 21:16:40 +0000118/*
Marshall Clow3c54f1b2011-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 Hinnant7f476b42012-01-22 19:14:27 +0000122
123 This is never called for a __cxa_dependent_exception.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000124*/
Howard Hinnant7f476b42012-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 Clowfb69a8b2011-08-15 18:06:47 +0000130 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-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 Clowfb69a8b2011-08-15 18:06:47 +0000136 __cxa_free_exception(thrown_object);
137}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000138
Howard Hinnant41df9c82012-01-24 23:42:30 +0000139static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) noexcept {
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 Hinnant41df9c82012-01-24 23:42:30 +0000158void * __cxa_allocate_exception (size_t thrown_size) noexcept {
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 Hinnant41df9c82012-01-24 23:42:30 +0000169void __cxa_free_exception (void * thrown_object) noexcept {
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 Hinnant41df9c82012-01-24 23:42:30 +0000177void * __cxa_allocate_dependent_exception () noexcept {
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 Hinnant41df9c82012-01-24 23:42:30 +0000189void __cxa_free_dependent_exception (void * dependent_exception) noexcept {
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).
252*/
253void*
Howard Hinnant41df9c82012-01-24 23:42:30 +0000254__cxa_get_exception_ptr(void* unwind_exception) noexcept
Howard Hinnant7f476b42012-01-22 19:14:27 +0000255{
256 return cxa_exception_from_exception_unwind_exception
257 (
258 static_cast<_Unwind_Exception*>(unwind_exception)
259 )->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000260}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000261
262
263/*
264This routine:
265* Increment's the exception's handler count.
266* Places the exception on the stack of currently-caught exceptions if it is not
267 already there, linking the exception to the previous top of the stack.
268* Decrements the uncaught_exception count.
269* Returns the adjusted pointer to the exception object.
270*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000271void*
Howard Hinnant41df9c82012-01-24 23:42:30 +0000272__cxa_begin_catch(void* unwind_exception) noexcept
Howard Hinnant7f476b42012-01-22 19:14:27 +0000273{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000274 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000275 __cxa_exception* exception_header =
276 cxa_exception_from_exception_unwind_exception
277 (
278 static_cast<_Unwind_Exception*>(unwind_exception)
279 );
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000280
Howard Hinnanta6baba12011-12-08 19:35:18 +0000281// TODO: Handle foreign exceptions? How?
282
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000283// Increment the handler count, removing the flag about being rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000284 exception_header->handlerCount = exception_header->handlerCount < 0 ?
285 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000286
287// place the exception on the top of the stack if it's not there.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000288 if (exception_header != globals->caughtExceptions) {
289 exception_header->nextException = globals->caughtExceptions;
290 globals->caughtExceptions = exception_header;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000291 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000292
293 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnant7f476b42012-01-22 19:14:27 +0000294 return exception_header->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000295}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000296
297
298/*
299Upon exit for any reason, a handler must call:
300 void __cxa_end_catch ();
301
302This routine:
303* Locates the most recently caught exception and decrements its handler count.
304* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnanta6baba12011-12-08 19:35:18 +0000305* If the handler count goes down to zero, and the exception was not re-thrown
306 by throw, it locates the primary exception (which may be the same as the one
307 it's handling) and decrements its reference count. If that reference count
308 goes to zero, the function destroys the exception. In any case, if the current
309 exception is a dependent exception, it destroys that.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000310*/
311void __cxa_end_catch() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000312 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
313 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnanta6baba12011-12-08 19:35:18 +0000314 __cxa_eh_globals *globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
Howard Hinnant7f476b42012-01-22 19:14:27 +0000315 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000316
Howard Hinnant7f476b42012-01-22 19:14:27 +0000317 if (NULL != exception_header) {
Howard Hinnant939daa72011-12-12 19:11:42 +0000318 // TODO: Handle foreign exceptions? How?
Howard Hinnant7f476b42012-01-22 19:14:27 +0000319 if (exception_header->handlerCount < 0) {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000320 // The exception has been rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000321 if (0 == incrementHandlerCount(exception_header)) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000322 // Remove from the chain of uncaught exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000323 globals->caughtExceptions = exception_header->nextException;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000324 // but don't destroy
325 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000326 }
Howard Hinnanta6baba12011-12-08 19:35:18 +0000327 else { // The exception has not been rethrown
Howard Hinnant7f476b42012-01-22 19:14:27 +0000328 if (0 == decrementHandlerCount(exception_header)) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000329 // Remove from the chain of uncaught exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000330 globals->caughtExceptions = exception_header->nextException;
331 if (isDependentException(&exception_header->unwindHeader)) {
332 // Reset exception_header to primaryException and deallocate the dependent exception
333 __cxa_dependent_exception* dep_exception_header =
334 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
335 exception_header =
336 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
337 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000338 }
339 // Destroy the primary exception only if its referenceCount goes to 0
340 // (this decrement must be atomic)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000341 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000342 }
343 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000344 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000345}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000346
Howard Hinnant7f476b42012-01-22 19:14:27 +0000347// Note: exception_header may be masquerading as a __cxa_dependent_exception
348// and that's ok. exceptionType is there too.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000349std::type_info * __cxa_current_exception_type() {
350// get the current exception
Marshall Clow143cfb02011-12-22 15:45:05 +0000351 __cxa_eh_globals *globals = __cxa_get_globals_fast();
352 if (NULL == globals)
353 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000354 __cxa_exception *exception_header = globals->caughtExceptions;
355 if (NULL == exception_header)
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000356 return NULL; // No current exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000357 return exception_header->exceptionType;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000358}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000359
360// 2.5.4 Rethrowing Exceptions
361/* This routine
362* marks the exception object on top of the caughtExceptions stack
363 (in an implementation-defined way) as being rethrown.
364* If the caughtExceptions stack is empty, it calls terminate()
365 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant939daa72011-12-12 19:11:42 +0000366* It then calls _Unwind_Resume_or_Rethrow which should not return
367 (terminate if it does).
Howard Hinnant7f476b42012-01-22 19:14:27 +0000368 Note: exception_header may be masquerading as a __cxa_dependent_exception
369 and that's ok.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000370*/
371extern LIBCXXABI_NORETURN void __cxa_rethrow() {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000372 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000373 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000374
Howard Hinnant7f476b42012-01-22 19:14:27 +0000375 if (NULL == exception_header) // there's no current exception!
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000376 std::terminate ();
377
Howard Hinnant939daa72011-12-12 19:11:42 +0000378// TODO: Handle foreign exceptions? How?
Howard Hinnantcbe4c6d2012-01-28 00:34:46 +0000379// Rethrow the foreign exception without touching anything!
Howard Hinnant939daa72011-12-12 19:11:42 +0000380
381// Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000382 exception_header->handlerCount = -exception_header->handlerCount;
Howard Hinnant939daa72011-12-12 19:11:42 +0000383 globals->uncaughtExceptions += 1;
384// __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000385
386#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000387 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000388#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000389 (void) _Unwind_Resume_or_Rethrow (&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000390#endif
391
392// If we get here, some kind of unwinding error has occurred.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000393 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000394}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000395
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000396/*
397 If p is not null, atomically increment the referenceCount field of the
398 __cxa_exception header associated with the thrown object referred to by p.
399*/
400void
Howard Hinnant41df9c82012-01-24 23:42:30 +0000401__cxa_increment_exception_refcount(void* thrown_object) noexcept
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000402{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000403 if (thrown_object != NULL )
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000404 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000405 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
406 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000407 }
408}
409
410/*
411 If p is not null, atomically decrement the referenceCount field of the
412 __cxa_exception header associated with the thrown object referred to by p.
413 If the referenceCount drops to zero, destroy and deallocate the exception.
414*/
415void
Howard Hinnant41df9c82012-01-24 23:42:30 +0000416__cxa_decrement_exception_refcount(void* thrown_object) noexcept
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000417{
418 if (thrown_object != NULL )
419 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000420 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
421 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000422 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000423 if (NULL != exception_header->exceptionDestructor)
424 exception_header->exceptionDestructor(thrown_object);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000425 __cxa_free_exception(thrown_object);
426 }
427 }
428}
429
430/*
431 Returns a pointer to the thrown object (if any) at the top of the
432 caughtExceptions stack. Atommically increment the exception's referenceCount.
433 If there is no such thrown object, returns null.
Marshall Clow15997562012-01-04 22:18:10 +0000434
435 We can use __cxa_get_globals_fast here to get the globals because if there have
436 been no exceptions thrown, ever, on this thread, we can return NULL without
437 the need to allocate the exception-handling globals.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000438*/
439void*
Howard Hinnant41df9c82012-01-24 23:42:30 +0000440__cxa_current_primary_exception() noexcept
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000441{
442// get the current exception
Marshall Clowe80ed7d2012-01-03 23:26:09 +0000443 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow57908522012-01-03 23:10:20 +0000444 if (NULL == globals)
Marshall Clow8989e4e2012-01-04 14:56:09 +0000445 return NULL; // If there are no globals, there is no exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000446 __cxa_exception* exception_header = globals->caughtExceptions;
447 if (NULL == exception_header)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000448 return NULL; // No current exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000449 if (isDependentException(&exception_header->unwindHeader)) {
450 __cxa_dependent_exception* dep_exception_header =
451 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
452 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000453 }
Howard Hinnant7f476b42012-01-22 19:14:27 +0000454 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000455 __cxa_increment_exception_refcount(thrown_object);
456 return thrown_object;
457}
458
459/*
460 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
461 stored in exc is called. Otherwise the referenceCount stored in the
462 primary exception is decremented, destroying the primary if necessary.
463 Finally the dependent exception is destroyed.
464*/
465static
466void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000467dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000468{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000469 __cxa_dependent_exception* dep_exception_header =
470 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000471 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000472 std::__terminate(dep_exception_header->terminateHandler);
473 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
474 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000475}
476
477/*
478 If thrown_object is not null, allocate, initialize and thow a dependent
479 exception.
480*/
481void
482__cxa_rethrow_primary_exception(void* thrown_object)
483{
484 if ( thrown_object != NULL )
485 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000486 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
487 __cxa_dependent_exception* dep_exception_header =
488 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
489 dep_exception_header->primaryException = thrown_object;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000490 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnant7f476b42012-01-22 19:14:27 +0000491 dep_exception_header->exceptionType = exception_header->exceptionType;
492 dep_exception_header->unexpectedHandler = std::get_unexpected();
493 dep_exception_header->terminateHandler = std::get_terminate();
494 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantb9bf50b2011-12-21 23:48:05 +0000495 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000496 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000497#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000498 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000499#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000500 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000501#endif
502 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000503 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000504 }
505 // If we return client will call terminate()
506}
507
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000508bool
Howard Hinnant41df9c82012-01-24 23:42:30 +0000509__cxa_uncaught_exception() noexcept
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000510{
511 __cxa_eh_globals* globals = __cxa_get_globals_fast();
512 if (globals == 0)
513 return false;
514 return globals->uncaughtExceptions != 0;
515}
516
Marshall Clowe0252022011-07-20 15:04:39 +0000517} // extern "C"
518
519} // abi