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