-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtti.cpp
More file actions
373 lines (312 loc) · 7.89 KB
/
rtti.cpp
File metadata and controls
373 lines (312 loc) · 7.89 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "rtti.h"
#ifdef WIN32
struct _s_RTTIBaseClassDescriptor;
typedef _s_RTTIBaseClassDescriptor ClassDescriptor;
#else
class __class_type_info;
typedef __class_type_info ClassDescriptor;
#endif
class CBaseType : public IBaseType
{
public:
CBaseType(const ClassDescriptor *descriptor, size_t offset);
~CBaseType()
{
for (size_t i=0; i<m_BaseTypes; i++)
{
m_Bases[i].~CBaseType();
}
free(m_Bases);
}
ptrdiff_t GetOffset()
{
return m_Offset;
}
const std::type_info &GetTypeInfo()
{
return *m_TypeInfo;
}
size_t GetNumBaseClasses()
{
return m_BaseTypes;
}
IBaseType *GetBaseClass(size_t num)
{
assert(num<GetNumBaseClasses());
return &m_Bases[num];
}
private:
ptrdiff_t m_Offset;
size_t m_BaseTypes;
CBaseType *m_Bases;
const std::type_info *m_TypeInfo;
};
class ClassTypeWrapper : public IType
{
public:
ClassTypeWrapper(const void *ptr);
~ClassTypeWrapper()
{
delete m_Type;
}
IBaseType *GetBaseType()
{
return m_Type;
}
void Destroy()
{
delete this;
}
private:
CBaseType *m_Type;
};
IType *GetType(const void *ptr)
{
return new ClassTypeWrapper(ptr);
}
/* OS Specific Implementations */
const char *GetTypeName(const std::type_info &type)
{
const char *temp = type.name();
#ifdef WIN32
/* WIN32 uses 'class <classname>' */
while (*temp != ' ')
{
temp++;
}
return ++temp;
#else
/* GCC uses '<length><classname>' */
while (isdigit(*temp))
{
temp++;
}
return temp;
#endif
}
#ifdef WIN32
#include <windows.h>
#include <new>
#include <assert.h>
#include <extension.h>
using namespace std;
using std::type_info;
typedef type_info TypeDescriptor;
struct PMD
{
ptrdiff_t mdisp; //vftable offset
ptrdiff_t pdisp; //vbtable offset
ptrdiff_t vdisp; //vftable offset (for virtual base class)
};
struct _s_RTTICompleteObjectLocator;
struct _s_RTTIClassHierarchyDescriptor;
struct _s_RTTIBaseClassDescriptor;
struct _s_RTTICompleteObjectLocator
{
DWORD signature;
DWORD offset; //vftable offset to this
DWORD cdOffset;
TypeDescriptor *pTypeDescriptor;
_s_RTTIClassHierarchyDescriptor *pClassHierarchyDescriptor;
};
struct _s_RTTIClassHierarchyDescriptor
{
DWORD signature;
DWORD attributes; //bit 0 multiple inheritance, bit 1 virtual inheritance
size_t numBaseClasses; //at least 1 (all base classes, including itself)
_s_RTTIBaseClassDescriptor **pBaseClassArray;
};
struct _s_RTTIBaseClassDescriptor
{
TypeDescriptor *pTypeDescriptor;
size_t numBaseClasses; //direct base classes
PMD pmd; //Len=0xC
DWORD attributes;
_s_RTTIClassHierarchyDescriptor *pClassHierarchyDescriptor; //of this base class
};
template <typename R> // only ptrdiff_t and size_t allowed
R ReadData(size_t ptr)
{
return *(( const R *)ptr);
}
template < typename R, typename T>
R ReadData( const T *ptr)
{
return ReadData<R>((size_t)ptr);
}
//We assume the vftable is at offset 0
//If it is not the case, we assume the vbtable should be at offset 0, and with its first entry equal to 0 (point to itself).
//Then its second entry should contain the offset of vftable
template < typename T>
_s_RTTICompleteObjectLocator *GetCompleteObjectLocator( const T *ptr) //not have vbtable
{
ptrdiff_t offset=0;
if (ReadData<ptrdiff_t>(ReadData<size_t>(ptr))==0) offset=ReadData<ptrdiff_t>(ReadData<size_t>(ptr)+4);
return (_s_RTTICompleteObjectLocator *)(ReadData<size_t>(ReadData<size_t>((size_t)ptr+offset)-4));
}
CBaseType::CBaseType(const ClassDescriptor *descriptor, size_t offset)
{
m_BaseTypes = 0;
int basecount = descriptor->numBaseClasses;
int current = 1;
while (current <= basecount)
{
_s_RTTIBaseClassDescriptor *temp = descriptor->pClassHierarchyDescriptor->pBaseClassArray[current];
current+= temp->numBaseClasses;
current++;
m_BaseTypes++;
}
m_Offset = descriptor->pmd.mdisp + offset;
m_TypeInfo = descriptor->pTypeDescriptor;
m_Bases = (CBaseType *)malloc(sizeof(CBaseType) * m_BaseTypes);
current = 1;
int count = 0;
while (current <= basecount)
{
_s_RTTIBaseClassDescriptor *temp = descriptor->pClassHierarchyDescriptor->pBaseClassArray[current];
current+= temp->numBaseClasses;
current++;
new (&m_Bases[count]) CBaseType(temp, m_Offset);
count++;
}
}
ClassTypeWrapper::ClassTypeWrapper(const void *ptr)
{
_s_RTTICompleteObjectLocator *locator = GetCompleteObjectLocator(ptr);
m_Type = new CBaseType(locator->pClassHierarchyDescriptor->pBaseClassArray[0], 0);
}
#else
class __class_type_info;
/* helper class for __vmi_class_type */
class __base_class_info
{
/* abi defined member variables */
public:
const __class_type_info *__base; /* base class type */
long __offset_flags; /* offset and info */
/* implementation defined types */
public:
enum __offset_flags_masks {
__virtual_mask = 0x1,
__public_mask = 0x2,
hwm_bit = 2,
offset_shift = 8 /* bits to shift offset by */
};
/* implementation defined member functions */
public:
bool __is_virtual_p () const
{
return __offset_flags & __virtual_mask;
}
bool __is_public_p () const
{
return __offset_flags & __public_mask;
}
__PTRDIFF_TYPE__ __offset () const
{
// This shift, being of a signed type, is implementation defined. GCC
// implements such shifts as arithmetic, which is what we want.
return static_cast<__PTRDIFF_TYPE__> (__offset_flags) >> offset_shift;
}
};
class __class_type_info
: public std::type_info
{
};
class __si_class_type_info
: public __class_type_info
{
public:
const __class_type_info *__base_type;
};
class __vmi_class_type_info : public __class_type_info
{
/* abi defined member variables */
public:
unsigned int __flags; /* details about the class hierarchy */
unsigned int __base_count; /* number of direct bases */
__base_class_info const __base_info[1]; /* array of bases */
};
struct vtable_prefix {
ptrdiff_t whole_object; // offset to most derived object
const __class_type_info *whole_type; // pointer to most derived type_info
const void *origin; // what a class's vptr points to
};
template <typename T>
inline const T *
adjust_pointer (const void *base, ptrdiff_t offset)
{
return reinterpret_cast <const T *>
(reinterpret_cast <const char *> (base) + offset);
}
const __class_type_info *typeid2(const void *ptr)
{
const void *pVtable = *(const void **)(ptr);
const vtable_prefix *prefix = adjust_pointer <vtable_prefix> (pVtable, -offsetof (vtable_prefix, origin));
return prefix->whole_type;
}
enum InheritanceType
{
Inheritance_None, /* Doubles as unknown */
Inheritance_Single,
Inheritance_VirtualMultiple,
};
InheritanceType GetInheritanceType(const __class_type_info *ptr)
{
const __class_type_info *typeinfo = typeid2(ptr);
if (strcmp(typeinfo->name(), "N10__cxxabiv120__si_class_type_infoE") == 0)
{
return Inheritance_Single;
}
else if (strcmp(typeinfo->name(), "N10__cxxabiv121__vmi_class_type_infoE") == 0)
{
return Inheritance_VirtualMultiple;
}
return Inheritance_None;
}
CBaseType::CBaseType(const ClassDescriptor *descriptor, size_t offset)
{
InheritanceType type = GetInheritanceType(descriptor);
switch (type)
{
case Inheritance_Single:
{
m_BaseTypes = 1;
break;
}
case Inheritance_VirtualMultiple:
{
__vmi_class_type_info *vmiObject = (__vmi_class_type_info *)descriptor;
m_BaseTypes = vmiObject->__base_count;
break;
}
default:
{
m_BaseTypes = 0;
}
}
m_Offset = offset;
m_TypeInfo = descriptor;
if (type == Inheritance_None)
{
m_Bases = NULL;
return;
}
m_Bases = (CBaseType *)malloc(sizeof(CBaseType) * m_BaseTypes);
if (type == Inheritance_Single)
{
__si_class_type_info *siObject = (__si_class_type_info *)descriptor;
new (m_Bases) CBaseType(siObject->__base_type, offset);
return;
}
__vmi_class_type_info *vmiObject = (__vmi_class_type_info *)descriptor;
for (size_t i=0; i<m_BaseTypes; i++)
{
new (&m_Bases[i]) CBaseType(vmiObject->__base_info[i].__base, offset + vmiObject->__base_info[i].__offset());
}
}
ClassTypeWrapper::ClassTypeWrapper(const void *ptr)
{
m_Type = new CBaseType(typeid2(ptr), 0);
}
#endif