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.

il2cpp-codegen.cpp 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. #include <string>
  2. #include <stdarg.h>
  3. #include "il2cpp-config.h"
  4. #include "il2cpp-codegen.h"
  5. #include "utils/Exception.h"
  6. #include "os/Atomic.h"
  7. #include "metadata/GenericMethod.h"
  8. #include "gc/GarbageCollector.h"
  9. #include "gc/WriteBarrier.h"
  10. #include "vm/Array.h"
  11. #include "vm/CCW.h"
  12. #include "vm/COM.h"
  13. #include "vm/Class.h"
  14. #include "vm/Exception.h"
  15. #include "vm/Field.h"
  16. #include "vm/InternalCalls.h"
  17. #include "vm/LastError.h"
  18. #include "vm/MarshalAlloc.h"
  19. #include "vm/MetadataCache.h"
  20. #include "vm/Method.h"
  21. #include "vm/Object.h"
  22. #include "vm/PlatformInvoke.h"
  23. #include "vm/Profiler.h"
  24. #include "vm/RCW.h"
  25. #include "vm/Reflection.h"
  26. #include "vm/Runtime.h"
  27. #include "vm/StackTrace.h"
  28. #include "vm/String.h"
  29. #include "vm/Thread.h"
  30. #include "vm/ThreadPoolMs.h"
  31. #include "vm/Type.h"
  32. #include "vm/WindowsRuntime.h"
  33. #include "vm-utils/VmThreadUtils.h"
  34. #include "utils/Runtime.h"
  35. #if IL2CPP_ENABLE_WRITE_BARRIERS
  36. void Il2CppCodeGenWriteBarrier(void** targetAddress, void* object)
  37. {
  38. il2cpp::gc::GarbageCollector::SetWriteBarrier(targetAddress);
  39. }
  40. #endif
  41. // This function exists to help with generation of callstacks for exceptions
  42. // on iOS and MacOS x64 with clang 6.0 (newer versions of clang don't have this
  43. // problem on x64). There we call the backtrace function, which does not play nicely
  44. // with NORETURN, since the compiler eliminates the method prologue code setting up
  45. // the address of the return frame (which makes sense). So on iOS we need to make
  46. // the NORETURN define do nothing, then we use this dummy method which has the
  47. // attribute for clang on iOS defined to prevent clang compiler errors for
  48. // method that end by throwing a managed exception.
  49. REAL_NORETURN IL2CPP_NO_INLINE void il2cpp_codegen_no_return()
  50. {
  51. IL2CPP_UNREACHABLE;
  52. }
  53. REAL_NORETURN void il2cpp_codegen_abort()
  54. {
  55. il2cpp::utils::Runtime::Abort();
  56. il2cpp_codegen_no_return();
  57. }
  58. #if IL2CPP_ENABLE_WRITE_BARRIERS
  59. void Il2CppCodeGenWriteBarrierForType(const Il2CppType* type, void** targetAddress, void* object)
  60. {
  61. #if IL2CPP_ENABLE_STRICT_WRITE_BARRIERS
  62. if (il2cpp::vm::Type::IsPointerType(type))
  63. return;
  64. if (il2cpp::vm::Type::IsStruct(type))
  65. {
  66. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  67. FieldInfo* field;
  68. void* iter = NULL;
  69. while ((field = il2cpp::vm::Class::GetFields(klass, &iter)))
  70. {
  71. if (il2cpp::vm::Field::GetFlags(field) & FIELD_ATTRIBUTE_STATIC)
  72. continue;
  73. void* fieldTargetAddress = il2cpp::vm::Field::GetInstanceFieldDataPointer((void*)targetAddress, field);
  74. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldTargetAddress, NULL);
  75. }
  76. }
  77. else
  78. {
  79. il2cpp::gc::GarbageCollector::SetWriteBarrier(targetAddress);
  80. }
  81. #else
  82. il2cpp::gc::GarbageCollector::SetWriteBarrier(targetAddress);
  83. #endif
  84. }
  85. void Il2CppCodeGenWriteBarrierForClass(Il2CppClass* klass, void** targetAddress, void* object)
  86. {
  87. #if IL2CPP_ENABLE_STRICT_WRITE_BARRIERS
  88. Il2CppCodeGenWriteBarrierForType(il2cpp::vm::Class::GetType(klass), targetAddress, object);
  89. #else
  90. il2cpp::gc::GarbageCollector::SetWriteBarrier(targetAddress);
  91. #endif
  92. }
  93. #endif // IL2CPP_ENABLE_WRITE_BARRIERS
  94. void* il2cpp_codegen_atomic_compare_exchange_pointer(void** dest, void* exchange, void* comparand)
  95. {
  96. return il2cpp::os::Atomic::CompareExchangePointer(dest, exchange, comparand);
  97. }
  98. void il2cpp_codegen_marshal_store_last_error()
  99. {
  100. il2cpp::vm::LastError::StoreLastError();
  101. }
  102. Il2CppAsyncResult* il2cpp_codegen_delegate_begin_invoke(RuntimeDelegate* delegate, void** params, RuntimeDelegate* asyncCallback, RuntimeObject* state)
  103. {
  104. return il2cpp::vm::ThreadPoolMs::DelegateBeginInvoke(delegate, params, asyncCallback, state);
  105. }
  106. RuntimeObject* il2cpp_codegen_delegate_end_invoke(Il2CppAsyncResult* asyncResult, void **out_args)
  107. {
  108. return il2cpp::vm::ThreadPoolMs::DelegateEndInvoke(asyncResult, out_args);
  109. }
  110. void il2cpp_codegen_set_closed_delegate_invoke(RuntimeObject* delegate, RuntimeObject* target, void* methodPtr)
  111. {
  112. IL2CPP_ASSERT(delegate->klass->parent == il2cpp_defaults.multicastdelegate_class);
  113. il2cpp::vm::Type::SetClosedDelegateInvokeMethod((RuntimeDelegate*)delegate, target, (Il2CppMethodPointer)methodPtr);
  114. }
  115. Il2CppMethodPointer il2cpp_codegen_resolve_icall(const char* name)
  116. {
  117. Il2CppMethodPointer method = il2cpp::vm::InternalCalls::Resolve(name);
  118. if (!method)
  119. {
  120. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingMethodException(name));
  121. }
  122. return method;
  123. }
  124. Type_t* il2cpp_codegen_type_get_object(const RuntimeType* type)
  125. {
  126. return (Type_t*)il2cpp::vm::Reflection::GetTypeObject(type);
  127. }
  128. MethodBase_t* il2cpp_codegen_get_method_object_internal(const RuntimeMethod* method, RuntimeClass* refclass)
  129. {
  130. return (MethodBase_t*)il2cpp::vm::Reflection::GetMethodObject(method, method->klass);
  131. }
  132. Assembly_t* il2cpp_codegen_get_executing_assembly(const RuntimeMethod* method)
  133. {
  134. return (Assembly_t*)il2cpp::vm::Reflection::GetAssemblyObject(method->klass->image->assembly);
  135. }
  136. void il2cpp_codegen_register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)
  137. {
  138. il2cpp::vm::MetadataCache::Register(codeRegistration, metadataRegistration, codeGenOptions);
  139. }
  140. extern MetadataInitializerCleanupFunc g_ClearMethodMetadataInitializedFlags;
  141. void il2cpp_codegen_register_metadata_initialized_cleanup(MetadataInitializerCleanupFunc cleanup)
  142. {
  143. g_ClearMethodMetadataInitializedFlags = cleanup;
  144. }
  145. void il2cpp_codegen_initialize_runtime_metadata(uintptr_t* metadataPointer)
  146. {
  147. il2cpp::vm::MetadataCache::InitializeRuntimeMetadata(metadataPointer);
  148. // We don't need a memory barrier here, InitializeRuntimeMetadata already has one
  149. // What we need is a barrier before setting s_Il2CppMethodInitialized = true in the generated code
  150. // but adding that to every function increases code size, so instead we rely on this function
  151. // being called before s_Il2CppCodeRegistrationInitialized is set to true
  152. il2cpp::os::Atomic::FullMemoryBarrier();
  153. }
  154. void* il2cpp_codegen_initialize_runtime_metadata_inline(uintptr_t* metadataPointer)
  155. {
  156. return il2cpp::vm::MetadataCache::InitializeRuntimeMetadata(metadataPointer);
  157. }
  158. const RuntimeClass* il2cpp_codegen_get_generic_type_definition(const RuntimeClass* klass)
  159. {
  160. IL2CPP_ASSERT(klass->generic_class);
  161. return il2cpp::vm::Class::FromIl2CppType(klass->generic_class->type);
  162. }
  163. const RuntimeMethod* il2cpp_codegen_get_generic_method_definition(const RuntimeMethod* method)
  164. {
  165. return il2cpp::vm::MetadataCache::GetGenericMethodDefinition(method);
  166. }
  167. const RuntimeMethod* il2cpp_codegen_get_generic_instance_method_from_method_definition(RuntimeClass* genericInstanceClass, const RuntimeMethod* methodDefinition)
  168. {
  169. return il2cpp::vm::Class::GetGenericInstanceMethodFromDefintion(genericInstanceClass, methodDefinition);
  170. }
  171. void* il2cpp_codegen_get_thread_static_data(RuntimeClass* klass)
  172. {
  173. return il2cpp::vm::Thread::GetThreadStaticData(klass->thread_static_fields_offset);
  174. }
  175. void il2cpp_codegen_assert_field_size(RuntimeField* field, size_t size)
  176. {
  177. IL2CPP_ASSERT(size == il2cpp_codegen_sizeof(InitializedTypeInfo(il2cpp::vm::Class::FromIl2CppType(field->type))));
  178. }
  179. void* il2cpp_codegen_get_instance_field_data_pointer(void* instance, RuntimeField* field)
  180. {
  181. return il2cpp::vm::Field::GetInstanceFieldDataPointer(instance, field);
  182. }
  183. void il2cpp_codegen_write_instance_field_data(void* instance, RuntimeField* field, void* data, uint32_t size)
  184. {
  185. il2cpp_codegen_assert_field_size(field, size);
  186. IL2CPP_ASSERT(il2cpp::vm::Field::IsInstance(field));
  187. void* fieldPointer = il2cpp_codegen_get_instance_field_data_pointer(instance, field);
  188. il2cpp_codegen_memcpy(fieldPointer, data, size);
  189. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  190. }
  191. void* il2cpp_codegen_get_static_field_data_pointer(RuntimeField* field)
  192. {
  193. IL2CPP_ASSERT(il2cpp::vm::Field::IsNormalStatic(field));
  194. return ((uint8_t*)field->parent->static_fields) + field->offset;
  195. }
  196. void il2cpp_codegen_write_static_field_data(RuntimeField* field, void* data, uint32_t size)
  197. {
  198. il2cpp_codegen_assert_field_size(field, size);
  199. IL2CPP_ASSERT(il2cpp::vm::Field::IsNormalStatic(field));
  200. void* fieldPointer = il2cpp_codegen_get_static_field_data_pointer(field);
  201. il2cpp_codegen_memcpy(fieldPointer, data, size);
  202. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  203. }
  204. void* il2cpp_codegen_get_thread_static_field_data_pointer(RuntimeField* field)
  205. {
  206. IL2CPP_ASSERT(il2cpp::vm::Field::IsThreadStatic(field));
  207. int threadStaticFieldOffset = il2cpp::vm::MetadataCache::GetThreadLocalStaticOffsetForField(field);
  208. void* threadStaticData = il2cpp::vm::Thread::GetThreadStaticData(field->parent->thread_static_fields_offset);
  209. return static_cast<uint8_t*>(threadStaticData) + threadStaticFieldOffset;
  210. }
  211. void il2cpp_codegen_write_thread_static_field_data(RuntimeField* field, void* data, uint32_t size)
  212. {
  213. il2cpp_codegen_assert_field_size(field, size);
  214. IL2CPP_ASSERT(il2cpp::vm::Field::IsThreadStatic(field));
  215. void* fieldPointer = il2cpp_codegen_get_thread_static_field_data_pointer(field);
  216. il2cpp_codegen_memcpy(fieldPointer, data, size);
  217. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  218. }
  219. void il2cpp_codegen_memory_barrier()
  220. {
  221. il2cpp::vm::Thread::FullMemoryBarrier();
  222. }
  223. void SetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, void* value)
  224. {
  225. il2cpp_array_setrefwithsize(thisPtr, thisPtr->klass->element_size, pos, value);
  226. }
  227. RuntimeArray* SZArrayNew(RuntimeClass* arrayType, uint32_t length)
  228. {
  229. return il2cpp::vm::Array::NewSpecific(arrayType, length);
  230. }
  231. RuntimeArray* GenArrayNew(RuntimeClass* arrayType, il2cpp_array_size_t* dimensions)
  232. {
  233. return il2cpp::vm::Array::NewFull(arrayType, dimensions, NULL);
  234. }
  235. bool il2cpp_codegen_method_is_generic_instance_method(RuntimeMethod* method)
  236. {
  237. return il2cpp::vm::Method::IsGenericInstanceMethod(method);
  238. }
  239. bool il2cpp_codegen_method_is_generic_instance(RuntimeClass* klass)
  240. {
  241. return il2cpp::vm::Class::IsInflated(klass);
  242. }
  243. RuntimeClass* il2cpp_codegen_method_get_declaring_type(const RuntimeMethod* method)
  244. {
  245. return il2cpp::vm::Method::GetClass(method);
  246. }
  247. bool MethodIsStatic(const RuntimeMethod* method)
  248. {
  249. return !il2cpp::vm::Method::IsInstance(method);
  250. }
  251. bool MethodHasParameters(const RuntimeMethod* method)
  252. {
  253. return il2cpp::vm::Method::GetParamCount(method) != 0;
  254. }
  255. NORETURN void il2cpp_codegen_raise_profile_exception(const RuntimeMethod* method)
  256. {
  257. std::string methodName = il2cpp::vm::Method::GetFullName(method);
  258. il2cpp_codegen_raise_exception(il2cpp_codegen_get_not_supported_exception(methodName.c_str()));
  259. }
  260. const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method_internal(const RuntimeMethod* vtableSlotMethod, const RuntimeMethod* genericVirtualMethod)
  261. {
  262. return il2cpp::metadata::GenericMethod::GetGenericVirtualMethod(vtableSlotMethod, genericVirtualMethod);
  263. }
  264. void il2cpp_codegen_runtime_class_init(RuntimeClass* klass)
  265. {
  266. il2cpp::vm::Runtime::ClassInit(klass);
  267. }
  268. void il2cpp_codegen_raise_execution_engine_exception(const RuntimeMethod* method)
  269. {
  270. il2cpp::vm::Runtime::AlwaysRaiseExecutionEngineException(method);
  271. }
  272. void il2cpp_codegen_raise_execution_engine_exception_missing_virtual(const RuntimeMethod* method)
  273. {
  274. il2cpp::vm::Runtime::AlwaysRaiseExecutionEngineExceptionOnVirtualCall(method);
  275. }
  276. RuntimeObject* IsInst(RuntimeObject *obj, RuntimeClass* targetType)
  277. {
  278. return il2cpp::vm::Object::IsInst(obj, targetType);
  279. }
  280. RuntimeObject* Box(RuntimeClass* type, void* data)
  281. {
  282. return il2cpp::vm::Object::Box(type, data);
  283. }
  284. void* Unbox_internal(Il2CppObject* obj)
  285. {
  286. return il2cpp::vm::Object::Unbox(obj);
  287. }
  288. void UnBoxNullable_internal(RuntimeObject* obj, RuntimeClass* nullableClass, void* storage)
  289. {
  290. il2cpp::vm::Object::UnboxNullable(obj, nullableClass, storage);
  291. }
  292. void* UnBox_Any(RuntimeObject* obj, RuntimeClass* expectedBoxedClass, void* unboxStorage)
  293. {
  294. IL2CPP_ASSERT(unboxStorage != NULL);
  295. // We assume unboxStorage is on the stack, if not we'll need a write barrier
  296. IL2CPP_ASSERT_STACK_PTR(unboxStorage);
  297. if (il2cpp::vm::Class::IsValuetype(expectedBoxedClass))
  298. {
  299. if (il2cpp::vm::Class::IsNullable(expectedBoxedClass))
  300. {
  301. UnBoxNullable(obj, expectedBoxedClass, unboxStorage);
  302. return unboxStorage;
  303. }
  304. return UnBox(obj, expectedBoxedClass);
  305. }
  306. // Use unboxStorage to return a pointer to obj
  307. // This keeps the return value of UnBox_Any consistent; it always returns a pointer to the data we want
  308. // This saves a runtime check on the class type
  309. *((void**)unboxStorage) = Castclass(obj, expectedBoxedClass);
  310. return unboxStorage;
  311. }
  312. bool il2cpp_codegen_would_box_to_non_null(RuntimeClass* klass, void* objBuffer)
  313. {
  314. if (il2cpp::vm::Class::IsValuetype(klass))
  315. {
  316. if (il2cpp::vm::Class::IsNullable(klass))
  317. return il2cpp::vm::Object::NullableHasValue(klass, objBuffer);
  318. return true;
  319. }
  320. return *(void**)objBuffer != NULL;
  321. }
  322. RuntimeObject* il2cpp_codegen_object_new(RuntimeClass *klass)
  323. {
  324. return il2cpp::vm::Object::New(klass);
  325. }
  326. void* il2cpp_codegen_marshal_allocate(size_t size)
  327. {
  328. return il2cpp::vm::MarshalAlloc::Allocate(size);
  329. }
  330. #if _DEBUG
  331. void il2cpp_codegen_marshal_allocate_push_allocation_frame()
  332. {
  333. il2cpp::vm::MarshalAlloc::PushAllocationFrame();
  334. }
  335. void il2cpp_codegen_marshal_allocate_pop_allocation_frame()
  336. {
  337. il2cpp::vm::MarshalAlloc::PopAllocationFrame();
  338. }
  339. bool il2cpp_codegen_marshal_allocate_has_unfreed_allocations()
  340. {
  341. return il2cpp::vm::MarshalAlloc::HasUnfreedAllocations();
  342. }
  343. void il2cpp_codegen_marshal_allocate_clear_all_tracked_allocations()
  344. {
  345. il2cpp::vm::MarshalAlloc::ClearAllTrackedAllocations();
  346. }
  347. #endif
  348. #if IL2CPP_ENABLE_PROFILER
  349. void il2cpp_codegen_profiler_method_enter(const RuntimeMethod* method)
  350. {
  351. il2cpp::vm::Profiler::MethodEnter(method);
  352. }
  353. void il2cpp_codegen_profiler_method_exit(const RuntimeMethod* method)
  354. {
  355. il2cpp::vm::Profiler::MethodExit(method);
  356. }
  357. #endif
  358. NORETURN void il2cpp_codegen_raise_exception(Exception_t *ex, MethodInfo* lastManagedFrame)
  359. {
  360. RuntimeException* exc = (RuntimeException*)ex;
  361. IL2CPP_OBJECT_SETREF_NULL(exc, trace_ips);
  362. IL2CPP_OBJECT_SETREF_NULL(exc, stack_trace);
  363. il2cpp::vm::Exception::Raise(exc, lastManagedFrame);
  364. }
  365. NORETURN void il2cpp_codegen_rethrow_exception(Exception_t *ex)
  366. {
  367. il2cpp::vm::Exception::Rethrow((RuntimeException*)ex);
  368. }
  369. NORETURN void il2cpp_codegen_raise_exception(il2cpp_hresult_t hresult, bool defaultToCOMException)
  370. {
  371. il2cpp::vm::Exception::Raise(hresult, defaultToCOMException);
  372. }
  373. NORETURN void il2cpp_codegen_raise_out_of_memory_exception()
  374. {
  375. il2cpp::vm::Exception::RaiseOutOfMemoryException();
  376. }
  377. NORETURN void il2cpp_codegen_raise_null_reference_exception()
  378. {
  379. il2cpp::vm::Exception::RaiseNullReferenceException();
  380. }
  381. NORETURN void il2cpp_codegen_raise_divide_by_zero_exception()
  382. {
  383. il2cpp::vm::Exception::RaiseDivideByZeroException();
  384. }
  385. NORETURN void il2cpp_codegen_raise_index_out_of_range_exception()
  386. {
  387. il2cpp::vm::Exception::RaiseIndexOutOfRangeException();
  388. }
  389. NORETURN void il2cpp_codegen_raise_index_out_of_range_exception(const RuntimeMethod* method)
  390. {
  391. IL2CPP_RAISE_MANAGED_EXCEPTION(il2cpp_codegen_get_overflow_exception(), method);
  392. }
  393. NORETURN void il2cpp_codegen_raise_invalid_unmanaged_callers_usage(const RuntimeMethod* method, const char* msg)
  394. {
  395. std::string fullName = il2cpp::vm::Method::GetFullName(method);
  396. IL2CPP_RAISE_MANAGED_EXCEPTION(il2cpp::vm::Exception::GetExecutionEngineException((fullName + ": " + msg).c_str()), method);
  397. }
  398. Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg)
  399. {
  400. return (Exception_t*)il2cpp::vm::Exception::GetArgumentException(param, msg);
  401. }
  402. Exception_t* il2cpp_codegen_get_argument_null_exception(const char* param)
  403. {
  404. return (Exception_t*)il2cpp::vm::Exception::GetArgumentNullException(param);
  405. }
  406. Exception_t* il2cpp_codegen_get_overflow_exception()
  407. {
  408. return (Exception_t*)il2cpp::vm::Exception::GetOverflowException("Arithmetic operation resulted in an overflow.");
  409. }
  410. Exception_t* il2cpp_codegen_get_not_supported_exception(const char* msg)
  411. {
  412. return (Exception_t*)il2cpp::vm::Exception::GetNotSupportedException(msg);
  413. }
  414. Exception_t* il2cpp_codegen_get_array_type_mismatch_exception()
  415. {
  416. return (Exception_t*)il2cpp::vm::Exception::GetArrayTypeMismatchException();
  417. }
  418. Exception_t* il2cpp_codegen_get_invalid_cast_exception(const char* msg)
  419. {
  420. return (Exception_t*)il2cpp::vm::Exception::GetInvalidCastException(msg);
  421. }
  422. Exception_t* il2cpp_codegen_get_invalid_operation_exception(const char* msg)
  423. {
  424. return (Exception_t*)il2cpp::vm::Exception::GetInvalidOperationException(msg);
  425. }
  426. Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg)
  427. {
  428. return (Exception_t*)il2cpp::vm::Exception::GetMarshalDirectiveException(msg);
  429. }
  430. Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  431. {
  432. return (Exception_t*)il2cpp::vm::Exception::GetMissingMethodException(msg);
  433. }
  434. Exception_t* il2cpp_codegen_get_maximum_nested_generics_exception()
  435. {
  436. return (Exception_t*)il2cpp::vm::Exception::GetMaximumNestedGenericsException();
  437. }
  438. Exception_t* il2cpp_codegen_get_engine_execution_exception(const char* msg)
  439. {
  440. return (Exception_t*)il2cpp::vm::Exception::GetExecutionEngineException(msg);
  441. }
  442. Exception_t* il2cpp_codegen_get_index_out_of_range_exception()
  443. {
  444. return (Exception_t*)il2cpp::vm::Exception::GetIndexOutOfRangeException();
  445. }
  446. Exception_t* il2cpp_codegen_get_exception(il2cpp_hresult_t hresult, bool defaultToCOMException)
  447. {
  448. return (Exception_t*)il2cpp::vm::Exception::Get(hresult, defaultToCOMException);
  449. }
  450. void il2cpp_codegen_store_exception_info(RuntimeException* ex, String_t* exceptionString)
  451. {
  452. il2cpp::vm::Exception::StoreExceptionInfo(ex, reinterpret_cast<RuntimeString*>(exceptionString));
  453. }
  454. void il2cpp_codegen_com_marshal_variant(RuntimeObject* obj, Il2CppVariant* variant)
  455. {
  456. il2cpp::vm::COM::MarshalVariant(obj, variant);
  457. }
  458. RuntimeObject* il2cpp_codegen_com_marshal_variant_result(const Il2CppVariant* variant)
  459. {
  460. return il2cpp::vm::COM::MarshalVariantResult(variant);
  461. }
  462. void il2cpp_codegen_com_destroy_variant(Il2CppVariant* variant)
  463. {
  464. il2cpp::vm::COM::DestroyVariant(variant);
  465. }
  466. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array(Il2CppChar type, RuntimeArray* managedArray)
  467. {
  468. return il2cpp::vm::COM::MarshalSafeArray(type, managedArray);
  469. }
  470. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_result(Il2CppChar variantType, RuntimeClass* type, Il2CppSafeArray* safeArray)
  471. {
  472. return il2cpp::vm::COM::MarshalSafeArrayResult(variantType, type, safeArray);
  473. }
  474. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array_bstring(RuntimeArray* managedArray)
  475. {
  476. return il2cpp::vm::COM::MarshalSafeArrayBString(managedArray);
  477. }
  478. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_bstring_result(RuntimeClass* type, Il2CppSafeArray* safeArray)
  479. {
  480. return il2cpp::vm::COM::MarshalSafeArrayBStringResult(type, safeArray);
  481. }
  482. void il2cpp_codegen_com_destroy_safe_array(Il2CppSafeArray* safeArray)
  483. {
  484. il2cpp::vm::COM::DestroySafeArray(safeArray);
  485. }
  486. void il2cpp_codegen_com_create_instance(const Il2CppGuid& clsid, Il2CppIUnknown** identity)
  487. {
  488. il2cpp::vm::COM::CreateInstance(clsid, identity);
  489. }
  490. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(const char* fromType, const char* toType)
  491. {
  492. return il2cpp::vm::CCW::HandleInvalidIPropertyConversion(fromType, toType);
  493. }
  494. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(RuntimeObject* value, const char* fromType, const char* toType)
  495. {
  496. return il2cpp::vm::CCW::HandleInvalidIPropertyConversion(value, fromType, toType);
  497. }
  498. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index)
  499. {
  500. return il2cpp::vm::CCW::HandleInvalidIPropertyArrayConversion(fromArrayType, fromElementType, toElementType, index);
  501. }
  502. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(RuntimeObject* value, const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index)
  503. {
  504. return il2cpp::vm::CCW::HandleInvalidIPropertyArrayConversion(value, fromArrayType, fromElementType, toElementType, index);
  505. }
  506. Il2CppIUnknown* il2cpp_codegen_com_get_or_create_ccw_internal(RuntimeObject* obj, const Il2CppGuid& iid)
  507. {
  508. return il2cpp::vm::CCW::GetOrCreate(obj, iid);
  509. }
  510. Il2CppObject* il2cpp_codegen_com_unpack_ccw(Il2CppIUnknown* obj)
  511. {
  512. return il2cpp::vm::CCW::Unpack(obj);
  513. }
  514. void il2cpp_codegen_com_register_rcw(Il2CppComObject* rcw)
  515. {
  516. il2cpp::vm::RCW::Register(rcw);
  517. }
  518. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass)
  519. {
  520. return il2cpp::vm::RCW::GetOrCreateFromIUnknown(unknown, fallbackClass);
  521. }
  522. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass)
  523. {
  524. return il2cpp::vm::RCW::GetOrCreateFromIInspectable(unknown, fallbackClass);
  525. }
  526. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal(Il2CppIUnknown* unknown, RuntimeClass* objectClass)
  527. {
  528. return il2cpp::vm::RCW::GetOrCreateForSealedClass(unknown, objectClass);
  529. }
  530. Il2CppIUnknown* il2cpp_codegen_com_query_interface_internal(Il2CppComObject* rcw, const Il2CppGuid& guid)
  531. {
  532. return il2cpp::vm::RCW::QueryInterfaceNoAddRef<true>(rcw, guid);
  533. }
  534. Il2CppIUnknown* il2cpp_codegen_com_query_interface_no_throw_internal(Il2CppComObject* rcw, const Il2CppGuid& guid)
  535. {
  536. return il2cpp::vm::RCW::QueryInterfaceNoAddRef<false>(rcw, guid);
  537. }
  538. void il2cpp_codegen_com_cache_queried_interface(Il2CppComObject* rcw, const Il2CppGuid& iid, Il2CppIUnknown* queriedInterface)
  539. {
  540. if (il2cpp::vm::RCW::CacheQueriedInterface(rcw, iid, queriedInterface))
  541. queriedInterface->AddRef();
  542. }
  543. void il2cpp_codegen_il2cpp_com_object_cleanup(Il2CppComObject* rcw)
  544. {
  545. il2cpp::vm::RCW::Cleanup(rcw);
  546. }
  547. String_t* il2cpp_codegen_string_new_wrapper(const char* str)
  548. {
  549. return (String_t*)il2cpp::vm::String::NewWrapper(str);
  550. }
  551. String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  552. {
  553. return (String_t*)il2cpp::vm::String::NewUtf16(str.Str(), static_cast<int32_t>(str.Length()));
  554. }
  555. RuntimeString* il2cpp_codegen_type_append_assembly_name_if_necessary(RuntimeString* typeName, const RuntimeMethod* callingMethod)
  556. {
  557. return il2cpp::vm::Type::AppendAssemblyNameIfNecessary(typeName, callingMethod);
  558. }
  559. Type_t* il2cpp_codegen_get_type(String_t* typeName, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod)
  560. {
  561. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  562. // Try to find the type using a hint about about calling assembly. If it is not found, fall back to calling GetType without the hint.
  563. Il2CppException* exc = NULL;
  564. void* params[] = {assemblyQualifiedTypeName};
  565. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  566. if (exc)
  567. il2cpp::vm::Exception::Raise(exc);
  568. if (type == NULL)
  569. {
  570. params[0] = typeName;
  571. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  572. if (exc)
  573. il2cpp::vm::Exception::Raise(exc);
  574. }
  575. return type;
  576. }
  577. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod)
  578. {
  579. typedef Type_t* (*getTypeFuncType)(String_t*, bool);
  580. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  581. // Try to find the type using a hint about about calling assembly. If it is not found, fall back to calling GetType without the hint.
  582. Il2CppException* exc = NULL;
  583. void* params[] = {assemblyQualifiedTypeName, &throwOnError};
  584. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  585. if (exc)
  586. il2cpp::vm::Exception::Raise(exc);
  587. if (type == NULL)
  588. {
  589. params[0] = typeName;
  590. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  591. if (exc)
  592. il2cpp::vm::Exception::Raise(exc);
  593. }
  594. return type;
  595. }
  596. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, bool ignoreCase, const RuntimeMethod* getTypeMethod , const RuntimeMethod* callingMethod)
  597. {
  598. typedef Type_t* (*getTypeFuncType)(String_t*, bool, bool);
  599. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  600. // Try to find the type using a hint about about calling assembly. If it is not found, fall back to calling GetType without the hint.
  601. Il2CppException* exc = NULL;
  602. void* params[] = {assemblyQualifiedTypeName, &throwOnError, &ignoreCase};
  603. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  604. if (exc)
  605. il2cpp::vm::Exception::Raise(exc);
  606. if (type == NULL)
  607. {
  608. params[0] = typeName;
  609. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  610. if (exc)
  611. il2cpp::vm::Exception::Raise(exc);
  612. }
  613. return type;
  614. }
  615. NORETURN void RaiseInvalidCastException(RuntimeObject* obj, RuntimeClass* targetType)
  616. {
  617. std::string exceptionMessage = il2cpp::utils::Exception::FormatInvalidCastException(obj->klass->element_class, targetType);
  618. Exception_t* exception = il2cpp_codegen_get_invalid_cast_exception(exceptionMessage.c_str());
  619. il2cpp_codegen_raise_exception(exception);
  620. }
  621. bool il2cpp_codegen_method_is_interface_method(RuntimeMethod* method)
  622. {
  623. return il2cpp::vm::Class::IsInterface(il2cpp_codegen_method_get_declaring_type(method));
  624. }
  625. bool il2cpp_codegen_class_is_assignable_from(RuntimeClass *klass, RuntimeClass *oklass)
  626. {
  627. return il2cpp::vm::Class::IsAssignableFrom(klass, oklass);
  628. }
  629. bool il2cpp_codegen_class_is_nullable(RuntimeClass* type)
  630. {
  631. return il2cpp::vm::Class::IsNullable(type);
  632. }
  633. RuntimeClass* il2cpp_codegen_get_generic_argument(RuntimeClass* klass, uint32_t argNum)
  634. {
  635. const Il2CppGenericInst* classInst = il2cpp_codegen_get_generic_class_inst(klass);
  636. IL2CPP_ASSERT(argNum < classInst->type_argc);
  637. return il2cpp::vm::Class::FromIl2CppType(classInst->type_argv[argNum]);
  638. }
  639. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const Il2CppGenericInst* genericInst)
  640. {
  641. return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, genericInst);
  642. }
  643. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const RuntimeType* p1, /*const RuntimeType*, const RuntimeType* */ ...)
  644. {
  645. IL2CPP_ASSERT(genericClassDefinition->is_generic);
  646. const uint32_t genericParameterCount = il2cpp::vm::MetadataCache::GetGenericContainerCount(genericClassDefinition->genericContainerHandle);
  647. const RuntimeType** types = (const RuntimeType**)alloca(sizeof(RuntimeType*) * genericParameterCount);
  648. types[0] = p1;
  649. if (genericParameterCount > 1)
  650. {
  651. va_list genericArguments;
  652. va_start(genericArguments, p1);
  653. for (uint32_t i = 1; i < genericParameterCount; i++)
  654. types[i] = va_arg(genericArguments, const RuntimeType*);
  655. va_end(genericArguments);
  656. }
  657. return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, il2cpp::vm::MetadataCache::GetGenericInst(types, genericParameterCount));
  658. }
  659. int32_t il2cpp_codgen_class_get_instance_size(RuntimeClass* klass)
  660. {
  661. return il2cpp::vm::Class::GetInstanceSize(klass);
  662. }
  663. RuntimeClass* il2cpp_codegen_class_from_type_internal(const RuntimeType* type)
  664. {
  665. return il2cpp::vm::Class::FromIl2CppType(type);
  666. }
  667. char* il2cpp_codegen_marshal_string(String_t* string)
  668. {
  669. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppString((RuntimeString*)string);
  670. }
  671. void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, int numberOfCharacters)
  672. {
  673. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  674. }
  675. Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  676. {
  677. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWString((RuntimeString*)string);
  678. }
  679. void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, int numberOfCharacters)
  680. {
  681. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  682. }
  683. Il2CppChar* il2cpp_codegen_marshal_bstring(String_t* string)
  684. {
  685. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppBString((RuntimeString*)string);
  686. }
  687. String_t* il2cpp_codegen_marshal_string_result(const char* value)
  688. {
  689. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value);
  690. }
  691. String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  692. {
  693. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult(value);
  694. }
  695. String_t* il2cpp_codegen_marshal_bstring_result(const Il2CppChar* value)
  696. {
  697. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppBStringToCSharpStringResult(value);
  698. }
  699. void il2cpp_codegen_marshal_free_bstring(Il2CppChar* value)
  700. {
  701. il2cpp::vm::PlatformInvoke::MarshalFreeBString(value);
  702. }
  703. char* il2cpp_codegen_marshal_empty_string_builder(StringBuilder_t* stringBuilder)
  704. {
  705. return il2cpp::vm::PlatformInvoke::MarshalEmptyStringBuilder((RuntimeStringBuilder*)stringBuilder);
  706. }
  707. char* il2cpp_codegen_marshal_string_builder(StringBuilder_t* stringBuilder)
  708. {
  709. return il2cpp::vm::PlatformInvoke::MarshalStringBuilder((RuntimeStringBuilder*)stringBuilder);
  710. }
  711. Il2CppChar* il2cpp_codegen_marshal_empty_wstring_builder(StringBuilder_t* stringBuilder)
  712. {
  713. return il2cpp::vm::PlatformInvoke::MarshalEmptyWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  714. }
  715. Il2CppChar* il2cpp_codegen_marshal_wstring_builder(StringBuilder_t* stringBuilder)
  716. {
  717. return il2cpp::vm::PlatformInvoke::MarshalWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  718. }
  719. void il2cpp_codegen_marshal_string_builder_result(StringBuilder_t* stringBuilder, char* buffer)
  720. {
  721. il2cpp::vm::PlatformInvoke::MarshalStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  722. }
  723. void il2cpp_codegen_marshal_wstring_builder_result(StringBuilder_t* stringBuilder, Il2CppChar* buffer)
  724. {
  725. il2cpp::vm::PlatformInvoke::MarshalWStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  726. }
  727. void il2cpp_codegen_marshal_free(void* ptr)
  728. {
  729. il2cpp::vm::PlatformInvoke::MarshalFree(ptr);
  730. }
  731. Il2CppMethodPointer il2cpp_codegen_marshal_delegate(MulticastDelegate_t* d)
  732. {
  733. return (Il2CppMethodPointer)il2cpp::vm::PlatformInvoke::MarshalDelegate((RuntimeDelegate*)d);
  734. }
  735. Il2CppDelegate* il2cpp_codegen_marshal_function_ptr_to_delegate_internal(void* functionPtr, Il2CppClass* delegateType)
  736. {
  737. return il2cpp::vm::PlatformInvoke::MarshalFunctionPointerToDelegate(functionPtr, delegateType);
  738. }
  739. bool il2cpp_codegen_is_marshalled_delegate(MulticastDelegate_t* d)
  740. {
  741. return il2cpp::vm::PlatformInvoke::IsFakeDelegateMethodMarshaledFromNativeCode((const RuntimeDelegate*)d);
  742. }
  743. Il2CppMethodPointer il2cpp_codegen_resolve(const PInvokeArguments& pinvokeArgs)
  744. {
  745. return il2cpp::vm::PlatformInvoke::Resolve(pinvokeArgs);
  746. }
  747. Il2CppHString il2cpp_codegen_create_hstring(String_t* str)
  748. {
  749. return il2cpp::vm::WindowsRuntime::CreateHString(reinterpret_cast<RuntimeString*>(str));
  750. }
  751. String_t* il2cpp_codegen_marshal_hstring_result(Il2CppHString hstring)
  752. {
  753. return reinterpret_cast<String_t*>(il2cpp::vm::WindowsRuntime::HStringToManagedString(hstring));
  754. }
  755. void il2cpp_codegen_marshal_free_hstring(Il2CppHString hstring)
  756. {
  757. il2cpp::vm::WindowsRuntime::DeleteHString(hstring);
  758. }
  759. void il2cpp_codegen_marshal_type_to_native(Type_t* type, Il2CppWindowsRuntimeTypeName& nativeType)
  760. {
  761. return il2cpp::vm::WindowsRuntime::MarshalTypeToNative(type != NULL ? reinterpret_cast<Il2CppReflectionType*>(type)->type : NULL, nativeType);
  762. }
  763. const Il2CppType* il2cpp_codegen_marshal_type_from_native_internal(Il2CppWindowsRuntimeTypeName& nativeType)
  764. {
  765. return il2cpp::vm::WindowsRuntime::MarshalTypeFromNative(nativeType);
  766. }
  767. void il2cpp_codegen_delete_native_type(Il2CppWindowsRuntimeTypeName& nativeType)
  768. {
  769. return il2cpp::vm::WindowsRuntime::DeleteNativeType(nativeType);
  770. }
  771. Il2CppIActivationFactory* il2cpp_codegen_windows_runtime_get_activation_factory(const il2cpp::utils::StringView<Il2CppNativeChar>& runtimeClassName)
  772. {
  773. return il2cpp::vm::WindowsRuntime::GetActivationFactory(runtimeClassName);
  774. }
  775. void il2cpp_codegen_stacktrace_push_frame(Il2CppStackFrameInfo& frame)
  776. {
  777. il2cpp::vm::StackTrace::PushFrame(frame);
  778. }
  779. void il2cpp_codegen_stacktrace_pop_frame()
  780. {
  781. il2cpp::vm::StackTrace::PopFrame();
  782. }
  783. void il2cpp_codegen_array_unsafe_mov(RuntimeClass * destClass, void* dest, RuntimeClass * srcClass, void* src)
  784. {
  785. // A runtime implementation of System.Array::UnsafeMov
  786. IL2CPP_ASSERT(destClass);
  787. IL2CPP_ASSERT(dest);
  788. IL2CPP_ASSERT(srcClass);
  789. IL2CPP_ASSERT(src);
  790. uint32_t destSize = il2cpp_codegen_sizeof(destClass);
  791. uint32_t srcSize = il2cpp_codegen_sizeof(srcClass);
  792. // If the types are the same size we can just memcpy them
  793. // otherwise we need to "move" them using the correct casting rules for primitive types
  794. if (destSize == srcSize)
  795. {
  796. il2cpp_codegen_memcpy(dest, src, destSize);
  797. return;
  798. }
  799. const Il2CppType* destType = il2cpp::vm::Class::IsEnum(destClass) ? il2cpp::vm::Class::GetEnumBaseType(destClass) : &destClass->byval_arg;
  800. const Il2CppType* srcType = il2cpp::vm::Class::IsEnum(srcClass) ? il2cpp::vm::Class::GetEnumBaseType(srcClass) : &srcClass->byval_arg;
  801. switch (destType->type)
  802. {
  803. case IL2CPP_TYPE_BOOLEAN:
  804. il2cpp_codegen_array_unsafe_mov_primitive<bool>(destType, (bool*)dest, srcType, src);
  805. break;
  806. case IL2CPP_TYPE_I1:
  807. il2cpp_codegen_array_unsafe_mov_primitive<int8_t>(destType, (int8_t *)dest, srcType, src);
  808. break;
  809. case IL2CPP_TYPE_U1:
  810. il2cpp_codegen_array_unsafe_mov_primitive<uint8_t>(destType, (uint8_t *)dest, srcType, src);
  811. break;
  812. case IL2CPP_TYPE_I2:
  813. il2cpp_codegen_array_unsafe_mov_primitive<int16_t>(destType, (int16_t *)dest, srcType, src);
  814. break;
  815. case IL2CPP_TYPE_CHAR:
  816. case IL2CPP_TYPE_U2:
  817. il2cpp_codegen_array_unsafe_mov_primitive<uint16_t>(destType, (uint16_t *)dest, srcType, src);
  818. break;
  819. case IL2CPP_TYPE_I4:
  820. il2cpp_codegen_array_unsafe_mov_primitive<int32_t>(destType, (int32_t *)dest, srcType, src);
  821. break;
  822. case IL2CPP_TYPE_U4:
  823. il2cpp_codegen_array_unsafe_mov_primitive<uint32_t>(destType, (uint32_t *)dest, srcType, src);
  824. break;
  825. case IL2CPP_TYPE_I8:
  826. il2cpp_codegen_array_unsafe_mov_primitive<int64_t>(destType, (int64_t *)dest, srcType, src);
  827. break;
  828. case IL2CPP_TYPE_U8:
  829. il2cpp_codegen_array_unsafe_mov_primitive<uint64_t>(destType, (uint64_t *)dest, srcType, src);
  830. break;
  831. case IL2CPP_TYPE_I:
  832. il2cpp_codegen_array_unsafe_mov_primitive<intptr_t>(destType, (intptr_t *)dest, srcType, src);
  833. break;
  834. case IL2CPP_TYPE_U:
  835. il2cpp_codegen_array_unsafe_mov_primitive<uintptr_t>(destType, (uintptr_t *)dest, srcType, src);
  836. break;
  837. default:
  838. il2cpp_codegen_array_unsafe_mov_type_exception(destType, srcType);
  839. }
  840. }
  841. NORETURN void il2cpp_codegen_array_unsafe_mov_type_exception(const RuntimeType * destType, const RuntimeType* srcType)
  842. {
  843. // No other primitive types are supported by Array::UnsafeMov (floating point types are not supported)
  844. // Or we're trying to assign structs of different sizes
  845. IL2CPP_ASSERT(false);
  846. std::string msg;
  847. msg += "Unsupported call to ";
  848. msg += il2cpp::vm::Class::GetName(il2cpp::vm::Class::FromIl2CppType(destType));
  849. msg += " System.Array::UnsafeMov(";
  850. msg += il2cpp::vm::Class::GetName(il2cpp::vm::Class::FromIl2CppType(srcType));
  851. msg += ");";
  852. il2cpp_codegen_raise_exception(il2cpp_codegen_get_not_supported_exception(msg.c_str()));
  853. }
  854. void il2cpp_codegen_runtime_constrained_call(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* boxBuffer, void* objBuffer, void** args, void* retVal)
  855. {
  856. // Reference types use a virtual method call
  857. if (!type->byval_arg.valuetype)
  858. {
  859. RuntimeObject* refObj = *(RuntimeObject**)objBuffer;
  860. NullCheck(refObj);
  861. const MethodInfo* virtualMethod = il2cpp::vm::Object::GetVirtualMethod(refObj, constrainedMethod);
  862. virtualMethod->invoker_method(virtualMethod->virtualMethodPointer, virtualMethod, refObj, args, retVal);
  863. }
  864. // For value types, the constrained RGCTX does our lookup for us
  865. else if (type == constrainedMethod->klass)
  866. {
  867. il2cpp_codegen_runtime_class_init_inline(type);
  868. // If the value type overrode the method, do a direct call wiht the pointer to the struct
  869. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, objBuffer, args, retVal);
  870. }
  871. else if (il2cpp::vm::Class::IsInterface(constrainedMethod->klass))
  872. {
  873. // We are invoking a default interface method on a struct
  874. // We need to box to call the interface method, and the boxing is observable
  875. IL2CPP_ASSERT(type->byval_arg.valuetype);
  876. RuntimeObject* boxed = il2cpp::vm::Object::Box(type, objBuffer);
  877. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, boxed, args, retVal);
  878. }
  879. else
  880. {
  881. IL2CPP_ASSERT(constrainedMethod->klass == il2cpp_defaults.object_class || constrainedMethod->klass == il2cpp_defaults.value_type_class || constrainedMethod->klass == il2cpp_defaults.enum_class);
  882. // The value type did not override the method, so we're making a call to a method declared on
  883. // System.Object, System.ValueType, or System.Enum so we need to box, but fake boxing should work
  884. // because these methods will not mutate "this" and we can assume that they do not store the "this" pointer past the call
  885. if (il2cpp::vm::Class::IsNullable(type))
  886. {
  887. if (!il2cpp::vm::Object::NullableHasValue(type, objBuffer))
  888. il2cpp_codegen_raise_null_reference_exception();
  889. type = il2cpp::vm::Class::GetNullableArgument(type);
  890. }
  891. Il2CppFakeBoxBuffer* boxed = new(boxBuffer) Il2CppFakeBoxBuffer(type, objBuffer);
  892. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, boxed, args, retVal);
  893. }
  894. }
  895. void* il2cpp_codegen_runtime_box_constrained_this(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* obj)
  896. {
  897. // We are calling a method defined on the type, no need to box
  898. IL2CPP_ASSERT(il2cpp::vm::Class::IsValuetype(type));
  899. if (type == constrainedMethod->klass)
  900. return obj;
  901. // We are calling a default interface method with a value type, we have to box
  902. IL2CPP_ASSERT(il2cpp::vm::Class::IsInterface(constrainedMethod->klass));
  903. return il2cpp::vm::Object::Box(type, obj);
  904. }
  905. bool il2cpp_codegen_is_reference_or_contains_references(const RuntimeMethod* method)
  906. {
  907. IL2CPP_ASSERT(il2cpp::vm::Method::IsGenericInstance(method));
  908. const Il2CppGenericContext* context = il2cpp::metadata::GenericMethod::GetContext(method->genericMethod);
  909. IL2CPP_ASSERT(context->method_inst);
  910. IL2CPP_ASSERT(context->method_inst->type_argc == 1);
  911. IL2CPP_ASSERT(!il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(context->method_inst->type_argv[0], &il2cpp_defaults.il2cpp_fully_shared_type->byval_arg));
  912. const Il2CppType* type = context->method_inst->type_argv[0];
  913. if (!type->valuetype)
  914. return true;
  915. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  916. il2cpp::vm::ClassInlines::InitFromCodegen(klass);
  917. return klass->has_references;
  918. }
  919. bool il2cpp_codegen_is_unmanaged(const RuntimeMethod* method)
  920. {
  921. return !il2cpp_codegen_is_reference_or_contains_references(method);
  922. }