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_MIRROR_ART_FIELD_H_
18 #define ART_RUNTIME_MIRROR_ART_FIELD_H_
19 
20 #include <jni.h>
21 
22 #include "gc_root.h"
23 #include "modifiers.h"
24 #include "object.h"
25 #include "object_callbacks.h"
26 #include "primitive.h"
27 #include "read_barrier_option.h"
28 
29 namespace art {
30 
31 struct ArtFieldOffsets;
32 class DexFile;
33 class ScopedObjectAccessAlreadyRunnable;
34 
35 namespace mirror {
36 
37 class DexCache;
38 
39 // C++ mirror of java.lang.reflect.ArtField
40 class MANAGED ArtField FINAL : public Object {
41  public:
42   // Size of java.lang.reflect.ArtField.class.
43   static uint32_t ClassSize();
44 
45   // Size of an instance of java.lang.reflect.ArtField not including its value array.
InstanceSize()46   static constexpr uint32_t InstanceSize() {
47     return sizeof(ArtField);
48   }
49 
50   ALWAYS_INLINE static ArtField* FromReflectedField(const ScopedObjectAccessAlreadyRunnable& soa,
51                                                     jobject jlr_field)
52       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
53 
54   Class* GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
55 
56   void SetDeclaringClass(Class *new_declaring_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
57 
58   uint32_t GetAccessFlags() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
59 
SetAccessFlags(uint32_t new_access_flags)60   void SetAccessFlags(uint32_t new_access_flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
61     // Not called within a transaction.
62     SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ArtField, access_flags_), new_access_flags);
63   }
64 
IsPublic()65   bool IsPublic() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
66     return (GetAccessFlags() & kAccPublic) != 0;
67   }
68 
IsStatic()69   bool IsStatic() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
70     return (GetAccessFlags() & kAccStatic) != 0;
71   }
72 
IsFinal()73   bool IsFinal() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74     return (GetAccessFlags() & kAccFinal) != 0;
75   }
76 
GetDexFieldIndex()77   uint32_t GetDexFieldIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
78     return GetField32(OFFSET_OF_OBJECT_MEMBER(ArtField, field_dex_idx_));
79   }
80 
SetDexFieldIndex(uint32_t new_idx)81   void SetDexFieldIndex(uint32_t new_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
82     // Not called within a transaction.
83     SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ArtField, field_dex_idx_), new_idx);
84   }
85 
86   // Offset to field within an Object.
87   MemberOffset GetOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88 
OffsetOffset()89   static MemberOffset OffsetOffset() {
90     return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
91   }
92 
93   MemberOffset GetOffsetDuringLinking() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94 
95   void SetOffset(MemberOffset num_bytes) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96 
97   // field access, null object for static fields
98   bool GetBoolean(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
99   template<bool kTransactionActive>
100   void SetBoolean(Object* object, bool z) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101   int8_t GetByte(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102   template<bool kTransactionActive>
103   void SetByte(Object* object, int8_t b) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
104   uint16_t GetChar(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
105   template<bool kTransactionActive>
106   void SetChar(Object* object, uint16_t c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107   int16_t GetShort(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108   template<bool kTransactionActive>
109   void SetShort(Object* object, int16_t s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110   int32_t GetInt(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111   template<bool kTransactionActive>
112   void SetInt(Object* object, int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
113   int64_t GetLong(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114   template<bool kTransactionActive>
115   void SetLong(Object* object, int64_t j) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116   float GetFloat(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
117   template<bool kTransactionActive>
118   void SetFloat(Object* object, float f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
119   double GetDouble(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120   template<bool kTransactionActive>
121   void SetDouble(Object* object, double d) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122   Object* GetObject(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123   template<bool kTransactionActive>
124   void SetObject(Object* object, Object* l) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
125 
126   // Raw field accesses.
127   uint32_t Get32(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128   template<bool kTransactionActive>
129   void Set32(Object* object, uint32_t new_value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
130   uint64_t Get64(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131   template<bool kTransactionActive>
132   void Set64(Object* object, uint64_t new_value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
133   Object* GetObj(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
134   template<bool kTransactionActive>
135   void SetObj(Object* object, Object* new_value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
136 
137   template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
GetJavaLangReflectArtField()138   static Class* GetJavaLangReflectArtField()  SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
139     DCHECK(!java_lang_reflect_ArtField_.IsNull());
140     return java_lang_reflect_ArtField_.Read<kReadBarrierOption>();
141   }
142 
143   static void SetClass(Class* java_lang_reflect_ArtField);
144   static void ResetClass();
145   static void VisitRoots(RootCallback* callback, void* arg)
146       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147 
IsVolatile()148   bool IsVolatile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
149     return (GetAccessFlags() & kAccVolatile) != 0;
150   }
151 
152   // Returns an instance field with this offset in the given class or nullptr if not found.
153   static ArtField* FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
154       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155 
156   const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157 
158   const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
159 
160   Primitive::Type GetTypeAsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
161 
162   bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163 
164   size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165 
166   mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
167 
168   const DexFile* GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169 
170  private:
171   // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
172   // The class we are a part of
173   HeapReference<Class> declaring_class_;
174 
175   uint32_t access_flags_;
176 
177   // Dex cache index of field id
178   uint32_t field_dex_idx_;
179 
180   // Offset of field within an instance or in the Class' static fields
181   uint32_t offset_;
182 
183   static GcRoot<Class> java_lang_reflect_ArtField_;
184 
185   friend struct art::ArtFieldOffsets;  // for verifying offset information
186   DISALLOW_IMPLICIT_CONSTRUCTORS(ArtField);
187 };
188 
189 }  // namespace mirror
190 }  // namespace art
191 
192 #endif  // ART_RUNTIME_MIRROR_ART_FIELD_H_
193