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