abi::__cxa_demangle 不能重用自己返回的内存

abi::__cxa_demangle can't reuse memory returned by itself

我尝试使用 abi::__cxa_demangle 来分解用户定义的类型:

#include <iostream>
#include <mutex>
#include <memory>
#include <string>
#include <typeinfo>

#include <cassert>
#include <cstdlib>

#include <cxxabi.h>

namespace
{

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
std::mutex m;
std::unique_ptr< char, decltype(std::free) & > demangled_name{nullptr, std::free};
#pragma clang diagnostic pop

}

inline
std::string
get_demangled_name(char const * const symbol) noexcept
{
    if (!symbol) {
        return "<null>";
    }
    std::lock_guard< std::mutex > lock(m);
    int status = -4;
    demangled_name.reset(abi::__cxa_demangle(symbol, demangled_name.release(), nullptr, &status));
    return ((status == 0) ? demangled_name.get() : symbol);
}

template< typename ...types >
void
f(std::size_t const i)
{
    using F = void (*)();
    assert(i < sizeof...(types));
    static F const a[sizeof...(types)] = {static_cast< F >([] () { std::cout << get_demangled_name(typeid(types).name()) << std::endl; })...};
    return a[i]();
};

struct A {};
struct B {};
struct X {};
struct Y {};
struct Z {};

int
main()
{
    for (std::size_t i = 0; i < 5; ++i) {
       f< A, B, X, Y, Z >(i);
    }
    return EXIT_SUCCESS;
}

但是abi::__cxa_demanglereturnsstatus"-3: One of the arguments is invalid."每隔一秒。

在第一次调用时(对于 A)智能指针包含 nullptrabi::__cxa_demangle returns 零 status "0: The demangling operation succeeded."。但是文档说:

output_buffer: A region of memory, allocated with malloc, of *length bytes, into which the demangled name is stored. If output_buffer is not long enough, it is expanded using realloc. output_buffer may instead be NULL; in that case, the demangled name is placed in a region of memory allocated with malloc.

因此,我得出结论,该函数不能重用由其自身一致分配的内存。是bug还是我对文档的误解?

要在 gcc 下进行 demangle,请使用此代码。靠谱

#include <cxxabi.h>
#include <memory>
#include <string>
#include <cassert>

template <typename T>
std::string demangle(T& e)
{
    int status;
    std::unique_ptr<char> realname;
    const std::type_info  &ti = typeid(e);
    realname.reset(abi::__cxa_demangle(ti.name(), 0, 0, &status));
    assert(status == 0);
    return std::string(realname.get());
}

您误解了文档:

output_buffer: A region of memory, allocated with malloc, of *length bytes

您正在传递分配有 malloc 的内存区域,但 length 为空,因此 *length 未定义。

为了知道是否可以重新使用内存需要知道块有多大,所以需要传入长度作为第三个参数。

GCC 中的实现(实际上是在 libiberty support library 中):

  if (output_buffer != NULL && length == NULL)
    {
      if (status != NULL)
        *status = -3;
      return NULL;
    }

因此,如果您传递一个非空 output_buffer 指针,您还必须传递一个非空 length 指针。

由于您不知道分配的块有多大,因此您可以使用当前代码做的最好的事情是使用 strlen(demangled_name.get())+1 找到确定分配的最小长度。

更好的做法是保留一个 size_t 全局变量来存储之前的大小,然后将其传入。您可能应该将其全部包装在一个 class 中,而不仅仅是一堆全局变量。