一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Overriding the virtual table in a C++ object | Moy Blog

 astrotycoon 2017-05-25

Before starting, I just want to mention that I recently joined http://www./ , is a nice group of Linux bloggers, if you read this blog and want to read some interesting posts ( mostly in spanish, but some guys post in english ) go there!

Having said that, let’s finish this post.

Yesterday I was discussing with a friend of mine about how polymorphism is implemented in C++, and that is, using a virtual table ( remember the “virtual” keyword in method definitions? ). A virtual table is, rawly speaking, just like an array of function pointers. Each created object with virtual methods needs a virtual table. So, where does the virtual table is stored?, I really don’t know, but I do know where I can find the address of the virtual table associated to an object ( at least in g++ 4.1.1 ), the first sizeof(void*) bytes of an object are used to store a pointer to the virtual table. With this knowledge, one could think that is possible to override the virtual table pointer of the object and call arbitrary functions, and yes, we can. Let’s see some fun code.


#include <iostream>

using namespace std;

class Parent
{
    public:
        virtual void VirtFunc1() { cout << "Parent::VirtFunc1" << endl; }

        virtual void VirtFunc2() { cout << "Parent::VirtFunc2" << endl; }
};

class Child : public Parent
{
    public:

        void VirtFunc1() { cout << "Child::VirtFunc1" << endl; }
        void VirtFunc2() { cout << "Child::VirtFunc2" << endl; }
};

typedef void (*virtual_function)();

struct FakeVirtualTable {
    virtual_function virtual_one;

    virtual_function virtual_two;
};

void fake_virtual_one()
{
    cout << "Faked virtual call 1" << endl;
}

void fake_virtual_two()
{
    cout << "Faked virtual call 2" << endl;
}

int main()
{
    /* declare a Child class and a base pointer to it. */
    Child child_class_obj;
    Parent* parent_class_ptr = &child_class_obj;

    /* create our fake virtual table with pointers to our fake methods */
    FakeVirtualTable custom_table;
    custom_table.virtual_one = fake_virtual_one;

    custom_table.virtual_two = fake_virtual_two;

    /* take the address of our stack virtual table and override the real object pointer to the virtual table */
    FakeVirtualTable* table_ptr = &custom_table;

    memcpy(parent_class_ptr, &table_ptr, sizeof(void*));

    /* call the methods ( but we're really calling the faked functions ) */

    parent_class_ptr->VirtFunc1();
    parent_class_ptr->VirtFunc2();

    return 0;
}

So, try to run that code and, of course, the expected result is having fake_virtual_one() and fake_virtual_two() functions called. No magic there, we just replace the first sizeof(void*) bytes of the object with our own table pointer. There is not a use I can think of right now, but it is funny ….

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    欧美欧美欧美欧美一区| 色丁香之五月婷婷开心| 日韩欧美国产高清在线| 国产不卡免费高清视频| 中文字幕乱码亚洲三区| 国产精品视频一级香蕉| 在线观看国产午夜福利| 中国一区二区三区不卡| 欧美一区二区不卡专区| 深夜日本福利在线观看| 国内精品偷拍视频久久| 正在播放玩弄漂亮少妇高潮| 办公室丝袜高跟秘书国产| 免费精品国产日韩热久久| 欧洲自拍偷拍一区二区| 少妇在线一区二区三区| 日本欧美一区二区三区在线播| 欧美激情床戏一区二区三| 国产麻豆视频一二三区| 国产中文另类天堂二区| 熟女乱一区二区三区四区| 中国日韩一级黄色大片| 亚洲国产成人精品一区刚刚| 麻豆看片麻豆免费视频| 亚洲一区二区三区国产| 丝袜美女诱惑在线观看| 国产成人高清精品尤物| 亚洲欧美中文日韩综合| 又大又紧又硬又湿又爽又猛| 欧美一级黄片欧美精品| 中文字幕在线五月婷婷| 国产精品丝袜美腿一区二区| 日本精品视频一二三区| 精品人妻一区二区三区四区久久 | 亚洲日本久久国产精品久久| 成人精品一区二区三区综合| 少妇被粗大进猛进出处故事| 黄片在线免费看日韩欧美| 一二区中文字幕在线观看| 在线欧洲免费无线码二区免费| 大香蕉网国产在线观看av|