-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_func_address.cpp
More file actions
63 lines (55 loc) · 1.6 KB
/
Copy pathtest_func_address.cpp
File metadata and controls
63 lines (55 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdint>
class Base {
public:
virtual ~Base() {
};
virtual void A() {
}
bool isFuncAOverride() const {
class Base1 : public Base {
//继承自Base但不重载
};
class Base2 : public Base {
//继承自Base且进行重载
public:
virtual void A() {
}
};
static int funcAIndex = -1;
static intptr_t funcAAddr = 0;
if (funcAAddr == 0) {
Base a;
Base1 b;
Base2 c;
intptr_t *v_table_a = (intptr_t *) *(intptr_t *) &a;
intptr_t *v_table_b = (intptr_t *) *(intptr_t *) &b;
intptr_t *v_table_c = (intptr_t *) *(intptr_t *) &c;
for (int i = 0; ; ++i) {
if (v_table_a[i] == v_table_b[i]) {
//b是a的派生类,且未进行任何重写,因此这两个类的虚函数表中只有析构函数地址是不一样的,通过这种方式来排除派生类的析构函数
if (v_table_a[i] != v_table_c[i]) {
funcAIndex = i;
funcAAddr = v_table_a[i];
break;
}
}
}
}
intptr_t *v_table_this = (intptr_t *) *(intptr_t *) this;
return v_table_this[funcAIndex] != funcAAddr;
}
};
class B : public Base {
public:
virtual void A() {
}
};
class C : public Base {
};
#include <iostream>
int main() {
B b;
C c;
std::cout << b.isFuncAOverride() << "," << c.isFuncAOverride() << std::endl;
return 0;
}