ArrayMetadata.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include "il2cpp-config.h"
  2. #include "il2cpp-runtime-stats.h"
  3. #include "os/Mutex.h"
  4. #include "vm/Class.h"
  5. #include "vm/GenericClass.h"
  6. #include "vm/Image.h"
  7. #include "vm/MetadataLock.h"
  8. #include "vm/Method.h"
  9. #include "vm/Type.h"
  10. #include "metadata/ArrayMetadata.h"
  11. #include "metadata/GenericMetadata.h"
  12. #include "metadata/GenericMethod.h"
  13. #include "metadata/Il2CppGenericClassHash.h"
  14. #include "metadata/Il2CppGenericClassCompare.h"
  15. #include "metadata/Il2CppGenericInstCompare.h"
  16. #include "metadata/Il2CppGenericInstHash.h"
  17. #include "metadata/Il2CppTypeCompare.h"
  18. #include "metadata/Il2CppTypeHash.h"
  19. #include "vm/MetadataAlloc.h"
  20. #include "vm/MetadataCache.h"
  21. #include "vm/Runtime.h"
  22. #include "utils/Memory.h"
  23. #include "utils/Il2CppHashMap.h"
  24. #include "utils/StringUtils.h"
  25. #include "il2cpp-class-internals.h"
  26. #include "il2cpp-tabledefs.h"
  27. #include <vector>
  28. #include <limits>
  29. using namespace il2cpp::vm;
  30. using il2cpp::os::FastAutoLock;
  31. using il2cpp::utils::StringUtils;
  32. using il2cpp::vm::MetadataCache;
  33. using il2cpp::vm::MetadataCalloc;
  34. using il2cpp::vm::MetadataMalloc;
  35. using std::vector;
  36. using std::pair;
  37. const size_t kImplicitArrayInterfaceCount = 5;
  38. namespace il2cpp
  39. {
  40. namespace metadata
  41. {
  42. static const char* GetArrayName(const char* elementClassName, uint32_t rank, bool bounded)
  43. {
  44. std::string name;
  45. name += elementClassName;
  46. name += "[";
  47. for (uint32_t i = 1; i < rank; i++)
  48. name += ",";
  49. if (bounded)
  50. name += "*";
  51. name += "]";
  52. return StringUtils::StringDuplicate(name.c_str());
  53. }
  54. static MethodInfo* ConstructArrayMethod(Il2CppClass* declaringType, const char* name, const Il2CppType* returnType, uint8_t parameterCount, const Il2CppType** parameterTypes)
  55. {
  56. MethodInfo* method = (MethodInfo*)MetadataCalloc(1, sizeof(MethodInfo));
  57. method->klass = declaringType;
  58. method->flags = METHOD_ATTRIBUTE_PUBLIC;
  59. method->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
  60. method->name = name;
  61. method->slot = kInvalidIl2CppMethodSlot;
  62. method->return_type = returnType;
  63. method->parameters_count = parameterCount;
  64. const Il2CppType** parameters = (const Il2CppType**)MetadataCalloc(parameterCount, sizeof(Il2CppType*));
  65. for (uint8_t i = 0; i < parameterCount; i++)
  66. {
  67. parameters[i] = parameterTypes[i];
  68. }
  69. method->parameters = parameters;
  70. if (!strcmp(".ctor", name))
  71. {
  72. method->flags |= METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
  73. }
  74. else
  75. {
  76. method->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
  77. }
  78. ++il2cpp_runtime_stats.method_count;
  79. return method;
  80. }
  81. struct GenericArrayMethod
  82. {
  83. const char* name;
  84. const MethodInfo* method;
  85. const MethodInfo* interfaceMethodDefinition;
  86. };
  87. typedef vector<GenericArrayMethod> GenericArrayMethods;
  88. static GenericArrayMethods s_GenericArrayMethods;
  89. static size_t GetArrayGenericMethodsCount();
  90. static void PopulateArrayGenericMethods(Il2CppClass* klass, uint16_t offset);
  91. static void CollectImplicitArrayInterfaces(Il2CppClass* elementClass, ::std::vector<Il2CppClass*>& interfaces);
  92. static void SetupArrayMethods(Il2CppClass* arrayClass)
  93. {
  94. size_t methodIndex = 0;
  95. uint8_t rank = arrayClass->rank;
  96. ::std::vector<Il2CppClass*> interfaces;
  97. CollectImplicitArrayInterfaces(arrayClass, interfaces);
  98. size_t methodCount = 3 + (rank > 1 ? 2 : 1) + interfaces.size() * GetArrayGenericMethodsCount();
  99. IL2CPP_ASSERT(methodCount <= std::numeric_limits<uint16_t>::max());
  100. arrayClass->method_count = static_cast<uint16_t>(methodCount);
  101. arrayClass->methods = (const MethodInfo**)MetadataCalloc(methodCount, sizeof(MethodInfo*));
  102. const Il2CppType** parameters = (const Il2CppType**)alloca(rank * sizeof(Il2CppType*));
  103. for (uint8_t i = 0; i < rank; i++)
  104. parameters[i] = &il2cpp_defaults.int32_class->byval_arg;
  105. arrayClass->methods[methodIndex++] = ConstructArrayMethod(arrayClass, ".ctor", &il2cpp_defaults.void_class->byval_arg, rank, parameters);
  106. if (rank > 1)
  107. {
  108. parameters = (const Il2CppType**)alloca(2 * rank * sizeof(Il2CppType*));
  109. for (uint8_t i = 0; i < 2 * rank; i++)
  110. parameters[i] = &il2cpp_defaults.int32_class->byval_arg;
  111. arrayClass->methods[methodIndex++] = ConstructArrayMethod(arrayClass, ".ctor", &il2cpp_defaults.void_class->byval_arg, 2 * rank, parameters);
  112. }
  113. parameters = (const Il2CppType**)alloca((rank + 1) * sizeof(Il2CppType*));
  114. for (uint8_t i = 0; i < rank; i++)
  115. parameters[i] = &il2cpp_defaults.int32_class->byval_arg;
  116. parameters[rank] = &arrayClass->element_class->byval_arg;
  117. arrayClass->methods[methodIndex++] = ConstructArrayMethod(arrayClass, "Set", &il2cpp_defaults.void_class->byval_arg, rank + 1, parameters);
  118. parameters = (const Il2CppType**)alloca(rank * sizeof(Il2CppType*));
  119. for (uint8_t i = 0; i < rank; i++)
  120. parameters[i] = &il2cpp_defaults.int32_class->byval_arg;
  121. arrayClass->methods[methodIndex++] = ConstructArrayMethod(arrayClass, "Address", &arrayClass->element_class->this_arg, rank, parameters);
  122. parameters = (const Il2CppType**)alloca(rank * sizeof(Il2CppType*));
  123. for (uint8_t i = 0; i < rank; i++)
  124. parameters[i] = &il2cpp_defaults.int32_class->byval_arg;
  125. arrayClass->methods[methodIndex++] = ConstructArrayMethod(arrayClass, "Get", &arrayClass->element_class->byval_arg, rank, parameters);
  126. IL2CPP_ASSERT(methodIndex <= std::numeric_limits<uint16_t>::max());
  127. PopulateArrayGenericMethods(arrayClass, static_cast<uint16_t>(methodIndex));
  128. }
  129. static void CollectImplicitArrayInterfacesFromElementClass(Il2CppClass* elementClass, ::std::vector<Il2CppClass*>& interfaces)
  130. {
  131. while (elementClass != NULL)
  132. {
  133. interfaces.push_back(elementClass);
  134. if (!elementClass->byval_arg.valuetype && elementClass != il2cpp_defaults.value_type_class && elementClass != il2cpp_defaults.enum_class)
  135. {
  136. void* iter = NULL;
  137. while (Il2CppClass* itf = Class::GetInterfaces(elementClass, &iter))
  138. interfaces.push_back(itf);
  139. }
  140. if (elementClass->rank == 1)
  141. {
  142. ::std::vector<Il2CppClass*> elementInterfaces;
  143. CollectImplicitArrayInterfacesFromElementClass(elementClass->element_class, elementInterfaces);
  144. for (::std::vector<Il2CppClass*>::iterator iter = elementInterfaces.begin(); iter != elementInterfaces.end(); ++iter)
  145. {
  146. const Il2CppType* genericArgument = &(*iter)->byval_arg;
  147. interfaces.push_back(Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ilist_class, &genericArgument, 1));
  148. interfaces.push_back(Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_icollection_class, &genericArgument, 1));
  149. interfaces.push_back(Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ienumerable_class, &genericArgument, 1));
  150. interfaces.push_back(Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ireadonlylist_class, &genericArgument, 1));
  151. interfaces.push_back(Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ireadonlycollection_class, &genericArgument, 1));
  152. }
  153. }
  154. elementClass = Class::GetParent(elementClass);
  155. if (elementClass != NULL && (elementClass->byval_arg.valuetype || elementClass == il2cpp_defaults.value_type_class || elementClass == il2cpp_defaults.enum_class))
  156. break;
  157. }
  158. }
  159. static void CollectImplicitArrayInterfaces(Il2CppClass* arrayClass, ::std::vector<Il2CppClass*>& interfaces)
  160. {
  161. if (arrayClass->byval_arg.type != IL2CPP_TYPE_SZARRAY)
  162. return;
  163. CollectImplicitArrayInterfacesFromElementClass(arrayClass->element_class, interfaces);
  164. }
  165. // note assuming list is ordered as IList, ICollection, IEnumerable
  166. static void CollectGenericArrayMethods()
  167. {
  168. const size_t kNumGenericArrayMethods = 13;
  169. s_GenericArrayMethods.reserve(kNumGenericArrayMethods);
  170. void* iter = NULL;
  171. while (const MethodInfo* method = Class::GetMethods(il2cpp_defaults.array_class, &iter))
  172. {
  173. if (strncmp(method->name, "InternalArray__", 15))
  174. continue;
  175. std::string name;
  176. std::string methodName;
  177. Il2CppClass* implementingInterface = NULL;
  178. if (!strncmp(method->name, "InternalArray__ICollection_", 27))
  179. {
  180. implementingInterface = il2cpp_defaults.generic_icollection_class;
  181. methodName = method->name + 27;
  182. name = StringUtils::Printf("System.Collections.Generic.ICollection`1.%s", method->name + 27);
  183. }
  184. else if (!strncmp(method->name, "InternalArray__IEnumerable_", 27))
  185. {
  186. implementingInterface = il2cpp_defaults.generic_ienumerable_class;
  187. methodName = method->name + 27;
  188. name = StringUtils::Printf("System.Collections.Generic.IEnumerable`1.%s", method->name + 27);
  189. }
  190. else if (!strncmp(method->name, "InternalArray__IReadOnlyList_", 29))
  191. {
  192. implementingInterface = il2cpp_defaults.generic_ireadonlylist_class;
  193. methodName = method->name + 29;
  194. name = StringUtils::Printf("System.Collections.Generic.IReadOnlyList`1.%s", method->name + 29);
  195. }
  196. else if (!strncmp(method->name, "InternalArray__IReadOnlyCollection_", 35))
  197. {
  198. implementingInterface = il2cpp_defaults.generic_ireadonlycollection_class;
  199. methodName = method->name + 35;
  200. name = StringUtils::Printf("System.Collections.Generic.IReadOnlyCollection`1.%s", method->name + 35);
  201. }
  202. else
  203. {
  204. implementingInterface = il2cpp_defaults.generic_ilist_class;
  205. methodName = method->name + 15;
  206. name = StringUtils::Printf("System.Collections.Generic.IList`1.%s", method->name + 15);
  207. }
  208. Class::Init(implementingInterface);
  209. const MethodInfo* matchingInterfacesMethod = NULL;
  210. for (int methodIndex = 0; methodIndex < implementingInterface->method_count; methodIndex++)
  211. {
  212. const MethodInfo* interfaceMethod = implementingInterface->methods[methodIndex];
  213. if (methodName == interfaceMethod->name)
  214. matchingInterfacesMethod = interfaceMethod;
  215. }
  216. if (matchingInterfacesMethod != NULL)
  217. {
  218. GenericArrayMethod genericArrayMethod = { StringUtils::StringDuplicate(name.c_str()), method, matchingInterfacesMethod };
  219. s_GenericArrayMethods.push_back(genericArrayMethod);
  220. }
  221. }
  222. }
  223. static size_t GetArrayGenericMethodsCount()
  224. {
  225. if (s_GenericArrayMethods.size() == 0)
  226. CollectGenericArrayMethods();
  227. return s_GenericArrayMethods.size();
  228. }
  229. static MethodInfo* ConstructGenericArrayMethod(const GenericArrayMethod& genericArrayMethod, Il2CppClass* klass, Il2CppGenericContext* context)
  230. {
  231. MethodInfo* inflatedMethod;
  232. if (genericArrayMethod.method->is_generic)
  233. {
  234. inflatedMethod = GenericMethod::AllocateNewMethodInfo(genericArrayMethod.method, context->class_inst, context->method_inst);
  235. }
  236. else
  237. {
  238. inflatedMethod = (MethodInfo*)MetadataCalloc(1, sizeof(MethodInfo));
  239. memcpy(inflatedMethod, genericArrayMethod.method, sizeof(MethodInfo));
  240. }
  241. inflatedMethod->name = genericArrayMethod.name;
  242. // The array methods are owned by the specific array instance, but they do not exist in metadata
  243. // Ensure that the metadata token is zero (and not copied from the method definition) so any
  244. // metadata lookup (e.g. custom attributes) will not find anything
  245. inflatedMethod->klass = klass;
  246. inflatedMethod->token = 0;
  247. return inflatedMethod;
  248. }
  249. static void PopulateArrayGenericMethods(Il2CppClass* klass, uint16_t offset)
  250. {
  251. for (int i = 0; i < klass->interface_offsets_count; i++)
  252. {
  253. Il2CppClass* interfaceType = klass->interfaceOffsets[i].interfaceType;
  254. if (!interfaceType->generic_class)
  255. continue;
  256. Il2CppClass* interfaceDefinition = GenericClass::GetTypeDefinition(interfaceType->generic_class);
  257. Il2CppGenericContext context = { 0 };
  258. context.method_inst = MetadataCache::GetGenericInst(&interfaceType->generic_class->context.class_inst->type_argv[0], 1);
  259. for (GenericArrayMethods::const_iterator iter = s_GenericArrayMethods.begin(); iter != s_GenericArrayMethods.end(); ++iter)
  260. {
  261. if (iter->interfaceMethodDefinition->klass != interfaceDefinition)
  262. continue;
  263. MethodInfo* arrayMethod = ConstructGenericArrayMethod(*iter, klass, &context);
  264. klass->methods[offset++] = arrayMethod;
  265. size_t vtableIndex = klass->interfaceOffsets[i].offset + iter->interfaceMethodDefinition->slot;
  266. klass->vtable[vtableIndex].method = arrayMethod;
  267. klass->vtable[vtableIndex].methodPtr = arrayMethod->virtualMethodPointer;
  268. }
  269. }
  270. }
  271. static void SetupArrayVTableAndInterfaceOffsets(Il2CppClass* klass)
  272. {
  273. Il2CppClass* arrayClass = Class::GetParent(klass);
  274. size_t arrayInterfacesCount = arrayClass->interface_offsets_count;
  275. ::std::vector<Il2CppClass*> interfaces;
  276. if (klass->byval_arg.type == IL2CPP_TYPE_SZARRAY)
  277. {
  278. CollectImplicitArrayInterfaces(klass, interfaces);
  279. }
  280. Il2CppRuntimeInterfaceOffsetPair* newInterfaceOffsets = (Il2CppRuntimeInterfaceOffsetPair*)MetadataMalloc((arrayInterfacesCount + kImplicitArrayInterfaceCount * interfaces.size()) * sizeof(Il2CppRuntimeInterfaceOffsetPair));
  281. memcpy(newInterfaceOffsets, arrayClass->interfaceOffsets, (arrayInterfacesCount) * sizeof(Il2CppRuntimeInterfaceOffsetPair));
  282. int32_t arrayVTableSlot = arrayClass->vtable_count;
  283. size_t slots = arrayVTableSlot + interfaces.size() * (il2cpp_defaults.generic_ilist_class->method_count + il2cpp_defaults.generic_icollection_class->method_count + il2cpp_defaults.generic_ienumerable_class->method_count);
  284. slots += interfaces.size() * (il2cpp_defaults.generic_ireadonlylist_class->method_count + il2cpp_defaults.generic_ireadonlycollection_class->method_count);
  285. memcpy(klass->vtable, arrayClass->vtable, arrayVTableSlot * sizeof(VirtualInvokeData));
  286. size_t index = arrayInterfacesCount;
  287. int32_t vtableSlot = arrayVTableSlot;
  288. for (::std::vector<Il2CppClass*>::iterator iter = interfaces.begin(); iter != interfaces.end(); iter++, index += kImplicitArrayInterfaceCount)
  289. {
  290. const Il2CppType* genericArgument = &(*iter)->byval_arg;
  291. newInterfaceOffsets[index].interfaceType = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ilist_class, &genericArgument, 1);
  292. newInterfaceOffsets[index].offset = vtableSlot;
  293. vtableSlot += newInterfaceOffsets[index].interfaceType->method_count;
  294. newInterfaceOffsets[index + 1].interfaceType = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_icollection_class, &genericArgument, 1);
  295. newInterfaceOffsets[index + 1].offset = vtableSlot;
  296. vtableSlot += newInterfaceOffsets[index + 1].interfaceType->method_count;
  297. newInterfaceOffsets[index + 2].interfaceType = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ienumerable_class, &genericArgument, 1);
  298. newInterfaceOffsets[index + 2].offset = vtableSlot;
  299. vtableSlot += newInterfaceOffsets[index + 2].interfaceType->method_count;
  300. newInterfaceOffsets[index + 3].interfaceType = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ireadonlylist_class, &genericArgument, 1);
  301. newInterfaceOffsets[index + 3].offset = vtableSlot;
  302. vtableSlot += newInterfaceOffsets[index + 3].interfaceType->method_count;
  303. newInterfaceOffsets[index + 4].interfaceType = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ireadonlycollection_class, &genericArgument, 1);
  304. newInterfaceOffsets[index + 4].offset = vtableSlot;
  305. vtableSlot += newInterfaceOffsets[index + 4].interfaceType->method_count;
  306. }
  307. size_t interfaceOffsetsCount = arrayInterfacesCount + kImplicitArrayInterfaceCount * interfaces.size();
  308. IL2CPP_ASSERT(interfaceOffsetsCount <= std::numeric_limits<uint16_t>::max());
  309. klass->interface_offsets_count = static_cast<uint16_t>(interfaceOffsetsCount);
  310. klass->interfaceOffsets = newInterfaceOffsets;
  311. }
  312. void SetupCastClass(Il2CppClass *arrayType)
  313. {
  314. Il2CppClass *elementType = arrayType->element_class;
  315. arrayType->castClass = ArrayMetadata::GetArrayVarianceReducedType(elementType);
  316. arrayType->has_references = Type::IsReference(&elementType->byval_arg) || elementType->has_references;
  317. }
  318. void ArrayMetadata::SetupArrayInterfaces(Il2CppClass* klass, const FastAutoLock& lock)
  319. {
  320. if (klass->byval_arg.type == IL2CPP_TYPE_SZARRAY)
  321. {
  322. IL2CPP_ASSERT(klass->implementedInterfaces == NULL);
  323. const Il2CppType* genericArguments = &klass->element_class->byval_arg;
  324. IL2CPP_ASSERT(klass->interfaces_count == kImplicitArrayInterfaceCount);
  325. klass->implementedInterfaces = (Il2CppClass**)MetadataMalloc(klass->interfaces_count * sizeof(Il2CppClass*));
  326. klass->implementedInterfaces[0] = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ilist_class, &genericArguments, 1);
  327. IL2CPP_ASSERT(klass->implementedInterfaces[0]);
  328. klass->implementedInterfaces[1] = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_icollection_class, &genericArguments, 1);
  329. IL2CPP_ASSERT(klass->implementedInterfaces[1]);
  330. klass->implementedInterfaces[2] = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ienumerable_class, &genericArguments, 1);
  331. IL2CPP_ASSERT(klass->implementedInterfaces[2]);
  332. klass->implementedInterfaces[3] = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ireadonlylist_class, &genericArguments, 1);
  333. IL2CPP_ASSERT(klass->implementedInterfaces[3]);
  334. klass->implementedInterfaces[4] = Class::GetInflatedGenericInstanceClass(il2cpp_defaults.generic_ireadonlycollection_class, &genericArguments, 1);
  335. IL2CPP_ASSERT(klass->implementedInterfaces[4]);
  336. }
  337. }
  338. void ArrayMetadata::SetupArrayVTable(Il2CppClass* klass, const FastAutoLock& lock)
  339. {
  340. // we assume we are being called as part of Class::Init and that the element class has already been initialized
  341. IL2CPP_ASSERT(klass->element_class->initialized);
  342. SetupCastClass(klass);
  343. SetupArrayVTableAndInterfaceOffsets(klass);
  344. SetupArrayMethods(klass);
  345. }
  346. struct SZArrayClassHash
  347. {
  348. size_t operator()(const Il2CppClass* arrayClass) const
  349. {
  350. return Il2CppTypeHash::Hash(&arrayClass->byval_arg);
  351. }
  352. };
  353. struct SZArrayClassCompare
  354. {
  355. bool operator()(const Il2CppClass* arrayClass1, const Il2CppClass* arrayClass2) const
  356. {
  357. return Il2CppTypeEqualityComparer::AreEqual(&arrayClass1->byval_arg, &arrayClass2->byval_arg);
  358. }
  359. };
  360. struct ArrayClassHash
  361. {
  362. size_t operator()(const std::pair<Il2CppClass*, uint32_t>& arrayClass) const
  363. {
  364. return Il2CppTypeHash::Hash(&arrayClass.first->byval_arg) * arrayClass.second;
  365. }
  366. };
  367. struct ArrayClassCompare
  368. {
  369. bool operator()(const std::pair<Il2CppClass*, uint32_t>& arrayClass1, const std::pair<Il2CppClass*, uint32_t>& arrayClass2) const
  370. {
  371. return Il2CppTypeEqualityComparer::AreEqual(&arrayClass1.first->byval_arg, &arrayClass2.first->byval_arg) && arrayClass1.second == arrayClass2.second;
  372. }
  373. };
  374. typedef Il2CppReaderWriterLockedHashMap<Il2CppClass*, Il2CppClass*, SZArrayClassHash, SZArrayClassCompare> SZArrayClassMap;
  375. typedef Il2CppReaderWriterLockedHashMap<std::pair<Il2CppClass*, uint32_t>, Il2CppClass*, ArrayClassHash, ArrayClassCompare> ArrayClassMap;
  376. SZArrayClassMap s_SZArrayClassMap;
  377. ArrayClassMap s_ArrayClassMap;
  378. void ArrayMetadata::Clear()
  379. {
  380. s_SZArrayClassMap.Clear();
  381. s_ArrayClassMap.Clear();
  382. for (auto genericArrayMethod : s_GenericArrayMethods)
  383. IL2CPP_FREE((void*)genericArrayMethod.name);
  384. s_GenericArrayMethods.clear();
  385. }
  386. static Il2CppClass* FindBoundedArrayClass(Il2CppClass* elementClass, uint32_t rank, bool bounded)
  387. {
  388. Il2CppClass* arrayClass = NULL;
  389. if (rank > 1 || bounded)
  390. {
  391. if (s_ArrayClassMap.TryGet(std::make_pair(elementClass, rank), &arrayClass))
  392. return arrayClass;
  393. }
  394. else
  395. {
  396. if (s_SZArrayClassMap.TryGet(elementClass, &arrayClass))
  397. return arrayClass;
  398. }
  399. return NULL;
  400. }
  401. Il2CppClass* ArrayMetadata::GetBoundedArrayClass(Il2CppClass* elementClass, uint32_t rank, bool bounded)
  402. {
  403. IL2CPP_ASSERT(rank <= 255);
  404. if (rank > 1)
  405. bounded = false;
  406. // Check for a cached array class using the reader lock only
  407. Il2CppClass* cachedArrayClass = FindBoundedArrayClass(elementClass, rank, bounded);
  408. if (cachedArrayClass != NULL)
  409. return cachedArrayClass;
  410. FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
  411. // Check if the array class was created while we were waiting for the g_MetadataLock
  412. cachedArrayClass = FindBoundedArrayClass(elementClass, rank, bounded);
  413. if (cachedArrayClass != NULL)
  414. return cachedArrayClass;
  415. Il2CppClass* arrayClass = il2cpp_defaults.array_class;
  416. Class::Init(arrayClass);
  417. //count number of virtual call slots for array class
  418. ::std::vector<Il2CppClass*> interfaces;
  419. if (rank <= 1 && !bounded)
  420. CollectImplicitArrayInterfacesFromElementClass(elementClass, interfaces);
  421. size_t slots = arrayClass->vtable_count + interfaces.size() * (il2cpp_defaults.generic_ilist_class->method_count + il2cpp_defaults.generic_icollection_class->method_count + il2cpp_defaults.generic_ienumerable_class->method_count);
  422. slots += interfaces.size() * (il2cpp_defaults.generic_ireadonlylist_class->method_count + il2cpp_defaults.generic_ireadonlycollection_class->method_count);
  423. Il2CppClass* klass = (Il2CppClass*)MetadataCalloc(1, sizeof(Il2CppClass) + (slots * sizeof(VirtualInvokeData)));
  424. klass->klass = klass;
  425. klass->image = elementClass->image;
  426. // can share the const char* since it's immutable
  427. klass->namespaze = elementClass->namespaze;
  428. klass->name = GetArrayName(elementClass->name, rank, bounded);
  429. klass->parent = il2cpp_defaults.array_class;
  430. klass->flags = TYPE_ATTRIBUTE_AUTO_LAYOUT | TYPE_ATTRIBUTE_ANSI_CLASS | TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_PUBLIC | TYPE_ATTRIBUTE_SEALED | TYPE_ATTRIBUTE_SERIALIZABLE;
  431. klass->rank = rank;
  432. klass->instance_size = Class::GetInstanceSize(arrayClass);
  433. klass->stack_slot_size = sizeof(void*);
  434. klass->vtable_count = static_cast<uint16_t>(slots);
  435. // need this before we access the size or has_references
  436. Class::SetupFields(elementClass);
  437. klass->element_size = Class::GetArrayElementSize(elementClass);
  438. klass->native_size = klass->thread_static_fields_offset = -1;
  439. klass->has_references = Type::IsReference(&elementClass->byval_arg) || elementClass->has_references;
  440. klass->size_inited = true; // set only after instance_size and has_references are set
  441. klass->element_class = elementClass;
  442. SetupCastClass(klass);
  443. if (rank > 1 || bounded)
  444. {
  445. Il2CppArrayType *at = (Il2CppArrayType*)MetadataCalloc(1, sizeof(Il2CppArrayType));
  446. klass->byval_arg.type = IL2CPP_TYPE_ARRAY;
  447. klass->byval_arg.data.array = at;
  448. at->etype = &elementClass->byval_arg;
  449. at->rank = rank;
  450. }
  451. else
  452. {
  453. klass->byval_arg.type = IL2CPP_TYPE_SZARRAY;
  454. klass->byval_arg.data.type = &elementClass->byval_arg;
  455. }
  456. klass->this_arg = klass->byval_arg;
  457. klass->this_arg.byref = 1;
  458. if (rank > 1 || bounded)
  459. {
  460. klass->interfaces_count = 0;
  461. }
  462. else
  463. {
  464. klass->interfaces_count = kImplicitArrayInterfaceCount;
  465. }
  466. klass->interopData = MetadataCache::GetInteropDataForType(&klass->byval_arg);
  467. // Insert the new array class while still holding the g_MetadataLock
  468. // This ensures that the class is only added once
  469. // And WalkSZArrays and WalkArrays (see below) only take the g_MetadataLock and assume that the maps won't be changed
  470. // It's not safe to take the reader/writer lock there because those locks aren't re-entrant and those methods take
  471. // call backs that call arbitrary code
  472. if (rank > 1 || bounded)
  473. s_ArrayClassMap.Add(ArrayClassMap::key_type(std::make_pair(klass->element_class, klass->rank)), klass);
  474. else
  475. s_SZArrayClassMap.Add(klass->element_class, klass);
  476. return klass;
  477. }
  478. void ArrayMetadata::WalkSZArrays(ArrayTypeWalkCallback callback, void* context)
  479. {
  480. FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
  481. for (SZArrayClassMap::iterator it = s_SZArrayClassMap.UnlockedBegin(); it != s_SZArrayClassMap.UnlockedEnd(); it++)
  482. {
  483. callback(it->second, context);
  484. }
  485. }
  486. void ArrayMetadata::WalkArrays(ArrayTypeWalkCallback callback, void* context)
  487. {
  488. FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
  489. for (ArrayClassMap::iterator it = s_ArrayClassMap.UnlockedBegin(); it != s_ArrayClassMap.UnlockedEnd(); it++)
  490. {
  491. callback(it->second, context);
  492. }
  493. }
  494. } /* namespace vm */
  495. } /* namespace il2cpp */