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