PlatformInvoke.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include "il2cpp-config.h"
  2. #include "il2cpp-object-internals.h"
  3. #include "il2cpp-class-internals.h"
  4. #include "PlatformInvoke.h"
  5. #include "Exception.h"
  6. #include "MetadataCache.h"
  7. #include "Method.h"
  8. #include "Object.h"
  9. #include "Runtime.h"
  10. #include "Type.h"
  11. #include "gc/WriteBarrier.h"
  12. #include "os/LibraryLoader.h"
  13. #include "os/MarshalStringAlloc.h"
  14. #include "utils/Memory.h"
  15. #include "utils/StringUtils.h"
  16. #include "vm-utils/VmStringUtils.h"
  17. #include <stdint.h>
  18. #include <algorithm>
  19. namespace il2cpp
  20. {
  21. namespace vm
  22. {
  23. void PlatformInvoke::SetFindPluginCallback(Il2CppSetFindPlugInCallback method)
  24. {
  25. os::LibraryLoader::SetFindPluginCallback(method);
  26. }
  27. Il2CppMethodPointer PlatformInvoke::Resolve(const PInvokeArguments& pinvokeArgs)
  28. {
  29. // Before resolving a P/Invoke, check against a hardcoded list of "known P/Invokes" that is different for every platform.
  30. // This bit solves several different problems we have when P/Invoking into native system libraries from mscorlib.dll.
  31. // Some platforms, like UWP, just don't allow you to load to load system libraries at runtime dynamically.
  32. // On other platforms (THEY SHALL NOT BE NAMED :O), while the functions that mscorlib.dll wants to P/Invoke into exist,
  33. // They exist in different system libraries than it is said in the DllImport attribute.
  34. Il2CppMethodPointer function = os::LibraryLoader::GetHardcodedPInvokeDependencyFunctionPointer(pinvokeArgs.moduleName, pinvokeArgs.entryPoint, pinvokeArgs.charSet);
  35. if (function != NULL)
  36. return function;
  37. Baselib_DynamicLibrary_Handle dynamicLibrary = Baselib_DynamicLibrary_Handle_Invalid;
  38. std::string detailedLoadError;
  39. if (utils::VmStringUtils::CaseSensitiveEquals(il2cpp::utils::StringUtils::NativeStringToUtf8(pinvokeArgs.moduleName.Str()).c_str(), "__InternalDynamic"))
  40. dynamicLibrary = os::LibraryLoader::LoadDynamicLibrary(il2cpp::utils::StringView<Il2CppNativeChar>::Empty(), detailedLoadError);
  41. else
  42. dynamicLibrary = os::LibraryLoader::LoadDynamicLibrary(pinvokeArgs.moduleName, detailedLoadError);
  43. if (dynamicLibrary == Baselib_DynamicLibrary_Handle_Invalid)
  44. {
  45. std::string message;
  46. message += "Unable to load DLL '";
  47. message += il2cpp::utils::StringUtils::NativeStringToUtf8(pinvokeArgs.moduleName.Str());
  48. message += "'. Tried the load the following dynamic libraries: ";
  49. message += detailedLoadError;
  50. Exception::Raise(Exception::GetDllNotFoundException(message.c_str()));
  51. }
  52. std::string detailedGetFunctionError;
  53. function = os::LibraryLoader::GetFunctionPointer(dynamicLibrary, pinvokeArgs, detailedGetFunctionError);
  54. if (function == NULL)
  55. {
  56. std::string message;
  57. message += "Unable to find an entry point named '";
  58. message += pinvokeArgs.entryPoint.Str();
  59. message += "' in '";
  60. message += il2cpp::utils::StringUtils::NativeStringToUtf8(pinvokeArgs.moduleName.Str());
  61. message += "'. Tried the following entry points: ";
  62. message += detailedGetFunctionError;
  63. Exception::Raise(Exception::GetEntryPointNotFoundException(message.c_str()));
  64. }
  65. return function;
  66. }
  67. void PlatformInvoke::MarshalFree(void* ptr)
  68. {
  69. if (ptr != NULL)
  70. MarshalAlloc::Free(ptr);
  71. }
  72. char* PlatformInvoke::MarshalCSharpStringToCppString(Il2CppString* managedString)
  73. {
  74. if (managedString == NULL)
  75. return NULL;
  76. std::string utf8String = utils::StringUtils::Utf16ToUtf8(managedString->chars);
  77. char* nativeString = MarshalAllocateStringBuffer<char>(utf8String.size() + 1);
  78. strcpy(nativeString, utf8String.c_str());
  79. return nativeString;
  80. }
  81. void PlatformInvoke::MarshalCSharpStringToCppStringFixed(Il2CppString* managedString, char* buffer, int numberOfCharacters)
  82. {
  83. if (managedString == NULL)
  84. {
  85. *buffer = '\0';
  86. }
  87. else
  88. {
  89. std::string utf8String = utils::StringUtils::Utf16ToUtf8(managedString->chars, numberOfCharacters - 1);
  90. strcpy(buffer, utf8String.c_str());
  91. }
  92. }
  93. Il2CppChar* PlatformInvoke::MarshalCSharpStringToCppWString(Il2CppString* managedString)
  94. {
  95. if (managedString == NULL)
  96. return NULL;
  97. int32_t stringLength = utils::StringUtils::GetLength(managedString);
  98. Il2CppChar* nativeString = MarshalAllocateStringBuffer<Il2CppChar>(stringLength + 1);
  99. for (int32_t i = 0; i < managedString->length; ++i)
  100. nativeString[i] = managedString->chars[i];
  101. nativeString[managedString->length] = '\0';
  102. return nativeString;
  103. }
  104. void PlatformInvoke::MarshalCSharpStringToCppWStringFixed(Il2CppString* managedString, Il2CppChar* buffer, int numberOfCharacters)
  105. {
  106. if (managedString == NULL)
  107. {
  108. *buffer = '\0';
  109. }
  110. else
  111. {
  112. int32_t stringLength = std::min(utils::StringUtils::GetLength(managedString), numberOfCharacters - 1);
  113. for (int32_t i = 0; i < stringLength; ++i)
  114. buffer[i] = managedString->chars[i];
  115. buffer[stringLength] = '\0';
  116. }
  117. }
  118. il2cpp_hresult_t PlatformInvoke::MarshalCSharpStringToCppBStringNoThrow(Il2CppString* managedString, Il2CppChar** bstr)
  119. {
  120. IL2CPP_ASSERT(bstr);
  121. if (managedString == NULL)
  122. {
  123. *bstr = NULL;
  124. return IL2CPP_S_OK;
  125. }
  126. int32_t stringLength = utils::StringUtils::GetLength(managedString);
  127. Il2CppChar* stringChars = utils::StringUtils::GetChars(managedString);
  128. return os::MarshalStringAlloc::AllocateBStringLength(stringChars, stringLength, bstr);
  129. }
  130. Il2CppChar* PlatformInvoke::MarshalCSharpStringToCppBString(Il2CppString* managedString)
  131. {
  132. Il2CppChar* bstr;
  133. const il2cpp_hresult_t hr = MarshalCSharpStringToCppBStringNoThrow(managedString, &bstr);
  134. vm::Exception::RaiseIfFailed(hr, true);
  135. return bstr;
  136. }
  137. Il2CppString* PlatformInvoke::MarshalCppStringToCSharpStringResult(const char* value)
  138. {
  139. if (value == NULL)
  140. return NULL;
  141. return String::New(value);
  142. }
  143. Il2CppString* PlatformInvoke::MarshalCppWStringToCSharpStringResult(const Il2CppChar* value)
  144. {
  145. if (value == NULL)
  146. return NULL;
  147. return String::NewUtf16(value, (int32_t)utils::StringUtils::StrLen(value));
  148. }
  149. Il2CppString* PlatformInvoke::MarshalCppBStringToCSharpStringResult(const Il2CppChar* value)
  150. {
  151. if (value == NULL)
  152. return NULL;
  153. int32_t length;
  154. const il2cpp_hresult_t hr = os::MarshalStringAlloc::GetBStringLength(value, &length);
  155. vm::Exception::RaiseIfFailed(hr, true);
  156. return String::NewUtf16(value, length);
  157. }
  158. void PlatformInvoke::MarshalFreeBString(Il2CppChar* value)
  159. {
  160. const il2cpp_hresult_t hr = os::MarshalStringAlloc::FreeBString(value);
  161. vm::Exception::RaiseIfFailed(hr, true);
  162. }
  163. char* PlatformInvoke::MarshalEmptyStringBuilder(Il2CppStringBuilder* stringBuilder, size_t& stringLength, std::vector<std::string>& utf8Chunks, std::vector<Il2CppStringBuilder*>& builders)
  164. {
  165. if (stringBuilder == NULL)
  166. return NULL;
  167. stringLength = 0;
  168. Il2CppStringBuilder* currentBuilder = stringBuilder;
  169. while (true)
  170. {
  171. if (currentBuilder == NULL)
  172. break;
  173. const Il2CppChar *str = (Il2CppChar*)il2cpp::vm::Array::GetFirstElementAddress(currentBuilder->chunkChars);
  174. std::string utf8String = utils::StringUtils::Utf16ToUtf8(str, (int)currentBuilder->chunkChars->max_length);
  175. utf8Chunks.push_back(utf8String);
  176. builders.push_back(currentBuilder);
  177. size_t lenToCount = std::max((size_t)currentBuilder->chunkChars->max_length, utf8String.size());
  178. stringLength += lenToCount;
  179. currentBuilder = currentBuilder->chunkPrevious;
  180. }
  181. char* nativeString = MarshalAllocateStringBuffer<char>(stringLength + 1);
  182. // We need to zero out the memory because the chunkChar array lengh may have been larger than the chunkLength
  183. // and when this happens we'll have a utf8String that is smaller than the the nativeString we allocated. When we go to copy the
  184. // chunk utf8String into the nativeString it won't fill everything and we can end up with w/e junk value was in that memory before
  185. memset(nativeString, 0, sizeof(char) * (stringLength + 1));
  186. return nativeString;
  187. }
  188. char* PlatformInvoke::MarshalEmptyStringBuilder(Il2CppStringBuilder* stringBuilder)
  189. {
  190. size_t sizeLength;
  191. std::vector<std::string> utf8Chunks;
  192. std::vector<Il2CppStringBuilder*> builders;
  193. return MarshalEmptyStringBuilder(stringBuilder, sizeLength, utf8Chunks, builders);
  194. }
  195. char* PlatformInvoke::MarshalStringBuilder(Il2CppStringBuilder* stringBuilder)
  196. {
  197. if (stringBuilder == NULL)
  198. return NULL;
  199. size_t stringLength;
  200. std::vector<std::string> utf8Chunks;
  201. std::vector<Il2CppStringBuilder*> builders;
  202. char* nativeString = MarshalEmptyStringBuilder(stringBuilder, stringLength, utf8Chunks, builders);
  203. if (stringLength > 0)
  204. {
  205. int offsetAdjustment = 0;
  206. for (int i = (int)utf8Chunks.size() - 1; i >= 0; i--)
  207. {
  208. std::string utf8String = utf8Chunks[i];
  209. const char* utf8CString = utf8String.c_str();
  210. memcpy(nativeString + builders[i]->chunkOffset + offsetAdjustment, utf8CString, (int)utf8String.size());
  211. offsetAdjustment += (int)utf8String.size() - builders[i]->chunkLength;
  212. }
  213. }
  214. return nativeString;
  215. }
  216. Il2CppChar* PlatformInvoke::MarshalEmptyWStringBuilder(Il2CppStringBuilder* stringBuilder, size_t& stringLength)
  217. {
  218. if (stringBuilder == NULL)
  219. return NULL;
  220. stringLength = 0;
  221. Il2CppStringBuilder* currentBuilder = stringBuilder;
  222. while (true)
  223. {
  224. if (currentBuilder == NULL)
  225. break;
  226. stringLength += (size_t)currentBuilder->chunkChars->max_length;
  227. currentBuilder = currentBuilder->chunkPrevious;
  228. }
  229. return MarshalAllocateStringBuffer<Il2CppChar>(stringLength + 1);
  230. }
  231. Il2CppChar* PlatformInvoke::MarshalEmptyWStringBuilder(Il2CppStringBuilder* stringBuilder)
  232. {
  233. size_t stringLength;
  234. return MarshalEmptyWStringBuilder(stringBuilder, stringLength);
  235. }
  236. Il2CppChar* PlatformInvoke::MarshalWStringBuilder(Il2CppStringBuilder* stringBuilder)
  237. {
  238. if (stringBuilder == NULL)
  239. return NULL;
  240. size_t stringLength;
  241. Il2CppChar* nativeString = MarshalEmptyWStringBuilder(stringBuilder, stringLength);
  242. if (stringLength > 0)
  243. {
  244. Il2CppStringBuilder* currentBuilder = stringBuilder;
  245. while (true)
  246. {
  247. if (currentBuilder == NULL)
  248. break;
  249. const Il2CppChar *str = (Il2CppChar*)il2cpp::vm::Array::GetFirstElementAddress(currentBuilder->chunkChars);
  250. memcpy(nativeString + currentBuilder->chunkOffset, str, (int)currentBuilder->chunkChars->max_length * sizeof(Il2CppChar));
  251. currentBuilder = currentBuilder->chunkPrevious;
  252. }
  253. }
  254. nativeString[stringLength] = '\0';
  255. return nativeString;
  256. }
  257. void PlatformInvoke::MarshalStringBuilderResult(Il2CppStringBuilder* stringBuilder, char* buffer)
  258. {
  259. if (stringBuilder == NULL || buffer == NULL)
  260. return;
  261. UTF16String utf16String = utils::StringUtils::Utf8ToUtf16(buffer);
  262. IL2CPP_OBJECT_SETREF(stringBuilder, chunkChars, il2cpp::vm::Array::New(il2cpp_defaults.char_class, (int)utf16String.size() + 1));
  263. for (int i = 0; i < (int)utf16String.size(); i++)
  264. il2cpp_array_set(stringBuilder->chunkChars, Il2CppChar, i, utf16String[i]);
  265. il2cpp_array_set(stringBuilder->chunkChars, Il2CppChar, (int)utf16String.size(), '\0');
  266. stringBuilder->chunkLength = (int)utf16String.size();
  267. stringBuilder->chunkOffset = 0;
  268. IL2CPP_OBJECT_SETREF_NULL(stringBuilder, chunkPrevious);
  269. }
  270. void PlatformInvoke::MarshalWStringBuilderResult(Il2CppStringBuilder* stringBuilder, Il2CppChar* buffer)
  271. {
  272. if (stringBuilder == NULL || buffer == NULL)
  273. return;
  274. int len = (int)utils::StringUtils::StrLen(buffer);
  275. IL2CPP_OBJECT_SETREF(stringBuilder, chunkChars, il2cpp::vm::Array::New(il2cpp_defaults.char_class, len + 1));
  276. for (int i = 0; i < len; i++)
  277. il2cpp_array_set(stringBuilder->chunkChars, Il2CppChar, i, buffer[i]);
  278. il2cpp_array_set(stringBuilder->chunkChars, Il2CppChar, len, '\0');
  279. stringBuilder->chunkLength = len;
  280. stringBuilder->chunkOffset = 0;
  281. IL2CPP_OBJECT_SETREF_NULL(stringBuilder, chunkPrevious);
  282. }
  283. static bool IsGenericInstance(const Il2CppType* type)
  284. {
  285. if (type->type == IL2CPP_TYPE_GENERICINST)
  286. return true;
  287. while (type->type == IL2CPP_TYPE_SZARRAY)
  288. {
  289. if (type->data.type->type == IL2CPP_TYPE_GENERICINST)
  290. return true;
  291. type = type->data.type;
  292. }
  293. return false;
  294. }
  295. static std::string TypeNameListFor(const Il2CppGenericInst* genericInst)
  296. {
  297. std::string typeNameList;
  298. if (genericInst != NULL)
  299. {
  300. int numberOfTypeArguments = genericInst->type_argc;
  301. for (int i = 0; i < numberOfTypeArguments; i++)
  302. {
  303. typeNameList += Type::GetName(genericInst->type_argv[i], IL2CPP_TYPE_NAME_FORMAT_FULL_NAME);
  304. if (i != numberOfTypeArguments - 1)
  305. typeNameList += ", ";
  306. }
  307. }
  308. return typeNameList;
  309. }
  310. #if !IL2CPP_TINY
  311. intptr_t PlatformInvoke::MarshalDelegate(Il2CppDelegate* d)
  312. {
  313. if (d == NULL)
  314. return 0;
  315. if (IsFakeDelegateMethodMarshaledFromNativeCode(d))
  316. return reinterpret_cast<intptr_t>(d->delegate_trampoline);
  317. Il2CppMethodPointer reversePInvokeWrapper = MetadataCache::GetReversePInvokeWrapper(d->method->klass->image, d->method);
  318. if (reversePInvokeWrapper == NULL)
  319. {
  320. std::string methodName = il2cpp::vm::Method::GetFullName(d->method);
  321. // Okay, we cannot marshal it for some reason. Figure out why.
  322. if (Method::IsInstance(d->method))
  323. {
  324. std::string errorMessage = "IL2CPP does not support marshaling delegates that point to instance methods to native code. The method we're attempting to marshal is: " + methodName;
  325. vm::Exception::Raise(vm::Exception::GetNotSupportedException(errorMessage.c_str()));
  326. }
  327. if (il2cpp::vm::Method::HasFullGenericSharingSignature(d->method))
  328. {
  329. std::string errorMessage = "IL2CPP does not support marshaling generic delegates when full generic sharing is enabled. The method we're attempting to marshal is: " + methodName;
  330. errorMessage += "\nTo marshal this delegate, please add an attribute named 'MonoPInvokeCallback' to the method definition.";
  331. errorMessage += "\nThis attribute should have a type argument which is a generic delegate with all of the types required for this generic instantiation:";
  332. std::string genericTypeArguments = TypeNameListFor(d->method->genericMethod->context.class_inst);
  333. if (!genericTypeArguments.empty())
  334. errorMessage += "\nGeneric type arguments: " + genericTypeArguments;
  335. std::string genericMethodArguments = TypeNameListFor(d->method->genericMethod->context.method_inst);
  336. if (!genericMethodArguments.empty())
  337. errorMessage += "\nGeneric method arguments: " + genericMethodArguments;
  338. errorMessage += "\nThis C# code should work, for example:";
  339. std::string maybeComma = !genericTypeArguments.empty() ? ", " : "";
  340. errorMessage += "\n[MonoPInvokeCallback(typeof(System.Action<" + genericTypeArguments + maybeComma + genericMethodArguments + ">))]";
  341. vm::Exception::Raise(vm::Exception::GetNotSupportedException(errorMessage.c_str()));
  342. }
  343. if (d->method->parameters != NULL)
  344. {
  345. for (int i = 0; i < d->method->parameters_count; ++i)
  346. {
  347. if (IsGenericInstance(d->method->parameters[i]))
  348. {
  349. std::string errorMessage = "Cannot marshal method '" + methodName + "' parameter '" + Method::GetParamName(d->method, i) + "': Generic types cannot be marshaled.";
  350. vm::Exception::Raise(vm::Exception::GetMarshalDirectiveException(errorMessage.c_str()));
  351. }
  352. }
  353. }
  354. std::string errorMessage = "To marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition. The method we're attempting to marshal is: " + methodName;
  355. vm::Exception::Raise(vm::Exception::GetNotSupportedException(errorMessage.c_str()));
  356. }
  357. return reinterpret_cast<intptr_t>(reversePInvokeWrapper);
  358. }
  359. Il2CppDelegate* PlatformInvoke::MarshalFunctionPointerToDelegate(void* functionPtr, Il2CppClass* delegateType)
  360. {
  361. if (functionPtr == NULL)
  362. return NULL;
  363. if (!Class::HasParent(delegateType, il2cpp_defaults.delegate_class))
  364. Exception::Raise(Exception::GetArgumentException("t", "Type must derive from Delegate."));
  365. const Il2CppInteropData* interopData = delegateType->interopData;
  366. Il2CppMethodPointer managedToNativeWrapperMethodPointer = interopData != NULL ? interopData->delegatePInvokeWrapperFunction : NULL;
  367. if (managedToNativeWrapperMethodPointer == NULL)
  368. Exception::Raise(Exception::GetMarshalDirectiveException(utils::StringUtils::Printf("Cannot marshal P/Invoke call through delegate of type '%s.%s'", Class::GetNamespace(delegateType), Class::GetName(delegateType)).c_str()));
  369. const MethodInfo* invokeMethod = il2cpp::vm::Runtime::GetDelegateInvoke(delegateType);
  370. Il2CppDelegate* delegate = (Il2CppDelegate*)il2cpp::vm::Object::New(delegateType);
  371. Type::ConstructClosedDelegate(delegate, (Il2CppObject*)delegate, managedToNativeWrapperMethodPointer, invokeMethod);
  372. delegate->delegate_trampoline = functionPtr;
  373. return delegate;
  374. }
  375. // When a delegate is marshalled from native code via Marshal.GetDelegateForFunctionPointer
  376. // It will store the native function pointer in delegate_trampoline
  377. bool PlatformInvoke::IsFakeDelegateMethodMarshaledFromNativeCode(const Il2CppDelegate* d)
  378. {
  379. return d->delegate_trampoline != NULL;
  380. }
  381. #endif // !IL2CPP_TINY
  382. } /* namespace vm */
  383. } /* namespace il2cpp */