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