GlobalMetadata.cpp 76 KB

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