GlobalMetadata.cpp 88 KB

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