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