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