如果一个虚函数从一个虚基类继承,那么“虚thunk”是什么?

13

当我尝试访问从虚拟基类继承的派生类对象的内存布局时,出现了一些问题。


编程环境:GNU/Linux 3.19.0-32-generic,x86_64


编译器:gcc 4.8.4

//virtual base class
class Base {
public :
    virtual void f() {
        cout << "Base::f()" << endl;
    }
private:
    long x;
};

//derived class
class Derived : public virtual Base {
public:
    virtual void f() {
        cout << "Derived::f()" << endl;
    }
private:
    long y;
};

int main() {
    typedef void (*FUNC)(void);
    Derived d;

    //In my machine, sizeof(long) == sizeof(pointers). My code below is neither portable nor concise. You can just read the annotation.

    //dereference the first element of the first virtual function table(equals to *(vptr1->slot[0]))
    cout << hex << *((long*)*((long*)(&d) + 0) + 0) << endl;
    ((FUNC)*((long*)*((long*)(&d) + 0) + 0))();//invoke Derived::f()

    //dereference the first element of the second virtual function table(equals to *(vptr2->slot[0]))
    cout << hex << *((long*)*((long*)(&d) + 2) + 0) << endl;
    ((FUNC)*((long*)*((long*)(&d) + 2) + 0))();//maybe Derived::f()?

    return 0;
}

当我运行代码时,出现了"段错误(segment fault)"的提示:

400c12
Derived::f()
400c3c
segment fault

所以我对可执行文件进行了反汇编。
我在0x400c3c中找到了函数<_ZTv0_n24_N7Derived1fEv>。

0000000000400c3c <_ZTv0_n24_N7Derived1fEv>:
  400c3c:       4c 8b 17                mov    (%rdi),%r10
  400c3f:       49 03 7a e8             add    -0x18(%r10),%rdi
  400c43:       eb cd                   jmp    400c12 <_ZN7Derived1fEv>
  400c45:       90                      nop

在我的终端中解码符号:

> c++filt _ZTv0_n24_N7Derived1fEv
virtual thunk to Derived::f()

那么Derived::f()中的虚拟thunk是什么?它为什么存在?


2
你的问题是“什么是虚拟thunk”还是“为什么它会导致段错误”? - xtofl
@xtofl 前者。 - llinvokerl
3
你为什么要试图做这件事? - n. m.
1
@n.m. 标准并没有规定编译器如何实现虚拟继承。我只是想知道这个问题的答案。 - llinvokerl
2个回答

18

虚拟函数中的 thunk 是一个帮助函数,它在调用实际函数之前修复 this 参数。看这个例子:

Derived *d = new Derived();
// d now points to some address, e.g. 0x6eac40

d->f(); // This calls _ZN7Derived1fEv (Derived::f() directly)

Base *b = d;
// b now points to some other address (!), e.g. 0x6eac50

b->f(); // This calls _ZTv0_n24_N7Derived1fEv (the virtual thunk
        // of Derived::f()), which subtracts some amount from `this`
        // and then jumps to the _ZN7Derived1fEv (Derived::f())

一块在内存中的 Base 对象看起来像这样:

      * Pointer to part of Base vtable with Base's virtual functions.
          This vtable contains Base::f()

      * Data of Base class (variable `x`)

一个存在内存中的 Derived 对象看起来有点像这样:

   |> * Pointer to part of Derived vtable with Derived's virtual functions.
   |>     This vtable contains the Derived::f()
   |>
|> |> * Pointer to part of Derived vtable with the same layout as Base vtable.
|> |>     This vtable contains the thunk of Derived::f()
|> |>
|> |> * Data of Base class (variable `x`)
|  |>
|  |> * Data of Derived class (variable `y`)
|  |
|  \ This is complete Derived object.
|    The `d` pointer points at the beginning of this.
|
\ This is the part of Derived object that can act as a Base object.
  The `b` pointer points at beginning of this.

提示:现在应该清楚为什么在d指针上调用_ZTv0_n24_N7Derived1fEv会导致崩溃。只有当给出指向Derived对象内部的this指针时,该函数才起作用 - 即指向可以像Base对象一样使用的部分。


0

问题中有一些不清楚的地方。 在“virtual thunk to Derived::f()”中,我认为“add -0x18(%r10),%rdi”不能修复这个指针,因为派生对象和其子对象(基类)之间的偏移量不是24(0x18)。


哦,我知道。这里需要以间接的方式获取偏移量。“vtbl - 0x18”指向虚函数表中的偏移值。然后通过添加偏移来纠正“this”。 - Jason
请参见https://dev59.com/pJzha4cB1Zd3GeqPGYj0#40649781。 - Jason

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接