[libcxx-commits] [libcxx] [libc++] [libc++abi] Initialize exception directly in make_exception_ptr if __cxa_init_primary_exception is available in ABI-library (PR #65534)

James Y Knight via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 19 07:58:34 PDT 2023


================
@@ -34,6 +48,44 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
     return *this;
 }
 
+#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
+{
+    #if !defined(_WIN32)
+    // We perform a runtime lookup of __cxa_init_primary_exception to
+    // preserve compability with older versions of libcxxrt/libcxxabi.
+    // If the function is not present we return nullptr because no meaningful work can be done,
+    // and the caller knows how to handle this (it fallbacks to throw + catch).
+    using CxaInitPrimaryExceptionPrototype = void *(*)(void *, type_info *, void(*)(void *));
+    static CxaInitPrimaryExceptionPrototype cxa_init_primary_exception_fn = reinterpret_cast<CxaInitPrimaryExceptionPrototype>(
+        dlsym(RTLD_DEFAULT, "__cxa_init_primary_exception"));
----------------
jyknight wrote:

It would be better to use a weak reference to the __cxa_init_primary_exception instead of dlsym.

That way, the loader takes care of the lookup, and if you are building libc++ and libc++abi together (which is the common case), there's no extra overhead.

https://github.com/llvm/llvm-project/pull/65534


More information about the libcxx-commits mailing list