blob: 0767ab417f545b707dadfdc82818a84c7ede0bed [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"
Jonathan Roelofsa49ff1d2014-02-12 04:49:09 +000010// http://mentorembedded.github.io/cxx-abi/abi-eh.html
Marshall Clowe0252022011-07-20 15:04:39 +000011//
12//===----------------------------------------------------------------------===//
13
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000014#include "config.h"
Marshall Clowe0252022011-07-20 15:04:39 +000015#include "cxxabi.h"
16
17#include <exception> // for std::terminate
18#include <cstdlib> // for malloc, free
Howard Hinnant9071fe32013-06-17 18:10:34 +000019#include <cstring> // for memset
Jonathan Roelofsaf6a31c2014-09-05 17:46:40 +000020#if !LIBCXXABI_HAS_NO_THREADS
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000021# include <pthread.h> // for fallback_malloc.ipp's mutexes
22#endif
Marshall Clowe0252022011-07-20 15:04:39 +000023#include "cxa_exception.hpp"
Howard Hinnantb80931e2011-12-07 21:16:40 +000024#include "cxa_handlers.hpp"
Marshall Clowe0252022011-07-20 15:04:39 +000025
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
Howard Hinnant8ee279b2012-02-02 20:47:28 +000039#pragma GCC visibility push(default)
40
Marshall Clowe0252022011-07-20 15:04:39 +000041// Utility routines
Howard Hinnant7f476b42012-01-22 19:14:27 +000042static
43inline
44__cxa_exception*
Howard Hinnantb50cda72012-01-30 16:07:00 +000045cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnant7f476b42012-01-22 19:14:27 +000046{
47 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000048}
Marshall Clowe0252022011-07-20 15:04:39 +000049
Howard Hinnant7f476b42012-01-22 19:14:27 +000050// Note: This is never called when exception_header is masquerading as a
51// __cxa_dependent_exception.
52static
53inline
54void*
Howard Hinnantb50cda72012-01-30 16:07:00 +000055thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnant7f476b42012-01-22 19:14:27 +000056{
57 return static_cast<void*>(exception_header + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000058}
Marshall Clowe0252022011-07-20 15:04:39 +000059
Marshall Clow3c54f1b2011-08-09 15:09:41 +000060// Get the exception object from the unwind pointer.
61// Relies on the structure layout, where the unwind pointer is right in
62// front of the user's exception object
Howard Hinnant7f476b42012-01-22 19:14:27 +000063static
64inline
65__cxa_exception*
Howard Hinnantb50cda72012-01-30 16:07:00 +000066cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnant7f476b42012-01-22 19:14:27 +000067{
68 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clowfb69a8b2011-08-15 18:06:47 +000069}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000070
Howard Hinnant7f476b42012-01-22 19:14:27 +000071static
72inline
73size_t
Howard Hinnantb50cda72012-01-30 16:07:00 +000074cxa_exception_size_from_exception_thrown_size(size_t size)
Howard Hinnant7f476b42012-01-22 19:14:27 +000075{
76 return size + sizeof (__cxa_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000077}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000078
Howard Hinnantb50cda72012-01-30 16:07:00 +000079static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000080 unwind_exception->exception_class = kOurExceptionClass;
81}
82
Howard Hinnantb50cda72012-01-30 16:07:00 +000083static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000084 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000085}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000086
87// Is it one of ours?
Howard Hinnantb50cda72012-01-30 16:07:00 +000088static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant1f5bb2c2012-02-01 18:15:15 +000089 return (unwind_exception->exception_class & get_vendor_and_language) ==
90 (kOurExceptionClass & get_vendor_and_language);
Marshall Clowfb69a8b2011-08-15 18:06:47 +000091}
Marshall Clow3c54f1b2011-08-09 15:09:41 +000092
Howard Hinnantb50cda72012-01-30 16:07:00 +000093static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnant7f476b42012-01-22 19:14:27 +000094 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clowfb69a8b2011-08-15 18:06:47 +000095}
96
Howard Hinnanta6baba12011-12-08 19:35:18 +000097// This does not need to be atomic
Howard Hinnantb50cda72012-01-30 16:07:00 +000098static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnanta6baba12011-12-08 19:35:18 +000099 return ++exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000100}
101
Howard Hinnanta6baba12011-12-08 19:35:18 +0000102// This does not need to be atomic
Howard Hinnantb50cda72012-01-30 16:07:00 +0000103static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnanta6baba12011-12-08 19:35:18 +0000104 return --exception->handlerCount;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000105}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000106
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000107#include "fallback_malloc.ipp"
Marshall Clowe0252022011-07-20 15:04:39 +0000108
109// Allocate some memory from _somewhere_
Howard Hinnantb50cda72012-01-30 16:07:00 +0000110static void *do_malloc(size_t size) {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000111 void *ptr = std::malloc(size);
112 if (NULL == ptr) // if malloc fails, fall back to emergency stash
113 ptr = fallback_malloc(size);
Marshall Clowe0252022011-07-20 15:04:39 +0000114 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000115}
Marshall Clowe0252022011-07-20 15:04:39 +0000116
Howard Hinnantb50cda72012-01-30 16:07:00 +0000117static void do_free(void *ptr) {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000118 is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000119}
Marshall Clowe0252022011-07-20 15:04:39 +0000120
Howard Hinnantb80931e2011-12-07 21:16:40 +0000121/*
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000122 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
123 stored in exc is called. Otherwise the exceptionDestructor stored in
124 exc is called, and then the memory for the exception is deallocated.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000125
126 This is never called for a __cxa_dependent_exception.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000127*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000128static
129void
130exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
131{
132 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000133 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000134 std::__terminate(exception_header->terminateHandler);
Howard Hinnantbe01d952012-02-01 18:44:21 +0000135 // Just in case there exists a dependent exception that is pointing to this,
136 // check the reference count and only destroy this if that count goes to zero.
137 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000138}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000139
Howard Hinnantb50cda72012-01-30 16:07:00 +0000140static LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000141// Section 2.5.3 says:
142// * For purposes of this ABI, several things are considered exception handlers:
143// ** A terminate() call due to a throw.
144// and
145// * Upon entry, Following initialization of the catch parameter,
146// a handler must call:
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000147// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnant7f476b42012-01-22 19:14:27 +0000148 (void) __cxa_begin_catch(&exception_header->unwindHeader);
149 std::__terminate(exception_header->terminateHandler);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000150}
Marshall Clowe0252022011-07-20 15:04:39 +0000151
152extern "C" {
153
154// Allocate a __cxa_exception object, and zero-fill it.
155// Reserve "thrown_size" bytes on the end for the user's exception
156// object. Zero-fill the object. If memory can't be allocated, call
157// std::terminate. Return a pointer to the memory to be used for the
158// user's exception object.
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000159void *__cxa_allocate_exception(size_t thrown_size) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000160 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
161 __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size));
162 if (NULL == exception_header)
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000163 std::terminate();
Howard Hinnant7f476b42012-01-22 19:14:27 +0000164 std::memset(exception_header, 0, actual_size);
165 return thrown_object_from_cxa_exception(exception_header);
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000166}
Marshall Clowe0252022011-07-20 15:04:39 +0000167
168
169// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000170void __cxa_free_exception(void *thrown_object) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000171 do_free(cxa_exception_from_thrown_object(thrown_object));
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000172}
Marshall Clowe0252022011-07-20 15:04:39 +0000173
174
175// This function shall allocate a __cxa_dependent_exception and
176// return a pointer to it. (Really to the object, not past its' end).
177// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000178void * __cxa_allocate_dependent_exception () {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000179 size_t actual_size = sizeof(__cxa_dependent_exception);
180 void *ptr = do_malloc(actual_size);
181 if (NULL == ptr)
182 std::terminate();
183 std::memset(ptr, 0, actual_size);
Marshall Clowe0252022011-07-20 15:04:39 +0000184 return ptr;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000185}
Marshall Clowe0252022011-07-20 15:04:39 +0000186
187
188// This function shall free a dependent_exception.
189// It does not affect the reference count of the primary exception.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000190void __cxa_free_dependent_exception (void * dependent_exception) {
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000191 do_free(dependent_exception);
192}
Marshall Clowe0252022011-07-20 15:04:39 +0000193
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000194
195// 2.4.3 Throwing the Exception Object
196/*
197After constructing the exception object with the throw argument value,
198the generated code calls the __cxa_throw runtime library routine. This
199routine never returns.
200
201The __cxa_throw routine will do the following:
202
203* Obtain the __cxa_exception header from the thrown exception object address,
204which can be computed as follows:
205 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
206* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
207* Save the tinfo and dest arguments in the __cxa_exception header.
208* Set the exception_class field in the unwind header. This is a 64-bit value
209representing the ASCII string "XXXXC++\0", where "XXXX" is a
210vendor-dependent string. That is, for implementations conforming to this
211ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
212* Increment the uncaught_exception flag.
213* Call _Unwind_RaiseException in the system unwind library, Its argument is the
214pointer to the thrown exception, which __cxa_throw itself received as an argument.
215__Unwind_RaiseException begins the process of stack unwinding, described
216in Section 2.5. In special cases, such as an inability to find a
217handler, _Unwind_RaiseException may return. In that case, __cxa_throw
218will call terminate, assuming that there was no handler for the
219exception.
220*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000221LIBCXXABI_NORETURN
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000222void __cxa_throw(void *thrown_object, std::type_info *tinfo,
223 void (*dest)(void *)) {
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;
Dan Albert610cde12015-02-05 01:33:15 +0000236#ifdef __USING_SJLJ_EXCEPTIONS__
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).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000252
253 Requires: exception is native
Howard Hinnant7f476b42012-01-22 19:14:27 +0000254*/
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000255void *__cxa_get_exception_ptr(void *unwind_exception) throw() {
Logan Chien30a5e882014-05-10 00:42:10 +0000256#if LIBCXXABI_ARM_EHABI
257 return reinterpret_cast<void*>(
Dan Albert135a4f82015-02-05 23:48:06 +0000258 static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]);
Logan Chien30a5e882014-05-10 00:42:10 +0000259#else
Dan Albert135a4f82015-02-05 23:48:06 +0000260 return cxa_exception_from_exception_unwind_exception(
261 static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr;
Logan Chien30a5e882014-05-10 00:42:10 +0000262#endif
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000263}
Logan Chien30a5e882014-05-10 00:42:10 +0000264
265#if LIBCXXABI_ARM_EHABI
266/*
267The routine to be called before the cleanup. This will save __cxa_exception in
268__cxa_eh_globals, so that __cxa_end_cleanup() can recover later.
269*/
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000270bool __cxa_begin_cleanup(void *unwind_arg) throw() {
Logan Chien30a5e882014-05-10 00:42:10 +0000271 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
272 __cxa_eh_globals* globals = __cxa_get_globals();
273 __cxa_exception* exception_header =
274 cxa_exception_from_exception_unwind_exception(unwind_exception);
275
276 if (isOurExceptionClass(unwind_exception))
277 {
278 if (0 == exception_header->propagationCount)
279 {
280 exception_header->nextPropagatingException = globals->propagatingExceptions;
281 globals->propagatingExceptions = exception_header;
282 }
283 ++exception_header->propagationCount;
284 }
285 else
286 {
287 // If the propagatingExceptions stack is not empty, since we can't
288 // chain the foreign exception, terminate it.
289 if (NULL != globals->propagatingExceptions)
290 std::terminate();
291 globals->propagatingExceptions = exception_header;
292 }
293 return true;
294}
295
296/*
297The routine to be called after the cleanup has been performed. It will get the
298propagating __cxa_exception from __cxa_eh_globals, and continue the stack
299unwinding with _Unwind_Resume.
300
301According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
302register, thus we have to write this function in assembly so that we can save
303{r1, r2, r3}. We don't have to save r0 because it is the return value and the
304first argument to _Unwind_Resume(). In addition, we are saving r4 in order to
305align the stack to 16 bytes, even though it is a callee-save register.
306*/
307__attribute__((used)) static _Unwind_Exception *
308__cxa_end_cleanup_impl()
309{
310 __cxa_eh_globals* globals = __cxa_get_globals();
311 __cxa_exception* exception_header = globals->propagatingExceptions;
312 if (NULL == exception_header)
313 {
314 // It seems that __cxa_begin_cleanup() is not called properly.
315 // We have no choice but terminate the program now.
316 std::terminate();
317 }
318
319 if (isOurExceptionClass(&exception_header->unwindHeader))
320 {
321 --exception_header->propagationCount;
322 if (0 == exception_header->propagationCount)
323 {
324 globals->propagatingExceptions = exception_header->nextPropagatingException;
325 exception_header->nextPropagatingException = NULL;
326 }
327 }
328 else
329 {
330 globals->propagatingExceptions = NULL;
331 }
332 return &exception_header->unwindHeader;
333}
334
335asm (
336 " .pushsection .text.__cxa_end_cleanup\n"
337 " .globl __cxa_end_cleanup\n"
338 " .type __cxa_end_cleanup,%function\n"
339 "__cxa_end_cleanup:\n"
340 " push {r1, r2, r3, r4}\n"
341 " bl __cxa_end_cleanup_impl\n"
342 " pop {r1, r2, r3, r4}\n"
343 " bl _Unwind_Resume\n"
344 " bl abort\n"
345 " .popsection"
346);
347#endif // LIBCXXABI_ARM_EHABI
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000348
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000349/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000350This routine can catch foreign or native exceptions. If native, the exception
351can be a primary or dependent variety. This routine may remain blissfully
352ignorant of whether the native exception is primary or dependent.
353
354If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000355* Increment's the exception's handler count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000356* Push the exception on the stack of currently-caught exceptions if it is not
357 already there (from a rethrow).
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000358* Decrements the uncaught_exception count.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000359* Returns the adjusted pointer to the exception object, which is stored in
360 the __cxa_exception by the personality routine.
361
362If the exception is foreign, this means it did not originate from one of throw
363routines. The foreign exception does not necessarily have a __cxa_exception
364header. However we can catch it here with a catch (...), or with a call
365to terminate or unexpected during unwinding.
366* Do not try to increment the exception's handler count, we don't know where
367 it is.
368* Push the exception on the stack of currently-caught exceptions only if the
369 stack is empty. The foreign exception has no way to link to the current
370 top of stack. If the stack is not empty, call terminate. Even with an
371 empty stack, this is hacked in by pushing a pointer to an imaginary
372 __cxa_exception block in front of the foreign exception. It would be better
373 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
374 doesn't. It has a stack of __cxa_exception (which has a next* in it).
375* Do not decrement the uncaught_exception count because we didn't increment it
376 in __cxa_throw (or one of our rethrow functions).
377* If we haven't terminated, assume the exception object is just past the
378 _Unwind_Exception and return a pointer to that.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000379*/
Howard Hinnant7f476b42012-01-22 19:14:27 +0000380void*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000381__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnant7f476b42012-01-22 19:14:27 +0000382{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000383 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
384 bool native_exception = isOurExceptionClass(unwind_exception);
385 __cxa_eh_globals* globals = __cxa_get_globals();
386 // exception_header is a hackish offset from a foreign exception, but it
387 // works as long as we're careful not to try to access any __cxa_exception
388 // parts.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000389 __cxa_exception* exception_header =
390 cxa_exception_from_exception_unwind_exception
391 (
392 static_cast<_Unwind_Exception*>(unwind_exception)
393 );
Howard Hinnantb50cda72012-01-30 16:07:00 +0000394 if (native_exception)
395 {
396 // Increment the handler count, removing the flag about being rethrown
397 exception_header->handlerCount = exception_header->handlerCount < 0 ?
398 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
399 // place the exception on the top of the stack if it's not already
400 // there by a previous rethrow
401 if (exception_header != globals->caughtExceptions)
402 {
403 exception_header->nextException = globals->caughtExceptions;
404 globals->caughtExceptions = exception_header;
405 }
406 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Logan Chien30a5e882014-05-10 00:42:10 +0000407#if LIBCXXABI_ARM_EHABI
408 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);
409#else
Howard Hinnantb50cda72012-01-30 16:07:00 +0000410 return exception_header->adjustedPtr;
Logan Chien30a5e882014-05-10 00:42:10 +0000411#endif
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000412 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000413 // Else this is a foreign exception
414 // If the caughtExceptions stack is not empty, terminate
415 if (globals->caughtExceptions != 0)
416 std::terminate();
417 // Push the foreign exception on to the stack
418 globals->caughtExceptions = exception_header;
419 return unwind_exception + 1;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000420}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000421
422
423/*
424Upon exit for any reason, a handler must call:
425 void __cxa_end_catch ();
426
Howard Hinnantb50cda72012-01-30 16:07:00 +0000427This routine can be called for either a native or foreign exception.
428For a native exception:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000429* Locates the most recently caught exception and decrements its handler count.
430* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnanta6baba12011-12-08 19:35:18 +0000431* If the handler count goes down to zero, and the exception was not re-thrown
432 by throw, it locates the primary exception (which may be the same as the one
433 it's handling) and decrements its reference count. If that reference count
434 goes to zero, the function destroys the exception. In any case, if the current
435 exception is a dependent exception, it destroys that.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000436
437For a foreign exception:
438* If it has been rethrown, there is nothing to do.
439* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000440*/
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000441void __cxa_end_catch() {
Nico Webercb407bb2014-06-25 23:52:07 +0000442 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
443 "sizeof(__cxa_exception) must be equal to "
444 "sizeof(__cxa_dependent_exception)");
Saleem Abdulrasool5c07ac92015-11-18 05:33:38 +0000445 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==
446 __builtin_offsetof(__cxa_dependent_exception,
447 primaryException),
448 "the layout of __cxa_exception must match the layout of "
449 "__cxa_dependent_exception");
450 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==
451 __builtin_offsetof(__cxa_dependent_exception, handlerCount),
452 "the layout of __cxa_exception must match the layout of "
453 "__cxa_dependent_exception");
Howard Hinnantb50cda72012-01-30 16:07:00 +0000454 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
455 __cxa_exception* exception_header = globals->caughtExceptions;
456 // If we've rethrown a foreign exception, then globals->caughtExceptions
457 // will have been made an empty stack by __cxa_rethrow() and there is
458 // nothing more to be done. Do nothing!
459 if (NULL != exception_header)
460 {
461 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
462 if (native_exception)
463 {
464 // This is a native exception
465 if (exception_header->handlerCount < 0)
466 {
467 // The exception has been rethrown by __cxa_rethrow, so don't delete it
468 if (0 == incrementHandlerCount(exception_header))
469 {
470 // Remove from the chain of uncaught exceptions
471 globals->caughtExceptions = exception_header->nextException;
472 // but don't destroy
Howard Hinnanta6baba12011-12-08 19:35:18 +0000473 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000474 // Keep handlerCount negative in case there are nested catch's
475 // that need to be told that this exception is rethrown. Don't
476 // erase this rethrow flag until the exception is recaught.
477 }
478 else
479 {
480 // The native exception has not been rethrown
481 if (0 == decrementHandlerCount(exception_header))
482 {
483 // Remove from the chain of uncaught exceptions
484 globals->caughtExceptions = exception_header->nextException;
485 // Destroy this exception, being careful to distinguish
486 // between dependent and primary exceptions
487 if (isDependentException(&exception_header->unwindHeader))
488 {
489 // Reset exception_header to primaryException and deallocate the dependent exception
490 __cxa_dependent_exception* dep_exception_header =
491 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
492 exception_header =
493 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
494 __cxa_free_dependent_exception(dep_exception_header);
495 }
496 // Destroy the primary exception only if its referenceCount goes to 0
497 // (this decrement must be atomic)
498 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
499 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000500 }
501 }
Howard Hinnantb50cda72012-01-30 16:07:00 +0000502 else
503 {
504 // The foreign exception has not been rethrown. Pop the stack
505 // and delete it. If there are nested catch's and they try
506 // to touch a foreign exception in any way, that is undefined
507 // behavior. They likely can't since the only way to catch
508 // a foreign exception is with catch (...)!
509 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
510 globals->caughtExceptions = 0;
511 }
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000512 }
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000513}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000514
Howard Hinnant7f476b42012-01-22 19:14:27 +0000515// Note: exception_header may be masquerading as a __cxa_dependent_exception
516// and that's ok. exceptionType is there too.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000517// However watch out for foreign exceptions. Return null for them.
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000518std::type_info *__cxa_current_exception_type() {
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000519// get the current exception
Marshall Clow143cfb02011-12-22 15:45:05 +0000520 __cxa_eh_globals *globals = __cxa_get_globals_fast();
521 if (NULL == globals)
522 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000523 __cxa_exception *exception_header = globals->caughtExceptions;
524 if (NULL == exception_header)
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000525 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000526 if (!isOurExceptionClass(&exception_header->unwindHeader))
527 return NULL;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000528 return exception_header->exceptionType;
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000529}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000530
531// 2.5.4 Rethrowing Exceptions
Howard Hinnantb50cda72012-01-30 16:07:00 +0000532/* This routine can rethrow native or foreign exceptions.
533If the exception is native:
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000534* marks the exception object on top of the caughtExceptions stack
535 (in an implementation-defined way) as being rethrown.
536* If the caughtExceptions stack is empty, it calls terminate()
537 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnantb50cda72012-01-30 16:07:00 +0000538* It then calls _Unwind_RaiseException which should not return
Howard Hinnant939daa72011-12-12 19:11:42 +0000539 (terminate if it does).
Howard Hinnant7f476b42012-01-22 19:14:27 +0000540 Note: exception_header may be masquerading as a __cxa_dependent_exception
541 and that's ok.
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000542*/
Howard Hinnantb50cda72012-01-30 16:07:00 +0000543LIBCXXABI_NORETURN
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000544void __cxa_rethrow() {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000545 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnantb50cda72012-01-30 16:07:00 +0000546 __cxa_exception* exception_header = globals->caughtExceptions;
547 if (NULL == exception_header)
548 std::terminate(); // throw; called outside of a exception handler
549 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
550 if (native_exception)
551 {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000552 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
553 exception_header->handlerCount = -exception_header->handlerCount;
554 globals->uncaughtExceptions += 1;
555 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
556 }
557 else // this is a foreign exception
558 {
559 // The only way to communicate to __cxa_end_catch that we've rethrown
560 // a foreign exception, so don't delete us, is to pop the stack here
561 // which must be empty afterwards. Then __cxa_end_catch will do
562 // nothing
563 globals->caughtExceptions = 0;
564 }
Dan Albert610cde12015-02-05 01:33:15 +0000565#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnantb36b2c52012-02-29 22:14:19 +0000566 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000567#else
Howard Hinnantb36b2c52012-02-29 22:14:19 +0000568 _Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000569#endif
570
Howard Hinnantb50cda72012-01-30 16:07:00 +0000571 // If we get here, some kind of unwinding error has occurred.
572 // There is some weird code generation bug happening with
573 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
574 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
575 __cxa_begin_catch(&exception_header->unwindHeader);
576 if (native_exception)
577 std::__terminate(exception_header->terminateHandler);
578 // Foreign exception: can't get exception_header->terminateHandler
579 std::terminate();
Marshall Clowfb69a8b2011-08-15 18:06:47 +0000580}
Marshall Clow3c54f1b2011-08-09 15:09:41 +0000581
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000582/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000583 If thrown_object is not null, atomically increment the referenceCount field
584 of the __cxa_exception header associated with the thrown object referred to
585 by thrown_object.
586
587 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000588*/
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000589void __cxa_increment_exception_refcount(void *thrown_object) throw() {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000590 if (thrown_object != NULL )
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000591 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000592 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
593 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000594 }
595}
596
597/*
Howard Hinnantb50cda72012-01-30 16:07:00 +0000598 If thrown_object is not null, atomically decrement the referenceCount field
599 of the __cxa_exception header associated with the thrown object referred to
600 by thrown_object. If the referenceCount drops to zero, destroy and
601 deallocate the exception.
602
603 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000604*/
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000605void __cxa_decrement_exception_refcount(void *thrown_object) throw() {
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000606 if (thrown_object != NULL )
607 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000608 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
609 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000610 {
Howard Hinnant7f476b42012-01-22 19:14:27 +0000611 if (NULL != exception_header->exceptionDestructor)
612 exception_header->exceptionDestructor(thrown_object);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000613 __cxa_free_exception(thrown_object);
614 }
615 }
616}
617
618/*
619 Returns a pointer to the thrown object (if any) at the top of the
Howard Hinnantb7118a72013-02-15 15:48:49 +0000620 caughtExceptions stack. Atomically increment the exception's referenceCount.
Howard Hinnantb50cda72012-01-30 16:07:00 +0000621 If there is no such thrown object or if the thrown object is foreign,
622 returns null.
Marshall Clow15997562012-01-04 22:18:10 +0000623
624 We can use __cxa_get_globals_fast here to get the globals because if there have
625 been no exceptions thrown, ever, on this thread, we can return NULL without
626 the need to allocate the exception-handling globals.
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000627*/
Saleem Abdulrasoole5cf2752015-12-04 02:14:41 +0000628void *__cxa_current_primary_exception() throw() {
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000629// get the current exception
Marshall Clowe80ed7d2012-01-03 23:26:09 +0000630 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow57908522012-01-03 23:10:20 +0000631 if (NULL == globals)
Marshall Clow8989e4e2012-01-04 14:56:09 +0000632 return NULL; // If there are no globals, there is no exception
Howard Hinnant7f476b42012-01-22 19:14:27 +0000633 __cxa_exception* exception_header = globals->caughtExceptions;
634 if (NULL == exception_header)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000635 return NULL; // No current exception
Howard Hinnantb50cda72012-01-30 16:07:00 +0000636 if (!isOurExceptionClass(&exception_header->unwindHeader))
637 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000638 if (isDependentException(&exception_header->unwindHeader)) {
639 __cxa_dependent_exception* dep_exception_header =
640 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
641 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000642 }
Howard Hinnant7f476b42012-01-22 19:14:27 +0000643 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000644 __cxa_increment_exception_refcount(thrown_object);
645 return thrown_object;
646}
647
648/*
649 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
650 stored in exc is called. Otherwise the referenceCount stored in the
651 primary exception is decremented, destroying the primary if necessary.
652 Finally the dependent exception is destroyed.
653*/
654static
655void
Howard Hinnant7f476b42012-01-22 19:14:27 +0000656dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000657{
Howard Hinnant7f476b42012-01-22 19:14:27 +0000658 __cxa_dependent_exception* dep_exception_header =
659 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000660 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnant7f476b42012-01-22 19:14:27 +0000661 std::__terminate(dep_exception_header->terminateHandler);
662 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
663 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000664}
665
666/*
Howard Hinnantb7118a72013-02-15 15:48:49 +0000667 If thrown_object is not null, allocate, initialize and throw a dependent
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000668 exception.
669*/
670void
671__cxa_rethrow_primary_exception(void* thrown_object)
672{
673 if ( thrown_object != NULL )
674 {
Howard Hinnantb50cda72012-01-30 16:07:00 +0000675 // thrown_object guaranteed to be native because
676 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnant7f476b42012-01-22 19:14:27 +0000677 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
678 __cxa_dependent_exception* dep_exception_header =
679 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
680 dep_exception_header->primaryException = thrown_object;
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000681 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnant7f476b42012-01-22 19:14:27 +0000682 dep_exception_header->exceptionType = exception_header->exceptionType;
683 dep_exception_header->unexpectedHandler = std::get_unexpected();
684 dep_exception_header->terminateHandler = std::get_terminate();
685 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantb9bf50b2011-12-21 23:48:05 +0000686 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnant7f476b42012-01-22 19:14:27 +0000687 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Dan Albert610cde12015-02-05 01:33:15 +0000688#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f476b42012-01-22 19:14:27 +0000689 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000690#else
Howard Hinnant7f476b42012-01-22 19:14:27 +0000691 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000692#endif
693 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnant7f476b42012-01-22 19:14:27 +0000694 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnantf7b47f72011-12-21 23:32:11 +0000695 }
696 // If we return client will call terminate()
697}
698
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000699bool
Marshall Clow701adfd2015-06-02 13:03:17 +0000700__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }
701
702unsigned int
703__cxa_uncaught_exceptions() throw()
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000704{
Howard Hinnantb50cda72012-01-30 16:07:00 +0000705 // This does not report foreign exceptions in flight
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000706 __cxa_eh_globals* globals = __cxa_get_globals_fast();
707 if (globals == 0)
Marshall Clow701adfd2015-06-02 13:03:17 +0000708 return 0;
709 return globals->uncaughtExceptions;
Howard Hinnantf710f1e2012-01-24 00:01:31 +0000710}
711
Marshall Clowe0252022011-07-20 15:04:39 +0000712} // extern "C"
713
Howard Hinnant8ee279b2012-02-02 20:47:28 +0000714#pragma GCC visibility pop
715
Marshall Clowe0252022011-07-20 15:04:39 +0000716} // abi