forked from kweatherman/IDA_ClassInformer_PlugIn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTTI.cpp
More file actions
1795 lines (1609 loc) · 64 KB
/
RTTI.cpp
File metadata and controls
1795 lines (1609 loc) · 64 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Run-Time Type Information (RTTI) support
#include "stdafx.h"
#include "Main.h"
#include "RTTI.h"
#include "Vftable.h"
#include <WaitBoxEx.h>
// const Name::`vftable'
static LPCSTR FORMAT_RTTI_VFTABLE_PREFIX = "??_7";
static LPCSTR FORMAT_RTTI_VFTABLE = "??_7%s6B@";
// type 'RTTI Type Descriptor'
static LPCSTR FORMAT_RTTI_TYPE = "??_R0?%s@8";
// 'RTTI Base Class Descriptor at (a,b,c,d)'
static LPCSTR FORMAT_RTTI_BCD = "??_R1%s%s%s%s%s8";
// `RTTI Base Class Array'
static LPCSTR FORMAT_RTTI_BCA = "??_R2%s8";
// 'RTTI Class Hierarchy Descriptor'
static LPCSTR FORMAT_RTTI_CHD = "??_R3%s8";
// 'RTTI Complete Object Locator'
static LPCSTR FORMAT_RTTI_COL_PREFIX = "??_R4";
static LPCSTR FORMAT_RTTI_COL = "??_R4%s6B@";
// Skip type_info tag for class/struct mangled name strings
#define SKIP_TD_TAG(_str) ((_str) + SIZESTR(".?Ax"))
// Class name list container
struct bcdInfo
{
char m_name[MAXSTR];
UINT32 m_attribute;
RTTI::PMD m_pmd;
};
typedef std::vector<bcdInfo> bcdList;
// Cache of IDA strings we have already read for performance
static std::map<ea_t, qstring> stringCache;
// Set of known RTTI types to address location
static eaSet tdSet; // Known "type_info" type defines set
static eaSet chdSet; // _RTTIClassHierarchyDescriptor "Class Hierarchy Descriptor" (CHD) set
static eaSet bcdSet; // _RTTIBaseClassDescriptor "Base Class Descriptor" (BCD) set
eaSet vftSet; // `vftable'
eaSet colSet; // _RTTICompleteObjectLocator "Complete Object Locator" (COL) set
eaSet superSet; // Combined for faster scanning
#define IN_SUPER(_addr) (superSet.find(_addr) != superSet.end())
#define TO_INT64(_uint32) ((INT64) *((PINT32) &_uint32))
namespace RTTI
{
void getBCDInfo(ea_t col, __out bcdList& nameList, __out UINT32& numBaseClasses);
};
void RTTI::freeWorkingData()
{
stringCache.clear();
tdSet.clear();
chdSet.clear();
bcdSet.clear();
colSet.clear();
vftSet.clear();
superSet.clear();
}
// Make a mangled number string for labeling
static LPSTR mangleNumber(UINT32 number, __out_bcount(64) LPSTR buffer)
{
//
// 0 = A@
// X = X-1 (1 <= X <= 10)
// -X = ? (X - 1)
// 0x0..0xF = 'A'..'P'
// Can only get unsigned inputs
int num = *((PINT32) &number);
if (num == 0)
return strcpy(buffer, "A@");
else
{
int sign = 0;
if(num < 0)
{
sign = 1;
num = -num;
}
if(num <= 10)
{
_snprintf_s(buffer, 64, (64 - 1), "%s%d", (sign ? "?" : ""), (num - 1));
return buffer;
}
else
{
// Count digits
char buffer2[64];
int count = sizeof(buffer2);
while((num > 0) && (count > 0))
{
buffer2[sizeof(buffer2) - count] = ('A' + (num % 16));
num = (num / 16);
count--;
};
if(count == 0)
msg(" *** mangleNumber() overflow! ***");
_snprintf_s(buffer, 64, (64-1), "%s%s@", (sign ? "?" : ""), buffer2);
return buffer;
}
}
}
// Return a short label indicating the CHD inheritance type by attributes
// TODO: Consider CHD_AMBIGUOUS?
static LPCSTR attributeLabel(UINT32 attributes)
{
switch (attributes & 3)
{
case RTTI::CHD_MULTINH: return "[MI]";
case RTTI::CHD_VIRTINH: return "[VI]";
case (RTTI::CHD_MULTINH | RTTI::CHD_VIRTINH): return "[MI VI]";
};
return "";
}
// Get or make RTTI type definitions
static tid_t s_type_info_ID = BADADDR;
static tid_t s_PMD_ID = BADADDR;
static tid_t s_ClassHierarchyDescriptor_ID = BADADDR;
static tid_t s_BaseClassDescriptor_ID = BADADDR;
static tid_t s_CompleteObjectLocator_ID = BADADDR;
static void typeDump(tinfo_t &tinf)
{
qstring out;
tinf.print(&out, NULL, PRTYPE_DEF | PRTYPE_MULTI | PRTYPE_1LINCMT | PRTYPE_OFFSETS);
msg("type dump: \n\"%s\".\n", out.c_str());
}
// Look for existing type from a list of names
static tid_t GetKnownTypeID(LPCSTR names[], int namesCount, asize_t expectedTypeSize)
{
for (int i = 0; i < namesCount; i++)
{
tinfo_t tinf;
if (tinf.get_named_type(names[i], /*BTF_STRUCT*/ BTF_TYPEDEF))
{
if (tinf.present())
{
//qstring out;
//tinf.print(&out);
//msg("print: \"%s\".\n", out.c_str());
asize_t size = tinf.get_size();
if (size == expectedTypeSize)
{
// TODO: Could verify the type
//type_t _decltype = tinf.get_decltype(); // Looking for struct but it will be BTF_TYPEDEF
//typeDump(tinf);
return tinf.force_tid();
}
}
}
}
return BADADDR;
}
void RTTI::addDefinitionsToIda()
{
// std::type_info, aka "_TypeDescriptor" and "_RTTITypeDescriptor" in CRT source, class representation
static LPCSTR type_info_names[] =
{
"type_info",
"??_Rtype_info_std@@3Vtype_info@@A", /*std::type_info*/
// From at at least MSVC CRT sources 2017 and later. Will typically be there if the IDB has a matching PDB
"_TypeDescriptor",
"TypeDescriptor"
};
// Look for existing type by name
s_type_info_ID = GetKnownTypeID(type_info_names, _countof(type_info_names), (plat.is64 ? sizeof(type_info_64) : sizeof(type_info_32)));
// Not found so create it
if (s_type_info_ID == BADADDR)
{
LPCSTR def = R"DEF(
// RTTI std::type_info class (#classinformer)
struct type_info
{
const void *vfptr;
void *_M_data;
char _M_d_name[];
};
)DEF";
if (parse_decls(NULL, def, msg, HTI_DCL) != 0)
msg("** addDefinitionsToIda(): \"type_info\" create failed! **\n");
s_type_info_ID = get_named_type_tid("type_info");
// Set the representation of the "name" field to a string literal
// TODO: Can put __strlit(C) in the definition?
if (s_type_info_ID != BADADDR)
{
tinfo_t tinf;
value_repr_t repr;
if (tinf.get_type_by_tid(s_type_info_ID) && repr.parse_value_repr("__strlit(C,\"windows - 1252\");" /*"__strlit(C)"*/))
{
tinf.set_udm_repr(2, repr);
//typeDump(tinf);
}
}
}
// PDM
static LPCSTR pdm_names[] = { "_PDM", "PDM" };
s_PMD_ID = GetKnownTypeID(pdm_names, _countof(pdm_names), sizeof(PMD));
if (s_PMD_ID == BADADDR)
{
LPCSTR def = R"DEF(
// RTTI Base class descriptor displacement container (#classinformer)
struct PMD
{
int mdisp;
int pdisp;
int vdisp;
};
)DEF";
if (parse_decls(NULL, def, msg, HTI_DCL) != 0)
msg("** addDefinitionsToIda(): \"PMD\" create failed! **\n");
s_PMD_ID = get_named_type_tid("PMD");
/*
tinfo_t tinf;
if (tinf.get_type_by_tid(s_PMD_ID))
typeDump(tinf);
*/
}
// _RTTIClassHierarchyDescriptor
static LPCSTR chd_names[] = { "_s__RTTIClassHierarchyDescriptor", "__RTTIClassHierarchyDescriptor", "_RTTIClassHierarchyDescriptor", "RTTIClassHierarchyDescriptor" };
s_ClassHierarchyDescriptor_ID = GetKnownTypeID(chd_names, _countof(chd_names), (sizeof(_RTTIClassHierarchyDescriptor) + (plat.is64 ? sizeof(UINT32) : 0))); // The 64bit one in the CRT source shows it's a pointer while it's an int offset in binary
if (s_ClassHierarchyDescriptor_ID == BADADDR)
{
LPCSTR def;
if (!plat.is64)
{
// 32bit
def = R"DEF(
// RTTI Class Hierarchy Descriptor (#classinformer)
struct _RTTIClassHierarchyDescriptor
{
unsigned int signature;
unsigned int attributes;
unsigned int numBaseClasses;
void *baseClassArray; // _RTTIBaseClassArray*
};
)DEF";
}
else
{
// 64bit
def = R"DEF(
// RTTI Class Hierarchy Descriptor (#classinformer)
struct _RTTIClassHierarchyDescriptor
{
unsigned int signature;
unsigned int attributes;
unsigned int numBaseClasses;
int baseClassArray;
};
)DEF";
}
if (parse_decls(NULL, def, msg, HTI_DCL) != 0)
msg("** addDefinitionsToIda(): \"_RTTIClassHierarchyDescriptor\" create failed! **\n");
s_ClassHierarchyDescriptor_ID = get_named_type_tid("_RTTIClassHierarchyDescriptor");
/*
tinfo_t tinf;
if (tinf.get_type_by_tid(s_ClassHierarchyDescriptor_ID))
typeDump(tinf);
*/
}
// _RTTIBaseClassDescriptor
static LPCSTR bcd_names[] = { "_s__RTTIBaseClassDescriptor", "__RTTIBaseClassDescriptor", "_RTTIBaseClassDescriptor", "RTTIBaseClassDescriptor" };
s_BaseClassDescriptor_ID = GetKnownTypeID(bcd_names, _countof(bcd_names), (sizeof(_RTTIBaseClassDescriptor) + (plat.is64 ? sizeof(UINT64) : 0)));
if (s_BaseClassDescriptor_ID == BADADDR)
{
LPCSTR def;
if (!plat.is64)
{
// 32bit
def = R"DEF(
// RTTI Base class descriptor displacement container (#classinformer)
struct _RTTIBaseClassDescriptor
{
int typeDescriptor;
unsigned int numContainedBases;
PMD pmd;
unsigned int attributes;
void *classDescriptor; // _RTTIClassHierarchyDescriptor*
};
)DEF";
}
else
{
// 64bit
def = R"DEF(
// RTTI Base class descriptor displacement container (#classinformer)
struct _RTTIBaseClassDescriptor
{
int typeDescriptor;
unsigned int numContainedBases;
PMD pmd;
unsigned int attributes;
int classDescriptor;
};
)DEF";
}
if (parse_decls(NULL, def, msg, HTI_DCL) != 0)
msg("** addDefinitionsToIda(): \"_RTTIBaseClassDescriptor\" create failed! **\n");
s_BaseClassDescriptor_ID = get_named_type_tid("_RTTIBaseClassDescriptor");
/*
tinfo_t tinf;
if (tinf.get_type_by_tid(s_BaseClassDescriptor_ID))
typeDump(tinf);
*/
}
// _RTTICompleteObjectLocator
static LPCSTR col_names[] = { "_s__RTTICompleteObjectLocator2", "_s__RTTICompleteObjectLocator", "__RTTICompleteObjectLocator", "_RTTICompleteObjectLocator", "RTTICompleteObjectLocator" };
s_CompleteObjectLocator_ID = GetKnownTypeID(col_names, _countof(col_names), (sizeof(_RTTICompleteObjectLocator) + (plat.is64 ? 16 : 0)));
if (s_CompleteObjectLocator_ID == BADADDR)
{
LPCSTR def;
if (!plat.is64)
{
// 32bit
def = R"DEF(
// RTTI Complete Object Locator (#classinformer)
struct _RTTICompleteObjectLocator
{
unsigned int signature;
unsigned int offset;
unsigned int cdOffset;
void *typeDescriptor; // type_info*
void *classDescriptor; // _RTTIClassHierarchyDescriptor*
};
)DEF";
}
else
{
// 64bit
def = R"DEF(
// RTTI Complete Object Locator (#classinformer)
struct _RTTICompleteObjectLocator
{
unsigned int signature;
unsigned int offset;
unsigned int cdOffset;
int typeDescriptor;
int classDescriptor;
int objectBase;
};
)DEF";
}
if (parse_decls(NULL, def, msg, HTI_DCL) != 0)
msg("** addDefinitionsToIda(): \"_RTTICompleteObjectLocator\" create failed! **\n");
s_CompleteObjectLocator_ID = get_named_type_tid("_RTTICompleteObjectLocator");
/*
tinfo_t tinf;
if (tinf.get_type_by_tid(s_CompleteObjectLocator_ID))
typeDump(tinf);
*/
}
}
// Place an RTTI structure by type ID add address w/optional name
// Returns TRUE if structure was placed, else FLASE it was already set
static BOOL tryStructRTTI(ea_t ea, tid_t tid, __in_opt LPSTR typeName = NULL, BOOL bHasChd = FALSE)
{
if (tid == BADADDR)
{
_ASSERT(FALSE);
return FALSE;
}
#define put32(ea) create_dword(ea, sizeof(EA_32), TRUE)
#define put64(ea) create_qword(ea, sizeof(ea_t), TRUE)
// type_info
if(tid == s_type_info_ID)
{
if (!plat.is64)
{
// EA_32
if (!hasName(ea))
{
_ASSERT(typeName != NULL);
UINT32 nameLen = (UINT32) (strlen(typeName) + 1);
UINT32 structSize = (offsetof(RTTI::type_info_32, _M_d_name) + nameLen);
// Place struct
setUnknown(ea, structSize);
BOOL result = FALSE;
if (g_optionPlaceStructs)
result = create_struct(ea, structSize, s_type_info_ID);
if (!result)
{
// Else fix/place the proper type fields and name it
put32(ea + offsetof(RTTI::type_info_32, vfptr));
put32(ea + offsetof(RTTI::type_info_32, _M_data));
create_strlit((ea + offsetof(RTTI::type_info_32, _M_d_name)), nameLen, STRTYPE_C);
}
// sh!ft: End should be aligned
ea_t end = (ea + offsetof(RTTI::type_info_32, _M_d_name) + nameLen);
if (end % 4)
create_align(end, (4 - (end % 4)), 0);
return TRUE;
}
}
else
{
// EA_64
if (!hasName(ea))
{
_ASSERT(typeName != NULL);
UINT32 nameLen = (UINT32) (strlen(typeName) + 1);
UINT32 structSize = (offsetof(RTTI::type_info_64, _M_d_name) + nameLen);
// Place struct
setUnknown(ea, structSize);
BOOL result = FALSE;
if (g_optionPlaceStructs && (s_type_info_ID > 5))
result = create_struct(ea, structSize, s_type_info_ID);
if (!result)
{
put64(ea + offsetof(RTTI::type_info_64, vfptr));
put64(ea + offsetof(RTTI::type_info_64, _M_data));
create_strlit((ea + offsetof(RTTI::type_info_64, _M_d_name)), nameLen, STRTYPE_C);
}
// sh!ft: End should be aligned
#pragma message(__LOC2__ " >> Should be align 8? Do we really even need this?")
ea_t end = (ea + offsetof(RTTI::type_info_64, _M_d_name) + nameLen);
if (end % 4)
create_align(end, (4 - (end % 4)), 0);
return TRUE;
}
}
return FALSE;
}
// _RTTIClassHierarchyDescriptor
if (tid == s_ClassHierarchyDescriptor_ID)
{
if (!hasName(ea))
{
setUnknown(ea, sizeof(RTTI::_RTTIClassHierarchyDescriptor));
BOOL result = FALSE;
if (g_optionPlaceStructs)
result = create_struct(ea, sizeof(RTTI::_RTTIClassHierarchyDescriptor), s_ClassHierarchyDescriptor_ID);
if (!result)
{
put32(ea + offsetof(RTTI::_RTTIClassHierarchyDescriptor, signature));
put32(ea + offsetof(RTTI::_RTTIClassHierarchyDescriptor, attributes));
put32(ea + offsetof(RTTI::_RTTIClassHierarchyDescriptor, numBaseClasses));
put32(ea + offsetof(RTTI::_RTTIClassHierarchyDescriptor, baseClassArray));
}
return TRUE;
}
return FALSE;
}
// PMD
if(tid == s_PMD_ID)
{
if (!hasName(ea))
{
setUnknown(ea, sizeof(RTTI::PMD));
BOOL result = FALSE;
if (g_optionPlaceStructs)
result = create_struct(ea, sizeof(RTTI::PMD), s_PMD_ID);
if (!result)
{
put32(ea + offsetof(RTTI::PMD, mdisp));
put32(ea + offsetof(RTTI::PMD, pdisp));
put32(ea + offsetof(RTTI::PMD, vdisp));
}
return TRUE;
}
return FALSE;
}
// _RTTICompleteObjectLocator
if(tid == s_CompleteObjectLocator_ID)
{
if (!plat.is64)
{
// EA_32
if (!hasName(ea))
{
setUnknown(ea, sizeof(RTTI::_RTTICompleteObjectLocator_32));
BOOL result = FALSE;
if (g_optionPlaceStructs)
result = create_struct(ea, sizeof(RTTI::_RTTICompleteObjectLocator_32), s_CompleteObjectLocator_ID);
if (!result)
{
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_32, signature));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_32, offset));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_32, cdOffset));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_32, typeDescriptor));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_32, classDescriptor));
}
return TRUE;
}
}
else
{
// EA_64
if (!hasName(ea))
{
setUnknown(ea, sizeof(RTTI::_RTTICompleteObjectLocator_64));
BOOL result = FALSE;
if (g_optionPlaceStructs)
result = create_struct(ea, sizeof(RTTI::_RTTICompleteObjectLocator_64), s_CompleteObjectLocator_ID);
if (!result)
{
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_64, signature));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_64, offset));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_64, cdOffset));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_64, typeDescriptor));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_64, classDescriptor));
put32(ea + offsetof(RTTI::_RTTICompleteObjectLocator_64, objectBase));
}
return TRUE;
}
}
return FALSE;
}
// _RTTIBaseClassDescriptor
if (tid == s_BaseClassDescriptor_ID)
{
// Recursive
//msg("PMD: 0x%llX\n", (ea + offsetof(RTTI::_RTTIBaseClassDescriptor, pmd)));
tryStructRTTI(ea + offsetof(RTTI::_RTTIBaseClassDescriptor, pmd), s_PMD_ID);
if (!hasName(ea))
{
setUnknown(ea, sizeof(RTTI::_RTTIBaseClassDescriptor));
BOOL result = FALSE;
if (g_optionPlaceStructs)
result = create_struct(ea, sizeof(RTTI::_RTTIBaseClassDescriptor), s_BaseClassDescriptor_ID);
if (!result)
{
put32(ea + offsetof(RTTI::_RTTIBaseClassDescriptor, typeDescriptor));
put32(ea + offsetof(RTTI::_RTTIBaseClassDescriptor, numContainedBases));
put32(ea + offsetof(RTTI::_RTTIBaseClassDescriptor, attributes));
//if (bHasChd)
put32(ea + offsetof(RTTI::_RTTIBaseClassDescriptor, classDescriptor));
if (bHasChd)
setComment((ea + offsetof(RTTI::_RTTIBaseClassDescriptor, classDescriptor)), "BCD_HASPCHD set", TRUE);
}
return TRUE;
}
return FALSE;
}
_ASSERT(FALSE);
return FALSE;
}
// Read ASCII string from IDB at address
static int getIdaString(ea_t ea, __out LPSTR buffer, int bufferSize)
{
buffer[0] = 0;
// Return cached name if already exists
auto it = stringCache.find(ea);
if (it != stringCache.end())
{
LPCSTR str = it->second.c_str();
int len = (int) strlen(str);
if (len > bufferSize)
len = bufferSize;
strncpy_s(buffer, MAXSTR, str, len);
return len;
}
else
{
// Read string at ea if it exists
int len = (int) get_max_strlit_length(ea, STRTYPE_C, ALOPT_IGNHEADS);
if (len > 0)
{
// Length includes terminator
if (len > bufferSize)
len = bufferSize;
qstring str;
int len2 = get_strlit_contents(&str, ea, len, STRTYPE_C);
if (len2 > 0)
{
// Length with out terminator
if (len2 > bufferSize)
len2 = bufferSize;
// Cache it
memcpy(buffer, str.c_str(), len2);
buffer[len2] = 0;
stringCache[ea] = buffer;
}
else
len = 0;
}
return len ;
}
}
// --------------------------- Type descriptor ---------------------------
// Get type name into a buffer
// type_info assumed to be valid
int RTTI::type_info::getName(ea_t typeInfo, __out LPSTR buffer, int bufferSize)
{
return getIdaString(typeInfo + (plat.is64 ? offsetof(type_info_64, _M_d_name) : offsetof(type_info_32, _M_d_name)), buffer, bufferSize);
}
// A valid type_info/TypeDescriptor at pointer?
BOOL RTTI::type_info::isValid(ea_t typeInfo)
{
// TRUE if we've already seen it
if (tdSet.find(typeInfo) != tdSet.end())
return TRUE;
if (IS_VALID_ADDR(typeInfo))
{
// Verify what should be a vftable
ea_t ea = plat.getEa(typeInfo + (plat.is64 ? offsetof(type_info_64, vfptr) : offsetof(type_info_32, vfptr)));
if (IS_VALID_ADDR(ea))
{
// _M_data should be NULL statically
ea_t _M_data = BADADDR;
if (getVerifyEa((typeInfo + (plat.is64 ? offsetof(type_info_64, _M_data) : offsetof(type_info_32, _M_data))), _M_data))
{
if (_M_data == 0)
return isTypeName(typeInfo + (plat.is64 ? offsetof(type_info_64, _M_d_name) : offsetof(type_info_32, _M_d_name)));
}
}
}
return FALSE;
}
//
// Returns TRUE if known typename at address
BOOL RTTI::type_info::isTypeName(ea_t name)
{
// Should start with a period
if (get_byte(name) == '.')
{
// Read the rest of the possible name string
char buffer[MAXSTR];
if (getIdaString(name, buffer, SIZESTR(buffer)))
{
// Should be valid if it properly demangles
if (LPSTR s = __unDName(NULL, buffer+1 /*skip the '.'*/, 0, mallocWrap, free, (UNDNAME_32_BIT_DECODE | UNDNAME_TYPE_ONLY)))
{
free(s);
return TRUE;
}
}
}
return FALSE;
}
// Put struct and place name at address
void RTTI::type_info::tryStruct(ea_t typeInfo)
{
// Only place once per address
if (tdSet.find(typeInfo) != tdSet.end())
return;
else
tdSet.insert(typeInfo);
// Get type name
char name[MAXSTR];
int nameLen = getName(typeInfo, name, SIZESTR(name));
//msg("TD: 0x%llX\n", typeInfo);
tryStructRTTI(typeInfo, s_type_info_ID, name);
if (nameLen > 0)
{
if (!hasName(typeInfo))
{
// Set decorated name/label
char name2[MAXSTR];
_snprintf_s(name2, sizeof(name2), SIZESTR(name2), FORMAT_RTTI_TYPE, (name + 2));
setName(typeInfo, name2);
}
}
else
{
_ASSERT(FALSE);
}
}
// --------------------------- Complete Object Locator ---------------------------
// Return TRUE if address is a valid RTTI structure
BOOL RTTI::_RTTICompleteObjectLocator::isValid(ea_t col)
{
// True if already known
if (colSet.find(col) != colSet.end())
return TRUE;
if (IS_VALID_ADDR(col))
{
// Check signature
UINT32 signature = -1;
if (getVerify32((col + offsetof(_RTTICompleteObjectLocator, signature)), signature))
{
if (!plat.is64)
{
// 32bit direct addresses
if (signature == 0)
{
// Check valid type_info
ea_t typeInfo = plat.getEa32(col + offsetof(_RTTICompleteObjectLocator_32, typeDescriptor));
if (type_info_32::isValid(typeInfo))
{
ea_t classDescriptor = plat.getEa32(col + offsetof(_RTTICompleteObjectLocator_32, classDescriptor));
if (_RTTIClassHierarchyDescriptor::isValid(classDescriptor))
{
//msg("%llX %llX %llX\n", col, typeInfo, classDescriptor);
return TRUE;
}
}
}
}
else
{
// 64bit bases plus objectBase offsets
if (signature == 1)
{
// TODO: Can any of these be zero and still be valid?
UINT32 objectLocator32 = get_32bit(col + offsetof(_RTTICompleteObjectLocator_64, objectBase));
INT64 objectLocator64 = TO_INT64(objectLocator32);
if (objectLocator64 != 0)
{
UINT32 tdOffset32 = get_32bit(col + offsetof(_RTTICompleteObjectLocator_64, typeDescriptor));
INT64 tdOffset64 = TO_INT64(tdOffset32);
if (tdOffset64 != 0)
{
UINT32 cdOffset32 = get_32bit(col + offsetof(_RTTICompleteObjectLocator_64, classDescriptor));
INT64 cdOffset64 = TO_INT64(cdOffset32);
if (cdOffset64 != 0)
{
INT64 colBase64 = ((INT64) col - objectLocator64);
ea_t typeInfo = (ea_t) (colBase64 + tdOffset64);
if (type_info_64::isValid(typeInfo))
{
ea_t classDescriptor = (ea_t) (colBase64 + cdOffset64);
if (_RTTIClassHierarchyDescriptor::isValid(classDescriptor, colBase64))
{
//msg("%llX %llX %llX\n", col, typeInfo, classDescriptor);
return TRUE;
}
}
}
}
}
}
}
}
}
return FALSE;
}
// Same as above but from an already validated type_info perspective
BOOL RTTI::_RTTICompleteObjectLocator_32::isValid2(ea_t col)
{
// True if already known
if (colSet.find(col) != colSet.end())
return TRUE;
// 'signature' should be zero
UINT32 signature = -1;
if (getVerify32((col + offsetof(_RTTICompleteObjectLocator_32, signature)), signature))
{
if (signature == 0)
{
// Verify CHD
ea_t classDescriptor = plat.getEa32(col + offsetof(_RTTICompleteObjectLocator_32, classDescriptor));
if (classDescriptor && (classDescriptor != BADADDR))
return _RTTIClassHierarchyDescriptor::isValid(classDescriptor);
}
}
return FALSE;
}
// Place full COL hierarchy structures if they don't already exist
BOOL RTTI::_RTTICompleteObjectLocator::tryStruct(ea_t col)
{
// Place it once
if (colSet.find(col) != colSet.end())
return TRUE;
// If it doesn't have a name, IDA's analyzer missed it
if (!hasName(col))
{
#if 0
qstring buf;
idaFlags2String(get_flags(col), buf);
msg("%llX fix COL (%s)\n", col, buf.c_str());
#endif
//msg("COL: 0x%llX\n", col);
tryStructRTTI(col, s_CompleteObjectLocator_ID);
// Put type_def
if (!plat.is64)
{
// 32bit direct address
ea_t typeInfo = plat.getEa32(col + offsetof(_RTTICompleteObjectLocator_32, typeDescriptor));
type_info_32::tryStruct(typeInfo);
// Place CHD hierarchy
ea_t classDescriptor = plat.getEa32(col + offsetof(_RTTICompleteObjectLocator_32, classDescriptor));
_RTTIClassHierarchyDescriptor::tryStruct(classDescriptor);
}
else
{
// 64bit offsets plus objectBase
UINT32 tdOffset32 = get_32bit(col + offsetof(_RTTICompleteObjectLocator_64, typeDescriptor));
INT64 tdOffset64 = TO_INT64(tdOffset32);
UINT32 cdOffset32 = get_32bit(col + offsetof(_RTTICompleteObjectLocator_64, classDescriptor));
INT64 cdOffset64 = TO_INT64(cdOffset32);
UINT32 objectLocator32 = get_32bit(col + offsetof(_RTTICompleteObjectLocator_64, objectBase));
INT64 objectLocator64 = TO_INT64(objectLocator32);
INT64 colBase64 = ((INT64) col - objectLocator64);
ea_t typeInfo = (ea_t) (colBase64 + tdOffset64);
type_info::tryStruct(typeInfo);
ea_t classDescriptor = (ea_t) (colBase64 + cdOffset64);
_RTTIClassHierarchyDescriptor::tryStruct(classDescriptor, colBase64);
// Set absolute address comments
ea_t ea = (col + offsetof(_RTTICompleteObjectLocator_64, typeDescriptor));
if (!hasComment(ea))
{
char buffer[64];
sprintf_s(buffer, sizeof(buffer), "0x%llX", typeInfo);
setComment(ea, buffer, TRUE);
}
ea = (col + offsetof(_RTTICompleteObjectLocator_64, classDescriptor));
if (!hasComment(ea))
{
char buffer[64];
sprintf_s(buffer, sizeof(buffer), "0x%llX", classDescriptor);
setComment(ea, buffer, TRUE);
}
}
return TRUE;
}
return FALSE;
}
// --------------------------- Base Class Descriptor ---------------------------
// Return TRUE if address is a valid BCD
BOOL RTTI::_RTTIBaseClassDescriptor::isValid(ea_t bcd, INT64 colBase64)
{
// TRUE if already known
if (bcdSet.find(bcd) != bcdSet.end())
return TRUE;
if (IS_VALID_ADDR(bcd))
{
// Check attributes flags first
UINT32 attributes = -1;
if (getVerify32((bcd + offsetof(_RTTIBaseClassDescriptor, attributes)), attributes))
{
// Valid flags are the lower byte only
if ((attributes & 0xFFFFFF00) == 0)
{
// Check for valid type_info
if (!plat.is64)
{
// When 32bit direct address
return type_info_32::isValid(plat.getEa32(bcd + offsetof(_RTTIBaseClassDescriptor, typeDescriptor)));
}
else
{
// When 64bit plus COL bass ea_t
UINT32 tdOffset32 = get_32bit(bcd + offsetof(_RTTIBaseClassDescriptor, typeDescriptor));
INT64 tdOffset64 = TO_INT64(tdOffset32);
return type_info_64::isValid((ea_t) (colBase64 + tdOffset64));
}
}
}
}
return FALSE;
}
// Put BCD structure at address
void RTTI::_RTTIBaseClassDescriptor::tryStruct(ea_t bcd, __out_bcount(MAXSTR) LPSTR baseClassName, INT64 colBase64)
{
// Only place it once
if (bcdSet.find(bcd) != bcdSet.end())
{
// Seen already, just return type name
ea_t typeInfo = BADADDR;
if (!plat.is64)
{
// When 32bit direct address
typeInfo = plat.getEa32(bcd + offsetof(_RTTIBaseClassDescriptor, typeDescriptor));
}
else
{
// When 64bit plus COL bass ea_t
UINT32 tdOffset32 = get_32bit(bcd + offsetof(_RTTIBaseClassDescriptor, typeDescriptor));
INT64 tdOffset64 = TO_INT64(tdOffset32);
typeInfo = (ea_t) (colBase64 + tdOffset64);
}
char buffer[MAXSTR];
type_info::getName(typeInfo, buffer, SIZESTR(buffer));
strcpy_s(baseClassName, sizeof(buffer), SKIP_TD_TAG(buffer));
return;
}
else
bcdSet.insert(bcd);
if (IS_VALID_ADDR(bcd))
{
UINT32 attributes = get_32bit(bcd + offsetof(_RTTIBaseClassDescriptor, attributes));
//msg("BCD: 0x%llX\n", bcd);
tryStructRTTI(bcd, s_BaseClassDescriptor_ID, NULL, ((attributes & BCD_HASPCHD) > 0));
// Has appended CHD?
if (attributes & BCD_HASPCHD)
{
// yes, process it
ea_t chdOffset = (bcd + (offsetof(_RTTIBaseClassDescriptor, classDescriptor)));
ea_t chd = BADADDR;
if (!plat.is64)
{
// EA_32
fixDword(chdOffset);
chd = get_32bit(chdOffset);
}
else
{
// EA_64
fixDword(chdOffset);
UINT32 chdOffset32 = get_32bit(chdOffset);
INT64 chdOffset64 = TO_INT64(chdOffset32);
chd = (ea_t) (colBase64 + chdOffset64);
if (!hasComment(chdOffset))
{
char buffer[32];
sprintf_s(buffer, sizeof(buffer), "0x%llX", chd);
setComment(chdOffset, buffer, TRUE);
}
}
if (IS_VALID_ADDR(chd))
_RTTIClassHierarchyDescriptor::tryStruct(chd, colBase64);
else