Object.cpp 14 KB

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