blob: 0794444bcc5c040e0e69580677bd6b3e9974d21a [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
14#include "cxxabi.h"
15
16#include <exception> // for std::terminate
Howard Hinnant6c33e762013-06-17 18:10:34 +000017#include <cstring> // for memset
Marshall Clow47c29eb2011-07-20 15:04:39 +000018#include "cxa_exception.hpp"
Howard Hinnante8fcf832011-12-07 21:16:40 +000019#include "cxa_handlers.hpp"
Igor Kudrinace65722016-10-07 08:48:28 +000020#include "fallback_malloc.h"
Marshall Clow47c29eb2011-07-20 15:04:39 +000021
Howard Hinnanta36fb302012-01-22 19:14:27 +000022// +---------------------------+-----------------------------+---------------+
23// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
24// +---------------------------+-----------------------------+---------------+
25// ^
26// |
27// +-------------------------------------------------------+
28// |
29// +---------------------------+-----------------------------+
30// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
31// +---------------------------+-----------------------------+
32
Marshall Clow47c29eb2011-07-20 15:04:39 +000033namespace __cxxabiv1 {
Howard Hinnant2a70c102012-01-24 18:15:20 +000034
Marshall Clow47c29eb2011-07-20 15:04:39 +000035// Utility routines
Howard Hinnanta36fb302012-01-22 19:14:27 +000036static
37inline
38__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000039cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnanta36fb302012-01-22 19:14:27 +000040{
41 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clow7ac31262011-08-15 18:06:47 +000042}
Marshall Clow47c29eb2011-07-20 15:04:39 +000043
Howard Hinnanta36fb302012-01-22 19:14:27 +000044// Note: This is never called when exception_header is masquerading as a
45// __cxa_dependent_exception.
46static
47inline
48void*
Howard Hinnant7da9b962012-01-30 16:07:00 +000049thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnanta36fb302012-01-22 19:14:27 +000050{
51 return static_cast<void*>(exception_header + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +000052}
Marshall Clow47c29eb2011-07-20 15:04:39 +000053
Marshall Clow7c4652f2011-08-09 15:09:41 +000054// Get the exception object from the unwind pointer.
55// Relies on the structure layout, where the unwind pointer is right in
56// front of the user's exception object
Howard Hinnanta36fb302012-01-22 19:14:27 +000057static
58inline
59__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000060cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnanta36fb302012-01-22 19:14:27 +000061{
62 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clow7ac31262011-08-15 18:06:47 +000063}
Marshall Clow7c4652f2011-08-09 15:09:41 +000064
Eric Fiselieraad05942017-03-04 02:04:45 +000065// Round s up to next multiple of a.
66static inline
67size_t aligned_allocation_size(size_t s, size_t a) {
68 return (s + a - 1) & ~(a - 1);
69}
70
71static inline
72size_t cxa_exception_size_from_exception_thrown_size(size_t size) {
73 return aligned_allocation_size(size + sizeof (__cxa_exception),
74 alignof(__cxa_exception));
Marshall Clow7ac31262011-08-15 18:06:47 +000075}
Marshall Clow7c4652f2011-08-09 15:09:41 +000076
Howard Hinnant7da9b962012-01-30 16:07:00 +000077static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000078 unwind_exception->exception_class = kOurExceptionClass;
79}
80
Howard Hinnant7da9b962012-01-30 16:07:00 +000081static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000082 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clow7ac31262011-08-15 18:06:47 +000083}
Marshall Clow7c4652f2011-08-09 15:09:41 +000084
85// Is it one of ours?
Howard Hinnant7da9b962012-01-30 16:07:00 +000086static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant7ab185e2012-02-01 18:15:15 +000087 return (unwind_exception->exception_class & get_vendor_and_language) ==
88 (kOurExceptionClass & get_vendor_and_language);
Marshall Clow7ac31262011-08-15 18:06:47 +000089}
Marshall Clow7c4652f2011-08-09 15:09:41 +000090
Howard Hinnant7da9b962012-01-30 16:07:00 +000091static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000092 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clow7ac31262011-08-15 18:06:47 +000093}
94
Howard Hinnant6eb54ad2011-12-08 19:35:18 +000095// This does not need to be atomic
Howard Hinnant7da9b962012-01-30 16:07:00 +000096static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +000097 return ++exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +000098}
99
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000100// This does not need to be atomic
Howard Hinnant7da9b962012-01-30 16:07:00 +0000101static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000102 return --exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +0000103}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000104
Howard Hinnante8fcf832011-12-07 21:16:40 +0000105/*
Marshall Clow7c4652f2011-08-09 15:09:41 +0000106 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
107 stored in exc is called. Otherwise the exceptionDestructor stored in
108 exc is called, and then the memory for the exception is deallocated.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000109
110 This is never called for a __cxa_dependent_exception.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000111*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000112static
113void
114exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
115{
116 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +0000117 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000118 std::__terminate(exception_header->terminateHandler);
Howard Hinnant8dd7a482012-02-01 18:44:21 +0000119 // Just in case there exists a dependent exception that is pointing to this,
120 // check the reference count and only destroy this if that count goes to zero.
121 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +0000122}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000123
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000124static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow7c4652f2011-08-09 15:09:41 +0000125// Section 2.5.3 says:
126// * For purposes of this ABI, several things are considered exception handlers:
127// ** A terminate() call due to a throw.
128// and
129// * Upon entry, Following initialization of the catch parameter,
130// a handler must call:
Marshall Clow7ac31262011-08-15 18:06:47 +0000131// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnanta36fb302012-01-22 19:14:27 +0000132 (void) __cxa_begin_catch(&exception_header->unwindHeader);
133 std::__terminate(exception_header->terminateHandler);
Marshall Clow7ac31262011-08-15 18:06:47 +0000134}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000135
136extern "C" {
137
138// Allocate a __cxa_exception object, and zero-fill it.
139// Reserve "thrown_size" bytes on the end for the user's exception
140// object. Zero-fill the object. If memory can't be allocated, call
141// std::terminate. Return a pointer to the memory to be used for the
142// user's exception object.
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000143void *__cxa_allocate_exception(size_t thrown_size) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000144 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
Igor Kudrinace65722016-10-07 08:48:28 +0000145 __cxa_exception *exception_header =
Eric Fiselieraad05942017-03-04 02:04:45 +0000146 static_cast<__cxa_exception *>(__aligned_malloc_with_fallback(actual_size));
Howard Hinnanta36fb302012-01-22 19:14:27 +0000147 if (NULL == exception_header)
Marshall Clow7ac31262011-08-15 18:06:47 +0000148 std::terminate();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000149 std::memset(exception_header, 0, actual_size);
150 return thrown_object_from_cxa_exception(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000151}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000152
153
154// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000155void __cxa_free_exception(void *thrown_object) throw() {
Eric Fiselieraad05942017-03-04 02:04:45 +0000156 __aligned_free_with_fallback(cxa_exception_from_thrown_object(thrown_object));
Marshall Clow7ac31262011-08-15 18:06:47 +0000157}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000158
159
160// This function shall allocate a __cxa_dependent_exception and
161// return a pointer to it. (Really to the object, not past its' end).
162// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000163void * __cxa_allocate_dependent_exception () {
Marshall Clow7ac31262011-08-15 18:06:47 +0000164 size_t actual_size = sizeof(__cxa_dependent_exception);
Eric Fiselieraad05942017-03-04 02:04:45 +0000165 void *ptr = __aligned_malloc_with_fallback(actual_size);
Marshall Clow7ac31262011-08-15 18:06:47 +0000166 if (NULL == ptr)
167 std::terminate();
168 std::memset(ptr, 0, actual_size);
Marshall Clow47c29eb2011-07-20 15:04:39 +0000169 return ptr;
Marshall Clow7ac31262011-08-15 18:06:47 +0000170}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000171
172
173// This function shall free a dependent_exception.
174// It does not affect the reference count of the primary exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000175void __cxa_free_dependent_exception (void * dependent_exception) {
Eric Fiselieraad05942017-03-04 02:04:45 +0000176 __aligned_free_with_fallback(dependent_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +0000177}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000178
Marshall Clow7c4652f2011-08-09 15:09:41 +0000179
180// 2.4.3 Throwing the Exception Object
181/*
182After constructing the exception object with the throw argument value,
183the generated code calls the __cxa_throw runtime library routine. This
184routine never returns.
185
186The __cxa_throw routine will do the following:
187
188* Obtain the __cxa_exception header from the thrown exception object address,
189which can be computed as follows:
190 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
191* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
192* Save the tinfo and dest arguments in the __cxa_exception header.
193* Set the exception_class field in the unwind header. This is a 64-bit value
194representing the ASCII string "XXXXC++\0", where "XXXX" is a
195vendor-dependent string. That is, for implementations conforming to this
196ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
197* Increment the uncaught_exception flag.
198* Call _Unwind_RaiseException in the system unwind library, Its argument is the
199pointer to the thrown exception, which __cxa_throw itself received as an argument.
200__Unwind_RaiseException begins the process of stack unwinding, described
201in Section 2.5. In special cases, such as an inability to find a
202handler, _Unwind_RaiseException may return. In that case, __cxa_throw
203will call terminate, assuming that there was no handler for the
204exception.
205*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000206void
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000207__cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {
Marshall Clow7ac31262011-08-15 18:06:47 +0000208 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000209 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000210
Howard Hinnanta36fb302012-01-22 19:14:27 +0000211 exception_header->unexpectedHandler = std::get_unexpected();
212 exception_header->terminateHandler = std::get_terminate();
213 exception_header->exceptionType = tinfo;
214 exception_header->exceptionDestructor = dest;
215 setExceptionClass(&exception_header->unwindHeader);
216 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000217 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
218
Howard Hinnanta36fb302012-01-22 19:14:27 +0000219 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Dan Alberta1fce462015-02-05 01:33:15 +0000220#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000221 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000222#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000223 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000224#endif
Howard Hinnant978bfde2012-01-28 00:34:46 +0000225 // This only happens when there is no handler, or some unexpected unwinding
226 // error happens.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000227 failed_throw(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000228}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000229
230
231// 2.5.3 Exception Handlers
Howard Hinnanta36fb302012-01-22 19:14:27 +0000232/*
233The adjusted pointer is computed by the personality routine during phase 1
234 and saved in the exception header (either __cxa_exception or
235 __cxa_dependent_exception).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000236
237 Requires: exception is native
Howard Hinnanta36fb302012-01-22 19:14:27 +0000238*/
Saleem Abdulrasool77a304b2015-12-04 02:14:41 +0000239void *__cxa_get_exception_ptr(void *unwind_exception) throw() {
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000240#if defined(_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
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000249#if defined(_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 Abdulrasool77a304b2015-12-04 02:14:41 +0000254bool __cxa_begin_cleanup(void *unwind_arg) throw() {
Logan Chien05d51bc2014-05-10 00:42:10 +0000255 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
256 __cxa_eh_globals* globals = __cxa_get_globals();
257 __cxa_exception* exception_header =
258 cxa_exception_from_exception_unwind_exception(unwind_exception);
259
260 if (isOurExceptionClass(unwind_exception))
261 {
262 if (0 == exception_header->propagationCount)
263 {
264 exception_header->nextPropagatingException = globals->propagatingExceptions;
265 globals->propagatingExceptions = exception_header;
266 }
267 ++exception_header->propagationCount;
268 }
269 else
270 {
271 // If the propagatingExceptions stack is not empty, since we can't
272 // chain the foreign exception, terminate it.
273 if (NULL != globals->propagatingExceptions)
274 std::terminate();
275 globals->propagatingExceptions = exception_header;
276 }
277 return true;
278}
279
280/*
281The routine to be called after the cleanup has been performed. It will get the
282propagating __cxa_exception from __cxa_eh_globals, and continue the stack
283unwinding with _Unwind_Resume.
284
285According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
286register, thus we have to write this function in assembly so that we can save
287{r1, r2, r3}. We don't have to save r0 because it is the return value and the
288first argument to _Unwind_Resume(). In addition, we are saving r4 in order to
289align the stack to 16 bytes, even though it is a callee-save register.
290*/
291__attribute__((used)) static _Unwind_Exception *
292__cxa_end_cleanup_impl()
293{
294 __cxa_eh_globals* globals = __cxa_get_globals();
295 __cxa_exception* exception_header = globals->propagatingExceptions;
296 if (NULL == exception_header)
297 {
298 // It seems that __cxa_begin_cleanup() is not called properly.
299 // We have no choice but terminate the program now.
300 std::terminate();
301 }
302
303 if (isOurExceptionClass(&exception_header->unwindHeader))
304 {
305 --exception_header->propagationCount;
306 if (0 == exception_header->propagationCount)
307 {
308 globals->propagatingExceptions = exception_header->nextPropagatingException;
309 exception_header->nextPropagatingException = NULL;
310 }
311 }
312 else
313 {
314 globals->propagatingExceptions = NULL;
315 }
316 return &exception_header->unwindHeader;
317}
318
319asm (
Logan Chienf67395c2015-12-22 14:38:30 +0000320 " .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
Logan Chien05d51bc2014-05-10 00:42:10 +0000321 " .globl __cxa_end_cleanup\n"
322 " .type __cxa_end_cleanup,%function\n"
323 "__cxa_end_cleanup:\n"
324 " push {r1, r2, r3, r4}\n"
325 " bl __cxa_end_cleanup_impl\n"
326 " pop {r1, r2, r3, r4}\n"
327 " bl _Unwind_Resume\n"
328 " bl abort\n"
329 " .popsection"
330);
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000331#endif // defined(_LIBCXXABI_ARM_EHABI)
Marshall Clow7c4652f2011-08-09 15:09:41 +0000332
Marshall Clow7c4652f2011-08-09 15:09:41 +0000333/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000334This routine can catch foreign or native exceptions. If native, the exception
335can be a primary or dependent variety. This routine may remain blissfully
336ignorant of whether the native exception is primary or dependent.
337
338If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000339* Increment's the exception's handler count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000340* Push the exception on the stack of currently-caught exceptions if it is not
341 already there (from a rethrow).
Marshall Clow7c4652f2011-08-09 15:09:41 +0000342* Decrements the uncaught_exception count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000343* Returns the adjusted pointer to the exception object, which is stored in
344 the __cxa_exception by the personality routine.
345
346If the exception is foreign, this means it did not originate from one of throw
347routines. The foreign exception does not necessarily have a __cxa_exception
348header. However we can catch it here with a catch (...), or with a call
349to terminate or unexpected during unwinding.
350* Do not try to increment the exception's handler count, we don't know where
351 it is.
352* Push the exception on the stack of currently-caught exceptions only if the
353 stack is empty. The foreign exception has no way to link to the current
354 top of stack. If the stack is not empty, call terminate. Even with an
355 empty stack, this is hacked in by pushing a pointer to an imaginary
356 __cxa_exception block in front of the foreign exception. It would be better
357 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
358 doesn't. It has a stack of __cxa_exception (which has a next* in it).
359* Do not decrement the uncaught_exception count because we didn't increment it
360 in __cxa_throw (or one of our rethrow functions).
361* If we haven't terminated, assume the exception object is just past the
362 _Unwind_Exception and return a pointer to that.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000363*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000364void*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000365__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnanta36fb302012-01-22 19:14:27 +0000366{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000367 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
368 bool native_exception = isOurExceptionClass(unwind_exception);
369 __cxa_eh_globals* globals = __cxa_get_globals();
370 // exception_header is a hackish offset from a foreign exception, but it
371 // works as long as we're careful not to try to access any __cxa_exception
372 // parts.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000373 __cxa_exception* exception_header =
374 cxa_exception_from_exception_unwind_exception
375 (
376 static_cast<_Unwind_Exception*>(unwind_exception)
377 );
Howard Hinnant7da9b962012-01-30 16:07:00 +0000378 if (native_exception)
379 {
380 // Increment the handler count, removing the flag about being rethrown
381 exception_header->handlerCount = exception_header->handlerCount < 0 ?
382 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
383 // place the exception on the top of the stack if it's not already
384 // there by a previous rethrow
385 if (exception_header != globals->caughtExceptions)
386 {
387 exception_header->nextException = globals->caughtExceptions;
388 globals->caughtExceptions = exception_header;
389 }
390 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000391#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chien05d51bc2014-05-10 00:42:10 +0000392 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);
393#else
Howard Hinnant7da9b962012-01-30 16:07:00 +0000394 return exception_header->adjustedPtr;
Logan Chien05d51bc2014-05-10 00:42:10 +0000395#endif
Marshall Clow7ac31262011-08-15 18:06:47 +0000396 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000397 // Else this is a foreign exception
398 // If the caughtExceptions stack is not empty, terminate
399 if (globals->caughtExceptions != 0)
400 std::terminate();
401 // Push the foreign exception on to the stack
402 globals->caughtExceptions = exception_header;
403 return unwind_exception + 1;
Marshall Clow7ac31262011-08-15 18:06:47 +0000404}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000405
406
407/*
408Upon exit for any reason, a handler must call:
409 void __cxa_end_catch ();
410
Howard Hinnant7da9b962012-01-30 16:07:00 +0000411This routine can be called for either a native or foreign exception.
412For a native exception:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000413* Locates the most recently caught exception and decrements its handler count.
414* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000415* If the handler count goes down to zero, and the exception was not re-thrown
416 by throw, it locates the primary exception (which may be the same as the one
417 it's handling) and decrements its reference count. If that reference count
418 goes to zero, the function destroys the exception. In any case, if the current
419 exception is a dependent exception, it destroys that.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000420
421For a foreign exception:
422* If it has been rethrown, there is nothing to do.
423* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000424*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000425void __cxa_end_catch() {
Nico Weberd6e23362014-06-25 23:52:07 +0000426 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
427 "sizeof(__cxa_exception) must be equal to "
428 "sizeof(__cxa_dependent_exception)");
Saleem Abdulrasool4c91e382015-11-18 05:33:38 +0000429 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==
430 __builtin_offsetof(__cxa_dependent_exception,
431 primaryException),
432 "the layout of __cxa_exception must match the layout of "
433 "__cxa_dependent_exception");
434 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==
435 __builtin_offsetof(__cxa_dependent_exception, handlerCount),
436 "the layout of __cxa_exception must match the layout of "
437 "__cxa_dependent_exception");
Howard Hinnant7da9b962012-01-30 16:07:00 +0000438 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
439 __cxa_exception* exception_header = globals->caughtExceptions;
440 // If we've rethrown a foreign exception, then globals->caughtExceptions
441 // will have been made an empty stack by __cxa_rethrow() and there is
442 // nothing more to be done. Do nothing!
443 if (NULL != exception_header)
444 {
445 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
446 if (native_exception)
447 {
448 // This is a native exception
449 if (exception_header->handlerCount < 0)
450 {
451 // The exception has been rethrown by __cxa_rethrow, so don't delete it
452 if (0 == incrementHandlerCount(exception_header))
453 {
454 // Remove from the chain of uncaught exceptions
455 globals->caughtExceptions = exception_header->nextException;
456 // but don't destroy
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000457 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000458 // Keep handlerCount negative in case there are nested catch's
459 // that need to be told that this exception is rethrown. Don't
460 // erase this rethrow flag until the exception is recaught.
461 }
462 else
463 {
464 // The native exception has not been rethrown
465 if (0 == decrementHandlerCount(exception_header))
466 {
467 // Remove from the chain of uncaught exceptions
468 globals->caughtExceptions = exception_header->nextException;
469 // Destroy this exception, being careful to distinguish
470 // between dependent and primary exceptions
471 if (isDependentException(&exception_header->unwindHeader))
472 {
473 // Reset exception_header to primaryException and deallocate the dependent exception
474 __cxa_dependent_exception* dep_exception_header =
475 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
476 exception_header =
477 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
478 __cxa_free_dependent_exception(dep_exception_header);
479 }
480 // Destroy the primary exception only if its referenceCount goes to 0
481 // (this decrement must be atomic)
482 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
483 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000484 }
485 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000486 else
487 {
488 // The foreign exception has not been rethrown. Pop the stack
489 // and delete it. If there are nested catch's and they try
490 // to touch a foreign exception in any way, that is undefined
491 // behavior. They likely can't since the only way to catch
492 // a foreign exception is with catch (...)!
493 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
494 globals->caughtExceptions = 0;
495 }
Marshall Clow7c4652f2011-08-09 15:09:41 +0000496 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000497}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000498
Howard Hinnanta36fb302012-01-22 19:14:27 +0000499// Note: exception_header may be masquerading as a __cxa_dependent_exception
500// and that's ok. exceptionType is there too.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000501// However watch out for foreign exceptions. Return null for them.
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000502std::type_info *__cxa_current_exception_type() {
Marshall Clow7c4652f2011-08-09 15:09:41 +0000503// get the current exception
Marshall Clow64e00972011-12-22 15:45:05 +0000504 __cxa_eh_globals *globals = __cxa_get_globals_fast();
505 if (NULL == globals)
506 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000507 __cxa_exception *exception_header = globals->caughtExceptions;
508 if (NULL == exception_header)
Marshall Clow7c4652f2011-08-09 15:09:41 +0000509 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000510 if (!isOurExceptionClass(&exception_header->unwindHeader))
511 return NULL;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000512 return exception_header->exceptionType;
Marshall Clow7ac31262011-08-15 18:06:47 +0000513}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000514
515// 2.5.4 Rethrowing Exceptions
Howard Hinnant7da9b962012-01-30 16:07:00 +0000516/* This routine can rethrow native or foreign exceptions.
517If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000518* marks the exception object on top of the caughtExceptions stack
519 (in an implementation-defined way) as being rethrown.
520* If the caughtExceptions stack is empty, it calls terminate()
521 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000522* It then calls _Unwind_RaiseException which should not return
Howard Hinnantd4028fe2011-12-12 19:11:42 +0000523 (terminate if it does).
Howard Hinnanta36fb302012-01-22 19:14:27 +0000524 Note: exception_header may be masquerading as a __cxa_dependent_exception
525 and that's ok.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000526*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000527void __cxa_rethrow() {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000528 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnant7da9b962012-01-30 16:07:00 +0000529 __cxa_exception* exception_header = globals->caughtExceptions;
530 if (NULL == exception_header)
531 std::terminate(); // throw; called outside of a exception handler
532 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
533 if (native_exception)
534 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000535 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
536 exception_header->handlerCount = -exception_header->handlerCount;
537 globals->uncaughtExceptions += 1;
538 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
539 }
540 else // this is a foreign exception
541 {
542 // The only way to communicate to __cxa_end_catch that we've rethrown
543 // a foreign exception, so don't delete us, is to pop the stack here
544 // which must be empty afterwards. Then __cxa_end_catch will do
545 // nothing
546 globals->caughtExceptions = 0;
547 }
Dan Alberta1fce462015-02-05 01:33:15 +0000548#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000549 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000550#else
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000551 _Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000552#endif
553
Howard Hinnant7da9b962012-01-30 16:07:00 +0000554 // If we get here, some kind of unwinding error has occurred.
555 // There is some weird code generation bug happening with
556 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
557 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
558 __cxa_begin_catch(&exception_header->unwindHeader);
559 if (native_exception)
560 std::__terminate(exception_header->terminateHandler);
561 // Foreign exception: can't get exception_header->terminateHandler
562 std::terminate();
Marshall Clow7ac31262011-08-15 18:06:47 +0000563}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000564
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000565/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000566 If thrown_object is not null, atomically increment the referenceCount field
567 of the __cxa_exception header associated with the thrown object referred to
568 by thrown_object.
569
570 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000571*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000572void
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000573__cxa_increment_exception_refcount(void *thrown_object) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000574 if (thrown_object != NULL )
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000575 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000576 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
577 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000578 }
579}
580
581/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000582 If thrown_object is not null, atomically decrement the referenceCount field
583 of the __cxa_exception header associated with the thrown object referred to
584 by thrown_object. If the referenceCount drops to zero, destroy and
585 deallocate the exception.
586
587 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000588*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000589void
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000590__cxa_decrement_exception_refcount(void *thrown_object) throw() {
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000591 if (thrown_object != NULL )
592 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000593 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
594 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000595 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000596 if (NULL != exception_header->exceptionDestructor)
597 exception_header->exceptionDestructor(thrown_object);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000598 __cxa_free_exception(thrown_object);
599 }
600 }
601}
602
603/*
604 Returns a pointer to the thrown object (if any) at the top of the
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000605 caughtExceptions stack. Atomically increment the exception's referenceCount.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000606 If there is no such thrown object or if the thrown object is foreign,
607 returns null.
Marshall Clow0acbd982012-01-04 22:18:10 +0000608
609 We can use __cxa_get_globals_fast here to get the globals because if there have
610 been no exceptions thrown, ever, on this thread, we can return NULL without
611 the need to allocate the exception-handling globals.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000612*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000613void *__cxa_current_primary_exception() throw() {
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000614// get the current exception
Marshall Clowaeed3a92012-01-03 23:26:09 +0000615 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow62edc012012-01-03 23:10:20 +0000616 if (NULL == globals)
Marshall Clow2bce81b2012-01-04 14:56:09 +0000617 return NULL; // If there are no globals, there is no exception
Howard Hinnanta36fb302012-01-22 19:14:27 +0000618 __cxa_exception* exception_header = globals->caughtExceptions;
619 if (NULL == exception_header)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000620 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000621 if (!isOurExceptionClass(&exception_header->unwindHeader))
622 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000623 if (isDependentException(&exception_header->unwindHeader)) {
624 __cxa_dependent_exception* dep_exception_header =
625 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
626 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000627 }
Howard Hinnanta36fb302012-01-22 19:14:27 +0000628 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000629 __cxa_increment_exception_refcount(thrown_object);
630 return thrown_object;
631}
632
633/*
634 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
635 stored in exc is called. Otherwise the referenceCount stored in the
636 primary exception is decremented, destroying the primary if necessary.
637 Finally the dependent exception is destroyed.
638*/
639static
640void
Howard Hinnanta36fb302012-01-22 19:14:27 +0000641dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000642{
Howard Hinnanta36fb302012-01-22 19:14:27 +0000643 __cxa_dependent_exception* dep_exception_header =
644 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000645 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000646 std::__terminate(dep_exception_header->terminateHandler);
647 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
648 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000649}
650
651/*
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000652 If thrown_object is not null, allocate, initialize and throw a dependent
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000653 exception.
654*/
655void
656__cxa_rethrow_primary_exception(void* thrown_object)
657{
658 if ( thrown_object != NULL )
659 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000660 // thrown_object guaranteed to be native because
661 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnanta36fb302012-01-22 19:14:27 +0000662 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
663 __cxa_dependent_exception* dep_exception_header =
664 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
665 dep_exception_header->primaryException = thrown_object;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000666 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnanta36fb302012-01-22 19:14:27 +0000667 dep_exception_header->exceptionType = exception_header->exceptionType;
668 dep_exception_header->unexpectedHandler = std::get_unexpected();
669 dep_exception_header->terminateHandler = std::get_terminate();
670 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantdd47ca42011-12-21 23:48:05 +0000671 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000672 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Dan Alberta1fce462015-02-05 01:33:15 +0000673#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000674 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000675#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000676 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000677#endif
678 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000679 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000680 }
681 // If we return client will call terminate()
682}
683
Howard Hinnantca6514d2012-01-24 00:01:31 +0000684bool
Marshall Clow5013d7c2015-06-02 13:03:17 +0000685__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }
686
687unsigned int
688__cxa_uncaught_exceptions() throw()
Howard Hinnantca6514d2012-01-24 00:01:31 +0000689{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000690 // This does not report foreign exceptions in flight
Howard Hinnantca6514d2012-01-24 00:01:31 +0000691 __cxa_eh_globals* globals = __cxa_get_globals_fast();
692 if (globals == 0)
Marshall Clow5013d7c2015-06-02 13:03:17 +0000693 return 0;
694 return globals->uncaughtExceptions;
Howard Hinnantca6514d2012-01-24 00:01:31 +0000695}
696
Marshall Clow47c29eb2011-07-20 15:04:39 +0000697} // extern "C"
698
Marshall Clow47c29eb2011-07-20 15:04:39 +0000699} // abi