il2cpp-codegen-tiny.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. #pragma once
  2. #include "il2cpp-codegen-common.h"
  3. #include "il2cpp-object-internals.h"
  4. #include "il2cpp-debug-metadata.h"
  5. #include "gc/GarbageCollector.h"
  6. #include "gc/WriteBarrier.h"
  7. #include "os/Memory.h"
  8. #include "vm/Array.h"
  9. #include "vm/Exception.h"
  10. #include "vm/Object.h"
  11. #include "vm/PlatformInvoke.h"
  12. #include "vm/ScopedThreadAttacher.h"
  13. #include "vm/String.h"
  14. #include "vm/Runtime.h"
  15. #include "vm/Thread.h"
  16. #include "vm/Type.h"
  17. #include "vm/TypeUniverse.h"
  18. #include "vm-utils/Finally.h"
  19. #include "vm-utils/icalls/mscorlib/System.Threading/Interlocked.h"
  20. #include "vm-utils/VmThreadUtils.h"
  21. #include "utils/ExceptionSupportStack.h"
  22. #include "utils/MemoryUtils.h"
  23. #include "utils/StringView.h"
  24. #include <string>
  25. #include <cstdlib>
  26. struct Exception_t;
  27. struct Delegate_t;
  28. struct MulticastDelegate_t;
  29. struct String_t;
  30. struct Type_t;
  31. typedef Il2CppObject RuntimeObject;
  32. typedef Il2CppArray RuntimeArray;
  33. #if IL2CPP_COMPILER_MSVC
  34. #define DEFAULT_CALL STDCALL
  35. #else
  36. #define DEFAULT_CALL
  37. #endif
  38. inline RuntimeObject* il2cpp_codegen_object_new(size_t size, TinyType* typeInfo)
  39. {
  40. return (RuntimeObject*)tiny::vm::Object::New(size, typeInfo);
  41. }
  42. inline Il2CppObject* Box(TinyType* type, void* value, size_t size)
  43. {
  44. COMPILE_TIME_CONST size_t alignedObjectSize = IL2CPP_ALIGNED_OBJECT_SIZE;
  45. Il2CppObject* obj = il2cpp_codegen_object_new(size + alignedObjectSize, type);
  46. memcpy(reinterpret_cast<uint8_t*>(obj) + alignedObjectSize, value, size);
  47. il2cpp::gc::GarbageCollector::SetWriteBarrier((void**)(reinterpret_cast<uint8_t*>(obj) + alignedObjectSize), size);
  48. return obj;
  49. }
  50. static intptr_t align(intptr_t x, size_t alignment)
  51. {
  52. return (x + alignment - 1) & ~(alignment - 1);
  53. }
  54. template<typename ArgumentType>
  55. static uint8_t* NullableValueField(void* storage)
  56. {
  57. // The hasValue field is the first one in the Nullable struct. It is a one byte Boolean.
  58. // We're trying to get the address of the value field in the Nullable struct, so offset
  59. // past the hasValue field, then offset to the alignment value of the type stored in the
  60. // Nullable struct.
  61. uint8_t* byteAfterhasValueField = static_cast<uint8_t*>(storage) + 1;
  62. size_t alignmentOfArgumentType = alignof(ArgumentType);
  63. intptr_t offsetToAlign = 0;
  64. if ((intptr_t)byteAfterhasValueField % alignmentOfArgumentType != 0)
  65. offsetToAlign = alignmentOfArgumentType - 1;
  66. return byteAfterhasValueField + offsetToAlign;
  67. }
  68. template<typename NullableType, typename ArgumentType>
  69. inline Il2CppObject* BoxNullable(TinyType* type, NullableType* value)
  70. {
  71. /*
  72. From ECMA-335, I.8.2.4 Boxing and unboxing of values:
  73. All value types have an operation called box. Boxing a value of any value type produces its boxed value;
  74. i.e., a value of the corresponding boxed type containing a bitwise copy of the original value. If the
  75. value type is a nullable type defined as an instantiation of the value type System.Nullable<T> the result
  76. is a null reference or bitwise copy of its Value property of type T, depending on its HasValue property
  77. (false and true, respectively).
  78. */
  79. bool hasValue = *reinterpret_cast<bool*>(reinterpret_cast<uint8_t*>(value));
  80. if (!hasValue)
  81. return NULL;
  82. uint32_t valueSize = sizeof(ArgumentType);
  83. return Box(type, NullableValueField<ArgumentType>(value), valueSize);
  84. }
  85. inline void* UnBox(Il2CppObject* obj)
  86. {
  87. return tiny::vm::Object::Unbox(obj);
  88. }
  89. inline void* UnBox(Il2CppObject* obj, TinyType* expectedBoxedType)
  90. {
  91. COMPILE_TIME_CONST size_t alignedObjectSize = IL2CPP_ALIGNED_OBJECT_SIZE;
  92. if (obj->klass == expectedBoxedType)
  93. return reinterpret_cast<uint8_t*>(obj) + alignedObjectSize;
  94. tiny::vm::Exception::RaiseInvalidCastException(obj, expectedBoxedType);
  95. return NULL;
  96. }
  97. template<typename ArgumentType>
  98. inline void UnBoxNullable(Il2CppObject* obj, TinyType* expectedBoxedClass, void* storage)
  99. {
  100. // We assume storage is on the stack, if not we'll need a write barrier
  101. IL2CPP_ASSERT_STACK_PTR(storage);
  102. // We only need to do type checks if obj is not null
  103. // Unboxing null nullable is perfectly valid and returns an instance that has no value
  104. if (obj != NULL)
  105. {
  106. if (obj->klass != expectedBoxedClass)
  107. tiny::vm::Exception::RaiseInvalidCastException(obj, expectedBoxedClass);
  108. }
  109. uint32_t valueSize = sizeof(ArgumentType);
  110. if (obj == NULL)
  111. {
  112. memset(NullableValueField<ArgumentType>(storage), 0, valueSize);
  113. *(static_cast<uint8_t*>(storage)) = false;
  114. }
  115. else
  116. {
  117. memcpy(NullableValueField<ArgumentType>(storage), UnBox(obj), valueSize);
  118. *(static_cast<uint8_t*>(storage)) = true;
  119. }
  120. }
  121. inline bool il2cpp_codegen_is_fake_boxed_object(RuntimeObject* object)
  122. {
  123. return false;
  124. }
  125. template<typename T>
  126. struct Il2CppFakeBox : RuntimeObject
  127. {
  128. alignas(IL2CPP_ALIGNED_OBJECT_SIZE) T m_Value;
  129. Il2CppFakeBox(TinyType* boxedType, T* value)
  130. {
  131. klass = boxedType;
  132. m_Value = *value;
  133. }
  134. };
  135. // Exception support macros
  136. #define IL2CPP_PUSH_ACTIVE_EXCEPTION(Exception) \
  137. __active_exceptions.push(Exception)
  138. #if HYBRIDCLR_UNITY_VERSION >= 20220311
  139. #define IL2CPP_POP_ACTIVE_EXCEPTION(ExcType) \
  140. (ExcType)__active_exceptions.pop()
  141. #else
  142. #define IL2CPP_POP_ACTIVE_EXCEPTION() \
  143. __active_exceptions.pop()
  144. #endif
  145. #define IL2CPP_GET_ACTIVE_EXCEPTION(ExcType) \
  146. (ExcType)__active_exceptions.top()
  147. #define IL2CPP_LEAVE(Offset, Target) \
  148. __leave_targets.push(Offset); \
  149. goto Target;
  150. #define IL2CPP_END_FINALLY(Id) \
  151. goto __CLEANUP_ ## Id;
  152. #define IL2CPP_CLEANUP(Id) \
  153. __CLEANUP_ ## Id:
  154. #define IL2CPP_RETHROW_IF_UNHANDLED(ExcType) \
  155. if(__last_unhandled_exception) { \
  156. ExcType _tmp_exception_local = __last_unhandled_exception; \
  157. __last_unhandled_exception = 0; \
  158. il2cpp_codegen_raise_exception(_tmp_exception_local); \
  159. }
  160. #define IL2CPP_JUMP_TBL(Offset, Target) \
  161. if(!__leave_targets.empty() && __leave_targets.top() == Offset) { \
  162. __leave_targets.pop(); \
  163. goto Target; \
  164. }
  165. #define IL2CPP_END_CLEANUP(Offset, Target) \
  166. if(!__leave_targets.empty() && __leave_targets.top() == Offset) \
  167. goto Target;
  168. inline void il2cpp_codegen_memory_barrier()
  169. {
  170. // The joy of singlethreading
  171. }
  172. inline TinyType* LookupTypeInfoFromCursor(uint32_t typeCursor)
  173. {
  174. return reinterpret_cast<TinyType*>(Il2CppGetTinyTypeUniverse() + typeCursor);
  175. }
  176. inline String_t* LookupStringFromCursor(uint32_t stringCursor)
  177. {
  178. return reinterpret_cast<String_t*>(Il2CppGetStringLiterals() + stringCursor);
  179. }
  180. inline bool HasParentOrIs(const TinyType* type, const TinyType* targetType)
  181. {
  182. IL2CPP_ASSERT(type != NULL);
  183. IL2CPP_ASSERT(targetType != NULL);
  184. if (type == targetType)
  185. return true;
  186. uint16_t typeHierarchySize = type->typeHierarchySize;
  187. uint16_t targetTypeHierarchySize = targetType->typeHierarchySize;
  188. if (typeHierarchySize <= targetTypeHierarchySize)
  189. return false;
  190. if (type->GetTypeHierarchy()[targetTypeHierarchySize] == targetType)
  191. return true;
  192. return false;
  193. }
  194. inline bool IsAssignableFrom(const TinyType* klass, const TinyType* oklass)
  195. {
  196. if (HasParentOrIs(oklass, klass))
  197. return true;
  198. const TinyType* const* interfaces = oklass->GetInterfaces();
  199. uint8_t size = oklass->interfacesSize;
  200. for (uint8_t i = 0; i != size; i++)
  201. {
  202. if (interfaces[i] == klass)
  203. return true;
  204. }
  205. return false;
  206. }
  207. inline Il2CppObject* IsInst(Il2CppObject* obj, TinyType* klass)
  208. {
  209. if (!obj)
  210. return NULL;
  211. TinyType* objClass = obj->klass;
  212. if (IsAssignableFrom(klass, objClass))
  213. return obj;
  214. return NULL;
  215. }
  216. inline RuntimeObject* IsInstClass(RuntimeObject* obj, TinyType* targetType)
  217. {
  218. IL2CPP_ASSERT(targetType != NULL);
  219. if (!obj)
  220. return NULL;
  221. if (HasParentOrIs(obj->klass, targetType))
  222. return obj;
  223. return NULL;
  224. }
  225. inline RuntimeObject* IsInstSealed(RuntimeObject* obj, TinyType* targetType)
  226. {
  227. if (!obj)
  228. return NULL;
  229. // optimized version to compare sealed classes
  230. return (obj->klass == targetType ? obj : NULL);
  231. }
  232. inline RuntimeObject* Castclass(RuntimeObject* obj, TinyType* targetType)
  233. {
  234. if (!obj)
  235. return NULL;
  236. RuntimeObject* result = IsInst(obj, targetType);
  237. if (result)
  238. return result;
  239. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  240. return NULL;
  241. }
  242. inline RuntimeObject* CastclassSealed(RuntimeObject *obj, TinyType* targetType)
  243. {
  244. if (!obj)
  245. return NULL;
  246. RuntimeObject* result = IsInstSealed(obj, targetType);
  247. if (result)
  248. return result;
  249. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  250. return NULL;
  251. }
  252. inline RuntimeObject* CastclassClass(RuntimeObject *obj, TinyType* targetType)
  253. {
  254. if (!obj)
  255. return NULL;
  256. RuntimeObject* result = IsInstClass(obj, targetType);
  257. if (result)
  258. return result;
  259. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  260. return NULL;
  261. }
  262. inline bool il2cpp_codegen_is_assignable_from(Type_t* left, Type_t* right)
  263. {
  264. if (right == NULL)
  265. return false;
  266. return IsAssignableFrom(reinterpret_cast<Il2CppReflectionType*>(left)->typeHandle, reinterpret_cast<Il2CppReflectionType*>(right)->typeHandle);
  267. }
  268. // il2cpp generates direct calls to this specific name
  269. inline bool il2cpp_codegen_class_is_assignable_from(TinyType* left, TinyType* right)
  270. {
  271. if (right == NULL)
  272. return false;
  273. return IsAssignableFrom(left, right);
  274. }
  275. inline TinyType* il2cpp_codegen_object_class(RuntimeObject *obj)
  276. {
  277. return obj->klass;
  278. }
  279. inline String_t* il2cpp_codegen_string_new_length(int length)
  280. {
  281. return reinterpret_cast<String_t*>(tiny::vm::String::NewLen(length));
  282. }
  283. inline String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  284. {
  285. return (String_t*)tiny::vm::String::NewLen(str.Str(), static_cast<uint32_t>(str.Length()));
  286. }
  287. template<typename T>
  288. inline Il2CppArray* SZArrayNew(TinyType* arrayType, uint32_t elementSize, uint32_t arrayLength)
  289. {
  290. return tiny::vm::Array::New<T>(arrayType, elementSize, arrayLength);
  291. }
  292. template<size_t N>
  293. inline Il2CppMultidimensionalArray<N>* GenArrayNew(TinyType* arrayType, uint32_t elementSize, il2cpp_array_size_t(&dimensions)[N])
  294. {
  295. il2cpp_array_size_t arrayLength = elementSize;
  296. for (uint32_t i = 0; i < N; i++)
  297. arrayLength *= dimensions[i];
  298. Il2CppMultidimensionalArray<N>* genArray = static_cast<Il2CppMultidimensionalArray<N>*>(il2cpp_codegen_object_new(sizeof(Il2CppMultidimensionalArray<N>) + elementSize * arrayLength, arrayType));
  299. for (uint32_t i = 0; i < N; i++)
  300. genArray->bounds[i] = dimensions[i];
  301. return genArray;
  302. }
  303. inline int32_t il2cpp_codegen_get_array_length(Il2CppArray* genArray, int32_t dimension)
  304. {
  305. return static_cast<int32_t>(reinterpret_cast<Il2CppMultidimensionalArray<1>*>(genArray)->bounds[dimension]);
  306. }
  307. inline Type_t* il2cpp_codegen_get_type(Il2CppObject* obj)
  308. {
  309. return reinterpret_cast<Type_t*>(tiny::vm::Type::GetTypeFromHandle((intptr_t)obj->klass));
  310. }
  311. inline Type_t* il2cpp_codegen_get_base_type(const Type_t* t)
  312. {
  313. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  314. const TinyType* tinyType = type->typeHandle;
  315. uint8_t typeHierarchySize = tinyType->typeHierarchySize;
  316. if (typeHierarchySize == 0)
  317. return NULL;
  318. return const_cast<Type_t*>(reinterpret_cast<const Type_t*>(tiny::vm::Type::GetTypeFromHandle((intptr_t)(tinyType->GetTypeHierarchy()[typeHierarchySize - 1]))));
  319. }
  320. inline MulticastDelegate_t* il2cpp_codegen_create_combined_delegate(Type_t* type, Il2CppArray* delegates, int delegateCount)
  321. {
  322. Il2CppMulticastDelegate* result = static_cast<Il2CppMulticastDelegate*>(il2cpp_codegen_object_new(sizeof(Il2CppMulticastDelegate), const_cast<TinyType*>(reinterpret_cast<Il2CppReflectionType*>(type)->typeHandle)));
  323. IL2CPP_OBJECT_SETREF(result, delegates, delegates);
  324. IL2CPP_OBJECT_SETREF(result, m_target, result);
  325. result->delegateCount = delegateCount;
  326. result->invoke_impl = il2cpp_array_get(delegates, Il2CppDelegate*, 0)->multicast_invoke_impl;
  327. result->multicast_invoke_impl = result->invoke_impl;
  328. return reinterpret_cast<MulticastDelegate_t*>(result);
  329. }
  330. inline const VirtualInvokeData& il2cpp_codegen_get_virtual_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj)
  331. {
  332. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_virtual_invoke_data got called on a non-virtual method");
  333. return obj->klass->GetVTable()[slot];
  334. }
  335. inline const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, const Il2CppObject* obj, TinyType* declaringInterface)
  336. {
  337. for (int i = 0; i < obj->klass->interfacesSize; ++i)
  338. {
  339. if (obj->klass->GetInterfaces()[i] == declaringInterface)
  340. return il2cpp_codegen_get_virtual_invoke_data((Il2CppMethodSlot)obj->klass->GetInterfaceOffsets()[i] + slot, obj);
  341. }
  342. tiny::vm::Exception::Raise();
  343. IL2CPP_UNREACHABLE;
  344. }
  345. inline Exception_t* il2cpp_codegen_get_overflow_exception()
  346. {
  347. return NULL;
  348. }
  349. inline Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg)
  350. {
  351. return NULL;
  352. }
  353. inline Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  354. {
  355. return NULL;
  356. }
  357. NORETURN inline void il2cpp_codegen_no_return()
  358. {
  359. IL2CPP_UNREACHABLE;
  360. }
  361. NORETURN inline void il2cpp_codegen_raise_exception(Exception_t* ex, RuntimeMethod* lastManagedFrame)
  362. {
  363. tiny::vm::Exception::Raise((Il2CppException*)ex);
  364. IL2CPP_UNREACHABLE;
  365. }
  366. NORETURN inline void il2cpp_codegen_raise_exception(const char* message)
  367. {
  368. tiny::vm::Exception::Raise(message);
  369. IL2CPP_UNREACHABLE;
  370. }
  371. NORETURN void il2cpp_codegen_raise_generic_virtual_method_exception(const char* methodFullName);
  372. inline Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg)
  373. {
  374. return NULL;
  375. }
  376. #define IL2CPP_RAISE_NULL_REFERENCE_EXCEPTION() \
  377. do {\
  378. il2cpp_codegen_raise_null_reference_exception();\
  379. IL2CPP_UNREACHABLE;\
  380. } while (0)
  381. #define IL2CPP_RAISE_MANAGED_EXCEPTION(ex, lastManagedFrame) \
  382. do {\
  383. il2cpp_codegen_raise_exception(ex);\
  384. IL2CPP_UNREACHABLE;\
  385. } while (0)
  386. #define IL2CPP_RETHROW_MANAGED_EXCEPTION(ex) \
  387. do {\
  388. il2cpp_codegen_raise_exception(ex);\
  389. IL2CPP_UNREACHABLE;\
  390. } while (0)
  391. #if _DEBUG
  392. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length) \
  393. do { \
  394. if (((uint32_t)(index)) >= ((uint32_t)length)) tiny::vm::Exception::RaiseGetIndexOutOfRangeException(); \
  395. } while (0)
  396. #else
  397. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length)
  398. #endif
  399. inline void ArrayElementTypeCheck(Il2CppArray* array, void* value)
  400. {
  401. }
  402. template<typename FunctionPointerType, size_t dynamicLibraryLength, size_t entryPointLength>
  403. inline FunctionPointerType il2cpp_codegen_resolve_pinvoke(const Il2CppNativeChar(&nativeDynamicLibrary)[dynamicLibraryLength], const char(&entryPoint)[entryPointLength],
  404. Il2CppCallConvention callingConvention, Il2CppCharSet charSet, int parameterSize, bool isNoMangle)
  405. {
  406. const PInvokeArguments pinvokeArgs =
  407. {
  408. il2cpp::utils::StringView<Il2CppNativeChar>(nativeDynamicLibrary),
  409. il2cpp::utils::StringView<char>(entryPoint),
  410. callingConvention,
  411. charSet,
  412. parameterSize,
  413. isNoMangle
  414. };
  415. return reinterpret_cast<FunctionPointerType>(tiny::vm::PlatformInvoke::Resolve(pinvokeArgs));
  416. }
  417. template<typename T>
  418. inline T* il2cpp_codegen_marshal_allocate()
  419. {
  420. return static_cast<T*>(tiny::vm::PlatformInvoke::MarshalAllocate(sizeof(T)));
  421. }
  422. inline char* il2cpp_codegen_marshal_string(String_t* string)
  423. {
  424. if (string == NULL)
  425. return NULL;
  426. Il2CppString* managedString = ((Il2CppString*)string);
  427. return tiny::vm::PlatformInvoke::MarshalCSharpStringToCppString(managedString->chars, managedString->length);
  428. }
  429. inline void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, uint32_t numberOfCharacters)
  430. {
  431. IL2CPP_ASSERT(numberOfCharacters > 0);
  432. if (string == NULL)
  433. {
  434. *buffer = '\0';
  435. return;
  436. }
  437. Il2CppString* managedString = ((Il2CppString*)string);
  438. tiny::vm::PlatformInvoke::MarshalCSharpStringToFixedCppStringBuffer(managedString->chars, managedString->length, buffer, numberOfCharacters);
  439. }
  440. inline Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  441. {
  442. if (string == NULL)
  443. return NULL;
  444. Il2CppString* managedString = ((Il2CppString*)string);
  445. return tiny::vm::PlatformInvoke::MarshalCSharpStringToCppWString(managedString->chars, managedString->length);
  446. }
  447. inline void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, uint32_t numberOfCharacters)
  448. {
  449. IL2CPP_ASSERT(numberOfCharacters > 0);
  450. if (string == NULL)
  451. {
  452. *buffer = '\0';
  453. return;
  454. }
  455. Il2CppString* managedString = ((Il2CppString*)string);
  456. tiny::vm::PlatformInvoke::MarshalCSharpStringToFixedCppWStringBuffer(managedString->chars, managedString->length, buffer, numberOfCharacters);
  457. }
  458. inline String_t* il2cpp_codegen_marshal_string_result(const char* value)
  459. {
  460. if (value == NULL)
  461. return NULL;
  462. return reinterpret_cast<String_t*>(tiny::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value));
  463. }
  464. inline String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  465. {
  466. if (value == NULL)
  467. return NULL;
  468. return reinterpret_cast<String_t*>(tiny::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult(value));
  469. }
  470. template<typename T>
  471. inline T* il2cpp_codegen_marshal_allocate_array(size_t length)
  472. {
  473. return static_cast<T*>(tiny::vm::PlatformInvoke::MarshalAllocate(sizeof(T) * length));
  474. }
  475. inline void il2cpp_codegen_marshal_free(void* ptr)
  476. {
  477. tiny::vm::PlatformInvoke::MarshalFree(ptr);
  478. }
  479. inline String_t* il2cpp_codegen_marshal_ptr_to_string_ansi(intptr_t ptr)
  480. {
  481. return il2cpp_codegen_marshal_string_result(reinterpret_cast<const char*>(ptr));
  482. }
  483. inline intptr_t il2cpp_codegen_marshal_string_to_co_task_mem_ansi(String_t* ptr)
  484. {
  485. return reinterpret_cast<intptr_t>(il2cpp_codegen_marshal_string(ptr));
  486. }
  487. inline void il2cpp_codegen_marshal_string_free_co_task_mem(intptr_t ptr)
  488. {
  489. il2cpp_codegen_marshal_free(reinterpret_cast<void*>(ptr));
  490. }
  491. template<typename T>
  492. struct Il2CppReversePInvokeMethodHolder
  493. {
  494. Il2CppReversePInvokeMethodHolder(T** storageAddress) :
  495. m_LastValue(*storageAddress),
  496. m_StorageAddress(storageAddress)
  497. {
  498. }
  499. ~Il2CppReversePInvokeMethodHolder()
  500. {
  501. *m_StorageAddress = m_LastValue;
  502. }
  503. private:
  504. T* const m_LastValue;
  505. T** const m_StorageAddress;
  506. };
  507. inline void* InterlockedExchangeImplRef(void** location, void* value)
  508. {
  509. return tiny::icalls::mscorlib::System::Threading::Interlocked::ExchangePointer(location, value);
  510. }
  511. template<typename T>
  512. inline T InterlockedCompareExchangeImpl(T* location, T value, T comparand)
  513. {
  514. return (T)tiny::icalls::mscorlib::System::Threading::Interlocked::CompareExchange_T((void**)location, value, comparand);
  515. }
  516. template<typename T>
  517. inline T InterlockedExchangeImpl(T* location, T value)
  518. {
  519. return (T)InterlockedExchangeImplRef((void**)location, value);
  520. }
  521. void il2cpp_codegen_stacktrace_push_frame(TinyStackFrameInfo& frame);
  522. void il2cpp_codegen_stacktrace_pop_frame();
  523. struct StackTraceSentry
  524. {
  525. StackTraceSentry(const RuntimeMethod* method) : m_method(method)
  526. {
  527. TinyStackFrameInfo frame_info;
  528. frame_info.method = (TinyMethod*)method;
  529. il2cpp_codegen_stacktrace_push_frame(frame_info);
  530. }
  531. ~StackTraceSentry()
  532. {
  533. il2cpp_codegen_stacktrace_pop_frame();
  534. }
  535. private:
  536. const RuntimeMethod* m_method;
  537. };
  538. inline const RuntimeMethod* GetVirtualMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot)
  539. {
  540. if (!pThis)
  541. tiny::vm::Exception::Raise();
  542. return (const RuntimeMethod*)pThis->klass->GetVTable()[slot];
  543. }
  544. inline void il2cpp_codegen_no_reverse_pinvoke_wrapper(const char* methodName, const char* reason)
  545. {
  546. std::string message = "No reverse pinvoke wrapper exists for method: '";
  547. message += methodName;
  548. message += "' because ";
  549. message += reason;
  550. tiny::vm::Runtime::FailFast(message.c_str());
  551. }
  552. #define IL2CPP_TINY_IS_INTERFACE 1
  553. #define IL2CPP_TINY_IS_ABSTRACT 2
  554. #define IL2CPP_TINY_IS_POINTER 4
  555. inline bool il2cpp_codegen_type_is_interface(Type_t* t)
  556. {
  557. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  558. const TinyType* tinyType = type->typeHandle;
  559. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_INTERFACE)
  560. return true;
  561. return false;
  562. }
  563. inline bool il2cpp_codegen_type_is_abstract(Type_t* t)
  564. {
  565. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  566. const TinyType* tinyType = type->typeHandle;
  567. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_ABSTRACT)
  568. return true;
  569. return false;
  570. }
  571. inline bool il2cpp_codegen_type_is_pointer(Type_t* t)
  572. {
  573. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  574. const TinyType* tinyType = type->typeHandle;
  575. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_POINTER)
  576. return true;
  577. return false;
  578. }
  579. template<typename T>
  580. void GetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, T* value)
  581. {
  582. // GetGenericValueImpl is only called from the class libs internally and T is never a field
  583. IL2CPP_ASSERT_STACK_PTR(value);
  584. memcpy(value, il2cpp_array_addr_with_size(thisPtr, sizeof(T), pos), sizeof(T));
  585. }
  586. template<typename T>
  587. void SetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, T* value)
  588. {
  589. il2cpp_array_setrefwithsize(thisPtr, sizeof(T), pos, value);
  590. }
  591. void il2cpp_codegen_marshal_store_last_error();
  592. template<typename T>
  593. inline void* il2cpp_codegen_unsafe_cast(T* ptr)
  594. {
  595. return reinterpret_cast<void*>(ptr);
  596. }