class_linker.h revision be4e64303cc66bda0a12eaab835caa0bcfda3cd9
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 // Returns 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(Locks::classlinker_classes_lock_) 239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 240 241 // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage 242 // so that it can visit individual classes without holding the doesn't hold the 243 // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code 244 // can race with insertion and deletion of classes while the visitor is being called. 245 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) 246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 247 248 void VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags) 249 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 251 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) 252 LOCKS_EXCLUDED(dex_lock_) 253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 254 255 mirror::DexCache* FindDexCache(const DexFile& dex_file) 256 LOCKS_EXCLUDED(dex_lock_) 257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 258 bool IsDexFileRegistered(const DexFile& dex_file) 259 LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 260 void FixupDexCaches(mirror::ArtMethod* resolution_method) 261 LOCKS_EXCLUDED(dex_lock_) 262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 263 264 // Generate an oat file from a dex file 265 bool GenerateOatFile(const char* dex_filename, 266 int oat_fd, 267 const char* oat_cache_filename, 268 std::string* error_msg) 269 LOCKS_EXCLUDED(Locks::mutator_lock_); 270 271 // Find or create the oat file holding dex_location. Then load all corresponding dex files 272 // (if multidex) into the given vector. 273 bool OpenDexFilesFromOat(const char* dex_location, const char* oat_location, 274 std::vector<std::string>* error_msgs, 275 std::vector<const DexFile*>* dex_files) 276 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 277 278 // Returns true if the given oat file has the same image checksum as the image it is paired with. 279 static bool VerifyOatImageChecksum(const OatFile* oat_file, const InstructionSet instruction_set); 280 // Returns true if the oat file checksums match with the image and the offsets are such that it 281 // could be loaded with it. 282 static bool VerifyOatChecksums(const OatFile* oat_file, const InstructionSet instruction_set, 283 std::string* error_msg); 284 // Returns true if oat file contains the dex file with the given location and checksum. 285 static bool VerifyOatAndDexFileChecksums(const OatFile* oat_file, 286 const char* dex_location, 287 uint32_t dex_location_checksum, 288 InstructionSet instruction_set, 289 std::string* error_msg); 290 291 // TODO: replace this with multiple methods that allocate the correct managed type. 292 template <class T> 293 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length) 294 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 295 296 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length) 297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 298 299 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length) 300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 301 302 mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length) 303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 304 305 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount) 306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 307 308 mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length) 309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 310 311 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self, 312 size_t length) 313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 314 315 void VerifyClass(Handle<mirror::Class> klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 316 bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, 317 mirror::Class::Status& oat_file_class_status) 318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 319 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, 320 Handle<mirror::Class> klass) 321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 322 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass) 323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 324 325 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, 326 jobjectArray interfaces, jobject loader, jobjectArray methods, 327 jobjectArray throws) 328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 329 std::string GetDescriptorForProxy(mirror::Class* proxy_class) 330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 331 mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class, 332 mirror::ArtMethod* proxy_method) 333 LOCKS_EXCLUDED(dex_lock_) 334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 335 336 // Get the oat code for a method when its class isn't yet initialized 337 const void* GetQuickOatCodeFor(mirror::ArtMethod* method) 338 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 339 const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code) 340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 341 342 // Get the oat code for a method from a method index. 343 const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx) 344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 345 const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx) 346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 347 348 pid_t GetClassesLockOwner(); // For SignalCatcher. 349 pid_t GetDexLockOwner(); // For SignalCatcher. 350 351 const void* GetPortableResolutionTrampoline() const { 352 return portable_resolution_trampoline_; 353 } 354 355 const void* GetQuickGenericJniTrampoline() const { 356 return quick_generic_jni_trampoline_; 357 } 358 359 const void* GetQuickResolutionTrampoline() const { 360 return quick_resolution_trampoline_; 361 } 362 363 const void* GetPortableImtConflictTrampoline() const { 364 return portable_imt_conflict_trampoline_; 365 } 366 367 const void* GetQuickImtConflictTrampoline() const { 368 return quick_imt_conflict_trampoline_; 369 } 370 371 const void* GetQuickToInterpreterBridgeTrampoline() const { 372 return quick_to_interpreter_bridge_trampoline_; 373 } 374 375 InternTable* GetInternTable() const { 376 return intern_table_; 377 } 378 379 // Attempts to insert a class into a class table. Returns NULL if 380 // the class was inserted, otherwise returns an existing class with 381 // the same descriptor and ClassLoader. 382 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash) 383 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 384 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 385 386 // Special code to allocate an art method, use this instead of class->AllocObject. 387 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 388 389 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 390 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); 391 DCHECK(class_roots != NULL); 392 return class_roots; 393 } 394 395 private: 396 bool FindOatMethodFor(mirror::ArtMethod* method, OatFile::OatMethod* oat_method) 397 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 398 399 OatFile& GetImageOatFile(gc::space::ImageSpace* space) 400 LOCKS_EXCLUDED(dex_lock_) 401 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 402 403 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 404 405 // For early bootstrapping by Init 406 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size) 407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 408 409 // Alloc* convenience functions to avoid needing to pass in mirror::Class* 410 // values that are known to the ClassLinker such as 411 // kObjectArrayClass and kJavaLangString etc. 412 mirror::Class* AllocClass(Thread* self, uint32_t class_size) 413 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 414 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file) 415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 416 mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 417 418 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type) 419 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 420 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type) 421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 422 423 424 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor, 425 Handle<mirror::ClassLoader> class_loader) 426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 427 428 void AppendToBootClassPath(const DexFile& dex_file) 429 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 430 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 431 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 432 433 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, 434 mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map) 435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 436 437 // Precomputes size needed for Class, in the case of a non-temporary class this size must be 438 // sufficient to hold all static fields. 439 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, 440 const DexFile::ClassDef& dex_class_def); 441 442 void LoadClass(const DexFile& dex_file, 443 const DexFile::ClassDef& dex_class_def, 444 Handle<mirror::Class> klass, 445 mirror::ClassLoader* class_loader) 446 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 447 void LoadClassMembers(const DexFile& dex_file, 448 const byte* class_data, 449 Handle<mirror::Class> klass, 450 mirror::ClassLoader* class_loader, 451 const OatFile::OatClass* oat_class) 452 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 453 454 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, 455 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst) 456 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 457 458 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file, 459 const ClassDataItemIterator& dex_method, 460 Handle<mirror::Class> klass) 461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 462 463 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 464 465 // Finds the associated oat class for a dex_file and descriptor. Returns whether the class 466 // was found, and sets the data in oat_class. 467 bool FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, OatFile::OatClass* oat_class) 468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 469 470 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 471 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_) 472 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 473 bool IsDexFileRegisteredLocked(const DexFile& dex_file) 474 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_); 475 476 bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit, 477 bool can_init_parents) 478 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 479 bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, 480 ObjectLock<mirror::Class>& lock); 481 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass) 482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 483 484 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor, 485 Handle<mirror::ClassLoader> class_loader1, 486 Handle<mirror::ClassLoader> class_loader2) 487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 488 489 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method, 490 mirror::Class* klass1, 491 mirror::Class* klass2) 492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 493 494 bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, 495 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 496 mirror::Class** new_class) 497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 498 499 bool LinkSuperClass(Handle<mirror::Class> klass) 500 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 501 502 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) 503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 504 505 bool LinkMethods(Thread* self, Handle<mirror::Class> klass, 506 Handle<mirror::ObjectArray<mirror::Class>> interfaces) 507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 508 509 bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) 510 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 511 512 bool LinkInterfaceMethods(Handle<mirror::Class> klass, 513 Handle<mirror::ObjectArray<mirror::Class>> interfaces) 514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 515 516 bool LinkStaticFields(Handle<mirror::Class> klass, size_t* class_size) 517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 518 bool LinkInstanceFields(Handle<mirror::Class> klass) 519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 520 bool LinkFields(Handle<mirror::Class> klass, bool is_static, size_t* class_size) 521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 522 void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class, 523 const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index) 524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 525 526 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) 527 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 528 void CreateReferenceStaticOffsets(Handle<mirror::Class> klass) 529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 530 void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static, 531 uint32_t reference_offsets) 532 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 533 534 // For use by ImageWriter to find DexCaches for its roots 535 ReaderWriterMutex* DexLock() 536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) { 537 return &dex_lock_; 538 } 539 size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) { 540 return dex_caches_.size(); 541 } 542 mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_); 543 544 const OatFile::OatDexFile* FindOpenedOatDexFileForDexFile(const DexFile& dex_file) 545 LOCKS_EXCLUDED(dex_lock_) 546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 547 548 // Find an opened oat dex file that contains dex_location. If oat_location is not nullptr, 549 // the file must have that location, else any oat location is accepted. 550 const OatFile::OatDexFile* FindOpenedOatDexFile(const char* oat_location, 551 const char* dex_location, 552 const uint32_t* dex_location_checksum) 553 LOCKS_EXCLUDED(dex_lock_); 554 555 // Will open the oat file directly without relocating, even if we could/should do relocation. 556 const OatFile* FindOatFileFromOatLocation(const std::string& oat_location, 557 std::string* error_msg) 558 LOCKS_EXCLUDED(dex_lock_); 559 560 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) 561 LOCKS_EXCLUDED(dex_lock_); 562 563 const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location, 564 InstructionSet isa, 565 bool* already_opened, 566 bool* obsolete_file_cleanup_failed, 567 std::vector<std::string>* error_msg) 568 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 569 570 const OatFile* GetInterpretedOnlyOat(const std::string& oat_path, 571 InstructionSet isa, 572 std::string* error_msg); 573 574 const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output, 575 const std::string& image_location, InstructionSet isa, 576 std::string* error_msg) 577 LOCKS_EXCLUDED(Locks::mutator_lock_); 578 579 bool CheckOatFile(const OatFile* oat_file, InstructionSet isa, 580 bool* checksum_verified, std::string* error_msg); 581 int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa); 582 583 // Note: will not register the oat file. 584 const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location, 585 uint32_t dex_location_checksum, 586 const char* oat_location, 587 std::string* error_msg) 588 LOCKS_EXCLUDED(dex_lock_); 589 590 // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for 591 // the file to be written, which is assumed to be under a lock. 592 const OatFile* CreateOatFileForDexLocation(const char* dex_location, 593 int fd, const char* oat_location, 594 std::vector<std::string>* error_msgs) 595 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 596 597 // Finds an OatFile that contains a DexFile for the given a DexFile location. 598 // 599 // Note 1: this will not check open oat files, which are assumed to be stale when this is run. 600 // Note 2: Does not register the oat file. It is the caller's job to register if the file is to 601 // be kept. 602 const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* dex_location, 603 const uint32_t* dex_location_checksum, 604 InstructionSet isa, 605 std::vector<std::string>* error_msgs, 606 bool* obsolete_file_cleanup_failed) 607 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 608 609 // Verifies: 610 // - that the oat file contains the dex file (with a matching checksum, which may be null if the 611 // file was pre-opted) 612 // - the checksums of the oat file (against the image space) 613 // - the checksum of the dex file against dex_location_checksum 614 // - that the dex file can be opened 615 // Returns true iff all verification succeed. 616 // 617 // The dex_location is the dex location as stored in the oat file header. 618 // (see DexFile::GetDexCanonicalLocation for a description of location conventions) 619 bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location, 620 const uint32_t* dex_location_checksum, 621 std::string* error_msg); 622 623 mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass, 624 mirror::Class* proxy_class) 625 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 626 mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass, 627 Handle<mirror::ArtMethod> prototype) 628 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 629 630 mirror::Class* LookupClassFromTableLocked(const char* descriptor, 631 const mirror::ClassLoader* class_loader, 632 size_t hash) 633 SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 634 635 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) 636 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 637 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 638 639 void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 640 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 641 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 642 mirror::Class* LookupClassFromImage(const char* descriptor) 643 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 644 645 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved 646 // before returning it to the caller. Its the responsibility of the thread that placed the class 647 // in the table to make it resolved. The thread doing resolution must notify on the class' lock 648 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may 649 // retire a class, the version of the class in the table is returned and this may differ from 650 // the class passed in. 651 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass) 652 WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 653 654 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) 655 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 656 657 std::vector<const DexFile*> boot_class_path_; 658 659 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 660 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);; 661 std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_); 662 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_); 663 664 665 // multimap from a string hash code of a class descriptor to 666 // mirror::Class* instances. Results should be compared for a matching 667 // Class::descriptor_ and Class::class_loader_. 668 typedef AllocationTrackingMultiMap<size_t, GcRoot<mirror::Class>, kAllocatorTagClassTable> Table; 669 // This contains strong roots. To enable concurrent root scanning of 670 // the class table, be careful to use a read barrier when accessing this. 671 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_); 672 std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_; 673 674 // Do we need to search dex caches to find image classes? 675 bool dex_cache_image_class_lookup_required_; 676 // Number of times we've searched dex caches for a class. After a certain number of misses we move 677 // the classes into the class_table_ to avoid dex cache based searches. 678 Atomic<uint32_t> failed_dex_cache_class_lookups_; 679 680 // indexes into class_roots_. 681 // needs to be kept in sync with class_roots_descriptors_. 682 enum ClassRoot { 683 kJavaLangClass, 684 kJavaLangObject, 685 kClassArrayClass, 686 kObjectArrayClass, 687 kJavaLangString, 688 kJavaLangDexCache, 689 kJavaLangRefReference, 690 kJavaLangReflectArtField, 691 kJavaLangReflectArtMethod, 692 kJavaLangReflectProxy, 693 kJavaLangStringArrayClass, 694 kJavaLangReflectArtFieldArrayClass, 695 kJavaLangReflectArtMethodArrayClass, 696 kJavaLangClassLoader, 697 kJavaLangThrowable, 698 kJavaLangClassNotFoundException, 699 kJavaLangStackTraceElement, 700 kPrimitiveBoolean, 701 kPrimitiveByte, 702 kPrimitiveChar, 703 kPrimitiveDouble, 704 kPrimitiveFloat, 705 kPrimitiveInt, 706 kPrimitiveLong, 707 kPrimitiveShort, 708 kPrimitiveVoid, 709 kBooleanArrayClass, 710 kByteArrayClass, 711 kCharArrayClass, 712 kDoubleArrayClass, 713 kFloatArrayClass, 714 kIntArrayClass, 715 kLongArrayClass, 716 kShortArrayClass, 717 kJavaLangStackTraceElementArrayClass, 718 kClassRootsMax, 719 }; 720 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_; 721 722 mirror::Class* GetClassRoot(ClassRoot class_root) 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 static const char* class_roots_descriptors_[]; 728 729 const char* GetClassRootDescriptor(ClassRoot class_root) { 730 const char* descriptor = class_roots_descriptors_[class_root]; 731 CHECK(descriptor != NULL); 732 return descriptor; 733 } 734 735 // The interface table used by all arrays. 736 GcRoot<mirror::IfTable> array_iftable_; 737 738 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class 739 // descriptors for the sake of performing FindClass. 740 static constexpr size_t kFindArrayCacheSize = 16; 741 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize]; 742 size_t find_array_class_cache_next_victim_; 743 744 bool init_done_; 745 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_); 746 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 747 748 InternTable* intern_table_; 749 750 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single 751 // patch point within the image. TODO: make these proper relocations. 752 const void* portable_resolution_trampoline_; 753 const void* quick_resolution_trampoline_; 754 const void* portable_imt_conflict_trampoline_; 755 const void* quick_imt_conflict_trampoline_; 756 const void* quick_generic_jni_trampoline_; 757 const void* quick_to_interpreter_bridge_trampoline_; 758 759 friend class ImageWriter; // for GetClassRoots 760 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation 761 friend class ElfPatcher; // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation 762 friend class NoDex2OatTest; // for FindOpenedOatFileForDexFile 763 friend class NoPatchoatTest; // for FindOpenedOatFileForDexFile 764 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors); 765 FRIEND_TEST(mirror::DexCacheTest, Open); 766 FRIEND_TEST(ExceptionTest, FindExceptionHandler); 767 FRIEND_TEST(ObjectTest, AllocObjectArray); 768 DISALLOW_COPY_AND_ASSIGN(ClassLinker); 769}; 770 771} // namespace art 772 773#endif // ART_RUNTIME_CLASS_LINKER_H_ 774