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