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_CLASS_LINKER_H_ 18 #define ART_RUNTIME_CLASS_LINKER_H_ 19 20 #include <list> 21 #include <map> 22 #include <set> 23 #include <string> 24 #include <type_traits> 25 #include <unordered_map> 26 #include <utility> 27 #include <vector> 28 29 #include "base/array_ref.h" 30 #include "base/hash_map.h" 31 #include "base/intrusive_forward_list.h" 32 #include "base/locks.h" 33 #include "base/macros.h" 34 #include "base/mutex.h" 35 #include "base/pointer_size.h" 36 #include "dex/class_accessor.h" 37 #include "dex/dex_file_types.h" 38 #include "gc_root.h" 39 #include "handle.h" 40 #include "interpreter/mterp/nterp.h" 41 #include "jni.h" 42 #include "mirror/class.h" 43 #include "mirror/object.h" 44 #include "oat/jni_stub_hash_map.h" 45 #include "oat/oat_file.h" 46 #include "verifier/verifier_enums.h" 47 48 namespace art HIDDEN { 49 50 class ArtField; 51 class ArtMethod; 52 class ClassHierarchyAnalysis; 53 class ClassLoaderContext; 54 enum class ClassRoot : uint32_t; 55 class ClassTable; 56 class DexFile; 57 template<class T> class Handle; 58 class ImtConflictTable; 59 template<typename T> class LengthPrefixedArray; 60 template<class T> class MutableHandle; 61 class InternTable; 62 class LinearAlloc; 63 class OatFile; 64 template<class T> class ObjectLock; 65 class Runtime; 66 class ScopedObjectAccessAlreadyRunnable; 67 template<size_t kNumReferences> class PACKED(4) StackHandleScope; 68 class Thread; 69 class VariableSizedHandleScope; 70 71 enum VisitRootFlags : uint8_t; 72 73 namespace dex { 74 struct ClassDef; 75 struct MethodHandleItem; 76 } // namespace dex 77 78 namespace gc { 79 namespace space { 80 class ImageSpace; 81 } // namespace space 82 } // namespace gc 83 84 namespace linker { 85 struct CompilationHelper; 86 class ImageWriter; 87 class OatWriter; 88 } // namespace linker 89 90 namespace mirror { 91 class ClassLoader; 92 class DexCache; 93 class DexCachePointerArray; 94 class DexCacheMethodHandlesTest_Open_Test; 95 class DexCacheTest_Open_Test; 96 class IfTable; 97 class MethodHandle; 98 class MethodHandlesLookup; 99 class MethodType; 100 template<class T> class ObjectArray; 101 class RawMethodType; 102 class StackTraceElement; 103 } // namespace mirror 104 105 namespace verifier { 106 class VerifierDeps; 107 } 108 109 class ClassVisitor { 110 public: ~ClassVisitor()111 virtual ~ClassVisitor() {} 112 // Return true to continue visiting. 113 virtual bool operator()(ObjPtr<mirror::Class> klass) = 0; 114 }; 115 116 template <typename Func> 117 class ClassFuncVisitor final : public ClassVisitor { 118 public: ClassFuncVisitor(Func func)119 explicit ClassFuncVisitor(Func func) : func_(func) {} operator()120 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { 121 return func_(klass); 122 } 123 124 private: 125 Func func_; 126 }; 127 128 class ClassLoaderVisitor { 129 public: ~ClassLoaderVisitor()130 virtual ~ClassLoaderVisitor() {} 131 virtual void Visit(ObjPtr<mirror::ClassLoader> class_loader) 132 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0; 133 }; 134 135 class DexCacheVisitor { 136 public: ~DexCacheVisitor()137 virtual ~DexCacheVisitor() {} 138 virtual void Visit(ObjPtr<mirror::DexCache> dex_cache) 139 REQUIRES_SHARED(Locks::dex_lock_, Locks::mutator_lock_) = 0; 140 }; 141 142 template <typename Func> 143 class ClassLoaderFuncVisitor final : public ClassLoaderVisitor { 144 public: ClassLoaderFuncVisitor(Func func)145 explicit ClassLoaderFuncVisitor(Func func) : func_(func) {} Visit(ObjPtr<mirror::ClassLoader> cl)146 void Visit(ObjPtr<mirror::ClassLoader> cl) override REQUIRES_SHARED(Locks::mutator_lock_) { 147 func_(cl); 148 } 149 150 private: 151 Func func_; 152 }; 153 154 class AllocatorVisitor { 155 public: ~AllocatorVisitor()156 virtual ~AllocatorVisitor() {} 157 // Return true to continue visiting. 158 virtual bool Visit(LinearAlloc* alloc) 159 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0; 160 }; 161 162 class ClassLinker { 163 public: 164 static constexpr bool kAppImageMayContainStrings = true; 165 166 EXPORT explicit ClassLinker(InternTable* intern_table, 167 bool fast_class_not_found_exceptions = true); 168 EXPORT virtual ~ClassLinker(); 169 170 // Initialize class linker by bootstraping from dex files. 171 bool InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, 172 std::string* error_msg) 173 REQUIRES_SHARED(Locks::mutator_lock_) 174 REQUIRES(!Locks::dex_lock_); 175 176 // Initialize class linker from one or more boot images. 177 bool InitFromBootImage(std::string* error_msg) 178 REQUIRES_SHARED(Locks::mutator_lock_) 179 REQUIRES(!Locks::dex_lock_); 180 181 // Add boot class path dex files that were not included in the boot image. 182 // ClassLinker takes ownership of these dex files. 183 // DO NOT use directly. Use `Runtime::AddExtraBootDexFiles`. 184 void AddExtraBootDexFiles(Thread* self, 185 std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) 186 REQUIRES_SHARED(Locks::mutator_lock_); 187 188 // Add image spaces to the class linker, may fix up classloader fields and dex cache fields. 189 // The dex files that were newly opened for the space are placed in the out argument `dex_files`. 190 // Returns true if the operation succeeded. 191 // The space must be already added to the heap before calling AddImageSpace since we need to 192 // properly handle read barriers and object marking. 193 bool AddImageSpaces(ArrayRef<gc::space::ImageSpace*> spaces, 194 Handle<mirror::ClassLoader> class_loader, 195 ClassLoaderContext* context, 196 /*out*/ std::vector<std::unique_ptr<const DexFile>>* dex_files, 197 /*out*/ std::string* error_msg) REQUIRES(!Locks::dex_lock_) 198 REQUIRES_SHARED(Locks::mutator_lock_); 199 200 EXPORT bool OpenImageDexFiles(gc::space::ImageSpace* space, 201 std::vector<std::unique_ptr<const DexFile>>* out_dex_files, 202 std::string* error_msg) 203 REQUIRES(!Locks::dex_lock_) 204 REQUIRES_SHARED(Locks::mutator_lock_); 205 206 // Finds a class by its descriptor, loading it if necessary. 207 // If class_loader is null, searches boot_class_path_. 208 EXPORT ObjPtr<mirror::Class> FindClass(Thread* self, 209 const char* descriptor, 210 size_t descriptor_length, 211 Handle<mirror::ClassLoader> class_loader) 212 REQUIRES_SHARED(Locks::mutator_lock_) 213 REQUIRES(!Locks::dex_lock_); 214 215 // Helper overload that retrieves the descriptor and its length from the `dex_file`. 216 EXPORT ObjPtr<mirror::Class> FindClass(Thread* self, 217 const DexFile& dex_file, 218 dex::TypeIndex type_index, 219 Handle<mirror::ClassLoader> class_loader) 220 REQUIRES_SHARED(Locks::mutator_lock_) 221 REQUIRES(!Locks::dex_lock_); 222 223 // Finds a class by its descriptor using the "system" class loader, ie by searching the 224 // boot_class_path_. FindSystemClass(Thread * self,const char * descriptor)225 ObjPtr<mirror::Class> FindSystemClass(Thread* self, const char* descriptor) 226 REQUIRES_SHARED(Locks::mutator_lock_) 227 REQUIRES(!Locks::dex_lock_) { 228 return FindClass(self, descriptor, strlen(descriptor), ScopedNullHandle<mirror::ClassLoader>()); 229 } 230 231 // Finds the array class given for the element class. 232 ObjPtr<mirror::Class> FindArrayClass(Thread* self, ObjPtr<mirror::Class> element_class) 233 REQUIRES_SHARED(Locks::mutator_lock_) 234 REQUIRES(!Locks::dex_lock_); 235 236 // Returns true if the class linker is initialized. IsInitialized()237 bool IsInitialized() const { 238 return init_done_; 239 } 240 241 // Define a new a class based on a ClassDef from a DexFile 242 ObjPtr<mirror::Class> DefineClass(Thread* self, 243 const char* descriptor, 244 size_t descriptor_length, 245 size_t hash, 246 Handle<mirror::ClassLoader> class_loader, 247 const DexFile& dex_file, 248 const dex::ClassDef& dex_class_def) 249 REQUIRES_SHARED(Locks::mutator_lock_) 250 REQUIRES(!Locks::dex_lock_); 251 252 // Finds a class by its descriptor, returning null if it isn't wasn't loaded 253 // by the given 'class_loader'. 254 EXPORT ObjPtr<mirror::Class> LookupClass(Thread* self, 255 std::string_view descriptor, 256 ObjPtr<mirror::ClassLoader> class_loader) 257 REQUIRES(!Locks::classlinker_classes_lock_) 258 REQUIRES_SHARED(Locks::mutator_lock_); 259 260 ObjPtr<mirror::Class> LookupPrimitiveClass(char type) REQUIRES_SHARED(Locks::mutator_lock_); 261 ObjPtr<mirror::Class> FindPrimitiveClass(char type) REQUIRES_SHARED(Locks::mutator_lock_); 262 263 void DumpForSigQuit(std::ostream& os) REQUIRES(!Locks::classlinker_classes_lock_); 264 265 size_t NumLoadedClasses() 266 REQUIRES(!Locks::classlinker_classes_lock_) 267 REQUIRES_SHARED(Locks::mutator_lock_); 268 269 // Resolve a String with the given index from the DexFile associated with the given `referrer`, 270 // storing the result in the DexCache. The `referrer` is used to identify the target DexCache 271 // to use for resolution. 272 ObjPtr<mirror::String> ResolveString(dex::StringIndex string_idx, 273 ArtField* referrer) 274 REQUIRES_SHARED(Locks::mutator_lock_); 275 ObjPtr<mirror::String> ResolveString(dex::StringIndex string_idx, 276 ArtMethod* referrer) 277 REQUIRES_SHARED(Locks::mutator_lock_); 278 279 // Resolve a String with the given index from the DexFile associated with the given DexCache, 280 // storing the result in the DexCache. 281 ObjPtr<mirror::String> ResolveString(dex::StringIndex string_idx, 282 Handle<mirror::DexCache> dex_cache) 283 REQUIRES_SHARED(Locks::mutator_lock_); 284 285 // Find a String with the given index from the DexFile associated with the given DexCache, 286 // storing the result in the DexCache if found. Return null if not found. 287 ObjPtr<mirror::String> LookupString(dex::StringIndex string_idx, 288 ObjPtr<mirror::DexCache> dex_cache) 289 REQUIRES_SHARED(Locks::mutator_lock_); 290 291 // Resolve a Type with the given index from the DexFile associated with the given `referrer`, 292 // storing the result in the DexCache. The `referrer` is used to identify the target DexCache 293 // and ClassLoader to use for resolution. 294 ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx, ObjPtr<mirror::Class> referrer) 295 REQUIRES_SHARED(Locks::mutator_lock_) 296 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 297 ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx, ArtField* referrer) 298 REQUIRES_SHARED(Locks::mutator_lock_) 299 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 300 ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx, ArtMethod* referrer) 301 REQUIRES_SHARED(Locks::mutator_lock_) 302 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 303 304 // Resolve a type with the given index from the DexFile associated with the given DexCache 305 // and ClassLoader, storing the result in DexCache. The ClassLoader is used to search for 306 // the type, since it may be referenced from but not contained within the DexFile. 307 ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx, 308 Handle<mirror::DexCache> dex_cache, 309 Handle<mirror::ClassLoader> class_loader) 310 REQUIRES_SHARED(Locks::mutator_lock_) 311 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 312 313 // Look up a resolved type with the given index from the DexFile associated with the given 314 // `referrer`, storing the result in the DexCache. The `referrer` is used to identify the 315 // target DexCache and ClassLoader to use for lookup. 316 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx, 317 ObjPtr<mirror::Class> referrer) 318 REQUIRES_SHARED(Locks::mutator_lock_); 319 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx, ArtField* referrer) 320 REQUIRES_SHARED(Locks::mutator_lock_); 321 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx, ArtMethod* referrer) 322 REQUIRES_SHARED(Locks::mutator_lock_); 323 324 // Look up a resolved type with the given index from the DexFile associated with the given 325 // DexCache and ClassLoader. The ClassLoader is used to search for the type, since it may 326 // be referenced from but not contained within the DexFile. 327 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx, 328 ObjPtr<mirror::DexCache> dex_cache, 329 ObjPtr<mirror::ClassLoader> class_loader) 330 REQUIRES_SHARED(Locks::mutator_lock_); 331 332 // Look up a resolved type with the given descriptor associated with the given ClassLoader. 333 ObjPtr<mirror::Class> LookupResolvedType(std::string_view descriptor, 334 ObjPtr<mirror::ClassLoader> class_loader) 335 REQUIRES_SHARED(Locks::mutator_lock_); 336 337 // Look up a previously resolved method with the given index. 338 ArtMethod* LookupResolvedMethod(uint32_t method_idx, 339 ObjPtr<mirror::DexCache> dex_cache, 340 ObjPtr<mirror::ClassLoader> class_loader) 341 REQUIRES_SHARED(Locks::mutator_lock_); 342 343 // Find a method with the given index from class `klass`, and update the dex cache. 344 EXPORT ArtMethod* FindResolvedMethod(ObjPtr<mirror::Class> klass, 345 ObjPtr<mirror::DexCache> dex_cache, 346 ObjPtr<mirror::ClassLoader> class_loader, 347 uint32_t method_idx) 348 REQUIRES_SHARED(Locks::mutator_lock_); 349 350 // Find a method using the wrong lookup mechanism. If `klass` is an interface, 351 // search for a class method. If it is a class, search for an interface method. 352 // This is useful when throwing IncompatibleClassChangeError. 353 ArtMethod* FindIncompatibleMethod(ObjPtr<mirror::Class> klass, 354 ObjPtr<mirror::DexCache> dex_cache, 355 ObjPtr<mirror::ClassLoader> class_loader, 356 uint32_t method_idx) 357 REQUIRES_SHARED(Locks::mutator_lock_); 358 359 // Check invoke type against the referenced class. Throws IncompatibleClassChangeError 360 // and returns true on mismatch (kInterface on a non-interface class, 361 // kVirtual on interface, kDefault on interface for dex files not supporting default methods), 362 // otherwise returns false. 363 static bool ThrowIfInvokeClassMismatch(ObjPtr<mirror::Class> cls, 364 const DexFile& dex_file, 365 InvokeType type) 366 REQUIRES_SHARED(Locks::mutator_lock_); 367 368 ArtMethod* ResolveMethodWithChecks(uint32_t method_idx, ArtMethod* referrer, InvokeType type) 369 REQUIRES_SHARED(Locks::mutator_lock_) 370 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 371 372 EXPORT ArtMethod* ResolveMethodId(uint32_t method_idx, 373 Handle<mirror::DexCache> dex_cache, 374 Handle<mirror::ClassLoader> class_loader) 375 REQUIRES_SHARED(Locks::mutator_lock_) 376 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 377 378 ArtMethod* ResolveMethodId(uint32_t method_idx, ArtMethod* referrer) 379 REQUIRES_SHARED(Locks::mutator_lock_) 380 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 381 382 ArtField* LookupResolvedField(uint32_t field_idx, ArtMethod* referrer, bool is_static) 383 REQUIRES_SHARED(Locks::mutator_lock_); 384 // Find a field by its field index. 385 ArtField* LookupResolvedField(uint32_t field_idx, 386 ObjPtr<mirror::DexCache> dex_cache, 387 ObjPtr<mirror::ClassLoader> class_loader, 388 bool is_static) 389 REQUIRES_SHARED(Locks::mutator_lock_); 390 ArtField* ResolveField(uint32_t field_idx, ArtMethod* referrer, bool is_static) 391 REQUIRES_SHARED(Locks::mutator_lock_) 392 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 393 394 // Resolve a field with a given ID from the DexFile associated with the given DexCache 395 // and ClassLoader, storing the result in DexCache. The ClassLinker and ClassLoader 396 // are used as in ResolveType. What is unique is the is_static argument which is used 397 // to determine if we are resolving a static or non-static field. 398 ArtField* ResolveField(uint32_t field_idx, 399 Handle<mirror::DexCache> dex_cache, 400 Handle<mirror::ClassLoader> class_loader, 401 bool is_static) 402 REQUIRES_SHARED(Locks::mutator_lock_) 403 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 404 405 // Resolve a field with a given ID from the DexFile associated with the given DexCache 406 // and ClassLoader, storing the result in DexCache. The ClassLinker and ClassLoader 407 // are used as in ResolveType. No is_static argument is provided so that Java 408 // field resolution semantics are followed. 409 EXPORT ArtField* ResolveFieldJLS(uint32_t field_idx, 410 Handle<mirror::DexCache> dex_cache, 411 Handle<mirror::ClassLoader> class_loader) 412 REQUIRES_SHARED(Locks::mutator_lock_) 413 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 414 415 // Find a field with a given ID from the DexFile associated with the given DexCache 416 // and ClassLoader, storing the result in DexCache. The declaring class is assumed 417 // to have been already resolved into `klass`. The `is_static` argument is used to 418 // determine if we are resolving a static or non-static field. 419 EXPORT ArtField* FindResolvedField(ObjPtr<mirror::Class> klass, 420 ObjPtr<mirror::DexCache> dex_cache, 421 ObjPtr<mirror::ClassLoader> class_loader, 422 uint32_t field_idx, 423 bool is_static) 424 REQUIRES_SHARED(Locks::mutator_lock_); 425 426 // Find a field with a given ID from the DexFile associated with the given DexCache 427 // and ClassLoader, storing the result in DexCache. The declaring class is assumed 428 // to have been already resolved into `klass`. No is_static argument is provided 429 // so that Java field resolution semantics are followed. 430 ArtField* FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, 431 ObjPtr<mirror::DexCache> dex_cache, 432 ObjPtr<mirror::ClassLoader> class_loader, 433 uint32_t field_idx) 434 REQUIRES_SHARED(Locks::mutator_lock_); 435 436 // Resolve a method type with a given ID from the DexFile associated with a given DexCache 437 // and ClassLoader, storing the result in the DexCache. 438 ObjPtr<mirror::MethodType> ResolveMethodType(Thread* self, 439 dex::ProtoIndex proto_idx, 440 Handle<mirror::DexCache> dex_cache, 441 Handle<mirror::ClassLoader> class_loader) 442 REQUIRES_SHARED(Locks::mutator_lock_) 443 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 444 445 EXPORT ObjPtr<mirror::MethodType> ResolveMethodType(Thread* self, 446 dex::ProtoIndex proto_idx, 447 ArtMethod* referrer) 448 REQUIRES_SHARED(Locks::mutator_lock_); 449 450 bool ResolveMethodType(Thread* self, 451 dex::ProtoIndex proto_idx, 452 Handle<mirror::DexCache> dex_cache, 453 Handle<mirror::ClassLoader> class_loader, 454 /*out*/ mirror::RawMethodType method_type) 455 REQUIRES_SHARED(Locks::mutator_lock_) 456 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 457 458 // Resolve a method handle with a given ID from the DexFile. The 459 // result is not cached in the DexCache as the instance will only be 460 // used once in most circumstances. 461 EXPORT ObjPtr<mirror::MethodHandle> ResolveMethodHandle(Thread* self, 462 uint32_t method_handle_idx, 463 ArtMethod* referrer) 464 REQUIRES_SHARED(Locks::mutator_lock_); 465 466 // Returns true on success, false if there's an exception pending. 467 // can_run_clinit=false allows the compiler to attempt to init a class, 468 // given the restriction that no <clinit> execution is possible. 469 EXPORT bool EnsureInitialized(Thread* self, 470 Handle<mirror::Class> c, 471 bool can_init_fields, 472 bool can_init_parents) 473 REQUIRES_SHARED(Locks::mutator_lock_) 474 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 475 476 // Initializes a few essential classes, namely `java.lang.Class`, 477 // `java.lang.Object` and `java.lang.reflect.Field`. 478 EXPORT void RunEarlyRootClinits(Thread* self) 479 REQUIRES_SHARED(Locks::mutator_lock_) 480 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 481 482 // Initializes classes that have instances in the image but that have 483 // <clinit> methods so they could not be initialized by the compiler. 484 void RunRootClinits(Thread* self) 485 REQUIRES_SHARED(Locks::mutator_lock_) 486 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 487 488 // Directly register an already existing dex cache. RegisterDexFile should be preferred since that 489 // reduplicates DexCaches when possible. The DexCache given to this function must already be fully 490 // initialized and not already registered. 491 EXPORT void RegisterExistingDexCache(ObjPtr<mirror::DexCache> cache, 492 ObjPtr<mirror::ClassLoader> class_loader) 493 REQUIRES(!Locks::dex_lock_) 494 REQUIRES_SHARED(Locks::mutator_lock_); 495 EXPORT ObjPtr<mirror::DexCache> RegisterDexFile(const DexFile& dex_file, 496 ObjPtr<mirror::ClassLoader> class_loader) 497 REQUIRES(!Locks::dex_lock_) 498 REQUIRES_SHARED(Locks::mutator_lock_); 499 GetBootClassPath()500 const std::vector<const DexFile*>& GetBootClassPath() { 501 return boot_class_path_; 502 } 503 504 EXPORT void VisitClasses(ClassVisitor* visitor) 505 REQUIRES(!Locks::classlinker_classes_lock_) 506 REQUIRES_SHARED(Locks::mutator_lock_); 507 508 // Visits only the classes in the boot class path. 509 template <typename Visitor> 510 inline void VisitBootClasses(Visitor* visitor) 511 REQUIRES_SHARED(Locks::classlinker_classes_lock_) 512 REQUIRES_SHARED(Locks::mutator_lock_); 513 // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage 514 // so that it can visit individual classes without holding the doesn't hold the 515 // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code 516 // can race with insertion and deletion of classes while the visitor is being called. 517 EXPORT void VisitClassesWithoutClassesLock(ClassVisitor* visitor) 518 REQUIRES_SHARED(Locks::mutator_lock_) 519 REQUIRES(!Locks::dex_lock_); 520 521 void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) 522 REQUIRES(!Locks::classlinker_classes_lock_, !Locks::trace_lock_) 523 REQUIRES_SHARED(Locks::mutator_lock_); 524 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags, bool visit_class_roots = true) 525 REQUIRES(!Locks::dex_lock_, !Locks::classlinker_classes_lock_, !Locks::trace_lock_) 526 REQUIRES_SHARED(Locks::mutator_lock_); 527 // Visits all dex-files accessible by any class-loader or the BCP. 528 template<typename Visitor> 529 void VisitKnownDexFiles(Thread* self, Visitor visitor) REQUIRES(Locks::mutator_lock_); 530 531 EXPORT bool IsDexFileRegistered(Thread* self, const DexFile& dex_file) 532 REQUIRES(!Locks::dex_lock_) 533 REQUIRES_SHARED(Locks::mutator_lock_); 534 EXPORT ObjPtr<mirror::DexCache> FindDexCache(Thread* self, const DexFile& dex_file) 535 REQUIRES(!Locks::dex_lock_) 536 REQUIRES_SHARED(Locks::mutator_lock_); 537 ObjPtr<mirror::DexCache> FindDexCache(Thread* self, const OatDexFile& oat_dex_file) 538 REQUIRES(!Locks::dex_lock_) REQUIRES_SHARED(Locks::mutator_lock_); 539 ClassTable* FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) 540 REQUIRES(!Locks::dex_lock_) 541 REQUIRES_SHARED(Locks::mutator_lock_); 542 543 LengthPrefixedArray<ArtField>* AllocArtFieldArray(Thread* self, 544 LinearAlloc* allocator, 545 size_t length); 546 547 LengthPrefixedArray<ArtMethod>* AllocArtMethodArray(Thread* self, 548 LinearAlloc* allocator, 549 size_t length); 550 551 // Convenience AllocClass() overload that uses mirror::Class::InitializeClassVisitor 552 // for the class initialization and uses the `java_lang_Class` from class roots 553 // instead of an explicit argument. 554 EXPORT ObjPtr<mirror::Class> AllocClass(Thread* self, uint32_t class_size) 555 REQUIRES_SHARED(Locks::mutator_lock_) 556 REQUIRES(!Roles::uninterruptible_); 557 558 // Setup the classloader, class def index, type idx so that we can insert this class in the class 559 // table. 560 EXPORT void SetupClass(const DexFile& dex_file, 561 const dex::ClassDef& dex_class_def, 562 Handle<mirror::Class> klass, 563 ObjPtr<mirror::ClassLoader> class_loader) 564 REQUIRES_SHARED(Locks::mutator_lock_); 565 566 EXPORT void LoadClass(Thread* self, 567 const DexFile& dex_file, 568 const dex::ClassDef& dex_class_def, 569 Handle<mirror::Class> klass) 570 REQUIRES_SHARED(Locks::mutator_lock_); 571 572 // Link the class and place it into the class-table using the given descriptor. NB if the 573 // descriptor is null the class will not be placed in any class-table. This is useful implementing 574 // obsolete classes and should not be used otherwise. 575 EXPORT bool LinkClass(Thread* self, 576 const char* descriptor, 577 Handle<mirror::Class> klass, 578 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 579 MutableHandle<mirror::Class>* h_new_class_out) 580 REQUIRES_SHARED(Locks::mutator_lock_) 581 REQUIRES(!Locks::classlinker_classes_lock_); 582 583 ObjPtr<mirror::PointerArray> AllocPointerArray(Thread* self, size_t length) 584 REQUIRES_SHARED(Locks::mutator_lock_) 585 REQUIRES(!Roles::uninterruptible_); 586 587 ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> AllocStackTraceElementArray(Thread* self, 588 size_t length) 589 REQUIRES_SHARED(Locks::mutator_lock_) 590 REQUIRES(!Roles::uninterruptible_); 591 592 EXPORT verifier::FailureKind VerifyClass( 593 Thread* self, 594 verifier::VerifierDeps* verifier_deps, 595 Handle<mirror::Class> klass, 596 verifier::HardFailLogMode log_level = verifier::HardFailLogMode::kLogNone) 597 REQUIRES_SHARED(Locks::mutator_lock_) 598 REQUIRES(!Locks::dex_lock_); 599 EXPORT // For `libarttest.so`. 600 bool VerifyClassUsingOatFile(Thread* self, 601 const DexFile& dex_file, 602 Handle<mirror::Class> klass, 603 ClassStatus& oat_file_class_status) 604 REQUIRES_SHARED(Locks::mutator_lock_) 605 REQUIRES(!Locks::dex_lock_); 606 void ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) 607 REQUIRES_SHARED(Locks::mutator_lock_) 608 REQUIRES(!Locks::dex_lock_); 609 void ResolveMethodExceptionHandlerTypes(ArtMethod* klass) 610 REQUIRES_SHARED(Locks::mutator_lock_) 611 REQUIRES(!Locks::dex_lock_); 612 613 ObjPtr<mirror::Class> CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, 614 jstring name, 615 jobjectArray interfaces, 616 jobject loader, 617 jobjectArray methods, 618 jobjectArray throws) 619 REQUIRES_SHARED(Locks::mutator_lock_); 620 621 pid_t GetClassesLockOwner(); // For SignalCatcher. 622 pid_t GetDexLockOwner(); // For SignalCatcher. 623 624 // Is the given entry point quick code to run the resolution stub? 625 EXPORT bool IsQuickResolutionStub(const void* entry_point) const; 626 627 // Is the given entry point quick code to bridge into the interpreter? 628 EXPORT bool IsQuickToInterpreterBridge(const void* entry_point) const; 629 630 // Is the given entry point quick code to run the generic JNI stub? 631 EXPORT bool IsQuickGenericJniStub(const void* entry_point) const; 632 633 // Is the given entry point the JNI dlsym lookup stub? 634 EXPORT bool IsJniDlsymLookupStub(const void* entry_point) const; 635 636 // Is the given entry point the JNI dlsym lookup critical stub? 637 EXPORT bool IsJniDlsymLookupCriticalStub(const void* entry_point) const; 638 639 // Is the given entry point the nterp trampoline? IsNterpTrampoline(const void * entry_point)640 bool IsNterpTrampoline(const void* entry_point) const { 641 return nterp_trampoline_ == entry_point; 642 } 643 IsNterpEntryPoint(const void * entry_point)644 bool IsNterpEntryPoint(const void* entry_point) const { 645 return entry_point == interpreter::GetNterpEntryPoint() || 646 entry_point == interpreter::GetNterpWithClinitEntryPoint(); 647 } 648 GetQuickToInterpreterBridgeTrampoline()649 const void* GetQuickToInterpreterBridgeTrampoline() const { 650 return quick_to_interpreter_bridge_trampoline_; 651 } 652 GetInternTable()653 InternTable* GetInternTable() const { 654 return intern_table_; 655 } 656 657 // Set the entrypoints up for an obsolete method. 658 EXPORT void SetEntryPointsForObsoleteMethod(ArtMethod* method) const 659 REQUIRES_SHARED(Locks::mutator_lock_); 660 661 // Attempts to insert a class into a class table. Returns null if 662 // the class was inserted, otherwise returns an existing class with 663 // the same descriptor and ClassLoader. 664 ObjPtr<mirror::Class> InsertClass(std::string_view descriptor, 665 ObjPtr<mirror::Class> klass, 666 size_t hash) 667 REQUIRES(!Locks::classlinker_classes_lock_) 668 REQUIRES_SHARED(Locks::mutator_lock_); 669 670 // Add an oat file with .bss GC roots to be visited again at the end of GC 671 // for collector types that need it. 672 void WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) 673 REQUIRES(!Locks::classlinker_classes_lock_) 674 REQUIRES_SHARED(Locks::mutator_lock_); 675 676 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 677 ObjPtr<mirror::ObjectArray<mirror::Class>> GetClassRoots() REQUIRES_SHARED(Locks::mutator_lock_); 678 679 // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring 680 // that no more classes are ever added to the pre zygote table which makes it that the pages 681 // always remain shared dirty instead of private dirty. 682 void MoveClassTableToPreZygote() 683 REQUIRES(!Locks::classlinker_classes_lock_) 684 REQUIRES_SHARED(Locks::mutator_lock_); 685 686 // Calls `CreateWellKnownClassLoader()` with `WellKnownClasses::dalvik_system_PathClassLoader`, 687 // and null parent and libraries. Wraps the result in a JNI global reference. 688 EXPORT jobject CreatePathClassLoader(Thread* self, const std::vector<const DexFile*>& dex_files) 689 REQUIRES_SHARED(Locks::mutator_lock_) 690 REQUIRES(!Locks::dex_lock_); 691 692 // Creates a `PathClassLoader`, `DelegateLastClassLoader` or `InMemoryDexClassLoader` 693 // (specified by loader_class) that can be used to load classes from the given dex files. 694 // The parent of the class loader will be set to `parent_loader`. If `parent_loader` is 695 // null the parent will be the boot class loader. 696 // If `loader_class` points to a different class than `PathClassLoader`, 697 // `DelegateLastClassLoader` or `InMemoryDexClassLoader` this method will abort. 698 // Note: the objects are not completely set up. Do not use this outside of tests and the compiler. 699 ObjPtr<mirror::ClassLoader> CreateWellKnownClassLoader( 700 Thread* self, 701 const std::vector<const DexFile*>& dex_files, 702 Handle<mirror::Class> loader_class, 703 Handle<mirror::ClassLoader> parent_loader, 704 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries, 705 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries_after) 706 REQUIRES_SHARED(Locks::mutator_lock_) 707 REQUIRES(!Locks::dex_lock_); 708 GetImagePointerSize()709 PointerSize GetImagePointerSize() const { 710 return image_pointer_size_; 711 } 712 713 // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache 714 // entries are roots, but potentially not image classes. 715 EXPORT void DropFindArrayClassCache() REQUIRES_SHARED(Locks::mutator_lock_); 716 717 // Clean up class loaders, this needs to happen after JNI weak globals are cleared. 718 void CleanupClassLoaders() 719 REQUIRES(!Locks::classlinker_classes_lock_) 720 REQUIRES_SHARED(Locks::mutator_lock_); 721 722 // Unlike GetOrCreateAllocatorForClassLoader, GetAllocatorForClassLoader asserts that the 723 // allocator for this class loader is already created. 724 EXPORT LinearAlloc* GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 725 REQUIRES_SHARED(Locks::mutator_lock_); 726 727 // Return the linear alloc for a class loader if it is already allocated, otherwise allocate and 728 // set it. TODO: Consider using a lock other than classlinker_classes_lock_. 729 LinearAlloc* GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 730 REQUIRES(!Locks::classlinker_classes_lock_) 731 REQUIRES_SHARED(Locks::mutator_lock_); 732 733 // May be called with null class_loader due to legacy code. b/27954959 734 void InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, 735 ObjPtr<mirror::ClassLoader> class_loader) 736 REQUIRES(!Locks::classlinker_classes_lock_) 737 REQUIRES_SHARED(Locks::mutator_lock_); 738 739 EXPORT static bool IsBootClassLoader(ObjPtr<mirror::Object> class_loader) 740 REQUIRES_SHARED(Locks::mutator_lock_); 741 742 ArtMethod* AddMethodToConflictTable(ObjPtr<mirror::Class> klass, 743 ArtMethod* conflict_method, 744 ArtMethod* interface_method, 745 ArtMethod* method) 746 REQUIRES_SHARED(Locks::mutator_lock_); 747 748 // Create a conflict table with a specified capacity. 749 ImtConflictTable* CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc); 750 751 // Static version for when the class linker is not yet created. 752 static ImtConflictTable* CreateImtConflictTable(size_t count, 753 LinearAlloc* linear_alloc, 754 PointerSize pointer_size); 755 756 757 // Create the IMT and conflict tables for a class. 758 EXPORT void FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) 759 REQUIRES_SHARED(Locks::mutator_lock_); 760 761 // Visit all of the class tables. This is used by dex2oat to allow pruning dex caches. 762 template <class Visitor> 763 void VisitClassTables(const Visitor& visitor) 764 REQUIRES(!Locks::classlinker_classes_lock_) 765 REQUIRES_SHARED(Locks::mutator_lock_); 766 767 // Visit all of the allocators that belong to classloaders except boot classloader. 768 // This is used by 616-cha-unloading test to confirm memory reuse. 769 EXPORT // For `libarttest.so`. 770 void VisitAllocators(AllocatorVisitor* visitor) const 771 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 772 773 // Throw the class initialization failure recorded when first trying to initialize the given 774 // class. 775 void ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, 776 bool wrap_in_no_class_def = false, 777 bool log = false) 778 REQUIRES_SHARED(Locks::mutator_lock_) 779 REQUIRES(!Locks::dex_lock_); 780 781 // Get the actual holding class for a copied method. Pretty slow, don't call often. 782 ObjPtr<mirror::Class> GetHoldingClassOfCopiedMethod(ArtMethod* method) 783 REQUIRES_SHARED(Locks::mutator_lock_); 784 785 // Get the class loader holding class for a copied method. 786 ObjPtr<mirror::ClassLoader> GetHoldingClassLoaderOfCopiedMethod(Thread* self, ArtMethod* method) 787 REQUIRES_SHARED(Locks::mutator_lock_) 788 REQUIRES(!Locks::classlinker_classes_lock_); 789 790 void GetClassLoaders(Thread* self, VariableSizedHandleScope* handles) 791 REQUIRES_SHARED(Locks::mutator_lock_) 792 REQUIRES(!Locks::classlinker_classes_lock_); 793 794 // Returns null if not found. 795 // This returns a pointer to the class-table, without requiring any locking - including the 796 // boot class-table. It is the caller's responsibility to access this under lock, if required. 797 EXPORT ClassTable* ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 798 REQUIRES_SHARED(Locks::mutator_lock_) 799 NO_THREAD_SAFETY_ANALYSIS; 800 801 // Dirty card in the card-table corresponding to the class_loader. Also log 802 // the root if we are logging new roots and class_loader is null. 803 void WriteBarrierOnClassLoaderLocked(ObjPtr<mirror::ClassLoader> class_loader, 804 ObjPtr<mirror::Object> root) 805 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::classlinker_classes_lock_); 806 void WriteBarrierOnClassLoader(Thread* self, 807 ObjPtr<mirror::ClassLoader> class_loader, 808 ObjPtr<mirror::Object> root) REQUIRES_SHARED(Locks::mutator_lock_) 809 REQUIRES(!Locks::classlinker_classes_lock_); 810 811 // DO NOT use directly. Use `Runtime::AppendToBootClassPath`. 812 void AppendToBootClassPath(Thread* self, const DexFile* dex_file) 813 REQUIRES_SHARED(Locks::mutator_lock_) 814 REQUIRES(!Locks::dex_lock_); 815 816 // DO NOT use directly. Use `Runtime::AppendToBootClassPath`. 817 void AppendToBootClassPath(const DexFile* dex_file, ObjPtr<mirror::DexCache> dex_cache) 818 REQUIRES_SHARED(Locks::mutator_lock_) 819 REQUIRES(!Locks::dex_lock_); 820 821 // Visit all of the class loaders in the class linker. 822 EXPORT void VisitClassLoaders(ClassLoaderVisitor* visitor) const 823 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 824 825 // Visit all of the dex caches in the class linker. 826 void VisitDexCaches(DexCacheVisitor* visitor) const 827 REQUIRES_SHARED(Locks::dex_lock_, Locks::mutator_lock_); 828 829 // Checks that a class and its superclass from another class loader have the same virtual methods. 830 EXPORT bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass) 831 REQUIRES_SHARED(Locks::mutator_lock_); 832 GetClassHierarchyAnalysis()833 ClassHierarchyAnalysis* GetClassHierarchyAnalysis() { 834 return cha_.get(); 835 } 836 837 EXPORT 838 void MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait /* ==> no locks held */); 839 840 // Registers the native method and returns the new entry point. NB The returned entry point 841 // might be different from the native_method argument if some MethodCallback modifies it. 842 const void* RegisterNative(Thread* self, ArtMethod* method, const void* native_method) 843 REQUIRES_SHARED(Locks::mutator_lock_); 844 845 // Unregister native code for a method. 846 void UnregisterNative(Thread* self, ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); 847 848 // Get the registered native method entrypoint, if any, otherwise null. 849 const void* GetRegisteredNative(Thread* self, ArtMethod* method) 850 REQUIRES_SHARED(Locks::mutator_lock_) 851 REQUIRES(!critical_native_code_with_clinit_check_lock_); 852 853 struct DexCacheData { 854 // Construct an invalid data object. DexCacheDataDexCacheData855 DexCacheData() : weak_root(nullptr), class_table(nullptr) { 856 static std::atomic_uint64_t s_registration_count(0); 857 registration_index = s_registration_count.fetch_add(1, std::memory_order_seq_cst); 858 } 859 DexCacheData(DexCacheData&&) = default; 860 861 // Weak root to the DexCache. Note: Do not decode this unnecessarily or else class unloading may 862 // not work properly. 863 jweak weak_root; 864 // Identify the associated class loader's class table. This is used to make sure that 865 // the Java call to native DexCache.setResolvedType() inserts the resolved type in that 866 // class table. It is also used to make sure we don't register the same dex cache with 867 // multiple class loaders. 868 ClassTable* class_table; 869 // Monotonically increasing integer which records the order in which DexFiles were registered. 870 // Used only to preserve determinism when creating compiled image. 871 uint64_t registration_index; 872 873 private: 874 DISALLOW_COPY_AND_ASSIGN(DexCacheData); 875 }; 876 877 // Forces a class to be marked as initialized without actually running initializers. Should only 878 // be used by plugin code when creating new classes directly. 879 EXPORT void ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) 880 REQUIRES_SHARED(Locks::mutator_lock_) 881 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 882 883 // Verifies if the method is accessible according to the SdkChecker (if installed). 884 virtual bool DenyAccessBasedOnPublicSdk(ArtMethod* art_method) const 885 REQUIRES_SHARED(Locks::mutator_lock_); 886 // Verifies if the field is accessible according to the SdkChecker (if installed). 887 virtual bool DenyAccessBasedOnPublicSdk(ArtField* art_field) const 888 REQUIRES_SHARED(Locks::mutator_lock_); 889 // Verifies if the descriptor is accessible according to the SdkChecker (if installed). 890 virtual bool DenyAccessBasedOnPublicSdk(std::string_view type_descriptor) const; 891 // Enable or disable public sdk checks. 892 virtual void SetEnablePublicSdkChecks(bool enabled); 893 894 // Transaction constraint checks for AOT compilation. 895 virtual bool TransactionWriteConstraint(Thread* self, ObjPtr<mirror::Object> obj) 896 REQUIRES_SHARED(Locks::mutator_lock_); 897 virtual bool TransactionWriteValueConstraint(Thread* self, ObjPtr<mirror::Object> value) 898 REQUIRES_SHARED(Locks::mutator_lock_); 899 virtual bool TransactionAllocationConstraint(Thread* self, ObjPtr<mirror::Class> klass) 900 REQUIRES_SHARED(Locks::mutator_lock_); 901 902 // Transaction bookkeeping for AOT compilation. 903 virtual void RecordWriteFieldBoolean(mirror::Object* obj, 904 MemberOffset field_offset, 905 uint8_t value, 906 bool is_volatile); 907 virtual void RecordWriteFieldByte(mirror::Object* obj, 908 MemberOffset field_offset, 909 int8_t value, 910 bool is_volatile); 911 virtual void RecordWriteFieldChar(mirror::Object* obj, 912 MemberOffset field_offset, 913 uint16_t value, 914 bool is_volatile); 915 virtual void RecordWriteFieldShort(mirror::Object* obj, 916 MemberOffset field_offset, 917 int16_t value, 918 bool is_volatile); 919 virtual void RecordWriteField32(mirror::Object* obj, 920 MemberOffset field_offset, 921 uint32_t value, 922 bool is_volatile); 923 virtual void RecordWriteField64(mirror::Object* obj, 924 MemberOffset field_offset, 925 uint64_t value, 926 bool is_volatile); 927 virtual void RecordWriteFieldReference(mirror::Object* obj, 928 MemberOffset field_offset, 929 ObjPtr<mirror::Object> value, 930 bool is_volatile) 931 REQUIRES_SHARED(Locks::mutator_lock_); 932 virtual void RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) 933 REQUIRES_SHARED(Locks::mutator_lock_); 934 virtual void RecordStrongStringInsertion(ObjPtr<mirror::String> s) 935 REQUIRES(Locks::intern_table_lock_); 936 virtual void RecordWeakStringInsertion(ObjPtr<mirror::String> s) 937 REQUIRES(Locks::intern_table_lock_); 938 virtual void RecordStrongStringRemoval(ObjPtr<mirror::String> s) 939 REQUIRES(Locks::intern_table_lock_); 940 virtual void RecordWeakStringRemoval(ObjPtr<mirror::String> s) 941 REQUIRES(Locks::intern_table_lock_); 942 virtual void RecordResolveString(ObjPtr<mirror::DexCache> dex_cache, dex::StringIndex string_idx) 943 REQUIRES_SHARED(Locks::mutator_lock_); 944 virtual void RecordResolveMethodType(ObjPtr<mirror::DexCache> dex_cache, 945 dex::ProtoIndex proto_idx) 946 REQUIRES_SHARED(Locks::mutator_lock_); 947 948 // Aborting transactions for AOT compilation. 949 virtual void ThrowTransactionAbortError(Thread* self) 950 REQUIRES_SHARED(Locks::mutator_lock_); 951 virtual void AbortTransactionF(Thread* self, const char* fmt, ...) 952 __attribute__((__format__(__printf__, 3, 4))) 953 REQUIRES_SHARED(Locks::mutator_lock_); 954 virtual void AbortTransactionV(Thread* self, const char* fmt, va_list args) 955 REQUIRES_SHARED(Locks::mutator_lock_); 956 virtual bool IsTransactionAborted() const; 957 958 // Visit transaction roots for AOT compilation. 959 virtual void VisitTransactionRoots(RootVisitor* visitor) 960 REQUIRES_SHARED(Locks::mutator_lock_); 961 962 // Get transactional switch interpreter entrypoint for AOT compilation. 963 virtual const void* GetTransactionalInterpreter(); 964 965 void RemoveDexFromCaches(const DexFile& dex_file); GetBootClassTable()966 ClassTable* GetBootClassTable() REQUIRES_SHARED(Locks::classlinker_classes_lock_) { 967 return boot_class_table_.get(); 968 } 969 // Find a matching JNI stub from boot images that we could reuse as entrypoint. 970 EXPORT const void* FindBootJniStub(ArtMethod* method) 971 REQUIRES_SHARED(Locks::mutator_lock_); 972 973 EXPORT const void* FindBootJniStub(uint32_t flags, std::string_view shorty); 974 975 const void* FindBootJniStub(JniStubKey key); 976 977 protected: 978 EXPORT virtual bool InitializeClass(Thread* self, 979 Handle<mirror::Class> klass, 980 bool can_run_clinit, 981 bool can_init_parents) 982 REQUIRES_SHARED(Locks::mutator_lock_) 983 REQUIRES(!Locks::dex_lock_); 984 985 EXPORT 986 virtual verifier::FailureKind PerformClassVerification(Thread* self, 987 verifier::VerifierDeps* verifier_deps, 988 Handle<mirror::Class> klass, 989 verifier::HardFailLogMode log_level, 990 std::string* error_msg) 991 REQUIRES_SHARED(Locks::mutator_lock_); 992 CanAllocClass()993 virtual bool CanAllocClass() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::dex_lock_) { 994 return true; 995 } 996 997 private: 998 class LinkFieldsHelper; 999 template <PointerSize kPointerSize> 1000 class LinkMethodsHelper; 1001 class MethodAnnotationsIterator; 1002 class OatClassCodeIterator; 1003 class VisiblyInitializedCallback; 1004 1005 struct ClassLoaderData { 1006 jweak weak_root; // Weak root to enable class unloading. 1007 ClassTable* class_table; 1008 LinearAlloc* allocator; 1009 }; 1010 1011 void VisiblyInitializedCallbackDone(Thread* self, VisiblyInitializedCallback* callback); 1012 VisiblyInitializedCallback* MarkClassInitialized(Thread* self, Handle<mirror::Class> klass) 1013 REQUIRES_SHARED(Locks::mutator_lock_); 1014 1015 // Ensures that the supertype of 'klass' ('supertype') is verified. Returns false and throws 1016 // appropriate exceptions if verification failed hard. Returns true for successful verification or 1017 // soft-failures. 1018 bool AttemptSupertypeVerification(Thread* self, 1019 verifier::VerifierDeps* verifier_deps, 1020 Handle<mirror::Class> klass, 1021 Handle<mirror::Class> supertype) 1022 REQUIRES(!Locks::dex_lock_) 1023 REQUIRES_SHARED(Locks::mutator_lock_); 1024 1025 // Prepare by removing dependencies on things allocated in data.allocator. 1026 // Please note that the allocator and class_table are not deleted in this 1027 // function. They are to be deleted after preparing all the class-loaders that 1028 // are to be deleted (see b/298575095). 1029 void PrepareToDeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) 1030 REQUIRES_SHARED(Locks::mutator_lock_); 1031 1032 void VisitClassesInternal(ClassVisitor* visitor) 1033 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 1034 1035 // Returns the number of zygote and image classes. 1036 size_t NumZygoteClasses() const 1037 REQUIRES(Locks::classlinker_classes_lock_) 1038 REQUIRES_SHARED(Locks::mutator_lock_); 1039 1040 // Returns the number of non zygote nor image classes. 1041 size_t NumNonZygoteClasses() const 1042 REQUIRES(Locks::classlinker_classes_lock_) 1043 REQUIRES_SHARED(Locks::mutator_lock_); 1044 1045 void FinishInit(Thread* self) 1046 REQUIRES_SHARED(Locks::mutator_lock_) 1047 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 1048 1049 // If we do not allow moving classes (`art::kMovingClass` is false) or if 1050 // parameter `kMovable` is false (or both), the class object is allocated in 1051 // the non-moving space. 1052 template <bool kMovable = true, class PreFenceVisitor> 1053 ObjPtr<mirror::Class> AllocClass(Thread* self, 1054 ObjPtr<mirror::Class> java_lang_Class, 1055 uint32_t class_size, 1056 const PreFenceVisitor& pre_fence_visitor) 1057 REQUIRES_SHARED(Locks::mutator_lock_) 1058 REQUIRES(!Roles::uninterruptible_); 1059 1060 // Convenience AllocClass() overload that uses mirror::Class::InitializeClassVisitor 1061 // for the class initialization. 1062 template <bool kMovable = true> 1063 ObjPtr<mirror::Class> AllocClass(Thread* self, 1064 ObjPtr<mirror::Class> java_lang_Class, 1065 uint32_t class_size) 1066 REQUIRES_SHARED(Locks::mutator_lock_) 1067 REQUIRES(!Roles::uninterruptible_); 1068 1069 // Allocate a primitive array class and store it in appropriate class root. 1070 void AllocPrimitiveArrayClass(Thread* self, 1071 ClassRoot primitive_root, 1072 ClassRoot array_root) 1073 REQUIRES_SHARED(Locks::mutator_lock_) 1074 REQUIRES(!Roles::uninterruptible_); 1075 1076 // Finish setup of an array class. 1077 void FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) 1078 REQUIRES_SHARED(Locks::mutator_lock_) 1079 REQUIRES(!Roles::uninterruptible_); 1080 1081 // Finish setup of a core array class (Object[], Class[], String[] and 1082 // primitive arrays) and insert it into the class table. 1083 void FinishCoreArrayClassSetup(ClassRoot array_root) 1084 REQUIRES_SHARED(Locks::mutator_lock_) 1085 REQUIRES(!Roles::uninterruptible_); 1086 1087 ObjPtr<mirror::DexCache> AllocDexCache(Thread* self, const DexFile& dex_file) 1088 REQUIRES_SHARED(Locks::mutator_lock_) 1089 REQUIRES(!Roles::uninterruptible_); 1090 1091 // Used for tests and AppendToBootClassPath. 1092 ObjPtr<mirror::DexCache> AllocAndInitializeDexCache(Thread* self, 1093 const DexFile& dex_file, 1094 ObjPtr<mirror::ClassLoader> class_loader) 1095 REQUIRES_SHARED(Locks::mutator_lock_) 1096 REQUIRES(!Locks::dex_lock_) 1097 REQUIRES(!Roles::uninterruptible_); 1098 1099 // Create a primitive class and store it in the appropriate class root. 1100 void CreatePrimitiveClass(Thread* self, Primitive::Type type, ClassRoot primitive_root) 1101 REQUIRES_SHARED(Locks::mutator_lock_) 1102 REQUIRES(!Roles::uninterruptible_); 1103 1104 ObjPtr<mirror::Class> CreateArrayClass(Thread* self, 1105 const char* descriptor, 1106 size_t descriptor_length, 1107 size_t hash, 1108 Handle<mirror::ClassLoader> class_loader) 1109 REQUIRES_SHARED(Locks::mutator_lock_) 1110 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 1111 1112 // Precomputes size needed for Class, in the case of a non-temporary class this size must be 1113 // sufficient to hold all static fields. 1114 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, 1115 const dex::ClassDef& dex_class_def); 1116 1117 void LoadField(const ClassAccessor::Field& field, Handle<mirror::Class> klass, ArtField* dst) 1118 REQUIRES_SHARED(Locks::mutator_lock_); 1119 1120 void LoadMethod(const DexFile& dex_file, 1121 const ClassAccessor::Method& method, 1122 ObjPtr<mirror::Class> klass, 1123 /*inout*/ MethodAnnotationsIterator* mai, 1124 /*out*/ ArtMethod* dst) 1125 REQUIRES_SHARED(Locks::mutator_lock_); 1126 1127 void LinkCode(ArtMethod* method, 1128 uint32_t class_def_method_index, 1129 /*inout*/ OatClassCodeIterator* occi) REQUIRES_SHARED(Locks::mutator_lock_); 1130 1131 void FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) 1132 REQUIRES_SHARED(Locks::mutator_lock_); 1133 1134 // Finds a class in a Path- or DexClassLoader, loading it if necessary without using JNI. Hash 1135 // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the 1136 // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader 1137 // was encountered while walking the parent chain (currently only BootClassLoader and 1138 // PathClassLoader are supported). 1139 bool FindClassInBaseDexClassLoader(Thread* self, 1140 const char* descriptor, 1141 size_t descriptor_length, 1142 size_t hash, 1143 Handle<mirror::ClassLoader> class_loader, 1144 /*out*/ ObjPtr<mirror::Class>* result) 1145 REQUIRES_SHARED(Locks::mutator_lock_) 1146 REQUIRES(!Locks::dex_lock_); 1147 1148 bool FindClassInSharedLibraries(Thread* self, 1149 const char* descriptor, 1150 size_t descriptor_length, 1151 size_t hash, 1152 Handle<mirror::ClassLoader> class_loader, 1153 /*out*/ ObjPtr<mirror::Class>* result) 1154 REQUIRES_SHARED(Locks::mutator_lock_) 1155 REQUIRES(!Locks::dex_lock_); 1156 1157 bool FindClassInSharedLibrariesHelper(Thread* self, 1158 const char* descriptor, 1159 size_t descriptor_length, 1160 size_t hash, 1161 Handle<mirror::ClassLoader> class_loader, 1162 ArtField* field, 1163 /*out*/ ObjPtr<mirror::Class>* result) 1164 REQUIRES_SHARED(Locks::mutator_lock_) 1165 REQUIRES(!Locks::dex_lock_); 1166 1167 bool FindClassInSharedLibrariesAfter(Thread* self, 1168 const char* descriptor, 1169 size_t descriptor_length, 1170 size_t hash, 1171 Handle<mirror::ClassLoader> class_loader, 1172 /*out*/ ObjPtr<mirror::Class>* result) 1173 REQUIRES_SHARED(Locks::mutator_lock_) 1174 REQUIRES(!Locks::dex_lock_); 1175 1176 // Finds the class in the classpath of the given class loader. It only searches the class loader 1177 // dex files and does not recurse into its parent. 1178 // The method checks that the provided class loader is either a PathClassLoader or a 1179 // DexClassLoader. 1180 // If the class is found the method updates `result`. 1181 // The method always returns true, to notify to the caller a 1182 // BaseDexClassLoader has a known lookup. 1183 bool FindClassInBaseDexClassLoaderClassPath( 1184 Thread* self, 1185 const char* descriptor, 1186 size_t descriptor_length, 1187 size_t hash, 1188 Handle<mirror::ClassLoader> class_loader, 1189 /*out*/ ObjPtr<mirror::Class>* result) 1190 REQUIRES_SHARED(Locks::mutator_lock_) 1191 REQUIRES(!Locks::dex_lock_); 1192 1193 // Finds the class in the boot class loader. 1194 // If the class is found the method updates `result`. 1195 // The method always returns true, to notify to the caller the 1196 // boot class loader has a known lookup. 1197 bool FindClassInBootClassLoaderClassPath(Thread* self, 1198 const char* descriptor, 1199 size_t descriptor_length, 1200 size_t hash, 1201 /*out*/ ObjPtr<mirror::Class>* result) 1202 REQUIRES_SHARED(Locks::mutator_lock_) 1203 REQUIRES(!Locks::dex_lock_); 1204 1205 // Implementation of LookupResolvedType() called when the type was not found in the dex cache. 1206 EXPORT ObjPtr<mirror::Class> DoLookupResolvedType(dex::TypeIndex type_idx, 1207 ObjPtr<mirror::Class> referrer) 1208 REQUIRES_SHARED(Locks::mutator_lock_); 1209 EXPORT ObjPtr<mirror::Class> DoLookupResolvedType(dex::TypeIndex type_idx, 1210 ObjPtr<mirror::DexCache> dex_cache, 1211 ObjPtr<mirror::ClassLoader> class_loader) 1212 REQUIRES_SHARED(Locks::mutator_lock_); 1213 1214 // Implementation of ResolveString() called when the string was not found in the dex cache. 1215 EXPORT ObjPtr<mirror::String> DoResolveString(dex::StringIndex string_idx, 1216 ObjPtr<mirror::DexCache> dex_cache) 1217 REQUIRES_SHARED(Locks::mutator_lock_); 1218 EXPORT ObjPtr<mirror::String> DoResolveString(dex::StringIndex string_idx, 1219 Handle<mirror::DexCache> dex_cache) 1220 REQUIRES_SHARED(Locks::mutator_lock_); 1221 1222 // Implementation of LookupString() called when the string was not found in the dex cache. 1223 EXPORT ObjPtr<mirror::String> DoLookupString(dex::StringIndex string_idx, 1224 ObjPtr<mirror::DexCache> dex_cache) 1225 REQUIRES_SHARED(Locks::mutator_lock_); 1226 1227 // Implementation of ResolveType() called when the type was not found in the dex cache. May be 1228 // used with ArtField*, ArtMethod* or ObjPtr<Class>. 1229 template <typename RefType> 1230 EXPORT ObjPtr<mirror::Class> DoResolveType(dex::TypeIndex type_idx, RefType referrer) 1231 REQUIRES_SHARED(Locks::mutator_lock_) 1232 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 1233 EXPORT ObjPtr<mirror::Class> DoResolveType(dex::TypeIndex type_idx, 1234 Handle<mirror::DexCache> dex_cache, 1235 Handle<mirror::ClassLoader> class_loader) 1236 REQUIRES_SHARED(Locks::mutator_lock_) 1237 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 1238 1239 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded 1240 // by the given 'class_loader'. Uses the provided hash for the descriptor. 1241 ObjPtr<mirror::Class> LookupClass(Thread* self, 1242 std::string_view descriptor, 1243 size_t hash, 1244 ObjPtr<mirror::ClassLoader> class_loader) 1245 REQUIRES(!Locks::classlinker_classes_lock_) 1246 REQUIRES_SHARED(Locks::mutator_lock_); 1247 1248 void RegisterDexFileLocked(const DexFile& dex_file, 1249 ObjPtr<mirror::DexCache> dex_cache, 1250 ObjPtr<mirror::ClassLoader> class_loader) 1251 REQUIRES(Locks::dex_lock_) 1252 REQUIRES_SHARED(Locks::mutator_lock_); 1253 const DexCacheData* FindDexCacheDataLocked(const DexFile& dex_file) 1254 REQUIRES_SHARED(Locks::dex_lock_); 1255 const DexCacheData* FindDexCacheDataLocked(const OatDexFile& oat_dex_file) 1256 REQUIRES_SHARED(Locks::dex_lock_); 1257 static ObjPtr<mirror::DexCache> DecodeDexCacheLocked(Thread* self, const DexCacheData* data) 1258 REQUIRES_SHARED(Locks::dex_lock_, Locks::mutator_lock_); 1259 bool IsSameClassLoader(ObjPtr<mirror::DexCache> dex_cache, 1260 const DexCacheData* data, 1261 ObjPtr<mirror::ClassLoader> class_loader) 1262 REQUIRES_SHARED(Locks::dex_lock_, Locks::mutator_lock_); 1263 1264 bool InitializeDefaultInterfaceRecursive(Thread* self, 1265 Handle<mirror::Class> klass, 1266 bool can_run_clinit, 1267 bool can_init_parents) 1268 REQUIRES(!Locks::dex_lock_) 1269 REQUIRES_SHARED(Locks::mutator_lock_); 1270 bool WaitForInitializeClass(Handle<mirror::Class> klass, 1271 Thread* self, 1272 ObjectLock<mirror::Class>& lock); 1273 1274 bool IsSameDescriptorInDifferentClassContexts(Thread* self, 1275 const char* descriptor, 1276 Handle<mirror::ClassLoader> class_loader1, 1277 Handle<mirror::ClassLoader> class_loader2) 1278 REQUIRES_SHARED(Locks::mutator_lock_); 1279 1280 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, 1281 ArtMethod* method, 1282 ObjPtr<mirror::Class> klass1, 1283 ObjPtr<mirror::Class> klass2) 1284 REQUIRES_SHARED(Locks::mutator_lock_); 1285 1286 bool LinkSuperClass(Handle<mirror::Class> klass) 1287 REQUIRES_SHARED(Locks::mutator_lock_); 1288 1289 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) 1290 REQUIRES_SHARED(Locks::mutator_lock_) 1291 REQUIRES(!Locks::dex_lock_); 1292 1293 bool LinkMethods(Thread* self, 1294 Handle<mirror::Class> klass, 1295 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 1296 bool* out_new_conflict, 1297 ArtMethod** out_imt) 1298 REQUIRES_SHARED(Locks::mutator_lock_); 1299 1300 ObjPtr<mirror::MethodHandle> ResolveMethodHandleForField( 1301 Thread* self, 1302 const dex::MethodHandleItem& method_handle, 1303 ArtMethod* referrer) REQUIRES_SHARED(Locks::mutator_lock_); 1304 1305 ObjPtr<mirror::MethodHandle> ResolveMethodHandleForMethod( 1306 Thread* self, 1307 const dex::MethodHandleItem& method_handle, 1308 ArtMethod* referrer) REQUIRES_SHARED(Locks::mutator_lock_); 1309 1310 bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) 1311 REQUIRES_SHARED(Locks::mutator_lock_); 1312 bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) 1313 REQUIRES_SHARED(Locks::mutator_lock_); 1314 bool VerifyRecordClass(Handle<mirror::Class> klass, ObjPtr<mirror::Class> super) 1315 REQUIRES_SHARED(Locks::mutator_lock_); 1316 1317 void CheckProxyConstructor(ArtMethod* constructor) const 1318 REQUIRES_SHARED(Locks::mutator_lock_); 1319 void CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const 1320 REQUIRES_SHARED(Locks::mutator_lock_); 1321 GetDexCacheCount()1322 size_t GetDexCacheCount() REQUIRES_SHARED(Locks::mutator_lock_, Locks::dex_lock_) { 1323 return dex_caches_.size(); 1324 } GetDexCachesData()1325 const std::unordered_map<const DexFile*, DexCacheData>& GetDexCachesData() 1326 REQUIRES_SHARED(Locks::mutator_lock_, Locks::dex_lock_) { 1327 return dex_caches_; 1328 } 1329 1330 void CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) 1331 REQUIRES_SHARED(Locks::mutator_lock_); 1332 void CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out) 1333 REQUIRES_SHARED(Locks::mutator_lock_); 1334 1335 // Register a class loader and create its class table and allocator. Should not be called if 1336 // these are already created. 1337 void RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 1338 REQUIRES_SHARED(Locks::mutator_lock_) 1339 REQUIRES(Locks::classlinker_classes_lock_); 1340 1341 // Insert a new class table if not found. 1342 ClassTable* InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 1343 REQUIRES_SHARED(Locks::mutator_lock_) 1344 REQUIRES(Locks::classlinker_classes_lock_); 1345 1346 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved 1347 // before returning it to the caller. Its the responsibility of the thread that placed the class 1348 // in the table to make it resolved. The thread doing resolution must notify on the class' lock 1349 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may 1350 // retire a class, the version of the class in the table is returned and this may differ from 1351 // the class passed in. 1352 ObjPtr<mirror::Class> EnsureResolved(Thread* self, 1353 std::string_view descriptor, 1354 ObjPtr<mirror::Class> klass) 1355 WARN_UNUSED 1356 REQUIRES_SHARED(Locks::mutator_lock_) 1357 REQUIRES(!Locks::dex_lock_); 1358 1359 void FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, 1360 ObjPtr<mirror::Class> new_class) 1361 REQUIRES_SHARED(Locks::mutator_lock_); 1362 1363 void SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) 1364 REQUIRES_SHARED(Locks::mutator_lock_); 1365 1366 // Return the quick generic JNI stub for testing. 1367 const void* GetRuntimeQuickGenericJniStub() const; 1368 1369 bool CanWeInitializeClass(ObjPtr<mirror::Class> klass, 1370 bool can_init_statics, 1371 bool can_init_parents) 1372 REQUIRES_SHARED(Locks::mutator_lock_); 1373 1374 void UpdateClassMethods(ObjPtr<mirror::Class> klass, 1375 LengthPrefixedArray<ArtMethod>* new_methods) 1376 REQUIRES_SHARED(Locks::mutator_lock_) 1377 REQUIRES(!Locks::classlinker_classes_lock_); 1378 1379 // Check that c1 == FindSystemClass(self, descriptor). Abort with class dumps otherwise. 1380 void CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) 1381 REQUIRES(!Locks::dex_lock_) 1382 REQUIRES_SHARED(Locks::mutator_lock_); 1383 1384 // Sets imt_ref appropriately for LinkInterfaceMethods. 1385 // If there is no method in the imt location of imt_ref it will store the given method there. 1386 // Otherwise it will set the conflict method which will figure out which method to use during 1387 // runtime. 1388 void SetIMTRef(ArtMethod* unimplemented_method, 1389 ArtMethod* imt_conflict_method, 1390 ArtMethod* current_method, 1391 /*out*/bool* new_conflict, 1392 /*out*/ArtMethod** imt_ref) REQUIRES_SHARED(Locks::mutator_lock_); 1393 1394 void FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, 1395 ArtMethod* unimplemented_method, 1396 ArtMethod* imt_conflict_method, 1397 ObjPtr<mirror::Class> klass, 1398 bool create_conflict_tables, 1399 bool ignore_copied_methods, 1400 /*out*/bool* new_conflict, 1401 /*out*/ArtMethod** imt) REQUIRES_SHARED(Locks::mutator_lock_); 1402 1403 ObjPtr<mirror::IfTable> GetArrayIfTable() REQUIRES_SHARED(Locks::mutator_lock_); 1404 1405 bool OpenAndInitImageDexFiles(const gc::space::ImageSpace* space, 1406 Handle<mirror::ClassLoader> class_loader, 1407 std::vector<std::unique_ptr<const DexFile>>* out_dex_files, 1408 std::string* error_msg) REQUIRES(!Locks::dex_lock_) 1409 REQUIRES_SHARED(Locks::mutator_lock_); 1410 1411 bool AddImageSpace(gc::space::ImageSpace* space, 1412 Handle<mirror::ClassLoader> class_loader, 1413 ClassLoaderContext* context, 1414 const std::vector<std::unique_ptr<const DexFile>>& dex_files, 1415 std::string* error_msg) REQUIRES(!Locks::dex_lock_) 1416 REQUIRES_SHARED(Locks::mutator_lock_); 1417 1418 std::vector<const DexFile*> boot_class_path_; 1419 std::vector<std::unique_ptr<const DexFile>> boot_dex_files_; 1420 1421 // JNI weak globals and side data to allow dex caches to get unloaded. We lazily delete weak 1422 // globals when we register new dex files. 1423 std::unordered_map<const DexFile*, DexCacheData> dex_caches_ GUARDED_BY(Locks::dex_lock_); 1424 1425 // This contains the class loaders which have class tables. It is populated by 1426 // InsertClassTableForClassLoader. 1427 std::list<ClassLoaderData> class_loaders_ 1428 GUARDED_BY(Locks::classlinker_classes_lock_); 1429 1430 // Boot class path table. Since the class loader for this is null. 1431 std::unique_ptr<ClassTable> boot_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_); 1432 1433 // New gc-roots, only used by CMS/CMC since the GC needs to mark these in the pause. 1434 std::vector<GcRoot<mirror::Object>> new_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 1435 1436 // Boot image oat files with new .bss GC roots to be visited in the pause by CMS. 1437 std::vector<const OatFile*> new_bss_roots_boot_oat_files_ 1438 GUARDED_BY(Locks::classlinker_classes_lock_); 1439 1440 // Number of times we've searched dex caches for a class. After a certain number of misses we move 1441 // the classes into the class_table_ to avoid dex cache based searches. 1442 Atomic<uint32_t> failed_dex_cache_class_lookups_; 1443 1444 // Well known mirror::Class roots. 1445 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_; 1446 1447 // Method hashes for virtual methods from java.lang.Object used 1448 // to avoid recalculating them for each class we link. 1449 uint32_t object_virtual_method_hashes_[mirror::Object::kVTableLength]; 1450 1451 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class 1452 // descriptors for the sake of performing FindClass. 1453 static constexpr size_t kFindArrayCacheSize = 16; 1454 std::atomic<GcRoot<mirror::Class>> find_array_class_cache_[kFindArrayCacheSize]; 1455 size_t find_array_class_cache_next_victim_; 1456 1457 bool init_done_; 1458 bool log_new_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 1459 1460 InternTable* intern_table_; 1461 1462 const bool fast_class_not_found_exceptions_; 1463 1464 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single 1465 // patch point within the image. TODO: make these proper relocations. 1466 const void* jni_dlsym_lookup_trampoline_; 1467 const void* jni_dlsym_lookup_critical_trampoline_; 1468 const void* quick_resolution_trampoline_; 1469 const void* quick_imt_conflict_trampoline_; 1470 const void* quick_generic_jni_trampoline_; 1471 const void* quick_to_interpreter_bridge_trampoline_; 1472 const void* nterp_trampoline_; 1473 1474 // Image pointer size. 1475 PointerSize image_pointer_size_; 1476 1477 // Classes to transition from ClassStatus::kInitialized to ClassStatus::kVisiblyInitialized. 1478 Mutex visibly_initialized_callback_lock_; 1479 std::unique_ptr<VisiblyInitializedCallback> visibly_initialized_callback_ 1480 GUARDED_BY(visibly_initialized_callback_lock_); 1481 IntrusiveForwardList<VisiblyInitializedCallback> running_visibly_initialized_callbacks_ 1482 GUARDED_BY(visibly_initialized_callback_lock_); 1483 1484 // Whether to use `membarrier()` to make classes visibly initialized. 1485 bool visibly_initialize_classes_with_membarier_; 1486 1487 // Registered native code for @CriticalNative methods of classes that are not visibly 1488 // initialized. These code pointers cannot be stored in ArtMethod as that would risk 1489 // skipping the class initialization check for direct calls from compiled code. 1490 Mutex critical_native_code_with_clinit_check_lock_; 1491 std::map<ArtMethod*, void*> critical_native_code_with_clinit_check_ 1492 GUARDED_BY(critical_native_code_with_clinit_check_lock_); 1493 1494 // Load unique JNI stubs from boot images. If the subsequently loaded native methods could find a 1495 // matching stub, then reuse it without JIT/AOT compilation. 1496 JniStubHashMap<const void*> boot_image_jni_stubs_; 1497 1498 std::unique_ptr<ClassHierarchyAnalysis> cha_; 1499 1500 class FindVirtualMethodHolderVisitor; 1501 1502 friend class AppImageLoadingHelper; 1503 friend class ImageDumper; // for DexLock 1504 friend struct linker::CompilationHelper; // For Compile in ImageTest. 1505 friend class linker::ImageWriter; // for GetClassRoots 1506 friend class JniCompilerTest; // for GetRuntimeQuickGenericJniStub 1507 friend class JniInternalTest; // for GetRuntimeQuickGenericJniStub 1508 friend class VerifyClassesFuzzerHelper; // for FindDexCacheDataLocked. 1509 friend class VerifyClassesFuzzerCorpusTestHelper; // for FindDexCacheDataLocked. 1510 friend class VMClassLoader; // for LookupClass and FindClassInBaseDexClassLoader. 1511 ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for DexLock, and RegisterDexFileLocked 1512 ART_FRIEND_TEST(mirror::DexCacheMethodHandlesTest, Open); // for AllocDexCache 1513 ART_FRIEND_TEST(mirror::DexCacheTest, Open); // for AllocDexCache 1514 DISALLOW_COPY_AND_ASSIGN(ClassLinker); 1515 }; 1516 1517 class ClassLoadCallback { 1518 public: ~ClassLoadCallback()1519 virtual ~ClassLoadCallback() {} 1520 1521 // Called immediately before beginning class-definition and immediately before returning from it. BeginDefineClass()1522 virtual void BeginDefineClass() REQUIRES_SHARED(Locks::mutator_lock_) {} EndDefineClass()1523 virtual void EndDefineClass() REQUIRES_SHARED(Locks::mutator_lock_) {} 1524 1525 // If set we will replace initial_class_def & initial_dex_file with the final versions. The 1526 // callback author is responsible for ensuring these are allocated in such a way they can be 1527 // cleaned up if another transformation occurs. Note that both must be set or null/unchanged on 1528 // return. 1529 // Note: the class may be temporary, in which case a following ClassPrepare event will be a 1530 // different object. It is the listener's responsibility to handle this. 1531 // Note: This callback is rarely useful so a default implementation has been given that does 1532 // nothing. ClassPreDefine(const char * descriptor,Handle<mirror::Class> klass,Handle<mirror::ClassLoader> class_loader,const DexFile & initial_dex_file,const dex::ClassDef & initial_class_def,DexFile const ** final_dex_file,dex::ClassDef const ** final_class_def)1533 virtual void ClassPreDefine([[maybe_unused]] const char* descriptor, 1534 [[maybe_unused]] Handle<mirror::Class> klass, 1535 [[maybe_unused]] Handle<mirror::ClassLoader> class_loader, 1536 [[maybe_unused]] const DexFile& initial_dex_file, 1537 [[maybe_unused]] const dex::ClassDef& initial_class_def, 1538 [[maybe_unused]] /*out*/ DexFile const** final_dex_file, 1539 [[maybe_unused]] /*out*/ dex::ClassDef const** final_class_def) 1540 REQUIRES_SHARED(Locks::mutator_lock_) {} 1541 1542 // A class has been loaded. 1543 // Note: the class may be temporary, in which case a following ClassPrepare event will be a 1544 // different object. It is the listener's responsibility to handle this. 1545 virtual void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) = 0; 1546 1547 // A class has been prepared, i.e., resolved. As the ClassLoad event might have been for a 1548 // temporary class, provide both the former and the current class. 1549 virtual void ClassPrepare(Handle<mirror::Class> temp_klass, 1550 Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) = 0; 1551 }; 1552 1553 } // namespace art 1554 1555 #endif // ART_RUNTIME_CLASS_LINKER_H_ 1556