blob: 08d5627d0528720630d49052d9b16ff7d9116c3c [file] [log] [blame]
Marshall Clow47c29eb2011-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 Roelofsc82e02d2014-02-12 04:49:09 +000010// http://mentorembedded.github.io/cxx-abi/abi-eh.html
Marshall Clow47c29eb2011-07-20 15:04:39 +000011//
12//===----------------------------------------------------------------------===//
13
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000014#include "config.h"
Marshall Clow47c29eb2011-07-20 15:04:39 +000015#include "cxxabi.h"
16
17#include <exception> // for std::terminate
Howard Hinnant6c33e762013-06-17 18:10:34 +000018#include <cstring> // for memset
Marshall Clow47c29eb2011-07-20 15:04:39 +000019#include "cxa_exception.hpp"
Howard Hinnante8fcf832011-12-07 21:16:40 +000020#include "cxa_handlers.hpp"
Igor Kudrinace65722016-10-07 08:48:28 +000021#include "fallback_malloc.h"
Marshall Clow47c29eb2011-07-20 15:04:39 +000022
Howard Hinnanta36fb302012-01-22 19:14:27 +000023// +---------------------------+-----------------------------+---------------+
24// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
25// +---------------------------+-----------------------------+---------------+
26// ^
27// |
28// +-------------------------------------------------------+
29// |
30// +---------------------------+-----------------------------+
31// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
32// +---------------------------+-----------------------------+
33
Marshall Clow47c29eb2011-07-20 15:04:39 +000034namespace __cxxabiv1 {
Howard Hinnant2a70c102012-01-24 18:15:20 +000035
Howard Hinnant02406852012-02-02 20:47:28 +000036#pragma GCC visibility push(default)
37
Marshall Clow47c29eb2011-07-20 15:04:39 +000038// Utility routines
Howard Hinnanta36fb302012-01-22 19:14:27 +000039static
40inline
41__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000042cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnanta36fb302012-01-22 19:14:27 +000043{
44 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clow7ac31262011-08-15 18:06:47 +000045}
Marshall Clow47c29eb2011-07-20 15:04:39 +000046
Howard Hinnanta36fb302012-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 Hinnant7da9b962012-01-30 16:07:00 +000052thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnanta36fb302012-01-22 19:14:27 +000053{
54 return static_cast<void*>(exception_header + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +000055}
Marshall Clow47c29eb2011-07-20 15:04:39 +000056
Marshall Clow7c4652f2011-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 Hinnanta36fb302012-01-22 19:14:27 +000060static
61inline
62__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000063cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnanta36fb302012-01-22 19:14:27 +000064{
65 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clow7ac31262011-08-15 18:06:47 +000066}
Marshall Clow7c4652f2011-08-09 15:09:41 +000067
Howard Hinnanta36fb302012-01-22 19:14:27 +000068static
69inline
70size_t
Howard Hinnant7da9b962012-01-30 16:07:00 +000071cxa_exception_size_from_exception_thrown_size(size_t size)
Howard Hinnanta36fb302012-01-22 19:14:27 +000072{
73 return size + sizeof (__cxa_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +000074}
Marshall Clow7c4652f2011-08-09 15:09:41 +000075
Howard Hinnant7da9b962012-01-30 16:07:00 +000076static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000077 unwind_exception->exception_class = kOurExceptionClass;
78}
79
Howard Hinnant7da9b962012-01-30 16:07:00 +000080static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000081 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clow7ac31262011-08-15 18:06:47 +000082}
Marshall Clow7c4652f2011-08-09 15:09:41 +000083
84// Is it one of ours?
Howard Hinnant7da9b962012-01-30 16:07:00 +000085static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant7ab185e2012-02-01 18:15:15 +000086 return (unwind_exception->exception_class & get_vendor_and_language) ==
87 (kOurExceptionClass & get_vendor_and_language);
Marshall Clow7ac31262011-08-15 18:06:47 +000088}
Marshall Clow7c4652f2011-08-09 15:09:41 +000089
Howard Hinnant7da9b962012-01-30 16:07:00 +000090static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000091 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clow7ac31262011-08-15 18:06:47 +000092}
93
Howard Hinnant6eb54ad2011-12-08 19:35:18 +000094// This does not need to be atomic
Howard Hinnant7da9b962012-01-30 16:07:00 +000095static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +000096 return ++exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +000097}
98
Howard Hinnant6eb54ad2011-12-08 19:35:18 +000099// This does not need to be atomic
Howard Hinnant7da9b962012-01-30 16:07:00 +0000100static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000101 return --exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +0000102}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000103
Howard Hinnante8fcf832011-12-07 21:16:40 +0000104/*
Marshall Clow7c4652f2011-08-09 15:09:41 +0000105 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
106 stored in exc is called. Otherwise the exceptionDestructor stored in
107 exc is called, and then the memory for the exception is deallocated.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000108
109 This is never called for a __cxa_dependent_exception.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000110*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000111static
112void
113exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
114{
115 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +0000116 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000117 std::__terminate(exception_header->terminateHandler);
Howard Hinnant8dd7a482012-02-01 18:44:21 +0000118 // Just in case there exists a dependent exception that is pointing to this,
119 // check the reference count and only destroy this if that count goes to zero.
120 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +0000121}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000122
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000123static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow7c4652f2011-08-09 15:09:41 +0000124// Section 2.5.3 says:
125// * For purposes of this ABI, several things are considered exception handlers:
126// ** A terminate() call due to a throw.
127// and
128// * Upon entry, Following initialization of the catch parameter,
129// a handler must call:
Marshall Clow7ac31262011-08-15 18:06:47 +0000130// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnanta36fb302012-01-22 19:14:27 +0000131 (void) __cxa_begin_catch(&exception_header->unwindHeader);
132 std::__terminate(exception_header->terminateHandler);
Marshall Clow7ac31262011-08-15 18:06:47 +0000133}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000134
135extern "C" {
136
137// Allocate a __cxa_exception object, and zero-fill it.
138// Reserve "thrown_size" bytes on the end for the user's exception
139// object. Zero-fill the object. If memory can't be allocated, call
140// std::terminate. Return a pointer to the memory to be used for the
141// user's exception object.
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000142_LIBCXXABI_FUNC_VIS void *__cxa_allocate_exception(size_t thrown_size) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000143 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
Igor Kudrinace65722016-10-07 08:48:28 +0000144 __cxa_exception *exception_header =
145 static_cast<__cxa_exception *>(__malloc_with_fallback(actual_size));
Howard Hinnanta36fb302012-01-22 19:14:27 +0000146 if (NULL == exception_header)
Marshall Clow7ac31262011-08-15 18:06:47 +0000147 std::terminate();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000148 std::memset(exception_header, 0, actual_size);
149 return thrown_object_from_cxa_exception(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000150}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000151
152
153// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000154_LIBCXXABI_FUNC_VIS void __cxa_free_exception(void *thrown_object) throw() {
Igor Kudrinace65722016-10-07 08:48:28 +0000155 __free_with_fallback(cxa_exception_from_thrown_object(thrown_object));
Marshall Clow7ac31262011-08-15 18:06:47 +0000156}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000157
158
159// This function shall allocate a __cxa_dependent_exception and
160// return a pointer to it. (Really to the object, not past its' end).
161// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000162void * __cxa_allocate_dependent_exception () {
Marshall Clow7ac31262011-08-15 18:06:47 +0000163 size_t actual_size = sizeof(__cxa_dependent_exception);
Igor Kudrinace65722016-10-07 08:48:28 +0000164 void *ptr = __malloc_with_fallback(actual_size);
Marshall Clow7ac31262011-08-15 18:06:47 +0000165 if (NULL == ptr)
166 std::terminate();
167 std::memset(ptr, 0, actual_size);
Marshall Clow47c29eb2011-07-20 15:04:39 +0000168 return ptr;
Marshall Clow7ac31262011-08-15 18:06:47 +0000169}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000170
171
172// This function shall free a dependent_exception.
173// It does not affect the reference count of the primary exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000174void __cxa_free_dependent_exception (void * dependent_exception) {
Igor Kudrinace65722016-10-07 08:48:28 +0000175 __free_with_fallback(dependent_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +0000176}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000177
Marshall Clow7c4652f2011-08-09 15:09:41 +0000178
179// 2.4.3 Throwing the Exception Object
180/*
181After constructing the exception object with the throw argument value,
182the generated code calls the __cxa_throw runtime library routine. This
183routine never returns.
184
185The __cxa_throw routine will do the following:
186
187* Obtain the __cxa_exception header from the thrown exception object address,
188which can be computed as follows:
189 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
190* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
191* Save the tinfo and dest arguments in the __cxa_exception header.
192* Set the exception_class field in the unwind header. This is a 64-bit value
193representing the ASCII string "XXXXC++\0", where "XXXX" is a
194vendor-dependent string. That is, for implementations conforming to this
195ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
196* Increment the uncaught_exception flag.
197* Call _Unwind_RaiseException in the system unwind library, Its argument is the
198pointer to the thrown exception, which __cxa_throw itself received as an argument.
199__Unwind_RaiseException begins the process of stack unwinding, described
200in Section 2.5. In special cases, such as an inability to find a
201handler, _Unwind_RaiseException may return. In that case, __cxa_throw
202will call terminate, assuming that there was no handler for the
203exception.
204*/
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000205_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000206__cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {
Marshall Clow7ac31262011-08-15 18:06:47 +0000207 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000208 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000209
Howard Hinnanta36fb302012-01-22 19:14:27 +0000210 exception_header->unexpectedHandler = std::get_unexpected();
211 exception_header->terminateHandler = std::get_terminate();
212 exception_header->exceptionType = tinfo;
213 exception_header->exceptionDestructor = dest;
214 setExceptionClass(&exception_header->unwindHeader);
215 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000216 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
217
Howard Hinnanta36fb302012-01-22 19:14:27 +0000218 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Dan Alberta1fce462015-02-05 01:33:15 +0000219#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000220 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000221#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000222 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000223#endif
Howard Hinnant978bfde2012-01-28 00:34:46 +0000224 // This only happens when there is no handler, or some unexpected unwinding
225 // error happens.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000226 failed_throw(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000227}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000228
229
230// 2.5.3 Exception Handlers
Howard Hinnanta36fb302012-01-22 19:14:27 +0000231/*
232The adjusted pointer is computed by the personality routine during phase 1
233 and saved in the exception header (either __cxa_exception or
234 __cxa_dependent_exception).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000235
236 Requires: exception is native
Howard Hinnanta36fb302012-01-22 19:14:27 +0000237*/
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000238_LIBCXXABI_FUNC_VIS
Saleem Abdulrasool77a304b2015-12-04 02:14:41 +0000239void *__cxa_get_exception_ptr(void *unwind_exception) throw() {
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000240#if _LIBCXXABI_ARM_EHABI
Logan Chien05d51bc2014-05-10 00:42:10 +0000241 return reinterpret_cast<void*>(
Dan Albertda5a6b12015-02-05 23:48:06 +0000242 static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]);
Logan Chien05d51bc2014-05-10 00:42:10 +0000243#else
Dan Albertda5a6b12015-02-05 23:48:06 +0000244 return cxa_exception_from_exception_unwind_exception(
245 static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr;
Logan Chien05d51bc2014-05-10 00:42:10 +0000246#endif
Marshall Clow7ac31262011-08-15 18:06:47 +0000247}
Logan Chien05d51bc2014-05-10 00:42:10 +0000248
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000249#if _LIBCXXABI_ARM_EHABI
Logan Chien05d51bc2014-05-10 00:42:10 +0000250/*
251The routine to be called before the cleanup. This will save __cxa_exception in
252__cxa_eh_globals, so that __cxa_end_cleanup() can recover later.
253*/
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000254_LIBCXXABI_FUNC_VIS
Saleem Abdulrasool77a304b2015-12-04 02:14:41 +0000255bool __cxa_begin_cleanup(void *unwind_arg) throw() {
Logan Chien05d51bc2014-05-10 00:42:10 +0000256 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
257 __cxa_eh_globals* globals = __cxa_get_globals();
258 __cxa_exception* exception_header =
259 cxa_exception_from_exception_unwind_exception(unwind_exception);
260
261 if (isOurExceptionClass(unwind_exception))
262 {
263 if (0 == exception_header->propagationCount)
264 {
265 exception_header->nextPropagatingException = globals->propagatingExceptions;
266 globals->propagatingExceptions = exception_header;
267 }
268 ++exception_header->propagationCount;
269 }
270 else
271 {
272 // If the propagatingExceptions stack is not empty, since we can't
273 // chain the foreign exception, terminate it.
274 if (NULL != globals->propagatingExceptions)
275 std::terminate();
276 globals->propagatingExceptions = exception_header;
277 }
278 return true;
279}
280
281/*
282The routine to be called after the cleanup has been performed. It will get the
283propagating __cxa_exception from __cxa_eh_globals, and continue the stack
284unwinding with _Unwind_Resume.
285
286According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
287register, thus we have to write this function in assembly so that we can save
288{r1, r2, r3}. We don't have to save r0 because it is the return value and the
289first argument to _Unwind_Resume(). In addition, we are saving r4 in order to
290align the stack to 16 bytes, even though it is a callee-save register.
291*/
292__attribute__((used)) static _Unwind_Exception *
293__cxa_end_cleanup_impl()
294{
295 __cxa_eh_globals* globals = __cxa_get_globals();
296 __cxa_exception* exception_header = globals->propagatingExceptions;
297 if (NULL == exception_header)
298 {
299 // It seems that __cxa_begin_cleanup() is not called properly.
300 // We have no choice but terminate the program now.
301 std::terminate();
302 }
303
304 if (isOurExceptionClass(&exception_header->unwindHeader))
305 {
306 --exception_header->propagationCount;
307 if (0 == exception_header->propagationCount)
308 {
309 globals->propagatingExceptions = exception_header->nextPropagatingException;
310 exception_header->nextPropagatingException = NULL;
311 }
312 }
313 else
314 {
315 globals->propagatingExceptions = NULL;
316 }
317 return &exception_header->unwindHeader;
318}
319
320asm (
Logan Chienf67395c2015-12-22 14:38:30 +0000321 " .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
Logan Chien05d51bc2014-05-10 00:42:10 +0000322 " .globl __cxa_end_cleanup\n"
323 " .type __cxa_end_cleanup,%function\n"
324 "__cxa_end_cleanup:\n"
325 " push {r1, r2, r3, r4}\n"
326 " bl __cxa_end_cleanup_impl\n"
327 " pop {r1, r2, r3, r4}\n"
328 " bl _Unwind_Resume\n"
329 " bl abort\n"
330 " .popsection"
331);
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000332#endif // _LIBCXXABI_ARM_EHABI
Marshall Clow7c4652f2011-08-09 15:09:41 +0000333
Marshall Clow7c4652f2011-08-09 15:09:41 +0000334/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000335This routine can catch foreign or native exceptions. If native, the exception
336can be a primary or dependent variety. This routine may remain blissfully
337ignorant of whether the native exception is primary or dependent.
338
339If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000340* Increment's the exception's handler count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000341* Push the exception on the stack of currently-caught exceptions if it is not
342 already there (from a rethrow).
Marshall Clow7c4652f2011-08-09 15:09:41 +0000343* Decrements the uncaught_exception count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000344* Returns the adjusted pointer to the exception object, which is stored in
345 the __cxa_exception by the personality routine.
346
347If the exception is foreign, this means it did not originate from one of throw
348routines. The foreign exception does not necessarily have a __cxa_exception
349header. However we can catch it here with a catch (...), or with a call
350to terminate or unexpected during unwinding.
351* Do not try to increment the exception's handler count, we don't know where
352 it is.
353* Push the exception on the stack of currently-caught exceptions only if the
354 stack is empty. The foreign exception has no way to link to the current
355 top of stack. If the stack is not empty, call terminate. Even with an
356 empty stack, this is hacked in by pushing a pointer to an imaginary
357 __cxa_exception block in front of the foreign exception. It would be better
358 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
359 doesn't. It has a stack of __cxa_exception (which has a next* in it).
360* Do not decrement the uncaught_exception count because we didn't increment it
361 in __cxa_throw (or one of our rethrow functions).
362* If we haven't terminated, assume the exception object is just past the
363 _Unwind_Exception and return a pointer to that.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000364*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000365void*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000366__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnanta36fb302012-01-22 19:14:27 +0000367{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000368 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
369 bool native_exception = isOurExceptionClass(unwind_exception);
370 __cxa_eh_globals* globals = __cxa_get_globals();
371 // exception_header is a hackish offset from a foreign exception, but it
372 // works as long as we're careful not to try to access any __cxa_exception
373 // parts.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000374 __cxa_exception* exception_header =
375 cxa_exception_from_exception_unwind_exception
376 (
377 static_cast<_Unwind_Exception*>(unwind_exception)
378 );
Howard Hinnant7da9b962012-01-30 16:07:00 +0000379 if (native_exception)
380 {
381 // Increment the handler count, removing the flag about being rethrown
382 exception_header->handlerCount = exception_header->handlerCount < 0 ?
383 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
384 // place the exception on the top of the stack if it's not already
385 // there by a previous rethrow
386 if (exception_header != globals->caughtExceptions)
387 {
388 exception_header->nextException = globals->caughtExceptions;
389 globals->caughtExceptions = exception_header;
390 }
391 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000392#if _LIBCXXABI_ARM_EHABI
Logan Chien05d51bc2014-05-10 00:42:10 +0000393 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);
394#else
Howard Hinnant7da9b962012-01-30 16:07:00 +0000395 return exception_header->adjustedPtr;
Logan Chien05d51bc2014-05-10 00:42:10 +0000396#endif
Marshall Clow7ac31262011-08-15 18:06:47 +0000397 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000398 // Else this is a foreign exception
399 // If the caughtExceptions stack is not empty, terminate
400 if (globals->caughtExceptions != 0)
401 std::terminate();
402 // Push the foreign exception on to the stack
403 globals->caughtExceptions = exception_header;
404 return unwind_exception + 1;
Marshall Clow7ac31262011-08-15 18:06:47 +0000405}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000406
407
408/*
409Upon exit for any reason, a handler must call:
410 void __cxa_end_catch ();
411
Howard Hinnant7da9b962012-01-30 16:07:00 +0000412This routine can be called for either a native or foreign exception.
413For a native exception:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000414* Locates the most recently caught exception and decrements its handler count.
415* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000416* If the handler count goes down to zero, and the exception was not re-thrown
417 by throw, it locates the primary exception (which may be the same as the one
418 it's handling) and decrements its reference count. If that reference count
419 goes to zero, the function destroys the exception. In any case, if the current
420 exception is a dependent exception, it destroys that.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000421
422For a foreign exception:
423* If it has been rethrown, there is nothing to do.
424* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000425*/
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000426_LIBCXXABI_FUNC_VIS void __cxa_end_catch() {
Nico Weberd6e23362014-06-25 23:52:07 +0000427 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
428 "sizeof(__cxa_exception) must be equal to "
429 "sizeof(__cxa_dependent_exception)");
Saleem Abdulrasool4c91e382015-11-18 05:33:38 +0000430 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==
431 __builtin_offsetof(__cxa_dependent_exception,
432 primaryException),
433 "the layout of __cxa_exception must match the layout of "
434 "__cxa_dependent_exception");
435 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==
436 __builtin_offsetof(__cxa_dependent_exception, handlerCount),
437 "the layout of __cxa_exception must match the layout of "
438 "__cxa_dependent_exception");
Howard Hinnant7da9b962012-01-30 16:07:00 +0000439 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
440 __cxa_exception* exception_header = globals->caughtExceptions;
441 // If we've rethrown a foreign exception, then globals->caughtExceptions
442 // will have been made an empty stack by __cxa_rethrow() and there is
443 // nothing more to be done. Do nothing!
444 if (NULL != exception_header)
445 {
446 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
447 if (native_exception)
448 {
449 // This is a native exception
450 if (exception_header->handlerCount < 0)
451 {
452 // The exception has been rethrown by __cxa_rethrow, so don't delete it
453 if (0 == incrementHandlerCount(exception_header))
454 {
455 // Remove from the chain of uncaught exceptions
456 globals->caughtExceptions = exception_header->nextException;
457 // but don't destroy
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000458 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000459 // Keep handlerCount negative in case there are nested catch's
460 // that need to be told that this exception is rethrown. Don't
461 // erase this rethrow flag until the exception is recaught.
462 }
463 else
464 {
465 // The native exception has not been rethrown
466 if (0 == decrementHandlerCount(exception_header))
467 {
468 // Remove from the chain of uncaught exceptions
469 globals->caughtExceptions = exception_header->nextException;
470 // Destroy this exception, being careful to distinguish
471 // between dependent and primary exceptions
472 if (isDependentException(&exception_header->unwindHeader))
473 {
474 // Reset exception_header to primaryException and deallocate the dependent exception
475 __cxa_dependent_exception* dep_exception_header =
476 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
477 exception_header =
478 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
479 __cxa_free_dependent_exception(dep_exception_header);
480 }
481 // Destroy the primary exception only if its referenceCount goes to 0
482 // (this decrement must be atomic)
483 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
484 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000485 }
486 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000487 else
488 {
489 // The foreign exception has not been rethrown. Pop the stack
490 // and delete it. If there are nested catch's and they try
491 // to touch a foreign exception in any way, that is undefined
492 // behavior. They likely can't since the only way to catch
493 // a foreign exception is with catch (...)!
494 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
495 globals->caughtExceptions = 0;
496 }
Marshall Clow7c4652f2011-08-09 15:09:41 +0000497 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000498}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000499
Howard Hinnanta36fb302012-01-22 19:14:27 +0000500// Note: exception_header may be masquerading as a __cxa_dependent_exception
501// and that's ok. exceptionType is there too.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000502// However watch out for foreign exceptions. Return null for them.
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000503_LIBCXXABI_FUNC_VIS std::type_info *__cxa_current_exception_type() {
Marshall Clow7c4652f2011-08-09 15:09:41 +0000504// get the current exception
Marshall Clow64e00972011-12-22 15:45:05 +0000505 __cxa_eh_globals *globals = __cxa_get_globals_fast();
506 if (NULL == globals)
507 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000508 __cxa_exception *exception_header = globals->caughtExceptions;
509 if (NULL == exception_header)
Marshall Clow7c4652f2011-08-09 15:09:41 +0000510 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000511 if (!isOurExceptionClass(&exception_header->unwindHeader))
512 return NULL;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000513 return exception_header->exceptionType;
Marshall Clow7ac31262011-08-15 18:06:47 +0000514}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000515
516// 2.5.4 Rethrowing Exceptions
Howard Hinnant7da9b962012-01-30 16:07:00 +0000517/* This routine can rethrow native or foreign exceptions.
518If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000519* marks the exception object on top of the caughtExceptions stack
520 (in an implementation-defined way) as being rethrown.
521* If the caughtExceptions stack is empty, it calls terminate()
522 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000523* It then calls _Unwind_RaiseException which should not return
Howard Hinnantd4028fe2011-12-12 19:11:42 +0000524 (terminate if it does).
Howard Hinnanta36fb302012-01-22 19:14:27 +0000525 Note: exception_header may be masquerading as a __cxa_dependent_exception
526 and that's ok.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000527*/
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000528_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_rethrow() {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000529 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnant7da9b962012-01-30 16:07:00 +0000530 __cxa_exception* exception_header = globals->caughtExceptions;
531 if (NULL == exception_header)
532 std::terminate(); // throw; called outside of a exception handler
533 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
534 if (native_exception)
535 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000536 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
537 exception_header->handlerCount = -exception_header->handlerCount;
538 globals->uncaughtExceptions += 1;
539 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
540 }
541 else // this is a foreign exception
542 {
543 // The only way to communicate to __cxa_end_catch that we've rethrown
544 // a foreign exception, so don't delete us, is to pop the stack here
545 // which must be empty afterwards. Then __cxa_end_catch will do
546 // nothing
547 globals->caughtExceptions = 0;
548 }
Dan Alberta1fce462015-02-05 01:33:15 +0000549#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000550 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000551#else
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000552 _Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000553#endif
554
Howard Hinnant7da9b962012-01-30 16:07:00 +0000555 // If we get here, some kind of unwinding error has occurred.
556 // There is some weird code generation bug happening with
557 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
558 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
559 __cxa_begin_catch(&exception_header->unwindHeader);
560 if (native_exception)
561 std::__terminate(exception_header->terminateHandler);
562 // Foreign exception: can't get exception_header->terminateHandler
563 std::terminate();
Marshall Clow7ac31262011-08-15 18:06:47 +0000564}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000565
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000566/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000567 If thrown_object is not null, atomically increment the referenceCount field
568 of the __cxa_exception header associated with the thrown object referred to
569 by thrown_object.
570
571 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000572*/
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000573_LIBCXXABI_FUNC_VIS void
574__cxa_increment_exception_refcount(void *thrown_object) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000575 if (thrown_object != NULL )
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000576 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000577 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
578 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000579 }
580}
581
582/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000583 If thrown_object is not null, atomically decrement the referenceCount field
584 of the __cxa_exception header associated with the thrown object referred to
585 by thrown_object. If the referenceCount drops to zero, destroy and
586 deallocate the exception.
587
588 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000589*/
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000590_LIBCXXABI_FUNC_VIS void
591__cxa_decrement_exception_refcount(void *thrown_object) throw() {
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000592 if (thrown_object != NULL )
593 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000594 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
595 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000596 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000597 if (NULL != exception_header->exceptionDestructor)
598 exception_header->exceptionDestructor(thrown_object);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000599 __cxa_free_exception(thrown_object);
600 }
601 }
602}
603
604/*
605 Returns a pointer to the thrown object (if any) at the top of the
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000606 caughtExceptions stack. Atomically increment the exception's referenceCount.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000607 If there is no such thrown object or if the thrown object is foreign,
608 returns null.
Marshall Clow0acbd982012-01-04 22:18:10 +0000609
610 We can use __cxa_get_globals_fast here to get the globals because if there have
611 been no exceptions thrown, ever, on this thread, we can return NULL without
612 the need to allocate the exception-handling globals.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000613*/
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000614_LIBCXXABI_FUNC_VIS void *__cxa_current_primary_exception() throw() {
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000615// get the current exception
Marshall Clowaeed3a92012-01-03 23:26:09 +0000616 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow62edc012012-01-03 23:10:20 +0000617 if (NULL == globals)
Marshall Clow2bce81b2012-01-04 14:56:09 +0000618 return NULL; // If there are no globals, there is no exception
Howard Hinnanta36fb302012-01-22 19:14:27 +0000619 __cxa_exception* exception_header = globals->caughtExceptions;
620 if (NULL == exception_header)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000621 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000622 if (!isOurExceptionClass(&exception_header->unwindHeader))
623 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000624 if (isDependentException(&exception_header->unwindHeader)) {
625 __cxa_dependent_exception* dep_exception_header =
626 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
627 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000628 }
Howard Hinnanta36fb302012-01-22 19:14:27 +0000629 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000630 __cxa_increment_exception_refcount(thrown_object);
631 return thrown_object;
632}
633
634/*
635 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
636 stored in exc is called. Otherwise the referenceCount stored in the
637 primary exception is decremented, destroying the primary if necessary.
638 Finally the dependent exception is destroyed.
639*/
640static
641void
Howard Hinnanta36fb302012-01-22 19:14:27 +0000642dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000643{
Howard Hinnanta36fb302012-01-22 19:14:27 +0000644 __cxa_dependent_exception* dep_exception_header =
645 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000646 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000647 std::__terminate(dep_exception_header->terminateHandler);
648 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
649 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000650}
651
652/*
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000653 If thrown_object is not null, allocate, initialize and throw a dependent
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000654 exception.
655*/
656void
657__cxa_rethrow_primary_exception(void* thrown_object)
658{
659 if ( thrown_object != NULL )
660 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000661 // thrown_object guaranteed to be native because
662 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnanta36fb302012-01-22 19:14:27 +0000663 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
664 __cxa_dependent_exception* dep_exception_header =
665 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
666 dep_exception_header->primaryException = thrown_object;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000667 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnanta36fb302012-01-22 19:14:27 +0000668 dep_exception_header->exceptionType = exception_header->exceptionType;
669 dep_exception_header->unexpectedHandler = std::get_unexpected();
670 dep_exception_header->terminateHandler = std::get_terminate();
671 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantdd47ca42011-12-21 23:48:05 +0000672 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000673 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Dan Alberta1fce462015-02-05 01:33:15 +0000674#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000675 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000676#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000677 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000678#endif
679 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000680 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000681 }
682 // If we return client will call terminate()
683}
684
Howard Hinnantca6514d2012-01-24 00:01:31 +0000685bool
Marshall Clow5013d7c2015-06-02 13:03:17 +0000686__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }
687
688unsigned int
689__cxa_uncaught_exceptions() throw()
Howard Hinnantca6514d2012-01-24 00:01:31 +0000690{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000691 // This does not report foreign exceptions in flight
Howard Hinnantca6514d2012-01-24 00:01:31 +0000692 __cxa_eh_globals* globals = __cxa_get_globals_fast();
693 if (globals == 0)
Marshall Clow5013d7c2015-06-02 13:03:17 +0000694 return 0;
695 return globals->uncaughtExceptions;
Howard Hinnantca6514d2012-01-24 00:01:31 +0000696}
697
Marshall Clow47c29eb2011-07-20 15:04:39 +0000698} // extern "C"
699
Howard Hinnant02406852012-02-02 20:47:28 +0000700#pragma GCC visibility pop
701
Marshall Clow47c29eb2011-07-20 15:04:39 +0000702} // abi