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