1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // GCC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 #include <bits/c++config.h>
26 #include "eh_atomics.h"
27 
28 // This macro causes exception_ptr to declare an older API (with corresponding
29 // definitions in this file).
30 #define _GLIBCXX_EH_PTR_COMPAT
31 
32 #if ! _GLIBCXX_INLINE_VERSION
33 // This macro causes some inline functions in exception_ptr to be marked
34 // as "used" so that definitions will be emitted in this translation unit.
35 // We need this because those functions were not inline in previous releases.
36 #define _GLIBCXX_EH_PTR_RELOPS_COMPAT
37 #endif
38 
39 #include <exception>
40 #include <bits/exception_ptr.h>
41 #include "unwind-cxx.h"
42 
43 using namespace __cxxabiv1;
44 
45 // Verify assumptions about member layout in exception types
46 namespace
47 {
48 template<typename Ex>
unwindhdr()49   constexpr std::size_t unwindhdr()
50   { return offsetof(Ex, unwindHeader); }
51 
52 template<typename Ex>
termHandler()53   constexpr std::size_t termHandler()
54   { return unwindhdr<Ex>() - offsetof(Ex, terminateHandler); }
55 
56 static_assert( termHandler<__cxa_exception>()
57 	       == termHandler<__cxa_dependent_exception>(),
58 	       "__cxa_dependent_exception::termHandler layout must be"
59 	       " consistent with __cxa_exception::termHandler" );
60 
61 #ifndef __ARM_EABI_UNWINDER__
62 template<typename Ex>
adjptr()63   constexpr std::ptrdiff_t adjptr()
64   { return unwindhdr<Ex>() - offsetof(Ex, adjustedPtr); }
65 
66 static_assert( adjptr<__cxa_exception>()
67 	       == adjptr<__cxa_dependent_exception>(),
68 	       "__cxa_dependent_exception::adjustedPtr layout must be"
69 	       " consistent with __cxa_exception::adjustedPtr" );
70 #endif
71 }
72 
73 // Define non-inline functions.
74 
exception_ptr(void * obj)75 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) noexcept
76 : _M_exception_object(obj)  { _M_addref(); }
77 
78 
exception_ptr(__safe_bool)79 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) noexcept
80 : _M_exception_object(nullptr) { }
81 
82 
83 void
_M_addref()84 std::__exception_ptr::exception_ptr::_M_addref() noexcept
85 {
86   if (__builtin_expect(_M_exception_object != nullptr, true))
87     {
88       __cxa_refcounted_exception *eh =
89 	__get_refcounted_exception_header_from_obj (_M_exception_object);
90       __gnu_cxx::__eh_atomic_inc (&eh->referenceCount);
91     }
92 }
93 
94 
95 void
_M_release()96 std::__exception_ptr::exception_ptr::_M_release() noexcept
97 {
98   if (__builtin_expect(_M_exception_object != nullptr, true))
99     {
100       __cxa_refcounted_exception *eh =
101 	__get_refcounted_exception_header_from_obj (_M_exception_object);
102       if (__gnu_cxx::__eh_atomic_dec (&eh->referenceCount))
103         {
104 	  if (eh->exc.exceptionDestructor)
105 	    eh->exc.exceptionDestructor (_M_exception_object);
106 
107           __cxa_free_exception (_M_exception_object);
108           _M_exception_object = nullptr;
109         }
110     }
111 }
112 
113 
114 void*
_M_get() const115 std::__exception_ptr::exception_ptr::_M_get() const noexcept
116 { return _M_exception_object; }
117 
118 
119 
120 // Retained for compatibility with CXXABI_1.3.
121 void
_M_safe_bool_dummy()122 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy() noexcept { }
123 
124 
125 // Retained for compatibility with CXXABI_1.3.
126 bool
operator !() const127 std::__exception_ptr::exception_ptr::operator!() const noexcept
128 { return _M_exception_object == nullptr; }
129 
130 
131 // Retained for compatibility with CXXABI_1.3.
operator __safe_bool() const132 std::__exception_ptr::exception_ptr::operator __safe_bool() const noexcept
133 {
134   return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : nullptr;
135 }
136 
137 const std::type_info*
__cxa_exception_type() const138 std::__exception_ptr::exception_ptr::__cxa_exception_type() const noexcept
139 {
140   __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
141   return eh->exceptionType;
142 }
143 
144 std::exception_ptr
current_exception()145 std::current_exception() noexcept
146 {
147   __cxa_eh_globals *globals = __cxa_get_globals ();
148   __cxa_exception *header = globals->caughtExceptions;
149 
150   if (!header)
151     return std::exception_ptr();
152 
153   // Since foreign exceptions can't be counted, we can't return them.
154   if (!__is_gxx_exception_class (header->unwindHeader.exception_class))
155     return std::exception_ptr();
156 
157   return std::exception_ptr(
158     __get_object_from_ambiguous_exception (header));
159 }
160 
161 
162 static void
__gxx_dependent_exception_cleanup(_Unwind_Reason_Code code,_Unwind_Exception * exc)163 __gxx_dependent_exception_cleanup(_Unwind_Reason_Code code,
164 				  _Unwind_Exception *exc)
165 {
166   // This cleanup is set only for dependents.
167   __cxa_dependent_exception *dep = __get_dependent_exception_from_ue (exc);
168   __cxa_refcounted_exception *header =
169     __get_refcounted_exception_header_from_obj (dep->primaryException);
170 
171   // We only want to be called through _Unwind_DeleteException.
172   // _Unwind_DeleteException in the HP-UX IA64 libunwind library
173   // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
174   // like the GCC _Unwind_DeleteException function does.
175   if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
176     __terminate (header->exc.terminateHandler);
177 
178   __cxa_free_dependent_exception (dep);
179 
180   if (__gnu_cxx::__eh_atomic_dec (&header->referenceCount))
181     {
182       if (header->exc.exceptionDestructor)
183 	header->exc.exceptionDestructor (header + 1);
184 
185       __cxa_free_exception (header + 1);
186     }
187 }
188 
189 
190 void
rethrow_exception(std::exception_ptr ep)191 std::rethrow_exception(std::exception_ptr ep)
192 {
193   void *obj = ep._M_get();
194   __cxa_refcounted_exception *eh
195     = __get_refcounted_exception_header_from_obj (obj);
196 
197   __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
198   dep->primaryException = obj;
199   __gnu_cxx::__eh_atomic_inc (&eh->referenceCount);
200 
201   dep->unexpectedHandler = get_unexpected ();
202   dep->terminateHandler = get_terminate ();
203   __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep->unwindHeader.exception_class);
204   dep->unwindHeader.exception_cleanup = __gxx_dependent_exception_cleanup;
205 
206   __cxa_eh_globals *globals = __cxa_get_globals ();
207   globals->uncaughtExceptions += 1;
208 
209 #ifdef __USING_SJLJ_EXCEPTIONS__
210   _Unwind_SjLj_RaiseException (&dep->unwindHeader);
211 #else
212   _Unwind_RaiseException (&dep->unwindHeader);
213 #endif
214 
215   // Some sort of unwinding error.  Note that terminate is a handler.
216   __cxa_begin_catch (&dep->unwindHeader);
217   std::terminate();
218 }
219 
220 #undef _GLIBCXX_EH_PTR_COMPAT
221