blob: ce2cfba35fd64f71fd56816409398e7301e86032 [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"
Nico Weber49f09fd2014-06-25 23:55:37 +000017#include "config.h"
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000018#include "cxxabi.h"
Howard Hinnant5ec91832011-12-07 21:16:40 +000019#include "cxa_handlers.hpp"
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000020#include "cxa_exception.hpp"
21#include "private_typeinfo.h"
Howard Hinnant2642af92011-12-06 17:51:25 +000022
23namespace std
24{
25
Howard Hinnant2642af92011-12-06 17:51:25 +000026unexpected_handler
27get_unexpected() _NOEXCEPT
28{
Howard Hinnante59dbd72012-03-19 16:56:51 +000029 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 Hinnant2642af92011-12-06 17:51:25 +000033}
34
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000035__attribute__((visibility("hidden"), noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000036void
37__unexpected(unexpected_handler func)
38{
39 func();
40 // unexpected handler should not return
41 abort_message("unexpected_handler unexpectedly returned");
42}
43
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000044__attribute__((noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000045void
46unexpected()
47{
48 __unexpected(get_unexpected());
49}
50
51terminate_handler
Howard Hinnant2642af92011-12-06 17:51:25 +000052get_terminate() _NOEXCEPT
53{
Howard Hinnante59dbd72012-03-19 16:56:51 +000054 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 Hinnant2642af92011-12-06 17:51:25 +000058}
59
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000060__attribute__((visibility("hidden"), noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000061void
62__terminate(terminate_handler func) _NOEXCEPT
63{
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000064#ifndef _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant2642af92011-12-06 17:51:25 +000065 try
66 {
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000067#endif // _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant2642af92011-12-06 17:51:25 +000068 func();
69 // handler should not return
70 abort_message("terminate_handler unexpectedly returned");
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000071#ifndef _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant2642af92011-12-06 17:51:25 +000072 }
73 catch (...)
74 {
75 // handler should not throw exception
76 abort_message("terminate_handler unexpectedly threw an exception");
77 }
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000078#endif // _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant2642af92011-12-06 17:51:25 +000079}
80
Howard Hinnanta9d8ec42012-01-24 18:26:29 +000081__attribute__((noreturn))
Howard Hinnant2642af92011-12-06 17:51:25 +000082void
83terminate() _NOEXCEPT
84{
Howard Hinnantd6d4c252012-01-31 01:51:15 +000085 // 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 Hinnant8aa78512012-02-01 18:15:15 +000095 bool native_exception =
96 (unwind_exception->exception_class & get_vendor_and_language) ==
97 (kOurExceptionClass & get_vendor_and_language);
Howard Hinnantd6d4c252012-01-31 01:51:15 +000098 if (native_exception)
Howard Hinnantd6d4c252012-01-31 01:51:15 +000099 __terminate(exception_header->terminateHandler);
Howard Hinnantd6d4c252012-01-31 01:51:15 +0000100 }
101 }
Howard Hinnant2642af92011-12-06 17:51:25 +0000102 __terminate(get_terminate());
103}
104
Saleem Abdulrasool18ef5642016-05-26 02:12:20 +0000105// In the future this will become:
Howard Hinnante59dbd72012-03-19 16:56:51 +0000106// std::atomic<std::new_handler> __cxa_new_handler(0);
Saleem Abdulrasool51f0c202017-01-04 05:45:24 +0000107extern "C" {
108_LIBCXXABI_DATA_VIS new_handler __cxa_new_handler = 0;
109}
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000110
Howard Hinnant2642af92011-12-06 17:51:25 +0000111new_handler
112set_new_handler(new_handler handler) _NOEXCEPT
113{
Reid Klecknercf6b0c62014-10-03 20:03:47 +0000114 return __atomic_exchange_n(&__cxa_new_handler, handler, __ATOMIC_ACQ_REL);
Howard Hinnante59dbd72012-03-19 16:56:51 +0000115// Using of C++11 atomics this should be rewritten
116// return __cxa_new_handler.exchange(handler, memory_order_acq_rel);
Howard Hinnant2642af92011-12-06 17:51:25 +0000117}
118
119new_handler
120get_new_handler() _NOEXCEPT
121{
Howard Hinnante59dbd72012-03-19 16:56:51 +0000122 return __sync_fetch_and_add(&__cxa_new_handler, (new_handler)0);
123// The above is safe but overkill on x86
124// Using of C++11 atomics this should be rewritten
125// return __cxa_new_handler.load(memory_order_acq);
Howard Hinnant2642af92011-12-06 17:51:25 +0000126}
127
128} // std