blob: be8815aba2229f1b9316cf5f96d7a7611c1e6576 [file] [log] [blame]
Ian Rogers39ebcb82013-05-30 16:57:23 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
18#define ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
Ian Rogers39ebcb82013-05-30 16:57:23 -070019
20#include "dex_cache.h"
21
Mathieu Chartierc7853442015-03-27 14:35:38 -070022#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010024#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070026#include "base/logging.h"
Narayan Kamathd08e39b2016-10-19 14:16:35 +010027#include "gc_root.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070028#include "mirror/class.h"
Narayan Kamath25352fc2016-08-03 12:46:58 +010029#include "mirror/method_type.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070030#include "runtime.h"
Mathieu Chartier31e88222016-10-14 18:43:19 -070031#include "obj_ptr.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070032
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070033#include <atomic>
34
Ian Rogers39ebcb82013-05-30 16:57:23 -070035namespace art {
36namespace mirror {
37
Andreas Gampe542451c2016-07-26 09:02:02 -070038inline uint32_t DexCache::ClassSize(PointerSize pointer_size) {
Vladimir Markoc1363122015-04-09 14:13:13 +010039 uint32_t vtable_entries = Object::kVTableLength + 5;
Mathieu Chartiere401d142015-04-22 13:56:20 -070040 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Ian Rogers39ebcb82013-05-30 16:57:23 -070041}
42
Andreas Gampe8a0128a2016-11-28 07:38:35 -080043inline mirror::String* DexCache::GetResolvedString(dex::StringIndex string_idx) {
44 DCHECK_LT(string_idx.index_, GetDexFile()->NumStringIds());
45 return StringDexCachePair::Lookup(GetStrings(), string_idx.index_, NumStrings()).Read();
Andreas Gampeaa910d52014-07-30 18:59:05 -070046}
47
Andreas Gampe8a0128a2016-11-28 07:38:35 -080048inline void DexCache::SetResolvedString(dex::StringIndex string_idx,
49 ObjPtr<mirror::String> resolved) {
50 StringDexCachePair::Assign(GetStrings(), string_idx.index_, resolved.Ptr(), NumStrings());
Mathieu Chartierbb816d62016-09-07 10:17:46 -070051 Runtime* const runtime = Runtime::Current();
52 if (UNLIKELY(runtime->IsActiveTransaction())) {
53 DCHECK(runtime->IsAotCompiler());
54 runtime->RecordResolveString(this, string_idx);
55 }
Vladimir Marko05792b92015-08-03 11:56:49 +010056 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
Mathieu Chartierbb816d62016-09-07 10:17:46 -070057 runtime->GetHeap()->WriteBarrierEveryFieldOf(this);
58}
59
Andreas Gampe8a0128a2016-11-28 07:38:35 -080060inline void DexCache::ClearString(dex::StringIndex string_idx) {
61 const uint32_t slot_idx = string_idx.index_ % NumStrings();
Mathieu Chartierbb816d62016-09-07 10:17:46 -070062 DCHECK(Runtime::Current()->IsAotCompiler());
63 StringDexCacheType* slot = &GetStrings()[slot_idx];
64 // This is racy but should only be called from the transactional interpreter.
Andreas Gampe8a0128a2016-11-28 07:38:35 -080065 if (slot->load(std::memory_order_relaxed).index == string_idx.index_) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070066 StringDexCachePair cleared(
67 nullptr,
Narayan Kamathc38a6f82016-09-29 17:07:20 +010068 StringDexCachePair::InvalidIndexForSlot(slot_idx));
Mathieu Chartierbb816d62016-09-07 10:17:46 -070069 slot->store(cleared, std::memory_order_relaxed);
70 }
Vladimir Marko05792b92015-08-03 11:56:49 +010071}
72
Andreas Gampea5b09a62016-11-17 15:21:22 -080073inline Class* DexCache::GetResolvedType(dex::TypeIndex type_idx) {
74 DCHECK_LT(type_idx.index_, NumResolvedTypes());
75 return GetResolvedTypes()[type_idx.index_].Read();
Vladimir Marko05792b92015-08-03 11:56:49 +010076}
77
Andreas Gampea5b09a62016-11-17 15:21:22 -080078inline void DexCache::SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved) {
79 DCHECK_LT(type_idx.index_, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB.
Vladimir Marko05792b92015-08-03 11:56:49 +010080 // TODO default transaction support.
Andreas Gampea5b09a62016-11-17 15:21:22 -080081 GetResolvedTypes()[type_idx.index_] = GcRoot<Class>(resolved);
Vladimir Marko05792b92015-08-03 11:56:49 +010082 // 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
Narayan Kamath25352fc2016-08-03 12:46:58 +010086inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) {
87 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
88 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
89 return MethodTypeDexCachePair::Lookup(
90 GetResolvedMethodTypes(), proto_idx, NumResolvedMethodTypes()).Read();
91}
92
93inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) {
94 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
Narayan Kamath42b3dd02016-10-03 11:33:01 +010095 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
Narayan Kamath25352fc2016-08-03 12:46:58 +010096
Narayan Kamath23136d12016-09-30 16:29:19 +010097 MethodTypeDexCachePair::Assign(GetResolvedMethodTypes(), proto_idx, resolved,
98 NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010099 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
100 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
101}
102
Andreas Gampe542451c2016-07-26 09:02:02 -0700103inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100105 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
106 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700107 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
108 return nullptr;
109 }
110 return field;
111}
112
Andreas Gampe542451c2016-07-26 09:02:02 -0700113inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700114 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100115 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
116 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117}
118
Andreas Gampe542451c2016-07-26 09:02:02 -0700119inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100121 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
122 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 // Hide resolution trampoline methods from the caller
124 if (method != nullptr && method->IsRuntimeMethod()) {
125 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
126 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700127 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128 return method;
129}
130
Andreas Gampe542451c2016-07-26 09:02:02 -0700131inline void DexCache::SetResolvedMethod(uint32_t method_idx,
132 ArtMethod* method,
133 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100135 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
136 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
137}
138
139template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700140inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
141 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
143 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
144 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100145 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
146 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
147 }
148}
149
150template <typename PtrType>
151inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
152 size_t idx,
153 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700154 PointerSize ptr_size) {
155 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100156 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
157 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
158 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100159 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
160 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
161 }
162}
163
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100164template <typename T,
165 ReadBarrierOption kReadBarrierOption,
166 typename Visitor>
167inline void VisitDexCachePairs(std::atomic<DexCachePair<T>>* pairs,
168 size_t num_pairs,
169 const Visitor& visitor)
170 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
171 for (size_t i = 0; i < num_pairs; ++i) {
172 DexCachePair<T> source = pairs[i].load(std::memory_order_relaxed);
173 // NOTE: We need the "template" keyword here to avoid a compilation
174 // failure. GcRoot<T> is a template argument-dependent type and we need to
175 // tell the compiler to treat "Read" as a template rather than a field or
176 // function. Otherwise, on encountering the "<" token, the compiler would
177 // treat "Read" as a field.
Mathieu Chartier6b4c2872016-11-01 14:45:26 -0700178 T* const before = source.object.template Read<kReadBarrierOption>();
179 visitor.VisitRootIfNonNull(source.object.AddressWithoutBarrier());
180 if (source.object.template Read<kReadBarrierOption>() != before) {
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100181 pairs[i].store(source, std::memory_order_relaxed);
182 }
183 }
184}
185
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800186template <bool kVisitNativeRoots,
187 VerifyObjectFlags kVerifyFlags,
188 ReadBarrierOption kReadBarrierOption,
189 typename Visitor>
Mathieu Chartier31e88222016-10-14 18:43:19 -0700190inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100191 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800192 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100193 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800194 if (kVisitNativeRoots) {
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100195 VisitDexCachePairs<mirror::String, kReadBarrierOption, Visitor>(
196 GetStrings(), NumStrings(), visitor);
197
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800198 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
199 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
200 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
201 }
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100202
203 VisitDexCachePairs<mirror::MethodType, kReadBarrierOption, Visitor>(
204 GetResolvedMethodTypes(), NumResolvedMethodTypes(), visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100205 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700206}
207
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800208template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700209inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
210 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800211 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700212 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100213 mirror::String* ptr = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700214 mirror::String* new_source = visitor(ptr);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100215 source.object = GcRoot<String>(new_source);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700216 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800217 }
218}
219
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800220template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800221inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
222 GcRoot<mirror::Class>* src = GetResolvedTypes();
223 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800224 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800225 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100226 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800227 }
228}
229
Narayan Kamath7fe56582016-10-14 18:49:12 +0100230template <ReadBarrierOption kReadBarrierOption, typename Visitor>
231inline void DexCache::FixupResolvedMethodTypes(mirror::MethodTypeDexCacheType* dest,
232 const Visitor& visitor) {
233 mirror::MethodTypeDexCacheType* src = GetResolvedMethodTypes();
234 for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) {
235 MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed);
236 mirror::MethodType* ptr = source.object.Read<kReadBarrierOption>();
237 mirror::MethodType* new_source = visitor(ptr);
238 source.object = GcRoot<MethodType>(new_source);
239 dest[i].store(source, std::memory_order_relaxed);
240 }
241}
242
Ian Rogers39ebcb82013-05-30 16:57:23 -0700243} // namespace mirror
244} // namespace art
245
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700246#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_