blob: 8401b66ee40b71fee96b9a5820efe0e870b2a4ff [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"
Vladimir Marko6834d342018-05-25 13:12:09 +010024#include "gc_root-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025#include "object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "object_array.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070027
28namespace art {
29
Vladimir Markoca8de0a2018-07-04 11:56:08 +010030namespace linker {
31class ImageWriter;
32} // namespace linker
33
Vladimir Marko8d6768d2017-03-14 10:13:21 +000034class ArtField;
Alex Lightdba61482016-12-21 08:20:29 -080035class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036struct DexCacheOffsets;
37class DexFile;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038union JValue;
Andreas Gampecc1b5352016-12-01 16:58:38 -080039class LinearAlloc;
40class Thread;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
42namespace mirror {
43
Orion Hodsonc069a302017-01-18 09:23:12 +000044class CallSite;
Vladimir Marko8d6768d2017-03-14 10:13:21 +000045class Class;
Narayan Kamath25352fc2016-08-03 12:46:58 +010046class MethodType;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070047class String;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070048
Narayan Kamathc38a6f82016-09-29 17:07:20 +010049template <typename T> struct PACKED(8) DexCachePair {
50 GcRoot<T> object;
51 uint32_t index;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070052 // The array is initially [ {0,0}, {0,0}, {0,0} ... ]
53 // We maintain the invariant that once a dex cache entry is populated,
54 // the pointer is always non-0
55 // Any given entry would thus be:
56 // {non-0, non-0} OR {0,0}
57 //
58 // It's generally sufficiently enough then to check if the
Narayan Kamathc38a6f82016-09-29 17:07:20 +010059 // lookup index matches the stored index (for a >0 lookup index)
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070060 // because if it's true the pointer is also non-null.
61 //
62 // For the 0th entry which is a special case, the value is either
63 // {0,0} (initial state) or {non-0, 0} which indicates
Narayan Kamathc38a6f82016-09-29 17:07:20 +010064 // that a valid object is stored at that index for a dex section id of 0.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070065 //
Narayan Kamathc38a6f82016-09-29 17:07:20 +010066 // As an optimization, we want to avoid branching on the object pointer since
67 // it's always non-null if the id branch succeeds (except for the 0th id).
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070068 // Set the initial state for the 0th entry to be {0,1} which is guaranteed to fail
Narayan Kamathc38a6f82016-09-29 17:07:20 +010069 // the lookup id == stored id branch.
Vladimir Marko8d6768d2017-03-14 10:13:21 +000070 DexCachePair(ObjPtr<T> object, uint32_t index)
Narayan Kamathc38a6f82016-09-29 17:07:20 +010071 : object(object),
72 index(index) {}
Andreas Gamped9911ee2017-03-27 13:27:24 -070073 DexCachePair() : index(0) {}
Narayan Kamathc38a6f82016-09-29 17:07:20 +010074 DexCachePair(const DexCachePair<T>&) = default;
75 DexCachePair& operator=(const DexCachePair<T>&) = default;
Mathieu Chartierbb816d62016-09-07 10:17:46 -070076
Narayan Kamathc38a6f82016-09-29 17:07:20 +010077 static void Initialize(std::atomic<DexCachePair<T>>* dex_cache) {
78 DexCachePair<T> first_elem;
79 first_elem.object = GcRoot<T>(nullptr);
80 first_elem.index = InvalidIndexForSlot(0);
81 dex_cache[0].store(first_elem, std::memory_order_relaxed);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070082 }
Mathieu Chartierbb816d62016-09-07 10:17:46 -070083
Narayan Kamathc38a6f82016-09-29 17:07:20 +010084 static uint32_t InvalidIndexForSlot(uint32_t slot) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070085 // Since the cache size is a power of two, 0 will always map to slot 0.
86 // Use 1 for slot 0 and 0 for all other slots.
87 return (slot == 0) ? 1u : 0u;
88 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +000089
90 T* GetObjectForIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
91 if (idx != index) {
92 return nullptr;
93 }
94 DCHECK(!object.IsNull());
95 return object.Read();
96 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070097};
Narayan Kamathc38a6f82016-09-29 17:07:20 +010098
Vladimir Markof44d36c2017-03-14 14:18:46 +000099template <typename T> struct PACKED(2 * __SIZEOF_POINTER__) NativeDexCachePair {
100 T* object;
101 size_t index;
102 // This is similar to DexCachePair except that we're storing a native pointer
103 // instead of a GC root. See DexCachePair for the details.
104 NativeDexCachePair(T* object, uint32_t index)
105 : object(object),
106 index(index) {}
107 NativeDexCachePair() : object(nullptr), index(0u) { }
108 NativeDexCachePair(const NativeDexCachePair<T>&) = default;
109 NativeDexCachePair& operator=(const NativeDexCachePair<T>&) = default;
110
111 static void Initialize(std::atomic<NativeDexCachePair<T>>* dex_cache, PointerSize pointer_size);
112
113 static uint32_t InvalidIndexForSlot(uint32_t slot) {
114 // Since the cache size is a power of two, 0 will always map to slot 0.
115 // Use 1 for slot 0 and 0 for all other slots.
116 return (slot == 0) ? 1u : 0u;
117 }
118
119 T* GetObjectForIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) {
120 if (idx != index) {
121 return nullptr;
122 }
123 DCHECK(object != nullptr);
124 return object;
125 }
126};
127
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000128using TypeDexCachePair = DexCachePair<Class>;
129using TypeDexCacheType = std::atomic<TypeDexCachePair>;
130
131using StringDexCachePair = DexCachePair<String>;
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700132using StringDexCacheType = std::atomic<StringDexCachePair>;
133
Vladimir Markof44d36c2017-03-14 14:18:46 +0000134using FieldDexCachePair = NativeDexCachePair<ArtField>;
135using FieldDexCacheType = std::atomic<FieldDexCachePair>;
136
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100137using MethodDexCachePair = NativeDexCachePair<ArtMethod>;
138using MethodDexCacheType = std::atomic<MethodDexCachePair>;
139
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000140using MethodTypeDexCachePair = DexCachePair<MethodType>;
Narayan Kamath25352fc2016-08-03 12:46:58 +0100141using MethodTypeDexCacheType = std::atomic<MethodTypeDexCachePair>;
142
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143// C++ mirror of java.lang.DexCache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100144class MANAGED DexCache final : public Object {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700145 public:
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700146 // Size of java.lang.DexCache.class.
Andreas Gampe542451c2016-07-26 09:02:02 -0700147 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700148
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000149 // Size of type dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
150 static constexpr size_t kDexCacheTypeCacheSize = 1024;
151 static_assert(IsPowerOfTwo(kDexCacheTypeCacheSize),
152 "Type dex cache size is not a power of 2.");
153
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700154 // Size of string dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
155 static constexpr size_t kDexCacheStringCacheSize = 1024;
156 static_assert(IsPowerOfTwo(kDexCacheStringCacheSize),
157 "String dex cache size is not a power of 2.");
158
Vladimir Markof44d36c2017-03-14 14:18:46 +0000159 // Size of field dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
Vladimir Marko990fc442018-08-29 13:58:23 +0000160 static constexpr size_t kDexCacheFieldCacheSize = 1024;
Vladimir Markof44d36c2017-03-14 14:18:46 +0000161 static_assert(IsPowerOfTwo(kDexCacheFieldCacheSize),
162 "Field dex cache size is not a power of 2.");
163
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100164 // Size of method dex cache. Needs to be a power of 2 for entrypoint assumptions to hold.
Vladimir Marko990fc442018-08-29 13:58:23 +0000165 static constexpr size_t kDexCacheMethodCacheSize = 1024;
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100166 static_assert(IsPowerOfTwo(kDexCacheMethodCacheSize),
167 "Method dex cache size is not a power of 2.");
168
Narayan Kamath25352fc2016-08-03 12:46:58 +0100169 // Size of method type dex cache. Needs to be a power of 2 for entrypoint assumptions
170 // to hold.
171 static constexpr size_t kDexCacheMethodTypeCacheSize = 1024;
172 static_assert(IsPowerOfTwo(kDexCacheMethodTypeCacheSize),
173 "MethodType dex cache size is not a power of 2.");
174
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000175 static constexpr size_t StaticTypeSize() {
176 return kDexCacheTypeCacheSize;
177 }
178
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700179 static constexpr size_t StaticStringSize() {
180 return kDexCacheStringCacheSize;
181 }
182
Vladimir Markof44d36c2017-03-14 14:18:46 +0000183 static constexpr size_t StaticArtFieldSize() {
184 return kDexCacheFieldCacheSize;
185 }
186
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100187 static constexpr size_t StaticMethodSize() {
188 return kDexCacheMethodCacheSize;
189 }
190
Narayan Kamath25352fc2016-08-03 12:46:58 +0100191 static constexpr size_t StaticMethodTypeSize() {
192 return kDexCacheMethodTypeCacheSize;
193 }
194
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700195 // Size of an instance of java.lang.DexCache not including referenced values.
196 static constexpr uint32_t InstanceSize() {
197 return sizeof(DexCache);
198 }
199
Andreas Gampecc1b5352016-12-01 16:58:38 -0800200 static void InitializeDexCache(Thread* self,
201 ObjPtr<mirror::DexCache> dex_cache,
202 ObjPtr<mirror::String> location,
203 const DexFile* dex_file,
204 LinearAlloc* linear_alloc,
205 PointerSize image_pointer_size)
206 REQUIRES_SHARED(Locks::mutator_lock_)
207 REQUIRES(Locks::dex_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700208
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800209 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700210 void FixupStrings(StringDexCacheType* 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
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800213 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000214 void FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800216
Narayan Kamath7fe56582016-10-14 18:49:12 +0100217 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
218 void FixupResolvedMethodTypes(MethodTypeDexCacheType* dest, const Visitor& visitor)
219 REQUIRES_SHARED(Locks::mutator_lock_);
220
Orion Hodsonc069a302017-01-18 09:23:12 +0000221 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
222 void FixupResolvedCallSites(GcRoot<mirror::CallSite>* dest, const Visitor& visitor)
223 REQUIRES_SHARED(Locks::mutator_lock_);
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 String* GetLocation() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700226 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700227 }
228
buzbee5cd21802011-08-26 10:40:14 -0700229 static MemberOffset StringsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700230 return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_);
buzbeec5ef0462011-08-25 18:44:49 -0700231 }
232
Vladimir Marko05792b92015-08-03 11:56:49 +0100233 static MemberOffset ResolvedTypesOffset() {
234 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_);
235 }
236
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700237 static MemberOffset ResolvedFieldsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700238 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_);
buzbeec5ef0462011-08-25 18:44:49 -0700239 }
240
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700241 static MemberOffset ResolvedMethodsOffset() {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700242 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_);
buzbeec5ef0462011-08-25 18:44:49 -0700243 }
244
Narayan Kamath25352fc2016-08-03 12:46:58 +0100245 static MemberOffset ResolvedMethodTypesOffset() {
246 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_method_types_);
247 }
248
Orion Hodsonc069a302017-01-18 09:23:12 +0000249 static MemberOffset ResolvedCallSitesOffset() {
250 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_call_sites_);
251 }
252
Vladimir Marko05792b92015-08-03 11:56:49 +0100253 static MemberOffset NumStringsOffset() {
254 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_strings_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700255 }
256
Vladimir Marko05792b92015-08-03 11:56:49 +0100257 static MemberOffset NumResolvedTypesOffset() {
258 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_types_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700259 }
260
Vladimir Marko05792b92015-08-03 11:56:49 +0100261 static MemberOffset NumResolvedFieldsOffset() {
262 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_fields_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700263 }
264
Vladimir Marko05792b92015-08-03 11:56:49 +0100265 static MemberOffset NumResolvedMethodsOffset() {
266 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_methods_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700267 }
268
Narayan Kamath25352fc2016-08-03 12:46:58 +0100269 static MemberOffset NumResolvedMethodTypesOffset() {
270 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_method_types_);
271 }
272
Orion Hodsonc069a302017-01-18 09:23:12 +0000273 static MemberOffset NumResolvedCallSitesOffset() {
274 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_call_sites_);
275 }
276
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000277 String* GetResolvedString(dex::StringIndex string_idx) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700279
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800280 void SetResolvedString(dex::StringIndex string_idx, ObjPtr<mirror::String> resolved) ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700282
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700283 // Clear a string for a string_idx, used to undo string intern transactions to make sure
284 // the string isn't kept live.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800285 void ClearString(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700286
Andreas Gampea5b09a62016-11-17 15:21:22 -0800287 Class* GetResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100288
Andreas Gampea5b09a62016-11-17 15:21:22 -0800289 void SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved)
Mathieu Chartier31e88222016-10-14 18:43:19 -0700290 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100291
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000292 void ClearResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
293
Andreas Gampe542451c2016-07-26 09:02:02 -0700294 ALWAYS_INLINE ArtMethod* GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700295 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700296
Andreas Gampe542451c2016-07-26 09:02:02 -0700297 ALWAYS_INLINE void SetResolvedMethod(uint32_t method_idx,
298 ArtMethod* resolved,
299 PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700300 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100301 ALWAYS_INLINE void ClearResolvedMethod(uint32_t method_idx, PointerSize ptr_size)
302 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700303
Mathieu Chartierc7853442015-03-27 14:35:38 -0700304 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700305 ALWAYS_INLINE ArtField* GetResolvedField(uint32_t idx, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307
308 // Pointer sized variant, used for patching.
Andreas Gampe542451c2016-07-26 09:02:02 -0700309 ALWAYS_INLINE void SetResolvedField(uint32_t idx, ArtField* field, PointerSize ptr_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markof44d36c2017-03-14 14:18:46 +0000311 ALWAYS_INLINE void ClearResolvedField(uint32_t idx, PointerSize ptr_size)
312 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700313
Orion Hodson06d10a72018-05-14 08:53:38 +0100314 MethodType* GetResolvedMethodType(dex::ProtoIndex proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100315
Orion Hodson06d10a72018-05-14 08:53:38 +0100316 void SetResolvedMethodType(dex::ProtoIndex proto_idx, MethodType* resolved)
Orion Hodsonc069a302017-01-18 09:23:12 +0000317 REQUIRES_SHARED(Locks::mutator_lock_);
318
319 CallSite* GetResolvedCallSite(uint32_t call_site_idx) REQUIRES_SHARED(Locks::mutator_lock_);
320
321 // Attempts to bind |call_site_idx| to the call site |resolved|. The
322 // caller must use the return value in place of |resolved|. This is
323 // because multiple threads can invoke the bootstrap method each
324 // producing a call site, but the method handle invocation on the
325 // call site must be on a common agreed value.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100326 ObjPtr<CallSite> SetResolvedCallSite(uint32_t call_site_idx, ObjPtr<CallSite> resolved)
327 REQUIRES_SHARED(Locks::mutator_lock_) WARN_UNUSED;
Narayan Kamath25352fc2016-08-03 12:46:58 +0100328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 StringDexCacheType* GetStrings() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700330 return GetFieldPtr64<StringDexCacheType*>(StringsOffset());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700331 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700332
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700333 void SetStrings(StringDexCacheType* strings) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800334 SetFieldPtr<false>(StringsOffset(), strings);
335 }
336
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000337 TypeDexCacheType* GetResolvedTypes() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
338 return GetFieldPtr<TypeDexCacheType*>(ResolvedTypesOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700339 }
340
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000341 void SetResolvedTypes(TypeDexCacheType* resolved_types)
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800342 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700343 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800344 SetFieldPtr<false>(ResolvedTypesOffset(), resolved_types);
345 }
346
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100347 MethodDexCacheType* GetResolvedMethods() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
348 return GetFieldPtr<MethodDexCacheType*>(ResolvedMethodsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700349 }
350
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100351 void SetResolvedMethods(MethodDexCacheType* resolved_methods)
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800352 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700353 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800354 SetFieldPtr<false>(ResolvedMethodsOffset(), resolved_methods);
355 }
356
Vladimir Markof44d36c2017-03-14 14:18:46 +0000357 FieldDexCacheType* GetResolvedFields() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
358 return GetFieldPtr<FieldDexCacheType*>(ResolvedFieldsOffset());
Vladimir Marko05792b92015-08-03 11:56:49 +0100359 }
360
Vladimir Markof44d36c2017-03-14 14:18:46 +0000361 void SetResolvedFields(FieldDexCacheType* resolved_fields)
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800362 ALWAYS_INLINE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700363 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800364 SetFieldPtr<false>(ResolvedFieldsOffset(), resolved_fields);
365 }
366
Narayan Kamath25352fc2016-08-03 12:46:58 +0100367 MethodTypeDexCacheType* GetResolvedMethodTypes()
368 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath7fe56582016-10-14 18:49:12 +0100369 return GetFieldPtr64<MethodTypeDexCacheType*>(ResolvedMethodTypesOffset());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100370 }
371
372 void SetResolvedMethodTypes(MethodTypeDexCacheType* resolved_method_types)
373 ALWAYS_INLINE
374 REQUIRES_SHARED(Locks::mutator_lock_) {
375 SetFieldPtr<false>(ResolvedMethodTypesOffset(), resolved_method_types);
376 }
377
Orion Hodsonc069a302017-01-18 09:23:12 +0000378 GcRoot<CallSite>* GetResolvedCallSites()
379 ALWAYS_INLINE
380 REQUIRES_SHARED(Locks::mutator_lock_) {
381 return GetFieldPtr<GcRoot<CallSite>*>(ResolvedCallSitesOffset());
382 }
383
384 void SetResolvedCallSites(GcRoot<CallSite>* resolved_call_sites)
385 ALWAYS_INLINE
386 REQUIRES_SHARED(Locks::mutator_lock_) {
387 SetFieldPtr<false>(ResolvedCallSitesOffset(), resolved_call_sites);
388 }
389
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700390 size_t NumStrings() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100391 return GetField32(NumStringsOffset());
392 }
393
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700394 size_t NumResolvedTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100395 return GetField32(NumResolvedTypesOffset());
396 }
397
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700398 size_t NumResolvedMethods() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100399 return GetField32(NumResolvedMethodsOffset());
400 }
401
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700402 size_t NumResolvedFields() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100403 return GetField32(NumResolvedFieldsOffset());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700404 }
405
Narayan Kamath25352fc2016-08-03 12:46:58 +0100406 size_t NumResolvedMethodTypes() REQUIRES_SHARED(Locks::mutator_lock_) {
407 return GetField32(NumResolvedMethodTypesOffset());
408 }
409
Orion Hodsonc069a302017-01-18 09:23:12 +0000410 size_t NumResolvedCallSites() REQUIRES_SHARED(Locks::mutator_lock_) {
411 return GetField32(NumResolvedCallSitesOffset());
412 }
413
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700414 const DexFile* GetDexFile() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700415 return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700416 }
417
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700418 void SetDexFile(const DexFile* dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier76172162016-01-26 14:54:06 -0800419 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700420 }
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700421
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000422 void SetLocation(ObjPtr<String> location) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -0800423
Vladimir Markof44d36c2017-03-14 14:18:46 +0000424 template <typename T>
425 static NativeDexCachePair<T> GetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array,
426 size_t idx,
427 PointerSize ptr_size);
428
429 template <typename T>
430 static void SetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array,
431 size_t idx,
432 NativeDexCachePair<T> pair,
433 PointerSize ptr_size);
434
Vladimir Markof25cc732017-03-16 16:18:15 +0000435 uint32_t StringSlotIndex(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_);
436 uint32_t TypeSlotIndex(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
437 uint32_t FieldSlotIndex(uint32_t field_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100438 uint32_t MethodSlotIndex(uint32_t method_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Orion Hodson06d10a72018-05-14 08:53:38 +0100439 uint32_t MethodTypeSlotIndex(dex::ProtoIndex proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markof25cc732017-03-16 16:18:15 +0000440
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700441 private:
Andreas Gampecc1b5352016-12-01 16:58:38 -0800442 void Init(const DexFile* dex_file,
443 ObjPtr<String> location,
444 StringDexCacheType* strings,
445 uint32_t num_strings,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000446 TypeDexCacheType* resolved_types,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800447 uint32_t num_resolved_types,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100448 MethodDexCacheType* resolved_methods,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800449 uint32_t num_resolved_methods,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000450 FieldDexCacheType* resolved_fields,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800451 uint32_t num_resolved_fields,
Orion Hodsonc069a302017-01-18 09:23:12 +0000452 MethodTypeDexCacheType* resolved_method_types,
453 uint32_t num_resolved_method_types,
454 GcRoot<CallSite>* resolved_call_sites,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100455 uint32_t num_resolved_call_sites)
Andreas Gampecc1b5352016-12-01 16:58:38 -0800456 REQUIRES_SHARED(Locks::mutator_lock_);
457
Vladimir Markof44d36c2017-03-14 14:18:46 +0000458 // std::pair<> is not trivially copyable and as such it is unsuitable for atomic operations,
459 // so we use a custom pair class for loading and storing the NativeDexCachePair<>.
460 template <typename IntType>
461 struct PACKED(2 * sizeof(IntType)) ConversionPair {
462 ConversionPair(IntType f, IntType s) : first(f), second(s) { }
463 ConversionPair(const ConversionPair&) = default;
464 ConversionPair& operator=(const ConversionPair&) = default;
465 IntType first;
466 IntType second;
467 };
468 using ConversionPair32 = ConversionPair<uint32_t>;
469 using ConversionPair64 = ConversionPair<uint64_t>;
470
Vladimir Marko05792b92015-08-03 11:56:49 +0100471 // Visit instance fields of the dex cache as well as its associated arrays.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800472 template <bool kVisitNativeRoots,
473 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
474 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
475 typename Visitor>
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000476 void VisitReferences(ObjPtr<Class> klass, const Visitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700477 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100478
Vladimir Markof44d36c2017-03-14 14:18:46 +0000479 // Due to lack of 16-byte atomics support, we use hand-crafted routines.
Alexey Frunze279cfba2017-07-22 00:24:43 -0700480#if defined(__aarch64__) || defined(__mips__)
481 // 16-byte atomics are supported on aarch64, mips and mips64.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000482 ALWAYS_INLINE static ConversionPair64 AtomicLoadRelaxed16B(
483 std::atomic<ConversionPair64>* target) {
484 return target->load(std::memory_order_relaxed);
485 }
486
487 ALWAYS_INLINE static void AtomicStoreRelease16B(
488 std::atomic<ConversionPair64>* target, ConversionPair64 value) {
489 target->store(value, std::memory_order_release);
490 }
491#elif defined(__x86_64__)
492 ALWAYS_INLINE static ConversionPair64 AtomicLoadRelaxed16B(
493 std::atomic<ConversionPair64>* target) {
494 uint64_t first, second;
495 __asm__ __volatile__(
496 "lock cmpxchg16b (%2)"
497 : "=&a"(first), "=&d"(second)
498 : "r"(target), "a"(0), "d"(0), "b"(0), "c"(0)
499 : "cc");
500 return ConversionPair64(first, second);
501 }
502
503 ALWAYS_INLINE static void AtomicStoreRelease16B(
504 std::atomic<ConversionPair64>* target, ConversionPair64 value) {
505 uint64_t first, second;
506 __asm__ __volatile__ (
507 "movq (%2), %%rax\n\t"
508 "movq 8(%2), %%rdx\n\t"
509 "1:\n\t"
510 "lock cmpxchg16b (%2)\n\t"
511 "jnz 1b"
512 : "=&a"(first), "=&d"(second)
513 : "r"(target), "b"(value.first), "c"(value.second)
514 : "cc");
515 }
516#else
517 static ConversionPair64 AtomicLoadRelaxed16B(std::atomic<ConversionPair64>* target);
518 static void AtomicStoreRelease16B(std::atomic<ConversionPair64>* target, ConversionPair64 value);
519#endif
520
Ian Rogersef7d42f2014-01-06 12:55:46 -0800521 HeapReference<String> location_;
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000522 // Number of elements in the call_sites_ array. Note that this appears here
523 // because of our packing logic for 32 bit fields.
524 uint32_t num_resolved_call_sites_;
525
Narayan Kamath25352fc2016-08-03 12:46:58 +0100526 uint64_t dex_file_; // const DexFile*
Orion Hodsonc069a302017-01-18 09:23:12 +0000527 uint64_t resolved_call_sites_; // GcRoot<CallSite>* array with num_resolved_call_sites_
528 // elements.
Vladimir Markof44d36c2017-03-14 14:18:46 +0000529 uint64_t resolved_fields_; // std::atomic<FieldDexCachePair>*, array with
530 // num_resolved_fields_ elements.
Narayan Kamath25352fc2016-08-03 12:46:58 +0100531 uint64_t resolved_method_types_; // std::atomic<MethodTypeDexCachePair>* array with
532 // num_resolved_method_types_ elements.
533 uint64_t resolved_methods_; // ArtMethod*, array with num_resolved_methods_ elements.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000534 uint64_t resolved_types_; // TypeDexCacheType*, array with num_resolved_types_ elements.
Narayan Kamath25352fc2016-08-03 12:46:58 +0100535 uint64_t strings_; // std::atomic<StringDexCachePair>*, array with num_strings_
536 // elements.
537
538 uint32_t num_resolved_fields_; // Number of elements in the resolved_fields_ array.
539 uint32_t num_resolved_method_types_; // Number of elements in the resolved_method_types_ array.
540 uint32_t num_resolved_methods_; // Number of elements in the resolved_methods_ array.
541 uint32_t num_resolved_types_; // Number of elements in the resolved_types_ array.
542 uint32_t num_strings_; // Number of elements in the strings_ array.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700543
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700544 friend struct art::DexCacheOffsets; // for verifying offset information
Vladimir Markoca8de0a2018-07-04 11:56:08 +0100545 friend class linker::ImageWriter;
Vladimir Marko05792b92015-08-03 11:56:49 +0100546 friend class Object; // For VisitReferences
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700547 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700548};
549
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700551} // namespace art
552
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700553#endif // ART_RUNTIME_MIRROR_DEX_CACHE_H_