1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008-2021 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 #if ! _GLIBCXX_INLINE_VERSION
29 // This macro causes exception_ptr to declare an older API (with corresponding
30 // definitions in this file) and to mark some inline functions as "used" so
31 // that definitions will be emitted in this translation unit.
32 #define _GLIBCXX_EH_PTR_COMPAT
36 #include <bits/exception_ptr.h>
37 #include "unwind-cxx.h"
39 using namespace __cxxabiv1
;
41 // Verify assumptions about member layout in exception types
45 constexpr std::size_t unwindhdr()
46 { return offsetof(Ex
, unwindHeader
); }
49 constexpr std::size_t termHandler()
50 { return unwindhdr
<Ex
>() - offsetof(Ex
, terminateHandler
); }
52 static_assert( termHandler
<__cxa_exception
>()
53 == termHandler
<__cxa_dependent_exception
>(),
54 "__cxa_dependent_exception::termHandler layout must be"
55 " consistent with __cxa_exception::termHandler" );
57 #ifndef __ARM_EABI_UNWINDER__
59 constexpr std::ptrdiff_t adjptr()
60 { return unwindhdr
<Ex
>() - offsetof(Ex
, adjustedPtr
); }
62 static_assert( adjptr
<__cxa_exception
>()
63 == adjptr
<__cxa_dependent_exception
>(),
64 "__cxa_dependent_exception::adjustedPtr layout must be"
65 " consistent with __cxa_exception::adjustedPtr" );
69 // Define non-inline functions.
71 std::__exception_ptr::exception_ptr::exception_ptr(void* obj
) noexcept
72 : _M_exception_object(obj
) { _M_addref(); }
75 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool
) noexcept
76 : _M_exception_object(nullptr) { }
80 std::__exception_ptr::exception_ptr::_M_addref() noexcept
82 if (__builtin_expect(_M_exception_object
!= nullptr, true))
84 __cxa_refcounted_exception
*eh
=
85 __get_refcounted_exception_header_from_obj (_M_exception_object
);
86 __gnu_cxx::__eh_atomic_inc (&eh
->referenceCount
);
92 std::__exception_ptr::exception_ptr::_M_release() noexcept
94 if (__builtin_expect(_M_exception_object
!= nullptr, true))
96 __cxa_refcounted_exception
*eh
=
97 __get_refcounted_exception_header_from_obj (_M_exception_object
);
98 if (__gnu_cxx::__eh_atomic_dec (&eh
->referenceCount
))
100 if (eh
->exc
.exceptionDestructor
)
101 eh
->exc
.exceptionDestructor (_M_exception_object
);
103 __cxa_free_exception (_M_exception_object
);
104 _M_exception_object
= nullptr;
111 std::__exception_ptr::exception_ptr::_M_get() const noexcept
112 { return _M_exception_object
; }
116 // Retained for compatibility with CXXABI_1.3.
118 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy() noexcept
{ }
121 // Retained for compatibility with CXXABI_1.3.
123 std::__exception_ptr::exception_ptr::operator!() const noexcept
124 { return _M_exception_object
== nullptr; }
127 // Retained for compatibility with CXXABI_1.3.
128 std::__exception_ptr::exception_ptr::operator __safe_bool() const noexcept
130 return _M_exception_object
? &exception_ptr::_M_safe_bool_dummy
: nullptr;
133 const std::type_info
*
134 std::__exception_ptr::exception_ptr::__cxa_exception_type() const noexcept
136 __cxa_exception
*eh
= __get_exception_header_from_obj (_M_exception_object
);
137 return eh
->exceptionType
;
141 std::current_exception() noexcept
143 __cxa_eh_globals
*globals
= __cxa_get_globals ();
144 __cxa_exception
*header
= globals
->caughtExceptions
;
147 return std::exception_ptr();
149 // Since foreign exceptions can't be counted, we can't return them.
150 if (!__is_gxx_exception_class (header
->unwindHeader
.exception_class
))
151 return std::exception_ptr();
153 return std::exception_ptr(
154 __get_object_from_ambiguous_exception (header
));
159 __gxx_dependent_exception_cleanup(_Unwind_Reason_Code code
,
160 _Unwind_Exception
*exc
)
162 // This cleanup is set only for dependents.
163 __cxa_dependent_exception
*dep
= __get_dependent_exception_from_ue (exc
);
164 __cxa_refcounted_exception
*header
=
165 __get_refcounted_exception_header_from_obj (dep
->primaryException
);
167 // We only want to be called through _Unwind_DeleteException.
168 // _Unwind_DeleteException in the HP-UX IA64 libunwind library
169 // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
170 // like the GCC _Unwind_DeleteException function does.
171 if (code
!= _URC_FOREIGN_EXCEPTION_CAUGHT
&& code
!= _URC_NO_REASON
)
172 __terminate (header
->exc
.terminateHandler
);
174 __cxa_free_dependent_exception (dep
);
176 if (__gnu_cxx::__eh_atomic_dec (&header
->referenceCount
))
178 if (header
->exc
.exceptionDestructor
)
179 header
->exc
.exceptionDestructor (header
+ 1);
181 __cxa_free_exception (header
+ 1);
187 std::rethrow_exception(std::exception_ptr ep
)
189 void *obj
= ep
._M_get();
190 __cxa_refcounted_exception
*eh
191 = __get_refcounted_exception_header_from_obj (obj
);
193 __cxa_dependent_exception
*dep
= __cxa_allocate_dependent_exception ();
194 dep
->primaryException
= obj
;
195 __gnu_cxx::__eh_atomic_inc (&eh
->referenceCount
);
197 dep
->unexpectedHandler
= get_unexpected ();
198 dep
->terminateHandler
= get_terminate ();
199 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep
->unwindHeader
.exception_class
);
200 dep
->unwindHeader
.exception_cleanup
= __gxx_dependent_exception_cleanup
;
202 __cxa_eh_globals
*globals
= __cxa_get_globals ();
203 globals
->uncaughtExceptions
+= 1;
205 #ifdef __USING_SJLJ_EXCEPTIONS__
206 _Unwind_SjLj_RaiseException (&dep
->unwindHeader
);
208 _Unwind_RaiseException (&dep
->unwindHeader
);
211 // Some sort of unwinding error. Note that terminate is a handler.
212 __cxa_begin_catch (&dep
->unwindHeader
);
216 #undef _GLIBCXX_EH_PTR_COMPAT