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_H_
18 #define ART_RUNTIME_ART_FIELD_H_
19 
20 #include "dex/dex_file_types.h"
21 #include "dex/modifiers.h"
22 #include "dex/primitive.h"
23 #include "gc_root.h"
24 #include "obj_ptr.h"
25 #include "offsets.h"
26 #include "read_barrier_option.h"
27 
28 namespace art {
29 
30 class DexFile;
31 class ScopedObjectAccessAlreadyRunnable;
32 
33 namespace mirror {
34 class Class;
35 class ClassLoader;
36 class DexCache;
37 class Object;
38 class String;
39 }  // namespace mirror
40 
41 class ArtField final {
42  public:
43   template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
44   ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
45 
46   ObjPtr<mirror::ClassLoader> GetClassLoader() REQUIRES_SHARED(Locks::mutator_lock_);
47 
48   void SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)
49       REQUIRES_SHARED(Locks::mutator_lock_);
50 
GetDeclaringClassAddressWithoutBarrier()51   mirror::CompressedReference<mirror::Object>* GetDeclaringClassAddressWithoutBarrier() {
52     return declaring_class_.AddressWithoutBarrier();
53   }
54 
GetAccessFlags()55   uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) {
56     if (kIsDebugBuild) {
57       GetAccessFlagsDCheck();
58     }
59     return access_flags_;
60   }
61 
SetAccessFlags(uint32_t new_access_flags)62   void SetAccessFlags(uint32_t new_access_flags) REQUIRES_SHARED(Locks::mutator_lock_) {
63     // Not called within a transaction.
64     access_flags_ = new_access_flags;
65   }
66 
IsPublic()67   bool IsPublic() REQUIRES_SHARED(Locks::mutator_lock_) {
68     return (GetAccessFlags() & kAccPublic) != 0;
69   }
70 
IsStatic()71   bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) {
72     return (GetAccessFlags() & kAccStatic) != 0;
73   }
74 
IsFinal()75   bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) {
76     return (GetAccessFlags() & kAccFinal) != 0;
77   }
78 
GetDexFieldIndex()79   uint32_t GetDexFieldIndex() {
80     return field_dex_idx_;
81   }
82 
SetDexFieldIndex(uint32_t new_idx)83   void SetDexFieldIndex(uint32_t new_idx) {
84     // Not called within a transaction.
85     field_dex_idx_ = new_idx;
86   }
87 
88   // Offset to field within an Object.
GetOffset()89   MemberOffset GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) {
90     if (kIsDebugBuild) {
91       GetOffsetDCheck();
92     }
93     return MemberOffset(offset_);
94   }
95 
OffsetOffset()96   static MemberOffset OffsetOffset() {
97     return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
98   }
99 
100   MemberOffset GetOffsetDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_);
101 
102   void SetOffset(MemberOffset num_bytes) REQUIRES_SHARED(Locks::mutator_lock_);
103 
104   // field access, null object for static fields
105   uint8_t GetBoolean(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
106 
107   template<bool kTransactionActive>
108   void SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) REQUIRES_SHARED(Locks::mutator_lock_);
109 
110   int8_t GetByte(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
111 
112   template<bool kTransactionActive>
113   void SetByte(ObjPtr<mirror::Object> object, int8_t b) REQUIRES_SHARED(Locks::mutator_lock_);
114 
115   uint16_t GetChar(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
116 
117   template<bool kTransactionActive>
118   void SetChar(ObjPtr<mirror::Object> object, uint16_t c) REQUIRES_SHARED(Locks::mutator_lock_);
119 
120   int16_t GetShort(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
121 
122   template<bool kTransactionActive>
123   void SetShort(ObjPtr<mirror::Object> object, int16_t s) REQUIRES_SHARED(Locks::mutator_lock_);
124 
125   int32_t GetInt(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
126 
127   template<bool kTransactionActive>
128   void SetInt(ObjPtr<mirror::Object> object, int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
129 
130   int64_t GetLong(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
131 
132   template<bool kTransactionActive>
133   void SetLong(ObjPtr<mirror::Object> object, int64_t j) REQUIRES_SHARED(Locks::mutator_lock_);
134 
135   float GetFloat(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
136 
137   template<bool kTransactionActive>
138   void SetFloat(ObjPtr<mirror::Object> object, float f) REQUIRES_SHARED(Locks::mutator_lock_);
139 
140   double GetDouble(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
141 
142   template<bool kTransactionActive>
143   void SetDouble(ObjPtr<mirror::Object> object, double d) REQUIRES_SHARED(Locks::mutator_lock_);
144 
145   ObjPtr<mirror::Object> GetObject(ObjPtr<mirror::Object> object)
146       REQUIRES_SHARED(Locks::mutator_lock_);
147 
148   template<bool kTransactionActive>
149   void SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l)
150       REQUIRES_SHARED(Locks::mutator_lock_);
151 
152   // Raw field accesses.
153   uint32_t Get32(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
154 
155   template<bool kTransactionActive>
156   void Set32(ObjPtr<mirror::Object> object, uint32_t new_value)
157       REQUIRES_SHARED(Locks::mutator_lock_);
158 
159   uint64_t Get64(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
160 
161   template<bool kTransactionActive>
162   void Set64(ObjPtr<mirror::Object> object, uint64_t new_value)
163       REQUIRES_SHARED(Locks::mutator_lock_);
164 
165   template<class MirrorType = mirror::Object>
166   ObjPtr<MirrorType> GetObj(ObjPtr<mirror::Object> object)
167       REQUIRES_SHARED(Locks::mutator_lock_);
168 
169   template<bool kTransactionActive>
170   void SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value)
171       REQUIRES_SHARED(Locks::mutator_lock_);
172 
173   // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
174   template<typename RootVisitorType>
VisitRoots(RootVisitorType & visitor)175   ALWAYS_INLINE inline void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS {
176     visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
177   }
178 
IsVolatile()179   bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) {
180     return (GetAccessFlags() & kAccVolatile) != 0;
181   }
182 
183   // Returns an instance field with this offset in the given class or null if not found.
184   // If kExactOffset is true then we only find the matching offset, not the field containing the
185   // offset.
186   template <bool kExactOffset = true>
187   static ArtField* FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
188       REQUIRES_SHARED(Locks::mutator_lock_);
189 
190   // Returns a static field with this offset in the given class or null if not found.
191   // If kExactOffset is true then we only find the matching offset, not the field containing the
192   // offset.
193   template <bool kExactOffset = true>
194   static ArtField* FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
195       REQUIRES_SHARED(Locks::mutator_lock_);
196 
197   const char* GetName() REQUIRES_SHARED(Locks::mutator_lock_);
198 
199   // Resolves / returns the name from the dex cache.
200   ObjPtr<mirror::String> ResolveNameString() REQUIRES_SHARED(Locks::mutator_lock_);
201 
202   const char* GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
203 
204   Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
205 
206   bool IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
207 
208   ObjPtr<mirror::Class> LookupResolvedType() REQUIRES_SHARED(Locks::mutator_lock_);
209   ObjPtr<mirror::Class> ResolveType() REQUIRES_SHARED(Locks::mutator_lock_);
210 
211   size_t FieldSize() REQUIRES_SHARED(Locks::mutator_lock_);
212 
213   template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
214   ObjPtr<mirror::DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
215 
216   const DexFile* GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_);
217 
DeclaringClassRoot()218   GcRoot<mirror::Class>& DeclaringClassRoot() {
219     return declaring_class_;
220   }
221 
222   // Returns a human-readable signature. Something like "a.b.C.f" or
223   // "int a.b.C.f" (depending on the value of 'with_type').
224   static std::string PrettyField(ArtField* f, bool with_type = true)
225       REQUIRES_SHARED(Locks::mutator_lock_);
226   std::string PrettyField(bool with_type = true)
227       REQUIRES_SHARED(Locks::mutator_lock_);
228 
229   // Update the declaring class with the passed in visitor. Does not use read barrier.
230   template <typename Visitor>
231   ALWAYS_INLINE void UpdateObjects(const Visitor& visitor)
232       REQUIRES_SHARED(Locks::mutator_lock_);
233 
234  private:
235   bool IsProxyField() REQUIRES_SHARED(Locks::mutator_lock_);
236 
237   ObjPtr<mirror::Class> ProxyFindSystemClass(const char* descriptor)
238       REQUIRES_SHARED(Locks::mutator_lock_);
239 
240   void GetAccessFlagsDCheck() REQUIRES_SHARED(Locks::mutator_lock_);
241   void GetOffsetDCheck() REQUIRES_SHARED(Locks::mutator_lock_);
242 
243   GcRoot<mirror::Class> declaring_class_;
244 
245   uint32_t access_flags_ = 0;
246 
247   // Dex cache index of field id
248   uint32_t field_dex_idx_ = 0;
249 
250   // Offset of field within an instance or in the Class' static fields
251   uint32_t offset_ = 0;
252 };
253 
254 }  // namespace art
255 
256 #endif  // ART_RUNTIME_ART_FIELD_H_
257