blob: 13d0108b2fd5d9bb3518a89a56c5acf78c3c1ca0 [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"
27#include "mirror/class.h"
Narayan Kamath25352fc2016-08-03 12:46:58 +010028#include "mirror/method_type.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070029#include "runtime.h"
Mathieu Chartier31e88222016-10-14 18:43:19 -070030#include "obj_ptr.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070031
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070032#include <atomic>
33
Ian Rogers39ebcb82013-05-30 16:57:23 -070034namespace art {
35namespace mirror {
36
Andreas Gampe542451c2016-07-26 09:02:02 -070037inline uint32_t DexCache::ClassSize(PointerSize pointer_size) {
Vladimir Markoc1363122015-04-09 14:13:13 +010038 uint32_t vtable_entries = Object::kVTableLength + 5;
Mathieu Chartiere401d142015-04-22 13:56:20 -070039 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
Ian Rogers39ebcb82013-05-30 16:57:23 -070040}
41
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070042inline mirror::String* DexCache::GetResolvedString(uint32_t string_idx) {
43 DCHECK_LT(string_idx, GetDexFile()->NumStringIds());
Narayan Kamathc38a6f82016-09-29 17:07:20 +010044 return StringDexCachePair::Lookup(GetStrings(), string_idx, NumStrings()).Read();
Andreas Gampeaa910d52014-07-30 18:59:05 -070045}
46
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070047inline void DexCache::SetResolvedString(uint32_t string_idx, mirror::String* resolved) {
Narayan Kamath23136d12016-09-30 16:29:19 +010048 StringDexCachePair::Assign(GetStrings(), string_idx, resolved, NumStrings());
Mathieu Chartierbb816d62016-09-07 10:17:46 -070049 Runtime* const runtime = Runtime::Current();
50 if (UNLIKELY(runtime->IsActiveTransaction())) {
51 DCHECK(runtime->IsAotCompiler());
52 runtime->RecordResolveString(this, string_idx);
53 }
Vladimir Marko05792b92015-08-03 11:56:49 +010054 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
Mathieu Chartierbb816d62016-09-07 10:17:46 -070055 runtime->GetHeap()->WriteBarrierEveryFieldOf(this);
56}
57
58inline void DexCache::ClearString(uint32_t string_idx) {
59 const uint32_t slot_idx = string_idx % NumStrings();
60 DCHECK(Runtime::Current()->IsAotCompiler());
61 StringDexCacheType* slot = &GetStrings()[slot_idx];
62 // This is racy but should only be called from the transactional interpreter.
Narayan Kamathc38a6f82016-09-29 17:07:20 +010063 if (slot->load(std::memory_order_relaxed).index == string_idx) {
Mathieu Chartierbb816d62016-09-07 10:17:46 -070064 StringDexCachePair cleared(
65 nullptr,
Narayan Kamathc38a6f82016-09-29 17:07:20 +010066 StringDexCachePair::InvalidIndexForSlot(slot_idx));
Mathieu Chartierbb816d62016-09-07 10:17:46 -070067 slot->store(cleared, std::memory_order_relaxed);
68 }
Vladimir Marko05792b92015-08-03 11:56:49 +010069}
70
71inline Class* DexCache::GetResolvedType(uint32_t type_idx) {
72 DCHECK_LT(type_idx, NumResolvedTypes());
73 return GetResolvedTypes()[type_idx].Read();
74}
75
Mathieu Chartier31e88222016-10-14 18:43:19 -070076inline void DexCache::SetResolvedType(uint32_t type_idx, ObjPtr<Class> resolved) {
Vladimir Marko05792b92015-08-03 11:56:49 +010077 DCHECK_LT(type_idx, NumResolvedTypes()); // NOTE: Unchecked, i.e. not throwing AIOOB.
78 // TODO default transaction support.
Vladimir Marko05792b92015-08-03 11:56:49 +010079 GetResolvedTypes()[type_idx] = GcRoot<Class>(resolved);
80 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
81 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
82}
83
Narayan Kamath25352fc2016-08-03 12:46:58 +010084inline MethodType* DexCache::GetResolvedMethodType(uint32_t proto_idx) {
85 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
86 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
87 return MethodTypeDexCachePair::Lookup(
88 GetResolvedMethodTypes(), proto_idx, NumResolvedMethodTypes()).Read();
89}
90
91inline void DexCache::SetResolvedMethodType(uint32_t proto_idx, MethodType* resolved) {
92 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
Narayan Kamath42b3dd02016-10-03 11:33:01 +010093 DCHECK_LT(proto_idx, GetDexFile()->NumProtoIds());
Narayan Kamath25352fc2016-08-03 12:46:58 +010094
Narayan Kamath23136d12016-09-30 16:29:19 +010095 MethodTypeDexCachePair::Assign(GetResolvedMethodTypes(), proto_idx, resolved,
96 NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010097 // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
98 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(this);
99}
100
Andreas Gampe542451c2016-07-26 09:02:02 -0700101inline ArtField* DexCache::GetResolvedField(uint32_t field_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100103 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
104 ArtField* field = GetElementPtrSize(GetResolvedFields(), field_idx, ptr_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700105 if (field == nullptr || field->GetDeclaringClass()->IsErroneous()) {
106 return nullptr;
107 }
108 return field;
109}
110
Andreas Gampe542451c2016-07-26 09:02:02 -0700111inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100113 DCHECK_LT(field_idx, NumResolvedFields()); // NOTE: Unchecked, i.e. not throwing AIOOB.
114 SetElementPtrSize(GetResolvedFields(), field_idx, field, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115}
116
Andreas Gampe542451c2016-07-26 09:02:02 -0700117inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100119 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
120 ArtMethod* method = GetElementPtrSize<ArtMethod*>(GetResolvedMethods(), method_idx, ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 // Hide resolution trampoline methods from the caller
122 if (method != nullptr && method->IsRuntimeMethod()) {
123 DCHECK_EQ(method, Runtime::Current()->GetResolutionMethod());
124 return nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700125 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 return method;
127}
128
Andreas Gampe542451c2016-07-26 09:02:02 -0700129inline void DexCache::SetResolvedMethod(uint32_t method_idx,
130 ArtMethod* method,
131 PointerSize ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100133 DCHECK_LT(method_idx, NumResolvedMethods()); // NOTE: Unchecked, i.e. not throwing AIOOB.
134 SetElementPtrSize(GetResolvedMethods(), method_idx, method, ptr_size);
135}
136
137template <typename PtrType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700138inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
139 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100140 uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
141 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
142 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100143 uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
144 return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
145 }
146}
147
148template <typename PtrType>
149inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
150 size_t idx,
151 PtrType ptr,
Andreas Gampe542451c2016-07-26 09:02:02 -0700152 PointerSize ptr_size) {
153 if (ptr_size == PointerSize::k64) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100154 reinterpret_cast<uint64_t*>(ptr_array)[idx] =
155 dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
156 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +0100157 reinterpret_cast<uint32_t*>(ptr_array)[idx] =
158 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
159 }
160}
161
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800162template <bool kVisitNativeRoots,
163 VerifyObjectFlags kVerifyFlags,
164 ReadBarrierOption kReadBarrierOption,
165 typename Visitor>
Mathieu Chartier31e88222016-10-14 18:43:19 -0700166inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100167 // Visit instance fields first.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800168 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
Vladimir Marko05792b92015-08-03 11:56:49 +0100169 // Visit arrays after.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800170 if (kVisitNativeRoots) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700171 mirror::StringDexCacheType* strings = GetStrings();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800172 for (size_t i = 0, num_strings = NumStrings(); i != num_strings; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700173 StringDexCachePair source = strings[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100174 mirror::String* before = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700175 GcRoot<mirror::String> root(before);
176 visitor.VisitRootIfNonNull(root.AddressWithoutBarrier());
177 if (root.Read() != before) {
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100178 source.object = GcRoot<String>(root.Read());
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700179 strings[i].store(source, std::memory_order_relaxed);
180 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800181 }
182 GcRoot<mirror::Class>* resolved_types = GetResolvedTypes();
183 for (size_t i = 0, num_types = NumResolvedTypes(); i != num_types; ++i) {
184 visitor.VisitRootIfNonNull(resolved_types[i].AddressWithoutBarrier());
185 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100186 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700187}
188
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800189template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700190inline void DexCache::FixupStrings(mirror::StringDexCacheType* dest, const Visitor& visitor) {
191 mirror::StringDexCacheType* src = GetStrings();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800192 for (size_t i = 0, count = NumStrings(); i < count; ++i) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700193 StringDexCachePair source = src[i].load(std::memory_order_relaxed);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100194 mirror::String* ptr = source.object.Read<kReadBarrierOption>();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700195 mirror::String* new_source = visitor(ptr);
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100196 source.object = GcRoot<String>(new_source);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700197 dest[i].store(source, std::memory_order_relaxed);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800198 }
199}
200
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800201template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800202inline void DexCache::FixupResolvedTypes(GcRoot<mirror::Class>* dest, const Visitor& visitor) {
203 GcRoot<mirror::Class>* src = GetResolvedTypes();
204 for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
Mathieu Chartier60bc39c2016-01-27 18:37:48 -0800205 mirror::Class* source = src[i].Read<kReadBarrierOption>();
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800206 mirror::Class* new_source = visitor(source);
Nicolas Geoffrayd0668f22016-04-26 18:30:31 +0100207 dest[i] = GcRoot<mirror::Class>(new_source);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800208 }
209}
210
Ian Rogers39ebcb82013-05-30 16:57:23 -0700211} // namespace mirror
212} // namespace art
213
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700214#endif // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_