blob: b46d80cf1567214e9a3b207f968bf6980c7404d2 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_DEX_CACHE_H_
18#define ART_RUNTIME_MIRROR_DEX_CACHE_H_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070019
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "array.h"
Vladimir Marko8d6768d2017-03-14 10:13:21 +000021#include "base/bit_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080022#include "base/mutex.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file_types.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070024#include "object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "object_array.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026
27namespace art {
28
Vladimir Marko8d6768d2017-03-14 10:13:21 +000029class ArtField;
Alex Lightdba61482016-12-21 08:20:29 -080030class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031struct DexCacheOffsets;
32class DexFile;
33class ImageWriter;
34union JValue;
Andreas Gampecc1b5352016-12-01 16:58:38 -080035class LinearAlloc;
36class Thread;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037
38namespace mirror {
39
Orion Hodsonc069a302017-01-18 09:23:12 +000040class CallSite;
Vladimir Marko8d6768d2017-03-14 10:13:21 +000041class Class;
Narayan Kamath25352fc2016-08-03 12:46:58 +010042class MethodType;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070043class String;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070044
Narayan Kamathc38a6f82016-09-29 17:07:20 +010045template <typename T> struct PACKED(8) DexCachePair {
46 GcRoot<T> object;
47 uint32_t index;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070048 // The array is initially [ {0,0}, {0,0}, {0,0} ... ]
49 // We maintain the invariant that once a dex cache entry is populated,
50 // the pointer is always non-0
51 // Any given entry would thus be:
52 // {non-0, non-0} OR {0,0}
53 //
54 // It's generally sufficiently enough then to check if the
Narayan Kamathc38a6f82016-09-29 17:07:20 +010055 // lookup index matches the stored index (for a >0 lookup index)
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070056 // because if it's true the pointer is also non-null.
57 //
58 // For the 0th entry which is a special case, the value is either
59 // {0,0} (initial state) or {non-0, 0} which indicates
Narayan Kamathc38a6f82016-09-29 17:07:20 +010060 // that a valid object is stored at that index for a dex section id of 0.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070061 //
Narayan Kamathc38a6f82016-09-29 17:07:20 +010062 // As an optimization, we want to avoid branching on the object pointer since
63 // it's always non-null if the id branch succeeds (except for the 0th id).
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070064 // Set the initial state for the 0th entry to be {0,1} which is guaranteed to fail
Narayan Kamathc38a6f82016-09-29 17:07:20 +010065 // the lookup id == stored id branch.
Vladimir Marko8d6768d2017-03-14 10:13:21 +000066 DexCachePair(ObjPtr<T> object, uint32_t index)
Narayan Kamathc38a6f82016-09-29 17:07:20 +010067 : object(object),
68 index(index) {}
Andreas Gamped9911ee2017-03-27 13:27:24 -070069 DexCachePair() : index(0) {}
Narayan Kamathc38a6f82016-09-29 17:07:20 +010070 DexCachePair(const DexCachePair<T>&) = default;
71 DexCachePair& operator=(const DexCachePair<T>&) = default;
Mathieu Chartierbb816d62016-09-07 10:17:46 -070072
Narayan Kamathc38a6f82016-09-29 17:07:20 +010073 static void Initialize(std::atomic<DexCachePair<T>>* dex_cache) {
74 DexCachePair<T> first_elem;
75 first_elem.object = GcRoot<T>(nullptr);
76 first_elem.index = InvalidIndexForSlot(0);
77 dex_cache[0].store(first_elem, std::memory_order_relaxed);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070078 }
Mathieu Chartierbb816d62016-09-07 10:17:46 -070079
Narayan Kamathc38a6f82016-09-29 17:07:20 +010080 static uint32_t InvalidIndexForSlot(uint32_t slot) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070081 // Since the cache size is a power of two, 0 will always map to slot 0.
82 // Use 1 for slot 0 and 0 for all other slots.
83 return (slot == 0) ? 1u : 0u;
84 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +000085
86 T* GetObjectForIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
87 if (idx != index) {
88 return nullptr;
89 }
90 DCHECK(!object.IsNull());
91 return object.Read();
92 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070093};
Narayan Kamathc38a6f82016-09-29 17:07:20 +010094
Vladimir Markof44d36c2017-03-14 14:18:46 +000095template <typename T> struct PACKED(2 * __SIZEOF_POINTER__) NativeDexCachePair {
96 T* object;
97 size_t index;
98 // This is similar to DexCachePair except that we're storing a native pointer
99 // instead of a GC root. See DexCachePair for the details.
100 NativeDexCachePair(T* object, uint32_t index)
101 : object(object),
102 index(index) {}
103 NativeDexCachePair() : object(nullptr), index(0u) { }
104 NativeDexCachePair(const NativeDexCachePair<T>&) = default;
105 NativeDexCachePair& operator=(const NativeDexCachePair<T>&) = default;
106
107 static void Initialize(std::atomic<NativeDexCachePair<T>>* dex_cache, PointerSize pointer_size);
108
109 static uint32_t InvalidIndexForSlot(uint32_t slot) {
110 // Since the cache size is a power of two, 0 will always map to slot 0.
111 // Use 1 for slot 0 and 0 for all other slots.
112 return (slot == 0) ? 1u : 0u;
113 }
114
115 T* GetObjectForIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
116 if (idx != index) {
117 return nullptr;
118 }
119 DCHECK(object != nullptr);
120 return object;
121 }
122};
123
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000124using TypeDexCachePair = DexCachePair<Class>;
125using TypeDexCacheType = std::atomic<TypeDexCachePair>;
126
127using StringDexCachePair = DexCachePair<String>;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700128using StringDexCacheType = std::atomic<StringDexCachePair>;
129
Vladimir Markof44d36c2017-03-14 14:18:46 +0000130using FieldDexCachePair = NativeDexCachePair<ArtField>;
131using FieldDexCacheType = std::atomic<FieldDexCachePair>;
132
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100133using MethodDexCachePair = NativeDexCachePair<ArtMethod>;
134using MethodDexCacheType = std::atomic<MethodDexCachePair>;
135
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000136using MethodTypeDexCachePair = DexCachePair<MethodType>;
Narayan Kamath25352fc2016-08-03 12:46:58 +0100137using MethodTypeDexCacheType = std::atomic<MethodTypeDexCachePair>;
138
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139// C++ mirror of java.lang.DexCache.
140class MANAGED DexCache FINAL : public Object {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700141 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700142 // Size of java.lang.DexCache.class.
Andreas Gampe542451c2016-07-26 09:02:02 -0700143 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700144
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000145 // Size of type dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
146 static constexpr size_t kDexCacheTypeCacheSize = 1024;
147 static_assert(IsPowerOfTwo(kDexCacheTypeCacheSize),
148 "Type dex cache size is not a power of 2.");
149
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700150 // Size of string dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
151 static constexpr size_t kDexCacheStringCacheSize = 1024;
152 static_assert(IsPowerOfTwo(kDexCacheStringCacheSize),
153 "String dex cache size is not a power of 2.");
154
Vladimir Markof44d36c2017-03-14 14:18:46 +0000155 // Size of field dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
156 static constexpr size_t kDexCacheFieldCacheSize = 1024;
157 static_assert(IsPowerOfTwo(kDexCacheFieldCacheSize),
158 "Field dex cache size is not a power of 2.");
159
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100160 // Size of method dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
161 static constexpr size_t kDexCacheMethodCacheSize = 1024;
162 static_assert(IsPowerOfTwo(kDexCacheMethodCacheSize),
163 "Method dex cache size is not a power of 2.");
164
Narayan Kamath25352fc2016-08-03 12:46:58 +0100165 // Size of method type dex cache. Needs to be a power of 2 for entrypoint assumptions
166 // to hold.
167 static constexpr size_t kDexCacheMethodTypeCacheSize = 1024;
168 static_assert(IsPowerOfTwo(kDexCacheMethodTypeCacheSize),
169 "MethodType dex cache size is not a power of 2.");
170
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000171 static constexpr size_t StaticTypeSize() {
172 return kDexCacheTypeCacheSize;
173 }
174
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700175 static constexpr size_t StaticStringSize() {
176 return kDexCacheStringCacheSize;
177 }
178
Vladimir Markof44d36c2017-03-14 14:18:46 +0000179 static constexpr size_t StaticArtFieldSize() {
180 return kDexCacheFieldCacheSize;
181 }
182
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100183 static constexpr size_t StaticMethodSize() {
184 return kDexCacheMethodCacheSize;
185 }
186
Narayan Kamath25352fc2016-08-03 12:46:58 +0100187 static constexpr size_t StaticMethodTypeSize() {
188 return kDexCacheMethodTypeCacheSize;
189 }
190
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700191 // Size of an instance of java.lang.DexCache not including referenced values.
192 static constexpr uint32_t InstanceSize() {
193 return sizeof(DexCache);
194 }
195
Andreas Gampecc1b5352016-12-01 16:58:38 -0800196 static void InitializeDexCache(Thread* self,
197 ObjPtr<mirror::DexCache> dex_cache,
198 ObjPtr<mirror::String> location,
199 const DexFile* dex_file,
200 LinearAlloc* linear_alloc,
201 PointerSize image_pointer_size)
202 REQUIRES_SHARED(Locks::mutator_lock_)
203 REQUIRES(Locks::dex_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700204
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800205 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700206 void FixupStrings(StringDexCacheType* dest, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700207 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800208
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800209 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000210 void FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800212
Narayan Kamath7fe56582016-10-14 18:49:12 +0100213 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
214 void FixupResolvedMethodTypes(MethodTypeDexCacheType* dest, const Visitor& visitor)
215 REQUIRES_SHARED(Locks::mutator_lock_);
216
Orion Hodsonc069a302017-01-18 09:23:12 +0000217 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
218 void FixupResolvedCallSites(GcRoot<mirror::CallSite>* dest, const Visitor& visitor)
219 REQUIRES_SHARED(Locks::mutator_lock_);
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 String* GetLocation() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700222 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700223 }
224
buzbee5cd21802011-08-26 10:40:14 -0700225 static MemberOffset StringsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700226 return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_);
buzbeec5ef0462011-08-25 18:44:49 -0700227 }
228
Vladimir Marko05792b92015-08-03 11:56:49 +0100229 static MemberOffset ResolvedTypesOffset() {
230 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_);
231 }
232
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700233 static MemberOffset ResolvedFieldsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700234 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_);
buzbeec5ef0462011-08-25 18:44:49 -0700235 }
236
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700237 static MemberOffset ResolvedMethodsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700238 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_);
buzbeec5ef0462011-08-25 18:44:49 -0700239 }
240
Narayan Kamath25352fc2016-08-03 12:46:58 +0100241 static MemberOffset ResolvedMethodTypesOffset() {
242 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_method_types_);
243 }
244
Orion Hodsonc069a302017-01-18 09:23:12 +0000245 static MemberOffset ResolvedCallSitesOffset() {
246 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_call_sites_);
247 }
248
Vladimir Marko05792b92015-08-03 11:56:49 +0100249 static MemberOffset NumStringsOffset() {
250 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_strings_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700251 }
252
Vladimir Marko05792b92015-08-03 11:56:49 +0100253 static MemberOffset NumResolvedTypesOffset() {
254 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_types_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700255 }
256
Vladimir Marko05792b92015-08-03 11:56:49 +0100257 static MemberOffset NumResolvedFieldsOffset() {
258 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_fields_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700259 }
260
Vladimir Marko05792b92015-08-03 11:56:49 +0100261 static MemberOffset NumResolvedMethodsOffset() {
262 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_methods_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700263 }
264
Narayan Kamath25352fc2016-08-03 12:46:58 +0100265 static MemberOffset NumResolvedMethodTypesOffset() {
266 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_method_types_);
267 }
268
Orion Hodsonc069a302017-01-18 09:23:12 +0000269 static MemberOffset NumResolvedCallSitesOffset() {
270 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_call_sites_);
271 }
272
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000273 String* GetResolvedString(dex::StringIndex string_idx) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700275
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800276 void SetResolvedString(dex::StringIndex string_idx, ObjPtr<mirror::String> resolved) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700277 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700278
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700279 // Clear a string for a string_idx, used to undo string intern transactions to make sure
280 // the string isn't kept live.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800281 void ClearString(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700282
Andreas Gampea5b09a62016-11-17 15:21:22 -0800283 Class* GetResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100284
Andreas Gampea5b09a62016-11-17 15:21:22 -0800285 void SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved)
Mathieu Chartier31e88222016-10-14 18:43:19 -0700286 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100287
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000288 void ClearResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
289
Andreas Gampe542451c2016-07-26 09:02:02 -0700290 ALWAYS_INLINE ArtMethod* GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700291 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700292
Andreas Gampe542451c2016-07-26 09:02:02 -0700293 ALWAYS_INLINE void SetResolvedMethod(uint32_t method_idx,
294 ArtMethod* resolved,
295 PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700296 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100297 ALWAYS_INLINE void ClearResolvedMethod(uint32_t method_idx, PointerSize ptr_size)
298 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700299
Mathieu Chartierc7853442015-03-27 14:35:38 -0700300 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700301 ALWAYS_INLINE ArtField* GetResolvedField(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700302 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303
304 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700305 ALWAYS_INLINE void SetResolvedField(uint32_t idx, ArtField* field, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markof44d36c2017-03-14 14:18:46 +0000307 ALWAYS_INLINE void ClearResolvedField(uint32_t idx, PointerSize ptr_size)
308 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700309
Narayan Kamath25352fc2016-08-03 12:46:58 +0100310 MethodType* GetResolvedMethodType(uint32_t proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);
311
Orion Hodsonc069a302017-01-18 09:23:12 +0000312 void SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved)
313 REQUIRES_SHARED(Locks::mutator_lock_);
314
315 CallSite* GetResolvedCallSite(uint32_t call_site_idx) REQUIRES_SHARED(Locks::mutator_lock_);
316
317 // Attempts to bind |call_site_idx| to the call site |resolved|. The
318 // caller must use the return value in place of |resolved|. This is
319 // because multiple threads can invoke the bootstrap method each
320 // producing a call site, but the method handle invocation on the
321 // call site must be on a common agreed value.
322 CallSite* SetResolvedCallSite(uint32_t call_site_idx, CallSite* resolved) WARN_UNUSED
323 REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100324
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 StringDexCacheType* GetStrings() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700326 return GetFieldPtr64<StringDexCacheType*>(StringsOffset());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700327 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 void SetStrings(StringDexCacheType* strings) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800330 SetFieldPtr<false>(StringsOffset(), strings);
331 }
332
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000333 TypeDexCacheType* GetResolvedTypes() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
334 return GetFieldPtr<TypeDexCacheType*>(ResolvedTypesOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700335 }
336
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000337 void SetResolvedTypes(TypeDexCacheType* resolved_types)
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800338 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700339 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800340 SetFieldPtr<false>(ResolvedTypesOffset(), resolved_types);
341 }
342
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100343 MethodDexCacheType* GetResolvedMethods() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
344 return GetFieldPtr<MethodDexCacheType*>(ResolvedMethodsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700345 }
346
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100347 void SetResolvedMethods(MethodDexCacheType* resolved_methods)
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800348 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700349 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800350 SetFieldPtr<false>(ResolvedMethodsOffset(), resolved_methods);
351 }
352
Vladimir Markof44d36c2017-03-14 14:18:46 +0000353 FieldDexCacheType* GetResolvedFields() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
354 return GetFieldPtr<FieldDexCacheType*>(ResolvedFieldsOffset());
Vladimir Marko05792b92015-08-03 11:56:49 +0100355 }
356
Vladimir Markof44d36c2017-03-14 14:18:46 +0000357 void SetResolvedFields(FieldDexCacheType* resolved_fields)
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800358 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700359 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800360 SetFieldPtr<false>(ResolvedFieldsOffset(), resolved_fields);
361 }
362
Narayan Kamath25352fc2016-08-03 12:46:58 +0100363 MethodTypeDexCacheType* GetResolvedMethodTypes()
364 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath7fe56582016-10-14 18:49:12 +0100365 return GetFieldPtr64<MethodTypeDexCacheType*>(ResolvedMethodTypesOffset());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100366 }
367
368 void SetResolvedMethodTypes(MethodTypeDexCacheType* resolved_method_types)
369 ALWAYS_INLINE
370 REQUIRES_SHARED(Locks::mutator_lock_) {
371 SetFieldPtr<false>(ResolvedMethodTypesOffset(), resolved_method_types);
372 }
373
Orion Hodsonc069a302017-01-18 09:23:12 +0000374 GcRoot<CallSite>* GetResolvedCallSites()
375 ALWAYS_INLINE
376 REQUIRES_SHARED(Locks::mutator_lock_) {
377 return GetFieldPtr<GcRoot<CallSite>*>(ResolvedCallSitesOffset());
378 }
379
380 void SetResolvedCallSites(GcRoot<CallSite>* resolved_call_sites)
381 ALWAYS_INLINE
382 REQUIRES_SHARED(Locks::mutator_lock_) {
383 SetFieldPtr<false>(ResolvedCallSitesOffset(), resolved_call_sites);
384 }
385
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700386 size_t NumStrings() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100387 return GetField32(NumStringsOffset());
388 }
389
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700390 size_t NumResolvedTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100391 return GetField32(NumResolvedTypesOffset());
392 }
393
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700394 size_t NumResolvedMethods() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100395 return GetField32(NumResolvedMethodsOffset());
396 }
397
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700398 size_t NumResolvedFields() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100399 return GetField32(NumResolvedFieldsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700400 }
401
Narayan Kamath25352fc2016-08-03 12:46:58 +0100402 size_t NumResolvedMethodTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
403 return GetField32(NumResolvedMethodTypesOffset());
404 }
405
Orion Hodsonc069a302017-01-18 09:23:12 +0000406 size_t NumResolvedCallSites() REQUIRES_SHARED(Locks::mutator_lock_) {
407 return GetField32(NumResolvedCallSitesOffset());
408 }
409
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700410 const DexFile* GetDexFile() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700411 return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700412 }
413
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700414 void SetDexFile(const DexFile* dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier76172162016-01-26 14:54:06 -0800415 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700416 }
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700417
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000418 void SetLocation(ObjPtr<String> location) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -0800419
Vladimir Marko05792b92015-08-03 11:56:49 +0100420 // NOTE: Get/SetElementPtrSize() are intended for working with ArtMethod** and ArtField**
421 // provided by GetResolvedMethods/Fields() and ArtMethod::GetDexCacheResolvedMethods(),
422 // so they need to be public.
423
424 template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700425 static PtrType GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100426
427 template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700428 static void SetElementPtrSize(PtrType* ptr_array, size_t idx, PtrType ptr, PointerSize ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100429
Vladimir Markof44d36c2017-03-14 14:18:46 +0000430 template <typename T>
431 static NativeDexCachePair<T> GetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array,
432 size_t idx,
433 PointerSize ptr_size);
434
435 template <typename T>
436 static void SetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array,
437 size_t idx,
438 NativeDexCachePair<T> pair,
439 PointerSize ptr_size);
440
Vladimir Markof25cc732017-03-16 16:18:15 +0000441 uint32_t StringSlotIndex(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_);
442 uint32_t TypeSlotIndex(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
443 uint32_t FieldSlotIndex(uint32_t field_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100444 uint32_t MethodSlotIndex(uint32_t method_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markof25cc732017-03-16 16:18:15 +0000445 uint32_t MethodTypeSlotIndex(uint32_t proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);
446
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700447 private:
Andreas Gampecc1b5352016-12-01 16:58:38 -0800448 void Init(const DexFile* dex_file,
449 ObjPtr<String> location,
450 StringDexCacheType* strings,
451 uint32_t num_strings,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000452 TypeDexCacheType* resolved_types,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800453 uint32_t num_resolved_types,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100454 MethodDexCacheType* resolved_methods,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800455 uint32_t num_resolved_methods,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000456 FieldDexCacheType* resolved_fields,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800457 uint32_t num_resolved_fields,
Orion Hodsonc069a302017-01-18 09:23:12 +0000458 MethodTypeDexCacheType* resolved_method_types,
459 uint32_t num_resolved_method_types,
460 GcRoot<CallSite>* resolved_call_sites,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100461 uint32_t num_resolved_call_sites)
Andreas Gampecc1b5352016-12-01 16:58:38 -0800462 REQUIRES_SHARED(Locks::mutator_lock_);
463
Vladimir Markof44d36c2017-03-14 14:18:46 +0000464 // std::pair<> is not trivially copyable and as such it is unsuitable for atomic operations,
465 // so we use a custom pair class for loading and storing the NativeDexCachePair<>.
466 template <typename IntType>
467 struct PACKED(2 * sizeof(IntType)) ConversionPair {
468 ConversionPair(IntType f, IntType s) : first(f), second(s) { }
469 ConversionPair(const ConversionPair&) = default;
470 ConversionPair& operator=(const ConversionPair&) = default;
471 IntType first;
472 IntType second;
473 };
474 using ConversionPair32 = ConversionPair<uint32_t>;
475 using ConversionPair64 = ConversionPair<uint64_t>;
476
Vladimir Marko05792b92015-08-03 11:56:49 +0100477 // Visit instance fields of the dex cache as well as its associated arrays.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800478 template <bool kVisitNativeRoots,
479 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
480 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
481 typename Visitor>
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000482 void VisitReferences(ObjPtr<Class> klass, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700483 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100484
Vladimir Markof44d36c2017-03-14 14:18:46 +0000485 // Due to lack of 16-byte atomics support, we use hand-crafted routines.
Alexey Frunze279cfba2017-07-22 00:24:43 -0700486#if defined(__aarch64__) || defined(__mips__)
487 // 16-byte atomics are supported on aarch64, mips and mips64.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000488 ALWAYS_INLINE static ConversionPair64 AtomicLoadRelaxed16B(
489 std::atomic<ConversionPair64>* target) {
490 return target->load(std::memory_order_relaxed);
491 }
492
493 ALWAYS_INLINE static void AtomicStoreRelease16B(
494 std::atomic<ConversionPair64>* target, ConversionPair64 value) {
495 target->store(value, std::memory_order_release);
496 }
497#elif defined(__x86_64__)
498 ALWAYS_INLINE static ConversionPair64 AtomicLoadRelaxed16B(
499 std::atomic<ConversionPair64>* target) {
500 uint64_t first, second;
501 __asm__ __volatile__(
502 "lock cmpxchg16b (%2)"
503 : "=&a"(first), "=&d"(second)
504 : "r"(target), "a"(0), "d"(0), "b"(0), "c"(0)
505 : "cc");
506 return ConversionPair64(first, second);
507 }
508
509 ALWAYS_INLINE static void AtomicStoreRelease16B(
510 std::atomic<ConversionPair64>* target, ConversionPair64 value) {
511 uint64_t first, second;
512 __asm__ __volatile__ (
513 "movq (%2), %%rax\n\t"
514 "movq 8(%2), %%rdx\n\t"
515 "1:\n\t"
516 "lock cmpxchg16b (%2)\n\t"
517 "jnz 1b"
518 : "=&a"(first), "=&d"(second)
519 : "r"(target), "b"(value.first), "c"(value.second)
520 : "cc");
521 }
522#else
523 static ConversionPair64 AtomicLoadRelaxed16B(std::atomic<ConversionPair64>* target);
524 static void AtomicStoreRelease16B(std::atomic<ConversionPair64>* target, ConversionPair64 value);
525#endif
526
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527 HeapReference<String> location_;
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000528 // Number of elements in the call_sites_ array. Note that this appears here
529 // because of our packing logic for 32 bit fields.
530 uint32_t num_resolved_call_sites_;
531
Narayan Kamath25352fc2016-08-03 12:46:58 +0100532 uint64_t dex_file_; // const DexFile*
Orion Hodsonc069a302017-01-18 09:23:12 +0000533 uint64_t resolved_call_sites_; // GcRoot<CallSite>* array with num_resolved_call_sites_
534 // elements.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000535 uint64_t resolved_fields_; // std::atomic<FieldDexCachePair>*, array with
536 // num_resolved_fields_ elements.
Narayan Kamath25352fc2016-08-03 12:46:58 +0100537 uint64_t resolved_method_types_; // std::atomic<MethodTypeDexCachePair>* array with
538 // num_resolved_method_types_ elements.
539 uint64_t resolved_methods_; // ArtMethod*, array with num_resolved_methods_ elements.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000540 uint64_t resolved_types_; // TypeDexCacheType*, array with num_resolved_types_ elements.
Narayan Kamath25352fc2016-08-03 12:46:58 +0100541 uint64_t strings_; // std::atomic<StringDexCachePair>*, array with num_strings_
542 // elements.
543
544 uint32_t num_resolved_fields_; // Number of elements in the resolved_fields_ array.
545 uint32_t num_resolved_method_types_; // Number of elements in the resolved_method_types_ array.
546 uint32_t num_resolved_methods_; // Number of elements in the resolved_methods_ array.
547 uint32_t num_resolved_types_; // Number of elements in the resolved_types_ array.
548 uint32_t num_strings_; // Number of elements in the strings_ array.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700549
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700550 friend struct art::DexCacheOffsets; // for verifying offset information
Vladimir Marko05792b92015-08-03 11:56:49 +0100551 friend class Object; // For VisitReferences
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700552 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700553};
554
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700556} // namespace art
557
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700558#endif // ART_RUNTIME_MIRROR_DEX_CACHE_H_