class_linker.h revision 921094ad3af4484537e19d62235f22792b7ce595
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 <set> 21#include <string> 22#include <unordered_map> 23#include <unordered_set> 24#include <utility> 25#include <vector> 26 27#include "base/allocator.h" 28#include "base/enums.h" 29#include "base/hash_set.h" 30#include "base/macros.h" 31#include "base/mutex.h" 32#include "class_table.h" 33#include "dex_cache_resolved_classes.h" 34#include "dex_file.h" 35#include "dex_file_types.h" 36#include "gc_root.h" 37#include "jni.h" 38#include "mirror/class.h" 39#include "object_callbacks.h" 40#include "verifier/method_verifier.h" 41#include "verifier/verifier_log_mode.h" 42 43namespace art { 44 45namespace gc { 46namespace space { 47 class ImageSpace; 48} // namespace space 49} // namespace gc 50namespace mirror { 51 class ClassLoader; 52 class DexCache; 53 class DexCachePointerArray; 54 class DexCacheMethodHandlesTest_Open_Test; 55 class DexCacheTest_Open_Test; 56 class IfTable; 57 class MethodType; 58 template<class T> class ObjectArray; 59 class StackTraceElement; 60} // namespace mirror 61 62template<class T> class Handle; 63class ImtConflictTable; 64template<typename T> class LengthPrefixedArray; 65template<class T> class MutableHandle; 66class InternTable; 67template<class T> class ObjectLock; 68class Runtime; 69class ScopedObjectAccessAlreadyRunnable; 70template<size_t kNumReferences> class PACKED(4) StackHandleScope; 71 72enum VisitRootFlags : uint8_t; 73 74class ClassVisitor { 75 public: 76 virtual ~ClassVisitor() {} 77 // Return true to continue visiting. 78 virtual bool operator()(ObjPtr<mirror::Class> klass) = 0; 79}; 80 81class ClassLoaderVisitor { 82 public: 83 virtual ~ClassLoaderVisitor() {} 84 virtual void Visit(ObjPtr<mirror::ClassLoader> class_loader) 85 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0; 86}; 87 88class ClassLinker { 89 public: 90 // Well known mirror::Class roots accessed via GetClassRoot. 91 enum ClassRoot { 92 kJavaLangClass, 93 kJavaLangObject, 94 kClassArrayClass, 95 kObjectArrayClass, 96 kJavaLangString, 97 kJavaLangDexCache, 98 kJavaLangRefReference, 99 kJavaLangReflectConstructor, 100 kJavaLangReflectField, 101 kJavaLangReflectMethod, 102 kJavaLangReflectProxy, 103 kJavaLangStringArrayClass, 104 kJavaLangReflectConstructorArrayClass, 105 kJavaLangReflectFieldArrayClass, 106 kJavaLangReflectMethodArrayClass, 107 kJavaLangInvokeMethodHandleImpl, 108 kJavaLangInvokeMethodType, 109 kJavaLangClassLoader, 110 kJavaLangThrowable, 111 kJavaLangClassNotFoundException, 112 kJavaLangStackTraceElement, 113 kDalvikSystemEmulatedStackFrame, 114 kPrimitiveBoolean, 115 kPrimitiveByte, 116 kPrimitiveChar, 117 kPrimitiveDouble, 118 kPrimitiveFloat, 119 kPrimitiveInt, 120 kPrimitiveLong, 121 kPrimitiveShort, 122 kPrimitiveVoid, 123 kBooleanArrayClass, 124 kByteArrayClass, 125 kCharArrayClass, 126 kDoubleArrayClass, 127 kFloatArrayClass, 128 kIntArrayClass, 129 kLongArrayClass, 130 kShortArrayClass, 131 kJavaLangStackTraceElementArrayClass, 132 kDalvikSystemClassExt, 133 kClassRootsMax, 134 }; 135 136 explicit ClassLinker(InternTable* intern_table); 137 ~ClassLinker(); 138 139 // Initialize class linker by bootstraping from dex files. 140 bool InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, 141 std::string* error_msg) 142 REQUIRES_SHARED(Locks::mutator_lock_) 143 REQUIRES(!Locks::dex_lock_); 144 145 // Initialize class linker from one or more boot images. 146 bool InitFromBootImage(std::string* error_msg) 147 REQUIRES_SHARED(Locks::mutator_lock_) 148 REQUIRES(!Locks::dex_lock_); 149 150 // Add an image space to the class linker, may fix up classloader fields and dex cache fields. 151 // The dex files that were newly opened for the space are placed in the out argument 152 // out_dex_files. Returns true if the operation succeeded. 153 // The space must be already added to the heap before calling AddImageSpace since we need to 154 // properly handle read barriers and object marking. 155 bool AddImageSpace(gc::space::ImageSpace* space, 156 Handle<mirror::ClassLoader> class_loader, 157 jobjectArray dex_elements, 158 const char* dex_location, 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 bool OpenImageDexFiles(gc::space::ImageSpace* space, 165 std::vector<std::unique_ptr<const DexFile>>* out_dex_files, 166 std::string* error_msg) 167 REQUIRES(!Locks::dex_lock_) 168 REQUIRES_SHARED(Locks::mutator_lock_); 169 170 // Finds a class by its descriptor, loading it if necessary. 171 // If class_loader is null, searches boot_class_path_. 172 mirror::Class* FindClass(Thread* self, 173 const char* descriptor, 174 Handle<mirror::ClassLoader> class_loader) 175 REQUIRES_SHARED(Locks::mutator_lock_) 176 REQUIRES(!Locks::dex_lock_); 177 178 // Finds a class by its descriptor using the "system" class loader, ie by searching the 179 // boot_class_path_. 180 mirror::Class* FindSystemClass(Thread* self, const char* descriptor) 181 REQUIRES_SHARED(Locks::mutator_lock_) 182 REQUIRES(!Locks::dex_lock_); 183 184 // Finds the array class given for the element class. 185 mirror::Class* FindArrayClass(Thread* self, ObjPtr<mirror::Class>* element_class) 186 REQUIRES_SHARED(Locks::mutator_lock_) 187 REQUIRES(!Locks::dex_lock_); 188 189 // Returns true if the class linker is initialized. 190 bool IsInitialized() const { 191 return init_done_; 192 } 193 194 // Define a new a class based on a ClassDef from a DexFile 195 mirror::Class* DefineClass(Thread* self, 196 const char* descriptor, 197 size_t hash, 198 Handle<mirror::ClassLoader> class_loader, 199 const DexFile& dex_file, 200 const DexFile::ClassDef& dex_class_def) 201 REQUIRES_SHARED(Locks::mutator_lock_) 202 REQUIRES(!Locks::dex_lock_); 203 204 // Finds a class by its descriptor, returning null if it isn't wasn't loaded 205 // by the given 'class_loader'. 206 mirror::Class* LookupClass(Thread* self, 207 const char* descriptor, 208 ObjPtr<mirror::ClassLoader> class_loader) 209 REQUIRES(!Locks::classlinker_classes_lock_) 210 REQUIRES_SHARED(Locks::mutator_lock_) { 211 return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); 212 } 213 214 // Finds all the classes with the given descriptor, regardless of ClassLoader. 215 void LookupClasses(const char* descriptor, std::vector<ObjPtr<mirror::Class>>& classes) 216 REQUIRES(!Locks::classlinker_classes_lock_) 217 REQUIRES_SHARED(Locks::mutator_lock_); 218 219 mirror::Class* FindPrimitiveClass(char type) REQUIRES_SHARED(Locks::mutator_lock_); 220 221 void DumpForSigQuit(std::ostream& os) REQUIRES(!Locks::classlinker_classes_lock_); 222 223 size_t NumLoadedClasses() 224 REQUIRES(!Locks::classlinker_classes_lock_) 225 REQUIRES_SHARED(Locks::mutator_lock_); 226 227 // Resolve a String with the given index from the DexFile, storing the 228 // result in the DexCache. The referrer is used to identify the 229 // target DexCache and ClassLoader to use for resolution. 230 mirror::String* ResolveString(dex::StringIndex string_idx, ArtMethod* referrer) 231 REQUIRES_SHARED(Locks::mutator_lock_); 232 233 // Resolve a String with the given index from the DexFile, storing the 234 // result in the DexCache. 235 mirror::String* ResolveString(const DexFile& dex_file, 236 dex::StringIndex string_idx, 237 Handle<mirror::DexCache> dex_cache) 238 REQUIRES_SHARED(Locks::mutator_lock_); 239 240 // Find a String with the given index from the DexFile, storing the 241 // result in the DexCache if found. Return null if not found. 242 mirror::String* LookupString(const DexFile& dex_file, 243 dex::StringIndex string_idx, 244 Handle<mirror::DexCache> dex_cache) 245 REQUIRES_SHARED(Locks::mutator_lock_); 246 247 // Resolve a Type with the given index from the DexFile, storing the 248 // result in the DexCache. The referrer is used to identify the 249 // target DexCache and ClassLoader to use for resolution. 250 mirror::Class* ResolveType(const DexFile& dex_file, 251 dex::TypeIndex type_idx, 252 ObjPtr<mirror::Class> referrer) 253 REQUIRES_SHARED(Locks::mutator_lock_) 254 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 255 256 // Resolve a Type with the given index from the DexFile, storing the 257 // result in the DexCache. The referrer is used to identify the 258 // target DexCache and ClassLoader to use for resolution. 259 mirror::Class* ResolveType(dex::TypeIndex type_idx, ArtMethod* referrer) 260 REQUIRES_SHARED(Locks::mutator_lock_) 261 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 262 263 mirror::Class* ResolveType(dex::TypeIndex type_idx, ArtField* referrer) 264 REQUIRES_SHARED(Locks::mutator_lock_) 265 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 266 267 // Look up a resolved type with the given ID from the DexFile. The ClassLoader is used to search 268 // for the type, since it may be referenced from but not contained within the given DexFile. 269 ObjPtr<mirror::Class> LookupResolvedType(const DexFile& dex_file, 270 dex::TypeIndex type_idx, 271 ObjPtr<mirror::DexCache> dex_cache, 272 ObjPtr<mirror::ClassLoader> class_loader) 273 REQUIRES_SHARED(Locks::mutator_lock_); 274 275 // Resolve a type with the given ID from the DexFile, storing the 276 // result in DexCache. The ClassLoader is used to search for the 277 // type, since it may be referenced from but not contained within 278 // the given DexFile. 279 mirror::Class* ResolveType(const DexFile& dex_file, 280 dex::TypeIndex type_idx, 281 Handle<mirror::DexCache> dex_cache, 282 Handle<mirror::ClassLoader> class_loader) 283 REQUIRES_SHARED(Locks::mutator_lock_) 284 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 285 286 // Determine whether a dex cache result should be trusted, or an IncompatibleClassChangeError 287 // check should be performed even after a hit. 288 enum ResolveMode { // private. 289 kNoICCECheckForCache, 290 kForceICCECheck 291 }; 292 293 // Resolve a method with a given ID from the DexFile, storing the 294 // result in DexCache. The ClassLinker and ClassLoader are used as 295 // in ResolveType. What is unique is the method type argument which 296 // is used to determine if this method is a direct, static, or 297 // virtual method. 298 template <ResolveMode kResolveMode> 299 ArtMethod* ResolveMethod(const DexFile& dex_file, 300 uint32_t method_idx, 301 Handle<mirror::DexCache> dex_cache, 302 Handle<mirror::ClassLoader> class_loader, 303 ArtMethod* referrer, 304 InvokeType type) 305 REQUIRES_SHARED(Locks::mutator_lock_) 306 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 307 308 ArtMethod* GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) 309 REQUIRES_SHARED(Locks::mutator_lock_); 310 311 // This returns the class referred to by GetMethodId(method_idx).class_idx_. This might be 312 // different then the declaring class of the resolved method due to copied 313 // miranda/default/conflict methods. 314 mirror::Class* ResolveReferencedClassOfMethod(uint32_t method_idx, 315 Handle<mirror::DexCache> dex_cache, 316 Handle<mirror::ClassLoader> class_loader) 317 REQUIRES_SHARED(Locks::mutator_lock_) 318 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 319 template <ResolveMode kResolveMode> 320 ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, ArtMethod* referrer, InvokeType type) 321 REQUIRES_SHARED(Locks::mutator_lock_) 322 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 323 ArtMethod* ResolveMethodWithoutInvokeType(const DexFile& dex_file, 324 uint32_t method_idx, 325 Handle<mirror::DexCache> dex_cache, 326 Handle<mirror::ClassLoader> class_loader) 327 REQUIRES_SHARED(Locks::mutator_lock_) 328 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 329 330 ArtField* GetResolvedField(uint32_t field_idx, ObjPtr<mirror::DexCache> dex_cache) 331 REQUIRES_SHARED(Locks::mutator_lock_); 332 ArtField* ResolveField(uint32_t field_idx, ArtMethod* referrer, bool is_static) 333 REQUIRES_SHARED(Locks::mutator_lock_) 334 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 335 336 // Resolve a field with a given ID from the DexFile, storing the 337 // result in DexCache. The ClassLinker and ClassLoader are used as 338 // in ResolveType. What is unique is the is_static argument which is 339 // used to determine if we are resolving a static or non-static 340 // field. 341 ArtField* ResolveField(const DexFile& dex_file, uint32_t field_idx, 342 Handle<mirror::DexCache> dex_cache, 343 Handle<mirror::ClassLoader> class_loader, 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, storing the 348 // result in DexCache. The ClassLinker and ClassLoader are used as 349 // in ResolveType. No is_static argument is provided so that Java 350 // field resolution semantics are followed. 351 ArtField* ResolveFieldJLS(const DexFile& dex_file, 352 uint32_t field_idx, 353 Handle<mirror::DexCache> dex_cache, 354 Handle<mirror::ClassLoader> class_loader) 355 REQUIRES_SHARED(Locks::mutator_lock_) 356 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 357 358 // Resolve a method type with a given ID from the DexFile, storing 359 // the result in the DexCache. 360 mirror::MethodType* ResolveMethodType(const DexFile& dex_file, 361 uint32_t proto_idx, 362 Handle<mirror::DexCache> dex_cache, 363 Handle<mirror::ClassLoader> class_loader) 364 REQUIRES_SHARED(Locks::mutator_lock_) 365 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 366 367 // Returns true on success, false if there's an exception pending. 368 // can_run_clinit=false allows the compiler to attempt to init a class, 369 // given the restriction that no <clinit> execution is possible. 370 bool EnsureInitialized(Thread* self, 371 Handle<mirror::Class> c, 372 bool can_init_fields, 373 bool can_init_parents) 374 REQUIRES_SHARED(Locks::mutator_lock_) 375 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 376 377 // Initializes classes that have instances in the image but that have 378 // <clinit> methods so they could not be initialized by the compiler. 379 void RunRootClinits() 380 REQUIRES_SHARED(Locks::mutator_lock_) 381 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 382 383 mirror::DexCache* RegisterDexFile(const DexFile& dex_file, 384 ObjPtr<mirror::ClassLoader> class_loader) 385 REQUIRES(!Locks::dex_lock_) 386 REQUIRES_SHARED(Locks::mutator_lock_); 387 void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 388 REQUIRES(!Locks::dex_lock_) 389 REQUIRES_SHARED(Locks::mutator_lock_); 390 391 const std::vector<const DexFile*>& GetBootClassPath() { 392 return boot_class_path_; 393 } 394 395 void VisitClasses(ClassVisitor* visitor) 396 REQUIRES(!Locks::classlinker_classes_lock_) 397 REQUIRES_SHARED(Locks::mutator_lock_); 398 399 // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage 400 // so that it can visit individual classes without holding the doesn't hold the 401 // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code 402 // can race with insertion and deletion of classes while the visitor is being called. 403 void VisitClassesWithoutClassesLock(ClassVisitor* visitor) 404 REQUIRES_SHARED(Locks::mutator_lock_) 405 REQUIRES(!Locks::dex_lock_); 406 407 void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) 408 REQUIRES(!Locks::classlinker_classes_lock_, !Locks::trace_lock_) 409 REQUIRES_SHARED(Locks::mutator_lock_); 410 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) 411 REQUIRES(!Locks::dex_lock_, !Locks::classlinker_classes_lock_, !Locks::trace_lock_) 412 REQUIRES_SHARED(Locks::mutator_lock_); 413 414 mirror::DexCache* FindDexCache(Thread* self, 415 const DexFile& dex_file, 416 bool allow_failure = false) 417 REQUIRES(!Locks::dex_lock_) 418 REQUIRES_SHARED(Locks::mutator_lock_); 419 void FixupDexCaches(ArtMethod* resolution_method) 420 REQUIRES(!Locks::dex_lock_) 421 REQUIRES_SHARED(Locks::mutator_lock_); 422 423 // Allocate an instance of a java.lang.Object. 424 mirror::Object* AllocObject(Thread* self) 425 REQUIRES_SHARED(Locks::mutator_lock_) 426 REQUIRES(!Roles::uninterruptible_); 427 428 // TODO: replace this with multiple methods that allocate the correct managed type. 429 template <class T> 430 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length) 431 REQUIRES_SHARED(Locks::mutator_lock_) 432 REQUIRES(!Roles::uninterruptible_); 433 434 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length) 435 REQUIRES_SHARED(Locks::mutator_lock_) 436 REQUIRES(!Roles::uninterruptible_); 437 438 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length) 439 REQUIRES_SHARED(Locks::mutator_lock_) 440 REQUIRES(!Roles::uninterruptible_); 441 442 LengthPrefixedArray<ArtField>* AllocArtFieldArray(Thread* self, 443 LinearAlloc* allocator, 444 size_t length); 445 446 LengthPrefixedArray<ArtMethod>* AllocArtMethodArray(Thread* self, 447 LinearAlloc* allocator, 448 size_t length); 449 450 mirror::PointerArray* AllocPointerArray(Thread* self, size_t length) 451 REQUIRES_SHARED(Locks::mutator_lock_) 452 REQUIRES(!Roles::uninterruptible_); 453 454 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount) 455 REQUIRES_SHARED(Locks::mutator_lock_) 456 REQUIRES(!Roles::uninterruptible_); 457 458 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self, 459 size_t length) 460 REQUIRES_SHARED(Locks::mutator_lock_) 461 REQUIRES(!Roles::uninterruptible_); 462 463 verifier::MethodVerifier::FailureKind VerifyClass( 464 Thread* self, 465 Handle<mirror::Class> klass, 466 verifier::HardFailLogMode log_level = verifier::HardFailLogMode::kLogNone) 467 REQUIRES_SHARED(Locks::mutator_lock_) 468 REQUIRES(!Locks::dex_lock_); 469 bool VerifyClassUsingOatFile(const DexFile& dex_file, 470 ObjPtr<mirror::Class> klass, 471 mirror::Class::Status& oat_file_class_status) 472 REQUIRES_SHARED(Locks::mutator_lock_) 473 REQUIRES(!Locks::dex_lock_); 474 void ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) 475 REQUIRES_SHARED(Locks::mutator_lock_) 476 REQUIRES(!Locks::dex_lock_); 477 void ResolveMethodExceptionHandlerTypes(ArtMethod* klass) 478 REQUIRES_SHARED(Locks::mutator_lock_) 479 REQUIRES(!Locks::dex_lock_); 480 481 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, 482 jstring name, 483 jobjectArray interfaces, 484 jobject loader, 485 jobjectArray methods, 486 jobjectArray throws) 487 REQUIRES_SHARED(Locks::mutator_lock_); 488 std::string GetDescriptorForProxy(ObjPtr<mirror::Class> proxy_class) 489 REQUIRES_SHARED(Locks::mutator_lock_); 490 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 491 ArtMethod* FindMethodForProxy(ObjPtr<mirror::Class> proxy_class, ArtMethod* proxy_method) 492 REQUIRES(!Locks::dex_lock_) 493 REQUIRES_SHARED(Locks::mutator_lock_); 494 495 // Get the oat code for a method when its class isn't yet initialized. 496 const void* GetQuickOatCodeFor(ArtMethod* method) 497 REQUIRES_SHARED(Locks::mutator_lock_); 498 499 pid_t GetClassesLockOwner(); // For SignalCatcher. 500 pid_t GetDexLockOwner(); // For SignalCatcher. 501 502 mirror::Class* GetClassRoot(ClassRoot class_root) REQUIRES_SHARED(Locks::mutator_lock_); 503 504 static const char* GetClassRootDescriptor(ClassRoot class_root); 505 506 // Is the given entry point quick code to run the resolution stub? 507 bool IsQuickResolutionStub(const void* entry_point) const; 508 509 // Is the given entry point quick code to bridge into the interpreter? 510 bool IsQuickToInterpreterBridge(const void* entry_point) const; 511 512 // Is the given entry point quick code to run the generic JNI stub? 513 bool IsQuickGenericJniStub(const void* entry_point) const; 514 515 const void* GetQuickToInterpreterBridgeTrampoline() const { 516 return quick_to_interpreter_bridge_trampoline_; 517 } 518 519 InternTable* GetInternTable() const { 520 return intern_table_; 521 } 522 523 // Set the entrypoints up for method to the given code. 524 void SetEntryPointsToCompiledCode(ArtMethod* method, const void* method_code) const 525 REQUIRES_SHARED(Locks::mutator_lock_); 526 527 // Set the entrypoints up for method to the enter the interpreter. 528 void SetEntryPointsToInterpreter(ArtMethod* method) const 529 REQUIRES_SHARED(Locks::mutator_lock_); 530 531 // Attempts to insert a class into a class table. Returns null if 532 // the class was inserted, otherwise returns an existing class with 533 // the same descriptor and ClassLoader. 534 mirror::Class* InsertClass(const char* descriptor, ObjPtr<mirror::Class> klass, size_t hash) 535 REQUIRES(!Locks::classlinker_classes_lock_) 536 REQUIRES_SHARED(Locks::mutator_lock_); 537 538 mirror::ObjectArray<mirror::Class>* GetClassRoots() REQUIRES_SHARED(Locks::mutator_lock_) { 539 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); 540 DCHECK(class_roots != nullptr); 541 return class_roots; 542 } 543 544 // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring 545 // that no more classes are ever added to the pre zygote table which makes it that the pages 546 // always remain shared dirty instead of private dirty. 547 void MoveClassTableToPreZygote() 548 REQUIRES(!Locks::classlinker_classes_lock_) 549 REQUIRES_SHARED(Locks::mutator_lock_); 550 551 // Creates a GlobalRef PathClassLoader that can be used to load classes from the given dex files. 552 // Note: the objects are not completely set up. Do not use this outside of tests and the compiler. 553 jobject CreatePathClassLoader(Thread* self, const std::vector<const DexFile*>& dex_files) 554 REQUIRES_SHARED(Locks::mutator_lock_) 555 REQUIRES(!Locks::dex_lock_); 556 557 PointerSize GetImagePointerSize() const { 558 return image_pointer_size_; 559 } 560 561 // Used by image writer for checking. 562 bool ClassInClassTable(ObjPtr<mirror::Class> klass) 563 REQUIRES(Locks::classlinker_classes_lock_) 564 REQUIRES_SHARED(Locks::mutator_lock_); 565 566 // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache 567 // entries are roots, but potentially not image classes. 568 void DropFindArrayClassCache() REQUIRES_SHARED(Locks::mutator_lock_); 569 570 // Clean up class loaders, this needs to happen after JNI weak globals are cleared. 571 void CleanupClassLoaders() 572 REQUIRES(!Locks::classlinker_classes_lock_) 573 REQUIRES_SHARED(Locks::mutator_lock_); 574 575 // Unlike GetOrCreateAllocatorForClassLoader, GetAllocatorForClassLoader asserts that the 576 // allocator for this class loader is already created. 577 LinearAlloc* GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 578 REQUIRES_SHARED(Locks::mutator_lock_); 579 580 // Return the linear alloc for a class loader if it is already allocated, otherwise allocate and 581 // set it. TODO: Consider using a lock other than classlinker_classes_lock_. 582 LinearAlloc* GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 583 REQUIRES(!Locks::classlinker_classes_lock_) 584 REQUIRES_SHARED(Locks::mutator_lock_); 585 586 // May be called with null class_loader due to legacy code. b/27954959 587 void InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, 588 ObjPtr<mirror::ClassLoader> class_loader) 589 REQUIRES(!Locks::classlinker_classes_lock_) 590 REQUIRES_SHARED(Locks::mutator_lock_); 591 592 static bool ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) 593 REQUIRES_SHARED(Locks::mutator_lock_); 594 595 std::set<DexCacheResolvedClasses> GetResolvedClasses(bool ignore_boot_classes) 596 REQUIRES(!Locks::dex_lock_); 597 598 std::unordered_set<std::string> GetClassDescriptorsForProfileKeys( 599 const std::set<DexCacheResolvedClasses>& classes) 600 REQUIRES(!Locks::dex_lock_); 601 602 static bool IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, 603 ObjPtr<mirror::ClassLoader> class_loader) 604 REQUIRES_SHARED(Locks::mutator_lock_); 605 606 ArtMethod* AddMethodToConflictTable(ObjPtr<mirror::Class> klass, 607 ArtMethod* conflict_method, 608 ArtMethod* interface_method, 609 ArtMethod* method, 610 bool force_new_conflict_method) 611 REQUIRES_SHARED(Locks::mutator_lock_); 612 613 // Create a conflict table with a specified capacity. 614 ImtConflictTable* CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc); 615 616 // Static version for when the class linker is not yet created. 617 static ImtConflictTable* CreateImtConflictTable(size_t count, 618 LinearAlloc* linear_alloc, 619 PointerSize pointer_size); 620 621 622 // Create the IMT and conflict tables for a class. 623 void FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 624 625 // Clear class table strong roots (other than classes themselves). This is done by dex2oat to 626 // allow pruning dex caches. 627 void ClearClassTableStrongRoots() const 628 REQUIRES(!Locks::classlinker_classes_lock_) 629 REQUIRES_SHARED(Locks::mutator_lock_); 630 631 // Throw the class initialization failure recorded when first trying to initialize the given 632 // class. 633 void ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, bool wrap_in_no_class_def = false) 634 REQUIRES_SHARED(Locks::mutator_lock_) 635 REQUIRES(!Locks::dex_lock_); 636 637 // Get the actual holding class for a copied method. Pretty slow, don't call often. 638 mirror::Class* GetHoldingClassOfCopiedMethod(ArtMethod* method) 639 REQUIRES_SHARED(Locks::mutator_lock_); 640 641 struct DexCacheData { 642 // Weak root to the DexCache. Note: Do not decode this unnecessarily or else class unloading may 643 // not work properly. 644 jweak weak_root; 645 // The following two fields are caches to the DexCache's fields and here to avoid unnecessary 646 // jweak decode that triggers read barriers (and mark them alive unnecessarily and mess with 647 // class unloading.) 648 const DexFile* dex_file; 649 GcRoot<mirror::Class>* resolved_types; 650 }; 651 652 private: 653 class LinkInterfaceMethodsHelper; 654 655 struct ClassLoaderData { 656 jweak weak_root; // Weak root to enable class unloading. 657 ClassTable* class_table; 658 LinearAlloc* allocator; 659 }; 660 661 // Ensures that the supertype of 'klass' ('supertype') is verified. Returns false and throws 662 // appropriate exceptions if verification failed hard. Returns true for successful verification or 663 // soft-failures. 664 bool AttemptSupertypeVerification(Thread* self, 665 Handle<mirror::Class> klass, 666 Handle<mirror::Class> supertype) 667 REQUIRES(!Locks::dex_lock_) 668 REQUIRES_SHARED(Locks::mutator_lock_); 669 670 static void DeleteClassLoader(Thread* self, const ClassLoaderData& data) 671 REQUIRES_SHARED(Locks::mutator_lock_); 672 673 void VisitClassLoaders(ClassLoaderVisitor* visitor) const 674 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 675 676 void VisitClassesInternal(ClassVisitor* visitor) 677 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 678 679 // Returns the number of zygote and image classes. 680 size_t NumZygoteClasses() const 681 REQUIRES(Locks::classlinker_classes_lock_) 682 REQUIRES_SHARED(Locks::mutator_lock_); 683 684 // Returns the number of non zygote nor image classes. 685 size_t NumNonZygoteClasses() const 686 REQUIRES(Locks::classlinker_classes_lock_) 687 REQUIRES_SHARED(Locks::mutator_lock_); 688 689 void FinishInit(Thread* self) 690 REQUIRES_SHARED(Locks::mutator_lock_) 691 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 692 693 // For early bootstrapping by Init 694 mirror::Class* AllocClass(Thread* self, 695 ObjPtr<mirror::Class> java_lang_Class, 696 uint32_t class_size) 697 REQUIRES_SHARED(Locks::mutator_lock_) 698 REQUIRES(!Roles::uninterruptible_); 699 700 // Alloc* convenience functions to avoid needing to pass in mirror::Class* 701 // values that are known to the ClassLinker such as 702 // kObjectArrayClass and kJavaLangString etc. 703 mirror::Class* AllocClass(Thread* self, uint32_t class_size) 704 REQUIRES_SHARED(Locks::mutator_lock_) 705 REQUIRES(!Roles::uninterruptible_); 706 707 mirror::DexCache* AllocDexCache(ObjPtr<mirror::String>* out_location, 708 Thread* self, 709 const DexFile& dex_file) 710 REQUIRES_SHARED(Locks::mutator_lock_) 711 REQUIRES(!Roles::uninterruptible_); 712 713 // Used for tests and AppendToBootClassPath. 714 mirror::DexCache* AllocAndInitializeDexCache(Thread* self, 715 const DexFile& dex_file, 716 LinearAlloc* linear_alloc) 717 REQUIRES_SHARED(Locks::mutator_lock_) 718 REQUIRES(!Locks::dex_lock_) 719 REQUIRES(!Roles::uninterruptible_); 720 721 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type) 722 REQUIRES_SHARED(Locks::mutator_lock_) 723 REQUIRES(!Roles::uninterruptible_); 724 mirror::Class* InitializePrimitiveClass(ObjPtr<mirror::Class> primitive_class, 725 Primitive::Type type) 726 REQUIRES_SHARED(Locks::mutator_lock_) 727 REQUIRES(!Roles::uninterruptible_); 728 729 mirror::Class* CreateArrayClass(Thread* self, 730 const char* descriptor, 731 size_t hash, 732 Handle<mirror::ClassLoader> class_loader) 733 REQUIRES_SHARED(Locks::mutator_lock_) 734 REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_); 735 736 void AppendToBootClassPath(Thread* self, const DexFile& dex_file) 737 REQUIRES_SHARED(Locks::mutator_lock_) 738 REQUIRES(!Locks::dex_lock_); 739 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 740 REQUIRES_SHARED(Locks::mutator_lock_) 741 REQUIRES(!Locks::dex_lock_); 742 743 // Precomputes size needed for Class, in the case of a non-temporary class this size must be 744 // sufficient to hold all static fields. 745 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, 746 const DexFile::ClassDef& dex_class_def); 747 748 // Setup the classloader, class def index, type idx so that we can insert this class in the class 749 // table. 750 void SetupClass(const DexFile& dex_file, 751 const DexFile::ClassDef& dex_class_def, 752 Handle<mirror::Class> klass, 753 ObjPtr<mirror::ClassLoader> class_loader) 754 REQUIRES_SHARED(Locks::mutator_lock_); 755 756 void LoadClass(Thread* self, 757 const DexFile& dex_file, 758 const DexFile::ClassDef& dex_class_def, 759 Handle<mirror::Class> klass) 760 REQUIRES_SHARED(Locks::mutator_lock_); 761 void LoadClassMembers(Thread* self, 762 const DexFile& dex_file, 763 const uint8_t* class_data, 764 Handle<mirror::Class> klass) 765 REQUIRES_SHARED(Locks::mutator_lock_); 766 767 void LoadField(const ClassDataItemIterator& it, Handle<mirror::Class> klass, ArtField* dst) 768 REQUIRES_SHARED(Locks::mutator_lock_); 769 770 void LoadMethod(const DexFile& dex_file, 771 const ClassDataItemIterator& it, 772 Handle<mirror::Class> klass, ArtMethod* dst) 773 REQUIRES_SHARED(Locks::mutator_lock_); 774 775 void FixupStaticTrampolines(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 776 777 // Finds a class in a Path- or DexClassLoader, loading it if necessary without using JNI. Hash 778 // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the 779 // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader 780 // was encountered while walking the parent chain (currently only BootClassLoader and 781 // PathClassLoader are supported). 782 bool FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, 783 Thread* self, 784 const char* descriptor, 785 size_t hash, 786 Handle<mirror::ClassLoader> class_loader, 787 ObjPtr<mirror::Class>* result) 788 REQUIRES_SHARED(Locks::mutator_lock_) 789 REQUIRES(!Locks::dex_lock_); 790 791 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded 792 // by the given 'class_loader'. Uses the provided hash for the descriptor. 793 mirror::Class* LookupClass(Thread* self, 794 const char* descriptor, 795 size_t hash, 796 ObjPtr<mirror::ClassLoader> class_loader) 797 REQUIRES(!Locks::classlinker_classes_lock_) 798 REQUIRES_SHARED(Locks::mutator_lock_); 799 800 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 801 REQUIRES(Locks::dex_lock_) 802 REQUIRES_SHARED(Locks::mutator_lock_); 803 mirror::DexCache* FindDexCacheLocked(Thread* self, const DexFile& dex_file, bool allow_failure) 804 REQUIRES(Locks::dex_lock_) 805 REQUIRES_SHARED(Locks::mutator_lock_); 806 807 bool InitializeClass(Thread* self, 808 Handle<mirror::Class> klass, 809 bool can_run_clinit, 810 bool can_init_parents) 811 REQUIRES_SHARED(Locks::mutator_lock_) 812 REQUIRES(!Locks::dex_lock_); 813 bool InitializeDefaultInterfaceRecursive(Thread* self, 814 Handle<mirror::Class> klass, 815 bool can_run_clinit, 816 bool can_init_parents) 817 REQUIRES(!Locks::dex_lock_) 818 REQUIRES_SHARED(Locks::mutator_lock_); 819 bool WaitForInitializeClass(Handle<mirror::Class> klass, 820 Thread* self, 821 ObjectLock<mirror::Class>& lock); 822 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass) 823 REQUIRES_SHARED(Locks::mutator_lock_); 824 825 bool IsSameDescriptorInDifferentClassContexts(Thread* self, 826 const char* descriptor, 827 Handle<mirror::ClassLoader> class_loader1, 828 Handle<mirror::ClassLoader> class_loader2) 829 REQUIRES_SHARED(Locks::mutator_lock_); 830 831 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, 832 ArtMethod* method, 833 ObjPtr<mirror::Class> klass1, 834 ObjPtr<mirror::Class> klass2) 835 REQUIRES_SHARED(Locks::mutator_lock_); 836 837 bool LinkClass(Thread* self, 838 const char* descriptor, 839 Handle<mirror::Class> klass, 840 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 841 MutableHandle<mirror::Class>* h_new_class_out) 842 REQUIRES_SHARED(Locks::mutator_lock_) 843 REQUIRES(!Locks::classlinker_classes_lock_); 844 845 bool LinkSuperClass(Handle<mirror::Class> klass) 846 REQUIRES_SHARED(Locks::mutator_lock_); 847 848 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) 849 REQUIRES_SHARED(Locks::mutator_lock_) 850 REQUIRES(!Locks::dex_lock_); 851 852 bool LinkMethods(Thread* self, 853 Handle<mirror::Class> klass, 854 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 855 bool* out_new_conflict, 856 ArtMethod** out_imt) 857 REQUIRES_SHARED(Locks::mutator_lock_); 858 859 // A wrapper class representing the result of a method translation used for linking methods and 860 // updating superclass default methods. For each method in a classes vtable there are 4 states it 861 // could be in: 862 // 1) No translation is necessary. In this case there is no MethodTranslation object for it. This 863 // is the standard case and is true when the method is not overridable by a default method, 864 // the class defines a concrete implementation of the method, the default method implementation 865 // remains the same, or an abstract method stayed abstract. 866 // 2) The method must be translated to a different default method. We note this with 867 // CreateTranslatedMethod. 868 // 3) The method must be replaced with a conflict method. This happens when a superclass 869 // implements an interface with a default method and this class implements an unrelated 870 // interface that also defines that default method. We note this with CreateConflictingMethod. 871 // 4) The method must be replaced with an abstract miranda method. This happens when a superclass 872 // implements an interface with a default method and this class implements a subinterface of 873 // the superclass's interface which declares the default method abstract. We note this with 874 // CreateAbstractMethod. 875 // 876 // When a method translation is unnecessary (case #1), we don't put it into the 877 // default_translation maps. So an instance of MethodTranslation must be in one of #2-#4. 878 class MethodTranslation { 879 public: 880 // This slot must become a default conflict method. 881 static MethodTranslation CreateConflictingMethod() { 882 return MethodTranslation(Type::kConflict, /*translation*/nullptr); 883 } 884 885 // This slot must become an abstract method. 886 static MethodTranslation CreateAbstractMethod() { 887 return MethodTranslation(Type::kAbstract, /*translation*/nullptr); 888 } 889 890 // Use the given method as the current value for this vtable slot during translation. 891 static MethodTranslation CreateTranslatedMethod(ArtMethod* new_method) { 892 return MethodTranslation(Type::kTranslation, new_method); 893 } 894 895 // Returns true if this is a method that must become a conflict method. 896 bool IsInConflict() const { 897 return type_ == Type::kConflict; 898 } 899 900 // Returns true if this is a method that must become an abstract method. 901 bool IsAbstract() const { 902 return type_ == Type::kAbstract; 903 } 904 905 // Returns true if this is a method that must become a different method. 906 bool IsTranslation() const { 907 return type_ == Type::kTranslation; 908 } 909 910 // Get the translated version of this method. 911 ArtMethod* GetTranslation() const { 912 DCHECK(IsTranslation()); 913 DCHECK(translation_ != nullptr); 914 return translation_; 915 } 916 917 private: 918 enum class Type { 919 kTranslation, 920 kConflict, 921 kAbstract, 922 }; 923 924 MethodTranslation(Type type, ArtMethod* translation) 925 : translation_(translation), type_(type) {} 926 927 ArtMethod* const translation_; 928 const Type type_; 929 }; 930 931 // Links the virtual methods for the given class and records any default methods that will need to 932 // be updated later. 933 // 934 // Arguments: 935 // * self - The current thread. 936 // * klass - class, whose vtable will be filled in. 937 // * default_translations - Vtable index to new method map. 938 // Any vtable entries that need to be updated with new default methods 939 // are stored into the default_translations map. The default_translations 940 // map is keyed on the vtable index that needs to be updated. We use this 941 // map because if we override a default method with another default 942 // method we need to update the vtable to point to the new method. 943 // Unfortunately since we copy the ArtMethod* we cannot just do a simple 944 // scan, we therefore store the vtable index's that might need to be 945 // updated with the method they will turn into. 946 // TODO This whole default_translations thing is very dirty. There should be a better way. 947 bool LinkVirtualMethods( 948 Thread* self, 949 Handle<mirror::Class> klass, 950 /*out*/std::unordered_map<size_t, MethodTranslation>* default_translations) 951 REQUIRES_SHARED(Locks::mutator_lock_); 952 953 // Sets up the interface lookup table (IFTable) in the correct order to allow searching for 954 // default methods. 955 bool SetupInterfaceLookupTable(Thread* self, 956 Handle<mirror::Class> klass, 957 Handle<mirror::ObjectArray<mirror::Class>> interfaces) 958 REQUIRES_SHARED(Locks::mutator_lock_); 959 960 961 enum class DefaultMethodSearchResult { 962 kDefaultFound, 963 kAbstractFound, 964 kDefaultConflict 965 }; 966 967 // Find the default method implementation for 'interface_method' in 'klass', if one exists. 968 // 969 // Arguments: 970 // * self - The current thread. 971 // * target_method - The method we are trying to find a default implementation for. 972 // * klass - The class we are searching for a definition of target_method. 973 // * out_default_method - The pointer we will store the found default method to on success. 974 // 975 // Return value: 976 // * kDefaultFound - There were no conflicting method implementations found in the class while 977 // searching for target_method. The default method implementation is stored into 978 // out_default_method. 979 // * kAbstractFound - There were no conflicting method implementations found in the class while 980 // searching for target_method but no default implementation was found either. 981 // out_default_method is set to null and the method should be considered not 982 // implemented. 983 // * kDefaultConflict - Conflicting method implementations were found when searching for 984 // target_method. The value of *out_default_method is null. 985 DefaultMethodSearchResult FindDefaultMethodImplementation( 986 Thread* self, 987 ArtMethod* target_method, 988 Handle<mirror::Class> klass, 989 /*out*/ArtMethod** out_default_method) const 990 REQUIRES_SHARED(Locks::mutator_lock_); 991 992 // Sets the imt entries and fixes up the vtable for the given class by linking all the interface 993 // methods. See LinkVirtualMethods for an explanation of what default_translations is. 994 bool LinkInterfaceMethods( 995 Thread* self, 996 Handle<mirror::Class> klass, 997 const std::unordered_map<size_t, MethodTranslation>& default_translations, 998 bool* out_new_conflict, 999 ArtMethod** out_imt) 1000 REQUIRES_SHARED(Locks::mutator_lock_); 1001 1002 bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) 1003 REQUIRES_SHARED(Locks::mutator_lock_); 1004 bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) 1005 REQUIRES_SHARED(Locks::mutator_lock_); 1006 bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size) 1007 REQUIRES_SHARED(Locks::mutator_lock_); 1008 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) 1009 REQUIRES_SHARED(Locks::mutator_lock_); 1010 1011 void CheckProxyConstructor(ArtMethod* constructor) const 1012 REQUIRES_SHARED(Locks::mutator_lock_); 1013 void CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const 1014 REQUIRES_SHARED(Locks::mutator_lock_); 1015 1016 size_t GetDexCacheCount() REQUIRES_SHARED(Locks::mutator_lock_, Locks::dex_lock_) { 1017 return dex_caches_.size(); 1018 } 1019 const std::list<DexCacheData>& GetDexCachesData() 1020 REQUIRES_SHARED(Locks::mutator_lock_, Locks::dex_lock_) { 1021 return dex_caches_; 1022 } 1023 1024 void CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) 1025 REQUIRES_SHARED(Locks::mutator_lock_); 1026 void CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out) 1027 REQUIRES_SHARED(Locks::mutator_lock_); 1028 1029 // Register a class loader and create its class table and allocator. Should not be called if 1030 // these are already created. 1031 void RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 1032 REQUIRES_SHARED(Locks::mutator_lock_) 1033 REQUIRES(Locks::classlinker_classes_lock_); 1034 1035 // Returns null if not found. 1036 ClassTable* ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 1037 REQUIRES_SHARED(Locks::mutator_lock_); 1038 1039 // Insert a new class table if not found. 1040 ClassTable* InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) 1041 REQUIRES_SHARED(Locks::mutator_lock_) 1042 REQUIRES(Locks::classlinker_classes_lock_); 1043 1044 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved 1045 // before returning it to the caller. Its the responsibility of the thread that placed the class 1046 // in the table to make it resolved. The thread doing resolution must notify on the class' lock 1047 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may 1048 // retire a class, the version of the class in the table is returned and this may differ from 1049 // the class passed in. 1050 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, ObjPtr<mirror::Class> klass) 1051 WARN_UNUSED 1052 REQUIRES_SHARED(Locks::mutator_lock_) 1053 REQUIRES(!Locks::dex_lock_); 1054 1055 void FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, 1056 ObjPtr<mirror::Class> new_class) 1057 REQUIRES_SHARED(Locks::mutator_lock_); 1058 1059 void SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) 1060 REQUIRES_SHARED(Locks::mutator_lock_); 1061 1062 // Return the quick generic JNI stub for testing. 1063 const void* GetRuntimeQuickGenericJniStub() const; 1064 1065 bool CanWeInitializeClass(ObjPtr<mirror::Class> klass, 1066 bool can_init_statics, 1067 bool can_init_parents) 1068 REQUIRES_SHARED(Locks::mutator_lock_); 1069 1070 void UpdateClassMethods(ObjPtr<mirror::Class> klass, 1071 LengthPrefixedArray<ArtMethod>* new_methods) 1072 REQUIRES_SHARED(Locks::mutator_lock_) 1073 REQUIRES(!Locks::classlinker_classes_lock_); 1074 1075 // new_class_set is the set of classes that were read from the class table section in the image. 1076 // If there was no class table section, it is null. 1077 bool UpdateAppImageClassLoadersAndDexCaches( 1078 gc::space::ImageSpace* space, 1079 Handle<mirror::ClassLoader> class_loader, 1080 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, 1081 ClassTable::ClassSet* new_class_set, 1082 bool* out_forward_dex_cache_array, 1083 std::string* out_error_msg) 1084 REQUIRES(!Locks::dex_lock_) 1085 REQUIRES_SHARED(Locks::mutator_lock_); 1086 1087 // Check that c1 == FindSystemClass(self, descriptor). Abort with class dumps otherwise. 1088 void CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) 1089 REQUIRES(!Locks::dex_lock_) 1090 REQUIRES_SHARED(Locks::mutator_lock_); 1091 1092 // Allocate method arrays for interfaces. 1093 bool AllocateIfTableMethodArrays(Thread* self, 1094 Handle<mirror::Class> klass, 1095 Handle<mirror::IfTable> iftable) 1096 REQUIRES_SHARED(Locks::mutator_lock_); 1097 1098 // Sets imt_ref appropriately for LinkInterfaceMethods. 1099 // If there is no method in the imt location of imt_ref it will store the given method there. 1100 // Otherwise it will set the conflict method which will figure out which method to use during 1101 // runtime. 1102 void SetIMTRef(ArtMethod* unimplemented_method, 1103 ArtMethod* imt_conflict_method, 1104 ArtMethod* current_method, 1105 /*out*/bool* new_conflict, 1106 /*out*/ArtMethod** imt_ref) REQUIRES_SHARED(Locks::mutator_lock_); 1107 1108 void FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, 1109 ArtMethod* unimplemented_method, 1110 ArtMethod* imt_conflict_method, 1111 ObjPtr<mirror::Class> klass, 1112 bool create_conflict_tables, 1113 bool ignore_copied_methods, 1114 /*out*/bool* new_conflict, 1115 /*out*/ArtMethod** imt) REQUIRES_SHARED(Locks::mutator_lock_); 1116 1117 void FillImtFromSuperClass(Handle<mirror::Class> klass, 1118 ArtMethod* unimplemented_method, 1119 ArtMethod* imt_conflict_method, 1120 bool* new_conflict, 1121 ArtMethod** imt) REQUIRES_SHARED(Locks::mutator_lock_); 1122 1123 std::vector<const DexFile*> boot_class_path_; 1124 std::vector<std::unique_ptr<const DexFile>> boot_dex_files_; 1125 1126 // JNI weak globals and side data to allow dex caches to get unloaded. We lazily delete weak 1127 // globals when we register new dex files. 1128 std::list<DexCacheData> dex_caches_ GUARDED_BY(Locks::dex_lock_); 1129 1130 // This contains the class loaders which have class tables. It is populated by 1131 // InsertClassTableForClassLoader. 1132 std::list<ClassLoaderData> class_loaders_ 1133 GUARDED_BY(Locks::classlinker_classes_lock_); 1134 1135 // Boot class path table. Since the class loader for this is null. 1136 ClassTable boot_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_); 1137 1138 // New class roots, only used by CMS since the GC needs to mark these in the pause. 1139 std::vector<GcRoot<mirror::Class>> new_class_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 1140 1141 // Number of times we've searched dex caches for a class. After a certain number of misses we move 1142 // the classes into the class_table_ to avoid dex cache based searches. 1143 Atomic<uint32_t> failed_dex_cache_class_lookups_; 1144 1145 // Well known mirror::Class roots. 1146 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_; 1147 1148 // The interface table used by all arrays. 1149 GcRoot<mirror::IfTable> array_iftable_; 1150 1151 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class 1152 // descriptors for the sake of performing FindClass. 1153 static constexpr size_t kFindArrayCacheSize = 16; 1154 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize]; 1155 size_t find_array_class_cache_next_victim_; 1156 1157 bool init_done_; 1158 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 1159 1160 InternTable* intern_table_; 1161 1162 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single 1163 // patch point within the image. TODO: make these proper relocations. 1164 const void* quick_resolution_trampoline_; 1165 const void* quick_imt_conflict_trampoline_; 1166 const void* quick_generic_jni_trampoline_; 1167 const void* quick_to_interpreter_bridge_trampoline_; 1168 1169 // Image pointer size. 1170 PointerSize image_pointer_size_; 1171 1172 class FindVirtualMethodHolderVisitor; 1173 friend struct CompilationHelper; // For Compile in ImageTest. 1174 friend class ImageDumper; // for DexLock 1175 friend class ImageWriter; // for GetClassRoots 1176 friend class VMClassLoader; // for LookupClass and FindClassInBaseDexClassLoader. 1177 friend class JniCompilerTest; // for GetRuntimeQuickGenericJniStub 1178 friend class JniInternalTest; // for GetRuntimeQuickGenericJniStub 1179 ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for DexLock, and RegisterDexFileLocked 1180 ART_FRIEND_TEST(mirror::DexCacheMethodHandlesTest, Open); // for AllocDexCache 1181 ART_FRIEND_TEST(mirror::DexCacheTest, Open); // for AllocDexCache 1182 DISALLOW_COPY_AND_ASSIGN(ClassLinker); 1183}; 1184 1185} // namespace art 1186 1187#endif // ART_RUNTIME_CLASS_LINKER_H_ 1188