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