blob: 6c13fcd2c3cf35807075b588efef12178fc64f4f [file] [log] [blame]
Howard Hinnant2642af92011-12-06 17:51:25 +00001//===------------------------- 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 Hinnanta9d8ec42012-01-24 18:26:29 +000017#include "cxxabi.h"
Howard Hinnant5ec91832011-12-07 21:16:40 +000018#include "cxa_handlers.hpp"
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000019#include "cxa_exception.hpp"
20#include "private_typeinfo.h"
Howard Hinnant2642af92011-12-06 17:51:25 +000021
22namespace std
23{
24
Howard Hinnant2642af92011-12-06 17:51:25 +000025unexpected_handler
26get_unexpected() _NOEXCEPT
27{
Howard Hinnante59dbd72012-03-19 16:56:51 +000028 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 Hinnant2642af92011-12-06 17:51:25 +000032}
33
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000034__attribute__((visibility("hidden"), noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000035void
36__unexpected(unexpected_handler func)
37{
38 func();
39 // unexpected handler should not return
40 abort_message("unexpected_handler unexpectedly returned");
41}
42
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000043__attribute__((noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000044void
45unexpected()
46{
47 __unexpected(get_unexpected());
48}
49
50terminate_handler
Howard Hinnant2642af92011-12-06 17:51:25 +000051get_terminate() _NOEXCEPT
52{
Howard Hinnante59dbd72012-03-19 16:56:51 +000053 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 Hinnant2642af92011-12-06 17:51:25 +000057}
58
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000059__attribute__((visibility("hidden"), noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000060void
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 Hinnanta9d8ec42012-01-24 18:26:29 +000080__attribute__((noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000081void
82terminate() _NOEXCEPT
83{
Howard Hinnantd6d4c252012-01-31 01:51:15 +000084 // 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 Hinnant8aa78512012-02-01 18:15:15 +000094 bool native_exception =
95 (unwind_exception->exception_class & get_vendor_and_language) ==
96 (kOurExceptionClass & get_vendor_and_language);
Howard Hinnantd6d4c252012-01-31 01:51:15 +000097 if (native_exception)
Howard Hinnantd6d4c252012-01-31 01:51:15 +000098 __terminate(exception_header->terminateHandler);
Howard Hinnantd6d4c252012-01-31 01:51:15 +000099 }
100 }
Howard Hinnant2642af92011-12-06 17:51:25 +0000101 __terminate(get_terminate());
102}
103
Howard Hinnant9de0b352012-04-28 16:46:04 +0000104extern "C" new_handler __cxa_new_handler = 0;
Howard Hinnante59dbd72012-03-19 16:56:51 +0000105// In the future these will become:
106// std::atomic<std::new_handler> __cxa_new_handler(0);
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000107
Howard Hinnant2642af92011-12-06 17:51:25 +0000108new_handler
109set_new_handler(new_handler handler) _NOEXCEPT
110{
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000111 return __sync_swap(&__cxa_new_handler, handler);
Howard Hinnante59dbd72012-03-19 16:56:51 +0000112// Using of C++11 atomics this should be rewritten
113// return __cxa_new_handler.exchange(handler, memory_order_acq_rel);
Howard Hinnant2642af92011-12-06 17:51:25 +0000114}
115
116new_handler
117get_new_handler() _NOEXCEPT
118{
Howard Hinnante59dbd72012-03-19 16:56:51 +0000119 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 Hinnant2642af92011-12-06 17:51:25 +0000123}
124
125} // std