blob: 4662ccddf1de267c2a4c28ade2d9548246737711 [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
Petr Hosek05e2ac52017-09-13 23:35:07 +000022#if __has_feature(address_sanitizer)
23#include <sanitizer/asan_interface.h>
24#endif
25
Howard Hinnanta36fb302012-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 Clow47c29eb2011-07-20 15:04:39 +000037namespace __cxxabiv1 {
Howard Hinnant2a70c102012-01-24 18:15:20 +000038
Marshall Clow47c29eb2011-07-20 15:04:39 +000039// Utility routines
Howard Hinnanta36fb302012-01-22 19:14:27 +000040static
41inline
42__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000043cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnanta36fb302012-01-22 19:14:27 +000044{
45 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clow7ac31262011-08-15 18:06:47 +000046}
Marshall Clow47c29eb2011-07-20 15:04:39 +000047
Howard Hinnanta36fb302012-01-22 19:14:27 +000048// Note: This is never called when exception_header is masquerading as a
49// __cxa_dependent_exception.
50static
51inline
52void*
Howard Hinnant7da9b962012-01-30 16:07:00 +000053thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnanta36fb302012-01-22 19:14:27 +000054{
55 return static_cast<void*>(exception_header + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +000056}
Marshall Clow47c29eb2011-07-20 15:04:39 +000057
Marshall Clow7c4652f2011-08-09 15:09:41 +000058// Get the exception object from the unwind pointer.
59// Relies on the structure layout, where the unwind pointer is right in
60// front of the user's exception object
Howard Hinnanta36fb302012-01-22 19:14:27 +000061static
62inline
63__cxa_exception*
Howard Hinnant7da9b962012-01-30 16:07:00 +000064cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnanta36fb302012-01-22 19:14:27 +000065{
66 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clow7ac31262011-08-15 18:06:47 +000067}
Marshall Clow7c4652f2011-08-09 15:09:41 +000068
Eric Fiselieraad05942017-03-04 02:04:45 +000069// Round s up to next multiple of a.
70static inline
71size_t aligned_allocation_size(size_t s, size_t a) {
72 return (s + a - 1) & ~(a - 1);
73}
74
75static inline
76size_t cxa_exception_size_from_exception_thrown_size(size_t size) {
77 return aligned_allocation_size(size + sizeof (__cxa_exception),
78 alignof(__cxa_exception));
Marshall Clow7ac31262011-08-15 18:06:47 +000079}
Marshall Clow7c4652f2011-08-09 15:09:41 +000080
Howard Hinnant7da9b962012-01-30 16:07:00 +000081static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000082 unwind_exception->exception_class = kOurExceptionClass;
83}
84
Howard Hinnant7da9b962012-01-30 16:07:00 +000085static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000086 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clow7ac31262011-08-15 18:06:47 +000087}
Marshall Clow7c4652f2011-08-09 15:09:41 +000088
89// Is it one of ours?
Howard Hinnant7da9b962012-01-30 16:07:00 +000090static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant7ab185e2012-02-01 18:15:15 +000091 return (unwind_exception->exception_class & get_vendor_and_language) ==
92 (kOurExceptionClass & get_vendor_and_language);
Marshall Clow7ac31262011-08-15 18:06:47 +000093}
Marshall Clow7c4652f2011-08-09 15:09:41 +000094
Howard Hinnant7da9b962012-01-30 16:07:00 +000095static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnanta36fb302012-01-22 19:14:27 +000096 return (unwind_exception->exception_class & 0xFF) == 0x01;
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 incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000101 return ++exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +0000102}
103
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000104// This does not need to be atomic
Howard Hinnant7da9b962012-01-30 16:07:00 +0000105static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000106 return --exception->handlerCount;
Marshall Clow7ac31262011-08-15 18:06:47 +0000107}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000108
Howard Hinnante8fcf832011-12-07 21:16:40 +0000109/*
Marshall Clow7c4652f2011-08-09 15:09:41 +0000110 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
111 stored in exc is called. Otherwise the exceptionDestructor stored in
112 exc is called, and then the memory for the exception is deallocated.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000113
114 This is never called for a __cxa_dependent_exception.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000115*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000116static
117void
118exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
119{
120 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +0000121 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000122 std::__terminate(exception_header->terminateHandler);
Howard Hinnant8dd7a482012-02-01 18:44:21 +0000123 // Just in case there exists a dependent exception that is pointing to this,
124 // check the reference count and only destroy this if that count goes to zero.
125 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clow7ac31262011-08-15 18:06:47 +0000126}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000127
Eric Fiselier9e2169e2017-03-01 02:23:54 +0000128static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow7c4652f2011-08-09 15:09:41 +0000129// Section 2.5.3 says:
130// * For purposes of this ABI, several things are considered exception handlers:
131// ** A terminate() call due to a throw.
132// and
133// * Upon entry, Following initialization of the catch parameter,
134// a handler must call:
Marshall Clow7ac31262011-08-15 18:06:47 +0000135// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnanta36fb302012-01-22 19:14:27 +0000136 (void) __cxa_begin_catch(&exception_header->unwindHeader);
137 std::__terminate(exception_header->terminateHandler);
Marshall Clow7ac31262011-08-15 18:06:47 +0000138}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000139
140extern "C" {
141
142// Allocate a __cxa_exception object, and zero-fill it.
143// Reserve "thrown_size" bytes on the end for the user's exception
144// object. Zero-fill the object. If memory can't be allocated, call
145// std::terminate. Return a pointer to the memory to be used for the
146// user's exception object.
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000147void *__cxa_allocate_exception(size_t thrown_size) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000148 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
Igor Kudrinace65722016-10-07 08:48:28 +0000149 __cxa_exception *exception_header =
Eric Fiselieraad05942017-03-04 02:04:45 +0000150 static_cast<__cxa_exception *>(__aligned_malloc_with_fallback(actual_size));
Howard Hinnanta36fb302012-01-22 19:14:27 +0000151 if (NULL == exception_header)
Marshall Clow7ac31262011-08-15 18:06:47 +0000152 std::terminate();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000153 std::memset(exception_header, 0, actual_size);
154 return thrown_object_from_cxa_exception(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000155}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000156
157
158// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000159void __cxa_free_exception(void *thrown_object) throw() {
Eric Fiselieraad05942017-03-04 02:04:45 +0000160 __aligned_free_with_fallback(cxa_exception_from_thrown_object(thrown_object));
Marshall Clow7ac31262011-08-15 18:06:47 +0000161}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000162
163
164// This function shall allocate a __cxa_dependent_exception and
165// return a pointer to it. (Really to the object, not past its' end).
166// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000167void * __cxa_allocate_dependent_exception () {
Marshall Clow7ac31262011-08-15 18:06:47 +0000168 size_t actual_size = sizeof(__cxa_dependent_exception);
Eric Fiselieraad05942017-03-04 02:04:45 +0000169 void *ptr = __aligned_malloc_with_fallback(actual_size);
Marshall Clow7ac31262011-08-15 18:06:47 +0000170 if (NULL == ptr)
171 std::terminate();
172 std::memset(ptr, 0, actual_size);
Marshall Clow47c29eb2011-07-20 15:04:39 +0000173 return ptr;
Marshall Clow7ac31262011-08-15 18:06:47 +0000174}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000175
176
177// This function shall free a dependent_exception.
178// It does not affect the reference count of the primary exception.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000179void __cxa_free_dependent_exception (void * dependent_exception) {
Eric Fiselieraad05942017-03-04 02:04:45 +0000180 __aligned_free_with_fallback(dependent_exception);
Marshall Clow7ac31262011-08-15 18:06:47 +0000181}
Marshall Clow47c29eb2011-07-20 15:04:39 +0000182
Marshall Clow7c4652f2011-08-09 15:09:41 +0000183
184// 2.4.3 Throwing the Exception Object
185/*
186After constructing the exception object with the throw argument value,
187the generated code calls the __cxa_throw runtime library routine. This
188routine never returns.
189
190The __cxa_throw routine will do the following:
191
192* Obtain the __cxa_exception header from the thrown exception object address,
193which can be computed as follows:
194 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
195* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
196* Save the tinfo and dest arguments in the __cxa_exception header.
197* Set the exception_class field in the unwind header. This is a 64-bit value
198representing the ASCII string "XXXXC++\0", where "XXXX" is a
199vendor-dependent string. That is, for implementations conforming to this
200ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
201* Increment the uncaught_exception flag.
202* Call _Unwind_RaiseException in the system unwind library, Its argument is the
203pointer to the thrown exception, which __cxa_throw itself received as an argument.
204__Unwind_RaiseException begins the process of stack unwinding, described
205in Section 2.5. In special cases, such as an inability to find a
206handler, _Unwind_RaiseException may return. In that case, __cxa_throw
207will call terminate, assuming that there was no handler for the
208exception.
209*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000210void
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000211__cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {
Marshall Clow7ac31262011-08-15 18:06:47 +0000212 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnanta36fb302012-01-22 19:14:27 +0000213 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000214
Howard Hinnanta36fb302012-01-22 19:14:27 +0000215 exception_header->unexpectedHandler = std::get_unexpected();
216 exception_header->terminateHandler = std::get_terminate();
217 exception_header->exceptionType = tinfo;
218 exception_header->exceptionDestructor = dest;
219 setExceptionClass(&exception_header->unwindHeader);
220 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000221 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
222
Howard Hinnanta36fb302012-01-22 19:14:27 +0000223 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Petr Hosek05e2ac52017-09-13 23:35:07 +0000224
Eric Fiselier6b4fee22017-09-14 22:19:28 +0000225#if __has_feature(address_sanitizer) && \
226 defined(SANITIZER_ASAN_INTERFACE_HAS_HANDLE_NO_RETURN)
Petr Hosek05e2ac52017-09-13 23:35:07 +0000227 // Inform the ASan runtime that now might be a good time to clean stuff up.
228 __asan_handle_no_return();
229#endif
230
Dan Alberta1fce462015-02-05 01:33:15 +0000231#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000232 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000233#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000234 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000235#endif
Howard Hinnant978bfde2012-01-28 00:34:46 +0000236 // This only happens when there is no handler, or some unexpected unwinding
237 // error happens.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000238 failed_throw(exception_header);
Marshall Clow7ac31262011-08-15 18:06:47 +0000239}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000240
241
242// 2.5.3 Exception Handlers
Howard Hinnanta36fb302012-01-22 19:14:27 +0000243/*
244The adjusted pointer is computed by the personality routine during phase 1
245 and saved in the exception header (either __cxa_exception or
246 __cxa_dependent_exception).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000247
248 Requires: exception is native
Howard Hinnanta36fb302012-01-22 19:14:27 +0000249*/
Saleem Abdulrasool77a304b2015-12-04 02:14:41 +0000250void *__cxa_get_exception_ptr(void *unwind_exception) throw() {
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000251#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chien05d51bc2014-05-10 00:42:10 +0000252 return reinterpret_cast<void*>(
Dan Albertda5a6b12015-02-05 23:48:06 +0000253 static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]);
Logan Chien05d51bc2014-05-10 00:42:10 +0000254#else
Dan Albertda5a6b12015-02-05 23:48:06 +0000255 return cxa_exception_from_exception_unwind_exception(
256 static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr;
Logan Chien05d51bc2014-05-10 00:42:10 +0000257#endif
Marshall Clow7ac31262011-08-15 18:06:47 +0000258}
Logan Chien05d51bc2014-05-10 00:42:10 +0000259
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000260#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chien05d51bc2014-05-10 00:42:10 +0000261/*
262The routine to be called before the cleanup. This will save __cxa_exception in
263__cxa_eh_globals, so that __cxa_end_cleanup() can recover later.
264*/
Saleem Abdulrasool77a304b2015-12-04 02:14:41 +0000265bool __cxa_begin_cleanup(void *unwind_arg) throw() {
Logan Chien05d51bc2014-05-10 00:42:10 +0000266 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
267 __cxa_eh_globals* globals = __cxa_get_globals();
268 __cxa_exception* exception_header =
269 cxa_exception_from_exception_unwind_exception(unwind_exception);
270
271 if (isOurExceptionClass(unwind_exception))
272 {
273 if (0 == exception_header->propagationCount)
274 {
275 exception_header->nextPropagatingException = globals->propagatingExceptions;
276 globals->propagatingExceptions = exception_header;
277 }
278 ++exception_header->propagationCount;
279 }
280 else
281 {
282 // If the propagatingExceptions stack is not empty, since we can't
283 // chain the foreign exception, terminate it.
284 if (NULL != globals->propagatingExceptions)
285 std::terminate();
286 globals->propagatingExceptions = exception_header;
287 }
288 return true;
289}
290
291/*
292The routine to be called after the cleanup has been performed. It will get the
293propagating __cxa_exception from __cxa_eh_globals, and continue the stack
294unwinding with _Unwind_Resume.
295
296According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
297register, thus we have to write this function in assembly so that we can save
298{r1, r2, r3}. We don't have to save r0 because it is the return value and the
299first argument to _Unwind_Resume(). In addition, we are saving r4 in order to
300align the stack to 16 bytes, even though it is a callee-save register.
301*/
302__attribute__((used)) static _Unwind_Exception *
303__cxa_end_cleanup_impl()
304{
305 __cxa_eh_globals* globals = __cxa_get_globals();
306 __cxa_exception* exception_header = globals->propagatingExceptions;
307 if (NULL == exception_header)
308 {
309 // It seems that __cxa_begin_cleanup() is not called properly.
310 // We have no choice but terminate the program now.
311 std::terminate();
312 }
313
314 if (isOurExceptionClass(&exception_header->unwindHeader))
315 {
316 --exception_header->propagationCount;
317 if (0 == exception_header->propagationCount)
318 {
319 globals->propagatingExceptions = exception_header->nextPropagatingException;
320 exception_header->nextPropagatingException = NULL;
321 }
322 }
323 else
324 {
325 globals->propagatingExceptions = NULL;
326 }
327 return &exception_header->unwindHeader;
328}
329
330asm (
Logan Chienf67395c2015-12-22 14:38:30 +0000331 " .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
Logan Chien05d51bc2014-05-10 00:42:10 +0000332 " .globl __cxa_end_cleanup\n"
333 " .type __cxa_end_cleanup,%function\n"
334 "__cxa_end_cleanup:\n"
335 " push {r1, r2, r3, r4}\n"
336 " bl __cxa_end_cleanup_impl\n"
337 " pop {r1, r2, r3, r4}\n"
338 " bl _Unwind_Resume\n"
339 " bl abort\n"
340 " .popsection"
341);
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000342#endif // defined(_LIBCXXABI_ARM_EHABI)
Marshall Clow7c4652f2011-08-09 15:09:41 +0000343
Marshall Clow7c4652f2011-08-09 15:09:41 +0000344/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000345This routine can catch foreign or native exceptions. If native, the exception
346can be a primary or dependent variety. This routine may remain blissfully
347ignorant of whether the native exception is primary or dependent.
348
349If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000350* Increment's the exception's handler count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000351* Push the exception on the stack of currently-caught exceptions if it is not
352 already there (from a rethrow).
Marshall Clow7c4652f2011-08-09 15:09:41 +0000353* Decrements the uncaught_exception count.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000354* Returns the adjusted pointer to the exception object, which is stored in
355 the __cxa_exception by the personality routine.
356
357If the exception is foreign, this means it did not originate from one of throw
358routines. The foreign exception does not necessarily have a __cxa_exception
359header. However we can catch it here with a catch (...), or with a call
360to terminate or unexpected during unwinding.
361* Do not try to increment the exception's handler count, we don't know where
362 it is.
363* Push the exception on the stack of currently-caught exceptions only if the
364 stack is empty. The foreign exception has no way to link to the current
365 top of stack. If the stack is not empty, call terminate. Even with an
366 empty stack, this is hacked in by pushing a pointer to an imaginary
367 __cxa_exception block in front of the foreign exception. It would be better
368 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
369 doesn't. It has a stack of __cxa_exception (which has a next* in it).
370* Do not decrement the uncaught_exception count because we didn't increment it
371 in __cxa_throw (or one of our rethrow functions).
372* If we haven't terminated, assume the exception object is just past the
373 _Unwind_Exception and return a pointer to that.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000374*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000375void*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000376__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnanta36fb302012-01-22 19:14:27 +0000377{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000378 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
379 bool native_exception = isOurExceptionClass(unwind_exception);
380 __cxa_eh_globals* globals = __cxa_get_globals();
381 // exception_header is a hackish offset from a foreign exception, but it
382 // works as long as we're careful not to try to access any __cxa_exception
383 // parts.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000384 __cxa_exception* exception_header =
385 cxa_exception_from_exception_unwind_exception
386 (
387 static_cast<_Unwind_Exception*>(unwind_exception)
388 );
Howard Hinnant7da9b962012-01-30 16:07:00 +0000389 if (native_exception)
390 {
391 // Increment the handler count, removing the flag about being rethrown
392 exception_header->handlerCount = exception_header->handlerCount < 0 ?
393 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
394 // place the exception on the top of the stack if it's not already
395 // there by a previous rethrow
396 if (exception_header != globals->caughtExceptions)
397 {
398 exception_header->nextException = globals->caughtExceptions;
399 globals->caughtExceptions = exception_header;
400 }
401 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000402#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chien05d51bc2014-05-10 00:42:10 +0000403 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);
404#else
Howard Hinnant7da9b962012-01-30 16:07:00 +0000405 return exception_header->adjustedPtr;
Logan Chien05d51bc2014-05-10 00:42:10 +0000406#endif
Marshall Clow7ac31262011-08-15 18:06:47 +0000407 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000408 // Else this is a foreign exception
409 // If the caughtExceptions stack is not empty, terminate
410 if (globals->caughtExceptions != 0)
411 std::terminate();
412 // Push the foreign exception on to the stack
413 globals->caughtExceptions = exception_header;
414 return unwind_exception + 1;
Marshall Clow7ac31262011-08-15 18:06:47 +0000415}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000416
417
418/*
419Upon exit for any reason, a handler must call:
420 void __cxa_end_catch ();
421
Howard Hinnant7da9b962012-01-30 16:07:00 +0000422This routine can be called for either a native or foreign exception.
423For a native exception:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000424* Locates the most recently caught exception and decrements its handler count.
425* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000426* If the handler count goes down to zero, and the exception was not re-thrown
427 by throw, it locates the primary exception (which may be the same as the one
428 it's handling) and decrements its reference count. If that reference count
429 goes to zero, the function destroys the exception. In any case, if the current
430 exception is a dependent exception, it destroys that.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000431
432For a foreign exception:
433* If it has been rethrown, there is nothing to do.
434* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000435*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000436void __cxa_end_catch() {
Nico Weberd6e23362014-06-25 23:52:07 +0000437 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
438 "sizeof(__cxa_exception) must be equal to "
439 "sizeof(__cxa_dependent_exception)");
Saleem Abdulrasool4c91e382015-11-18 05:33:38 +0000440 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==
441 __builtin_offsetof(__cxa_dependent_exception,
442 primaryException),
443 "the layout of __cxa_exception must match the layout of "
444 "__cxa_dependent_exception");
445 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==
446 __builtin_offsetof(__cxa_dependent_exception, handlerCount),
447 "the layout of __cxa_exception must match the layout of "
448 "__cxa_dependent_exception");
Howard Hinnant7da9b962012-01-30 16:07:00 +0000449 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
450 __cxa_exception* exception_header = globals->caughtExceptions;
451 // If we've rethrown a foreign exception, then globals->caughtExceptions
452 // will have been made an empty stack by __cxa_rethrow() and there is
453 // nothing more to be done. Do nothing!
454 if (NULL != exception_header)
455 {
456 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
457 if (native_exception)
458 {
459 // This is a native exception
460 if (exception_header->handlerCount < 0)
461 {
462 // The exception has been rethrown by __cxa_rethrow, so don't delete it
463 if (0 == incrementHandlerCount(exception_header))
464 {
465 // Remove from the chain of uncaught exceptions
466 globals->caughtExceptions = exception_header->nextException;
467 // but don't destroy
Howard Hinnant6eb54ad2011-12-08 19:35:18 +0000468 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000469 // Keep handlerCount negative in case there are nested catch's
470 // that need to be told that this exception is rethrown. Don't
471 // erase this rethrow flag until the exception is recaught.
472 }
473 else
474 {
475 // The native exception has not been rethrown
476 if (0 == decrementHandlerCount(exception_header))
477 {
478 // Remove from the chain of uncaught exceptions
479 globals->caughtExceptions = exception_header->nextException;
480 // Destroy this exception, being careful to distinguish
481 // between dependent and primary exceptions
482 if (isDependentException(&exception_header->unwindHeader))
483 {
484 // Reset exception_header to primaryException and deallocate the dependent exception
485 __cxa_dependent_exception* dep_exception_header =
486 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
487 exception_header =
488 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
489 __cxa_free_dependent_exception(dep_exception_header);
490 }
491 // Destroy the primary exception only if its referenceCount goes to 0
492 // (this decrement must be atomic)
493 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
494 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000495 }
496 }
Howard Hinnant7da9b962012-01-30 16:07:00 +0000497 else
498 {
499 // The foreign exception has not been rethrown. Pop the stack
500 // and delete it. If there are nested catch's and they try
501 // to touch a foreign exception in any way, that is undefined
502 // behavior. They likely can't since the only way to catch
503 // a foreign exception is with catch (...)!
504 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
505 globals->caughtExceptions = 0;
506 }
Marshall Clow7c4652f2011-08-09 15:09:41 +0000507 }
Marshall Clow7ac31262011-08-15 18:06:47 +0000508}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000509
Howard Hinnanta36fb302012-01-22 19:14:27 +0000510// Note: exception_header may be masquerading as a __cxa_dependent_exception
511// and that's ok. exceptionType is there too.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000512// However watch out for foreign exceptions. Return null for them.
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000513std::type_info *__cxa_current_exception_type() {
Marshall Clow7c4652f2011-08-09 15:09:41 +0000514// get the current exception
Marshall Clow64e00972011-12-22 15:45:05 +0000515 __cxa_eh_globals *globals = __cxa_get_globals_fast();
516 if (NULL == globals)
517 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000518 __cxa_exception *exception_header = globals->caughtExceptions;
519 if (NULL == exception_header)
Marshall Clow7c4652f2011-08-09 15:09:41 +0000520 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000521 if (!isOurExceptionClass(&exception_header->unwindHeader))
522 return NULL;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000523 return exception_header->exceptionType;
Marshall Clow7ac31262011-08-15 18:06:47 +0000524}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000525
526// 2.5.4 Rethrowing Exceptions
Howard Hinnant7da9b962012-01-30 16:07:00 +0000527/* This routine can rethrow native or foreign exceptions.
528If the exception is native:
Marshall Clow7c4652f2011-08-09 15:09:41 +0000529* marks the exception object on top of the caughtExceptions stack
530 (in an implementation-defined way) as being rethrown.
531* If the caughtExceptions stack is empty, it calls terminate()
532 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant7da9b962012-01-30 16:07:00 +0000533* It then calls _Unwind_RaiseException which should not return
Howard Hinnantd4028fe2011-12-12 19:11:42 +0000534 (terminate if it does).
Howard Hinnanta36fb302012-01-22 19:14:27 +0000535 Note: exception_header may be masquerading as a __cxa_dependent_exception
536 and that's ok.
Marshall Clow7c4652f2011-08-09 15:09:41 +0000537*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000538void __cxa_rethrow() {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000539 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnant7da9b962012-01-30 16:07:00 +0000540 __cxa_exception* exception_header = globals->caughtExceptions;
541 if (NULL == exception_header)
542 std::terminate(); // throw; called outside of a exception handler
543 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
544 if (native_exception)
545 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000546 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
547 exception_header->handlerCount = -exception_header->handlerCount;
548 globals->uncaughtExceptions += 1;
549 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
550 }
551 else // this is a foreign exception
552 {
553 // The only way to communicate to __cxa_end_catch that we've rethrown
554 // a foreign exception, so don't delete us, is to pop the stack here
555 // which must be empty afterwards. Then __cxa_end_catch will do
556 // nothing
557 globals->caughtExceptions = 0;
558 }
Dan Alberta1fce462015-02-05 01:33:15 +0000559#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000560 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000561#else
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000562 _Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow7c4652f2011-08-09 15:09:41 +0000563#endif
564
Howard Hinnant7da9b962012-01-30 16:07:00 +0000565 // If we get here, some kind of unwinding error has occurred.
566 // There is some weird code generation bug happening with
567 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
568 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
569 __cxa_begin_catch(&exception_header->unwindHeader);
570 if (native_exception)
571 std::__terminate(exception_header->terminateHandler);
572 // Foreign exception: can't get exception_header->terminateHandler
573 std::terminate();
Marshall Clow7ac31262011-08-15 18:06:47 +0000574}
Marshall Clow7c4652f2011-08-09 15:09:41 +0000575
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000576/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000577 If thrown_object is not null, atomically increment the referenceCount field
578 of the __cxa_exception header associated with the thrown object referred to
579 by thrown_object.
580
581 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000582*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000583void
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000584__cxa_increment_exception_refcount(void *thrown_object) throw() {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000585 if (thrown_object != NULL )
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000586 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000587 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
588 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000589 }
590}
591
592/*
Howard Hinnant7da9b962012-01-30 16:07:00 +0000593 If thrown_object is not null, atomically decrement the referenceCount field
594 of the __cxa_exception header associated with the thrown object referred to
595 by thrown_object. If the referenceCount drops to zero, destroy and
596 deallocate the exception.
597
598 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000599*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000600void
Saleem Abdulrasoolb4ec5792015-12-04 02:14:58 +0000601__cxa_decrement_exception_refcount(void *thrown_object) throw() {
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000602 if (thrown_object != NULL )
603 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000604 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
605 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000606 {
Howard Hinnanta36fb302012-01-22 19:14:27 +0000607 if (NULL != exception_header->exceptionDestructor)
608 exception_header->exceptionDestructor(thrown_object);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000609 __cxa_free_exception(thrown_object);
610 }
611 }
612}
613
614/*
615 Returns a pointer to the thrown object (if any) at the top of the
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000616 caughtExceptions stack. Atomically increment the exception's referenceCount.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000617 If there is no such thrown object or if the thrown object is foreign,
618 returns null.
Marshall Clow0acbd982012-01-04 22:18:10 +0000619
620 We can use __cxa_get_globals_fast here to get the globals because if there have
621 been no exceptions thrown, ever, on this thread, we can return NULL without
622 the need to allocate the exception-handling globals.
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000623*/
Shoaib Meenai54227ae2017-03-01 03:55:57 +0000624void *__cxa_current_primary_exception() throw() {
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000625// get the current exception
Marshall Clowaeed3a92012-01-03 23:26:09 +0000626 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow62edc012012-01-03 23:10:20 +0000627 if (NULL == globals)
Marshall Clow2bce81b2012-01-04 14:56:09 +0000628 return NULL; // If there are no globals, there is no exception
Howard Hinnanta36fb302012-01-22 19:14:27 +0000629 __cxa_exception* exception_header = globals->caughtExceptions;
630 if (NULL == exception_header)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000631 return NULL; // No current exception
Howard Hinnant7da9b962012-01-30 16:07:00 +0000632 if (!isOurExceptionClass(&exception_header->unwindHeader))
633 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000634 if (isDependentException(&exception_header->unwindHeader)) {
635 __cxa_dependent_exception* dep_exception_header =
636 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
637 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000638 }
Howard Hinnanta36fb302012-01-22 19:14:27 +0000639 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000640 __cxa_increment_exception_refcount(thrown_object);
641 return thrown_object;
642}
643
644/*
645 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
646 stored in exc is called. Otherwise the referenceCount stored in the
647 primary exception is decremented, destroying the primary if necessary.
648 Finally the dependent exception is destroyed.
649*/
650static
651void
Howard Hinnanta36fb302012-01-22 19:14:27 +0000652dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000653{
Howard Hinnanta36fb302012-01-22 19:14:27 +0000654 __cxa_dependent_exception* dep_exception_header =
655 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000656 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnanta36fb302012-01-22 19:14:27 +0000657 std::__terminate(dep_exception_header->terminateHandler);
658 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
659 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000660}
661
662/*
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000663 If thrown_object is not null, allocate, initialize and throw a dependent
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000664 exception.
665*/
666void
667__cxa_rethrow_primary_exception(void* thrown_object)
668{
669 if ( thrown_object != NULL )
670 {
Howard Hinnant7da9b962012-01-30 16:07:00 +0000671 // thrown_object guaranteed to be native because
672 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnanta36fb302012-01-22 19:14:27 +0000673 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
674 __cxa_dependent_exception* dep_exception_header =
675 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
676 dep_exception_header->primaryException = thrown_object;
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000677 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnanta36fb302012-01-22 19:14:27 +0000678 dep_exception_header->exceptionType = exception_header->exceptionType;
679 dep_exception_header->unexpectedHandler = std::get_unexpected();
680 dep_exception_header->terminateHandler = std::get_terminate();
681 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnantdd47ca42011-12-21 23:48:05 +0000682 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnanta36fb302012-01-22 19:14:27 +0000683 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Dan Alberta1fce462015-02-05 01:33:15 +0000684#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnanta36fb302012-01-22 19:14:27 +0000685 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000686#else
Howard Hinnanta36fb302012-01-22 19:14:27 +0000687 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000688#endif
689 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnanta36fb302012-01-22 19:14:27 +0000690 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnant1b0aed92011-12-21 23:32:11 +0000691 }
692 // If we return client will call terminate()
693}
694
Howard Hinnantca6514d2012-01-24 00:01:31 +0000695bool
Marshall Clow5013d7c2015-06-02 13:03:17 +0000696__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }
697
698unsigned int
699__cxa_uncaught_exceptions() throw()
Howard Hinnantca6514d2012-01-24 00:01:31 +0000700{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000701 // This does not report foreign exceptions in flight
Howard Hinnantca6514d2012-01-24 00:01:31 +0000702 __cxa_eh_globals* globals = __cxa_get_globals_fast();
703 if (globals == 0)
Marshall Clow5013d7c2015-06-02 13:03:17 +0000704 return 0;
705 return globals->uncaughtExceptions;
Howard Hinnantca6514d2012-01-24 00:01:31 +0000706}
707
Marshall Clow47c29eb2011-07-20 15:04:39 +0000708} // extern "C"
709
Marshall Clow47c29eb2011-07-20 15:04:39 +0000710} // abi