FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Source.Python/src/core/modules/memory/memory_rtti.cpp at master · srpg/Source.Python · GitHub
srpg
Source.Python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Source.Python
/
src
/
core
/
modules
/
memory
/
memory_rtti.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
407 lines (345 loc) · 9.4 KB
Breadcrumbs
Source.Python
/
src
/
core
/
modules
/
memory
/
memory_rtti.cpp
Copy path
File metadata and controls
407 lines (345 loc) · 9.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
*
* =============================================================================
* Source Python
* Copyright (C) 2012-2015 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
#
include
"
memory_rtti.h
"
#
include
"
utilities/call_python.h
"
#
include
<
assert.h
>
#
include
<
ctype.h
>
#
include
<
new
>
#
include
<
stdlib.h
>
#
include
<
string
>
#
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];
}
const
char
*
GetName
()
{
return
GetTypeName
(
GetTypeInfo
());
}
bool
IsDerivedFrom
(
const
char
* name)
{
return
IsClassDerivedFrom
(
this
, name);
}
bool
IsDerivedFrom
(IBaseType* pType)
{
return
IsDerivedFrom
(pType->
GetName
());
}
void
Dump
()
{
Dump
(
0
);
}
void
Dump
(
int
level)
{
std::string x =
"
"
;
for
(
int
i=
0
; i < level; ++i)
x +=
"
-
"
;
PythonLog
(-
1
,
"
%s%s
"
, x.
c_str
(),
GetName
());
for
(
unsigned
int
i=
0
; i <
GetNumBaseClasses
(); ++i) {
GetBaseClass
(i)->
Dump
(level +
1
);
}
}
private:
ptrdiff_t
m_Offset;
size_t
m_BaseTypes;
CBaseType *m_Bases;
const
std::type_info *m_TypeInfo;
};
/*
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
>
using
namespace
std
;
using
std::type_info;
typedef
std::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++;
}
}
#
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
());
}
}
#
endif
IBaseType *
GetType
(
const
void
*ptr)
{
#
ifdef
_WIN32
_s_RTTICompleteObjectLocator *locator =
GetCompleteObjectLocator
(ptr);
return
new
CBaseType
(locator->
pClassHierarchyDescriptor
->
pBaseClassArray
[
0
],
0
);
#
elif
__linux__
return
new
CBaseType
(
typeid2
(ptr),
0
);
#
else
#
error
Unsupported platform.
#
endif
}
Back
|
FazBrowse Home
|
New Git URL