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 default terminate_handler, unexpected_handler and
9// new_handler.
10//===----------------------------------------------------------------------===//
11
12#include <cstdlib> // std::abort
13#include <exception>
14#include <new>
15#include "abort_message.h"
16#include "cxxabi.h"
17#include "cxa_handlers.h"
18#include "cxa_exception.h"
19#include "private_typeinfo.h"
20#include "include/atomic_support.h" // from libc++
21
22#if !defined(LIBCXXABI_SILENT_TERMINATE)
23
24static constinit const char* cause = "uncaught";
25
26# ifndef _LIBCXXABI_NO_EXCEPTIONS
27[[noreturn]] static void demangling_terminate_handler()
28{
29 using namespace __cxxabiv1;
30 __cxa_eh_globals* globals = __cxa_get_globals_fast();
31
32 // If there is no uncaught exception, just note that we're terminating
33 if (!globals)
34 __abort_message(format: "terminating");
35
36 __cxa_exception* exception_header = globals->caughtExceptions;
37 if (!exception_header)
38 __abort_message(format: "terminating");
39
40 _Unwind_Exception* unwind_exception =
41 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
42
43 // If we're terminating due to a foreign exception
44 if (!__isOurExceptionClass(unwind_exception))
45 __abort_message(format: "terminating due to %s foreign exception", cause);
46
47 void* thrown_object =
48 __getExceptionClass(unwind_exception) == kOurDependentExceptionClass ?
49 ((__cxa_dependent_exception*)exception_header)->primaryException :
50 exception_header + 1;
51 const __shim_type_info* thrown_type =
52 static_cast<const __shim_type_info*>(exception_header->exceptionType);
53
54 auto name = [str = thrown_type->name()] {
55# ifndef LIBCXXABI_NON_DEMANGLING_TERMINATE
56 if (const char* result = __cxxabiv1::__cxa_demangle(mangled_name: str, output_buffer: nullptr, length: nullptr, status: nullptr))
57 // We're about to abort(), this memory can never be freed; so it's fine
58 // to just return a raw pointer
59 return result;
60# endif
61 return str;
62 }();
63
64 // If the uncaught exception can be caught with std::exception&
65 const __shim_type_info* catch_type =
66 static_cast<const __shim_type_info*>(&typeid(std::exception));
67 if (catch_type->can_catch(thrown_type, adjustedPtr&: thrown_object))
68 {
69 // Include the what() message from the exception
70 const std::exception* e = static_cast<const std::exception*>(thrown_object);
71 __abort_message(format: "terminating due to %s exception of type %s: %s", cause, name, e->what());
72 }
73 else
74 {
75 // Else just note that we're terminating due to an exception
76 __abort_message(format: "terminating due to %s exception of type %s", cause, name);
77 }
78}
79#else // !_LIBCXXABI_NO_EXCEPTIONS
80[[noreturn]] static void demangling_terminate_handler()
81{
82 (void)cause;
83 __abort_message("terminating");
84}
85#endif // !_LIBCXXABI_NO_EXCEPTIONS
86
87[[noreturn]] static void demangling_unexpected_handler()
88{
89 cause = "unexpected";
90 std::terminate();
91}
92
93static constexpr std::terminate_handler default_terminate_handler = demangling_terminate_handler;
94static constexpr std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;
95#else // !LIBCXXABI_SILENT_TERMINATE
96static constexpr std::terminate_handler default_terminate_handler = std::abort;
97static constexpr std::terminate_handler default_unexpected_handler = std::terminate;
98#endif // !LIBCXXABI_SILENT_TERMINATE
99
100//
101// Global variables that hold the pointers to the current handler
102//
103_LIBCXXABI_DATA_VIS
104constinit std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
105
106_LIBCXXABI_DATA_VIS
107constinit std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
108
109_LIBCXXABI_DATA_VIS
110constinit std::new_handler __cxa_new_handler = nullptr;
111
112namespace std
113{
114
115unexpected_handler
116set_unexpected(unexpected_handler func) noexcept
117{
118 if (func == 0)
119 func = default_unexpected_handler;
120 return __libcpp_atomic_exchange(target: &__cxa_unexpected_handler, value: func,
121 order: _AO_Acq_Rel);
122}
123
124terminate_handler
125set_terminate(terminate_handler func) noexcept
126{
127 if (func == 0)
128 func = default_terminate_handler;
129 return __libcpp_atomic_exchange(target: &__cxa_terminate_handler, value: func,
130 order: _AO_Acq_Rel);
131}
132
133new_handler
134set_new_handler(new_handler handler) noexcept
135{
136 return __libcpp_atomic_exchange(target: &__cxa_new_handler, value: handler, order: _AO_Acq_Rel);
137}
138
139}
140