MetadataCache.h 12 KB

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