[libcxx-commits] [libcxxabi] [libc++abi] Remove unnecessary dependency on std::unique_ptr (PR #73277)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 23 19:11:24 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxxabi

Author: Michael Kenzel (michael-kenzel)

<details>
<summary>Changes</summary>

The demangling terminate handler relies on a function `demangle()` to perform the demangling. This function returns an `std::unique_ptr`, relying on custom deleter and `const_cast` hacks to deal with the fact that only one branch actually allocates memory, and that the returned pointer needs to be a `const char*` in the fallback case. However, the destructor of the returned `std::unique_ptr` will never actually be called, because the sole place this function is ever called is right before the terminate handler aborts the program. So all this is unnecessary, and creates a dependency onto `<memory>`. This change removes the `demangle()` function, and replaces the call with an immediately-invoked lambda expression that simply returns a raw pointer in both cases (which should be fine because the memory can never be freed here anyways).

---
Full diff: https://github.com/llvm/llvm-project/pull/73277.diff


1 Files Affected:

- (modified) libcxxabi/src/cxa_default_handlers.cpp (+14-15) 


``````````diff
diff --git a/libcxxabi/src/cxa_default_handlers.cpp b/libcxxabi/src/cxa_default_handlers.cpp
index 7bcfca069fb7f71..e88d6c87a56e555 100644
--- a/libcxxabi/src/cxa_default_handlers.cpp
+++ b/libcxxabi/src/cxa_default_handlers.cpp
@@ -10,8 +10,6 @@
 //===----------------------------------------------------------------------===//
 
 #include <exception>
-#include <memory>
-#include <stdlib.h>
 #include "abort_message.h"
 #include "cxxabi.h"
 #include "cxa_handlers.h"
@@ -24,16 +22,6 @@
 static constinit const char* cause = "uncaught";
 
 #ifndef _LIBCXXABI_NO_EXCEPTIONS
-// Demangle the given string, or return the string as-is in case of an error.
-static std::unique_ptr<char const, void (*)(char const*)> demangle(char const* str)
-{
-#if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE)
-    if (const char* result = __cxxabiv1::__cxa_demangle(str, nullptr, nullptr, nullptr))
-        return {result, [](char const* p) { std::free(const_cast<char*>(p)); }};
-#endif
-    return {str, [](char const*) { /* nothing to free */ }};
-}
-
 __attribute__((noreturn))
 static void demangling_terminate_handler()
 {
@@ -55,13 +43,24 @@ static void demangling_terminate_handler()
     if (!__isOurExceptionClass(unwind_exception))
         abort_message("terminating due to %s foreign exception", cause);
 
+    auto name = [str = thrown_type->name()]
+    {
+    #ifndef LIBCXXABI_NON_DEMANGLING_TERMINATE
+        if (const char* result = __cxxabiv1::__cxa_demangle(str, nullptr,
+                                                            nullptr, nullptr))
+            // We're about to abort, this memory can never be freed; so it's
+            // alright to just return a raw pointer
+            return result;
+    #endif
+        return str;
+    }();
+
     void* thrown_object =
         __getExceptionClass(unwind_exception) == kOurDependentExceptionClass ?
             ((__cxa_dependent_exception*)exception_header)->primaryException :
             exception_header + 1;
     const __shim_type_info* thrown_type =
         static_cast<const __shim_type_info*>(exception_header->exceptionType);
-    auto name = demangle(thrown_type->name());
     // If the uncaught exception can be caught with std::exception&
     const __shim_type_info* catch_type =
         static_cast<const __shim_type_info*>(&typeid(std::exception));
@@ -69,12 +68,12 @@ static void demangling_terminate_handler()
     {
         // Include the what() message from the exception
         const std::exception* e = static_cast<const std::exception*>(thrown_object);
-        abort_message("terminating due to %s exception of type %s: %s", cause, name.get(), e->what());
+        abort_message("terminating due to %s exception of type %s: %s", cause, name, e->what());
     }
     else
     {
         // Else just note that we're terminating due to an exception
-        abort_message("terminating due to %s exception of type %s", cause, name.get());
+        abort_message("terminating due to %s exception of type %s", cause, name);
     }
 }
 #else // !_LIBCXXABI_NO_EXCEPTIONS

``````````

</details>


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


More information about the libcxx-commits mailing list