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. We do need to be careful since if a
44 // structural redefinition occurs during <clinit> we can end up trying to set the non-obsolete
45 // class's fields from the obsolete class. This is something we want to allow. This is tested by
46 // run-test 2002-virtual-structural-initializing.
CanBeChangedBy(ArtMethod * method)47 inline bool ArtField::CanBeChangedBy(ArtMethod* method) {
48 ObjPtr<mirror::Class> declaring_class(GetDeclaringClass());
49 ObjPtr<mirror::Class> referring_class(method->GetDeclaringClass());
50 return !IsFinal() || (declaring_class == referring_class) ||
51 UNLIKELY(referring_class->IsObsoleteVersionOf(declaring_class));
52 }
53
54 template<ReadBarrierOption kReadBarrierOption>
GetDeclaringClass()55 inline ObjPtr<mirror::Class> ArtField::GetDeclaringClass() {
56 GcRootSource gc_root_source(this);
57 ObjPtr<mirror::Class> result = declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
58 DCHECK(result != nullptr);
59 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << result->GetStatus();
60 return result;
61 }
62
SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)63 inline void ArtField::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
64 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
65 }
66
GetOffsetDuringLinking()67 inline MemberOffset ArtField::GetOffsetDuringLinking() {
68 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
69 return MemberOffset(offset_);
70 }
71
Get32(ObjPtr<mirror::Object> object)72 inline uint32_t ArtField::Get32(ObjPtr<mirror::Object> object) {
73 DCHECK(object != nullptr) << PrettyField();
74 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
75 if (UNLIKELY(IsVolatile())) {
76 return object->GetField32Volatile(GetOffset());
77 }
78 return object->GetField32(GetOffset());
79 }
80
81 template<bool kTransactionActive>
Set32(ObjPtr<mirror::Object> object,uint32_t new_value)82 inline void ArtField::Set32(ObjPtr<mirror::Object> object, uint32_t new_value) {
83 DCHECK(object != nullptr) << PrettyField();
84 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
85 if (UNLIKELY(IsVolatile())) {
86 object->SetField32Volatile<kTransactionActive>(GetOffset(), new_value);
87 } else {
88 object->SetField32<kTransactionActive>(GetOffset(), new_value);
89 }
90 }
91
Get64(ObjPtr<mirror::Object> object)92 inline uint64_t ArtField::Get64(ObjPtr<mirror::Object> object) {
93 DCHECK(object != nullptr) << PrettyField();
94 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
95 if (UNLIKELY(IsVolatile())) {
96 return object->GetField64Volatile(GetOffset());
97 }
98 return object->GetField64(GetOffset());
99 }
100
101 template<bool kTransactionActive>
Set64(ObjPtr<mirror::Object> object,uint64_t new_value)102 inline void ArtField::Set64(ObjPtr<mirror::Object> object, uint64_t new_value) {
103 DCHECK(object != nullptr) << PrettyField();
104 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
105 if (UNLIKELY(IsVolatile())) {
106 object->SetField64Volatile<kTransactionActive>(GetOffset(), new_value);
107 } else {
108 object->SetField64<kTransactionActive>(GetOffset(), new_value);
109 }
110 }
111
112 template<class MirrorType, ReadBarrierOption kReadBarrierOption>
GetObj(ObjPtr<mirror::Object> object)113 inline ObjPtr<MirrorType> ArtField::GetObj(ObjPtr<mirror::Object> object) {
114 DCHECK(object != nullptr) << PrettyField();
115 DCHECK(!IsStatic() ||
116 (object == GetDeclaringClass<kReadBarrierOption>()) ||
117 !Runtime::Current()->IsStarted());
118 if (UNLIKELY(IsVolatile())) {
119 return object->GetFieldObjectVolatile<MirrorType, kDefaultVerifyFlags, kReadBarrierOption>(
120 GetOffset());
121 }
122 return object->GetFieldObject<MirrorType, kDefaultVerifyFlags, kReadBarrierOption>(GetOffset());
123 }
124
125 template<bool kTransactionActive>
SetObj(ObjPtr<mirror::Object> object,ObjPtr<mirror::Object> new_value)126 inline void ArtField::SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value) {
127 DCHECK(object != nullptr) << PrettyField();
128 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
129 if (UNLIKELY(IsVolatile())) {
130 object->SetFieldObjectVolatile<kTransactionActive>(GetOffset(), new_value);
131 } else {
132 object->SetFieldObject<kTransactionActive>(GetOffset(), new_value);
133 }
134 }
135
136 #define FIELD_GET(object, type) \
137 DCHECK_EQ(Primitive::kPrim ## type, GetTypeAsPrimitiveType()) << PrettyField(); \
138 DCHECK((object) != nullptr) << PrettyField(); \
139 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
140 if (UNLIKELY(IsVolatile())) { \
141 return (object)->GetField ## type ## Volatile(GetOffset()); \
142 } \
143 return (object)->GetField ## type(GetOffset());
144
145 #define FIELD_SET(object, type, value) \
146 DCHECK((object) != nullptr) << PrettyField(); \
147 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
148 if (UNLIKELY(IsVolatile())) { \
149 (object)->SetField ## type ## Volatile<kTransactionActive>(GetOffset(), value); \
150 } else { \
151 (object)->SetField ## type<kTransactionActive>(GetOffset(), value); \
152 }
153
GetBoolean(ObjPtr<mirror::Object> object)154 inline uint8_t ArtField::GetBoolean(ObjPtr<mirror::Object> object) {
155 FIELD_GET(object, Boolean);
156 }
157
158 template<bool kTransactionActive>
SetBoolean(ObjPtr<mirror::Object> object,uint8_t z)159 inline void ArtField::SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) {
160 if (kIsDebugBuild) {
161 // For simplicity, this method is being called by the compiler entrypoint for
162 // both boolean and byte fields.
163 Primitive::Type type = GetTypeAsPrimitiveType();
164 DCHECK(type == Primitive::kPrimBoolean || type == Primitive::kPrimByte) << PrettyField();
165 }
166 FIELD_SET(object, Boolean, z);
167 }
168
GetByte(ObjPtr<mirror::Object> object)169 inline int8_t ArtField::GetByte(ObjPtr<mirror::Object> object) {
170 FIELD_GET(object, Byte);
171 }
172
173 template<bool kTransactionActive>
SetByte(ObjPtr<mirror::Object> object,int8_t b)174 inline void ArtField::SetByte(ObjPtr<mirror::Object> object, int8_t b) {
175 DCHECK_EQ(Primitive::kPrimByte, GetTypeAsPrimitiveType()) << PrettyField();
176 FIELD_SET(object, Byte, b);
177 }
178
GetChar(ObjPtr<mirror::Object> object)179 inline uint16_t ArtField::GetChar(ObjPtr<mirror::Object> object) {
180 FIELD_GET(object, Char);
181 }
182
183 template<bool kTransactionActive>
SetChar(ObjPtr<mirror::Object> object,uint16_t c)184 inline void ArtField::SetChar(ObjPtr<mirror::Object> object, uint16_t c) {
185 if (kIsDebugBuild) {
186 // For simplicity, this method is being called by the compiler entrypoint for
187 // both char and short fields.
188 Primitive::Type type = GetTypeAsPrimitiveType();
189 DCHECK(type == Primitive::kPrimChar || type == Primitive::kPrimShort) << PrettyField();
190 }
191 FIELD_SET(object, Char, c);
192 }
193
GetShort(ObjPtr<mirror::Object> object)194 inline int16_t ArtField::GetShort(ObjPtr<mirror::Object> object) {
195 FIELD_GET(object, Short);
196 }
197
198 template<bool kTransactionActive>
SetShort(ObjPtr<mirror::Object> object,int16_t s)199 inline void ArtField::SetShort(ObjPtr<mirror::Object> object, int16_t s) {
200 DCHECK_EQ(Primitive::kPrimShort, GetTypeAsPrimitiveType()) << PrettyField();
201 FIELD_SET(object, Short, s);
202 }
203
204 #undef FIELD_GET
205 #undef FIELD_SET
206
GetInt(ObjPtr<mirror::Object> object)207 inline int32_t ArtField::GetInt(ObjPtr<mirror::Object> object) {
208 if (kIsDebugBuild) {
209 // For simplicity, this method is being called by the compiler entrypoint for
210 // both int and float fields.
211 Primitive::Type type = GetTypeAsPrimitiveType();
212 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
213 }
214 return Get32(object);
215 }
216
217 template<bool kTransactionActive>
SetInt(ObjPtr<mirror::Object> object,int32_t i)218 inline void ArtField::SetInt(ObjPtr<mirror::Object> object, int32_t i) {
219 if (kIsDebugBuild) {
220 // For simplicity, this method is being called by the compiler entrypoint for
221 // both int and float fields.
222 Primitive::Type type = GetTypeAsPrimitiveType();
223 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
224 }
225 Set32<kTransactionActive>(object, i);
226 }
227
GetLong(ObjPtr<mirror::Object> object)228 inline int64_t ArtField::GetLong(ObjPtr<mirror::Object> object) {
229 if (kIsDebugBuild) {
230 // For simplicity, this method is being called by the compiler entrypoint for
231 // both long and double fields.
232 Primitive::Type type = GetTypeAsPrimitiveType();
233 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
234 }
235 return Get64(object);
236 }
237
238 template<bool kTransactionActive>
SetLong(ObjPtr<mirror::Object> object,int64_t j)239 inline void ArtField::SetLong(ObjPtr<mirror::Object> object, int64_t j) {
240 if (kIsDebugBuild) {
241 // For simplicity, this method is being called by the compiler entrypoint for
242 // both long and double fields.
243 Primitive::Type type = GetTypeAsPrimitiveType();
244 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
245 }
246 Set64<kTransactionActive>(object, j);
247 }
248
GetFloat(ObjPtr<mirror::Object> object)249 inline float ArtField::GetFloat(ObjPtr<mirror::Object> object) {
250 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
251 JValue bits;
252 bits.SetI(Get32(object));
253 return bits.GetF();
254 }
255
256 template<bool kTransactionActive>
SetFloat(ObjPtr<mirror::Object> object,float f)257 inline void ArtField::SetFloat(ObjPtr<mirror::Object> object, float f) {
258 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
259 JValue bits;
260 bits.SetF(f);
261 Set32<kTransactionActive>(object, bits.GetI());
262 }
263
GetDouble(ObjPtr<mirror::Object> object)264 inline double ArtField::GetDouble(ObjPtr<mirror::Object> object) {
265 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
266 JValue bits;
267 bits.SetJ(Get64(object));
268 return bits.GetD();
269 }
270
271 template<bool kTransactionActive>
SetDouble(ObjPtr<mirror::Object> object,double d)272 inline void ArtField::SetDouble(ObjPtr<mirror::Object> object, double d) {
273 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
274 JValue bits;
275 bits.SetD(d);
276 Set64<kTransactionActive>(object, bits.GetJ());
277 }
278
279 template<ReadBarrierOption kReadBarrierOption>
GetObject(ObjPtr<mirror::Object> object)280 inline ObjPtr<mirror::Object> ArtField::GetObject(ObjPtr<mirror::Object> object) {
281 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
282 return GetObj<mirror::Object, kReadBarrierOption>(object);
283 }
284
285 template<bool kTransactionActive>
SetObject(ObjPtr<mirror::Object> object,ObjPtr<mirror::Object> l)286 inline void ArtField::SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l) {
287 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
288 SetObj<kTransactionActive>(object, l);
289 }
290
GetName()291 inline const char* ArtField::GetName() REQUIRES_SHARED(Locks::mutator_lock_) {
292 uint32_t field_index = GetDexFieldIndex();
293 if (UNLIKELY(IsProxyField())) {
294 DCHECK(IsStatic());
295 DCHECK_LT(field_index, 2U);
296 return field_index == 0 ? "interfaces" : "throws";
297 }
298 const DexFile* dex_file = GetDexFile();
299 return dex_file->GetFieldName(dex_file->GetFieldId(field_index));
300 }
301
GetTypeDescriptor()302 inline const char* ArtField::GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
303 uint32_t field_index = GetDexFieldIndex();
304 if (UNLIKELY(IsProxyField())) {
305 DCHECK(IsStatic());
306 DCHECK_LT(field_index, 2U);
307 // 0 == Class[] interfaces; 1 == Class[][] throws;
308 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
309 }
310 const DexFile* dex_file = GetDexFile();
311 const dex::FieldId& field_id = dex_file->GetFieldId(field_index);
312 return dex_file->GetFieldTypeDescriptor(field_id);
313 }
314
GetTypeAsPrimitiveType()315 inline Primitive::Type ArtField::GetTypeAsPrimitiveType()
316 REQUIRES_SHARED(Locks::mutator_lock_) {
317 return Primitive::GetType(GetTypeDescriptor()[0]);
318 }
319
IsPrimitiveType()320 inline bool ArtField::IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_) {
321 return GetTypeAsPrimitiveType() != Primitive::kPrimNot;
322 }
323
LookupResolvedType()324 inline ObjPtr<mirror::Class> ArtField::LookupResolvedType() {
325 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
326 if (UNLIKELY(IsProxyField())) {
327 return ProxyFindSystemClass(GetTypeDescriptor());
328 }
329 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
330 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
331 DCHECK(!Thread::Current()->IsExceptionPending());
332 return type;
333 }
334
ResolveType()335 inline ObjPtr<mirror::Class> ArtField::ResolveType() {
336 if (UNLIKELY(IsProxyField())) {
337 return ProxyFindSystemClass(GetTypeDescriptor());
338 }
339 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(
340 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
341 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
342 return type;
343 }
344
FieldSize()345 inline size_t ArtField::FieldSize() REQUIRES_SHARED(Locks::mutator_lock_) {
346 return Primitive::ComponentSize(GetTypeAsPrimitiveType());
347 }
348
349 template <ReadBarrierOption kReadBarrierOption>
GetDexCache()350 inline ObjPtr<mirror::DexCache> ArtField::GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_) {
351 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
352 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
353 }
354
GetDexFile()355 inline const DexFile* ArtField::GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_) {
356 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
357 }
358
ResolveNameString()359 inline ObjPtr<mirror::String> ArtField::ResolveNameString() {
360 uint32_t dex_field_index = GetDexFieldIndex();
361 CHECK_NE(dex_field_index, dex::kDexNoIndex);
362 const dex::FieldId& field_id = GetDexFile()->GetFieldId(dex_field_index);
363 return Runtime::Current()->GetClassLinker()->ResolveString(field_id.name_idx_, this);
364 }
365
366 // If kExactOffset is true then we only find the matching offset, not the field containing the
367 // offset.
368 template <bool kExactOffset>
FindFieldWithOffset(const IterationRange<StrideIterator<ArtField>> & fields,uint32_t field_offset)369 static inline ArtField* FindFieldWithOffset(
370 const IterationRange<StrideIterator<ArtField>>& fields,
371 uint32_t field_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
372 for (ArtField& field : fields) {
373 if (kExactOffset) {
374 if (field.GetOffset().Uint32Value() == field_offset) {
375 return &field;
376 }
377 } else {
378 const uint32_t offset = field.GetOffset().Uint32Value();
379 Primitive::Type type = field.GetTypeAsPrimitiveType();
380 const size_t field_size = Primitive::ComponentSize(type);
381 DCHECK_GT(field_size, 0u);
382 if (offset <= field_offset && field_offset < offset + field_size) {
383 return &field;
384 }
385 }
386 }
387 return nullptr;
388 }
389
390 template <bool kExactOffset>
FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)391 inline ArtField* ArtField::FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,
392 uint32_t field_offset) {
393 DCHECK(klass != nullptr);
394 ArtField* field = FindFieldWithOffset<kExactOffset>(klass->GetIFields(), field_offset);
395 if (field != nullptr) {
396 return field;
397 }
398 // We did not find field in the class: look into superclass.
399 return (klass->GetSuperClass() != nullptr) ?
400 FindInstanceFieldWithOffset<kExactOffset>(klass->GetSuperClass(), field_offset) : nullptr;
401 }
402
403 template <bool kExactOffset>
FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)404 inline ArtField* ArtField::FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,
405 uint32_t field_offset) {
406 DCHECK(klass != nullptr);
407 return FindFieldWithOffset<kExactOffset>(klass->GetSFields(), field_offset);
408 }
409
GetClassLoader()410 inline ObjPtr<mirror::ClassLoader> ArtField::GetClassLoader() {
411 return GetDeclaringClass()->GetClassLoader();
412 }
413
414 } // namespace art
415
416 #endif // ART_RUNTIME_ART_FIELD_INL_H_
417