/src/llvm-project-18.1.8.src/libcxxabi/src/cxa_handlers.cpp
Line | Count | Source |
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 functionality associated with the terminate_handler, |
9 | | // unexpected_handler, and new_handler. |
10 | | //===----------------------------------------------------------------------===// |
11 | | |
12 | | #include <stdexcept> |
13 | | #include <new> |
14 | | #include <exception> |
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 | | namespace std |
23 | | { |
24 | | |
25 | | unexpected_handler |
26 | | get_unexpected() noexcept |
27 | 2.29M | { |
28 | 2.29M | return __libcpp_atomic_load(&__cxa_unexpected_handler, _AO_Acquire); |
29 | 2.29M | } |
30 | | |
31 | | void |
32 | | __unexpected(unexpected_handler func) |
33 | 0 | { |
34 | 0 | func(); |
35 | | // unexpected handler should not return |
36 | 0 | abort_message("unexpected_handler unexpectedly returned"); |
37 | 0 | } |
38 | | |
39 | | __attribute__((noreturn)) |
40 | | void |
41 | | unexpected() |
42 | 0 | { |
43 | 0 | __unexpected(get_unexpected()); |
44 | 0 | } |
45 | | |
46 | | terminate_handler |
47 | | get_terminate() noexcept |
48 | 2.29M | { |
49 | 2.29M | return __libcpp_atomic_load(&__cxa_terminate_handler, _AO_Acquire); |
50 | 2.29M | } |
51 | | |
52 | | void |
53 | | __terminate(terminate_handler func) noexcept |
54 | 0 | { |
55 | 0 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
56 | 0 | try |
57 | 0 | { |
58 | 0 | #endif // _LIBCXXABI_NO_EXCEPTIONS |
59 | 0 | func(); |
60 | | // handler should not return |
61 | 0 | abort_message("terminate_handler unexpectedly returned"); |
62 | 0 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
63 | 0 | } |
64 | 0 | catch (...) |
65 | 0 | { |
66 | | // handler should not throw exception |
67 | 0 | abort_message("terminate_handler unexpectedly threw an exception"); |
68 | 0 | } |
69 | 0 | #endif // _LIBCXXABI_NO_EXCEPTIONS |
70 | 0 | } |
71 | | |
72 | | __attribute__((noreturn)) |
73 | | void |
74 | | terminate() noexcept |
75 | 0 | { |
76 | 0 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
77 | | // If there might be an uncaught exception |
78 | 0 | using namespace __cxxabiv1; |
79 | 0 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
80 | 0 | if (globals) |
81 | 0 | { |
82 | 0 | __cxa_exception* exception_header = globals->caughtExceptions; |
83 | 0 | if (exception_header) |
84 | 0 | { |
85 | 0 | _Unwind_Exception* unwind_exception = |
86 | 0 | reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; |
87 | 0 | if (__isOurExceptionClass(unwind_exception)) |
88 | 0 | __terminate(exception_header->terminateHandler); |
89 | 0 | } |
90 | 0 | } |
91 | 0 | #endif |
92 | 0 | __terminate(get_terminate()); |
93 | 0 | } |
94 | | |
95 | | new_handler |
96 | | get_new_handler() noexcept |
97 | 0 | { |
98 | 0 | return __libcpp_atomic_load(&__cxa_new_handler, _AO_Acquire); |
99 | 0 | } |
100 | | |
101 | | } // std |