1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_ART_FIELD_INL_H_
18 #define ART_RUNTIME_ART_FIELD_INL_H_
19
20 #include "art_field.h"
21
22 #include <android-base/logging.h>
23
24 #include "class_linker-inl.h"
25 #include "dex/dex_file-inl.h"
26 #include "dex/primitive.h"
27 #include "gc/accounting/card_table-inl.h"
28 #include "gc_root-inl.h"
29 #include "jvalue.h"
30 #include "mirror/dex_cache-inl.h"
31 #include "mirror/object-inl.h"
32 #include "obj_ptr-inl.h"
33 #include "thread-current-inl.h"
34
35 namespace art {
36
IsProxyField()37 inline bool ArtField::IsProxyField() {
38 // No read barrier needed, we're reading the constant declaring class only to read
39 // the constant proxy flag. See ReadBarrierOption.
40 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass<kVerifyNone>();
41 }
42
43 // We are only ever allowed to set our own final fields
CanBeChangedBy(ArtMethod * method)44 inline bool ArtField::CanBeChangedBy(ArtMethod* method) {
45 ObjPtr<mirror::Class> declaring_class(GetDeclaringClass());
46 ObjPtr<mirror::Class> referring_class(method->GetDeclaringClass());
47 return !IsFinal() || (declaring_class == referring_class);
48 }
49
50 template<ReadBarrierOption kReadBarrierOption>
GetDeclaringClass()51 inline ObjPtr<mirror::Class> ArtField::GetDeclaringClass() {
52 GcRootSource gc_root_source(this);
53 ObjPtr<mirror::Class> result = declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
54 DCHECK(result != nullptr);
55 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << result->GetStatus();
56 return result;
57 }
58
SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)59 inline void ArtField::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
60 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
61 }
62
63 template<typename RootVisitorType>
VisitArrayRoots(RootVisitorType & visitor,uint8_t * start_boundary,uint8_t * end_boundary,LengthPrefixedArray<ArtField> * array)64 void ArtField::VisitArrayRoots(RootVisitorType& visitor,
65 uint8_t* start_boundary,
66 uint8_t* end_boundary,
67 LengthPrefixedArray<ArtField>* array) {
68 DCHECK_LE(start_boundary, end_boundary);
69 DCHECK_NE(array->size(), 0u);
70 ArtField* first_field = &array->At(0);
71 DCHECK_LE(static_cast<void*>(end_boundary), static_cast<void*>(first_field + array->size()));
72 static constexpr size_t kFieldSize = sizeof(ArtField);
73 // Confirm the assumption that ArtField size is power of two. It's important
74 // as we assume so below (RoundUp).
75 static_assert(IsPowerOfTwo(kFieldSize));
76 uint8_t* declaring_class =
77 reinterpret_cast<uint8_t*>(first_field) + DeclaringClassOffset().Int32Value();
78 // Jump to the first class to visit.
79 if (declaring_class < start_boundary) {
80 declaring_class += RoundUp(start_boundary - declaring_class, kFieldSize);
81 }
82 while (declaring_class < end_boundary) {
83 visitor.VisitRoot(
84 reinterpret_cast<mirror::CompressedReference<mirror::Object>*>(declaring_class));
85 declaring_class += kFieldSize;
86 }
87 }
88
GetOffsetDuringLinking()89 inline MemberOffset ArtField::GetOffsetDuringLinking() {
90 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
91 return MemberOffset(offset_);
92 }
93
Get32(ObjPtr<mirror::Object> object)94 inline uint32_t ArtField::Get32(ObjPtr<mirror::Object> object) {
95 DCHECK(object != nullptr) << PrettyField();
96 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
97 if (UNLIKELY(IsVolatile())) {
98 return object->GetField32Volatile(GetOffset());
99 }
100 return object->GetField32(GetOffset());
101 }
102
103 template<bool kTransactionActive>
Set32(ObjPtr<mirror::Object> object,uint32_t new_value)104 inline void ArtField::Set32(ObjPtr<mirror::Object> object, uint32_t new_value) {
105 DCHECK(object != nullptr) << PrettyField();
106 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
107 if (UNLIKELY(IsVolatile())) {
108 object->SetField32Volatile<kTransactionActive>(GetOffset(), new_value);
109 } else {
110 object->SetField32<kTransactionActive>(GetOffset(), new_value);
111 }
112 }
113
Get64(ObjPtr<mirror::Object> object)114 inline uint64_t ArtField::Get64(ObjPtr<mirror::Object> object) {
115 DCHECK(object != nullptr) << PrettyField();
116 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
117 if (UNLIKELY(IsVolatile())) {
118 return object->GetField64Volatile(GetOffset());
119 }
120 return object->GetField64(GetOffset());
121 }
122
123 template<bool kTransactionActive>
Set64(ObjPtr<mirror::Object> object,uint64_t new_value)124 inline void ArtField::Set64(ObjPtr<mirror::Object> object, uint64_t new_value) {
125 DCHECK(object != nullptr) << PrettyField();
126 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
127 if (UNLIKELY(IsVolatile())) {
128 object->SetField64Volatile<kTransactionActive>(GetOffset(), new_value);
129 } else {
130 object->SetField64<kTransactionActive>(GetOffset(), new_value);
131 }
132 }
133
134 template<class MirrorType, ReadBarrierOption kReadBarrierOption>
GetObj(ObjPtr<mirror::Object> object)135 inline ObjPtr<MirrorType> ArtField::GetObj(ObjPtr<mirror::Object> object) {
136 DCHECK(object != nullptr) << PrettyField();
137 DCHECK(!IsStatic() ||
138 (object == GetDeclaringClass<kReadBarrierOption>()) ||
139 !Runtime::Current()->IsStarted());
140 if (UNLIKELY(IsVolatile())) {
141 return object->GetFieldObjectVolatile<MirrorType, kDefaultVerifyFlags, kReadBarrierOption>(
142 GetOffset());
143 }
144 return object->GetFieldObject<MirrorType, kDefaultVerifyFlags, kReadBarrierOption>(GetOffset());
145 }
146
147 template<bool kTransactionActive>
SetObj(ObjPtr<mirror::Object> object,ObjPtr<mirror::Object> new_value)148 inline void ArtField::SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value) {
149 DCHECK(object != nullptr) << PrettyField();
150 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
151 if (UNLIKELY(IsVolatile())) {
152 object->SetFieldObjectVolatile<kTransactionActive>(GetOffset(), new_value);
153 } else {
154 object->SetFieldObject<kTransactionActive>(GetOffset(), new_value);
155 }
156 }
157
158 #define FIELD_GET(object, type) \
159 DCHECK_EQ(Primitive::kPrim ## type, GetTypeAsPrimitiveType()) << PrettyField(); \
160 DCHECK((object) != nullptr) << PrettyField(); \
161 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
162 if (UNLIKELY(IsVolatile())) { \
163 return (object)->GetField ## type ## Volatile(GetOffset()); \
164 } \
165 return (object)->GetField ## type(GetOffset());
166
167 #define FIELD_SET(object, type, value) \
168 DCHECK((object) != nullptr) << PrettyField(); \
169 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
170 if (UNLIKELY(IsVolatile())) { \
171 (object)->SetField ## type ## Volatile<kTransactionActive>(GetOffset(), value); \
172 } else { \
173 (object)->SetField ## type<kTransactionActive>(GetOffset(), value); \
174 }
175
GetBoolean(ObjPtr<mirror::Object> object)176 inline uint8_t ArtField::GetBoolean(ObjPtr<mirror::Object> object) {
177 FIELD_GET(object, Boolean);
178 }
179
180 template<bool kTransactionActive>
SetBoolean(ObjPtr<mirror::Object> object,uint8_t z)181 inline void ArtField::SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) {
182 if (kIsDebugBuild) {
183 // For simplicity, this method is being called by the compiler entrypoint for
184 // both boolean and byte fields.
185 Primitive::Type type = GetTypeAsPrimitiveType();
186 DCHECK(type == Primitive::kPrimBoolean || type == Primitive::kPrimByte) << PrettyField();
187 }
188 FIELD_SET(object, Boolean, z);
189 }
190
GetByte(ObjPtr<mirror::Object> object)191 inline int8_t ArtField::GetByte(ObjPtr<mirror::Object> object) {
192 FIELD_GET(object, Byte);
193 }
194
195 template<bool kTransactionActive>
SetByte(ObjPtr<mirror::Object> object,int8_t b)196 inline void ArtField::SetByte(ObjPtr<mirror::Object> object, int8_t b) {
197 DCHECK_EQ(Primitive::kPrimByte, GetTypeAsPrimitiveType()) << PrettyField();
198 FIELD_SET(object, Byte, b);
199 }
200
GetChar(ObjPtr<mirror::Object> object)201 inline uint16_t ArtField::GetChar(ObjPtr<mirror::Object> object) {
202 FIELD_GET(object, Char);
203 }
204
205 template<bool kTransactionActive>
SetChar(ObjPtr<mirror::Object> object,uint16_t c)206 inline void ArtField::SetChar(ObjPtr<mirror::Object> object, uint16_t c) {
207 if (kIsDebugBuild) {
208 // For simplicity, this method is being called by the compiler entrypoint for
209 // both char and short fields.
210 Primitive::Type type = GetTypeAsPrimitiveType();
211 DCHECK(type == Primitive::kPrimChar || type == Primitive::kPrimShort) << PrettyField();
212 }
213 FIELD_SET(object, Char, c);
214 }
215
GetShort(ObjPtr<mirror::Object> object)216 inline int16_t ArtField::GetShort(ObjPtr<mirror::Object> object) {
217 FIELD_GET(object, Short);
218 }
219
220 template<bool kTransactionActive>
SetShort(ObjPtr<mirror::Object> object,int16_t s)221 inline void ArtField::SetShort(ObjPtr<mirror::Object> object, int16_t s) {
222 DCHECK_EQ(Primitive::kPrimShort, GetTypeAsPrimitiveType()) << PrettyField();
223 FIELD_SET(object, Short, s);
224 }
225
226 #undef FIELD_GET
227 #undef FIELD_SET
228
GetInt(ObjPtr<mirror::Object> object)229 inline int32_t ArtField::GetInt(ObjPtr<mirror::Object> object) {
230 if (kIsDebugBuild) {
231 // For simplicity, this method is being called by the compiler entrypoint for
232 // both int and float fields.
233 Primitive::Type type = GetTypeAsPrimitiveType();
234 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
235 }
236 return Get32(object);
237 }
238
239 template<bool kTransactionActive>
SetInt(ObjPtr<mirror::Object> object,int32_t i)240 inline void ArtField::SetInt(ObjPtr<mirror::Object> object, int32_t i) {
241 if (kIsDebugBuild) {
242 // For simplicity, this method is being called by the compiler entrypoint for
243 // both int and float fields.
244 Primitive::Type type = GetTypeAsPrimitiveType();
245 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
246 }
247 Set32<kTransactionActive>(object, i);
248 }
249
GetLong(ObjPtr<mirror::Object> object)250 inline int64_t ArtField::GetLong(ObjPtr<mirror::Object> object) {
251 if (kIsDebugBuild) {
252 // For simplicity, this method is being called by the compiler entrypoint for
253 // both long and double fields.
254 Primitive::Type type = GetTypeAsPrimitiveType();
255 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
256 }
257 return Get64(object);
258 }
259
260 template<bool kTransactionActive>
SetLong(ObjPtr<mirror::Object> object,int64_t j)261 inline void ArtField::SetLong(ObjPtr<mirror::Object> object, int64_t j) {
262 if (kIsDebugBuild) {
263 // For simplicity, this method is being called by the compiler entrypoint for
264 // both long and double fields.
265 Primitive::Type type = GetTypeAsPrimitiveType();
266 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
267 }
268 Set64<kTransactionActive>(object, j);
269 }
270
GetFloat(ObjPtr<mirror::Object> object)271 inline float ArtField::GetFloat(ObjPtr<mirror::Object> object) {
272 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
273 JValue bits;
274 bits.SetI(Get32(object));
275 return bits.GetF();
276 }
277
278 template<bool kTransactionActive>
SetFloat(ObjPtr<mirror::Object> object,float f)279 inline void ArtField::SetFloat(ObjPtr<mirror::Object> object, float f) {
280 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
281 JValue bits;
282 bits.SetF(f);
283 Set32<kTransactionActive>(object, bits.GetI());
284 }
285
GetDouble(ObjPtr<mirror::Object> object)286 inline double ArtField::GetDouble(ObjPtr<mirror::Object> object) {
287 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
288 JValue bits;
289 bits.SetJ(Get64(object));
290 return bits.GetD();
291 }
292
293 template<bool kTransactionActive>
SetDouble(ObjPtr<mirror::Object> object,double d)294 inline void ArtField::SetDouble(ObjPtr<mirror::Object> object, double d) {
295 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
296 JValue bits;
297 bits.SetD(d);
298 Set64<kTransactionActive>(object, bits.GetJ());
299 }
300
301 template<ReadBarrierOption kReadBarrierOption>
GetObject(ObjPtr<mirror::Object> object)302 inline ObjPtr<mirror::Object> ArtField::GetObject(ObjPtr<mirror::Object> object) {
303 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
304 return GetObj<mirror::Object, kReadBarrierOption>(object);
305 }
306
307 template<bool kTransactionActive>
SetObject(ObjPtr<mirror::Object> object,ObjPtr<mirror::Object> l)308 inline void ArtField::SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l) {
309 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
310 SetObj<kTransactionActive>(object, l);
311 }
312
GetName()313 inline const char* ArtField::GetName() REQUIRES_SHARED(Locks::mutator_lock_) {
314 uint32_t field_index = GetDexFieldIndex();
315 if (UNLIKELY(IsProxyField())) {
316 DCHECK(IsStatic());
317 DCHECK_LT(field_index, 2U);
318 return field_index == 0 ? "interfaces" : "throws";
319 }
320 const DexFile* dex_file = GetDexFile();
321 return dex_file->GetFieldName(dex_file->GetFieldId(field_index));
322 }
323
GetTypeDescriptor()324 inline const char* ArtField::GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
325 uint32_t field_index = GetDexFieldIndex();
326 if (UNLIKELY(IsProxyField())) {
327 DCHECK(IsStatic());
328 DCHECK_LT(field_index, 2U);
329 // 0 == Class[] interfaces; 1 == Class[][] throws;
330 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
331 }
332 const DexFile* dex_file = GetDexFile();
333 const dex::FieldId& field_id = dex_file->GetFieldId(field_index);
334 return dex_file->GetFieldTypeDescriptor(field_id);
335 }
336
GetTypeAsPrimitiveType()337 inline Primitive::Type ArtField::GetTypeAsPrimitiveType()
338 REQUIRES_SHARED(Locks::mutator_lock_) {
339 return Primitive::GetType(GetTypeDescriptor()[0]);
340 }
341
IsPrimitiveType()342 inline bool ArtField::IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_) {
343 return GetTypeAsPrimitiveType() != Primitive::kPrimNot;
344 }
345
LookupResolvedType()346 inline ObjPtr<mirror::Class> ArtField::LookupResolvedType() {
347 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
348 if (UNLIKELY(IsProxyField())) {
349 return ProxyFindSystemClass(GetTypeDescriptor());
350 }
351 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
352 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
353 DCHECK(!Thread::Current()->IsExceptionPending());
354 return type;
355 }
356
ResolveType()357 inline ObjPtr<mirror::Class> ArtField::ResolveType() {
358 if (UNLIKELY(IsProxyField())) {
359 return ProxyFindSystemClass(GetTypeDescriptor());
360 }
361 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(
362 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
363 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
364 return type;
365 }
366
FieldSize()367 inline size_t ArtField::FieldSize() REQUIRES_SHARED(Locks::mutator_lock_) {
368 return Primitive::ComponentSize(GetTypeAsPrimitiveType());
369 }
370
371 template <ReadBarrierOption kReadBarrierOption>
GetDexCache()372 inline ObjPtr<mirror::DexCache> ArtField::GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_) {
373 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
374 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
375 }
376
GetDexFile()377 inline const DexFile* ArtField::GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_) {
378 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
379 }
380
ResolveNameString()381 inline ObjPtr<mirror::String> ArtField::ResolveNameString() {
382 uint32_t dex_field_index = GetDexFieldIndex();
383 CHECK_NE(dex_field_index, dex::kDexNoIndex);
384 const dex::FieldId& field_id = GetDexFile()->GetFieldId(dex_field_index);
385 return Runtime::Current()->GetClassLinker()->ResolveString(field_id.name_idx_, this);
386 }
387
388 // If kExactOffset is true then we only find the matching offset, not the field containing the
389 // offset.
390 template <bool kExactOffset>
FindFieldWithOffset(const IterationRange<StrideIterator<ArtField>> & fields,uint32_t field_offset)391 static inline ArtField* FindFieldWithOffset(
392 const IterationRange<StrideIterator<ArtField>>& fields,
393 uint32_t field_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
394 for (ArtField& field : fields) {
395 if (kExactOffset) {
396 if (field.GetOffset().Uint32Value() == field_offset) {
397 return &field;
398 }
399 } else {
400 const uint32_t offset = field.GetOffset().Uint32Value();
401 Primitive::Type type = field.GetTypeAsPrimitiveType();
402 const size_t field_size = Primitive::ComponentSize(type);
403 DCHECK_GT(field_size, 0u);
404 if (offset <= field_offset && field_offset < offset + field_size) {
405 return &field;
406 }
407 }
408 }
409 return nullptr;
410 }
411
412 template <bool kExactOffset>
FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)413 inline ArtField* ArtField::FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,
414 uint32_t field_offset) {
415 DCHECK(klass != nullptr);
416 ArtField* field = FindFieldWithOffset<kExactOffset>(klass->GetIFields(), field_offset);
417 if (field != nullptr) {
418 return field;
419 }
420 // We did not find field in the class: look into superclass.
421 return (klass->GetSuperClass() != nullptr) ?
422 FindInstanceFieldWithOffset<kExactOffset>(klass->GetSuperClass(), field_offset) : nullptr;
423 }
424
425 template <bool kExactOffset>
FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)426 inline ArtField* ArtField::FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,
427 uint32_t field_offset) {
428 DCHECK(klass != nullptr);
429 return FindFieldWithOffset<kExactOffset>(klass->GetSFields(), field_offset);
430 }
431
GetClassLoader()432 inline ObjPtr<mirror::ClassLoader> ArtField::GetClassLoader() {
433 return GetDeclaringClass()->GetClassLoader();
434 }
435
436 } // namespace art
437
438 #endif // ART_RUNTIME_ART_FIELD_INL_H_
439