Skip to content

[libc++] Fix std::make_exception_ptr interaction with ObjC #135386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jul 8, 2025
Merged
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9f88c67
fix std::make_exception_ptr interaction with ObjC
itrofimow Apr 11, 2025
764caba
add a comment about __OBJC__ define
itrofimow Apr 14, 2025
6e108eb
remove the __OBJC__ usage in favor of checking whether the type throw…
itrofimow May 13, 2025
96c9c38
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow May 14, 2025
966c3c7
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow May 17, 2025
bf5d0fa
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow May 20, 2025
a1e8586
relax the c++-standard requirement
itrofimow May 20, 2025
20083c2
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow May 21, 2025
b27dc70
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow May 30, 2025
25bdfd9
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow Jun 4, 2025
c6c111d
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow Jun 4, 2025
ec0735c
fix the closing #else comment typo
itrofimow Jun 10, 2025
60073e3
Merge branch 'main' into objc_make_exception_ptr_fix
itrofimow Jun 23, 2025
f3e83c1
Merge branch 'main' into objc_make_exception_ptr_fix
ldionne Jul 1, 2025
985f7a6
Add test
ldionne Jul 1, 2025
e647f47
Move test to proper location
ldionne Jul 1, 2025
ca8f571
Use is_pointer instead of is_pointer_v
ldionne Jul 2, 2025
ddbbe97
Refactor the function to be easier to understand
ldionne Jul 2, 2025
3808d88
Fix broken friend
ldionne Jul 2, 2025
f587c71
Fix C++03
ldionne Jul 2, 2025
860bec2
More C++03
ldionne Jul 2, 2025
455abac
Fix issue with -fno-exceptions
ldionne Jul 2, 2025
d8d66b9
Fix forgotten conditional for back-deployment
ldionne Jul 7, 2025
211cb5c
Merge branch 'main' into objc_make_exception_ptr_fix
ldionne Jul 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions libcxx/include/__exception/exception_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <__memory/addressof.h>
#include <__memory/construct_at.h>
#include <__type_traits/decay.h>
#include <__type_traits/is_pointer.h>
#include <cstdlib>
#include <typeinfo>

Expand Down Expand Up @@ -95,37 +96,49 @@ template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
# if _LIBCPP_HAS_EXCEPTIONS
# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
using _Ep2 = __decay_t<_Ep>;

void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
// Clang treats throwing ObjC types differently, and we have to preserve original throw-ing behavior
// to not break some ObjC invariants. ObjC types are thrown by a pointer, hence the condition;
// although it does also trigger for some valid c++ usages, this should be a case rare enough to
// not complicate the condition any further
if constexpr (std::is_pointer_v<_Ep>) {
try {
throw __e;
} catch (...) {
return current_exception();
}
} else {
using _Ep2 = __decay_t<_Ep>;

void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
# ifdef __wasm__
// In Wasm, a destructor returns its argument
(void)__cxxabiv1::__cxa_init_primary_exception(
__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
// In Wasm, a destructor returns its argument
(void)__cxxabiv1::__cxa_init_primary_exception(
__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
# else
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
# endif
std::__destroy_at(static_cast<_Ep2*>(__p));
std::__destroy_at(static_cast<_Ep2*>(__p));
# ifdef __wasm__
return __p;
return __p;
# endif
});

try {
::new (__ex) _Ep2(__e);
return exception_ptr::__from_native_exception_pointer(__ex);
} catch (...) {
__cxxabiv1::__cxa_free_exception(__ex);
return current_exception();
});

try {
::new (__ex) _Ep2(__e);
return exception_ptr::__from_native_exception_pointer(__ex);
} catch (...) {
__cxxabiv1::__cxa_free_exception(__ex);
return current_exception();
}
}
# else
# else // !(_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L)
try {
throw __e;
} catch (...) {
return current_exception();
}
# endif
# else
# else // !LIBCPP_HAS_EXCEPTIONS
((void)__e);
std::abort();
# endif
Expand Down
Loading