Field.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "il2cpp-config.h"
  2. #include "utils/StringUtils.h"
  3. #include "gc/GarbageCollector.h"
  4. #include "gc/WriteBarrier.h"
  5. #include "vm/Class.h"
  6. #include "vm/GenericClass.h"
  7. #include "vm/Field.h"
  8. #include "vm/Object.h"
  9. #include "vm/Reflection.h"
  10. #include "vm/Runtime.h"
  11. #include "vm/String.h"
  12. #include "vm/Type.h"
  13. #include <memory>
  14. #include "il2cpp-class-internals.h"
  15. #include "il2cpp-object-internals.h"
  16. #include "il2cpp-tabledefs.h"
  17. #include "utils/MemoryRead.h"
  18. #include "vm-utils/BlobReader.h"
  19. #include "Thread.h"
  20. namespace il2cpp
  21. {
  22. namespace vm
  23. {
  24. const char* Field::GetName(FieldInfo *field)
  25. {
  26. return field->name;
  27. }
  28. Il2CppClass* Field::GetParent(FieldInfo *field)
  29. {
  30. return field->parent;
  31. }
  32. int Field::GetFlags(FieldInfo *field)
  33. {
  34. return field->type->attrs;
  35. }
  36. size_t Field::GetOffset(FieldInfo *field)
  37. {
  38. return field->offset;
  39. }
  40. void Field::GetValue(Il2CppObject *obj, FieldInfo *field, void *value)
  41. {
  42. void *src;
  43. IL2CPP_ASSERT(obj);
  44. IL2CPP_ASSERT(!(field->type->attrs & FIELD_ATTRIBUTE_STATIC));
  45. src = (char*)obj + field->offset;
  46. SetValueRaw(field->type, value, src, true);
  47. }
  48. uint32_t Field::GetToken(const FieldInfo *field)
  49. {
  50. return field->token;
  51. }
  52. Il2CppObject* Field::GetValueObject(FieldInfo *field, Il2CppObject *obj)
  53. {
  54. Il2CppClass* fieldType = Class::FromIl2CppType(field->type);
  55. if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
  56. {
  57. if (fieldType->byval_arg.valuetype)
  58. {
  59. void* value = alloca(fieldType->instance_size - sizeof(Il2CppObject));
  60. Field::GetDefaultFieldValue(field, value);
  61. return Object::Box(fieldType, value);
  62. }
  63. else
  64. {
  65. Il2CppObject* value;
  66. Field::GetDefaultFieldValue(field, &value);
  67. return value;
  68. }
  69. }
  70. void* fieldAddress;
  71. if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
  72. {
  73. if (field->offset == THREAD_STATIC_FIELD_OFFSET)
  74. {
  75. Runtime::ClassInit(field->parent);
  76. int threadStaticFieldOffset = MetadataCache::GetThreadLocalStaticOffsetForField(field);
  77. void* threadStaticData = Thread::GetThreadStaticData(field->parent->thread_static_fields_offset);
  78. fieldAddress = static_cast<uint8_t*>(threadStaticData) + threadStaticFieldOffset;
  79. }
  80. else
  81. {
  82. Runtime::ClassInit(field->parent);
  83. fieldAddress = static_cast<uint8_t*>(field->parent->static_fields) + field->offset;
  84. }
  85. }
  86. else
  87. {
  88. IL2CPP_ASSERT(obj);
  89. fieldAddress = reinterpret_cast<uint8_t*>(obj) + field->offset;
  90. }
  91. return Object::Box(fieldType, fieldAddress);
  92. }
  93. const Il2CppType* Field::GetType(FieldInfo *field)
  94. {
  95. return field->type;
  96. }
  97. bool Field::HasAttribute(FieldInfo *field, Il2CppClass *attr_class)
  98. {
  99. return Reflection::HasAttribute(field, attr_class);
  100. }
  101. bool Field::IsDeleted(FieldInfo *field)
  102. {
  103. return false;
  104. }
  105. void Field::SetValue(Il2CppObject *obj, const FieldInfo *field, void *value)
  106. {
  107. void *dest;
  108. IL2CPP_ASSERT(!(field->type->attrs & FIELD_ATTRIBUTE_STATIC));
  109. dest = (char*)obj + field->offset;
  110. SetValueRaw(field->type, dest, value, false);
  111. }
  112. void Field::GetDefaultFieldValue(FieldInfo *field, void *value)
  113. {
  114. const Il2CppType* type = NULL;
  115. const char* data;
  116. data = Class::GetFieldDefaultValue(field, &type);
  117. utils::BlobReader::GetConstantValueFromBlob(field->parent->image, type->type, data, value);
  118. }
  119. void Field::StaticGetValue(FieldInfo *field, void *value)
  120. {
  121. StaticGetValueInternal(field, value, NULL);
  122. }
  123. void Field::StaticGetValueForThread(FieldInfo* field, void* value, Il2CppInternalThread* thread)
  124. {
  125. StaticGetValueInternal(field, value, thread);
  126. }
  127. void Field::StaticGetValueInternal(FieldInfo* field, void* value, Il2CppInternalThread* thread)
  128. {
  129. void *src = NULL;
  130. IL2CPP_ASSERT(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
  131. if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
  132. {
  133. GetDefaultFieldValue(field, value);
  134. return;
  135. }
  136. // ensure parent is initialized so that static fields memory has been allocated
  137. Class::SetupFields(field->parent);
  138. if (field->offset == THREAD_STATIC_FIELD_OFFSET)
  139. {
  140. int threadStaticFieldOffset = MetadataCache::GetThreadLocalStaticOffsetForField(field);
  141. void* threadStaticData = thread == NULL ? Thread::GetThreadStaticData(field->parent->thread_static_fields_offset) : Thread::GetThreadStaticDataForThread(field->parent->thread_static_fields_offset, thread);
  142. IL2CPP_ASSERT(NULL != threadStaticData);
  143. src = ((char*)threadStaticData) + threadStaticFieldOffset;
  144. }
  145. else
  146. {
  147. src = ((char*)field->parent->static_fields) + field->offset;
  148. }
  149. SetValueRaw(field->type, value, src, true);
  150. }
  151. void Field::StaticSetValue(FieldInfo *field, void *value)
  152. {
  153. void *dest = NULL;
  154. IL2CPP_ASSERT(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
  155. IL2CPP_ASSERT(!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
  156. // ensure parent is initialized so that static fields memory has been allocated
  157. Class::Init(field->parent);
  158. if (field->offset == THREAD_STATIC_FIELD_OFFSET)
  159. {
  160. int threadStaticFieldOffset = MetadataCache::GetThreadLocalStaticOffsetForField(field);
  161. void* threadStaticData = Thread::GetThreadStaticData(field->parent->thread_static_fields_offset);
  162. dest = ((char*)threadStaticData) + threadStaticFieldOffset;
  163. }
  164. else
  165. {
  166. dest = ((char*)field->parent->static_fields) + field->offset;
  167. }
  168. SetValueRaw(field->type, dest, value, false);
  169. }
  170. void Field::SetInstanceFieldValueObject(Il2CppObject* objectInstance, FieldInfo* field, Il2CppObject* value)
  171. {
  172. IL2CPP_ASSERT(!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
  173. IL2CPP_ASSERT(!field->type->valuetype);
  174. gc::WriteBarrier::GenericStore((Il2CppObject**)(reinterpret_cast<uint8_t*>(objectInstance) + field->offset), value);
  175. }
  176. void Field::SetValueRaw(const Il2CppType *type, void *dest, void *value, bool deref_pointer)
  177. {
  178. int t;
  179. if (type->byref)
  180. {
  181. /* object fields cannot be byref, so we don't need a
  182. wbarrier here */
  183. void* *p = (void**)dest;
  184. *p = value;
  185. return;
  186. }
  187. t = type->type;
  188. handle_enum:
  189. switch (t)
  190. {
  191. case IL2CPP_TYPE_BOOLEAN:
  192. case IL2CPP_TYPE_I1:
  193. case IL2CPP_TYPE_U1:
  194. {
  195. uint8_t *p = (uint8_t*)dest;
  196. *p = value ? *(uint8_t*)value : 0;
  197. return;
  198. }
  199. case IL2CPP_TYPE_I2:
  200. case IL2CPP_TYPE_U2:
  201. {
  202. uint16_t *p = (uint16_t*)dest;
  203. *p = value ? *(uint16_t*)value : 0;
  204. return;
  205. }
  206. case IL2CPP_TYPE_CHAR:
  207. {
  208. Il2CppChar* p = (Il2CppChar*)dest;
  209. *p = value ? *(Il2CppChar*)value : 0;
  210. return;
  211. }
  212. #if IL2CPP_SIZEOF_VOID_P == 4
  213. case IL2CPP_TYPE_I:
  214. case IL2CPP_TYPE_U:
  215. #endif
  216. case IL2CPP_TYPE_I4:
  217. case IL2CPP_TYPE_U4:
  218. {
  219. int32_t *p = (int32_t*)dest;
  220. *p = value ? *(int32_t*)value : 0;
  221. return;
  222. }
  223. #if IL2CPP_SIZEOF_VOID_P == 8
  224. case IL2CPP_TYPE_I:
  225. case IL2CPP_TYPE_U:
  226. #endif
  227. case IL2CPP_TYPE_I8:
  228. case IL2CPP_TYPE_U8:
  229. {
  230. int64_t *p = (int64_t*)dest;
  231. *p = value ? *(int64_t*)value : 0;
  232. return;
  233. }
  234. case IL2CPP_TYPE_R4:
  235. {
  236. float *p = (float*)dest;
  237. *p = value ? *(float*)value : 0;
  238. return;
  239. }
  240. case IL2CPP_TYPE_R8:
  241. {
  242. double *p = (double*)dest;
  243. *p = value ? *(double*)value : 0;
  244. return;
  245. }
  246. case IL2CPP_TYPE_STRING:
  247. case IL2CPP_TYPE_SZARRAY:
  248. case IL2CPP_TYPE_CLASS:
  249. case IL2CPP_TYPE_OBJECT:
  250. case IL2CPP_TYPE_ARRAY:
  251. gc::WriteBarrier::GenericStore((void**)dest, (deref_pointer ? *(void**)value : value));
  252. return;
  253. case IL2CPP_TYPE_FNPTR:
  254. case IL2CPP_TYPE_PTR:
  255. {
  256. void* *p = (void**)dest;
  257. *p = deref_pointer ? *(void**)value : value;
  258. return;
  259. }
  260. case IL2CPP_TYPE_VALUETYPE:
  261. /* note that 't' and 'type->type' can be different */
  262. if (type->type == IL2CPP_TYPE_VALUETYPE && Type::IsEnum(type))
  263. {
  264. t = Class::GetEnumBaseType(Type::GetClass(type))->type;
  265. goto handle_enum;
  266. }
  267. else
  268. {
  269. Il2CppClass *klass = Class::FromIl2CppType(type);
  270. int size = Class::GetValueSize(klass, NULL);
  271. if (value == NULL)
  272. {
  273. memset(dest, 0, size);
  274. }
  275. else
  276. {
  277. memcpy(dest, value, size);
  278. gc::GarbageCollector::SetWriteBarrier(reinterpret_cast<void**>(dest), size);
  279. }
  280. }
  281. return;
  282. case IL2CPP_TYPE_GENERICINST:
  283. t = GenericClass::GetTypeDefinition(type->data.generic_class)->byval_arg.type;
  284. goto handle_enum;
  285. default:
  286. IL2CPP_ASSERT(0);
  287. }
  288. }
  289. const char* Field::GetData(FieldInfo *field)
  290. {
  291. if (field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT)
  292. {
  293. const Il2CppType* type = NULL;
  294. return Class::GetFieldDefaultValue(field, &type);
  295. }
  296. else if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
  297. {
  298. IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(Field::GetData, "This works for array initialization data. Revisit any other RVA use case.");
  299. const Il2CppType* type = NULL;
  300. return Class::GetFieldDefaultValue(field, &type);
  301. }
  302. else
  303. {
  304. return NULL;
  305. }
  306. }
  307. bool Field::IsInstance(FieldInfo* field)
  308. {
  309. return (field->type->attrs & FIELD_ATTRIBUTE_STATIC) == 0;
  310. }
  311. bool Field::IsNormalStatic(FieldInfo* field)
  312. {
  313. if ((field->type->attrs & FIELD_ATTRIBUTE_STATIC) == 0)
  314. return false;
  315. if (field->offset == THREAD_STATIC_FIELD_OFFSET)
  316. return false;
  317. if ((field->type->attrs & FIELD_ATTRIBUTE_LITERAL) != 0)
  318. return false;
  319. return true;
  320. }
  321. bool Field::IsThreadStatic(FieldInfo* field)
  322. {
  323. if ((field->type->attrs & FIELD_ATTRIBUTE_STATIC) == 0)
  324. return false;
  325. if (field->offset != THREAD_STATIC_FIELD_OFFSET)
  326. return false;
  327. if ((field->type->attrs & FIELD_ATTRIBUTE_LITERAL) != 0)
  328. return false;
  329. return true;
  330. }
  331. void* Field::GetInstanceFieldDataPointer(void* instance, FieldInfo* field)
  332. {
  333. IL2CPP_ASSERT(il2cpp::vm::Field::IsInstance(field));
  334. uint8_t* fieldPointer = ((uint8_t*)instance) + GetOffset(field);
  335. return field->parent->byval_arg.valuetype ? fieldPointer - sizeof(Il2CppObject) : fieldPointer;
  336. }
  337. } /* namespace vm */
  338. } /* namespace il2cpp */