blob: b54e416bc020b66f82bc3c2d9246dd1dff9396ef [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) {
Mathieu Chartier5df32d32016-12-06 16:02:27 -080074 // It is theorized that a load acquire is not required since obtaining the resolved class will
75 // always have an address depedency or a lock.
Andreas Gampea5b09a62016-11-17 15:21:22 -080076 DCHECK_LT(type_idx.index_, NumResolvedTypes());
77 return GetResolvedTypes()[type_idx.index_].Read();
Vladimir Marko05792b92015-08-03 11:56:49 +010078}
79
Andreas Gampea5b09a62016-11-17 15:21:22 -080080inline void DexCache::SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved) {
81 DCHECK_LT(type_idx.index_, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB.
Vladimir Marko05792b92015-08-03 11:56:49 +010082 // TODO default transaction support.
Mathieu Chartier5df32d32016-12-06 16:02:27 -080083 // Use a release store for SetResolvedType. This is done to prevent other threads from seeing a
84 // class but not necessarily seeing the loaded members like the static fields array.
85 // See b/32075261.
86 reinterpret_cast<Atomic<GcRoot<mirror::Class>>&>(GetResolvedTypes()[type_idx.index_]).
87 StoreRelease(GcRoot<Class>(resolved));
Vladimir Marko05792b92015-08-03 11:56:49 +010088 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
89 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
90}
91
Narayan Kamath25352fc2016-08-03 12:46:58 +010092inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) {
93 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
94 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
95 return MethodTypeDexCachePair::Lookup(
96 GetResolvedMethodTypes(), proto_idx, NumResolvedMethodTypes()).Read();
97}
98
99inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) {
100 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
Narayan Kamath42b3dd02016-10-03 11:33:01 +0100101 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100102
Narayan Kamath23136d12016-09-30 16:29:19 +0100103 MethodTypeDexCachePair::Assign(GetResolvedMethodTypes(), proto_idx, resolved,
104 NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100105 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
106 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
107}
108
Andreas Gampe542451c2016-07-26 09:02:02 -0700109inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100111 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
112 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700113 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
114 return nullptr;
115 }
116 return field;
117}
118
Andreas Gampe542451c2016-07-26 09:02:02 -0700119inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, 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(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
122 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123}
124
Andreas Gampe542451c2016-07-26 09:02:02 -0700125inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100127 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
128 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 // Hide resolution trampoline methods from the caller
130 if (method != nullptr && method->IsRuntimeMethod()) {
131 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
132 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700133 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 return method;
135}
136
Andreas Gampe542451c2016-07-26 09:02:02 -0700137inline void DexCache::SetResolvedMethod(uint32_t method_idx,
138 ArtMethod* method,
139 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100141 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
142 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
143}
144
145template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700146inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
147 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100148 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
149 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
150 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100151 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
152 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
153 }
154}
155
156template <typename PtrType>
157inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
158 size_t idx,
159 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700160 PointerSize ptr_size) {
161 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100162 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
163 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
164 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100165 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
166 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
167 }
168}
169
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100170template <typename T,
171 ReadBarrierOption kReadBarrierOption,
172 typename Visitor>
173inline void VisitDexCachePairs(std::atomic<DexCachePair<T>>* pairs,
174 size_t num_pairs,
175 const Visitor& visitor)
176 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
177 for (size_t i = 0; i < num_pairs; ++i) {
178 DexCachePair<T> source = pairs[i].load(std::memory_order_relaxed);
179 // NOTE: We need the "template" keyword here to avoid a compilation
180 // failure. GcRoot<T> is a template argument-dependent type and we need to
181 // tell the compiler to treat "Read" as a template rather than a field or
182 // function. Otherwise, on encountering the "<" token, the compiler would
183 // treat "Read" as a field.
Mathieu Chartier6b4c2872016-11-01 14:45:26 -0700184 T* const before = source.object.template Read<kReadBarrierOption>();
185 visitor.VisitRootIfNonNull(source.object.AddressWithoutBarrier());
186 if (source.object.template Read<kReadBarrierOption>() != before) {
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100187 pairs[i].store(source, std::memory_order_relaxed);
188 }
189 }
190}
191
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800192template <bool kVisitNativeRoots,
193 VerifyObjectFlags kVerifyFlags,
194 ReadBarrierOption kReadBarrierOption,
195 typename Visitor>
Mathieu Chartier31e88222016-10-14 18:43:19 -0700196inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100197 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800198 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100199 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800200 if (kVisitNativeRoots) {
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100201 VisitDexCachePairs<mirror::String, kReadBarrierOption, Visitor>(
202 GetStrings(), NumStrings(), visitor);
203
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800204 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
205 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
206 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
207 }
Narayan Kamathd08e39b2016-10-19 14:16:35 +0100208
209 VisitDexCachePairs<mirror::MethodType, kReadBarrierOption, Visitor>(
210 GetResolvedMethodTypes(), NumResolvedMethodTypes(), visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100211 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700212}
213
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800214template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700215inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
216 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800217 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700218 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100219 mirror::String* ptr = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700220 mirror::String* new_source = visitor(ptr);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100221 source.object = GcRoot<String>(new_source);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700222 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800223 }
224}
225
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800226template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800227inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
228 GcRoot<mirror::Class>* src = GetResolvedTypes();
229 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800230 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800231 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100232 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800233 }
234}
235
Narayan Kamath7fe56582016-10-14 18:49:12 +0100236template <ReadBarrierOption kReadBarrierOption, typename Visitor>
237inline void DexCache::FixupResolvedMethodTypes(mirror::MethodTypeDexCacheType* dest,
238 const Visitor& visitor) {
239 mirror::MethodTypeDexCacheType* src = GetResolvedMethodTypes();
240 for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) {
241 MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed);
242 mirror::MethodType* ptr = source.object.Read<kReadBarrierOption>();
243 mirror::MethodType* new_source = visitor(ptr);
244 source.object = GcRoot<MethodType>(new_source);
245 dest[i].store(source, std::memory_order_relaxed);
246 }
247}
248
Ian Rogers39ebcb82013-05-30 16:57:23 -0700249} // namespace mirror
250} // namespace art
251
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700252#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_