blob: 3f516c5ef838ab360a3f2cf27c9916d16ced8e6f [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
Howard Hinnant9aa46842012-01-28 00:34:46 +0000241 // This only happens when there is no handler, or some unexpected unwinding
242 // error happens.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000243 failed_throw(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000244}
Marshall Clow87694492011-08-09 15:09:41 +0000245
246
247// 2.5.3 Exception Handlers
Howard Hinnantafcf7ac2012-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 Hinnantabc77062012-01-24 23:42:30 +0000254__cxa_get_exception_ptr(void* unwind_exception) noexcept
Howard Hinnantafcf7ac2012-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 Clow9b454bc2011-08-15 18:06:47 +0000260}
Marshall Clow87694492011-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 Hinnantafcf7ac2012-01-22 19:14:27 +0000271void*
Howard Hinnantabc77062012-01-24 23:42:30 +0000272__cxa_begin_catch(void* unwind_exception) noexcept
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000273{
Marshall Clow9b454bc2011-08-15 18:06:47 +0000274 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnantafcf7ac2012-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 Clow87694492011-08-09 15:09:41 +0000280
Howard Hinnant6ccae152011-12-08 19:35:18 +0000281// TODO: Handle foreign exceptions? How?
282
Marshall Clow87694492011-08-09 15:09:41 +0000283// Increment the handler count, removing the flag about being rethrown
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000284 exception_header->handlerCount = exception_header->handlerCount < 0 ?
285 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
Marshall Clow87694492011-08-09 15:09:41 +0000286
287// place the exception on the top of the stack if it's not there.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000288 if (exception_header != globals->caughtExceptions) {
289 exception_header->nextException = globals->caughtExceptions;
290 globals->caughtExceptions = exception_header;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000291 }
Marshall Clow87694492011-08-09 15:09:41 +0000292
293 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000294 return exception_header->adjustedPtr;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000295}
Marshall Clow87694492011-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 Hinnant6ccae152011-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 Clow87694492011-08-09 15:09:41 +0000310*/
311void __cxa_end_catch() {
Howard Hinnantafcf7ac2012-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 Hinnant6ccae152011-12-08 19:35:18 +0000314 __cxa_eh_globals *globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000315 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow87694492011-08-09 15:09:41 +0000316
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000317 if (NULL != exception_header) {
Howard Hinnant58926c92011-12-12 19:11:42 +0000318 // TODO: Handle foreign exceptions? How?
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000319 if (exception_header->handlerCount < 0) {
Marshall Clow87694492011-08-09 15:09:41 +0000320 // The exception has been rethrown
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000321 if (0 == incrementHandlerCount(exception_header)) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000322 // Remove from the chain of uncaught exceptions
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000323 globals->caughtExceptions = exception_header->nextException;
Howard Hinnant6ccae152011-12-08 19:35:18 +0000324 // but don't destroy
325 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000326 }
Howard Hinnant6ccae152011-12-08 19:35:18 +0000327 else { // The exception has not been rethrown
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000328 if (0 == decrementHandlerCount(exception_header)) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000329 // Remove from the chain of uncaught exceptions
Howard Hinnantafcf7ac2012-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 Hinnant6ccae152011-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 Hinnantafcf7ac2012-01-22 19:14:27 +0000341 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
Marshall Clow9b454bc2011-08-15 18:06:47 +0000342 }
343 }
Marshall Clow87694492011-08-09 15:09:41 +0000344 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000345}
Marshall Clow87694492011-08-09 15:09:41 +0000346
Howard Hinnantafcf7ac2012-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 Clow87694492011-08-09 15:09:41 +0000349std::type_info * __cxa_current_exception_type() {
350// get the current exception
Marshall Clow1de4fc02011-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 Hinnantafcf7ac2012-01-22 19:14:27 +0000354 __cxa_exception *exception_header = globals->caughtExceptions;
355 if (NULL == exception_header)
Marshall Clow87694492011-08-09 15:09:41 +0000356 return NULL; // No current exception
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000357 return exception_header->exceptionType;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000358}
Marshall Clow87694492011-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 Hinnant58926c92011-12-12 19:11:42 +0000366* It then calls _Unwind_Resume_or_Rethrow which should not return
367 (terminate if it does).
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000368 Note: exception_header may be masquerading as a __cxa_dependent_exception
369 and that's ok.
Marshall Clow87694492011-08-09 15:09:41 +0000370*/
371extern LIBCXXABI_NORETURN void __cxa_rethrow() {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000372 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000373 __cxa_exception *exception_header = globals->caughtExceptions;
Marshall Clow87694492011-08-09 15:09:41 +0000374
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000375 if (NULL == exception_header) // there's no current exception!
Marshall Clow87694492011-08-09 15:09:41 +0000376 std::terminate ();
377
Howard Hinnant58926c92011-12-12 19:11:42 +0000378// TODO: Handle foreign exceptions? How?
Howard Hinnant9aa46842012-01-28 00:34:46 +0000379// Rethrow the foreign exception without touching anything!
Howard Hinnant58926c92011-12-12 19:11:42 +0000380
381// Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000382 exception_header->handlerCount = -exception_header->handlerCount;
Howard Hinnant58926c92011-12-12 19:11:42 +0000383 globals->uncaughtExceptions += 1;
384// __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
Marshall Clow87694492011-08-09 15:09:41 +0000385
386#if __arm__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000387 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000388#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000389 (void) _Unwind_Resume_or_Rethrow (&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000390#endif
391
392// If we get here, some kind of unwinding error has occurred.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000393 failed_throw(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000394}
Marshall Clow87694492011-08-09 15:09:41 +0000395
Howard Hinnante04f5162011-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 Hinnantabc77062012-01-24 23:42:30 +0000401__cxa_increment_exception_refcount(void* thrown_object) noexcept
Howard Hinnante04f5162011-12-21 23:32:11 +0000402{
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000403 if (thrown_object != NULL )
Howard Hinnante04f5162011-12-21 23:32:11 +0000404 {
Howard Hinnantafcf7ac2012-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 Hinnante04f5162011-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 Hinnantabc77062012-01-24 23:42:30 +0000416__cxa_decrement_exception_refcount(void* thrown_object) noexcept
Howard Hinnante04f5162011-12-21 23:32:11 +0000417{
418 if (thrown_object != NULL )
419 {
Howard Hinnantafcf7ac2012-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 Hinnante04f5162011-12-21 23:32:11 +0000422 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000423 if (NULL != exception_header->exceptionDestructor)
424 exception_header->exceptionDestructor(thrown_object);
Howard Hinnante04f5162011-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 Clowd2bab352012-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 Hinnante04f5162011-12-21 23:32:11 +0000438*/
439void*
Howard Hinnantabc77062012-01-24 23:42:30 +0000440__cxa_current_primary_exception() noexcept
Howard Hinnante04f5162011-12-21 23:32:11 +0000441{
442// get the current exception
Marshall Clowf83663a2012-01-03 23:26:09 +0000443 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow3e417e72012-01-03 23:10:20 +0000444 if (NULL == globals)
Marshall Clowf3684862012-01-04 14:56:09 +0000445 return NULL; // If there are no globals, there is no exception
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000446 __cxa_exception* exception_header = globals->caughtExceptions;
447 if (NULL == exception_header)
Howard Hinnante04f5162011-12-21 23:32:11 +0000448 return NULL; // No current exception
Howard Hinnantafcf7ac2012-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 Hinnante04f5162011-12-21 23:32:11 +0000453 }
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000454 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnante04f5162011-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 Hinnantafcf7ac2012-01-22 19:14:27 +0000467dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnante04f5162011-12-21 23:32:11 +0000468{
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000469 __cxa_dependent_exception* dep_exception_header =
470 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnante04f5162011-12-21 23:32:11 +0000471 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnantafcf7ac2012-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 Hinnante04f5162011-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 Hinnantafcf7ac2012-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 Hinnante04f5162011-12-21 23:32:11 +0000490 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnantafcf7ac2012-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 Hinnant22f28b22011-12-21 23:48:05 +0000495 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000496 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnante04f5162011-12-21 23:32:11 +0000497#if __arm__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000498 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000499#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000500 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000501#endif
502 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000503 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000504 }
505 // If we return client will call terminate()
506}
507
Howard Hinnante33b2f52012-01-24 00:01:31 +0000508bool
Howard Hinnantabc77062012-01-24 23:42:30 +0000509__cxa_uncaught_exception() noexcept
Howard Hinnante33b2f52012-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 Clowe2dcb752011-07-20 15:04:39 +0000517} // extern "C"
518
519} // abi