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" |
Nico Weber | 49f09fd | 2014-06-25 23:55:37 +0000 | [diff] [blame] | 17 | #include "config.h" |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 18 | #include "cxxabi.h" |
Howard Hinnant | 5ec9183 | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 19 | #include "cxa_handlers.hpp" |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 20 | #include "cxa_exception.hpp" |
| 21 | #include "private_typeinfo.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 | { |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 29 | return __sync_fetch_and_add(&__cxa_unexpected_handler, (unexpected_handler)0); |
| 30 | // The above is safe but overkill on x86 |
| 31 | // Using of C++11 atomics this should be rewritten |
| 32 | // return __cxa_unexpected_handler.load(memory_order_acq); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 35 | __attribute__((visibility("hidden"), noreturn)) |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 36 | void |
| 37 | __unexpected(unexpected_handler func) |
| 38 | { |
| 39 | func(); |
| 40 | // unexpected handler should not return |
| 41 | abort_message("unexpected_handler unexpectedly returned"); |
| 42 | } |
| 43 | |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 44 | __attribute__((noreturn)) |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 45 | void |
| 46 | unexpected() |
| 47 | { |
| 48 | __unexpected(get_unexpected()); |
| 49 | } |
| 50 | |
| 51 | terminate_handler |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 52 | get_terminate() _NOEXCEPT |
| 53 | { |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 54 | return __sync_fetch_and_add(&__cxa_terminate_handler, (terminate_handler)0); |
| 55 | // The above is safe but overkill on x86 |
| 56 | // Using of C++11 atomics this should be rewritten |
| 57 | // return __cxa_terminate_handler.load(memory_order_acq); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 60 | __attribute__((visibility("hidden"), noreturn)) |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 61 | void |
| 62 | __terminate(terminate_handler func) _NOEXCEPT |
| 63 | { |
| 64 | #if __has_feature(cxx_exceptions) |
| 65 | try |
| 66 | { |
| 67 | #endif // __has_feature(cxx_exceptions) |
| 68 | func(); |
| 69 | // handler should not return |
| 70 | abort_message("terminate_handler unexpectedly returned"); |
| 71 | #if __has_feature(cxx_exceptions) |
| 72 | } |
| 73 | catch (...) |
| 74 | { |
| 75 | // handler should not throw exception |
| 76 | abort_message("terminate_handler unexpectedly threw an exception"); |
| 77 | } |
| 78 | #endif // #if __has_feature(cxx_exceptions) |
| 79 | } |
| 80 | |
Howard Hinnant | a9d8ec4 | 2012-01-24 18:26:29 +0000 | [diff] [blame] | 81 | __attribute__((noreturn)) |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 82 | void |
| 83 | terminate() _NOEXCEPT |
| 84 | { |
Howard Hinnant | d6d4c25 | 2012-01-31 01:51:15 +0000 | [diff] [blame] | 85 | // If there might be an uncaught exception |
| 86 | using namespace __cxxabiv1; |
| 87 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 88 | if (globals) |
| 89 | { |
| 90 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 91 | if (exception_header) |
| 92 | { |
| 93 | _Unwind_Exception* unwind_exception = |
| 94 | reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; |
Howard Hinnant | 8aa7851 | 2012-02-01 18:15:15 +0000 | [diff] [blame] | 95 | bool native_exception = |
| 96 | (unwind_exception->exception_class & get_vendor_and_language) == |
| 97 | (kOurExceptionClass & get_vendor_and_language); |
Howard Hinnant | d6d4c25 | 2012-01-31 01:51:15 +0000 | [diff] [blame] | 98 | if (native_exception) |
Howard Hinnant | d6d4c25 | 2012-01-31 01:51:15 +0000 | [diff] [blame] | 99 | __terminate(exception_header->terminateHandler); |
Howard Hinnant | d6d4c25 | 2012-01-31 01:51:15 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 102 | __terminate(get_terminate()); |
| 103 | } |
| 104 | |
Reid Kleckner | fa990f0 | 2015-04-15 15:35:56 +0000 | [diff] [blame] | 105 | extern "C" new_handler __cxa_new_handler = 0; |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 106 | // In the future these will become: |
| 107 | // std::atomic<std::new_handler> __cxa_new_handler(0); |
Howard Hinnant | 4ac72dd | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 108 | |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 109 | new_handler |
| 110 | set_new_handler(new_handler handler) _NOEXCEPT |
| 111 | { |
Reid Kleckner | cf6b0c6 | 2014-10-03 20:03:47 +0000 | [diff] [blame] | 112 | return __atomic_exchange_n(&__cxa_new_handler, handler, __ATOMIC_ACQ_REL); |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 113 | // Using of C++11 atomics this should be rewritten |
| 114 | // return __cxa_new_handler.exchange(handler, memory_order_acq_rel); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | new_handler |
| 118 | get_new_handler() _NOEXCEPT |
| 119 | { |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 120 | return __sync_fetch_and_add(&__cxa_new_handler, (new_handler)0); |
| 121 | // The above is safe but overkill on x86 |
| 122 | // Using of C++11 atomics this should be rewritten |
| 123 | // return __cxa_new_handler.load(memory_order_acq); |
Howard Hinnant | 2642af9 | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // std |