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