MetadataCache.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. #include "il2cpp-config.h"
  2. #include "MetadataCache.h"
  3. #include "GlobalMetadata.h"
  4. #include <map>
  5. #include <limits>
  6. #include "il2cpp-tabledefs.h"
  7. #include "il2cpp-runtime-stats.h"
  8. #include "gc/GarbageCollector.h"
  9. #include "gc/GCHandle.h"
  10. #include "metadata/ArrayMetadata.h"
  11. #include "metadata/GenericMetadata.h"
  12. #include "metadata/GenericMethod.h"
  13. #include "os/Atomic.h"
  14. #include "os/Mutex.h"
  15. #include "utils/CallOnce.h"
  16. #include "utils/Collections.h"
  17. #include "utils/Il2CppHashSet.h"
  18. #include "utils/Memory.h"
  19. #include "utils/PathUtils.h"
  20. #include "vm/Assembly.h"
  21. #include "vm/Class.h"
  22. #include "vm/ClassInlines.h"
  23. #include "vm/GenericClass.h"
  24. #include "vm/MetadataAlloc.h"
  25. #include "vm/MetadataLoader.h"
  26. #include "vm/MetadataLock.h"
  27. #include "vm/Method.h"
  28. #include "vm/Object.h"
  29. #include "vm/Runtime.h"
  30. #include "vm/String.h"
  31. #include "vm/Type.h"
  32. #include "vm-utils/MethodDefinitionKey.h"
  33. #include "vm-utils/NativeSymbol.h"
  34. #include "Baselib.h"
  35. #include "Cpp/ReentrantLock.h"
  36. typedef Il2CppReaderWriterLockedHashMap<Il2CppClass*, Il2CppClass*> PointerTypeMap;
  37. typedef Il2CppHashSet<const Il2CppGenericMethod*, il2cpp::metadata::Il2CppGenericMethodHash, il2cpp::metadata::Il2CppGenericMethodCompare> Il2CppGenericMethodSet;
  38. typedef Il2CppGenericMethodSet::const_iterator Il2CppGenericMethodSetIter;
  39. struct Il2CppMetadataCache
  40. {
  41. il2cpp::os::FastReaderReaderWriterLock m_CacheLock;
  42. PointerTypeMap m_PointerTypes;
  43. };
  44. static Il2CppMetadataCache s_MetadataCache;
  45. static int32_t s_ImagesCount = 0;
  46. static Il2CppImage* s_ImagesTable = NULL;
  47. static int32_t s_AssembliesCount = 0;
  48. static Il2CppAssembly* s_AssembliesTable = NULL;
  49. typedef Il2CppReaderWriterLockedHashSet<const Il2CppGenericInst*, il2cpp::metadata::Il2CppGenericInstHash, il2cpp::metadata::Il2CppGenericInstCompare> Il2CppGenericInstSet;
  50. static Il2CppGenericInstSet s_GenericInstSet;
  51. typedef il2cpp::vm::Il2CppMethodTableMap::const_iterator Il2CppMethodTableMapIter;
  52. static il2cpp::vm::Il2CppMethodTableMap s_MethodTableMap;
  53. typedef il2cpp::vm::Il2CppUnresolvedSignatureMap::const_iterator Il2CppUnresolvedSignatureMapIter;
  54. static il2cpp::vm::Il2CppUnresolvedSignatureMap *s_pUnresolvedSignatureMap;
  55. typedef Il2CppHashMap<FieldInfo*, int32_t, il2cpp::utils::PointerHash<FieldInfo> > Il2CppThreadLocalStaticOffsetHashMap;
  56. typedef Il2CppThreadLocalStaticOffsetHashMap::iterator Il2CppThreadLocalStaticOffsetHashMapIter;
  57. static Il2CppThreadLocalStaticOffsetHashMap s_ThreadLocalStaticOffsetMap;
  58. static const Il2CppCodeRegistration * s_Il2CppCodeRegistration;
  59. static const Il2CppMetadataRegistration* s_MetadataCache_Il2CppMetadataRegistration;
  60. static const Il2CppCodeGenOptions* s_Il2CppCodeGenOptions;
  61. static il2cpp::vm::WindowsRuntimeTypeNameToClassMap s_WindowsRuntimeTypeNameToClassMap;
  62. static il2cpp::vm::ClassToWindowsRuntimeTypeNameMap s_ClassToWindowsRuntimeTypeNameMap;
  63. struct InteropDataToTypeConverter
  64. {
  65. inline const Il2CppType* operator()(const Il2CppInteropData& interopData) const
  66. {
  67. return interopData.type;
  68. }
  69. };
  70. typedef il2cpp::utils::collections::ArrayValueMap<const Il2CppType*, Il2CppInteropData, InteropDataToTypeConverter, il2cpp::metadata::Il2CppTypeLess, il2cpp::metadata::Il2CppTypeEqualityComparer> InteropDataMap;
  71. static InteropDataMap s_InteropData;
  72. struct WindowsRuntimeFactoryTableEntryToTypeConverter
  73. {
  74. inline const Il2CppType* operator()(const Il2CppWindowsRuntimeFactoryTableEntry& entry) const
  75. {
  76. return entry.type;
  77. }
  78. };
  79. typedef il2cpp::utils::collections::ArrayValueMap<const Il2CppType*, Il2CppWindowsRuntimeFactoryTableEntry, WindowsRuntimeFactoryTableEntryToTypeConverter, il2cpp::metadata::Il2CppTypeLess, il2cpp::metadata::Il2CppTypeEqualityComparer> WindowsRuntimeFactoryTable;
  80. static WindowsRuntimeFactoryTable s_WindowsRuntimeFactories;
  81. template<typename K, typename V>
  82. struct PairToKeyConverter
  83. {
  84. inline const K& operator()(const std::pair<K, V>& pair) const
  85. {
  86. return pair.first;
  87. }
  88. };
  89. typedef il2cpp::utils::collections::ArrayValueMap<const Il2CppGuid*, std::pair<const Il2CppGuid*, Il2CppClass*>, PairToKeyConverter<const Il2CppGuid*, Il2CppClass*> > GuidToClassMap;
  90. static GuidToClassMap s_GuidToNonImportClassMap;
  91. void il2cpp::vm::MetadataCache::Register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)
  92. {
  93. il2cpp::vm::GlobalMetadata::Register(codeRegistration, metadataRegistration, codeGenOptions);
  94. s_Il2CppCodeRegistration = codeRegistration;
  95. s_MetadataCache_Il2CppMetadataRegistration = metadataRegistration;
  96. s_Il2CppCodeGenOptions = codeGenOptions;
  97. }
  98. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromTypeIndex(const Il2CppImage *image, TypeIndex index)
  99. {
  100. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeIndex(index);
  101. }
  102. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromMethodDefinitionIndex(const Il2CppImage *image, MethodIndex index)
  103. {
  104. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(index);
  105. }
  106. const MethodInfo* il2cpp::vm::MetadataCache::GetAssemblyEntryPoint(const Il2CppImage* image)
  107. {
  108. return il2cpp::vm::GlobalMetadata::GetAssemblyEntryPoint(image);
  109. }
  110. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetAssemblyTypeHandle(const Il2CppImage* image, AssemblyTypeIndex index)
  111. {
  112. return il2cpp::vm::GlobalMetadata::GetAssemblyTypeHandle(image, index);
  113. }
  114. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetAssemblyExportedTypeHandle(const Il2CppImage* image, AssemblyExportedTypeIndex index)
  115. {
  116. return il2cpp::vm::GlobalMetadata::GetAssemblyExportedTypeHandle(image, index);
  117. }
  118. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromMethodHandle(Il2CppMetadataMethodDefinitionHandle handle)
  119. {
  120. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodHandle(handle);
  121. }
  122. bool il2cpp::vm::MetadataCache::Initialize()
  123. {
  124. if (!il2cpp::vm::GlobalMetadata::Initialize(&s_ImagesCount, &s_AssembliesCount))
  125. {
  126. return false;
  127. }
  128. il2cpp::metadata::GenericMetadata::RegisterGenericClasses(s_MetadataCache_Il2CppMetadataRegistration->genericClasses, s_MetadataCache_Il2CppMetadataRegistration->genericClassesCount);
  129. il2cpp::metadata::GenericMetadata::SetMaximumRuntimeGenericDepth(s_Il2CppCodeGenOptions->maximumRuntimeGenericDepth);
  130. il2cpp::metadata::GenericMetadata::SetGenericVirtualIterations(s_Il2CppCodeGenOptions->recursiveGenericIterations);
  131. s_GenericInstSet.Resize(s_MetadataCache_Il2CppMetadataRegistration->genericInstsCount);
  132. for (int32_t i = 0; i < s_MetadataCache_Il2CppMetadataRegistration->genericInstsCount; i++)
  133. {
  134. bool inserted = s_GenericInstSet.Add(s_MetadataCache_Il2CppMetadataRegistration->genericInsts[i]);
  135. IL2CPP_ASSERT(inserted);
  136. }
  137. s_InteropData.assign_external(s_Il2CppCodeRegistration->interopData, s_Il2CppCodeRegistration->interopDataCount);
  138. s_WindowsRuntimeFactories.assign_external(s_Il2CppCodeRegistration->windowsRuntimeFactoryTable, s_Il2CppCodeRegistration->windowsRuntimeFactoryCount);
  139. // Pre-allocate these arrays so we don't need to lock when reading later.
  140. // These arrays hold the runtime metadata representation for metadata explicitly
  141. // referenced during conversion. There is a corresponding table of same size
  142. // in the converted metadata, giving a description of runtime metadata to construct.
  143. s_ImagesTable = (Il2CppImage*)IL2CPP_CALLOC(s_ImagesCount, sizeof(Il2CppImage));
  144. s_AssembliesTable = (Il2CppAssembly*)IL2CPP_CALLOC(s_AssembliesCount, sizeof(Il2CppAssembly));
  145. // setup all the Il2CppImages. There are not many and it avoid locks later on
  146. for (int32_t imageIndex = 0; imageIndex < s_ImagesCount; imageIndex++)
  147. {
  148. Il2CppImage* image = s_ImagesTable + imageIndex;
  149. AssemblyIndex imageAssemblyIndex;
  150. il2cpp::vm::GlobalMetadata::BuildIl2CppImage(image, imageIndex, &imageAssemblyIndex);
  151. image->assembly = const_cast<Il2CppAssembly*>(GetAssemblyFromIndex(imageAssemblyIndex));
  152. std::string nameNoExt = il2cpp::utils::PathUtils::PathNoExtension(image->name);
  153. image->nameNoExt = (char*)IL2CPP_CALLOC(nameNoExt.size() + 1, sizeof(char));
  154. strcpy(const_cast<char*>(image->nameNoExt), nameNoExt.c_str());
  155. for (uint32_t codeGenModuleIndex = 0; codeGenModuleIndex < s_Il2CppCodeRegistration->codeGenModulesCount; ++codeGenModuleIndex)
  156. {
  157. if (strcmp(image->name, s_Il2CppCodeRegistration->codeGenModules[codeGenModuleIndex]->moduleName) == 0)
  158. image->codeGenModule = s_Il2CppCodeRegistration->codeGenModules[codeGenModuleIndex];
  159. }
  160. IL2CPP_ASSERT(image->codeGenModule);
  161. image->dynamic = false;
  162. }
  163. // setup all the Il2CppAssemblies.
  164. for (int32_t assemblyIndex = 0; assemblyIndex < s_ImagesCount; assemblyIndex++)
  165. {
  166. Il2CppAssembly* assembly = s_AssembliesTable + assemblyIndex;
  167. ImageIndex assemblyImageIndex;
  168. il2cpp::vm::GlobalMetadata::BuildIl2CppAssembly(assembly, assemblyIndex, &assemblyImageIndex);
  169. assembly->image = il2cpp::vm::MetadataCache::GetImageFromIndex(assemblyImageIndex);
  170. Assembly::Register(assembly);
  171. }
  172. InitializeUnresolvedSignatureTable();
  173. #if IL2CPP_ENABLE_NATIVE_STACKTRACES
  174. std::vector<MethodDefinitionKey> managedMethods;
  175. il2cpp::vm::GlobalMetadata::GetAllManagedMethods(managedMethods);
  176. il2cpp::utils::NativeSymbol::RegisterMethods(managedMethods);
  177. #endif
  178. return true;
  179. }
  180. void il2cpp::vm::MetadataCache::ExecuteEagerStaticClassConstructors()
  181. {
  182. for (int32_t i = 0; i < s_AssembliesCount; i++)
  183. {
  184. const Il2CppImage* image = s_AssembliesTable[i].image;
  185. if (image->codeGenModule->staticConstructorTypeIndices != NULL)
  186. {
  187. TypeDefinitionIndex* indexPointer = image->codeGenModule->staticConstructorTypeIndices;
  188. while (*indexPointer) // 0 terminated
  189. {
  190. Il2CppMetadataTypeHandle handle = GetTypeHandleFromIndex(image, *indexPointer);
  191. Il2CppClass* klass = GlobalMetadata::GetTypeInfoFromHandle(handle);
  192. Runtime::ClassInit(klass);
  193. indexPointer++;
  194. }
  195. }
  196. }
  197. }
  198. typedef void(*Il2CppModuleInitializerMethodPointer)(const MethodInfo*);
  199. void il2cpp::vm::MetadataCache::ExecuteModuleInitializers()
  200. {
  201. for (int32_t i = 0; i < s_AssembliesCount; i++)
  202. {
  203. const Il2CppImage* image = s_AssembliesTable[i].image;
  204. if (image->codeGenModule->moduleInitializer != NULL)
  205. {
  206. Il2CppModuleInitializerMethodPointer moduleInitializer = (Il2CppModuleInitializerMethodPointer)image->codeGenModule->moduleInitializer;
  207. moduleInitializer(NULL);
  208. }
  209. }
  210. }
  211. void ClearGenericMethodTable()
  212. {
  213. s_MethodTableMap.clear();
  214. }
  215. void ClearWindowsRuntimeTypeNamesTables()
  216. {
  217. s_ClassToWindowsRuntimeTypeNameMap.clear();
  218. }
  219. void il2cpp::vm::MetadataCache::InitializeGuidToClassTable()
  220. {
  221. Il2CppInteropData* interopData = s_Il2CppCodeRegistration->interopData;
  222. uint32_t interopDataCount = s_Il2CppCodeRegistration->interopDataCount;
  223. std::vector<std::pair<const Il2CppGuid*, Il2CppClass*> > guidToNonImportClassMap;
  224. guidToNonImportClassMap.reserve(interopDataCount);
  225. for (uint32_t i = 0; i < interopDataCount; i++)
  226. {
  227. // It's important to check for non-import types because type projections will have identical GUIDs (e.g. IEnumerable<T> and IIterable<T>)
  228. if (interopData[i].guid != NULL)
  229. {
  230. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(interopData[i].type);
  231. if (!klass->is_import_or_windows_runtime)
  232. guidToNonImportClassMap.push_back(std::make_pair(interopData[i].guid, klass));
  233. }
  234. }
  235. s_GuidToNonImportClassMap.assign(guidToNonImportClassMap);
  236. }
  237. // this is called later in the intialization cycle with more systems setup like GC
  238. void il2cpp::vm::MetadataCache::InitializeGCSafe()
  239. {
  240. il2cpp::vm::GlobalMetadata::InitializeStringLiteralTable();
  241. il2cpp::vm::GlobalMetadata::InitializeGenericMethodTable(s_MethodTableMap);
  242. il2cpp::vm::GlobalMetadata::InitializeWindowsRuntimeTypeNamesTables(s_WindowsRuntimeTypeNameToClassMap, s_ClassToWindowsRuntimeTypeNameMap);
  243. InitializeGuidToClassTable();
  244. }
  245. void ClearImageNames()
  246. {
  247. for (int32_t imageIndex = 0; imageIndex < s_ImagesCount; imageIndex++)
  248. {
  249. Il2CppImage* image = s_ImagesTable + imageIndex;
  250. IL2CPP_FREE((void*)image->nameNoExt);
  251. }
  252. }
  253. void il2cpp::vm::MetadataCache::AcquireMetadataLocks()
  254. {
  255. // Acquires "all" of the metadata locks
  256. // This will ensure that no other threads can be in metadata code
  257. // NOTE: Some of these locks are non-reentrant reader/writer locks so not everything is safe!
  258. g_MetadataLock.Acquire();
  259. s_MetadataCache.m_PointerTypes.LockExclusive();
  260. metadata::GenericMetadata::AcquireMetadataLocks();
  261. metadata::ArrayMetadata::AcquireMetadataLocks();
  262. metadata::GenericMethod::AcquireMetadataLocks();
  263. gc::GCHandle::AcquireMetadataLocks();
  264. }
  265. void il2cpp::vm::MetadataCache::ReleaseMetadataLocks()
  266. {
  267. gc::GCHandle::ReleaseMetadataLocks();
  268. metadata::GenericMethod::ReleaseMetadataLocks();
  269. metadata::ArrayMetadata::ReleaseMetadataLocks();
  270. metadata::GenericMetadata::ReleaseMetadataLocks();
  271. s_MetadataCache.m_PointerTypes.ReleaseExclusive();
  272. g_MetadataLock.Release();
  273. }
  274. void il2cpp::vm::MetadataCache::Clear()
  275. {
  276. ClearGenericMethodTable();
  277. ClearWindowsRuntimeTypeNamesTables();
  278. delete s_pUnresolvedSignatureMap;
  279. Assembly::ClearAllAssemblies();
  280. ClearImageNames();
  281. IL2CPP_FREE(s_ImagesTable);
  282. s_ImagesTable = NULL;
  283. s_ImagesCount = 0;
  284. IL2CPP_FREE(s_AssembliesTable);
  285. s_AssembliesTable = NULL;
  286. s_AssembliesCount = 0;
  287. metadata::ArrayMetadata::Clear();
  288. s_GenericInstSet.Clear();
  289. s_Il2CppCodeRegistration = NULL;
  290. s_Il2CppCodeGenOptions = NULL;
  291. il2cpp::metadata::GenericMetadata::Clear();
  292. il2cpp::metadata::GenericMethod::ClearStatics();
  293. il2cpp::vm::GlobalMetadata::Clear();
  294. }
  295. void il2cpp::vm::MetadataCache::InitializeUnresolvedSignatureTable()
  296. {
  297. s_pUnresolvedSignatureMap = new Il2CppUnresolvedSignatureMap();
  298. il2cpp::vm::GlobalMetadata::InitializeUnresolvedSignatureTable(*s_pUnresolvedSignatureMap);
  299. }
  300. Il2CppClass* il2cpp::vm::MetadataCache::GetGenericInstanceType(Il2CppClass* genericTypeDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount)
  301. {
  302. const Il2CppGenericInst* inst = il2cpp::vm::MetadataCache::GetGenericInst(genericArgumentTypes, genericArgumentCount);
  303. Il2CppGenericClass* genericClass = il2cpp::metadata::GenericMetadata::GetGenericClass(genericTypeDefinition, inst);
  304. return il2cpp::vm::GenericClass::GetClass(genericClass);
  305. }
  306. const MethodInfo* il2cpp::vm::MetadataCache::GetGenericInstanceMethod(const MethodInfo* genericMethodDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount)
  307. {
  308. Il2CppGenericContext context = { NULL, GetGenericInst(genericArgumentTypes, genericArgumentCount) };
  309. return il2cpp::vm::GlobalMetadata::GetGenericInstanceMethod(genericMethodDefinition, &context);
  310. }
  311. const Il2CppGenericContext* il2cpp::vm::MetadataCache::GetMethodGenericContext(const MethodInfo* method)
  312. {
  313. if (!method->is_inflated)
  314. {
  315. IL2CPP_NOT_IMPLEMENTED(Image::GetMethodGenericContext);
  316. return NULL;
  317. }
  318. return &method->genericMethod->context;
  319. }
  320. const MethodInfo* il2cpp::vm::MetadataCache::GetGenericMethodDefinition(const MethodInfo* method)
  321. {
  322. if (!method->is_inflated)
  323. {
  324. IL2CPP_NOT_IMPLEMENTED(Image::GetGenericMethodDefinition);
  325. return NULL;
  326. }
  327. return method->genericMethod->methodDefinition;
  328. }
  329. Il2CppClass* il2cpp::vm::MetadataCache::GetPointerType(Il2CppClass* type)
  330. {
  331. Il2CppClass* pointerClass;
  332. if (s_MetadataCache.m_PointerTypes.TryGet(type, &pointerClass))
  333. return pointerClass;
  334. return NULL;
  335. }
  336. Il2CppClass* il2cpp::vm::MetadataCache::GetWindowsRuntimeClass(const char* fullName)
  337. {
  338. WindowsRuntimeTypeNameToClassMap::iterator it = s_WindowsRuntimeTypeNameToClassMap.find(fullName);
  339. if (it != s_WindowsRuntimeTypeNameToClassMap.end())
  340. return it->second;
  341. return NULL;
  342. }
  343. const char* il2cpp::vm::MetadataCache::GetWindowsRuntimeClassName(const Il2CppClass* klass)
  344. {
  345. ClassToWindowsRuntimeTypeNameMap::iterator it = s_ClassToWindowsRuntimeTypeNameMap.find(klass);
  346. if (it != s_ClassToWindowsRuntimeTypeNameMap.end())
  347. return it->second;
  348. return NULL;
  349. }
  350. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetWindowsRuntimeFactoryCreationFunction(const char* fullName)
  351. {
  352. Il2CppClass* klass = GetWindowsRuntimeClass(fullName);
  353. if (klass == NULL)
  354. return NULL;
  355. WindowsRuntimeFactoryTable::iterator factoryEntry = s_WindowsRuntimeFactories.find_first(&klass->byval_arg);
  356. if (factoryEntry == s_WindowsRuntimeFactories.end())
  357. return NULL;
  358. return factoryEntry->createFactoryFunction;
  359. }
  360. Il2CppClass* il2cpp::vm::MetadataCache::GetClassForGuid(const Il2CppGuid* guid)
  361. {
  362. IL2CPP_ASSERT(guid != NULL);
  363. GuidToClassMap::iterator it = s_GuidToNonImportClassMap.find_first(guid);
  364. if (it != s_GuidToNonImportClassMap.end())
  365. return it->second;
  366. return NULL;
  367. }
  368. void il2cpp::vm::MetadataCache::AddPointerTypeLocked(Il2CppClass* type, Il2CppClass* pointerType, const il2cpp::os::FastAutoLock& lock)
  369. {
  370. // This method must be called while holding the g_MetadataLock to ensure that we don't insert the same pointer type twice
  371. // And WalkPointerTypes assumes this
  372. IL2CPP_ASSERT(lock.IsLock(&g_MetadataLock));
  373. s_MetadataCache.m_PointerTypes.Add(type, pointerType);
  374. }
  375. const Il2CppGenericInst* il2cpp::vm::MetadataCache::GetGenericInst(const Il2CppType* const* types, uint32_t typeCount)
  376. {
  377. // temporary inst to lookup a permanent one that may already exist
  378. Il2CppGenericInst inst;
  379. inst.type_argc = typeCount;
  380. inst.type_argv = (const Il2CppType**)types;
  381. const Il2CppGenericInst* foundInst;
  382. if (s_GenericInstSet.TryGet(&inst, &foundInst))
  383. return foundInst;
  384. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  385. // Check if instance was added while we were blocked on g_MetadataLock
  386. if (s_GenericInstSet.TryGet(&inst, &foundInst))
  387. return foundInst;
  388. Il2CppGenericInst* newInst = NULL;
  389. newInst = (Il2CppGenericInst*)MetadataMalloc(sizeof(Il2CppGenericInst));
  390. newInst->type_argc = typeCount;
  391. newInst->type_argv = (const Il2CppType**)MetadataMalloc(newInst->type_argc * sizeof(Il2CppType*));
  392. int index = 0;
  393. const Il2CppType* const* typesEnd = types + typeCount;
  394. for (const Il2CppType* const* iter = types; iter != typesEnd; ++iter, ++index)
  395. newInst->type_argv[index] = *iter;
  396. // Do this while still holding the g_MetadataLock to prevent the same instance from being added twice
  397. bool added = s_GenericInstSet.Add(newInst);
  398. IL2CPP_ASSERT(added);
  399. ++il2cpp_runtime_stats.generic_instance_count;
  400. return newInst;
  401. }
  402. static bool IsShareableEnum(const Il2CppType* type)
  403. {
  404. // Base case for recursion - we've found an enum.
  405. if (il2cpp::vm::Type::IsEnum(type))
  406. return true;
  407. if (il2cpp::vm::Type::IsGenericInstance(type))
  408. {
  409. // Recursive case - look "inside" the generic instance type to see if this is a nested enum.
  410. Il2CppClass* definition = il2cpp::vm::GenericClass::GetTypeDefinition(type->data.generic_class);
  411. return IsShareableEnum(il2cpp::vm::Class::GetType(definition));
  412. }
  413. // Base case for recurion - this is not an enum or a generic instance type.
  414. return false;
  415. }
  416. static il2cpp::vm::GenericParameterRestriction IsReferenceTypeGenericConstraint(const Il2CppType* constraint)
  417. {
  418. // This must match GenericSharingAnalsyis.GetGenericParameterConstraintRestriction()
  419. if (constraint->type == IL2CPP_TYPE_VAR || constraint->type == IL2CPP_TYPE_MVAR)
  420. return il2cpp::vm::GenericParameterRestrictionNone;
  421. if (il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(constraint, &il2cpp_defaults.enum_class->byval_arg))
  422. return il2cpp::vm::GenericParameterRestrictionValueType;
  423. if (il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(constraint, &il2cpp_defaults.value_type_class->byval_arg))
  424. return il2cpp::vm::GenericParameterRestrictionNone; // Not a valid constraint, so consider it unconstrained
  425. else if (il2cpp::vm::Class::IsInterface(il2cpp::vm::Class::FromIl2CppType(constraint)))
  426. return il2cpp::vm::GenericParameterRestrictionNone; // Interfaces constraints can be satisfied by reference or value types
  427. // Any other type constraint e.g. T : SomeType, SomeType must be a reference type
  428. return il2cpp::vm::GenericParameterRestrictionReferenceType;
  429. }
  430. il2cpp::vm::GenericParameterRestriction il2cpp::vm::MetadataCache::IsReferenceTypeGenericParameter(Il2CppMetadataGenericParameterHandle genericParameter)
  431. {
  432. uint16_t flags = il2cpp::vm::GlobalMetadata::GetGenericParameterFlags(genericParameter);
  433. if ((flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0)
  434. return GenericParameterRestrictionReferenceType;
  435. if ((flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_NOT_NULLABLE_VALUE_TYPE_CONSTRAINT) != 0)
  436. return GenericParameterRestrictionValueType; // Must be a value type
  437. uint32_t count = il2cpp::vm::GlobalMetadata::GetGenericConstraintCount(genericParameter);
  438. for (uint32_t constraintIndex = 0; constraintIndex < count; ++constraintIndex)
  439. {
  440. const Il2CppType* constraint = il2cpp::vm::GlobalMetadata::GetGenericParameterConstraintFromIndex(genericParameter, constraintIndex);
  441. GenericParameterRestriction restriction = IsReferenceTypeGenericConstraint(constraint);
  442. if (restriction != GenericParameterRestrictionNone)
  443. return restriction;
  444. }
  445. return GenericParameterRestrictionNone;
  446. }
  447. static const Il2CppGenericInst* GetFullySharedInst(Il2CppMetadataGenericContainerHandle genericContainer, const Il2CppGenericInst* inst)
  448. {
  449. if (inst == NULL || !il2cpp::vm::Runtime::IsFullGenericSharingEnabled())
  450. return NULL;
  451. const Il2CppType** types = (const Il2CppType**)alloca(inst->type_argc * sizeof(Il2CppType*));
  452. for (uint32_t i = 0; i < inst->type_argc; ++i)
  453. {
  454. const Il2CppType* type;
  455. switch (il2cpp::vm::MetadataCache::IsReferenceTypeGenericParameter(il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndex(genericContainer, i)))
  456. {
  457. case il2cpp::vm::GenericParameterRestrictionValueType:
  458. type = &il2cpp_defaults.il2cpp_fully_shared_struct_type->byval_arg;
  459. break;
  460. case il2cpp::vm::GenericParameterRestrictionReferenceType:
  461. type = &il2cpp_defaults.object_class->byval_arg;
  462. break;
  463. default:
  464. type = &il2cpp_defaults.il2cpp_fully_shared_type->byval_arg;
  465. break;
  466. }
  467. types[i] = type;
  468. }
  469. const Il2CppGenericInst* sharedInst = il2cpp::vm::MetadataCache::GetGenericInst(types, inst->type_argc);
  470. return sharedInst;
  471. }
  472. // this logic must match the C# logic in GenericSharingAnalysis.GetSharedTypeForGenericParameter
  473. static const Il2CppGenericInst* GetSharedInst(const Il2CppGenericInst* inst)
  474. {
  475. if (inst == NULL)
  476. return NULL;
  477. const Il2CppType** types = (const Il2CppType**)alloca(inst->type_argc * sizeof(Il2CppType*));
  478. for (uint32_t i = 0; i < inst->type_argc; ++i)
  479. {
  480. if (il2cpp::vm::Type::IsReference(inst->type_argv[i]))
  481. types[i] = &il2cpp_defaults.object_class->byval_arg;
  482. else
  483. {
  484. const Il2CppType* type = inst->type_argv[i];
  485. if (s_Il2CppCodeGenOptions->enablePrimitiveValueTypeGenericSharing)
  486. {
  487. if (IsShareableEnum(type))
  488. {
  489. const Il2CppType* underlyingType = il2cpp::vm::Type::GetUnderlyingType(type);
  490. switch (underlyingType->type)
  491. {
  492. case IL2CPP_TYPE_I1:
  493. type = &il2cpp_defaults.sbyte_shared_enum->byval_arg;
  494. break;
  495. case IL2CPP_TYPE_I2:
  496. type = &il2cpp_defaults.int16_shared_enum->byval_arg;
  497. break;
  498. case IL2CPP_TYPE_I4:
  499. type = &il2cpp_defaults.int32_shared_enum->byval_arg;
  500. break;
  501. case IL2CPP_TYPE_I8:
  502. type = &il2cpp_defaults.int64_shared_enum->byval_arg;
  503. break;
  504. case IL2CPP_TYPE_U1:
  505. type = &il2cpp_defaults.byte_shared_enum->byval_arg;
  506. break;
  507. case IL2CPP_TYPE_U2:
  508. case IL2CPP_TYPE_CHAR:
  509. type = &il2cpp_defaults.uint16_shared_enum->byval_arg;
  510. break;
  511. case IL2CPP_TYPE_U4:
  512. type = &il2cpp_defaults.uint32_shared_enum->byval_arg;
  513. break;
  514. case IL2CPP_TYPE_U8:
  515. type = &il2cpp_defaults.uint64_shared_enum->byval_arg;
  516. break;
  517. case IL2CPP_TYPE_I:
  518. case IL2CPP_TYPE_U:
  519. break;
  520. default:
  521. IL2CPP_ASSERT(0 && "Invalid enum underlying type");
  522. break;
  523. }
  524. }
  525. }
  526. if (il2cpp::vm::Type::IsGenericInstance(type))
  527. {
  528. const Il2CppGenericInst* sharedInst = GetSharedInst(type->data.generic_class->context.class_inst);
  529. Il2CppGenericClass* gklass = il2cpp::metadata::GenericMetadata::GetGenericClass(type->data.generic_class->type, sharedInst);
  530. Il2CppClass* klass = il2cpp::vm::GenericClass::GetClass(gklass);
  531. type = &klass->byval_arg;
  532. }
  533. types[i] = type;
  534. }
  535. }
  536. const Il2CppGenericInst* sharedInst = il2cpp::vm::MetadataCache::GetGenericInst(types, inst->type_argc);
  537. return sharedInst;
  538. }
  539. static il2cpp::vm::Il2CppGenericMethodPointers MakeGenericMethodPointers(const Il2CppGenericMethodIndices* methodIndicies, bool isFullyShared)
  540. {
  541. IL2CPP_ASSERT(methodIndicies->methodIndex >= 0 && (methodIndicies->invokerIndex >= 0 || methodIndicies->invokerIndex == kMethodIndexInvalid));
  542. if (static_cast<uint32_t>(methodIndicies->methodIndex) < s_Il2CppCodeRegistration->genericMethodPointersCount && static_cast<uint32_t>(methodIndicies->invokerIndex) < s_Il2CppCodeRegistration->invokerPointersCount)
  543. {
  544. Il2CppMethodPointer virtualMethod;
  545. Il2CppMethodPointer method;
  546. method = s_Il2CppCodeRegistration->genericMethodPointers[methodIndicies->methodIndex];
  547. if (methodIndicies->adjustorThunkIndex != -1)
  548. {
  549. virtualMethod = s_Il2CppCodeRegistration->genericAdjustorThunks[methodIndicies->adjustorThunkIndex];
  550. }
  551. else
  552. {
  553. virtualMethod = method;
  554. }
  555. InvokerMethod invokerMethod;
  556. if (methodIndicies->invokerIndex == kMethodIndexInvalid)
  557. invokerMethod = il2cpp::vm::Runtime::GetMissingMethodInvoker();
  558. else
  559. invokerMethod = s_Il2CppCodeRegistration->invokerPointers[methodIndicies->invokerIndex];
  560. return { method, virtualMethod, invokerMethod, isFullyShared };
  561. }
  562. return { NULL, NULL, NULL, false };
  563. }
  564. il2cpp::vm::Il2CppGenericMethodPointers il2cpp::vm::MetadataCache::GetGenericMethodPointers(const MethodInfo* methodDefinition, const Il2CppGenericContext* context)
  565. {
  566. Il2CppGenericMethod method = { 0 };
  567. method.methodDefinition = methodDefinition;
  568. method.context.class_inst = context->class_inst;
  569. method.context.method_inst = context->method_inst;
  570. il2cpp::metadata::Il2CppMethodSpecOrGenericMethod specOrGeneric(&method);
  571. Il2CppMethodTableMapIter iter = s_MethodTableMap.find(specOrGeneric);
  572. if (iter != s_MethodTableMap.end())
  573. return MakeGenericMethodPointers(iter->second, false);
  574. // get the shared version if it exists
  575. method.context.class_inst = GetSharedInst(context->class_inst);
  576. method.context.method_inst = GetSharedInst(context->method_inst);
  577. iter = s_MethodTableMap.find(specOrGeneric);
  578. if (iter != s_MethodTableMap.end())
  579. return MakeGenericMethodPointers(iter->second, false);
  580. // get the fully shared version if it exists
  581. method.context.class_inst = GetFullySharedInst(methodDefinition->klass->genericContainerHandle, context->class_inst);
  582. method.context.method_inst = GetFullySharedInst(methodDefinition->genericContainerHandle, context->method_inst);
  583. iter = s_MethodTableMap.find(specOrGeneric);
  584. if (iter != s_MethodTableMap.end())
  585. return MakeGenericMethodPointers(iter->second, true);
  586. return { NULL, NULL, NULL };
  587. }
  588. const Il2CppType* il2cpp::vm::MetadataCache::GetIl2CppTypeFromIndex(const Il2CppImage* image, TypeIndex index)
  589. {
  590. return il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(index);
  591. }
  592. const Il2CppType* il2cpp::vm::MetadataCache::GetTypeFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  593. {
  594. return il2cpp::vm::GlobalMetadata::GetTypeFromRgctxDefinition(rgctxDef);
  595. }
  596. Il2CppGenericMethod il2cpp::vm::MetadataCache::GetGenericMethodFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  597. {
  598. return il2cpp::vm::GlobalMetadata::BuildGenericMethodFromRgctxDefinition(rgctxDef);
  599. }
  600. std::pair<const Il2CppType*, const MethodInfo*> il2cpp::vm::MetadataCache::GetConstrainedCallFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  601. {
  602. return il2cpp::vm::GlobalMetadata::GetConstrainedCallFromRgctxDefinition(rgctxDef);
  603. }
  604. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot)
  605. {
  606. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromVTableSlot(klass, vTableSlot);
  607. }
  608. static int CompareIl2CppTokenAdjustorThunkPair(const void* pkey, const void* pelem)
  609. {
  610. return (int)(((Il2CppTokenAdjustorThunkPair*)pkey)->token - ((Il2CppTokenAdjustorThunkPair*)pelem)->token);
  611. }
  612. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetAdjustorThunk(const Il2CppImage* image, uint32_t token)
  613. {
  614. if (image->codeGenModule->adjustorThunkCount == 0)
  615. return NULL;
  616. Il2CppTokenAdjustorThunkPair key;
  617. memset(&key, 0, sizeof(Il2CppTokenAdjustorThunkPair));
  618. key.token = token;
  619. const Il2CppTokenAdjustorThunkPair* result = (const Il2CppTokenAdjustorThunkPair*)bsearch(&key, image->codeGenModule->adjustorThunks,
  620. image->codeGenModule->adjustorThunkCount, sizeof(Il2CppTokenAdjustorThunkPair), CompareIl2CppTokenAdjustorThunkPair);
  621. if (result == NULL)
  622. return NULL;
  623. return result->adjustorThunk;
  624. }
  625. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetMethodPointer(const Il2CppImage* image, uint32_t token)
  626. {
  627. uint32_t rid = GetTokenRowId(token);
  628. uint32_t table = GetTokenType(token);
  629. if (rid == 0)
  630. return NULL;
  631. IL2CPP_ASSERT(rid <= image->codeGenModule->methodPointerCount);
  632. return image->codeGenModule->methodPointers[rid - 1];
  633. }
  634. InvokerMethod il2cpp::vm::MetadataCache::GetMethodInvoker(const Il2CppImage* image, uint32_t token)
  635. {
  636. uint32_t rid = GetTokenRowId(token);
  637. uint32_t table = GetTokenType(token);
  638. if (rid == 0)
  639. return Runtime::GetMissingMethodInvoker();
  640. int32_t index = image->codeGenModule->invokerIndices[rid - 1];
  641. if (index == (uint32_t)kMethodIndexInvalid)
  642. return Runtime::GetMissingMethodInvoker();
  643. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_Il2CppCodeRegistration->invokerPointersCount);
  644. return s_Il2CppCodeRegistration->invokerPointers[index];
  645. }
  646. const Il2CppInteropData* il2cpp::vm::MetadataCache::GetInteropDataForType(const Il2CppType* type)
  647. {
  648. IL2CPP_ASSERT(type != NULL);
  649. InteropDataMap::iterator interopData = s_InteropData.find_first(type);
  650. if (interopData == s_InteropData.end())
  651. return NULL;
  652. return interopData;
  653. }
  654. static bool MatchTokens(Il2CppTokenIndexMethodTuple key, Il2CppTokenIndexMethodTuple element)
  655. {
  656. return key.token < element.token;
  657. }
  658. static bool GenericInstancesMatch(const MethodInfo* method, const MethodInfo* matchingMethod)
  659. {
  660. if (method->genericMethod->context.class_inst != NULL && matchingMethod->genericMethod->context.class_inst != NULL)
  661. {
  662. if (!il2cpp::metadata::Il2CppGenericInstCompare::AreEqual(method->genericMethod->context.class_inst, matchingMethod->genericMethod->context.class_inst))
  663. return false;
  664. }
  665. if (method->genericMethod->context.method_inst != NULL && matchingMethod->genericMethod->context.method_inst != NULL)
  666. {
  667. if (!il2cpp::metadata::Il2CppGenericInstCompare::AreEqual(method->genericMethod->context.method_inst, matchingMethod->genericMethod->context.method_inst))
  668. return false;
  669. }
  670. return true;
  671. }
  672. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetReversePInvokeWrapper(const Il2CppImage* image, const MethodInfo* method)
  673. {
  674. if (image->codeGenModule->reversePInvokeWrapperCount == 0)
  675. return NULL;
  676. // For each image (i.e. assembly), the reverse pinvoke wrapper indices are in an array sorted by
  677. // metadata token. Each entry also might have the method metadata pointer, which is used to further
  678. // find methods that have a matching metadata token.
  679. Il2CppTokenIndexMethodTuple key;
  680. memset(&key, 0, sizeof(Il2CppTokenIndexMethodTuple));
  681. key.token = method->token;
  682. // Binary search for a range which matches the metadata token.
  683. auto begin = image->codeGenModule->reversePInvokeWrapperIndices;
  684. auto end = image->codeGenModule->reversePInvokeWrapperIndices + image->codeGenModule->reversePInvokeWrapperCount;
  685. auto matchingRange = std::equal_range(begin, end, key, &MatchTokens);
  686. int32_t index = -1;
  687. auto numberOfMatches = std::distance(matchingRange.first, matchingRange.second);
  688. if (numberOfMatches == 1)
  689. {
  690. if (method->genericMethod == NULL)
  691. {
  692. // We found one non-generic method.
  693. index = matchingRange.first->index;
  694. }
  695. else
  696. {
  697. // We found one generic method - let's make sure the class and method generic instances match. This reverse p/invoke
  698. // wrapper might be for a different inflated generic instance.
  699. const MethodInfo* possibleMatch = il2cpp::metadata::GenericMethod::GetMethod(il2cpp::vm::GlobalMetadata::BuildGenericMethodFromTokenMethodTuple(matchingRange.first));
  700. if (possibleMatch->genericMethod != NULL && GenericInstancesMatch(method, possibleMatch))
  701. index = matchingRange.first->index;
  702. }
  703. }
  704. else if (numberOfMatches > 1)
  705. {
  706. // Multiple generic instance methods share the same token, since it is from the generic method definition.
  707. // To find the proper method, look for the one with a matching method metadata pointer.
  708. const Il2CppTokenIndexMethodTuple* currentMatch = matchingRange.first;
  709. const Il2CppTokenIndexMethodTuple* lastMatch = matchingRange.second;
  710. while (currentMatch != lastMatch)
  711. {
  712. // First, check the method metadata, and use it if it has been initialized.
  713. // If not, let's fall back to the generic method.
  714. const MethodInfo* possibleMatch = (const MethodInfo*)*currentMatch->method;
  715. if (!il2cpp::vm::GlobalMetadata::IsRuntimeMetadataInitialized(possibleMatch))
  716. possibleMatch = il2cpp::metadata::GenericMethod::GetMethod(il2cpp::vm::GlobalMetadata::BuildGenericMethodFromTokenMethodTuple(currentMatch));
  717. if (possibleMatch == method)
  718. {
  719. index = currentMatch->index;
  720. break;
  721. }
  722. currentMatch++;
  723. }
  724. }
  725. if (index == -1)
  726. return NULL;
  727. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_Il2CppCodeRegistration->reversePInvokeWrapperCount);
  728. return s_Il2CppCodeRegistration->reversePInvokeWrappers[index];
  729. }
  730. static const Il2CppType* GetReducedType(const Il2CppType* type)
  731. {
  732. if (type->byref)
  733. return &il2cpp_defaults.object_class->byval_arg;
  734. if (il2cpp::vm::Type::IsEnum(type))
  735. type = il2cpp::vm::Type::GetUnderlyingType(type);
  736. switch (type->type)
  737. {
  738. case IL2CPP_TYPE_BOOLEAN:
  739. return &il2cpp_defaults.byte_class->byval_arg;
  740. case IL2CPP_TYPE_CHAR:
  741. return &il2cpp_defaults.uint16_class->byval_arg;
  742. case IL2CPP_TYPE_BYREF:
  743. case IL2CPP_TYPE_CLASS:
  744. case IL2CPP_TYPE_OBJECT:
  745. case IL2CPP_TYPE_STRING:
  746. case IL2CPP_TYPE_ARRAY:
  747. case IL2CPP_TYPE_SZARRAY:
  748. return &il2cpp_defaults.object_class->byval_arg;
  749. case IL2CPP_TYPE_GENERICINST:
  750. if (il2cpp::vm::Type::IsValueType(type))
  751. {
  752. // We can't inflate a generic instance that contains generic arguments
  753. if (il2cpp::metadata::GenericMetadata::ContainsGenericParameters(type))
  754. return type;
  755. const Il2CppGenericInst* sharedInst = GetSharedInst(type->data.generic_class->context.class_inst);
  756. Il2CppGenericClass* gklass = il2cpp::metadata::GenericMetadata::GetGenericClass(type->data.generic_class->type, sharedInst);
  757. Il2CppClass* klass = il2cpp::vm::GenericClass::GetClass(gklass);
  758. return &klass->byval_arg;
  759. }
  760. return &il2cpp_defaults.object_class->byval_arg;
  761. default:
  762. return type;
  763. }
  764. }
  765. il2cpp::vm::Il2CppUnresolvedCallStubs il2cpp::vm::MetadataCache::GetUnresovledCallStubs(const MethodInfo* method)
  766. {
  767. il2cpp::vm::Il2CppUnresolvedCallStubs stubs;
  768. stubs.stubsFound = false;
  769. il2cpp::metadata::Il2CppSignature signature;
  770. signature.Count = method->parameters_count + 1;
  771. signature.Types = (const Il2CppType**)alloca(signature.Count * sizeof(Il2CppType*));
  772. signature.Types[0] = GetReducedType(method->return_type);
  773. for (int i = 0; i < method->parameters_count; ++i)
  774. signature.Types[i + 1] = GetReducedType(method->parameters[i]);
  775. Il2CppUnresolvedSignatureMapIter it = s_pUnresolvedSignatureMap->find(signature);
  776. if (it != s_pUnresolvedSignatureMap->end())
  777. {
  778. if (il2cpp::vm::Method::IsInstance(method))
  779. {
  780. stubs.methodPointer = s_Il2CppCodeRegistration->unresolvedInstanceCallPointers[it->second];
  781. stubs.virtualMethodPointer = s_Il2CppCodeRegistration->unresolvedVirtualCallPointers[it->second];
  782. stubs.stubsFound = true;
  783. }
  784. else
  785. {
  786. stubs.methodPointer = s_Il2CppCodeRegistration->unresolvedStaticCallPointers[it->second];
  787. stubs.virtualMethodPointer = stubs.methodPointer;
  788. stubs.stubsFound = true;
  789. }
  790. }
  791. else
  792. {
  793. const MethodInfo* entryPointNotFoundMethod = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo();
  794. stubs.methodPointer = entryPointNotFoundMethod->methodPointer;
  795. stubs.virtualMethodPointer = entryPointNotFoundMethod->methodPointer;
  796. }
  797. return stubs;
  798. }
  799. const Il2CppAssembly* il2cpp::vm::MetadataCache::GetAssemblyFromIndex(AssemblyIndex index)
  800. {
  801. if (index == kGenericContainerIndexInvalid)
  802. return NULL;
  803. IL2CPP_ASSERT(index <= s_AssembliesCount);
  804. return s_AssembliesTable + index;
  805. }
  806. const Il2CppAssembly* il2cpp::vm::MetadataCache::GetAssemblyByName(const char* nameToFind)
  807. {
  808. for (int i = 0; i < s_AssembliesCount; i++)
  809. {
  810. const Il2CppAssembly* assembly = s_AssembliesTable + i;
  811. const char* assemblyName = assembly->aname.name;
  812. if (strcmp(assemblyName, nameToFind) == 0)
  813. return assembly;
  814. }
  815. return NULL;
  816. }
  817. Il2CppImage* il2cpp::vm::MetadataCache::GetImageFromIndex(ImageIndex index)
  818. {
  819. if (index == kGenericContainerIndexInvalid)
  820. return NULL;
  821. IL2CPP_ASSERT(index <= s_ImagesCount);
  822. return s_ImagesTable + index;
  823. }
  824. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromType(const Il2CppType* type)
  825. {
  826. if (type == NULL)
  827. return NULL;
  828. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromType(type);
  829. }
  830. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromHandle(Il2CppMetadataTypeHandle handle)
  831. {
  832. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle(handle);
  833. }
  834. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromHandle_OnlyCached(Il2CppMetadataTypeHandle handle)
  835. {
  836. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle_OnlyCached(handle);
  837. }
  838. Il2CppMetadataGenericContainerHandle il2cpp::vm::MetadataCache::GetGenericContainerFromGenericClass(const Il2CppImage* image, const Il2CppGenericClass* genericClass)
  839. {
  840. return il2cpp::vm::GlobalMetadata::GetGenericContainerFromGenericClass(genericClass);
  841. }
  842. Il2CppMetadataGenericContainerHandle il2cpp::vm::MetadataCache::GetGenericContainerFromMethod(Il2CppMetadataMethodDefinitionHandle handle)
  843. {
  844. return il2cpp::vm::GlobalMetadata::GetGenericContainerFromMethod(handle);
  845. }
  846. Il2CppMetadataGenericParameterHandle il2cpp::vm::MetadataCache::GetGenericParameterFromType(const Il2CppType* type)
  847. {
  848. return il2cpp::vm::GlobalMetadata::GetGenericParameterFromType(type);
  849. }
  850. Il2CppClass* il2cpp::vm::MetadataCache::GetContainerDeclaringType(Il2CppMetadataGenericContainerHandle handle)
  851. {
  852. return il2cpp::vm::GlobalMetadata::GetContainerDeclaringType(handle);
  853. }
  854. Il2CppClass* il2cpp::vm::MetadataCache::GetParameterDeclaringType(Il2CppMetadataGenericParameterHandle handle)
  855. {
  856. return il2cpp::vm::GlobalMetadata::GetParameterDeclaringType(handle);
  857. }
  858. const MethodInfo* il2cpp::vm::MetadataCache::GetParameterDeclaringMethod(Il2CppMetadataGenericParameterHandle handle)
  859. {
  860. return il2cpp::vm::GlobalMetadata::GetParameterDeclaringMethod(handle);
  861. }
  862. Il2CppMetadataGenericParameterHandle il2cpp::vm::MetadataCache::GetGenericParameterFromIndex(Il2CppMetadataGenericContainerHandle handle, GenericContainerParameterIndex index)
  863. {
  864. return il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndex(handle, index);
  865. }
  866. const Il2CppType* il2cpp::vm::MetadataCache::GetGenericParameterConstraintFromIndex(Il2CppMetadataGenericParameterHandle handle, GenericParameterConstraintIndex index)
  867. {
  868. return il2cpp::vm::GlobalMetadata::GetGenericParameterConstraintFromIndex(handle, index);
  869. }
  870. Il2CppClass* il2cpp::vm::MetadataCache::GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset)
  871. {
  872. return il2cpp::vm::GlobalMetadata::GetNestedTypeFromOffset(klass, offset);
  873. }
  874. const Il2CppType* il2cpp::vm::MetadataCache::GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex offset)
  875. {
  876. return il2cpp::vm::GlobalMetadata::GetInterfaceFromOffset(klass, offset);
  877. }
  878. Il2CppInterfaceOffsetInfo il2cpp::vm::MetadataCache::GetInterfaceOffsetInfo(const Il2CppClass* klass, TypeInterfaceOffsetIndex index)
  879. {
  880. return il2cpp::vm::GlobalMetadata::GetInterfaceOffsetInfo(klass, index);
  881. }
  882. static int CompareIl2CppTokenRangePair(const void* pkey, const void* pelem)
  883. {
  884. return (int)(((Il2CppTokenRangePair*)pkey)->token - ((Il2CppTokenRangePair*)pelem)->token);
  885. }
  886. il2cpp::vm::RGCTXCollection il2cpp::vm::MetadataCache::GetRGCTXs(const Il2CppImage* image, uint32_t token)
  887. {
  888. RGCTXCollection collection = { 0, NULL };
  889. if (image->codeGenModule->rgctxRangesCount == 0)
  890. return collection;
  891. Il2CppTokenRangePair key;
  892. memset(&key, 0, sizeof(Il2CppTokenRangePair));
  893. key.token = token;
  894. const Il2CppTokenRangePair* res = (const Il2CppTokenRangePair*)bsearch(&key, image->codeGenModule->rgctxRanges, image->codeGenModule->rgctxRangesCount, sizeof(Il2CppTokenRangePair), CompareIl2CppTokenRangePair);
  895. if (res == NULL)
  896. return collection;
  897. collection.count = res->range.length;
  898. collection.items = image->codeGenModule->rgctxs + res->range.start;
  899. return collection;
  900. }
  901. const uint8_t* il2cpp::vm::MetadataCache::GetFieldDefaultValue(const FieldInfo* field, const Il2CppType** type)
  902. {
  903. return il2cpp::vm::GlobalMetadata::GetFieldDefaultValue(field, type);
  904. }
  905. const uint8_t* il2cpp::vm::MetadataCache::GetParameterDefaultValue(const MethodInfo* method, int32_t parameterPosition, const Il2CppType** type, bool* isExplicitySetNullDefaultValue)
  906. {
  907. return il2cpp::vm::GlobalMetadata::GetParameterDefaultValue(method, parameterPosition, type, isExplicitySetNullDefaultValue);
  908. }
  909. int il2cpp::vm::MetadataCache::GetFieldMarshaledSizeForField(const FieldInfo* field)
  910. {
  911. return il2cpp::vm::GlobalMetadata::GetFieldMarshaledSizeForField(field);
  912. }
  913. int32_t il2cpp::vm::MetadataCache::GetFieldOffsetFromIndexLocked(const Il2CppClass* klass, int32_t fieldIndexInType, FieldInfo* field, const il2cpp::os::FastAutoLock& lock)
  914. {
  915. int32_t offset = il2cpp::vm::GlobalMetadata::GetFieldOffset(klass, fieldIndexInType, field);
  916. if (offset < 0)
  917. {
  918. AddThreadLocalStaticOffsetForFieldLocked(field, offset & ~THREAD_LOCAL_STATIC_MASK, lock);
  919. return THREAD_STATIC_FIELD_OFFSET;
  920. }
  921. return offset;
  922. }
  923. void il2cpp::vm::MetadataCache::AddThreadLocalStaticOffsetForFieldLocked(FieldInfo* field, int32_t offset, const il2cpp::os::FastAutoLock& lock)
  924. {
  925. s_ThreadLocalStaticOffsetMap.add(field, offset);
  926. }
  927. int32_t il2cpp::vm::MetadataCache::GetThreadLocalStaticOffsetForField(FieldInfo* field)
  928. {
  929. IL2CPP_ASSERT(field->offset == THREAD_STATIC_FIELD_OFFSET);
  930. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  931. Il2CppThreadLocalStaticOffsetHashMapIter iter = s_ThreadLocalStaticOffsetMap.find(field);
  932. IL2CPP_ASSERT(iter != s_ThreadLocalStaticOffsetMap.end());
  933. return iter->second;
  934. }
  935. Il2CppMetadataCustomAttributeHandle il2cpp::vm::MetadataCache::GetCustomAttributeTypeToken(const Il2CppImage* image, uint32_t token)
  936. {
  937. return il2cpp::vm::GlobalMetadata::GetCustomAttributeTypeToken(image, token);
  938. }
  939. il2cpp::metadata::CustomAttributeDataReader il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(const Il2CppImage* image, uint32_t token)
  940. {
  941. return il2cpp::vm::GlobalMetadata::GetCustomAttributeDataReader(image, token);
  942. }
  943. il2cpp::metadata::CustomAttributeDataReader il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(Il2CppMetadataCustomAttributeHandle handle)
  944. {
  945. return il2cpp::vm::GlobalMetadata::GetCustomAttributeDataReader(handle);
  946. }
  947. const Il2CppAssembly* il2cpp::vm::MetadataCache::GetReferencedAssembly(const Il2CppAssembly* assembly, int32_t referencedAssemblyTableIndex)
  948. {
  949. return il2cpp::vm::GlobalMetadata::GetReferencedAssembly(assembly, referencedAssemblyTableIndex, s_AssembliesTable, s_AssembliesCount);
  950. }
  951. void il2cpp::vm::MetadataCache::InitializeAllMethodMetadata()
  952. {
  953. il2cpp::vm::GlobalMetadata::InitializeAllMethodMetadata();
  954. }
  955. void* il2cpp::vm::MetadataCache::InitializeRuntimeMetadata(uintptr_t* metadataPointer)
  956. {
  957. return il2cpp::vm::GlobalMetadata::InitializeRuntimeMetadata(metadataPointer, true);
  958. }
  959. void il2cpp::vm::MetadataCache::WalkPointerTypes(WalkTypesCallback callback, void* context)
  960. {
  961. s_MetadataCache.m_PointerTypes.LockShared();
  962. for (PointerTypeMap::iterator it = s_MetadataCache.m_PointerTypes.UnlockedBegin(); it != s_MetadataCache.m_PointerTypes.UnlockedEnd(); it++)
  963. {
  964. callback(it->second, context);
  965. }
  966. s_MetadataCache.m_PointerTypes.ReleaseShared();
  967. }
  968. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetTypeHandleFromIndex(const Il2CppImage* image, TypeDefinitionIndex typeIndex)
  969. {
  970. return il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(typeIndex);
  971. }
  972. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetTypeHandleFromType(const Il2CppType* type)
  973. {
  974. return il2cpp::vm::GlobalMetadata::GetTypeHandleFromType(type);
  975. }
  976. bool il2cpp::vm::MetadataCache::TypeIsNested(Il2CppMetadataTypeHandle handle)
  977. {
  978. return il2cpp::vm::GlobalMetadata::TypeIsNested(handle);
  979. }
  980. bool il2cpp::vm::MetadataCache::TypeIsValueType(Il2CppMetadataTypeHandle handle)
  981. {
  982. return il2cpp::vm::GlobalMetadata::TypeIsValueType(handle);
  983. }
  984. bool il2cpp::vm::MetadataCache::StructLayoutPackIsDefault(Il2CppMetadataTypeHandle handle)
  985. {
  986. return il2cpp::vm::GlobalMetadata::StructLayoutPackIsDefault(handle);
  987. }
  988. int32_t il2cpp::vm::MetadataCache::StructLayoutPack(Il2CppMetadataTypeHandle handle)
  989. {
  990. return il2cpp::vm::GlobalMetadata::StructLayoutPack(handle);
  991. }
  992. bool il2cpp::vm::MetadataCache::StructLayoutSizeIsDefault(Il2CppMetadataTypeHandle handle)
  993. {
  994. return il2cpp::vm::GlobalMetadata::StructLayoutSizeIsDefault(handle);
  995. }
  996. std::pair<const char*, const char*> il2cpp::vm::MetadataCache::GetTypeNamespaceAndName(Il2CppMetadataTypeHandle handle)
  997. {
  998. return il2cpp::vm::GlobalMetadata::GetTypeNamespaceAndName(handle);
  999. }
  1000. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetNestedTypes(Il2CppClass *klass, void* *iter)
  1001. {
  1002. return GetNestedTypes(
  1003. klass->typeMetadataHandle,
  1004. iter
  1005. );
  1006. }
  1007. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetNestedTypes(Il2CppMetadataTypeHandle handle, void** iter)
  1008. {
  1009. return il2cpp::vm::GlobalMetadata::GetNestedTypes(handle, iter);
  1010. }
  1011. Il2CppMetadataFieldInfo il2cpp::vm::MetadataCache::GetFieldInfo(const Il2CppClass* klass, TypeFieldIndex fieldIndex)
  1012. {
  1013. return il2cpp::vm::GlobalMetadata::GetFieldInfo(klass, fieldIndex);
  1014. }
  1015. Il2CppMetadataMethodInfo il2cpp::vm::MetadataCache::GetMethodInfo(const Il2CppClass* klass, TypeMethodIndex index)
  1016. {
  1017. return il2cpp::vm::GlobalMetadata::GetMethodInfo(klass, index);
  1018. }
  1019. Il2CppMetadataParameterInfo il2cpp::vm::MetadataCache::GetParameterInfo(const Il2CppClass* klass, Il2CppMetadataMethodDefinitionHandle handle, MethodParameterIndex paramIndex)
  1020. {
  1021. return il2cpp::vm::GlobalMetadata::GetParameterInfo(klass, handle, paramIndex);
  1022. }
  1023. Il2CppMetadataPropertyInfo il2cpp::vm::MetadataCache::GetPropertyInfo(const Il2CppClass* klass, TypePropertyIndex index)
  1024. {
  1025. return il2cpp::vm::GlobalMetadata::GetPropertyInfo(klass, index);
  1026. }
  1027. Il2CppMetadataEventInfo il2cpp::vm::MetadataCache::GetEventInfo(const Il2CppClass* klass, TypeEventIndex index)
  1028. {
  1029. return il2cpp::vm::GlobalMetadata::GetEventInfo(klass, index);
  1030. }
  1031. uint32_t il2cpp::vm::MetadataCache::GetReturnParameterToken(Il2CppMetadataMethodDefinitionHandle handle)
  1032. {
  1033. return il2cpp::vm::GlobalMetadata::GetReturnParameterToken(handle);
  1034. }
  1035. uint32_t il2cpp::vm::MetadataCache::GetGenericContainerCount(Il2CppMetadataGenericContainerHandle handle)
  1036. {
  1037. return il2cpp::vm::GlobalMetadata::GetGenericContainerCount(handle);
  1038. }
  1039. void il2cpp::vm::MetadataCache::MakeGenericArgType(Il2CppMetadataGenericContainerHandle containerHandle, Il2CppMetadataGenericParameterHandle paramHandle, Il2CppType* arg)
  1040. {
  1041. return il2cpp::vm::GlobalMetadata::MakeGenericArgType(containerHandle, paramHandle, arg);
  1042. }
  1043. bool il2cpp::vm::MetadataCache::GetGenericContainerIsMethod(Il2CppMetadataGenericContainerHandle handle)
  1044. {
  1045. return il2cpp::vm::GlobalMetadata::GetGenericContainerIsMethod(handle);
  1046. }
  1047. int16_t il2cpp::vm::MetadataCache::GetGenericConstraintCount(Il2CppMetadataGenericParameterHandle handle)
  1048. {
  1049. return il2cpp::vm::GlobalMetadata::GetGenericConstraintCount(handle);
  1050. }
  1051. const char* il2cpp::vm::MetadataCache::GetGenericParameterName(Il2CppMetadataGenericParameterHandle handle)
  1052. {
  1053. return il2cpp::vm::GlobalMetadata::GetGenericParameterName(handle);
  1054. }
  1055. Il2CppGenericParameterInfo il2cpp::vm::MetadataCache::GetGenericParameterInfo(Il2CppMetadataGenericParameterHandle handle)
  1056. {
  1057. return il2cpp::vm::GlobalMetadata::GetGenericParameterInfo(handle);
  1058. }
  1059. uint16_t il2cpp::vm::MetadataCache::GetGenericParameterFlags(Il2CppMetadataGenericParameterHandle handle)
  1060. {
  1061. return il2cpp::vm::GlobalMetadata::GetGenericParameterFlags(handle);
  1062. }
  1063. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromCatchPoint(const Il2CppImage* image, const Il2CppCatchPoint* cp)
  1064. {
  1065. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromCatchPoint(cp);
  1066. }
  1067. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromSequencePoint(const Il2CppImage* image, const Il2CppSequencePoint* seqPoint)
  1068. {
  1069. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromSequencePoint(seqPoint);
  1070. }
  1071. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromTypeSourcePair(const Il2CppImage* image, const Il2CppTypeSourceFilePair* pair)
  1072. {
  1073. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeSourcePair(pair);
  1074. }