forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataCache.cpp
More file actions
1325 lines (1086 loc) · 51.9 KB
/
MetadataCache.cpp
File metadata and controls
1325 lines (1086 loc) · 51.9 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
#include "il2cpp-config.h"
#include "MetadataCache.h"
#include "GlobalMetadata.h"
#include <map>
#include <unordered_set>
#include <limits>
#include "il2cpp-tabledefs.h"
#include "il2cpp-runtime-stats.h"
#include "gc/GarbageCollector.h"
#include "metadata/ArrayMetadata.h"
#include "metadata/GenericMetadata.h"
#include "metadata/GenericMethod.h"
#include "os/Atomic.h"
#include "os/Mutex.h"
#include "utils/CallOnce.h"
#include "utils/Collections.h"
#include "utils/Il2CppHashSet.h"
#include "utils/Memory.h"
#include "utils/PathUtils.h"
#include "vm/Assembly.h"
#include "vm/Class.h"
#include "vm/ClassInlines.h"
#include "vm/GenericClass.h"
#include "vm/MetadataAlloc.h"
#include "vm/MetadataLoader.h"
#include "vm/MetadataLock.h"
#include "vm/Method.h"
#include "vm/Object.h"
#include "vm/Runtime.h"
#include "vm/String.h"
#include "vm/Type.h"
#include "vm-utils/MethodDefinitionKey.h"
#include "vm-utils/NativeSymbol.h"
#include "Baselib.h"
#include "Cpp/ReentrantLock.h"
#include "hybridclr/metadata/Assembly.h"
#include "hybridclr/metadata/MetadataModule.h"
typedef Il2CppReaderWriterLockedHashMap<Il2CppClass*, Il2CppClass*> PointerTypeMap;
typedef Il2CppHashSet<const Il2CppGenericMethod*, il2cpp::metadata::Il2CppGenericMethodHash, il2cpp::metadata::Il2CppGenericMethodCompare> Il2CppGenericMethodSet;
typedef Il2CppGenericMethodSet::const_iterator Il2CppGenericMethodSetIter;
struct Il2CppMetadataCache
{
il2cpp::os::FastReaderReaderWriterLock m_CacheLock;
PointerTypeMap m_PointerTypes;
};
static Il2CppMetadataCache s_MetadataCache;
static int32_t s_ImagesCount = 0;
static Il2CppImage* s_ImagesTable = NULL;
static int32_t s_AssembliesCount = 0;
static Il2CppAssembly* s_AssembliesTable = NULL;
typedef std::unordered_set<const Il2CppGenericInst*, il2cpp::metadata::Il2CppGenericInstHash, il2cpp::metadata::Il2CppGenericInstCompare> Il2CppGenericInstSet;
static Il2CppGenericInstSet s_GenericInstSet;
typedef il2cpp::vm::Il2CppMethodTableMap::const_iterator Il2CppMethodTableMapIter;
static il2cpp::vm::Il2CppMethodTableMap s_MethodTableMap;
typedef il2cpp::vm::Il2CppUnresolvedSignatureMap::const_iterator Il2CppUnresolvedSignatureMapIter;
static il2cpp::vm::Il2CppUnresolvedSignatureMap *s_pUnresolvedSignatureMap;
typedef Il2CppHashMap<FieldInfo*, int32_t, il2cpp::utils::PointerHash<FieldInfo> > Il2CppThreadLocalStaticOffsetHashMap;
typedef Il2CppThreadLocalStaticOffsetHashMap::iterator Il2CppThreadLocalStaticOffsetHashMapIter;
static Il2CppThreadLocalStaticOffsetHashMap s_ThreadLocalStaticOffsetMap;
static const Il2CppCodeRegistration * s_Il2CppCodeRegistration;
static const Il2CppMetadataRegistration* s_MetadataCache_Il2CppMetadataRegistration;
static const Il2CppCodeGenOptions* s_Il2CppCodeGenOptions;
static il2cpp::vm::WindowsRuntimeTypeNameToClassMap s_WindowsRuntimeTypeNameToClassMap;
static il2cpp::vm::ClassToWindowsRuntimeTypeNameMap s_ClassToWindowsRuntimeTypeNameMap;
struct InteropDataToTypeConverter
{
inline const Il2CppType* operator()(const Il2CppInteropData& interopData) const
{
return interopData.type;
}
};
typedef il2cpp::utils::collections::ArrayValueMap<const Il2CppType*, Il2CppInteropData, InteropDataToTypeConverter, il2cpp::metadata::Il2CppTypeLess, il2cpp::metadata::Il2CppTypeEqualityComparer> InteropDataMap;
static InteropDataMap s_InteropData;
struct WindowsRuntimeFactoryTableEntryToTypeConverter
{
inline const Il2CppType* operator()(const Il2CppWindowsRuntimeFactoryTableEntry& entry) const
{
return entry.type;
}
};
typedef il2cpp::utils::collections::ArrayValueMap<const Il2CppType*, Il2CppWindowsRuntimeFactoryTableEntry, WindowsRuntimeFactoryTableEntryToTypeConverter, il2cpp::metadata::Il2CppTypeLess, il2cpp::metadata::Il2CppTypeEqualityComparer> WindowsRuntimeFactoryTable;
static WindowsRuntimeFactoryTable s_WindowsRuntimeFactories;
template<typename K, typename V>
struct PairToKeyConverter
{
inline const K& operator()(const std::pair<K, V>& pair) const
{
return pair.first;
}
};
typedef il2cpp::utils::collections::ArrayValueMap<const Il2CppGuid*, std::pair<const Il2CppGuid*, Il2CppClass*>, PairToKeyConverter<const Il2CppGuid*, Il2CppClass*> > GuidToClassMap;
static GuidToClassMap s_GuidToNonImportClassMap;
static il2cpp::utils::dynamic_array<Il2CppAssembly*> s_cliAssemblies;
void il2cpp::vm::MetadataCache::Register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)
{
il2cpp::vm::GlobalMetadata::Register(codeRegistration, metadataRegistration, codeGenOptions);
s_Il2CppCodeRegistration = codeRegistration;
s_MetadataCache_Il2CppMetadataRegistration = metadataRegistration;
s_Il2CppCodeGenOptions = codeGenOptions;
}
Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromTypeIndex(const Il2CppImage *image, TypeIndex index)
{
return il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeIndex(index);
}
const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromMethodDefinitionIndex(const Il2CppImage *image, MethodIndex index)
{
return il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(index);
}
const MethodInfo* il2cpp::vm::MetadataCache::GetAssemblyEntryPoint(const Il2CppImage* image)
{
return il2cpp::vm::GlobalMetadata::GetAssemblyEntryPoint(image);
}
Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetAssemblyTypeHandle(const Il2CppImage* image, AssemblyTypeIndex index)
{
return il2cpp::vm::GlobalMetadata::GetAssemblyTypeHandle(image, index);
}
Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetAssemblyExportedTypeHandle(const Il2CppImage* image, AssemblyExportedTypeIndex index)
{
return il2cpp::vm::GlobalMetadata::GetAssemblyExportedTypeHandle(image, index);
}
const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromMethodHandle(Il2CppMetadataMethodDefinitionHandle handle)
{
return il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodHandle(handle);
}
bool il2cpp::vm::MetadataCache::Initialize()
{
if (!il2cpp::vm::GlobalMetadata::Initialize(&s_ImagesCount, &s_AssembliesCount))
{
return false;
}
il2cpp::metadata::GenericMetadata::RegisterGenericClasses(s_MetadataCache_Il2CppMetadataRegistration->genericClasses, s_MetadataCache_Il2CppMetadataRegistration->genericClassesCount);
il2cpp::metadata::GenericMetadata::SetMaximumRuntimeGenericDepth(s_Il2CppCodeGenOptions->maximumRuntimeGenericDepth);
il2cpp::metadata::GenericMetadata::SetGenericVirtualIterations(s_Il2CppCodeGenOptions->recursiveGenericIterations);
s_GenericInstSet.reserve(s_MetadataCache_Il2CppMetadataRegistration->genericInstsCount);
for (int32_t i = 0; i < s_MetadataCache_Il2CppMetadataRegistration->genericInstsCount; i++)
{
s_GenericInstSet.insert(s_MetadataCache_Il2CppMetadataRegistration->genericInsts[i]);
}
s_InteropData.assign_external(s_Il2CppCodeRegistration->interopData, s_Il2CppCodeRegistration->interopDataCount);
s_WindowsRuntimeFactories.assign_external(s_Il2CppCodeRegistration->windowsRuntimeFactoryTable, s_Il2CppCodeRegistration->windowsRuntimeFactoryCount);
// Pre-allocate these arrays so we don't need to lock when reading later.
// These arrays hold the runtime metadata representation for metadata explicitly
// referenced during conversion. There is a corresponding table of same size
// in the converted metadata, giving a description of runtime metadata to construct.
s_ImagesTable = (Il2CppImage*)IL2CPP_CALLOC(s_ImagesCount, sizeof(Il2CppImage));
s_AssembliesTable = (Il2CppAssembly*)IL2CPP_CALLOC(s_AssembliesCount, sizeof(Il2CppAssembly));
// setup all the Il2CppImages. There are not many and it avoid locks later on
for (int32_t imageIndex = 0; imageIndex < s_ImagesCount; imageIndex++)
{
Il2CppImage* image = s_ImagesTable + imageIndex;
AssemblyIndex imageAssemblyIndex;
il2cpp::vm::GlobalMetadata::BuildIl2CppImage(image, imageIndex, &imageAssemblyIndex);
image->assembly = const_cast<Il2CppAssembly*>(GetAssemblyFromIndex(imageAssemblyIndex));
std::string nameNoExt = il2cpp::utils::PathUtils::PathNoExtension(image->name);
image->nameNoExt = (char*)IL2CPP_CALLOC(nameNoExt.size() + 1, sizeof(char));
strcpy(const_cast<char*>(image->nameNoExt), nameNoExt.c_str());
for (uint32_t codeGenModuleIndex = 0; codeGenModuleIndex < s_Il2CppCodeRegistration->codeGenModulesCount; ++codeGenModuleIndex)
{
if (strcmp(image->name, s_Il2CppCodeRegistration->codeGenModules[codeGenModuleIndex]->moduleName) == 0)
image->codeGenModule = s_Il2CppCodeRegistration->codeGenModules[codeGenModuleIndex];
}
IL2CPP_ASSERT(image->codeGenModule);
image->dynamic = false;
}
// setup all the Il2CppAssemblies.
for (int32_t assemblyIndex = 0; assemblyIndex < s_ImagesCount; assemblyIndex++)
{
Il2CppAssembly* assembly = s_AssembliesTable + assemblyIndex;
ImageIndex assemblyImageIndex;
il2cpp::vm::GlobalMetadata::BuildIl2CppAssembly(assembly, assemblyIndex, &assemblyImageIndex);
assembly->image = il2cpp::vm::MetadataCache::GetImageFromIndex(assemblyImageIndex);
Assembly::Register(assembly);
}
InitializeUnresolvedSignatureTable();
#if IL2CPP_ENABLE_NATIVE_STACKTRACES
std::vector<MethodDefinitionKey> managedMethods;
il2cpp::vm::GlobalMetadata::GetAllManagedMethods(managedMethods);
il2cpp::utils::NativeSymbol::RegisterMethods(managedMethods);
#endif
return true;
}
void il2cpp::vm::MetadataCache::ExecuteEagerStaticClassConstructors()
{
for (int32_t i = 0; i < s_AssembliesCount; i++)
{
const Il2CppImage* image = s_AssembliesTable[i].image;
if (image->codeGenModule->staticConstructorTypeIndices != NULL)
{
TypeDefinitionIndex* indexPointer = image->codeGenModule->staticConstructorTypeIndices;
while (*indexPointer) // 0 terminated
{
Il2CppMetadataTypeHandle handle = GetTypeHandleFromIndex(image, *indexPointer);
Il2CppClass* klass = GlobalMetadata::GetTypeInfoFromHandle(handle);
Runtime::ClassInit(klass);
indexPointer++;
}
}
}
}
typedef void(*Il2CppModuleInitializerMethodPointer)(const MethodInfo*);
void il2cpp::vm::MetadataCache::ExecuteModuleInitializers()
{
for (int32_t i = 0; i < s_AssembliesCount; i++)
{
const Il2CppImage* image = s_AssembliesTable[i].image;
if (image->codeGenModule->moduleInitializer != NULL)
{
Il2CppModuleInitializerMethodPointer moduleInitializer = (Il2CppModuleInitializerMethodPointer)image->codeGenModule->moduleInitializer;
moduleInitializer(NULL);
}
}
}
void ClearGenericMethodTable()
{
s_MethodTableMap.clear();
}
void ClearWindowsRuntimeTypeNamesTables()
{
s_ClassToWindowsRuntimeTypeNameMap.clear();
}
void il2cpp::vm::MetadataCache::InitializeGuidToClassTable()
{
Il2CppInteropData* interopData = s_Il2CppCodeRegistration->interopData;
uint32_t interopDataCount = s_Il2CppCodeRegistration->interopDataCount;
std::vector<std::pair<const Il2CppGuid*, Il2CppClass*> > guidToNonImportClassMap;
guidToNonImportClassMap.reserve(interopDataCount);
for (uint32_t i = 0; i < interopDataCount; i++)
{
// It's important to check for non-import types because type projections will have identical GUIDs (e.g. IEnumerable<T> and IIterable<T>)
if (interopData[i].guid != NULL)
{
Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(interopData[i].type);
if (!klass->is_import_or_windows_runtime)
guidToNonImportClassMap.push_back(std::make_pair(interopData[i].guid, klass));
}
}
s_GuidToNonImportClassMap.assign(guidToNonImportClassMap);
}
// this is called later in the intialization cycle with more systems setup like GC
void il2cpp::vm::MetadataCache::InitializeGCSafe()
{
il2cpp::vm::GlobalMetadata::InitializeStringLiteralTable();
il2cpp::vm::GlobalMetadata::InitializeGenericMethodTable(s_MethodTableMap);
il2cpp::vm::GlobalMetadata::InitializeWindowsRuntimeTypeNamesTables(s_WindowsRuntimeTypeNameToClassMap, s_ClassToWindowsRuntimeTypeNameMap);
InitializeGuidToClassTable();
}
void ClearImageNames()
{
for (int32_t imageIndex = 0; imageIndex < s_ImagesCount; imageIndex++)
{
Il2CppImage* image = s_ImagesTable + imageIndex;
IL2CPP_FREE((void*)image->nameNoExt);
}
}
void il2cpp::vm::MetadataCache::Clear()
{
ClearGenericMethodTable();
ClearWindowsRuntimeTypeNamesTables();
delete s_pUnresolvedSignatureMap;
Assembly::ClearAllAssemblies();
ClearImageNames();
IL2CPP_FREE(s_ImagesTable);
s_ImagesTable = NULL;
s_ImagesCount = 0;
IL2CPP_FREE(s_AssembliesTable);
s_AssembliesTable = NULL;
s_AssembliesCount = 0;
metadata::ArrayMetadata::Clear();
s_GenericInstSet.clear();
s_Il2CppCodeRegistration = NULL;
s_Il2CppCodeGenOptions = NULL;
il2cpp::metadata::GenericMetadata::Clear();
il2cpp::metadata::GenericMethod::ClearStatics();
il2cpp::vm::GlobalMetadata::Clear();
}
void il2cpp::vm::MetadataCache::InitializeUnresolvedSignatureTable()
{
s_pUnresolvedSignatureMap = new Il2CppUnresolvedSignatureMap();
il2cpp::vm::GlobalMetadata::InitializeUnresolvedSignatureTable(*s_pUnresolvedSignatureMap);
}
Il2CppClass* il2cpp::vm::MetadataCache::GetGenericInstanceType(Il2CppClass* genericTypeDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount)
{
const Il2CppGenericInst* inst = il2cpp::vm::MetadataCache::GetGenericInst(genericArgumentTypes, genericArgumentCount);
Il2CppGenericClass* genericClass = il2cpp::metadata::GenericMetadata::GetGenericClass(genericTypeDefinition, inst);
return il2cpp::vm::GenericClass::GetClass(genericClass);
}
const MethodInfo* il2cpp::vm::MetadataCache::GetGenericInstanceMethod(const MethodInfo* genericMethodDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount)
{
Il2CppGenericContext context = { NULL, GetGenericInst(genericArgumentTypes, genericArgumentCount) };
return il2cpp::vm::GlobalMetadata::GetGenericInstanceMethod(genericMethodDefinition, &context);
}
const Il2CppGenericContext* il2cpp::vm::MetadataCache::GetMethodGenericContext(const MethodInfo* method)
{
if (!method->is_inflated)
{
IL2CPP_NOT_IMPLEMENTED(Image::GetMethodGenericContext);
return NULL;
}
return &method->genericMethod->context;
}
const MethodInfo* il2cpp::vm::MetadataCache::GetGenericMethodDefinition(const MethodInfo* method)
{
if (!method->is_inflated)
{
IL2CPP_NOT_IMPLEMENTED(Image::GetGenericMethodDefinition);
return NULL;
}
return method->genericMethod->methodDefinition;
}
Il2CppClass* il2cpp::vm::MetadataCache::GetPointerType(Il2CppClass* type)
{
Il2CppClass* pointerClass;
if (s_MetadataCache.m_PointerTypes.TryGet(type, &pointerClass))
return pointerClass;
return NULL;
}
Il2CppClass* il2cpp::vm::MetadataCache::GetWindowsRuntimeClass(const char* fullName)
{
WindowsRuntimeTypeNameToClassMap::iterator it = s_WindowsRuntimeTypeNameToClassMap.find(fullName);
if (it != s_WindowsRuntimeTypeNameToClassMap.end())
return it->second;
return NULL;
}
const char* il2cpp::vm::MetadataCache::GetWindowsRuntimeClassName(const Il2CppClass* klass)
{
ClassToWindowsRuntimeTypeNameMap::iterator it = s_ClassToWindowsRuntimeTypeNameMap.find(klass);
if (it != s_ClassToWindowsRuntimeTypeNameMap.end())
return it->second;
return NULL;
}
Il2CppMethodPointer il2cpp::vm::MetadataCache::GetWindowsRuntimeFactoryCreationFunction(const char* fullName)
{
Il2CppClass* klass = GetWindowsRuntimeClass(fullName);
if (klass == NULL)
return NULL;
WindowsRuntimeFactoryTable::iterator factoryEntry = s_WindowsRuntimeFactories.find_first(&klass->byval_arg);
if (factoryEntry == s_WindowsRuntimeFactories.end())
return NULL;
return factoryEntry->createFactoryFunction;
}
Il2CppClass* il2cpp::vm::MetadataCache::GetClassForGuid(const Il2CppGuid* guid)
{
IL2CPP_ASSERT(guid != NULL);
GuidToClassMap::iterator it = s_GuidToNonImportClassMap.find_first(guid);
if (it != s_GuidToNonImportClassMap.end())
return it->second;
return NULL;
}
void il2cpp::vm::MetadataCache::AddPointerTypeLocked(Il2CppClass* type, Il2CppClass* pointerType, const il2cpp::os::FastAutoLock& lock)
{
// This method must be called while holding the g_MetadataLock to ensure that we don't insert the same pointer type twice
// And WalkPointerTypes assumes this
IL2CPP_ASSERT(lock.IsLock(&g_MetadataLock));
s_MetadataCache.m_PointerTypes.Add(type, pointerType);
}
const Il2CppGenericInst* il2cpp::vm::MetadataCache::GetGenericInst(const Il2CppType* const* types, uint32_t typeCount)
{
// temporary inst to lookup a permanent one that may already exist
Il2CppGenericInst inst;
inst.type_argc = typeCount;
inst.type_argv = (const Il2CppType**)types;
il2cpp::os::FastAutoLock lock(&g_MetadataLock);
// Check if instance was added while we were blocked on g_MetadataLock
auto it = s_GenericInstSet.find(&inst);
if (it != s_GenericInstSet.end())
{
return *it;
}
Il2CppGenericInst* newInst = NULL;
newInst = (Il2CppGenericInst*)MetadataMalloc(sizeof(Il2CppGenericInst));
newInst->type_argc = typeCount;
newInst->type_argv = (const Il2CppType**)MetadataMalloc(newInst->type_argc * sizeof(Il2CppType*));
std::memcpy(newInst->type_argv, types, newInst->type_argc * sizeof(Il2CppType*));
// Do this while still holding the g_MetadataLock to prevent the same instance from being added twice
s_GenericInstSet.insert(newInst);
++il2cpp_runtime_stats.generic_instance_count;
return newInst;
}
static bool IsShareableEnum(const Il2CppType* type)
{
// Base case for recursion - we've found an enum.
if (il2cpp::vm::Type::IsEnum(type))
return true;
if (il2cpp::vm::Type::IsGenericInstance(type))
{
// Recursive case - look "inside" the generic instance type to see if this is a nested enum.
Il2CppClass* definition = il2cpp::vm::GenericClass::GetTypeDefinition(type->data.generic_class);
return IsShareableEnum(il2cpp::vm::Class::GetType(definition));
}
// Base case for recurion - this is not an enum or a generic instance type.
return false;
}
static il2cpp::vm::GenericParameterRestriction IsReferenceTypeGenericConstraint(const Il2CppType* constraint)
{
// This must match GenericSharingAnalsyis.GetGenericParameterConstraintRestriction()
if (constraint->type == IL2CPP_TYPE_VAR || constraint->type == IL2CPP_TYPE_MVAR)
return il2cpp::vm::GenericParameterRestrictionNone;
if (il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(constraint, &il2cpp_defaults.enum_class->byval_arg))
return il2cpp::vm::GenericParameterRestrictionValueType;
if (il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(constraint, &il2cpp_defaults.value_type_class->byval_arg))
return il2cpp::vm::GenericParameterRestrictionNone; // Not a valid constraint, so consider it unconstrained
else if (il2cpp::vm::Class::IsInterface(il2cpp::vm::Class::FromIl2CppType(constraint)))
return il2cpp::vm::GenericParameterRestrictionNone; // Interfaces constraints can be satisfied by reference or value types
// Any other type constraint e.g. T : SomeType, SomeType must be a reference type
return il2cpp::vm::GenericParameterRestrictionReferenceType;
}
il2cpp::vm::GenericParameterRestriction il2cpp::vm::MetadataCache::IsReferenceTypeGenericParameter(Il2CppMetadataGenericParameterHandle genericParameter)
{
uint16_t flags = il2cpp::vm::GlobalMetadata::GetGenericParameterFlags(genericParameter);
if ((flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0)
return GenericParameterRestrictionReferenceType;
if ((flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_NOT_NULLABLE_VALUE_TYPE_CONSTRAINT) != 0)
return GenericParameterRestrictionValueType; // Must be a value type
uint32_t count = il2cpp::vm::GlobalMetadata::GetGenericConstraintCount(genericParameter);
for (uint32_t constraintIndex = 0; constraintIndex < count; ++constraintIndex)
{
const Il2CppType* constraint = il2cpp::vm::GlobalMetadata::GetGenericParameterConstraintFromIndex(genericParameter, constraintIndex);
GenericParameterRestriction restriction = IsReferenceTypeGenericConstraint(constraint);
if (restriction != GenericParameterRestrictionNone)
return restriction;
}
return GenericParameterRestrictionNone;
}
static const Il2CppGenericInst* GetFullySharedInst(Il2CppMetadataGenericContainerHandle genericContainer, const Il2CppGenericInst* inst)
{
if (inst == NULL || !il2cpp::vm::Runtime::IsFullGenericSharingEnabled())
return NULL;
const Il2CppType** types = (const Il2CppType**)alloca(inst->type_argc * sizeof(Il2CppType*));
for (uint32_t i = 0; i < inst->type_argc; ++i)
{
const Il2CppType* type;
switch (il2cpp::vm::MetadataCache::IsReferenceTypeGenericParameter(il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndex(genericContainer, i)))
{
case il2cpp::vm::GenericParameterRestrictionValueType:
type = &il2cpp_defaults.il2cpp_fully_shared_struct_type->byval_arg;
break;
case il2cpp::vm::GenericParameterRestrictionReferenceType:
type = &il2cpp_defaults.object_class->byval_arg;
break;
default:
type = &il2cpp_defaults.il2cpp_fully_shared_type->byval_arg;
break;
}
types[i] = type;
}
const Il2CppGenericInst* sharedInst = il2cpp::vm::MetadataCache::GetGenericInst(types, inst->type_argc);
return sharedInst;
}
// this logic must match the C# logic in GenericSharingAnalysis.GetSharedTypeForGenericParameter
static const Il2CppGenericInst* GetSharedInst(const Il2CppGenericInst* inst)
{
if (inst == NULL)
return NULL;
const Il2CppType** types = (const Il2CppType**)alloca(inst->type_argc * sizeof(Il2CppType*));
for (uint32_t i = 0; i < inst->type_argc; ++i)
{
if (il2cpp::vm::Type::IsReference(inst->type_argv[i]))
types[i] = &il2cpp_defaults.object_class->byval_arg;
else
{
const Il2CppType* type = inst->type_argv[i];
if (s_Il2CppCodeGenOptions->enablePrimitiveValueTypeGenericSharing)
{
if (IsShareableEnum(type))
{
const Il2CppType* underlyingType = il2cpp::vm::Type::GetUnderlyingType(type);
switch (underlyingType->type)
{
case IL2CPP_TYPE_I1:
type = &il2cpp_defaults.sbyte_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_I2:
type = &il2cpp_defaults.int16_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_I4:
type = &il2cpp_defaults.int32_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_I8:
type = &il2cpp_defaults.int64_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_U1:
type = &il2cpp_defaults.byte_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_U2:
case IL2CPP_TYPE_CHAR:
type = &il2cpp_defaults.uint16_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_U4:
type = &il2cpp_defaults.uint32_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_U8:
type = &il2cpp_defaults.uint64_shared_enum->byval_arg;
break;
case IL2CPP_TYPE_I:
case IL2CPP_TYPE_U:
break;
default:
IL2CPP_ASSERT(0 && "Invalid enum underlying type");
break;
}
}
}
if (il2cpp::vm::Type::IsGenericInstance(type))
{
const Il2CppGenericInst* sharedInst = GetSharedInst(type->data.generic_class->context.class_inst);
Il2CppGenericClass* gklass = il2cpp::metadata::GenericMetadata::GetGenericClass(type->data.generic_class->type, sharedInst);
Il2CppClass* klass = il2cpp::vm::GenericClass::GetClass(gklass);
type = &klass->byval_arg;
}
types[i] = type;
}
}
const Il2CppGenericInst* sharedInst = il2cpp::vm::MetadataCache::GetGenericInst(types, inst->type_argc);
return sharedInst;
}
static il2cpp::vm::Il2CppGenericMethodPointers MakeGenericMethodPointers(const Il2CppGenericMethodIndices* methodIndicies, bool isFullyShared)
{
IL2CPP_ASSERT(methodIndicies->methodIndex >= 0 && (methodIndicies->invokerIndex >= 0 || methodIndicies->invokerIndex == kMethodIndexInvalid));
if (static_cast<uint32_t>(methodIndicies->methodIndex) < s_Il2CppCodeRegistration->genericMethodPointersCount && static_cast<uint32_t>(methodIndicies->invokerIndex) < s_Il2CppCodeRegistration->invokerPointersCount)
{
Il2CppMethodPointer virtualMethod;
Il2CppMethodPointer method;
method = s_Il2CppCodeRegistration->genericMethodPointers[methodIndicies->methodIndex];
if (methodIndicies->adjustorThunkIndex != -1)
{
virtualMethod = s_Il2CppCodeRegistration->genericAdjustorThunks[methodIndicies->adjustorThunkIndex];
}
else
{
virtualMethod = method;
}
InvokerMethod invokerMethod;
if (methodIndicies->invokerIndex == kMethodIndexInvalid)
invokerMethod = il2cpp::vm::Runtime::GetMissingMethodInvoker();
else
invokerMethod = s_Il2CppCodeRegistration->invokerPointers[methodIndicies->invokerIndex];
return { method, virtualMethod, invokerMethod, isFullyShared };
}
return { NULL, NULL, NULL, false };
}
il2cpp::vm::Il2CppGenericMethodPointers il2cpp::vm::MetadataCache::GetGenericMethodPointers(const MethodInfo* methodDefinition, const Il2CppGenericContext* context)
{
Il2CppGenericMethod method = { 0 };
method.methodDefinition = methodDefinition;
method.context.class_inst = context->class_inst;
method.context.method_inst = context->method_inst;
il2cpp::metadata::Il2CppMethodSpecOrGenericMethod specOrGeneric(&method);
Il2CppMethodTableMapIter iter = s_MethodTableMap.find(specOrGeneric);
if (iter != s_MethodTableMap.end())
return MakeGenericMethodPointers(iter->second, false);
// get the shared version if it exists
method.context.class_inst = GetSharedInst(context->class_inst);
method.context.method_inst = GetSharedInst(context->method_inst);
iter = s_MethodTableMap.find(specOrGeneric);
if (iter != s_MethodTableMap.end())
return MakeGenericMethodPointers(iter->second, false);
// get the fully shared version if it exists
method.context.class_inst = GetFullySharedInst(methodDefinition->klass->genericContainerHandle, context->class_inst);
method.context.method_inst = GetFullySharedInst(methodDefinition->genericContainerHandle, context->method_inst);
iter = s_MethodTableMap.find(specOrGeneric);
if (iter != s_MethodTableMap.end())
return MakeGenericMethodPointers(iter->second, true);
return { NULL, NULL, NULL };
}
const Il2CppType* il2cpp::vm::MetadataCache::GetIl2CppTypeFromIndex(const Il2CppImage* image, TypeIndex index)
{
return il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(index);
}
const Il2CppType* il2cpp::vm::MetadataCache::GetTypeFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
{
return il2cpp::vm::GlobalMetadata::GetTypeFromRgctxDefinition(rgctxDef);
}
Il2CppGenericMethod il2cpp::vm::MetadataCache::GetGenericMethodFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
{
return il2cpp::vm::GlobalMetadata::BuildGenericMethodFromRgctxDefinition(rgctxDef);
}
std::pair<const Il2CppType*, const MethodInfo*> il2cpp::vm::MetadataCache::GetConstrainedCallFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
{
return il2cpp::vm::GlobalMetadata::GetConstrainedCallFromRgctxDefinition(rgctxDef);
}
const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot)
{
return il2cpp::vm::GlobalMetadata::GetMethodInfoFromVTableSlot(klass, vTableSlot);
}
static int CompareIl2CppTokenAdjustorThunkPair(const void* pkey, const void* pelem)
{
return (int)(((Il2CppTokenAdjustorThunkPair*)pkey)->token - ((Il2CppTokenAdjustorThunkPair*)pelem)->token);
}
Il2CppMethodPointer il2cpp::vm::MetadataCache::GetAdjustorThunk(const Il2CppImage* image, uint32_t token)
{
if (hybridclr::metadata::IsInterpreterIndex(image->token))
{
return hybridclr::metadata::MetadataModule::GetAdjustorThunk(image, token);
}
if (image->codeGenModule->adjustorThunkCount == 0)
return NULL;
Il2CppTokenAdjustorThunkPair key;
memset(&key, 0, sizeof(Il2CppTokenAdjustorThunkPair));
key.token = token;
const Il2CppTokenAdjustorThunkPair* result = (const Il2CppTokenAdjustorThunkPair*)bsearch(&key, image->codeGenModule->adjustorThunks,
image->codeGenModule->adjustorThunkCount, sizeof(Il2CppTokenAdjustorThunkPair), CompareIl2CppTokenAdjustorThunkPair);
if (result == NULL)
return NULL;
return result->adjustorThunk;
}
Il2CppMethodPointer il2cpp::vm::MetadataCache::GetMethodPointer(const Il2CppImage* image, uint32_t token)
{
uint32_t rid = GetTokenRowId(token);
uint32_t table = GetTokenType(token);
if (rid == 0)
return NULL;
if (hybridclr::metadata::IsInterpreterImage(image))
{
return hybridclr::metadata::MetadataModule::GetMethodPointer(image, token);
}
IL2CPP_ASSERT(rid <= image->codeGenModule->methodPointerCount);
return image->codeGenModule->methodPointers[rid - 1];
}
InvokerMethod il2cpp::vm::MetadataCache::GetMethodInvoker(const Il2CppImage* image, uint32_t token)
{
uint32_t rid = GetTokenRowId(token);
uint32_t table = GetTokenType(token);
if (rid == 0)
return Runtime::GetMissingMethodInvoker();
if (hybridclr::metadata::IsInterpreterImage(image))
{
return hybridclr::metadata::MetadataModule::GetMethodInvoker(image, token);
}
int32_t index = image->codeGenModule->invokerIndices[rid - 1];
if (index == (uint32_t)kMethodIndexInvalid)
return Runtime::GetMissingMethodInvoker();
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_Il2CppCodeRegistration->invokerPointersCount);
return s_Il2CppCodeRegistration->invokerPointers[index];
}
const Il2CppInteropData* il2cpp::vm::MetadataCache::GetInteropDataForType(const Il2CppType* type)
{
IL2CPP_ASSERT(type != NULL);
InteropDataMap::iterator interopData = s_InteropData.find_first(type);
if (interopData == s_InteropData.end())
return NULL;
return interopData;
}
static bool MatchTokens(Il2CppTokenIndexMethodTuple key, Il2CppTokenIndexMethodTuple element)
{
return key.token < element.token;
}
static bool GenericInstancesMatch(const MethodInfo* method, const MethodInfo* matchingMethod)
{
if (method->genericMethod->context.class_inst != NULL && matchingMethod->genericMethod->context.class_inst != NULL)
{
if (!il2cpp::metadata::Il2CppGenericInstCompare::AreEqual(method->genericMethod->context.class_inst, matchingMethod->genericMethod->context.class_inst))
return false;
}
if (method->genericMethod->context.method_inst != NULL && matchingMethod->genericMethod->context.method_inst != NULL)
{
if (!il2cpp::metadata::Il2CppGenericInstCompare::AreEqual(method->genericMethod->context.method_inst, matchingMethod->genericMethod->context.method_inst))
return false;
}
return true;
}
Il2CppMethodPointer il2cpp::vm::MetadataCache::GetReversePInvokeWrapper(const Il2CppImage* image, const MethodInfo* method)
{
if (image->codeGenModule->reversePInvokeWrapperCount == 0)
return NULL;
// For each image (i.e. assembly), the reverse pinvoke wrapper indices are in an array sorted by
// metadata token. Each entry also might have the method metadata pointer, which is used to further
// find methods that have a matching metadata token.
Il2CppTokenIndexMethodTuple key;
memset(&key, 0, sizeof(Il2CppTokenIndexMethodTuple));
key.token = method->token;
// Binary search for a range which matches the metadata token.
auto begin = image->codeGenModule->reversePInvokeWrapperIndices;
auto end = image->codeGenModule->reversePInvokeWrapperIndices + image->codeGenModule->reversePInvokeWrapperCount;
auto matchingRange = std::equal_range(begin, end, key, &MatchTokens);
int32_t index = -1;
auto numberOfMatches = std::distance(matchingRange.first, matchingRange.second);
if (numberOfMatches == 1)
{
if (method->genericMethod == NULL)
{
// We found one non-generic method.
index = matchingRange.first->index;
}
else
{
// We found one generic method - let's make sure the class and method generic instances match. This reverse p/invoke
// wrapper might be for a different inflated generic instance.
const MethodInfo* possibleMatch = il2cpp::metadata::GenericMethod::GetMethod(il2cpp::vm::GlobalMetadata::BuildGenericMethodFromTokenMethodTuple(matchingRange.first));
if (possibleMatch->genericMethod != NULL && GenericInstancesMatch(method, possibleMatch))
index = matchingRange.first->index;
}
}
else if (numberOfMatches > 1)
{
// Multiple generic instance methods share the same token, since it is from the generic method definition.
// To find the proper method, look for the one with a matching method metadata pointer.
const Il2CppTokenIndexMethodTuple* currentMatch = matchingRange.first;
const Il2CppTokenIndexMethodTuple* lastMatch = matchingRange.second;
while (currentMatch != lastMatch)
{
// First, check the method metadata, and use it if it has been initialized.
// If not, let's fall back to the generic method.
const MethodInfo* possibleMatch = (const MethodInfo*)*currentMatch->method;
if (!il2cpp::vm::GlobalMetadata::IsRuntimeMetadataInitialized(possibleMatch))
possibleMatch = il2cpp::metadata::GenericMethod::GetMethod(il2cpp::vm::GlobalMetadata::BuildGenericMethodFromTokenMethodTuple(currentMatch));
if (possibleMatch == method)
{
index = currentMatch->index;
break;
}
currentMatch++;
}
}
if (index == -1)
return NULL;
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_Il2CppCodeRegistration->reversePInvokeWrapperCount);
return s_Il2CppCodeRegistration->reversePInvokeWrappers[index];
}
static const Il2CppType* GetReducedType(const Il2CppType* type)
{
if (type->byref)
return &il2cpp_defaults.object_class->byval_arg;
if (il2cpp::vm::Type::IsEnum(type))
type = il2cpp::vm::Type::GetUnderlyingType(type);
switch (type->type)
{
case IL2CPP_TYPE_BOOLEAN:
return &il2cpp_defaults.byte_class->byval_arg;
case IL2CPP_TYPE_CHAR:
return &il2cpp_defaults.uint16_class->byval_arg;
case IL2CPP_TYPE_BYREF:
case IL2CPP_TYPE_CLASS:
case IL2CPP_TYPE_OBJECT:
case IL2CPP_TYPE_STRING:
case IL2CPP_TYPE_ARRAY:
case IL2CPP_TYPE_SZARRAY:
return &il2cpp_defaults.object_class->byval_arg;
case IL2CPP_TYPE_GENERICINST:
if (il2cpp::vm::Type::IsValueType(type))
{
// We can't inflate a generic instance that contains generic arguments
if (il2cpp::metadata::GenericMetadata::ContainsGenericParameters(type))
return type;
const Il2CppGenericInst* sharedInst = GetSharedInst(type->data.generic_class->context.class_inst);
Il2CppGenericClass* gklass = il2cpp::metadata::GenericMetadata::GetGenericClass(type->data.generic_class->type, sharedInst);
Il2CppClass* klass = il2cpp::vm::GenericClass::GetClass(gklass);
return &klass->byval_arg;
}
return &il2cpp_defaults.object_class->byval_arg;
default:
return type;
}
}
il2cpp::vm::Il2CppUnresolvedCallStubs il2cpp::vm::MetadataCache::GetUnresovledCallStubs(const MethodInfo* method)
{
il2cpp::vm::Il2CppUnresolvedCallStubs stubs;
stubs.stubsFound = false;
il2cpp::metadata::Il2CppSignature signature;
signature.Count = method->parameters_count + 1;
signature.Types = (const Il2CppType**)alloca(signature.Count * sizeof(Il2CppType*));
signature.Types[0] = GetReducedType(method->return_type);
for (int i = 0; i < method->parameters_count; ++i)
signature.Types[i + 1] = GetReducedType(method->parameters[i]);
Il2CppUnresolvedSignatureMapIter it = s_pUnresolvedSignatureMap->find(signature);
if (it != s_pUnresolvedSignatureMap->end())
{
if (il2cpp::vm::Method::IsInstance(method))
{
stubs.methodPointer = s_Il2CppCodeRegistration->unresolvedInstanceCallPointers[it->second];
stubs.virtualMethodPointer = s_Il2CppCodeRegistration->unresolvedVirtualCallPointers[it->second];
stubs.stubsFound = true;
}
else
{
stubs.methodPointer = s_Il2CppCodeRegistration->unresolvedStaticCallPointers[it->second];
stubs.virtualMethodPointer = stubs.methodPointer;
stubs.stubsFound = true;
}
}
else
{
const MethodInfo* entryPointNotFoundMethod = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo();
stubs.methodPointer = entryPointNotFoundMethod->methodPointer;
stubs.virtualMethodPointer = entryPointNotFoundMethod->methodPointer;
}
return stubs;
}
const Il2CppAssembly* il2cpp::vm::MetadataCache::GetAssemblyFromIndex(AssemblyIndex index)
{
if (index == kGenericContainerIndexInvalid)
return NULL;
IL2CPP_ASSERT(index <= s_AssembliesCount);
return s_AssembliesTable + index;
}
const Il2CppAssembly* il2cpp::vm::MetadataCache::GetAssemblyByName(const char* nameToFind)
{
const char* assemblyName = hybridclr::GetAssemblyNameFromPath(nameToFind);
il2cpp::utils::VmStringUtils::CaseInsensitiveComparer comparer;
for (int i = 0; i < s_AssembliesCount; i++)
{
const Il2CppAssembly* assembly = s_AssembliesTable + i;
if (comparer(assembly->aname.name, assemblyName) || comparer(assembly->image->name, assemblyName))
return assembly;
}
il2cpp::os::FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
for (auto assembly : s_cliAssemblies)
{
if (comparer(assembly->aname.name, assemblyName) || comparer(assembly->image->name, assemblyName))
return assembly;
}
return nullptr;
}
void il2cpp::vm::MetadataCache::RegisterInterpreterAssembly(Il2CppAssembly* assembly)
{
// avoid register placeholder assembly twicely.
for (Il2CppAssembly* ass : s_cliAssemblies)
{
if (ass == assembly)
{
il2cpp::vm::Assembly::InvalidateAssemblyList();
return;
}
}
il2cpp::vm::Assembly::Register(assembly);
s_cliAssemblies.push_back(assembly);
}