GenericMetadata.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/Runtime.h"
  8. #include "vm/Type.h"
  9. #include "metadata/GenericMetadata.h"
  10. #include "metadata/GenericMethod.h"
  11. #include "metadata/Il2CppGenericClassHash.h"
  12. #include "metadata/Il2CppGenericClassCompare.h"
  13. #include "metadata/Il2CppGenericInstCompare.h"
  14. #include "metadata/Il2CppGenericInstHash.h"
  15. #include "metadata/Il2CppTypeCompare.h"
  16. #include "metadata/Il2CppTypeHash.h"
  17. #include "utils/Memory.h"
  18. #include "utils/Il2CppHashMap.h"
  19. #include "utils/Il2CppHashSet.h"
  20. #include "utils/StringUtils.h"
  21. #include "vm/MetadataAlloc.h"
  22. #include "vm/MetadataCache.h"
  23. #include "il2cpp-class-internals.h"
  24. #include "il2cpp-tabledefs.h"
  25. #include <vector>
  26. #include "Baselib.h"
  27. #include "Cpp/ReentrantLock.h"
  28. using namespace il2cpp::vm;
  29. using il2cpp::metadata::GenericMethod;
  30. using il2cpp::os::FastAutoLock;
  31. using il2cpp::utils::StringUtils;
  32. using std::vector;
  33. using std::pair;
  34. namespace il2cpp
  35. {
  36. namespace metadata
  37. {
  38. const Il2CppType** GenericMetadata::InflateParameters(const Il2CppType** parameters, uint8_t parameterCount, const Il2CppGenericContext* context, bool inflateMethodVars)
  39. {
  40. const Il2CppType** inflatedParameters = (const Il2CppType**)MetadataCalloc(parameterCount, sizeof(Il2CppType*));
  41. for (uint8_t j = 0; j < parameterCount; j++)
  42. {
  43. inflatedParameters[j] = InflateIfNeeded(parameters[j], context, inflateMethodVars);
  44. }
  45. return inflatedParameters;
  46. }
  47. static const Il2CppType* InflateGenericParameterIfNeeded(const Il2CppType* type, const Il2CppGenericInst* inst)
  48. {
  49. IL2CPP_ASSERT(inst);
  50. Il2CppGenericParameterInfo gp = Type::GetGenericParameterInfo(type);
  51. IL2CPP_ASSERT(gp.num < inst->type_argc);
  52. const Il2CppType* genericArgument = inst->type_argv[gp.num];
  53. if (genericArgument->attrs == type->attrs && genericArgument->byref == type->byref)
  54. return genericArgument;
  55. Il2CppType* inflatedType = (Il2CppType*)MetadataMalloc(sizeof(Il2CppType));
  56. memcpy(inflatedType, genericArgument, sizeof(Il2CppType));
  57. inflatedType->byref = type->byref;
  58. inflatedType->attrs = type->attrs;
  59. ++il2cpp_runtime_stats.inflated_type_count;
  60. return inflatedType;
  61. }
  62. const Il2CppType* GenericMetadata::InflateIfNeeded(const Il2CppType* type, const Il2CppGenericContext* context, bool inflateMethodVars)
  63. {
  64. switch (type->type)
  65. {
  66. case IL2CPP_TYPE_VAR:
  67. return InflateGenericParameterIfNeeded(type, context->class_inst);
  68. case IL2CPP_TYPE_MVAR:
  69. {
  70. if (context->method_inst)
  71. return InflateGenericParameterIfNeeded(type, context->method_inst);
  72. return type;
  73. }
  74. case IL2CPP_TYPE_ARRAY:
  75. {
  76. const Il2CppType* inflatedElementType = InflateIfNeeded(type->data.array->etype, context, inflateMethodVars);
  77. if (!Il2CppTypeEqualityComparer::AreEqual(inflatedElementType, type->data.array->etype))
  78. {
  79. Il2CppType* inflatedType = (Il2CppType*)MetadataMalloc(sizeof(Il2CppType));
  80. memcpy(inflatedType, type, sizeof(Il2CppType));
  81. Il2CppArrayType* arrayType = (Il2CppArrayType*)MetadataMalloc(sizeof(Il2CppArrayType));
  82. memcpy(arrayType, type->data.array, sizeof(Il2CppArrayType));
  83. arrayType->etype = inflatedElementType;
  84. inflatedType->data.array = arrayType;
  85. ++il2cpp_runtime_stats.inflated_type_count;
  86. return inflatedType;
  87. }
  88. return type;
  89. }
  90. case IL2CPP_TYPE_PTR:
  91. case IL2CPP_TYPE_SZARRAY:
  92. {
  93. const Il2CppType* inflatedElementType = InflateIfNeeded(type->data.type, context, inflateMethodVars);
  94. if (!Il2CppTypeEqualityComparer::AreEqual(inflatedElementType, type->data.type))
  95. {
  96. Il2CppType* arrayType = (Il2CppType*)MetadataMalloc(sizeof(Il2CppType));
  97. memcpy(arrayType, type, sizeof(Il2CppType));
  98. arrayType->data.type = inflatedElementType;
  99. ++il2cpp_runtime_stats.inflated_type_count;
  100. return arrayType;
  101. }
  102. return type;
  103. }
  104. case IL2CPP_TYPE_GENERICINST:
  105. {
  106. const Il2CppGenericInst* inst = type->data.generic_class->context.class_inst;
  107. if (inst == NULL)
  108. return NULL; // This is a generic type that was too deeply nested to generate
  109. const Il2CppGenericInst* inflatedInst = GetInflatedGenericIntance(inst, context, inflateMethodVars);
  110. Il2CppGenericClass* genericClass = GenericMetadata::GetGenericClass(GenericClass::GetTypeDefinition(type->data.generic_class), inflatedInst);
  111. if (genericClass != type->data.generic_class)
  112. {
  113. Il2CppType* genericType = (Il2CppType*)MetadataMalloc(sizeof(Il2CppType));
  114. memcpy(genericType, type, sizeof(Il2CppType));
  115. genericType->data.generic_class = genericClass;
  116. ++il2cpp_runtime_stats.inflated_type_count;
  117. return genericType;
  118. }
  119. return type;
  120. }
  121. default:
  122. return type;
  123. }
  124. }
  125. static baselib::ReentrantLock s_GenericClassMutex;
  126. typedef Il2CppHashSet<Il2CppGenericClass*, Il2CppGenericClassHash, Il2CppGenericClassCompare> Il2CppGenericClassSet;
  127. static Il2CppGenericClassSet s_GenericClassSet;
  128. Il2CppGenericClass* GenericMetadata::GetGenericClass(const Il2CppClass* genericTypeDefinition, const Il2CppGenericInst* inst)
  129. {
  130. return GetGenericClass(&genericTypeDefinition->byval_arg, inst);
  131. }
  132. Il2CppGenericClass* GenericMetadata::GetGenericClass(const Il2CppType* genericTypeDefinition, const Il2CppGenericInst* inst)
  133. {
  134. // Assert that the element type is a non-inflated generic type defintion
  135. IL2CPP_ASSERT(il2cpp::vm::Class::IsGenericTypeDefinition(vm::Class::FromIl2CppType(genericTypeDefinition)));
  136. // temporary inst to lookup a permanent one that may already exist
  137. Il2CppGenericClass genericClass = { 0 };
  138. genericClass.type = genericTypeDefinition;
  139. genericClass.context.class_inst = inst;
  140. FastAutoLock lock(&s_GenericClassMutex);
  141. Il2CppGenericClassSet::const_iterator iter = s_GenericClassSet.find(&genericClass);
  142. if (iter != s_GenericClassSet.end())
  143. return *iter;
  144. Il2CppGenericClass* newClass = MetadataAllocGenericClass();
  145. newClass->type = genericTypeDefinition;
  146. newClass->context.class_inst = inst;
  147. s_GenericClassSet.insert(newClass);
  148. ++il2cpp_runtime_stats.generic_class_count;
  149. return newClass;
  150. }
  151. const MethodInfo* GenericMetadata::Inflate(const MethodInfo* methodDefinition, const Il2CppGenericContext* context)
  152. {
  153. return GenericMethod::GetMethod(methodDefinition, context->class_inst, context->method_inst);
  154. }
  155. static int RecursiveGenericDepthFor(const Il2CppGenericInst* inst);
  156. static int RecursiveGenericDepthFor(Il2CppGenericClass* genericClass)
  157. {
  158. int classInstDepth = RecursiveGenericDepthFor(genericClass->context.class_inst);
  159. int methodInstDepth = RecursiveGenericDepthFor(genericClass->context.method_inst);
  160. return std::max(classInstDepth, methodInstDepth);
  161. }
  162. static int RecursiveGenericDepthFor(const Il2CppGenericInst* inst)
  163. {
  164. if (inst == NULL)
  165. return 0;
  166. int maximumDepth = 0;
  167. for (size_t i = 0; i < inst->type_argc; i++)
  168. {
  169. if (inst->type_argv[i]->type == IL2CPP_TYPE_GENERICINST)
  170. {
  171. maximumDepth = std::max(maximumDepth, RecursiveGenericDepthFor(inst->type_argv[i]->data.generic_class));
  172. }
  173. }
  174. return maximumDepth + 1;
  175. }
  176. const Il2CppGenericMethod* GenericMetadata::Inflate(const Il2CppGenericMethod* genericMethod, const Il2CppGenericContext* context)
  177. {
  178. const Il2CppGenericInst* classInst = GetInflatedGenericIntance(genericMethod->context.class_inst, context, true);
  179. const Il2CppGenericInst* methodInst = GetInflatedGenericIntance(genericMethod->context.method_inst, context, true);
  180. // We have cases where we could infinitely recurse, inflating generics at runtime. This will lead to a stack overflow.
  181. // As we do for code generation, let's cut this off at an arbitrary level. If something tries to execute code at this
  182. // level, a crash will happen. We'll assume that this code won't actually be executed though.
  183. int maximumRuntimeGenericDepth = GetMaximumRuntimeGenericDepth();
  184. if (!il2cpp::vm::Runtime::IsLazyRGCTXInflationEnabled() && (RecursiveGenericDepthFor(classInst) > maximumRuntimeGenericDepth || RecursiveGenericDepthFor(methodInst) > maximumRuntimeGenericDepth))
  185. return NULL;
  186. return MetadataCache::GetGenericMethod(genericMethod->methodDefinition, classInst, methodInst);
  187. }
  188. const Il2CppGenericInst* GenericMetadata::GetInflatedGenericIntance(const Il2CppGenericInst* inst, const Il2CppGenericContext* context, bool inflateMethodVars)
  189. {
  190. if (inst == NULL)
  191. return NULL;
  192. const Il2CppType** inflatedArgs = (const Il2CppType**)alloca(inst->type_argc * sizeof(Il2CppType*));
  193. for (size_t i = 0; i < inst->type_argc; i++)
  194. inflatedArgs[i] = InflateIfNeeded(inst->type_argv[i], context, inflateMethodVars);
  195. return MetadataCache::GetGenericInst(inflatedArgs, inst->type_argc);
  196. }
  197. static void ConstrainedCallsToGenericInterfaceMethodsOnStructsAreNotSupported()
  198. {
  199. vm::Exception::Raise(vm::Exception::GetNotSupportedException("Cannot make a constrained call to a default interface method from a value type"));
  200. }
  201. static void ConstrainedCallsToGenericInterfaceMethodsOnStructsAreNotSupportedInvoker(Il2CppMethodPointer ptr, const MethodInfo* method, void* obj, void** args, void* ret)
  202. {
  203. ConstrainedCallsToGenericInterfaceMethodsOnStructsAreNotSupported();
  204. }
  205. Il2CppRGCTXData* GenericMetadata::InflateRGCTXLocked(const Il2CppImage* image, uint32_t token, const Il2CppGenericContext* context, const FastAutoLock& lock)
  206. {
  207. // This method assumes that it has the g_MetadataLock
  208. RGCTXCollection collection = MetadataCache::GetRGCTXs(image, token);
  209. if (collection.count == 0)
  210. return NULL;
  211. Il2CppRGCTXData* dataValues = (Il2CppRGCTXData*)MetadataCalloc(collection.count, sizeof(Il2CppRGCTXData));
  212. for (RGCTXIndex rgctxIndex = 0; rgctxIndex < collection.count; rgctxIndex++)
  213. {
  214. const Il2CppRGCTXDefinition* definitionData = collection.items + rgctxIndex;
  215. switch (definitionData->type)
  216. {
  217. case IL2CPP_RGCTX_DATA_TYPE:
  218. dataValues[rgctxIndex].type = GenericMetadata::InflateIfNeeded(MetadataCache::GetTypeFromRgctxDefinition(definitionData), context, true);
  219. break;
  220. case IL2CPP_RGCTX_DATA_CLASS:
  221. dataValues[rgctxIndex].klass = Class::FromIl2CppType(GenericMetadata::InflateIfNeeded(MetadataCache::GetTypeFromRgctxDefinition(definitionData), context, true));
  222. Class::InitSizeAndFieldLayoutLocked(dataValues[rgctxIndex].klass, lock);
  223. break;
  224. case IL2CPP_RGCTX_DATA_METHOD:
  225. dataValues[rgctxIndex].method = GenericMethod::GetMethod(Inflate(MetadataCache::GetGenericMethodFromRgctxDefinition(definitionData), context));
  226. break;
  227. case IL2CPP_RGCTX_DATA_CONSTRAINED:
  228. {
  229. const Il2CppType* type;
  230. const MethodInfo* method;
  231. std::tie(type, method) = MetadataCache::GetConstrainedCallFromRgctxDefinition(definitionData);
  232. const Il2CppType* inflatedType = GenericMetadata::InflateIfNeeded(type, context, true);
  233. if (method->is_inflated)
  234. method = GenericMethod::GetMethod(Inflate(method->genericMethod, context));
  235. if (inflatedType->valuetype)
  236. {
  237. Il2CppClass* inflatedClass = Class::FromIl2CppType(inflatedType);
  238. Class::InitLocked(inflatedClass, lock);
  239. Class::InitLocked(method->klass, lock);
  240. method = Class::GetVirtualMethod(inflatedClass, method);
  241. }
  242. dataValues[rgctxIndex].method = method;
  243. }
  244. break;
  245. default:
  246. IL2CPP_ASSERT(0);
  247. }
  248. }
  249. return dataValues;
  250. }
  251. // temporary while we generate generics
  252. void GenericMetadata::RegisterGenericClasses(Il2CppGenericClass* const * genericClasses, int32_t genericClassesCount)
  253. {
  254. s_GenericClassSet.resize(genericClassesCount / 2 + 1);
  255. // don't lock, this should only be called from startup and temporarily
  256. for (int32_t i = 0; i < genericClassesCount; i++)
  257. {
  258. if (genericClasses[i]->type != NULL)
  259. s_GenericClassSet.insert(genericClasses[i]);
  260. }
  261. }
  262. bool GenericMetadata::ContainsGenericParameters(const Il2CppClass* klass)
  263. {
  264. if (!klass->generic_class)
  265. return false;
  266. return ContainsGenericParameters(klass->generic_class->context.class_inst);
  267. }
  268. bool GenericMetadata::ContainsGenericParameters(const MethodInfo* method)
  269. {
  270. if (!method->is_inflated)
  271. return false;
  272. if (ContainsGenericParameters(method->genericMethod->context.method_inst))
  273. return true;
  274. if (method->genericMethod->context.class_inst == NULL)
  275. return false;
  276. return ContainsGenericParameters(method->genericMethod->context.class_inst);
  277. }
  278. bool GenericMetadata::ContainsGenericParameters(const Il2CppGenericInst* inst)
  279. {
  280. for (uint32_t i = 0; i < inst->type_argc; i++)
  281. {
  282. if (ContainsGenericParameters(inst->type_argv[i]))
  283. return true;
  284. }
  285. return false;
  286. }
  287. bool GenericMetadata::ContainsGenericParameters(const Il2CppType* type)
  288. {
  289. switch (type->type)
  290. {
  291. case IL2CPP_TYPE_VAR:
  292. case IL2CPP_TYPE_MVAR:
  293. return true;
  294. case IL2CPP_TYPE_GENERICINST:
  295. return ContainsGenericParameters(type->data.generic_class->context.class_inst);
  296. case IL2CPP_TYPE_ARRAY:
  297. return ContainsGenericParameters(type->data.array->etype);
  298. case IL2CPP_TYPE_SZARRAY:
  299. case IL2CPP_TYPE_PTR:
  300. case IL2CPP_TYPE_BYREF:
  301. return ContainsGenericParameters(type->data.type);
  302. default:
  303. return false;
  304. }
  305. return false;
  306. }
  307. void GenericMetadata::WalkAllGenericClasses(GenericClassWalkCallback callback, void* context)
  308. {
  309. FastAutoLock lock(&s_GenericClassMutex);
  310. for (Il2CppGenericClassSet::iterator it = s_GenericClassSet.begin(); it != s_GenericClassSet.end(); it++)
  311. {
  312. if ((*it).key->cached_class != NULL)
  313. callback((*it).key->cached_class, context);
  314. }
  315. }
  316. void GenericMetadata::Clear()
  317. {
  318. for (Il2CppGenericClassSet::iterator genericClass = s_GenericClassSet.begin(); genericClass != s_GenericClassSet.end(); genericClass++)
  319. (*genericClass).key->cached_class = NULL;
  320. s_GenericClassSet.clear();
  321. }
  322. static int s_MaximumRuntimeGenericDepth;
  323. static int s_GenericVirtualIterations;
  324. int GenericMetadata::GetMaximumRuntimeGenericDepth()
  325. {
  326. return s_MaximumRuntimeGenericDepth;
  327. }
  328. void GenericMetadata::SetMaximumRuntimeGenericDepth(int depth)
  329. {
  330. s_MaximumRuntimeGenericDepth = depth;
  331. }
  332. int GenericMetadata::GetGenericVirtualIterations()
  333. {
  334. return s_GenericVirtualIterations;
  335. }
  336. void GenericMetadata::SetGenericVirtualIterations(int iterations)
  337. {
  338. s_GenericVirtualIterations = iterations;
  339. }
  340. } /* namespace vm */
  341. } /* namespace il2cpp */