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 HIDDEN {
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 end_boundary = std::min(end_boundary, reinterpret_cast<uint8_t*>(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
GetCharacter(ObjPtr<mirror::Object> object)205 inline uint16_t ArtField::GetCharacter(ObjPtr<mirror::Object> object) {
206 return GetChar(object);
207 }
208
209 template<bool kTransactionActive>
SetChar(ObjPtr<mirror::Object> object,uint16_t c)210 inline void ArtField::SetChar(ObjPtr<mirror::Object> object, uint16_t c) {
211 if (kIsDebugBuild) {
212 // For simplicity, this method is being called by the compiler entrypoint for
213 // both char and short fields.
214 Primitive::Type type = GetTypeAsPrimitiveType();
215 DCHECK(type == Primitive::kPrimChar || type == Primitive::kPrimShort) << PrettyField();
216 }
217 FIELD_SET(object, Char, c);
218 }
219
GetShort(ObjPtr<mirror::Object> object)220 inline int16_t ArtField::GetShort(ObjPtr<mirror::Object> object) {
221 FIELD_GET(object, Short);
222 }
223
224 template<bool kTransactionActive>
SetShort(ObjPtr<mirror::Object> object,int16_t s)225 inline void ArtField::SetShort(ObjPtr<mirror::Object> object, int16_t s) {
226 DCHECK_EQ(Primitive::kPrimShort, GetTypeAsPrimitiveType()) << PrettyField();
227 FIELD_SET(object, Short, s);
228 }
229
230 #undef FIELD_GET
231 #undef FIELD_SET
232
GetInt(ObjPtr<mirror::Object> object)233 inline int32_t ArtField::GetInt(ObjPtr<mirror::Object> object) {
234 if (kIsDebugBuild) {
235 // For simplicity, this method is being called by the compiler entrypoint for
236 // both int and float fields.
237 Primitive::Type type = GetTypeAsPrimitiveType();
238 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
239 }
240 return Get32(object);
241 }
242
GetInteger(ObjPtr<mirror::Object> object)243 inline int32_t ArtField::GetInteger(ObjPtr<mirror::Object> object) {
244 return GetInt(object);
245 }
246
247 template<bool kTransactionActive>
SetInt(ObjPtr<mirror::Object> object,int32_t i)248 inline void ArtField::SetInt(ObjPtr<mirror::Object> object, int32_t i) {
249 if (kIsDebugBuild) {
250 // For simplicity, this method is being called by the compiler entrypoint for
251 // both int and float fields.
252 Primitive::Type type = GetTypeAsPrimitiveType();
253 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
254 }
255 Set32<kTransactionActive>(object, i);
256 }
257
GetLong(ObjPtr<mirror::Object> object)258 inline int64_t ArtField::GetLong(ObjPtr<mirror::Object> object) {
259 if (kIsDebugBuild) {
260 // For simplicity, this method is being called by the compiler entrypoint for
261 // both long and double fields.
262 Primitive::Type type = GetTypeAsPrimitiveType();
263 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
264 }
265 return Get64(object);
266 }
267
268 template<bool kTransactionActive>
SetLong(ObjPtr<mirror::Object> object,int64_t j)269 inline void ArtField::SetLong(ObjPtr<mirror::Object> object, int64_t j) {
270 if (kIsDebugBuild) {
271 // For simplicity, this method is being called by the compiler entrypoint for
272 // both long and double fields.
273 Primitive::Type type = GetTypeAsPrimitiveType();
274 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
275 }
276 Set64<kTransactionActive>(object, j);
277 }
278
GetFloat(ObjPtr<mirror::Object> object)279 inline float ArtField::GetFloat(ObjPtr<mirror::Object> object) {
280 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
281 JValue bits;
282 bits.SetI(Get32(object));
283 return bits.GetF();
284 }
285
286 template<bool kTransactionActive>
SetFloat(ObjPtr<mirror::Object> object,float f)287 inline void ArtField::SetFloat(ObjPtr<mirror::Object> object, float f) {
288 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
289 JValue bits;
290 bits.SetF(f);
291 Set32<kTransactionActive>(object, bits.GetI());
292 }
293
GetDouble(ObjPtr<mirror::Object> object)294 inline double ArtField::GetDouble(ObjPtr<mirror::Object> object) {
295 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
296 JValue bits;
297 bits.SetJ(Get64(object));
298 return bits.GetD();
299 }
300
301 template<bool kTransactionActive>
SetDouble(ObjPtr<mirror::Object> object,double d)302 inline void ArtField::SetDouble(ObjPtr<mirror::Object> object, double d) {
303 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
304 JValue bits;
305 bits.SetD(d);
306 Set64<kTransactionActive>(object, bits.GetJ());
307 }
308
309 template<ReadBarrierOption kReadBarrierOption>
GetObject(ObjPtr<mirror::Object> object)310 inline ObjPtr<mirror::Object> ArtField::GetObject(ObjPtr<mirror::Object> object) {
311 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
312 return GetObj<mirror::Object, kReadBarrierOption>(object);
313 }
314
315 template<bool kTransactionActive>
SetObject(ObjPtr<mirror::Object> object,ObjPtr<mirror::Object> l)316 inline void ArtField::SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l) {
317 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
318 SetObj<kTransactionActive>(object, l);
319 }
320
GetName()321 inline const char* ArtField::GetName() {
322 uint32_t field_index = GetDexFieldIndex();
323 if (UNLIKELY(IsProxyField())) {
324 DCHECK(IsStatic());
325 DCHECK_LT(field_index, 2U);
326 return field_index == 0 ? "interfaces" : "throws";
327 }
328 return GetDexFile()->GetFieldName(field_index);
329 }
330
GetNameView()331 inline std::string_view ArtField::GetNameView() {
332 uint32_t field_index = GetDexFieldIndex();
333 if (UNLIKELY(IsProxyField())) {
334 DCHECK(IsStatic());
335 DCHECK_LT(field_index, 2U);
336 return field_index == 0 ? "interfaces" : "throws";
337 }
338 return GetDexFile()->GetFieldNameView(field_index);
339 }
340
GetTypeDescriptor()341 inline const char* ArtField::GetTypeDescriptor() {
342 uint32_t field_index = GetDexFieldIndex();
343 if (UNLIKELY(IsProxyField())) {
344 DCHECK(IsStatic());
345 DCHECK_LT(field_index, 2U);
346 // 0 == Class[] interfaces; 1 == Class[][] throws;
347 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
348 }
349 return GetDexFile()->GetFieldTypeDescriptor(field_index);
350 }
351
GetTypeDescriptorView()352 inline std::string_view ArtField::GetTypeDescriptorView() {
353 uint32_t field_index = GetDexFieldIndex();
354 if (UNLIKELY(IsProxyField())) {
355 DCHECK(IsStatic());
356 DCHECK_LT(field_index, 2U);
357 // 0 == Class[] interfaces; 1 == Class[][] throws;
358 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
359 }
360 return GetDexFile()->GetFieldTypeDescriptorView(field_index);
361 }
362
GetTypeAsPrimitiveType()363 inline Primitive::Type ArtField::GetTypeAsPrimitiveType() {
364 return Primitive::GetType(GetTypeDescriptor()[0]);
365 }
366
IsPrimitiveType()367 inline bool ArtField::IsPrimitiveType() {
368 return GetTypeAsPrimitiveType() != Primitive::kPrimNot;
369 }
370
LookupResolvedType()371 inline ObjPtr<mirror::Class> ArtField::LookupResolvedType() {
372 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
373 if (UNLIKELY(IsProxyField())) {
374 return ProxyFindSystemClass(GetTypeDescriptor());
375 }
376 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
377 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
378 DCHECK(!Thread::Current()->IsExceptionPending());
379 return type;
380 }
381
ResolveType()382 inline ObjPtr<mirror::Class> ArtField::ResolveType() {
383 if (UNLIKELY(IsProxyField())) {
384 return ProxyFindSystemClass(GetTypeDescriptor());
385 }
386 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(
387 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
388 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
389 return type;
390 }
391
FieldSize()392 inline size_t ArtField::FieldSize() {
393 return Primitive::ComponentSize(GetTypeAsPrimitiveType());
394 }
395
396 template <ReadBarrierOption kReadBarrierOption>
GetDexCache()397 inline ObjPtr<mirror::DexCache> ArtField::GetDexCache() {
398 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
399 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
400 }
401
GetDexFile()402 inline const DexFile* ArtField::GetDexFile() {
403 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
404 }
405
GetDeclaringClassDescriptor()406 inline const char* ArtField::GetDeclaringClassDescriptor() {
407 DCHECK(!IsProxyField());
408 return GetDexFile()->GetFieldDeclaringClassDescriptor(GetDexFieldIndex());
409 }
410
GetDeclaringClassDescriptorView()411 inline std::string_view ArtField::GetDeclaringClassDescriptorView() {
412 DCHECK(!IsProxyField());
413 return GetDexFile()->GetFieldDeclaringClassDescriptorView(GetDexFieldIndex());
414 }
415
ResolveNameString()416 inline ObjPtr<mirror::String> ArtField::ResolveNameString() {
417 uint32_t dex_field_index = GetDexFieldIndex();
418 CHECK_NE(dex_field_index, dex::kDexNoIndex);
419 const dex::FieldId& field_id = GetDexFile()->GetFieldId(dex_field_index);
420 return Runtime::Current()->GetClassLinker()->ResolveString(field_id.name_idx_, this);
421 }
422
423 // If kExactOffset is true then we only find the matching offset, not the field containing the
424 // offset.
425 template <bool kExactOffset>
FindFieldWithOffset(const IterationRange<StrideIterator<ArtField>> & fields,uint32_t field_offset)426 static inline ArtField* FindFieldWithOffset(
427 const IterationRange<StrideIterator<ArtField>>& fields,
428 uint32_t field_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
429 for (ArtField& field : fields) {
430 if (kExactOffset) {
431 if (field.GetOffset().Uint32Value() == field_offset) {
432 return &field;
433 }
434 } else {
435 const uint32_t offset = field.GetOffset().Uint32Value();
436 Primitive::Type type = field.GetTypeAsPrimitiveType();
437 const size_t field_size = Primitive::ComponentSize(type);
438 DCHECK_GT(field_size, 0u);
439 if (offset <= field_offset && field_offset < offset + field_size) {
440 return &field;
441 }
442 }
443 }
444 return nullptr;
445 }
446
447 template <bool kExactOffset, VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)448 inline ArtField* ArtField::FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,
449 uint32_t field_offset) {
450 DCHECK(klass != nullptr);
451 ArtField* field = FindFieldWithOffset<kExactOffset>(klass->GetIFields(), field_offset);
452 if (field != nullptr) {
453 return field;
454 }
455 // We did not find field in the class: look into superclass.
456 ObjPtr<mirror::Class> super_class = klass->GetSuperClass<kVerifyFlags, kReadBarrierOption>();
457 return (super_class != nullptr)
458 ? FindInstanceFieldWithOffset<kExactOffset, kVerifyFlags, kReadBarrierOption>(
459 super_class, field_offset) :
460 nullptr;
461 }
462
463 template <bool kExactOffset>
FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)464 inline ArtField* ArtField::FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,
465 uint32_t field_offset) {
466 DCHECK(klass != nullptr);
467 return FindFieldWithOffset<kExactOffset>(klass->GetSFields(), field_offset);
468 }
469
GetClassLoader()470 inline ObjPtr<mirror::ClassLoader> ArtField::GetClassLoader() {
471 return GetDeclaringClass()->GetClassLoader();
472 }
473
474 } // namespace art
475
476 #endif // ART_RUNTIME_ART_FIELD_INL_H_
477