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