• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===------------------------- cxa_default_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 default terminate_handler and unexpected_handler.
10 //===----------------------------------------------------------------------===//
11 
12 #include <stdexcept>
13 #include <new>
14 #include <exception>
15 #include <cstdlib>
16 #include "abort_message.h"
17 #include "config.h" // For __sync_swap
18 #include "cxxabi.h"
19 #include "cxa_handlers.hpp"
20 #include "cxa_exception.hpp"
21 #include "private_typeinfo.h"
22 
23 static const char* cause = "uncaught";
24 
25 __attribute__((noreturn))
demangling_terminate_handler()26 static void demangling_terminate_handler()
27 {
28     // If there might be an uncaught exception
29     using namespace __cxxabiv1;
30     __cxa_eh_globals* globals = __cxa_get_globals_fast();
31     if (globals)
32     {
33         __cxa_exception* exception_header = globals->caughtExceptions;
34         // If there is an uncaught exception
35         if (exception_header)
36         {
37             _Unwind_Exception* unwind_exception =
38                 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
39             bool native_exception =
40                 (unwind_exception->exception_class   & get_vendor_and_language) ==
41                                  (kOurExceptionClass & get_vendor_and_language);
42             if (native_exception)
43             {
44                 void* thrown_object =
45                     unwind_exception->exception_class == kOurDependentExceptionClass ?
46                         ((__cxa_dependent_exception*)exception_header)->primaryException :
47                         exception_header + 1;
48                 const __shim_type_info* thrown_type =
49                     static_cast<const __shim_type_info*>(exception_header->exceptionType);
50                 // Try to get demangled name of thrown_type
51                 int status;
52                 char buf[1024];
53                 size_t len = sizeof(buf);
54                 const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
55                 if (status != 0)
56                     name = thrown_type->name();
57                 // If the uncaught exception can be caught with std::exception&
58                 const __shim_type_info* catch_type =
59 				 static_cast<const __shim_type_info*>(&typeid(std::exception));
60                 if (catch_type->can_catch(thrown_type, thrown_object))
61                 {
62                     // Include the what() message from the exception
63                     const std::exception* e = static_cast<const std::exception*>(thrown_object);
64                     abort_message("terminating with %s exception of type %s: %s",
65                                   cause, name, e->what());
66                 }
67                 else
68                     // Else just note that we're terminating with an exception
69                     abort_message("terminating with %s exception of type %s",
70                                    cause, name);
71             }
72             else
73                 // Else we're terminating with a foreign exception
74                 abort_message("terminating with %s foreign exception", cause);
75         }
76     }
77     // Else just note that we're terminating
78     abort_message("terminating");
79 }
80 
81 __attribute__((noreturn))
demangling_unexpected_handler()82 static void demangling_unexpected_handler()
83 {
84     cause = "unexpected";
85     std::terminate();
86 }
87 
88 #if !LIBCXXABI_SILENT_TERMINATE
89 static std::terminate_handler default_terminate_handler = demangling_terminate_handler;
90 static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;
91 #else
92 static std::terminate_handler default_terminate_handler = std::abort;
93 static std::terminate_handler default_unexpected_handler = std::abort;
94 #endif
95 
96 //
97 // Global variables that hold the pointers to the current handler
98 //
99 _LIBCXXABI_DATA_VIS
100 std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
101 
102 _LIBCXXABI_DATA_VIS
103 std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
104 
105 // In the future these will become:
106 // std::atomic<std::terminate_handler>  __cxa_terminate_handler(default_terminate_handler);
107 // std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
108 
109 namespace std
110 {
111 
112 unexpected_handler
set_unexpected(unexpected_handler func)113 set_unexpected(unexpected_handler func) _NOEXCEPT
114 {
115     if (func == 0)
116         func = default_unexpected_handler;
117     return __atomic_exchange_n(&__cxa_unexpected_handler, func,
118                                __ATOMIC_ACQ_REL);
119 //  Using of C++11 atomics this should be rewritten
120 //  return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
121 }
122 
123 terminate_handler
set_terminate(terminate_handler func)124 set_terminate(terminate_handler func) _NOEXCEPT
125 {
126     if (func == 0)
127         func = default_terminate_handler;
128     return __atomic_exchange_n(&__cxa_terminate_handler, func,
129                                __ATOMIC_ACQ_REL);
130 //  Using of C++11 atomics this should be rewritten
131 //  return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
132 }
133 
134 }
135