Object.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include "il2cpp-config.h"
  2. #include <memory>
  3. #include "il2cpp-class-internals.h"
  4. #include "il2cpp-object-internals.h"
  5. #include "il2cpp-tabledefs.h"
  6. #include "il2cpp-runtime-stats.h"
  7. #include "gc/gc_wrapper.h"
  8. #include "gc/GarbageCollector.h"
  9. #include "metadata/GenericMethod.h"
  10. #include "metadata/Il2CppTypeCompare.h"
  11. #include "utils/StringUtils.h"
  12. #include "vm-utils/VmThreadUtils.h"
  13. #include "vm/Array.h"
  14. #include "vm/Class.h"
  15. #include "vm/ClassInlines.h"
  16. #include "vm/Exception.h"
  17. #include "vm/Field.h"
  18. #include "vm/MetadataCache.h"
  19. #include "vm/Method.h"
  20. #include "vm/Object.h"
  21. #include "vm/Profiler.h"
  22. #include "vm/RCW.h"
  23. #include "vm/Reflection.h"
  24. #include "vm/Runtime.h"
  25. #include "vm/String.h"
  26. #include "vm/Thread.h"
  27. #include "vm/Type.h"
  28. #if IL2CPP_GC_BOEHM
  29. #define ALLOC_PTRFREE(obj, vt, size) do { (obj) = (Il2CppObject*)GC_MALLOC_ATOMIC ((size)); (obj)->klass = (vt); (obj)->monitor = NULL;} while (0)
  30. #define ALLOC_OBJECT(obj, vt, size) do { (obj) = (Il2CppObject*)GC_MALLOC ((size)); (obj)->klass = (vt);} while (0)
  31. #ifdef GC_GCJ_SUPPORT
  32. #define ALLOC_TYPED(dest, size, type) do { (dest) = (Il2CppObject*)GC_gcj_malloc ((size),(type)); } while (0)
  33. #else
  34. #define GC_NO_DESCRIPTOR (NULL)
  35. #define ALLOC_TYPED(dest, size, type) do { (dest) = GC_MALLOC ((size)); *(void**)dest = (type);} while (0)
  36. #endif
  37. #else
  38. #ifdef HAVE_SGEN_GC
  39. #define GC_NO_DESCRIPTOR (NULL)
  40. #define ALLOC_PTRFREE(obj, vt, size) do { (obj) = mono_gc_alloc_obj (vt, size);} while (0)
  41. #define ALLOC_OBJECT(obj, vt, size) do { (obj) = mono_gc_alloc_obj (vt, size);} while (0)
  42. #define ALLOC_TYPED(dest, size, type) do { (dest) = mono_gc_alloc_obj (type, size);} while (0)
  43. #else
  44. #define ALLOC_PTRFREE(obj, vt, size) do { (obj) = (Il2CppObject*)malloc ((size)); (obj)->klass = (vt); (obj)->monitor = NULL;} while (0)
  45. #define ALLOC_OBJECT(obj, vt, size) do { (obj) = (Il2CppObject*)calloc (1, (size)); (obj)->klass = (vt);} while (0)
  46. #define ALLOC_TYPED(dest, size, type) do { (dest) = (Il2CppObject*)(calloc (1, (size))); *(void**)dest = (type);} while (0)
  47. #endif
  48. #endif
  49. namespace il2cpp
  50. {
  51. namespace vm
  52. {
  53. Il2CppObject * Object::Allocate(size_t size, Il2CppClass *typeInfo)
  54. {
  55. IL2CPP_ASSERT(typeInfo->initialized);
  56. Il2CppObject *o;
  57. ALLOC_OBJECT(o, typeInfo, size);
  58. ++il2cpp_runtime_stats.new_object_count;
  59. return o;
  60. }
  61. Il2CppObject * Object::AllocatePtrFree(size_t size, Il2CppClass *typeInfo)
  62. {
  63. IL2CPP_ASSERT(typeInfo->initialized);
  64. Il2CppObject *o;
  65. ALLOC_PTRFREE(o, typeInfo, size);
  66. ++il2cpp_runtime_stats.new_object_count;
  67. return o;
  68. }
  69. Il2CppObject * Object::AllocateSpec(size_t size, Il2CppClass *typeInfo)
  70. {
  71. IL2CPP_ASSERT(typeInfo->initialized);
  72. Il2CppObject *o;
  73. ALLOC_TYPED(o, size, typeInfo);
  74. ++il2cpp_runtime_stats.new_object_count;
  75. return o;
  76. }
  77. Il2CppObject* Object::Box(Il2CppClass *typeInfo, void* val)
  78. {
  79. if (!typeInfo->byval_arg.valuetype)
  80. return *(Il2CppObject**)val;
  81. bool isNullable = Class::IsNullable(typeInfo);
  82. if (isNullable)
  83. {
  84. /* From ECMA-335, I.8.2.4 Boxing and unboxing of values:
  85. All value types have an operation called box. Boxing a value of any value type produces its boxed value;
  86. i.e., a value of the corresponding boxed type containing a bitwise copy of the original value. If the
  87. value type is a nullable type defined as an instantiation of the value type System.Nullable<T> the result
  88. is a null reference or bitwise copy of its Value property of type T, depending on its HasValue property
  89. (false and true, respectively).
  90. */
  91. if (!NullableHasValue(typeInfo, val))
  92. return NULL;
  93. }
  94. Il2CppObject* obj = Object::New(typeInfo);
  95. size_t size = Class::GetInstanceSize(typeInfo);
  96. // At this point we know we have a value type and we need to adjust the
  97. // copy size by the size of Il2CppObject
  98. size = size - sizeof(Il2CppObject);
  99. uint8_t* valueStart = static_cast<uint8_t*>(val);
  100. if (isNullable)
  101. {
  102. IL2CPP_ASSERT(metadata::Il2CppTypeEqualityComparer::AreEqual(typeInfo->fields[1].type, &Class::GetNullableArgument(typeInfo)->byval_arg));
  103. // Shift the valueStart right past the bool for nullable
  104. int32_t nullableShift = typeInfo->fields[1].offset - sizeof(Il2CppObject);
  105. valueStart += nullableShift;
  106. // the size needs to be further adjusted to be smaller
  107. size -= nullableShift;
  108. }
  109. memcpy(((char*)obj) + sizeof(Il2CppObject), valueStart, size);
  110. gc::GarbageCollector::SetWriteBarrier((void**)(((char*)obj) + sizeof(Il2CppObject)), size);
  111. return obj;
  112. }
  113. Il2CppObject* Object::Clone(Il2CppObject *obj)
  114. {
  115. Il2CppObject *o;
  116. int size;
  117. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Object::Clone, "Finish implementation");
  118. if (obj->klass->rank)
  119. {
  120. return Array::Clone((Il2CppArray*)obj);
  121. }
  122. size = obj->klass->instance_size;
  123. o = Allocate(size, obj->klass);
  124. /* do not copy the sync state */
  125. memcpy((char*)o + sizeof(Il2CppObject), (char*)obj + sizeof(Il2CppObject), size - sizeof(Il2CppObject));
  126. gc::GarbageCollector::SetWriteBarrier((void**)(((char*)o) + sizeof(Il2CppObject)), size);
  127. //#ifdef HAVE_SGEN_GC
  128. // if (obj->vtable->klass->has_references)
  129. // mono_gc_wbarrier_object (o);
  130. //#endif
  131. if (obj->klass->has_finalize)
  132. il2cpp::gc::GarbageCollector::RegisterFinalizerForNewObject(o);
  133. #if IL2CPP_ENABLE_PROFILER
  134. if (Profiler::ProfileAllocations())
  135. Profiler::Allocation(o, obj->klass);
  136. #endif
  137. return o;
  138. }
  139. Il2CppClass* Object::GetClass(Il2CppObject* obj)
  140. {
  141. return obj->klass;
  142. }
  143. #if IL2CPP_SIZEOF_VOID_P == 8
  144. const int kObjectAlignmentShift = 3;
  145. #elif IL2CPP_SIZEOF_VOID_P == 4
  146. const int kObjectAlignmentShift = 2;
  147. #else
  148. #error Invalid architecture size
  149. #endif
  150. int32_t Object::GetHash(Il2CppObject* obj)
  151. {
  152. // shift away unused bits due to alignment, then use Knuth's multiplicative hash
  153. return (((uint32_t)(intptr_t)(obj)) >> kObjectAlignmentShift) * 2654435761u;
  154. }
  155. uint32_t Object::GetSize(Il2CppObject* obj)
  156. {
  157. Il2CppClass* klass = GetClass(obj);
  158. if (klass == il2cpp_defaults.string_class)
  159. {
  160. return sizeof(Il2CppString) + 2 * utils::StringUtils::GetLength((Il2CppString*)obj) + 2;
  161. }
  162. else if (obj->klass->rank)
  163. {
  164. Il2CppArray *array = (Il2CppArray*)obj;
  165. size_t size = kIl2CppSizeOfArray + Array::GetElementSize(klass) * Array::GetLength(array);
  166. if (array->bounds)
  167. {
  168. size += 3;
  169. size &= ~3;
  170. size += sizeof(Il2CppArrayBounds) * obj->klass->rank;
  171. }
  172. return (uint32_t)size;
  173. }
  174. else
  175. {
  176. return Class::GetInstanceSize(klass);
  177. }
  178. }
  179. const MethodInfo* Object::GetVirtualMethod(Il2CppObject *obj, const MethodInfo *virtualMethod)
  180. {
  181. if ((virtualMethod->flags & METHOD_ATTRIBUTE_FINAL) || !(virtualMethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
  182. return virtualMethod;
  183. Il2CppClass* methodDeclaringType = virtualMethod->klass;
  184. const MethodInfo* vtableSlotMethod;
  185. if (Class::IsInterface(methodDeclaringType))
  186. {
  187. vtableSlotMethod = ClassInlines::GetInterfaceInvokeDataFromVTable(obj, methodDeclaringType, virtualMethod->slot).method;
  188. }
  189. else
  190. {
  191. IL2CPP_ASSERT(virtualMethod->slot < obj->klass->vtable_count);
  192. vtableSlotMethod = obj->klass->vtable[virtualMethod->slot].method;
  193. }
  194. if (Method::IsGenericInstanceMethod(virtualMethod))
  195. return il2cpp::metadata::GenericMethod::GetGenericVirtualMethod(vtableSlotMethod, virtualMethod);
  196. return vtableSlotMethod;
  197. }
  198. Il2CppObject* Object::IsInst(Il2CppObject *obj, Il2CppClass *klass)
  199. {
  200. if (!obj)
  201. return NULL;
  202. Il2CppClass* objClass = Object::GetClass(obj);
  203. if (Class::IsAssignableFrom(klass, objClass))
  204. return obj;
  205. if (!objClass->is_import_or_windows_runtime)
  206. return NULL;
  207. // check if klass has an interface id
  208. if (Class::IsInterface(klass) && klass->interopData != NULL)
  209. {
  210. const Il2CppGuid* iid = klass->interopData->guid;
  211. if (iid != NULL)
  212. {
  213. Il2CppIUnknown* unknown = RCW::QueryInterfaceNoAddRef<false>(static_cast<Il2CppComObject*>(obj), *iid);
  214. if (unknown)
  215. return static_cast<Il2CppComObject*>(obj);
  216. }
  217. }
  218. return (klass == il2cpp_defaults.object_class) ? obj : NULL;
  219. }
  220. Il2CppObject* Object::New(Il2CppClass *klass)
  221. {
  222. // same as NewAllocSpecific as we only support a single domain
  223. return NewAllocSpecific(klass);
  224. }
  225. Il2CppObject* Object::NewPinned(Il2CppClass *klass)
  226. {
  227. #if (IL2CPP_GC_BOEHM || IL2CPP_GC_NULL)
  228. return New(klass);
  229. #else
  230. IL2CPP_NOT_IMPLEMENTED(Object::NewPinned);
  231. #endif
  232. }
  233. Il2CppObject * Object::NewAllocSpecific(Il2CppClass *klass)
  234. {
  235. Il2CppObject *o = NULL;
  236. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Object::NewAllocSpecific, "We really shouldn't need this initialization");
  237. Class::Init(klass);
  238. if (Class::IsNullable(klass))
  239. klass = il2cpp::vm::Class::GetNullableArgument(klass);
  240. if (!klass->has_references)
  241. {
  242. o = NewPtrFree(klass);
  243. }
  244. #if IL2CPP_HAS_GC_DESCRIPTORS
  245. else if (klass->gc_desc != GC_NO_DESCRIPTOR)
  246. {
  247. o = AllocateSpec(klass->instance_size, klass);
  248. }
  249. #endif
  250. else
  251. {
  252. o = Allocate(klass->instance_size, klass);
  253. }
  254. if (klass->has_finalize)
  255. il2cpp::gc::GarbageCollector::RegisterFinalizerForNewObject(o);
  256. #if IL2CPP_ENABLE_PROFILER
  257. if (Profiler::ProfileAllocations())
  258. Profiler::Allocation(o, klass);
  259. #endif
  260. Runtime::ClassInit(klass);
  261. return o;
  262. }
  263. Il2CppObject* Object::NewPtrFree(Il2CppClass *klass)
  264. {
  265. Il2CppObject *obj = {0};
  266. IL2CPP_ASSERT(klass->initialized);
  267. IL2CPP_ASSERT(!klass->has_references);
  268. ALLOC_PTRFREE(obj, klass, klass->instance_size);
  269. #if NEED_TO_ZERO_PTRFREE
  270. /* an inline memset is much faster for the common vcase of small objects
  271. * note we assume the allocated size is a multiple of sizeof (void*).
  272. */
  273. if (klass->instance_size < 128)
  274. {
  275. void* *p, *end;
  276. end = (void**)((char*)obj + klass->instance_size);
  277. p = (void**)((char*)obj + sizeof(Il2CppObject));
  278. while (p < end)
  279. {
  280. *p = NULL;
  281. ++p;
  282. }
  283. }
  284. else
  285. {
  286. memset((char*)obj + sizeof(Il2CppObject), 0, klass->instance_size - sizeof(Il2CppObject));
  287. }
  288. #endif
  289. ++il2cpp_runtime_stats.new_object_count;
  290. return obj;
  291. }
  292. void* Object::Unbox(Il2CppObject* obj)
  293. {
  294. void* val = (void*)(((char*)obj) + sizeof(Il2CppObject));
  295. return val;
  296. }
  297. void Object::UnboxNullable(Il2CppObject* obj, Il2CppClass* nullableClass, void* storage)
  298. {
  299. // We assume storage is on the stack, if not we'll need a write barrier
  300. IL2CPP_ASSERT_STACK_PTR(storage);
  301. // After the assert above, we can safely call this method, because the GC will find storage as a root,
  302. // since it is on the stack.
  303. UnboxNullableGCUnsafe(obj, nullableClass, storage);
  304. }
  305. void Object::UnboxNullableWithWriteBarrier(Il2CppObject* obj, Il2CppClass* nullableClass, void* storage)
  306. {
  307. uint32_t valueSize = UnboxNullableGCUnsafe(obj, nullableClass, storage);
  308. il2cpp::gc::GarbageCollector::SetWriteBarrier((void**)storage, valueSize);
  309. }
  310. // Hey! You probably don't want to call this method. Call Object::UnboxNullable or
  311. // Object::UnboxNullableWithWriteBarrier instead.
  312. //
  313. //
  314. // Ok - still here? If you call this method and storage is not on the stack, you need to set a
  315. // GC write barrier for the pointer at storage with a length that is the number of bytes, which
  316. // this method returns. That's what UnboxNullableWithWriteBarrier. Use it!
  317. uint32_t Object::UnboxNullableGCUnsafe(Il2CppObject* obj, Il2CppClass* nullableClass, void* storage)
  318. {
  319. IL2CPP_ASSERT(Class::IsNullable(nullableClass));
  320. IL2CPP_ASSERT(nullableClass->field_count == 2);
  321. IL2CPP_ASSERT(metadata::Il2CppTypeEqualityComparer::AreEqual(nullableClass->fields[0].type, &il2cpp_defaults.boolean_class->byval_arg));
  322. IL2CPP_ASSERT(obj == NULL || metadata::Il2CppTypeEqualityComparer::AreEqual(nullableClass->fields[1].type, &obj->klass->byval_arg));
  323. void* valueField = Field::GetInstanceFieldDataPointer(storage, &nullableClass->fields[1]);
  324. uint32_t valueSize = Class::GetNullableArgument(nullableClass)->instance_size - sizeof(Il2CppObject);
  325. if (obj == NULL)
  326. {
  327. memset(valueField, 0, valueSize);
  328. *(static_cast<uint8_t*>(storage)) = false;
  329. }
  330. else
  331. {
  332. memcpy(valueField, Unbox(obj), valueSize);
  333. *(static_cast<uint8_t*>(storage)) = true;
  334. }
  335. return valueSize;
  336. }
  337. void Object::NullableInit(uint8_t* buf, Il2CppObject* value, Il2CppClass* klass)
  338. {
  339. Il2CppClass *parameterClass = klass->castClass;
  340. IL2CPP_ASSERT(Class::FromIl2CppType(klass->fields[0].type) == il2cpp_defaults.boolean_class);
  341. IL2CPP_ASSERT(Class::FromIl2CppType(klass->fields[1].type) == parameterClass);
  342. *(uint8_t*)(buf + klass->fields[0].offset - sizeof(Il2CppObject)) = value ? 1 : 0;
  343. if (value)
  344. memcpy(buf + klass->fields[1].offset - sizeof(Il2CppObject), Object::Unbox(value), Class::GetValueSize(parameterClass, NULL));
  345. else
  346. memset(buf + klass->fields[1].offset - sizeof(Il2CppObject), 0, Class::GetValueSize(parameterClass, NULL));
  347. }
  348. } /* namespace vm */
  349. } /* namespace il2cpp */