1 /* 2 * Copyright (C) 2011 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_H_ 18 #define ART_RUNTIME_MIRROR_DEX_CACHE_H_ 19 20 #include "array.h" 21 #include "base/bit_utils.h" 22 #include "base/locks.h" 23 #include "dex/dex_file_types.h" 24 #include "gc_root.h" // Note: must not use -inl here to avoid circular dependency. 25 #include "object.h" 26 #include "object_array.h" 27 28 namespace art { 29 30 namespace linker { 31 class ImageWriter; 32 } // namespace linker 33 34 class ArtField; 35 class ArtMethod; 36 struct DexCacheOffsets; 37 class DexFile; 38 union JValue; 39 class LinearAlloc; 40 class ReflectiveValueVisitor; 41 class Thread; 42 43 namespace mirror { 44 45 class CallSite; 46 class Class; 47 class ClassLoader; 48 class MethodType; 49 class String; 50 51 template <typename T> struct PACKED(8) DexCachePair { 52 GcRoot<T> object; 53 uint32_t index; 54 // The array is initially [ {0,0}, {0,0}, {0,0} ... ] 55 // We maintain the invariant that once a dex cache entry is populated, 56 // the pointer is always non-0 57 // Any given entry would thus be: 58 // {non-0, non-0} OR {0,0} 59 // 60 // It's generally sufficiently enough then to check if the 61 // lookup index matches the stored index (for a >0 lookup index) 62 // because if it's true the pointer is also non-null. 63 // 64 // For the 0th entry which is a special case, the value is either 65 // {0,0} (initial state) or {non-0, 0} which indicates 66 // that a valid object is stored at that index for a dex section id of 0. 67 // 68 // As an optimization, we want to avoid branching on the object pointer since 69 // it's always non-null if the id branch succeeds (except for the 0th id). 70 // Set the initial state for the 0th entry to be {0,1} which is guaranteed to fail 71 // the lookup id == stored id branch. 72 DexCachePair(ObjPtr<T> object, uint32_t index); DexCachePairDexCachePair73 DexCachePair() : index(0) {} 74 DexCachePair(const DexCachePair<T>&) = default; 75 DexCachePair& operator=(const DexCachePair<T>&) = default; 76 77 static void Initialize(std::atomic<DexCachePair<T>>* dex_cache); 78 InvalidIndexForSlotDexCachePair79 static uint32_t InvalidIndexForSlot(uint32_t slot) { 80 // Since the cache size is a power of two, 0 will always map to slot 0. 81 // Use 1 for slot 0 and 0 for all other slots. 82 return (slot == 0) ? 1u : 0u; 83 } 84 85 T* GetObjectForIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_); 86 }; 87 88 template <typename T> struct PACKED(2 * __SIZEOF_POINTER__) NativeDexCachePair { 89 T* object; 90 size_t index; 91 // This is similar to DexCachePair except that we're storing a native pointer 92 // instead of a GC root. See DexCachePair for the details. NativeDexCachePairNativeDexCachePair93 NativeDexCachePair(T* object, uint32_t index) 94 : object(object), 95 index(index) {} NativeDexCachePairNativeDexCachePair96 NativeDexCachePair() : object(nullptr), index(0u) { } 97 NativeDexCachePair(const NativeDexCachePair<T>&) = default; 98 NativeDexCachePair& operator=(const NativeDexCachePair<T>&) = default; 99 100 static void Initialize(std::atomic<NativeDexCachePair<T>>* dex_cache); 101 InvalidIndexForSlotNativeDexCachePair102 static uint32_t InvalidIndexForSlot(uint32_t slot) { 103 // Since the cache size is a power of two, 0 will always map to slot 0. 104 // Use 1 for slot 0 and 0 for all other slots. 105 return (slot == 0) ? 1u : 0u; 106 } 107 GetObjectForIndexNativeDexCachePair108 T* GetObjectForIndex(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_) { 109 if (idx != index) { 110 return nullptr; 111 } 112 DCHECK(object != nullptr); 113 return object; 114 } 115 }; 116 117 using TypeDexCachePair = DexCachePair<Class>; 118 using TypeDexCacheType = std::atomic<TypeDexCachePair>; 119 120 using StringDexCachePair = DexCachePair<String>; 121 using StringDexCacheType = std::atomic<StringDexCachePair>; 122 123 using FieldDexCachePair = NativeDexCachePair<ArtField>; 124 using FieldDexCacheType = std::atomic<FieldDexCachePair>; 125 126 using MethodDexCachePair = NativeDexCachePair<ArtMethod>; 127 using MethodDexCacheType = std::atomic<MethodDexCachePair>; 128 129 using MethodTypeDexCachePair = DexCachePair<MethodType>; 130 using MethodTypeDexCacheType = std::atomic<MethodTypeDexCachePair>; 131 132 // C++ mirror of java.lang.DexCache. 133 class MANAGED DexCache final : public Object { 134 public: 135 MIRROR_CLASS("Ljava/lang/DexCache;"); 136 137 // Size of java.lang.DexCache.class. 138 static uint32_t ClassSize(PointerSize pointer_size); 139 140 // Size of type dex cache. Needs to be a power of 2 for entrypoint assumptions to hold. 141 static constexpr size_t kDexCacheTypeCacheSize = 1024; 142 static_assert(IsPowerOfTwo(kDexCacheTypeCacheSize), 143 "Type dex cache size is not a power of 2."); 144 145 // Size of string dex cache. Needs to be a power of 2 for entrypoint assumptions to hold. 146 static constexpr size_t kDexCacheStringCacheSize = 1024; 147 static_assert(IsPowerOfTwo(kDexCacheStringCacheSize), 148 "String dex cache size is not a power of 2."); 149 150 // Size of field dex cache. Needs to be a power of 2 for entrypoint assumptions to hold. 151 static constexpr size_t kDexCacheFieldCacheSize = 1024; 152 static_assert(IsPowerOfTwo(kDexCacheFieldCacheSize), 153 "Field dex cache size is not a power of 2."); 154 155 // Size of method dex cache. Needs to be a power of 2 for entrypoint assumptions to hold. 156 static constexpr size_t kDexCacheMethodCacheSize = 1024; 157 static_assert(IsPowerOfTwo(kDexCacheMethodCacheSize), 158 "Method dex cache size is not a power of 2."); 159 160 // Size of method type dex cache. Needs to be a power of 2 for entrypoint assumptions 161 // to hold. 162 static constexpr size_t kDexCacheMethodTypeCacheSize = 1024; 163 static_assert(IsPowerOfTwo(kDexCacheMethodTypeCacheSize), 164 "MethodType dex cache size is not a power of 2."); 165 StaticTypeSize()166 static constexpr size_t StaticTypeSize() { 167 return kDexCacheTypeCacheSize; 168 } 169 StaticStringSize()170 static constexpr size_t StaticStringSize() { 171 return kDexCacheStringCacheSize; 172 } 173 StaticArtFieldSize()174 static constexpr size_t StaticArtFieldSize() { 175 return kDexCacheFieldCacheSize; 176 } 177 StaticMethodSize()178 static constexpr size_t StaticMethodSize() { 179 return kDexCacheMethodCacheSize; 180 } 181 StaticMethodTypeSize()182 static constexpr size_t StaticMethodTypeSize() { 183 return kDexCacheMethodTypeCacheSize; 184 } 185 186 // Size of an instance of java.lang.DexCache not including referenced values. InstanceSize()187 static constexpr uint32_t InstanceSize() { 188 return sizeof(DexCache); 189 } 190 191 // Initialize native fields and allocate memory. 192 void InitializeNativeFields(const DexFile* dex_file, LinearAlloc* linear_alloc) 193 REQUIRES_SHARED(Locks::mutator_lock_) 194 REQUIRES(Locks::dex_lock_); 195 196 // Clear all native fields. 197 void ResetNativeFields() REQUIRES_SHARED(Locks::mutator_lock_); 198 199 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor> 200 void FixupStrings(StringDexCacheType* dest, const Visitor& visitor) 201 REQUIRES_SHARED(Locks::mutator_lock_); 202 203 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor> 204 void FixupResolvedTypes(TypeDexCacheType* dest, const Visitor& visitor) 205 REQUIRES_SHARED(Locks::mutator_lock_); 206 207 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor> 208 void FixupResolvedMethodTypes(MethodTypeDexCacheType* dest, const Visitor& visitor) 209 REQUIRES_SHARED(Locks::mutator_lock_); 210 211 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor> 212 void FixupResolvedCallSites(GcRoot<mirror::CallSite>* dest, const Visitor& visitor) 213 REQUIRES_SHARED(Locks::mutator_lock_); 214 215 ObjPtr<String> GetLocation() REQUIRES_SHARED(Locks::mutator_lock_); 216 StringsOffset()217 static constexpr MemberOffset StringsOffset() { 218 return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_); 219 } 220 PreResolvedStringsOffset()221 static constexpr MemberOffset PreResolvedStringsOffset() { 222 return OFFSET_OF_OBJECT_MEMBER(DexCache, preresolved_strings_); 223 } 224 ResolvedTypesOffset()225 static constexpr MemberOffset ResolvedTypesOffset() { 226 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_); 227 } 228 ResolvedFieldsOffset()229 static constexpr MemberOffset ResolvedFieldsOffset() { 230 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_); 231 } 232 ResolvedMethodsOffset()233 static constexpr MemberOffset ResolvedMethodsOffset() { 234 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_); 235 } 236 ResolvedMethodTypesOffset()237 static constexpr MemberOffset ResolvedMethodTypesOffset() { 238 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_method_types_); 239 } 240 ResolvedCallSitesOffset()241 static constexpr MemberOffset ResolvedCallSitesOffset() { 242 return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_call_sites_); 243 } 244 NumStringsOffset()245 static constexpr MemberOffset NumStringsOffset() { 246 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_strings_); 247 } 248 NumPreResolvedStringsOffset()249 static constexpr MemberOffset NumPreResolvedStringsOffset() { 250 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_preresolved_strings_); 251 } 252 NumResolvedTypesOffset()253 static constexpr MemberOffset NumResolvedTypesOffset() { 254 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_types_); 255 } 256 NumResolvedFieldsOffset()257 static constexpr MemberOffset NumResolvedFieldsOffset() { 258 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_fields_); 259 } 260 NumResolvedMethodsOffset()261 static constexpr MemberOffset NumResolvedMethodsOffset() { 262 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_methods_); 263 } 264 NumResolvedMethodTypesOffset()265 static constexpr MemberOffset NumResolvedMethodTypesOffset() { 266 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_method_types_); 267 } 268 NumResolvedCallSitesOffset()269 static constexpr MemberOffset NumResolvedCallSitesOffset() { 270 return OFFSET_OF_OBJECT_MEMBER(DexCache, num_resolved_call_sites_); 271 } 272 PreResolvedStringsAlignment()273 static constexpr size_t PreResolvedStringsAlignment() { 274 return alignof(GcRoot<mirror::String>); 275 } 276 277 String* GetResolvedString(dex::StringIndex string_idx) ALWAYS_INLINE 278 REQUIRES_SHARED(Locks::mutator_lock_); 279 280 void SetResolvedString(dex::StringIndex string_idx, ObjPtr<mirror::String> resolved) ALWAYS_INLINE 281 REQUIRES_SHARED(Locks::mutator_lock_); 282 283 void SetPreResolvedString(dex::StringIndex string_idx, 284 ObjPtr<mirror::String> resolved) 285 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 286 287 // Clear the preresolved string cache to prevent further usage. 288 void ClearPreResolvedStrings() 289 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 290 291 // Clear a string for a string_idx, used to undo string intern transactions to make sure 292 // the string isn't kept live. 293 void ClearString(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_); 294 295 Class* GetResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_); 296 297 void SetResolvedType(dex::TypeIndex type_idx, ObjPtr<Class> resolved) 298 REQUIRES_SHARED(Locks::mutator_lock_); 299 300 void ClearResolvedType(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_); 301 302 ALWAYS_INLINE ArtMethod* GetResolvedMethod(uint32_t method_idx) 303 REQUIRES_SHARED(Locks::mutator_lock_); 304 305 ALWAYS_INLINE void SetResolvedMethod(uint32_t method_idx, ArtMethod* resolved) 306 REQUIRES_SHARED(Locks::mutator_lock_); 307 308 ALWAYS_INLINE ArtField* GetResolvedField(uint32_t idx) 309 REQUIRES_SHARED(Locks::mutator_lock_); 310 311 ALWAYS_INLINE void SetResolvedField(uint32_t idx, ArtField* field) 312 REQUIRES_SHARED(Locks::mutator_lock_); 313 314 MethodType* GetResolvedMethodType(dex::ProtoIndex proto_idx) REQUIRES_SHARED(Locks::mutator_lock_); 315 316 void SetResolvedMethodType(dex::ProtoIndex proto_idx, MethodType* resolved) 317 REQUIRES_SHARED(Locks::mutator_lock_); 318 319 CallSite* GetResolvedCallSite(uint32_t call_site_idx) REQUIRES_SHARED(Locks::mutator_lock_); 320 321 // Attempts to bind |call_site_idx| to the call site |resolved|. The 322 // caller must use the return value in place of |resolved|. This is 323 // because multiple threads can invoke the bootstrap method each 324 // producing a call site, but the method handle invocation on the 325 // call site must be on a common agreed value. 326 ObjPtr<CallSite> SetResolvedCallSite(uint32_t call_site_idx, ObjPtr<CallSite> resolved) 327 REQUIRES_SHARED(Locks::mutator_lock_) WARN_UNUSED; 328 329 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetStrings()330 StringDexCacheType* GetStrings() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 331 return GetFieldPtr64<StringDexCacheType*, kVerifyFlags>(StringsOffset()); 332 } 333 334 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetPreResolvedStrings()335 GcRoot<mirror::String>* GetPreResolvedStrings() ALWAYS_INLINE 336 REQUIRES_SHARED(Locks::mutator_lock_) { 337 return GetFieldPtr64<GcRoot<mirror::String>*, kVerifyFlags>(PreResolvedStringsOffset()); 338 } 339 SetStrings(StringDexCacheType * strings)340 void SetStrings(StringDexCacheType* strings) ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 341 SetFieldPtr<false>(StringsOffset(), strings); 342 } 343 SetPreResolvedStrings(GcRoot<mirror::String> * strings)344 void SetPreResolvedStrings(GcRoot<mirror::String>* strings) 345 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 346 SetFieldPtr<false>(PreResolvedStringsOffset(), strings); 347 } 348 349 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetResolvedTypes()350 TypeDexCacheType* GetResolvedTypes() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 351 return GetFieldPtr<TypeDexCacheType*, kVerifyFlags>(ResolvedTypesOffset()); 352 } 353 SetResolvedTypes(TypeDexCacheType * resolved_types)354 void SetResolvedTypes(TypeDexCacheType* resolved_types) 355 ALWAYS_INLINE 356 REQUIRES_SHARED(Locks::mutator_lock_) { 357 SetFieldPtr<false>(ResolvedTypesOffset(), resolved_types); 358 } 359 GetResolvedMethods()360 MethodDexCacheType* GetResolvedMethods() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 361 return GetFieldPtr<MethodDexCacheType*>(ResolvedMethodsOffset()); 362 } 363 SetResolvedMethods(MethodDexCacheType * resolved_methods)364 void SetResolvedMethods(MethodDexCacheType* resolved_methods) 365 ALWAYS_INLINE 366 REQUIRES_SHARED(Locks::mutator_lock_) { 367 SetFieldPtr<false>(ResolvedMethodsOffset(), resolved_methods); 368 } 369 GetResolvedFields()370 FieldDexCacheType* GetResolvedFields() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 371 return GetFieldPtr<FieldDexCacheType*>(ResolvedFieldsOffset()); 372 } 373 SetResolvedFields(FieldDexCacheType * resolved_fields)374 void SetResolvedFields(FieldDexCacheType* resolved_fields) 375 ALWAYS_INLINE 376 REQUIRES_SHARED(Locks::mutator_lock_) { 377 SetFieldPtr<false>(ResolvedFieldsOffset(), resolved_fields); 378 } 379 380 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetResolvedMethodTypes()381 MethodTypeDexCacheType* GetResolvedMethodTypes() 382 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 383 return GetFieldPtr64<MethodTypeDexCacheType*, kVerifyFlags>(ResolvedMethodTypesOffset()); 384 } 385 SetResolvedMethodTypes(MethodTypeDexCacheType * resolved_method_types)386 void SetResolvedMethodTypes(MethodTypeDexCacheType* resolved_method_types) 387 ALWAYS_INLINE 388 REQUIRES_SHARED(Locks::mutator_lock_) { 389 SetFieldPtr<false>(ResolvedMethodTypesOffset(), resolved_method_types); 390 } 391 392 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetResolvedCallSites()393 GcRoot<CallSite>* GetResolvedCallSites() 394 ALWAYS_INLINE 395 REQUIRES_SHARED(Locks::mutator_lock_) { 396 return GetFieldPtr<GcRoot<CallSite>*, kVerifyFlags>(ResolvedCallSitesOffset()); 397 } 398 SetResolvedCallSites(GcRoot<CallSite> * resolved_call_sites)399 void SetResolvedCallSites(GcRoot<CallSite>* resolved_call_sites) 400 ALWAYS_INLINE 401 REQUIRES_SHARED(Locks::mutator_lock_) { 402 SetFieldPtr<false>(ResolvedCallSitesOffset(), resolved_call_sites); 403 } 404 405 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumStrings()406 size_t NumStrings() REQUIRES_SHARED(Locks::mutator_lock_) { 407 return GetField32<kVerifyFlags>(NumStringsOffset()); 408 } 409 410 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumPreResolvedStrings()411 size_t NumPreResolvedStrings() REQUIRES_SHARED(Locks::mutator_lock_) { 412 return GetField32<kVerifyFlags>(NumPreResolvedStringsOffset()); 413 } 414 415 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumResolvedTypes()416 size_t NumResolvedTypes() REQUIRES_SHARED(Locks::mutator_lock_) { 417 return GetField32<kVerifyFlags>(NumResolvedTypesOffset()); 418 } 419 420 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumResolvedMethods()421 size_t NumResolvedMethods() REQUIRES_SHARED(Locks::mutator_lock_) { 422 return GetField32<kVerifyFlags>(NumResolvedMethodsOffset()); 423 } 424 425 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumResolvedFields()426 size_t NumResolvedFields() REQUIRES_SHARED(Locks::mutator_lock_) { 427 return GetField32<kVerifyFlags>(NumResolvedFieldsOffset()); 428 } 429 430 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumResolvedMethodTypes()431 size_t NumResolvedMethodTypes() REQUIRES_SHARED(Locks::mutator_lock_) { 432 return GetField32<kVerifyFlags>(NumResolvedMethodTypesOffset()); 433 } 434 435 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumResolvedCallSites()436 size_t NumResolvedCallSites() REQUIRES_SHARED(Locks::mutator_lock_) { 437 return GetField32<kVerifyFlags>(NumResolvedCallSitesOffset()); 438 } 439 GetDexFile()440 const DexFile* GetDexFile() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { 441 return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_)); 442 } 443 SetDexFile(const DexFile * dex_file)444 void SetDexFile(const DexFile* dex_file) REQUIRES_SHARED(Locks::mutator_lock_) { 445 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file); 446 } 447 448 void SetLocation(ObjPtr<String> location) REQUIRES_SHARED(Locks::mutator_lock_); 449 450 template <typename T> 451 static NativeDexCachePair<T> GetNativePair(std::atomic<NativeDexCachePair<T>>* pair_array, 452 size_t idx); 453 454 template <typename T> 455 static void SetNativePair(std::atomic<NativeDexCachePair<T>>* pair_array, 456 size_t idx, 457 NativeDexCachePair<T> pair); 458 PreResolvedStringsSize(size_t num_strings)459 static size_t PreResolvedStringsSize(size_t num_strings) { 460 return sizeof(GcRoot<mirror::String>) * num_strings; 461 } 462 463 uint32_t StringSlotIndex(dex::StringIndex string_idx) REQUIRES_SHARED(Locks::mutator_lock_); 464 uint32_t TypeSlotIndex(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_); 465 uint32_t FieldSlotIndex(uint32_t field_idx) REQUIRES_SHARED(Locks::mutator_lock_); 466 uint32_t MethodSlotIndex(uint32_t method_idx) REQUIRES_SHARED(Locks::mutator_lock_); 467 uint32_t MethodTypeSlotIndex(dex::ProtoIndex proto_idx) REQUIRES_SHARED(Locks::mutator_lock_); 468 469 // Returns true if we succeeded in adding the pre-resolved string array. 470 bool AddPreResolvedStringsArray() REQUIRES_SHARED(Locks::mutator_lock_); 471 472 void VisitReflectiveTargets(ReflectiveValueVisitor* visitor) REQUIRES(Locks::mutator_lock_); 473 474 void SetClassLoader(ObjPtr<ClassLoader> class_loader) REQUIRES_SHARED(Locks::mutator_lock_); 475 476 private: 477 void SetNativeArrays(StringDexCacheType* strings, 478 uint32_t num_strings, 479 TypeDexCacheType* resolved_types, 480 uint32_t num_resolved_types, 481 MethodDexCacheType* resolved_methods, 482 uint32_t num_resolved_methods, 483 FieldDexCacheType* resolved_fields, 484 uint32_t num_resolved_fields, 485 MethodTypeDexCacheType* resolved_method_types, 486 uint32_t num_resolved_method_types, 487 GcRoot<CallSite>* resolved_call_sites, 488 uint32_t num_resolved_call_sites) 489 REQUIRES_SHARED(Locks::mutator_lock_); 490 491 // std::pair<> is not trivially copyable and as such it is unsuitable for atomic operations, 492 // so we use a custom pair class for loading and storing the NativeDexCachePair<>. 493 template <typename IntType> 494 struct PACKED(2 * sizeof(IntType)) ConversionPair { ConversionPairConversionPair495 ConversionPair(IntType f, IntType s) : first(f), second(s) { } 496 ConversionPair(const ConversionPair&) = default; 497 ConversionPair& operator=(const ConversionPair&) = default; 498 IntType first; 499 IntType second; 500 }; 501 using ConversionPair32 = ConversionPair<uint32_t>; 502 using ConversionPair64 = ConversionPair<uint64_t>; 503 504 // Visit instance fields of the dex cache as well as its associated arrays. 505 template <bool kVisitNativeRoots, 506 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 507 ReadBarrierOption kReadBarrierOption = kWithReadBarrier, 508 typename Visitor> 509 void VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) 510 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_); 511 512 // Due to lack of 16-byte atomics support, we use hand-crafted routines. 513 #if defined(__aarch64__) 514 // 16-byte atomics are supported on aarch64. AtomicLoadRelaxed16B(std::atomic<ConversionPair64> * target)515 ALWAYS_INLINE static ConversionPair64 AtomicLoadRelaxed16B( 516 std::atomic<ConversionPair64>* target) { 517 return target->load(std::memory_order_relaxed); 518 } 519 AtomicStoreRelease16B(std::atomic<ConversionPair64> * target,ConversionPair64 value)520 ALWAYS_INLINE static void AtomicStoreRelease16B( 521 std::atomic<ConversionPair64>* target, ConversionPair64 value) { 522 target->store(value, std::memory_order_release); 523 } 524 #elif defined(__x86_64__) AtomicLoadRelaxed16B(std::atomic<ConversionPair64> * target)525 ALWAYS_INLINE static ConversionPair64 AtomicLoadRelaxed16B( 526 std::atomic<ConversionPair64>* target) { 527 uint64_t first, second; 528 __asm__ __volatile__( 529 "lock cmpxchg16b (%2)" 530 : "=&a"(first), "=&d"(second) 531 : "r"(target), "a"(0), "d"(0), "b"(0), "c"(0) 532 : "cc"); 533 return ConversionPair64(first, second); 534 } 535 AtomicStoreRelease16B(std::atomic<ConversionPair64> * target,ConversionPair64 value)536 ALWAYS_INLINE static void AtomicStoreRelease16B( 537 std::atomic<ConversionPair64>* target, ConversionPair64 value) { 538 uint64_t first, second; 539 __asm__ __volatile__ ( 540 "movq (%2), %%rax\n\t" 541 "movq 8(%2), %%rdx\n\t" 542 "1:\n\t" 543 "lock cmpxchg16b (%2)\n\t" 544 "jnz 1b" 545 : "=&a"(first), "=&d"(second) 546 : "r"(target), "b"(value.first), "c"(value.second) 547 : "cc"); 548 } 549 #else 550 static ConversionPair64 AtomicLoadRelaxed16B(std::atomic<ConversionPair64>* target); 551 static void AtomicStoreRelease16B(std::atomic<ConversionPair64>* target, ConversionPair64 value); 552 #endif 553 554 HeapReference<ClassLoader> class_loader_; 555 HeapReference<String> location_; 556 557 uint64_t dex_file_; // const DexFile* 558 uint64_t preresolved_strings_; // GcRoot<mirror::String*> array with num_preresolved_strings 559 // elements. 560 uint64_t resolved_call_sites_; // GcRoot<CallSite>* array with num_resolved_call_sites_ 561 // elements. 562 uint64_t resolved_fields_; // std::atomic<FieldDexCachePair>*, array with 563 // num_resolved_fields_ elements. 564 uint64_t resolved_method_types_; // std::atomic<MethodTypeDexCachePair>* array with 565 // num_resolved_method_types_ elements. 566 uint64_t resolved_methods_; // ArtMethod*, array with num_resolved_methods_ elements. 567 uint64_t resolved_types_; // TypeDexCacheType*, array with num_resolved_types_ elements. 568 uint64_t strings_; // std::atomic<StringDexCachePair>*, array with num_strings_ 569 // elements. 570 571 uint32_t num_preresolved_strings_; // Number of elements in the preresolved_strings_ array. 572 uint32_t num_resolved_call_sites_; // Number of elements in the call_sites_ array. 573 uint32_t num_resolved_fields_; // Number of elements in the resolved_fields_ array. 574 uint32_t num_resolved_method_types_; // Number of elements in the resolved_method_types_ array. 575 uint32_t num_resolved_methods_; // Number of elements in the resolved_methods_ array. 576 uint32_t num_resolved_types_; // Number of elements in the resolved_types_ array. 577 uint32_t num_strings_; // Number of elements in the strings_ array. 578 579 friend struct art::DexCacheOffsets; // for verifying offset information 580 friend class linker::ImageWriter; 581 friend class Object; // For VisitReferences 582 DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache); 583 }; 584 585 } // namespace mirror 586 } // namespace art 587 588 #endif // ART_RUNTIME_MIRROR_DEX_CACHE_H_ 589