1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 // This file is part of GCC.
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)
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.
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.
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/>.
25 #include <bits/c++config.h>
26 #include "eh_atomics.h"
28 // This macro causes exception_ptr to declare an older API (with corresponding
29 // definitions in this file).
30 #define _GLIBCXX_EH_PTR_COMPAT
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
40 #include <bits/exception_ptr.h>
41 #include "unwind-cxx.h"
43 using namespace __cxxabiv1
;
45 // Verify assumptions about member layout in exception types
49 constexpr std::size_t unwindhdr()
50 { return offsetof(Ex
, unwindHeader
); }
53 constexpr std::size_t termHandler()
54 { return unwindhdr
<Ex
>() - offsetof(Ex
, terminateHandler
); }
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" );
61 #ifndef __ARM_EABI_UNWINDER__
63 constexpr std::ptrdiff_t adjptr()
64 { return unwindhdr
<Ex
>() - offsetof(Ex
, adjustedPtr
); }
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" );
73 // Define non-inline functions.
75 std::__exception_ptr::exception_ptr::exception_ptr(void* obj
) noexcept
76 : _M_exception_object(obj
) { _M_addref(); }
79 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool
) noexcept
80 : _M_exception_object(nullptr) { }
84 std::__exception_ptr::exception_ptr::_M_addref() noexcept
86 if (__builtin_expect(_M_exception_object
!= nullptr, true))
88 __cxa_refcounted_exception
*eh
=
89 __get_refcounted_exception_header_from_obj (_M_exception_object
);
90 __gnu_cxx::__eh_atomic_inc (&eh
->referenceCount
);
96 std::__exception_ptr::exception_ptr::_M_release() noexcept
98 if (__builtin_expect(_M_exception_object
!= nullptr, true))
100 __cxa_refcounted_exception
*eh
=
101 __get_refcounted_exception_header_from_obj (_M_exception_object
);
102 if (__gnu_cxx::__eh_atomic_dec (&eh
->referenceCount
))
104 if (eh
->exc
.exceptionDestructor
)
105 eh
->exc
.exceptionDestructor (_M_exception_object
);
107 __cxa_free_exception (_M_exception_object
);
108 _M_exception_object
= nullptr;
115 std::__exception_ptr::exception_ptr::_M_get() const noexcept
116 { return _M_exception_object
; }
120 // Retained for compatibility with CXXABI_1.3.
122 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy() noexcept
{ }
125 // Retained for compatibility with CXXABI_1.3.
127 std::__exception_ptr::exception_ptr::operator!() const noexcept
128 { return _M_exception_object
== nullptr; }
131 // Retained for compatibility with CXXABI_1.3.
132 std::__exception_ptr::exception_ptr::operator __safe_bool() const noexcept
134 return _M_exception_object
? &exception_ptr::_M_safe_bool_dummy
: nullptr;
137 const std::type_info
*
138 std::__exception_ptr::exception_ptr::__cxa_exception_type() const noexcept
140 __cxa_exception
*eh
= __get_exception_header_from_obj (_M_exception_object
);
141 return eh
->exceptionType
;
145 std::current_exception() noexcept
147 __cxa_eh_globals
*globals
= __cxa_get_globals ();
148 __cxa_exception
*header
= globals
->caughtExceptions
;
151 return std::exception_ptr();
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();
157 return std::exception_ptr(
158 __get_object_from_ambiguous_exception (header
));
163 __gxx_dependent_exception_cleanup(_Unwind_Reason_Code code
,
164 _Unwind_Exception
*exc
)
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
);
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
);
178 __cxa_free_dependent_exception (dep
);
180 if (__gnu_cxx::__eh_atomic_dec (&header
->referenceCount
))
182 if (header
->exc
.exceptionDestructor
)
183 header
->exc
.exceptionDestructor (header
+ 1);
185 __cxa_free_exception (header
+ 1);
191 std::rethrow_exception(std::exception_ptr ep
)
193 void *obj
= ep
._M_get();
194 __cxa_refcounted_exception
*eh
195 = __get_refcounted_exception_header_from_obj (obj
);
197 __cxa_dependent_exception
*dep
= __cxa_allocate_dependent_exception ();
198 dep
->primaryException
= obj
;
199 __gnu_cxx::__eh_atomic_inc (&eh
->referenceCount
);
201 #pragma GCC diagnostic push
202 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
203 dep
->unexpectedHandler
= get_unexpected ();
204 #pragma GCC diagnostic pop
205 dep
->terminateHandler
= get_terminate ();
206 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep
->unwindHeader
.exception_class
);
207 dep
->unwindHeader
.exception_cleanup
= __gxx_dependent_exception_cleanup
;
209 __cxa_eh_globals
*globals
= __cxa_get_globals ();
210 globals
->uncaughtExceptions
+= 1;
212 #ifdef __USING_SJLJ_EXCEPTIONS__
213 _Unwind_SjLj_RaiseException (&dep
->unwindHeader
);
215 _Unwind_RaiseException (&dep
->unwindHeader
);
218 // Some sort of unwinding error. Note that terminate is a handler.
219 __cxa_begin_catch (&dep
->unwindHeader
);
223 #undef _GLIBCXX_EH_PTR_COMPAT