blob: 2035fae56028d0fbeac0f89b30714805bdcf6791 [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 Hinnantb50cda72012-01-30 16:07:00 +000024#include <stdio.h>
25
Howard Hinnant7f476b42012-01-22 19:14:27 +000026// +---------------------------+-----------------------------+---------------+
27// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
28// +---------------------------+-----------------------------+---------------+
29// ^
30// |
31// +-------------------------------------------------------+
32// |
33// +---------------------------+-----------------------------+
34// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
35// +---------------------------+-----------------------------+
36
Marshall Clowe0252022011-07-20 15:04:39 +000037namespace __cxxabiv1 {
Howard Hinnantb94f2252012-01-24 18:15:20 +000038
Marshall Clowe0252022011-07-20 15:04:39 +000039// Utility routines
Howard Hinnant7f476b42012-01-22 19:14:27 +000040static
41inline
42__cxa_exception*
Howard Hinnantb50cda72012-01-30 16:07:00 +000043cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnant7f476b42012-01-22 19:14:27 +000044{
45 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000046}
Marshall Clowe0252022011-07-20 15:04:39 +000047
Howard Hinnant7f476b42012-01-22 19:14:27 +000048// Note: This is never called when exception_header is masquerading as a
49// __cxa_dependent_exception.
50static
51inline
52void*
Howard Hinnantb50cda72012-01-30 16:07:00 +000053thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnant7f476b42012-01-22 19:14:27 +000054{
55 return static_cast<void*>(exception_header + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000056}
Marshall Clowe0252022011-07-20 15:04:39 +000057
Marshall Clow3c54f1b2011-08-09 15:09:41 +000058// Get the exception object from the unwind pointer.
59// Relies on the structure layout, where the unwind pointer is right in
60// front of the user's exception object
Howard Hinnant7f476b42012-01-22 19:14:27 +000061static
62inline
63__cxa_exception*
Howard Hinnantb50cda72012-01-30 16:07:00 +000064cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnant7f476b42012-01-22 19:14:27 +000065{
66 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clowfb69a8b2011-08-15 18:06:47 +000067}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000068
Howard Hinnant7f476b42012-01-22 19:14:27 +000069static
70inline
71size_t
Howard Hinnantb50cda72012-01-30 16:07:00 +000072cxa_exception_size_from_exception_thrown_size(size_t size)
Howard Hinnant7f476b42012-01-22 19:14:27 +000073{
74 return size + sizeof (__cxa_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000075}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000076
Howard Hinnantb50cda72012-01-30 16:07:00 +000077static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000078 unwind_exception->exception_class = kOurExceptionClass;
79}
80
Howard Hinnantb50cda72012-01-30 16:07:00 +000081static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000082 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000083}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000084
85// Is it one of ours?
Howard Hinnantb50cda72012-01-30 16:07:00 +000086static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
87 return (unwind_exception->exception_class & get_language) ==
88 (kOurExceptionClass & get_language);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000089}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000090
Howard Hinnantb50cda72012-01-30 16:07:00 +000091static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000092 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000093}
94
Howard Hinnanta6baba12011-12-08 19:35:18 +000095// This does not need to be atomic
Howard Hinnantb50cda72012-01-30 16:07:00 +000096static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnanta6baba12011-12-08 19:35:18 +000097 return ++exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000098}
99
Howard Hinnanta6baba12011-12-08 19:35:18 +0000100// This does not need to be atomic
Howard Hinnantb50cda72012-01-30 16:07:00 +0000101static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000102 return --exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000103}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000104
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000105#include "fallback_malloc.ipp"
Marshall Clowe0252022011-07-20 15:04:39 +0000106
107// Allocate some memory from _somewhere_
Howard Hinnantb50cda72012-01-30 16:07:00 +0000108static void *do_malloc(size_t size) {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000109 void *ptr = std::malloc(size);
110 if (NULL == ptr) // if malloc fails, fall back to emergency stash
111 ptr = fallback_malloc(size);
Marshall Clowe0252022011-07-20 15:04:39 +0000112 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000113}
Marshall Clowe0252022011-07-20 15:04:39 +0000114
Howard Hinnantb50cda72012-01-30 16:07:00 +0000115static void do_free(void *ptr) {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000116 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000117}
Marshall Clowe0252022011-07-20 15:04:39 +0000118
Howard Hinnantb80931e2011-12-07 21:16:40 +0000119/*
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000120 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
121 stored in exc is called. Otherwise the exceptionDestructor stored in
122 exc is called, and then the memory for the exception is deallocated.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000123
124 This is never called for a __cxa_dependent_exception.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000125*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000126static
127void
128exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
129{
130 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000131 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000132 std::__terminate(exception_header->terminateHandler);
133
Howard Hinnantb50cda72012-01-30 16:07:00 +0000134 // TODO: Shouldn't this check the reference count first?
Howard Hinnant7f476b42012-01-22 19:14:27 +0000135 void * thrown_object = thrown_object_from_cxa_exception(exception_header);
136 if (NULL != exception_header->exceptionDestructor)
137 exception_header->exceptionDestructor(thrown_object);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000138 __cxa_free_exception(thrown_object);
139}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000140
Howard Hinnantb50cda72012-01-30 16:07:00 +0000141static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000142// Section 2.5.3 says:
143// * For purposes of this ABI, several things are considered exception handlers:
144// ** A terminate() call due to a throw.
145// and
146// * Upon entry, Following initialization of the catch parameter,
147// a handler must call:
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000148// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnant7f476b42012-01-22 19:14:27 +0000149 (void) __cxa_begin_catch(&exception_header->unwindHeader);
150 std::__terminate(exception_header->terminateHandler);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000151}
Marshall Clowe0252022011-07-20 15:04:39 +0000152
153extern "C" {
154
155// Allocate a __cxa_exception object, and zero-fill it.
156// Reserve "thrown_size" bytes on the end for the user's exception
157// object. Zero-fill the object. If memory can't be allocated, call
158// std::terminate. Return a pointer to the memory to be used for the
159// user's exception object.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000160void * __cxa_allocate_exception (size_t thrown_size) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000161 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
162 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size));
163 if (NULL == exception_header)
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000164 std::terminate();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000165 std::memset(exception_header, 0, actual_size);
166 return thrown_object_from_cxa_exception(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000167}
Marshall Clowe0252022011-07-20 15:04:39 +0000168
169
170// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000171void __cxa_free_exception (void * thrown_object) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000172 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000173}
Marshall Clowe0252022011-07-20 15:04:39 +0000174
175
176// This function shall allocate a __cxa_dependent_exception and
177// return a pointer to it. (Really to the object, not past its' end).
178// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000179void * __cxa_allocate_dependent_exception () {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000180 size_t actual_size = sizeof(__cxa_dependent_exception);
181 void *ptr = do_malloc(actual_size);
182 if (NULL == ptr)
183 std::terminate();
184 std::memset(ptr, 0, actual_size);
Marshall Clowe0252022011-07-20 15:04:39 +0000185 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000186}
Marshall Clowe0252022011-07-20 15:04:39 +0000187
188
189// This function shall free a dependent_exception.
190// It does not affect the reference count of the primary exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000191void __cxa_free_dependent_exception (void * dependent_exception) {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000192 do_free(dependent_exception);
193}
Marshall Clowe0252022011-07-20 15:04:39 +0000194
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000195
196// 2.4.3 Throwing the Exception Object
197/*
198After constructing the exception object with the throw argument value,
199the generated code calls the __cxa_throw runtime library routine. This
200routine never returns.
201
202The __cxa_throw routine will do the following:
203
204* Obtain the __cxa_exception header from the thrown exception object address,
205which can be computed as follows:
206 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
207* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
208* Save the tinfo and dest arguments in the __cxa_exception header.
209* Set the exception_class field in the unwind header. This is a 64-bit value
210representing the ASCII string "XXXXC++\0", where "XXXX" is a
211vendor-dependent string. That is, for implementations conforming to this
212ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
213* Increment the uncaught_exception flag.
214* Call _Unwind_RaiseException in the system unwind library, Its argument is the
215pointer to the thrown exception, which __cxa_throw itself received as an argument.
216__Unwind_RaiseException begins the process of stack unwinding, described
217in Section 2.5. In special cases, such as an inability to find a
218handler, _Unwind_RaiseException may return. In that case, __cxa_throw
219will call terminate, assuming that there was no handler for the
220exception.
221*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000222LIBCXXABI_NORETURN
223void
224__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
225{
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000226 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000227 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000228
Howard Hinnant7f476b42012-01-22 19:14:27 +0000229 exception_header->unexpectedHandler = std::get_unexpected();
230 exception_header->terminateHandler = std::get_terminate();
231 exception_header->exceptionType = tinfo;
232 exception_header->exceptionDestructor = dest;
233 setExceptionClass(&exception_header->unwindHeader);
234 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000235 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
236
Howard Hinnant7f476b42012-01-22 19:14:27 +0000237 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Howard Hinnanta6baba12011-12-08 19:35:18 +0000238#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000239 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000240#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000241 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnanta6baba12011-12-08 19:35:18 +0000242#endif
Howard Hinnantcbe4c6d2012-01-28 00:34:46 +0000243 // This only happens when there is no handler, or some unexpected unwinding
244 // error happens.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000245 failed_throw(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000246}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000247
248
249// 2.5.3 Exception Handlers
Howard Hinnant7f476b42012-01-22 19:14:27 +0000250/*
251The adjusted pointer is computed by the personality routine during phase 1
252 and saved in the exception header (either __cxa_exception or
253 __cxa_dependent_exception).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000254
255 Requires: exception is native
Howard Hinnant7f476b42012-01-22 19:14:27 +0000256*/
257void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000258__cxa_get_exception_ptr(void* unwind_exception) throw()
Howard Hinnant7f476b42012-01-22 19:14:27 +0000259{
260 return cxa_exception_from_exception_unwind_exception
261 (
262 static_cast<_Unwind_Exception*>(unwind_exception)
263 )->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000264}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000265
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000266/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000267This routine can catch foreign or native exceptions. If native, the exception
268can be a primary or dependent variety. This routine may remain blissfully
269ignorant of whether the native exception is primary or dependent.
270
271If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000272* Increment's the exception's handler count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000273* Push the exception on the stack of currently-caught exceptions if it is not
274 already there (from a rethrow).
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000275* Decrements the uncaught_exception count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000276* Returns the adjusted pointer to the exception object, which is stored in
277 the __cxa_exception by the personality routine.
278
279If the exception is foreign, this means it did not originate from one of throw
280routines. The foreign exception does not necessarily have a __cxa_exception
281header. However we can catch it here with a catch (...), or with a call
282to terminate or unexpected during unwinding.
283* Do not try to increment the exception's handler count, we don't know where
284 it is.
285* Push the exception on the stack of currently-caught exceptions only if the
286 stack is empty. The foreign exception has no way to link to the current
287 top of stack. If the stack is not empty, call terminate. Even with an
288 empty stack, this is hacked in by pushing a pointer to an imaginary
289 __cxa_exception block in front of the foreign exception. It would be better
290 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
291 doesn't. It has a stack of __cxa_exception (which has a next* in it).
292* Do not decrement the uncaught_exception count because we didn't increment it
293 in __cxa_throw (or one of our rethrow functions).
294* If we haven't terminated, assume the exception object is just past the
295 _Unwind_Exception and return a pointer to that.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000296*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000297void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000298__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnant7f476b42012-01-22 19:14:27 +0000299{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000300printf("entering __cxa_begin_catch\n");
301 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
302 bool native_exception = isOurExceptionClass(unwind_exception);
303 __cxa_eh_globals* globals = __cxa_get_globals();
304 // exception_header is a hackish offset from a foreign exception, but it
305 // works as long as we're careful not to try to access any __cxa_exception
306 // parts.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000307 __cxa_exception* exception_header =
308 cxa_exception_from_exception_unwind_exception
309 (
310 static_cast<_Unwind_Exception*>(unwind_exception)
311 );
Howard Hinnantb50cda72012-01-30 16:07:00 +0000312 if (native_exception)
313 {
314 // Increment the handler count, removing the flag about being rethrown
315 exception_header->handlerCount = exception_header->handlerCount < 0 ?
316 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
317 // place the exception on the top of the stack if it's not already
318 // there by a previous rethrow
319 if (exception_header != globals->caughtExceptions)
320 {
321 exception_header->nextException = globals->caughtExceptions;
322 globals->caughtExceptions = exception_header;
323 }
324 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
325printf("leaving __cxa_begin_catch\n");
326 return exception_header->adjustedPtr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000327 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000328 // Else this is a foreign exception
329 // If the caughtExceptions stack is not empty, terminate
330 if (globals->caughtExceptions != 0)
331 std::terminate();
332 // Push the foreign exception on to the stack
333 globals->caughtExceptions = exception_header;
334 return unwind_exception + 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000335}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000336
337
338/*
339Upon exit for any reason, a handler must call:
340 void __cxa_end_catch ();
341
Howard Hinnantb50cda72012-01-30 16:07:00 +0000342This routine can be called for either a native or foreign exception.
343For a native exception:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000344* Locates the most recently caught exception and decrements its handler count.
345* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnanta6baba12011-12-08 19:35:18 +0000346* If the handler count goes down to zero, and the exception was not re-thrown
347 by throw, it locates the primary exception (which may be the same as the one
348 it's handling) and decrements its reference count. If that reference count
349 goes to zero, the function destroys the exception. In any case, if the current
350 exception is a dependent exception, it destroys that.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000351
352For a foreign exception:
353* If it has been rethrown, there is nothing to do.
354* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000355*/
Howard Hinnantb50cda72012-01-30 16:07:00 +0000356void __cxa_end_catch()
357{
358printf("entering __cxa_end_catch\n");
Howard Hinnant7f476b42012-01-22 19:14:27 +0000359 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
360 "sizeof(__cxa_exception) must be equal to sizeof(__cxa_dependent_exception)");
Howard Hinnantb50cda72012-01-30 16:07:00 +0000361 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
362 __cxa_exception* exception_header = globals->caughtExceptions;
363 // If we've rethrown a foreign exception, then globals->caughtExceptions
364 // will have been made an empty stack by __cxa_rethrow() and there is
365 // nothing more to be done. Do nothing!
366 if (NULL != exception_header)
367 {
368 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
369 if (native_exception)
370 {
371 // This is a native exception
372 if (exception_header->handlerCount < 0)
373 {
374 // The exception has been rethrown by __cxa_rethrow, so don't delete it
375 if (0 == incrementHandlerCount(exception_header))
376 {
377 // Remove from the chain of uncaught exceptions
378 globals->caughtExceptions = exception_header->nextException;
379 // but don't destroy
Howard Hinnanta6baba12011-12-08 19:35:18 +0000380 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000381 // Keep handlerCount negative in case there are nested catch's
382 // that need to be told that this exception is rethrown. Don't
383 // erase this rethrow flag until the exception is recaught.
384 }
385 else
386 {
387 // The native exception has not been rethrown
388 if (0 == decrementHandlerCount(exception_header))
389 {
390 // Remove from the chain of uncaught exceptions
391 globals->caughtExceptions = exception_header->nextException;
392 // Destroy this exception, being careful to distinguish
393 // between dependent and primary exceptions
394 if (isDependentException(&exception_header->unwindHeader))
395 {
396 // Reset exception_header to primaryException and deallocate the dependent exception
397 __cxa_dependent_exception* dep_exception_header =
398 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
399 exception_header =
400 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
401 __cxa_free_dependent_exception(dep_exception_header);
402 }
403 // Destroy the primary exception only if its referenceCount goes to 0
404 // (this decrement must be atomic)
405 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
406 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000407 }
408 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000409 else
410 {
411 // The foreign exception has not been rethrown. Pop the stack
412 // and delete it. If there are nested catch's and they try
413 // to touch a foreign exception in any way, that is undefined
414 // behavior. They likely can't since the only way to catch
415 // a foreign exception is with catch (...)!
416 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
417 globals->caughtExceptions = 0;
418 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000419 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000420printf("leaving __cxa_end_catch\n");
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000421}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000422
Howard Hinnant7f476b42012-01-22 19:14:27 +0000423// Note: exception_header may be masquerading as a __cxa_dependent_exception
424// and that's ok. exceptionType is there too.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000425// However watch out for foreign exceptions. Return null for them.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000426std::type_info * __cxa_current_exception_type() {
427// get the current exception
Marshall Clow143cfb02011-12-22 15:45:05 +0000428 __cxa_eh_globals *globals = __cxa_get_globals_fast();
429 if (NULL == globals)
430 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000431 __cxa_exception *exception_header = globals->caughtExceptions;
432 if (NULL == exception_header)
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000433 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000434 if (!isOurExceptionClass(&exception_header->unwindHeader))
435 return NULL;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000436 return exception_header->exceptionType;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000437}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000438
439// 2.5.4 Rethrowing Exceptions
Howard Hinnantb50cda72012-01-30 16:07:00 +0000440/* This routine can rethrow native or foreign exceptions.
441If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000442* marks the exception object on top of the caughtExceptions stack
443 (in an implementation-defined way) as being rethrown.
444* If the caughtExceptions stack is empty, it calls terminate()
445 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000446* It then calls _Unwind_RaiseException which should not return
Howard Hinnant939daa72011-12-12 19:11:42 +0000447 (terminate if it does).
Howard Hinnant7f476b42012-01-22 19:14:27 +0000448 Note: exception_header may be masquerading as a __cxa_dependent_exception
449 and that's ok.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000450*/
Howard Hinnantb50cda72012-01-30 16:07:00 +0000451LIBCXXABI_NORETURN
452void
453__cxa_rethrow()
454{
455 __cxa_eh_globals* globals = __cxa_get_globals();
456//printf("entering __cxa_rethrow\n");
457 __cxa_exception* exception_header = globals->caughtExceptions;
458 if (NULL == exception_header)
459 std::terminate(); // throw; called outside of a exception handler
460 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
461 if (native_exception)
462 {
463//printf("__cxa_rethrow native branch\n");
464 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
465 exception_header->handlerCount = -exception_header->handlerCount;
466 globals->uncaughtExceptions += 1;
467 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
468 }
469 else // this is a foreign exception
470 {
471 // The only way to communicate to __cxa_end_catch that we've rethrown
472 // a foreign exception, so don't delete us, is to pop the stack here
473 // which must be empty afterwards. Then __cxa_end_catch will do
474 // nothing
475 globals->caughtExceptions = 0;
476 }
477//printf("leaving __cxa_rethrow, private_1 = %lu\n", exception_header->unwindHeader.private_1);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000478#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000479 (void) _Unwind_SjLj_Resume_or_Rethrow(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000480#else
Howard Hinnantb50cda72012-01-30 16:07:00 +0000481 (void)_Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000482#endif
483
Howard Hinnantb50cda72012-01-30 16:07:00 +0000484 // If we get here, some kind of unwinding error has occurred.
485 // There is some weird code generation bug happening with
486 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
487 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
488 __cxa_begin_catch(&exception_header->unwindHeader);
489 if (native_exception)
490 std::__terminate(exception_header->terminateHandler);
491 // Foreign exception: can't get exception_header->terminateHandler
492 std::terminate();
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000493}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000494
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000495/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000496 If thrown_object is not null, atomically increment the referenceCount field
497 of the __cxa_exception header associated with the thrown object referred to
498 by thrown_object.
499
500 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000501*/
502void
Howard Hinnantb50cda72012-01-30 16:07:00 +0000503__cxa_increment_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000504{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000505 if (thrown_object != NULL )
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000506 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000507 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
508 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000509 }
510}
511
512/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000513 If thrown_object is not null, atomically decrement the referenceCount field
514 of the __cxa_exception header associated with the thrown object referred to
515 by thrown_object. If the referenceCount drops to zero, destroy and
516 deallocate the exception.
517
518 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000519*/
520void
Howard Hinnantb50cda72012-01-30 16:07:00 +0000521__cxa_decrement_exception_refcount(void* thrown_object) throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000522{
523 if (thrown_object != NULL )
524 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000525 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
526 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000527 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000528 if (NULL != exception_header->exceptionDestructor)
529 exception_header->exceptionDestructor(thrown_object);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000530 __cxa_free_exception(thrown_object);
531 }
532 }
533}
534
535/*
536 Returns a pointer to the thrown object (if any) at the top of the
537 caughtExceptions stack. Atommically increment the exception's referenceCount.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000538 If there is no such thrown object or if the thrown object is foreign,
539 returns null.
Marshall Clow15997562012-01-04 22:18:10 +0000540
541 We can use __cxa_get_globals_fast here to get the globals because if there have
542 been no exceptions thrown, ever, on this thread, we can return NULL without
543 the need to allocate the exception-handling globals.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000544*/
545void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000546__cxa_current_primary_exception() throw()
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000547{
548// get the current exception
Marshall Clowe80ed7d2012-01-03 23:26:09 +0000549 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow57908522012-01-03 23:10:20 +0000550 if (NULL == globals)
Marshall Clow8989e4e2012-01-04 14:56:09 +0000551 return NULL; // If there are no globals, there is no exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000552 __cxa_exception* exception_header = globals->caughtExceptions;
553 if (NULL == exception_header)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000554 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000555 if (!isOurExceptionClass(&exception_header->unwindHeader))
556 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000557 if (isDependentException(&exception_header->unwindHeader)) {
558 __cxa_dependent_exception* dep_exception_header =
559 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
560 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000561 }
Howard Hinnant7f476b42012-01-22 19:14:27 +0000562 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000563 __cxa_increment_exception_refcount(thrown_object);
564 return thrown_object;
565}
566
567/*
568 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
569 stored in exc is called. Otherwise the referenceCount stored in the
570 primary exception is decremented, destroying the primary if necessary.
571 Finally the dependent exception is destroyed.
572*/
573static
574void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000575dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000576{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000577 __cxa_dependent_exception* dep_exception_header =
578 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000579 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000580 std::__terminate(dep_exception_header->terminateHandler);
581 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
582 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000583}
584
585/*
586 If thrown_object is not null, allocate, initialize and thow a dependent
587 exception.
588*/
589void
590__cxa_rethrow_primary_exception(void* thrown_object)
591{
592 if ( thrown_object != NULL )
593 {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000594 // thrown_object guaranteed to be native because
595 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000596 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
597 __cxa_dependent_exception* dep_exception_header =
598 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
599 dep_exception_header->primaryException = thrown_object;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000600 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnant7f476b42012-01-22 19:14:27 +0000601 dep_exception_header->exceptionType = exception_header->exceptionType;
602 dep_exception_header->unexpectedHandler = std::get_unexpected();
603 dep_exception_header->terminateHandler = std::get_terminate();
604 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantb9bf50b2011-12-21 23:48:05 +0000605 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000606 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000607#if __arm__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000608 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000609#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000610 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000611#endif
612 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000613 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000614 }
615 // If we return client will call terminate()
616}
617
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000618bool
Howard Hinnantb50cda72012-01-30 16:07:00 +0000619__cxa_uncaught_exception() throw()
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000620{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000621 // This does not report foreign exceptions in flight
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000622 __cxa_eh_globals* globals = __cxa_get_globals_fast();
623 if (globals == 0)
624 return false;
625 return globals->uncaughtExceptions != 0;
626}
627
Marshall Clowe0252022011-07-20 15:04:39 +0000628} // extern "C"
629
630} // abi