[libcxx-commits] [libcxxabi] [libc++abi] Remove unnecessary dependency on std::unique_ptr (PR #73277)
Michael Kenzel via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 23 19:10:59 PST 2023
https://github.com/michael-kenzel created https://github.com/llvm/llvm-project/pull/73277
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 is fine because the memory can never be freed here anyways).
>From cae0d19c5d1a38b98d999fb58ed2c5c0e563c295 Mon Sep 17 00:00:00 2001
From: Michael Kenzel <michael.kenzel at gmail.com>
Date: Wed, 22 Nov 2023 05:33:56 +0100
Subject: [PATCH] [libc++abi] Remove unnecessary dependency on std::unique_ptr
---
libcxxabi/src/cxa_default_handlers.cpp | 29 +++++++++++++-------------
1 file changed, 14 insertions(+), 15 deletions(-)
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
More information about the libcxx-commits
mailing list