class_linker.h revision 7de5dfe37f3cf24e1166412b589f6f67dcd1f1c0
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 <string> 21#include <utility> 22#include <vector> 23 24#include "base/allocator.h" 25#include "base/hash_set.h" 26#include "base/macros.h" 27#include "base/mutex.h" 28#include "class_table.h" 29#include "dex_file.h" 30#include "gc_root.h" 31#include "jni.h" 32#include "oat_file.h" 33#include "object_callbacks.h" 34 35namespace art { 36 37namespace gc { 38namespace space { 39 class ImageSpace; 40} // namespace space 41} // namespace gc 42namespace mirror { 43 class ClassLoader; 44 class DexCache; 45 class DexCachePointerArray; 46 class DexCacheTest_Open_Test; 47 class IfTable; 48 template<class T> class ObjectArray; 49 class StackTraceElement; 50} // namespace mirror 51 52template<class T> class Handle; 53template<class T> class MutableHandle; 54class InternTable; 55template<class T> class ObjectLock; 56class Runtime; 57class ScopedObjectAccessAlreadyRunnable; 58template<size_t kNumReferences> class PACKED(4) StackHandleScope; 59 60enum VisitRootFlags : uint8_t; 61 62class ClassLoaderVisitor { 63 public: 64 virtual ~ClassLoaderVisitor() {} 65 virtual void Visit(mirror::ClassLoader* class_loader) 66 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0; 67}; 68 69class ClassLinker { 70 public: 71 // Well known mirror::Class roots accessed via GetClassRoot. 72 enum ClassRoot { 73 kJavaLangClass, 74 kJavaLangObject, 75 kClassArrayClass, 76 kObjectArrayClass, 77 kJavaLangString, 78 kJavaLangDexCache, 79 kJavaLangRefReference, 80 kJavaLangReflectConstructor, 81 kJavaLangReflectField, 82 kJavaLangReflectMethod, 83 kJavaLangReflectProxy, 84 kJavaLangStringArrayClass, 85 kJavaLangReflectConstructorArrayClass, 86 kJavaLangReflectFieldArrayClass, 87 kJavaLangReflectMethodArrayClass, 88 kJavaLangClassLoader, 89 kJavaLangThrowable, 90 kJavaLangClassNotFoundException, 91 kJavaLangStackTraceElement, 92 kPrimitiveBoolean, 93 kPrimitiveByte, 94 kPrimitiveChar, 95 kPrimitiveDouble, 96 kPrimitiveFloat, 97 kPrimitiveInt, 98 kPrimitiveLong, 99 kPrimitiveShort, 100 kPrimitiveVoid, 101 kBooleanArrayClass, 102 kByteArrayClass, 103 kCharArrayClass, 104 kDoubleArrayClass, 105 kFloatArrayClass, 106 kIntArrayClass, 107 kLongArrayClass, 108 kShortArrayClass, 109 kJavaLangStackTraceElementArrayClass, 110 kClassRootsMax, 111 }; 112 113 explicit ClassLinker(InternTable* intern_table); 114 ~ClassLinker(); 115 116 // Initialize class linker by bootstraping from dex files. 117 void InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path) 118 SHARED_REQUIRES(Locks::mutator_lock_) 119 REQUIRES(!dex_lock_); 120 121 // Initialize class linker from one or more images. 122 void InitFromImage() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_); 123 124 // Finds a class by its descriptor, loading it if necessary. 125 // If class_loader is null, searches boot_class_path_. 126 mirror::Class* FindClass(Thread* self, 127 const char* descriptor, 128 Handle<mirror::ClassLoader> class_loader) 129 SHARED_REQUIRES(Locks::mutator_lock_) 130 REQUIRES(!dex_lock_); 131 132 // Finds a class in the path class loader, loading it if necessary without using JNI. Hash 133 // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the 134 // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader 135 // was encountered while walking the parent chain (currently only BootClassLoader and 136 // PathClassLoader are supported). 137 bool FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, 138 Thread* self, 139 const char* descriptor, 140 size_t hash, 141 Handle<mirror::ClassLoader> class_loader, 142 mirror::Class** result) 143 SHARED_REQUIRES(Locks::mutator_lock_) 144 REQUIRES(!dex_lock_); 145 146 // Finds a class by its descriptor using the "system" class loader, ie by searching the 147 // boot_class_path_. 148 mirror::Class* FindSystemClass(Thread* self, const char* descriptor) 149 SHARED_REQUIRES(Locks::mutator_lock_) 150 REQUIRES(!dex_lock_); 151 152 // Finds the array class given for the element class. 153 mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class) 154 SHARED_REQUIRES(Locks::mutator_lock_) 155 REQUIRES(!dex_lock_); 156 157 // Returns true if the class linker is initialized. 158 bool IsInitialized() const { 159 return init_done_; 160 } 161 162 // Define a new a class based on a ClassDef from a DexFile 163 mirror::Class* DefineClass(Thread* self, 164 const char* descriptor, 165 size_t hash, 166 Handle<mirror::ClassLoader> class_loader, 167 const DexFile& dex_file, 168 const DexFile::ClassDef& dex_class_def) 169 SHARED_REQUIRES(Locks::mutator_lock_) 170 REQUIRES(!dex_lock_); 171 172 // Finds a class by its descriptor, returning null if it isn't wasn't loaded 173 // by the given 'class_loader'. 174 mirror::Class* LookupClass(Thread* self, 175 const char* descriptor, 176 size_t hash, 177 mirror::ClassLoader* class_loader) 178 REQUIRES(!Locks::classlinker_classes_lock_) 179 SHARED_REQUIRES(Locks::mutator_lock_); 180 181 // Finds all the classes with the given descriptor, regardless of ClassLoader. 182 void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes) 183 REQUIRES(!Locks::classlinker_classes_lock_) 184 SHARED_REQUIRES(Locks::mutator_lock_); 185 186 mirror::Class* FindPrimitiveClass(char type) SHARED_REQUIRES(Locks::mutator_lock_); 187 188 // General class unloading is not supported, this is used to prune 189 // unwanted classes during image writing. 190 bool RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader) 191 REQUIRES(!Locks::classlinker_classes_lock_) 192 SHARED_REQUIRES(Locks::mutator_lock_); 193 194 void DumpAllClasses(int flags) 195 REQUIRES(!Locks::classlinker_classes_lock_) 196 SHARED_REQUIRES(Locks::mutator_lock_); 197 198 void DumpForSigQuit(std::ostream& os) REQUIRES(!Locks::classlinker_classes_lock_); 199 200 size_t NumLoadedClasses() 201 REQUIRES(!Locks::classlinker_classes_lock_) 202 SHARED_REQUIRES(Locks::mutator_lock_); 203 204 // Resolve a String with the given index from the DexFile, storing the 205 // result in the DexCache. The referrer is used to identify the 206 // target DexCache and ClassLoader to use for resolution. 207 mirror::String* ResolveString(uint32_t string_idx, ArtMethod* referrer) 208 SHARED_REQUIRES(Locks::mutator_lock_); 209 210 // Resolve a String with the given index from the DexFile, storing the 211 // result in the DexCache. 212 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx, 213 Handle<mirror::DexCache> dex_cache) 214 SHARED_REQUIRES(Locks::mutator_lock_); 215 216 // Resolve a Type with the given index from the DexFile, storing the 217 // result in the DexCache. The referrer is used to identity the 218 // target DexCache and ClassLoader to use for resolution. 219 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer) 220 SHARED_REQUIRES(Locks::mutator_lock_) 221 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 222 223 // Resolve a Type with the given index from the DexFile, storing the 224 // result in the DexCache. The referrer is used to identify the 225 // target DexCache and ClassLoader to use for resolution. 226 mirror::Class* ResolveType(uint16_t type_idx, ArtMethod* referrer) 227 SHARED_REQUIRES(Locks::mutator_lock_) 228 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 229 230 mirror::Class* ResolveType(uint16_t type_idx, ArtField* referrer) 231 SHARED_REQUIRES(Locks::mutator_lock_) 232 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 233 234 // Resolve a type with the given ID from the DexFile, storing the 235 // result in DexCache. The ClassLoader is used to search for the 236 // type, since it may be referenced from but not contained within 237 // the given DexFile. 238 mirror::Class* ResolveType(const DexFile& dex_file, 239 uint16_t type_idx, 240 Handle<mirror::DexCache> dex_cache, 241 Handle<mirror::ClassLoader> class_loader) 242 SHARED_REQUIRES(Locks::mutator_lock_) 243 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 244 245 // Resolve a method with a given ID from the DexFile, storing the 246 // result in DexCache. The ClassLinker and ClassLoader are used as 247 // in ResolveType. What is unique is the method type argument which 248 // is used to determine if this method is a direct, static, or 249 // virtual method. 250 ArtMethod* ResolveMethod(const DexFile& dex_file, 251 uint32_t method_idx, 252 Handle<mirror::DexCache> dex_cache, 253 Handle<mirror::ClassLoader> class_loader, 254 ArtMethod* referrer, 255 InvokeType type) 256 SHARED_REQUIRES(Locks::mutator_lock_) 257 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 258 259 ArtMethod* GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) 260 SHARED_REQUIRES(Locks::mutator_lock_); 261 ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, ArtMethod* referrer, InvokeType type) 262 SHARED_REQUIRES(Locks::mutator_lock_) 263 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 264 ArtMethod* ResolveMethodWithoutInvokeType(const DexFile& dex_file, 265 uint32_t method_idx, 266 Handle<mirror::DexCache> dex_cache, 267 Handle<mirror::ClassLoader> class_loader) 268 SHARED_REQUIRES(Locks::mutator_lock_) 269 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 270 271 ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class) 272 SHARED_REQUIRES(Locks::mutator_lock_); 273 ArtField* GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache) 274 SHARED_REQUIRES(Locks::mutator_lock_); 275 ArtField* ResolveField(uint32_t field_idx, ArtMethod* referrer, bool is_static) 276 SHARED_REQUIRES(Locks::mutator_lock_) 277 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 278 279 // Resolve a field with a given ID from the DexFile, storing the 280 // result in DexCache. The ClassLinker and ClassLoader are used as 281 // in ResolveType. What is unique is the is_static argument which is 282 // used to determine if we are resolving a static or non-static 283 // field. 284 ArtField* ResolveField(const DexFile& dex_file, uint32_t field_idx, 285 Handle<mirror::DexCache> dex_cache, 286 Handle<mirror::ClassLoader> class_loader, bool is_static) 287 SHARED_REQUIRES(Locks::mutator_lock_) 288 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 289 290 // Resolve a field with a given ID from the DexFile, storing the 291 // result in DexCache. The ClassLinker and ClassLoader are used as 292 // in ResolveType. No is_static argument is provided so that Java 293 // field resolution semantics are followed. 294 ArtField* ResolveFieldJLS(const DexFile& dex_file, 295 uint32_t field_idx, 296 Handle<mirror::DexCache> dex_cache, 297 Handle<mirror::ClassLoader> class_loader) 298 SHARED_REQUIRES(Locks::mutator_lock_) 299 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 300 301 // Get shorty from method index without resolution. Used to do handlerization. 302 const char* MethodShorty(uint32_t method_idx, ArtMethod* referrer, uint32_t* length) 303 SHARED_REQUIRES(Locks::mutator_lock_); 304 305 // Returns true on success, false if there's an exception pending. 306 // can_run_clinit=false allows the compiler to attempt to init a class, 307 // given the restriction that no <clinit> execution is possible. 308 bool EnsureInitialized(Thread* self, 309 Handle<mirror::Class> c, 310 bool can_init_fields, 311 bool can_init_parents) 312 SHARED_REQUIRES(Locks::mutator_lock_) 313 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 314 315 // Initializes classes that have instances in the image but that have 316 // <clinit> methods so they could not be initialized by the compiler. 317 void RunRootClinits() 318 SHARED_REQUIRES(Locks::mutator_lock_) 319 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 320 321 mirror::DexCache* RegisterDexFile(const DexFile& dex_file) 322 REQUIRES(!dex_lock_) 323 SHARED_REQUIRES(Locks::mutator_lock_); 324 void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 325 REQUIRES(!dex_lock_) 326 SHARED_REQUIRES(Locks::mutator_lock_); 327 328 const OatFile* RegisterOatFile(const OatFile* oat_file) 329 REQUIRES(!dex_lock_); 330 331 const std::vector<const DexFile*>& GetBootClassPath() { 332 return boot_class_path_; 333 } 334 335 // Returns the first non-image oat file in the class path. 336 const OatFile* GetPrimaryOatFile() 337 REQUIRES(!dex_lock_); 338 339 void VisitClasses(ClassVisitor* visitor) 340 REQUIRES(!Locks::classlinker_classes_lock_) 341 SHARED_REQUIRES(Locks::mutator_lock_); 342 343 // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage 344 // so that it can visit individual classes without holding the doesn't hold the 345 // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code 346 // can race with insertion and deletion of classes while the visitor is being called. 347 void VisitClassesWithoutClassesLock(ClassVisitor* visitor) 348 SHARED_REQUIRES(Locks::mutator_lock_) 349 REQUIRES(!dex_lock_); 350 351 void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) 352 REQUIRES(!Locks::classlinker_classes_lock_) 353 SHARED_REQUIRES(Locks::mutator_lock_); 354 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) 355 REQUIRES(!dex_lock_) 356 SHARED_REQUIRES(Locks::mutator_lock_); 357 358 mirror::DexCache* FindDexCache(Thread* self, 359 const DexFile& dex_file, 360 bool allow_failure = false) 361 REQUIRES(!dex_lock_) 362 SHARED_REQUIRES(Locks::mutator_lock_); 363 void FixupDexCaches(ArtMethod* resolution_method) 364 REQUIRES(!dex_lock_) 365 SHARED_REQUIRES(Locks::mutator_lock_); 366 367 // Finds or creates the oat file holding dex_location. Then loads and returns 368 // all corresponding dex files (there may be more than one dex file loaded 369 // in the case of multidex). 370 // This may return the original, unquickened dex files if the oat file could 371 // not be generated. 372 // 373 // Returns an empty vector if the dex files could not be loaded. In this 374 // case, there will be at least one error message returned describing why no 375 // dex files could not be loaded. The 'error_msgs' argument must not be 376 // null, regardless of whether there is an error or not. 377 // 378 // This method should not be called with the mutator_lock_ held, because it 379 // could end up starving GC if we need to generate or relocate any oat 380 // files. 381 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat( 382 const char* dex_location, 383 const char* oat_location, 384 std::vector<std::string>* error_msgs) 385 REQUIRES(!dex_lock_, !Locks::mutator_lock_); 386 387 // Allocate an instance of a java.lang.Object. 388 mirror::Object* AllocObject(Thread* self) 389 SHARED_REQUIRES(Locks::mutator_lock_) 390 REQUIRES(!Roles::uninterruptible_); 391 392 // TODO: replace this with multiple methods that allocate the correct managed type. 393 template <class T> 394 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length) 395 SHARED_REQUIRES(Locks::mutator_lock_) 396 REQUIRES(!Roles::uninterruptible_); 397 398 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length) 399 SHARED_REQUIRES(Locks::mutator_lock_) 400 REQUIRES(!Roles::uninterruptible_); 401 402 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length) 403 SHARED_REQUIRES(Locks::mutator_lock_) 404 REQUIRES(!Roles::uninterruptible_); 405 406 LengthPrefixedArray<ArtField>* AllocArtFieldArray(Thread* self, size_t length); 407 408 LengthPrefixedArray<ArtMethod>* AllocArtMethodArray(Thread* self, size_t length); 409 410 mirror::PointerArray* AllocPointerArray(Thread* self, size_t length) 411 SHARED_REQUIRES(Locks::mutator_lock_) 412 REQUIRES(!Roles::uninterruptible_); 413 414 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount) 415 SHARED_REQUIRES(Locks::mutator_lock_) 416 REQUIRES(!Roles::uninterruptible_); 417 418 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self, 419 size_t length) 420 SHARED_REQUIRES(Locks::mutator_lock_) 421 REQUIRES(!Roles::uninterruptible_); 422 423 void VerifyClass(Thread* self, Handle<mirror::Class> klass) 424 SHARED_REQUIRES(Locks::mutator_lock_) 425 REQUIRES(!dex_lock_); 426 bool VerifyClassUsingOatFile(const DexFile& dex_file, 427 mirror::Class* klass, 428 mirror::Class::Status& oat_file_class_status) 429 SHARED_REQUIRES(Locks::mutator_lock_) 430 REQUIRES(!dex_lock_); 431 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, 432 Handle<mirror::Class> klass) 433 SHARED_REQUIRES(Locks::mutator_lock_) 434 REQUIRES(!dex_lock_); 435 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, ArtMethod* klass) 436 SHARED_REQUIRES(Locks::mutator_lock_) 437 REQUIRES(!dex_lock_); 438 439 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, 440 jstring name, 441 jobjectArray interfaces, 442 jobject loader, 443 jobjectArray methods, 444 jobjectArray throws) 445 SHARED_REQUIRES(Locks::mutator_lock_); 446 std::string GetDescriptorForProxy(mirror::Class* proxy_class) 447 SHARED_REQUIRES(Locks::mutator_lock_); 448 ArtMethod* FindMethodForProxy(mirror::Class* proxy_class, ArtMethod* proxy_method) 449 REQUIRES(!dex_lock_) 450 SHARED_REQUIRES(Locks::mutator_lock_); 451 452 // Get the oat code for a method when its class isn't yet initialized 453 const void* GetQuickOatCodeFor(ArtMethod* method) 454 SHARED_REQUIRES(Locks::mutator_lock_); 455 456 // Get the oat code for a method from a method index. 457 const void* GetQuickOatCodeFor(const DexFile& dex_file, 458 uint16_t class_def_idx, 459 uint32_t method_idx) 460 SHARED_REQUIRES(Locks::mutator_lock_); 461 462 // Get compiled code for a method, return null if no code 463 // exists. This is unlike Get..OatCodeFor which will return a bridge 464 // or interpreter entrypoint. 465 const void* GetOatMethodQuickCodeFor(ArtMethod* method) 466 SHARED_REQUIRES(Locks::mutator_lock_); 467 468 const OatFile::OatMethod FindOatMethodFor(ArtMethod* method, bool* found) 469 SHARED_REQUIRES(Locks::mutator_lock_); 470 471 pid_t GetClassesLockOwner(); // For SignalCatcher. 472 pid_t GetDexLockOwner(); // For SignalCatcher. 473 474 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_REQUIRES(Locks::mutator_lock_); 475 476 static const char* GetClassRootDescriptor(ClassRoot class_root); 477 478 // Is the given entry point quick code to run the resolution stub? 479 bool IsQuickResolutionStub(const void* entry_point) const; 480 481 // Is the given entry point quick code to bridge into the interpreter? 482 bool IsQuickToInterpreterBridge(const void* entry_point) const; 483 484 // Is the given entry point quick code to run the generic JNI stub? 485 bool IsQuickGenericJniStub(const void* entry_point) const; 486 487 InternTable* GetInternTable() const { 488 return intern_table_; 489 } 490 491 // Set the entrypoints up for method to the given code. 492 void SetEntryPointsToCompiledCode(ArtMethod* method, const void* method_code) const 493 SHARED_REQUIRES(Locks::mutator_lock_); 494 495 // Set the entrypoints up for method to the enter the interpreter. 496 void SetEntryPointsToInterpreter(ArtMethod* method) const 497 SHARED_REQUIRES(Locks::mutator_lock_); 498 499 // Attempts to insert a class into a class table. Returns null if 500 // the class was inserted, otherwise returns an existing class with 501 // the same descriptor and ClassLoader. 502 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash) 503 REQUIRES(!Locks::classlinker_classes_lock_) 504 SHARED_REQUIRES(Locks::mutator_lock_); 505 506 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_REQUIRES(Locks::mutator_lock_) { 507 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); 508 DCHECK(class_roots != nullptr); 509 return class_roots; 510 } 511 512 // Move all of the image classes into the class table for faster lookups. 513 void MoveImageClassesToClassTable() 514 REQUIRES(!Locks::classlinker_classes_lock_) 515 SHARED_REQUIRES(Locks::mutator_lock_); 516 // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring 517 // that no more classes are ever added to the pre zygote table which makes it that the pages 518 // always remain shared dirty instead of private dirty. 519 void MoveClassTableToPreZygote() 520 REQUIRES(!Locks::classlinker_classes_lock_) 521 SHARED_REQUIRES(Locks::mutator_lock_); 522 523 // Returns true if the method can be called with its direct code pointer, false otherwise. 524 bool MayBeCalledWithDirectCodePointer(ArtMethod* m) 525 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_); 526 527 // Creates a GlobalRef PathClassLoader that can be used to load classes from the given dex files. 528 // Note: the objects are not completely set up. Do not use this outside of tests and the compiler. 529 jobject CreatePathClassLoader(Thread* self, std::vector<const DexFile*>& dex_files) 530 SHARED_REQUIRES(Locks::mutator_lock_) 531 REQUIRES(!dex_lock_); 532 533 size_t GetImagePointerSize() const { 534 DCHECK(ValidPointerSize(image_pointer_size_)) << image_pointer_size_; 535 return image_pointer_size_; 536 } 537 538 // Used by image writer for checking. 539 bool ClassInClassTable(mirror::Class* klass) 540 REQUIRES(Locks::classlinker_classes_lock_) 541 SHARED_REQUIRES(Locks::mutator_lock_); 542 543 ArtMethod* CreateRuntimeMethod(); 544 545 // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache 546 // entries are roots, but potentially not image classes. 547 void DropFindArrayClassCache() SHARED_REQUIRES(Locks::mutator_lock_); 548 549 private: 550 // The RemoveClearedLoaders version removes cleared weak global class loaders and frees their 551 // class tables. This version can only be called with reader access to the 552 // classlinker_classes_lock_ since it modifies the class_loaders_ list. 553 void VisitClassLoadersAndRemoveClearedLoaders(ClassLoaderVisitor* visitor) 554 REQUIRES(Locks::classlinker_classes_lock_) 555 SHARED_REQUIRES(Locks::mutator_lock_); 556 void VisitClassLoaders(ClassLoaderVisitor* visitor) const 557 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 558 559 560 void VisitClassesInternal(ClassVisitor* visitor) 561 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 562 563 // Returns the number of zygote and image classes. 564 size_t NumZygoteClasses() const 565 REQUIRES(Locks::classlinker_classes_lock_) 566 SHARED_REQUIRES(Locks::mutator_lock_); 567 568 // Returns the number of non zygote nor image classes. 569 size_t NumNonZygoteClasses() const 570 REQUIRES(Locks::classlinker_classes_lock_) 571 SHARED_REQUIRES(Locks::mutator_lock_); 572 573 OatFile& GetImageOatFile(gc::space::ImageSpace* space) 574 REQUIRES(!dex_lock_) 575 SHARED_REQUIRES(Locks::mutator_lock_); 576 577 void FinishInit(Thread* self) 578 SHARED_REQUIRES(Locks::mutator_lock_) 579 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 580 581 // For early bootstrapping by Init 582 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size) 583 SHARED_REQUIRES(Locks::mutator_lock_) 584 REQUIRES(!Roles::uninterruptible_); 585 586 // Alloc* convenience functions to avoid needing to pass in mirror::Class* 587 // values that are known to the ClassLinker such as 588 // kObjectArrayClass and kJavaLangString etc. 589 mirror::Class* AllocClass(Thread* self, uint32_t class_size) 590 SHARED_REQUIRES(Locks::mutator_lock_) 591 REQUIRES(!Roles::uninterruptible_); 592 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file) 593 SHARED_REQUIRES(Locks::mutator_lock_) 594 REQUIRES(!Roles::uninterruptible_); 595 596 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type) 597 SHARED_REQUIRES(Locks::mutator_lock_) 598 REQUIRES(!Roles::uninterruptible_); 599 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type) 600 SHARED_REQUIRES(Locks::mutator_lock_) 601 REQUIRES(!Roles::uninterruptible_); 602 603 mirror::Class* CreateArrayClass(Thread* self, 604 const char* descriptor, 605 size_t hash, 606 Handle<mirror::ClassLoader> class_loader) 607 SHARED_REQUIRES(Locks::mutator_lock_) 608 REQUIRES(!dex_lock_, !Roles::uninterruptible_); 609 610 void AppendToBootClassPath(Thread* self, const DexFile& dex_file) 611 SHARED_REQUIRES(Locks::mutator_lock_) 612 REQUIRES(!dex_lock_); 613 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 614 SHARED_REQUIRES(Locks::mutator_lock_) 615 REQUIRES(!dex_lock_); 616 617 // Precomputes size needed for Class, in the case of a non-temporary class this size must be 618 // sufficient to hold all static fields. 619 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, 620 const DexFile::ClassDef& dex_class_def); 621 622 // Setup the classloader, class def index, type idx so that we can insert this class in the class 623 // table. 624 void SetupClass(const DexFile& dex_file, 625 const DexFile::ClassDef& dex_class_def, 626 Handle<mirror::Class> klass, 627 mirror::ClassLoader* class_loader) 628 SHARED_REQUIRES(Locks::mutator_lock_); 629 630 void LoadClass(Thread* self, 631 const DexFile& dex_file, 632 const DexFile::ClassDef& dex_class_def, 633 Handle<mirror::Class> klass) 634 SHARED_REQUIRES(Locks::mutator_lock_); 635 void LoadClassMembers(Thread* self, 636 const DexFile& dex_file, 637 const uint8_t* class_data, 638 Handle<mirror::Class> klass, 639 const OatFile::OatClass* oat_class) 640 SHARED_REQUIRES(Locks::mutator_lock_); 641 642 void LoadField(const ClassDataItemIterator& it, Handle<mirror::Class> klass, ArtField* dst) 643 SHARED_REQUIRES(Locks::mutator_lock_); 644 645 void LoadMethod(Thread* self, 646 const DexFile& dex_file, 647 const ClassDataItemIterator& it, 648 Handle<mirror::Class> klass, ArtMethod* dst) 649 SHARED_REQUIRES(Locks::mutator_lock_); 650 651 void FixupStaticTrampolines(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_); 652 653 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on 654 // error and sets found to false. 655 OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found) 656 SHARED_REQUIRES(Locks::mutator_lock_); 657 658 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 659 REQUIRES(dex_lock_) 660 SHARED_REQUIRES(Locks::mutator_lock_); 661 mirror::DexCache* FindDexCacheLocked(Thread* self, const DexFile& dex_file, bool allow_failure) 662 REQUIRES(dex_lock_) 663 SHARED_REQUIRES(Locks::mutator_lock_); 664 665 bool InitializeClass(Thread* self, 666 Handle<mirror::Class> klass, 667 bool can_run_clinit, 668 bool can_init_parents) 669 SHARED_REQUIRES(Locks::mutator_lock_) 670 REQUIRES(!dex_lock_); 671 bool WaitForInitializeClass(Handle<mirror::Class> klass, 672 Thread* self, 673 ObjectLock<mirror::Class>& lock); 674 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass) 675 SHARED_REQUIRES(Locks::mutator_lock_); 676 677 bool IsSameDescriptorInDifferentClassContexts(Thread* self, 678 const char* descriptor, 679 Handle<mirror::ClassLoader> class_loader1, 680 Handle<mirror::ClassLoader> class_loader2) 681 SHARED_REQUIRES(Locks::mutator_lock_); 682 683 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, 684 ArtMethod* method, 685 mirror::Class* klass1, 686 mirror::Class* klass2) 687 SHARED_REQUIRES(Locks::mutator_lock_); 688 689 bool LinkClass(Thread* self, 690 const char* descriptor, 691 Handle<mirror::Class> klass, 692 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 693 MutableHandle<mirror::Class>* h_new_class_out) 694 SHARED_REQUIRES(Locks::mutator_lock_) 695 REQUIRES(!Locks::classlinker_classes_lock_); 696 697 bool LinkSuperClass(Handle<mirror::Class> klass) 698 SHARED_REQUIRES(Locks::mutator_lock_); 699 700 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) 701 SHARED_REQUIRES(Locks::mutator_lock_) 702 REQUIRES(!dex_lock_); 703 704 bool LinkMethods(Thread* self, 705 Handle<mirror::Class> klass, 706 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 707 ArtMethod** out_imt) 708 SHARED_REQUIRES(Locks::mutator_lock_); 709 710 bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) 711 SHARED_REQUIRES(Locks::mutator_lock_); 712 713 bool LinkInterfaceMethods(Thread* self, 714 Handle<mirror::Class> klass, 715 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 716 ArtMethod** out_imt) 717 SHARED_REQUIRES(Locks::mutator_lock_); 718 719 bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) 720 SHARED_REQUIRES(Locks::mutator_lock_); 721 bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) 722 SHARED_REQUIRES(Locks::mutator_lock_); 723 bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size) 724 SHARED_REQUIRES(Locks::mutator_lock_); 725 void LinkCode(ArtMethod* method, 726 const OatFile::OatClass* oat_class, 727 uint32_t class_def_method_index) 728 SHARED_REQUIRES(Locks::mutator_lock_); 729 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) 730 SHARED_REQUIRES(Locks::mutator_lock_); 731 732 void CheckProxyConstructor(ArtMethod* constructor) const 733 SHARED_REQUIRES(Locks::mutator_lock_); 734 void CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const 735 SHARED_REQUIRES(Locks::mutator_lock_); 736 737 // For use by ImageWriter to find DexCaches for its roots 738 ReaderWriterMutex* DexLock() 739 SHARED_REQUIRES(Locks::mutator_lock_) 740 LOCK_RETURNED(dex_lock_) { 741 return &dex_lock_; 742 } 743 size_t GetDexCacheCount() SHARED_REQUIRES(Locks::mutator_lock_, dex_lock_) { 744 return dex_caches_.size(); 745 } 746 const std::list<jweak>& GetDexCaches() SHARED_REQUIRES(Locks::mutator_lock_, dex_lock_) { 747 return dex_caches_; 748 } 749 750 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) 751 REQUIRES(!dex_lock_); 752 753 // Returns the boot image oat file. 754 const OatFile* GetBootOatFile() SHARED_REQUIRES(dex_lock_); 755 756 void CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) 757 SHARED_REQUIRES(Locks::mutator_lock_); 758 void CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out) 759 SHARED_REQUIRES(Locks::mutator_lock_); 760 761 // Ensures that methods have the kAccPreverified bit set. We use the kAccPreverfied bit on the 762 // class access flags to determine whether this has been done before. 763 void EnsurePreverifiedMethods(Handle<mirror::Class> c) 764 SHARED_REQUIRES(Locks::mutator_lock_); 765 766 mirror::Class* LookupClassFromImage(const char* descriptor) 767 SHARED_REQUIRES(Locks::mutator_lock_); 768 769 // Returns null if not found. 770 ClassTable* ClassTableForClassLoader(mirror::ClassLoader* class_loader) 771 SHARED_REQUIRES(Locks::mutator_lock_, Locks::classlinker_classes_lock_); 772 // Insert a new class table if not found. 773 ClassTable* InsertClassTableForClassLoader(mirror::ClassLoader* class_loader) 774 SHARED_REQUIRES(Locks::mutator_lock_) 775 REQUIRES(Locks::classlinker_classes_lock_); 776 777 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved 778 // before returning it to the caller. Its the responsibility of the thread that placed the class 779 // in the table to make it resolved. The thread doing resolution must notify on the class' lock 780 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may 781 // retire a class, the version of the class in the table is returned and this may differ from 782 // the class passed in. 783 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass) 784 WARN_UNUSED 785 SHARED_REQUIRES(Locks::mutator_lock_) 786 REQUIRES(!dex_lock_); 787 788 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) 789 SHARED_REQUIRES(Locks::mutator_lock_); 790 791 void SetClassRoot(ClassRoot class_root, mirror::Class* klass) 792 SHARED_REQUIRES(Locks::mutator_lock_); 793 794 // Return the quick generic JNI stub for testing. 795 const void* GetRuntimeQuickGenericJniStub() const; 796 797 // Throw the class initialization failure recorded when first trying to initialize the given 798 // class. 799 // Note: Currently we only store the descriptor, so we cannot throw the exact throwable, only 800 // a recreation with a custom string. 801 void ThrowEarlierClassFailure(mirror::Class* c) 802 SHARED_REQUIRES(Locks::mutator_lock_) 803 REQUIRES(!dex_lock_); 804 805 // Check for duplicate class definitions of the given oat file against all open oat files. 806 bool HasCollisions(const OatFile* oat_file, std::string* error_msg) REQUIRES(!dex_lock_); 807 808 bool HasInitWithString(Thread* self, const char* descriptor) 809 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_); 810 811 bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, bool can_init_parents) 812 SHARED_REQUIRES(Locks::mutator_lock_); 813 814 void UpdateClassVirtualMethods(mirror::Class* klass, 815 LengthPrefixedArray<ArtMethod>* new_methods) 816 SHARED_REQUIRES(Locks::mutator_lock_) 817 REQUIRES(!Locks::classlinker_classes_lock_); 818 819 std::vector<const DexFile*> boot_class_path_; 820 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_; 821 822 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 823 // JNI weak globals to allow dex caches to get unloaded. We lazily delete weak globals when we 824 // register new dex files. 825 std::list<jweak> dex_caches_ GUARDED_BY(dex_lock_); 826 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_); 827 828 // This contains the class loaders which have class tables. It is populated by 829 // InsertClassTableForClassLoader. Weak roots to enable class unloading. 830 std::list<jweak> class_loaders_ 831 GUARDED_BY(Locks::classlinker_classes_lock_); 832 833 // Boot class path table. Since the class loader for this is null. 834 ClassTable boot_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_); 835 836 // New class roots, only used by CMS since the GC needs to mark these in the pause. 837 std::vector<GcRoot<mirror::Class>> new_class_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 838 839 // Do we need to search dex caches to find image classes? 840 bool dex_cache_image_class_lookup_required_; 841 // Number of times we've searched dex caches for a class. After a certain number of misses we move 842 // the classes into the class_table_ to avoid dex cache based searches. 843 Atomic<uint32_t> failed_dex_cache_class_lookups_; 844 845 // Well known mirror::Class roots. 846 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_; 847 848 // The interface table used by all arrays. 849 GcRoot<mirror::IfTable> array_iftable_; 850 851 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class 852 // descriptors for the sake of performing FindClass. 853 static constexpr size_t kFindArrayCacheSize = 16; 854 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize]; 855 size_t find_array_class_cache_next_victim_; 856 857 bool init_done_; 858 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 859 860 InternTable* intern_table_; 861 862 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single 863 // patch point within the image. TODO: make these proper relocations. 864 const void* quick_resolution_trampoline_; 865 const void* quick_imt_conflict_trampoline_; 866 const void* quick_generic_jni_trampoline_; 867 const void* quick_to_interpreter_bridge_trampoline_; 868 869 // Image pointer size. 870 size_t image_pointer_size_; 871 872 friend class ImageWriter; // for GetClassRoots 873 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation 874 friend class JniCompilerTest; // for GetRuntimeQuickGenericJniStub 875 ART_FRIEND_TEST(mirror::DexCacheTest, Open); // for AllocDexCache 876 877 DISALLOW_COPY_AND_ASSIGN(ClassLinker); 878}; 879 880} // namespace art 881 882#endif // ART_RUNTIME_CLASS_LINKER_H_ 883