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

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

本文关键字:自己 返回 内存 不能 cxa demangle abi      更新时间:2023-10-16

我尝试使用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_demangle返回status "-3:其中一个参数无效。"每隔一秒

在第一次调用时(对于A),智能指针包含nullptr, abi::__cxa_demangle返回零status "0:要求操作成功"。但是文档说:

output_buffer:使用malloc分配的内存区域,长度为*字节,其中存储了被请求的名称。如果output_buffer不够长,则使用realloc对其进行扩展。output_buffer可以改为NULL;在这种情况下,被请求的名称被放置在由malloc分配的内存区域中。

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

你误解了文档:

output_buffer:用malloc分配的*字节长度的内存区域

你正在传递一个由malloc分配的内存区域,但是length是空的,所以*length没有定义。

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

在GCC中的实现(实际上在自由支持库中)做:

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

所以如果你传递一个非空的output_buffer指针,你也必须传递一个非空的length指针。

因为你不知道分配的块有多大,所以你能做的最好的事情就是使用strlen(demangled_name.get())+1来找到确定分配的最小长度

最好是保留一个size_t全局变量来存储之前的大小,并将其传递进去。你应该把这些都包装在一个类中,而不仅仅是一堆全局变量。

在gcc下使用此代码。它是可靠的。

#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());
}