il2cpp-codegen-tiny.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. #define IL2CPP_POP_ACTIVE_EXCEPTION(ExcType) \
  139. (ExcType)__active_exceptions.pop()
  140. #define IL2CPP_GET_ACTIVE_EXCEPTION(ExcType) \
  141. (ExcType)__active_exceptions.top()
  142. #define IL2CPP_LEAVE(Offset, Target) \
  143. __leave_targets.push(Offset); \
  144. goto Target;
  145. #define IL2CPP_END_FINALLY(Id) \
  146. goto __CLEANUP_ ## Id;
  147. #define IL2CPP_CLEANUP(Id) \
  148. __CLEANUP_ ## Id:
  149. #define IL2CPP_RETHROW_IF_UNHANDLED(ExcType) \
  150. if(__last_unhandled_exception) { \
  151. ExcType _tmp_exception_local = __last_unhandled_exception; \
  152. __last_unhandled_exception = 0; \
  153. il2cpp_codegen_raise_exception(_tmp_exception_local); \
  154. }
  155. #define IL2CPP_JUMP_TBL(Offset, Target) \
  156. if(!__leave_targets.empty() && __leave_targets.top() == Offset) { \
  157. __leave_targets.pop(); \
  158. goto Target; \
  159. }
  160. #define IL2CPP_END_CLEANUP(Offset, Target) \
  161. if(!__leave_targets.empty() && __leave_targets.top() == Offset) \
  162. goto Target;
  163. inline void il2cpp_codegen_memory_barrier()
  164. {
  165. // The joy of singlethreading
  166. }
  167. inline TinyType* LookupTypeInfoFromCursor(uint32_t typeCursor)
  168. {
  169. return reinterpret_cast<TinyType*>(Il2CppGetTinyTypeUniverse() + typeCursor);
  170. }
  171. inline String_t* LookupStringFromCursor(uint32_t stringCursor)
  172. {
  173. return reinterpret_cast<String_t*>(Il2CppGetStringLiterals() + stringCursor);
  174. }
  175. inline bool HasParentOrIs(const TinyType* type, const TinyType* targetType)
  176. {
  177. IL2CPP_ASSERT(type != NULL);
  178. IL2CPP_ASSERT(targetType != NULL);
  179. if (type == targetType)
  180. return true;
  181. uint16_t typeHierarchySize = type->typeHierarchySize;
  182. uint16_t targetTypeHierarchySize = targetType->typeHierarchySize;
  183. if (typeHierarchySize <= targetTypeHierarchySize)
  184. return false;
  185. if (type->GetTypeHierarchy()[targetTypeHierarchySize] == targetType)
  186. return true;
  187. return false;
  188. }
  189. inline bool IsAssignableFrom(const TinyType* klass, const TinyType* oklass)
  190. {
  191. if (HasParentOrIs(oklass, klass))
  192. return true;
  193. const TinyType* const* interfaces = oklass->GetInterfaces();
  194. uint8_t size = oklass->interfacesSize;
  195. for (uint8_t i = 0; i != size; i++)
  196. {
  197. if (interfaces[i] == klass)
  198. return true;
  199. }
  200. return false;
  201. }
  202. inline Il2CppObject* IsInst(Il2CppObject* obj, TinyType* klass)
  203. {
  204. if (!obj)
  205. return NULL;
  206. TinyType* objClass = obj->klass;
  207. if (IsAssignableFrom(klass, objClass))
  208. return obj;
  209. return NULL;
  210. }
  211. inline RuntimeObject* IsInstClass(RuntimeObject* obj, TinyType* targetType)
  212. {
  213. IL2CPP_ASSERT(targetType != NULL);
  214. if (!obj)
  215. return NULL;
  216. if (HasParentOrIs(obj->klass, targetType))
  217. return obj;
  218. return NULL;
  219. }
  220. inline RuntimeObject* IsInstSealed(RuntimeObject* obj, TinyType* targetType)
  221. {
  222. if (!obj)
  223. return NULL;
  224. // optimized version to compare sealed classes
  225. return (obj->klass == targetType ? obj : NULL);
  226. }
  227. inline RuntimeObject* Castclass(RuntimeObject* obj, TinyType* targetType)
  228. {
  229. if (!obj)
  230. return NULL;
  231. RuntimeObject* result = IsInst(obj, targetType);
  232. if (result)
  233. return result;
  234. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  235. return NULL;
  236. }
  237. inline RuntimeObject* CastclassSealed(RuntimeObject *obj, TinyType* targetType)
  238. {
  239. if (!obj)
  240. return NULL;
  241. RuntimeObject* result = IsInstSealed(obj, targetType);
  242. if (result)
  243. return result;
  244. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  245. return NULL;
  246. }
  247. inline RuntimeObject* CastclassClass(RuntimeObject *obj, TinyType* targetType)
  248. {
  249. if (!obj)
  250. return NULL;
  251. RuntimeObject* result = IsInstClass(obj, targetType);
  252. if (result)
  253. return result;
  254. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  255. return NULL;
  256. }
  257. inline bool il2cpp_codegen_is_assignable_from(Type_t* left, Type_t* right)
  258. {
  259. if (right == NULL)
  260. return false;
  261. return IsAssignableFrom(reinterpret_cast<Il2CppReflectionType*>(left)->typeHandle, reinterpret_cast<Il2CppReflectionType*>(right)->typeHandle);
  262. }
  263. // il2cpp generates direct calls to this specific name
  264. inline bool il2cpp_codegen_class_is_assignable_from(TinyType* left, TinyType* right)
  265. {
  266. if (right == NULL)
  267. return false;
  268. return IsAssignableFrom(left, right);
  269. }
  270. inline TinyType* il2cpp_codegen_object_class(RuntimeObject *obj)
  271. {
  272. return obj->klass;
  273. }
  274. inline String_t* il2cpp_codegen_string_new_length(int length)
  275. {
  276. return reinterpret_cast<String_t*>(tiny::vm::String::NewLen(length));
  277. }
  278. inline String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  279. {
  280. return (String_t*)tiny::vm::String::NewLen(str.Str(), static_cast<uint32_t>(str.Length()));
  281. }
  282. template<typename T>
  283. inline Il2CppArray* SZArrayNew(TinyType* arrayType, uint32_t elementSize, uint32_t arrayLength)
  284. {
  285. return tiny::vm::Array::New<T>(arrayType, elementSize, arrayLength);
  286. }
  287. template<size_t N>
  288. inline Il2CppMultidimensionalArray<N>* GenArrayNew(TinyType* arrayType, uint32_t elementSize, il2cpp_array_size_t(&dimensions)[N])
  289. {
  290. il2cpp_array_size_t arrayLength = elementSize;
  291. for (uint32_t i = 0; i < N; i++)
  292. arrayLength *= dimensions[i];
  293. Il2CppMultidimensionalArray<N>* genArray = static_cast<Il2CppMultidimensionalArray<N>*>(il2cpp_codegen_object_new(sizeof(Il2CppMultidimensionalArray<N>) + elementSize * arrayLength, arrayType));
  294. for (uint32_t i = 0; i < N; i++)
  295. genArray->bounds[i] = dimensions[i];
  296. return genArray;
  297. }
  298. inline int32_t il2cpp_codegen_get_array_length(Il2CppArray* genArray, int32_t dimension)
  299. {
  300. return static_cast<int32_t>(reinterpret_cast<Il2CppMultidimensionalArray<1>*>(genArray)->bounds[dimension]);
  301. }
  302. inline Type_t* il2cpp_codegen_get_type(Il2CppObject* obj)
  303. {
  304. return reinterpret_cast<Type_t*>(tiny::vm::Type::GetTypeFromHandle((intptr_t)obj->klass));
  305. }
  306. inline Type_t* il2cpp_codegen_get_base_type(const Type_t* t)
  307. {
  308. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  309. const TinyType* tinyType = type->typeHandle;
  310. uint8_t typeHierarchySize = tinyType->typeHierarchySize;
  311. if (typeHierarchySize == 0)
  312. return NULL;
  313. return const_cast<Type_t*>(reinterpret_cast<const Type_t*>(tiny::vm::Type::GetTypeFromHandle((intptr_t)(tinyType->GetTypeHierarchy()[typeHierarchySize - 1]))));
  314. }
  315. inline MulticastDelegate_t* il2cpp_codegen_create_combined_delegate(Type_t* type, Il2CppArray* delegates, int delegateCount)
  316. {
  317. Il2CppMulticastDelegate* result = static_cast<Il2CppMulticastDelegate*>(il2cpp_codegen_object_new(sizeof(Il2CppMulticastDelegate), const_cast<TinyType*>(reinterpret_cast<Il2CppReflectionType*>(type)->typeHandle)));
  318. IL2CPP_OBJECT_SETREF(result, delegates, delegates);
  319. IL2CPP_OBJECT_SETREF(result, m_target, result);
  320. result->delegateCount = delegateCount;
  321. result->invoke_impl = il2cpp_array_get(delegates, Il2CppDelegate*, 0)->multicast_invoke_impl;
  322. result->multicast_invoke_impl = result->invoke_impl;
  323. return reinterpret_cast<MulticastDelegate_t*>(result);
  324. }
  325. inline const VirtualInvokeData& il2cpp_codegen_get_virtual_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj)
  326. {
  327. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_virtual_invoke_data got called on a non-virtual method");
  328. return obj->klass->GetVTable()[slot];
  329. }
  330. inline const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, const Il2CppObject* obj, TinyType* declaringInterface)
  331. {
  332. for (int i = 0; i < obj->klass->interfacesSize; ++i)
  333. {
  334. if (obj->klass->GetInterfaces()[i] == declaringInterface)
  335. return il2cpp_codegen_get_virtual_invoke_data((Il2CppMethodSlot)obj->klass->GetInterfaceOffsets()[i] + slot, obj);
  336. }
  337. tiny::vm::Exception::Raise();
  338. IL2CPP_UNREACHABLE;
  339. }
  340. inline Exception_t* il2cpp_codegen_get_overflow_exception()
  341. {
  342. return NULL;
  343. }
  344. inline Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg)
  345. {
  346. return NULL;
  347. }
  348. inline Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  349. {
  350. return NULL;
  351. }
  352. NORETURN inline void il2cpp_codegen_no_return()
  353. {
  354. IL2CPP_UNREACHABLE;
  355. }
  356. NORETURN inline void il2cpp_codegen_raise_exception(Exception_t* ex, RuntimeMethod* lastManagedFrame)
  357. {
  358. tiny::vm::Exception::Raise((Il2CppException*)ex);
  359. IL2CPP_UNREACHABLE;
  360. }
  361. NORETURN inline void il2cpp_codegen_raise_exception(const char* message)
  362. {
  363. tiny::vm::Exception::Raise(message);
  364. IL2CPP_UNREACHABLE;
  365. }
  366. NORETURN void il2cpp_codegen_raise_generic_virtual_method_exception(const char* methodFullName);
  367. inline Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg)
  368. {
  369. return NULL;
  370. }
  371. #define IL2CPP_RAISE_NULL_REFERENCE_EXCEPTION() \
  372. do {\
  373. il2cpp_codegen_raise_null_reference_exception();\
  374. IL2CPP_UNREACHABLE;\
  375. } while (0)
  376. #define IL2CPP_RAISE_MANAGED_EXCEPTION(ex, lastManagedFrame) \
  377. do {\
  378. il2cpp_codegen_raise_exception(ex);\
  379. IL2CPP_UNREACHABLE;\
  380. } while (0)
  381. #define IL2CPP_RETHROW_MANAGED_EXCEPTION(ex) \
  382. do {\
  383. il2cpp_codegen_raise_exception(ex);\
  384. IL2CPP_UNREACHABLE;\
  385. } while (0)
  386. #if _DEBUG
  387. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length) \
  388. do { \
  389. if (((uint32_t)(index)) >= ((uint32_t)length)) tiny::vm::Exception::RaiseGetIndexOutOfRangeException(); \
  390. } while (0)
  391. #else
  392. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length)
  393. #endif
  394. inline void ArrayElementTypeCheck(Il2CppArray* array, void* value)
  395. {
  396. }
  397. template<typename FunctionPointerType, size_t dynamicLibraryLength, size_t entryPointLength>
  398. inline FunctionPointerType il2cpp_codegen_resolve_pinvoke(const Il2CppNativeChar(&nativeDynamicLibrary)[dynamicLibraryLength], const char(&entryPoint)[entryPointLength],
  399. Il2CppCallConvention callingConvention, Il2CppCharSet charSet, int parameterSize, bool isNoMangle)
  400. {
  401. const PInvokeArguments pinvokeArgs =
  402. {
  403. il2cpp::utils::StringView<Il2CppNativeChar>(nativeDynamicLibrary),
  404. il2cpp::utils::StringView<char>(entryPoint),
  405. callingConvention,
  406. charSet,
  407. parameterSize,
  408. isNoMangle
  409. };
  410. return reinterpret_cast<FunctionPointerType>(tiny::vm::PlatformInvoke::Resolve(pinvokeArgs));
  411. }
  412. template<typename T>
  413. inline T* il2cpp_codegen_marshal_allocate()
  414. {
  415. return static_cast<T*>(tiny::vm::PlatformInvoke::MarshalAllocate(sizeof(T)));
  416. }
  417. inline char* il2cpp_codegen_marshal_string(String_t* string)
  418. {
  419. if (string == NULL)
  420. return NULL;
  421. Il2CppString* managedString = ((Il2CppString*)string);
  422. return tiny::vm::PlatformInvoke::MarshalCSharpStringToCppString(managedString->chars, managedString->length);
  423. }
  424. inline void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, uint32_t numberOfCharacters)
  425. {
  426. IL2CPP_ASSERT(numberOfCharacters > 0);
  427. if (string == NULL)
  428. {
  429. *buffer = '\0';
  430. return;
  431. }
  432. Il2CppString* managedString = ((Il2CppString*)string);
  433. tiny::vm::PlatformInvoke::MarshalCSharpStringToFixedCppStringBuffer(managedString->chars, managedString->length, buffer, numberOfCharacters);
  434. }
  435. inline Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  436. {
  437. if (string == NULL)
  438. return NULL;
  439. Il2CppString* managedString = ((Il2CppString*)string);
  440. return tiny::vm::PlatformInvoke::MarshalCSharpStringToCppWString(managedString->chars, managedString->length);
  441. }
  442. inline void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, uint32_t numberOfCharacters)
  443. {
  444. IL2CPP_ASSERT(numberOfCharacters > 0);
  445. if (string == NULL)
  446. {
  447. *buffer = '\0';
  448. return;
  449. }
  450. Il2CppString* managedString = ((Il2CppString*)string);
  451. tiny::vm::PlatformInvoke::MarshalCSharpStringToFixedCppWStringBuffer(managedString->chars, managedString->length, buffer, numberOfCharacters);
  452. }
  453. inline String_t* il2cpp_codegen_marshal_string_result(const char* value)
  454. {
  455. if (value == NULL)
  456. return NULL;
  457. return reinterpret_cast<String_t*>(tiny::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value));
  458. }
  459. inline String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  460. {
  461. if (value == NULL)
  462. return NULL;
  463. return reinterpret_cast<String_t*>(tiny::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult(value));
  464. }
  465. template<typename T>
  466. inline T* il2cpp_codegen_marshal_allocate_array(size_t length)
  467. {
  468. return static_cast<T*>(tiny::vm::PlatformInvoke::MarshalAllocate(sizeof(T) * length));
  469. }
  470. inline void il2cpp_codegen_marshal_free(void* ptr)
  471. {
  472. tiny::vm::PlatformInvoke::MarshalFree(ptr);
  473. }
  474. inline String_t* il2cpp_codegen_marshal_ptr_to_string_ansi(intptr_t ptr)
  475. {
  476. return il2cpp_codegen_marshal_string_result(reinterpret_cast<const char*>(ptr));
  477. }
  478. inline intptr_t il2cpp_codegen_marshal_string_to_co_task_mem_ansi(String_t* ptr)
  479. {
  480. return reinterpret_cast<intptr_t>(il2cpp_codegen_marshal_string(ptr));
  481. }
  482. inline void il2cpp_codegen_marshal_string_free_co_task_mem(intptr_t ptr)
  483. {
  484. il2cpp_codegen_marshal_free(reinterpret_cast<void*>(ptr));
  485. }
  486. template<typename T>
  487. struct Il2CppReversePInvokeMethodHolder
  488. {
  489. Il2CppReversePInvokeMethodHolder(T** storageAddress) :
  490. m_LastValue(*storageAddress),
  491. m_StorageAddress(storageAddress)
  492. {
  493. }
  494. ~Il2CppReversePInvokeMethodHolder()
  495. {
  496. *m_StorageAddress = m_LastValue;
  497. }
  498. private:
  499. T* const m_LastValue;
  500. T** const m_StorageAddress;
  501. };
  502. inline void* InterlockedExchangeImplRef(void** location, void* value)
  503. {
  504. return tiny::icalls::mscorlib::System::Threading::Interlocked::ExchangePointer(location, value);
  505. }
  506. template<typename T>
  507. inline T InterlockedCompareExchangeImpl(T* location, T value, T comparand)
  508. {
  509. return (T)tiny::icalls::mscorlib::System::Threading::Interlocked::CompareExchange_T((void**)location, value, comparand);
  510. }
  511. template<typename T>
  512. inline T InterlockedExchangeImpl(T* location, T value)
  513. {
  514. return (T)InterlockedExchangeImplRef((void**)location, value);
  515. }
  516. void il2cpp_codegen_stacktrace_push_frame(TinyStackFrameInfo& frame);
  517. void il2cpp_codegen_stacktrace_pop_frame();
  518. struct StackTraceSentry
  519. {
  520. StackTraceSentry(const RuntimeMethod* method) : m_method(method)
  521. {
  522. TinyStackFrameInfo frame_info;
  523. frame_info.method = (TinyMethod*)method;
  524. il2cpp_codegen_stacktrace_push_frame(frame_info);
  525. }
  526. ~StackTraceSentry()
  527. {
  528. il2cpp_codegen_stacktrace_pop_frame();
  529. }
  530. private:
  531. const RuntimeMethod* m_method;
  532. };
  533. inline const RuntimeMethod* GetVirtualMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot)
  534. {
  535. if (!pThis)
  536. tiny::vm::Exception::Raise();
  537. return (const RuntimeMethod*)pThis->klass->GetVTable()[slot];
  538. }
  539. inline void il2cpp_codegen_no_reverse_pinvoke_wrapper(const char* methodName, const char* reason)
  540. {
  541. std::string message = "No reverse pinvoke wrapper exists for method: '";
  542. message += methodName;
  543. message += "' because ";
  544. message += reason;
  545. tiny::vm::Runtime::FailFast(message.c_str());
  546. }
  547. #define IL2CPP_TINY_IS_INTERFACE 1
  548. #define IL2CPP_TINY_IS_ABSTRACT 2
  549. #define IL2CPP_TINY_IS_POINTER 4
  550. inline bool il2cpp_codegen_type_is_interface(Type_t* t)
  551. {
  552. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  553. const TinyType* tinyType = type->typeHandle;
  554. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_INTERFACE)
  555. return true;
  556. return false;
  557. }
  558. inline bool il2cpp_codegen_type_is_abstract(Type_t* t)
  559. {
  560. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  561. const TinyType* tinyType = type->typeHandle;
  562. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_ABSTRACT)
  563. return true;
  564. return false;
  565. }
  566. inline bool il2cpp_codegen_type_is_pointer(Type_t* t)
  567. {
  568. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  569. const TinyType* tinyType = type->typeHandle;
  570. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_POINTER)
  571. return true;
  572. return false;
  573. }
  574. template<typename T>
  575. void GetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, T* value)
  576. {
  577. // GetGenericValueImpl is only called from the class libs internally and T is never a field
  578. IL2CPP_ASSERT_STACK_PTR(value);
  579. memcpy(value, il2cpp_array_addr_with_size(thisPtr, sizeof(T), pos), sizeof(T));
  580. }
  581. template<typename T>
  582. void SetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, T* value)
  583. {
  584. il2cpp_array_setrefwithsize(thisPtr, sizeof(T), pos, value);
  585. }
  586. void il2cpp_codegen_marshal_store_last_error();
  587. template<typename T>
  588. inline void* il2cpp_codegen_unsafe_cast(T* ptr)
  589. {
  590. return reinterpret_cast<void*>(ptr);
  591. }