• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
18 #define ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
19 
20 #include "dex_cache.h"
21 
22 #include <android-base/logging.h>
23 
24 #include "art_field.h"
25 #include "art_method.h"
26 #include "base/casts.h"
27 #include "base/enums.h"
28 #include "class_linker.h"
29 #include "dex/dex_file.h"
30 #include "gc_root-inl.h"
31 #include "mirror/call_site.h"
32 #include "mirror/class.h"
33 #include "mirror/method_type.h"
34 #include "obj_ptr.h"
35 #include "object-inl.h"
36 #include "runtime.h"
37 #include "write_barrier-inl.h"
38 
39 #include <atomic>
40 
41 namespace art {
42 namespace mirror {
43 
44 template <typename T>
DexCachePair(ObjPtr<T> object,uint32_t index)45 inline DexCachePair<T>::DexCachePair(ObjPtr<T> object, uint32_t index)
46     : object(object), index(index) {}
47 
48 template <typename T>
Initialize(std::atomic<DexCachePair<T>> * dex_cache)49 inline void DexCachePair<T>::Initialize(std::atomic<DexCachePair<T>>* dex_cache) {
50   DexCachePair<T> first_elem;
51   first_elem.object = GcRoot<T>(nullptr);
52   first_elem.index = InvalidIndexForSlot(0);
53   dex_cache[0].store(first_elem, std::memory_order_relaxed);
54 }
55 
56 template <typename T>
GetObjectForIndex(uint32_t idx)57 inline T* DexCachePair<T>::GetObjectForIndex(uint32_t idx) {
58   if (idx != index) {
59     return nullptr;
60   }
61   DCHECK(!object.IsNull());
62   return object.Read();
63 }
64 
65 template <typename T>
Initialize(std::atomic<NativeDexCachePair<T>> * dex_cache)66 inline void NativeDexCachePair<T>::Initialize(std::atomic<NativeDexCachePair<T>>* dex_cache) {
67   NativeDexCachePair<T> first_elem;
68   first_elem.object = nullptr;
69   first_elem.index = InvalidIndexForSlot(0);
70   DexCache::SetNativePair(dex_cache, 0, first_elem);
71 }
72 
ClassSize(PointerSize pointer_size)73 inline uint32_t DexCache::ClassSize(PointerSize pointer_size) {
74   const uint32_t vtable_entries = Object::kVTableLength;
75   return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
76 }
77 
StringSlotIndex(dex::StringIndex string_idx)78 inline uint32_t DexCache::StringSlotIndex(dex::StringIndex string_idx) {
79   DCHECK_LT(string_idx.index_, GetDexFile()->NumStringIds());
80   const uint32_t slot_idx = string_idx.index_ % kDexCacheStringCacheSize;
81   DCHECK_LT(slot_idx, NumStrings());
82   return slot_idx;
83 }
84 
GetResolvedString(dex::StringIndex string_idx)85 inline String* DexCache::GetResolvedString(dex::StringIndex string_idx) {
86   const uint32_t num_preresolved_strings = NumPreResolvedStrings();
87   if (num_preresolved_strings != 0u) {
88     GcRoot<mirror::String>* preresolved_strings = GetPreResolvedStrings();
89     // num_preresolved_strings can become 0 and preresolved_strings can become null in any order
90     // when ClearPreResolvedStrings is called.
91     if (preresolved_strings != nullptr) {
92       DCHECK_LT(string_idx.index_, num_preresolved_strings);
93       DCHECK_EQ(num_preresolved_strings, GetDexFile()->NumStringIds());
94       mirror::String* string = preresolved_strings[string_idx.index_].Read();
95       if (LIKELY(string != nullptr)) {
96         return string;
97       }
98     }
99   }
100   return GetStrings()[StringSlotIndex(string_idx)].load(
101       std::memory_order_relaxed).GetObjectForIndex(string_idx.index_);
102 }
103 
SetResolvedString(dex::StringIndex string_idx,ObjPtr<String> resolved)104 inline void DexCache::SetResolvedString(dex::StringIndex string_idx, ObjPtr<String> resolved) {
105   DCHECK(resolved != nullptr);
106   GetStrings()[StringSlotIndex(string_idx)].store(
107       StringDexCachePair(resolved, string_idx.index_), std::memory_order_relaxed);
108   Runtime* const runtime = Runtime::Current();
109   if (UNLIKELY(runtime->IsActiveTransaction())) {
110     DCHECK(runtime->IsAotCompiler());
111     runtime->RecordResolveString(this, string_idx);
112   }
113   // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
114   WriteBarrier::ForEveryFieldWrite(this);
115 }
116 
SetPreResolvedString(dex::StringIndex string_idx,ObjPtr<String> resolved)117 inline void DexCache::SetPreResolvedString(dex::StringIndex string_idx, ObjPtr<String> resolved) {
118   DCHECK(resolved != nullptr);
119   DCHECK_LT(string_idx.index_, GetDexFile()->NumStringIds());
120   GetPreResolvedStrings()[string_idx.index_] = GcRoot<mirror::String>(resolved);
121   Runtime* const runtime = Runtime::Current();
122   CHECK(runtime->IsAotCompiler());
123   CHECK(!runtime->IsActiveTransaction());
124   // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
125   WriteBarrier::ForEveryFieldWrite(this);
126 }
127 
ClearPreResolvedStrings()128 inline void DexCache::ClearPreResolvedStrings() {
129   SetFieldPtr64</*kTransactionActive=*/false,
130                 /*kCheckTransaction=*/false,
131                 kVerifyNone,
132                 GcRoot<mirror::String>*>(PreResolvedStringsOffset(), nullptr);
133   SetField32</*kTransactionActive=*/false,
134              /*bool kCheckTransaction=*/false,
135              kVerifyNone,
136              /*kIsVolatile=*/false>(NumPreResolvedStringsOffset(), 0);
137 }
138 
ClearString(dex::StringIndex string_idx)139 inline void DexCache::ClearString(dex::StringIndex string_idx) {
140   DCHECK(Runtime::Current()->IsAotCompiler());
141   uint32_t slot_idx = StringSlotIndex(string_idx);
142   StringDexCacheType* slot = &GetStrings()[slot_idx];
143   // This is racy but should only be called from the transactional interpreter.
144   if (slot->load(std::memory_order_relaxed).index == string_idx.index_) {
145     StringDexCachePair cleared(nullptr, StringDexCachePair::InvalidIndexForSlot(slot_idx));
146     slot->store(cleared, std::memory_order_relaxed);
147   }
148 }
149 
TypeSlotIndex(dex::TypeIndex type_idx)150 inline uint32_t DexCache::TypeSlotIndex(dex::TypeIndex type_idx) {
151   DCHECK_LT(type_idx.index_, GetDexFile()->NumTypeIds());
152   const uint32_t slot_idx = type_idx.index_ % kDexCacheTypeCacheSize;
153   DCHECK_LT(slot_idx, NumResolvedTypes());
154   return slot_idx;
155 }
156 
GetResolvedType(dex::TypeIndex type_idx)157 inline Class* DexCache::GetResolvedType(dex::TypeIndex type_idx) {
158   // It is theorized that a load acquire is not required since obtaining the resolved class will
159   // always have an address dependency or a lock.
160   return GetResolvedTypes()[TypeSlotIndex(type_idx)].load(
161       std::memory_order_relaxed).GetObjectForIndex(type_idx.index_);
162 }
163 
SetResolvedType(dex::TypeIndex type_idx,ObjPtr<Class> resolved)164 inline void DexCache::SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved) {
165   DCHECK(resolved != nullptr);
166   DCHECK(resolved->IsResolved()) << resolved->GetStatus();
167   // TODO default transaction support.
168   // Use a release store for SetResolvedType. This is done to prevent other threads from seeing a
169   // class but not necessarily seeing the loaded members like the static fields array.
170   // See b/32075261.
171   GetResolvedTypes()[TypeSlotIndex(type_idx)].store(
172       TypeDexCachePair(resolved, type_idx.index_), std::memory_order_release);
173   // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
174   WriteBarrier::ForEveryFieldWrite(this);
175 }
176 
ClearResolvedType(dex::TypeIndex type_idx)177 inline void DexCache::ClearResolvedType(dex::TypeIndex type_idx) {
178   DCHECK(Runtime::Current()->IsAotCompiler());
179   uint32_t slot_idx = TypeSlotIndex(type_idx);
180   TypeDexCacheType* slot = &GetResolvedTypes()[slot_idx];
181   // This is racy but should only be called from the single-threaded ImageWriter and tests.
182   if (slot->load(std::memory_order_relaxed).index == type_idx.index_) {
183     TypeDexCachePair cleared(nullptr, TypeDexCachePair::InvalidIndexForSlot(slot_idx));
184     slot->store(cleared, std::memory_order_relaxed);
185   }
186 }
187 
MethodTypeSlotIndex(dex::ProtoIndex proto_idx)188 inline uint32_t DexCache::MethodTypeSlotIndex(dex::ProtoIndex proto_idx) {
189   DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
190   DCHECK_LT(proto_idx.index_, GetDexFile()->NumProtoIds());
191   const uint32_t slot_idx = proto_idx.index_ % kDexCacheMethodTypeCacheSize;
192   DCHECK_LT(slot_idx, NumResolvedMethodTypes());
193   return slot_idx;
194 }
195 
GetResolvedMethodType(dex::ProtoIndex proto_idx)196 inline MethodType* DexCache::GetResolvedMethodType(dex::ProtoIndex proto_idx) {
197   return GetResolvedMethodTypes()[MethodTypeSlotIndex(proto_idx)].load(
198       std::memory_order_relaxed).GetObjectForIndex(proto_idx.index_);
199 }
200 
SetResolvedMethodType(dex::ProtoIndex proto_idx,MethodType * resolved)201 inline void DexCache::SetResolvedMethodType(dex::ProtoIndex proto_idx, MethodType* resolved) {
202   DCHECK(resolved != nullptr);
203   GetResolvedMethodTypes()[MethodTypeSlotIndex(proto_idx)].store(
204       MethodTypeDexCachePair(resolved, proto_idx.index_), std::memory_order_relaxed);
205   // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
206   WriteBarrier::ForEveryFieldWrite(this);
207 }
208 
GetResolvedCallSite(uint32_t call_site_idx)209 inline CallSite* DexCache::GetResolvedCallSite(uint32_t call_site_idx) {
210   DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
211   DCHECK_LT(call_site_idx, GetDexFile()->NumCallSiteIds());
212   GcRoot<mirror::CallSite>& target = GetResolvedCallSites()[call_site_idx];
213   Atomic<GcRoot<mirror::CallSite>>& ref =
214       reinterpret_cast<Atomic<GcRoot<mirror::CallSite>>&>(target);
215   return ref.load(std::memory_order_seq_cst).Read();
216 }
217 
SetResolvedCallSite(uint32_t call_site_idx,ObjPtr<CallSite> call_site)218 inline ObjPtr<CallSite> DexCache::SetResolvedCallSite(uint32_t call_site_idx,
219                                                       ObjPtr<CallSite> call_site) {
220   DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
221   DCHECK_LT(call_site_idx, GetDexFile()->NumCallSiteIds());
222 
223   GcRoot<mirror::CallSite> null_call_site(nullptr);
224   GcRoot<mirror::CallSite> candidate(call_site);
225   GcRoot<mirror::CallSite>& target = GetResolvedCallSites()[call_site_idx];
226 
227   // The first assignment for a given call site wins.
228   Atomic<GcRoot<mirror::CallSite>>& ref =
229       reinterpret_cast<Atomic<GcRoot<mirror::CallSite>>&>(target);
230   if (ref.CompareAndSetStrongSequentiallyConsistent(null_call_site, candidate)) {
231     // TODO: Fine-grained marking, so that we don't need to go through all arrays in full.
232     WriteBarrier::ForEveryFieldWrite(this);
233     return call_site;
234   } else {
235     return target.Read();
236   }
237 }
238 
FieldSlotIndex(uint32_t field_idx)239 inline uint32_t DexCache::FieldSlotIndex(uint32_t field_idx) {
240   DCHECK_LT(field_idx, GetDexFile()->NumFieldIds());
241   const uint32_t slot_idx = field_idx % kDexCacheFieldCacheSize;
242   DCHECK_LT(slot_idx, NumResolvedFields());
243   return slot_idx;
244 }
245 
GetResolvedField(uint32_t field_idx)246 inline ArtField* DexCache::GetResolvedField(uint32_t field_idx) {
247   auto pair = GetNativePair(GetResolvedFields(), FieldSlotIndex(field_idx));
248   return pair.GetObjectForIndex(field_idx);
249 }
250 
SetResolvedField(uint32_t field_idx,ArtField * field)251 inline void DexCache::SetResolvedField(uint32_t field_idx, ArtField* field) {
252   DCHECK(field != nullptr);
253   FieldDexCachePair pair(field, field_idx);
254   SetNativePair(GetResolvedFields(), FieldSlotIndex(field_idx), pair);
255 }
256 
MethodSlotIndex(uint32_t method_idx)257 inline uint32_t DexCache::MethodSlotIndex(uint32_t method_idx) {
258   DCHECK_LT(method_idx, GetDexFile()->NumMethodIds());
259   const uint32_t slot_idx = method_idx % kDexCacheMethodCacheSize;
260   DCHECK_LT(slot_idx, NumResolvedMethods());
261   return slot_idx;
262 }
263 
GetResolvedMethod(uint32_t method_idx)264 inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx) {
265   auto pair = GetNativePair(GetResolvedMethods(), MethodSlotIndex(method_idx));
266   return pair.GetObjectForIndex(method_idx);
267 }
268 
SetResolvedMethod(uint32_t method_idx,ArtMethod * method)269 inline void DexCache::SetResolvedMethod(uint32_t method_idx, ArtMethod* method) {
270   DCHECK(method != nullptr);
271   MethodDexCachePair pair(method, method_idx);
272   SetNativePair(GetResolvedMethods(), MethodSlotIndex(method_idx), pair);
273 }
274 
275 template <typename T>
GetNativePair(std::atomic<NativeDexCachePair<T>> * pair_array,size_t idx)276 NativeDexCachePair<T> DexCache::GetNativePair(std::atomic<NativeDexCachePair<T>>* pair_array,
277                                               size_t idx) {
278   if (kRuntimePointerSize == PointerSize::k64) {
279     auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array);
280     ConversionPair64 value = AtomicLoadRelaxed16B(&array[idx]);
281     return NativeDexCachePair<T>(reinterpret_cast64<T*>(value.first),
282                                  dchecked_integral_cast<size_t>(value.second));
283   } else {
284     auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array);
285     ConversionPair32 value = array[idx].load(std::memory_order_relaxed);
286     return NativeDexCachePair<T>(reinterpret_cast32<T*>(value.first), value.second);
287   }
288 }
289 
290 template <typename T>
SetNativePair(std::atomic<NativeDexCachePair<T>> * pair_array,size_t idx,NativeDexCachePair<T> pair)291 void DexCache::SetNativePair(std::atomic<NativeDexCachePair<T>>* pair_array,
292                              size_t idx,
293                              NativeDexCachePair<T> pair) {
294   if (kRuntimePointerSize == PointerSize::k64) {
295     auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array);
296     ConversionPair64 v(reinterpret_cast64<uint64_t>(pair.object), pair.index);
297     AtomicStoreRelease16B(&array[idx], v);
298   } else {
299     auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array);
300     ConversionPair32 v(reinterpret_cast32<uint32_t>(pair.object),
301                        dchecked_integral_cast<uint32_t>(pair.index));
302     array[idx].store(v, std::memory_order_release);
303   }
304 }
305 
306 template <typename T,
307           ReadBarrierOption kReadBarrierOption,
308           typename Visitor>
VisitDexCachePairs(std::atomic<DexCachePair<T>> * pairs,size_t num_pairs,const Visitor & visitor)309 inline void VisitDexCachePairs(std::atomic<DexCachePair<T>>* pairs,
310                                size_t num_pairs,
311                                const Visitor& visitor)
312     REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
313   for (size_t i = 0; i < num_pairs; ++i) {
314     DexCachePair<T> source = pairs[i].load(std::memory_order_relaxed);
315     // NOTE: We need the "template" keyword here to avoid a compilation
316     // failure. GcRoot<T> is a template argument-dependent type and we need to
317     // tell the compiler to treat "Read" as a template rather than a field or
318     // function. Otherwise, on encountering the "<" token, the compiler would
319     // treat "Read" as a field.
320     T* const before = source.object.template Read<kReadBarrierOption>();
321     visitor.VisitRootIfNonNull(source.object.AddressWithoutBarrier());
322     if (source.object.template Read<kReadBarrierOption>() != before) {
323       pairs[i].store(source, std::memory_order_relaxed);
324     }
325   }
326 }
327 
328 template <bool kVisitNativeRoots,
329           VerifyObjectFlags kVerifyFlags,
330           ReadBarrierOption kReadBarrierOption,
331           typename Visitor>
VisitReferences(ObjPtr<Class> klass,const Visitor & visitor)332 inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) {
333   // Visit instance fields first.
334   VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
335   // Visit arrays after.
336   if (kVisitNativeRoots) {
337     VisitDexCachePairs<String, kReadBarrierOption, Visitor>(
338         GetStrings<kVerifyFlags>(), NumStrings<kVerifyFlags>(), visitor);
339 
340     VisitDexCachePairs<Class, kReadBarrierOption, Visitor>(
341         GetResolvedTypes<kVerifyFlags>(), NumResolvedTypes<kVerifyFlags>(), visitor);
342 
343     VisitDexCachePairs<MethodType, kReadBarrierOption, Visitor>(
344         GetResolvedMethodTypes<kVerifyFlags>(), NumResolvedMethodTypes<kVerifyFlags>(), visitor);
345 
346     GcRoot<mirror::CallSite>* resolved_call_sites = GetResolvedCallSites<kVerifyFlags>();
347     size_t num_call_sites = NumResolvedCallSites<kVerifyFlags>();
348     for (size_t i = 0; i != num_call_sites; ++i) {
349       visitor.VisitRootIfNonNull(resolved_call_sites[i].AddressWithoutBarrier());
350     }
351 
352     GcRoot<mirror::String>* const preresolved_strings = GetPreResolvedStrings();
353     if (preresolved_strings != nullptr) {
354       const size_t num_preresolved_strings = NumPreResolvedStrings();
355       for (size_t i = 0; i != num_preresolved_strings; ++i) {
356         visitor.VisitRootIfNonNull(preresolved_strings[i].AddressWithoutBarrier());
357       }
358     }
359   }
360 }
361 
362 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
FixupStrings(StringDexCacheType * dest,const Visitor & visitor)363 inline void DexCache::FixupStrings(StringDexCacheType* dest, const Visitor& visitor) {
364   StringDexCacheType* src = GetStrings();
365   for (size_t i = 0, count = NumStrings(); i < count; ++i) {
366     StringDexCachePair source = src[i].load(std::memory_order_relaxed);
367     String* ptr = source.object.Read<kReadBarrierOption>();
368     String* new_source = visitor(ptr);
369     source.object = GcRoot<String>(new_source);
370     dest[i].store(source, std::memory_order_relaxed);
371   }
372 }
373 
374 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
FixupResolvedTypes(TypeDexCacheType * dest,const Visitor & visitor)375 inline void DexCache::FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor) {
376   TypeDexCacheType* src = GetResolvedTypes();
377   for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
378     TypeDexCachePair source = src[i].load(std::memory_order_relaxed);
379     Class* ptr = source.object.Read<kReadBarrierOption>();
380     Class* new_source = visitor(ptr);
381     source.object = GcRoot<Class>(new_source);
382     dest[i].store(source, std::memory_order_relaxed);
383   }
384 }
385 
386 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
FixupResolvedMethodTypes(MethodTypeDexCacheType * dest,const Visitor & visitor)387 inline void DexCache::FixupResolvedMethodTypes(MethodTypeDexCacheType* dest,
388                                                const Visitor& visitor) {
389   MethodTypeDexCacheType* src = GetResolvedMethodTypes();
390   for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) {
391     MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed);
392     MethodType* ptr = source.object.Read<kReadBarrierOption>();
393     MethodType* new_source = visitor(ptr);
394     source.object = GcRoot<MethodType>(new_source);
395     dest[i].store(source, std::memory_order_relaxed);
396   }
397 }
398 
399 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
FixupResolvedCallSites(GcRoot<mirror::CallSite> * dest,const Visitor & visitor)400 inline void DexCache::FixupResolvedCallSites(GcRoot<mirror::CallSite>* dest,
401                                              const Visitor& visitor) {
402   GcRoot<mirror::CallSite>* src = GetResolvedCallSites();
403   for (size_t i = 0, count = NumResolvedCallSites(); i < count; ++i) {
404     mirror::CallSite* source = src[i].Read<kReadBarrierOption>();
405     mirror::CallSite* new_source = visitor(source);
406     dest[i] = GcRoot<mirror::CallSite>(new_source);
407   }
408 }
409 
GetLocation()410 inline ObjPtr<String> DexCache::GetLocation() {
411   return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_));
412 }
413 
414 }  // namespace mirror
415 }  // namespace art
416 
417 #endif  // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
418