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