No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Exception.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. #include "il2cpp-config.h"
  2. #include "gc/WriteBarrier.h"
  3. #include "os/MarshalStringAlloc.h"
  4. #include "os/WindowsRuntime.h"
  5. #include "metadata/GenericMetadata.h"
  6. #include "vm/Array.h"
  7. #include "vm/AssemblyName.h"
  8. #include "vm/Class.h"
  9. #include "vm/CCW.h"
  10. #include "vm/Exception.h"
  11. #include "vm/Object.h"
  12. #include "vm/Reflection.h"
  13. #include "vm/Runtime.h"
  14. #include "vm/StackTrace.h"
  15. #include "vm/String.h"
  16. #include "vm/Type.h"
  17. #include "Image.h"
  18. #include "../utils/StringUtils.h"
  19. #include "../utils/StringViewUtils.h"
  20. #include "il2cpp-tabledefs.h"
  21. #include "il2cpp-class-internals.h"
  22. #include "il2cpp-object-internals.h"
  23. #include "vm-utils/Debugger.h"
  24. #include "vm-utils/VmStringUtils.h"
  25. #include "vm-utils/DebugSymbolReader.h"
  26. namespace il2cpp
  27. {
  28. namespace vm
  29. {
  30. void Exception::PrepareExceptionForThrow(Il2CppException* ex, MethodInfo* lastManagedFrame)
  31. {
  32. #if IL2CPP_MONO_DEBUGGER
  33. il2cpp::utils::Debugger::HandleException(ex);
  34. #endif
  35. if (ex->trace_ips == NULL)
  36. {
  37. // Only write the stack trace if there is not one already in the exception.
  38. // When we exit managed try/finally and try/catch blocks with an exception, this method is
  39. // called with the original exception which already has the proper stack trace.
  40. // Getting the stack trace again here will lose the frames between the original throw
  41. // and the finally or catch block.
  42. const StackFrames& frames = *StackTrace::GetStackFrames();
  43. Il2CppArray* ips = NULL;
  44. Il2CppArray* raw_ips = NULL;
  45. size_t numberOfFrames = frames.size();
  46. if (numberOfFrames == 0 && lastManagedFrame != NULL)
  47. {
  48. // We didn't get any call stack. If we have one frame from codegen, use it.
  49. if (utils::DebugSymbolReader::DebugSymbolsAvailable())
  50. {
  51. Il2CppStackFrame *stackFrame = (Il2CppStackFrame*)vm::Object::New(il2cpp_defaults.stack_frame_class);
  52. IL2CPP_OBJECT_SETREF(stackFrame, method, vm::Reflection::GetMethodObject(lastManagedFrame, NULL));
  53. ips = Array::New(il2cpp_defaults.stack_frame_class, 1);
  54. il2cpp_array_setref(ips, 0, stackFrame);
  55. }
  56. else
  57. {
  58. ips = Array::New(il2cpp_defaults.int_class, 1);
  59. il2cpp_array_set(ips, const MethodInfo*, 0, lastManagedFrame);
  60. }
  61. }
  62. else
  63. {
  64. size_t i = numberOfFrames - 1;
  65. if (utils::DebugSymbolReader::DebugSymbolsAvailable())
  66. {
  67. ips = Array::New(il2cpp_defaults.stack_frame_class, numberOfFrames);
  68. }
  69. else
  70. {
  71. ips = Array::New(il2cpp_defaults.int_class, numberOfFrames);
  72. }
  73. raw_ips = Array::New(il2cpp_defaults.int_class, numberOfFrames);
  74. for (size_t frame = 0; frame != frames.size() && i >= 0; ++frame, --i)
  75. {
  76. const Il2CppStackFrameInfo& stackFrameInfo = frames[frame];
  77. if (utils::DebugSymbolReader::DebugSymbolsAvailable())
  78. {
  79. Il2CppStackFrame *stackFrame = (Il2CppStackFrame*)vm::Object::New(il2cpp_defaults.stack_frame_class);
  80. IL2CPP_OBJECT_SETREF(stackFrame, method, vm::Reflection::GetMethodObject(stackFrameInfo.method, NULL));
  81. stackFrame->line = stackFrameInfo.sourceCodeLineNumber;
  82. stackFrame->il_offset = stackFrameInfo.ilOffset;
  83. if (stackFrameInfo.filePath != NULL && strlen(stackFrameInfo.filePath) != 0)
  84. IL2CPP_OBJECT_SETREF(stackFrame, filename, il2cpp::vm::String::New(stackFrameInfo.filePath));
  85. il2cpp_array_setref(ips, i, stackFrame);
  86. }
  87. else
  88. {
  89. il2cpp_array_set(ips, const MethodInfo*, i, stackFrameInfo.method);
  90. }
  91. il2cpp_array_set(raw_ips, uintptr_t, i, stackFrameInfo.raw_ip);
  92. }
  93. }
  94. IL2CPP_ASSERT(ips != NULL);
  95. IL2CPP_OBJECT_SETREF(ex, trace_ips, ips);
  96. IL2CPP_OBJECT_SETREF(ex, native_trace_ips, raw_ips);
  97. }
  98. }
  99. NORETURN void Exception::Raise(Il2CppException* ex, MethodInfo* lastManagedFrame)
  100. {
  101. PrepareExceptionForThrow(ex, lastManagedFrame);
  102. throw Il2CppExceptionWrapper(ex);
  103. }
  104. NORETURN void Exception::Rethrow(Il2CppException* ex)
  105. {
  106. throw Il2CppExceptionWrapper(ex);
  107. }
  108. NORETURN void Exception::RaiseOutOfMemoryException()
  109. {
  110. RaiseOutOfMemoryException(utils::StringView<Il2CppChar>::Empty());
  111. }
  112. NORETURN void Exception::RaiseOutOfMemoryException(const utils::StringView<Il2CppChar>& msg)
  113. {
  114. Raise(GetOutOfMemoryException(msg));
  115. }
  116. NORETURN void Exception::RaiseNullReferenceException()
  117. {
  118. RaiseNullReferenceException(utils::StringView<Il2CppChar>::Empty());
  119. }
  120. NORETURN void Exception::RaiseNullReferenceException(const utils::StringView<Il2CppChar>& msg)
  121. {
  122. Raise(GetNullReferenceException(msg));
  123. }
  124. NORETURN void Exception::RaiseDivideByZeroException()
  125. {
  126. Raise(GetDivideByZeroException());
  127. }
  128. NORETURN void Exception::RaiseIndexOutOfRangeException()
  129. {
  130. Raise(GetIndexOutOfRangeException());
  131. }
  132. NORETURN void Exception::RaiseOverflowException()
  133. {
  134. Raise(GetOverflowException());
  135. }
  136. NORETURN void Exception::RaiseArgumentOutOfRangeException(const char* msg)
  137. {
  138. Raise(GetArgumentOutOfRangeException(msg));
  139. }
  140. static NORETURN void RaiseFromIl2CppError(const utils::Il2CppError& error)
  141. {
  142. utils::Il2CppErrorCode errorCode = error.GetErrorCode();
  143. if (errorCode == utils::NotSupported)
  144. Exception::Raise(Exception::GetNotSupportedException(error.GetErrorMessage().c_str()));
  145. if (errorCode == utils::ComError)
  146. Exception::Raise(error.GetHr(), false);
  147. if (errorCode == utils::UnauthorizedAccess)
  148. Exception::Raise(Exception::GetUnauthorizedAccessException(error.GetErrorMessage().c_str()));
  149. Exception::Raise(Exception::GetSystemException());
  150. }
  151. void Exception::RaiseIfError(const utils::Il2CppError& error)
  152. {
  153. if (error.GetErrorCode() != utils::NoError)
  154. RaiseFromIl2CppError(error);
  155. }
  156. inline static Il2CppException* TryGetExceptionFromRestrictedErrorInfo(Il2CppIRestrictedErrorInfo* errorInfo)
  157. {
  158. Il2CppILanguageExceptionErrorInfo* languageExceptionInfo;
  159. il2cpp_hresult_t hr = errorInfo->QueryInterface(Il2CppILanguageExceptionErrorInfo::IID, reinterpret_cast<void**>(&languageExceptionInfo));
  160. if (IL2CPP_HR_SUCCEEDED(hr))
  161. {
  162. Il2CppIUnknown* languageException;
  163. hr = languageExceptionInfo->GetLanguageException(&languageException);
  164. languageExceptionInfo->Release();
  165. if (IL2CPP_HR_SUCCEEDED(hr) && languageException != NULL) // It can succeed and return null exception if there's no exception info
  166. {
  167. Il2CppIManagedObjectHolder* managedObjectHolder;
  168. hr = languageException->QueryInterface(Il2CppIManagedObjectHolder::IID, reinterpret_cast<void**>(&managedObjectHolder));
  169. languageException->Release();
  170. if (IL2CPP_HR_SUCCEEDED(hr))
  171. {
  172. Il2CppException* exception = reinterpret_cast<Il2CppException*>(managedObjectHolder->GetManagedObject());
  173. managedObjectHolder->Release();
  174. // TODO: set restricted error info instead of releaseing it here
  175. errorInfo->Release();
  176. return exception;
  177. }
  178. }
  179. }
  180. return NULL;
  181. }
  182. inline static UTF16String GetMessageFromRestrictedErrorInfo(Il2CppIRestrictedErrorInfo* errorInfo)
  183. {
  184. UTF16String result;
  185. il2cpp_hresult_t error;
  186. Il2CppChar* bstrDescription;
  187. Il2CppChar* bstrRestrictedDescription;
  188. Il2CppChar* bstrCapabilitySid;
  189. il2cpp_hresult_t hr = errorInfo->GetErrorDetails(&bstrDescription, &error, &bstrRestrictedDescription, &bstrCapabilitySid);
  190. if (IL2CPP_HR_SUCCEEDED(hr))
  191. {
  192. int descriptionLength = 0;
  193. int restrictedDescriptionLength = 0;
  194. if (bstrDescription != NULL)
  195. os::MarshalStringAlloc::GetBStringLength(bstrDescription, &descriptionLength);
  196. if (bstrRestrictedDescription != NULL)
  197. os::MarshalStringAlloc::GetBStringLength(bstrRestrictedDescription, &restrictedDescriptionLength);
  198. result.append(bstrDescription, descriptionLength);
  199. if (restrictedDescriptionLength > 0)
  200. {
  201. result.append(kIl2CppNewLine);
  202. result.append(bstrRestrictedDescription, restrictedDescriptionLength);
  203. }
  204. if (bstrDescription != NULL)
  205. os::MarshalStringAlloc::FreeBString(bstrDescription);
  206. if (bstrRestrictedDescription != NULL)
  207. os::MarshalStringAlloc::FreeBString(bstrRestrictedDescription);
  208. if (bstrCapabilitySid != NULL)
  209. os::MarshalStringAlloc::FreeBString(bstrCapabilitySid);
  210. }
  211. return result;
  212. }
  213. // When doing COM interop, any unrecognized hresult gets turned into a COMException
  214. // When doing Windows Runtime interop, any unrecognized hresult gets turned into a System.Exception
  215. // Go figure.
  216. Il2CppException* Exception::Get(il2cpp_hresult_t hresult, bool defaultToCOMException)
  217. {
  218. UTF16String message;
  219. Il2CppIRestrictedErrorInfo* errorInfo = os::WindowsRuntime::GetRestrictedErrorInfo();
  220. if (errorInfo != NULL)
  221. {
  222. // First, try retrieving the original exception from restricted error info
  223. Il2CppException* exception = TryGetExceptionFromRestrictedErrorInfo(errorInfo);
  224. if (exception != NULL)
  225. return exception;
  226. // If we got here, restricted error info contained no existing managed exception
  227. message = GetMessageFromRestrictedErrorInfo(errorInfo);
  228. // To do: instead of releasing it here, store it on the exception that we're about to return
  229. errorInfo->Release();
  230. }
  231. switch (hresult)
  232. {
  233. case IL2CPP_E_NOTIMPL:
  234. return FromNameMsg(Image::GetCorlib(), "System", "NotImplementedException", STRING_TO_STRINGVIEW(message));
  235. case IL2CPP_E_NOINTERFACE:
  236. return GetInvalidCastException(STRING_TO_STRINGVIEW(message));
  237. case IL2CPP_E_POINTER:
  238. return GetNullReferenceException(STRING_TO_STRINGVIEW(message));
  239. case IL2CPP_COR_E_OPERATIONCANCELED:
  240. return FromNameMsg(Image::GetCorlib(), "System", "OperationCanceledException", STRING_TO_STRINGVIEW(message));
  241. case IL2CPP_E_ACCESS_DENIED:
  242. return GetUnauthorizedAccessException(STRING_TO_STRINGVIEW(message));
  243. case IL2CPP_E_OUTOFMEMORY:
  244. return GetOutOfMemoryException(STRING_TO_STRINGVIEW(message));
  245. case IL2CPP_E_INVALIDARG:
  246. return GetArgumentException(utils::StringView<Il2CppChar>::Empty(), STRING_TO_STRINGVIEW(message));
  247. case IL2CPP_COR_E_OBJECTDISPOSED:
  248. case IL2CPP_RO_E_CLOSED:
  249. return FromNameMsg(Image::GetCorlib(), "System", "ObjectDisposedException", STRING_TO_STRINGVIEW(message), hresult);
  250. case IL2CPP_E_FAIL:
  251. {
  252. if (message.empty())
  253. message = utils::StringUtils::Utf8ToUtf16("Unspecified error");
  254. return FromNameMsg(Image::GetCorlib(), "System.Runtime.InteropServices", "COMException", STRING_TO_STRINGVIEW(message), hresult);
  255. }
  256. case IL2CPP_COR_E_PLATFORMNOTSUPPORTED:
  257. {
  258. if (message.empty())
  259. message = utils::StringUtils::Utf8ToUtf16("Operation is not supported on this platform.");
  260. return GetPlatformNotSupportedException(STRING_TO_STRINGVIEW(message));
  261. }
  262. case IL2CPP_E_FILE_NOT_FOUND:
  263. return GetFileNotFoundException(STRING_TO_STRINGVIEW(message));
  264. default:
  265. return defaultToCOMException
  266. ? Exception::FromNameMsg(vm::Image::GetCorlib(), "System.Runtime.InteropServices", "COMException", STRING_TO_STRINGVIEW(message), hresult)
  267. : Exception::FromNameMsg(vm::Image::GetCorlib(), "System", "Exception", STRING_TO_STRINGVIEW(message), hresult);
  268. }
  269. }
  270. NORETURN void Exception::Raise(il2cpp_hresult_t hresult, bool defaultToCOMException)
  271. {
  272. Raise(Get(hresult, defaultToCOMException));
  273. }
  274. Il2CppException* Exception::FromNameMsg(const Il2CppImage* image, const char *name_space, const char *name, const char *msg)
  275. {
  276. UTF16String utf16Msg;
  277. if (msg != NULL)
  278. utf16Msg = utils::StringUtils::Utf8ToUtf16(msg);
  279. return FromNameMsg(image, name_space, name, STRING_TO_STRINGVIEW(utf16Msg));
  280. }
  281. Il2CppException* Exception::FromNameMsg(const Il2CppImage* image, const char* name_space, const char* name, const utils::StringView<Il2CppChar>& msg)
  282. {
  283. Il2CppClass* exceptionClass = Class::FromName(image, name_space, name);
  284. Il2CppException* ex = (Il2CppException*)Object::New(exceptionClass);
  285. Runtime::ObjectInit((Il2CppObject*)ex);
  286. if (msg.Length() > 0)
  287. IL2CPP_OBJECT_SETREF(ex, message, String::NewUtf16(msg));
  288. return ex;
  289. }
  290. Il2CppException* Exception::FromNameMsg(const Il2CppImage* image, const char *name_space, const char* name, const utils::StringView<Il2CppChar>& msg, il2cpp_hresult_t hresult)
  291. {
  292. Il2CppException* ex = FromNameMsg(image, name_space, name, msg);
  293. ex->hresult = hresult;
  294. return ex;
  295. }
  296. Il2CppException * Exception::GetArgumentException(const char *arg, const char *msg)
  297. {
  298. Il2CppException* ex = FromNameMsg(Image::GetCorlib(), "System", "ArgumentException", msg);
  299. if (arg)
  300. {
  301. Il2CppArgumentException *argex = (Il2CppArgumentException*)ex;
  302. IL2CPP_OBJECT_SETREF(argex, argName, String::New(arg));
  303. }
  304. return ex;
  305. }
  306. Il2CppException* Exception::GetArgumentException(const utils::StringView<Il2CppChar>& arg, const utils::StringView<Il2CppChar>& msg)
  307. {
  308. Il2CppException* ex = FromNameMsg(Image::GetCorlib(), "System", "ArgumentException", msg);
  309. if (arg.Length() > 0)
  310. {
  311. Il2CppArgumentException *argex = (Il2CppArgumentException*)ex;
  312. IL2CPP_OBJECT_SETREF(argex, argName, String::NewUtf16(arg));
  313. }
  314. return ex;
  315. }
  316. Il2CppException * Exception::GetArgumentNullException(const char *arg)
  317. {
  318. Il2CppException* ex = FromNameMsg(Image::GetCorlib(), "System", "ArgumentNullException", NULL);
  319. if (arg)
  320. {
  321. Il2CppArgumentException *argex = (Il2CppArgumentException*)ex;
  322. IL2CPP_OBJECT_SETREF(argex, argName, String::New(arg));
  323. }
  324. return ex;
  325. }
  326. Il2CppException * Exception::GetArgumentOutOfRangeException(const char *arg)
  327. {
  328. Il2CppException* ex = FromNameMsg(Image::GetCorlib(), "System", "ArgumentOutOfRangeException", NULL);
  329. if (arg)
  330. {
  331. Il2CppArgumentException *argex = (Il2CppArgumentException*)ex;
  332. IL2CPP_OBJECT_SETREF(argex, argName, String::New(arg));
  333. }
  334. return ex;
  335. }
  336. Il2CppException * Exception::GetTypeInitializationException(const char *msg, Il2CppException* innerException)
  337. {
  338. Il2CppException* ex = FromNameMsg(Image::GetCorlib(), "System", "TypeInitializationException", msg);
  339. if (innerException != NULL)
  340. IL2CPP_OBJECT_SETREF(ex, inner_ex, innerException);
  341. return ex;
  342. }
  343. Il2CppException* Exception::GetInvalidCastException(const char* msg)
  344. {
  345. return FromNameMsg(Image::GetCorlib(), "System", "InvalidCastException", msg);
  346. }
  347. Il2CppException* Exception::GetInvalidCastException(const utils::StringView<Il2CppChar>& msg)
  348. {
  349. return FromNameMsg(Image::GetCorlib(), "System", "InvalidCastException", msg);
  350. }
  351. Il2CppException* Exception::GetIndexOutOfRangeException()
  352. {
  353. return GetIndexOutOfRangeException(utils::StringView<Il2CppChar>::Empty());
  354. }
  355. Il2CppException* Exception::GetIndexOutOfRangeException(const utils::StringView<Il2CppChar>& msg)
  356. {
  357. return FromNameMsg(Image::GetCorlib(), "System", "IndexOutOfRangeException", msg);
  358. }
  359. Il2CppException* Exception::GetNullReferenceException(const utils::StringView<Il2CppChar>& msg)
  360. {
  361. return FromNameMsg(vm::Image::GetCorlib(), "System", "NullReferenceException", msg);
  362. }
  363. Il2CppException* Exception::GetTypeLoadException()
  364. {
  365. return FromNameMsg(vm::Image::GetCorlib(), "System", "TypeLoadException", NULL);
  366. }
  367. Il2CppException* Exception::GetTypeLoadException(const TypeNameParseInfo& info)
  368. {
  369. std::string assemblyNameStr;
  370. const TypeNameParseInfo::AssemblyName& assemblyName = info.assembly_name();
  371. if (!assemblyName.name.empty())
  372. {
  373. utils::VmStringUtils::CaseInsensitiveComparer comparer;
  374. if (comparer(assemblyName.name, "WindowsRuntimeMetadata"))
  375. return GetTypeLoadExceptionForWindowsRuntimeType(STRING_TO_STRINGVIEW(info.ns()), STRING_TO_STRINGVIEW(info.name()));
  376. assemblyNameStr += assemblyName.name;
  377. assemblyNameStr += ", Version=";
  378. const size_t bufferSize = 16;
  379. char buffer[bufferSize];
  380. snprintf(buffer, bufferSize, "%d.", assemblyName.major);
  381. assemblyNameStr += buffer;
  382. snprintf(buffer, bufferSize, "%d.", assemblyName.minor);
  383. assemblyNameStr += buffer;
  384. snprintf(buffer, bufferSize, "%d.", assemblyName.build);
  385. assemblyNameStr += buffer;
  386. snprintf(buffer, bufferSize, "%d", assemblyName.revision);
  387. assemblyNameStr += buffer;
  388. if (!assemblyName.culture.empty())
  389. {
  390. assemblyNameStr += ", Culture=";
  391. assemblyNameStr += assemblyName.culture;
  392. assemblyNameStr += ", PublicKeyToken=";
  393. }
  394. else
  395. {
  396. assemblyNameStr += ", Culture=neutral, PublicKeyToken=";
  397. }
  398. assemblyNameStr += assemblyName.public_key_token[0] ? assemblyName.public_key_token : "null";
  399. }
  400. return GetTypeLoadException(STRING_TO_STRINGVIEW(info.ns()), STRING_TO_STRINGVIEW(info.name()), STRING_TO_STRINGVIEW(assemblyNameStr));
  401. }
  402. Il2CppException* Exception::GetTypeLoadException(const utils::StringView<char>& namespaze, const utils::StringView<char>& typeName, const utils::StringView<char>& assemblyName)
  403. {
  404. std::string exceptionMessage = "Could not load type '";
  405. if (!namespaze.IsEmpty())
  406. {
  407. exceptionMessage.append(namespaze.Str(), namespaze.Length());
  408. exceptionMessage.push_back('.');
  409. }
  410. exceptionMessage.append(typeName.Str(), typeName.Length());
  411. exceptionMessage += "' from assembly '";
  412. if (assemblyName.IsEmpty())
  413. {
  414. exceptionMessage += AssemblyName::AssemblyNameToString(Image::GetAssembly(Image::GetCorlib())->aname);
  415. }
  416. else
  417. {
  418. exceptionMessage.append(assemblyName.Str(), assemblyName.Length());
  419. }
  420. exceptionMessage += "'.";
  421. return Exception::GetTypeLoadException(exceptionMessage.c_str());
  422. }
  423. Il2CppException* Exception::GetTypeLoadExceptionForWindowsRuntimeType(const utils::StringView<char>& namespaze, const utils::StringView<char>& typeName)
  424. {
  425. std::string typeLoadExceptionMessage = "Could not find Windows Runtime type '";
  426. if (namespaze.Length() != 0)
  427. {
  428. typeLoadExceptionMessage.append(namespaze.Str(), namespaze.Length());
  429. typeLoadExceptionMessage.push_back('.');
  430. }
  431. typeLoadExceptionMessage.append(typeName.Str(), typeName.Length());
  432. typeLoadExceptionMessage += "'.";
  433. Il2CppException* typeLoadException = Exception::GetTypeLoadException(typeLoadExceptionMessage.c_str());
  434. // If there's no '.' in neither typeName and namespace specified, it means there is no namespace specified
  435. // Therefore exception information should contain inner exception saying format is not recognized
  436. if (namespaze.Length() == 0 && typeName.Find('.') == utils::StringView<char>::NPos())
  437. {
  438. const char kInnerExceptionMessage[] = "The provided identity format is not recognized. (Exception from HRESULT: 0x80132003)";
  439. Il2CppException* innerException = Exception::GetArgumentException("", kInnerExceptionMessage);
  440. innerException->hresult = 0x80132003;
  441. IL2CPP_OBJECT_SETREF(typeLoadException, inner_ex, innerException);
  442. }
  443. return typeLoadException;
  444. }
  445. Il2CppException* Exception::GetOutOfMemoryException(const utils::StringView<Il2CppChar>& msg)
  446. {
  447. return FromNameMsg(vm::Image::GetCorlib(), "System", "OutOfMemoryException", msg);
  448. }
  449. Il2CppException* Exception::GetOverflowException()
  450. {
  451. return FromNameMsg(vm::Image::GetCorlib(), "System", "OverflowException", NULL);
  452. }
  453. Il2CppException* Exception::GetOverflowException(const char* msg)
  454. {
  455. return FromNameMsg(vm::Image::GetCorlib(), "System", "OverflowException", msg);
  456. }
  457. Il2CppException* Exception::GetFormatException(const char* msg)
  458. {
  459. return FromNameMsg(vm::Image::GetCorlib(), "System", "FormatException", msg);
  460. }
  461. Il2CppException* Exception::GetSystemException()
  462. {
  463. return FromNameMsg(vm::Image::GetCorlib(), "System", "SystemException", NULL);
  464. }
  465. Il2CppException* Exception::GetNotSupportedException(const char* msg)
  466. {
  467. return FromNameMsg(vm::Image::GetCorlib(), "System", "NotSupportedException", msg);
  468. }
  469. Il2CppException* Exception::GetArrayTypeMismatchException()
  470. {
  471. return FromNameMsg(vm::Image::GetCorlib(), "System", "ArrayTypeMismatchException", NULL);
  472. }
  473. Il2CppException* Exception::GetTypeLoadException(const char* msg)
  474. {
  475. return FromNameMsg(vm::Image::GetCorlib(), "System", "TypeLoadException", msg);
  476. }
  477. Il2CppException* Exception::GetEntryPointNotFoundException(const char* msg)
  478. {
  479. return FromNameMsg(vm::Image::GetCorlib(), "System", "EntryPointNotFoundException", msg);
  480. }
  481. Il2CppException* Exception::GetAmbiguousImplementationException(const char* msg)
  482. {
  483. return FromNameMsg(vm::Image::GetCorlib(), "System.Runtime", "AmbiguousImplementationException", msg);
  484. }
  485. Il2CppException* Exception::GetDllNotFoundException(const char* msg)
  486. {
  487. return FromNameMsg(vm::Image::GetCorlib(), "System", "DllNotFoundException", msg);
  488. }
  489. Il2CppException * Exception::GetInvalidOperationException(const char* msg)
  490. {
  491. return FromNameMsg(Image::GetCorlib(), "System", "InvalidOperationException", msg);
  492. }
  493. Il2CppException* Exception::GetThreadInterruptedException()
  494. {
  495. return FromNameMsg(vm::Image::GetCorlib(), "System.Threading", "ThreadInterruptedException", NULL);
  496. }
  497. Il2CppException* Exception::GetThreadAbortException()
  498. {
  499. return FromNameMsg(vm::Image::GetCorlib(), "System.Threading", "ThreadAbortException", NULL);
  500. }
  501. Il2CppException* Exception::GetThreadStateException(const char* msg)
  502. {
  503. return FromNameMsg(vm::Image::GetCorlib(), "System.Threading", "ThreadStateException", msg);
  504. }
  505. Il2CppException* Exception::GetSynchronizationLockException(const char* msg)
  506. {
  507. return FromNameMsg(vm::Image::GetCorlib(), "System.Threading", "SynchronizationLockException", msg);
  508. }
  509. Il2CppException * Exception::GetMissingMethodException(const char* msg)
  510. {
  511. return FromNameMsg(Image::GetCorlib(), "System", "MissingMethodException", msg);
  512. }
  513. Il2CppException * Exception::GetMarshalDirectiveException(const char* msg)
  514. {
  515. return FromNameMsg(Image::GetCorlib(), "System.Runtime.InteropServices", "MarshalDirectiveException", msg);
  516. }
  517. Il2CppException * Exception::GetTargetException(const char* msg)
  518. {
  519. return FromNameMsg(Image::GetCorlib(), "System.Reflection", "TargetException", msg);
  520. }
  521. Il2CppException* Exception::GetMethodAccessException(const char* msg)
  522. {
  523. return FromNameMsg(Image::GetCorlib(), "System", "MethodAccessException", msg);
  524. }
  525. Il2CppException * Exception::GetExecutionEngineException(const char* msg)
  526. {
  527. return FromNameMsg(Image::GetCorlib(), "System", "ExecutionEngineException", msg);
  528. }
  529. Il2CppException* Exception::GetUnauthorizedAccessException(const utils::StringView<Il2CppChar>& msg)
  530. {
  531. return FromNameMsg(Image::GetCorlib(), "System", "UnauthorizedAccessException", msg);
  532. }
  533. Il2CppException* Exception::GetUnauthorizedAccessException(const char* msg)
  534. {
  535. return FromNameMsg(Image::GetCorlib(), "System", "UnauthorizedAccessException", msg);
  536. }
  537. Il2CppException * Exception::GetMaximumNestedGenericsException()
  538. {
  539. int currentLimit = metadata::GenericMetadata::GetMaximumRuntimeGenericDepth();
  540. return GetNotSupportedException(utils::StringUtils::Printf(MAXIMUM_NESTED_GENERICS_EXCEPTION_MESSAGE, currentLimit).c_str());
  541. }
  542. Il2CppException* Exception::GetDivideByZeroException()
  543. {
  544. return FromNameMsg(vm::Image::GetCorlib(), "System", "DivideByZeroException", NULL);
  545. }
  546. Il2CppException* Exception::GetPlatformNotSupportedException(const utils::StringView<Il2CppChar>& msg)
  547. {
  548. return FromNameMsg(Image::GetCorlib(), "System", "PlatformNotSupportedException", msg);
  549. }
  550. Il2CppException* Exception::GetFileLoadException(const char* msg)
  551. {
  552. return FromNameMsg(Image::GetCorlib(), "System.IO", "FileLoadException", msg);
  553. }
  554. Il2CppException* Exception::GetFileNotFoundException(const utils::StringView<Il2CppChar>& msg)
  555. {
  556. return FromNameMsg(Image::GetCorlib(), "System.IO", "FileNotFoundException", msg);
  557. }
  558. Il2CppException* Exception::GetCustomAttributeFormatException(const char* msg)
  559. {
  560. return FromNameMsg(Image::GetCorlib(), "System.Reflection", "CustomAttributeFormatException", msg);
  561. }
  562. void Exception::StoreExceptionInfo(Il2CppException* ex, Il2CppString* exceptionString)
  563. {
  564. // To do: try retrieving IRestrictedErrorInfo here
  565. os::WindowsRuntime::OriginateLanguageException(ex->hresult, ex, exceptionString, CCW::GetOrCreate);
  566. }
  567. } /* namespace vm */
  568. } /* namespace il2cpp */