MetadataCache.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. #include <stdint.h>
  3. #include "il2cpp-config.h"
  4. #include "Assembly.h"
  5. #include "il2cpp-class-internals.h"
  6. #include "utils/dynamic_array.h"
  7. #include "utils/HashUtils.h"
  8. #include "utils/Il2CppHashMap.h"
  9. #include "utils/StringUtils.h"
  10. #include "metadata/CustomAttributeDataReader.h"
  11. #include "metadata/Il2CppGenericContextCompare.h"
  12. #include "metadata/Il2CppGenericContextHash.h"
  13. #include "metadata/Il2CppGenericInstCompare.h"
  14. #include "metadata/Il2CppGenericInstHash.h"
  15. #include "metadata/Il2CppGenericMethodCompare.h"
  16. #include "metadata/Il2CppGenericMethodHash.h"
  17. #include "metadata/Il2CppSignature.h"
  18. #include "metadata/Il2CppTypeCompare.h"
  19. #include "metadata/Il2CppTypeHash.h"
  20. #include "vm-utils/VmStringUtils.h"
  21. #include "os/Mutex.h"
  22. namespace il2cpp
  23. {
  24. namespace vm
  25. {
  26. struct RGCTXCollection
  27. {
  28. int32_t count;
  29. const Il2CppRGCTXDefinition* items;
  30. };
  31. typedef struct Il2CppGenericMethodPointers
  32. {
  33. Il2CppMethodPointer methodPointer;
  34. Il2CppMethodPointer virtualMethodPointer;
  35. InvokerMethod invoker_method;
  36. bool isFullGenericShared;
  37. } Il2CppGenericMethodPointers;
  38. typedef struct Il2CppUnresolvedCallStubs
  39. {
  40. Il2CppMethodPointer methodPointer;
  41. Il2CppMethodPointer virtualMethodPointer;
  42. bool stubsFound;
  43. } Il2CppUnresolvedCallStubs;
  44. enum GenericParameterRestriction
  45. {
  46. GenericParameterRestrictionNone,
  47. GenericParameterRestrictionValueType,
  48. GenericParameterRestrictionReferenceType,
  49. };
  50. typedef Il2CppHashMap<const char*, Il2CppClass*, il2cpp::utils::StringUtils::StringHasher<const char*>, il2cpp::utils::VmStringUtils::CaseSensitiveComparer> WindowsRuntimeTypeNameToClassMap;
  51. typedef Il2CppHashMap<const Il2CppClass*, const char*, il2cpp::utils::PointerHash<Il2CppClass> > ClassToWindowsRuntimeTypeNameMap;
  52. typedef Il2CppHashMap<il2cpp::metadata::Il2CppSignature, int32_t, il2cpp::metadata::Il2CppSignatureHash, il2cpp::metadata::Il2CppSignatureCompare> Il2CppUnresolvedSignatureMap;
  53. typedef Il2CppHashMap<const Il2CppGenericMethod*, const Il2CppGenericMethodIndices*, il2cpp::metadata::Il2CppGenericMethodHash, il2cpp::metadata::Il2CppGenericMethodCompare> Il2CppMethodTableMap;
  54. class LIBIL2CPP_CODEGEN_API MetadataCache
  55. {
  56. public:
  57. static void Register(const Il2CppCodeRegistration * const codeRegistration, const Il2CppMetadataRegistration * const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions);
  58. static bool Initialize();
  59. static void InitializeGCSafe();
  60. static void Clear();
  61. static void ExecuteEagerStaticClassConstructors();
  62. static void ExecuteModuleInitializers();
  63. static Il2CppClass* GetGenericInstanceType(Il2CppClass* genericTypeDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount);
  64. static const MethodInfo* GetGenericInstanceMethod(const MethodInfo* genericMethodDefinition, const Il2CppType** genericArgumentTypes, uint32_t genericArgumentCount);
  65. static const Il2CppGenericContext* GetMethodGenericContext(const MethodInfo* method);
  66. static const MethodInfo* GetGenericMethodDefinition(const MethodInfo* method);
  67. static Il2CppClass* GetPointerType(Il2CppClass* type);
  68. static Il2CppClass* GetWindowsRuntimeClass(const char* fullName);
  69. static const char* GetWindowsRuntimeClassName(const Il2CppClass* klass);
  70. static Il2CppMethodPointer GetWindowsRuntimeFactoryCreationFunction(const char* fullName);
  71. static Il2CppClass* GetClassForGuid(const Il2CppGuid* guid);
  72. static void AddPointerTypeLocked(Il2CppClass* type, Il2CppClass* pointerType, const il2cpp::os::FastAutoLock& lock);
  73. static const Il2CppGenericInst* GetGenericInst(const Il2CppType* const* types, uint32_t typeCount);
  74. static const Il2CppGenericMethod* GetGenericMethod(const MethodInfo* methodDefinition, const Il2CppGenericInst* classInst, const Il2CppGenericInst* methodInst);
  75. static GenericParameterRestriction IsReferenceTypeGenericParameter(Il2CppMetadataGenericParameterHandle genericParameter);
  76. static Il2CppGenericMethodPointers GetGenericMethodPointers(const MethodInfo* methodDefinition, const Il2CppGenericContext* context);
  77. static const MethodInfo* GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot);
  78. static const Il2CppType* GetTypeFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef);
  79. static const Il2CppGenericMethod* GetGenericMethodFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef);
  80. static std::pair<const Il2CppType*, const MethodInfo*> GetConstrainedCallFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef);
  81. static void InitializeAllMethodMetadata();
  82. static void* InitializeRuntimeMetadata(uintptr_t* metadataPointer);
  83. static Il2CppMethodPointer GetAdjustorThunk(const Il2CppImage* image, uint32_t token);
  84. static Il2CppMethodPointer GetMethodPointer(const Il2CppImage* image, uint32_t token);
  85. static InvokerMethod GetMethodInvoker(const Il2CppImage* image, uint32_t token);
  86. static const Il2CppInteropData* GetInteropDataForType(const Il2CppType* type);
  87. static Il2CppMethodPointer GetReversePInvokeWrapper(const Il2CppImage* image, const MethodInfo* method);
  88. static Il2CppUnresolvedCallStubs GetUnresovledCallStubs(const MethodInfo* method);
  89. static const Il2CppAssembly* GetAssemblyByName(const char* nameToFind);
  90. static Il2CppClass* GetTypeInfoFromType(const Il2CppType* type);
  91. static Il2CppMetadataGenericContainerHandle GetGenericContainerFromGenericClass(const Il2CppImage* image, const Il2CppGenericClass* genericClass);
  92. static Il2CppMetadataGenericContainerHandle GetGenericContainerFromMethod(Il2CppMetadataMethodDefinitionHandle handle);
  93. static Il2CppMetadataGenericParameterHandle GetGenericParameterFromType(const Il2CppType* type);
  94. static Il2CppMetadataGenericParameterHandle GetGenericParameterFromIndex(Il2CppMetadataGenericContainerHandle handle, GenericContainerParameterIndex index);
  95. static Il2CppClass* GetContainerDeclaringType(Il2CppMetadataGenericContainerHandle handle);
  96. static Il2CppClass* GetParameterDeclaringType(Il2CppMetadataGenericParameterHandle handle);
  97. static const MethodInfo* GetParameterDeclaringMethod(Il2CppMetadataGenericParameterHandle handle);
  98. static const Il2CppType* GetGenericParameterConstraintFromIndex(Il2CppMetadataGenericParameterHandle handle, GenericParameterConstraintIndex index);
  99. static Il2CppClass* GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset);
  100. static const Il2CppType* GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex index);
  101. static Il2CppInterfaceOffsetInfo GetInterfaceOffsetInfo(const Il2CppClass* klass, TypeInterfaceOffsetIndex index);
  102. static RGCTXCollection GetRGCTXs(const Il2CppImage* image, uint32_t token);
  103. static const uint8_t* GetFieldDefaultValue(const FieldInfo* field, const Il2CppType** type);
  104. static const uint8_t* GetParameterDefaultValue(const MethodInfo* method, int32_t parameterPosition, const Il2CppType** type, bool* isExplicitySetNullDefaultValue);
  105. static int GetFieldMarshaledSizeForField(const FieldInfo* field);
  106. static const MethodInfo* GetMethodInfoFromMethodHandle(Il2CppMetadataMethodDefinitionHandle handle);
  107. // returns the compiler computed field offset for type definition fields
  108. static int32_t GetFieldOffsetFromIndexLocked(const Il2CppClass* klass, int32_t fieldIndexInType, FieldInfo* field, const il2cpp::os::FastAutoLock& lock);
  109. static int32_t GetThreadLocalStaticOffsetForField(FieldInfo* field);
  110. static void AddThreadLocalStaticOffsetForFieldLocked(FieldInfo* field, int32_t offset, const il2cpp::os::FastAutoLock& lock);
  111. static const Il2CppAssembly* GetReferencedAssembly(const Il2CppAssembly* assembly, int32_t referencedAssemblyTableIndex);
  112. static Il2CppMetadataCustomAttributeHandle GetCustomAttributeTypeToken(const Il2CppImage* image, uint32_t token);
  113. static il2cpp::metadata::CustomAttributeDataReader GetCustomAttributeDataReader(const Il2CppImage* image, uint32_t token);
  114. static il2cpp::metadata::CustomAttributeDataReader GetCustomAttributeDataReader(Il2CppMetadataCustomAttributeHandle handle);
  115. typedef void(*WalkTypesCallback)(Il2CppClass* type, void* context);
  116. static void WalkPointerTypes(WalkTypesCallback callback, void* context);
  117. static Il2CppMetadataTypeHandle GetTypeHandleFromType(const Il2CppType* type);
  118. static bool TypeIsNested(Il2CppMetadataTypeHandle handle);
  119. static bool TypeIsValueType(Il2CppMetadataTypeHandle handle);
  120. static bool StructLayoutPackIsDefault(Il2CppMetadataTypeHandle handle);
  121. static int32_t StructLayoutPack(Il2CppMetadataTypeHandle handle);
  122. static bool StructLayoutSizeIsDefault(Il2CppMetadataTypeHandle handle);
  123. static std::pair<const char*, const char*> GetTypeNamespaceAndName(Il2CppMetadataTypeHandle handle);
  124. static Il2CppMetadataTypeHandle GetNestedTypes(Il2CppClass* klass, void** iter);
  125. static Il2CppMetadataTypeHandle GetNestedTypes(Il2CppMetadataTypeHandle handle, void** iter);
  126. static Il2CppMetadataFieldInfo GetFieldInfo(const Il2CppClass* klass, TypeFieldIndex index);
  127. static Il2CppMetadataMethodInfo GetMethodInfo(const Il2CppClass* klass, TypeMethodIndex index);
  128. static Il2CppMetadataParameterInfo GetParameterInfo(const Il2CppClass* klass, Il2CppMetadataMethodDefinitionHandle handle, MethodParameterIndex index);
  129. static Il2CppMetadataPropertyInfo GetPropertyInfo(const Il2CppClass* klass, TypePropertyIndex index);
  130. static Il2CppMetadataEventInfo GetEventInfo(const Il2CppClass* klass, TypeEventIndex index);
  131. static void MakeGenericArgType(Il2CppMetadataGenericContainerHandle containerHandle, Il2CppMetadataGenericParameterHandle paramHandle, Il2CppType* arg);
  132. static uint32_t GetGenericContainerCount(Il2CppMetadataGenericContainerHandle handle);
  133. static bool GetGenericContainerIsMethod(Il2CppMetadataGenericContainerHandle handle);
  134. static const char* GetGenericParameterName(Il2CppMetadataGenericParameterHandle handle);
  135. static Il2CppGenericParameterInfo GetGenericParameterInfo(Il2CppMetadataGenericParameterHandle handle);
  136. static uint16_t GetGenericParameterFlags(Il2CppMetadataGenericParameterHandle handle);
  137. static int16_t GetGenericConstraintCount(Il2CppMetadataGenericParameterHandle handle);
  138. static Il2CppClass* GetTypeInfoFromHandle(Il2CppMetadataTypeHandle handle);
  139. static const MethodInfo* GetAssemblyEntryPoint(const Il2CppImage* image);
  140. static Il2CppMetadataTypeHandle GetAssemblyTypeHandle(const Il2CppImage* image, AssemblyTypeIndex index);
  141. static Il2CppMetadataTypeHandle GetAssemblyExportedTypeHandle(const Il2CppImage* image, AssemblyExportedTypeIndex index);
  142. static const MethodInfo* GetMethodInfoFromCatchPoint(const Il2CppImage* image, const Il2CppCatchPoint* cp);
  143. static const MethodInfo* GetMethodInfoFromSequencePoint(const Il2CppImage* image, const Il2CppSequencePoint* cp);
  144. static Il2CppClass* GetTypeInfoFromTypeSourcePair(const Il2CppImage* image, const Il2CppTypeSourceFilePair* pair);
  145. // The following methods still expose indexes - but only need to be public for debugger support (in il2cpp-mono-api.cpp & mono/mini/debugger-agent.c)
  146. // Called from il2cpp_get_type_from_index
  147. static const Il2CppType* GetIl2CppTypeFromIndex(const Il2CppImage* image, TypeIndex index);
  148. // Called from il2cpp_get_class_from_index
  149. static Il2CppClass* GetTypeInfoFromTypeIndex(const Il2CppImage* image, TypeIndex index);
  150. static const MethodInfo* GetMethodInfoFromMethodDefinitionIndex(const Il2CppImage* image, MethodIndex index);
  151. private:
  152. static void InitializeUnresolvedSignatureTable();
  153. static void InitializeGenericMethodTable();
  154. static void InitializeGuidToClassTable();
  155. static Il2CppImage* GetImageFromIndex(ImageIndex index);
  156. static const Il2CppAssembly* GetAssemblyFromIndex(AssemblyIndex index);
  157. static Il2CppMetadataTypeHandle GetTypeHandleFromIndex(const Il2CppImage* image, TypeDefinitionIndex typeIndex);
  158. };
  159. } // namespace vm
  160. } // namespace il2cpp