Reflection.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. #include "il2cpp-config.h"
  2. #include "il2cpp-class-internals.h"
  3. #include "il2cpp-object-internals.h"
  4. #include "il2cpp-tabledefs.h"
  5. #include "mono-structs.h"
  6. #include "gc/GCHandle.h"
  7. #include "gc/WriteBarrier.h"
  8. #include "metadata/CustomAttributeDataReader.h"
  9. #include "metadata/CustomAttributeCreator.h"
  10. #include "metadata/Il2CppTypeCompare.h"
  11. #include "metadata/Il2CppTypeHash.h"
  12. #include "vm/Array.h"
  13. #include "vm/Class.h"
  14. #include "vm/Event.h"
  15. #include "vm/Exception.h"
  16. #include "vm/Field.h"
  17. #include "vm/Image.h"
  18. #include "vm/MetadataCache.h"
  19. #include "vm/Method.h"
  20. #include "vm/Object.h"
  21. #include "vm/Parameter.h"
  22. #include "vm/Property.h"
  23. #include "vm/Reflection.h"
  24. #include "vm/String.h"
  25. #include "vm/AssemblyName.h"
  26. #include "utils/Il2CppHashMap.h"
  27. #include "utils/StringUtils.h"
  28. #include "utils/HashUtils.h"
  29. #include "gc/AppendOnlyGCHashMap.h"
  30. #include "gc/Allocator.h"
  31. template<typename T>
  32. struct ReflectionMapHash
  33. {
  34. size_t operator()(const T& ea) const
  35. {
  36. return ((size_t)(intptr_t)(ea.first)) >> 3;
  37. }
  38. };
  39. template<typename T>
  40. struct ReflectionMapLess
  41. {
  42. bool operator()(const T& ea, const T& eb) const
  43. {
  44. if (ea.first < eb.first)
  45. return true;
  46. if (ea.second < eb.second)
  47. return true;
  48. return false;
  49. }
  50. };
  51. template<typename Key, typename Value>
  52. struct ReflectionMap : public il2cpp::gc::AppendOnlyGCHashMap<Key, Value, ReflectionMapHash<Key> >
  53. {
  54. };
  55. typedef ReflectionMap<std::pair<const Il2CppAssembly*, Il2CppClass*>, Il2CppReflectionAssembly*> AssemblyMap;
  56. typedef ReflectionMap<std::pair<const FieldInfo*, Il2CppClass*>, Il2CppReflectionField*> FieldMap;
  57. typedef ReflectionMap<std::pair<const PropertyInfo*, Il2CppClass*>, Il2CppReflectionProperty*> PropertyMap;
  58. typedef ReflectionMap<std::pair<const EventInfo*, Il2CppClass*>, Il2CppReflectionEvent*> EventMap;
  59. typedef ReflectionMap<std::pair<const MethodInfo*, Il2CppClass*>, Il2CppReflectionMethod*> MethodMap;
  60. typedef ReflectionMap<std::pair<const Il2CppImage*, Il2CppClass*>, Il2CppReflectionModule*> ModuleMap;
  61. typedef ReflectionMap<std::pair<const MethodInfo*, Il2CppClass*>, Il2CppArray*> ParametersMap;
  62. typedef il2cpp::gc::AppendOnlyGCHashMap<const Il2CppType*, Il2CppReflectionType*, il2cpp::metadata::Il2CppTypeHash, il2cpp::metadata::Il2CppTypeEqualityComparer> TypeMap;
  63. typedef Il2CppHashMap<Il2CppMetadataGenericParameterHandle, const MonoGenericParameterInfo*, il2cpp::utils::PassThroughHash<Il2CppMetadataGenericParameterHandle> > MonoGenericParameterMap;
  64. typedef Il2CppHashMap<const Il2CppAssembly*, const Il2CppMonoAssemblyName*, il2cpp::utils::PointerHash<const Il2CppAssembly> > MonoAssemblyNameMap;
  65. // these needs to be pointers and allocated after GC is initialized since it uses GC Allocator
  66. static AssemblyMap* s_AssemblyMap;
  67. static FieldMap* s_FieldMap;
  68. static PropertyMap* s_PropertyMap;
  69. static EventMap* s_EventMap;
  70. static MethodMap* s_MethodMap;
  71. static ModuleMap* s_ModuleMap;
  72. static ParametersMap* s_ParametersMap;
  73. static TypeMap* s_TypeMap;
  74. static MonoGenericParameterMap* s_MonoGenericParamterMap;
  75. static MonoAssemblyNameMap* s_MonoAssemblyNameMap;
  76. namespace il2cpp
  77. {
  78. namespace vm
  79. {
  80. static Il2CppClass *s_System_Reflection_Assembly;
  81. static Il2CppClass * s_System_Reflection_RuntimeFieldInfoKlass;
  82. static Il2CppClass *s_System_Reflection_Module;
  83. static Il2CppClass * s_System_Reflection_RuntimePropertyInfoKlass;
  84. static Il2CppClass * s_System_Reflection_RuntimeEventInfoKlass;
  85. static FieldInfo *s_DbNullValueField;
  86. static FieldInfo *s_ReflectionMissingField;
  87. static Il2CppClass *s_System_Reflection_ParameterInfo;
  88. static Il2CppClass *s_System_Reflection_ParameterInfo_array;
  89. /*
  90. * We use the same C representation for methods and constructors, but the type
  91. * name in C# is different.
  92. */
  93. static Il2CppClass *s_System_Reflection_MethodInfo;
  94. static Il2CppClass *s_System_Reflection_ConstructorInfo;
  95. static il2cpp::metadata::CustomAttributeFilter GetFilter(Il2CppClass*& attributeClass)
  96. {
  97. if (attributeClass == NULL)
  98. return [](const MethodInfo*) { return true; };
  99. return [attributeClass](const MethodInfo* ctor) {
  100. Il2CppClass* klass = ctor->klass;
  101. return il2cpp::vm::Class::HasParent(klass, attributeClass) || (il2cpp::vm::Class::IsInterface(attributeClass) && il2cpp::vm::Class::IsAssignableFrom(attributeClass, klass));
  102. };
  103. }
  104. Il2CppReflectionAssembly* Reflection::GetAssemblyObject(const Il2CppAssembly *assembly)
  105. {
  106. Il2CppReflectionAssembly *res;
  107. AssemblyMap::key_type::wrapped_type key(assembly, (Il2CppClass*)NULL);
  108. AssemblyMap::data_type value = NULL;
  109. if (s_AssemblyMap->TryGetValue(key, &value))
  110. return value;
  111. res = (Il2CppReflectionAssembly*)Object::New(s_System_Reflection_Assembly);
  112. res->assembly = assembly;
  113. return s_AssemblyMap->GetOrAdd(key, res);
  114. }
  115. Il2CppReflectionAssemblyName* Reflection::GetAssemblyNameObject(const Il2CppAssemblyName *assemblyName)
  116. {
  117. IL2CPP_ASSERT(il2cpp_defaults.assembly_name_class != NULL);
  118. std::string fullAssemblyName = vm::AssemblyName::AssemblyNameToString(*assemblyName);
  119. Il2CppReflectionAssemblyName* reflectionAssemblyName = (Il2CppReflectionAssemblyName*)Object::New(il2cpp_defaults.assembly_name_class);
  120. vm::AssemblyName::ParseName(reflectionAssemblyName, fullAssemblyName);
  121. return reflectionAssemblyName;
  122. }
  123. Il2CppReflectionField* Reflection::GetFieldObject(Il2CppClass *klass, FieldInfo *field)
  124. {
  125. Il2CppReflectionField *res;
  126. FieldMap::key_type::wrapped_type key(field, klass);
  127. FieldMap::data_type value = NULL;
  128. if (s_FieldMap->TryGetValue(key, &value))
  129. return value;
  130. res = (Il2CppReflectionField*)Object::New(s_System_Reflection_RuntimeFieldInfoKlass);
  131. res->klass = klass;
  132. res->field = field;
  133. IL2CPP_OBJECT_SETREF(res, name, String::New(Field::GetName(field)));
  134. res->attrs = field->type->attrs;
  135. IL2CPP_OBJECT_SETREF(res, type, GetTypeObject(field->type));
  136. return s_FieldMap->GetOrAdd(key, res);
  137. }
  138. const MethodInfo* Reflection::GetMethod(const Il2CppReflectionMethod* method)
  139. {
  140. return method->method;
  141. }
  142. Il2CppReflectionMethod* Reflection::GetMethodObject(const MethodInfo *method, Il2CppClass *refclass)
  143. {
  144. Il2CppClass *klass;
  145. Il2CppReflectionMethod *ret;
  146. if (!refclass)
  147. refclass = method->klass;
  148. MethodMap::key_type::wrapped_type key(method, refclass);
  149. MethodMap::data_type value = NULL;
  150. if (s_MethodMap->TryGetValue(key, &value))
  151. return value;
  152. if (*method->name == '.' && (strcmp(method->name, ".ctor") == 0 || strcmp(method->name, ".cctor") == 0))
  153. {
  154. klass = s_System_Reflection_ConstructorInfo;
  155. }
  156. else
  157. {
  158. klass = s_System_Reflection_MethodInfo;
  159. }
  160. ret = (Il2CppReflectionMethod*)Object::New(klass);
  161. ret->method = method;
  162. IL2CPP_OBJECT_SETREF(ret, reftype, GetTypeObject(&refclass->byval_arg));
  163. return s_MethodMap->GetOrAdd(key, ret);
  164. }
  165. Il2CppReflectionModule* Reflection::GetModuleObject(const Il2CppImage *image)
  166. {
  167. Il2CppReflectionModule *res;
  168. //char* basename;
  169. ModuleMap::key_type::wrapped_type key(image, (Il2CppClass*)NULL);
  170. ModuleMap::data_type value = NULL;
  171. if (s_ModuleMap->TryGetValue(key, &value))
  172. return value;
  173. res = (Il2CppReflectionModule*)Object::New(s_System_Reflection_Module);
  174. res->image = image;
  175. IL2CPP_OBJECT_SETREF(res, assembly, Reflection::GetAssemblyObject(image->assembly));
  176. IL2CPP_OBJECT_SETREF(res, fqname, String::New(image->name));
  177. IL2CPP_NOT_IMPLEMENTED_ICALL_NO_ASSERT(Reflection::GetModuleObject, "Missing Module fields need set");
  178. //basename = g_path_get_basename (image->name);
  179. //IL2CPP_OBJECT_SETREF (res, name, String::New (basename));
  180. IL2CPP_OBJECT_SETREF(res, name, String::New(image->name));
  181. IL2CPP_OBJECT_SETREF(res, scopename, String::New(image->nameNoExt));
  182. //g_free (basename);
  183. /*if (image->assembly->image == image) {
  184. res->token = mono_metadata_make_token (MONO_TABLE_MODULE, 1);
  185. } else {
  186. int i;
  187. res->token = 0;
  188. if (image->assembly->image->modules) {
  189. for (i = 0; i < image->assembly->image->module_count; i++) {
  190. if (image->assembly->image->modules [i] == image)
  191. res->token = mono_metadata_make_token (MONO_TABLE_MODULEREF, i + 1);
  192. }
  193. IL2CPP_ASSERT(res->token);
  194. }
  195. }*/
  196. return s_ModuleMap->GetOrAdd(key, res);
  197. }
  198. Il2CppReflectionProperty* Reflection::GetPropertyObject(Il2CppClass *klass, const PropertyInfo *property)
  199. {
  200. Il2CppReflectionProperty *res;
  201. PropertyMap::key_type::wrapped_type key(property, klass);
  202. PropertyMap::data_type value = NULL;
  203. if (s_PropertyMap->TryGetValue(key, &value))
  204. return value;
  205. res = (Il2CppReflectionProperty*)Object::New(s_System_Reflection_RuntimePropertyInfoKlass);
  206. res->klass = klass;
  207. res->property = property;
  208. return s_PropertyMap->GetOrAdd(key, res);
  209. }
  210. Il2CppReflectionEvent* Reflection::GetEventObject(Il2CppClass* klass, const EventInfo* event)
  211. {
  212. Il2CppReflectionEvent* result;
  213. EventMap::key_type::wrapped_type key(event, klass);
  214. EventMap::data_type value = NULL;
  215. if (s_EventMap->TryGetValue(key, &value))
  216. return value;
  217. Il2CppReflectionMonoEvent* monoEvent = reinterpret_cast<Il2CppReflectionMonoEvent*>(Object::New(s_System_Reflection_RuntimeEventInfoKlass));
  218. monoEvent->eventInfo = event;
  219. monoEvent->reflectedType = Reflection::GetTypeObject(&klass->byval_arg);
  220. result = reinterpret_cast<Il2CppReflectionEvent*>(monoEvent);
  221. return s_EventMap->GetOrAdd(key, result);
  222. }
  223. Il2CppReflectionType* Reflection::GetTypeObject(const Il2CppType *type)
  224. {
  225. Il2CppReflectionType* object = NULL;
  226. if (s_TypeMap->TryGetValue(type, &object))
  227. return object;
  228. Il2CppReflectionType* typeObject = (Il2CppReflectionType*)Object::New(il2cpp_defaults.runtimetype_class);
  229. typeObject->type = type;
  230. return s_TypeMap->GetOrAdd(type, typeObject);
  231. }
  232. Il2CppObject* Reflection::GetDBNullObject()
  233. {
  234. Il2CppObject* valueFieldValue;
  235. if (!s_DbNullValueField)
  236. {
  237. s_DbNullValueField = Class::GetFieldFromName(il2cpp_defaults.dbnull_class, "Value");
  238. IL2CPP_ASSERT(s_DbNullValueField);
  239. }
  240. valueFieldValue = Field::GetValueObject(s_DbNullValueField, NULL);
  241. IL2CPP_ASSERT(valueFieldValue);
  242. return valueFieldValue;
  243. }
  244. static Il2CppObject* GetReflectionMissingObject()
  245. {
  246. Il2CppObject* valueFieldValue;
  247. if (!s_ReflectionMissingField)
  248. {
  249. Il2CppClass* klass = Image::ClassFromName(il2cpp_defaults.corlib, "System.Reflection", "Missing");
  250. Class::Init(klass);
  251. s_ReflectionMissingField = Class::GetFieldFromName(klass, "Value");
  252. IL2CPP_ASSERT(s_ReflectionMissingField);
  253. }
  254. valueFieldValue = Field::GetValueObject(s_ReflectionMissingField, NULL);
  255. IL2CPP_ASSERT(valueFieldValue);
  256. return valueFieldValue;
  257. }
  258. static Il2CppObject* GetObjectForMissingDefaultValue(uint32_t parameterAttributes)
  259. {
  260. if (parameterAttributes & PARAM_ATTRIBUTE_OPTIONAL)
  261. return GetReflectionMissingObject();
  262. else
  263. return Reflection::GetDBNullObject();
  264. }
  265. Il2CppArray* Reflection::GetParamObjects(const MethodInfo *method, Il2CppClass *refclass)
  266. {
  267. Il2CppArray *res = NULL;
  268. Il2CppReflectionMethod *member = NULL;
  269. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Reflection::GetParamObjects, "Work in progress!");
  270. if (!method->parameters_count)
  271. return Array::NewSpecific(s_System_Reflection_ParameterInfo_array, 0);
  272. // Mono caches based on the address of the method pointer in the MethodInfo
  273. // since they put everything in one cache and the MethodInfo is already used as key for GetMethodObject caching
  274. // However, since we have distinct maps for the different types we can use MethodInfo as the key again
  275. ParametersMap::key_type::wrapped_type key(method, refclass);
  276. ParametersMap::data_type value;
  277. if (s_ParametersMap->TryGetValue(key, &value))
  278. return value;
  279. member = GetMethodObject(method, refclass);
  280. res = Array::NewSpecific(s_System_Reflection_ParameterInfo_array, method->parameters_count);
  281. for (int i = 0; i < method->parameters_count; ++i)
  282. {
  283. Il2CppReflectionParameter* param = (Il2CppReflectionParameter*)Object::New(s_System_Reflection_ParameterInfo);
  284. IL2CPP_OBJECT_SETREF(param, ClassImpl, GetTypeObject(method->parameters[i]));
  285. IL2CPP_OBJECT_SETREF(param, MemberImpl, (Il2CppObject*)member);
  286. const char* parameter_name = Method::GetParamName(method, i);
  287. IL2CPP_OBJECT_SETREF(param, NameImpl, parameter_name ? String::New(parameter_name) : NULL);
  288. param->PositionImpl = i;
  289. param->AttrsImpl = method->parameters[i]->attrs;
  290. Il2CppObject* defaultValue = NULL;
  291. if (param->AttrsImpl & PARAM_ATTRIBUTE_HAS_DEFAULT)
  292. {
  293. bool isExplicitySetNullDefaultValue = false;
  294. defaultValue = Parameter::GetDefaultParameterValueObject(method, i, &isExplicitySetNullDefaultValue);
  295. if (defaultValue == NULL && !isExplicitySetNullDefaultValue)
  296. defaultValue = GetObjectForMissingDefaultValue(param->AttrsImpl);
  297. }
  298. else
  299. {
  300. defaultValue = GetObjectForMissingDefaultValue(param->AttrsImpl);
  301. }
  302. IL2CPP_OBJECT_SETREF(param, DefaultValueImpl, defaultValue);
  303. il2cpp_array_setref(res, i, param);
  304. }
  305. return s_ParametersMap->GetOrAdd(key, res);
  306. }
  307. // TODO: move this somewhere else
  308. bool Reflection::IsType(Il2CppObject *obj)
  309. {
  310. return (obj->klass == il2cpp_defaults.runtimetype_class);
  311. }
  312. static bool IsMethod(Il2CppObject *obj)
  313. {
  314. return obj->klass == s_System_Reflection_MethodInfo;
  315. }
  316. static bool IsCMethod(Il2CppObject *obj)
  317. {
  318. return obj->klass == s_System_Reflection_ConstructorInfo;
  319. }
  320. bool Reflection::IsAnyMethod(Il2CppObject *obj)
  321. {
  322. return IsMethod(obj) || IsCMethod(obj);
  323. }
  324. bool Reflection::IsField(Il2CppObject *obj)
  325. {
  326. return obj->klass == s_System_Reflection_RuntimeFieldInfoKlass;
  327. }
  328. bool Reflection::IsProperty(Il2CppObject *obj)
  329. {
  330. return obj->klass == s_System_Reflection_RuntimePropertyInfoKlass;
  331. }
  332. bool Reflection::IsEvent(Il2CppObject *obj)
  333. {
  334. return obj->klass == s_System_Reflection_RuntimeEventInfoKlass;
  335. }
  336. static bool IsParameter(Il2CppObject *obj)
  337. {
  338. return obj->klass == il2cpp_defaults.parameter_info_class;
  339. }
  340. static bool IsAssembly(Il2CppObject *obj)
  341. {
  342. return obj->klass == s_System_Reflection_Assembly->klass;
  343. }
  344. static std::tuple<uint32_t, const Il2CppImage*> GetMetadataTokenFromReflectionType(Il2CppObject* obj, bool throwOnError = true)
  345. {
  346. if (vm::Reflection::IsField(obj))
  347. {
  348. Il2CppReflectionField* field = (Il2CppReflectionField*)obj;
  349. return std::make_tuple(vm::Field::GetToken(field->field), field->field->parent->image);
  350. }
  351. if (vm::Reflection::IsAnyMethod(obj))
  352. {
  353. Il2CppReflectionMethod* method = (Il2CppReflectionMethod*)obj;
  354. return std::make_tuple(vm::Method::GetToken(method->method), method->method->klass->image);
  355. }
  356. if (vm::Reflection::IsProperty(obj))
  357. {
  358. Il2CppReflectionProperty* prop = (Il2CppReflectionProperty*)obj;
  359. return std::make_tuple(vm::Property::GetToken(prop->property), prop->property->parent->image);
  360. }
  361. if (vm::Reflection::IsEvent(obj))
  362. {
  363. Il2CppReflectionMonoEvent* eventInfo = (Il2CppReflectionMonoEvent*)obj;
  364. return std::make_tuple(vm::Event::GetToken(eventInfo->eventInfo), eventInfo->eventInfo->parent->image);
  365. }
  366. if (vm::Reflection::IsType(obj))
  367. {
  368. Il2CppReflectionType* type = (Il2CppReflectionType*)obj;
  369. Il2CppClass* klass = vm::Class::FromIl2CppType(type->type);
  370. return std::make_tuple(klass->token, klass->image);
  371. }
  372. if (IsParameter(obj))
  373. {
  374. Il2CppReflectionParameter* parameter = (Il2CppReflectionParameter*)obj;
  375. Il2CppReflectionMethod* method = (Il2CppReflectionMethod*)parameter->MemberImpl;
  376. const Il2CppImage* image = method->method->klass->image;
  377. if (parameter->PositionImpl == -1)
  378. return std::make_tuple(0x8000000, method->method->klass->image); // This is what mono returns as a fixed value.
  379. return std::make_tuple(vm::Method::GetParameterToken(method->method, parameter->PositionImpl), method->method->klass->image);
  380. }
  381. if (IsAssembly(obj))
  382. {
  383. Il2CppReflectionAssembly* assembly = (Il2CppReflectionAssembly*)obj;
  384. return std::make_tuple(assembly->assembly->token, assembly->assembly->image);
  385. }
  386. if (throwOnError)
  387. NOT_SUPPORTED_IL2CPP(MemberInfo::get_MetadataToken, "This icall is not supported by il2cpp.");
  388. return std::make_tuple(0, (const Il2CppImage*)nullptr);
  389. }
  390. int Reflection::GetMetadataToken(Il2CppObject* obj)
  391. {
  392. uint32_t token;
  393. const Il2CppImage* image;
  394. std::tie(token, image) = GetMetadataTokenFromReflectionType(obj);
  395. return (int)token;
  396. }
  397. bool Reflection::HasAttribute(Il2CppReflectionParameter *parameter, Il2CppClass* attributeClass)
  398. {
  399. Il2CppReflectionMethod* method = (Il2CppReflectionMethod*)parameter->MemberImpl;
  400. if (method->method->parameters == NULL)
  401. return false;
  402. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Reflection::GetCustomAttributeTypeCacheFor, "-1 represents the return value. Need to emit custom attribute information for that.")
  403. if (parameter->PositionImpl == -1)
  404. return false;
  405. const MethodInfo* methodWithParameterAttributeInformation = method->method;
  406. if (method->method->is_inflated)
  407. methodWithParameterAttributeInformation = method->method->genericMethod->methodDefinition;
  408. auto reader = il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(methodWithParameterAttributeInformation->klass->image, Method::GetParameterToken(method->method, parameter->PositionImpl));
  409. return HasAttribute(reader, attributeClass);
  410. }
  411. Il2CppArray* Reflection::GetCustomAttrsInfo(Il2CppObject* obj, Il2CppClass* attributeClass)
  412. {
  413. il2cpp::metadata::CustomAttributeDataReader reader = GetCustomAttrsDataReader(obj);
  414. return GetCustomAttrsInfo(reader, attributeClass);
  415. }
  416. Il2CppArray* Reflection::GetCustomAttrsInfo(const il2cpp::metadata::CustomAttributeDataReader& reader, Il2CppClass* attributeClass)
  417. {
  418. if (reader.GetCount() == 0)
  419. return NULL;
  420. auto filter = GetFilter(attributeClass);
  421. uint32_t attributeCount = reader.GetCount(filter);
  422. if (attributeCount == 0)
  423. return NULL;
  424. Il2CppArray* attrArray = il2cpp::vm::Array::New(il2cpp_defaults.attribute_class, attributeCount);
  425. il2cpp::metadata::CustomAttributeDataIterator iter = reader.GetDataIterator(filter);
  426. for (uint32_t i = 0; i < attributeCount; i++)
  427. {
  428. Il2CppException* exc = NULL;
  429. il2cpp::metadata::CustomAttributeCreator creator;
  430. if (reader.VisitCustomAttributeData(&iter, &creator, &exc))
  431. {
  432. il2cpp_array_setref(attrArray, i, creator.GetAttribute(&exc));
  433. if (exc != NULL)
  434. il2cpp::vm::Exception::Raise(exc);
  435. }
  436. if (exc != NULL)
  437. il2cpp::vm::Exception::Raise(exc);
  438. }
  439. return attrArray;
  440. }
  441. il2cpp::metadata::CustomAttributeDataReader Reflection::GetCustomAttrsDataReader(Il2CppObject* obj)
  442. {
  443. uint32_t token;
  444. const Il2CppImage* image;
  445. std::tie(token, image) = GetMetadataTokenFromReflectionType(obj, false);
  446. if (token == 0)
  447. {
  448. // obj is not a reflection type, reflect on obj's class
  449. token = obj->klass->token;
  450. image = obj->klass->image;
  451. }
  452. if ((token & IL2CPP_TOKEN_PARAM_DEF) == IL2CPP_TOKEN_PARAM_DEF)
  453. {
  454. Il2CppReflectionParameter* parameter = (Il2CppReflectionParameter*)obj;
  455. Il2CppReflectionMethod* method = (Il2CppReflectionMethod*)parameter->MemberImpl;
  456. if (method->method->parameters == NULL)
  457. return il2cpp::metadata::CustomAttributeDataReader::Empty();
  458. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Reflection::GetCustomAttributeReaderFor, "-1 represents the return value. Need to emit custom attribute information for that.")
  459. if (parameter->PositionImpl == -1)
  460. return il2cpp::metadata::CustomAttributeDataReader::Empty();
  461. }
  462. return il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(image, token);
  463. }
  464. bool Reflection::HasAttribute(Il2CppObject *obj, Il2CppClass* attributeClass)
  465. {
  466. il2cpp::metadata::CustomAttributeDataReader reader = GetCustomAttrsDataReader(obj);
  467. return HasAttribute(reader, attributeClass);
  468. }
  469. bool Reflection::HasAttribute(const il2cpp::metadata::CustomAttributeDataReader& reader, Il2CppClass* attributeClass)
  470. {
  471. if (reader.GetCount() == 0)
  472. return false;
  473. auto filter = GetFilter(attributeClass);
  474. auto ctorIter = reader.GetCtorIterator(filter);
  475. const MethodInfo* ctor;
  476. return reader.IterateAttributeCtors(&ctor, &ctorIter);
  477. }
  478. Il2CppObject* Reflection::GetCustomAttribute(Il2CppMetadataCustomAttributeHandle token, Il2CppClass* attributeClass)
  479. {
  480. auto reader = il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(token);
  481. if (reader.GetCount() == 0)
  482. return NULL;
  483. auto filter = GetFilter(attributeClass);
  484. if (reader.GetCount(filter) == 0)
  485. return NULL;
  486. Il2CppObject* attr = NULL;
  487. il2cpp::metadata::CustomAttributeDataIterator iter = reader.GetDataIterator(filter);
  488. Il2CppException* exc = NULL;
  489. il2cpp::metadata::CustomAttributeCreator creator;
  490. if (reader.VisitCustomAttributeData(&iter, &creator, &exc))
  491. {
  492. if (exc == NULL)
  493. attr = creator.GetAttribute(&exc);
  494. }
  495. if (exc != NULL)
  496. il2cpp::vm::Exception::Raise(exc);
  497. return attr;
  498. }
  499. Il2CppArray* Reflection::ConstructCustomAttributes(Il2CppMetadataCustomAttributeHandle token)
  500. {
  501. auto reader = il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(token);
  502. return GetCustomAttrsInfo(reader, NULL);
  503. }
  504. void Reflection::Initialize()
  505. {
  506. s_AssemblyMap = new AssemblyMap();
  507. s_FieldMap = new FieldMap();
  508. s_PropertyMap = new PropertyMap();
  509. s_EventMap = new EventMap();
  510. s_MethodMap = new MethodMap();
  511. s_ModuleMap = new ModuleMap();
  512. s_ParametersMap = new ParametersMap();
  513. s_TypeMap = new TypeMap();
  514. s_MonoGenericParamterMap = new MonoGenericParameterMap();
  515. s_MonoAssemblyNameMap = new MonoAssemblyNameMap();
  516. s_System_Reflection_Assembly = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimeAssembly");
  517. IL2CPP_ASSERT(s_System_Reflection_Assembly != NULL);
  518. #if !IL2CPP_TINY_DEBUGGER
  519. s_System_Reflection_Module = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimeModule");
  520. IL2CPP_ASSERT(s_System_Reflection_Module != NULL);
  521. s_System_Reflection_ConstructorInfo = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimeConstructorInfo");
  522. IL2CPP_ASSERT(s_System_Reflection_ConstructorInfo != NULL);
  523. s_System_Reflection_MethodInfo = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimeMethodInfo");
  524. IL2CPP_ASSERT(s_System_Reflection_MethodInfo != NULL);
  525. s_System_Reflection_ParameterInfo = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimeParameterInfo");
  526. IL2CPP_ASSERT(s_System_Reflection_ParameterInfo != NULL);
  527. s_System_Reflection_ParameterInfo_array = Class::GetArrayClass(s_System_Reflection_ParameterInfo, 1);
  528. IL2CPP_ASSERT(s_System_Reflection_ParameterInfo_array != NULL);
  529. s_System_Reflection_RuntimeFieldInfoKlass = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimeFieldInfo");
  530. IL2CPP_ASSERT(s_System_Reflection_RuntimeFieldInfoKlass != NULL);
  531. s_System_Reflection_RuntimeEventInfoKlass = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimeEventInfo");
  532. IL2CPP_ASSERT(s_System_Reflection_RuntimeEventInfoKlass != NULL);
  533. s_System_Reflection_RuntimePropertyInfoKlass = Class::FromName(il2cpp_defaults.corlib, "System.Reflection", "RuntimePropertyInfo");
  534. IL2CPP_ASSERT(s_System_Reflection_RuntimePropertyInfoKlass != NULL);
  535. #endif
  536. }
  537. bool Reflection::HasAttribute(FieldInfo *field, Il2CppClass *attributeClass)
  538. {
  539. auto reader = il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(field->parent->image, Field::GetToken(field));
  540. return HasAttribute(reader, attributeClass);
  541. }
  542. bool Reflection::HasAttribute(const MethodInfo *method, Il2CppClass *attributeClass)
  543. {
  544. auto reader = il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(method->klass->image, Method::GetToken(method));
  545. return HasAttribute(reader, attributeClass);
  546. }
  547. bool Reflection::HasAttribute(Il2CppClass *klass, Il2CppClass *attributeClass)
  548. {
  549. auto reader = il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(klass->image, klass->token);
  550. return HasAttribute(reader, attributeClass);
  551. }
  552. bool Reflection::HasAttribute(Il2CppMetadataCustomAttributeHandle handle, Il2CppClass *attributeClass)
  553. {
  554. auto reader = il2cpp::vm::MetadataCache::GetCustomAttributeDataReader(handle);
  555. return HasAttribute(reader, attributeClass);
  556. }
  557. Il2CppClass* Reflection::TypeGetHandle(Il2CppReflectionType* ref)
  558. {
  559. if (!ref)
  560. return NULL;
  561. return Class::FromSystemType(ref);
  562. }
  563. const MonoGenericParameterInfo* Reflection::GetMonoGenericParameterInfo(Il2CppMetadataGenericParameterHandle param)
  564. {
  565. MonoGenericParameterMap::const_iterator it = s_MonoGenericParamterMap->find(param);
  566. if (it == s_MonoGenericParamterMap->end())
  567. return NULL;
  568. return it->second;
  569. }
  570. void Reflection::SetMonoGenericParameterInfo(Il2CppMetadataGenericParameterHandle param, const MonoGenericParameterInfo *monoParam)
  571. {
  572. s_MonoGenericParamterMap->insert(std::make_pair(param, monoParam));
  573. }
  574. const Il2CppMonoAssemblyName* Reflection::GetMonoAssemblyName(const Il2CppAssembly *assembly)
  575. {
  576. MonoAssemblyNameMap::const_iterator it = s_MonoAssemblyNameMap->find(assembly);
  577. if (it == s_MonoAssemblyNameMap->end())
  578. return NULL;
  579. return it->second;
  580. }
  581. void Reflection::SetMonoAssemblyName(const Il2CppAssembly *assembly, const Il2CppMonoAssemblyName *aname)
  582. {
  583. s_MonoAssemblyNameMap->insert(std::make_pair(assembly, aname));
  584. }
  585. void Reflection::ClearStatics()
  586. {
  587. delete s_AssemblyMap;
  588. s_AssemblyMap = NULL;
  589. delete s_FieldMap;
  590. s_FieldMap = NULL;
  591. delete s_PropertyMap;
  592. s_PropertyMap = NULL;
  593. delete s_EventMap;
  594. s_EventMap = NULL;
  595. delete s_MethodMap;
  596. s_MethodMap = NULL;
  597. delete s_ModuleMap;
  598. s_ModuleMap = NULL;
  599. delete s_ParametersMap;
  600. s_ParametersMap = NULL;
  601. delete s_TypeMap;
  602. s_TypeMap = NULL;
  603. delete s_MonoGenericParamterMap;
  604. s_MonoGenericParamterMap = NULL;
  605. delete s_MonoAssemblyNameMap;
  606. s_MonoAssemblyNameMap = NULL;
  607. s_System_Reflection_Assembly = NULL;
  608. s_System_Reflection_RuntimeFieldInfoKlass = NULL;
  609. s_System_Reflection_Module = NULL;
  610. s_System_Reflection_RuntimePropertyInfoKlass = NULL;
  611. s_System_Reflection_RuntimeEventInfoKlass = NULL;
  612. s_DbNullValueField = NULL;
  613. s_ReflectionMissingField = NULL;
  614. s_System_Reflection_ParameterInfo = NULL;
  615. s_System_Reflection_ParameterInfo_array = NULL;
  616. s_System_Reflection_MethodInfo = NULL;
  617. s_System_Reflection_ConstructorInfo = NULL;
  618. }
  619. } /* namespace vm */
  620. } /* namespace il2cpp */