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 uint32_t DexCache::MethodSlotIndex(uint32_t method_idx) {
    212   DCHECK_LT(method_idx, GetDexFile()->NumMethodIds());
    213   const uint32_t slot_idx = method_idx % kDexCacheMethodCacheSize;
    214   DCHECK_LT(slot_idx, NumResolvedMethods());
    215   return slot_idx;
    216 }
    217 
    218 inline ArtMethod* DexCache::GetResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
    219   DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
    220   auto pair = GetNativePairPtrSize(GetResolvedMethods(), MethodSlotIndex(method_idx), ptr_size);
    221   return pair.GetObjectForIndex(method_idx);
    222 }
    223 
    224 inline void DexCache::SetResolvedMethod(uint32_t method_idx,
    225                                         ArtMethod* method,
    226                                         PointerSize ptr_size) {
    227   DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
    228   DCHECK(method != nullptr);
    229   MethodDexCachePair pair(method, method_idx);
    230   SetNativePairPtrSize(GetResolvedMethods(), MethodSlotIndex(method_idx), pair, ptr_size);
    231 }
    232 
    233 inline void DexCache::ClearResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
    234   DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
    235   uint32_t slot_idx = MethodSlotIndex(method_idx);
    236   auto* resolved_methods = GetResolvedMethods();
    237   // This is racy but should only be called from the single-threaded ImageWriter.
    238   DCHECK(Runtime::Current()->IsAotCompiler());
    239   if (GetNativePairPtrSize(resolved_methods, slot_idx, ptr_size).index == method_idx) {
    240     MethodDexCachePair cleared(nullptr, MethodDexCachePair::InvalidIndexForSlot(slot_idx));
    241     SetNativePairPtrSize(resolved_methods, slot_idx, cleared, ptr_size);
    242   }
    243 }
    244 
    245 template <typename PtrType>
    246 inline PtrType DexCache::GetElementPtrSize(PtrType* ptr_array, size_t idx, PointerSize ptr_size) {
    247   if (ptr_size == PointerSize::k64) {
    248     uint64_t element = reinterpret_cast<const uint64_t*>(ptr_array)[idx];
    249     return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
    250   } else {
    251     uint32_t element = reinterpret_cast<const uint32_t*>(ptr_array)[idx];
    252     return reinterpret_cast<PtrType>(dchecked_integral_cast<uintptr_t>(element));
    253   }
    254 }
    255 
    256 template <typename PtrType>
    257 inline void DexCache::SetElementPtrSize(PtrType* ptr_array,
    258                                         size_t idx,
    259                                         PtrType ptr,
    260                                         PointerSize ptr_size) {
    261   if (ptr_size == PointerSize::k64) {
    262     reinterpret_cast<uint64_t*>(ptr_array)[idx] =
    263         dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
    264   } else {
    265     reinterpret_cast<uint32_t*>(ptr_array)[idx] =
    266         dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr));
    267   }
    268 }
    269 
    270 template <typename T>
    271 NativeDexCachePair<T> DexCache::GetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array,
    272                                                      size_t idx,
    273                                                      PointerSize ptr_size) {
    274   if (ptr_size == PointerSize::k64) {
    275     auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array);
    276     ConversionPair64 value = AtomicLoadRelaxed16B(&array[idx]);
    277     return NativeDexCachePair<T>(reinterpret_cast64<T*>(value.first),
    278                                  dchecked_integral_cast<size_t>(value.second));
    279   } else {
    280     auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array);
    281     ConversionPair32 value = array[idx].load(std::memory_order_relaxed);
    282     return NativeDexCachePair<T>(reinterpret_cast<T*>(value.first), value.second);
    283   }
    284 }
    285 
    286 template <typename T>
    287 void DexCache::SetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array,
    288                                     size_t idx,
    289                                     NativeDexCachePair<T> pair,
    290                                     PointerSize ptr_size) {
    291   if (ptr_size == PointerSize::k64) {
    292     auto* array = reinterpret_cast<std::atomic<ConversionPair64>*>(pair_array);
    293     ConversionPair64 v(reinterpret_cast64<uint64_t>(pair.object), pair.index);
    294     AtomicStoreRelease16B(&array[idx], v);
    295   } else {
    296     auto* array = reinterpret_cast<std::atomic<ConversionPair32>*>(pair_array);
    297     ConversionPair32 v(
    298         dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(pair.object)),
    299         dchecked_integral_cast<uint32_t>(pair.index));
    300     array[idx].store(v, std::memory_order_release);
    301   }
    302 }
    303 
    304 template <typename T,
    305           ReadBarrierOption kReadBarrierOption,
    306           typename Visitor>
    307 inline void VisitDexCachePairs(std::atomic<DexCachePair<T>>* pairs,
    308                                size_t num_pairs,
    309                                const Visitor& visitor)
    310     REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
    311   for (size_t i = 0; i < num_pairs; ++i) {
    312     DexCachePair<T> source = pairs[i].load(std::memory_order_relaxed);
    313     // NOTE: We need the "template" keyword here to avoid a compilation
    314     // failure. GcRoot<T> is a template argument-dependent type and we need to
    315     // tell the compiler to treat "Read" as a template rather than a field or
    316     // function. Otherwise, on encountering the "<" token, the compiler would
    317     // treat "Read" as a field.
    318     T* const before = source.object.template Read<kReadBarrierOption>();
    319     visitor.VisitRootIfNonNull(source.object.AddressWithoutBarrier());
    320     if (source.object.template Read<kReadBarrierOption>() != before) {
    321       pairs[i].store(source, std::memory_order_relaxed);
    322     }
    323   }
    324 }
    325 
    326 template <bool kVisitNativeRoots,
    327           VerifyObjectFlags kVerifyFlags,
    328           ReadBarrierOption kReadBarrierOption,
    329           typename Visitor>
    330 inline void DexCache::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) {
    331   // Visit instance fields first.
    332   VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
    333   // Visit arrays after.
    334   if (kVisitNativeRoots) {
    335     VisitDexCachePairs<String, kReadBarrierOption, Visitor>(
    336         GetStrings(), NumStrings(), visitor);
    337 
    338     VisitDexCachePairs<Class, kReadBarrierOption, Visitor>(
    339         GetResolvedTypes(), NumResolvedTypes(), visitor);
    340 
    341     VisitDexCachePairs<MethodType, kReadBarrierOption, Visitor>(
    342         GetResolvedMethodTypes(), NumResolvedMethodTypes(), visitor);
    343 
    344     GcRoot<mirror::CallSite>* resolved_call_sites = GetResolvedCallSites();
    345     for (size_t i = 0, num_call_sites = NumResolvedCallSites(); i != num_call_sites; ++i) {
    346       visitor.VisitRootIfNonNull(resolved_call_sites[i].AddressWithoutBarrier());
    347     }
    348   }
    349 }
    350 
    351 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
    352 inline void DexCache::FixupStrings(StringDexCacheType* dest, const Visitor& visitor) {
    353   StringDexCacheType* src = GetStrings();
    354   for (size_t i = 0, count = NumStrings(); i < count; ++i) {
    355     StringDexCachePair source = src[i].load(std::memory_order_relaxed);
    356     String* ptr = source.object.Read<kReadBarrierOption>();
    357     String* new_source = visitor(ptr);
    358     source.object = GcRoot<String>(new_source);
    359     dest[i].store(source, std::memory_order_relaxed);
    360   }
    361 }
    362 
    363 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
    364 inline void DexCache::FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor) {
    365   TypeDexCacheType* src = GetResolvedTypes();
    366   for (size_t i = 0, count = NumResolvedTypes(); i < count; ++i) {
    367     TypeDexCachePair source = src[i].load(std::memory_order_relaxed);
    368     Class* ptr = source.object.Read<kReadBarrierOption>();
    369     Class* new_source = visitor(ptr);
    370     source.object = GcRoot<Class>(new_source);
    371     dest[i].store(source, std::memory_order_relaxed);
    372   }
    373 }
    374 
    375 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
    376 inline void DexCache::FixupResolvedMethodTypes(MethodTypeDexCacheType* dest,
    377                                                const Visitor& visitor) {
    378   MethodTypeDexCacheType* src = GetResolvedMethodTypes();
    379   for (size_t i = 0, count = NumResolvedMethodTypes(); i < count; ++i) {
    380     MethodTypeDexCachePair source = src[i].load(std::memory_order_relaxed);
    381     MethodType* ptr = source.object.Read<kReadBarrierOption>();
    382     MethodType* new_source = visitor(ptr);
    383     source.object = GcRoot<MethodType>(new_source);
    384     dest[i].store(source, std::memory_order_relaxed);
    385   }
    386 }
    387 
    388 template <ReadBarrierOption kReadBarrierOption, typename Visitor>
    389 inline void DexCache::FixupResolvedCallSites(GcRoot<mirror::CallSite>* dest,
    390                                              const Visitor& visitor) {
    391   GcRoot<mirror::CallSite>* src = GetResolvedCallSites();
    392   for (size_t i = 0, count = NumResolvedCallSites(); i < count; ++i) {
    393     mirror::CallSite* source = src[i].Read<kReadBarrierOption>();
    394     mirror::CallSite* new_source = visitor(source);
    395     dest[i] = GcRoot<mirror::CallSite>(new_source);
    396   }
    397 }
    398 
    399 }  // namespace mirror
    400 }  // namespace art
    401 
    402 #endif  // ART_RUNTIME_MIRROR_DEX_CACHE_INL_H_
    403