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