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