blob: 4e77e7f0a5cee059b6935e2aa4b68f54cb15fbcd [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 Sehr8c0961f2018-01-23 16:11:38 -080020#include "dex/modifiers.h"
David Sehr67bf42e2018-02-26 16:43:04 -080021#include "dex/primitive.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070022#include "gc_root.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070023#include "obj_ptr.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "offsets.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070025#include "read_barrier_option.h"
26
27namespace art {
28
29class DexFile;
30class ScopedObjectAccessAlreadyRunnable;
31
32namespace mirror {
33class Class;
Andreas Gampe4835d212018-11-21 14:55:10 -080034class ClassLoader;
Mathieu Chartierc7853442015-03-27 14:35:38 -070035class DexCache;
36class Object;
37class String;
38} // namespace mirror
39
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010040class ArtField final {
Mathieu Chartierc7853442015-03-27 14:35:38 -070041 public:
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -070042 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier3398c782016-09-30 10:27:43 -070043 ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070044
Vladimir Markoc524e9e2019-03-26 10:54:50 +000045 ObjPtr<mirror::ClassLoader> GetClassLoader() REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe4835d212018-11-21 14:55:10 -080046
Mathieu Chartier3398c782016-09-30 10:27:43 -070047 void SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070048 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070049
Mathieu Chartier8c19d242017-03-06 12:35:10 -080050 mirror::CompressedReference<mirror::Object>* GetDeclaringClassAddressWithoutBarrier() {
51 return declaring_class_.AddressWithoutBarrier();
52 }
53
Vladimir Markof9625ec2022-02-11 17:19:45 +000054 uint32_t GetAccessFlags() {
Andreas Gampea1d2f952017-04-20 22:53:58 -070055 return access_flags_;
56 }
Mathieu Chartierc7853442015-03-27 14:35:38 -070057
Vladimir Markob6e0e022022-04-14 10:13:15 +010058 void SetAccessFlags(uint32_t new_access_flags) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070059 // Not called within a transaction.
60 access_flags_ = new_access_flags;
61 }
62
Vladimir Markob6e0e022022-04-14 10:13:15 +010063 bool IsPublic() {
Mathieu Chartierc7853442015-03-27 14:35:38 -070064 return (GetAccessFlags() & kAccPublic) != 0;
65 }
66
Vladimir Markob6e0e022022-04-14 10:13:15 +010067 bool IsStatic() {
Mathieu Chartierc7853442015-03-27 14:35:38 -070068 return (GetAccessFlags() & kAccStatic) != 0;
69 }
70
Vladimir Markob6e0e022022-04-14 10:13:15 +010071 bool IsFinal() {
Mathieu Chartierc7853442015-03-27 14:35:38 -070072 return (GetAccessFlags() & kAccFinal) != 0;
73 }
74
Vladimir Markob6e0e022022-04-14 10:13:15 +010075 bool IsPrivate() {
Alex Light24627892019-11-06 10:28:21 -080076 return (GetAccessFlags() & kAccPrivate) != 0;
77 }
78
Mathieu Chartierc7853442015-03-27 14:35:38 -070079 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.
Vladimir Markob6e0e022022-04-14 10:13:15 +010089 MemberOffset GetOffset() {
Andreas Gampea1d2f952017-04-20 22:53:58 -070090 return MemberOffset(offset_);
91 }
Mathieu Chartierc7853442015-03-27 14:35:38 -070092
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000093 static constexpr MemberOffset OffsetOffset() {
Mathieu Chartierc7853442015-03-27 14:35:38 -070094 return MemberOffset(OFFSETOF_MEMBER(ArtField, offset_));
95 }
96
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000097 static constexpr MemberOffset DeclaringClassOffset() {
98 return MemberOffset(OFFSETOF_MEMBER(ArtField, declaring_class_));
99 }
100
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 MemberOffset GetOffsetDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700102
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 void SetOffset(MemberOffset num_bytes) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700104
105 // field access, null object for static fields
Mathieu Chartier3398c782016-09-30 10:27:43 -0700106 uint8_t GetBoolean(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700107
108 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700109 void SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700110
Mathieu Chartier3398c782016-09-30 10:27:43 -0700111 int8_t GetByte(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700112
113 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700114 void SetByte(ObjPtr<mirror::Object> object, int8_t b) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700115
Mathieu Chartier3398c782016-09-30 10:27:43 -0700116 uint16_t GetChar(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700117
118 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700119 void SetChar(ObjPtr<mirror::Object> object, uint16_t c) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700120
Mathieu Chartier3398c782016-09-30 10:27:43 -0700121 int16_t GetShort(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700122
123 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700124 void SetShort(ObjPtr<mirror::Object> object, int16_t s) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700125
Mathieu Chartier3398c782016-09-30 10:27:43 -0700126 int32_t GetInt(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700127
128 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700129 void SetInt(ObjPtr<mirror::Object> object, int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700130
Mathieu Chartier3398c782016-09-30 10:27:43 -0700131 int64_t GetLong(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700132
133 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700134 void SetLong(ObjPtr<mirror::Object> object, int64_t j) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700135
Mathieu Chartier3398c782016-09-30 10:27:43 -0700136 float GetFloat(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700137
138 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700139 void SetFloat(ObjPtr<mirror::Object> object, float f) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700140
Mathieu Chartier3398c782016-09-30 10:27:43 -0700141 double GetDouble(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700142
143 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700144 void SetDouble(ObjPtr<mirror::Object> object, double d) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700145
Vladimir Marko5f958f62022-02-08 12:01:07 +0000146 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700147 ObjPtr<mirror::Object> GetObject(ObjPtr<mirror::Object> object)
148 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700149
150 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700151 void SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700152 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700153
154 // Raw field accesses.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700155 uint32_t Get32(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700156
157 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700158 void Set32(ObjPtr<mirror::Object> object, uint32_t new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700159 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700160
Mathieu Chartier3398c782016-09-30 10:27:43 -0700161 uint64_t Get64(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700162
163 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700164 void Set64(ObjPtr<mirror::Object> object, uint64_t new_value)
165 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700166
Vladimir Marko5f958f62022-02-08 12:01:07 +0000167 template<class MirrorType = mirror::Object,
168 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700169 ObjPtr<MirrorType> GetObj(ObjPtr<mirror::Object> object)
170 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700171
172 template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700173 void SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700175
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700176 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700177 template<typename RootVisitorType>
Andreas Gamped4901292017-05-30 18:41:34 -0700178 ALWAYS_INLINE inline void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS {
179 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
180 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700181
Vladimir Markob6e0e022022-04-14 10:13:15 +0100182 bool IsVolatile() {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700183 return (GetAccessFlags() & kAccVolatile) != 0;
184 }
185
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700186 // Returns an instance field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700187 // If kExactOffset is true then we only find the matching offset, not the field containing the
188 // offset.
189 template <bool kExactOffset = true>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700190 static ArtField* FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700192
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700193 // Returns a static field with this offset in the given class or null if not found.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700194 // If kExactOffset is true then we only find the matching offset, not the field containing the
195 // offset.
196 template <bool kExactOffset = true>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700197 static ArtField* FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass, uint32_t field_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700199
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700200 const char* GetName() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700201
202 // Resolves / returns the name from the dex cache.
Vladimir Marko18090d12018-06-01 16:53:12 +0100203 ObjPtr<mirror::String> ResolveNameString() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700204
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 const char* GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700206
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700207 Primitive::Type GetTypeAsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700210
Vladimir Marko208f6702017-12-08 12:00:50 +0000211 ObjPtr<mirror::Class> LookupResolvedType() REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000212 ObjPtr<mirror::Class> ResolveType() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 size_t FieldSize() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700215
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100216 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700217 ObjPtr<mirror::DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 const DexFile* GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700220
221 GcRoot<mirror::Class>& DeclaringClassRoot() {
222 return declaring_class_;
223 }
224
David Sehr709b0702016-10-13 09:12:37 -0700225 // Returns a human-readable signature. Something like "a.b.C.f" or
226 // "int a.b.C.f" (depending on the value of 'with_type').
227 static std::string PrettyField(ArtField* f, bool with_type = true)
228 REQUIRES_SHARED(Locks::mutator_lock_);
229 std::string PrettyField(bool with_type = true)
230 REQUIRES_SHARED(Locks::mutator_lock_);
231
Alex Lightc9af14e2019-12-12 09:51:38 -0800232 // Returns true if a set-* instruction in the given method is allowable.
233 ALWAYS_INLINE inline bool CanBeChangedBy(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
234
Mathieu Chartierc7853442015-03-27 14:35:38 -0700235 private:
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100236 bool IsProxyField() REQUIRES_SHARED(Locks::mutator_lock_);
237
Mathieu Chartier3398c782016-09-30 10:27:43 -0700238 ObjPtr<mirror::Class> ProxyFindSystemClass(const char* descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100240
Mathieu Chartierc7853442015-03-27 14:35:38 -0700241 GcRoot<mirror::Class> declaring_class_;
242
Mathieu Chartier3398c782016-09-30 10:27:43 -0700243 uint32_t access_flags_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700244
245 // Dex cache index of field id
Mathieu Chartier3398c782016-09-30 10:27:43 -0700246 uint32_t field_dex_idx_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247
248 // Offset of field within an instance or in the Class' static fields
Mathieu Chartier3398c782016-09-30 10:27:43 -0700249 uint32_t offset_ = 0;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700250};
251
252} // namespace art
253
254#endif // ART_RUNTIME_ART_FIELD_H_