Image.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "il2cpp-config.h"
  2. #include <vector>
  3. #include <map>
  4. #include <limits>
  5. #include "os/Mutex.h"
  6. #include "utils/StringUtils.h"
  7. #include "vm/Array.h"
  8. #include "vm/Class.h"
  9. #include "vm/Image.h"
  10. #include "vm/MetadataCache.h"
  11. #include "vm/Reflection.h"
  12. #include "vm/StackTrace.h"
  13. #include "vm/Type.h"
  14. #include "utils/HashUtils.h"
  15. #include "utils/Il2CppHashMap.h"
  16. #include "utils/MemoryMappedFile.h"
  17. #include "utils/StringUtils.h"
  18. #include "vm-utils/VmStringUtils.h"
  19. #include "Baselib.h"
  20. #include "Cpp/ReentrantLock.h"
  21. struct NamespaceAndNamePairHash
  22. {
  23. size_t operator()(const std::pair<const char*, const char*>& pair) const
  24. {
  25. return il2cpp::utils::HashUtils::Combine(il2cpp::utils::StringUtils::Hash(pair.first), il2cpp::utils::StringUtils::Hash(pair.second));
  26. }
  27. };
  28. struct NamespaceAndNamePairEquals
  29. {
  30. bool operator()(const std::pair<const char*, const char*>& p1, const std::pair<const char*, const char*>& p2) const
  31. {
  32. return !strcmp(p1.first, p2.first) && !strcmp(p1.second, p2.second);
  33. }
  34. };
  35. struct NamespaceAndNamePairLess
  36. {
  37. bool operator()(const std::pair<const char*, const char*>& p1, const std::pair<const char*, const char*>& p2) const
  38. {
  39. int namespaceCompare = strcmp(p1.first, p2.first);
  40. if (namespaceCompare < 0)
  41. return true;
  42. if (namespaceCompare > 0)
  43. return false;
  44. return strcmp(p1.second, p2.second) < 0;
  45. }
  46. };
  47. struct Il2CppNameToTypeHandleHashTable : public Il2CppHashMap<std::pair<const char*, const char*>, Il2CppMetadataTypeHandle, NamespaceAndNamePairHash, NamespaceAndNamePairEquals>
  48. {
  49. typedef Il2CppHashMap<std::pair<const char*, const char*>, Il2CppMetadataTypeHandle, NamespaceAndNamePairHash, NamespaceAndNamePairEquals> Base;
  50. Il2CppNameToTypeHandleHashTable() : Base()
  51. {
  52. }
  53. };
  54. namespace il2cpp
  55. {
  56. namespace vm
  57. {
  58. const Il2CppAssembly* Image::GetAssembly(const Il2CppImage* image)
  59. {
  60. return image->assembly;
  61. }
  62. typedef il2cpp::vm::StackFrames::const_reverse_iterator StackReverseIterator;
  63. static bool IsSystemType(Il2CppClass* klass)
  64. {
  65. return strcmp(klass->namespaze, "System") == 0 && strcmp(klass->name, "Type") == 0;
  66. }
  67. static bool IsSystemReflectionAssembly(Il2CppClass* klass)
  68. {
  69. return strcmp(klass->namespaze, "System.Reflection") == 0 && strcmp(klass->name, "Assembly") == 0;
  70. }
  71. static StackReverseIterator GetNextImageFromStack(StackReverseIterator first, StackReverseIterator last)
  72. {
  73. for (StackReverseIterator it = first; it != last; it++)
  74. {
  75. Il2CppClass* klass = it->method->klass;
  76. if (klass->image != NULL && !IsSystemType(klass) && !IsSystemReflectionAssembly(klass))
  77. {
  78. return it;
  79. }
  80. }
  81. return last;
  82. }
  83. const Il2CppImage* Image::GetExecutingImage()
  84. {
  85. const il2cpp::vm::StackFrames& stack = *StackTrace::GetStackFrames();
  86. StackReverseIterator imageIt = GetNextImageFromStack(stack.rbegin(), stack.rend());
  87. if (imageIt != stack.rend())
  88. {
  89. return imageIt->method->klass->image;
  90. }
  91. // Fallback to corlib if no image is found
  92. return const_cast<Il2CppImage*>(Image::GetCorlib());
  93. }
  94. const Il2CppImage* Image::GetCallingImage()
  95. {
  96. const il2cpp::vm::StackFrames& stack = *StackTrace::GetStackFrames();
  97. StackReverseIterator imageIt = GetNextImageFromStack(stack.rbegin(), stack.rend());
  98. if (imageIt != stack.rend())
  99. {
  100. imageIt = GetNextImageFromStack(++imageIt, stack.rend());
  101. if (imageIt != stack.rend())
  102. {
  103. return imageIt->method->klass->image;
  104. }
  105. }
  106. // Fallback to corlib if no image is found
  107. return const_cast<Il2CppImage*>(Image::GetCorlib());
  108. }
  109. const char * Image::GetName(const Il2CppImage* image)
  110. {
  111. return image->name;
  112. }
  113. const char * Image::GetFileName(const Il2CppImage* image)
  114. {
  115. return image->name;
  116. }
  117. const MethodInfo* Image::GetEntryPoint(const Il2CppImage* image)
  118. {
  119. return MetadataCache::GetAssemblyEntryPoint(image);
  120. }
  121. Il2CppImage* Image::GetCorlib()
  122. {
  123. return il2cpp_defaults.corlib;
  124. }
  125. static baselib::ReentrantLock s_ClassFromNameMutex;
  126. static void AddNestedTypesToNametoClassHashTable(Il2CppNameToTypeHandleHashTable* hashTable, const char *namespaze, const std::string& parentName, Il2CppMetadataTypeHandle handle)
  127. {
  128. std::pair<const char*, const char*> namespaceAndName = MetadataCache::GetTypeNamespaceAndName(handle);
  129. std::string name = parentName + "/" + namespaceAndName.second;
  130. char *pName = (char*)IL2CPP_CALLOC(name.size() + 1, sizeof(char));
  131. strcpy(pName, name.c_str());
  132. hashTable->insert(std::make_pair(std::make_pair(namespaze, (const char*)pName), handle));
  133. void *iter = NULL;
  134. while (Il2CppMetadataTypeHandle nestedClass = MetadataCache::GetNestedTypes(handle, &iter))
  135. AddNestedTypesToNametoClassHashTable(hashTable, namespaze, name, nestedClass);
  136. }
  137. static void AddNestedTypesToNametoClassHashTable(const Il2CppImage* image, Il2CppMetadataTypeHandle handle)
  138. {
  139. std::pair<const char*, const char*> namespaceAndName = MetadataCache::GetTypeNamespaceAndName(handle);
  140. void *iter = NULL;
  141. while (Il2CppMetadataTypeHandle nestedClass = MetadataCache::GetNestedTypes(handle, &iter))
  142. {
  143. AddNestedTypesToNametoClassHashTable(image->nameToClassHashTable, namespaceAndName.first, namespaceAndName.second, nestedClass);
  144. }
  145. }
  146. // This must be called when the s_ClassFromNameMutex is held.
  147. static void AddTypeToNametoClassHashTable(const Il2CppImage* image, Il2CppMetadataTypeHandle typeHandle)
  148. {
  149. if (typeHandle == NULL)
  150. return;
  151. // don't add nested types
  152. if (MetadataCache::TypeIsNested(typeHandle))
  153. return;
  154. if (image != il2cpp_defaults.corlib)
  155. AddNestedTypesToNametoClassHashTable(image, typeHandle);
  156. image->nameToClassHashTable->insert(std::make_pair(MetadataCache::GetTypeNamespaceAndName(typeHandle), typeHandle));
  157. }
  158. void Image::InitNestedTypes(const Il2CppImage *image)
  159. {
  160. for (uint32_t index = 0; index < image->typeCount; index++)
  161. {
  162. Il2CppMetadataTypeHandle handle = MetadataCache::GetAssemblyTypeHandle(image, index);
  163. // don't add nested types
  164. if (MetadataCache::TypeIsNested(handle))
  165. return;
  166. AddNestedTypesToNametoClassHashTable(image, handle);
  167. }
  168. for (uint32_t index = 0; index < image->exportedTypeCount; index++)
  169. {
  170. Il2CppMetadataTypeHandle handle = MetadataCache::GetAssemblyExportedTypeHandle(image, index);
  171. // don't add nested types
  172. if (MetadataCache::TypeIsNested(handle))
  173. return;
  174. AddNestedTypesToNametoClassHashTable(image, handle);
  175. }
  176. }
  177. Il2CppClass* Image::ClassFromName(const Il2CppImage* image, const char* namespaze, const char *name)
  178. {
  179. if (!image->nameToClassHashTable)
  180. {
  181. os::FastAutoLock lock(&s_ClassFromNameMutex);
  182. if (!image->nameToClassHashTable)
  183. {
  184. image->nameToClassHashTable = new Il2CppNameToTypeHandleHashTable();
  185. for (uint32_t index = 0; index < image->typeCount; index++)
  186. {
  187. AddTypeToNametoClassHashTable(image, MetadataCache::GetAssemblyTypeHandle(image, index));
  188. }
  189. for (uint32_t index = 0; index < image->exportedTypeCount; index++)
  190. {
  191. AddTypeToNametoClassHashTable(image, MetadataCache::GetAssemblyExportedTypeHandle(image, index));
  192. }
  193. }
  194. }
  195. Il2CppNameToTypeHandleHashTable::const_iterator iter = image->nameToClassHashTable->find(std::make_pair(namespaze, name));
  196. if (iter != image->nameToClassHashTable->end())
  197. return MetadataCache::GetTypeInfoFromHandle(iter->second);
  198. return NULL;
  199. }
  200. static bool IsExported(const Il2CppClass* type)
  201. {
  202. if ((type->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_PUBLIC)
  203. {
  204. return true;
  205. }
  206. if ((type->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC)
  207. {
  208. IL2CPP_ASSERT(type->declaringType);
  209. return IsExported(type->declaringType);
  210. }
  211. return false;
  212. }
  213. void Image::GetTypes(const Il2CppImage* image, bool exportedOnly, TypeVector* target)
  214. {
  215. uint32_t typeCount = Image::GetNumTypes(image);
  216. target->reserve(typeCount);
  217. for (uint32_t sourceIndex = 0; sourceIndex < typeCount; sourceIndex++)
  218. {
  219. const Il2CppClass* type = Image::GetType(image, sourceIndex);
  220. if (strcmp(type->name, "<Module>") == 0)
  221. {
  222. continue;
  223. }
  224. if (exportedOnly && !IsExported(type))
  225. {
  226. continue;
  227. }
  228. target->push_back(type);
  229. }
  230. }
  231. Il2CppArray* Image::GetTypes(const Il2CppImage* image, bool exportedOnly)
  232. {
  233. TypeVector types;
  234. GetTypes(image, exportedOnly, &types);
  235. Il2CppArray* result = Array::New(il2cpp_defaults.monotype_class, (il2cpp_array_size_t)types.size());
  236. size_t index = 0;
  237. for (vm::TypeVector::const_iterator type = types.begin(); type != types.end(); ++type)
  238. {
  239. Il2CppReflectionType* reflectionType = Reflection::GetTypeObject(&(*type)->byval_arg);
  240. il2cpp_array_setref(result, index, reflectionType);
  241. index++;
  242. }
  243. return result;
  244. }
  245. uint32_t Image::GetNumTypes(const Il2CppImage* image)
  246. {
  247. return image->typeCount;
  248. }
  249. const Il2CppClass* Image::GetType(const Il2CppImage* image, AssemblyTypeIndex index)
  250. {
  251. IL2CPP_ASSERT(index <= std::numeric_limits<AssemblyTypeIndex>::max());
  252. return MetadataCache::GetTypeInfoFromHandle(MetadataCache::GetAssemblyTypeHandle(image, index));
  253. }
  254. static bool StringsMatch(const char* left, const char* right, bool ignoreCase)
  255. {
  256. if (!ignoreCase)
  257. {
  258. return strcmp(left, right) == 0;
  259. }
  260. else
  261. {
  262. utils::VmStringUtils::CaseInsensitiveComparer comparer;
  263. return comparer(left, right);
  264. }
  265. }
  266. static bool ClassMatches(Il2CppMetadataTypeHandle typeHandle, const char* namespaze, bool ignoreCase, const char* name)
  267. {
  268. if (MetadataCache::TypeIsNested(typeHandle))
  269. return false;
  270. std::pair<const char*, const char*> namespaceAndName = MetadataCache::GetTypeNamespaceAndName(typeHandle);
  271. return StringsMatch(name, namespaceAndName.second, ignoreCase) && StringsMatch(namespaze, namespaceAndName.first, ignoreCase);
  272. }
  273. static Il2CppClass* FindClassMatching(const Il2CppImage* image, const char* namespaze, const char *name, bool ignoreCase)
  274. {
  275. for (uint32_t i = 0; i < image->typeCount; i++)
  276. {
  277. Il2CppMetadataTypeHandle typeHandle = MetadataCache::GetAssemblyTypeHandle(image, i);
  278. if (ClassMatches(typeHandle, namespaze, ignoreCase, name))
  279. return MetadataCache::GetTypeInfoFromHandle(typeHandle);
  280. }
  281. return NULL;
  282. }
  283. static Il2CppClass* FindExportedClassMatching(const Il2CppImage* image, const char* namespaze, const char *name, bool ignoreCase)
  284. {
  285. for (uint32_t i = 0; i < image->exportedTypeCount; i++)
  286. {
  287. Il2CppMetadataTypeHandle typeHandle = MetadataCache::GetAssemblyExportedTypeHandle(image, i);
  288. if (ClassMatches(typeHandle, namespaze, ignoreCase, name))
  289. return MetadataCache::GetTypeInfoFromHandle(typeHandle);
  290. }
  291. return NULL;
  292. }
  293. static Il2CppClass* FindNestedType(Il2CppClass* klass, const char* name, bool ignoreCase)
  294. {
  295. void* iter = NULL;
  296. while (Il2CppClass* nestedType = Class::GetNestedTypes(klass, &iter))
  297. {
  298. if (StringsMatch(name, nestedType->name, ignoreCase))
  299. return nestedType;
  300. }
  301. return NULL;
  302. }
  303. Il2CppClass* Image::FromTypeNameParseInfo(const Il2CppImage* image, const TypeNameParseInfo &info, bool ignoreCase)
  304. {
  305. const char* ns = info.ns().c_str();
  306. const char* name = info.name().c_str();
  307. Il2CppClass *parent_class = FindClassMatching(image, ns, name, ignoreCase);
  308. if (parent_class == NULL)
  309. {
  310. parent_class = FindExportedClassMatching(image, ns, name, ignoreCase);
  311. if (parent_class == NULL)
  312. return NULL;
  313. }
  314. std::vector<std::string>::const_iterator it = info.nested().begin();
  315. while (it != info.nested().end())
  316. {
  317. parent_class = FindNestedType(parent_class, (*it).c_str(), ignoreCase);
  318. if (parent_class == NULL)
  319. return NULL;
  320. ++it;
  321. }
  322. return parent_class;
  323. }
  324. static baselib::ReentrantLock s_Mutex;
  325. static std::vector<Image::EmbeddedResourceData> s_CachedResourceData;
  326. static std::map<Il2CppReflectionAssembly*, void*> s_CachedMemoryMappedResourceFiles;
  327. void Image::CacheMemoryMappedResourceFile(Il2CppReflectionAssembly* assembly, void* memoryMappedFile)
  328. {
  329. os::FastAutoLock lock(&s_Mutex);
  330. s_CachedMemoryMappedResourceFiles[assembly] = memoryMappedFile;
  331. }
  332. void* Image::GetCachedMemoryMappedResourceFile(Il2CppReflectionAssembly* assembly)
  333. {
  334. os::FastAutoLock lock(&s_Mutex);
  335. std::map<Il2CppReflectionAssembly*, void*>::iterator entry = s_CachedMemoryMappedResourceFiles.find(assembly);
  336. if (entry != s_CachedMemoryMappedResourceFiles.end())
  337. return entry->second;
  338. return NULL;
  339. }
  340. void Image::CacheResourceData(EmbeddedResourceRecord record, void* data)
  341. {
  342. os::FastAutoLock lock(&s_Mutex);
  343. s_CachedResourceData.push_back(EmbeddedResourceData(record, data));
  344. }
  345. void* Image::GetCachedResourceData(const Il2CppImage* image, const std::string& name)
  346. {
  347. os::FastAutoLock lock(&s_Mutex);
  348. for (std::vector<EmbeddedResourceData>::iterator it = s_CachedResourceData.begin(); it != s_CachedResourceData.end(); ++it)
  349. {
  350. if (it->record.image == image && it->record.name == name)
  351. return it->data;
  352. }
  353. return NULL;
  354. }
  355. void Image::ClearCachedResourceData()
  356. {
  357. os::FastAutoLock lock(&s_Mutex);
  358. for (std::map<Il2CppReflectionAssembly*, void*>::iterator i = s_CachedMemoryMappedResourceFiles.begin(); i != s_CachedMemoryMappedResourceFiles.end(); ++i)
  359. utils::MemoryMappedFile::Unmap(i->second);
  360. s_CachedMemoryMappedResourceFiles.clear();
  361. s_CachedResourceData.clear();
  362. }
  363. } /* namespace vm */
  364. } /* namespace il2cpp */