blob: 5c32a155f09546e0e0985d6398945bd780f3cb6b [file] [log] [blame]
Marshall Clowe2dcb752011-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 Roelofsc5f7e6f2014-02-12 04:49:09 +000010// http://mentorembedded.github.io/cxx-abi/abi-eh.html
Marshall Clowe2dcb752011-07-20 15:04:39 +000011//
12//===----------------------------------------------------------------------===//
13
Richard Smith8e6107a2018-02-07 23:23:23 +000014#define _LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
15
Marshall Clowe2dcb752011-07-20 15:04:39 +000016#include "cxxabi.h"
17
18#include <exception> // for std::terminate
Howard Hinnant862c4a02013-06-17 18:10:34 +000019#include <cstring> // for memset
Marshall Clowe2dcb752011-07-20 15:04:39 +000020#include "cxa_exception.hpp"
Howard Hinnant5ec91832011-12-07 21:16:40 +000021#include "cxa_handlers.hpp"
Igor Kudrind9edde42016-10-07 08:48:28 +000022#include "fallback_malloc.h"
Eli Friedman17a47b92018-04-16 22:00:14 +000023#include "include/atomic_support.h"
Marshall Clowe2dcb752011-07-20 15:04:39 +000024
Petr Hosek229e0852017-09-13 23:35:07 +000025#if __has_feature(address_sanitizer)
Eric Fiselierc4600cc2017-09-14 22:37:34 +000026extern "C" void __asan_handle_no_return(void);
Petr Hosek229e0852017-09-13 23:35:07 +000027#endif
28
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000029// +---------------------------+-----------------------------+---------------+
30// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
31// +---------------------------+-----------------------------+---------------+
32// ^
33// |
34// +-------------------------------------------------------+
35// |
36// +---------------------------+-----------------------------+
37// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
38// +---------------------------+-----------------------------+
39
Marshall Clowe2dcb752011-07-20 15:04:39 +000040namespace __cxxabiv1 {
Howard Hinnant6830b2a2012-01-24 18:15:20 +000041
Marshall Clowe2dcb752011-07-20 15:04:39 +000042// Utility routines
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000043static
44inline
45__cxa_exception*
Howard Hinnant47cb85482012-01-30 16:07:00 +000046cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000047{
48 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clow9b454bc2011-08-15 18:06:47 +000049}
Marshall Clowe2dcb752011-07-20 15:04:39 +000050
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000051// Note: This is never called when exception_header is masquerading as a
52// __cxa_dependent_exception.
53static
54inline
55void*
Howard Hinnant47cb85482012-01-30 16:07:00 +000056thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000057{
58 return static_cast<void*>(exception_header + 1);
Marshall Clow9b454bc2011-08-15 18:06:47 +000059}
Marshall Clowe2dcb752011-07-20 15:04:39 +000060
Marshall Clow87694492011-08-09 15:09:41 +000061// Get the exception object from the unwind pointer.
62// Relies on the structure layout, where the unwind pointer is right in
63// front of the user's exception object
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000064static
65inline
66__cxa_exception*
Howard Hinnant47cb85482012-01-30 16:07:00 +000067cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000068{
69 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clow9b454bc2011-08-15 18:06:47 +000070}
Marshall Clow87694492011-08-09 15:09:41 +000071
Eric Fiselierc74a2e12017-03-04 02:04:45 +000072// Round s up to next multiple of a.
73static inline
74size_t aligned_allocation_size(size_t s, size_t a) {
75 return (s + a - 1) & ~(a - 1);
76}
77
78static inline
79size_t cxa_exception_size_from_exception_thrown_size(size_t size) {
80 return aligned_allocation_size(size + sizeof (__cxa_exception),
81 alignof(__cxa_exception));
Marshall Clow9b454bc2011-08-15 18:06:47 +000082}
Marshall Clow87694492011-08-09 15:09:41 +000083
Howard Hinnant47cb85482012-01-30 16:07:00 +000084static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000085 unwind_exception->exception_class = kOurExceptionClass;
86}
87
Howard Hinnant47cb85482012-01-30 16:07:00 +000088static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000089 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clow9b454bc2011-08-15 18:06:47 +000090}
Marshall Clow87694492011-08-09 15:09:41 +000091
92// Is it one of ours?
Howard Hinnant47cb85482012-01-30 16:07:00 +000093static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant8aa78512012-02-01 18:15:15 +000094 return (unwind_exception->exception_class & get_vendor_and_language) ==
95 (kOurExceptionClass & get_vendor_and_language);
Marshall Clow9b454bc2011-08-15 18:06:47 +000096}
Marshall Clow87694492011-08-09 15:09:41 +000097
Howard Hinnant47cb85482012-01-30 16:07:00 +000098static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000099 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000100}
101
Howard Hinnant6ccae152011-12-08 19:35:18 +0000102// This does not need to be atomic
Howard Hinnant47cb85482012-01-30 16:07:00 +0000103static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000104 return ++exception->handlerCount;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000105}
106
Howard Hinnant6ccae152011-12-08 19:35:18 +0000107// This does not need to be atomic
Howard Hinnant47cb85482012-01-30 16:07:00 +0000108static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000109 return --exception->handlerCount;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000110}
Marshall Clow87694492011-08-09 15:09:41 +0000111
Howard Hinnant5ec91832011-12-07 21:16:40 +0000112/*
Marshall Clow87694492011-08-09 15:09:41 +0000113 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
114 stored in exc is called. Otherwise the exceptionDestructor stored in
115 exc is called, and then the memory for the exception is deallocated.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000116
117 This is never called for a __cxa_dependent_exception.
Marshall Clow87694492011-08-09 15:09:41 +0000118*/
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000119static
120void
121exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
122{
123 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000124 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000125 std::__terminate(exception_header->terminateHandler);
Howard Hinnantd0bfbb32012-02-01 18:44:21 +0000126 // Just in case there exists a dependent exception that is pointing to this,
127 // check the reference count and only destroy this if that count goes to zero.
128 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000129}
Marshall Clow87694492011-08-09 15:09:41 +0000130
Eric Fiselier71e32922017-03-01 02:23:54 +0000131static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow87694492011-08-09 15:09:41 +0000132// Section 2.5.3 says:
133// * For purposes of this ABI, several things are considered exception handlers:
134// ** A terminate() call due to a throw.
135// and
136// * Upon entry, Following initialization of the catch parameter,
137// a handler must call:
Marshall Clow9b454bc2011-08-15 18:06:47 +0000138// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000139 (void) __cxa_begin_catch(&exception_header->unwindHeader);
140 std::__terminate(exception_header->terminateHandler);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000141}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000142
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000143// Return the offset of the __cxa_exception header from the start of the
144// allocated buffer. If __cxa_exception's alignment is smaller than the maximum
145// useful alignment for the target machine, padding has to be inserted before
146// the header to ensure the thrown object that follows the header is
147// sufficiently aligned. This happens if _Unwind_exception isn't double-word
148// aligned (on Darwin, for example).
149static size_t get_cxa_exception_offset() {
150 struct S {
151 } __attribute__((aligned));
152
153 // Compute the maximum alignment for the target machine.
154 constexpr size_t alignment = std::alignment_of<S>::value;
155 constexpr size_t excp_size = sizeof(__cxa_exception);
156 constexpr size_t aligned_size =
157 (excp_size + alignment - 1) / alignment * alignment;
158 constexpr size_t offset = aligned_size - excp_size;
159 static_assert((offset == 0 ||
160 std::alignment_of<_Unwind_Exception>::value < alignment),
161 "offset is non-zero only if _Unwind_Exception isn't aligned");
162 return offset;
163}
164
Marshall Clowe2dcb752011-07-20 15:04:39 +0000165extern "C" {
166
167// Allocate a __cxa_exception object, and zero-fill it.
168// Reserve "thrown_size" bytes on the end for the user's exception
169// object. Zero-fill the object. If memory can't be allocated, call
170// std::terminate. Return a pointer to the memory to be used for the
171// user's exception object.
Shoaib Meenaife989a92017-03-01 03:55:57 +0000172void *__cxa_allocate_exception(size_t thrown_size) throw() {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000173 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000174
175 // Allocate extra space before the __cxa_exception header to ensure the
176 // start of the thrown object is sufficiently aligned.
177 size_t header_offset = get_cxa_exception_offset();
178 char *raw_buffer =
179 (char *)__aligned_malloc_with_fallback(header_offset + actual_size);
180 if (NULL == raw_buffer)
Marshall Clow9b454bc2011-08-15 18:06:47 +0000181 std::terminate();
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000182 __cxa_exception *exception_header =
183 static_cast<__cxa_exception *>((void *)(raw_buffer + header_offset));
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000184 std::memset(exception_header, 0, actual_size);
185 return thrown_object_from_cxa_exception(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000186}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000187
188
189// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Shoaib Meenaife989a92017-03-01 03:55:57 +0000190void __cxa_free_exception(void *thrown_object) throw() {
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000191 // Compute the size of the padding before the header.
192 size_t header_offset = get_cxa_exception_offset();
193 char *raw_buffer =
194 ((char *)cxa_exception_from_thrown_object(thrown_object)) - header_offset;
195 __aligned_free_with_fallback((void *)raw_buffer);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000196}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000197
198
199// This function shall allocate a __cxa_dependent_exception and
200// return a pointer to it. (Really to the object, not past its' end).
201// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000202void * __cxa_allocate_dependent_exception () {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000203 size_t actual_size = sizeof(__cxa_dependent_exception);
Eric Fiselierc74a2e12017-03-04 02:04:45 +0000204 void *ptr = __aligned_malloc_with_fallback(actual_size);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000205 if (NULL == ptr)
206 std::terminate();
207 std::memset(ptr, 0, actual_size);
Marshall Clowe2dcb752011-07-20 15:04:39 +0000208 return ptr;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000209}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000210
211
212// This function shall free a dependent_exception.
213// It does not affect the reference count of the primary exception.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000214void __cxa_free_dependent_exception (void * dependent_exception) {
Eric Fiselierc74a2e12017-03-04 02:04:45 +0000215 __aligned_free_with_fallback(dependent_exception);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000216}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000217
Marshall Clow87694492011-08-09 15:09:41 +0000218
219// 2.4.3 Throwing the Exception Object
220/*
221After constructing the exception object with the throw argument value,
222the generated code calls the __cxa_throw runtime library routine. This
223routine never returns.
224
225The __cxa_throw routine will do the following:
226
227* Obtain the __cxa_exception header from the thrown exception object address,
228which can be computed as follows:
229 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
230* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
231* Save the tinfo and dest arguments in the __cxa_exception header.
232* Set the exception_class field in the unwind header. This is a 64-bit value
233representing the ASCII string "XXXXC++\0", where "XXXX" is a
234vendor-dependent string. That is, for implementations conforming to this
235ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
236* Increment the uncaught_exception flag.
237* Call _Unwind_RaiseException in the system unwind library, Its argument is the
238pointer to the thrown exception, which __cxa_throw itself received as an argument.
239__Unwind_RaiseException begins the process of stack unwinding, described
240in Section 2.5. In special cases, such as an inability to find a
241handler, _Unwind_RaiseException may return. In that case, __cxa_throw
242will call terminate, assuming that there was no handler for the
243exception.
244*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000245void
Saleem Abdulrasool12315ed2015-12-04 02:14:58 +0000246__cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000247 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000248 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000249
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000250 exception_header->unexpectedHandler = std::get_unexpected();
251 exception_header->terminateHandler = std::get_terminate();
252 exception_header->exceptionType = tinfo;
253 exception_header->exceptionDestructor = dest;
254 setExceptionClass(&exception_header->unwindHeader);
255 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow87694492011-08-09 15:09:41 +0000256 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
257
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000258 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Petr Hosek229e0852017-09-13 23:35:07 +0000259
Eric Fiselierc4600cc2017-09-14 22:37:34 +0000260#if __has_feature(address_sanitizer)
Petr Hosek229e0852017-09-13 23:35:07 +0000261 // Inform the ASan runtime that now might be a good time to clean stuff up.
262 __asan_handle_no_return();
263#endif
264
Dan Albert3bd13ca2015-02-05 01:33:15 +0000265#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000266 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000267#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000268 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000269#endif
Howard Hinnant9aa46842012-01-28 00:34:46 +0000270 // This only happens when there is no handler, or some unexpected unwinding
271 // error happens.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000272 failed_throw(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000273}
Marshall Clow87694492011-08-09 15:09:41 +0000274
275
276// 2.5.3 Exception Handlers
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000277/*
278The adjusted pointer is computed by the personality routine during phase 1
279 and saved in the exception header (either __cxa_exception or
280 __cxa_dependent_exception).
Howard Hinnant47cb85482012-01-30 16:07:00 +0000281
282 Requires: exception is native
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000283*/
Saleem Abdulrasool242d67b2015-12-04 02:14:41 +0000284void *__cxa_get_exception_ptr(void *unwind_exception) throw() {
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000285#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chiendc65ab42014-05-10 00:42:10 +0000286 return reinterpret_cast<void*>(
Dan Albertb98c20c2015-02-05 23:48:06 +0000287 static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]);
Logan Chiendc65ab42014-05-10 00:42:10 +0000288#else
Dan Albertb98c20c2015-02-05 23:48:06 +0000289 return cxa_exception_from_exception_unwind_exception(
290 static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr;
Logan Chiendc65ab42014-05-10 00:42:10 +0000291#endif
Marshall Clow9b454bc2011-08-15 18:06:47 +0000292}
Logan Chiendc65ab42014-05-10 00:42:10 +0000293
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000294#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chiendc65ab42014-05-10 00:42:10 +0000295/*
296The routine to be called before the cleanup. This will save __cxa_exception in
297__cxa_eh_globals, so that __cxa_end_cleanup() can recover later.
298*/
Saleem Abdulrasool242d67b2015-12-04 02:14:41 +0000299bool __cxa_begin_cleanup(void *unwind_arg) throw() {
Logan Chiendc65ab42014-05-10 00:42:10 +0000300 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
301 __cxa_eh_globals* globals = __cxa_get_globals();
302 __cxa_exception* exception_header =
303 cxa_exception_from_exception_unwind_exception(unwind_exception);
304
305 if (isOurExceptionClass(unwind_exception))
306 {
307 if (0 == exception_header->propagationCount)
308 {
309 exception_header->nextPropagatingException = globals->propagatingExceptions;
310 globals->propagatingExceptions = exception_header;
311 }
312 ++exception_header->propagationCount;
313 }
314 else
315 {
316 // If the propagatingExceptions stack is not empty, since we can't
317 // chain the foreign exception, terminate it.
318 if (NULL != globals->propagatingExceptions)
319 std::terminate();
320 globals->propagatingExceptions = exception_header;
321 }
322 return true;
323}
324
325/*
326The routine to be called after the cleanup has been performed. It will get the
327propagating __cxa_exception from __cxa_eh_globals, and continue the stack
328unwinding with _Unwind_Resume.
329
330According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
331register, thus we have to write this function in assembly so that we can save
332{r1, r2, r3}. We don't have to save r0 because it is the return value and the
333first argument to _Unwind_Resume(). In addition, we are saving r4 in order to
334align the stack to 16 bytes, even though it is a callee-save register.
335*/
336__attribute__((used)) static _Unwind_Exception *
337__cxa_end_cleanup_impl()
338{
339 __cxa_eh_globals* globals = __cxa_get_globals();
340 __cxa_exception* exception_header = globals->propagatingExceptions;
341 if (NULL == exception_header)
342 {
343 // It seems that __cxa_begin_cleanup() is not called properly.
344 // We have no choice but terminate the program now.
345 std::terminate();
346 }
347
348 if (isOurExceptionClass(&exception_header->unwindHeader))
349 {
350 --exception_header->propagationCount;
351 if (0 == exception_header->propagationCount)
352 {
353 globals->propagatingExceptions = exception_header->nextPropagatingException;
354 exception_header->nextPropagatingException = NULL;
355 }
356 }
357 else
358 {
359 globals->propagatingExceptions = NULL;
360 }
361 return &exception_header->unwindHeader;
362}
363
364asm (
Logan Chien338d6de2015-12-22 14:38:30 +0000365 " .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
Logan Chiendc65ab42014-05-10 00:42:10 +0000366 " .globl __cxa_end_cleanup\n"
367 " .type __cxa_end_cleanup,%function\n"
368 "__cxa_end_cleanup:\n"
369 " push {r1, r2, r3, r4}\n"
370 " bl __cxa_end_cleanup_impl\n"
371 " pop {r1, r2, r3, r4}\n"
372 " bl _Unwind_Resume\n"
373 " bl abort\n"
374 " .popsection"
375);
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000376#endif // defined(_LIBCXXABI_ARM_EHABI)
Marshall Clow87694492011-08-09 15:09:41 +0000377
Marshall Clow87694492011-08-09 15:09:41 +0000378/*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000379This routine can catch foreign or native exceptions. If native, the exception
380can be a primary or dependent variety. This routine may remain blissfully
381ignorant of whether the native exception is primary or dependent.
382
383If the exception is native:
Marshall Clow87694492011-08-09 15:09:41 +0000384* Increment's the exception's handler count.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000385* Push the exception on the stack of currently-caught exceptions if it is not
386 already there (from a rethrow).
Marshall Clow87694492011-08-09 15:09:41 +0000387* Decrements the uncaught_exception count.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000388* Returns the adjusted pointer to the exception object, which is stored in
389 the __cxa_exception by the personality routine.
390
391If the exception is foreign, this means it did not originate from one of throw
392routines. The foreign exception does not necessarily have a __cxa_exception
393header. However we can catch it here with a catch (...), or with a call
394to terminate or unexpected during unwinding.
395* Do not try to increment the exception's handler count, we don't know where
396 it is.
397* Push the exception on the stack of currently-caught exceptions only if the
398 stack is empty. The foreign exception has no way to link to the current
399 top of stack. If the stack is not empty, call terminate. Even with an
400 empty stack, this is hacked in by pushing a pointer to an imaginary
401 __cxa_exception block in front of the foreign exception. It would be better
402 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
403 doesn't. It has a stack of __cxa_exception (which has a next* in it).
404* Do not decrement the uncaught_exception count because we didn't increment it
405 in __cxa_throw (or one of our rethrow functions).
406* If we haven't terminated, assume the exception object is just past the
407 _Unwind_Exception and return a pointer to that.
Marshall Clow87694492011-08-09 15:09:41 +0000408*/
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000409void*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000410__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000411{
Howard Hinnant47cb85482012-01-30 16:07:00 +0000412 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
413 bool native_exception = isOurExceptionClass(unwind_exception);
414 __cxa_eh_globals* globals = __cxa_get_globals();
415 // exception_header is a hackish offset from a foreign exception, but it
416 // works as long as we're careful not to try to access any __cxa_exception
417 // parts.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000418 __cxa_exception* exception_header =
419 cxa_exception_from_exception_unwind_exception
420 (
421 static_cast<_Unwind_Exception*>(unwind_exception)
422 );
Howard Hinnant47cb85482012-01-30 16:07:00 +0000423 if (native_exception)
424 {
425 // Increment the handler count, removing the flag about being rethrown
426 exception_header->handlerCount = exception_header->handlerCount < 0 ?
427 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
428 // place the exception on the top of the stack if it's not already
429 // there by a previous rethrow
430 if (exception_header != globals->caughtExceptions)
431 {
432 exception_header->nextException = globals->caughtExceptions;
433 globals->caughtExceptions = exception_header;
434 }
435 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000436#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chiendc65ab42014-05-10 00:42:10 +0000437 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);
438#else
Howard Hinnant47cb85482012-01-30 16:07:00 +0000439 return exception_header->adjustedPtr;
Logan Chiendc65ab42014-05-10 00:42:10 +0000440#endif
Marshall Clow9b454bc2011-08-15 18:06:47 +0000441 }
Howard Hinnant47cb85482012-01-30 16:07:00 +0000442 // Else this is a foreign exception
443 // If the caughtExceptions stack is not empty, terminate
444 if (globals->caughtExceptions != 0)
445 std::terminate();
446 // Push the foreign exception on to the stack
447 globals->caughtExceptions = exception_header;
448 return unwind_exception + 1;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000449}
Marshall Clow87694492011-08-09 15:09:41 +0000450
451
452/*
453Upon exit for any reason, a handler must call:
454 void __cxa_end_catch ();
455
Howard Hinnant47cb85482012-01-30 16:07:00 +0000456This routine can be called for either a native or foreign exception.
457For a native exception:
Marshall Clow87694492011-08-09 15:09:41 +0000458* Locates the most recently caught exception and decrements its handler count.
459* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnant6ccae152011-12-08 19:35:18 +0000460* If the handler count goes down to zero, and the exception was not re-thrown
461 by throw, it locates the primary exception (which may be the same as the one
462 it's handling) and decrements its reference count. If that reference count
463 goes to zero, the function destroys the exception. In any case, if the current
464 exception is a dependent exception, it destroys that.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000465
466For a foreign exception:
467* If it has been rethrown, there is nothing to do.
468* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow87694492011-08-09 15:09:41 +0000469*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000470void __cxa_end_catch() {
Nico Weberae543872014-06-25 23:52:07 +0000471 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
472 "sizeof(__cxa_exception) must be equal to "
473 "sizeof(__cxa_dependent_exception)");
Saleem Abdulrasoole113b5e2015-11-18 05:33:38 +0000474 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==
475 __builtin_offsetof(__cxa_dependent_exception,
476 primaryException),
477 "the layout of __cxa_exception must match the layout of "
478 "__cxa_dependent_exception");
479 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==
480 __builtin_offsetof(__cxa_dependent_exception, handlerCount),
481 "the layout of __cxa_exception must match the layout of "
482 "__cxa_dependent_exception");
Howard Hinnant47cb85482012-01-30 16:07:00 +0000483 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
484 __cxa_exception* exception_header = globals->caughtExceptions;
485 // If we've rethrown a foreign exception, then globals->caughtExceptions
486 // will have been made an empty stack by __cxa_rethrow() and there is
487 // nothing more to be done. Do nothing!
488 if (NULL != exception_header)
489 {
490 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
491 if (native_exception)
492 {
493 // This is a native exception
494 if (exception_header->handlerCount < 0)
495 {
496 // The exception has been rethrown by __cxa_rethrow, so don't delete it
497 if (0 == incrementHandlerCount(exception_header))
498 {
499 // Remove from the chain of uncaught exceptions
500 globals->caughtExceptions = exception_header->nextException;
501 // but don't destroy
Howard Hinnant6ccae152011-12-08 19:35:18 +0000502 }
Howard Hinnant47cb85482012-01-30 16:07:00 +0000503 // Keep handlerCount negative in case there are nested catch's
504 // that need to be told that this exception is rethrown. Don't
505 // erase this rethrow flag until the exception is recaught.
506 }
507 else
508 {
509 // The native exception has not been rethrown
510 if (0 == decrementHandlerCount(exception_header))
511 {
512 // Remove from the chain of uncaught exceptions
513 globals->caughtExceptions = exception_header->nextException;
514 // Destroy this exception, being careful to distinguish
515 // between dependent and primary exceptions
516 if (isDependentException(&exception_header->unwindHeader))
517 {
518 // Reset exception_header to primaryException and deallocate the dependent exception
519 __cxa_dependent_exception* dep_exception_header =
520 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
521 exception_header =
522 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
523 __cxa_free_dependent_exception(dep_exception_header);
524 }
525 // Destroy the primary exception only if its referenceCount goes to 0
526 // (this decrement must be atomic)
527 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
528 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000529 }
530 }
Howard Hinnant47cb85482012-01-30 16:07:00 +0000531 else
532 {
533 // The foreign exception has not been rethrown. Pop the stack
534 // and delete it. If there are nested catch's and they try
535 // to touch a foreign exception in any way, that is undefined
536 // behavior. They likely can't since the only way to catch
537 // a foreign exception is with catch (...)!
538 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
539 globals->caughtExceptions = 0;
540 }
Marshall Clow87694492011-08-09 15:09:41 +0000541 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000542}
Marshall Clow87694492011-08-09 15:09:41 +0000543
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000544// Note: exception_header may be masquerading as a __cxa_dependent_exception
545// and that's ok. exceptionType is there too.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000546// However watch out for foreign exceptions. Return null for them.
Shoaib Meenaife989a92017-03-01 03:55:57 +0000547std::type_info *__cxa_current_exception_type() {
Marshall Clow87694492011-08-09 15:09:41 +0000548// get the current exception
Marshall Clow1de4fc02011-12-22 15:45:05 +0000549 __cxa_eh_globals *globals = __cxa_get_globals_fast();
550 if (NULL == globals)
551 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000552 __cxa_exception *exception_header = globals->caughtExceptions;
553 if (NULL == exception_header)
Marshall Clow87694492011-08-09 15:09:41 +0000554 return NULL; // No current exception
Howard Hinnant47cb85482012-01-30 16:07:00 +0000555 if (!isOurExceptionClass(&exception_header->unwindHeader))
556 return NULL;
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000557 return exception_header->exceptionType;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000558}
Marshall Clow87694492011-08-09 15:09:41 +0000559
560// 2.5.4 Rethrowing Exceptions
Howard Hinnant47cb85482012-01-30 16:07:00 +0000561/* This routine can rethrow native or foreign exceptions.
562If the exception is native:
Marshall Clow87694492011-08-09 15:09:41 +0000563* marks the exception object on top of the caughtExceptions stack
564 (in an implementation-defined way) as being rethrown.
565* If the caughtExceptions stack is empty, it calls terminate()
566 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant47cb85482012-01-30 16:07:00 +0000567* It then calls _Unwind_RaiseException which should not return
Howard Hinnant58926c92011-12-12 19:11:42 +0000568 (terminate if it does).
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000569 Note: exception_header may be masquerading as a __cxa_dependent_exception
570 and that's ok.
Marshall Clow87694492011-08-09 15:09:41 +0000571*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000572void __cxa_rethrow() {
Howard Hinnant47cb85482012-01-30 16:07:00 +0000573 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnant47cb85482012-01-30 16:07:00 +0000574 __cxa_exception* exception_header = globals->caughtExceptions;
575 if (NULL == exception_header)
576 std::terminate(); // throw; called outside of a exception handler
577 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
578 if (native_exception)
579 {
Howard Hinnant47cb85482012-01-30 16:07:00 +0000580 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
581 exception_header->handlerCount = -exception_header->handlerCount;
582 globals->uncaughtExceptions += 1;
583 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
584 }
585 else // this is a foreign exception
586 {
587 // The only way to communicate to __cxa_end_catch that we've rethrown
588 // a foreign exception, so don't delete us, is to pop the stack here
589 // which must be empty afterwards. Then __cxa_end_catch will do
590 // nothing
591 globals->caughtExceptions = 0;
592 }
Dan Albert3bd13ca2015-02-05 01:33:15 +0000593#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant20d6c142012-02-29 22:14:19 +0000594 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000595#else
Howard Hinnant20d6c142012-02-29 22:14:19 +0000596 _Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000597#endif
598
Howard Hinnant47cb85482012-01-30 16:07:00 +0000599 // If we get here, some kind of unwinding error has occurred.
600 // There is some weird code generation bug happening with
601 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
602 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
603 __cxa_begin_catch(&exception_header->unwindHeader);
604 if (native_exception)
605 std::__terminate(exception_header->terminateHandler);
606 // Foreign exception: can't get exception_header->terminateHandler
607 std::terminate();
Marshall Clow9b454bc2011-08-15 18:06:47 +0000608}
Marshall Clow87694492011-08-09 15:09:41 +0000609
Howard Hinnante04f5162011-12-21 23:32:11 +0000610/*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000611 If thrown_object is not null, atomically increment the referenceCount field
612 of the __cxa_exception header associated with the thrown object referred to
613 by thrown_object.
614
615 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnante04f5162011-12-21 23:32:11 +0000616*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000617void
Saleem Abdulrasool12315ed2015-12-04 02:14:58 +0000618__cxa_increment_exception_refcount(void *thrown_object) throw() {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000619 if (thrown_object != NULL )
Howard Hinnante04f5162011-12-21 23:32:11 +0000620 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000621 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Eli Friedman17a47b92018-04-16 22:00:14 +0000622 std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(1));
Howard Hinnante04f5162011-12-21 23:32:11 +0000623 }
624}
625
626/*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000627 If thrown_object is not null, atomically decrement the referenceCount field
628 of the __cxa_exception header associated with the thrown object referred to
629 by thrown_object. If the referenceCount drops to zero, destroy and
630 deallocate the exception.
631
632 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnante04f5162011-12-21 23:32:11 +0000633*/
Vlad Tsyrklevichb89e9b52018-04-09 22:11:28 +0000634_LIBCXXABI_NO_CFI
635void __cxa_decrement_exception_refcount(void *thrown_object) throw() {
Howard Hinnante04f5162011-12-21 23:32:11 +0000636 if (thrown_object != NULL )
637 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000638 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Eli Friedman17a47b92018-04-16 22:00:14 +0000639 if (std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(-1)) == 0)
Howard Hinnante04f5162011-12-21 23:32:11 +0000640 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000641 if (NULL != exception_header->exceptionDestructor)
642 exception_header->exceptionDestructor(thrown_object);
Howard Hinnante04f5162011-12-21 23:32:11 +0000643 __cxa_free_exception(thrown_object);
644 }
645 }
646}
647
648/*
649 Returns a pointer to the thrown object (if any) at the top of the
Howard Hinnantbe2eced2013-02-15 15:48:49 +0000650 caughtExceptions stack. Atomically increment the exception's referenceCount.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000651 If there is no such thrown object or if the thrown object is foreign,
652 returns null.
Marshall Clowd2bab352012-01-04 22:18:10 +0000653
654 We can use __cxa_get_globals_fast here to get the globals because if there have
655 been no exceptions thrown, ever, on this thread, we can return NULL without
656 the need to allocate the exception-handling globals.
Howard Hinnante04f5162011-12-21 23:32:11 +0000657*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000658void *__cxa_current_primary_exception() throw() {
Howard Hinnante04f5162011-12-21 23:32:11 +0000659// get the current exception
Marshall Clowf83663a2012-01-03 23:26:09 +0000660 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow3e417e72012-01-03 23:10:20 +0000661 if (NULL == globals)
Marshall Clowf3684862012-01-04 14:56:09 +0000662 return NULL; // If there are no globals, there is no exception
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000663 __cxa_exception* exception_header = globals->caughtExceptions;
664 if (NULL == exception_header)
Howard Hinnante04f5162011-12-21 23:32:11 +0000665 return NULL; // No current exception
Howard Hinnant47cb85482012-01-30 16:07:00 +0000666 if (!isOurExceptionClass(&exception_header->unwindHeader))
667 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000668 if (isDependentException(&exception_header->unwindHeader)) {
669 __cxa_dependent_exception* dep_exception_header =
670 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
671 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnante04f5162011-12-21 23:32:11 +0000672 }
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000673 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnante04f5162011-12-21 23:32:11 +0000674 __cxa_increment_exception_refcount(thrown_object);
675 return thrown_object;
676}
677
678/*
679 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
680 stored in exc is called. Otherwise the referenceCount stored in the
681 primary exception is decremented, destroying the primary if necessary.
682 Finally the dependent exception is destroyed.
683*/
684static
685void
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000686dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnante04f5162011-12-21 23:32:11 +0000687{
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000688 __cxa_dependent_exception* dep_exception_header =
689 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnante04f5162011-12-21 23:32:11 +0000690 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000691 std::__terminate(dep_exception_header->terminateHandler);
692 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
693 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnante04f5162011-12-21 23:32:11 +0000694}
695
696/*
Howard Hinnantbe2eced2013-02-15 15:48:49 +0000697 If thrown_object is not null, allocate, initialize and throw a dependent
Howard Hinnante04f5162011-12-21 23:32:11 +0000698 exception.
699*/
700void
701__cxa_rethrow_primary_exception(void* thrown_object)
702{
703 if ( thrown_object != NULL )
704 {
Howard Hinnant47cb85482012-01-30 16:07:00 +0000705 // thrown_object guaranteed to be native because
706 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000707 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
708 __cxa_dependent_exception* dep_exception_header =
709 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
710 dep_exception_header->primaryException = thrown_object;
Howard Hinnante04f5162011-12-21 23:32:11 +0000711 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000712 dep_exception_header->exceptionType = exception_header->exceptionType;
713 dep_exception_header->unexpectedHandler = std::get_unexpected();
714 dep_exception_header->terminateHandler = std::get_terminate();
715 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnant22f28b22011-12-21 23:48:05 +0000716 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000717 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Dan Albert3bd13ca2015-02-05 01:33:15 +0000718#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000719 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000720#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000721 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000722#endif
723 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000724 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000725 }
726 // If we return client will call terminate()
727}
728
Howard Hinnante33b2f52012-01-24 00:01:31 +0000729bool
Marshall Clow604de5c2015-06-02 13:03:17 +0000730__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }
731
732unsigned int
733__cxa_uncaught_exceptions() throw()
Howard Hinnante33b2f52012-01-24 00:01:31 +0000734{
Howard Hinnant47cb85482012-01-30 16:07:00 +0000735 // This does not report foreign exceptions in flight
Howard Hinnante33b2f52012-01-24 00:01:31 +0000736 __cxa_eh_globals* globals = __cxa_get_globals_fast();
737 if (globals == 0)
Marshall Clow604de5c2015-06-02 13:03:17 +0000738 return 0;
739 return globals->uncaughtExceptions;
Howard Hinnante33b2f52012-01-24 00:01:31 +0000740}
741
Marshall Clowe2dcb752011-07-20 15:04:39 +0000742} // extern "C"
743
Marshall Clowe2dcb752011-07-20 15:04:39 +0000744} // abi