1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//
8// This file implements the "Exception Handling APIs"
9// https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
10//
11//===----------------------------------------------------------------------===//
12
13#include "cxxabi.h"
14
15#include <exception> // for std::terminate
16#include <string.h> // for memset
17#include "cxa_exception.h"
18#include "cxa_handlers.h"
19#include "fallback_malloc.h"
20#include "include/atomic_support.h" // from libc++
21
22#if __has_feature(address_sanitizer)
23#include <sanitizer/asan_interface.h>
24#endif
25
26// +---------------------------+-----------------------------+---------------+
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
37namespace __cxxabiv1 {
38
39// Utility routines
40static
41inline
42__cxa_exception*
43cxa_exception_from_thrown_object(void* thrown_object)
44{
45 return static_cast<__cxa_exception*>(thrown_object) - 1;
46}
47
48// Note: This is never called when exception_header is masquerading as a
49// __cxa_dependent_exception.
50static
51inline
52void*
53thrown_object_from_cxa_exception(__cxa_exception* exception_header)
54{
55 return static_cast<void*>(exception_header + 1);
56}
57
58// 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
61static
62inline
63__cxa_exception*
64cxa_exception_from_exception_unwind_exception(_Unwind_Exception* unwind_exception)
65{
66 return cxa_exception_from_thrown_object(thrown_object: unwind_exception + 1 );
67}
68
69// 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(s: size + sizeof (__cxa_exception),
78 a: alignof(__cxa_exception));
79}
80
81void __setExceptionClass(_Unwind_Exception* unwind_exception, uint64_t newValue) {
82 ::memcpy(dest: &unwind_exception->exception_class, src: &newValue, n: sizeof(newValue));
83}
84
85
86static void setOurExceptionClass(_Unwind_Exception* unwind_exception) {
87 __setExceptionClass(unwind_exception, newValue: kOurExceptionClass);
88}
89
90static void setDependentExceptionClass(_Unwind_Exception* unwind_exception) {
91 __setExceptionClass(unwind_exception, newValue: kOurDependentExceptionClass);
92}
93
94// Is it one of ours?
95uint64_t __getExceptionClass(const _Unwind_Exception* unwind_exception) {
96 // On x86 and some ARM unwinders, unwind_exception->exception_class is
97 // a uint64_t. On other ARM unwinders, it is a char[8].
98 // See: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0038b/IHI0038B_ehabi.pdf
99 // So we just copy it into a uint64_t to be sure.
100 uint64_t exClass;
101 ::memcpy(dest: &exClass, src: &unwind_exception->exception_class, n: sizeof(exClass));
102 return exClass;
103}
104
105bool __isOurExceptionClass(const _Unwind_Exception* unwind_exception) {
106 return (__getExceptionClass(unwind_exception) & get_vendor_and_language) ==
107 (kOurExceptionClass & get_vendor_and_language);
108}
109
110static bool isDependentException(_Unwind_Exception* unwind_exception) {
111 return (__getExceptionClass(unwind_exception) & 0xFF) == 0x01;
112}
113
114// This does not need to be atomic
115static inline int incrementHandlerCount(__cxa_exception *exception) {
116 return ++exception->handlerCount;
117}
118
119// This does not need to be atomic
120static inline int decrementHandlerCount(__cxa_exception *exception) {
121 return --exception->handlerCount;
122}
123
124/*
125 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
126 stored in exc is called. Otherwise the exceptionDestructor stored in
127 exc is called, and then the memory for the exception is deallocated.
128
129 This is never called for a __cxa_dependent_exception.
130*/
131static
132void
133exception_cleanup_func(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
134{
135 __cxa_exception* exception_header = cxa_exception_from_exception_unwind_exception(unwind_exception);
136 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
137 std::__terminate(func: exception_header->terminateHandler);
138 // Just in case there exists a dependent exception that is pointing to this,
139 // check the reference count and only destroy this if that count goes to zero.
140 __cxa_decrement_exception_refcount(primary_exception: unwind_exception + 1);
141}
142
143static _LIBCXXABI_NORETURN void failed_throw(__cxa_exception* exception_header) {
144// Section 2.5.3 says:
145// * For purposes of this ABI, several things are considered exception handlers:
146// ** A terminate() call due to a throw.
147// and
148// * Upon entry, Following initialization of the catch parameter,
149// a handler must call:
150// * void *__cxa_begin_catch(void *exceptionObject );
151 (void) __cxa_begin_catch(exceptionObject: &exception_header->unwindHeader);
152 std::__terminate(func: exception_header->terminateHandler);
153}
154
155// Return the offset of the __cxa_exception header from the start of the
156// allocated buffer. If __cxa_exception's alignment is smaller than the maximum
157// useful alignment for the target machine, padding has to be inserted before
158// the header to ensure the thrown object that follows the header is
159// sufficiently aligned. This happens if _Unwind_exception isn't double-word
160// aligned (on Darwin, for example).
161static size_t get_cxa_exception_offset() {
162 struct S {
163 } __attribute__((aligned));
164
165 // Compute the maximum alignment for the target machine.
166 constexpr size_t alignment = alignof(S);
167 constexpr size_t excp_size = sizeof(__cxa_exception);
168 constexpr size_t aligned_size =
169 (excp_size + alignment - 1) / alignment * alignment;
170 constexpr size_t offset = aligned_size - excp_size;
171 static_assert((offset == 0 || alignof(_Unwind_Exception) < alignment),
172 "offset is non-zero only if _Unwind_Exception isn't aligned");
173 return offset;
174}
175
176extern "C" {
177
178// Allocate a __cxa_exception object, and zero-fill it.
179// Reserve "thrown_size" bytes on the end for the user's exception
180// object. Zero-fill the object. If memory can't be allocated, call
181// std::terminate. Return a pointer to the memory to be used for the
182// user's exception object.
183void *__cxa_allocate_exception(size_t thrown_size) throw() {
184 size_t actual_size = cxa_exception_size_from_exception_thrown_size(size: thrown_size);
185
186 // Allocate extra space before the __cxa_exception header to ensure the
187 // start of the thrown object is sufficiently aligned.
188 size_t header_offset = get_cxa_exception_offset();
189 char *raw_buffer =
190 (char *)__aligned_malloc_with_fallback(size: header_offset + actual_size);
191 if (NULL == raw_buffer)
192 std::terminate();
193 __cxa_exception *exception_header =
194 static_cast<__cxa_exception *>((void *)(raw_buffer + header_offset));
195 // We warn on memset to a non-trivially castable type. We might want to
196 // change that diagnostic to not fire on a trivially obvious zero fill.
197 ::memset(s: static_cast<void*>(exception_header), c: 0, n: actual_size);
198 return thrown_object_from_cxa_exception(exception_header);
199}
200
201
202// Free a __cxa_exception object allocated with __cxa_allocate_exception.
203void __cxa_free_exception(void *thrown_object) throw() {
204 // Compute the size of the padding before the header.
205 size_t header_offset = get_cxa_exception_offset();
206 char *raw_buffer =
207 ((char *)cxa_exception_from_thrown_object(thrown_object)) - header_offset;
208 __aligned_free_with_fallback(ptr: (void *)raw_buffer);
209}
210
211__cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
212#ifdef __wasm__
213// In Wasm, a destructor returns its argument
214 void *(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
215#else
216 void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
217#endif
218 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object: object);
219 exception_header->referenceCount = 0;
220 exception_header->unexpectedHandler = std::get_unexpected();
221 exception_header->terminateHandler = std::get_terminate();
222 exception_header->exceptionType = tinfo;
223 exception_header->exceptionDestructor = dest;
224 setOurExceptionClass(&exception_header->unwindHeader);
225 exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
226
227 return exception_header;
228}
229
230// This function shall allocate a __cxa_dependent_exception and
231// return a pointer to it. (Really to the object, not past its end).
232// Otherwise, it will work like __cxa_allocate_exception.
233void * __cxa_allocate_dependent_exception () {
234 size_t actual_size = sizeof(__cxa_dependent_exception);
235 void *ptr = __aligned_malloc_with_fallback(size: actual_size);
236 if (NULL == ptr)
237 std::terminate();
238 ::memset(s: ptr, c: 0, n: actual_size);
239 return ptr;
240}
241
242
243// This function shall free a dependent_exception.
244// It does not affect the reference count of the primary exception.
245void __cxa_free_dependent_exception (void * dependent_exception) {
246 __aligned_free_with_fallback(ptr: dependent_exception);
247}
248
249
250// 2.4.3 Throwing the Exception Object
251/*
252After constructing the exception object with the throw argument value,
253the generated code calls the __cxa_throw runtime library routine. This
254routine never returns.
255
256The __cxa_throw routine will do the following:
257
258* Obtain the __cxa_exception header from the thrown exception object address,
259which can be computed as follows:
260 __cxa_exception *header = ((__cxa_exception *) thrown_exception - 1);
261* Save the current unexpected_handler and terminate_handler in the __cxa_exception header.
262* Save the tinfo and dest arguments in the __cxa_exception header.
263* Set the exception_class field in the unwind header. This is a 64-bit value
264representing the ASCII string "XXXXC++\0", where "XXXX" is a
265vendor-dependent string. That is, for implementations conforming to this
266ABI, the low-order 4 bytes of this 64-bit value will be "C++\0".
267* Increment the uncaught_exception flag.
268* Call _Unwind_RaiseException in the system unwind library, Its argument is the
269pointer to the thrown exception, which __cxa_throw itself received as an argument.
270__Unwind_RaiseException begins the process of stack unwinding, described
271in Section 2.5. In special cases, such as an inability to find a
272handler, _Unwind_RaiseException may return. In that case, __cxa_throw
273will call terminate, assuming that there was no handler for the
274exception.
275*/
276void
277#ifdef __wasm__
278// In Wasm, a destructor returns its argument
279__cxa_throw(void *thrown_object, std::type_info *tinfo, void *(_LIBCXXABI_DTOR_FUNC *dest)(void *)) {
280#else
281__cxa_throw(void *thrown_object, std::type_info *tinfo, void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) {
282#endif
283 __cxa_eh_globals* globals = __cxa_get_globals();
284 globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
285
286 __cxa_exception* exception_header = __cxa_init_primary_exception(object: thrown_object, tinfo, dest);
287 exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
288
289#if __has_feature(address_sanitizer)
290 // Inform the ASan runtime that now might be a good time to clean stuff up.
291 __asan_handle_no_return();
292#endif
293
294#ifdef __USING_SJLJ_EXCEPTIONS__
295 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
296#else
297 _Unwind_RaiseException(exception_object: &exception_header->unwindHeader);
298#endif
299 // This only happens when there is no handler, or some unexpected unwinding
300 // error happens.
301 failed_throw(exception_header);
302}
303
304
305// 2.5.3 Exception Handlers
306/*
307The adjusted pointer is computed by the personality routine during phase 1
308 and saved in the exception header (either __cxa_exception or
309 __cxa_dependent_exception).
310
311 Requires: exception is native
312*/
313void *__cxa_get_exception_ptr(void *unwind_exception) throw() {
314#if defined(_LIBCXXABI_ARM_EHABI)
315 return reinterpret_cast<void*>(
316 static_cast<_Unwind_Control_Block*>(unwind_exception)->barrier_cache.bitpattern[0]);
317#else
318 return cxa_exception_from_exception_unwind_exception(
319 unwind_exception: static_cast<_Unwind_Exception*>(unwind_exception))->adjustedPtr;
320#endif
321}
322
323#if defined(_LIBCXXABI_ARM_EHABI)
324/*
325The routine to be called before the cleanup. This will save __cxa_exception in
326__cxa_eh_globals, so that __cxa_end_cleanup() can recover later.
327*/
328bool __cxa_begin_cleanup(void *unwind_arg) throw() {
329 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
330 __cxa_eh_globals* globals = __cxa_get_globals();
331 __cxa_exception* exception_header =
332 cxa_exception_from_exception_unwind_exception(unwind_exception);
333
334 if (__isOurExceptionClass(unwind_exception))
335 {
336 if (0 == exception_header->propagationCount)
337 {
338 exception_header->nextPropagatingException = globals->propagatingExceptions;
339 globals->propagatingExceptions = exception_header;
340 }
341 ++exception_header->propagationCount;
342 }
343 else
344 {
345 // If the propagatingExceptions stack is not empty, since we can't
346 // chain the foreign exception, terminate it.
347 if (NULL != globals->propagatingExceptions)
348 std::terminate();
349 globals->propagatingExceptions = exception_header;
350 }
351 return true;
352}
353
354/*
355The routine to be called after the cleanup has been performed. It will get the
356propagating __cxa_exception from __cxa_eh_globals, and continue the stack
357unwinding with _Unwind_Resume.
358
359According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
360register, thus we have to write this function in assembly so that we can save
361{r1, r2, r3}. We don't have to save r0 because it is the return value and the
362first argument to _Unwind_Resume(). The function also saves/restores r4 to
363keep the stack aligned and to provide a temp register. _Unwind_Resume never
364returns and we need to keep the original lr so just branch to it. When
365targeting bare metal, the function also clobbers ip/r12 to hold the address of
366_Unwind_Resume, which may be too far away for an ordinary branch.
367*/
368__attribute__((used)) static _Unwind_Exception *
369__cxa_end_cleanup_impl()
370{
371 __cxa_eh_globals* globals = __cxa_get_globals();
372 __cxa_exception* exception_header = globals->propagatingExceptions;
373 if (NULL == exception_header)
374 {
375 // It seems that __cxa_begin_cleanup() is not called properly.
376 // We have no choice but terminate the program now.
377 std::terminate();
378 }
379
380 if (__isOurExceptionClass(&exception_header->unwindHeader))
381 {
382 --exception_header->propagationCount;
383 if (0 == exception_header->propagationCount)
384 {
385 globals->propagatingExceptions = exception_header->nextPropagatingException;
386 exception_header->nextPropagatingException = NULL;
387 }
388 }
389 else
390 {
391 globals->propagatingExceptions = NULL;
392 }
393 return &exception_header->unwindHeader;
394}
395
396asm(" .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
397 " .globl __cxa_end_cleanup\n"
398 " .type __cxa_end_cleanup,%function\n"
399 "__cxa_end_cleanup:\n"
400#if defined(__ARM_FEATURE_BTI_DEFAULT)
401 " bti\n"
402#endif
403 " push {r1, r2, r3, r4}\n"
404 " mov r4, lr\n"
405 " bl __cxa_end_cleanup_impl\n"
406 " mov lr, r4\n"
407#if defined(LIBCXXABI_BAREMETAL)
408 " ldr r4, =_Unwind_Resume\n"
409 " mov ip, r4\n"
410#endif
411 " pop {r1, r2, r3, r4}\n"
412#if defined(LIBCXXABI_BAREMETAL)
413 " bx ip\n"
414#else
415 " b _Unwind_Resume\n"
416#endif
417 " .popsection");
418#endif // defined(_LIBCXXABI_ARM_EHABI)
419
420/*
421This routine can catch foreign or native exceptions. If native, the exception
422can be a primary or dependent variety. This routine may remain blissfully
423ignorant of whether the native exception is primary or dependent.
424
425If the exception is native:
426* Increment's the exception's handler count.
427* Push the exception on the stack of currently-caught exceptions if it is not
428 already there (from a rethrow).
429* Decrements the uncaught_exception count.
430* Returns the adjusted pointer to the exception object, which is stored in
431 the __cxa_exception by the personality routine.
432
433If the exception is foreign, this means it did not originate from one of throw
434routines. The foreign exception does not necessarily have a __cxa_exception
435header. However we can catch it here with a catch (...), or with a call
436to terminate or unexpected during unwinding.
437* Do not try to increment the exception's handler count, we don't know where
438 it is.
439* Push the exception on the stack of currently-caught exceptions only if the
440 stack is empty. The foreign exception has no way to link to the current
441 top of stack. If the stack is not empty, call terminate. Even with an
442 empty stack, this is hacked in by pushing a pointer to an imaginary
443 __cxa_exception block in front of the foreign exception. It would be better
444 if the __cxa_eh_globals structure had a stack of _Unwind_Exception, but it
445 doesn't. It has a stack of __cxa_exception (which has a next* in it).
446* Do not decrement the uncaught_exception count because we didn't increment it
447 in __cxa_throw (or one of our rethrow functions).
448* If we haven't terminated, assume the exception object is just past the
449 _Unwind_Exception and return a pointer to that.
450*/
451void*
452__cxa_begin_catch(void* unwind_arg) throw()
453{
454 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(unwind_arg);
455 bool native_exception = __isOurExceptionClass(unwind_exception);
456 __cxa_eh_globals* globals = __cxa_get_globals();
457 // exception_header is a hackish offset from a foreign exception, but it
458 // works as long as we're careful not to try to access any __cxa_exception
459 // parts.
460 __cxa_exception* exception_header =
461 cxa_exception_from_exception_unwind_exception
462 (
463 unwind_exception: static_cast<_Unwind_Exception*>(unwind_exception)
464 );
465
466#if defined(__MVS__)
467 // Remove the exception object from the linked list of exceptions that the z/OS unwinder
468 // maintains before adding it to the libc++abi list of caught exceptions.
469 // The libc++abi will manage the lifetime of the exception from this point forward.
470 _UnwindZOS_PopException();
471#endif
472
473 if (native_exception)
474 {
475 // Increment the handler count, removing the flag about being rethrown
476 exception_header->handlerCount = exception_header->handlerCount < 0 ?
477 -exception_header->handlerCount + 1 : exception_header->handlerCount + 1;
478 // place the exception on the top of the stack if it's not already
479 // there by a previous rethrow
480 if (exception_header != globals->caughtExceptions)
481 {
482 exception_header->nextException = globals->caughtExceptions;
483 globals->caughtExceptions = exception_header;
484 }
485 globals->uncaughtExceptions -= 1; // Not atomically, since globals are thread-local
486#if defined(_LIBCXXABI_ARM_EHABI)
487 return reinterpret_cast<void*>(exception_header->unwindHeader.barrier_cache.bitpattern[0]);
488#else
489 return exception_header->adjustedPtr;
490#endif
491 }
492 // Else this is a foreign exception
493 // If the caughtExceptions stack is not empty, terminate
494 if (globals->caughtExceptions != 0)
495 std::terminate();
496 // Push the foreign exception on to the stack
497 globals->caughtExceptions = exception_header;
498 return unwind_exception + 1;
499}
500
501
502/*
503Upon exit for any reason, a handler must call:
504 void __cxa_end_catch ();
505
506This routine can be called for either a native or foreign exception.
507For a native exception:
508* Locates the most recently caught exception and decrements its handler count.
509* Removes the exception from the caught exception stack, if the handler count goes to zero.
510* If the handler count goes down to zero, and the exception was not re-thrown
511 by throw, it locates the primary exception (which may be the same as the one
512 it's handling) and decrements its reference count. If that reference count
513 goes to zero, the function destroys the exception. In any case, if the current
514 exception is a dependent exception, it destroys that.
515
516For a foreign exception:
517* If it has been rethrown, there is nothing to do.
518* Otherwise delete the exception and pop the catch stack to empty.
519*/
520void __cxa_end_catch() {
521 static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
522 "sizeof(__cxa_exception) must be equal to "
523 "sizeof(__cxa_dependent_exception)");
524 static_assert(__builtin_offsetof(__cxa_exception, referenceCount) ==
525 __builtin_offsetof(__cxa_dependent_exception,
526 primaryException),
527 "the layout of __cxa_exception must match the layout of "
528 "__cxa_dependent_exception");
529 static_assert(__builtin_offsetof(__cxa_exception, handlerCount) ==
530 __builtin_offsetof(__cxa_dependent_exception, handlerCount),
531 "the layout of __cxa_exception must match the layout of "
532 "__cxa_dependent_exception");
533 __cxa_eh_globals* globals = __cxa_get_globals_fast(); // __cxa_get_globals called in __cxa_begin_catch
534 __cxa_exception* exception_header = globals->caughtExceptions;
535 // If we've rethrown a foreign exception, then globals->caughtExceptions
536 // will have been made an empty stack by __cxa_rethrow() and there is
537 // nothing more to be done. Do nothing!
538 if (NULL != exception_header)
539 {
540 bool native_exception = __isOurExceptionClass(unwind_exception: &exception_header->unwindHeader);
541 if (native_exception)
542 {
543 // This is a native exception
544 if (exception_header->handlerCount < 0)
545 {
546 // The exception has been rethrown by __cxa_rethrow, so don't delete it
547 if (0 == incrementHandlerCount(exception: exception_header))
548 {
549 // Remove from the chain of uncaught exceptions
550 globals->caughtExceptions = exception_header->nextException;
551 // but don't destroy
552 }
553 // Keep handlerCount negative in case there are nested catch's
554 // that need to be told that this exception is rethrown. Don't
555 // erase this rethrow flag until the exception is recaught.
556 }
557 else
558 {
559 // The native exception has not been rethrown
560 if (0 == decrementHandlerCount(exception: exception_header))
561 {
562 // Remove from the chain of uncaught exceptions
563 globals->caughtExceptions = exception_header->nextException;
564 // Destroy this exception, being careful to distinguish
565 // between dependent and primary exceptions
566 if (isDependentException(unwind_exception: &exception_header->unwindHeader))
567 {
568 // Reset exception_header to primaryException and deallocate the dependent exception
569 __cxa_dependent_exception* dep_exception_header =
570 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
571 exception_header =
572 cxa_exception_from_thrown_object(thrown_object: dep_exception_header->primaryException);
573 __cxa_free_dependent_exception(dependent_exception: dep_exception_header);
574 }
575 // Destroy the primary exception only if its referenceCount goes to 0
576 // (this decrement must be atomic)
577 __cxa_decrement_exception_refcount(primary_exception: thrown_object_from_cxa_exception(exception_header));
578 }
579 }
580 }
581 else
582 {
583 // The foreign exception has not been rethrown. Pop the stack
584 // and delete it. If there are nested catch's and they try
585 // to touch a foreign exception in any way, that is undefined
586 // behavior. They likely can't since the only way to catch
587 // a foreign exception is with catch (...)!
588 _Unwind_DeleteException(exception_object: &globals->caughtExceptions->unwindHeader);
589 globals->caughtExceptions = 0;
590 }
591 }
592}
593
594void __cxa_call_terminate(void* unwind_arg) throw() {
595 __cxa_begin_catch(unwind_arg);
596 std::terminate();
597}
598
599// Note: exception_header may be masquerading as a __cxa_dependent_exception
600// and that's ok. exceptionType is there too.
601// However watch out for foreign exceptions. Return null for them.
602std::type_info *__cxa_current_exception_type() {
603// get the current exception
604 __cxa_eh_globals *globals = __cxa_get_globals_fast();
605 if (NULL == globals)
606 return NULL; // If there have never been any exceptions, there are none now.
607 __cxa_exception *exception_header = globals->caughtExceptions;
608 if (NULL == exception_header)
609 return NULL; // No current exception
610 if (!__isOurExceptionClass(unwind_exception: &exception_header->unwindHeader))
611 return NULL;
612 return exception_header->exceptionType;
613}
614
615// 2.5.4 Rethrowing Exceptions
616/* This routine can rethrow native or foreign exceptions.
617If the exception is native:
618* marks the exception object on top of the caughtExceptions stack
619 (in an implementation-defined way) as being rethrown.
620* If the caughtExceptions stack is empty, it calls terminate()
621 (see [C++FDIS] [except.throw], 15.1.8).
622* It then calls _Unwind_RaiseException which should not return
623 (terminate if it does).
624 Note: exception_header may be masquerading as a __cxa_dependent_exception
625 and that's ok.
626*/
627void __cxa_rethrow() {
628 __cxa_eh_globals* globals = __cxa_get_globals();
629 __cxa_exception* exception_header = globals->caughtExceptions;
630 if (NULL == exception_header)
631 std::terminate(); // throw; called outside of a exception handler
632 bool native_exception = __isOurExceptionClass(unwind_exception: &exception_header->unwindHeader);
633 if (native_exception)
634 {
635 // Mark the exception as being rethrown (reverse the effects of __cxa_begin_catch)
636 exception_header->handlerCount = -exception_header->handlerCount;
637 globals->uncaughtExceptions += 1;
638 // __cxa_end_catch will remove this exception from the caughtExceptions stack if necessary
639 }
640 else // this is a foreign exception
641 {
642 // The only way to communicate to __cxa_end_catch that we've rethrown
643 // a foreign exception, so don't delete us, is to pop the stack here
644 // which must be empty afterwards. Then __cxa_end_catch will do
645 // nothing
646 globals->caughtExceptions = 0;
647 }
648#ifdef __USING_SJLJ_EXCEPTIONS__
649 _Unwind_SjLj_RaiseException(&exception_header->unwindHeader);
650#else
651 _Unwind_RaiseException(exception_object: &exception_header->unwindHeader);
652#endif
653
654 // If we get here, some kind of unwinding error has occurred.
655 // There is some weird code generation bug happening with
656 // Apple clang version 4.0 (tags/Apple/clang-418.0.2) (based on LLVM 3.1svn)
657 // If we call failed_throw here. Turns up with -O2 or higher, and -Os.
658 __cxa_begin_catch(unwind_arg: &exception_header->unwindHeader);
659 if (native_exception)
660 std::__terminate(func: exception_header->terminateHandler);
661 // Foreign exception: can't get exception_header->terminateHandler
662 std::terminate();
663}
664
665/*
666 If thrown_object is not null, atomically increment the referenceCount field
667 of the __cxa_exception header associated with the thrown object referred to
668 by thrown_object.
669
670 Requires: If thrown_object is not NULL, it is a native exception.
671*/
672void
673__cxa_increment_exception_refcount(void *thrown_object) throw() {
674 if (thrown_object != NULL )
675 {
676 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
677 std::__libcpp_atomic_add(val: &exception_header->referenceCount, a: size_t(1));
678 }
679}
680
681/*
682 If thrown_object is not null, atomically decrement the referenceCount field
683 of the __cxa_exception header associated with the thrown object referred to
684 by thrown_object. If the referenceCount drops to zero, destroy and
685 deallocate the exception.
686
687 Requires: If thrown_object is not NULL, it is a native exception.
688*/
689_LIBCXXABI_NO_CFI
690void __cxa_decrement_exception_refcount(void *thrown_object) throw() {
691 if (thrown_object != NULL )
692 {
693 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
694 if (std::__libcpp_atomic_add(val: &exception_header->referenceCount, a: size_t(-1)) == 0)
695 {
696 if (NULL != exception_header->exceptionDestructor)
697 exception_header->exceptionDestructor(thrown_object);
698 __cxa_free_exception(thrown_object);
699 }
700 }
701}
702
703/*
704 Returns a pointer to the thrown object (if any) at the top of the
705 caughtExceptions stack. Atomically increment the exception's referenceCount.
706 If there is no such thrown object or if the thrown object is foreign,
707 returns null.
708
709 We can use __cxa_get_globals_fast here to get the globals because if there have
710 been no exceptions thrown, ever, on this thread, we can return NULL without
711 the need to allocate the exception-handling globals.
712*/
713void *__cxa_current_primary_exception() throw() {
714// get the current exception
715 __cxa_eh_globals* globals = __cxa_get_globals_fast();
716 if (NULL == globals)
717 return NULL; // If there are no globals, there is no exception
718 __cxa_exception* exception_header = globals->caughtExceptions;
719 if (NULL == exception_header)
720 return NULL; // No current exception
721 if (!__isOurExceptionClass(unwind_exception: &exception_header->unwindHeader))
722 return NULL; // Can't capture a foreign exception (no way to refcount it)
723 if (isDependentException(unwind_exception: &exception_header->unwindHeader)) {
724 __cxa_dependent_exception* dep_exception_header =
725 reinterpret_cast<__cxa_dependent_exception*>(exception_header);
726 exception_header = cxa_exception_from_thrown_object(thrown_object: dep_exception_header->primaryException);
727 }
728 void* thrown_object = thrown_object_from_cxa_exception(exception_header);
729 __cxa_increment_exception_refcount(thrown_object);
730 return thrown_object;
731}
732
733/*
734 If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler
735 stored in exc is called. Otherwise the referenceCount stored in the
736 primary exception is decremented, destroying the primary if necessary.
737 Finally the dependent exception is destroyed.
738*/
739static
740void
741dependent_exception_cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* unwind_exception)
742{
743 __cxa_dependent_exception* dep_exception_header =
744 reinterpret_cast<__cxa_dependent_exception*>(unwind_exception + 1) - 1;
745 if (_URC_FOREIGN_EXCEPTION_CAUGHT != reason)
746 std::__terminate(func: dep_exception_header->terminateHandler);
747 __cxa_decrement_exception_refcount(thrown_object: dep_exception_header->primaryException);
748 __cxa_free_dependent_exception(dependent_exception: dep_exception_header);
749}
750
751/*
752 If thrown_object is not null, allocate, initialize and throw a dependent
753 exception.
754*/
755void
756__cxa_rethrow_primary_exception(void* thrown_object)
757{
758 if ( thrown_object != NULL )
759 {
760 // thrown_object guaranteed to be native because
761 // __cxa_current_primary_exception returns NULL for foreign exceptions
762 __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
763 __cxa_dependent_exception* dep_exception_header =
764 static_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception());
765 dep_exception_header->primaryException = thrown_object;
766 __cxa_increment_exception_refcount(thrown_object);
767 dep_exception_header->exceptionType = exception_header->exceptionType;
768 dep_exception_header->unexpectedHandler = std::get_unexpected();
769 dep_exception_header->terminateHandler = std::get_terminate();
770 setDependentExceptionClass(&dep_exception_header->unwindHeader);
771 __cxa_get_globals()->uncaughtExceptions += 1;
772 dep_exception_header->unwindHeader.exception_cleanup = dependent_exception_cleanup;
773#ifdef __USING_SJLJ_EXCEPTIONS__
774 _Unwind_SjLj_RaiseException(&dep_exception_header->unwindHeader);
775#else
776 _Unwind_RaiseException(exception_object: &dep_exception_header->unwindHeader);
777#endif
778 // Some sort of unwinding error. Note that terminate is a handler.
779 __cxa_begin_catch(unwind_arg: &dep_exception_header->unwindHeader);
780 }
781 // If we return client will call terminate()
782}
783
784bool
785__cxa_uncaught_exception() throw() { return __cxa_uncaught_exceptions() != 0; }
786
787unsigned int
788__cxa_uncaught_exceptions() throw()
789{
790 // This does not report foreign exceptions in flight
791 __cxa_eh_globals* globals = __cxa_get_globals_fast();
792 if (globals == 0)
793 return 0;
794 return globals->uncaughtExceptions;
795}
796
797} // extern "C"
798
799} // abi
800