Class.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. #include "il2cpp-config.h"
  2. #include "mono-structs.h"
  3. #include <algorithm>
  4. #include "gc/GCHandle.h"
  5. #include "metadata/ArrayMetadata.h"
  6. #include "metadata/GenericMetadata.h"
  7. #include "metadata/GenericMethod.h"
  8. #include "metadata/FieldLayout.h"
  9. #include "metadata/Il2CppTypeCompare.h"
  10. #include "os/Atomic.h"
  11. #include "os/Mutex.h"
  12. #include "utils/Memory.h"
  13. #include "utils/StringUtils.h"
  14. #include "vm/Assembly.h"
  15. #include "vm/Class.h"
  16. #include "vm/ClassInlines.h"
  17. #include "vm/Exception.h"
  18. #include "vm/Field.h"
  19. #include "vm/GenericClass.h"
  20. #include "vm/GenericContainer.h"
  21. #include "vm/Method.h"
  22. #include "vm/Image.h"
  23. #include "vm/MetadataAlloc.h"
  24. #include "vm/MetadataCache.h"
  25. #include "vm/MetadataLock.h"
  26. #include "vm/Method.h"
  27. #include "vm/Property.h"
  28. #include "vm/Runtime.h"
  29. #include "vm/Reflection.h"
  30. #include "vm/Thread.h"
  31. #include "vm/Type.h"
  32. #include "vm/Object.h"
  33. #include "il2cpp-class-internals.h"
  34. #include "il2cpp-object-internals.h"
  35. #include "il2cpp-runtime-stats.h"
  36. #include "il2cpp-tabledefs.h"
  37. #include "gc/GarbageCollector.h"
  38. #include "utils/Il2CppHashMap.h"
  39. #include "utils/InitOnce.h"
  40. #include "utils/StringUtils.h"
  41. #include "utils/HashUtils.h"
  42. #include <string>
  43. #include <memory.h>
  44. #include <algorithm>
  45. #include <limits>
  46. #include <stdarg.h>
  47. namespace il2cpp
  48. {
  49. namespace vm
  50. {
  51. const int Class::IgnoreNumberOfArguments = -1;
  52. static il2cpp::utils::dynamic_array<Il2CppClass*> s_staticFieldData;
  53. static int32_t s_FinalizerSlot = -1;
  54. static int32_t s_GetHashCodeSlot = -1;
  55. static Il2CppClass* s_EmptyClassList[] = {NULL};
  56. static void SetupGCDescriptor(Il2CppClass* klass, const il2cpp::os::FastAutoLock& lock);
  57. static void GetBitmapNoInit(Il2CppClass* klass, size_t* bitmap, size_t& maxSetBit, size_t parentOffset, const il2cpp::os::FastAutoLock* lockPtr);
  58. static Il2CppClass* ResolveGenericInstanceType(Il2CppClass*, const il2cpp::vm::TypeNameParseInfo&, TypeSearchFlags searchFlags);
  59. static void SetupVTable(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock);
  60. Il2CppClass* Class::FromIl2CppType(const Il2CppType* type, bool throwOnError)
  61. {
  62. Il2CppClass* defaultClass = FromIl2CppTypeEnum(type->type);
  63. if (defaultClass != NULL)
  64. return defaultClass;
  65. switch (type->type)
  66. {
  67. case IL2CPP_TYPE_ARRAY:
  68. {
  69. Il2CppClass* elementClass = FromIl2CppType(type->data.array->etype, throwOnError);
  70. return Class::GetBoundedArrayClass(elementClass, type->data.array->rank, true);
  71. }
  72. case IL2CPP_TYPE_PTR:
  73. return Class::GetPtrClass(type->data.type);
  74. case IL2CPP_TYPE_FNPTR:
  75. return il2cpp_defaults.int_class;
  76. case IL2CPP_TYPE_SZARRAY:
  77. {
  78. Il2CppClass* elementClass = FromIl2CppType(type->data.type, throwOnError);
  79. return Class::GetArrayClass(elementClass, 1);
  80. }
  81. case IL2CPP_TYPE_CLASS:
  82. case IL2CPP_TYPE_VALUETYPE:
  83. return Type::GetClass(type);
  84. case IL2CPP_TYPE_GENERICINST:
  85. return GenericClass::GetClass(type->data.generic_class, throwOnError);
  86. case IL2CPP_TYPE_VAR:
  87. return Class::FromGenericParameter(Type::GetGenericParameterHandle(type));
  88. case IL2CPP_TYPE_MVAR:
  89. return Class::FromGenericParameter(Type::GetGenericParameterHandle(type));
  90. default:
  91. IL2CPP_NOT_IMPLEMENTED(Class::FromIl2CppType);
  92. }
  93. return NULL;
  94. }
  95. Il2CppClass* Class::FromIl2CppTypeEnum(Il2CppTypeEnum type)
  96. {
  97. #define RETURN_DEFAULT_TYPE(fieldName) do { IL2CPP_ASSERT(il2cpp_defaults.fieldName); return il2cpp_defaults.fieldName; } while (false)
  98. switch (type)
  99. {
  100. case IL2CPP_TYPE_OBJECT:
  101. RETURN_DEFAULT_TYPE(object_class);
  102. case IL2CPP_TYPE_VOID:
  103. RETURN_DEFAULT_TYPE(void_class);
  104. case IL2CPP_TYPE_BOOLEAN:
  105. RETURN_DEFAULT_TYPE(boolean_class);
  106. case IL2CPP_TYPE_CHAR:
  107. RETURN_DEFAULT_TYPE(char_class);
  108. case IL2CPP_TYPE_I1:
  109. RETURN_DEFAULT_TYPE(sbyte_class);
  110. case IL2CPP_TYPE_U1:
  111. RETURN_DEFAULT_TYPE(byte_class);
  112. case IL2CPP_TYPE_I2:
  113. RETURN_DEFAULT_TYPE(int16_class);
  114. case IL2CPP_TYPE_U2:
  115. RETURN_DEFAULT_TYPE(uint16_class);
  116. case IL2CPP_TYPE_I4:
  117. RETURN_DEFAULT_TYPE(int32_class);
  118. case IL2CPP_TYPE_U4:
  119. RETURN_DEFAULT_TYPE(uint32_class);
  120. case IL2CPP_TYPE_I:
  121. RETURN_DEFAULT_TYPE(int_class);
  122. case IL2CPP_TYPE_U:
  123. RETURN_DEFAULT_TYPE(uint_class);
  124. case IL2CPP_TYPE_I8:
  125. RETURN_DEFAULT_TYPE(int64_class);
  126. case IL2CPP_TYPE_U8:
  127. RETURN_DEFAULT_TYPE(uint64_class);
  128. case IL2CPP_TYPE_R4:
  129. RETURN_DEFAULT_TYPE(single_class);
  130. case IL2CPP_TYPE_R8:
  131. RETURN_DEFAULT_TYPE(double_class);
  132. case IL2CPP_TYPE_STRING:
  133. RETURN_DEFAULT_TYPE(string_class);
  134. case IL2CPP_TYPE_TYPEDBYREF:
  135. RETURN_DEFAULT_TYPE(typed_reference_class);
  136. case IL2CPP_TYPE_IL2CPP_TYPE_INDEX:
  137. RETURN_DEFAULT_TYPE(systemtype_class);
  138. default:
  139. break;
  140. }
  141. return NULL;
  142. #undef RETURN_DEFAULT_TYPE
  143. }
  144. Il2CppClass* Class::FromSystemType(Il2CppReflectionType *type)
  145. {
  146. Il2CppClass *klass = Class::FromIl2CppType(type->type);
  147. Class::Init(klass);
  148. return klass;
  149. }
  150. static void SetupInterfacesLocked(Il2CppClass* klass, const il2cpp::os::FastAutoLock& lock)
  151. {
  152. if (klass->generic_class)
  153. {
  154. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(klass->generic_class);
  155. Il2CppGenericContext* context = &klass->generic_class->context;
  156. if (genericTypeDefinition->interfaces_count > 0 && klass->implementedInterfaces == NULL)
  157. {
  158. IL2CPP_ASSERT(genericTypeDefinition->interfaces_count == klass->interfaces_count);
  159. klass->implementedInterfaces = (Il2CppClass**)MetadataCalloc(genericTypeDefinition->interfaces_count, sizeof(Il2CppClass*));
  160. for (uint16_t i = 0; i < genericTypeDefinition->interfaces_count; i++)
  161. klass->implementedInterfaces[i] = Class::FromIl2CppType(il2cpp::metadata::GenericMetadata::InflateIfNeeded(MetadataCache::GetInterfaceFromOffset(genericTypeDefinition, i), context, false));
  162. }
  163. }
  164. else if (klass->rank > 0)
  165. {
  166. if (klass->implementedInterfaces == NULL)
  167. il2cpp::metadata::ArrayMetadata::SetupArrayInterfaces(klass, lock);
  168. }
  169. else
  170. {
  171. if (klass->interfaces_count > 0 && klass->implementedInterfaces == NULL)
  172. {
  173. klass->implementedInterfaces = (Il2CppClass**)MetadataCalloc(klass->interfaces_count, sizeof(Il2CppClass*));
  174. for (uint16_t i = 0; i < klass->interfaces_count; i++)
  175. klass->implementedInterfaces[i] = Class::FromIl2CppType(MetadataCache::GetInterfaceFromOffset(klass, i));
  176. }
  177. }
  178. if (klass->implementedInterfaces == NULL)
  179. {
  180. IL2CPP_ASSERT(klass->interfaces_count == 0);
  181. klass->implementedInterfaces = s_EmptyClassList;
  182. }
  183. }
  184. typedef Il2CppHashMap<Il2CppMetadataGenericParameterHandle, Il2CppClass*, utils::PassThroughHash<Il2CppMetadataGenericParameterHandle> > GenericParameterMap;
  185. static GenericParameterMap s_GenericParameterMap;
  186. Il2CppClass* Class::FromGenericParameter(Il2CppMetadataGenericParameterHandle param)
  187. {
  188. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  189. GenericParameterMap::const_iterator iter = s_GenericParameterMap.find(param);
  190. if (iter != s_GenericParameterMap.end())
  191. return iter->second;
  192. Il2CppClass* klass = (Il2CppClass*)MetadataCalloc(1, sizeof(Il2CppClass));
  193. klass->klass = klass;
  194. Il2CppGenericParameterInfo paramInfo = MetadataCache::GetGenericParameterInfo(param);
  195. IL2CPP_ASSERT(paramInfo.containerHandle != NULL);
  196. klass->name = paramInfo.name;
  197. klass->namespaze = "";
  198. klass->image = GenericContainer::GetDeclaringType(paramInfo.containerHandle)->image;
  199. klass->parent = il2cpp_defaults.object_class;
  200. klass->castClass = klass->element_class = klass;
  201. klass->flags = TYPE_ATTRIBUTE_PUBLIC;
  202. bool containerIsMethod = MetadataCache::GetGenericContainerIsMethod(paramInfo.containerHandle);
  203. MetadataCache::MakeGenericArgType(paramInfo.containerHandle, param, &klass->byval_arg);
  204. MetadataCache::MakeGenericArgType(paramInfo.containerHandle, param, &klass->this_arg);
  205. klass->this_arg.byref = true;
  206. klass->instance_size = sizeof(void*);
  207. klass->stack_slot_size = sizeof(void*);
  208. klass->thread_static_fields_size = -1;
  209. klass->native_size = -1;
  210. klass->size_inited = true;
  211. klass->typeHierarchyDepth = 1;
  212. PublishInitialized(klass);
  213. s_GenericParameterMap.insert(std::make_pair(param, klass));
  214. return klass;
  215. }
  216. Il2CppClass* Class::GetElementClass(Il2CppClass *klass)
  217. {
  218. return klass->element_class;
  219. }
  220. const Il2CppType* Class::GetEnumBaseType(Il2CppClass *klass)
  221. {
  222. if (klass->element_class == klass)
  223. /* SRE or broken types */
  224. return NULL;
  225. else
  226. return &klass->element_class->byval_arg;
  227. }
  228. const EventInfo* Class::GetEvents(Il2CppClass *klass, void* *iter)
  229. {
  230. if (!iter)
  231. return NULL;
  232. if (!*iter)
  233. {
  234. Class::SetupEvents(klass);
  235. if (klass->event_count == 0)
  236. return NULL;
  237. *iter = const_cast<EventInfo*>(klass->events);
  238. return klass->events;
  239. }
  240. const EventInfo* eventInfo = (const EventInfo*)*iter;
  241. eventInfo++;
  242. if (eventInfo < klass->events + klass->event_count)
  243. {
  244. *iter = const_cast<EventInfo*>(eventInfo);
  245. return eventInfo;
  246. }
  247. return NULL;
  248. }
  249. FieldInfo* Class::GetFields(Il2CppClass *klass, void* *iter)
  250. {
  251. if (!iter)
  252. return NULL;
  253. if (!*iter)
  254. {
  255. Class::SetupFields(klass);
  256. if (klass->field_count == 0)
  257. return NULL;
  258. *iter = klass->fields;
  259. return klass->fields;
  260. }
  261. FieldInfo* fieldAddress = (FieldInfo*)*iter;
  262. fieldAddress++;
  263. if (fieldAddress < klass->fields + klass->field_count)
  264. {
  265. *iter = fieldAddress;
  266. return fieldAddress;
  267. }
  268. return NULL;
  269. }
  270. FieldInfo* Class::GetFieldFromName(Il2CppClass *klass, const char* name)
  271. {
  272. while (klass)
  273. {
  274. void* iter = NULL;
  275. FieldInfo* field;
  276. while ((field = GetFields(klass, &iter)))
  277. {
  278. if (strcmp(name, Field::GetName(field)) != 0)
  279. continue;
  280. return field;
  281. }
  282. klass = klass->parent;
  283. }
  284. return NULL;
  285. }
  286. const MethodInfo* Class::GetFinalizer(Il2CppClass *klass)
  287. {
  288. if (!klass->initialized)
  289. Class::Init(klass);
  290. if (!klass->has_finalize)
  291. return NULL;
  292. #if IL2CPP_TINY
  293. IL2CPP_ASSERT(0 && "System.Object does not have a finalizer in the Tiny mscorlib, so we don't have a finalizer slot.");
  294. #endif
  295. return klass->vtable[s_FinalizerSlot].method;
  296. }
  297. int32_t Class::GetInstanceSize(const Il2CppClass *klass)
  298. {
  299. IL2CPP_ASSERT(klass->size_inited);
  300. return klass->instance_size;
  301. }
  302. Il2CppClass* Class::GetInterfaces(Il2CppClass *klass, void* *iter)
  303. {
  304. if (!iter)
  305. return NULL;
  306. if (!*iter)
  307. {
  308. Class::SetupInterfaces(klass);
  309. if (klass->interfaces_count == 0)
  310. return NULL;
  311. *iter = &klass->implementedInterfaces[0];
  312. return klass->implementedInterfaces[0];
  313. }
  314. Il2CppClass** interfaceAddress = (Il2CppClass**)*iter;
  315. interfaceAddress++;
  316. if (interfaceAddress < &klass->implementedInterfaces[klass->interfaces_count])
  317. {
  318. *iter = interfaceAddress;
  319. return *interfaceAddress;
  320. }
  321. return NULL;
  322. }
  323. const MethodInfo* Class::GetMethods(Il2CppClass *klass, void* *iter)
  324. {
  325. if (!iter)
  326. return NULL;
  327. if (!*iter)
  328. {
  329. Class::SetupMethods(klass);
  330. if (klass->method_count == 0)
  331. return NULL;
  332. *iter = &klass->methods[0];
  333. return klass->methods[0];
  334. }
  335. const MethodInfo** methodAddress = (const MethodInfo**)*iter;
  336. methodAddress++;
  337. if (methodAddress < &klass->methods[klass->method_count])
  338. {
  339. *iter = methodAddress;
  340. return *methodAddress;
  341. }
  342. return NULL;
  343. }
  344. const MethodInfo* Class::GetMethodFromName(Il2CppClass *klass, const char* name, int argsCount)
  345. {
  346. return GetMethodFromNameFlags(klass, name, argsCount, 0);
  347. }
  348. const MethodInfo* Class::GetMethodFromNameFlags(Il2CppClass *klass, const char* name, int argsCount, int32_t flags)
  349. {
  350. return GetMethodFromNameFlagsAndSig(klass, name, argsCount, flags, NULL);
  351. }
  352. const MethodInfo* Class::GetMethodFromNameFlagsAndSig(Il2CppClass *klass, const char* name, int argsCount, int32_t flags, const Il2CppType** argTypes)
  353. {
  354. Class::Init(klass);
  355. while (klass != NULL)
  356. {
  357. void* iter = NULL;
  358. while (const MethodInfo* method = Class::GetMethods(klass, &iter))
  359. {
  360. if (method->name[0] == name[0] &&
  361. (argsCount == IgnoreNumberOfArguments || method->parameters_count == argsCount) &&
  362. ((method->flags & flags) == flags) &&
  363. !strcmp(name, method->name))
  364. {
  365. bool allArgTypeMatch = true;
  366. if (argTypes != NULL && argsCount != IgnoreNumberOfArguments)
  367. {
  368. for (int i = 0; allArgTypeMatch && i < argsCount; i++)
  369. {
  370. allArgTypeMatch = metadata::Il2CppTypeEqualityComparer::AreEqual(method->parameters[i], argTypes[i]);
  371. }
  372. }
  373. if (allArgTypeMatch)
  374. return method;
  375. }
  376. }
  377. klass = klass->parent;
  378. }
  379. return NULL;
  380. }
  381. const MethodInfo* Class::GetGenericInstanceMethodFromDefintion(Il2CppClass* genericInstanceClass, const MethodInfo* methodDefinition)
  382. {
  383. IL2CPP_ASSERT(Class::IsInflated(genericInstanceClass));
  384. IL2CPP_ASSERT(metadata::Il2CppTypeEqualityComparer::AreEqual(genericInstanceClass->generic_class->type, &methodDefinition->klass->byval_arg));
  385. ptrdiff_t index = methodDefinition - methodDefinition->klass->methods[0];
  386. IL2CPP_ASSERT(index >= 0 && index < methodDefinition->klass->method_count);
  387. IL2CPP_ASSERT(index < genericInstanceClass->method_count);
  388. const MethodInfo* genericClassMethod = genericInstanceClass->methods[index];
  389. IL2CPP_ASSERT(genericClassMethod->genericMethod->methodDefinition == methodDefinition);
  390. return genericClassMethod;
  391. }
  392. const char* Class::GetName(Il2CppClass *klass)
  393. {
  394. return klass->name;
  395. }
  396. const char* Class::GetNamespace(Il2CppClass *klass)
  397. {
  398. return klass->namespaze;
  399. }
  400. Il2CppClass* Class::GetNestedTypes(Il2CppClass *klass, void* *iter)
  401. {
  402. if (!iter)
  403. return NULL;
  404. if (klass->generic_class)
  405. {
  406. IL2CPP_ASSERT(0 && "Class::GetNestedTypes should only be called on non-generic types and generic type definitions");
  407. return NULL;
  408. }
  409. if (!*iter)
  410. {
  411. Class::SetupNestedTypes(klass);
  412. if (klass->nested_type_count == 0)
  413. return NULL;
  414. *iter = &klass->nestedTypes[0];
  415. return klass->nestedTypes[0];
  416. }
  417. Il2CppClass** nestedTypeAddress = (Il2CppClass**)*iter;
  418. nestedTypeAddress++;
  419. if (nestedTypeAddress < &klass->nestedTypes[klass->nested_type_count])
  420. {
  421. *iter = nestedTypeAddress;
  422. return *nestedTypeAddress;
  423. }
  424. return NULL;
  425. }
  426. size_t Class::GetNumMethods(const Il2CppClass* klass)
  427. {
  428. return klass->method_count;
  429. }
  430. size_t Class::GetNumProperties(const Il2CppClass* klass)
  431. {
  432. return klass->property_count;
  433. }
  434. size_t Class::GetNumFields(const Il2CppClass* klass)
  435. {
  436. return klass->field_count;
  437. }
  438. Il2CppClass* Class::GetParent(Il2CppClass *klass)
  439. {
  440. return klass->parent;
  441. }
  442. const PropertyInfo* Class::GetProperties(Il2CppClass *klass, void* *iter)
  443. {
  444. if (!iter)
  445. return NULL;
  446. if (!*iter)
  447. {
  448. Class::SetupProperties(klass);
  449. if (klass->property_count == 0)
  450. return NULL;
  451. *iter = const_cast<PropertyInfo*>(klass->properties);
  452. return klass->properties;
  453. }
  454. const PropertyInfo* property = (const PropertyInfo*)*iter;
  455. property++;
  456. if (property < klass->properties + klass->property_count)
  457. {
  458. *iter = const_cast<PropertyInfo*>(property);
  459. return property;
  460. }
  461. return NULL;
  462. }
  463. const PropertyInfo* Class::GetPropertyFromName(Il2CppClass *klass, const char* name)
  464. {
  465. while (klass)
  466. {
  467. void* iter = NULL;
  468. while (const PropertyInfo* prop = GetProperties(klass, &iter))
  469. {
  470. if (strcmp(name, Property::GetName(prop)) != 0)
  471. continue;
  472. return prop;
  473. }
  474. klass = klass->parent;
  475. }
  476. return NULL;
  477. }
  478. int32_t Class::GetValueSize(Il2CppClass *klass, uint32_t *align)
  479. {
  480. int32_t size;
  481. if (!klass->size_init_pending)
  482. SetupFields(klass);
  483. IL2CPP_ASSERT(klass->byval_arg.valuetype);
  484. if (!klass->size_inited)
  485. {
  486. // If the size of a value type is not intialized, we cannot continue.
  487. // This might mean this is a recursively defined struct, where one value type
  488. // has a field of anotehr value type, which in turn has the first as a field.
  489. // The runtime should throw a type load exception in this case.
  490. std::string message;
  491. message += "Could not load type '";
  492. if (strlen(klass->namespaze) != 0)
  493. {
  494. message += klass->namespaze;
  495. message += ":";
  496. }
  497. message += klass->name;
  498. message += "'";
  499. Class::SetClassInitializationError(klass, il2cpp::vm::Exception::GetTypeLoadException(message.c_str()));
  500. size = 1;
  501. }
  502. else
  503. {
  504. size = Class::GetInstanceSize(klass) - sizeof(Il2CppObject);
  505. }
  506. if (align)
  507. *align = klass->minimumAlignment;
  508. return size;
  509. }
  510. bool Class::HasParent(Il2CppClass *klass, Il2CppClass *parent)
  511. {
  512. Class::SetupTypeHierarchy(klass);
  513. Class::SetupTypeHierarchy(parent);
  514. return ClassInlines::HasParentUnsafe(klass, parent);
  515. }
  516. bool Class::IsAssignableFrom(Il2CppClass *klass, Il2CppClass *oklass)
  517. {
  518. // Cast to original class - fast path
  519. if (klass == oklass)
  520. return true;
  521. Class::Init(klass);
  522. Class::Init(oklass);
  523. // Following checks are always going to fail for interfaces
  524. if (!IsInterface(klass))
  525. {
  526. // Array
  527. if (klass->rank)
  528. {
  529. if (oklass->rank != klass->rank)
  530. return false;
  531. if (Class::IsValuetype(oklass->castClass))
  532. {
  533. // Full array covariance is defined only for reference types.
  534. // For value types, array element reduced types must match
  535. return klass->castClass == oklass->castClass;
  536. }
  537. return Class::IsAssignableFrom(klass->castClass, oklass->castClass);
  538. }
  539. // System.Object
  540. else if (klass == il2cpp_defaults.object_class)
  541. {
  542. return true;
  543. }
  544. // Left is System.Nullable<>
  545. else if (Class::IsNullable(klass))
  546. {
  547. if (Class::IsNullable(oklass))
  548. IL2CPP_NOT_IMPLEMENTED(Class::IsAssignableFrom);
  549. Il2CppClass* nullableArg = Class::GetNullableArgument(klass);
  550. return Class::IsAssignableFrom(nullableArg, oklass);
  551. }
  552. if (klass->parent == il2cpp_defaults.multicastdelegate_class && klass->generic_class != NULL)
  553. {
  554. if (IsGenericClassAssignableFrom(klass, oklass, oklass))
  555. return true;
  556. }
  557. return ClassInlines::HasParentUnsafe(oklass, klass);
  558. }
  559. if (klass->generic_class != NULL)
  560. {
  561. // checking for simple reference equality is not enough in this case because generic interface might have covariant and/or contravariant parameters
  562. for (Il2CppClass* iter = oklass; iter != NULL; iter = iter->parent)
  563. {
  564. if (IsGenericClassAssignableFrom(klass, iter, oklass))
  565. return true;
  566. for (uint16_t i = 0; i < iter->interfaces_count; ++i)
  567. {
  568. if (IsGenericClassAssignableFrom(klass, iter->implementedInterfaces[i], oklass))
  569. return true;
  570. }
  571. for (uint16_t i = 0; i < iter->interface_offsets_count; ++i)
  572. {
  573. if (IsGenericClassAssignableFrom(klass, iter->interfaceOffsets[i].interfaceType, oklass))
  574. return true;
  575. }
  576. }
  577. }
  578. else
  579. {
  580. for (Il2CppClass* iter = oklass; iter != NULL; iter = iter->parent)
  581. {
  582. for (uint16_t i = 0; i < iter->interfaces_count; ++i)
  583. {
  584. if (iter->implementedInterfaces[i] == klass)
  585. return true;
  586. }
  587. // Check the interfaces we may have grafted on to the type (e.g IList,
  588. // ICollection, IEnumerable for array types).
  589. for (uint16_t i = 0; i < iter->interface_offsets_count; ++i)
  590. {
  591. if (iter->interfaceOffsets[i].interfaceType == klass)
  592. return true;
  593. }
  594. }
  595. }
  596. return false;
  597. }
  598. bool Class::IsAssignableFrom(Il2CppReflectionType * type, Il2CppReflectionType * c)
  599. {
  600. Il2CppClass *klass;
  601. Il2CppClass *klassc;
  602. klass = FromIl2CppType(type->type);
  603. klassc = FromIl2CppType(c->type);
  604. if (type->type->byref && !c->type->byref)
  605. return false;
  606. return IsAssignableFrom(klass, klassc);
  607. }
  608. bool Class::IsGeneric(const Il2CppClass *klass)
  609. {
  610. return klass->is_generic;
  611. }
  612. bool Class::IsInflated(const Il2CppClass *klass)
  613. {
  614. return klass->generic_class != NULL;
  615. }
  616. bool Class::IsGenericTypeDefinition(const Il2CppClass* klass)
  617. {
  618. return IsGeneric(klass) && !IsInflated(klass);
  619. }
  620. bool Class::IsSubclassOf(Il2CppClass *klass, Il2CppClass *klassc, bool check_interfaces)
  621. {
  622. Class::SetupTypeHierarchy(klass);
  623. Class::SetupTypeHierarchy(klassc);
  624. Class::SetupInterfaces(klass);
  625. if (check_interfaces && IsInterface(klassc) && !IsInterface(klass))
  626. {
  627. Il2CppClass *oklass = klass;
  628. while (oklass)
  629. {
  630. Class::SetupInterfaces(oklass);
  631. // TODO: we probably need to implement a faster check here
  632. for (uint16_t i = 0; i < oklass->interfaces_count; i++)
  633. {
  634. if (oklass->implementedInterfaces[i] == klassc)
  635. return true;
  636. }
  637. oklass = oklass->parent;
  638. }
  639. }
  640. else if (check_interfaces && IsInterface(klassc) && IsInterface(klass))
  641. {
  642. // TODO: we probably need to implement a faster check here
  643. for (uint16_t i = 0; i < klass->interfaces_count; i++)
  644. {
  645. if (klass->implementedInterfaces[i] == klassc)
  646. return true;
  647. }
  648. }
  649. else
  650. {
  651. if (!IsInterface(klass) && ClassInlines::HasParentUnsafe(klass, klassc))
  652. return true;
  653. }
  654. /*
  655. * MS.NET thinks interfaces are a subclass of Object, so we think it as
  656. * well.
  657. */
  658. if (klassc == il2cpp_defaults.object_class)
  659. return true;
  660. return false;
  661. }
  662. enum FieldLayoutKind
  663. {
  664. FIELD_LAYOUT_INSTANCE,
  665. FIELD_LAYOUT_STATIC,
  666. FIELD_LAYOUT_THREADSTATIC,
  667. };
  668. static void SetupFieldOffsetsLocked(FieldLayoutKind fieldLayoutKind, Il2CppClass* klass, size_t size, const std::vector<size_t>& fieldOffsets, const il2cpp::os::FastAutoLock& lock)
  669. {
  670. IL2CPP_ASSERT(size < std::numeric_limits<uint32_t>::max());
  671. if (fieldLayoutKind == FIELD_LAYOUT_INSTANCE)
  672. klass->instance_size = static_cast<uint32_t>(size);
  673. if (fieldLayoutKind == FIELD_LAYOUT_STATIC)
  674. klass->static_fields_size = static_cast<uint32_t>(size);
  675. if (fieldLayoutKind == FIELD_LAYOUT_THREADSTATIC)
  676. klass->thread_static_fields_size = static_cast<uint32_t>(size);
  677. if (!(klass->flags & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
  678. {
  679. size_t fieldIndex = 0;
  680. for (uint16_t i = 0; i < klass->field_count; i++)
  681. {
  682. FieldInfo* field = klass->fields + i;
  683. if (fieldLayoutKind == FIELD_LAYOUT_INSTANCE && field->type->attrs & FIELD_ATTRIBUTE_STATIC)
  684. continue;
  685. if (fieldLayoutKind == FIELD_LAYOUT_STATIC && !Field::IsNormalStatic(field))
  686. continue;
  687. if (fieldLayoutKind == FIELD_LAYOUT_THREADSTATIC && !Field::IsThreadStatic(field))
  688. continue;
  689. if (fieldLayoutKind == FIELD_LAYOUT_THREADSTATIC)
  690. {
  691. field->offset = THREAD_STATIC_FIELD_OFFSET;
  692. MetadataCache::AddThreadLocalStaticOffsetForFieldLocked(field, static_cast<int32_t>(fieldOffsets[fieldIndex]), lock);
  693. fieldIndex++;
  694. continue;
  695. }
  696. field->offset = static_cast<int32_t>(fieldOffsets[fieldIndex]);
  697. fieldIndex++;
  698. }
  699. }
  700. }
  701. static void ValidateFieldOffsets(FieldLayoutKind fieldLayoutKind, Il2CppClass* klass, size_t size, const std::vector<size_t>& fieldOffsets)
  702. {
  703. if (fieldLayoutKind == FIELD_LAYOUT_INSTANCE && klass->parent && !(klass->flags & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
  704. IL2CPP_ASSERT(klass->instance_size == size);
  705. if (fieldLayoutKind == FIELD_LAYOUT_STATIC)
  706. IL2CPP_ASSERT(klass->static_fields_size == size);
  707. if (fieldLayoutKind == FIELD_LAYOUT_THREADSTATIC)
  708. IL2CPP_ASSERT(klass->thread_static_fields_size == size);
  709. if (!(klass->flags & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
  710. {
  711. size_t fieldIndex = 0;
  712. for (uint16_t i = 0; i < klass->field_count; i++)
  713. {
  714. FieldInfo* field = klass->fields + i;
  715. if (fieldLayoutKind == FIELD_LAYOUT_INSTANCE && field->type->attrs & FIELD_ATTRIBUTE_STATIC)
  716. continue;
  717. if (fieldLayoutKind == FIELD_LAYOUT_STATIC && !Field::IsNormalStatic(field))
  718. continue;
  719. if (fieldLayoutKind == FIELD_LAYOUT_THREADSTATIC && !Field::IsThreadStatic(field))
  720. continue;
  721. if (fieldLayoutKind == FIELD_LAYOUT_THREADSTATIC)
  722. {
  723. IL2CPP_ASSERT(fieldOffsets[fieldIndex] == -1);
  724. fieldIndex++;
  725. continue;
  726. }
  727. IL2CPP_ASSERT(field->offset == fieldOffsets[fieldIndex]);
  728. fieldIndex++;
  729. }
  730. }
  731. }
  732. static size_t UpdateInstanceSizeForGenericClass(Il2CppClass* klass, size_t instanceSize)
  733. {
  734. // need to set this in case there are no fields in a generic instance type
  735. if (klass->generic_class)
  736. {
  737. const Il2CppClass* genericTypeDef = il2cpp::vm::MetadataCache::GetTypeInfoFromType(klass->generic_class->type);
  738. // If the generic class has an instance size, it was explictly set
  739. if (genericTypeDef->instance_size > 0 && genericTypeDef->instance_size > instanceSize)
  740. instanceSize = genericTypeDef->instance_size;
  741. klass->instance_size = static_cast<uint32_t>(instanceSize);
  742. }
  743. return instanceSize;
  744. }
  745. static void LayoutFieldsLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  746. {
  747. if (Class::IsGeneric(klass))
  748. return;
  749. size_t instanceSize = 0;
  750. size_t actualSize = 0;
  751. if (klass->parent)
  752. {
  753. IL2CPP_ASSERT(klass->parent->size_inited);
  754. klass->has_references |= klass->parent->has_references;
  755. instanceSize = klass->parent->instance_size;
  756. actualSize = klass->parent->actualSize;
  757. if (klass->byval_arg.valuetype)
  758. klass->minimumAlignment = 1;
  759. else
  760. klass->minimumAlignment = klass->parent->minimumAlignment;
  761. }
  762. else
  763. {
  764. actualSize = instanceSize = sizeof(Il2CppObject);
  765. klass->minimumAlignment = sizeof(Il2CppObject*);
  766. }
  767. if (klass->field_count)
  768. {
  769. for (uint16_t i = 0; i < klass->field_count; i++)
  770. {
  771. FieldInfo* field = klass->fields + i;
  772. if (!Field::IsInstance(field))
  773. continue;
  774. const Il2CppType* ftype = Type::GetUnderlyingType(field->type);
  775. if (Type::IsReference(ftype) || (Type::IsStruct(ftype) && Class::HasReferences(Class::FromIl2CppType(ftype))))
  776. klass->has_references = true;
  777. }
  778. il2cpp::metadata::FieldLayout::FieldLayoutData layoutData;
  779. il2cpp::metadata::FieldLayout::FieldLayoutData staticLayoutData;
  780. il2cpp::metadata::FieldLayout::FieldLayoutData threadStaticLayoutData;
  781. // If a type has references, ignore any packing
  782. // All reference types must be aligned on a pointer sized boundary
  783. // And if it has references, it not blittable, and we can layout the type anyway we want
  784. uint8_t packingSize = klass->packingSize;
  785. if (klass->has_references)
  786. packingSize = 0;
  787. il2cpp::metadata::FieldLayout::LayoutInstanceFields(klass, instanceSize, actualSize, klass->minimumAlignment, packingSize, layoutData);
  788. instanceSize = layoutData.classSize;
  789. // This is a value type with no instance fields, but at least one static field.
  790. if (klass->byval_arg.valuetype && layoutData.FieldOffsets.size() == 0)
  791. {
  792. instanceSize = IL2CPP_SIZEOF_STRUCT_WITH_NO_INSTANCE_FIELDS + sizeof(Il2CppObject);
  793. klass->actualSize = IL2CPP_SIZEOF_STRUCT_WITH_NO_INSTANCE_FIELDS + sizeof(Il2CppObject);
  794. }
  795. instanceSize = UpdateInstanceSizeForGenericClass(klass, instanceSize);
  796. klass->size_inited = true;
  797. il2cpp::metadata::FieldLayout::LayoutStaticFields(klass, staticLayoutData);
  798. il2cpp::metadata::FieldLayout::LayoutThreadStaticFields(klass, threadStaticLayoutData);
  799. klass->minimumAlignment = layoutData.minimumAlignment;
  800. if (klass->generic_class != NULL || MetadataCache::StructLayoutSizeIsDefault(klass->typeMetadataHandle))
  801. klass->actualSize = static_cast<uint32_t>(layoutData.actualClassSize);
  802. else
  803. klass->actualSize = klass->instance_size;
  804. if (klass->image == il2cpp_defaults.corlib && !strcmp("Ephemeron", klass->name))
  805. {
  806. klass->has_references = false;
  807. }
  808. size_t staticSize = staticLayoutData.classSize;
  809. size_t threadStaticSize = threadStaticLayoutData.classSize;
  810. if (klass->generic_class)
  811. {
  812. SetupFieldOffsetsLocked(FIELD_LAYOUT_INSTANCE, klass, instanceSize, layoutData.FieldOffsets, lock);
  813. SetupFieldOffsetsLocked(FIELD_LAYOUT_STATIC, klass, staticSize, staticLayoutData.FieldOffsets, lock);
  814. SetupFieldOffsetsLocked(FIELD_LAYOUT_THREADSTATIC, klass, threadStaticSize, threadStaticLayoutData.FieldOffsets, lock);
  815. }
  816. else
  817. {
  818. #if IL2CPP_ENABLE_VALIDATE_FIELD_LAYOUT
  819. ValidateFieldOffsets(FIELD_LAYOUT_INSTANCE, klass, instanceSize, layoutData.FieldOffsets);
  820. ValidateFieldOffsets(FIELD_LAYOUT_STATIC, klass, staticSize, staticLayoutData.FieldOffsets);
  821. ValidateFieldOffsets(FIELD_LAYOUT_THREADSTATIC, klass, threadStaticSize, threadStaticLayoutData.FieldOffsets);
  822. #endif
  823. }
  824. }
  825. else
  826. {
  827. // need to set this in case there are no fields in a generic instance type
  828. instanceSize = UpdateInstanceSizeForGenericClass(klass, instanceSize);
  829. // Always set the actual size, as a derived class without fields could end up
  830. // with the wrong actual size (i.e. sizeof may be incorrect), if the last
  831. // field of the base class doesn't go to an alignment boundary and the compiler ABI
  832. // uses that extra space (as clang does).
  833. klass->actualSize = static_cast<uint32_t>(actualSize);
  834. }
  835. if (klass->static_fields_size)
  836. {
  837. klass->static_fields = il2cpp::gc::GarbageCollector::AllocateFixed(klass->static_fields_size, NULL);
  838. s_staticFieldData.push_back(klass);
  839. il2cpp_runtime_stats.class_static_data_size += klass->static_fields_size;
  840. }
  841. if (klass->thread_static_fields_size)
  842. klass->thread_static_fields_offset = il2cpp::vm::Thread::AllocThreadStaticData(klass->thread_static_fields_size);
  843. if (Class::IsValuetype(klass))
  844. klass->stack_slot_size = klass->instance_size - sizeof(Il2CppObject);
  845. else
  846. klass->stack_slot_size = sizeof(void*);
  847. }
  848. static void SetupFieldsFromDefinitionLocked(Il2CppClass* klass, const il2cpp::os::FastAutoLock& lock)
  849. {
  850. if (klass->field_count == 0)
  851. {
  852. klass->fields = NULL;
  853. return;
  854. }
  855. FieldInfo* fields = (FieldInfo*)MetadataCalloc(klass->field_count, sizeof(FieldInfo));
  856. FieldInfo* newField = fields;
  857. FieldIndex end = klass->field_count;
  858. for (FieldIndex fieldIndex = 0; fieldIndex < end; ++fieldIndex)
  859. {
  860. Il2CppMetadataFieldInfo fieldInfo = MetadataCache::GetFieldInfo(klass, fieldIndex);
  861. newField->type = fieldInfo.type;
  862. newField->name = fieldInfo.name;
  863. newField->parent = klass;
  864. newField->offset = MetadataCache::GetFieldOffsetFromIndexLocked(klass, fieldIndex, newField, lock);
  865. newField->token = fieldInfo.token;
  866. newField++;
  867. }
  868. klass->fields = fields;
  869. }
  870. // passing lock to ensure we have acquired it. We can add asserts later
  871. void SetupFieldsLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  872. {
  873. if (klass->size_inited)
  874. return;
  875. klass->size_init_pending = true;
  876. if (klass->parent && !klass->parent->size_inited)
  877. SetupFieldsLocked(klass->parent, lock);
  878. if (klass->generic_class)
  879. {
  880. // for generic instance types, they just inflate the fields of their generic type definition
  881. // initialize the generic type's fields and delegate to the generic logic
  882. SetupFieldsLocked(GenericClass::GetTypeDefinition(klass->generic_class), lock);
  883. GenericClass::SetupFields(klass);
  884. }
  885. else
  886. {
  887. SetupFieldsFromDefinitionLocked(klass, lock);
  888. }
  889. if (!Class::IsGeneric(klass))
  890. LayoutFieldsLocked(klass, lock);
  891. // Set the init flags after a barrier so they are set after all data is written
  892. il2cpp::os::Atomic::FullMemoryBarrier();
  893. klass->size_init_pending = false;
  894. klass->size_inited = true;
  895. }
  896. void Class::SetupFields(Il2CppClass *klass)
  897. {
  898. if (!klass->size_inited)
  899. {
  900. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  901. SetupFieldsLocked(klass, lock);
  902. }
  903. }
  904. // passing lock to ensure we have acquired it. We can add asserts later
  905. void SetupMethodsLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  906. {
  907. if ((!klass->method_count && !klass->rank) || klass->methods)
  908. return;
  909. if (klass->generic_class)
  910. {
  911. Class::InitLocked(GenericClass::GetTypeDefinition(klass->generic_class), lock);
  912. GenericClass::SetupMethods(klass);
  913. }
  914. else if (klass->rank)
  915. {
  916. Class::InitLocked(klass->element_class, lock);
  917. SetupVTable(klass, lock);
  918. }
  919. else
  920. {
  921. if (klass->method_count == 0)
  922. {
  923. klass->methods = NULL;
  924. return;
  925. }
  926. klass->methods = (const MethodInfo**)MetadataCalloc(klass->method_count, sizeof(MethodInfo*));
  927. MethodInfo* methods = (MethodInfo*)MetadataCalloc(klass->method_count, sizeof(MethodInfo));
  928. MethodInfo* newMethod = methods;
  929. MethodIndex end = klass->method_count;
  930. for (MethodIndex index = 0; index < end; ++index)
  931. {
  932. Il2CppMetadataMethodInfo methodInfo = MetadataCache::GetMethodInfo(klass, index);
  933. newMethod->name = methodInfo.name;
  934. newMethod->methodPointer = MetadataCache::GetMethodPointer(klass->image, methodInfo.token);
  935. if (klass->byval_arg.valuetype)
  936. {
  937. Il2CppMethodPointer adjustorThunk = MetadataCache::GetAdjustorThunk(klass->image, methodInfo.token);
  938. if (adjustorThunk != NULL)
  939. newMethod->virtualMethodPointer = adjustorThunk;
  940. }
  941. // We did not find an adjustor thunk, or maybe did not need to look for one. Let's get the real method pointer.
  942. if (newMethod->virtualMethodPointer == NULL)
  943. newMethod->virtualMethodPointer = newMethod->methodPointer;
  944. newMethod->klass = klass;
  945. newMethod->return_type = methodInfo.return_type;
  946. newMethod->parameters_count = (uint8_t)methodInfo.parameterCount;
  947. const Il2CppType** parameters = (const Il2CppType**)MetadataCalloc(methodInfo.parameterCount, sizeof(Il2CppType*));
  948. for (uint16_t paramIndex = 0; paramIndex < methodInfo.parameterCount; ++paramIndex)
  949. {
  950. Il2CppMetadataParameterInfo paramInfo = MetadataCache::GetParameterInfo(klass, methodInfo.handle, paramIndex);
  951. parameters[paramIndex] = paramInfo.type;
  952. }
  953. newMethod->parameters = parameters;
  954. newMethod->flags = methodInfo.flags;
  955. newMethod->iflags = methodInfo.iflags;
  956. newMethod->slot = methodInfo.slot;
  957. newMethod->is_inflated = false;
  958. newMethod->token = methodInfo.token;
  959. newMethod->methodMetadataHandle = methodInfo.handle;
  960. newMethod->genericContainerHandle = MetadataCache::GetGenericContainerFromMethod(methodInfo.handle);
  961. if (newMethod->genericContainerHandle)
  962. newMethod->is_generic = true;
  963. newMethod->has_full_generic_sharing_signature = false;
  964. if (newMethod->virtualMethodPointer)
  965. {
  966. newMethod->invoker_method = MetadataCache::GetMethodInvoker(klass->image, methodInfo.token);
  967. }
  968. else
  969. {
  970. newMethod->invoker_method = Runtime::GetMissingMethodInvoker();
  971. il2cpp::vm::Il2CppUnresolvedCallStubs stubs = MetadataCache::GetUnresovledCallStubs(newMethod);
  972. newMethod->methodPointer = stubs.methodPointer;
  973. newMethod->virtualMethodPointer = stubs.virtualMethodPointer;
  974. }
  975. klass->methods[index] = newMethod;
  976. newMethod++;
  977. }
  978. }
  979. }
  980. void Class::SetupMethods(Il2CppClass *klass)
  981. {
  982. if (klass->method_count || klass->rank)
  983. {
  984. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  985. SetupMethodsLocked(klass, lock);
  986. }
  987. }
  988. void SetupNestedTypesLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  989. {
  990. if (klass->generic_class || klass->nestedTypes)
  991. return;
  992. if (klass->nested_type_count > 0)
  993. {
  994. klass->nestedTypes = (Il2CppClass**)MetadataCalloc(klass->nested_type_count, sizeof(Il2CppClass*));
  995. for (uint16_t i = 0; i < klass->nested_type_count; i++)
  996. klass->nestedTypes[i] = MetadataCache::GetNestedTypeFromOffset(klass, i);
  997. }
  998. }
  999. void Class::SetupNestedTypes(Il2CppClass *klass)
  1000. {
  1001. if (klass->generic_class || klass->nestedTypes)
  1002. return;
  1003. if (klass->nested_type_count)
  1004. {
  1005. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  1006. SetupNestedTypesLocked(klass, lock);
  1007. }
  1008. }
  1009. static void SetupVTable(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  1010. {
  1011. if (klass->is_vtable_initialized)
  1012. return;
  1013. if (klass->generic_class)
  1014. {
  1015. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(klass->generic_class);
  1016. Il2CppGenericContext* context = &klass->generic_class->context;
  1017. if (genericTypeDefinition->interface_offsets_count > 0 && klass->interfaceOffsets == NULL)
  1018. {
  1019. klass->interface_offsets_count = genericTypeDefinition->interface_offsets_count;
  1020. klass->interfaceOffsets = (Il2CppRuntimeInterfaceOffsetPair*)MetadataCalloc(genericTypeDefinition->interface_offsets_count, sizeof(Il2CppRuntimeInterfaceOffsetPair));
  1021. for (uint16_t i = 0; i < genericTypeDefinition->interface_offsets_count; i++)
  1022. {
  1023. Il2CppInterfaceOffsetInfo interfaceOffset = MetadataCache::GetInterfaceOffsetInfo(genericTypeDefinition, i);
  1024. klass->interfaceOffsets[i].offset = interfaceOffset.offset;
  1025. klass->interfaceOffsets[i].interfaceType = Class::FromIl2CppType(il2cpp::metadata::GenericMetadata::InflateIfNeeded(interfaceOffset.interfaceType, context, false));
  1026. }
  1027. }
  1028. if (genericTypeDefinition->vtable_count > 0)
  1029. {
  1030. klass->vtable_count = genericTypeDefinition->vtable_count;
  1031. for (uint16_t i = 0; i < genericTypeDefinition->vtable_count; i++)
  1032. {
  1033. const MethodInfo* method = MetadataCache::GetMethodInfoFromVTableSlot(genericTypeDefinition, i);
  1034. if (method && method->klass)
  1035. {
  1036. if (method && method->is_inflated)
  1037. {
  1038. const Il2CppGenericMethod* genericMethod = il2cpp::metadata::GenericMetadata::Inflate(method->genericMethod, context);
  1039. method = il2cpp::metadata::GenericMethod::GetMethod(genericMethod);
  1040. }
  1041. if (method && method->klass && Class::IsGeneric(method->klass))
  1042. {
  1043. method = il2cpp::metadata::GenericMethod::GetMethod(method, context->class_inst, NULL);
  1044. }
  1045. }
  1046. klass->vtable[i].method = method;
  1047. if (method != NULL)
  1048. {
  1049. // For default interface methods on generic interfaces we need to ensure that their rgctx's are initalized
  1050. if (method->klass != NULL && method->klass != klass && Method::IsDefaultInterfaceMethodOnGenericInstance(method))
  1051. Class::InitLocked(method->klass, lock);
  1052. if (method->virtualMethodPointer)
  1053. klass->vtable[i].methodPtr = method->virtualMethodPointer;
  1054. else if (method->is_inflated && !method->is_generic && !method->genericMethod->context.method_inst)
  1055. klass->vtable[i].methodPtr = MetadataCache::GetUnresovledCallStubs(method).virtualMethodPointer;
  1056. else
  1057. klass->vtable[i].methodPtr = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo()->methodPointer;
  1058. }
  1059. else
  1060. {
  1061. klass->vtable[i].method = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo();
  1062. klass->vtable[i].methodPtr = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo()->methodPointer;
  1063. }
  1064. }
  1065. }
  1066. }
  1067. else if (klass->rank)
  1068. {
  1069. Class::InitLocked(klass->element_class, lock);
  1070. il2cpp::metadata::ArrayMetadata::SetupArrayVTable(klass, lock);
  1071. }
  1072. else
  1073. {
  1074. if (klass->interface_offsets_count > 0 && klass->interfaceOffsets == NULL)
  1075. {
  1076. klass->interfaceOffsets = (Il2CppRuntimeInterfaceOffsetPair*)MetadataCalloc(klass->interface_offsets_count, sizeof(Il2CppRuntimeInterfaceOffsetPair));
  1077. for (uint16_t i = 0; i < klass->interface_offsets_count; i++)
  1078. {
  1079. Il2CppInterfaceOffsetInfo interfaceOffset = MetadataCache::GetInterfaceOffsetInfo(klass, i);
  1080. klass->interfaceOffsets[i].offset = interfaceOffset.offset;
  1081. klass->interfaceOffsets[i].interfaceType = Class::FromIl2CppType(interfaceOffset.interfaceType);
  1082. }
  1083. }
  1084. if (klass->vtable_count > 0)
  1085. {
  1086. for (uint16_t i = 0; i < klass->vtable_count; i++)
  1087. {
  1088. const MethodInfo* method = MetadataCache::GetMethodInfoFromVTableSlot(klass, i);
  1089. klass->vtable[i].method = method;
  1090. if (method != NULL)
  1091. {
  1092. if (method->virtualMethodPointer)
  1093. klass->vtable[i].methodPtr = method->virtualMethodPointer;
  1094. else
  1095. klass->vtable[i].methodPtr = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo()->methodPointer;
  1096. }
  1097. else
  1098. {
  1099. klass->vtable[i].method = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo();
  1100. klass->vtable[i].methodPtr = il2cpp::vm::Method::GetEntryPointNotFoundMethodInfo()->methodPointer;
  1101. }
  1102. }
  1103. }
  1104. }
  1105. klass->is_vtable_initialized = 1;
  1106. }
  1107. static void SetupEventsLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  1108. {
  1109. if (klass->generic_class)
  1110. {
  1111. Class::InitLocked(GenericClass::GetTypeDefinition(klass->generic_class), lock);
  1112. GenericClass::SetupEvents(klass);
  1113. }
  1114. else if (klass->rank > 0)
  1115. {
  1116. // do nothing, arrays have no events
  1117. IL2CPP_ASSERT(klass->event_count == 0);
  1118. }
  1119. else if (klass->event_count != 0)
  1120. {
  1121. // we need methods initialized since we reference them via index below
  1122. SetupMethodsLocked(klass, lock);
  1123. EventInfo* events = (EventInfo*)MetadataCalloc(klass->event_count, sizeof(EventInfo));
  1124. EventInfo* newEvent = events;
  1125. EventIndex end = klass->event_count;
  1126. for (EventIndex eventIndex = 0; eventIndex < end; ++eventIndex)
  1127. {
  1128. Il2CppMetadataEventInfo eventInfo = MetadataCache::GetEventInfo(klass, eventIndex);
  1129. newEvent->eventType = eventInfo.type;
  1130. newEvent->name = eventInfo.name;
  1131. newEvent->parent = klass;
  1132. newEvent->add = eventInfo.add;
  1133. newEvent->remove = eventInfo.remove;
  1134. newEvent->raise = eventInfo.raise;
  1135. newEvent->token = eventInfo.token;
  1136. newEvent++;
  1137. }
  1138. klass->events = events;
  1139. }
  1140. }
  1141. void Class::SetupEvents(Il2CppClass *klass)
  1142. {
  1143. if (!klass->events && klass->event_count)
  1144. {
  1145. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  1146. SetupEventsLocked(klass, lock);
  1147. }
  1148. }
  1149. static void SetupPropertiesLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  1150. {
  1151. if (klass->generic_class)
  1152. {
  1153. Class::InitLocked(GenericClass::GetTypeDefinition(klass->generic_class), lock);
  1154. GenericClass::SetupProperties(klass);
  1155. }
  1156. else if (klass->property_count != 0)
  1157. {
  1158. // we need methods initialized since we reference them via index below
  1159. SetupMethodsLocked(klass, lock);
  1160. PropertyInfo* properties = (PropertyInfo*)MetadataCalloc(klass->property_count, sizeof(PropertyInfo));
  1161. PropertyInfo* newProperty = properties;
  1162. PropertyIndex end = klass->property_count;
  1163. for (PropertyIndex propertyIndex = 0; propertyIndex < end; ++propertyIndex)
  1164. {
  1165. Il2CppMetadataPropertyInfo propertyInfo = MetadataCache::GetPropertyInfo(klass, propertyIndex);
  1166. newProperty->name = propertyInfo.name;
  1167. newProperty->parent = klass;
  1168. newProperty->get = propertyInfo.get;
  1169. newProperty->set = propertyInfo.set;
  1170. newProperty->attrs = propertyInfo.attrs;
  1171. newProperty->token = propertyInfo.token;
  1172. newProperty++;
  1173. }
  1174. klass->properties = properties;
  1175. }
  1176. }
  1177. void Class::SetupProperties(Il2CppClass *klass)
  1178. {
  1179. if (!klass->properties && klass->property_count)
  1180. {
  1181. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  1182. SetupPropertiesLocked(klass, lock);
  1183. }
  1184. }
  1185. static void SetupTypeHierarchyLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  1186. {
  1187. if (klass->typeHierarchy != NULL)
  1188. return;
  1189. if (klass->parent && !klass->parent->typeHierarchy)
  1190. SetupTypeHierarchyLocked(klass->parent, lock);
  1191. if (klass->parent)
  1192. klass->typeHierarchyDepth = klass->parent->typeHierarchyDepth + 1;
  1193. else
  1194. klass->typeHierarchyDepth = 1;
  1195. klass->typeHierarchy = (Il2CppClass**)MetadataCalloc(klass->typeHierarchyDepth, sizeof(Il2CppClass*));
  1196. if (klass->parent)
  1197. {
  1198. klass->typeHierarchy[klass->typeHierarchyDepth - 1] = klass;
  1199. memcpy(klass->typeHierarchy, klass->parent->typeHierarchy, klass->parent->typeHierarchyDepth * sizeof(void*));
  1200. }
  1201. else
  1202. {
  1203. klass->typeHierarchy[0] = klass;
  1204. }
  1205. }
  1206. void Class::SetupTypeHierarchy(Il2CppClass *klass)
  1207. {
  1208. if (klass->typeHierarchy == NULL)
  1209. {
  1210. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  1211. SetupTypeHierarchyLocked(klass, lock);
  1212. }
  1213. }
  1214. void Class::SetupInterfaces(Il2CppClass *klass)
  1215. {
  1216. if (klass->implementedInterfaces == NULL)
  1217. {
  1218. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  1219. SetupInterfacesLocked(klass, lock);
  1220. }
  1221. }
  1222. static void SetupBlittableForGenericInstanceType(Il2CppClass *klass)
  1223. {
  1224. for (int i = 0; i < klass->field_count; i++)
  1225. if (Field::IsInstance(&klass->fields[i]) && !Class::FromIl2CppType(klass->fields[i].type)->is_blittable)
  1226. return;
  1227. klass->is_blittable = true;
  1228. }
  1229. bool Class::InitLocked(Il2CppClass *klass, const il2cpp::os::FastAutoLock& lock)
  1230. {
  1231. if (klass->initialized)
  1232. return true;
  1233. if (klass->init_pending)
  1234. return true;
  1235. if (klass->initializationExceptionGCHandle)
  1236. return false;
  1237. if (klass->generic_class && (klass->flags & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
  1238. {
  1239. std::string message;
  1240. message += "Could not load type '";
  1241. message += klass->namespaze;
  1242. message += ":";
  1243. message += klass->name;
  1244. message += "' because generic types cannot have explicit layout.";
  1245. Class::SetClassInitializationError(klass, il2cpp::vm::Exception::GetTypeLoadException(message.c_str()));
  1246. return false;
  1247. }
  1248. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Class::Init, "Audit and compare to mono version");
  1249. klass->init_pending = true;
  1250. klass->genericRecursionDepth++;
  1251. if (klass->generic_class)
  1252. {
  1253. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(klass->generic_class);
  1254. InitLocked(genericTypeDefinition, lock);
  1255. klass->is_blittable = genericTypeDefinition->is_blittable;
  1256. }
  1257. if (klass->byval_arg.type == IL2CPP_TYPE_ARRAY || klass->byval_arg.type == IL2CPP_TYPE_SZARRAY)
  1258. {
  1259. Il2CppClass *element_class = klass->element_class;
  1260. if (!element_class->initialized)
  1261. InitLocked(element_class, lock);
  1262. }
  1263. SetupInterfacesLocked(klass, lock);
  1264. if (klass->parent && !klass->parent->initialized)
  1265. InitLocked(klass->parent, lock);
  1266. SetupMethodsLocked(klass, lock);
  1267. SetupTypeHierarchyLocked(klass, lock);
  1268. SetupVTable(klass, lock);
  1269. if (!klass->size_inited)
  1270. SetupFieldsLocked(klass, lock);
  1271. // If this is generic instance class and it's generic type definition is not blittable,
  1272. // check each field to see if all fields are blittable, in which case the generic instance is blittable
  1273. if (!klass->is_blittable && klass->generic_class)
  1274. SetupBlittableForGenericInstanceType(klass);
  1275. if (klass->initializationExceptionGCHandle)
  1276. {
  1277. klass->init_pending = false;
  1278. return false;
  1279. }
  1280. SetupEventsLocked(klass, lock);
  1281. SetupPropertiesLocked(klass, lock);
  1282. SetupNestedTypesLocked(klass, lock);
  1283. if (klass == il2cpp_defaults.object_class)
  1284. {
  1285. for (uint16_t slot = 0; slot < klass->vtable_count; slot++)
  1286. {
  1287. const MethodInfo* vmethod = klass->vtable[slot].method;
  1288. if (!strcmp(vmethod->name, "GetHashCode"))
  1289. s_GetHashCodeSlot = slot;
  1290. else if (!strcmp(vmethod->name, "Finalize"))
  1291. s_FinalizerSlot = slot;
  1292. }
  1293. #if !IL2CPP_TINY
  1294. IL2CPP_ASSERT(s_FinalizerSlot > 0);
  1295. IL2CPP_ASSERT(s_GetHashCodeSlot > 0);
  1296. #endif
  1297. }
  1298. bool canBeInstantiated = !Class::IsGeneric(klass) && !il2cpp::metadata::GenericMetadata::ContainsGenericParameters(klass);
  1299. if (canBeInstantiated)
  1300. {
  1301. SetupGCDescriptor(klass, lock);
  1302. if (klass->generic_class)
  1303. {
  1304. // This should be kept last. InflateRGCTXLocked may need initialized data from the class we are initializing
  1305. if (klass->genericRecursionDepth < il2cpp::metadata::GenericMetadata::GetMaximumRuntimeGenericDepth() || il2cpp::vm::Runtime::IsLazyRGCTXInflationEnabled())
  1306. klass->rgctx_data = il2cpp::metadata::GenericMetadata::InflateRGCTXLocked(klass->image, klass->token, &klass->generic_class->context, lock);
  1307. }
  1308. }
  1309. Class::PublishInitialized(klass);
  1310. ++il2cpp_runtime_stats.initialized_class_count;
  1311. return true;
  1312. }
  1313. bool Class::InitSizeAndFieldLayoutLocked(Il2CppClass* klass, const il2cpp::os::FastAutoLock& lock)
  1314. {
  1315. if (klass->size_inited)
  1316. return true;
  1317. if (klass->size_init_pending)
  1318. return true;
  1319. if (klass->initializationExceptionGCHandle)
  1320. return false;
  1321. if (!Class::IsGeneric(klass))
  1322. SetupFieldsLocked(klass, lock);
  1323. return true;
  1324. }
  1325. void Class::Init(Il2CppClass *klass)
  1326. {
  1327. IL2CPP_ASSERT(klass);
  1328. if (!klass->initialized)
  1329. {
  1330. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  1331. IL2CPP_ASSERT(!klass->init_pending);
  1332. InitLocked(klass, lock);
  1333. }
  1334. }
  1335. void Class::SetClassInitializationError(Il2CppClass *klass, Il2CppException* error)
  1336. {
  1337. klass->initializationExceptionGCHandle = gc::GCHandle::New(error, false);
  1338. PublishInitialized(klass);
  1339. }
  1340. void Class::PublishInitialized(Il2CppClass *klass)
  1341. {
  1342. // Update the initialized flags last, so that other threads can't see the class as initialized
  1343. // until after everything else is set up.
  1344. il2cpp::os::Atomic::FullMemoryBarrier();
  1345. klass->initialized = true;
  1346. klass->initialized_and_no_error = !klass->initializationExceptionGCHandle;
  1347. klass->init_pending = false;
  1348. }
  1349. Il2CppClass* Class::FromName(const Il2CppImage* image, const char* namespaze, const char *name)
  1350. {
  1351. return Image::ClassFromName(image, namespaze, name);
  1352. }
  1353. Il2CppClass* Class::GetArrayClass(Il2CppClass *element_class, uint32_t rank)
  1354. {
  1355. return GetBoundedArrayClass(element_class, rank, false);
  1356. }
  1357. Il2CppClass* Class::GetBoundedArrayClass(Il2CppClass *eclass, uint32_t rank, bool bounded)
  1358. {
  1359. return il2cpp::metadata::ArrayMetadata::GetBoundedArrayClass(eclass, rank, bounded);
  1360. }
  1361. Il2CppClass* Class::GetInflatedGenericInstanceClass(Il2CppClass* klass, const Il2CppType** types, uint32_t typeCount)
  1362. {
  1363. return GetInflatedGenericInstanceClass(klass, MetadataCache::GetGenericInst(types, typeCount));
  1364. }
  1365. Il2CppClass* Class::GetInflatedGenericInstanceClass(Il2CppClass* klass, const Il2CppGenericInst* genericInst)
  1366. {
  1367. IL2CPP_ASSERT(Class::IsGeneric(klass));
  1368. Il2CppGenericClass* gclass = il2cpp::metadata::GenericMetadata::GetGenericClass(klass, genericInst);
  1369. return GenericClass::GetClass(gclass);
  1370. }
  1371. Il2CppClass* Class::InflateGenericClass(Il2CppClass* klass, Il2CppGenericContext *context)
  1372. {
  1373. const Il2CppType* inflated = InflateGenericType(&klass->byval_arg, context);
  1374. return FromIl2CppType(inflated);
  1375. }
  1376. const Il2CppType* Class::InflateGenericType(const Il2CppType* type, Il2CppGenericContext* context)
  1377. {
  1378. return il2cpp::metadata::GenericMetadata::InflateIfNeeded(type, context, true);
  1379. }
  1380. bool Class::HasDefaultConstructor(Il2CppClass* klass)
  1381. {
  1382. const char ctorName[] = ".ctor";
  1383. void* iter = NULL;
  1384. while (const MethodInfo* method = Class::GetMethods(klass, &iter))
  1385. {
  1386. if (strncmp(method->name, ctorName, utils::StringUtils::LiteralLength(ctorName)) == 0 && method->parameters_count == 0)
  1387. return true;
  1388. }
  1389. return false;
  1390. }
  1391. int Class::GetArrayElementSize(const Il2CppClass *klass)
  1392. {
  1393. const Il2CppType *type = &klass->byval_arg;
  1394. handle_enum:
  1395. switch (type->type)
  1396. {
  1397. case IL2CPP_TYPE_I1:
  1398. case IL2CPP_TYPE_U1:
  1399. case IL2CPP_TYPE_BOOLEAN:
  1400. return 1;
  1401. case IL2CPP_TYPE_I2:
  1402. case IL2CPP_TYPE_U2:
  1403. case IL2CPP_TYPE_CHAR:
  1404. return 2;
  1405. case IL2CPP_TYPE_I4:
  1406. case IL2CPP_TYPE_U4:
  1407. case IL2CPP_TYPE_R4:
  1408. return 4;
  1409. case IL2CPP_TYPE_I:
  1410. case IL2CPP_TYPE_U:
  1411. case IL2CPP_TYPE_PTR:
  1412. case IL2CPP_TYPE_CLASS:
  1413. case IL2CPP_TYPE_STRING:
  1414. case IL2CPP_TYPE_OBJECT:
  1415. case IL2CPP_TYPE_SZARRAY:
  1416. case IL2CPP_TYPE_ARRAY:
  1417. case IL2CPP_TYPE_VAR:
  1418. case IL2CPP_TYPE_MVAR:
  1419. return sizeof(Il2CppObject*);
  1420. case IL2CPP_TYPE_I8:
  1421. case IL2CPP_TYPE_U8:
  1422. case IL2CPP_TYPE_R8:
  1423. return 8;
  1424. case IL2CPP_TYPE_VALUETYPE:
  1425. if (Type::IsEnum(type))
  1426. {
  1427. type = Class::GetEnumBaseType(Type::GetClass(type));
  1428. klass = klass->element_class;
  1429. goto handle_enum;
  1430. }
  1431. return Class::GetInstanceSize(klass) - sizeof(Il2CppObject);
  1432. case IL2CPP_TYPE_GENERICINST:
  1433. type = &GenericClass::GetTypeDefinition(type->data.generic_class)->byval_arg;
  1434. goto handle_enum;
  1435. case IL2CPP_TYPE_VOID:
  1436. return 0;
  1437. default:
  1438. // g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
  1439. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Class::GetArrayElementSize, "Should throw a nice error");
  1440. }
  1441. return -1;
  1442. }
  1443. const Il2CppType* Class::GetType(Il2CppClass *klass, const TypeNameParseInfo &info)
  1444. {
  1445. // Attempt to resolve a generic type definition.
  1446. if (Class::IsGeneric(klass))
  1447. klass = ResolveGenericInstanceType(klass, info, kTypeSearchFlagNone);
  1448. bool bounded = false;
  1449. std::vector<int32_t>::const_iterator it = info.modifiers().begin();
  1450. if (info.is_bounded())
  1451. {
  1452. bounded = false;
  1453. }
  1454. while (it != info.modifiers().end())
  1455. {
  1456. if (*it == 0)
  1457. {
  1458. // byref is always the last modifier, so we can return here.
  1459. return &klass->this_arg;
  1460. }
  1461. if (*it == -1)
  1462. {
  1463. klass = Class::GetPtrClass(klass);
  1464. }
  1465. else if (*it == -2)
  1466. {
  1467. bounded = true;
  1468. }
  1469. else
  1470. {
  1471. klass = Class::GetBoundedArrayClass(klass, *it, bounded);
  1472. }
  1473. ++it;
  1474. }
  1475. if (klass == NULL)
  1476. return NULL;
  1477. return &klass->byval_arg;
  1478. }
  1479. bool Class::HasAttribute(Il2CppClass *klass, Il2CppClass *attr_class)
  1480. {
  1481. return Reflection::HasAttribute(klass, attr_class);
  1482. }
  1483. const Il2CppImage* Class::GetImage(Il2CppClass *klass)
  1484. {
  1485. return klass->image;
  1486. }
  1487. Il2CppMetadataGenericContainerHandle Class::GetGenericContainer(Il2CppClass *klass)
  1488. {
  1489. return klass->genericContainerHandle;
  1490. }
  1491. const MethodInfo* Class::GetCCtor(Il2CppClass *klass)
  1492. {
  1493. if (!klass->has_cctor)
  1494. return NULL;
  1495. return GetMethodFromNameFlags(klass, ".cctor", IgnoreNumberOfArguments, METHOD_ATTRIBUTE_SPECIAL_NAME);
  1496. }
  1497. const char* Class::GetFieldDefaultValue(const FieldInfo *field, const Il2CppType** type)
  1498. {
  1499. return reinterpret_cast<const char*>(MetadataCache::GetFieldDefaultValue(field, type));
  1500. }
  1501. int Class::GetFieldMarshaledSize(const FieldInfo *field)
  1502. {
  1503. int marshaledFieldSize = MetadataCache::GetFieldMarshaledSizeForField(field);
  1504. if (marshaledFieldSize != -1)
  1505. return marshaledFieldSize;
  1506. if (field->type->type == IL2CPP_TYPE_BOOLEAN)
  1507. return 4;
  1508. if (field->type->type == IL2CPP_TYPE_CHAR)
  1509. return 1;
  1510. size_t size = il2cpp::metadata::FieldLayout::GetTypeSizeAndAlignment(field->type).size;
  1511. IL2CPP_ASSERT(size < static_cast<size_t>(std::numeric_limits<int>::max()));
  1512. return static_cast<int>(size);
  1513. }
  1514. int Class::GetFieldMarshaledAlignment(const FieldInfo *field)
  1515. {
  1516. if (MetadataCache::GetFieldMarshaledSizeForField(field) == 0)
  1517. {
  1518. // We have no marshaled field size, so ignore marshaled alignment for this field.
  1519. return 0;
  1520. }
  1521. if (field->type->type == IL2CPP_TYPE_BOOLEAN)
  1522. return 4;
  1523. if (field->type->type == IL2CPP_TYPE_CHAR)
  1524. return 1;
  1525. uint8_t alignment = il2cpp::metadata::FieldLayout::GetTypeSizeAndAlignment(field->type).alignment;
  1526. return static_cast<int>(alignment);
  1527. }
  1528. Il2CppClass* Class::GetPtrClass(const Il2CppType* type)
  1529. {
  1530. return GetPtrClass(Class::FromIl2CppType(type));
  1531. }
  1532. Il2CppClass* Class::GetPtrClass(Il2CppClass* elementClass)
  1533. {
  1534. // Check if the pointer class was created before taking the g_MetadataLock
  1535. Il2CppClass* pointerClass = MetadataCache::GetPointerType(elementClass);
  1536. if (pointerClass)
  1537. return pointerClass;
  1538. il2cpp::os::FastAutoLock lock(&g_MetadataLock);
  1539. // Check if the pointer class was created while we were waiting for the g_MetadataLock
  1540. pointerClass = MetadataCache::GetPointerType(elementClass);
  1541. if (pointerClass)
  1542. return pointerClass;
  1543. pointerClass = (Il2CppClass*)MetadataCalloc(1, sizeof(Il2CppClass));
  1544. pointerClass->klass = pointerClass;
  1545. pointerClass->namespaze = elementClass->namespaze;
  1546. pointerClass->name = il2cpp::utils::StringUtils::StringDuplicate(il2cpp::utils::StringUtils::Printf("%s*", elementClass->name).c_str());
  1547. pointerClass->image = elementClass->image;
  1548. pointerClass->flags = TYPE_ATTRIBUTE_CLASS | (elementClass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
  1549. pointerClass->instance_size = sizeof(void*);
  1550. pointerClass->stack_slot_size = sizeof(void*);
  1551. pointerClass->size_inited = true;
  1552. pointerClass->this_arg.type = pointerClass->byval_arg.type = IL2CPP_TYPE_PTR;
  1553. pointerClass->this_arg.data.type = pointerClass->byval_arg.data.type = &elementClass->byval_arg;
  1554. pointerClass->this_arg.byref = true;
  1555. pointerClass->parent = NULL;
  1556. pointerClass->typeHierarchyDepth = 1;
  1557. pointerClass->castClass = pointerClass->element_class = elementClass;
  1558. PublishInitialized(pointerClass);
  1559. MetadataCache::AddPointerTypeLocked(elementClass, pointerClass, lock);
  1560. return pointerClass;
  1561. }
  1562. bool Class::HasReferences(Il2CppClass *klass)
  1563. {
  1564. if (!klass->size_inited)
  1565. {
  1566. SetupFields(klass);
  1567. if (!klass->size_inited)
  1568. {
  1569. abort();
  1570. /* Be conservative */
  1571. return true;
  1572. }
  1573. }
  1574. return klass->has_references;
  1575. }
  1576. const il2cpp::utils::dynamic_array<Il2CppClass*>& Class::GetStaticFieldData()
  1577. {
  1578. return s_staticFieldData;
  1579. }
  1580. const size_t kWordSize = (8 * sizeof(size_t));
  1581. static inline void set_bit(size_t* bitmap, size_t index)
  1582. {
  1583. bitmap[index / kWordSize] |= (size_t)1 << (index % kWordSize);
  1584. }
  1585. size_t Class::GetBitmapSize(const Il2CppClass* klass)
  1586. {
  1587. size_t maxBits = klass->instance_size / sizeof(void*);
  1588. size_t maxWords = 1 + (maxBits / sizeof(size_t));
  1589. return sizeof(size_t) * maxWords;
  1590. }
  1591. void Class::GetBitmap(Il2CppClass* klass, size_t* bitmap, size_t& maxSetBit)
  1592. {
  1593. Class::Init(klass);
  1594. return il2cpp::vm::GetBitmapNoInit(klass, bitmap, maxSetBit, 0, NULL);
  1595. }
  1596. const char *Class::GetAssemblyName(const Il2CppClass *klass)
  1597. {
  1598. return klass->image->name;
  1599. }
  1600. const char *Class::GetAssemblyNameNoExtension(const Il2CppClass *klass)
  1601. {
  1602. return klass->image->nameNoExt;
  1603. }
  1604. void GetBitmapNoInit(Il2CppClass* klass, size_t* bitmap, size_t& maxSetBit, size_t parentOffset, const il2cpp::os::FastAutoLock* lockPtr)
  1605. {
  1606. Il2CppClass* currentClass = klass;
  1607. while (currentClass)
  1608. {
  1609. for (uint16_t index = 0; index < currentClass->field_count; index++)
  1610. {
  1611. FieldInfo* field = currentClass->fields + index;
  1612. if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
  1613. continue;
  1614. IL2CPP_ASSERT(!field->type->byref);
  1615. size_t offset = parentOffset + field->offset;
  1616. const Il2CppType* type = Type::GetUnderlyingType(field->type);
  1617. switch (type->type)
  1618. {
  1619. case IL2CPP_TYPE_I1:
  1620. case IL2CPP_TYPE_U1:
  1621. case IL2CPP_TYPE_BOOLEAN:
  1622. case IL2CPP_TYPE_I2:
  1623. case IL2CPP_TYPE_U2:
  1624. case IL2CPP_TYPE_CHAR:
  1625. case IL2CPP_TYPE_I4:
  1626. case IL2CPP_TYPE_U4:
  1627. case IL2CPP_TYPE_I8:
  1628. case IL2CPP_TYPE_U8:
  1629. case IL2CPP_TYPE_I:
  1630. case IL2CPP_TYPE_U:
  1631. case IL2CPP_TYPE_R4:
  1632. case IL2CPP_TYPE_R8:
  1633. case IL2CPP_TYPE_PTR:
  1634. case IL2CPP_TYPE_FNPTR:
  1635. break;
  1636. case IL2CPP_TYPE_STRING:
  1637. case IL2CPP_TYPE_SZARRAY:
  1638. case IL2CPP_TYPE_CLASS:
  1639. case IL2CPP_TYPE_OBJECT:
  1640. case IL2CPP_TYPE_ARRAY:
  1641. case IL2CPP_TYPE_VAR:
  1642. case IL2CPP_TYPE_MVAR:
  1643. IL2CPP_ASSERT(0 == (offset % sizeof(void*)));
  1644. set_bit(bitmap, offset / sizeof(void*));
  1645. maxSetBit = std::max(maxSetBit, offset / sizeof(void*));
  1646. break;
  1647. case IL2CPP_TYPE_GENERICINST:
  1648. if (!Type::GenericInstIsValuetype(type))
  1649. {
  1650. IL2CPP_ASSERT(0 == (offset % sizeof(void*)));
  1651. set_bit(bitmap, offset / sizeof(void*));
  1652. maxSetBit = std::max(maxSetBit, offset / sizeof(void*));
  1653. break;
  1654. }
  1655. else
  1656. {
  1657. /* fall through */
  1658. }
  1659. case IL2CPP_TYPE_VALUETYPE:
  1660. {
  1661. Il2CppClass* fieldClass = Class::FromIl2CppType(field->type);
  1662. if (lockPtr == NULL)
  1663. Class::Init(fieldClass);
  1664. else
  1665. Class::InitLocked(fieldClass, *lockPtr);
  1666. if (fieldClass->has_references)
  1667. GetBitmapNoInit(fieldClass, bitmap, maxSetBit, offset - sizeof(Il2CppObject) /* nested field offset includes padding for boxed structure. Remove for struct fields */, lockPtr);
  1668. break;
  1669. }
  1670. default:
  1671. IL2CPP_NOT_IMPLEMENTED(Class::GetClassBitmap);
  1672. break;
  1673. }
  1674. }
  1675. currentClass = currentClass->parent;
  1676. }
  1677. }
  1678. void SetupGCDescriptor(Il2CppClass* klass, const il2cpp::os::FastAutoLock& lock)
  1679. {
  1680. const size_t kMaxAllocaSize = 1024;
  1681. size_t bitmapSize = Class::GetBitmapSize(klass);
  1682. size_t* bitmap = NULL;
  1683. std::vector<size_t> buffer(0);
  1684. if (bitmapSize > kMaxAllocaSize)
  1685. {
  1686. buffer.resize(bitmapSize / sizeof(size_t));
  1687. bitmap = (size_t*)&buffer[0];
  1688. }
  1689. else
  1690. {
  1691. bitmap = (size_t*)alloca(bitmapSize);
  1692. }
  1693. memset(bitmap, 0, bitmapSize);
  1694. size_t maxSetBit = 0;
  1695. GetBitmapNoInit(klass, bitmap, maxSetBit, 0, &lock);
  1696. if (klass == il2cpp_defaults.string_class)
  1697. klass->gc_desc = il2cpp::gc::GarbageCollector::MakeDescriptorForString();
  1698. else if (klass->rank)
  1699. klass->gc_desc = il2cpp::gc::GarbageCollector::MakeDescriptorForArray();
  1700. else
  1701. klass->gc_desc = il2cpp::gc::GarbageCollector::MakeDescriptorForObject(bitmap, (int)maxSetBit + 1);
  1702. }
  1703. #define CHECK_IF_NULL(v) \
  1704. if ( (v) == NULL && (searchFlags & kTypeSearchFlagThrowOnError) != 0 ) \
  1705. Exception::Raise (Exception::GetTypeLoadException (info)); \
  1706. if ( (v) == NULL ) \
  1707. return NULL;
  1708. static Il2CppClass * resolve_generic_instance_internal(const il2cpp::vm::TypeNameParseInfo &info, Il2CppClass *generic_class, const Il2CppType** generic_arguments, TypeSearchFlags searchFlags)
  1709. {
  1710. Il2CppClass *klass = NULL;
  1711. Il2CppMetadataGenericContainerHandle containerHandle = Class::GetGenericContainer(generic_class);
  1712. uint32_t type_argc = MetadataCache::GetGenericContainerCount(containerHandle);
  1713. if (type_argc != info.type_arguments().size())
  1714. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetArgumentException("name", "The number of generic arguments provided doesn't equal the arity of the generic type definition."));
  1715. if (info.assembly_name().name.empty())
  1716. {
  1717. const Il2CppImage* image = Image::GetExecutingImage();
  1718. klass = MetadataCache::GetGenericInstanceType(generic_class, generic_arguments, type_argc);
  1719. if (klass == NULL && image != Image::GetCorlib())
  1720. {
  1721. // Try mscorlib
  1722. image = (Il2CppImage*)Image::GetCorlib();
  1723. klass = MetadataCache::GetGenericInstanceType(generic_class, generic_arguments, type_argc);
  1724. }
  1725. }
  1726. else
  1727. {
  1728. const Il2CppAssembly *assembly = Assembly::Load(info.assembly_name().name.c_str());
  1729. CHECK_IF_NULL(assembly);
  1730. Il2CppImage *image = (Il2CppImage*)Assembly::GetImage(assembly);
  1731. CHECK_IF_NULL(image);
  1732. klass = MetadataCache::GetGenericInstanceType(generic_class, generic_arguments, type_argc);
  1733. }
  1734. return klass;
  1735. }
  1736. static Il2CppClass* ResolveGenericInstanceType(Il2CppClass* klass, const TypeNameParseInfo& info, TypeSearchFlags searchFlags)
  1737. {
  1738. if (info.has_generic_arguments())
  1739. {
  1740. const Il2CppType** generic_arguments = (const Il2CppType**)alloca(info.type_arguments().size() * sizeof(const Il2CppType*));
  1741. int i = 0;
  1742. std::vector<TypeNameParseInfo>::const_iterator it = info.type_arguments().begin();
  1743. while (it != info.type_arguments().end())
  1744. {
  1745. const Il2CppType * generic_argument = Class::il2cpp_type_from_type_info(*it, searchFlags);
  1746. CHECK_IF_NULL(generic_argument);
  1747. generic_arguments[i] = generic_argument;
  1748. ++it;
  1749. ++i;
  1750. }
  1751. klass = resolve_generic_instance_internal(info, klass, generic_arguments, searchFlags);
  1752. CHECK_IF_NULL(klass);
  1753. }
  1754. if (klass != NULL)
  1755. Class::Init(klass);
  1756. return klass;
  1757. }
  1758. static Il2CppClass* resolve_parse_info_internal(const TypeNameParseInfo& info, TypeSearchFlags searchFlags)
  1759. {
  1760. Il2CppClass *klass = NULL;
  1761. if (info.assembly_name().name.empty())
  1762. {
  1763. const Il2CppImage* image;
  1764. bool dontUseExecutingImage = searchFlags & kTypeSearchFlagDontUseExecutingImage;
  1765. if (dontUseExecutingImage)
  1766. {
  1767. image = Image::GetCorlib();
  1768. }
  1769. else
  1770. {
  1771. image = Image::GetExecutingImage();
  1772. }
  1773. klass = Image::FromTypeNameParseInfo(image, info, searchFlags & kTypeSearchFlagIgnoreCase);
  1774. // First, try mscorlib
  1775. if (klass == NULL && image != Image::GetCorlib())
  1776. klass = Image::FromTypeNameParseInfo(Image::GetCorlib(), info, searchFlags & kTypeSearchFlagIgnoreCase);
  1777. // If we did not find it, now look in all loaded assemblies, except the ones we have tried already.
  1778. if (klass == NULL && !dontUseExecutingImage)
  1779. {
  1780. for (auto assembly : *Assembly::GetAllAssemblies())
  1781. {
  1782. auto currentImage = Assembly::GetImage(assembly);
  1783. if (currentImage != Image::GetCorlib() && currentImage != image)
  1784. {
  1785. klass = Image::FromTypeNameParseInfo(currentImage, info, searchFlags & kTypeSearchFlagIgnoreCase);
  1786. if (klass != NULL)
  1787. break;
  1788. }
  1789. }
  1790. }
  1791. }
  1792. else
  1793. {
  1794. const Il2CppAssembly *assembly = Assembly::Load(info.assembly_name().name.c_str());
  1795. CHECK_IF_NULL(assembly);
  1796. Il2CppImage *image = (Il2CppImage*)Assembly::GetImage(assembly);
  1797. CHECK_IF_NULL(image);
  1798. klass = Image::FromTypeNameParseInfo(image, info, searchFlags & kTypeSearchFlagIgnoreCase);
  1799. }
  1800. return klass;
  1801. }
  1802. const Il2CppType* Class::il2cpp_type_from_type_info(const TypeNameParseInfo& info, TypeSearchFlags searchFlags)
  1803. {
  1804. Il2CppClass *klass = resolve_parse_info_internal(info, searchFlags);
  1805. CHECK_IF_NULL(klass);
  1806. klass = ResolveGenericInstanceType(klass, info, searchFlags);
  1807. CHECK_IF_NULL(klass);
  1808. const Il2CppType *type = Class::GetType(klass, info);
  1809. CHECK_IF_NULL(type);
  1810. return type;
  1811. }
  1812. Il2CppClass* Class::GetDeclaringType(Il2CppClass* klass)
  1813. {
  1814. return klass->declaringType;
  1815. }
  1816. const MethodInfo* Class::GetVirtualMethod(Il2CppClass *klass, const MethodInfo *virtualMethod)
  1817. {
  1818. IL2CPP_ASSERT(klass->is_vtable_initialized);
  1819. if ((virtualMethod->flags & METHOD_ATTRIBUTE_FINAL) || !(virtualMethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
  1820. return virtualMethod;
  1821. Il2CppClass* methodDeclaringType = virtualMethod->klass;
  1822. const MethodInfo* vtableSlotMethod;
  1823. if (Class::IsInterface(methodDeclaringType))
  1824. {
  1825. const VirtualInvokeData* invokeData = ClassInlines::GetInterfaceInvokeDataFromVTable(klass, methodDeclaringType, virtualMethod->slot);
  1826. if (invokeData == NULL)
  1827. return NULL;
  1828. vtableSlotMethod = invokeData->method;
  1829. }
  1830. else
  1831. {
  1832. IL2CPP_ASSERT(virtualMethod->slot < klass->vtable_count);
  1833. vtableSlotMethod = klass->vtable[virtualMethod->slot].method;
  1834. }
  1835. if (Method::IsGenericInstanceMethod(virtualMethod))
  1836. return il2cpp::metadata::GenericMethod::GetGenericVirtualMethod(vtableSlotMethod, virtualMethod);
  1837. return vtableSlotMethod;
  1838. }
  1839. static bool is_generic_argument(Il2CppType* type)
  1840. {
  1841. return type->type == IL2CPP_TYPE_VAR || type->type == IL2CPP_TYPE_VAR;
  1842. }
  1843. Il2CppClass* Class::GenericParamGetBaseType(Il2CppClass* klass)
  1844. {
  1845. Il2CppType* type = &klass->byval_arg;
  1846. IL2CPP_ASSERT(is_generic_argument(type));
  1847. Il2CppMetadataGenericParameterHandle gparam = type->data.genericParameterHandle;
  1848. Il2CppClass** constraints = GetOrCreateMonoGenericParameterInfo(gparam)->constraints;
  1849. Il2CppClass* base_class = il2cpp_defaults.object_class;
  1850. if (constraints)
  1851. {
  1852. for (int i = 0; constraints[i]; ++i)
  1853. {
  1854. Il2CppClass* constraint = constraints[i];
  1855. if (IsInterface(constraint))
  1856. continue;
  1857. Il2CppType* constraint_type = &constraint->byval_arg;
  1858. if (is_generic_argument(constraint_type))
  1859. {
  1860. Il2CppMetadataGenericParameterHandle constraint_param = constraint_type->data.genericParameterHandle;
  1861. Il2CppGenericParameterInfo constraint_info = MetadataCache::GetGenericParameterInfo(constraint_param);
  1862. if ((constraint_info.flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) == 0 &&
  1863. (constraint_info.flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_NOT_NULLABLE_VALUE_TYPE_CONSTRAINT) == 0)
  1864. continue;
  1865. }
  1866. base_class = constraint;
  1867. }
  1868. }
  1869. if (base_class == il2cpp_defaults.object_class)
  1870. {
  1871. Il2CppGenericParameterInfo gparamInfo = MetadataCache::GetGenericParameterInfo(gparam);
  1872. if ((gparamInfo.flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_NOT_NULLABLE_VALUE_TYPE_CONSTRAINT) != 0)
  1873. base_class = il2cpp_defaults.value_type_class;
  1874. }
  1875. return base_class;
  1876. }
  1877. MonoGenericParameterInfo* Class::GetOrCreateMonoGenericParameterInfo(Il2CppMetadataGenericParameterHandle parameterHandle)
  1878. {
  1879. Il2CppGenericParameterInfo paramInfo = il2cpp::vm::MetadataCache::GetGenericParameterInfo(parameterHandle);
  1880. MonoGenericParameterInfo *monoParam = (MonoGenericParameterInfo*)il2cpp::vm::Reflection::GetMonoGenericParameterInfo(parameterHandle);
  1881. if (monoParam == NULL)
  1882. {
  1883. monoParam = (MonoGenericParameterInfo*)IL2CPP_MALLOC(sizeof(MonoGenericParameterInfo));
  1884. monoParam->flags = paramInfo.flags;
  1885. monoParam->token = paramInfo.num;
  1886. monoParam->name = paramInfo.name;
  1887. monoParam->pklass = NULL;
  1888. if (paramInfo.containerHandle)
  1889. monoParam->pklass = il2cpp::vm::MetadataCache::GetContainerDeclaringType(paramInfo.containerHandle);
  1890. int16_t constraintsCount = il2cpp::vm::MetadataCache::GetGenericConstraintCount(parameterHandle);
  1891. monoParam->constraints = (Il2CppClass**)IL2CPP_MALLOC(sizeof(Il2CppClass*) * (constraintsCount + 1));
  1892. for (int i = 0; i < constraintsCount; ++i)
  1893. {
  1894. const Il2CppType *constraintType = il2cpp::vm::MetadataCache::GetGenericParameterConstraintFromIndex(parameterHandle, i);
  1895. monoParam->constraints[i] = il2cpp::vm::Class::FromIl2CppType(constraintType);
  1896. }
  1897. monoParam->constraints[constraintsCount] = NULL;
  1898. il2cpp::vm::Reflection::SetMonoGenericParameterInfo(parameterHandle, monoParam);
  1899. }
  1900. return monoParam;
  1901. }
  1902. } /* namespace vm */
  1903. } /* namespace il2cpp */