1 //===----------------------------------------------------------------------===//
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
8 // This file implements the functionality associated with the terminate_handler,
9 // unexpected_handler, and new_handler.
10 //===----------------------------------------------------------------------===//
15 #include "abort_message.h"
17 #include "cxa_handlers.h"
18 #include "cxa_exception.h"
19 #include "private_typeinfo.h"
20 #include "include/atomic_support.h" // from libc++
26 get_unexpected() noexcept
28 return __libcpp_atomic_load(&__cxa_unexpected_handler
, _AO_Acquire
);
32 __unexpected(unexpected_handler func
)
35 // unexpected handler should not return
36 __abort_message("unexpected_handler unexpectedly returned");
39 __attribute__((noreturn
))
43 __unexpected(get_unexpected());
47 get_terminate() noexcept
49 return __libcpp_atomic_load(&__cxa_terminate_handler
, _AO_Acquire
);
53 __terminate(terminate_handler func
) noexcept
55 #ifndef _LIBCXXABI_NO_EXCEPTIONS
58 #endif // _LIBCXXABI_NO_EXCEPTIONS
60 // handler should not return
61 __abort_message("terminate_handler unexpectedly returned");
62 #ifndef _LIBCXXABI_NO_EXCEPTIONS
66 // handler should not throw exception
67 __abort_message("terminate_handler unexpectedly threw an exception");
69 #endif // _LIBCXXABI_NO_EXCEPTIONS
72 __attribute__((noreturn
))
76 #ifndef _LIBCXXABI_NO_EXCEPTIONS
77 // If there might be an uncaught exception
78 using namespace __cxxabiv1
;
79 __cxa_eh_globals
* globals
= __cxa_get_globals_fast();
82 __cxa_exception
* exception_header
= globals
->caughtExceptions
;
85 _Unwind_Exception
* unwind_exception
=
86 reinterpret_cast<_Unwind_Exception
*>(exception_header
+ 1) - 1;
87 if (__isOurExceptionClass(unwind_exception
))
88 __terminate(exception_header
->terminateHandler
);
92 __terminate(get_terminate());
96 get_new_handler() noexcept
98 return __libcpp_atomic_load(&__cxa_new_handler
, _AO_Acquire
);