Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 1 | //===------------------------- cxa_handlers.cpp ---------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 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" |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 16 | #include "cxxabi.h" |
Howard Hinnant | 5ec9183 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 17 | #include "cxa_handlers.hpp" |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 18 | #include "cxa_exception.hpp" |
| 19 | #include "private_typeinfo.h" |
Eli Friedman | 17a47b9 | 2018-04-16 22:00:14 +0000 | [diff] [blame] | 20 | #include "include/atomic_support.h" |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace std |
| 23 | { |
| 24 | |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 25 | unexpected_handler |
| 26 | get_unexpected() _NOEXCEPT |
| 27 | { |
Eli Friedman | 17a47b9 | 2018-04-16 22:00:14 +0000 | [diff] [blame] | 28 | return __libcpp_atomic_load(&__cxa_unexpected_handler, _AO_Acquire); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 31 | void |
| 32 | __unexpected(unexpected_handler func) |
| 33 | { |
| 34 | func(); |
| 35 | // unexpected handler should not return |
| 36 | abort_message("unexpected_handler unexpectedly returned"); |
| 37 | } |
| 38 | |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 39 | __attribute__((noreturn)) |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 40 | void |
| 41 | unexpected() |
| 42 | { |
| 43 | __unexpected(get_unexpected()); |
| 44 | } |
| 45 | |
| 46 | terminate_handler |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 47 | get_terminate() _NOEXCEPT |
| 48 | { |
Eli Friedman | 17a47b9 | 2018-04-16 22:00:14 +0000 | [diff] [blame] | 49 | return __libcpp_atomic_load(&__cxa_terminate_handler, _AO_Acquire); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 52 | void |
| 53 | __terminate(terminate_handler func) _NOEXCEPT |
| 54 | { |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 55 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 56 | try |
| 57 | { |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 58 | #endif // _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 59 | func(); |
| 60 | // handler should not return |
| 61 | abort_message("terminate_handler unexpectedly returned"); |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 62 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 63 | } |
| 64 | catch (...) |
| 65 | { |
| 66 | // handler should not throw exception |
| 67 | abort_message("terminate_handler unexpectedly threw an exception"); |
| 68 | } |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 69 | #endif // _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 72 | __attribute__((noreturn)) |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 73 | void |
| 74 | terminate() _NOEXCEPT |
| 75 | { |
Howard Hinnant | d6d4c25 | 2012-01-31 01:51:15 +0000 | [diff] [blame] | 76 | // If there might be an uncaught exception |
| 77 | using namespace __cxxabiv1; |
| 78 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 79 | if (globals) |
| 80 | { |
| 81 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 82 | if (exception_header) |
| 83 | { |
| 84 | _Unwind_Exception* unwind_exception = |
| 85 | reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; |
Marshall Clow | 611a55a | 2018-10-10 16:18:37 +0000 | [diff] [blame] | 86 | if (__isOurExceptionClass(unwind_exception)) |
Howard Hinnant | d6d4c25 | 2012-01-31 01:51:15 +0000 | [diff] [blame] | 87 | __terminate(exception_header->terminateHandler); |
Howard Hinnant | d6d4c25 | 2012-01-31 01:51:15 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 90 | __terminate(get_terminate()); |
| 91 | } |
| 92 | |
Saleem Abdulrasool | 51f0c20 | 2017-01-04 05:45:24 +0000 | [diff] [blame] | 93 | extern "C" { |
Shoaib Meenai | fe989a9 | 2017-03-01 03:55:57 +0000 | [diff] [blame] | 94 | new_handler __cxa_new_handler = 0; |
Saleem Abdulrasool | 51f0c20 | 2017-01-04 05:45:24 +0000 | [diff] [blame] | 95 | } |
Howard Hinnant | 4ac72dd | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 96 | |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 97 | new_handler |
| 98 | set_new_handler(new_handler handler) _NOEXCEPT |
| 99 | { |
Eli Friedman | 17a47b9 | 2018-04-16 22:00:14 +0000 | [diff] [blame] | 100 | return __libcpp_atomic_exchange(&__cxa_new_handler, handler, _AO_Acq_Rel); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | new_handler |
| 104 | get_new_handler() _NOEXCEPT |
| 105 | { |
Eli Friedman | 17a47b9 | 2018-04-16 22:00:14 +0000 | [diff] [blame] | 106 | return __libcpp_atomic_load(&__cxa_new_handler, _AO_Acquire); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // std |