blob: 679c32fda722a603d8f4f2f88eedc4457e7205dc [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"
Marshall Clowe2dcb752011-07-20 15:04:39 +000023
Petr Hosek229e0852017-09-13 23:35:07 +000024#if __has_feature(address_sanitizer)
Eric Fiselierc4600cc2017-09-14 22:37:34 +000025extern "C" void __asan_handle_no_return(void);
Petr Hosek229e0852017-09-13 23:35:07 +000026#endif
27
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000028// +---------------------------+-----------------------------+---------------+
29// | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
30// +---------------------------+-----------------------------+---------------+
31// ^
32// |
33// +-------------------------------------------------------+
34// |
35// +---------------------------+-----------------------------+
36// | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
37// +---------------------------+-----------------------------+
38
Marshall Clowe2dcb752011-07-20 15:04:39 +000039namespace __cxxabiv1 {
Howard Hinnant6830b2a2012-01-24 18:15:20 +000040
Marshall Clowe2dcb752011-07-20 15:04:39 +000041// Utility routines
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000042static
43inline
44__cxa_exception*
Howard Hinnant47cb85482012-01-30 16:07:00 +000045cxa_exception_from_thrown_object(void* thrown_object)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000046{
47 return static_cast<__cxa_exception*>(thrown_object) - 1;
Marshall Clow9b454bc2011-08-15 18:06:47 +000048}
Marshall Clowe2dcb752011-07-20 15:04:39 +000049
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000050// Note: This is never called when exception_header is masquerading as a
51// __cxa_dependent_exception.
52static
53inline
54void*
Howard Hinnant47cb85482012-01-30 16:07:00 +000055thrown_object_from_cxa_exception(__cxa_exception* exception_header)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000056{
57 return static_cast<void*>(exception_header + 1);
Marshall Clow9b454bc2011-08-15 18:06:47 +000058}
Marshall Clowe2dcb752011-07-20 15:04:39 +000059
Marshall Clow87694492011-08-09 15:09:41 +000060// Get the exception object from the unwind pointer.
61// Relies on the structure layout, where the unwind pointer is right in
62// front of the user's exception object
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000063static
64inline
65__cxa_exception*
Howard Hinnant47cb85482012-01-30 16:07:00 +000066cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000067{
68 return cxa_exception_from_thrown_object(unwind_exception + 1 );
Marshall Clow9b454bc2011-08-15 18:06:47 +000069}
Marshall Clow87694492011-08-09 15:09:41 +000070
Eric Fiselierc74a2e12017-03-04 02:04:45 +000071// Round s up to next multiple of a.
72static inline
73size_t aligned_allocation_size(size_t s, size_t a) {
74 return (s + a - 1) & ~(a - 1);
75}
76
77static inline
78size_t cxa_exception_size_from_exception_thrown_size(size_t size) {
79 return aligned_allocation_size(size + sizeof (__cxa_exception),
80 alignof(__cxa_exception));
Marshall Clow9b454bc2011-08-15 18:06:47 +000081}
Marshall Clow87694492011-08-09 15:09:41 +000082
Howard Hinnant47cb85482012-01-30 16:07:00 +000083static void setExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000084 unwind_exception->exception_class = kOurExceptionClass;
85}
86
Howard Hinnant47cb85482012-01-30 16:07:00 +000087static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000088 unwind_exception->exception_class = kOurDependentExceptionClass;
Marshall Clow9b454bc2011-08-15 18:06:47 +000089}
Marshall Clow87694492011-08-09 15:09:41 +000090
91// Is it one of ours?
Howard Hinnant47cb85482012-01-30 16:07:00 +000092static bool isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
Howard Hinnant8aa78512012-02-01 18:15:15 +000093 return (unwind_exception->exception_class & get_vendor_and_language) ==
94 (kOurExceptionClass & get_vendor_and_language);
Marshall Clow9b454bc2011-08-15 18:06:47 +000095}
Marshall Clow87694492011-08-09 15:09:41 +000096
Howard Hinnant47cb85482012-01-30 16:07:00 +000097static bool isDependentException(_Unwind_Exception* unwind_exception) {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +000098 return (unwind_exception->exception_class & 0xFF) == 0x01;
Marshall Clow9b454bc2011-08-15 18:06:47 +000099}
100
Howard Hinnant6ccae152011-12-08 19:35:18 +0000101// This does not need to be atomic
Howard Hinnant47cb85482012-01-30 16:07:00 +0000102static inline int incrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000103 return ++exception->handlerCount;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000104}
105
Howard Hinnant6ccae152011-12-08 19:35:18 +0000106// This does not need to be atomic
Howard Hinnant47cb85482012-01-30 16:07:00 +0000107static inline int decrementHandlerCount(__cxa_exception *exception) {
Howard Hinnant6ccae152011-12-08 19:35:18 +0000108 return --exception->handlerCount;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000109}
Marshall Clow87694492011-08-09 15:09:41 +0000110
Howard Hinnant5ec91832011-12-07 21:16:40 +0000111/*
Marshall Clow87694492011-08-09 15:09:41 +0000112 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
113 stored in exc is called. Otherwise the exceptionDestructor stored in
114 exc is called, and then the memory for the exception is deallocated.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000115
116 This is never called for a __cxa_dependent_exception.
Marshall Clow87694492011-08-09 15:09:41 +0000117*/
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000118static
119void
120exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
121{
122 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000123 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000124 std::__terminate(exception_header->terminateHandler);
Howard Hinnantd0bfbb32012-02-01 18:44:21 +0000125 // Just in case there exists a dependent exception that is pointing to this,
126 // check the reference count and only destroy this if that count goes to zero.
127 __cxa_decrement_exception_refcount(unwind_exception + 1);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000128}
Marshall Clow87694492011-08-09 15:09:41 +0000129
Eric Fiselier71e32922017-03-01 02:23:54 +0000130static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
Marshall Clow87694492011-08-09 15:09:41 +0000131// Section 2.5.3 says:
132// * For purposes of this ABI, several things are considered exception handlers:
133// ** A terminate() call due to a throw.
134// and
135// * Upon entry, Following initialization of the catch parameter,
136// a handler must call:
Marshall Clow9b454bc2011-08-15 18:06:47 +0000137// * void *__cxa_begin_catch(void *exceptionObject );
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000138 (void) __cxa_begin_catch(&exception_header->unwindHeader);
139 std::__terminate(exception_header->terminateHandler);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000140}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000141
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000142// Return the offset of the __cxa_exception header from the start of the
143// allocated buffer. If __cxa_exception's alignment is smaller than the maximum
144// useful alignment for the target machine, padding has to be inserted before
145// the header to ensure the thrown object that follows the header is
146// sufficiently aligned. This happens if _Unwind_exception isn't double-word
147// aligned (on Darwin, for example).
148static size_t get_cxa_exception_offset() {
149 struct S {
150 } __attribute__((aligned));
151
152 // Compute the maximum alignment for the target machine.
153 constexpr size_t alignment = std::alignment_of<S>::value;
154 constexpr size_t excp_size = sizeof(__cxa_exception);
155 constexpr size_t aligned_size =
156 (excp_size + alignment - 1) / alignment * alignment;
157 constexpr size_t offset = aligned_size - excp_size;
158 static_assert((offset == 0 ||
159 std::alignment_of<_Unwind_Exception>::value < alignment),
160 "offset is non-zero only if _Unwind_Exception isn't aligned");
161 return offset;
162}
163
Marshall Clowe2dcb752011-07-20 15:04:39 +0000164extern "C" {
165
166// Allocate a __cxa_exception object, and zero-fill it.
167// Reserve "thrown_size" bytes on the end for the user's exception
168// object. Zero-fill the object. If memory can't be allocated, call
169// std::terminate. Return a pointer to the memory to be used for the
170// user's exception object.
Shoaib Meenaife989a92017-03-01 03:55:57 +0000171void *__cxa_allocate_exception(size_t thrown_size) throw() {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000172 size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size);
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000173
174 // Allocate extra space before the __cxa_exception header to ensure the
175 // start of the thrown object is sufficiently aligned.
176 size_t header_offset = get_cxa_exception_offset();
177 char *raw_buffer =
178 (char *)__aligned_malloc_with_fallback(header_offset + actual_size);
179 if (NULL == raw_buffer)
Marshall Clow9b454bc2011-08-15 18:06:47 +0000180 std::terminate();
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000181 __cxa_exception *exception_header =
182 static_cast<__cxa_exception *>((void *)(raw_buffer + header_offset));
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000183 std::memset(exception_header, 0, actual_size);
184 return thrown_object_from_cxa_exception(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000185}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000186
187
188// Free a __cxa_exception object allocated with __cxa_allocate_exception.
Shoaib Meenaife989a92017-03-01 03:55:57 +0000189void __cxa_free_exception(void *thrown_object) throw() {
Akira Hatanaka9ef1daa2017-11-28 00:36:29 +0000190 // Compute the size of the padding before the header.
191 size_t header_offset = get_cxa_exception_offset();
192 char *raw_buffer =
193 ((char *)cxa_exception_from_thrown_object(thrown_object)) - header_offset;
194 __aligned_free_with_fallback((void *)raw_buffer);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000195}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000196
197
198// This function shall allocate a __cxa_dependent_exception and
199// return a pointer to it. (Really to the object, not past its' end).
200// Otherwise, it will work like __cxa_allocate_exception.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000201void * __cxa_allocate_dependent_exception () {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000202 size_t actual_size = sizeof(__cxa_dependent_exception);
Eric Fiselierc74a2e12017-03-04 02:04:45 +0000203 void *ptr = __aligned_malloc_with_fallback(actual_size);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000204 if (NULL == ptr)
205 std::terminate();
206 std::memset(ptr, 0, actual_size);
Marshall Clowe2dcb752011-07-20 15:04:39 +0000207 return ptr;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000208}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000209
210
211// This function shall free a dependent_exception.
212// It does not affect the reference count of the primary exception.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000213void __cxa_free_dependent_exception (void * dependent_exception) {
Eric Fiselierc74a2e12017-03-04 02:04:45 +0000214 __aligned_free_with_fallback(dependent_exception);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000215}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000216
Marshall Clow87694492011-08-09 15:09:41 +0000217
218// 2.4.3 Throwing the Exception Object
219/*
220After constructing the exception object with the throw argument value,
221the generated code calls the __cxa_throw runtime library routine. This
222routine never returns.
223
224The __cxa_throw routine will do the following:
225
226* Obtain the __cxa_exception header from the thrown exception object address,
227which can be computed as follows:
228 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
229* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
230* Save the tinfo and dest arguments in the __cxa_exception header.
231* Set the exception_class field in the unwind header. This is a 64-bit value
232representing the ASCII string "XXXXC++\0", where "XXXX" is a
233vendor-dependent string. That is, for implementations conforming to this
234ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
235* Increment the uncaught_exception flag.
236* Call _Unwind_RaiseException in the system unwind library, Its argument is the
237pointer to the thrown exception, which __cxa_throw itself received as an argument.
238__Unwind_RaiseException begins the process of stack unwinding, described
239in Section 2.5. In special cases, such as an inability to find a
240handler, _Unwind_RaiseException may return. In that case, __cxa_throw
241will call terminate, assuming that there was no handler for the
242exception.
243*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000244void
Saleem Abdulrasool12315ed2015-12-04 02:14:58 +0000245__cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {
Marshall Clow9b454bc2011-08-15 18:06:47 +0000246 __cxa_eh_globals *globals = __cxa_get_globals();
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000247 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000248
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000249 exception_header->unexpectedHandler = std::get_unexpected();
250 exception_header->terminateHandler = std::get_terminate();
251 exception_header->exceptionType = tinfo;
252 exception_header->exceptionDestructor = dest;
253 setExceptionClass(&exception_header->unwindHeader);
254 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
Marshall Clow87694492011-08-09 15:09:41 +0000255 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
256
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000257 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
Petr Hosek229e0852017-09-13 23:35:07 +0000258
Eric Fiselierc4600cc2017-09-14 22:37:34 +0000259#if __has_feature(address_sanitizer)
Petr Hosek229e0852017-09-13 23:35:07 +0000260 // Inform the ASan runtime that now might be a good time to clean stuff up.
261 __asan_handle_no_return();
262#endif
263
Dan Albert3bd13ca2015-02-05 01:33:15 +0000264#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000265 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000266#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000267 _Unwind_RaiseException(&exception_header->unwindHeader);
Howard Hinnant6ccae152011-12-08 19:35:18 +0000268#endif
Howard Hinnant9aa46842012-01-28 00:34:46 +0000269 // This only happens when there is no handler, or some unexpected unwinding
270 // error happens.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000271 failed_throw(exception_header);
Marshall Clow9b454bc2011-08-15 18:06:47 +0000272}
Marshall Clow87694492011-08-09 15:09:41 +0000273
274
275// 2.5.3 Exception Handlers
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000276/*
277The adjusted pointer is computed by the personality routine during phase 1
278 and saved in the exception header (either __cxa_exception or
279 __cxa_dependent_exception).
Howard Hinnant47cb85482012-01-30 16:07:00 +0000280
281 Requires: exception is native
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000282*/
Saleem Abdulrasool242d67b2015-12-04 02:14:41 +0000283void *__cxa_get_exception_ptr(void *unwind_exception) throw() {
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000284#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chiendc65ab42014-05-10 00:42:10 +0000285 return reinterpret_cast<void*>(
Dan Albertb98c20c2015-02-05 23:48:06 +0000286 static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]);
Logan Chiendc65ab42014-05-10 00:42:10 +0000287#else
Dan Albertb98c20c2015-02-05 23:48:06 +0000288 return cxa_exception_from_exception_unwind_exception(
289 static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr;
Logan Chiendc65ab42014-05-10 00:42:10 +0000290#endif
Marshall Clow9b454bc2011-08-15 18:06:47 +0000291}
Logan Chiendc65ab42014-05-10 00:42:10 +0000292
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000293#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chiendc65ab42014-05-10 00:42:10 +0000294/*
295The routine to be called before the cleanup. This will save __cxa_exception in
296__cxa_eh_globals, so that __cxa_end_cleanup() can recover later.
297*/
Saleem Abdulrasool242d67b2015-12-04 02:14:41 +0000298bool __cxa_begin_cleanup(void *unwind_arg) throw() {
Logan Chiendc65ab42014-05-10 00:42:10 +0000299 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
300 __cxa_eh_globals* globals = __cxa_get_globals();
301 __cxa_exception* exception_header =
302 cxa_exception_from_exception_unwind_exception(unwind_exception);
303
304 if (isOurExceptionClass(unwind_exception))
305 {
306 if (0 == exception_header->propagationCount)
307 {
308 exception_header->nextPropagatingException = globals->propagatingExceptions;
309 globals->propagatingExceptions = exception_header;
310 }
311 ++exception_header->propagationCount;
312 }
313 else
314 {
315 // If the propagatingExceptions stack is not empty, since we can't
316 // chain the foreign exception, terminate it.
317 if (NULL != globals->propagatingExceptions)
318 std::terminate();
319 globals->propagatingExceptions = exception_header;
320 }
321 return true;
322}
323
324/*
325The routine to be called after the cleanup has been performed. It will get the
326propagating __cxa_exception from __cxa_eh_globals, and continue the stack
327unwinding with _Unwind_Resume.
328
329According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
330register, thus we have to write this function in assembly so that we can save
331{r1, r2, r3}. We don't have to save r0 because it is the return value and the
332first argument to _Unwind_Resume(). In addition, we are saving r4 in order to
333align the stack to 16 bytes, even though it is a callee-save register.
334*/
335__attribute__((used)) static _Unwind_Exception *
336__cxa_end_cleanup_impl()
337{
338 __cxa_eh_globals* globals = __cxa_get_globals();
339 __cxa_exception* exception_header = globals->propagatingExceptions;
340 if (NULL == exception_header)
341 {
342 // It seems that __cxa_begin_cleanup() is not called properly.
343 // We have no choice but terminate the program now.
344 std::terminate();
345 }
346
347 if (isOurExceptionClass(&exception_header->unwindHeader))
348 {
349 --exception_header->propagationCount;
350 if (0 == exception_header->propagationCount)
351 {
352 globals->propagatingExceptions = exception_header->nextPropagatingException;
353 exception_header->nextPropagatingException = NULL;
354 }
355 }
356 else
357 {
358 globals->propagatingExceptions = NULL;
359 }
360 return &exception_header->unwindHeader;
361}
362
363asm (
Logan Chien338d6de2015-12-22 14:38:30 +0000364 " .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
Logan Chiendc65ab42014-05-10 00:42:10 +0000365 " .globl __cxa_end_cleanup\n"
366 " .type __cxa_end_cleanup,%function\n"
367 "__cxa_end_cleanup:\n"
368 " push {r1, r2, r3, r4}\n"
369 " bl __cxa_end_cleanup_impl\n"
370 " pop {r1, r2, r3, r4}\n"
371 " bl _Unwind_Resume\n"
372 " bl abort\n"
373 " .popsection"
374);
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000375#endif // defined(_LIBCXXABI_ARM_EHABI)
Marshall Clow87694492011-08-09 15:09:41 +0000376
Marshall Clow87694492011-08-09 15:09:41 +0000377/*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000378This routine can catch foreign or native exceptions. If native, the exception
379can be a primary or dependent variety. This routine may remain blissfully
380ignorant of whether the native exception is primary or dependent.
381
382If the exception is native:
Marshall Clow87694492011-08-09 15:09:41 +0000383* Increment's the exception's handler count.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000384* Push the exception on the stack of currently-caught exceptions if it is not
385 already there (from a rethrow).
Marshall Clow87694492011-08-09 15:09:41 +0000386* Decrements the uncaught_exception count.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000387* Returns the adjusted pointer to the exception object, which is stored in
388 the __cxa_exception by the personality routine.
389
390If the exception is foreign, this means it did not originate from one of throw
391routines. The foreign exception does not necessarily have a __cxa_exception
392header. However we can catch it here with a catch (...), or with a call
393to terminate or unexpected during unwinding.
394* Do not try to increment the exception's handler count, we don't know where
395 it is.
396* Push the exception on the stack of currently-caught exceptions only if the
397 stack is empty. The foreign exception has no way to link to the current
398 top of stack. If the stack is not empty, call terminate. Even with an
399 empty stack, this is hacked in by pushing a pointer to an imaginary
400 __cxa_exception block in front of the foreign exception. It would be better
401 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
402 doesn't. It has a stack of __cxa_exception (which has a next* in it).
403* Do not decrement the uncaught_exception count because we didn't increment it
404 in __cxa_throw (or one of our rethrow functions).
405* If we haven't terminated, assume the exception object is just past the
406 _Unwind_Exception and return a pointer to that.
Marshall Clow87694492011-08-09 15:09:41 +0000407*/
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000408void*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000409__cxa_begin_catch(void* unwind_arg) throw()
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000410{
Howard Hinnant47cb85482012-01-30 16:07:00 +0000411 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
412 bool native_exception = isOurExceptionClass(unwind_exception);
413 __cxa_eh_globals* globals = __cxa_get_globals();
414 // exception_header is a hackish offset from a foreign exception, but it
415 // works as long as we're careful not to try to access any __cxa_exception
416 // parts.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000417 __cxa_exception* exception_header =
418 cxa_exception_from_exception_unwind_exception
419 (
420 static_cast<_Unwind_Exception*>(unwind_exception)
421 );
Howard Hinnant47cb85482012-01-30 16:07:00 +0000422 if (native_exception)
423 {
424 // Increment the handler count, removing the flag about being rethrown
425 exception_header->handlerCount = exception_header->handlerCount < 0 ?
426 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
427 // place the exception on the top of the stack if it's not already
428 // there by a previous rethrow
429 if (exception_header != globals->caughtExceptions)
430 {
431 exception_header->nextException = globals->caughtExceptions;
432 globals->caughtExceptions = exception_header;
433 }
434 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000435#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chiendc65ab42014-05-10 00:42:10 +0000436 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);
437#else
Howard Hinnant47cb85482012-01-30 16:07:00 +0000438 return exception_header->adjustedPtr;
Logan Chiendc65ab42014-05-10 00:42:10 +0000439#endif
Marshall Clow9b454bc2011-08-15 18:06:47 +0000440 }
Howard Hinnant47cb85482012-01-30 16:07:00 +0000441 // Else this is a foreign exception
442 // If the caughtExceptions stack is not empty, terminate
443 if (globals->caughtExceptions != 0)
444 std::terminate();
445 // Push the foreign exception on to the stack
446 globals->caughtExceptions = exception_header;
447 return unwind_exception + 1;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000448}
Marshall Clow87694492011-08-09 15:09:41 +0000449
450
451/*
452Upon exit for any reason, a handler must call:
453 void __cxa_end_catch ();
454
Howard Hinnant47cb85482012-01-30 16:07:00 +0000455This routine can be called for either a native or foreign exception.
456For a native exception:
Marshall Clow87694492011-08-09 15:09:41 +0000457* Locates the most recently caught exception and decrements its handler count.
458* Removes the exception from the caught exception stack, if the handler count goes to zero.
Howard Hinnant6ccae152011-12-08 19:35:18 +0000459* If the handler count goes down to zero, and the exception was not re-thrown
460 by throw, it locates the primary exception (which may be the same as the one
461 it's handling) and decrements its reference count. If that reference count
462 goes to zero, the function destroys the exception. In any case, if the current
463 exception is a dependent exception, it destroys that.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000464
465For a foreign exception:
466* If it has been rethrown, there is nothing to do.
467* Otherwise delete the exception and pop the catch stack to empty.
Marshall Clow87694492011-08-09 15:09:41 +0000468*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000469void __cxa_end_catch() {
Nico Weberae543872014-06-25 23:52:07 +0000470 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
471 "sizeof(__cxa_exception) must be equal to "
472 "sizeof(__cxa_dependent_exception)");
Saleem Abdulrasoole113b5e2015-11-18 05:33:38 +0000473 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==
474 __builtin_offsetof(__cxa_dependent_exception,
475 primaryException),
476 "the layout of __cxa_exception must match the layout of "
477 "__cxa_dependent_exception");
478 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==
479 __builtin_offsetof(__cxa_dependent_exception, handlerCount),
480 "the layout of __cxa_exception must match the layout of "
481 "__cxa_dependent_exception");
Howard Hinnant47cb85482012-01-30 16:07:00 +0000482 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
483 __cxa_exception* exception_header = globals->caughtExceptions;
484 // If we've rethrown a foreign exception, then globals->caughtExceptions
485 // will have been made an empty stack by __cxa_rethrow() and there is
486 // nothing more to be done. Do nothing!
487 if (NULL != exception_header)
488 {
489 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
490 if (native_exception)
491 {
492 // This is a native exception
493 if (exception_header->handlerCount < 0)
494 {
495 // The exception has been rethrown by __cxa_rethrow, so don't delete it
496 if (0 == incrementHandlerCount(exception_header))
497 {
498 // Remove from the chain of uncaught exceptions
499 globals->caughtExceptions = exception_header->nextException;
500 // but don't destroy
Howard Hinnant6ccae152011-12-08 19:35:18 +0000501 }
Howard Hinnant47cb85482012-01-30 16:07:00 +0000502 // Keep handlerCount negative in case there are nested catch's
503 // that need to be told that this exception is rethrown. Don't
504 // erase this rethrow flag until the exception is recaught.
505 }
506 else
507 {
508 // The native exception has not been rethrown
509 if (0 == decrementHandlerCount(exception_header))
510 {
511 // Remove from the chain of uncaught exceptions
512 globals->caughtExceptions = exception_header->nextException;
513 // Destroy this exception, being careful to distinguish
514 // between dependent and primary exceptions
515 if (isDependentException(&exception_header->unwindHeader))
516 {
517 // Reset exception_header to primaryException and deallocate the dependent exception
518 __cxa_dependent_exception* dep_exception_header =
519 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
520 exception_header =
521 cxa_exception_from_thrown_object(dep_exception_header->primaryException);
522 __cxa_free_dependent_exception(dep_exception_header);
523 }
524 // Destroy the primary exception only if its referenceCount goes to 0
525 // (this decrement must be atomic)
526 __cxa_decrement_exception_refcount(thrown_object_from_cxa_exception(exception_header));
527 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000528 }
529 }
Howard Hinnant47cb85482012-01-30 16:07:00 +0000530 else
531 {
532 // The foreign exception has not been rethrown. Pop the stack
533 // and delete it. If there are nested catch's and they try
534 // to touch a foreign exception in any way, that is undefined
535 // behavior. They likely can't since the only way to catch
536 // a foreign exception is with catch (...)!
537 _Unwind_DeleteException(&globals->caughtExceptions->unwindHeader);
538 globals->caughtExceptions = 0;
539 }
Marshall Clow87694492011-08-09 15:09:41 +0000540 }
Marshall Clow9b454bc2011-08-15 18:06:47 +0000541}
Marshall Clow87694492011-08-09 15:09:41 +0000542
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000543// Note: exception_header may be masquerading as a __cxa_dependent_exception
544// and that's ok. exceptionType is there too.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000545// However watch out for foreign exceptions. Return null for them.
Shoaib Meenaife989a92017-03-01 03:55:57 +0000546std::type_info *__cxa_current_exception_type() {
Marshall Clow87694492011-08-09 15:09:41 +0000547// get the current exception
Marshall Clow1de4fc02011-12-22 15:45:05 +0000548 __cxa_eh_globals *globals = __cxa_get_globals_fast();
549 if (NULL == globals)
550 return NULL; // If there have never been any exceptions, there are none now.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000551 __cxa_exception *exception_header = globals->caughtExceptions;
552 if (NULL == exception_header)
Marshall Clow87694492011-08-09 15:09:41 +0000553 return NULL; // No current exception
Howard Hinnant47cb85482012-01-30 16:07:00 +0000554 if (!isOurExceptionClass(&exception_header->unwindHeader))
555 return NULL;
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000556 return exception_header->exceptionType;
Marshall Clow9b454bc2011-08-15 18:06:47 +0000557}
Marshall Clow87694492011-08-09 15:09:41 +0000558
559// 2.5.4 Rethrowing Exceptions
Howard Hinnant47cb85482012-01-30 16:07:00 +0000560/* This routine can rethrow native or foreign exceptions.
561If the exception is native:
Marshall Clow87694492011-08-09 15:09:41 +0000562* marks the exception object on top of the caughtExceptions stack
563 (in an implementation-defined way) as being rethrown.
564* If the caughtExceptions stack is empty, it calls terminate()
565 (see [C++FDIS] [except.throw], 15.1.8).
Howard Hinnant47cb85482012-01-30 16:07:00 +0000566* It then calls _Unwind_RaiseException which should not return
Howard Hinnant58926c92011-12-12 19:11:42 +0000567 (terminate if it does).
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000568 Note: exception_header may be masquerading as a __cxa_dependent_exception
569 and that's ok.
Marshall Clow87694492011-08-09 15:09:41 +0000570*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000571void __cxa_rethrow() {
Howard Hinnant47cb85482012-01-30 16:07:00 +0000572 __cxa_eh_globals* globals = __cxa_get_globals();
Howard Hinnant47cb85482012-01-30 16:07:00 +0000573 __cxa_exception* exception_header = globals->caughtExceptions;
574 if (NULL == exception_header)
575 std::terminate(); // throw; called outside of a exception handler
576 bool native_exception = isOurExceptionClass(&exception_header->unwindHeader);
577 if (native_exception)
578 {
Howard Hinnant47cb85482012-01-30 16:07:00 +0000579 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
580 exception_header->handlerCount = -exception_header->handlerCount;
581 globals->uncaughtExceptions += 1;
582 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
583 }
584 else // this is a foreign exception
585 {
586 // The only way to communicate to __cxa_end_catch that we've rethrown
587 // a foreign exception, so don't delete us, is to pop the stack here
588 // which must be empty afterwards. Then __cxa_end_catch will do
589 // nothing
590 globals->caughtExceptions = 0;
591 }
Dan Albert3bd13ca2015-02-05 01:33:15 +0000592#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant20d6c142012-02-29 22:14:19 +0000593 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000594#else
Howard Hinnant20d6c142012-02-29 22:14:19 +0000595 _Unwind_RaiseException(&exception_header->unwindHeader);
Marshall Clow87694492011-08-09 15:09:41 +0000596#endif
597
Howard Hinnant47cb85482012-01-30 16:07:00 +0000598 // If we get here, some kind of unwinding error has occurred.
599 // There is some weird code generation bug happening with
600 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
601 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
602 __cxa_begin_catch(&exception_header->unwindHeader);
603 if (native_exception)
604 std::__terminate(exception_header->terminateHandler);
605 // Foreign exception: can't get exception_header->terminateHandler
606 std::terminate();
Marshall Clow9b454bc2011-08-15 18:06:47 +0000607}
Marshall Clow87694492011-08-09 15:09:41 +0000608
Howard Hinnante04f5162011-12-21 23:32:11 +0000609/*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000610 If thrown_object is not null, atomically increment the referenceCount field
611 of the __cxa_exception header associated with the thrown object referred to
612 by thrown_object.
613
614 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnante04f5162011-12-21 23:32:11 +0000615*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000616void
Saleem Abdulrasool12315ed2015-12-04 02:14:58 +0000617__cxa_increment_exception_refcount(void *thrown_object) throw() {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000618 if (thrown_object != NULL )
Howard Hinnante04f5162011-12-21 23:32:11 +0000619 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000620 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
621 __sync_add_and_fetch(&exception_header->referenceCount, 1);
Howard Hinnante04f5162011-12-21 23:32:11 +0000622 }
623}
624
625/*
Howard Hinnant47cb85482012-01-30 16:07:00 +0000626 If thrown_object is not null, atomically decrement the referenceCount field
627 of the __cxa_exception header associated with the thrown object referred to
628 by thrown_object. If the referenceCount drops to zero, destroy and
629 deallocate the exception.
630
631 Requires: If thrown_object is not NULL, it is a native exception.
Howard Hinnante04f5162011-12-21 23:32:11 +0000632*/
Vlad Tsyrklevichb89e9b52018-04-09 22:11:28 +0000633_LIBCXXABI_NO_CFI
634void __cxa_decrement_exception_refcount(void *thrown_object) throw() {
Howard Hinnante04f5162011-12-21 23:32:11 +0000635 if (thrown_object != NULL )
636 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000637 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
638 if (__sync_sub_and_fetch(&exception_header->referenceCount, size_t(1)) == 0)
Howard Hinnante04f5162011-12-21 23:32:11 +0000639 {
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000640 if (NULL != exception_header->exceptionDestructor)
641 exception_header->exceptionDestructor(thrown_object);
Howard Hinnante04f5162011-12-21 23:32:11 +0000642 __cxa_free_exception(thrown_object);
643 }
644 }
645}
646
647/*
648 Returns a pointer to the thrown object (if any) at the top of the
Howard Hinnantbe2eced2013-02-15 15:48:49 +0000649 caughtExceptions stack. Atomically increment the exception's referenceCount.
Howard Hinnant47cb85482012-01-30 16:07:00 +0000650 If there is no such thrown object or if the thrown object is foreign,
651 returns null.
Marshall Clowd2bab352012-01-04 22:18:10 +0000652
653 We can use __cxa_get_globals_fast here to get the globals because if there have
654 been no exceptions thrown, ever, on this thread, we can return NULL without
655 the need to allocate the exception-handling globals.
Howard Hinnante04f5162011-12-21 23:32:11 +0000656*/
Shoaib Meenaife989a92017-03-01 03:55:57 +0000657void *__cxa_current_primary_exception() throw() {
Howard Hinnante04f5162011-12-21 23:32:11 +0000658// get the current exception
Marshall Clowf83663a2012-01-03 23:26:09 +0000659 __cxa_eh_globals* globals = __cxa_get_globals_fast();
Marshall Clow3e417e72012-01-03 23:10:20 +0000660 if (NULL == globals)
Marshall Clowf3684862012-01-04 14:56:09 +0000661 return NULL; // If there are no globals, there is no exception
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000662 __cxa_exception* exception_header = globals->caughtExceptions;
663 if (NULL == exception_header)
Howard Hinnante04f5162011-12-21 23:32:11 +0000664 return NULL; // No current exception
Howard Hinnant47cb85482012-01-30 16:07:00 +0000665 if (!isOurExceptionClass(&exception_header->unwindHeader))
666 return NULL; // Can't capture a foreign exception (no way to refcount it)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000667 if (isDependentException(&exception_header->unwindHeader)) {
668 __cxa_dependent_exception* dep_exception_header =
669 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
670 exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
Howard Hinnante04f5162011-12-21 23:32:11 +0000671 }
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000672 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
Howard Hinnante04f5162011-12-21 23:32:11 +0000673 __cxa_increment_exception_refcount(thrown_object);
674 return thrown_object;
675}
676
677/*
678 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
679 stored in exc is called. Otherwise the referenceCount stored in the
680 primary exception is decremented, destroying the primary if necessary.
681 Finally the dependent exception is destroyed.
682*/
683static
684void
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000685dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
Howard Hinnante04f5162011-12-21 23:32:11 +0000686{
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000687 __cxa_dependent_exception* dep_exception_header =
688 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
Howard Hinnante04f5162011-12-21 23:32:11 +0000689 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000690 std::__terminate(dep_exception_header->terminateHandler);
691 __cxa_decrement_exception_refcount(dep_exception_header->primaryException);
692 __cxa_free_dependent_exception(dep_exception_header);
Howard Hinnante04f5162011-12-21 23:32:11 +0000693}
694
695/*
Howard Hinnantbe2eced2013-02-15 15:48:49 +0000696 If thrown_object is not null, allocate, initialize and throw a dependent
Howard Hinnante04f5162011-12-21 23:32:11 +0000697 exception.
698*/
699void
700__cxa_rethrow_primary_exception(void* thrown_object)
701{
702 if ( thrown_object != NULL )
703 {
Howard Hinnant47cb85482012-01-30 16:07:00 +0000704 // thrown_object guaranteed to be native because
705 // __cxa_current_primary_exception returns NULL for foreign exceptions
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000706 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
707 __cxa_dependent_exception* dep_exception_header =
708 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
709 dep_exception_header->primaryException = thrown_object;
Howard Hinnante04f5162011-12-21 23:32:11 +0000710 __cxa_increment_exception_refcount(thrown_object);
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000711 dep_exception_header->exceptionType = exception_header->exceptionType;
712 dep_exception_header->unexpectedHandler = std::get_unexpected();
713 dep_exception_header->terminateHandler = std::get_terminate();
714 setDependentExceptionClass(&dep_exception_header->unwindHeader);
Howard Hinnant22f28b22011-12-21 23:48:05 +0000715 __cxa_get_globals()->uncaughtExceptions += 1;
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000716 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
Dan Albert3bd13ca2015-02-05 01:33:15 +0000717#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000718 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000719#else
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000720 _Unwind_RaiseException(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000721#endif
722 // Some sort of unwinding error. Note that terminate is a handler.
Howard Hinnantafcf7ac2012-01-22 19:14:27 +0000723 __cxa_begin_catch(&dep_exception_header->unwindHeader);
Howard Hinnante04f5162011-12-21 23:32:11 +0000724 }
725 // If we return client will call terminate()
726}
727
Howard Hinnante33b2f52012-01-24 00:01:31 +0000728bool
Marshall Clow604de5c2015-06-02 13:03:17 +0000729__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }
730
731unsigned int
732__cxa_uncaught_exceptions() throw()
Howard Hinnante33b2f52012-01-24 00:01:31 +0000733{
Howard Hinnant47cb85482012-01-30 16:07:00 +0000734 // This does not report foreign exceptions in flight
Howard Hinnante33b2f52012-01-24 00:01:31 +0000735 __cxa_eh_globals* globals = __cxa_get_globals_fast();
736 if (globals == 0)
Marshall Clow604de5c2015-06-02 13:03:17 +0000737 return 0;
738 return globals->uncaughtExceptions;
Howard Hinnante33b2f52012-01-24 00:01:31 +0000739}
740
Marshall Clowe2dcb752011-07-20 15:04:39 +0000741} // extern "C"
742
Marshall Clowe2dcb752011-07-20 15:04:39 +0000743} // abi