Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit a0cfaa7

Browse files
committed
Hard simplification.
1 parent fe854db commit a0cfaa7

File tree

4 files changed

+66
-1719
lines changed

4 files changed

+66
-1719
lines changed

ClassFromRTTI/ClassFromRTTI.cpp

Lines changed: 11 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -21,193 +21,27 @@ typedef bool(__fastcall* fnIsTrue)(void* ecx, void* edx);
2121
typedef const char*(__fastcall* fnHelloWorld)(void* ecx, void* edx);
2222

2323
int main() {
24-
RTTI cRTTI1; // Default
25-
RTTI cRTTI2(true, true, true, true, false); // With caching
26-
RTTI cRTTI3(false, false, false, true, true); // With min iterations
27-
RTTI cRTTI4(true, true, true, true, true); // With cache and min iterations
28-
2924
HMODULE hTestDLL = LoadLibrary(TEXT("TestDLL.dll"));
3025
if (!hTestDLL) {
3126
printf("Error: TestDLL.dll not found.\n");
3227
return -1;
3328
}
3429

35-
printf("For start press <Enter>\n");
36-
_CRT_UNUSED(getchar());
37-
38-
uintptr_t unTestingRVA = cRTTI2.GetVTableOffsetFromModule(hTestDLL, "TestingDLL");
39-
printf("TestingDLL (RVA) = 0x%IX\n", unTestingRVA);
40-
41-
#ifdef BENCHMARK_TEST
42-
high_resolution_clock::time_point t1;
43-
high_resolution_clock::time_point t2;
44-
45-
// Stage 1
46-
printf("# Stage 1\n");
47-
t1 = high_resolution_clock::now();
48-
for (unsigned char i = 0; i < 100; ++i) {
49-
if (unTestingRVA != cRTTI1.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
50-
printf("Error: Data error!\n");
51-
}
52-
}
53-
t2 = high_resolution_clock::now();
54-
printf("Bench (100 calls)...............................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
55-
56-
t1 = high_resolution_clock::now();
57-
for (unsigned short i = 0; i < 1'000; ++i) {
58-
if (unTestingRVA != cRTTI1.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
59-
printf("Error: Data error!\n");
60-
}
61-
}
62-
t2 = high_resolution_clock::now();
63-
printf("Bench (1 000 calls).............................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
64-
65-
t1 = high_resolution_clock::now();
66-
for (unsigned int i = 0; i < 10'000; ++i) {
67-
if (unTestingRVA != cRTTI1.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
68-
printf("Error: Data error!\n");
69-
}
70-
}
71-
t2 = high_resolution_clock::now();
72-
printf("Bench (10 000 calls)............................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
73-
74-
t1 = high_resolution_clock::now();
75-
for (unsigned int i = 0; i < 50'000; i++) {
76-
if (unTestingRVA != cRTTI1.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
77-
printf("Error: Data error!\n");
78-
}
79-
}
80-
t2 = high_resolution_clock::now();
81-
printf("Bench (50 000 calls)............................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
82-
83-
// Stage 2
84-
printf("# Stage 2\n");
85-
t1 = high_resolution_clock::now();
86-
for (unsigned char i = 0; i < 100; ++i) {
87-
if (unTestingRVA != cRTTI2.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
88-
printf("Error: Data error!\n");
89-
}
90-
}
91-
t2 = high_resolution_clock::now();
92-
printf("Bench (100 calls + Cache).......................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
93-
94-
t1 = high_resolution_clock::now();
95-
for (unsigned short i = 0; i < 1'000; ++i) {
96-
if (unTestingRVA != cRTTI2.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
97-
printf("Error: Data error!\n");
98-
}
99-
}
100-
t2 = high_resolution_clock::now();
101-
printf("Bench (1 000 calls + Cache).....................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
102-
103-
t1 = high_resolution_clock::now();
104-
for (unsigned int i = 0; i < 10'000; ++i) {
105-
if (unTestingRVA != cRTTI2.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
106-
printf("Error: Data error!\n");
107-
}
108-
}
109-
t2 = high_resolution_clock::now();
110-
printf("Bench (10 000 calls + Cache)....................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
111-
112-
t1 = high_resolution_clock::now();
113-
for (unsigned int i = 0; i < 50'000; i++) {
114-
if (unTestingRVA != cRTTI2.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
115-
printf("Error: Data error!\n");
116-
}
117-
}
118-
t2 = high_resolution_clock::now();
119-
printf("Bench (50 000 calls + Cache)....................= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
120-
121-
// Stage 3
122-
printf("# Stage 3\n");
123-
t1 = high_resolution_clock::now();
124-
for (unsigned char i = 0; i < 100; ++i) {
125-
if (unTestingRVA != cRTTI3.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
126-
printf("Error: Data error!\n");
127-
}
128-
}
129-
t2 = high_resolution_clock::now();
130-
printf("Bench (100 calls + MinIterations)...............= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
131-
132-
t1 = high_resolution_clock::now();
133-
for (unsigned short i = 0; i < 1'000; ++i) {
134-
if (unTestingRVA != cRTTI3.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
135-
printf("Error: Data error!\n");
136-
}
137-
}
138-
t2 = high_resolution_clock::now();
139-
printf("Bench (1 000 calls + MinIterations).............= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
140-
141-
t1 = high_resolution_clock::now();
142-
for (unsigned int i = 0; i < 10'000; ++i) {
143-
if (unTestingRVA != cRTTI3.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
144-
printf("Error: Data error!\n");
145-
}
146-
}
147-
t2 = high_resolution_clock::now();
148-
printf("Bench (10 000 calls + MinIterations)............= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
149-
150-
t1 = high_resolution_clock::now();
151-
for (unsigned int i = 0; i < 50'000; i++) {
152-
if (unTestingRVA != cRTTI3.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
153-
printf("Error: Data error!\n");
154-
}
155-
}
156-
t2 = high_resolution_clock::now();
157-
printf("Bench (50 000 calls + MinIterations)............= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
158-
159-
// Stage 4
160-
printf("# Stage 4\n");
161-
t1 = high_resolution_clock::now();
162-
for (unsigned char i = 0; i < 100; ++i) {
163-
if (unTestingRVA != cRTTI4.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
164-
printf("Error: Data error!\n");
165-
}
166-
}
167-
t2 = high_resolution_clock::now();
168-
printf("Bench (100 calls + Cache + MinIterations).......= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
169-
170-
t1 = high_resolution_clock::now();
171-
for (unsigned short i = 0; i < 1'000; ++i) {
172-
if (unTestingRVA != cRTTI4.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
173-
printf("Error: Data error!\n");
174-
}
175-
}
176-
t2 = high_resolution_clock::now();
177-
printf("Bench (1 000 calls + Cache + MinIterations).....= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
178-
179-
t1 = high_resolution_clock::now();
180-
for (unsigned int i = 0; i < 10'000; ++i) {
181-
if (unTestingRVA != cRTTI4.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
182-
printf("Error: Data error!\n");
183-
}
184-
}
185-
t2 = high_resolution_clock::now();
186-
printf("Bench (10 000 calls + Cache + MinIterations)....= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
187-
188-
t1 = high_resolution_clock::now();
189-
for (unsigned int i = 0; i < 50'000; i++) {
190-
if (unTestingRVA != cRTTI4.GetVTableOffsetFromModule(hTestDLL, "TestingDLL")) {
191-
printf("Error: Data error!\n");
192-
}
193-
}
194-
t2 = high_resolution_clock::now();
195-
printf("Bench (50 000 calls + Cache + MinIterations)....= %lld ms\n", duration_cast<std::chrono::milliseconds>(t2 - t1).count());
196-
#endif // BENCHMARK_TEST
197-
198-
if (!unTestingRVA) {
199-
printf("Error: TestingDLL RVA VTable not found.\n");
30+
void* pTesting = FindRTTI(hTestDLL, ".?AVTestingDLL@@"); // ".?AV<NAME>@@"
31+
if (!pTesting) {
32+
printf("Error: TestingDLL VTable not found.\n");
20033
return -1;
20134
}
20235

203-
void** pTestingDLL_VT = reinterpret_cast<void**>(reinterpret_cast<char*>(hTestDLL) + unTestingRVA);
204-
205-
fnIsTrue IsTrue = reinterpret_cast<fnIsTrue>(pTestingDLL_VT[0]);
206-
fnHelloWorld HelloWorld = reinterpret_cast<fnHelloWorld>(pTestingDLL_VT[1]);
36+
void** pTestingVTable = reinterpret_cast<void**>(pTesting);
37+
fnIsTrue IsTrue = reinterpret_cast<fnIsTrue>(pTestingVTable[0]);
38+
fnHelloWorld HelloWorld = reinterpret_cast<fnHelloWorld>(pTestingVTable[1]);
20739

208-
printf("# Result:\n");
209-
printf("IsTrue = %s\n", IsTrue(pTestingDLL_VT, nullptr) ? "true" : "false");
210-
printf("HelloWorld = %s\n", HelloWorld(pTestingDLL_VT, nullptr));
40+
printf("RESULT:\n");
41+
printf(" > hTestDLL (0x%IX)\n", reinterpret_cast<uintptr_t>(hTestDLL));
42+
printf(" > pTestingVTable (0x%IX)\n", reinterpret_cast<uintptr_t>(pTestingVTable));
43+
printf(" > IsTrue (0x%IX) = %s\n", reinterpret_cast<uintptr_t>(&(pTestingVTable[0])), IsTrue(pTestingVTable, nullptr) ? "true" : "false");
44+
printf(" > HelloWorld (0x%IX) = %s\n", reinterpret_cast<uintptr_t>(&(pTestingVTable[1])), HelloWorld(pTestingVTable, nullptr));
21145

21246
return 0;
21347
}

0 commit comments

Comments
 (0)