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