Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 19 | |
| 20 | #include "dex_cache.h" |
| 21 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 24 | #include "art_field.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 25 | #include "art_method.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 26 | #include "base/casts.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 27 | #include "base/enums.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 28 | #include "class_linker.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 29 | #include "dex/dex_file.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 30 | #include "gc/heap-inl.h" |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 31 | #include "gc_root-inl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 32 | #include "mirror/call_site.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 33 | #include "mirror/class.h" |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 34 | #include "mirror/method_type.h" |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 35 | #include "obj_ptr.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 36 | #include "runtime.h" |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 37 | #include "write_barrier-inl.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 38 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 39 | #include <atomic> |
| 40 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 41 | namespace art { |
| 42 | namespace mirror { |
| 43 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 44 | template <typename T> |
| 45 | inline void NativeDexCachePair<T>::Initialize(std::atomic<NativeDexCachePair<T>>* dex_cache, |
| 46 | PointerSize pointer_size) { |
| 47 | NativeDexCachePair<T> first_elem; |
| 48 | first_elem.object = nullptr; |
| 49 | first_elem.index = InvalidIndexForSlot(0); |
| 50 | DexCache::SetNativePairPtrSize(dex_cache, 0, first_elem, pointer_size); |
| 51 | } |
| 52 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 53 | inline uint32_t DexCache::ClassSize(PointerSize pointer_size) { |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 54 | const uint32_t vtable_entries = Object::kVTableLength; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 55 | return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size); |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 58 | inline uint32_t DexCache::StringSlotIndex(dex::StringIndex string_idx) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 59 | DCHECK_LT(string_idx.index_, GetDexFile()->NumStringIds()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 60 | const uint32_t slot_idx = string_idx.index_ % kDexCacheStringCacheSize; |
| 61 | DCHECK_LT(slot_idx, NumStrings()); |
| 62 | return slot_idx; |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 65 | inline String* DexCache::GetResolvedString(dex::StringIndex string_idx) { |
| 66 | return GetStrings()[StringSlotIndex(string_idx)].load( |
| 67 | std::memory_order_relaxed).GetObjectForIndex(string_idx.index_); |
| 68 | } |
| 69 | |
| 70 | inline void DexCache::SetResolvedString(dex::StringIndex string_idx, ObjPtr<String> resolved) { |
| 71 | DCHECK(resolved != nullptr); |
| 72 | GetStrings()[StringSlotIndex(string_idx)].store( |
| 73 | StringDexCachePair(resolved, string_idx.index_), std::memory_order_relaxed); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 74 | Runtime* const runtime = Runtime::Current(); |
| 75 | if (UNLIKELY(runtime->IsActiveTransaction())) { |
| 76 | DCHECK(runtime->IsAotCompiler()); |
| 77 | runtime->RecordResolveString(this, string_idx); |
| 78 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 79 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 80 | WriteBarrier::ForEveryFieldWrite(this); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 83 | inline void DexCache::ClearString(dex::StringIndex string_idx) { |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 84 | DCHECK(Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 85 | uint32_t slot_idx = StringSlotIndex(string_idx); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 86 | StringDexCacheType* slot = &GetStrings()[slot_idx]; |
| 87 | // This is racy but should only be called from the transactional interpreter. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 88 | if (slot->load(std::memory_order_relaxed).index == string_idx.index_) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 89 | StringDexCachePair cleared(nullptr, StringDexCachePair::InvalidIndexForSlot(slot_idx)); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 90 | slot->store(cleared, std::memory_order_relaxed); |
| 91 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 94 | inline uint32_t DexCache::TypeSlotIndex(dex::TypeIndex type_idx) { |
| 95 | DCHECK_LT(type_idx.index_, GetDexFile()->NumTypeIds()); |
| 96 | const uint32_t slot_idx = type_idx.index_ % kDexCacheTypeCacheSize; |
| 97 | DCHECK_LT(slot_idx, NumResolvedTypes()); |
| 98 | return slot_idx; |
| 99 | } |
| 100 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 101 | inline Class* DexCache::GetResolvedType(dex::TypeIndex type_idx) { |
Mathieu Chartier | 5df32d3 | 2016-12-06 16:02:27 -0800 | [diff] [blame] | 102 | // It is theorized that a load acquire is not required since obtaining the resolved class will |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 103 | // always have an address dependency or a lock. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 104 | return GetResolvedTypes()[TypeSlotIndex(type_idx)].load( |
| 105 | std::memory_order_relaxed).GetObjectForIndex(type_idx.index_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 108 | inline void DexCache::SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 109 | DCHECK(resolved != nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 110 | // TODO default transaction support. |
Mathieu Chartier | 5df32d3 | 2016-12-06 16:02:27 -0800 | [diff] [blame] | 111 | // Use a release store for SetResolvedType. This is done to prevent other threads from seeing a |
| 112 | // class but not necessarily seeing the loaded members like the static fields array. |
| 113 | // See b/32075261. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 114 | GetResolvedTypes()[TypeSlotIndex(type_idx)].store( |
| 115 | TypeDexCachePair(resolved, type_idx.index_), std::memory_order_release); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 116 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 117 | WriteBarrier::ForEveryFieldWrite(this); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 120 | inline void DexCache::ClearResolvedType(dex::TypeIndex type_idx) { |
| 121 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 122 | uint32_t slot_idx = TypeSlotIndex(type_idx); |
| 123 | TypeDexCacheType* slot = &GetResolvedTypes()[slot_idx]; |
| 124 | // This is racy but should only be called from the single-threaded ImageWriter and tests. |
| 125 | if (slot->load(std::memory_order_relaxed).index == type_idx.index_) { |
| 126 | TypeDexCachePair cleared(nullptr, TypeDexCachePair::InvalidIndexForSlot(slot_idx)); |
| 127 | slot->store(cleared, std::memory_order_relaxed); |
| 128 | } |
| 129 | } |
| 130 | |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 131 | inline uint32_t DexCache::MethodTypeSlotIndex(dex::ProtoIndex proto_idx) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 132 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 133 | DCHECK_LT(proto_idx.index_, GetDexFile()->NumProtoIds()); |
| 134 | const uint32_t slot_idx = proto_idx.index_ % kDexCacheMethodTypeCacheSize; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 135 | DCHECK_LT(slot_idx, NumResolvedMethodTypes()); |
| 136 | return slot_idx; |
| 137 | } |
| 138 | |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 139 | inline MethodType* DexCache::GetResolvedMethodType(dex::ProtoIndex proto_idx) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 140 | return GetResolvedMethodTypes()[MethodTypeSlotIndex(proto_idx)].load( |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 141 | std::memory_order_relaxed).GetObjectForIndex(proto_idx.index_); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 144 | inline void DexCache::SetResolvedMethodType(dex::ProtoIndex proto_idx, MethodType* resolved) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 145 | DCHECK(resolved != nullptr); |
| 146 | GetResolvedMethodTypes()[MethodTypeSlotIndex(proto_idx)].store( |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 147 | MethodTypeDexCachePair(resolved, proto_idx.index_), std::memory_order_relaxed); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 148 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 149 | WriteBarrier::ForEveryFieldWrite(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 152 | inline CallSite* DexCache::GetResolvedCallSite(uint32_t call_site_idx) { |
| 153 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
| 154 | DCHECK_LT(call_site_idx, GetDexFile()->NumCallSiteIds()); |
| 155 | GcRoot<mirror::CallSite>& target = GetResolvedCallSites()[call_site_idx]; |
| 156 | Atomic<GcRoot<mirror::CallSite>>& ref = |
| 157 | reinterpret_cast<Atomic<GcRoot<mirror::CallSite>>&>(target); |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 158 | return ref.load(std::memory_order_seq_cst).Read(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 161 | inline ObjPtr<CallSite> DexCache::SetResolvedCallSite(uint32_t call_site_idx, |
| 162 | ObjPtr<CallSite> call_site) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 163 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
| 164 | DCHECK_LT(call_site_idx, GetDexFile()->NumCallSiteIds()); |
| 165 | |
| 166 | GcRoot<mirror::CallSite> null_call_site(nullptr); |
| 167 | GcRoot<mirror::CallSite> candidate(call_site); |
| 168 | GcRoot<mirror::CallSite>& target = GetResolvedCallSites()[call_site_idx]; |
| 169 | |
| 170 | // The first assignment for a given call site wins. |
| 171 | Atomic<GcRoot<mirror::CallSite>>& ref = |
| 172 | reinterpret_cast<Atomic<GcRoot<mirror::CallSite>>&>(target); |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 173 | if (ref.CompareAndSetStrongSequentiallyConsistent(null_call_site, candidate)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 174 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 175 | WriteBarrier::ForEveryFieldWrite(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 176 | return call_site; |
| 177 | } else { |
| 178 | return target.Read(); |
| 179 | } |
| 180 | } |
| 181 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 182 | inline uint32_t DexCache::FieldSlotIndex(uint32_t field_idx) { |
| 183 | DCHECK_LT(field_idx, GetDexFile()->NumFieldIds()); |
| 184 | const uint32_t slot_idx = field_idx % kDexCacheFieldCacheSize; |
| 185 | DCHECK_LT(slot_idx, NumResolvedFields()); |
| 186 | return slot_idx; |
| 187 | } |
| 188 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 189 | inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 190 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 191 | auto pair = GetNativePairPtrSize(GetResolvedFields(), FieldSlotIndex(field_idx), ptr_size); |
| 192 | return pair.GetObjectForIndex(field_idx); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 195 | inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 196 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 197 | DCHECK(field != nullptr); |
| 198 | FieldDexCachePair pair(field, field_idx); |
| 199 | SetNativePairPtrSize(GetResolvedFields(), FieldSlotIndex(field_idx), pair, ptr_size); |
| 200 | } |
| 201 | |
| 202 | inline void DexCache::ClearResolvedField(uint32_t field_idx, PointerSize ptr_size) { |
| 203 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
| 204 | uint32_t slot_idx = FieldSlotIndex(field_idx); |
| 205 | auto* resolved_fields = GetResolvedFields(); |
| 206 | // This is racy but should only be called from the single-threaded ImageWriter. |
| 207 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 208 | if (GetNativePairPtrSize(resolved_fields, slot_idx, ptr_size).index == field_idx) { |
| 209 | FieldDexCachePair cleared(nullptr, FieldDexCachePair::InvalidIndexForSlot(slot_idx)); |
| 210 | SetNativePairPtrSize(resolved_fields, slot_idx, cleared, ptr_size); |
| 211 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 214 | inline uint32_t DexCache::MethodSlotIndex(uint32_t method_idx) { |
| 215 | DCHECK_LT(method_idx, GetDexFile()->NumMethodIds()); |
| 216 | const uint32_t slot_idx = method_idx % kDexCacheMethodCacheSize; |
| 217 | DCHECK_LT(slot_idx, NumResolvedMethods()); |
| 218 | return slot_idx; |
| 219 | } |
| 220 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 221 | inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 222 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 223 | auto pair = GetNativePairPtrSize(GetResolvedMethods(), MethodSlotIndex(method_idx), ptr_size); |
| 224 | return pair.GetObjectForIndex(method_idx); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 227 | inline void DexCache::SetResolvedMethod(uint32_t method_idx, |
| 228 | ArtMethod* method, |
| 229 | PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 230 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 231 | DCHECK(method != nullptr); |
| 232 | MethodDexCachePair pair(method, method_idx); |
| 233 | SetNativePairPtrSize(GetResolvedMethods(), MethodSlotIndex(method_idx), pair, ptr_size); |
| 234 | } |
| 235 | |
| 236 | inline void DexCache::ClearResolvedMethod(uint32_t method_idx, PointerSize ptr_size) { |
| 237 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
| 238 | uint32_t slot_idx = MethodSlotIndex(method_idx); |
| 239 | auto* resolved_methods = GetResolvedMethods(); |
| 240 | // This is racy but should only be called from the single-threaded ImageWriter. |
| 241 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 242 | if (GetNativePairPtrSize(resolved_methods, slot_idx, ptr_size).index == method_idx) { |
| 243 | MethodDexCachePair cleared(nullptr, MethodDexCachePair::InvalidIndexForSlot(slot_idx)); |
| 244 | SetNativePairPtrSize(resolved_methods, slot_idx, cleared, ptr_size); |
| 245 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 248 | template <typename T> |
| 249 | NativeDexCachePair<T> DexCache::GetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array, |
| 250 | size_t idx, |
| 251 | PointerSize ptr_size) { |
| 252 | if (ptr_size == PointerSize::k64) { |
| 253 | auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array); |
| 254 | ConversionPair64 value = AtomicLoadRelaxed16B(&array[idx]); |
| 255 | return NativeDexCachePair<T>(reinterpret_cast64<T*>(value.first), |
| 256 | dchecked_integral_cast<size_t>(value.second)); |
| 257 | } else { |
| 258 | auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array); |
| 259 | ConversionPair32 value = array[idx].load(std::memory_order_relaxed); |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 260 | return NativeDexCachePair<T>(reinterpret_cast32<T*>(value.first), value.second); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | template <typename T> |
| 265 | void DexCache::SetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array, |
| 266 | size_t idx, |
| 267 | NativeDexCachePair<T> pair, |
| 268 | PointerSize ptr_size) { |
| 269 | if (ptr_size == PointerSize::k64) { |
| 270 | auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array); |
| 271 | ConversionPair64 v(reinterpret_cast64<uint64_t>(pair.object), pair.index); |
| 272 | AtomicStoreRelease16B(&array[idx], v); |
| 273 | } else { |
| 274 | auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array); |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 275 | ConversionPair32 v(reinterpret_cast32<uint32_t>(pair.object), |
| 276 | dchecked_integral_cast<uint32_t>(pair.index)); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 277 | array[idx].store(v, std::memory_order_release); |
| 278 | } |
| 279 | } |
| 280 | |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 281 | template <typename T, |
| 282 | ReadBarrierOption kReadBarrierOption, |
| 283 | typename Visitor> |
| 284 | inline void VisitDexCachePairs(std::atomic<DexCachePair<T>>* pairs, |
| 285 | size_t num_pairs, |
| 286 | const Visitor& visitor) |
| 287 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
| 288 | for (size_t i = 0; i < num_pairs; ++i) { |
| 289 | DexCachePair<T> source = pairs[i].load(std::memory_order_relaxed); |
| 290 | // NOTE: We need the "template" keyword here to avoid a compilation |
| 291 | // failure. GcRoot<T> is a template argument-dependent type and we need to |
| 292 | // tell the compiler to treat "Read" as a template rather than a field or |
| 293 | // function. Otherwise, on encountering the "<" token, the compiler would |
| 294 | // treat "Read" as a field. |
Mathieu Chartier | 6b4c287 | 2016-11-01 14:45:26 -0700 | [diff] [blame] | 295 | T* const before = source.object.template Read<kReadBarrierOption>(); |
| 296 | visitor.VisitRootIfNonNull(source.object.AddressWithoutBarrier()); |
| 297 | if (source.object.template Read<kReadBarrierOption>() != before) { |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 298 | pairs[i].store(source, std::memory_order_relaxed); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 303 | template <bool kVisitNativeRoots, |
| 304 | VerifyObjectFlags kVerifyFlags, |
| 305 | ReadBarrierOption kReadBarrierOption, |
| 306 | typename Visitor> |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 307 | inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 308 | // Visit instance fields first. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 309 | VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 310 | // Visit arrays after. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 311 | if (kVisitNativeRoots) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 312 | VisitDexCachePairs<String, kReadBarrierOption, Visitor>( |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 313 | GetStrings(), NumStrings(), visitor); |
| 314 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 315 | VisitDexCachePairs<Class, kReadBarrierOption, Visitor>( |
| 316 | GetResolvedTypes(), NumResolvedTypes(), visitor); |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 317 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 318 | VisitDexCachePairs<MethodType, kReadBarrierOption, Visitor>( |
Narayan Kamath | d08e39b | 2016-10-19 14:16:35 +0100 | [diff] [blame] | 319 | GetResolvedMethodTypes(), NumResolvedMethodTypes(), visitor); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 320 | |
| 321 | GcRoot<mirror::CallSite>* resolved_call_sites = GetResolvedCallSites(); |
| 322 | for (size_t i = 0, num_call_sites = NumResolvedCallSites(); i != num_call_sites; ++i) { |
| 323 | visitor.VisitRootIfNonNull(resolved_call_sites[i].AddressWithoutBarrier()); |
| 324 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 325 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 328 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 329 | inline void DexCache::FixupStrings(StringDexCacheType* dest, const Visitor& visitor) { |
| 330 | StringDexCacheType* src = GetStrings(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 331 | for (size_t i = 0, count = NumStrings(); i < count; ++i) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 332 | StringDexCachePair source = src[i].load(std::memory_order_relaxed); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 333 | String* ptr = source.object.Read<kReadBarrierOption>(); |
| 334 | String* new_source = visitor(ptr); |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 335 | source.object = GcRoot<String>(new_source); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 336 | dest[i].store(source, std::memory_order_relaxed); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 340 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 341 | inline void DexCache::FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor) { |
| 342 | TypeDexCacheType* src = GetResolvedTypes(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 343 | for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 344 | TypeDexCachePair source = src[i].load(std::memory_order_relaxed); |
| 345 | Class* ptr = source.object.Read<kReadBarrierOption>(); |
| 346 | Class* new_source = visitor(ptr); |
| 347 | source.object = GcRoot<Class>(new_source); |
| 348 | dest[i].store(source, std::memory_order_relaxed); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 352 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 353 | inline void DexCache::FixupResolvedMethodTypes(MethodTypeDexCacheType* dest, |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 354 | const Visitor& visitor) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 355 | MethodTypeDexCacheType* src = GetResolvedMethodTypes(); |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 356 | for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) { |
| 357 | MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 358 | MethodType* ptr = source.object.Read<kReadBarrierOption>(); |
| 359 | MethodType* new_source = visitor(ptr); |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 360 | source.object = GcRoot<MethodType>(new_source); |
| 361 | dest[i].store(source, std::memory_order_relaxed); |
| 362 | } |
| 363 | } |
| 364 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 365 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
| 366 | inline void DexCache::FixupResolvedCallSites(GcRoot<mirror::CallSite>* dest, |
| 367 | const Visitor& visitor) { |
| 368 | GcRoot<mirror::CallSite>* src = GetResolvedCallSites(); |
| 369 | for (size_t i = 0, count = NumResolvedCallSites(); i < count; ++i) { |
| 370 | mirror::CallSite* source = src[i].Read<kReadBarrierOption>(); |
| 371 | mirror::CallSite* new_source = visitor(source); |
| 372 | dest[i] = GcRoot<mirror::CallSite>(new_source); |
| 373 | } |
| 374 | } |
| 375 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 376 | } // namespace mirror |
| 377 | } // namespace art |
| 378 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 379 | #endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |