• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
GetObj(ObjPtr<mirror::Object> object)113 inline ObjPtr<MirrorType> ArtField::GetObj(ObjPtr<mirror::Object> object) {
114   DCHECK(object != nullptr) << PrettyField();
115   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
116   if (UNLIKELY(IsVolatile())) {
117     return object->GetFieldObjectVolatile<MirrorType>(GetOffset());
118   }
119   return object->GetFieldObject<MirrorType>(GetOffset());
120 }
121 
122 template<bool kTransactionActive>
SetObj(ObjPtr<mirror::Object> object,ObjPtr<mirror::Object> new_value)123 inline void ArtField::SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value) {
124   DCHECK(object != nullptr) << PrettyField();
125   DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
126   if (UNLIKELY(IsVolatile())) {
127     object->SetFieldObjectVolatile<kTransactionActive>(GetOffset(), new_value);
128   } else {
129     object->SetFieldObject<kTransactionActive>(GetOffset(), new_value);
130   }
131 }
132 
133 #define FIELD_GET(object, type) \
134   DCHECK_EQ(Primitive::kPrim ## type, GetTypeAsPrimitiveType()) << PrettyField(); \
135   DCHECK((object) != nullptr) << PrettyField(); \
136   DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
137   if (UNLIKELY(IsVolatile())) { \
138     return (object)->GetField ## type ## Volatile(GetOffset()); \
139   } \
140   return (object)->GetField ## type(GetOffset());
141 
142 #define FIELD_SET(object, type, value) \
143   DCHECK((object) != nullptr) << PrettyField(); \
144   DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
145   if (UNLIKELY(IsVolatile())) { \
146     (object)->SetField ## type ## Volatile<kTransactionActive>(GetOffset(), value); \
147   } else { \
148     (object)->SetField ## type<kTransactionActive>(GetOffset(), value); \
149   }
150 
GetBoolean(ObjPtr<mirror::Object> object)151 inline uint8_t ArtField::GetBoolean(ObjPtr<mirror::Object> object) {
152   FIELD_GET(object, Boolean);
153 }
154 
155 template<bool kTransactionActive>
SetBoolean(ObjPtr<mirror::Object> object,uint8_t z)156 inline void ArtField::SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) {
157   if (kIsDebugBuild) {
158     // For simplicity, this method is being called by the compiler entrypoint for
159     // both boolean and byte fields.
160     Primitive::Type type = GetTypeAsPrimitiveType();
161     DCHECK(type == Primitive::kPrimBoolean || type == Primitive::kPrimByte) << PrettyField();
162   }
163   FIELD_SET(object, Boolean, z);
164 }
165 
GetByte(ObjPtr<mirror::Object> object)166 inline int8_t ArtField::GetByte(ObjPtr<mirror::Object> object) {
167   FIELD_GET(object, Byte);
168 }
169 
170 template<bool kTransactionActive>
SetByte(ObjPtr<mirror::Object> object,int8_t b)171 inline void ArtField::SetByte(ObjPtr<mirror::Object> object, int8_t b) {
172   DCHECK_EQ(Primitive::kPrimByte, GetTypeAsPrimitiveType()) << PrettyField();
173   FIELD_SET(object, Byte, b);
174 }
175 
GetChar(ObjPtr<mirror::Object> object)176 inline uint16_t ArtField::GetChar(ObjPtr<mirror::Object> object) {
177   FIELD_GET(object, Char);
178 }
179 
180 template<bool kTransactionActive>
SetChar(ObjPtr<mirror::Object> object,uint16_t c)181 inline void ArtField::SetChar(ObjPtr<mirror::Object> object, uint16_t c) {
182   if (kIsDebugBuild) {
183     // For simplicity, this method is being called by the compiler entrypoint for
184     // both char and short fields.
185     Primitive::Type type = GetTypeAsPrimitiveType();
186     DCHECK(type == Primitive::kPrimChar || type == Primitive::kPrimShort) << PrettyField();
187   }
188   FIELD_SET(object, Char, c);
189 }
190 
GetShort(ObjPtr<mirror::Object> object)191 inline int16_t ArtField::GetShort(ObjPtr<mirror::Object> object) {
192   FIELD_GET(object, Short);
193 }
194 
195 template<bool kTransactionActive>
SetShort(ObjPtr<mirror::Object> object,int16_t s)196 inline void ArtField::SetShort(ObjPtr<mirror::Object> object, int16_t s) {
197   DCHECK_EQ(Primitive::kPrimShort, GetTypeAsPrimitiveType()) << PrettyField();
198   FIELD_SET(object, Short, s);
199 }
200 
201 #undef FIELD_GET
202 #undef FIELD_SET
203 
GetInt(ObjPtr<mirror::Object> object)204 inline int32_t ArtField::GetInt(ObjPtr<mirror::Object> object) {
205   if (kIsDebugBuild) {
206     // For simplicity, this method is being called by the compiler entrypoint for
207     // both int and float fields.
208     Primitive::Type type = GetTypeAsPrimitiveType();
209     CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
210   }
211   return Get32(object);
212 }
213 
214 template<bool kTransactionActive>
SetInt(ObjPtr<mirror::Object> object,int32_t i)215 inline void ArtField::SetInt(ObjPtr<mirror::Object> object, int32_t i) {
216   if (kIsDebugBuild) {
217     // For simplicity, this method is being called by the compiler entrypoint for
218     // both int and float fields.
219     Primitive::Type type = GetTypeAsPrimitiveType();
220     CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
221   }
222   Set32<kTransactionActive>(object, i);
223 }
224 
GetLong(ObjPtr<mirror::Object> object)225 inline int64_t ArtField::GetLong(ObjPtr<mirror::Object> object) {
226   if (kIsDebugBuild) {
227     // For simplicity, this method is being called by the compiler entrypoint for
228     // both long and double fields.
229     Primitive::Type type = GetTypeAsPrimitiveType();
230     CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
231   }
232   return Get64(object);
233 }
234 
235 template<bool kTransactionActive>
SetLong(ObjPtr<mirror::Object> object,int64_t j)236 inline void ArtField::SetLong(ObjPtr<mirror::Object> object, int64_t j) {
237   if (kIsDebugBuild) {
238     // For simplicity, this method is being called by the compiler entrypoint for
239     // both long and double fields.
240     Primitive::Type type = GetTypeAsPrimitiveType();
241     CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
242   }
243   Set64<kTransactionActive>(object, j);
244 }
245 
GetFloat(ObjPtr<mirror::Object> object)246 inline float ArtField::GetFloat(ObjPtr<mirror::Object> object) {
247   DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
248   JValue bits;
249   bits.SetI(Get32(object));
250   return bits.GetF();
251 }
252 
253 template<bool kTransactionActive>
SetFloat(ObjPtr<mirror::Object> object,float f)254 inline void ArtField::SetFloat(ObjPtr<mirror::Object> object, float f) {
255   DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
256   JValue bits;
257   bits.SetF(f);
258   Set32<kTransactionActive>(object, bits.GetI());
259 }
260 
GetDouble(ObjPtr<mirror::Object> object)261 inline double ArtField::GetDouble(ObjPtr<mirror::Object> object) {
262   DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
263   JValue bits;
264   bits.SetJ(Get64(object));
265   return bits.GetD();
266 }
267 
268 template<bool kTransactionActive>
SetDouble(ObjPtr<mirror::Object> object,double d)269 inline void ArtField::SetDouble(ObjPtr<mirror::Object> object, double d) {
270   DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
271   JValue bits;
272   bits.SetD(d);
273   Set64<kTransactionActive>(object, bits.GetJ());
274 }
275 
GetObject(ObjPtr<mirror::Object> object)276 inline ObjPtr<mirror::Object> ArtField::GetObject(ObjPtr<mirror::Object> object) {
277   DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
278   return GetObj(object);
279 }
280 
281 template<bool kTransactionActive>
SetObject(ObjPtr<mirror::Object> object,ObjPtr<mirror::Object> l)282 inline void ArtField::SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l) {
283   DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
284   SetObj<kTransactionActive>(object, l);
285 }
286 
GetName()287 inline const char* ArtField::GetName() REQUIRES_SHARED(Locks::mutator_lock_) {
288   uint32_t field_index = GetDexFieldIndex();
289   if (UNLIKELY(IsProxyField())) {
290     DCHECK(IsStatic());
291     DCHECK_LT(field_index, 2U);
292     return field_index == 0 ? "interfaces" : "throws";
293   }
294   const DexFile* dex_file = GetDexFile();
295   return dex_file->GetFieldName(dex_file->GetFieldId(field_index));
296 }
297 
GetTypeDescriptor()298 inline const char* ArtField::GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
299   uint32_t field_index = GetDexFieldIndex();
300   if (UNLIKELY(IsProxyField())) {
301     DCHECK(IsStatic());
302     DCHECK_LT(field_index, 2U);
303     // 0 == Class[] interfaces; 1 == Class[][] throws;
304     return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
305   }
306   const DexFile* dex_file = GetDexFile();
307   const dex::FieldId& field_id = dex_file->GetFieldId(field_index);
308   return dex_file->GetFieldTypeDescriptor(field_id);
309 }
310 
GetTypeAsPrimitiveType()311 inline Primitive::Type ArtField::GetTypeAsPrimitiveType()
312     REQUIRES_SHARED(Locks::mutator_lock_) {
313   return Primitive::GetType(GetTypeDescriptor()[0]);
314 }
315 
IsPrimitiveType()316 inline bool ArtField::IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_) {
317   return GetTypeAsPrimitiveType() != Primitive::kPrimNot;
318 }
319 
LookupResolvedType()320 inline ObjPtr<mirror::Class> ArtField::LookupResolvedType() {
321   ScopedAssertNoThreadSuspension ants(__FUNCTION__);
322   if (UNLIKELY(IsProxyField())) {
323     return ProxyFindSystemClass(GetTypeDescriptor());
324   }
325   ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
326       GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
327   DCHECK(!Thread::Current()->IsExceptionPending());
328   return type;
329 }
330 
ResolveType()331 inline ObjPtr<mirror::Class> ArtField::ResolveType() {
332   if (UNLIKELY(IsProxyField())) {
333     return ProxyFindSystemClass(GetTypeDescriptor());
334   }
335   ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(
336       GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
337   DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
338   return type;
339 }
340 
FieldSize()341 inline size_t ArtField::FieldSize() REQUIRES_SHARED(Locks::mutator_lock_) {
342   return Primitive::ComponentSize(GetTypeAsPrimitiveType());
343 }
344 
345 template <ReadBarrierOption kReadBarrierOption>
GetDexCache()346 inline ObjPtr<mirror::DexCache> ArtField::GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_) {
347   ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
348   return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
349 }
350 
GetDexFile()351 inline const DexFile* ArtField::GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_) {
352   return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
353 }
354 
ResolveNameString()355 inline ObjPtr<mirror::String> ArtField::ResolveNameString() {
356   uint32_t dex_field_index = GetDexFieldIndex();
357   CHECK_NE(dex_field_index, dex::kDexNoIndex);
358   const dex::FieldId& field_id = GetDexFile()->GetFieldId(dex_field_index);
359   return Runtime::Current()->GetClassLinker()->ResolveString(field_id.name_idx_, this);
360 }
361 
362 // If kExactOffset is true then we only find the matching offset, not the field containing the
363 // offset.
364 template <bool kExactOffset>
FindFieldWithOffset(const IterationRange<StrideIterator<ArtField>> & fields,uint32_t field_offset)365 static inline ArtField* FindFieldWithOffset(
366     const IterationRange<StrideIterator<ArtField>>& fields,
367     uint32_t field_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
368   for (ArtField& field : fields) {
369     if (kExactOffset) {
370       if (field.GetOffset().Uint32Value() == field_offset) {
371         return &field;
372       }
373     } else {
374       const uint32_t offset = field.GetOffset().Uint32Value();
375       Primitive::Type type = field.GetTypeAsPrimitiveType();
376       const size_t field_size = Primitive::ComponentSize(type);
377       DCHECK_GT(field_size, 0u);
378       if (offset <= field_offset && field_offset < offset + field_size) {
379         return &field;
380       }
381     }
382   }
383   return nullptr;
384 }
385 
386 template <bool kExactOffset>
FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)387 inline ArtField* ArtField::FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,
388                                                        uint32_t field_offset) {
389   DCHECK(klass != nullptr);
390   ArtField* field = FindFieldWithOffset<kExactOffset>(klass->GetIFields(), field_offset);
391   if (field != nullptr) {
392     return field;
393   }
394   // We did not find field in the class: look into superclass.
395   return (klass->GetSuperClass() != nullptr) ?
396       FindInstanceFieldWithOffset<kExactOffset>(klass->GetSuperClass(), field_offset) : nullptr;
397 }
398 
399 template <bool kExactOffset>
FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,uint32_t field_offset)400 inline ArtField* ArtField::FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,
401                                                      uint32_t field_offset) {
402   DCHECK(klass != nullptr);
403   return FindFieldWithOffset<kExactOffset>(klass->GetSFields(), field_offset);
404 }
405 
GetClassLoader()406 inline ObjPtr<mirror::ClassLoader> ArtField::GetClassLoader() {
407   return GetDeclaringClass()->GetClassLoader();
408 }
409 
410 }  // namespace art
411 
412 #endif  // ART_RUNTIME_ART_FIELD_INL_H_
413