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