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 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 24 | #include "base/casts.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 25 | #include "base/enums.h" |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 26 | #include "base/logging.h" |
| 27 | #include "mirror/class.h" |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 28 | #include "mirror/method_type.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 29 | #include "runtime.h" |
| 30 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 31 | #include <atomic> |
| 32 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | namespace mirror { |
| 35 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 36 | inline uint32_t DexCache::ClassSize(PointerSize pointer_size) { |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 37 | uint32_t vtable_entries = Object::kVTableLength + 5; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 | 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] | 39 | } |
| 40 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 41 | inline mirror::String* DexCache::GetResolvedString(uint32_t string_idx) { |
| 42 | DCHECK_LT(string_idx, GetDexFile()->NumStringIds()); |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 43 | return StringDexCachePair::Lookup(GetStrings(), string_idx, NumStrings()).Read(); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 46 | inline void DexCache::SetResolvedString(uint32_t string_idx, mirror::String* resolved) { |
Narayan Kamath | 23136d1 | 2016-09-30 16:29:19 +0100 | [diff] [blame] | 47 | StringDexCachePair::Assign(GetStrings(), string_idx, resolved, NumStrings()); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 48 | Runtime* const runtime = Runtime::Current(); |
| 49 | if (UNLIKELY(runtime->IsActiveTransaction())) { |
| 50 | DCHECK(runtime->IsAotCompiler()); |
| 51 | runtime->RecordResolveString(this, string_idx); |
| 52 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 53 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 54 | runtime->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 55 | } |
| 56 | |
| 57 | inline void DexCache::ClearString(uint32_t string_idx) { |
| 58 | const uint32_t slot_idx = string_idx % NumStrings(); |
| 59 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 60 | StringDexCacheType* slot = &GetStrings()[slot_idx]; |
| 61 | // This is racy but should only be called from the transactional interpreter. |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 62 | if (slot->load(std::memory_order_relaxed).index == string_idx) { |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 63 | StringDexCachePair cleared( |
| 64 | nullptr, |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 65 | StringDexCachePair::InvalidIndexForSlot(slot_idx)); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 66 | slot->store(cleared, std::memory_order_relaxed); |
| 67 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | inline Class* DexCache::GetResolvedType(uint32_t type_idx) { |
| 71 | DCHECK_LT(type_idx, NumResolvedTypes()); |
| 72 | return GetResolvedTypes()[type_idx].Read(); |
| 73 | } |
| 74 | |
| 75 | inline void DexCache::SetResolvedType(uint32_t type_idx, Class* resolved) { |
| 76 | DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 77 | // TODO default transaction support. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 78 | GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved); |
| 79 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 80 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 81 | } |
| 82 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 83 | inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) { |
| 84 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
| 85 | DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds()); |
| 86 | return MethodTypeDexCachePair::Lookup( |
| 87 | GetResolvedMethodTypes(), proto_idx, NumResolvedMethodTypes()).Read(); |
| 88 | } |
| 89 | |
| 90 | inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) { |
| 91 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Narayan Kamath | 42b3dd0 | 2016-10-03 11:33:01 +0100 | [diff] [blame] | 92 | DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 93 | |
Narayan Kamath | 23136d1 | 2016-09-30 16:29:19 +0100 | [diff] [blame] | 94 | MethodTypeDexCachePair::Assign(GetResolvedMethodTypes(), proto_idx, resolved, |
| 95 | NumResolvedMethodTypes()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 96 | // TODO: Fine-grained marking, so that we don't need to go through all arrays in full. |
| 97 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this); |
| 98 | } |
| 99 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 100 | inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 101 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 102 | DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 103 | ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 104 | if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) { |
| 105 | return nullptr; |
| 106 | } |
| 107 | return field; |
| 108 | } |
| 109 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 110 | 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] | 111 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 112 | DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 113 | SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 116 | inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 117 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 118 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 119 | ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 120 | // Hide resolution trampoline methods from the caller |
| 121 | if (method != nullptr && method->IsRuntimeMethod()) { |
| 122 | DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod()); |
| 123 | return nullptr; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 124 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 125 | return method; |
| 126 | } |
| 127 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 128 | inline void DexCache::SetResolvedMethod(uint32_t method_idx, |
| 129 | ArtMethod* method, |
| 130 | PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 131 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 132 | DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB. |
| 133 | SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size); |
| 134 | } |
| 135 | |
| 136 | template <typename PtrType> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 137 | inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) { |
| 138 | if (ptr_size == PointerSize::k64) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 139 | uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx]; |
| 140 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 141 | } else { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 142 | uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx]; |
| 143 | return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element)); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | template <typename PtrType> |
| 148 | inline void DexCache::SetElementPtrSize(PtrType* ptr_array, |
| 149 | size_t idx, |
| 150 | PtrType ptr, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 151 | PointerSize ptr_size) { |
| 152 | if (ptr_size == PointerSize::k64) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 153 | reinterpret_cast<uint64_t*>(ptr_array)[idx] = |
| 154 | dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 155 | } else { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 156 | reinterpret_cast<uint32_t*>(ptr_array)[idx] = |
| 157 | dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 158 | } |
| 159 | } |
| 160 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 161 | template <bool kVisitNativeRoots, |
| 162 | VerifyObjectFlags kVerifyFlags, |
| 163 | ReadBarrierOption kReadBarrierOption, |
| 164 | typename Visitor> |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 165 | inline void DexCache::VisitReferences(mirror::Class* klass, const Visitor& visitor) { |
| 166 | // Visit instance fields first. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 167 | VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 168 | // Visit arrays after. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 169 | if (kVisitNativeRoots) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 170 | mirror::StringDexCacheType* strings = GetStrings(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 171 | for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 172 | StringDexCachePair source = strings[i].load(std::memory_order_relaxed); |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 173 | mirror::String* before = source.object.Read<kReadBarrierOption>(); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 174 | GcRoot<mirror::String> root(before); |
| 175 | visitor.VisitRootIfNonNull(root.AddressWithoutBarrier()); |
| 176 | if (root.Read() != before) { |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 177 | source.object = GcRoot<String>(root.Read()); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 178 | strings[i].store(source, std::memory_order_relaxed); |
| 179 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 180 | } |
| 181 | GcRoot<mirror::Class>* resolved_types = GetResolvedTypes(); |
| 182 | for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) { |
| 183 | visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier()); |
| 184 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 185 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 188 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 189 | inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) { |
| 190 | mirror::StringDexCacheType* src = GetStrings(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 191 | for (size_t i = 0, count = NumStrings(); i < count; ++i) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 192 | StringDexCachePair source = src[i].load(std::memory_order_relaxed); |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 193 | mirror::String* ptr = source.object.Read<kReadBarrierOption>(); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 194 | mirror::String* new_source = visitor(ptr); |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 195 | source.object = GcRoot<String>(new_source); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 196 | dest[i].store(source, std::memory_order_relaxed); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 200 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 201 | inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) { |
| 202 | GcRoot<mirror::Class>* src = GetResolvedTypes(); |
| 203 | for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) { |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 204 | mirror::Class* source = src[i].Read<kReadBarrierOption>(); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 205 | mirror::Class* new_source = visitor(source); |
Nicolas Geoffray | d0668f2 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 206 | dest[i] = GcRoot<mirror::Class>(new_source); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame^] | 210 | template <ReadBarrierOption kReadBarrierOption, typename Visitor> |
| 211 | inline void DexCache::FixupResolvedMethodTypes(mirror::MethodTypeDexCacheType* dest, |
| 212 | const Visitor& visitor) { |
| 213 | mirror::MethodTypeDexCacheType* src = GetResolvedMethodTypes(); |
| 214 | for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) { |
| 215 | MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed); |
| 216 | mirror::MethodType* ptr = source.object.Read<kReadBarrierOption>(); |
| 217 | mirror::MethodType* new_source = visitor(ptr); |
| 218 | source.object = GcRoot<MethodType>(new_source); |
| 219 | dest[i].store(source, std::memory_order_relaxed); |
| 220 | } |
| 221 | } |
| 222 | |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 223 | } // namespace mirror |
| 224 | } // namespace art |
| 225 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 226 | #endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_ |