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