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