blob: 43adae51b54b5ed226ebb70a58272c277b814758 [file] [log] [blame]
Mathieu Chartierc7853442015-03-27 14:35:38 -07001/*
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
David Sehr9e734c72018-01-04 17:56:19 -080020#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080021#include "dex/modifiers.h"
David Sehr67bf42e2018-02-26 16:43:04 -080022#include "dex/primitive.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "gc_root.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070024#include "obj_ptr.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070025#include "offsets.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "read_barrier_option.h"
27
28namespace art {
29
30class DexFile;
31class ScopedObjectAccessAlreadyRunnable;
32
33namespace mirror {
34class Class;
Andreas Gampe4835d212018-11-21 14:55:10 -080035class ClassLoader;
Mathieu Chartierc7853442015-03-27 14:35:38 -070036class DexCache;
37class Object;
38class String;
39} // namespace mirror
40
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010041class ArtField final {
Mathieu Chartierc7853442015-03-27 14:35:38 -070042 public:
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -070043 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier3398c782016-09-30 10:27:43 -070044 ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070045
Andreas Gampe4835d212018-11-21 14:55:10 -080046 mirror::ClassLoader* GetClassLoader() REQUIRES_SHARED(Locks::mutator_lock_);
47
Mathieu Chartier3398c782016-09-30 10:27:43 -070048 void SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070050
Mathieu Chartier8c19d242017-03-06 12:35:10 -080051 mirror::CompressedReference<mirror::Object>* GetDeclaringClassAddressWithoutBarrier() {
52 return declaring_class_.AddressWithoutBarrier();
53 }
54
Andreas Gampea1d2f952017-04-20 22:53:58 -070055 uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) {
56 if (kIsDebugBuild) {
57 GetAccessFlagsDCheck();
58 }
59 return access_flags_;
60 }
Mathieu Chartierc7853442015-03-27 14:35:38 -070061
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 void SetAccessFlags(uint32_t new_access_flags) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070063 // Not called within a transaction.
64 access_flags_ = new_access_flags;
65 }
66
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 bool IsPublic() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070068 return (GetAccessFlags() & kAccPublic) != 0;
69 }
70
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 bool IsStatic() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070072 return (GetAccessFlags() & kAccStatic) != 0;
73 }
74
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070075 bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070076 return (GetAccessFlags() & kAccFinal) != 0;
77 }
78
79 uint32_t GetDexFieldIndex() {
80 return field_dex_idx_;
81 }
82
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.
Andreas Gampea1d2f952017-04-20 22:53:58 -070089 MemberOffset GetOffset() REQUIRES_SHARED(Locks::mutator_lock_) {
90 if (kIsDebugBuild) {
91 GetOffsetDCheck();
92 }
93 return MemberOffset(offset_);
94 }
Mathieu Chartierc7853442015-03-27 14:35:38 -070095
96 static MemberOffset OffsetOffset() {
97 return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
98 }
99
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700100 MemberOffset GetOffsetDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700101
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 void SetOffset(MemberOffset num_bytes) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700103
104 // field access, null object for static fields
Mathieu Chartier3398c782016-09-30 10:27:43 -0700105 uint8_t GetBoolean(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700106
107 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700108 void SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700109
Mathieu Chartier3398c782016-09-30 10:27:43 -0700110 int8_t GetByte(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700111
112 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700113 void SetByte(ObjPtr<mirror::Object> object, int8_t b) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700114
Mathieu Chartier3398c782016-09-30 10:27:43 -0700115 uint16_t GetChar(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700116
117 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700118 void SetChar(ObjPtr<mirror::Object> object, uint16_t c) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700119
Mathieu Chartier3398c782016-09-30 10:27:43 -0700120 int16_t GetShort(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700121
122 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700123 void SetShort(ObjPtr<mirror::Object> object, int16_t s) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700124
Mathieu Chartier3398c782016-09-30 10:27:43 -0700125 int32_t GetInt(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700126
127 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700128 void SetInt(ObjPtr<mirror::Object> object, int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700129
Mathieu Chartier3398c782016-09-30 10:27:43 -0700130 int64_t GetLong(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700131
132 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700133 void SetLong(ObjPtr<mirror::Object> object, int64_t j) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700134
Mathieu Chartier3398c782016-09-30 10:27:43 -0700135 float GetFloat(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700136
137 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700138 void SetFloat(ObjPtr<mirror::Object> object, float f) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700139
Mathieu Chartier3398c782016-09-30 10:27:43 -0700140 double GetDouble(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700141
142 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700143 void SetDouble(ObjPtr<mirror::Object> object, double d) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700144
Mathieu Chartier3398c782016-09-30 10:27:43 -0700145 ObjPtr<mirror::Object> GetObject(ObjPtr<mirror::Object> object)
146 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700147
148 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700149 void SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700151
152 // Raw field accesses.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700153 uint32_t Get32(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700154
155 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700156 void Set32(ObjPtr<mirror::Object> object, uint32_t new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700157 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700158
Mathieu Chartier3398c782016-09-30 10:27:43 -0700159 uint64_t Get64(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700160
161 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700162 void Set64(ObjPtr<mirror::Object> object, uint64_t new_value)
163 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700164
Mathieu Chartier3398c782016-09-30 10:27:43 -0700165 template<class MirrorType = mirror::Object>
166 ObjPtr<MirrorType> GetObj(ObjPtr<mirror::Object> object)
167 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700168
169 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700170 void SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700172
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700173 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 template<typename RootVisitorType>
Andreas Gamped4901292017-05-30 18:41:34 -0700175 ALWAYS_INLINE inline void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS {
176 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
177 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700178
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 bool IsVolatile() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700180 return (GetAccessFlags() & kAccVolatile) != 0;
181 }
182
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700183 // Returns an instance field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700184 // If kExactOffset is true then we only find the matching offset, not the field containing the
185 // offset.
186 template <bool kExactOffset = true>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700187 static ArtField* FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700188 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700189
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700190 // Returns a static field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700191 // If kExactOffset is true then we only find the matching offset, not the field containing the
192 // offset.
193 template <bool kExactOffset = true>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700194 static ArtField* FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700195 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700196
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 const char* GetName() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700198
199 // Resolves / returns the name from the dex cache.
Vladimir Marko18090d12018-06-01 16:53:12 +0100200 ObjPtr<mirror::String> ResolveNameString() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700201
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 const char* GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700203
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700207
Vladimir Marko208f6702017-12-08 12:00:50 +0000208 ObjPtr<mirror::Class> LookupResolvedType() REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000209 ObjPtr<mirror::Class> ResolveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 size_t FieldSize() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700212
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100213 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700214 ObjPtr<mirror::DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 const DexFile* GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700217
218 GcRoot<mirror::Class>& DeclaringClassRoot() {
219 return declaring_class_;
220 }
221
David Sehr709b0702016-10-13 09:12:37 -0700222 // 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
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800229 // 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)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800233
Mathieu Chartierc7853442015-03-27 14:35:38 -0700234 private:
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100235 bool IsProxyField() REQUIRES_SHARED(Locks::mutator_lock_);
236
Mathieu Chartier3398c782016-09-30 10:27:43 -0700237 ObjPtr<mirror::Class> ProxyFindSystemClass(const char* descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100239
Andreas Gampea1d2f952017-04-20 22:53:58 -0700240 void GetAccessFlagsDCheck() REQUIRES_SHARED(Locks::mutator_lock_);
241 void GetOffsetDCheck() REQUIRES_SHARED(Locks::mutator_lock_);
242
Mathieu Chartierc7853442015-03-27 14:35:38 -0700243 GcRoot<mirror::Class> declaring_class_;
244
Mathieu Chartier3398c782016-09-30 10:27:43 -0700245 uint32_t access_flags_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700246
247 // Dex cache index of field id
Mathieu Chartier3398c782016-09-30 10:27:43 -0700248 uint32_t field_dex_idx_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700249
250 // Offset of field within an instance or in the Class' static fields
Mathieu Chartier3398c782016-09-30 10:27:43 -0700251 uint32_t offset_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700252};
253
254} // namespace art
255
256#endif // ART_RUNTIME_ART_FIELD_H_