il2cpp-codegen.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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_marshal_directive_exception(const char* msg, const RuntimeType* type)
  431. {
  432. auto formattedMsg = il2cpp::utils::StringUtils::Printf(msg, il2cpp::vm::Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_FULL_NAME).c_str());
  433. return (Exception_t*)il2cpp::vm::Exception::GetMarshalDirectiveException(formattedMsg.c_str());
  434. }
  435. // format string will require first instance as a field and second instance as a type or this will break
  436. Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg, const RuntimeField *field, const RuntimeType* type)
  437. {
  438. auto formattedMsg = il2cpp::utils::StringUtils::Printf(msg, il2cpp::vm::Field::GetName(field), il2cpp::vm::Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_FULL_NAME).c_str());
  439. return (Exception_t*)il2cpp::vm::Exception::GetMarshalDirectiveException(formattedMsg.c_str());
  440. }
  441. Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  442. {
  443. return (Exception_t*)il2cpp::vm::Exception::GetMissingMethodException(msg);
  444. }
  445. Exception_t* il2cpp_codegen_get_maximum_nested_generics_exception()
  446. {
  447. return (Exception_t*)il2cpp::vm::Exception::GetMaximumNestedGenericsException();
  448. }
  449. Exception_t* il2cpp_codegen_get_engine_execution_exception(const char* msg)
  450. {
  451. return (Exception_t*)il2cpp::vm::Exception::GetExecutionEngineException(msg);
  452. }
  453. Exception_t* il2cpp_codegen_get_index_out_of_range_exception()
  454. {
  455. return (Exception_t*)il2cpp::vm::Exception::GetIndexOutOfRangeException();
  456. }
  457. Exception_t* il2cpp_codegen_get_exception(il2cpp_hresult_t hresult, bool defaultToCOMException)
  458. {
  459. return (Exception_t*)il2cpp::vm::Exception::Get(hresult, defaultToCOMException);
  460. }
  461. void il2cpp_codegen_store_exception_info(RuntimeException* ex, String_t* exceptionString)
  462. {
  463. il2cpp::vm::Exception::StoreExceptionInfo(ex, reinterpret_cast<RuntimeString*>(exceptionString));
  464. }
  465. void il2cpp_codegen_com_marshal_variant(RuntimeObject* obj, Il2CppVariant* variant)
  466. {
  467. il2cpp::vm::COM::MarshalVariant(obj, variant);
  468. }
  469. RuntimeObject* il2cpp_codegen_com_marshal_variant_result(const Il2CppVariant* variant)
  470. {
  471. return il2cpp::vm::COM::MarshalVariantResult(variant);
  472. }
  473. void il2cpp_codegen_com_destroy_variant(Il2CppVariant* variant)
  474. {
  475. il2cpp::vm::COM::DestroyVariant(variant);
  476. }
  477. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array(Il2CppChar type, RuntimeArray* managedArray)
  478. {
  479. return il2cpp::vm::COM::MarshalSafeArray(type, managedArray);
  480. }
  481. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_result(Il2CppChar variantType, RuntimeClass* type, Il2CppSafeArray* safeArray)
  482. {
  483. return il2cpp::vm::COM::MarshalSafeArrayResult(variantType, type, safeArray);
  484. }
  485. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array_bstring(RuntimeArray* managedArray)
  486. {
  487. return il2cpp::vm::COM::MarshalSafeArrayBString(managedArray);
  488. }
  489. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_bstring_result(RuntimeClass* type, Il2CppSafeArray* safeArray)
  490. {
  491. return il2cpp::vm::COM::MarshalSafeArrayBStringResult(type, safeArray);
  492. }
  493. void il2cpp_codegen_com_destroy_safe_array(Il2CppSafeArray* safeArray)
  494. {
  495. il2cpp::vm::COM::DestroySafeArray(safeArray);
  496. }
  497. void il2cpp_codegen_com_create_instance(const Il2CppGuid& clsid, Il2CppIUnknown** identity)
  498. {
  499. il2cpp::vm::COM::CreateInstance(clsid, identity);
  500. }
  501. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(const char* fromType, const char* toType)
  502. {
  503. return il2cpp::vm::CCW::HandleInvalidIPropertyConversion(fromType, toType);
  504. }
  505. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(RuntimeObject* value, const char* fromType, const char* toType)
  506. {
  507. return il2cpp::vm::CCW::HandleInvalidIPropertyConversion(value, fromType, toType);
  508. }
  509. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index)
  510. {
  511. return il2cpp::vm::CCW::HandleInvalidIPropertyArrayConversion(fromArrayType, fromElementType, toElementType, index);
  512. }
  513. 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)
  514. {
  515. return il2cpp::vm::CCW::HandleInvalidIPropertyArrayConversion(value, fromArrayType, fromElementType, toElementType, index);
  516. }
  517. Il2CppIUnknown* il2cpp_codegen_com_get_or_create_ccw_internal(RuntimeObject* obj, const Il2CppGuid& iid)
  518. {
  519. return il2cpp::vm::CCW::GetOrCreate(obj, iid);
  520. }
  521. Il2CppObject* il2cpp_codegen_com_unpack_ccw(Il2CppIUnknown* obj)
  522. {
  523. return il2cpp::vm::CCW::Unpack(obj);
  524. }
  525. void il2cpp_codegen_com_register_rcw(Il2CppComObject* rcw)
  526. {
  527. il2cpp::vm::RCW::Register(rcw);
  528. }
  529. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass)
  530. {
  531. return il2cpp::vm::RCW::GetOrCreateFromIUnknown(unknown, fallbackClass);
  532. }
  533. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass)
  534. {
  535. return il2cpp::vm::RCW::GetOrCreateFromIInspectable(unknown, fallbackClass);
  536. }
  537. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal(Il2CppIUnknown* unknown, RuntimeClass* objectClass)
  538. {
  539. return il2cpp::vm::RCW::GetOrCreateForSealedClass(unknown, objectClass);
  540. }
  541. Il2CppIUnknown* il2cpp_codegen_com_query_interface_internal(Il2CppComObject* rcw, const Il2CppGuid& guid)
  542. {
  543. return il2cpp::vm::RCW::QueryInterfaceNoAddRef<true>(rcw, guid);
  544. }
  545. Il2CppIUnknown* il2cpp_codegen_com_query_interface_no_throw_internal(Il2CppComObject* rcw, const Il2CppGuid& guid)
  546. {
  547. return il2cpp::vm::RCW::QueryInterfaceNoAddRef<false>(rcw, guid);
  548. }
  549. void il2cpp_codegen_com_cache_queried_interface(Il2CppComObject* rcw, const Il2CppGuid& iid, Il2CppIUnknown* queriedInterface)
  550. {
  551. if (il2cpp::vm::RCW::CacheQueriedInterface(rcw, iid, queriedInterface))
  552. queriedInterface->AddRef();
  553. }
  554. void il2cpp_codegen_il2cpp_com_object_cleanup(Il2CppComObject* rcw)
  555. {
  556. il2cpp::vm::RCW::Cleanup(rcw);
  557. }
  558. String_t* il2cpp_codegen_string_new_wrapper(const char* str)
  559. {
  560. return (String_t*)il2cpp::vm::String::NewWrapper(str);
  561. }
  562. String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  563. {
  564. return (String_t*)il2cpp::vm::String::NewUtf16(str.Str(), static_cast<int32_t>(str.Length()));
  565. }
  566. RuntimeString* il2cpp_codegen_type_append_assembly_name_if_necessary(RuntimeString* typeName, const RuntimeMethod* callingMethod)
  567. {
  568. return il2cpp::vm::Type::AppendAssemblyNameIfNecessary(typeName, callingMethod);
  569. }
  570. Type_t* il2cpp_codegen_get_type(String_t* typeName, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod)
  571. {
  572. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  573. // 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.
  574. Il2CppException* exc = NULL;
  575. void* params[] = {assemblyQualifiedTypeName};
  576. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  577. if (exc)
  578. il2cpp::vm::Exception::Raise(exc);
  579. if (type == NULL)
  580. {
  581. params[0] = typeName;
  582. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  583. if (exc)
  584. il2cpp::vm::Exception::Raise(exc);
  585. }
  586. return type;
  587. }
  588. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod)
  589. {
  590. typedef Type_t* (*getTypeFuncType)(String_t*, bool);
  591. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  592. // 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.
  593. Il2CppException* exc = NULL;
  594. void* params[] = {assemblyQualifiedTypeName, &throwOnError};
  595. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  596. if (exc)
  597. il2cpp::vm::Exception::Raise(exc);
  598. if (type == NULL)
  599. {
  600. params[0] = typeName;
  601. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  602. if (exc)
  603. il2cpp::vm::Exception::Raise(exc);
  604. }
  605. return type;
  606. }
  607. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, bool ignoreCase, const RuntimeMethod* getTypeMethod , const RuntimeMethod* callingMethod)
  608. {
  609. typedef Type_t* (*getTypeFuncType)(String_t*, bool, bool);
  610. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  611. // 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.
  612. Il2CppException* exc = NULL;
  613. void* params[] = {assemblyQualifiedTypeName, &throwOnError, &ignoreCase};
  614. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  615. if (exc)
  616. il2cpp::vm::Exception::Raise(exc);
  617. if (type == NULL)
  618. {
  619. params[0] = typeName;
  620. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  621. if (exc)
  622. il2cpp::vm::Exception::Raise(exc);
  623. }
  624. return type;
  625. }
  626. NORETURN void RaiseInvalidCastException(RuntimeObject* obj, RuntimeClass* targetType)
  627. {
  628. std::string exceptionMessage = il2cpp::utils::Exception::FormatInvalidCastException(obj->klass->element_class, targetType);
  629. Exception_t* exception = il2cpp_codegen_get_invalid_cast_exception(exceptionMessage.c_str());
  630. il2cpp_codegen_raise_exception(exception);
  631. }
  632. bool il2cpp_codegen_method_is_interface_method(RuntimeMethod* method)
  633. {
  634. return il2cpp::vm::Class::IsInterface(il2cpp_codegen_method_get_declaring_type(method));
  635. }
  636. bool il2cpp_codegen_class_is_assignable_from(RuntimeClass *klass, RuntimeClass *oklass)
  637. {
  638. return il2cpp::vm::Class::IsAssignableFrom(klass, oklass);
  639. }
  640. bool il2cpp_codegen_class_is_nullable(RuntimeClass* type)
  641. {
  642. return il2cpp::vm::Class::IsNullable(type);
  643. }
  644. RuntimeClass* il2cpp_codegen_get_generic_argument(RuntimeClass* klass, uint32_t argNum)
  645. {
  646. const Il2CppGenericInst* classInst = il2cpp_codegen_get_generic_class_inst(klass);
  647. IL2CPP_ASSERT(argNum < classInst->type_argc);
  648. return il2cpp::vm::Class::FromIl2CppType(classInst->type_argv[argNum]);
  649. }
  650. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const Il2CppGenericInst* genericInst)
  651. {
  652. return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, genericInst);
  653. }
  654. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const RuntimeType* p1, /*const RuntimeType*, const RuntimeType* */ ...)
  655. {
  656. IL2CPP_ASSERT(genericClassDefinition->is_generic);
  657. const uint32_t genericParameterCount = il2cpp::vm::MetadataCache::GetGenericContainerCount(genericClassDefinition->genericContainerHandle);
  658. const RuntimeType** types = (const RuntimeType**)alloca(sizeof(RuntimeType*) * genericParameterCount);
  659. types[0] = p1;
  660. if (genericParameterCount > 1)
  661. {
  662. va_list genericArguments;
  663. va_start(genericArguments, p1);
  664. for (uint32_t i = 1; i < genericParameterCount; i++)
  665. types[i] = va_arg(genericArguments, const RuntimeType*);
  666. va_end(genericArguments);
  667. }
  668. return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, il2cpp::vm::MetadataCache::GetGenericInst(types, genericParameterCount));
  669. }
  670. int32_t il2cpp_codgen_class_get_instance_size(RuntimeClass* klass)
  671. {
  672. return il2cpp::vm::Class::GetInstanceSize(klass);
  673. }
  674. RuntimeClass* il2cpp_codegen_class_from_type_internal(const RuntimeType* type)
  675. {
  676. return il2cpp::vm::Class::FromIl2CppType(type);
  677. }
  678. char* il2cpp_codegen_marshal_string(String_t* string)
  679. {
  680. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppString((RuntimeString*)string);
  681. }
  682. void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, int numberOfCharacters)
  683. {
  684. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  685. }
  686. Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  687. {
  688. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWString((RuntimeString*)string);
  689. }
  690. void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, int numberOfCharacters)
  691. {
  692. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  693. }
  694. Il2CppChar* il2cpp_codegen_marshal_bstring(String_t* string)
  695. {
  696. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppBString((RuntimeString*)string);
  697. }
  698. String_t* il2cpp_codegen_marshal_string_result(const char* value)
  699. {
  700. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value);
  701. }
  702. String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  703. {
  704. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult(value);
  705. }
  706. String_t* il2cpp_codegen_marshal_bstring_result(const Il2CppChar* value)
  707. {
  708. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppBStringToCSharpStringResult(value);
  709. }
  710. void il2cpp_codegen_marshal_free_bstring(Il2CppChar* value)
  711. {
  712. il2cpp::vm::PlatformInvoke::MarshalFreeBString(value);
  713. }
  714. char* il2cpp_codegen_marshal_empty_string_builder(StringBuilder_t* stringBuilder)
  715. {
  716. return il2cpp::vm::PlatformInvoke::MarshalEmptyStringBuilder((RuntimeStringBuilder*)stringBuilder);
  717. }
  718. char* il2cpp_codegen_marshal_string_builder(StringBuilder_t* stringBuilder)
  719. {
  720. return il2cpp::vm::PlatformInvoke::MarshalStringBuilder((RuntimeStringBuilder*)stringBuilder);
  721. }
  722. Il2CppChar* il2cpp_codegen_marshal_empty_wstring_builder(StringBuilder_t* stringBuilder)
  723. {
  724. return il2cpp::vm::PlatformInvoke::MarshalEmptyWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  725. }
  726. Il2CppChar* il2cpp_codegen_marshal_wstring_builder(StringBuilder_t* stringBuilder)
  727. {
  728. return il2cpp::vm::PlatformInvoke::MarshalWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  729. }
  730. void il2cpp_codegen_marshal_string_builder_result(StringBuilder_t* stringBuilder, char* buffer)
  731. {
  732. il2cpp::vm::PlatformInvoke::MarshalStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  733. }
  734. void il2cpp_codegen_marshal_wstring_builder_result(StringBuilder_t* stringBuilder, Il2CppChar* buffer)
  735. {
  736. il2cpp::vm::PlatformInvoke::MarshalWStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  737. }
  738. void il2cpp_codegen_marshal_free(void* ptr)
  739. {
  740. il2cpp::vm::PlatformInvoke::MarshalFree(ptr);
  741. }
  742. Il2CppMethodPointer il2cpp_codegen_marshal_delegate(MulticastDelegate_t* d)
  743. {
  744. return (Il2CppMethodPointer)il2cpp::vm::PlatformInvoke::MarshalDelegate((RuntimeDelegate*)d);
  745. }
  746. Il2CppDelegate* il2cpp_codegen_marshal_function_ptr_to_delegate_internal(void* functionPtr, Il2CppClass* delegateType)
  747. {
  748. return il2cpp::vm::PlatformInvoke::MarshalFunctionPointerToDelegate(functionPtr, delegateType);
  749. }
  750. bool il2cpp_codegen_is_marshalled_delegate(MulticastDelegate_t* d)
  751. {
  752. return il2cpp::vm::PlatformInvoke::IsFakeDelegateMethodMarshaledFromNativeCode((const RuntimeDelegate*)d);
  753. }
  754. Il2CppMethodPointer il2cpp_codegen_resolve(const PInvokeArguments& pinvokeArgs)
  755. {
  756. return il2cpp::vm::PlatformInvoke::Resolve(pinvokeArgs);
  757. }
  758. Il2CppHString il2cpp_codegen_create_hstring(String_t* str)
  759. {
  760. return il2cpp::vm::WindowsRuntime::CreateHString(reinterpret_cast<RuntimeString*>(str));
  761. }
  762. String_t* il2cpp_codegen_marshal_hstring_result(Il2CppHString hstring)
  763. {
  764. return reinterpret_cast<String_t*>(il2cpp::vm::WindowsRuntime::HStringToManagedString(hstring));
  765. }
  766. void il2cpp_codegen_marshal_free_hstring(Il2CppHString hstring)
  767. {
  768. il2cpp::vm::WindowsRuntime::DeleteHString(hstring);
  769. }
  770. void il2cpp_codegen_marshal_type_to_native(Type_t* type, Il2CppWindowsRuntimeTypeName& nativeType)
  771. {
  772. return il2cpp::vm::WindowsRuntime::MarshalTypeToNative(type != NULL ? reinterpret_cast<Il2CppReflectionType*>(type)->type : NULL, nativeType);
  773. }
  774. const Il2CppType* il2cpp_codegen_marshal_type_from_native_internal(Il2CppWindowsRuntimeTypeName& nativeType)
  775. {
  776. return il2cpp::vm::WindowsRuntime::MarshalTypeFromNative(nativeType);
  777. }
  778. void il2cpp_codegen_delete_native_type(Il2CppWindowsRuntimeTypeName& nativeType)
  779. {
  780. return il2cpp::vm::WindowsRuntime::DeleteNativeType(nativeType);
  781. }
  782. Il2CppIActivationFactory* il2cpp_codegen_windows_runtime_get_activation_factory(const il2cpp::utils::StringView<Il2CppNativeChar>& runtimeClassName)
  783. {
  784. return il2cpp::vm::WindowsRuntime::GetActivationFactory(runtimeClassName);
  785. }
  786. void il2cpp_codegen_stacktrace_push_frame(Il2CppStackFrameInfo& frame)
  787. {
  788. il2cpp::vm::StackTrace::PushFrame(frame);
  789. }
  790. void il2cpp_codegen_stacktrace_pop_frame()
  791. {
  792. il2cpp::vm::StackTrace::PopFrame();
  793. }
  794. void il2cpp_codegen_array_unsafe_mov(RuntimeClass * destClass, void* dest, RuntimeClass * srcClass, void* src)
  795. {
  796. // A runtime implementation of System.Array::UnsafeMov
  797. IL2CPP_ASSERT(destClass);
  798. IL2CPP_ASSERT(dest);
  799. IL2CPP_ASSERT(srcClass);
  800. IL2CPP_ASSERT(src);
  801. uint32_t destSize = il2cpp_codegen_sizeof(destClass);
  802. uint32_t srcSize = il2cpp_codegen_sizeof(srcClass);
  803. // If the types are the same size we can just memcpy them
  804. // otherwise we need to "move" them using the correct casting rules for primitive types
  805. if (destSize == srcSize)
  806. {
  807. il2cpp_codegen_memcpy(dest, src, destSize);
  808. return;
  809. }
  810. const Il2CppType* destType = il2cpp::vm::Class::IsEnum(destClass) ? il2cpp::vm::Class::GetEnumBaseType(destClass) : &destClass->byval_arg;
  811. const Il2CppType* srcType = il2cpp::vm::Class::IsEnum(srcClass) ? il2cpp::vm::Class::GetEnumBaseType(srcClass) : &srcClass->byval_arg;
  812. switch (destType->type)
  813. {
  814. case IL2CPP_TYPE_BOOLEAN:
  815. il2cpp_codegen_array_unsafe_mov_primitive<bool>(destType, (bool*)dest, srcType, src);
  816. break;
  817. case IL2CPP_TYPE_I1:
  818. il2cpp_codegen_array_unsafe_mov_primitive<int8_t>(destType, (int8_t *)dest, srcType, src);
  819. break;
  820. case IL2CPP_TYPE_U1:
  821. il2cpp_codegen_array_unsafe_mov_primitive<uint8_t>(destType, (uint8_t *)dest, srcType, src);
  822. break;
  823. case IL2CPP_TYPE_I2:
  824. il2cpp_codegen_array_unsafe_mov_primitive<int16_t>(destType, (int16_t *)dest, srcType, src);
  825. break;
  826. case IL2CPP_TYPE_CHAR:
  827. case IL2CPP_TYPE_U2:
  828. il2cpp_codegen_array_unsafe_mov_primitive<uint16_t>(destType, (uint16_t *)dest, srcType, src);
  829. break;
  830. case IL2CPP_TYPE_I4:
  831. il2cpp_codegen_array_unsafe_mov_primitive<int32_t>(destType, (int32_t *)dest, srcType, src);
  832. break;
  833. case IL2CPP_TYPE_U4:
  834. il2cpp_codegen_array_unsafe_mov_primitive<uint32_t>(destType, (uint32_t *)dest, srcType, src);
  835. break;
  836. case IL2CPP_TYPE_I8:
  837. il2cpp_codegen_array_unsafe_mov_primitive<int64_t>(destType, (int64_t *)dest, srcType, src);
  838. break;
  839. case IL2CPP_TYPE_U8:
  840. il2cpp_codegen_array_unsafe_mov_primitive<uint64_t>(destType, (uint64_t *)dest, srcType, src);
  841. break;
  842. case IL2CPP_TYPE_I:
  843. il2cpp_codegen_array_unsafe_mov_primitive<intptr_t>(destType, (intptr_t *)dest, srcType, src);
  844. break;
  845. case IL2CPP_TYPE_U:
  846. il2cpp_codegen_array_unsafe_mov_primitive<uintptr_t>(destType, (uintptr_t *)dest, srcType, src);
  847. break;
  848. default:
  849. il2cpp_codegen_array_unsafe_mov_type_exception(destType, srcType);
  850. }
  851. }
  852. NORETURN void il2cpp_codegen_array_unsafe_mov_type_exception(const RuntimeType * destType, const RuntimeType* srcType)
  853. {
  854. // No other primitive types are supported by Array::UnsafeMov (floating point types are not supported)
  855. // Or we're trying to assign structs of different sizes
  856. IL2CPP_ASSERT(false);
  857. std::string msg;
  858. msg += "Unsupported call to ";
  859. msg += il2cpp::vm::Class::GetName(il2cpp::vm::Class::FromIl2CppType(destType));
  860. msg += " System.Array::UnsafeMov(";
  861. msg += il2cpp::vm::Class::GetName(il2cpp::vm::Class::FromIl2CppType(srcType));
  862. msg += ");";
  863. il2cpp_codegen_raise_exception(il2cpp_codegen_get_not_supported_exception(msg.c_str()));
  864. }
  865. void il2cpp_codegen_runtime_constrained_call(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* boxBuffer, void* objBuffer, void** args, void* retVal)
  866. {
  867. // Reference types use a virtual method call
  868. if (!type->byval_arg.valuetype)
  869. {
  870. RuntimeObject* refObj = *(RuntimeObject**)objBuffer;
  871. NullCheck(refObj);
  872. const MethodInfo* virtualMethod = il2cpp::vm::Object::GetVirtualMethod(refObj, constrainedMethod);
  873. virtualMethod->invoker_method(virtualMethod->virtualMethodPointer, virtualMethod, refObj, args, retVal);
  874. }
  875. // For value types, the constrained RGCTX does our lookup for us
  876. else if (type == constrainedMethod->klass)
  877. {
  878. il2cpp_codegen_runtime_class_init_inline(type);
  879. // If the value type overrode the method, do a direct call wiht the pointer to the struct
  880. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, objBuffer, args, retVal);
  881. }
  882. else if (il2cpp::vm::Class::IsInterface(constrainedMethod->klass))
  883. {
  884. // We are invoking a default interface method on a struct
  885. // We need to box to call the interface method, and the boxing is observable
  886. IL2CPP_ASSERT(type->byval_arg.valuetype);
  887. RuntimeObject* boxed = il2cpp::vm::Object::Box(type, objBuffer);
  888. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, boxed, args, retVal);
  889. }
  890. else
  891. {
  892. IL2CPP_ASSERT(constrainedMethod->klass == il2cpp_defaults.object_class || constrainedMethod->klass == il2cpp_defaults.value_type_class || constrainedMethod->klass == il2cpp_defaults.enum_class);
  893. // The value type did not override the method, so we're making a call to a method declared on
  894. // System.Object, System.ValueType, or System.Enum so we need to box, but fake boxing should work
  895. // because these methods will not mutate "this" and we can assume that they do not store the "this" pointer past the call
  896. if (il2cpp::vm::Class::IsNullable(type))
  897. {
  898. if (!il2cpp::vm::Object::NullableHasValue(type, objBuffer))
  899. il2cpp_codegen_raise_null_reference_exception();
  900. type = il2cpp::vm::Class::GetNullableArgument(type);
  901. }
  902. Il2CppFakeBoxBuffer* boxed = new(boxBuffer) Il2CppFakeBoxBuffer(type, objBuffer);
  903. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, boxed, args, retVal);
  904. }
  905. }
  906. void* il2cpp_codegen_runtime_box_constrained_this(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* obj)
  907. {
  908. // We are calling a method defined on the type, no need to box
  909. IL2CPP_ASSERT(il2cpp::vm::Class::IsValuetype(type));
  910. if (type == constrainedMethod->klass)
  911. return obj;
  912. // We are calling a default interface method with a value type, we have to box
  913. IL2CPP_ASSERT(il2cpp::vm::Class::IsInterface(constrainedMethod->klass));
  914. return il2cpp::vm::Object::Box(type, obj);
  915. }
  916. bool il2cpp_codegen_is_reference_or_contains_references(const RuntimeMethod* method)
  917. {
  918. IL2CPP_ASSERT(il2cpp::vm::Method::IsGenericInstance(method));
  919. const Il2CppGenericContext* context = il2cpp::metadata::GenericMethod::GetContext(method->genericMethod);
  920. IL2CPP_ASSERT(context->method_inst);
  921. IL2CPP_ASSERT(context->method_inst->type_argc == 1);
  922. IL2CPP_ASSERT(!il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(context->method_inst->type_argv[0], &il2cpp_defaults.il2cpp_fully_shared_type->byval_arg));
  923. const Il2CppType* type = context->method_inst->type_argv[0];
  924. if (!type->valuetype)
  925. return true;
  926. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  927. il2cpp::vm::ClassInlines::InitFromCodegen(klass);
  928. return klass->has_references;
  929. }
  930. bool il2cpp_codegen_is_unmanaged(const RuntimeMethod* method)
  931. {
  932. return !il2cpp_codegen_is_reference_or_contains_references(method);
  933. }