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