MetadataUtil.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #pragma once
  2. #include <tuple>
  3. #include "vm/GlobalMetadata.h"
  4. #include "vm/Exception.h"
  5. #include "utils/HashUtils.h"
  6. #include "metadata/Il2CppTypeHash.h"
  7. #include "metadata/Il2CppTypeCompare.h"
  8. #include "../CommonDef.h"
  9. #include "MetadataDef.h"
  10. namespace hybridclr
  11. {
  12. namespace metadata
  13. {
  14. class Image;
  15. #pragma region byteorder
  16. template<int N>
  17. inline void* GetAlignBorder(const void* pointer)
  18. {
  19. uint64_t p = (uint64_t)pointer;
  20. if (p % N == 0)
  21. {
  22. return (void*)pointer;
  23. }
  24. else
  25. {
  26. return (void*)((p + N - 1) / N * N);
  27. }
  28. }
  29. inline int32_t GetI1(const byte* data)
  30. {
  31. return *(int8_t*)data;
  32. }
  33. inline int16_t GetI2LittleEndian(const byte* data)
  34. {
  35. #if SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS
  36. uint16_t value = *(uint16_t*)data;
  37. #else
  38. uint16_t value = (uint16_t)data[0] | ((uint16_t)data[1] << 8);
  39. #endif
  40. return (int16_t)value;
  41. }
  42. inline uint16_t GetU2LittleEndian(const byte* data)
  43. {
  44. return (uint16_t)GetI2LittleEndian(data);
  45. }
  46. inline int32_t GetI4LittleEndian(const byte* data)
  47. {
  48. #if SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS
  49. uint32_t value = *(uint32_t*)data;
  50. #else
  51. uint32_t value = (uint32_t)data[0]
  52. | ((uint32_t)data[1] << 8)
  53. | ((uint32_t)data[2] << 16)
  54. | ((uint32_t)data[3] << 24);
  55. #endif
  56. return (int32_t)value;
  57. }
  58. inline int64_t GetI8LittleEndian(const byte* data)
  59. {
  60. #if SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS
  61. uint64_t value = *(uint64_t*)data;
  62. #else
  63. uint64_t value = (uint64_t)data[0]
  64. + ((uint64_t)data[1] << 8)
  65. + ((uint64_t)data[2] << 16)
  66. + ((uint64_t)data[3] << 24)
  67. + ((uint64_t)data[4] << 32)
  68. + ((uint64_t)data[5] << 40)
  69. + ((uint64_t)data[6] << 48)
  70. + ((uint64_t)data[7] << 56);
  71. #endif
  72. return value;
  73. }
  74. uint32_t GetNotZeroBitCount(uint64_t x);
  75. #pragma endregion
  76. #pragma region interpreter metadtata index
  77. const uint32_t kMetadataIndexBits = 22;
  78. const uint32_t kMetadataKindBits = 2;
  79. const uint32_t kMetadataKindShiftBits = 32 - kMetadataKindBits;
  80. const uint32_t kMetadataImageIndexShiftBits = kMetadataIndexBits;
  81. const uint32_t kMetadataImageIndexExtraShiftBitsA = 6;
  82. const uint32_t kMetadataImageIndexExtraShiftBitsB = 4;
  83. const uint32_t kMetadataImageIndexExtraShiftBitsC = 2;
  84. const uint32_t kMetadataImageIndexExtraShiftBitsD = 0;
  85. extern const uint32_t kMetadataImageIndexExtraShiftBitsArr[4];
  86. const uint32_t kMetadataIndexMaskA = (1 << (kMetadataIndexBits + kMetadataImageIndexExtraShiftBitsA)) - 1;
  87. const uint32_t kMetadataIndexMaskB = (1 << (kMetadataIndexBits + kMetadataImageIndexExtraShiftBitsB)) - 1;
  88. const uint32_t kMetadataIndexMaskC = (1 << (kMetadataIndexBits + kMetadataImageIndexExtraShiftBitsC)) - 1;
  89. const uint32_t kMetadataIndexMaskD = (1 << (kMetadataIndexBits + kMetadataImageIndexExtraShiftBitsD)) - 1;
  90. extern const uint32_t kMetadataIndexMaskArr[4];
  91. const uint32_t kMetadataImageIndexBits = 32 - kMetadataIndexBits;
  92. const uint32_t kMaxMetadataImageCount = (1 << kMetadataImageIndexBits);
  93. const uint32_t kMaxMetadataImageIndexWithoutKind = 1u << (kMetadataImageIndexBits - kMetadataKindBits);
  94. const uint32_t kInvalidImageIndex = 0;
  95. const int32_t kInvalidIndex = -1;
  96. inline int32_t DecodeMetadataKind(uint32_t index)
  97. {
  98. return index >> kMetadataKindShiftBits;
  99. }
  100. inline uint32_t DecodeImageIndex(int32_t index)
  101. {
  102. if (index == kInvalidIndex)
  103. {
  104. return 0;
  105. }
  106. uint32_t uindex = (uint32_t)index;
  107. uint32_t kind = uindex >> kMetadataKindShiftBits;
  108. return (uindex & ~kMetadataIndexMaskArr[kind]) >> kMetadataImageIndexShiftBits;
  109. }
  110. inline uint32_t DecodeMetadataIndex(int32_t index)
  111. {
  112. if (index == kInvalidIndex)
  113. {
  114. return kInvalidIndex;
  115. }
  116. uint32_t uindex = (uint32_t)index;
  117. uint32_t kind = uindex >> kMetadataKindShiftBits;
  118. return uindex & kMetadataIndexMaskArr[kind];
  119. }
  120. inline int32_t EncodeImageAndMetadataIndex(uint32_t imageIndex, int32_t rawIndex)
  121. {
  122. if (rawIndex == kInvalidIndex)
  123. {
  124. return kInvalidIndex;
  125. }
  126. IL2CPP_ASSERT(((imageIndex << kMetadataImageIndexShiftBits) & (uint32_t)rawIndex) == 0);
  127. return (imageIndex << kMetadataIndexBits) | (uint32_t)rawIndex;
  128. }
  129. inline bool IsInterpreterIndex(int32_t index)
  130. {
  131. //return DecodeImageIndex(index) != 0;
  132. return index != kInvalidIndex && ((uint32_t)index & ~kMetadataIndexMaskA) != 0;
  133. }
  134. inline bool IsInterpreterType(const Il2CppTypeDefinition* typeDefinition)
  135. {
  136. return IsInterpreterIndex(typeDefinition->byvalTypeIndex);
  137. }
  138. inline bool IsInterpreterType(const Il2CppClass* klass)
  139. {
  140. return IsInterpreterIndex(klass->image->token) && klass->rank == 0;
  141. }
  142. inline bool IsInterpreterImage(const Il2CppImage* image)
  143. {
  144. return IsInterpreterIndex(image->token);
  145. }
  146. inline bool IsPrologHasThis(uint32_t flags)
  147. {
  148. return flags & 0x20;
  149. }
  150. inline bool IsPrologExplicitThis(uint32_t flags)
  151. {
  152. return flags & 0x40;
  153. }
  154. #pragma endregion
  155. #pragma region method and klass
  156. inline bool IsInstanceField(const Il2CppType* type)
  157. {
  158. return (type->attrs & FIELD_ATTRIBUTE_STATIC) == 0;
  159. }
  160. inline bool IsInterpreterMethod(const MethodInfo* method)
  161. {
  162. return IsInterpreterType(method->klass);
  163. }
  164. inline bool IsInterpreterMethod(const Il2CppMethodDefinition* method)
  165. {
  166. return IsInterpreterIndex(method->declaringType);
  167. }
  168. inline bool IsInterpreterImplement(const MethodInfo* method)
  169. {
  170. return method->isInterpterImpl;
  171. }
  172. inline bool IsInstanceMethod(const MethodInfo* method)
  173. {
  174. return !(method->flags & METHOD_ATTRIBUTE_STATIC);
  175. }
  176. inline bool IsInstanceMethod(const Il2CppMethodDefinition* method)
  177. {
  178. return !(method->flags & METHOD_ATTRIBUTE_STATIC);
  179. }
  180. inline bool IsStaticMethod(const MethodInfo* method)
  181. {
  182. return (method->flags & METHOD_ATTRIBUTE_STATIC);
  183. }
  184. inline bool IsPrivateMethod(uint32_t flags)
  185. {
  186. return (flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PRIVATE;
  187. }
  188. inline bool IsPublicMethod(uint32_t flags)
  189. {
  190. return (flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC;
  191. }
  192. inline bool IsGenericIns(const Il2CppType* type)
  193. {
  194. return type->type == IL2CPP_TYPE_GENERICINST;
  195. }
  196. inline bool IsVirtualMethod(uint32_t flags)
  197. {
  198. return flags & METHOD_ATTRIBUTE_VIRTUAL;
  199. }
  200. inline bool IsAbstractMethod(uint32_t flags)
  201. {
  202. return flags & METHOD_ATTRIBUTE_ABSTRACT;
  203. }
  204. inline bool IsNewSlot(uint32_t flags)
  205. {
  206. return flags & METHOD_ATTRIBUTE_NEW_SLOT;
  207. }
  208. inline bool IsSealed(uint32_t flags)
  209. {
  210. return flags & METHOD_ATTRIBUTE_FINAL;
  211. }
  212. inline bool IsInterface(uint32_t flags)
  213. {
  214. return flags & TYPE_ATTRIBUTE_INTERFACE;
  215. }
  216. bool IsValueType(const Il2CppType* type);
  217. inline bool IsValueType(const Il2CppTypeDefinition* typeDef)
  218. {
  219. return typeDef->bitfield & (1 << (il2cpp::vm::kBitIsValueType - 1));
  220. }
  221. inline bool IsEnumType(const Il2CppTypeDefinition* typeDef)
  222. {
  223. return (typeDef->bitfield >> (il2cpp::vm::kBitIsEnum - 1)) & 0x1;
  224. }
  225. inline const Il2CppTypeDefinition* GetUnderlyingTypeDefinition(const Il2CppType* type)
  226. {
  227. if (IsGenericIns(type))
  228. {
  229. return (Il2CppTypeDefinition*)type->data.generic_class->type->data.typeHandle;
  230. }
  231. else
  232. {
  233. return (Il2CppTypeDefinition*)type->data.typeHandle;
  234. }
  235. }
  236. const Il2CppType* GetIl2CppTypeFromTypeDefinition(const Il2CppTypeDefinition* typeDef);
  237. inline uint32_t GetActualArgumentNum(const MethodInfo* method)
  238. {
  239. return (uint32_t)method->parameters_count + (!(method->flags & METHOD_ATTRIBUTE_STATIC));
  240. }
  241. inline bool IsReturnVoidMethod(const MethodInfo* method)
  242. {
  243. return method->return_type->type == IL2CPP_TYPE_VOID;
  244. }
  245. inline bool IsVoidType(const Il2CppType* type)
  246. {
  247. return type->type == IL2CPP_TYPE_VOID;
  248. }
  249. inline const MethodInfo* GetUnderlyingMethodInfo(const MethodInfo* method)
  250. {
  251. return !method->genericMethod || method->is_generic ? method : method->genericMethod->methodDefinition;
  252. }
  253. inline bool IsChildTypeOfMulticastDelegate(const Il2CppClass* klass)
  254. {
  255. return klass->parent == il2cpp_defaults.multicastdelegate_class;
  256. }
  257. inline int32_t GetActualParamCount(const MethodInfo* methodInfo)
  258. {
  259. return IsInstanceMethod(methodInfo) ? (methodInfo->parameters_count + 1) : methodInfo->parameters_count;
  260. }
  261. inline int32_t GetFieldOffset(const FieldInfo* fieldInfo)
  262. {
  263. Il2CppClass* klass = fieldInfo->parent;
  264. return IS_CLASS_VALUE_TYPE(klass) ? (fieldInfo->offset - sizeof(Il2CppObject)) : fieldInfo->offset;
  265. }
  266. inline int32_t GetThreadStaticFieldOffset(const FieldInfo* fieldInfo)
  267. {
  268. return il2cpp::vm::MetadataCache::GetThreadLocalStaticOffsetForField(const_cast<FieldInfo*>(fieldInfo));
  269. }
  270. const Il2CppType* TryInflateIfNeed(const Il2CppType* selfType, const Il2CppGenericContext* genericContext, bool inflateMethodVars);
  271. const Il2CppType* TryInflateIfNeed(const Il2CppType* containerType, const Il2CppType* selfType);
  272. bool IsTypeSameByTypeIndex(TypeIndex t1, TypeIndex t2);
  273. bool IsTypeEqual(const Il2CppType* t1, const Il2CppType* t2);
  274. bool IsTypeGenericCompatible(const Il2CppType* t1, const Il2CppType* t2);
  275. bool IsOverrideMethod(const Il2CppType* type1, const Il2CppMethodDefinition* method1, const Il2CppType* type2, const Il2CppMethodDefinition* method2);
  276. bool IsOverrideMethodIgnoreName(const Il2CppType* type1, const Il2CppMethodDefinition* methodDef1, const Il2CppType* type2, const Il2CppMethodDefinition* methodDef2);
  277. const Il2CppMethodDefinition* ResolveMethodDefinition(const Il2CppType* type, const char* resolveMethodName, const MethodRefSig& resolveSig);
  278. const MethodInfo* GetMethodInfoFromMethodDef(const Il2CppType* type, const Il2CppMethodDefinition* methodDef);
  279. bool ResolveField(const Il2CppType* type, const char* resolveFieldName, const Il2CppType* resolveFieldType, const Il2CppFieldDefinition*& retFieldDef);
  280. inline void ResolveFieldThrow(const Il2CppType* type, const char* resolveFieldName, const Il2CppType* resolveFieldType, const Il2CppFieldDefinition*& retFieldDef)
  281. {
  282. if (!ResolveField(type, resolveFieldName, resolveFieldType, retFieldDef))
  283. {
  284. RaiseMissingFieldException(type, resolveFieldName);
  285. }
  286. }
  287. const Il2CppGenericContainer* GetGenericContainerFromIl2CppType(const Il2CppType* type);
  288. inline const Il2CppGenericContainer* GetGenericContainer(const MethodInfo* methodDef)
  289. {
  290. return methodDef->is_inflated ?
  291. (const Il2CppGenericContainer*)methodDef->genericMethod->methodDefinition->genericContainerHandle :
  292. (const Il2CppGenericContainer*)methodDef->genericContainerHandle;
  293. }
  294. bool IsMatchSigType(const Il2CppType* dstType, const Il2CppType* sigType, const Il2CppGenericContainer* klassGenericContainer, const Il2CppGenericContainer* methodGenericContainer);
  295. bool IsMatchMethodSig(const Il2CppMethodDefinition* methodDef, const MethodRefSig& resolveSig, const Il2CppGenericContainer* klassGenericContainer);
  296. bool IsMatchMethodSig(const MethodInfo* methodDef, const MethodRefSig& resolveSig, const Il2CppGenericContainer* klassGenericContainer);
  297. bool IsMatchMethodSig(const MethodInfo* methodDef, const MethodRefSig& resolveSig, const Il2CppType** klassInstArgv, const Il2CppType** methodInstArgv);
  298. const Il2CppGenericInst* TryInflateGenericInst(const Il2CppGenericInst* inst, const Il2CppGenericContext* genericContext);
  299. #pragma endregion
  300. #pragma region misc
  301. int32_t GetTypeValueSize(const Il2CppType* type);
  302. inline int32_t GetTypeValueSize(const Il2CppClass* klass)
  303. {
  304. if (IS_CLASS_VALUE_TYPE(klass))
  305. {
  306. return il2cpp::vm::Class::GetValueSize((Il2CppClass*)klass, nullptr);
  307. }
  308. else
  309. {
  310. return sizeof(Il2CppObject*);
  311. }
  312. }
  313. inline int32_t GetStackSizeByByteSize(int32_t size)
  314. {
  315. return (size + 7) / 8;
  316. }
  317. inline int32_t GetTypeValueStackObjectCount(const Il2CppType* type)
  318. {
  319. return (GetTypeValueSize(type) + 7) / 8;
  320. }
  321. inline void RaiseBadImageException(const char* msg = nullptr)
  322. {
  323. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetBadImageFormatException(msg));
  324. }
  325. #pragma endregion
  326. class Il2CppTypeHashShallow
  327. {
  328. public:
  329. size_t operator()(const Il2CppType* t1) const
  330. {
  331. size_t h = (size_t)t1->data.dummy;
  332. h = il2cpp::utils::HashUtils::Combine(h, t1->attrs);
  333. h = il2cpp::utils::HashUtils::Combine(h, (size_t)t1->type);
  334. h = il2cpp::utils::HashUtils::Combine(h, t1->byref);
  335. h = il2cpp::utils::HashUtils::Combine(h, t1->pinned);
  336. #if HYBRIDCLR_UNITY_2021_OR_NEW
  337. h = il2cpp::utils::HashUtils::Combine(h, t1->valuetype);
  338. #endif
  339. return h;
  340. }
  341. };
  342. class Il2CppTypeEqualityComparerShallow
  343. {
  344. public:
  345. bool operator()(const Il2CppType* t1, const Il2CppType* t2) const
  346. {
  347. return (t1->data.dummy == t2->data.dummy)
  348. && t1->type == t2->type
  349. && t1->attrs == t2->attrs
  350. && t1->byref == t2->byref
  351. && t1->pinned == t2->pinned
  352. #if HYBRIDCLR_UNITY_2021_OR_NEW
  353. && t1->valuetype == t2->valuetype
  354. #endif
  355. ;
  356. }
  357. };
  358. struct Il2CppTypeHash {
  359. size_t operator()(const Il2CppType* x) const noexcept {
  360. return il2cpp::metadata::Il2CppTypeHash::Hash(x);
  361. }
  362. };
  363. struct Il2CppTypeEqualTo
  364. {
  365. bool operator()(const Il2CppType* a, const Il2CppType* b) const {
  366. return il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(a, b);
  367. }
  368. };
  369. }
  370. }