Exception.cpp 27 KB

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