RCW.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. #include "il2cpp-config.h"
  2. #include "il2cpp-object-internals.h"
  3. #include "il2cpp-class-internals.h"
  4. #include "il2cpp-tabledefs.h"
  5. #include "gc/GCHandle.h"
  6. #include "metadata/GenericMetadata.h"
  7. #include "vm/Exception.h"
  8. #include "vm/Field.h"
  9. #include "vm/GenericClass.h"
  10. #include "vm/MetadataCache.h"
  11. #include "vm/Object.h"
  12. #include "vm/PlatformInvoke.h"
  13. #include "vm/RCW.h"
  14. #include "vm/Runtime.h"
  15. #include "os/Atomic.h"
  16. #include "os/COM.h"
  17. #include "vm/Monitor.h"
  18. #include "os/Mutex.h"
  19. #include "os/WindowsRuntime.h"
  20. #include "utils/Il2CppError.h"
  21. #include "utils/Il2CppHashMap.h"
  22. #include "utils/HashUtils.h"
  23. #include "utils/StringUtils.h"
  24. #include "Baselib.h"
  25. #include "Cpp/ReentrantLock.h"
  26. const Il2CppGuid Il2CppIUnknown::IID = { 0x00000000, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 };
  27. const Il2CppGuid Il2CppISequentialStream::IID = { 0x0c733a30, 0x2a1c, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d };
  28. const Il2CppGuid Il2CppIStream::IID = { 0x0000000c, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 };
  29. const Il2CppGuid Il2CppIMarshal::IID = { 0x00000003, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 };
  30. const Il2CppGuid Il2CppIManagedObject::IID = { 0xc3fcc19e, 0xa970, 0x11d2, 0x8b, 0x5a, 0x00, 0xa0, 0xc9, 0xb7, 0xc9, 0xc4 };
  31. const Il2CppGuid Il2CppIManagedObjectHolder::IID = { 0xd4bbc1c8, 0xf5bf, 0x4647, 0x94, 0x95, 0x2e, 0x5c, 0xf, 0x20, 0xf7, 0x5d };
  32. const Il2CppGuid Il2CppIInspectable::IID = { 0xaf86e2e0, 0xb12d, 0x4c6a, 0x9c, 0x5a, 0xd7, 0xaa, 0x65, 0x10, 0x1E, 0x90 };
  33. const Il2CppGuid Il2CppIActivationFactory::IID = { 0x00000035, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 };
  34. const Il2CppGuid Il2CppIRestrictedErrorInfo::IID = { 0x82ba7092, 0x4c88, 0x427d, 0xa7, 0xbc, 0x16, 0xdd, 0x93, 0xfe, 0xb6, 0x7e };
  35. const Il2CppGuid Il2CppILanguageExceptionErrorInfo::IID = { 0x04a2dbf3, 0xdf83, 0x116c, 0x09, 0x46, 0x08, 0x12, 0xab, 0xf6, 0xe0, 0x7d };
  36. const Il2CppGuid Il2CppIAgileObject::IID = { 0x94ea2b94, 0xe9cc, 0x49e0, 0xc0, 0xff, 0xee, 0x64, 0xca, 0x8f, 0x5b, 0x90 };
  37. const Il2CppGuid Il2CppIWeakReference::IID = { 0x00000037, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 };
  38. const Il2CppGuid Il2CppIWeakReferenceSource::IID = { 0x00000038, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 };
  39. namespace il2cpp
  40. {
  41. namespace vm
  42. {
  43. typedef Il2CppHashMap<Il2CppIUnknown*, /* Weak GC Handle */ uint32_t, il2cpp::utils::PointerHash<Il2CppIUnknown> > RCWCache;
  44. static baselib::ReentrantLock s_RCWCacheMutex;
  45. static RCWCache s_RCWCache;
  46. void RCW::Register(Il2CppComObject* rcw)
  47. {
  48. os::FastAutoLock lock(&s_RCWCacheMutex);
  49. rcw->refCount = 1;
  50. auto weakRef = gc::GCHandle::NewWeakref(rcw, false);
  51. vm::Exception::RaiseIfError(weakRef.GetError());
  52. const bool inserted = s_RCWCache.insert(std::make_pair(rcw->identity, weakRef.Get())).second;
  53. Assert(inserted);
  54. }
  55. static inline Il2CppIUnknown* GetIdentity(Il2CppIUnknown* unknown)
  56. {
  57. Il2CppIUnknown* identity;
  58. il2cpp_hresult_t hr = unknown->QueryInterface(Il2CppIUnknown::IID, reinterpret_cast<void**>(&identity));
  59. vm::Exception::RaiseIfFailed(hr, true);
  60. IL2CPP_ASSERT(identity);
  61. return identity;
  62. }
  63. // Shameless comment copycat from .NET Native (https://github.com/dotnet/corert/blob/374c3d47992a7c444ec7d1dfe94b1780de942a55/src/System.Private.Interop/src/Shared/McgComHelpers.cs#L557):
  64. // 1. Prefer using the class returned from GetRuntimeClassName
  65. // 2. Otherwise use the class (if there) in the signature
  66. // 3. Out of options - create Il2CppComObject
  67. static inline Il2CppClass* GetClassForRCW(Il2CppIInspectable* inspectable, Il2CppClass* fallbackClass)
  68. {
  69. Il2CppHString className;
  70. il2cpp_hresult_t hr = inspectable->GetRuntimeClassName(&className);
  71. if (IL2CPP_HR_FAILED(hr) || className == NULL)
  72. return fallbackClass;
  73. uint32_t classNameLength;
  74. auto classNamePtr = os::WindowsRuntime::GetHStringBuffer(className, &classNameLength);
  75. vm::Exception::RaiseIfError(classNamePtr.GetError());
  76. std::string classNameUtf8 = utils::StringUtils::Utf16ToUtf8(classNamePtr.Get(), classNameLength);
  77. os::WindowsRuntime::DeleteHString(className);
  78. Il2CppClass* rcwClass = MetadataCache::GetWindowsRuntimeClass(classNameUtf8.c_str());
  79. return rcwClass != NULL ? rcwClass : fallbackClass;
  80. }
  81. static inline Il2CppClass* GetClassForRCW(Il2CppIUnknown* unknown, Il2CppClass* fallbackClass)
  82. {
  83. Il2CppIInspectable* inspectable;
  84. il2cpp_hresult_t hr = unknown->QueryInterface(Il2CppIInspectable::IID, reinterpret_cast<void**>(&inspectable));
  85. if (IL2CPP_HR_FAILED(hr))
  86. return fallbackClass;
  87. Il2CppClass* result = GetClassForRCW(inspectable, fallbackClass);
  88. inspectable->Release();
  89. return result;
  90. }
  91. Il2CppObject* ReboxIReference(Il2CppIUnknown* comObject, Il2CppClass* objectClass);
  92. Il2CppObject* ReboxKeyValuePair(Il2CppIUnknown* comObject, Il2CppClass* keyValuePairGenericInstance);
  93. Il2CppObject* ReboxUri(Il2CppIUnknown* comObject);
  94. Il2CppObject* ReboxIfBoxed(Il2CppIUnknown* comObject, Il2CppClass* objectClass)
  95. {
  96. if (strcmp(objectClass->namespaze, "Windows.Foundation") == 0)
  97. {
  98. if (strcmp(objectClass->name, "IReference`1") == 0 || strcmp(objectClass->name, "IReferenceArray`1") == 0)
  99. return ReboxIReference(comObject, objectClass);
  100. }
  101. else if (strcmp(objectClass->namespaze, "System.Collections.Generic") == 0 && strcmp(objectClass->name, "KeyValuePair`2") == 0)
  102. {
  103. return ReboxKeyValuePair(comObject, objectClass);
  104. }
  105. else if (objectClass == il2cpp_defaults.system_uri_class)
  106. {
  107. return ReboxUri(comObject);
  108. }
  109. return NULL;
  110. }
  111. Il2CppObject* ReboxIReference(Il2CppIUnknown* comObject, Il2CppClass* objectClass)
  112. {
  113. Class::Init(objectClass);
  114. // Sanity checks
  115. IL2CPP_ASSERT(Class::IsInflated(objectClass));
  116. IL2CPP_ASSERT(objectClass->vtable_count == 1); // IReference`1<T> only has get_Value method
  117. const MethodInfo* getValueMethod = objectClass->vtable[0].method;
  118. IL2CPP_ASSERT(strcmp(getValueMethod->name, "get_Value") == 0);
  119. // We don't really want to allocate it on the GC heap for this little invocation
  120. Il2CppComObject fakeRcw;
  121. memset(&fakeRcw, 0, sizeof(fakeRcw));
  122. fakeRcw.klass = objectClass;
  123. fakeRcw.identity = comObject;
  124. Il2CppException* exception = NULL;
  125. Il2CppObject* reboxed = Runtime::Invoke(getValueMethod, &fakeRcw, NULL, &exception);
  126. if (exception != NULL)
  127. Exception::Raise(exception);
  128. return reboxed;
  129. }
  130. Il2CppObject* ReboxKeyValuePair(Il2CppIUnknown* comObject, Il2CppClass* keyValuePairGenericInstance)
  131. {
  132. Class::Init(keyValuePairGenericInstance);
  133. // Sanity checks
  134. IL2CPP_ASSERT(Class::IsInflated(keyValuePairGenericInstance));
  135. IL2CPP_ASSERT(il2cpp_defaults.ikey_value_pair_class != NULL);
  136. // Retrieve Windows.Foundation.Collections.IKeyValuePair`1<K, V> generic instance
  137. Il2CppGenericClass* iKeyValuePairGenericClass = metadata::GenericMetadata::GetGenericClass(il2cpp_defaults.ikey_value_pair_class, keyValuePairGenericInstance->generic_class->context.class_inst);
  138. Il2CppClass* iKeyValuePairGenericInstance = GenericClass::GetClass(iKeyValuePairGenericClass);
  139. Class::Init(iKeyValuePairGenericInstance);
  140. IL2CPP_ASSERT(iKeyValuePairGenericInstance->vtable_count == 2);
  141. const MethodInfo* getKeyMethod = iKeyValuePairGenericInstance->vtable[0].method;
  142. IL2CPP_ASSERT(strcmp(getKeyMethod->name, "get_Key") == 0);
  143. const MethodInfo* getValueMethod = iKeyValuePairGenericInstance->vtable[1].method;
  144. IL2CPP_ASSERT(strcmp(getValueMethod->name, "get_Value") == 0);
  145. Il2CppComObject fakeRcw;
  146. memset(&fakeRcw, 0, sizeof(fakeRcw));
  147. fakeRcw.klass = il2cpp_defaults.il2cpp_com_object_class;
  148. fakeRcw.identity = comObject;
  149. // Create new boxed key value pair
  150. Il2CppObject* reboxed = Object::New(keyValuePairGenericInstance);
  151. for (uint16_t i = 0; i < 2; i++)
  152. {
  153. const MethodInfo* methodToInvoke = NULL;
  154. const FieldInfo& field = keyValuePairGenericInstance->fields[i];
  155. // Figure out which getter to call
  156. if (strcmp(field.name, "key") == 0)
  157. {
  158. methodToInvoke = getKeyMethod;
  159. }
  160. else if (strcmp(field.name, "value") == 0)
  161. {
  162. methodToInvoke = getValueMethod;
  163. }
  164. // Call the getter
  165. Il2CppException* exception = NULL;
  166. Il2CppObject* fieldValue = Runtime::Invoke(methodToInvoke, &fakeRcw, NULL, &exception);
  167. if (exception != NULL)
  168. Exception::Raise(exception);
  169. // Set the field in our reboxed key value pair instance
  170. if (Class::FromIl2CppType(field.type)->byval_arg.valuetype)
  171. {
  172. Field::SetValue(reboxed, &field, Object::Unbox(fieldValue));
  173. }
  174. else
  175. {
  176. Field::SetValue(reboxed, &field, fieldValue);
  177. }
  178. }
  179. return reboxed;
  180. }
  181. Il2CppObject* ReboxUri(Il2CppIUnknown* comObject)
  182. {
  183. Il2CppClass* systemUriClass = il2cpp_defaults.system_uri_class;
  184. Il2CppClass* iUriRuntimeClassClass = il2cpp_defaults.windows_foundation_iuri_runtime_class_class;
  185. Class::Init(systemUriClass);
  186. Class::Init(iUriRuntimeClassClass);
  187. const int kGetRawUriMethodIndex = 10; // IUriRuntimeClass::get_RawUri
  188. IL2CPP_ASSERT(iUriRuntimeClassClass->vtable_count > kGetRawUriMethodIndex);
  189. VirtualInvokeData getRawUriInvokeData = iUriRuntimeClassClass->vtable[kGetRawUriMethodIndex];
  190. IL2CPP_ASSERT(strcmp(getRawUriInvokeData.method->name, "get_RawUri") == 0);
  191. Il2CppComObject fakeRcw;
  192. memset(&fakeRcw, 0, sizeof(fakeRcw));
  193. fakeRcw.klass = il2cpp_defaults.il2cpp_com_object_class;
  194. fakeRcw.identity = comObject;
  195. Il2CppObject* rawUri = Runtime::InvokeWithThrow(getRawUriInvokeData.method, &fakeRcw, NULL);
  196. const MethodInfo* uriConstructor = NULL;
  197. uint16_t uriMethodCount = systemUriClass->method_count;
  198. for (uint16_t i = 0; i < uriMethodCount; i++)
  199. {
  200. const MethodInfo* method = systemUriClass->methods[i];
  201. if (strcmp(method->name, ".ctor") == 0 && method->parameters_count == 1 && method->parameters[0]->type == IL2CPP_TYPE_STRING)
  202. {
  203. uriConstructor = method;
  204. break;
  205. }
  206. }
  207. IL2CPP_ASSERT(uriConstructor);
  208. Il2CppObject* reboxedUri = Object::New(systemUriClass);
  209. void* constructorArgs[1] = { rawUri };
  210. Runtime::InvokeWithThrow(uriConstructor, reboxedUri, constructorArgs);
  211. return reboxedUri;
  212. }
  213. template<typename T, bool isSealedClassInstance>
  214. static inline Il2CppObject* GetOrCreateRCW(T* comObject, Il2CppClass* objectClass)
  215. {
  216. IL2CPP_ASSERT(comObject != NULL);
  217. if (!isSealedClassInstance)
  218. {
  219. // 1. Check if comObject is actually our COM Callable Wrapper
  220. Il2CppIManagedObjectHolder* managedHolder;
  221. il2cpp_hresult_t hr = comObject->QueryInterface(Il2CppIManagedObjectHolder::IID, reinterpret_cast<void**>(&managedHolder));
  222. if (IL2CPP_HR_SUCCEEDED(hr))
  223. {
  224. Il2CppObject* instance = managedHolder->GetManagedObject();
  225. managedHolder->Release();
  226. IL2CPP_ASSERT(instance);
  227. return instance;
  228. }
  229. }
  230. Il2CppIUnknown* identity = GetIdentity(comObject);
  231. // 2. Try to find it in RCW cache
  232. os::FastAutoLock lock(&s_RCWCacheMutex);
  233. RCWCache::iterator iter = s_RCWCache.find(identity);
  234. if (iter != s_RCWCache.end())
  235. {
  236. Il2CppComObject* obj = static_cast<Il2CppComObject*>(gc::GCHandle::GetTarget(iter->second));
  237. if (obj != NULL)
  238. {
  239. // Make sure the RCW isn't dead. If increment returns 1, it means
  240. // that the ref count had previous reached 0 and was released
  241. if (os::Atomic::Increment(&obj->refCount) > 1)
  242. {
  243. identity->Release();
  244. identity = NULL;
  245. return obj;
  246. }
  247. }
  248. // The RCW was already queued for finalization or destroyed by ref count reaching 0.
  249. // Erase it from the cache and let us create a new one.
  250. s_RCWCache.erase(iter);
  251. }
  252. // 3. Figure out the concrete RCW class
  253. if (!isSealedClassInstance)
  254. {
  255. Il2CppClass* fallbackClass = objectClass;
  256. objectClass = GetClassForRCW(comObject, fallbackClass);
  257. // If object class is one of the blessed unboxable classes,
  258. // unbox the object from its windows runtime representation,
  259. // unmarshal it, box it to Il2CppObject and return it
  260. //
  261. // Current list of unboxable classes:
  262. // Windows.Foundation.IReference`1<T>
  263. // Windows.Foundation.IReferenceArray`1<T>
  264. // System.Collections.Generic.KeyValuePair`2<K, V>
  265. // System.Uri
  266. Il2CppObject* reboxed = ReboxIfBoxed(comObject, objectClass);
  267. if (reboxed != NULL)
  268. return reboxed;
  269. if (objectClass->byval_arg.type != IL2CPP_TYPE_CLASS ||
  270. objectClass->flags & TYPE_ATTRIBUTE_INTERFACE ||
  271. objectClass->is_generic)
  272. {
  273. // We must be able to instantiate the type. If we can't, fallback to a caller passed in type
  274. objectClass = fallbackClass;
  275. }
  276. }
  277. IL2CPP_ASSERT(Class::HasParent(objectClass, il2cpp_defaults.il2cpp_com_object_class));
  278. // 4. Create RCW object
  279. Il2CppComObject* rcw = static_cast<Il2CppComObject*>(Object::New(objectClass));
  280. rcw->identity = identity;
  281. rcw->refCount = 1;
  282. // 5. Insert it into the cache
  283. auto weakRef = gc::GCHandle::NewWeakref(rcw, false);
  284. vm::Exception::RaiseIfError(weakRef.GetError());
  285. const bool inserted = s_RCWCache.insert(std::make_pair(identity, weakRef.Get())).second;
  286. Assert(inserted);
  287. return rcw;
  288. }
  289. Il2CppObject* RCW::GetOrCreateFromIUnknown(Il2CppIUnknown* unknown, Il2CppClass* fallbackClass)
  290. {
  291. return GetOrCreateRCW<Il2CppIUnknown, false>(unknown, fallbackClass);
  292. }
  293. Il2CppObject* RCW::GetOrCreateFromIInspectable(Il2CppIInspectable* inspectable, Il2CppClass* fallbackClass)
  294. {
  295. return GetOrCreateRCW<Il2CppIInspectable, false>(inspectable, fallbackClass);
  296. }
  297. Il2CppObject* RCW::GetOrCreateForSealedClass(Il2CppIUnknown* unknown, Il2CppClass* objectClass)
  298. {
  299. return GetOrCreateRCW<Il2CppIUnknown, true>(unknown, objectClass);
  300. }
  301. void RCW::Cleanup(Il2CppComObject* rcw)
  302. {
  303. if (rcw->klass->is_import_or_windows_runtime)
  304. {
  305. os::FastAutoLock lock(&s_RCWCacheMutex);
  306. RCWCache::iterator iter = s_RCWCache.find(rcw->identity);
  307. // It is possible for us to not find object in the cache if two RCWs for the same IUnknown get
  308. // finalized in a row: then, the first finalizer will remove the NULL object, and the second one
  309. // will not find it.
  310. if (iter != s_RCWCache.end())
  311. {
  312. Il2CppObject* obj = gc::GCHandle::GetTarget(iter->second);
  313. // If it's null, it means that the cache contains our object
  314. // but the weak GC handle has been invalidated by the GC already
  315. // If it's equal to our object, it means that RCW::Cleanup was
  316. // called manually, and we should also delete it from the cache
  317. // Otherwise, it's a different object. It means that we have already
  318. // created a new RCW in place of this one during the time
  319. // it had been queued for finalization
  320. if (obj == NULL || obj == rcw)
  321. s_RCWCache.erase(iter);
  322. }
  323. }
  324. int32_t shortCacheSize = rcw->qiShortCacheSize;
  325. for (int32_t i = 0; i < shortCacheSize; i++)
  326. rcw->qiShortCache[i].qiResult->Release();
  327. int32_t longCacheSize = rcw->qiLongCacheSize;
  328. if (longCacheSize > 0)
  329. {
  330. for (int32_t i = 0; i < longCacheSize; i++)
  331. rcw->qiLongCache[i].qiResult->Release();
  332. IL2CPP_FREE(rcw->qiLongCache);
  333. }
  334. }
  335. Il2CppIUnknown* RCW::QueryInterfaceCached(Il2CppComObject* rcw, const Il2CppGuid& iid)
  336. {
  337. MonitorHolder monitorHolder(rcw);
  338. int32_t shortCacheSize = rcw->qiShortCacheSize;
  339. for (int32_t i = 0; i < shortCacheSize; i++)
  340. {
  341. const Il2CppGuid* queriedInterface = rcw->qiShortCache[i].iid;
  342. if (queriedInterface == &iid)
  343. return rcw->qiShortCache[i].qiResult;
  344. }
  345. int32_t longCacheSize = rcw->qiLongCacheSize;
  346. for (int32_t i = 0; i < longCacheSize; i++)
  347. {
  348. const Il2CppGuid* queriedInterface = rcw->qiLongCache[i].iid;
  349. if (queriedInterface == &iid)
  350. return rcw->qiLongCache[i].qiResult;
  351. }
  352. return NULL;
  353. }
  354. bool RCW::CacheQueriedInterface(Il2CppComObject* rcw, const Il2CppGuid& iid, Il2CppIUnknown* queriedInterface)
  355. {
  356. MonitorHolder monitorHolder(rcw);
  357. QICache cache = { &iid, queriedInterface };
  358. // We need to rescan caches in case another thread got to cache it first
  359. int32_t shortCacheSize = rcw->qiShortCacheSize;
  360. IL2CPP_ASSERT(shortCacheSize <= IL2CPP_ARRAY_SIZE(rcw->qiShortCache));
  361. for (int32_t i = 0; i < shortCacheSize; i++)
  362. {
  363. const Il2CppGuid* queriedInterface = rcw->qiShortCache[i].iid;
  364. if (queriedInterface == &iid)
  365. return false;
  366. }
  367. if (shortCacheSize == IL2CPP_ARRAY_SIZE(rcw->qiShortCache))
  368. {
  369. // We only need to check long cache if short cache is full
  370. int32_t longCacheSize = rcw->qiLongCacheSize;
  371. for (int32_t i = 0; i < longCacheSize; i++)
  372. {
  373. const Il2CppGuid* queriedInterface = rcw->qiLongCache[i].iid;
  374. if (queriedInterface == &iid)
  375. return false;
  376. }
  377. }
  378. else
  379. {
  380. rcw->qiShortCache[shortCacheSize] = cache;
  381. rcw->qiShortCacheSize = shortCacheSize + 1;
  382. return true;
  383. }
  384. int32_t longCacheSize = rcw->qiLongCacheSize;
  385. int32_t longCacheCapacity = rcw->qiLongCacheCapacity;
  386. IL2CPP_ASSERT(longCacheSize <= longCacheCapacity);
  387. if (longCacheSize == longCacheCapacity)
  388. {
  389. longCacheCapacity *= 2;
  390. rcw->qiLongCache = static_cast<QICache*>(IL2CPP_REALLOC(rcw->qiLongCache, sizeof(QICache) * longCacheCapacity));
  391. rcw->qiLongCacheCapacity = longCacheCapacity;
  392. }
  393. rcw->qiLongCache[longCacheSize] = cache;
  394. rcw->qiLongCacheSize = longCacheSize + 1;
  395. return true;
  396. }
  397. const VirtualInvokeData* RCW::GetComInterfaceInvokeData(Il2CppClass* queriedInterface, const Il2CppClass* targetInterface, Il2CppMethodSlot slot)
  398. {
  399. Class::Init(queriedInterface);
  400. uint16_t vtableCount = queriedInterface->vtable_count;
  401. if (targetInterface->generic_class != NULL)
  402. {
  403. if (Class::IsGenericClassAssignableFrom(targetInterface, queriedInterface))
  404. return NULL;
  405. const Il2CppRuntimeInterfaceOffsetPair* interfaceOffsets = queriedInterface->interfaceOffsets;
  406. uint16_t interfaceOffsetsCount = queriedInterface->interface_offsets_count;
  407. for (uint16_t i = 0; i < interfaceOffsetsCount; i++)
  408. {
  409. if (Class::IsGenericClassAssignableFrom(targetInterface, interfaceOffsets[i].interfaceType))
  410. {
  411. Il2CppMethodSlot slotWithOffset = interfaceOffsets[i].offset + slot;
  412. if (slotWithOffset < vtableCount)
  413. return &queriedInterface->vtable[slotWithOffset];
  414. }
  415. }
  416. }
  417. else
  418. {
  419. const Il2CppRuntimeInterfaceOffsetPair* interfaceOffsets = queriedInterface->interfaceOffsets;
  420. uint16_t interfaceOffsetsCount = queriedInterface->interface_offsets_count;
  421. for (uint16_t i = 0; i < interfaceOffsetsCount; ++i)
  422. {
  423. if (interfaceOffsets[i].interfaceType == targetInterface)
  424. {
  425. Il2CppMethodSlot slotWithOffset = interfaceOffsets[i].offset + slot;
  426. if (slotWithOffset < vtableCount)
  427. return &queriedInterface->vtable[slotWithOffset];
  428. }
  429. }
  430. }
  431. Il2CppClass* const* implementedInterfaces = queriedInterface->implementedInterfaces;
  432. uint16_t implementedInterfacesCount = queriedInterface->interfaces_count;
  433. for (uint16_t i = 0; i < implementedInterfacesCount; i++)
  434. {
  435. Il2CppClass* implementedInterface = implementedInterfaces[i];
  436. const VirtualInvokeData* invokeData = GetComInterfaceInvokeData(implementedInterface, targetInterface, slot);
  437. if (invokeData != NULL)
  438. return invokeData;
  439. }
  440. return NULL;
  441. }
  442. const VirtualInvokeData* RCW::GetComInterfaceInvokeData(Il2CppComObject* rcw, const Il2CppClass* targetInterface, Il2CppMethodSlot slot)
  443. {
  444. uint16_t vtableCount = targetInterface->vtable_count;
  445. if (slot < vtableCount)
  446. {
  447. const Il2CppInteropData* itfInteropData = targetInterface->interopData;
  448. if (itfInteropData != NULL)
  449. {
  450. const Il2CppGuid* itfGuid = itfInteropData->guid;
  451. if (itfGuid != NULL)
  452. {
  453. // Try querying for the interface we were asked
  454. if (RCW::QueryInterfaceNoAddRef<false>(rcw, *itfGuid) != NULL)
  455. return &targetInterface->vtable[slot];
  456. }
  457. }
  458. }
  459. if (targetInterface->is_import_or_windows_runtime)
  460. return NULL;
  461. // For projected interfaces, we look in the cache for compatible interface in order to handle these scenarios:
  462. // * Covariable/Contravariance. For instance, we should be able to invoke IReadOnlyList<object> methods on IReadOnlyList<string>, even though if QI fails for IVectorView<object>
  463. // * Inherited interfaces on CLR but not Windows Runtime side. For instance, IEnumerable<T> implements IEnumerable but IIterable<T> does not implement IBindableIterable
  464. MonitorHolder monitorHolder(rcw);
  465. int32_t shortCacheSize = rcw->qiShortCacheSize;
  466. for (int32_t i = 0; i < shortCacheSize; i++)
  467. {
  468. Il2CppClass* queriedInterface = vm::MetadataCache::GetClassForGuid(rcw->qiShortCache[i].iid);
  469. if (queriedInterface != NULL)
  470. {
  471. const VirtualInvokeData* invokeData = GetComInterfaceInvokeData(queriedInterface, targetInterface, slot);
  472. if (invokeData != NULL)
  473. return invokeData;
  474. }
  475. }
  476. int32_t longCacheSize = rcw->qiLongCacheSize;
  477. for (int32_t i = 0; i < longCacheSize; i++)
  478. {
  479. Il2CppClass* queriedInterface = vm::MetadataCache::GetClassForGuid(rcw->qiLongCache[i].iid);
  480. if (queriedInterface != NULL)
  481. {
  482. const VirtualInvokeData* invokeData = GetComInterfaceInvokeData(queriedInterface, targetInterface, slot);
  483. if (invokeData != NULL)
  484. return invokeData;
  485. }
  486. }
  487. if (slot < vtableCount)
  488. return &targetInterface->vtable[slot];
  489. return NULL;
  490. }
  491. } /* namespace vm */
  492. } /* namespace il2cpp */