MetadataCache.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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 "metadata/ArrayMetadata.h"
  10. #include "metadata/GenericMetadata.h"
  11. #include "metadata/GenericMethod.h"
  12. #include "os/Atomic.h"
  13. #include "os/Mutex.h"
  14. #include "utils/CallOnce.h"
  15. #include "utils/Collections.h"
  16. #include "utils/Il2CppHashSet.h"
  17. #include "utils/Memory.h"
  18. #include "utils/PathUtils.h"
  19. #include "vm/Assembly.h"
  20. #include "vm/Class.h"
  21. #include "vm/ClassInlines.h"
  22. #include "vm/GenericClass.h"
  23. #include "vm/MetadataAlloc.h"
  24. #include "vm/MetadataLoader.h"
  25. #include "vm/MetadataLock.h"
  26. #include "vm/Method.h"
  27. #include "vm/Object.h"
  28. #include "vm/Runtime.h"
  29. #include "vm/String.h"
  30. #include "vm/Type.h"
  31. #include "vm-utils/MethodDefinitionKey.h"
  32. #include "vm-utils/NativeSymbol.h"
  33. #include "Baselib.h"
  34. #include "Cpp/ReentrantLock.h"
  35. typedef Il2CppReaderWriterLockedHashMap<Il2CppClass*, Il2CppClass*> PointerTypeMap;
  36. typedef Il2CppHashSet<const Il2CppGenericMethod*, il2cpp::metadata::Il2CppGenericMethodHash, il2cpp::metadata::Il2CppGenericMethodCompare> Il2CppGenericMethodSet;
  37. typedef Il2CppGenericMethodSet::const_iterator Il2CppGenericMethodSetIter;
  38. static Il2CppGenericMethodSet s_GenericMethodSet;
  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::Clear()
  254. {
  255. ClearGenericMethodTable();
  256. ClearWindowsRuntimeTypeNamesTables();
  257. delete s_pUnresolvedSignatureMap;
  258. Assembly::ClearAllAssemblies();
  259. ClearImageNames();
  260. IL2CPP_FREE(s_ImagesTable);
  261. s_ImagesTable = NULL;
  262. s_ImagesCount = 0;
  263. IL2CPP_FREE(s_AssembliesTable);
  264. s_AssembliesTable = NULL;
  265. s_AssembliesCount = 0;
  266. s_GenericMethodSet.clear();
  267. metadata::ArrayMetadata::Clear();
  268. s_GenericInstSet.Clear();
  269. s_Il2CppCodeRegistration = NULL;
  270. s_Il2CppCodeGenOptions = NULL;
  271. il2cpp::metadata::GenericMetadata::Clear();
  272. il2cpp::metadata::GenericMethod::ClearStatics();
  273. il2cpp::vm::GlobalMetadata::Clear();
  274. }
  275. void il2cpp::vm::MetadataCache::InitializeUnresolvedSignatureTable()
  276. {
  277. s_pUnresolvedSignatureMap = new Il2CppUnresolvedSignatureMap();
  278. il2cpp::vm::GlobalMetadata::InitializeUnresolvedSignatureTable(*s_pUnresolvedSignatureMap);
  279. }
  280. Il2CppClass* il2cpp::vm::MetadataCache::GetGenericInstanceType(Il2CppClass* genericTypeDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount)
  281. {
  282. const Il2CppGenericInst* inst = il2cpp::vm::MetadataCache::GetGenericInst(genericArgumentTypes, genericArgumentCount);
  283. Il2CppGenericClass* genericClass = il2cpp::metadata::GenericMetadata::GetGenericClass(genericTypeDefinition, inst);
  284. return il2cpp::vm::GenericClass::GetClass(genericClass);
  285. }
  286. const MethodInfo* il2cpp::vm::MetadataCache::GetGenericInstanceMethod(const MethodInfo* genericMethodDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount)
  287. {
  288. Il2CppGenericContext context = { NULL, GetGenericInst(genericArgumentTypes, genericArgumentCount) };
  289. return il2cpp::vm::GlobalMetadata::GetGenericInstanceMethod(genericMethodDefinition, &context);
  290. }
  291. const Il2CppGenericContext* il2cpp::vm::MetadataCache::GetMethodGenericContext(const MethodInfo* method)
  292. {
  293. if (!method->is_inflated)
  294. {
  295. IL2CPP_NOT_IMPLEMENTED(Image::GetMethodGenericContext);
  296. return NULL;
  297. }
  298. return &method->genericMethod->context;
  299. }
  300. const MethodInfo* il2cpp::vm::MetadataCache::GetGenericMethodDefinition(const MethodInfo* method)
  301. {
  302. if (!method->is_inflated)
  303. {
  304. IL2CPP_NOT_IMPLEMENTED(Image::GetGenericMethodDefinition);
  305. return NULL;
  306. }
  307. return method->genericMethod->methodDefinition;
  308. }
  309. Il2CppClass* il2cpp::vm::MetadataCache::GetPointerType(Il2CppClass* type)
  310. {
  311. Il2CppClass* pointerClass;
  312. if (s_MetadataCache.m_PointerTypes.TryGet(type, &pointerClass))
  313. return pointerClass;
  314. return NULL;
  315. }
  316. Il2CppClass* il2cpp::vm::MetadataCache::GetWindowsRuntimeClass(const char* fullName)
  317. {
  318. WindowsRuntimeTypeNameToClassMap::iterator it = s_WindowsRuntimeTypeNameToClassMap.find(fullName);
  319. if (it != s_WindowsRuntimeTypeNameToClassMap.end())
  320. return it->second;
  321. return NULL;
  322. }
  323. const char* il2cpp::vm::MetadataCache::GetWindowsRuntimeClassName(const Il2CppClass* klass)
  324. {
  325. ClassToWindowsRuntimeTypeNameMap::iterator it = s_ClassToWindowsRuntimeTypeNameMap.find(klass);
  326. if (it != s_ClassToWindowsRuntimeTypeNameMap.end())
  327. return it->second;
  328. return NULL;
  329. }
  330. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetWindowsRuntimeFactoryCreationFunction(const char* fullName)
  331. {
  332. Il2CppClass* klass = GetWindowsRuntimeClass(fullName);
  333. if (klass == NULL)
  334. return NULL;
  335. WindowsRuntimeFactoryTable::iterator factoryEntry = s_WindowsRuntimeFactories.find_first(&klass->byval_arg);
  336. if (factoryEntry == s_WindowsRuntimeFactories.end())
  337. return NULL;
  338. return factoryEntry->createFactoryFunction;
  339. }
  340. Il2CppClass* il2cpp::vm::MetadataCache::GetClassForGuid(const Il2CppGuid* guid)
  341. {
  342. IL2CPP_ASSERT(guid != NULL);
  343. GuidToClassMap::iterator it = s_GuidToNonImportClassMap.find_first(guid);
  344. if (it != s_GuidToNonImportClassMap.end())
  345. return it->second;
  346. return NULL;
  347. }
  348. void il2cpp::vm::MetadataCache::AddPointerTypeLocked(Il2CppClass* type, Il2CppClass* pointerType, const il2cpp::os::FastAutoLock& lock)
  349. {
  350. // This method must be called while holding the g_MetadataLock to ensure that we don't insert the same pointer type twice
  351. // And WalkPointerTypes assumes this
  352. IL2CPP_ASSERT(lock.IsLock(&g_MetadataLock));
  353. s_MetadataCache.m_PointerTypes.Add(type, pointerType);
  354. }
  355. const Il2CppGenericInst* il2cpp::vm::MetadataCache::GetGenericInst(const Il2CppType* const* types, uint32_t typeCount)
  356. {
  357. // temporary inst to lookup a permanent one that may already exist
  358. Il2CppGenericInst inst;
  359. inst.type_argc = typeCount;
  360. inst.type_argv = (const Il2CppType**)types;
  361. const Il2CppGenericInst* foundInst;
  362. if (s_GenericInstSet.TryGet(&inst, &foundInst))
  363. return foundInst;
  364. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  365. // Check if instance was added while we were blocked on g_MetadataLock
  366. if (s_GenericInstSet.TryGet(&inst, &foundInst))
  367. return foundInst;
  368. Il2CppGenericInst* newInst = NULL;
  369. newInst = (Il2CppGenericInst*)MetadataMalloc(sizeof(Il2CppGenericInst));
  370. newInst->type_argc = typeCount;
  371. newInst->type_argv = (const Il2CppType**)MetadataMalloc(newInst->type_argc * sizeof(Il2CppType*));
  372. int index = 0;
  373. const Il2CppType* const* typesEnd = types + typeCount;
  374. for (const Il2CppType* const* iter = types; iter != typesEnd; ++iter, ++index)
  375. newInst->type_argv[index] = *iter;
  376. // Do this while still holding the g_MetadataLock to prevent the same instance from being added twice
  377. bool added = s_GenericInstSet.Add(newInst);
  378. IL2CPP_ASSERT(added);
  379. ++il2cpp_runtime_stats.generic_instance_count;
  380. return newInst;
  381. }
  382. static baselib::ReentrantLock s_GenericMethodMutex;
  383. const Il2CppGenericMethod* il2cpp::vm::MetadataCache::GetGenericMethod(const MethodInfo* methodDefinition, const Il2CppGenericInst* classInst, const Il2CppGenericInst* methodInst)
  384. {
  385. Il2CppGenericMethod method = { 0 };
  386. method.methodDefinition = methodDefinition;
  387. method.context.class_inst = classInst;
  388. method.context.method_inst = methodInst;
  389. il2cpp::os::FastAutoLock lock(&s_GenericMethodMutex);
  390. Il2CppGenericMethodSet::const_iterator iter = s_GenericMethodSet.find(&method);
  391. if (iter != s_GenericMethodSet.end())
  392. return *iter;
  393. Il2CppGenericMethod* newMethod = MetadataAllocGenericMethod();
  394. newMethod->methodDefinition = methodDefinition;
  395. newMethod->context.class_inst = classInst;
  396. newMethod->context.method_inst = methodInst;
  397. s_GenericMethodSet.insert(newMethod);
  398. return newMethod;
  399. }
  400. static bool IsShareableEnum(const Il2CppType* type)
  401. {
  402. // Base case for recursion - we've found an enum.
  403. if (il2cpp::vm::Type::IsEnum(type))
  404. return true;
  405. if (il2cpp::vm::Type::IsGenericInstance(type))
  406. {
  407. // Recursive case - look "inside" the generic instance type to see if this is a nested enum.
  408. Il2CppClass* definition = il2cpp::vm::GenericClass::GetTypeDefinition(type->data.generic_class);
  409. return IsShareableEnum(il2cpp::vm::Class::GetType(definition));
  410. }
  411. // Base case for recurion - this is not an enum or a generic instance type.
  412. return false;
  413. }
  414. static il2cpp::vm::GenericParameterRestriction IsReferenceTypeGenericConstraint(const Il2CppType* constraint)
  415. {
  416. // This must match GenericSharingAnalsyis.GetGenericParameterConstraintRestriction()
  417. if (constraint->type == IL2CPP_TYPE_VAR || constraint->type == IL2CPP_TYPE_MVAR)
  418. return il2cpp::vm::GenericParameterRestrictionNone;
  419. if (il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(constraint, &il2cpp_defaults.enum_class->byval_arg))
  420. return il2cpp::vm::GenericParameterRestrictionValueType;
  421. if (il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(constraint, &il2cpp_defaults.value_type_class->byval_arg))
  422. return il2cpp::vm::GenericParameterRestrictionNone; // Not a valid constraint, so consider it unconstrained
  423. else if (il2cpp::vm::Class::IsInterface(il2cpp::vm::Class::FromIl2CppType(constraint)))
  424. return il2cpp::vm::GenericParameterRestrictionNone; // Interfaces constraints can be satisfied by reference or value types
  425. // Any other type constraint e.g. T : SomeType, SomeType must be a reference type
  426. return il2cpp::vm::GenericParameterRestrictionReferenceType;
  427. }
  428. il2cpp::vm::GenericParameterRestriction il2cpp::vm::MetadataCache::IsReferenceTypeGenericParameter(Il2CppMetadataGenericParameterHandle genericParameter)
  429. {
  430. uint16_t flags = il2cpp::vm::GlobalMetadata::GetGenericParameterFlags(genericParameter);
  431. if ((flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0)
  432. return GenericParameterRestrictionReferenceType;
  433. if ((flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_NOT_NULLABLE_VALUE_TYPE_CONSTRAINT) != 0)
  434. return GenericParameterRestrictionValueType; // Must be a value type
  435. uint32_t count = il2cpp::vm::GlobalMetadata::GetGenericConstraintCount(genericParameter);
  436. for (uint32_t constraintIndex = 0; constraintIndex < count; ++constraintIndex)
  437. {
  438. const Il2CppType* constraint = il2cpp::vm::GlobalMetadata::GetGenericParameterConstraintFromIndex(genericParameter, constraintIndex);
  439. GenericParameterRestriction restriction = IsReferenceTypeGenericConstraint(constraint);
  440. if (restriction != GenericParameterRestrictionNone)
  441. return restriction;
  442. }
  443. return GenericParameterRestrictionNone;
  444. }
  445. static const Il2CppGenericInst* GetFullySharedInst(Il2CppMetadataGenericContainerHandle genericContainer, const Il2CppGenericInst* inst)
  446. {
  447. if (inst == NULL || !il2cpp::vm::Runtime::IsFullGenericSharingEnabled())
  448. return NULL;
  449. const Il2CppType** types = (const Il2CppType**)alloca(inst->type_argc * sizeof(Il2CppType*));
  450. for (uint32_t i = 0; i < inst->type_argc; ++i)
  451. {
  452. const Il2CppType* type;
  453. switch (il2cpp::vm::MetadataCache::IsReferenceTypeGenericParameter(il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndex(genericContainer, i)))
  454. {
  455. case il2cpp::vm::GenericParameterRestrictionValueType:
  456. type = &il2cpp_defaults.il2cpp_fully_shared_struct_type->byval_arg;
  457. break;
  458. case il2cpp::vm::GenericParameterRestrictionReferenceType:
  459. type = &il2cpp_defaults.object_class->byval_arg;
  460. break;
  461. default:
  462. type = &il2cpp_defaults.il2cpp_fully_shared_type->byval_arg;
  463. break;
  464. }
  465. types[i] = type;
  466. }
  467. const Il2CppGenericInst* sharedInst = il2cpp::vm::MetadataCache::GetGenericInst(types, inst->type_argc);
  468. return sharedInst;
  469. }
  470. // this logic must match the C# logic in GenericSharingAnalysis.GetSharedTypeForGenericParameter
  471. static const Il2CppGenericInst* GetSharedInst(const Il2CppGenericInst* inst)
  472. {
  473. if (inst == NULL)
  474. return NULL;
  475. const Il2CppType** types = (const Il2CppType**)alloca(inst->type_argc * sizeof(Il2CppType*));
  476. for (uint32_t i = 0; i < inst->type_argc; ++i)
  477. {
  478. if (il2cpp::vm::Type::IsReference(inst->type_argv[i]))
  479. types[i] = &il2cpp_defaults.object_class->byval_arg;
  480. else
  481. {
  482. const Il2CppType* type = inst->type_argv[i];
  483. if (s_Il2CppCodeGenOptions->enablePrimitiveValueTypeGenericSharing)
  484. {
  485. if (IsShareableEnum(type))
  486. {
  487. const Il2CppType* underlyingType = il2cpp::vm::Type::GetUnderlyingType(type);
  488. switch (underlyingType->type)
  489. {
  490. case IL2CPP_TYPE_I1:
  491. type = &il2cpp_defaults.sbyte_shared_enum->byval_arg;
  492. break;
  493. case IL2CPP_TYPE_I2:
  494. type = &il2cpp_defaults.int16_shared_enum->byval_arg;
  495. break;
  496. case IL2CPP_TYPE_I4:
  497. type = &il2cpp_defaults.int32_shared_enum->byval_arg;
  498. break;
  499. case IL2CPP_TYPE_I8:
  500. type = &il2cpp_defaults.int64_shared_enum->byval_arg;
  501. break;
  502. case IL2CPP_TYPE_U1:
  503. type = &il2cpp_defaults.byte_shared_enum->byval_arg;
  504. break;
  505. case IL2CPP_TYPE_U2:
  506. type = &il2cpp_defaults.uint16_shared_enum->byval_arg;
  507. break;
  508. case IL2CPP_TYPE_U4:
  509. type = &il2cpp_defaults.uint32_shared_enum->byval_arg;
  510. break;
  511. case IL2CPP_TYPE_U8:
  512. type = &il2cpp_defaults.uint64_shared_enum->byval_arg;
  513. break;
  514. default:
  515. IL2CPP_ASSERT(0 && "Invalid enum underlying type");
  516. break;
  517. }
  518. }
  519. }
  520. if (il2cpp::vm::Type::IsGenericInstance(type))
  521. {
  522. const Il2CppGenericInst* sharedInst = GetSharedInst(type->data.generic_class->context.class_inst);
  523. Il2CppGenericClass* gklass = il2cpp::metadata::GenericMetadata::GetGenericClass(type->data.generic_class->type, sharedInst);
  524. Il2CppClass* klass = il2cpp::vm::GenericClass::GetClass(gklass);
  525. type = &klass->byval_arg;
  526. }
  527. types[i] = type;
  528. }
  529. }
  530. const Il2CppGenericInst* sharedInst = il2cpp::vm::MetadataCache::GetGenericInst(types, inst->type_argc);
  531. return sharedInst;
  532. }
  533. static il2cpp::vm::Il2CppGenericMethodPointers MakeGenericMethodPointers(const Il2CppGenericMethodIndices* methodIndicies, bool isFullyShared)
  534. {
  535. IL2CPP_ASSERT(methodIndicies->methodIndex >= 0 && methodIndicies->invokerIndex >= 0);
  536. if (static_cast<uint32_t>(methodIndicies->methodIndex) < s_Il2CppCodeRegistration->genericMethodPointersCount && static_cast<uint32_t>(methodIndicies->invokerIndex) < s_Il2CppCodeRegistration->invokerPointersCount)
  537. {
  538. Il2CppMethodPointer virtualMethod;
  539. Il2CppMethodPointer method;
  540. method = s_Il2CppCodeRegistration->genericMethodPointers[methodIndicies->methodIndex];
  541. if (methodIndicies->adjustorThunkIndex != -1)
  542. {
  543. virtualMethod = s_Il2CppCodeRegistration->genericAdjustorThunks[methodIndicies->adjustorThunkIndex];
  544. }
  545. else
  546. {
  547. virtualMethod = method;
  548. }
  549. return { method, virtualMethod, s_Il2CppCodeRegistration->invokerPointers[methodIndicies->invokerIndex], isFullyShared };
  550. }
  551. return { NULL, NULL, NULL, false };
  552. }
  553. il2cpp::vm::Il2CppGenericMethodPointers il2cpp::vm::MetadataCache::GetGenericMethodPointers(const MethodInfo* methodDefinition, const Il2CppGenericContext* context)
  554. {
  555. Il2CppGenericMethod method = { 0 };
  556. method.methodDefinition = const_cast<MethodInfo*>(methodDefinition);
  557. method.context.class_inst = context->class_inst;
  558. method.context.method_inst = context->method_inst;
  559. Il2CppMethodTableMapIter iter = s_MethodTableMap.find(&method);
  560. if (iter != s_MethodTableMap.end())
  561. return MakeGenericMethodPointers(iter->second, false);
  562. // get the shared version if it exists
  563. method.context.class_inst = GetSharedInst(context->class_inst);
  564. method.context.method_inst = GetSharedInst(context->method_inst);
  565. iter = s_MethodTableMap.find(&method);
  566. if (iter != s_MethodTableMap.end())
  567. return MakeGenericMethodPointers(iter->second, false);
  568. // get the fully shared version if it exists
  569. method.context.class_inst = GetFullySharedInst(methodDefinition->klass->genericContainerHandle, context->class_inst);
  570. method.context.method_inst = GetFullySharedInst(methodDefinition->genericContainerHandle, context->method_inst);
  571. iter = s_MethodTableMap.find(&method);
  572. if (iter != s_MethodTableMap.end())
  573. return MakeGenericMethodPointers(iter->second, true);
  574. return { NULL, NULL, NULL };
  575. }
  576. const Il2CppType* il2cpp::vm::MetadataCache::GetIl2CppTypeFromIndex(const Il2CppImage* image, TypeIndex index)
  577. {
  578. return il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(index);
  579. }
  580. const Il2CppType* il2cpp::vm::MetadataCache::GetTypeFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  581. {
  582. return il2cpp::vm::GlobalMetadata::GetTypeFromRgctxDefinition(rgctxDef);
  583. }
  584. const Il2CppGenericMethod* il2cpp::vm::MetadataCache::GetGenericMethodFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  585. {
  586. return il2cpp::vm::GlobalMetadata::GetGenericMethodFromRgctxDefinition(rgctxDef);
  587. }
  588. std::pair<const Il2CppType*, const MethodInfo*> il2cpp::vm::MetadataCache::GetConstrainedCallFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  589. {
  590. return il2cpp::vm::GlobalMetadata::GetConstrainedCallFromRgctxDefinition(rgctxDef);
  591. }
  592. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot)
  593. {
  594. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromVTableSlot(klass, vTableSlot);
  595. }
  596. static int CompareIl2CppTokenAdjustorThunkPair(const void* pkey, const void* pelem)
  597. {
  598. return (int)(((Il2CppTokenAdjustorThunkPair*)pkey)->token - ((Il2CppTokenAdjustorThunkPair*)pelem)->token);
  599. }
  600. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetAdjustorThunk(const Il2CppImage* image, uint32_t token)
  601. {
  602. if (image->codeGenModule->adjustorThunkCount == 0)
  603. return NULL;
  604. Il2CppTokenAdjustorThunkPair key;
  605. memset(&key, 0, sizeof(Il2CppTokenAdjustorThunkPair));
  606. key.token = token;
  607. const Il2CppTokenAdjustorThunkPair* result = (const Il2CppTokenAdjustorThunkPair*)bsearch(&key, image->codeGenModule->adjustorThunks,
  608. image->codeGenModule->adjustorThunkCount, sizeof(Il2CppTokenAdjustorThunkPair), CompareIl2CppTokenAdjustorThunkPair);
  609. if (result == NULL)
  610. return NULL;
  611. return result->adjustorThunk;
  612. }
  613. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetMethodPointer(const Il2CppImage* image, uint32_t token)
  614. {
  615. uint32_t rid = GetTokenRowId(token);
  616. uint32_t table = GetTokenType(token);
  617. if (rid == 0)
  618. return NULL;
  619. IL2CPP_ASSERT(rid <= image->codeGenModule->methodPointerCount);
  620. return image->codeGenModule->methodPointers[rid - 1];
  621. }
  622. InvokerMethod il2cpp::vm::MetadataCache::GetMethodInvoker(const Il2CppImage* image, uint32_t token)
  623. {
  624. uint32_t rid = GetTokenRowId(token);
  625. uint32_t table = GetTokenType(token);
  626. if (rid == 0)
  627. return Runtime::GetMissingMethodInvoker();
  628. int32_t index = image->codeGenModule->invokerIndices[rid - 1];
  629. if (index == kMethodIndexInvalid)
  630. return Runtime::GetMissingMethodInvoker();
  631. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_Il2CppCodeRegistration->invokerPointersCount);
  632. return s_Il2CppCodeRegistration->invokerPointers[index];
  633. }
  634. const Il2CppInteropData* il2cpp::vm::MetadataCache::GetInteropDataForType(const Il2CppType* type)
  635. {
  636. IL2CPP_ASSERT(type != NULL);
  637. InteropDataMap::iterator interopData = s_InteropData.find_first(type);
  638. if (interopData == s_InteropData.end())
  639. return NULL;
  640. return interopData;
  641. }
  642. static bool MatchTokens(Il2CppTokenIndexMethodTuple key, Il2CppTokenIndexMethodTuple element)
  643. {
  644. return key.token < element.token;
  645. }
  646. static bool GenericInstancesMatch(const MethodInfo* method, const MethodInfo* matchingMethod)
  647. {
  648. if (method->genericMethod->context.class_inst != NULL && matchingMethod->genericMethod->context.class_inst != NULL)
  649. {
  650. if (!il2cpp::metadata::Il2CppGenericInstCompare::AreEqual(method->genericMethod->context.class_inst, matchingMethod->genericMethod->context.class_inst))
  651. return false;
  652. }
  653. if (method->genericMethod->context.method_inst != NULL && matchingMethod->genericMethod->context.method_inst != NULL)
  654. {
  655. if (!il2cpp::metadata::Il2CppGenericInstCompare::AreEqual(method->genericMethod->context.method_inst, matchingMethod->genericMethod->context.method_inst))
  656. return false;
  657. }
  658. return true;
  659. }
  660. Il2CppMethodPointer il2cpp::vm::MetadataCache::GetReversePInvokeWrapper(const Il2CppImage* image, const MethodInfo* method)
  661. {
  662. if (image->codeGenModule->reversePInvokeWrapperCount == 0)
  663. return NULL;
  664. // For each image (i.e. assembly), the reverse pinvoke wrapper indices are in an array sorted by
  665. // metadata token. Each entry also might have the method metadata pointer, which is used to further
  666. // find methods that have a matching metadata token.
  667. Il2CppTokenIndexMethodTuple key;
  668. memset(&key, 0, sizeof(Il2CppTokenIndexMethodTuple));
  669. key.token = method->token;
  670. // Binary search for a range which matches the metadata token.
  671. auto begin = image->codeGenModule->reversePInvokeWrapperIndices;
  672. auto end = image->codeGenModule->reversePInvokeWrapperIndices + image->codeGenModule->reversePInvokeWrapperCount;
  673. auto matchingRange = std::equal_range(begin, end, key, &MatchTokens);
  674. int32_t index = -1;
  675. auto numberOfMatches = std::distance(matchingRange.first, matchingRange.second);
  676. if (numberOfMatches == 1)
  677. {
  678. if (method->genericMethod == NULL)
  679. {
  680. // We found one non-generic method.
  681. index = matchingRange.first->index;
  682. }
  683. else
  684. {
  685. // We found one generic method - let's make sure the class and method generic instances match. This reverse p/invoke
  686. // wrapper might be for a different inflated generic instance.
  687. const MethodInfo* possibleMatch = il2cpp::metadata::GenericMethod::GetMethod(il2cpp::vm::GlobalMetadata::GetGenericMethodFromTokenMethodTuple(matchingRange.first));
  688. if (possibleMatch->genericMethod != NULL && GenericInstancesMatch(method, possibleMatch))
  689. index = matchingRange.first->index;
  690. }
  691. }
  692. else if (numberOfMatches > 1)
  693. {
  694. // Multiple generic instance methods share the same token, since it is from the generic method definition.
  695. // To find the proper method, look for the one with a matching method metadata pointer.
  696. const Il2CppTokenIndexMethodTuple* currentMatch = matchingRange.first;
  697. const Il2CppTokenIndexMethodTuple* lastMatch = matchingRange.second;
  698. while (currentMatch != lastMatch)
  699. {
  700. // First, check the method metadata, and use it if it has been initialized.
  701. // If not, let's fall back to the generic method.
  702. const MethodInfo* possibleMatch = (const MethodInfo*)*currentMatch->method;
  703. if (!il2cpp::vm::GlobalMetadata::IsRuntimeMetadataInitialized(possibleMatch))
  704. possibleMatch = il2cpp::metadata::GenericMethod::GetMethod(il2cpp::vm::GlobalMetadata::GetGenericMethodFromTokenMethodTuple(currentMatch));
  705. if (possibleMatch == method)
  706. {
  707. index = currentMatch->index;
  708. break;
  709. }
  710. currentMatch++;
  711. }
  712. }
  713. if (index == -1)
  714. return NULL;
  715. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_Il2CppCodeRegistration->reversePInvokeWrapperCount);
  716. return s_Il2CppCodeRegistration->reversePInvokeWrappers[index];
  717. }
  718. static const Il2CppType* GetReducedType(const Il2CppType* type)
  719. {
  720. if (type->byref)
  721. return &il2cpp_defaults.object_class->byval_arg;
  722. if (il2cpp::vm::Type::IsEnum(type))
  723. type = il2cpp::vm::Type::GetUnderlyingType(type);
  724. switch (type->type)
  725. {
  726. case IL2CPP_TYPE_BOOLEAN:
  727. return &il2cpp_defaults.byte_class->byval_arg;
  728. case IL2CPP_TYPE_CHAR:
  729. return &il2cpp_defaults.uint16_class->byval_arg;
  730. case IL2CPP_TYPE_BYREF:
  731. case IL2CPP_TYPE_CLASS:
  732. case IL2CPP_TYPE_OBJECT:
  733. case IL2CPP_TYPE_STRING:
  734. case IL2CPP_TYPE_ARRAY:
  735. case IL2CPP_TYPE_SZARRAY:
  736. return &il2cpp_defaults.object_class->byval_arg;
  737. case IL2CPP_TYPE_GENERICINST:
  738. if (il2cpp::vm::Type::GenericInstIsValuetype(type))
  739. {
  740. // We can't inflate a generic instance that contains generic arguments
  741. if (il2cpp::metadata::GenericMetadata::ContainsGenericParameters(type))
  742. return type;
  743. const Il2CppGenericInst* sharedInst = GetSharedInst(type->data.generic_class->context.class_inst);
  744. Il2CppGenericClass* gklass = il2cpp::metadata::GenericMetadata::GetGenericClass(type->data.generic_class->type, sharedInst);
  745. Il2CppClass* klass = il2cpp::vm::GenericClass::GetClass(gklass);
  746. return &klass->byval_arg;
  747. }
  748. return &il2cpp_defaults.object_class->byval_arg;
  749. default:
  750. return type;
  751. }
  752. }
  753. il2cpp::vm::Il2CppUnresolvedCallStubs il2cpp::vm::MetadataCache::GetUnresovledCallStubs(const MethodInfo* method)
  754. {
  755. il2cpp::vm::Il2CppUnresolvedCallStubs stubs;
  756. stubs.stubsFound = false;
  757. il2cpp::metadata::Il2CppSignature signature;
  758. signature.Count = method->parameters_count + 1;
  759. signature.Types = (const Il2CppType**)alloca(signature.Count * sizeof(Il2CppType*));
  760. signature.Types[0] = GetReducedType(method->return_type);
  761. for (int i = 0; i < method->parameters_count; ++i)
  762. signature.Types[i + 1] = GetReducedType(method->parameters[i]);
  763. Il2CppUnresolvedSignatureMapIter it = s_pUnresolvedSignatureMap->find(signature);
  764. if (it != s_pUnresolvedSignatureMap->end())
  765. {
  766. if (il2cpp::vm::Method::IsInstance(method))
  767. {
  768. stubs.methodPointer = s_Il2CppCodeRegistration->unresolvedInstanceCallPointers[it->second];
  769. stubs.virtualMethodPointer = s_Il2CppCodeRegistration->unresolvedVirtualCallPointers[it->second];
  770. stubs.stubsFound = true;
  771. }
  772. else
  773. {
  774. stubs.methodPointer = s_Il2CppCodeRegistration->unresolvedStaticCallPointers[it->second];
  775. stubs.virtualMethodPointer = stubs.methodPointer;
  776. stubs.stubsFound = true;
  777. }
  778. }
  779. else
  780. {
  781. const MethodInfo* entryPointNotFoundMethod = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo();
  782. stubs.methodPointer = entryPointNotFoundMethod->methodPointer;
  783. stubs.virtualMethodPointer = entryPointNotFoundMethod->methodPointer;
  784. }
  785. return stubs;
  786. }
  787. const Il2CppAssembly* il2cpp::vm::MetadataCache::GetAssemblyFromIndex(AssemblyIndex index)
  788. {
  789. if (index == kGenericContainerIndexInvalid)
  790. return NULL;
  791. IL2CPP_ASSERT(index <= s_AssembliesCount);
  792. return s_AssembliesTable + index;
  793. }
  794. const Il2CppAssembly* il2cpp::vm::MetadataCache::GetAssemblyByName(const char* nameToFind)
  795. {
  796. for (int i = 0; i < s_AssembliesCount; i++)
  797. {
  798. const Il2CppAssembly* assembly = s_AssembliesTable + i;
  799. const char* assemblyName = assembly->aname.name;
  800. if (strcmp(assemblyName, nameToFind) == 0)
  801. return assembly;
  802. }
  803. return NULL;
  804. }
  805. Il2CppImage* il2cpp::vm::MetadataCache::GetImageFromIndex(ImageIndex index)
  806. {
  807. if (index == kGenericContainerIndexInvalid)
  808. return NULL;
  809. IL2CPP_ASSERT(index <= s_ImagesCount);
  810. return s_ImagesTable + index;
  811. }
  812. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromType(const Il2CppType* type)
  813. {
  814. if (type == NULL)
  815. return NULL;
  816. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromType(type);
  817. }
  818. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromHandle(Il2CppMetadataTypeHandle handle)
  819. {
  820. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle(handle);
  821. }
  822. Il2CppMetadataGenericContainerHandle il2cpp::vm::MetadataCache::GetGenericContainerFromGenericClass(const Il2CppImage* image, const Il2CppGenericClass* genericClass)
  823. {
  824. return il2cpp::vm::GlobalMetadata::GetGenericContainerFromGenericClass(genericClass);
  825. }
  826. Il2CppMetadataGenericContainerHandle il2cpp::vm::MetadataCache::GetGenericContainerFromMethod(Il2CppMetadataMethodDefinitionHandle handle)
  827. {
  828. return il2cpp::vm::GlobalMetadata::GetGenericContainerFromMethod(handle);
  829. }
  830. Il2CppMetadataGenericParameterHandle il2cpp::vm::MetadataCache::GetGenericParameterFromType(const Il2CppType* type)
  831. {
  832. return il2cpp::vm::GlobalMetadata::GetGenericParameterFromType(type);
  833. }
  834. Il2CppClass* il2cpp::vm::MetadataCache::GetContainerDeclaringType(Il2CppMetadataGenericContainerHandle handle)
  835. {
  836. return il2cpp::vm::GlobalMetadata::GetContainerDeclaringType(handle);
  837. }
  838. Il2CppClass* il2cpp::vm::MetadataCache::GetParameterDeclaringType(Il2CppMetadataGenericParameterHandle handle)
  839. {
  840. return il2cpp::vm::GlobalMetadata::GetParameterDeclaringType(handle);
  841. }
  842. const MethodInfo* il2cpp::vm::MetadataCache::GetParameterDeclaringMethod(Il2CppMetadataGenericParameterHandle handle)
  843. {
  844. return il2cpp::vm::GlobalMetadata::GetParameterDeclaringMethod(handle);
  845. }
  846. Il2CppMetadataGenericParameterHandle il2cpp::vm::MetadataCache::GetGenericParameterFromIndex(Il2CppMetadataGenericContainerHandle handle, GenericContainerParameterIndex index)
  847. {
  848. return il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndex(handle, index);
  849. }
  850. const Il2CppType* il2cpp::vm::MetadataCache::GetGenericParameterConstraintFromIndex(Il2CppMetadataGenericParameterHandle handle, GenericParameterConstraintIndex index)
  851. {
  852. return il2cpp::vm::GlobalMetadata::GetGenericParameterConstraintFromIndex(handle, index);
  853. }
  854. Il2CppClass* il2cpp::vm::MetadataCache::GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset)
  855. {
  856. return il2cpp::vm::GlobalMetadata::GetNestedTypeFromOffset(klass, offset);
  857. }
  858. const Il2CppType* il2cpp::vm::MetadataCache::GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex offset)
  859. {
  860. return il2cpp::vm::GlobalMetadata::GetInterfaceFromOffset(klass, offset);
  861. }
  862. Il2CppInterfaceOffsetInfo il2cpp::vm::MetadataCache::GetInterfaceOffsetInfo(const Il2CppClass* klass, TypeInterfaceOffsetIndex index)
  863. {
  864. return il2cpp::vm::GlobalMetadata::GetInterfaceOffsetInfo(klass, index);
  865. }
  866. static int CompareIl2CppTokenRangePair(const void* pkey, const void* pelem)
  867. {
  868. return (int)(((Il2CppTokenRangePair*)pkey)->token - ((Il2CppTokenRangePair*)pelem)->token);
  869. }
  870. il2cpp::vm::RGCTXCollection il2cpp::vm::MetadataCache::GetRGCTXs(const Il2CppImage* image, uint32_t token)
  871. {
  872. RGCTXCollection collection = { 0, NULL };
  873. if (image->codeGenModule->rgctxRangesCount == 0)
  874. return collection;
  875. Il2CppTokenRangePair key;
  876. memset(&key, 0, sizeof(Il2CppTokenRangePair));
  877. key.token = token;
  878. const Il2CppTokenRangePair* res = (const Il2CppTokenRangePair*)bsearch(&key, image->codeGenModule->rgctxRanges, image->codeGenModule->rgctxRangesCount, sizeof(Il2CppTokenRangePair), CompareIl2CppTokenRangePair);
  879. if (res == NULL)
  880. return collection;
  881. collection.count = res->range.length;
  882. collection.items = image->codeGenModule->rgctxs + res->range.start;
  883. return collection;
  884. }
  885. const uint8_t* il2cpp::vm::MetadataCache::GetFieldDefaultValue(const FieldInfo* field, const Il2CppType** type)
  886. {
  887. return il2cpp::vm::GlobalMetadata::GetFieldDefaultValue(field, type);
  888. }
  889. const uint8_t* il2cpp::vm::MetadataCache::GetParameterDefaultValue(const MethodInfo* method, int32_t parameterPosition, const Il2CppType** type, bool* isExplicitySetNullDefaultValue)
  890. {
  891. return il2cpp::vm::GlobalMetadata::GetParameterDefaultValue(method, parameterPosition, type, isExplicitySetNullDefaultValue);
  892. }
  893. int il2cpp::vm::MetadataCache::GetFieldMarshaledSizeForField(const FieldInfo* field)
  894. {
  895. return il2cpp::vm::GlobalMetadata::GetFieldMarshaledSizeForField(field);
  896. }
  897. int32_t il2cpp::vm::MetadataCache::GetFieldOffsetFromIndexLocked(const Il2CppClass* klass, int32_t fieldIndexInType, FieldInfo* field, const il2cpp::os::FastAutoLock& lock)
  898. {
  899. int32_t offset = il2cpp::vm::GlobalMetadata::GetFieldOffset(klass, fieldIndexInType, field);
  900. if (offset < 0)
  901. {
  902. AddThreadLocalStaticOffsetForFieldLocked(field, offset & ~THREAD_LOCAL_STATIC_MASK, lock);
  903. return THREAD_STATIC_FIELD_OFFSET;
  904. }
  905. return offset;
  906. }
  907. void il2cpp::vm::MetadataCache::AddThreadLocalStaticOffsetForFieldLocked(FieldInfo* field, int32_t offset, const il2cpp::os::FastAutoLock& lock)
  908. {
  909. s_ThreadLocalStaticOffsetMap.add(field, offset);
  910. }
  911. int32_t il2cpp::vm::MetadataCache::GetThreadLocalStaticOffsetForField(FieldInfo* field)
  912. {
  913. IL2CPP_ASSERT(field->offset == THREAD_STATIC_FIELD_OFFSET);
  914. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  915. Il2CppThreadLocalStaticOffsetHashMapIter iter = s_ThreadLocalStaticOffsetMap.find(field);
  916. IL2CPP_ASSERT(iter != s_ThreadLocalStaticOffsetMap.end());
  917. return iter->second;
  918. }
  919. Il2CppMetadataCustomAttributeHandle il2cpp::vm::MetadataCache::GetCustomAttributeTypeToken(const Il2CppImage* image, uint32_t token)
  920. {
  921. return il2cpp::vm::GlobalMetadata::GetCustomAttributeTypeToken(image, token);
  922. }
  923. il2cpp::metadata::CustomAttributeDataReader il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(const Il2CppImage* image, uint32_t token)
  924. {
  925. return il2cpp::vm::GlobalMetadata::GetCustomAttributeDataReader(image, token);
  926. }
  927. il2cpp::metadata::CustomAttributeDataReader il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(Il2CppMetadataCustomAttributeHandle handle)
  928. {
  929. return il2cpp::vm::GlobalMetadata::GetCustomAttributeDataReader(handle);
  930. }
  931. const Il2CppAssembly* il2cpp::vm::MetadataCache::GetReferencedAssembly(const Il2CppAssembly* assembly, int32_t referencedAssemblyTableIndex)
  932. {
  933. return il2cpp::vm::GlobalMetadata::GetReferencedAssembly(assembly, referencedAssemblyTableIndex, s_AssembliesTable, s_AssembliesCount);
  934. }
  935. void il2cpp::vm::MetadataCache::InitializeAllMethodMetadata()
  936. {
  937. il2cpp::vm::GlobalMetadata::InitializeAllMethodMetadata();
  938. }
  939. void* il2cpp::vm::MetadataCache::InitializeRuntimeMetadata(uintptr_t* metadataPointer)
  940. {
  941. return il2cpp::vm::GlobalMetadata::InitializeRuntimeMetadata(metadataPointer, true);
  942. }
  943. void il2cpp::vm::MetadataCache::WalkPointerTypes(WalkTypesCallback callback, void* context)
  944. {
  945. os::FastAutoLock lock(&g_MetadataLock);
  946. for (PointerTypeMap::iterator it = s_MetadataCache.m_PointerTypes.UnlockedBegin(); it != s_MetadataCache.m_PointerTypes.UnlockedEnd(); it++)
  947. {
  948. callback(it->second, context);
  949. }
  950. }
  951. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetTypeHandleFromIndex(const Il2CppImage* image, TypeDefinitionIndex typeIndex)
  952. {
  953. return il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(typeIndex);
  954. }
  955. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetTypeHandleFromType(const Il2CppType* type)
  956. {
  957. return il2cpp::vm::GlobalMetadata::GetTypeHandleFromType(type);
  958. }
  959. bool il2cpp::vm::MetadataCache::TypeIsNested(Il2CppMetadataTypeHandle handle)
  960. {
  961. return il2cpp::vm::GlobalMetadata::TypeIsNested(handle);
  962. }
  963. bool il2cpp::vm::MetadataCache::TypeIsValueType(Il2CppMetadataTypeHandle handle)
  964. {
  965. return il2cpp::vm::GlobalMetadata::TypeIsValueType(handle);
  966. }
  967. bool il2cpp::vm::MetadataCache::StructLayoutPackIsDefault(Il2CppMetadataTypeHandle handle)
  968. {
  969. return il2cpp::vm::GlobalMetadata::StructLayoutPackIsDefault(handle);
  970. }
  971. int32_t il2cpp::vm::MetadataCache::StructLayoutPack(Il2CppMetadataTypeHandle handle)
  972. {
  973. return il2cpp::vm::GlobalMetadata::StructLayoutPack(handle);
  974. }
  975. bool il2cpp::vm::MetadataCache::StructLayoutSizeIsDefault(Il2CppMetadataTypeHandle handle)
  976. {
  977. return il2cpp::vm::GlobalMetadata::StructLayoutSizeIsDefault(handle);
  978. }
  979. std::pair<const char*, const char*> il2cpp::vm::MetadataCache::GetTypeNamespaceAndName(Il2CppMetadataTypeHandle handle)
  980. {
  981. return il2cpp::vm::GlobalMetadata::GetTypeNamespaceAndName(handle);
  982. }
  983. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetNestedTypes(Il2CppClass *klass, void* *iter)
  984. {
  985. return GetNestedTypes(
  986. klass->typeMetadataHandle,
  987. iter
  988. );
  989. }
  990. Il2CppMetadataTypeHandle il2cpp::vm::MetadataCache::GetNestedTypes(Il2CppMetadataTypeHandle handle, void** iter)
  991. {
  992. return il2cpp::vm::GlobalMetadata::GetNestedTypes(handle, iter);
  993. }
  994. Il2CppMetadataFieldInfo il2cpp::vm::MetadataCache::GetFieldInfo(const Il2CppClass* klass, TypeFieldIndex fieldIndex)
  995. {
  996. return il2cpp::vm::GlobalMetadata::GetFieldInfo(klass, fieldIndex);
  997. }
  998. Il2CppMetadataMethodInfo il2cpp::vm::MetadataCache::GetMethodInfo(const Il2CppClass* klass, TypeMethodIndex index)
  999. {
  1000. return il2cpp::vm::GlobalMetadata::GetMethodInfo(klass, index);
  1001. }
  1002. Il2CppMetadataParameterInfo il2cpp::vm::MetadataCache::GetParameterInfo(const Il2CppClass* klass, Il2CppMetadataMethodDefinitionHandle handle, MethodParameterIndex paramIndex)
  1003. {
  1004. return il2cpp::vm::GlobalMetadata::GetParameterInfo(klass, handle, paramIndex);
  1005. }
  1006. Il2CppMetadataPropertyInfo il2cpp::vm::MetadataCache::GetPropertyInfo(const Il2CppClass* klass, TypePropertyIndex index)
  1007. {
  1008. return il2cpp::vm::GlobalMetadata::GetPropertyInfo(klass, index);
  1009. }
  1010. Il2CppMetadataEventInfo il2cpp::vm::MetadataCache::GetEventInfo(const Il2CppClass* klass, TypeEventIndex index)
  1011. {
  1012. return il2cpp::vm::GlobalMetadata::GetEventInfo(klass, index);
  1013. }
  1014. uint32_t il2cpp::vm::MetadataCache::GetGenericContainerCount(Il2CppMetadataGenericContainerHandle handle)
  1015. {
  1016. return il2cpp::vm::GlobalMetadata::GetGenericContainerCount(handle);
  1017. }
  1018. void il2cpp::vm::MetadataCache::MakeGenericArgType(Il2CppMetadataGenericContainerHandle containerHandle, Il2CppMetadataGenericParameterHandle paramHandle, Il2CppType* arg)
  1019. {
  1020. return il2cpp::vm::GlobalMetadata::MakeGenericArgType(containerHandle, paramHandle, arg);
  1021. }
  1022. bool il2cpp::vm::MetadataCache::GetGenericContainerIsMethod(Il2CppMetadataGenericContainerHandle handle)
  1023. {
  1024. return il2cpp::vm::GlobalMetadata::GetGenericContainerIsMethod(handle);
  1025. }
  1026. int16_t il2cpp::vm::MetadataCache::GetGenericConstraintCount(Il2CppMetadataGenericParameterHandle handle)
  1027. {
  1028. return il2cpp::vm::GlobalMetadata::GetGenericConstraintCount(handle);
  1029. }
  1030. const char* il2cpp::vm::MetadataCache::GetGenericParameterName(Il2CppMetadataGenericParameterHandle handle)
  1031. {
  1032. return il2cpp::vm::GlobalMetadata::GetGenericParameterName(handle);
  1033. }
  1034. Il2CppGenericParameterInfo il2cpp::vm::MetadataCache::GetGenericParameterInfo(Il2CppMetadataGenericParameterHandle handle)
  1035. {
  1036. return il2cpp::vm::GlobalMetadata::GetGenericParameterInfo(handle);
  1037. }
  1038. uint16_t il2cpp::vm::MetadataCache::GetGenericParameterFlags(Il2CppMetadataGenericParameterHandle handle)
  1039. {
  1040. return il2cpp::vm::GlobalMetadata::GetGenericParameterFlags(handle);
  1041. }
  1042. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromCatchPoint(const Il2CppImage* image, const Il2CppCatchPoint* cp)
  1043. {
  1044. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromCatchPoint(cp);
  1045. }
  1046. const MethodInfo* il2cpp::vm::MetadataCache::GetMethodInfoFromSequencePoint(const Il2CppImage* image, const Il2CppSequencePoint* seqPoint)
  1047. {
  1048. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromSequencePoint(seqPoint);
  1049. }
  1050. Il2CppClass* il2cpp::vm::MetadataCache::GetTypeInfoFromTypeSourcePair(const Il2CppImage* image, const Il2CppTypeSourceFilePair* pair)
  1051. {
  1052. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeSourcePair(pair);
  1053. }