GlobalMetadata.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. #include "GlobalMetadata.h"
  2. #include "il2cpp-config.h"
  3. #include <map>
  4. #include <limits>
  5. #include <il2cpp-runtime-metadata.h>
  6. #include "il2cpp-class-internals.h"
  7. #include "il2cpp-tabledefs.h"
  8. #include "il2cpp-runtime-stats.h"
  9. #include "gc/GarbageCollector.h"
  10. #include "metadata/ArrayMetadata.h"
  11. #include "metadata/CustomAttributeDataReader.h"
  12. #include "metadata/CustomAttributeCreator.h"
  13. #include "metadata/GenericMetadata.h"
  14. #include "metadata/GenericMethod.h"
  15. #include "metadata/Il2CppTypeCompare.h"
  16. #include "metadata/Il2CppTypeHash.h"
  17. #include "metadata/Il2CppGenericContextCompare.h"
  18. #include "metadata/Il2CppGenericContextHash.h"
  19. #include "metadata/Il2CppGenericInstCompare.h"
  20. #include "metadata/Il2CppGenericInstHash.h"
  21. #include "metadata/Il2CppGenericMethodCompare.h"
  22. #include "metadata/Il2CppGenericMethodHash.h"
  23. #include "metadata/Il2CppSignature.h"
  24. #include "os/Atomic.h"
  25. #include "os/Mutex.h"
  26. #include "utils/CallOnce.h"
  27. #include "utils/Collections.h"
  28. #include "utils/HashUtils.h"
  29. #include "utils/Il2CppHashMap.h"
  30. #include "utils/Il2CppHashSet.h"
  31. #include "utils/InitOnce.h"
  32. #include "utils/Memory.h"
  33. #include "utils/StringUtils.h"
  34. #include "utils/PathUtils.h"
  35. #include "utils/MemoryMappedFile.h"
  36. #include "vm/Assembly.h"
  37. #include "vm/Class.h"
  38. #include "vm/ClassInlines.h"
  39. #include "vm/GenericClass.h"
  40. #include "vm/MetadataAlloc.h"
  41. #include "vm/MetadataLoader.h"
  42. #include "vm/MetadataLock.h"
  43. #include "vm/Exception.h"
  44. #include "vm/Method.h"
  45. #include "vm/Object.h"
  46. #include "vm/String.h"
  47. #include "vm/Type.h"
  48. #include "vm-utils/MethodDefinitionKey.h"
  49. #include "vm-utils/NativeSymbol.h"
  50. #include "vm-utils/VmStringUtils.h"
  51. #include "Baselib.h"
  52. #include "Cpp/ReentrantLock.h"
  53. #include "GlobalMetadataFileInternals.h"
  54. #include "hybridclr/metadata/MetadataUtil.h"
  55. #include "hybridclr/metadata/MetadataModule.h"
  56. static int32_t s_MetadataImagesCount = 0;
  57. static Il2CppImageGlobalMetadata* s_MetadataImagesTable = NULL;
  58. static TypeDefinitionIndex GetIndexForTypeDefinitionInternal(const Il2CppTypeDefinition* typeDefinition);
  59. static GenericParameterIndex GetIndexForGenericParameter(Il2CppMetadataGenericParameterHandle handle);
  60. static const MethodInfo* GetMethodInfoFromEncodedIndex(EncodedMethodIndex methodIndex);
  61. static void* s_GlobalMetadata;
  62. static const Il2CppGlobalMetadataHeader* s_GlobalMetadataHeader;
  63. static const Il2CppGenericMethod** s_GenericMethodTable = NULL;
  64. static const MethodInfo** s_MethodInfoDefinitionTable = NULL;
  65. static Il2CppString** s_StringLiteralTable = NULL;
  66. static il2cpp::utils::OnceFlag s_CustomAttributesOnceFlag;
  67. static int s_CustomAttributesCount;
  68. static const Il2CppCodeRegistration * s_GlobalMetadata_CodeRegistration;
  69. static const Il2CppMetadataRegistration * s_Il2CppMetadataRegistration;
  70. static Il2CppClass** s_TypeInfoTable = NULL;
  71. static Il2CppClass** s_TypeInfoDefinitionTable = NULL;
  72. template<typename T>
  73. static T MetadataOffset(const void* metadata, size_t sectionOffset, size_t itemIndex)
  74. {
  75. return reinterpret_cast<T>(reinterpret_cast<uint8_t*>(const_cast<void*>(metadata)) + sectionOffset) + itemIndex;
  76. }
  77. const char* il2cpp::vm::GlobalMetadata::GetStringFromIndex(StringIndex index)
  78. {
  79. if (hybridclr::metadata::IsInterpreterIndex(index))
  80. {
  81. return hybridclr::metadata::MetadataModule::GetStringFromEncodeIndex(index);
  82. }
  83. IL2CPP_ASSERT(index <= s_GlobalMetadataHeader->stringSize);
  84. return MetadataOffset<const char*>(s_GlobalMetadata, s_GlobalMetadataHeader->stringOffset, index);
  85. }
  86. static const char* GetWindowsRuntimeStringFromIndex(StringIndex index)
  87. {
  88. IL2CPP_ASSERT(index <= s_GlobalMetadataHeader->windowsRuntimeStringsSize);
  89. return MetadataOffset<const char*>(s_GlobalMetadata, s_GlobalMetadataHeader->windowsRuntimeStringsOffset, index);
  90. }
  91. const Il2CppMethodDefinition* il2cpp::vm::GlobalMetadata::GetMethodDefinitionFromIndex(MethodIndex index)
  92. {
  93. if (hybridclr::metadata::IsInterpreterIndex(index))
  94. {
  95. return hybridclr::metadata::MetadataModule::GetMethodDefinitionFromIndex(index);
  96. }
  97. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition));
  98. return MetadataOffset<const Il2CppMethodDefinition*>(s_GlobalMetadata, s_GlobalMetadataHeader->methodsOffset, index);
  99. }
  100. MethodIndex il2cpp::vm::GlobalMetadata::GetMethodIndexFromDefinition(const Il2CppMethodDefinition* methodDef)
  101. {
  102. if (hybridclr::metadata::IsInterpreterMethod(methodDef))
  103. {
  104. return hybridclr::metadata::MetadataModule::GetImage(methodDef)->GetMethodIndexFromDefinition(methodDef);
  105. }
  106. return (MethodIndex)(methodDef - MetadataOffset<const Il2CppMethodDefinition*>(s_GlobalMetadata, s_GlobalMetadataHeader->methodsOffset, 0));
  107. }
  108. const MethodInfo* il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(MethodIndex index)
  109. {
  110. if (hybridclr::metadata::IsInterpreterIndex(index))
  111. {
  112. return hybridclr::metadata::MetadataModule::GetMethodInfoFromMethodDefinitionIndex(index);
  113. }
  114. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition));
  115. return utils::InitOnce(&s_MethodInfoDefinitionTable[index], &g_MetadataLock, [index](il2cpp::os::FastAutoLock& _)
  116. {
  117. const Il2CppMethodDefinition* methodDefinition = il2cpp::vm::GlobalMetadata::GetMethodDefinitionFromIndex(index);
  118. Il2CppClass* typeInfo = il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeDefinitionIndex(methodDefinition->declaringType);
  119. //[WL]
  120. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(typeInfo->typeMetadataHandle);
  121. MethodIndex indexInType = index - typeDefinition->methodStart;
  122. return il2cpp::vm::Class::GetOrSetupOneMethod(typeInfo, indexInType);
  123. });
  124. }
  125. static const Il2CppEventDefinition* GetEventDefinitionFromIndex(const Il2CppImage* image, EventIndex index)
  126. {
  127. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->eventsSize / sizeof(Il2CppEventDefinition));
  128. const Il2CppEventDefinition* events = (const Il2CppEventDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->eventsOffset);
  129. return events + index;
  130. }
  131. static const Il2CppPropertyDefinition* GetPropertyDefinitionFromIndex(const Il2CppImage* image, PropertyIndex index)
  132. {
  133. if (hybridclr::metadata::IsInterpreterImage(image))
  134. {
  135. return hybridclr::metadata::MetadataModule::GetImage(image)->GetPropertyDefinitionFromIndex(hybridclr::metadata::DecodeMetadataIndex(index));
  136. }
  137. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->propertiesSize / sizeof(Il2CppPropertyDefinition));
  138. const Il2CppPropertyDefinition* properties = (const Il2CppPropertyDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->propertiesOffset);
  139. return properties + index;
  140. }
  141. const Il2CppParameterDefinition* il2cpp::vm::GlobalMetadata::GetParameterDefinitionFromIndex(const Il2CppClass* klass, ParameterIndex index)
  142. {
  143. if (hybridclr::metadata::IsInterpreterType(klass))
  144. {
  145. return hybridclr::metadata::MetadataModule::GetParameterDefinitionFromIndex(klass->image, index);
  146. }
  147. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->parametersSize / sizeof(Il2CppParameterDefinition)); const Il2CppParameterDefinition* parameters = (const Il2CppParameterDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->parametersOffset);
  148. return parameters + index;
  149. }
  150. const Il2CppParameterDefinition* il2cpp::vm::GlobalMetadata::GetParameterDefinitionFromIndex(const Il2CppMethodDefinition* methodDef, ParameterIndex index)
  151. {
  152. if (hybridclr::metadata::IsInterpreterIndex(methodDef->nameIndex))
  153. {
  154. return hybridclr::metadata::MetadataModule::GetParameterDefinitionFromIndex(hybridclr::metadata::MetadataModule::GetImage(methodDef)->GetIl2CppImage(), index);
  155. }
  156. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->parametersSize / sizeof(Il2CppParameterDefinition));
  157. const Il2CppParameterDefinition* parameters = (const Il2CppParameterDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->parametersOffset);
  158. return parameters + index;
  159. }
  160. static const Il2CppMethodDefinition* GetMethodDefinitionFromEncodedIndex(EncodedMethodIndex methodIndex)
  161. {
  162. const MethodInfo* method = GetMethodInfoFromEncodedIndex(methodIndex);
  163. if (!method)
  164. {
  165. return nullptr;
  166. }
  167. if (method->is_inflated)
  168. {
  169. method = method->genericMethod->methodDefinition;
  170. }
  171. return (const Il2CppMethodDefinition *)method->methodMetadataHandle;
  172. }
  173. uint8_t il2cpp::vm::GlobalMetadata::ConvertPackingSizeEnumToValue(PackingSize packingSize)
  174. {
  175. switch (packingSize)
  176. {
  177. case Zero:
  178. return 0;
  179. case One:
  180. return 1;
  181. case Two:
  182. return 2;
  183. case Four:
  184. return 4;
  185. case Eight:
  186. return 8;
  187. case Sixteen:
  188. return 16;
  189. case ThirtyTwo:
  190. return 32;
  191. case SixtyFour:
  192. return 64;
  193. case OneHundredTwentyEight:
  194. return 128;
  195. default:
  196. Assert(0 && "Invalid packing size!");
  197. return 0;
  198. }
  199. }
  200. il2cpp::vm::PackingSize il2cpp::vm::GlobalMetadata::ConvertPackingSizeToEnum(uint8_t packingSize)
  201. {
  202. switch (packingSize)
  203. {
  204. case 0:
  205. return PackingSize::Zero;
  206. case 1:
  207. return PackingSize::One;
  208. case 2:
  209. return PackingSize::Two;
  210. case 4:
  211. return PackingSize::Four;
  212. case 8:
  213. return PackingSize::Eight;
  214. case 16:
  215. return PackingSize::Sixteen;
  216. case 32:
  217. return PackingSize::ThirtyTwo;
  218. case 64:
  219. return PackingSize::SixtyFour;
  220. case 128:
  221. return OneHundredTwentyEight;
  222. default:
  223. Assert(0 && "Invalid packing size!");
  224. return PackingSize::Zero;
  225. }
  226. }
  227. static const Il2CppGenericMethod* GetGenericMethodFromIndex(GenericMethodIndex index)
  228. {
  229. IL2CPP_ASSERT(index < s_Il2CppMetadataRegistration->methodSpecsCount);
  230. return il2cpp::utils::InitOnce(&s_GenericMethodTable[index], &il2cpp::vm::g_MetadataLock, [index](il2cpp::os::FastAutoLock& _)
  231. {
  232. const Il2CppMethodSpec* methodSpec = s_Il2CppMetadataRegistration->methodSpecs + index;
  233. const MethodInfo* methodDefinition = il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(methodSpec->methodDefinitionIndex);
  234. const Il2CppGenericInst* classInst = NULL;
  235. const Il2CppGenericInst* methodInst = NULL;
  236. if (methodSpec->classIndexIndex != -1)
  237. {
  238. IL2CPP_ASSERT(methodSpec->classIndexIndex < s_Il2CppMetadataRegistration->genericInstsCount);
  239. classInst = s_Il2CppMetadataRegistration->genericInsts[methodSpec->classIndexIndex];
  240. }
  241. if (methodSpec->methodIndexIndex != -1)
  242. {
  243. IL2CPP_ASSERT(methodSpec->methodIndexIndex < s_Il2CppMetadataRegistration->genericInstsCount);
  244. methodInst = s_Il2CppMetadataRegistration->genericInsts[methodSpec->methodIndexIndex];
  245. }
  246. return il2cpp::vm::MetadataCache::GetGenericMethod(methodDefinition, classInst, methodInst);
  247. });
  248. }
  249. static const MethodInfo* GetMethodInfoFromEncodedIndex(EncodedMethodIndex methodIndex)
  250. {
  251. Il2CppMetadataUsage usage = GetEncodedIndexType(methodIndex);
  252. uint32_t index = GetDecodedMethodIndex(methodIndex);
  253. switch (GetEncodedIndexType(methodIndex))
  254. {
  255. case kIl2CppMetadataUsageMethodRef:
  256. return il2cpp::metadata::GenericMethod::GetMethod(GetGenericMethodFromIndex(index));
  257. case kIl2CppMetadataUsageMethodDef:
  258. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(index);
  259. case kIl2CppMetadataUsageInvalid:
  260. {
  261. switch (index)
  262. {
  263. case kIl2CppInvalidMetadataUsageNoData:
  264. return NULL;
  265. case kIl2CppInvalidMetadataUsageAmbiguousMethod:
  266. return il2cpp::vm::Method::GetAmbiguousMethodInfo();
  267. default:
  268. IL2CPP_ASSERT(0);
  269. break;
  270. }
  271. }
  272. default:
  273. IL2CPP_ASSERT(0);
  274. break;
  275. }
  276. return NULL;
  277. }
  278. static Il2CppString* GetStringLiteralFromIndex(StringLiteralIndex index)
  279. {
  280. if (index == kStringLiteralIndexInvalid)
  281. return NULL;
  282. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_GlobalMetadataHeader->stringLiteralSize / sizeof(Il2CppStringLiteral) && "Invalid string literal index ");
  283. if (s_StringLiteralTable[index])
  284. return s_StringLiteralTable[index];
  285. const Il2CppStringLiteral* stringLiteral = (const Il2CppStringLiteral*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->stringLiteralOffset) + index;
  286. Il2CppString* newString = il2cpp::vm::String::NewLen((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->stringLiteralDataOffset + stringLiteral->dataIndex, stringLiteral->length);
  287. Il2CppString* prevString = il2cpp::os::Atomic::CompareExchangePointer<Il2CppString>(s_StringLiteralTable + index, newString, NULL);
  288. if (prevString == NULL)
  289. {
  290. il2cpp::gc::GarbageCollector::SetWriteBarrier((void**)s_StringLiteralTable + index);
  291. return newString;
  292. }
  293. return prevString;
  294. }
  295. static FieldInfo* GetFieldInfoFromIndex(EncodedMethodIndex index)
  296. {
  297. IL2CPP_ASSERT(s_GlobalMetadataHeader->fieldRefsSize >= 0 && index <= static_cast<uint32_t>(s_GlobalMetadataHeader->fieldRefsSize / sizeof(Il2CppFieldRef)));
  298. const Il2CppFieldRef* fieldRef = MetadataOffset<const Il2CppFieldRef*>(s_GlobalMetadata, s_GlobalMetadataHeader->fieldRefsOffset, index);
  299. const Il2CppClass* typeInfo = il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeIndex(fieldRef->typeIndex);
  300. return typeInfo->fields + fieldRef->fieldIndex;
  301. }
  302. void il2cpp::vm::GlobalMetadata::Register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)
  303. {
  304. s_GlobalMetadata_CodeRegistration = codeRegistration;
  305. s_Il2CppMetadataRegistration = metadataRegistration;
  306. }
  307. typedef void (*Il2CppTypeUpdater)(Il2CppType*);
  308. void il2cpp::vm::GlobalMetadata::InitializeTypeHandle(Il2CppType* type)
  309. {
  310. type->data.typeHandle = il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(type->data.__klassIndex);
  311. }
  312. static void ClearTypeHandle(Il2CppType* type)
  313. {
  314. type->data.__klassIndex = GetIndexForTypeDefinitionInternal(reinterpret_cast<const Il2CppTypeDefinition*>(type->data.typeHandle));
  315. }
  316. static void InitializeGenericParameterHandle(Il2CppType* type)
  317. {
  318. type->data.genericParameterHandle = il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndexInternal(type->data.__genericParameterIndex);
  319. }
  320. static void ClearGenericParameterHandle(Il2CppType* type)
  321. {
  322. type->data.__genericParameterIndex = GetIndexForGenericParameter(reinterpret_cast<Il2CppMetadataGenericParameterHandle>(type->data.genericParameterHandle));
  323. }
  324. static void ProcessIl2CppTypeDefinitions(Il2CppTypeUpdater updateTypeDef, Il2CppTypeUpdater updateGenericParam)
  325. {
  326. for (int32_t i = 0; i < s_Il2CppMetadataRegistration->typesCount; i++)
  327. {
  328. const Il2CppType* type = s_Il2CppMetadataRegistration->types[i];
  329. switch (type->type)
  330. {
  331. case IL2CPP_TYPE_VOID:
  332. case IL2CPP_TYPE_BOOLEAN:
  333. case IL2CPP_TYPE_CHAR:
  334. case IL2CPP_TYPE_I1:
  335. case IL2CPP_TYPE_U1:
  336. case IL2CPP_TYPE_I2:
  337. case IL2CPP_TYPE_U2:
  338. case IL2CPP_TYPE_I4:
  339. case IL2CPP_TYPE_U4:
  340. case IL2CPP_TYPE_I8:
  341. case IL2CPP_TYPE_U8:
  342. case IL2CPP_TYPE_R4:
  343. case IL2CPP_TYPE_R8:
  344. case IL2CPP_TYPE_STRING:
  345. case IL2CPP_TYPE_VALUETYPE:
  346. case IL2CPP_TYPE_CLASS:
  347. case IL2CPP_TYPE_I:
  348. case IL2CPP_TYPE_U:
  349. case IL2CPP_TYPE_OBJECT:
  350. case IL2CPP_TYPE_TYPEDBYREF:
  351. // The Il2Cpp conversion process writes these types in a writeable section
  352. // So we can const_cast them here safely
  353. updateTypeDef(const_cast<Il2CppType*>(type));
  354. break;
  355. case IL2CPP_TYPE_VAR:
  356. case IL2CPP_TYPE_MVAR:
  357. updateGenericParam(const_cast<Il2CppType*>(type));
  358. break;
  359. default:
  360. // Nothing do to
  361. break;
  362. }
  363. }
  364. }
  365. bool il2cpp::vm::GlobalMetadata::Initialize(int32_t* imagesCount, int32_t* assembliesCount)
  366. {
  367. #if ENABLE_HMI_MODE && IL2CPP_TARGET_ANDROID
  368. s_GlobalMetadata = vm::MetadataLoader::LoadMetadataFile("global-metadata.so"); // HACKY!! enforce 4k zip alignment
  369. #else
  370. s_GlobalMetadata = vm::MetadataLoader::LoadMetadataFile("global-metadata.dat");
  371. #endif
  372. if (!s_GlobalMetadata)
  373. return false;
  374. #if IL2CPP_ENABLE_MEM_STATS
  375. il2cpp_mem_stats.globalMetadataMapfile = il2cpp::utils::MemoryMappedFile::MapSize(s_GlobalMetadata);
  376. #endif
  377. il2cpp_runtime_stats.global_metadata_file_size = il2cpp::utils::MemoryMappedFile::MapSize(s_GlobalMetadata);
  378. s_GlobalMetadataHeader = (const Il2CppGlobalMetadataHeader*)s_GlobalMetadata;
  379. IL2CPP_ASSERT(s_GlobalMetadataHeader->sanity == 0xFAB11BAF);
  380. #if SUPPORT_METHOD_RETURN_TYPE_CUSTOM_ATTRIBUTE
  381. #if SLIM_GLOBAL_METADATA_FILE
  382. IL2CPP_ASSERT(s_GlobalMetadataHeader->version == 32);
  383. #else
  384. IL2CPP_ASSERT(s_GlobalMetadataHeader->version == 31);
  385. #endif
  386. #else
  387. #if SLIM_GLOBAL_METADATA_FILE
  388. IL2CPP_ASSERT(s_GlobalMetadataHeader->version == 30);
  389. #else
  390. IL2CPP_ASSERT(s_GlobalMetadataHeader->version == 29);
  391. #endif
  392. #endif
  393. IL2CPP_ASSERT(s_GlobalMetadataHeader->stringLiteralOffset == sizeof(Il2CppGlobalMetadataHeader));
  394. s_MetadataImagesCount = *imagesCount = s_GlobalMetadataHeader->imagesSize / sizeof(Il2CppImageDefinition);
  395. *assembliesCount = s_GlobalMetadataHeader->assembliesSize / sizeof(Il2CppAssemblyDefinition);
  396. // Pre-allocate these arrays so we don't need to lock when reading later.
  397. // These arrays hold the runtime metadata representation for metadata explicitly
  398. // referenced during conversion. There is a corresponding table of same size
  399. // in the converted metadata, giving a description of runtime metadata to construct.
  400. s_MetadataImagesTable = (Il2CppImageGlobalMetadata*)IL2CPP_CALLOC(s_MetadataImagesCount, sizeof(Il2CppImageGlobalMetadata), IL2CPP_MEM_GLOBAL_METADATA);
  401. s_TypeInfoTable = (Il2CppClass**)IL2CPP_CALLOC(s_Il2CppMetadataRegistration->typesCount, sizeof(Il2CppClass*), IL2CPP_MEM_GLOBAL_METADATA);
  402. s_TypeInfoDefinitionTable = (Il2CppClass**)IL2CPP_CALLOC(s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition), sizeof(Il2CppClass*), IL2CPP_MEM_GLOBAL_METADATA);
  403. s_MethodInfoDefinitionTable = (const MethodInfo**)IL2CPP_CALLOC(s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition), sizeof(MethodInfo*), IL2CPP_MEM_GLOBAL_METADATA);
  404. s_GenericMethodTable = (const Il2CppGenericMethod**)IL2CPP_CALLOC(s_Il2CppMetadataRegistration->methodSpecsCount, sizeof(Il2CppGenericMethod*), IL2CPP_MEM_GLOBAL_METADATA);
  405. ProcessIl2CppTypeDefinitions(InitializeTypeHandle, InitializeGenericParameterHandle);
  406. return true;
  407. }
  408. void il2cpp::vm::GlobalMetadata::InitializeAllMethodMetadata()
  409. {
  410. for (size_t i = 0; i < s_Il2CppMetadataRegistration->metadataUsagesCount; i++)
  411. {
  412. uintptr_t* metadataPointer = reinterpret_cast<uintptr_t*>(s_Il2CppMetadataRegistration->metadataUsages[i]);
  413. Il2CppMetadataUsage usage = GetEncodedIndexType(static_cast<uint32_t>(*metadataPointer));
  414. switch (usage)
  415. {
  416. case kIl2CppMetadataUsageTypeInfo:
  417. case kIl2CppMetadataUsageMethodDef:
  418. case kIl2CppMetadataUsageMethodRef:
  419. InitializeRuntimeMetadata(metadataPointer, false);
  420. break;
  421. default:
  422. break;
  423. }
  424. }
  425. }
  426. // This method can be called from multiple threads, so it does have a data race. However, each
  427. // thread is reading from the same read-only metadata, so each thread will set the same values.
  428. // Therefore, we can safely ignore thread sanitizer issues in this method.
  429. void* il2cpp::vm::GlobalMetadata::InitializeRuntimeMetadata(uintptr_t* metadataPointer, bool throwOnError) IL2CPP_DISABLE_TSAN
  430. {
  431. // This must be the only read of *metadataPointer
  432. // This code has no locks and we need to ensure that we only read metadataPointer once
  433. // so we don't read it once as an encoded token and once as an initialized pointer
  434. uintptr_t metadataValue = (uintptr_t)os::Atomic::ReadPtrVal((intptr_t*)metadataPointer);
  435. if (IsRuntimeMetadataInitialized(metadataValue))
  436. return (void*)metadataValue;
  437. uint32_t encodedToken = static_cast<uint32_t>(metadataValue);
  438. Il2CppMetadataUsage usage = GetEncodedIndexType(encodedToken);
  439. uint32_t decodedIndex = GetDecodedMethodIndex(encodedToken);
  440. void* initialized = NULL;
  441. switch (usage)
  442. {
  443. case kIl2CppMetadataUsageTypeInfo:
  444. initialized = (void*)il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeIndex(decodedIndex, throwOnError);
  445. break;
  446. case kIl2CppMetadataUsageIl2CppType:
  447. initialized = (void*)il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(decodedIndex);
  448. break;
  449. case kIl2CppMetadataUsageMethodDef:
  450. case kIl2CppMetadataUsageMethodRef:
  451. initialized = (void*)GetMethodInfoFromEncodedIndex(encodedToken);
  452. break;
  453. case kIl2CppMetadataUsageFieldInfo:
  454. initialized = (void*)GetFieldInfoFromIndex(decodedIndex);
  455. break;
  456. case kIl2CppMetadataUsageStringLiteral:
  457. initialized = (void*)GetStringLiteralFromIndex(decodedIndex);
  458. break;
  459. case kIl2CppMetadataUsageFieldRva:
  460. const Il2CppType* unused;
  461. initialized = (void*)GetFieldDefaultValue(GetFieldInfoFromIndex(decodedIndex), &unused);
  462. {
  463. const size_t MappedFieldDataAlignment = 8; // Should match System.Reflection.Metadata.ManagedPEBuilder.MappedFieldDataAlignment
  464. IL2CPP_ASSERT(((uintptr_t)initialized % MappedFieldDataAlignment) == 0);
  465. }
  466. break;
  467. case kIl2CppMetadataUsageInvalid:
  468. break;
  469. default:
  470. IL2CPP_NOT_IMPLEMENTED(il2cpp::vm::GlobalMetadata::InitializeMethodMetadata);
  471. break;
  472. }
  473. IL2CPP_ASSERT(IsRuntimeMetadataInitialized(initialized) && "ERROR: The low bit of the metadata item is still set, alignment issue");
  474. if (initialized != NULL)
  475. {
  476. il2cpp::os::Atomic::PublishPointer((void**)metadataPointer, initialized);
  477. }
  478. return initialized;
  479. }
  480. void il2cpp::vm::GlobalMetadata::InitializeStringLiteralTable()
  481. {
  482. s_StringLiteralTable = (Il2CppString**)il2cpp::gc::GarbageCollector::AllocateFixed(s_GlobalMetadataHeader->stringLiteralSize / sizeof(Il2CppStringLiteral) * sizeof(Il2CppString*), NULL);
  483. }
  484. #if !IL2CPP_TRIM_COM
  485. void il2cpp::vm::GlobalMetadata::InitializeWindowsRuntimeTypeNamesTables(WindowsRuntimeTypeNameToClassMap& windowsRuntimeTypeNameToClassMap, ClassToWindowsRuntimeTypeNameMap& classToWindowsRuntimeTypeNameMap)
  486. {
  487. int32_t typeCount = s_GlobalMetadataHeader->windowsRuntimeTypeNamesSize / sizeof(Il2CppWindowsRuntimeTypeNamePair);
  488. const Il2CppWindowsRuntimeTypeNamePair* windowsRuntimeTypeNames = MetadataOffset<Il2CppWindowsRuntimeTypeNamePair*>(s_GlobalMetadata, s_GlobalMetadataHeader->windowsRuntimeTypeNamesOffset, 0);
  489. windowsRuntimeTypeNameToClassMap.resize(typeCount / 2 + 1);
  490. classToWindowsRuntimeTypeNameMap.resize(typeCount);
  491. for (int32_t i = 0; i < typeCount; i++)
  492. {
  493. Il2CppWindowsRuntimeTypeNamePair typeNamePair = windowsRuntimeTypeNames[i];
  494. const char* name = GetWindowsRuntimeStringFromIndex(typeNamePair.nameIndex);
  495. const Il2CppType* type = GetIl2CppTypeFromIndex(typeNamePair.typeIndex);
  496. Il2CppClass* klass = Class::FromIl2CppType(type);
  497. if (!Class::IsNullable(klass))
  498. {
  499. // Don't add nullable types to name -> klass map because IReference`1<T> and Nullable`1<T>
  500. // share windows runtime type names, and that would cause a collision.
  501. windowsRuntimeTypeNameToClassMap.insert(std::make_pair(name, klass));
  502. }
  503. classToWindowsRuntimeTypeNameMap.insert(std::make_pair(klass, name));
  504. }
  505. }
  506. #endif
  507. void il2cpp::vm::GlobalMetadata::InitializeUnresolvedSignatureTable(Il2CppUnresolvedSignatureMap& unresolvedSignatureMap)
  508. {
  509. unresolvedSignatureMap.resize(s_GlobalMetadata_CodeRegistration->unresolvedIndirectCallCount);
  510. for (uint32_t i = 0; i < s_GlobalMetadata_CodeRegistration->unresolvedIndirectCallCount; ++i)
  511. {
  512. const Il2CppMetadataRange* range = MetadataOffset<Il2CppMetadataRange*>(s_GlobalMetadata, s_GlobalMetadataHeader->unresolvedIndirectCallParameterRangesOffset, i);
  513. il2cpp::metadata::Il2CppSignature signature;
  514. signature.Count = range->length;
  515. signature.Types = (const Il2CppType**)MetadataMalloc(range->length * sizeof(Il2CppType*), IL2CPP_MSTAT_TYPE);
  516. for (int j = 0; j < range->length; ++j)
  517. {
  518. TypeIndex typeIndex = *MetadataOffset<TypeIndex*>(s_GlobalMetadata, s_GlobalMetadataHeader->unresolvedIndirectCallParameterTypesOffset, range->start + j);
  519. const Il2CppType* type = GetIl2CppTypeFromIndex(typeIndex);
  520. signature.Types[j] = type;
  521. }
  522. unresolvedSignatureMap.insert(std::make_pair(signature, i));
  523. }
  524. }
  525. void il2cpp::vm::GlobalMetadata::InitializeGenericMethodTable(Il2CppMethodTableMap& methodTableMap)
  526. {
  527. methodTableMap.resize(s_Il2CppMetadataRegistration->genericMethodTableCount);
  528. for (int32_t i = 0; i < s_Il2CppMetadataRegistration->genericMethodTableCount; i++)
  529. {
  530. const Il2CppGenericMethodFunctionsDefinitions* genericMethodIndices = s_Il2CppMetadataRegistration->genericMethodTable + i;
  531. const Il2CppGenericMethod* genericMethod = GetGenericMethodFromIndex(genericMethodIndices->genericMethodIndex);
  532. methodTableMap.insert(std::make_pair(genericMethod, &genericMethodIndices->indices));
  533. }
  534. }
  535. #if ENABLE_HMI_MODE || PLATFORM_WEIXINMINIGAME
  536. uint32_t il2cpp::vm::GlobalMetadata::InitializeGenericMethodTableFast(Il2CppGenericMethodTableItem** outVector)
  537. {
  538. *outVector = (Il2CppGenericMethodTableItem*)MetadataMalloc(s_Il2CppMetadataRegistration->genericMethodTableCount * sizeof(Il2CppGenericMethodTableItem), IL2CPP_MSTAT_METHOD);
  539. for (uint32_t i = 0; i < s_Il2CppMetadataRegistration->genericMethodTableCount; i ++)
  540. {
  541. const Il2CppGenericMethodFunctionsDefinitions* genericMethodIndices = s_Il2CppMetadataRegistration->genericMethodTable + i;
  542. GenericMethodIndex index = genericMethodIndices->genericMethodIndex;
  543. const Il2CppGenericMethod* genericMethod = s_GenericMethodTable[index];
  544. if (genericMethod == NULL) {
  545. const Il2CppMethodSpec* methodSpec = s_Il2CppMetadataRegistration->methodSpecs + index;
  546. const MethodInfo* methodDefinition = il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(methodSpec->methodDefinitionIndex);
  547. const Il2CppGenericInst* classInst = NULL;
  548. const Il2CppGenericInst* methodInst = NULL;
  549. if (methodSpec->classIndexIndex != -1)
  550. {
  551. IL2CPP_ASSERT(methodSpec->classIndexIndex < s_Il2CppMetadataRegistration->genericInstsCount);
  552. classInst = s_Il2CppMetadataRegistration->genericInsts[methodSpec->classIndexIndex];
  553. }
  554. if (methodSpec->methodIndexIndex != -1)
  555. {
  556. IL2CPP_ASSERT(methodSpec->methodIndexIndex < s_Il2CppMetadataRegistration->genericInstsCount);
  557. methodInst = s_Il2CppMetadataRegistration->genericInsts[methodSpec->methodIndexIndex];
  558. }
  559. Il2CppGenericMethod* newMethod = MetadataAllocGenericMethod();
  560. newMethod->methodDefinition = methodDefinition;
  561. newMethod->context.class_inst = classInst;
  562. newMethod->context.method_inst = methodInst;
  563. s_GenericMethodTable[index] = newMethod;
  564. (*outVector)[i].method = newMethod;
  565. }
  566. else
  567. {
  568. (*outVector)[i].method = const_cast<Il2CppGenericMethod*>(genericMethod);
  569. }
  570. (*outVector)[i].indices = const_cast<Il2CppGenericMethodIndices*>(&genericMethodIndices->indices);
  571. }
  572. std::sort(*outVector, (*outVector) + s_Il2CppMetadataRegistration->genericMethodTableCount);
  573. return s_Il2CppMetadataRegistration->genericMethodTableCount;
  574. }
  575. #endif
  576. static void ClearStringLiteralTable()
  577. {
  578. il2cpp::gc::GarbageCollector::FreeFixed(s_StringLiteralTable);
  579. s_StringLiteralTable = NULL;
  580. }
  581. static void FreeAndNull(void** pointer)
  582. {
  583. IL2CPP_FREE(*pointer, IL2CPP_MEM_GLOBAL_METADATA);
  584. *pointer = NULL;
  585. }
  586. void il2cpp::vm::GlobalMetadata::Clear()
  587. {
  588. ClearStringLiteralTable();
  589. FreeAndNull((void**)&s_MethodInfoDefinitionTable);
  590. FreeAndNull((void**)&s_GenericMethodTable);
  591. FreeAndNull((void**)&s_TypeInfoTable);
  592. FreeAndNull((void**)&s_TypeInfoDefinitionTable);
  593. ProcessIl2CppTypeDefinitions(ClearTypeHandle, ClearGenericParameterHandle);
  594. vm::MetadataLoader::UnloadMetadataFile(s_GlobalMetadata);
  595. s_GlobalMetadataHeader = NULL;
  596. s_GlobalMetadata = NULL;
  597. s_GlobalMetadata_CodeRegistration = NULL;
  598. s_Il2CppMetadataRegistration = NULL;
  599. }
  600. void il2cpp::vm::GlobalMetadata::BuildIl2CppImage(Il2CppImage* image, ImageIndex imageIndex, AssemblyIndex* imageAssemblyIndex)
  601. {
  602. const Il2CppImageDefinition* imagesDefinitions = (const Il2CppImageDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->imagesOffset);
  603. const Il2CppImageDefinition* imageDefinition = imagesDefinitions + imageIndex;
  604. image->name = GetStringFromIndex(imageDefinition->nameIndex);
  605. *imageAssemblyIndex = imageDefinition->assemblyIndex;
  606. image->typeCount = imageDefinition->typeCount;
  607. image->exportedTypeCount = imageDefinition->exportedTypeCount;
  608. image->token = imageDefinition->token;
  609. image->customAttributeCount = imageDefinition->customAttributeCount;
  610. Il2CppImageGlobalMetadata* metadataImage = s_MetadataImagesTable + imageIndex;
  611. metadataImage->typeStart = imageDefinition->typeStart;
  612. metadataImage->customAttributeStart = imageDefinition->customAttributeStart;
  613. metadataImage->entryPointIndex = imageDefinition->entryPointIndex;
  614. metadataImage->exportedTypeStart = imageDefinition->exportedTypeStart;
  615. metadataImage->image = image;
  616. image->metadataHandle = reinterpret_cast<Il2CppMetadataImageHandle>(metadataImage);
  617. }
  618. void il2cpp::vm::GlobalMetadata::BuildIl2CppAssembly(Il2CppAssembly* assembly, AssemblyIndex assemblyIndex, ImageIndex* assemblyImageIndex)
  619. {
  620. const Il2CppAssemblyDefinition* assemblyDefinitions = (const Il2CppAssemblyDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->assembliesOffset);
  621. const Il2CppAssemblyDefinition* assemblyDefinition = assemblyDefinitions + assemblyIndex;
  622. assembly->token = assemblyDefinition->token;
  623. assembly->referencedAssemblyStart = assemblyDefinition->referencedAssemblyStart;
  624. assembly->referencedAssemblyCount = assemblyDefinition->referencedAssemblyCount;
  625. Il2CppAssemblyName* assemblyName = &assembly->aname;
  626. const Il2CppAssemblyNameDefinition* assemblyNameDefinition = &assemblyDefinition->aname;
  627. assemblyName->name = GetStringFromIndex(assemblyNameDefinition->nameIndex);
  628. assemblyName->culture = GetStringFromIndex(assemblyNameDefinition->cultureIndex);
  629. assemblyName->public_key = (const uint8_t*)GetStringFromIndex(assemblyNameDefinition->publicKeyIndex);
  630. assemblyName->hash_alg = assemblyNameDefinition->hash_alg;
  631. assemblyName->hash_len = assemblyNameDefinition->hash_len;
  632. assemblyName->flags = assemblyNameDefinition->flags;
  633. assemblyName->major = assemblyNameDefinition->major;
  634. assemblyName->minor = assemblyNameDefinition->minor;
  635. assemblyName->build = assemblyNameDefinition->build;
  636. assemblyName->revision = assemblyNameDefinition->revision;
  637. memcpy(assemblyName->public_key_token, assemblyNameDefinition->public_key_token, sizeof(assemblyNameDefinition->public_key_token));
  638. *assemblyImageIndex = assemblyDefinition->imageIndex;
  639. }
  640. static const Il2CppImageGlobalMetadata* GetImageMetadata(const Il2CppImage* image)
  641. {
  642. return reinterpret_cast<const Il2CppImageGlobalMetadata*>(image->metadataHandle);
  643. }
  644. const MethodInfo* il2cpp::vm::GlobalMetadata::GetAssemblyEntryPoint(const Il2CppImage* image)
  645. {
  646. const Il2CppImageGlobalMetadata* imageMetadata = GetImageMetadata(image);
  647. if (imageMetadata == NULL || imageMetadata->entryPointIndex == -1)
  648. return NULL;
  649. return GetMethodInfoFromMethodDefinitionIndex(imageMetadata->entryPointIndex);
  650. }
  651. Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetAssemblyTypeHandle(const Il2CppImage* image, AssemblyTypeIndex index)
  652. {
  653. if (hybridclr::metadata::IsInterpreterIndex(image->token))
  654. {
  655. return hybridclr::metadata::MetadataModule::GetAssemblyTypeHandleFromRawIndex(image, index);
  656. }
  657. const Il2CppImageGlobalMetadata* imageMetadata = GetImageMetadata(image);
  658. IL2CPP_ASSERT(index >= 0 && index < static_cast<AssemblyTypeIndex>(image->typeCount));
  659. TypeDefinitionIndex typeDefintionIndex = imageMetadata->typeStart + index;
  660. return GetTypeHandleFromIndex(typeDefintionIndex);
  661. }
  662. const Il2CppAssembly* il2cpp::vm::GlobalMetadata::GetReferencedAssembly(const Il2CppAssembly* assembly, int32_t referencedAssemblyTableIndex, const Il2CppAssembly assembliesTable[], int assembliesCount)
  663. {
  664. IL2CPP_ASSERT(referencedAssemblyTableIndex < assembly->referencedAssemblyCount);
  665. if (hybridclr::metadata::IsInterpreterImage(assembly->image))
  666. {
  667. return hybridclr::metadata::MetadataModule::GetImage(assembly->image)->GetReferencedAssembly(referencedAssemblyTableIndex, assembliesTable, assembliesCount);
  668. }
  669. referencedAssemblyTableIndex = assembly->referencedAssemblyStart + referencedAssemblyTableIndex;
  670. IL2CPP_ASSERT(referencedAssemblyTableIndex >= 0 && static_cast<uint32_t>(referencedAssemblyTableIndex) <= s_GlobalMetadataHeader->referencedAssembliesSize / sizeof(int32_t));
  671. const int32_t* referenceAssemblyIndicies = (const int32_t*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->referencedAssembliesOffset);
  672. return assembliesTable + referenceAssemblyIndicies[referencedAssemblyTableIndex];
  673. }
  674. Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetAssemblyExportedTypeHandle(const Il2CppImage* image, AssemblyExportedTypeIndex index)
  675. {
  676. if (hybridclr::metadata::IsInterpreterIndex(index))
  677. {
  678. return hybridclr::metadata::MetadataModule::GetAssemblyExportedTypeHandleFromEncodeIndex(index);
  679. }
  680. if (index == kTypeDefinitionIndexInvalid)
  681. return NULL;
  682. IL2CPP_ASSERT(index >= 0 && index < static_cast<AssemblyExportedTypeIndex>(image->exportedTypeCount));
  683. const Il2CppImageGlobalMetadata* imageMetadata = GetImageMetadata(image);
  684. int32_t exportedTypeIndex = imageMetadata->exportedTypeStart + index;
  685. IL2CPP_ASSERT(exportedTypeIndex >= 0 && static_cast<uint32_t>(exportedTypeIndex) < s_GlobalMetadataHeader->exportedTypeDefinitionsSize / sizeof(TypeDefinitionIndex));
  686. TypeDefinitionIndex* exportedTypes = (TypeDefinitionIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->exportedTypeDefinitionsOffset);
  687. TypeDefinitionIndex typeDefintionIndex = *(exportedTypes + exportedTypeIndex);
  688. return GetTypeHandleFromIndex(typeDefintionIndex);
  689. }
  690. static const Il2CppTypeDefinition* GetTypeDefinitionForIndex(TypeDefinitionIndex index)
  691. {
  692. if (index == kTypeDefinitionIndexInvalid)
  693. return NULL;
  694. if (hybridclr::metadata::IsInterpreterIndex(index))
  695. {
  696. return (const Il2CppTypeDefinition*)hybridclr::metadata::MetadataModule::GetAssemblyTypeHandleFromEncodeIndex(index);
  697. }
  698. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition));
  699. const Il2CppTypeDefinition* typeDefinitions = (const Il2CppTypeDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->typeDefinitionsOffset);
  700. return typeDefinitions + index;
  701. }
  702. static TypeDefinitionIndex GetIndexForTypeDefinitionInternal(const Il2CppTypeDefinition* typeDefinition)
  703. {
  704. IL2CPP_ASSERT(typeDefinition);
  705. if (hybridclr::metadata::IsInterpreterType(typeDefinition))
  706. {
  707. return static_cast<TypeDefinitionIndex>(hybridclr::metadata::MetadataModule::GetTypeEncodeIndex(typeDefinition));
  708. }
  709. const Il2CppTypeDefinition* typeDefinitions = (const Il2CppTypeDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->typeDefinitionsOffset);
  710. IL2CPP_ASSERT(typeDefinition >= typeDefinitions && typeDefinition < typeDefinitions + s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition));
  711. ptrdiff_t index = typeDefinition - typeDefinitions;
  712. IL2CPP_ASSERT(index <= std::numeric_limits<TypeDefinitionIndex>::max());
  713. return static_cast<TypeDefinitionIndex>(index);
  714. }
  715. Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeDefinitionIndex(TypeDefinitionIndex index)
  716. {
  717. if (hybridclr::metadata::IsInterpreterIndex(index))
  718. {
  719. return hybridclr::metadata::MetadataModule::GetTypeInfoFromTypeDefinitionEncodeIndex(index);
  720. }
  721. if (index == kTypeIndexInvalid)
  722. return NULL;
  723. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition));
  724. return utils::InitOnce(&s_TypeInfoDefinitionTable[index], &il2cpp::vm::g_MetadataLock, [index](il2cpp::os::FastAutoLock& _) { return FromTypeDefinition(index); });
  725. }
  726. Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle(Il2CppMetadataTypeHandle handle)
  727. {
  728. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(handle);
  729. return GetTypeInfoFromTypeDefinitionIndex(GetIndexForTypeDefinitionInternal(typeDefinition));
  730. }
  731. Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromType(const Il2CppType* type)
  732. {
  733. return GetTypeInfoFromHandle(type->data.typeHandle);
  734. }
  735. const Il2CppType* il2cpp::vm::GlobalMetadata::GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex offset)
  736. {
  737. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  738. return GetInterfaceFromOffset(typeDefinition, offset);
  739. }
  740. const Il2CppType* il2cpp::vm::GlobalMetadata::GetInterfaceFromOffset(const Il2CppTypeDefinition* typeDefinition, TypeInterfaceIndex offset)
  741. {
  742. IL2CPP_ASSERT(offset >= 0 && offset < typeDefinition->interfaces_count);
  743. if (hybridclr::metadata::IsInterpreterType(typeDefinition))
  744. {
  745. return hybridclr::metadata::MetadataModule::GetInterfaceFromOffset(typeDefinition, offset);
  746. }
  747. InterfacesIndex index = typeDefinition->interfacesStart + offset;
  748. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->interfacesSize / sizeof(TypeIndex));
  749. const TypeIndex* interfaceIndices = (const TypeIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->interfacesOffset);
  750. return GetIl2CppTypeFromIndex(interfaceIndices[index]);
  751. }
  752. Il2CppInterfaceOffsetInfo il2cpp::vm::GlobalMetadata::GetInterfaceOffsetInfo(const Il2CppClass* klass, TypeInterfaceOffsetIndex index)
  753. {
  754. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  755. return GetInterfaceOffsetInfo(typeDefinition, index);
  756. }
  757. Il2CppInterfaceOffsetInfo il2cpp::vm::GlobalMetadata::GetInterfaceOffsetInfo(const Il2CppTypeDefinition* typeDefine, TypeInterfaceOffsetIndex index)
  758. {
  759. IL2CPP_ASSERT(index >= 0 && index < typeDefine->interface_offsets_count);
  760. if (hybridclr::metadata::IsInterpreterType(typeDefine))
  761. {
  762. return hybridclr::metadata::MetadataModule::GetInterfaceOffsetInfo(typeDefine, index);
  763. }
  764. index = index + typeDefine->interfaceOffsetsStart;
  765. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->interfaceOffsetsSize / sizeof(Il2CppInterfaceOffsetPair));
  766. const Il2CppInterfaceOffsetPair* interfaceOffsets = (const Il2CppInterfaceOffsetPair*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->interfaceOffsetsOffset);
  767. return
  768. {
  769. GetIl2CppTypeFromIndex(interfaceOffsets[index].interfaceTypeIndex),
  770. interfaceOffsets[index].offset
  771. };
  772. }
  773. Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(TypeDefinitionIndex typeIndex)
  774. {
  775. return reinterpret_cast<Il2CppMetadataTypeHandle>(GetTypeDefinitionForIndex(typeIndex));
  776. }
  777. Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetTypeHandleFromType(const Il2CppType* type)
  778. {
  779. IL2CPP_ASSERT(type->type == IL2CPP_TYPE_CLASS || type->type == IL2CPP_TYPE_VALUETYPE);
  780. return type->data.typeHandle;
  781. }
  782. bool il2cpp::vm::GlobalMetadata::TypeIsNested(Il2CppMetadataTypeHandle handle)
  783. {
  784. return reinterpret_cast<const Il2CppTypeDefinition*>(handle)->declaringTypeIndex != kTypeIndexInvalid;
  785. }
  786. bool il2cpp::vm::GlobalMetadata::TypeIsValueType(Il2CppMetadataTypeHandle handle)
  787. {
  788. return (reinterpret_cast<const Il2CppTypeDefinition*>(handle)->bitfield >> (kBitIsValueType - 1)) & 0x1;
  789. }
  790. bool il2cpp::vm::GlobalMetadata::StructLayoutPackIsDefault(Il2CppMetadataTypeHandle handle)
  791. {
  792. return (reinterpret_cast<const Il2CppTypeDefinition*>(handle)->bitfield >> (kPackingSizeIsDefault - 1)) & 0x1;
  793. }
  794. bool il2cpp::vm::GlobalMetadata::StructLayoutSizeIsDefault(Il2CppMetadataTypeHandle handle)
  795. {
  796. return (reinterpret_cast<const Il2CppTypeDefinition*>(handle)->bitfield >> (kClassSizeIsDefault - 1)) & 0x1;
  797. }
  798. std::pair<const char*, const char*> il2cpp::vm::GlobalMetadata::GetTypeNamespaceAndName(Il2CppMetadataTypeHandle handle)
  799. {
  800. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(handle);
  801. return std::make_pair
  802. (
  803. GetStringFromIndex(typeDefinition->namespaceIndex),
  804. GetStringFromIndex(typeDefinition->nameIndex)
  805. );
  806. }
  807. Il2CppClass* il2cpp::vm::GlobalMetadata::GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset)
  808. {
  809. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  810. IL2CPP_ASSERT(offset >= 0 && offset < typeDefinition->nested_type_count);
  811. if (hybridclr::metadata::IsInterpreterType(klass))
  812. {
  813. return hybridclr::metadata::MetadataModule::GetNestedTypeFromOffset(klass, offset);
  814. }
  815. NestedTypeIndex index = typeDefinition->nestedTypesStart + offset;
  816. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->nestedTypesSize / sizeof(TypeDefinitionIndex));
  817. const TypeDefinitionIndex* nestedTypeIndices = (const TypeDefinitionIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->nestedTypesOffset);
  818. return GetTypeInfoFromTypeDefinitionIndex(nestedTypeIndices[index]);
  819. }
  820. Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetNestedTypes(Il2CppMetadataTypeHandle handle, void** iter)
  821. {
  822. if (!iter)
  823. return NULL;
  824. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(handle);
  825. if (hybridclr::metadata::IsInterpreterType(typeDefinition))
  826. {
  827. return hybridclr::metadata::MetadataModule::GetNestedTypes(handle, iter);
  828. }
  829. const TypeDefinitionIndex* nestedTypeIndices = (const TypeDefinitionIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->nestedTypesOffset);
  830. if (!*iter)
  831. {
  832. if (typeDefinition->nested_type_count == 0)
  833. return NULL;
  834. *iter = (void*)(nestedTypeIndices + typeDefinition->nestedTypesStart);
  835. return GetTypeHandleFromIndex(nestedTypeIndices[typeDefinition->nestedTypesStart]);
  836. }
  837. TypeDefinitionIndex* nestedTypeAddress = (TypeDefinitionIndex*)*iter;
  838. nestedTypeAddress++;
  839. ptrdiff_t index = nestedTypeAddress - nestedTypeIndices;
  840. if (index < typeDefinition->nestedTypesStart + typeDefinition->nested_type_count)
  841. {
  842. *iter = nestedTypeAddress;
  843. return GetTypeHandleFromIndex(*nestedTypeAddress);
  844. }
  845. return NULL;
  846. }
  847. static void InitializeCustomAttributesCaches(void* param)
  848. {
  849. s_CustomAttributesCount = 0;
  850. for (int i = 0; i < s_MetadataImagesCount; i++)
  851. {
  852. s_CustomAttributesCount += s_MetadataImagesTable[i].image->customAttributeCount;
  853. }
  854. }
  855. static int CompareTokens(const void* pkey, const void* pelem)
  856. {
  857. return (int)(((Il2CppCustomAttributeDataRange*)pkey)->token - ((Il2CppCustomAttributeDataRange*)pelem)->token);
  858. }
  859. static const Il2CppImageGlobalMetadata* GetImageForCustomAttributeIndex(CustomAttributeIndex index)
  860. {
  861. if (hybridclr::metadata::IsInterpreterIndex(index))
  862. {
  863. return reinterpret_cast<const Il2CppImageGlobalMetadata*>(hybridclr::metadata::MetadataModule::GetImageByEncodedIndex(index)->GetIl2CppImage()->metadataHandle);
  864. }
  865. for (int32_t imageIndex = 0; imageIndex < s_MetadataImagesCount; imageIndex++)
  866. {
  867. const Il2CppImageGlobalMetadata* imageMetadta = s_MetadataImagesTable + imageIndex;
  868. IL2CPP_ASSERT(index >= 0);
  869. if (index >= imageMetadta->customAttributeStart && static_cast<uint32_t>(index) < (imageMetadta->customAttributeStart + imageMetadta->image->customAttributeCount))
  870. return imageMetadta;
  871. }
  872. IL2CPP_ASSERT(0 && "Failed to find owning image for custom attribute index");
  873. return NULL;
  874. }
  875. static CustomAttributeIndex GetCustomAttributeIndex(const Il2CppCustomAttributeDataRange* attrDataRange)
  876. {
  877. if (attrDataRange == NULL)
  878. return kCustomAttributeIndexInvalid;
  879. if (hybridclr::metadata::IsInterpreterIndex(GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(*attrDataRange)))
  880. {
  881. return hybridclr::metadata::MetadataModule::GetImage(hybridclr::metadata::DecodeImageIndex(GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(*attrDataRange)))
  882. ->GetCustomAttributeIndex(attrDataRange->token);
  883. }
  884. const Il2CppCustomAttributeDataRange* attributeTypeRangeStart = MetadataOffset<const Il2CppCustomAttributeDataRange*>(s_GlobalMetadata, s_GlobalMetadataHeader->attributeDataRangeOffset, 0);
  885. CustomAttributeIndex index = (CustomAttributeIndex)(attrDataRange - attributeTypeRangeStart);
  886. IL2CPP_ASSERT(index >= 0 && index < (CustomAttributeIndex)(s_GlobalMetadataHeader->attributeDataRangeSize / sizeof(Il2CppCustomAttributeDataRange)));
  887. return index;
  888. }
  889. static const Il2CppImage* GetCustomAttributeImageFromHandle(Il2CppMetadataCustomAttributeHandle handle)
  890. {
  891. if (handle == NULL)
  892. return NULL;
  893. const Il2CppCustomAttributeDataRange *dataRange = reinterpret_cast<const Il2CppCustomAttributeDataRange*>(handle);
  894. CustomAttributeIndex index = GetCustomAttributeIndex(dataRange);
  895. const Il2CppImageGlobalMetadata* imageMetadata = GetImageForCustomAttributeIndex(index);
  896. if (imageMetadata == NULL)
  897. return NULL;
  898. return imageMetadata->image;
  899. }
  900. static CustomAttributeIndex GetCustomAttributeIndex(const Il2CppImage* image, uint32_t token)
  901. {
  902. const Il2CppCustomAttributeDataRange* attrDataRange = reinterpret_cast<const Il2CppCustomAttributeDataRange*>(il2cpp::vm::GlobalMetadata::GetCustomAttributeTypeToken(image, token));
  903. return GetCustomAttributeIndex(attrDataRange);
  904. }
  905. Il2CppMetadataCustomAttributeHandle il2cpp::vm::GlobalMetadata::GetCustomAttributeTypeToken(const Il2CppImage* image, uint32_t token)
  906. {
  907. if (hybridclr::metadata::IsInterpreterImage(image))
  908. {
  909. return hybridclr::metadata::MetadataModule::GetImage(image)->GetCustomAttributeTypeToken(token);
  910. }
  911. const Il2CppCustomAttributeDataRange* attributeTypeRange = MetadataOffset<const Il2CppCustomAttributeDataRange*>(s_GlobalMetadata, s_GlobalMetadataHeader->attributeDataRangeOffset, 0);
  912. Il2CppCustomAttributeDataRange key = {0};
  913. key.token = token;
  914. const Il2CppImageGlobalMetadata* imageMetadata = GetImageMetadata(image);
  915. const Il2CppCustomAttributeDataRange* res = (const Il2CppCustomAttributeDataRange*)bsearch(&key, attributeTypeRange + imageMetadata->customAttributeStart, image->customAttributeCount, sizeof(Il2CppCustomAttributeDataRange), CompareTokens);
  916. return reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(res);
  917. }
  918. static il2cpp::metadata::CustomAttributeDataReader CreateCustomAttributeDataReader(Il2CppMetadataCustomAttributeHandle handle, const Il2CppImage* image)
  919. {
  920. if (handle == NULL)
  921. return il2cpp::metadata::CustomAttributeDataReader::Empty();
  922. if (hybridclr::metadata::IsInterpreterImage(image))
  923. {
  924. return hybridclr::metadata::MetadataModule::GetImage(image)->CreateCustomAttributeDataReader(handle);
  925. }
  926. Il2CppCustomAttributeDataRange* range = (Il2CppCustomAttributeDataRange*)handle;
  927. const Il2CppCustomAttributeDataRange* next = range + 1;
  928. void* start = MetadataOffset<uint8_t*>(s_GlobalMetadata, s_GlobalMetadataHeader->attributeDataOffset, range->startOffset);
  929. void* end = MetadataOffset<uint8_t*>(s_GlobalMetadata, s_GlobalMetadataHeader->attributeDataOffset, next->startOffset);
  930. return il2cpp::metadata::CustomAttributeDataReader(image, start, end);
  931. }
  932. il2cpp::metadata::CustomAttributeDataReader il2cpp::vm::GlobalMetadata::GetCustomAttributeDataReader(const Il2CppImage* image, uint32_t token)
  933. {
  934. return CreateCustomAttributeDataReader(GetCustomAttributeTypeToken(image, token), image);
  935. }
  936. il2cpp::metadata::CustomAttributeDataReader il2cpp::vm::GlobalMetadata::GetCustomAttributeDataReader(Il2CppMetadataCustomAttributeHandle handle)
  937. {
  938. return CreateCustomAttributeDataReader(handle, GetCustomAttributeImageFromHandle(handle));
  939. }
  940. const Il2CppMethodDefinition* il2cpp::vm::GlobalMetadata::GetMethodDefinitionFromVTableSlot(const Il2CppTypeDefinition* typeDefinition, int32_t vTableSlot)
  941. {
  942. if (hybridclr::metadata::IsInterpreterType(typeDefinition))
  943. {
  944. return hybridclr::metadata::MetadataModule::GetMethodDefinitionFromVTableSlot(typeDefinition, vTableSlot);
  945. }
  946. uint32_t index = typeDefinition->vtableStart + vTableSlot;
  947. IL2CPP_ASSERT(index >= 0 && index <= s_GlobalMetadataHeader->vtableMethodsSize / sizeof(EncodedMethodIndex));
  948. const EncodedMethodIndex* vTableMethodReferences = (const EncodedMethodIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->vtableMethodsOffset);
  949. EncodedMethodIndex vTableMethodReference = vTableMethodReferences[index];
  950. if (vTableMethodReference == 0)
  951. {
  952. return nullptr;
  953. }
  954. IL2CPP_ASSERT(vTableMethodReference != 0);
  955. return GetMethodDefinitionFromEncodedIndex(vTableMethodReference);
  956. }
  957. const MethodInfo* il2cpp::vm::GlobalMetadata::GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot)
  958. {
  959. if (hybridclr::metadata::IsInterpreterType(klass))
  960. {
  961. return hybridclr::metadata::MetadataModule::GetMethodInfoFromVTableSlot(klass, vTableSlot);
  962. }
  963. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  964. uint32_t index = typeDefinition->vtableStart + vTableSlot;
  965. IL2CPP_ASSERT(index >= 0 && index <= s_GlobalMetadataHeader->vtableMethodsSize / sizeof(EncodedMethodIndex));
  966. const EncodedMethodIndex* vTableMethodReferences = (const EncodedMethodIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->vtableMethodsOffset);
  967. EncodedMethodIndex vTableMethodReference = vTableMethodReferences[index];
  968. return GetMethodInfoFromEncodedIndex(vTableMethodReference);
  969. }
  970. static int CompareFieldDefaultValues(const void* pkey, const void* pelem)
  971. {
  972. return (int)(((Il2CppFieldDefaultValue*)pkey)->fieldIndex - ((Il2CppFieldDefaultValue*)pelem)->fieldIndex);
  973. }
  974. static const Il2CppFieldDefaultValue* GetFieldDefaultValueEntry(const FieldInfo* field)
  975. {
  976. Il2CppClass* parent = field->parent;
  977. FieldIndex fieldIndex = (FieldIndex)(field - parent->fields);
  978. if (il2cpp::vm::Type::IsGenericInstance(&parent->byval_arg))
  979. parent = il2cpp::vm::GenericClass::GetTypeDefinition(parent->generic_class);
  980. fieldIndex += reinterpret_cast<const Il2CppTypeDefinition*>(parent->typeMetadataHandle)->fieldStart;
  981. if (hybridclr::metadata::IsInterpreterIndex((uint32_t)fieldIndex))
  982. {
  983. return hybridclr::metadata::MetadataModule::GetFieldDefaultValueEntry((uint32_t)fieldIndex);
  984. }
  985. Il2CppFieldDefaultValue key;
  986. key.fieldIndex = fieldIndex;
  987. const Il2CppFieldDefaultValue* start = (const Il2CppFieldDefaultValue*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->fieldDefaultValuesOffset);
  988. const Il2CppFieldDefaultValue* res = (const Il2CppFieldDefaultValue*)bsearch(&key, start, s_GlobalMetadataHeader->fieldDefaultValuesSize / sizeof(Il2CppFieldDefaultValue), sizeof(Il2CppFieldDefaultValue), CompareFieldDefaultValues);
  989. return res;
  990. }
  991. static const uint8_t* GetFieldOrParameterDefalutValue(uint32_t index)
  992. {
  993. if (index == kDefaultValueIndexNull)
  994. return NULL;
  995. if (hybridclr::metadata::IsInterpreterIndex(index))
  996. {
  997. return hybridclr::metadata::MetadataModule::GetFieldOrParameterDefalutValue(index);
  998. }
  999. IL2CPP_ASSERT(index >= 0 && index <= s_GlobalMetadataHeader->fieldAndParameterDefaultValueDataSize / sizeof(uint8_t));
  1000. const uint8_t* defaultValuesData = (const uint8_t*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->fieldAndParameterDefaultValueDataOffset);
  1001. return defaultValuesData + index;
  1002. }
  1003. const uint8_t* il2cpp::vm::GlobalMetadata::GetFieldDefaultValue(const FieldInfo* field, const Il2CppType** type)
  1004. {
  1005. const Il2CppFieldDefaultValue* entry = GetFieldDefaultValueEntry(field);
  1006. if (entry != NULL)
  1007. {
  1008. *type = GetIl2CppTypeFromIndex(entry->typeIndex);
  1009. return GetFieldOrParameterDefalutValue(entry->dataIndex);
  1010. }
  1011. return NULL;
  1012. }
  1013. static int CompareParameterDefaultValues(const void* pkey, const void* pelem)
  1014. {
  1015. return (int)(((Il2CppParameterDefaultValue*)pkey)->parameterIndex - ((Il2CppParameterDefaultValue*)pelem)->parameterIndex);
  1016. }
  1017. static const Il2CppParameterDefaultValue * GetParameterDefaultValueEntry(const MethodInfo* method, int32_t parameterPosition)
  1018. {
  1019. if (il2cpp::vm::Method::IsGenericInstance(method))
  1020. method = il2cpp::vm::MetadataCache::GetGenericMethodDefinition(method);
  1021. IL2CPP_ASSERT(!il2cpp::vm::Method::IsGenericInstance(method));
  1022. const Il2CppMethodDefinition* methodDefinition = reinterpret_cast<const Il2CppMethodDefinition*>(method->methodMetadataHandle);
  1023. if (methodDefinition == NULL)
  1024. return NULL;
  1025. ParameterIndex parameterIndex = methodDefinition->parameterStart + parameterPosition;
  1026. if (hybridclr::metadata::IsInterpreterMethod(method))
  1027. {
  1028. return hybridclr::metadata::MetadataModule::GetImage(method->klass)
  1029. ->GetParameterDefaultValueEntryByRawIndex(parameterIndex);
  1030. }
  1031. Il2CppParameterDefaultValue key;
  1032. key.parameterIndex = parameterIndex;
  1033. const Il2CppParameterDefaultValue* start = (const Il2CppParameterDefaultValue*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->parameterDefaultValuesOffset);
  1034. const Il2CppParameterDefaultValue* res = (const Il2CppParameterDefaultValue*)bsearch(&key, start, s_GlobalMetadataHeader->parameterDefaultValuesSize / sizeof(Il2CppParameterDefaultValue), sizeof(Il2CppParameterDefaultValue), CompareParameterDefaultValues);
  1035. return res;
  1036. }
  1037. const uint8_t* il2cpp::vm::GlobalMetadata::GetParameterDefaultValue(const MethodInfo* method, int32_t parameterPosition, const Il2CppType** type, bool* isExplicitySetNullDefaultValue)
  1038. {
  1039. *isExplicitySetNullDefaultValue = false;
  1040. const Il2CppParameterDefaultValue* parameterDefaultValue = GetParameterDefaultValueEntry(method, parameterPosition);
  1041. if (parameterDefaultValue != NULL)
  1042. {
  1043. *type = GetIl2CppTypeFromIndex(parameterDefaultValue->typeIndex);
  1044. *isExplicitySetNullDefaultValue = parameterDefaultValue->dataIndex == kDefaultValueIndexNull;
  1045. return GetFieldOrParameterDefalutValue(parameterDefaultValue->dataIndex);
  1046. }
  1047. return NULL;
  1048. }
  1049. TypeDefinitionIndex il2cpp::vm::GlobalMetadata::GetIndexForTypeDefinition(const Il2CppClass* klass)
  1050. {
  1051. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  1052. return GetIndexForTypeDefinitionInternal(typeDefinition);
  1053. }
  1054. TypeDefinitionIndex il2cpp::vm::GlobalMetadata::GetIndexForTypeDefinition(const Il2CppTypeDefinition* typeDef)
  1055. {
  1056. return GetIndexForTypeDefinitionInternal(typeDef);
  1057. }
  1058. uint32_t il2cpp::vm::GlobalMetadata::GetFieldOffset(const Il2CppClass* klass, int32_t fieldIndexInType, FieldInfo* field)
  1059. {
  1060. if (hybridclr::metadata::IsInterpreterType(klass))
  1061. {
  1062. return hybridclr::metadata::MetadataModule::GetFieldOffset(klass, fieldIndexInType, field);
  1063. }
  1064. uint32_t typeIndex = GetIndexForTypeDefinition(klass);
  1065. IL2CPP_ASSERT(typeIndex <= static_cast<uint32_t>(s_Il2CppMetadataRegistration->typeDefinitionsSizesCount));
  1066. int32_t offset = s_Il2CppMetadataRegistration->fieldOffsets[typeIndex][fieldIndexInType];
  1067. return offset;
  1068. }
  1069. static int CompareFieldMarshaledSize(const void* pkey, const void* pelem)
  1070. {
  1071. return (int)(((Il2CppFieldMarshaledSize*)pkey)->fieldIndex - ((Il2CppFieldMarshaledSize*)pelem)->fieldIndex);
  1072. }
  1073. int il2cpp::vm::GlobalMetadata::GetFieldMarshaledSizeForField(const FieldInfo* field)
  1074. {
  1075. if (hybridclr::metadata::IsInterpreterType(field->parent))
  1076. {
  1077. return -1;
  1078. }
  1079. Il2CppClass* parent = field->parent;
  1080. size_t fieldIndex = (field - parent->fields);
  1081. if (il2cpp::vm::Type::IsGenericInstance(&parent->byval_arg))
  1082. parent = il2cpp::vm::GenericClass::GetTypeDefinition(parent->generic_class);
  1083. fieldIndex += reinterpret_cast<const Il2CppTypeDefinition*>(parent->typeMetadataHandle)->fieldStart;
  1084. const Il2CppFieldMarshaledSize *start = (const Il2CppFieldMarshaledSize*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->fieldMarshaledSizesOffset);
  1085. Il2CppFieldMarshaledSize key;
  1086. key.fieldIndex = (FieldIndex)fieldIndex;
  1087. const Il2CppFieldMarshaledSize* res = (const Il2CppFieldMarshaledSize*)bsearch(&key, start, s_GlobalMetadataHeader->fieldMarshaledSizesSize / sizeof(Il2CppFieldMarshaledSize), sizeof(Il2CppFieldMarshaledSize), CompareFieldMarshaledSize);
  1088. if (res != NULL)
  1089. return res->size;
  1090. return -1;
  1091. }
  1092. static const Il2CppFieldDefinition* GetFieldDefinitionFromIndex(FieldIndex index)
  1093. {
  1094. if (hybridclr::metadata::IsInterpreterIndex(index))
  1095. {
  1096. return hybridclr::metadata::MetadataModule::GetFieldDefinitionFromEncodeIndex(index);
  1097. }
  1098. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->fieldsSize / sizeof(Il2CppFieldDefinition)); const Il2CppFieldDefinition* fields = (const Il2CppFieldDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->fieldsOffset);
  1099. return fields + index;
  1100. }
  1101. const Il2CppFieldDefinition* il2cpp::vm::GlobalMetadata::GetFieldDefinitionFromTypeDefAndFieldIndex(const Il2CppTypeDefinition* typeDef, FieldIndex index)
  1102. {
  1103. return GetFieldDefinitionFromIndex(typeDef->fieldStart + index);
  1104. }
  1105. Il2CppMetadataFieldInfo il2cpp::vm::GlobalMetadata::GetFieldInfo(const Il2CppClass* klass, TypeFieldIndex fieldIndex)
  1106. {
  1107. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  1108. IL2CPP_ASSERT(typeDefinition != NULL);
  1109. IL2CPP_ASSERT(fieldIndex >= 0 && fieldIndex < typeDefinition->field_count);
  1110. IL2CPP_ASSERT(typeDefinition->fieldStart != kFieldIndexInvalid);
  1111. const Il2CppFieldDefinition* fieldDefinition = GetFieldDefinitionFromIndex(typeDefinition->fieldStart + fieldIndex);
  1112. return {
  1113. GetIl2CppTypeFromIndex(fieldDefinition->typeIndex),
  1114. GetStringFromIndex(fieldDefinition->nameIndex),
  1115. fieldDefinition->token
  1116. };
  1117. }
  1118. Il2CppMetadataMethodInfo il2cpp::vm::GlobalMetadata::GetMethodInfo(const Il2CppClass* klass, TypeMethodIndex index)
  1119. {
  1120. const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  1121. IL2CPP_ASSERT(typeDefinition != NULL);
  1122. IL2CPP_ASSERT(index >= 0 && index < typeDefinition->method_count);
  1123. IL2CPP_ASSERT(typeDefinition->methodStart != kMethodIndexInvalid);
  1124. const Il2CppMethodDefinition* methodDefinition = GetMethodDefinitionFromIndex(typeDefinition->methodStart + index);
  1125. return {
  1126. reinterpret_cast<Il2CppMetadataMethodDefinitionHandle>(methodDefinition),
  1127. GetStringFromIndex(methodDefinition->nameIndex),
  1128. GetIl2CppTypeFromIndex(methodDefinition->returnType),
  1129. methodDefinition->token,
  1130. methodDefinition->flags,
  1131. methodDefinition->iflags,
  1132. methodDefinition->slot,
  1133. methodDefinition->parameterCount,
  1134. };
  1135. }
  1136. Il2CppMetadataParameterInfo il2cpp::vm::GlobalMetadata::GetParameterInfo(const Il2CppClass* klass, Il2CppMetadataMethodDefinitionHandle handle, MethodParameterIndex paramIndex)
  1137. {
  1138. const Il2CppMethodDefinition* methodDefinition = reinterpret_cast<const Il2CppMethodDefinition*>(handle);
  1139. IL2CPP_ASSERT(methodDefinition != NULL);
  1140. IL2CPP_ASSERT(paramIndex >= 0 && paramIndex < methodDefinition->parameterCount);
  1141. const Il2CppParameterDefinition* parameterDefinition = GetParameterDefinitionFromIndex(klass, methodDefinition->parameterStart + paramIndex);
  1142. return {
  1143. GetStringFromIndex(parameterDefinition->nameIndex),
  1144. parameterDefinition->token,
  1145. GetIl2CppTypeFromIndex(parameterDefinition->typeIndex),
  1146. };
  1147. }
  1148. Il2CppMetadataPropertyInfo il2cpp::vm::GlobalMetadata::GetPropertyInfo(const Il2CppClass* klass, TypePropertyIndex index)
  1149. {
  1150. if (hybridclr::metadata::IsInterpreterType(klass))
  1151. {
  1152. return hybridclr::metadata::MetadataModule::GetImage(klass)->GetPropertyInfo(klass, index);
  1153. }
  1154. const Il2CppTypeDefinition* typeDefintion = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  1155. IL2CPP_ASSERT(typeDefintion != NULL);
  1156. IL2CPP_ASSERT(index >= 0 && index < typeDefintion->property_count);
  1157. IL2CPP_ASSERT(typeDefintion->propertyStart != kPropertyIndexInvalid);
  1158. const Il2CppPropertyDefinition* propertyDefintion = GetPropertyDefinitionFromIndex(klass->image, typeDefintion->propertyStart + index);
  1159. return {
  1160. GetStringFromIndex(propertyDefintion->nameIndex),
  1161. propertyDefintion->get != kMethodIndexInvalid ? Class::GetOrSetupOneMethod(const_cast<Il2CppClass*>(klass),propertyDefintion->get) : NULL,
  1162. propertyDefintion->set != kMethodIndexInvalid ? Class::GetOrSetupOneMethod(const_cast<Il2CppClass*>(klass),propertyDefintion->set) : NULL,
  1163. propertyDefintion->attrs,
  1164. propertyDefintion->token,
  1165. };
  1166. }
  1167. Il2CppMetadataEventInfo il2cpp::vm::GlobalMetadata::GetEventInfo(const Il2CppClass* klass, TypeEventIndex index)
  1168. {
  1169. if (hybridclr::metadata::IsInterpreterType(klass))
  1170. {
  1171. return hybridclr::metadata::MetadataModule::GetImage(klass)->GetEventInfo(klass, index);
  1172. }
  1173. const Il2CppTypeDefinition* typeDefintion = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
  1174. IL2CPP_ASSERT(typeDefintion != NULL);
  1175. IL2CPP_ASSERT(index >= 0 && index < typeDefintion->event_count);
  1176. const Il2CppEventDefinition* eventDefintion = GetEventDefinitionFromIndex(klass->image, typeDefintion->eventStart + index);
  1177. return {
  1178. GetStringFromIndex(eventDefintion->nameIndex),
  1179. GetIl2CppTypeFromIndex(eventDefintion->typeIndex),
  1180. eventDefintion->add != kMethodIndexInvalid ? Class::GetOrSetupOneMethod(const_cast<Il2CppClass*>(klass),eventDefintion->add) : NULL,
  1181. eventDefintion->remove != kMethodIndexInvalid ? Class::GetOrSetupOneMethod(const_cast<Il2CppClass*>(klass),eventDefintion->remove) : NULL,
  1182. eventDefintion->raise != kMethodIndexInvalid ? Class::GetOrSetupOneMethod(const_cast<Il2CppClass*>(klass),eventDefintion->raise) : NULL,
  1183. eventDefintion->token,
  1184. };
  1185. }
  1186. #if SUPPORT_METHOD_RETURN_TYPE_CUSTOM_ATTRIBUTE
  1187. uint32_t il2cpp::vm::GlobalMetadata::GetReturnParameterToken(Il2CppMetadataMethodDefinitionHandle handle)
  1188. {
  1189. const Il2CppMethodDefinition* methodDefinition = reinterpret_cast<const Il2CppMethodDefinition*>(handle);
  1190. IL2CPP_ASSERT(methodDefinition != NULL);
  1191. return methodDefinition->returnParameterToken;
  1192. }
  1193. #endif
  1194. static const Il2CppGenericContainer* GetGenericContainerFromIndexInternal(GenericContainerIndex index)
  1195. {
  1196. if (index == kGenericContainerIndexInvalid)
  1197. return NULL;
  1198. if (hybridclr::metadata::IsInterpreterIndex(index))
  1199. {
  1200. return hybridclr::metadata::MetadataModule::GetGenericContainerFromEncodeIndex(index);
  1201. }
  1202. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->genericContainersSize / sizeof(Il2CppGenericContainer)); const Il2CppGenericContainer* genericContainers = (const Il2CppGenericContainer*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->genericContainersOffset);
  1203. return genericContainers + index;
  1204. }
  1205. Il2CppMetadataGenericContainerHandle il2cpp::vm::GlobalMetadata::GetGenericContainerFromIndex(GenericContainerIndex index)
  1206. {
  1207. const Il2CppGenericContainer* container = GetGenericContainerFromIndexInternal(index);
  1208. return reinterpret_cast<Il2CppMetadataGenericContainerHandle>(container);
  1209. }
  1210. Il2CppMetadataGenericContainerHandle il2cpp::vm::GlobalMetadata::GetGenericContainerFromGenericClass(const Il2CppGenericClass* genericClass)
  1211. {
  1212. const Il2CppTypeDefinition* genericType = reinterpret_cast<const Il2CppTypeDefinition*>(GetTypeHandleFromType(genericClass->type));
  1213. return GetGenericContainerFromIndex(genericType->genericContainerIndex);
  1214. }
  1215. Il2CppMetadataGenericContainerHandle il2cpp::vm::GlobalMetadata::GetGenericContainerFromMethod(Il2CppMetadataMethodDefinitionHandle handle)
  1216. {
  1217. const Il2CppMethodDefinition* methodDefinition = reinterpret_cast<const Il2CppMethodDefinition*>(handle);
  1218. return GetGenericContainerFromIndex(methodDefinition->genericContainerIndex);
  1219. }
  1220. const Il2CppGenericMethod* il2cpp::vm::GlobalMetadata::GetGenericMethodFromTokenMethodTuple(const Il2CppTokenIndexMethodTuple* tuple)
  1221. {
  1222. return GetGenericMethodFromIndex(tuple->__genericMethodIndex);
  1223. }
  1224. Il2CppMetadataGenericParameterHandle il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndexInternal(GenericParameterIndex index)
  1225. {
  1226. if (index == kGenericParameterIndexInvalid)
  1227. return NULL;
  1228. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->genericParametersSize / sizeof(Il2CppGenericParameter));
  1229. const Il2CppGenericParameter* genericParameters = (const Il2CppGenericParameter*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->genericParametersOffset);
  1230. return reinterpret_cast<Il2CppMetadataGenericParameterHandle>(genericParameters + index);
  1231. }
  1232. Il2CppMetadataGenericParameterHandle il2cpp::vm::GlobalMetadata::GetGenericParameterFromType(const Il2CppType* type)
  1233. {
  1234. IL2CPP_ASSERT(type->type == IL2CPP_TYPE_VAR || type->type == IL2CPP_TYPE_MVAR);
  1235. return type->data.genericParameterHandle;
  1236. }
  1237. Il2CppClass* il2cpp::vm::GlobalMetadata::GetContainerDeclaringType(Il2CppMetadataGenericContainerHandle handle)
  1238. {
  1239. const Il2CppGenericContainer* genericContainer = reinterpret_cast<const Il2CppGenericContainer*>(handle);
  1240. if (genericContainer->is_method)
  1241. return GetMethodInfoFromMethodDefinitionIndex(genericContainer->ownerIndex)->klass;
  1242. return GetTypeInfoFromTypeDefinitionIndex(static_cast<TypeDefinitionIndex>(genericContainer->ownerIndex));
  1243. }
  1244. Il2CppClass* il2cpp::vm::GlobalMetadata::GetParameterDeclaringType(Il2CppMetadataGenericParameterHandle handle)
  1245. {
  1246. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1247. const Il2CppGenericContainer* genericContainer = GetGenericContainerFromIndexInternal(genericParameter->ownerIndex);
  1248. if (genericContainer->is_method)
  1249. return GetMethodInfoFromMethodDefinitionIndex(genericContainer->ownerIndex)->klass;
  1250. return GetTypeInfoFromTypeDefinitionIndex(static_cast<TypeDefinitionIndex>(genericContainer->ownerIndex));
  1251. }
  1252. const MethodInfo* il2cpp::vm::GlobalMetadata::GetParameterDeclaringMethod(Il2CppMetadataGenericParameterHandle handle)
  1253. {
  1254. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1255. const Il2CppGenericContainer* genericContainer = GetGenericContainerFromIndexInternal(genericParameter->ownerIndex);
  1256. if (genericContainer->is_method)
  1257. return GetMethodInfoFromMethodDefinitionIndex(genericContainer->ownerIndex);
  1258. return NULL;
  1259. }
  1260. Il2CppMetadataGenericParameterHandle il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndex(Il2CppMetadataGenericContainerHandle handle, GenericContainerParameterIndex index)
  1261. {
  1262. const Il2CppGenericContainer* genericContainer = reinterpret_cast<const Il2CppGenericContainer*>(handle);
  1263. IL2CPP_ASSERT(index >= 0 && index < genericContainer->type_argc);
  1264. if (hybridclr::metadata::IsInterpreterIndex(genericContainer->ownerIndex))
  1265. {
  1266. return (Il2CppMetadataGenericParameterHandle)hybridclr::metadata::MetadataModule::GetImage(hybridclr::metadata::DecodeImageIndex(genericContainer->ownerIndex))->GetGenericParameterByRawIndex(genericContainer, index);
  1267. }
  1268. return GetGenericParameterFromIndexInternal(genericContainer->genericParameterStart + index);
  1269. }
  1270. const Il2CppType* il2cpp::vm::GlobalMetadata::GetGenericParameterConstraintFromIndex(Il2CppMetadataGenericParameterHandle handle, GenericParameterConstraintIndex index)
  1271. {
  1272. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1273. IL2CPP_ASSERT(index >= 0 && index < genericParameter->constraintsCount);
  1274. index = genericParameter->constraintsStart + index;
  1275. if (hybridclr::metadata::IsInterpreterIndex(genericParameter->ownerIndex))
  1276. {
  1277. return hybridclr::metadata::MetadataModule::GetImage(hybridclr::metadata::DecodeImageIndex(genericParameter->ownerIndex))
  1278. ->GetGenericParameterConstraintFromIndex(index);
  1279. }
  1280. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->genericParameterConstraintsSize / sizeof(TypeIndex));
  1281. const TypeIndex* constraintIndices = (const TypeIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->genericParameterConstraintsOffset);
  1282. return GetIl2CppTypeFromIndex(constraintIndices[index]);
  1283. }
  1284. static GenericParameterIndex GetIndexForGenericParameter(Il2CppMetadataGenericParameterHandle handle)
  1285. {
  1286. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1287. const Il2CppGenericParameter* genericParameters = (const Il2CppGenericParameter*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->genericParametersOffset);
  1288. IL2CPP_ASSERT(genericParameter >= genericParameters && genericParameter < genericParameters + s_GlobalMetadataHeader->genericParametersSize / sizeof(Il2CppGenericParameter));
  1289. ptrdiff_t index = genericParameter - genericParameters;
  1290. IL2CPP_ASSERT(index <= std::numeric_limits<GenericParameterIndex>::max());
  1291. return static_cast<GenericParameterIndex>(index);
  1292. }
  1293. const MethodInfo* il2cpp::vm::GlobalMetadata::GetGenericInstanceMethod(const MethodInfo* genericMethodDefinition, const Il2CppGenericContext* context)
  1294. {
  1295. const MethodInfo* method = genericMethodDefinition;
  1296. const Il2CppGenericInst* classInst = context->class_inst;
  1297. const Il2CppGenericInst* methodInst = context->method_inst;
  1298. if (genericMethodDefinition->is_inflated)
  1299. {
  1300. IL2CPP_ASSERT(genericMethodDefinition->klass->generic_class);
  1301. classInst = genericMethodDefinition->klass->generic_class->context.class_inst;
  1302. method = genericMethodDefinition->genericMethod->methodDefinition;
  1303. }
  1304. return il2cpp::metadata::GenericMethod::GetMethod(method, classInst, methodInst);
  1305. }
  1306. const Il2CppType* il2cpp::vm::GlobalMetadata::GetTypeFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  1307. {
  1308. IL2CPP_ASSERT(rgctxDef->type == IL2CPP_RGCTX_DATA_TYPE || rgctxDef->type == IL2CPP_RGCTX_DATA_CLASS);
  1309. return GetIl2CppTypeFromIndex(((const Il2CppRGCTXDefinitionData*)rgctxDef->data)->__typeIndex);
  1310. }
  1311. const Il2CppGenericMethod* il2cpp::vm::GlobalMetadata::GetGenericMethodFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  1312. {
  1313. IL2CPP_ASSERT(rgctxDef->type == IL2CPP_RGCTX_DATA_METHOD);
  1314. return GetGenericMethodFromIndex(((const Il2CppRGCTXDefinitionData*)rgctxDef->data)->__methodIndex);
  1315. }
  1316. std::pair<const Il2CppType*, const MethodInfo*> il2cpp::vm::GlobalMetadata::GetConstrainedCallFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef)
  1317. {
  1318. IL2CPP_ASSERT(rgctxDef->type == IL2CPP_RGCTX_DATA_CONSTRAINED);
  1319. const Il2CppRGCTXConstrainedData* constrainedData = (const Il2CppRGCTXConstrainedData*)rgctxDef->data;
  1320. const Il2CppType* type = GetIl2CppTypeFromIndex(constrainedData->__typeIndex);
  1321. const MethodInfo* method = GetMethodInfoFromEncodedIndex(constrainedData->__encodedMethodIndex);
  1322. return std::make_pair(type, method);
  1323. }
  1324. enum PackingSize
  1325. {
  1326. Zero,
  1327. One,
  1328. Two,
  1329. Four,
  1330. Eight,
  1331. Sixteen,
  1332. ThirtyTwo,
  1333. SixtyFour,
  1334. OneHundredTwentyEight
  1335. };
  1336. static uint8_t ConvertPackingSizeEnumToValue(PackingSize packingSize)
  1337. {
  1338. switch (packingSize)
  1339. {
  1340. case Zero:
  1341. return 0;
  1342. case One:
  1343. return 1;
  1344. case Two:
  1345. return 2;
  1346. case Four:
  1347. return 4;
  1348. case Eight:
  1349. return 8;
  1350. case Sixteen:
  1351. return 16;
  1352. case ThirtyTwo:
  1353. return 32;
  1354. case SixtyFour:
  1355. return 64;
  1356. case OneHundredTwentyEight:
  1357. return 128;
  1358. default:
  1359. Assert(0 && "Invalid packing size!");
  1360. return 0;
  1361. }
  1362. }
  1363. int32_t il2cpp::vm::GlobalMetadata::StructLayoutPack(Il2CppMetadataTypeHandle handle)
  1364. {
  1365. return ConvertPackingSizeEnumToValue(static_cast<PackingSize>((reinterpret_cast<const Il2CppTypeDefinition*>(handle)->bitfield >> (kSpecifiedPackingSize - 1)) & 0xF));
  1366. }
  1367. static const Il2CppImage* GetImageForTypeDefinitionIndex(TypeDefinitionIndex index)
  1368. {
  1369. if (hybridclr::metadata::IsInterpreterIndex(index))
  1370. {
  1371. return hybridclr::metadata::MetadataModule::GetImage(hybridclr::metadata::DecodeImageIndex(index))->GetIl2CppImage();
  1372. }
  1373. for (int32_t imageIndex = 0; imageIndex < s_MetadataImagesCount; imageIndex++)
  1374. {
  1375. const Il2CppImageGlobalMetadata* imageMetadata = s_MetadataImagesTable + imageIndex;
  1376. IL2CPP_ASSERT(index >= 0);
  1377. if (index >= imageMetadata->typeStart && static_cast<uint32_t>(index) < (imageMetadata->typeStart + imageMetadata->image->typeCount))
  1378. return imageMetadata->image;
  1379. }
  1380. IL2CPP_ASSERT(0 && "Failed to find owning image for type defintion index");
  1381. return NULL;
  1382. }
  1383. Il2CppClass* il2cpp::vm::GlobalMetadata::FromTypeDefinition(TypeDefinitionIndex index)
  1384. {
  1385. const Il2CppTypeDefinition* typeDefinition;
  1386. const Il2CppTypeDefinitionSizes* typeDefinitionSizes;
  1387. if (hybridclr::metadata::IsInterpreterIndex(index))
  1388. {
  1389. typeDefinition = (const Il2CppTypeDefinition *)hybridclr::metadata::MetadataModule::GetAssemblyTypeHandleFromEncodeIndex(index);
  1390. typeDefinitionSizes = hybridclr::metadata::MetadataModule::GetTypeDefinitionSizesFromEncodeIndex(index);
  1391. }
  1392. else
  1393. {
  1394. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition));
  1395. const Il2CppTypeDefinition* typeDefinitions = (const Il2CppTypeDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->typeDefinitionsOffset);
  1396. typeDefinition = typeDefinitions + index;
  1397. typeDefinitionSizes = s_Il2CppMetadataRegistration->typeDefinitionsSizes[index];
  1398. }
  1399. Il2CppClass* typeInfo = (Il2CppClass*)IL2CPP_CALLOC(1, sizeof(Il2CppClass) + (sizeof(VirtualInvokeData) * typeDefinition->vtable_count), IL2CPP_MEM_FromTypeDefinition);
  1400. #if IL2CPP_ENABLE_MEM_STATS
  1401. ++il2cpp_mem_stats.classFromTypeDef_count;
  1402. #endif
  1403. #if !IL2CPP_SLIM_CLASS
  1404. typeInfo->klass = typeInfo;
  1405. #endif
  1406. typeInfo->image = GetImageForTypeDefinitionIndex(index);
  1407. typeInfo->name = il2cpp::vm::GlobalMetadata::GetStringFromIndex(typeDefinition->nameIndex);
  1408. typeInfo->namespaze = il2cpp::vm::GlobalMetadata::GetStringFromIndex(typeDefinition->namespaceIndex);
  1409. typeInfo->byval_arg = *il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDefinition->byvalTypeIndex);
  1410. typeInfo->this_arg = typeInfo->byval_arg;
  1411. typeInfo->this_arg.byref = true;
  1412. typeInfo->this_arg.valuetype = 0;
  1413. typeInfo->typeMetadataHandle = reinterpret_cast<const Il2CppMetadataTypeHandle>(typeDefinition);
  1414. typeInfo->genericContainerHandle = GetGenericContainerFromIndex(typeDefinition->genericContainerIndex);
  1415. typeInfo->instance_size = typeDefinitionSizes->instance_size;
  1416. typeInfo->actualSize = typeDefinitionSizes->instance_size; // actualySize is instance_size for compiler generated values
  1417. typeInfo->native_size = typeDefinitionSizes->native_size;
  1418. typeInfo->static_fields_size = typeDefinitionSizes->static_fields_size;
  1419. typeInfo->thread_static_fields_size = typeDefinitionSizes->thread_static_fields_size;
  1420. typeInfo->thread_static_fields_offset = -1;
  1421. typeInfo->flags = typeDefinition->flags;
  1422. typeInfo->enumtype = (typeDefinition->bitfield >> (kBitIsEnum - 1)) & 0x1;
  1423. typeInfo->is_generic = typeDefinition->genericContainerIndex != kGenericContainerIndexInvalid; // generic if we have a generic container
  1424. typeInfo->has_finalize = (typeDefinition->bitfield >> (kBitHasFinalizer - 1)) & 0x1;
  1425. typeInfo->has_cctor = (typeDefinition->bitfield >> (kBitHasStaticConstructor - 1)) & 0x1;
  1426. typeInfo->cctor_finished_or_no_cctor = !typeInfo->has_cctor;
  1427. typeInfo->is_blittable = (typeDefinition->bitfield >> (kBitIsBlittable - 1)) & 0x1;
  1428. typeInfo->is_import_or_windows_runtime = (typeDefinition->bitfield >> (kBitIsImportOrWindowsRuntime - 1)) & 0x1;
  1429. typeInfo->packingSize = ConvertPackingSizeEnumToValue(static_cast<PackingSize>((typeDefinition->bitfield >> (kPackingSize - 1)) & 0xF));
  1430. typeInfo->is_byref_like = (typeDefinition->bitfield >> (kBitIsByRefLike - 1)) & 0x1;
  1431. typeInfo->method_count = typeDefinition->method_count;
  1432. typeInfo->property_count = typeDefinition->property_count;
  1433. typeInfo->field_count = typeDefinition->field_count;
  1434. typeInfo->event_count = typeDefinition->event_count;
  1435. typeInfo->nested_type_count = typeDefinition->nested_type_count;
  1436. typeInfo->vtable_count = typeDefinition->vtable_count;
  1437. typeInfo->interfaces_count = typeDefinition->interfaces_count;
  1438. typeInfo->interface_offsets_count = typeDefinition->interface_offsets_count;
  1439. typeInfo->token = typeDefinition->token;
  1440. typeInfo->interopData = il2cpp::vm::MetadataCache::GetInteropDataForType(&typeInfo->byval_arg);
  1441. if (typeDefinition->parentIndex != kTypeIndexInvalid)
  1442. typeInfo->parent = il2cpp::vm::Class::FromIl2CppType(il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDefinition->parentIndex));
  1443. if (typeDefinition->declaringTypeIndex != kTypeIndexInvalid)
  1444. typeInfo->declaringType = il2cpp::vm::Class::FromIl2CppType(il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDefinition->declaringTypeIndex));
  1445. typeInfo->castClass = typeInfo->element_class = typeInfo;
  1446. if (typeInfo->enumtype)
  1447. typeInfo->castClass = typeInfo->element_class = il2cpp::vm::Class::FromIl2CppType(il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDefinition->elementTypeIndex));
  1448. return typeInfo;
  1449. }
  1450. const Il2CppType* il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(TypeIndex index)
  1451. {
  1452. if (index == kTypeIndexInvalid)
  1453. return NULL;
  1454. if (hybridclr::metadata::IsInterpreterIndex(index))
  1455. {
  1456. return hybridclr::metadata::MetadataModule::GetIl2CppTypeFromEncodeIndex(index);
  1457. }
  1458. IL2CPP_ASSERT(index < s_Il2CppMetadataRegistration->typesCount && "Invalid type index ");
  1459. return s_Il2CppMetadataRegistration->types[index];
  1460. }
  1461. uint32_t il2cpp::vm::GlobalMetadata::GetGenericContainerCount(Il2CppMetadataGenericContainerHandle handle)
  1462. {
  1463. const Il2CppGenericContainer* container = reinterpret_cast<const Il2CppGenericContainer*>(handle);
  1464. return container != NULL ? container->type_argc : 0;
  1465. }
  1466. void il2cpp::vm::GlobalMetadata::MakeGenericArgType(Il2CppMetadataGenericContainerHandle containerHandle, Il2CppMetadataGenericParameterHandle paramHandle, Il2CppType* arg)
  1467. {
  1468. const Il2CppGenericContainer* container = reinterpret_cast<const Il2CppGenericContainer*>(containerHandle);
  1469. arg->type = container->is_method ? IL2CPP_TYPE_MVAR : IL2CPP_TYPE_VAR;
  1470. arg->data.genericParameterHandle = paramHandle;
  1471. }
  1472. bool il2cpp::vm::GlobalMetadata::GetGenericContainerIsMethod(Il2CppMetadataGenericContainerHandle handle)
  1473. {
  1474. const Il2CppGenericContainer* container = reinterpret_cast<const Il2CppGenericContainer*>(handle);
  1475. IL2CPP_ASSERT(container != NULL);
  1476. return container != NULL ? container->is_method : false;
  1477. }
  1478. int16_t il2cpp::vm::GlobalMetadata::GetGenericConstraintCount(Il2CppMetadataGenericParameterHandle handle)
  1479. {
  1480. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1481. return genericParameter->constraintsCount;
  1482. }
  1483. const char* il2cpp::vm::GlobalMetadata::GetGenericParameterName(Il2CppMetadataGenericParameterHandle handle)
  1484. {
  1485. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1486. return GetStringFromIndex(genericParameter->nameIndex);
  1487. }
  1488. Il2CppGenericParameterInfo il2cpp::vm::GlobalMetadata::GetGenericParameterInfo(Il2CppMetadataGenericParameterHandle handle)
  1489. {
  1490. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1491. return {
  1492. reinterpret_cast<Il2CppMetadataGenericContainerHandle>(GetGenericContainerFromIndexInternal(genericParameter->ownerIndex)),
  1493. GetStringFromIndex(genericParameter->nameIndex),
  1494. genericParameter->num,
  1495. genericParameter->flags
  1496. };
  1497. }
  1498. uint16_t il2cpp::vm::GlobalMetadata::GetGenericParameterFlags(Il2CppMetadataGenericParameterHandle handle)
  1499. {
  1500. const Il2CppGenericParameter* genericParameter = reinterpret_cast<const Il2CppGenericParameter*>(handle);
  1501. return genericParameter->flags;
  1502. }
  1503. const MethodInfo* il2cpp::vm::GlobalMetadata::GetMethodInfoFromCatchPoint(const Il2CppCatchPoint* cp)
  1504. {
  1505. return GetMethodInfoFromMethodDefinitionIndex(cp->__methodDefinitionIndex);
  1506. }
  1507. const MethodInfo* il2cpp::vm::GlobalMetadata::GetMethodInfoFromSequencePoint(const Il2CppSequencePoint* seqPoint)
  1508. {
  1509. return GetMethodInfoFromMethodDefinitionIndex(seqPoint->__methodDefinitionIndex);
  1510. }
  1511. Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeSourcePair(const Il2CppTypeSourceFilePair* pair)
  1512. {
  1513. return GetTypeInfoFromTypeDefinitionIndex(pair->__klassIndex);
  1514. }
  1515. Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeIndex(TypeIndex index, bool throwOnError)
  1516. {
  1517. if (index == kTypeIndexInvalid)
  1518. return NULL;
  1519. if (hybridclr::metadata::IsInterpreterIndex(index))
  1520. {
  1521. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(hybridclr::metadata::MetadataModule::GetIl2CppTypeFromEncodeIndex(index));
  1522. ClassInlines::InitFromCodegen(klass);
  1523. return klass;
  1524. }
  1525. IL2CPP_ASSERT(index < s_Il2CppMetadataRegistration->typesCount && "Invalid type index ");
  1526. return utils::InitOnce(&s_TypeInfoTable[index], &g_MetadataLock, [index, throwOnError](il2cpp::os::FastAutoLock& _)
  1527. {
  1528. const Il2CppType* type = s_Il2CppMetadataRegistration->types[index];
  1529. Il2CppClass *klass = Class::FromIl2CppType(type, throwOnError);
  1530. if (klass != NULL)
  1531. ClassInlines::InitFromCodegenSlow(klass, throwOnError);
  1532. return klass;
  1533. });
  1534. }
  1535. const MethodInfo* il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodHandle(Il2CppMetadataMethodDefinitionHandle handle)
  1536. {
  1537. const Il2CppMethodDefinition* methodDefinition = reinterpret_cast<const Il2CppMethodDefinition*>(handle);
  1538. if (hybridclr::metadata::IsInterpreterIndex(methodDefinition->nameIndex))
  1539. {
  1540. return hybridclr::metadata::MetadataModule::GetMethodInfoFromMethodDefinition(methodDefinition);
  1541. }
  1542. const Il2CppMethodDefinition* methods = (const Il2CppMethodDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->methodsOffset);
  1543. const MethodIndex index = static_cast<MethodIndex>(methodDefinition - methods);
  1544. IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition));
  1545. return GetMethodInfoFromMethodDefinitionIndex(index);
  1546. }
  1547. #if IL2CPP_ENABLE_NATIVE_STACKTRACES
  1548. void il2cpp::vm::GlobalMetadata::GetAllManagedMethods(std::vector<MethodDefinitionKey>& managedMethods)
  1549. {
  1550. size_t methodDefinitionsCount = s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition);
  1551. managedMethods.reserve(methodDefinitionsCount + s_Il2CppMetadataRegistration->genericMethodTableCount);
  1552. const Il2CppTypeDefinition* typeDefinitions = (const Il2CppTypeDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->typeDefinitionsOffset);
  1553. for (int32_t i = 0; i < s_MetadataImagesCount; i++)
  1554. {
  1555. const Il2CppImageGlobalMetadata* image = s_MetadataImagesTable + i;
  1556. for (size_t j = 0; j < image->image->typeCount; j++)
  1557. {
  1558. const Il2CppTypeDefinition* type = typeDefinitions + image->typeStart + j;
  1559. for (uint16_t u = 0; u < type->method_count; u++)
  1560. {
  1561. const Il2CppMethodDefinition* methodDefinition = GetMethodDefinitionFromIndex(type->methodStart + u);
  1562. MethodDefinitionKey currentMethodList;
  1563. currentMethodList.methodHandle = reinterpret_cast<Il2CppMetadataMethodDefinitionHandle>(methodDefinition);
  1564. currentMethodList.method = il2cpp::vm::MetadataCache::GetMethodPointer(image->image, methodDefinition->token);
  1565. if (currentMethodList.method)
  1566. managedMethods.push_back(currentMethodList);
  1567. }
  1568. }
  1569. }
  1570. for (int32_t i = 0; i < s_Il2CppMetadataRegistration->genericMethodTableCount; i++)
  1571. {
  1572. const Il2CppGenericMethodFunctionsDefinitions* genericMethodIndices = s_Il2CppMetadataRegistration->genericMethodTable + i;
  1573. MethodDefinitionKey currentMethodList;
  1574. GenericMethodIndex genericMethodIndex = genericMethodIndices->genericMethodIndex;
  1575. IL2CPP_ASSERT(genericMethodIndex < s_Il2CppMetadataRegistration->methodSpecsCount);
  1576. const Il2CppMethodSpec* methodSpec = s_Il2CppMetadataRegistration->methodSpecs + genericMethodIndex;
  1577. const Il2CppMethodDefinition* methodDefinition = GetMethodDefinitionFromIndex(methodSpec->methodDefinitionIndex);
  1578. currentMethodList.methodHandle = reinterpret_cast<Il2CppMetadataMethodDefinitionHandle>(methodDefinition);
  1579. IL2CPP_ASSERT(genericMethodIndices->indices.methodIndex < static_cast<int32_t>(s_GlobalMetadata_CodeRegistration->genericMethodPointersCount));
  1580. currentMethodList.method = s_GlobalMetadata_CodeRegistration->genericMethodPointers[genericMethodIndices->indices.methodIndex];
  1581. managedMethods.push_back(currentMethodList);
  1582. }
  1583. }
  1584. #endif