gpt4 book ai didi

c++ - 在 MSVC ABI 中,如何可靠地找到仅给出 (void*) 的 vtable?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:21:18 29 4
gpt4 key购买 nike

这个问题专门针对不可移植的 MSVC ABI 内容。

我正在尝试用显然不可移植但不神奇的 C++ 编写与 C++ 的 typeid 等效的代码。对于 Itanium ABI(在 Linux/Mac 上使用),它非常简单:

const std::type_info& dynamicast_typeid(void *mdo)
{
std::type_info **vptr = *reinterpret_cast<std::type_info ***>(mdo);
std::type_info *typeinfo_ptr = vptr[-1];
return *typeinfo_ptr;
}

所以现在我正在查看 64 位 MSVC ABI,该死的,我被难住了。对于非常简单的类,即以偏移量 0 处的 vfptr 开头的类,它几乎与 Itanium 一样简单:

const std::type_info& dynamicast_typeid(void *mdo)
{
int *rtti_complete_object_locator = ((int ***)mdo)[0][-1];
char *result = (char *) rtti_complete_object_locator;
result -= rtti_complete_object_locator[5];
result += rtti_complete_object_locator[3];
return *(std::type_info*)result;
}

(此代码基于 the Wine project's __RTtypeid 。)

问题是某些 C++ 类以偏移量 0 处的 vfptr 开头!有时它们以 vbptr 开头。

struct Class1 { virtual ~Class1() {} };
struct Class2 : virtual Class1 {};

Class1 以 vfptr 开头; Class2 以 vbptr 开头。

当一个类以 vbptr 开始时,我相信它的第一个虚拟基础子对象(它在布局顺序中的第一个,因此它的“最叶”)总是有一个 vfptr 在它的 偏移量为 0。因此,如果我知道我正在处理以 vbptr 开头的类,我想改为这样做:

const std::type_info& dynamicast_typeid_for_vbptr_class(void *mdo)
{
int first_vbase_offset = ((int**)mdo)[0][1];
mdo = (char*)mdo + first_vbase_offset;
int *rtti_complete_object_locator = ((int ***)mdo)[0][-1];
char *result = (char *) rtti_complete_object_locator;
result -= rtti_complete_object_locator[5];
result += rtti_complete_object_locator[3];
return *(std::type_info*)result;
}

我已经确定 MSVC does actually generate相当于

    if constexpr(IS_VBPTR_CLASS) {
int first_vbase_offset = ((int**)mdo)[0][1];
mdo = (char*)mdo + first_vbase_offset;
}
return __RTtypeid(mdo);

当编译 C++ 表达式 typeid(x) 时——其中 IS_VBPTR_CLASS 是伪代码,“编译器神奇地知道 x 是否有一个 vbptr ,基于 x 的静态类型以及编译器知道每种类型的布局这一事实。”

但是,在我的例子中,我不知道 x 的静态类型,即使我知道,我也不知道如何找出(从在 C++ 中,使用模板元编程)x 是否以 vbptr 开头。

最后,我继续用

const std::type_info& dynamicast_typeid(void *mdo)
{
while (((int**)mdo)[0][0] == 0) {
mdo = (char *)mdo + ((int**)mdo)[0][1];
}
int *rtti_complete_object_locator = ((int ***)mdo)[0][-1];
char *result = (char *)complete_object_locator;
result -= rtti_complete_object_locator[5];
result += rtti_complete_object_locator[3];
return *(std::type_info*)result;
}

只是发现存储在“Class1 in Class2”的 vftable 中的类型信息包含子对象类型 Class1 的类型信息,而不是最派生类型 Class2!所以还缺少一 block 拼图。

所以我的问题简而言之:给定 (void*)&object_of_type_class2,我如何检索 typeid(Class2)

最佳答案

我现在有一些东西可以用了!

函数dynamicast_to_mdo相当于dynamic_cast<void*>(p) — 它调整 p指向其最派生的对象。

函数dynamicast_typeid相当于typeid(p) — 它从 p 获取类型信息的虚表。我只是在使用我在问题中给出的软糖/技巧,实际上我不确定为什么几个小时前我得到了错误的答案;我认为当我看到错误的类型信息时,可能是因为我不小心尝试使用 typeid。对已销毁堆栈变量的悬空引用。

// 64-bit MSVC ABI
void *dynamicast_to_mdo(void *p)
{
if (((int**)p)[0][0] == 0) {
p = (char *)p + ((int**)p)[0][1];
}
int *complete_object_locator = ((int ***)p)[0][-1];
int mdoffset = complete_object_locator[1];
void *adjusted_this = static_cast<char *>(p) - mdoffset;
return adjusted_this;
}

// 64-bit MSVC ABI
const std::type_info& dynamicast_typeid(void *p)
{
if (((int**)p)[0][0] == 0) {
p = (char *)p + ((int**)p)[0][1];
}
int *complete_object_locator = ((int ***)p)[0][-1];

char *result = (char *)complete_object_locator;
result -= complete_object_locator[5];
result += complete_object_locator[3];
return *(const std::type_info*)result;
}

关于c++ - 在 MSVC ABI 中,如何可靠地找到仅给出 (void*) 的 vtable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44618230/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com