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