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