il2cpp-codegen-il2cpp.cpp 41 KB

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