class_linker.h revision 63557459a4098294a9ff44d035241de2966047c0
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 // Finds a class by its descriptor using the "system" class loader, ie by searching the 80 // boot_class_path_. 81 mirror::Class* FindSystemClass(Thread* self, const char* descriptor) 82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 83 84 // Finds the array class given for the element class. 85 mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class) 86 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 87 88 // Returns true if the class linker is initialized. 89 bool IsInitialized() const; 90 91 // Define a new a class based on a ClassDef from a DexFile 92 mirror::Class* DefineClass(const char* descriptor, 93 ConstHandle<mirror::ClassLoader> class_loader, 94 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def) 95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 96 97 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded 98 // by the given 'class_loader'. 99 mirror::Class* LookupClass(const char* descriptor, const mirror::ClassLoader* class_loader) 100 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 102 103 // Finds all the classes with the given descriptor, regardless of ClassLoader. 104 void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes) 105 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 107 108 mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 109 110 // General class unloading is not supported, this is used to prune 111 // unwanted classes during image writing. 112 bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader) 113 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 115 116 void DumpAllClasses(int flags) 117 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 119 120 void DumpForSigQuit(std::ostream& os) 121 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 123 124 size_t NumLoadedClasses() 125 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 127 128 // Resolve a String with the given index from the DexFile, storing the 129 // result in the DexCache. The referrer is used to identify the 130 // target DexCache and ClassLoader to use for resolution. 131 mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer) 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. 136 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx, 137 ConstHandle<mirror::DexCache> dex_cache) 138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 139 140 // Resolve a Type with the given index from the DexFile, storing the 141 // result in the DexCache. The referrer is used to identity the 142 // target DexCache and ClassLoader to use for resolution. 143 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer) 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 identify the 148 // target DexCache and ClassLoader to use for resolution. 149 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer) 150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 151 152 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer) 153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 154 155 // Resolve a type with the given ID from the DexFile, storing the 156 // result in DexCache. The ClassLoader is used to search for the 157 // type, since it may be referenced from but not contained within 158 // the given DexFile. 159 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, 160 ConstHandle<mirror::DexCache> dex_cache, 161 ConstHandle<mirror::ClassLoader> class_loader) 162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 163 164 // Resolve a method with a given ID from the DexFile, storing the 165 // result in DexCache. The ClassLinker and ClassLoader are used as 166 // in ResolveType. What is unique is the method type argument which 167 // is used to determine if this method is a direct, static, or 168 // virtual method. 169 mirror::ArtMethod* ResolveMethod(const DexFile& dex_file, 170 uint32_t method_idx, 171 ConstHandle<mirror::DexCache> dex_cache, 172 ConstHandle<mirror::ClassLoader> class_loader, 173 ConstHandle<mirror::ArtMethod> referrer, 174 InvokeType type) 175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 176 177 mirror::ArtMethod* GetResolvedMethod(uint32_t method_idx, mirror::ArtMethod* referrer, 178 InvokeType type) 179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 180 mirror::ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, mirror::ArtMethod** referrer, 181 InvokeType type) 182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 183 184 mirror::ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class) 185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 186 mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer, 187 bool is_static) 188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 189 190 // Resolve a field with a given ID from the DexFile, storing the 191 // result in DexCache. The ClassLinker and ClassLoader are used as 192 // in ResolveType. What is unique is the is_static argument which is 193 // used to determine if we are resolving a static or non-static 194 // field. 195 mirror::ArtField* ResolveField(const DexFile& dex_file, 196 uint32_t field_idx, 197 ConstHandle<mirror::DexCache> dex_cache, 198 ConstHandle<mirror::ClassLoader> class_loader, 199 bool is_static) 200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 201 202 // Resolve a field with a given ID from the DexFile, storing the 203 // result in DexCache. The ClassLinker and ClassLoader are used as 204 // in ResolveType. No is_static argument is provided so that Java 205 // field resolution semantics are followed. 206 mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx, 207 ConstHandle<mirror::DexCache> dex_cache, 208 ConstHandle<mirror::ClassLoader> class_loader) 209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 210 211 // Get shorty from method index without resolution. Used to do handlerization. 212 const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length) 213 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 214 215 // Returns true on success, false if there's an exception pending. 216 // can_run_clinit=false allows the compiler to attempt to init a class, 217 // given the restriction that no <clinit> execution is possible. 218 bool EnsureInitialized(ConstHandle<mirror::Class> c, bool can_init_fields, bool can_init_parents) 219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 220 221 // Initializes classes that have instances in the image but that have 222 // <clinit> methods so they could not be initialized by the compiler. 223 void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 224 225 void RegisterDexFile(const DexFile& dex_file) 226 LOCKS_EXCLUDED(dex_lock_) 227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 228 void RegisterDexFile(const DexFile& dex_file, ConstHandle<mirror::DexCache> dex_cache) 229 LOCKS_EXCLUDED(dex_lock_) 230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 231 232 const OatFile* RegisterOatFile(const OatFile* oat_file) 233 LOCKS_EXCLUDED(dex_lock_); 234 235 const std::vector<const DexFile*>& GetBootClassPath() { 236 return boot_class_path_; 237 } 238 239 void VisitClasses(ClassVisitor* visitor, void* arg) 240 LOCKS_EXCLUDED(dex_lock_) 241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 242 // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_ 243 // when calling the visitor. 244 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) 245 LOCKS_EXCLUDED(dex_lock_) 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(ConstHandle<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 ConstHandle<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 // Get compiled code for a method, return null if no code 349 // exists. This is unlike Get..OatCodeFor which will return a bridge 350 // or interpreter entrypoint. 351 const void* GetOatMethodQuickCodeFor(mirror::ArtMethod* method) 352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 353 const void* GetOatMethodPortableCodeFor(mirror::ArtMethod* method) 354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 355 356 pid_t GetClassesLockOwner(); // For SignalCatcher. 357 pid_t GetDexLockOwner(); // For SignalCatcher. 358 359 const void* GetPortableResolutionTrampoline() const { 360 return portable_resolution_trampoline_; 361 } 362 363 const void* GetQuickGenericJniTrampoline() const { 364 return quick_generic_jni_trampoline_; 365 } 366 367 const void* GetQuickResolutionTrampoline() const { 368 return quick_resolution_trampoline_; 369 } 370 371 const void* GetPortableImtConflictTrampoline() const { 372 return portable_imt_conflict_trampoline_; 373 } 374 375 const void* GetQuickImtConflictTrampoline() const { 376 return quick_imt_conflict_trampoline_; 377 } 378 379 const void* GetQuickToInterpreterBridgeTrampoline() const { 380 return quick_to_interpreter_bridge_trampoline_; 381 } 382 383 InternTable* GetInternTable() const { 384 return intern_table_; 385 } 386 387 // Attempts to insert a class into a class table. Returns NULL if 388 // the class was inserted, otherwise returns an existing class with 389 // the same descriptor and ClassLoader. 390 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash) 391 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 392 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 393 394 // Special code to allocate an art method, use this instead of class->AllocObject. 395 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 396 397 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 398 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); 399 DCHECK(class_roots != NULL); 400 return class_roots; 401 } 402 403 private: 404 const OatFile::OatMethod FindOatMethodFor(mirror::ArtMethod* method, bool* found) 405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 406 407 OatFile& GetImageOatFile(gc::space::ImageSpace* space) 408 LOCKS_EXCLUDED(dex_lock_) 409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 410 411 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 412 413 // For early bootstrapping by Init 414 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size) 415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 416 417 // Alloc* convenience functions to avoid needing to pass in mirror::Class* 418 // values that are known to the ClassLinker such as 419 // kObjectArrayClass and kJavaLangString etc. 420 mirror::Class* AllocClass(Thread* self, uint32_t class_size) 421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 422 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file) 423 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 424 mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 425 426 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type) 427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 428 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type) 429 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 430 431 432 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor, 433 ConstHandle<mirror::ClassLoader> class_loader) 434 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 435 436 void AppendToBootClassPath(const DexFile& dex_file) 437 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 438 void AppendToBootClassPath(const DexFile& dex_file, ConstHandle<mirror::DexCache> dex_cache) 439 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 440 441 // Precomputes size needed for Class, in the case of a non-temporary class this size must be 442 // sufficient to hold all static fields. 443 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, 444 const DexFile::ClassDef& dex_class_def); 445 446 void LoadClass(const DexFile& dex_file, 447 const DexFile::ClassDef& dex_class_def, 448 ConstHandle<mirror::Class> klass, 449 mirror::ClassLoader* class_loader) 450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 451 void LoadClassMembers(const DexFile& dex_file, 452 const byte* class_data, 453 ConstHandle<mirror::Class> klass, 454 mirror::ClassLoader* class_loader, 455 const OatFile::OatClass* oat_class) 456 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 457 458 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, 459 ConstHandle<mirror::Class> klass, ConstHandle<mirror::ArtField> dst) 460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 461 462 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file, 463 const ClassDataItemIterator& dex_method, 464 ConstHandle<mirror::Class> klass) 465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 466 467 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 468 469 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on 470 // error and sets found to false. 471 OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found) 472 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 473 474 void RegisterDexFileLocked(const DexFile& dex_file, ConstHandle<mirror::DexCache> dex_cache) 475 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_) 476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 477 bool IsDexFileRegisteredLocked(const DexFile& dex_file) 478 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_); 479 480 bool InitializeClass(ConstHandle<mirror::Class> klass, bool can_run_clinit, 481 bool can_init_parents) 482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 483 bool WaitForInitializeClass(ConstHandle<mirror::Class> klass, Thread* self, 484 ObjectLock<mirror::Class>& lock); 485 bool ValidateSuperClassDescriptors(ConstHandle<mirror::Class> klass) 486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 487 488 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor, 489 ConstHandle<mirror::ClassLoader> class_loader1, 490 ConstHandle<mirror::ClassLoader> class_loader2) 491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 492 493 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method, 494 mirror::Class* klass1, 495 mirror::Class* klass2) 496 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 497 498 bool LinkClass(Thread* self, const char* descriptor, ConstHandle<mirror::Class> klass, 499 ConstHandle<mirror::ObjectArray<mirror::Class>> interfaces, 500 mirror::Class** new_class) 501 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 502 503 bool LinkSuperClass(ConstHandle<mirror::Class> klass) 504 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 505 506 bool LoadSuperAndInterfaces(ConstHandle<mirror::Class> klass, const DexFile& dex_file) 507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 508 509 bool LinkMethods(Thread* self, ConstHandle<mirror::Class> klass, 510 ConstHandle<mirror::ObjectArray<mirror::Class>> interfaces) 511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 512 513 bool LinkVirtualMethods(Thread* self, ConstHandle<mirror::Class> klass) 514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 515 516 bool LinkInterfaceMethods(ConstHandle<mirror::Class> klass, 517 ConstHandle<mirror::ObjectArray<mirror::Class>> interfaces) 518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 519 520 bool LinkStaticFields(ConstHandle<mirror::Class> klass, size_t* class_size) 521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 522 bool LinkInstanceFields(ConstHandle<mirror::Class> klass) 523 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 524 bool LinkFields(ConstHandle<mirror::Class> klass, bool is_static, size_t* class_size) 525 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 526 void LinkCode(ConstHandle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class, 527 const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index) 528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 529 void CreateReferenceInstanceOffsets(ConstHandle<mirror::Class> klass) 530 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 531 void CreateReferenceStaticOffsets(ConstHandle<mirror::Class> klass) 532 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 533 void CreateReferenceOffsets(ConstHandle<mirror::Class> klass, bool is_static, 534 uint32_t reference_offsets) 535 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 536 537 // For use by ImageWriter to find DexCaches for its roots 538 ReaderWriterMutex* DexLock() 539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) { 540 return &dex_lock_; 541 } 542 size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) { 543 return dex_caches_.size(); 544 } 545 mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_); 546 547 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file) 548 LOCKS_EXCLUDED(dex_lock_) 549 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 550 551 // Find an opened oat file that contains dex_location. If oat_location is not nullptr, the file 552 // must have that location, else any oat location is accepted. 553 const OatFile* FindOpenedOatFile(const char* oat_location, const char* dex_location, 554 const uint32_t* const dex_location_checksum) 555 LOCKS_EXCLUDED(dex_lock_); 556 557 // Will open the oat file directly without relocating, even if we could/should do relocation. 558 const OatFile* FindOatFileFromOatLocation(const std::string& oat_location, 559 std::string* error_msg) 560 LOCKS_EXCLUDED(dex_lock_); 561 562 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) 563 LOCKS_EXCLUDED(dex_lock_); 564 565 const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location, 566 InstructionSet isa, 567 bool* already_opened, 568 bool* obsolete_file_cleanup_failed, 569 std::vector<std::string>* error_msg) 570 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 571 572 const OatFile* GetInterpretedOnlyOat(const std::string& oat_path, 573 InstructionSet isa, 574 std::string* error_msg); 575 576 const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output, 577 const std::string& image_location, InstructionSet isa, 578 std::string* error_msg) 579 LOCKS_EXCLUDED(Locks::mutator_lock_); 580 581 bool CheckOatFile(const OatFile* oat_file, InstructionSet isa, 582 bool* checksum_verified, std::string* error_msg); 583 int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa); 584 585 // Note: will not register the oat file. 586 const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location, 587 uint32_t dex_location_checksum, 588 const char* oat_location, 589 std::string* error_msg) 590 LOCKS_EXCLUDED(dex_lock_); 591 592 // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for 593 // the file to be written, which is assumed to be under a lock. 594 const OatFile* CreateOatFileForDexLocation(const char* dex_location, 595 int fd, const char* oat_location, 596 std::vector<std::string>* error_msgs) 597 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 598 599 // Finds an OatFile that contains a DexFile for the given a DexFile location. 600 // 601 // Note 1: this will not check open oat files, which are assumed to be stale when this is run. 602 // Note 2: Does not register the oat file. It is the caller's job to register if the file is to 603 // be kept. 604 const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* location, 605 const uint32_t* const location_checksum, 606 InstructionSet isa, 607 std::vector<std::string>* error_msgs, 608 bool* obsolete_file_cleanup_failed) 609 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 610 611 // verify an oat file with the given dex file. Will return false when the dex file could not be 612 // verified. Will return true otherwise. 613 bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location, 614 std::string* error_msg); 615 616 mirror::ArtMethod* CreateProxyConstructor(Thread* self, ConstHandle<mirror::Class> klass, 617 mirror::Class* proxy_class) 618 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 619 mirror::ArtMethod* CreateProxyMethod(Thread* self, ConstHandle<mirror::Class> klass, 620 ConstHandle<mirror::ArtMethod> prototype) 621 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 622 623 std::vector<const DexFile*> boot_class_path_; 624 625 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 626 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);; 627 std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_); 628 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_); 629 630 631 // multimap from a string hash code of a class descriptor to 632 // mirror::Class* instances. Results should be compared for a matching 633 // Class::descriptor_ and Class::class_loader_. 634 typedef AllocationTrackingMultiMap<size_t, GcRoot<mirror::Class>, kAllocatorTagClassTable> Table; 635 // This contains strong roots. To enable concurrent root scanning of 636 // the class table, be careful to use a read barrier when accessing this. 637 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_); 638 std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_; 639 640 // Do we need to search dex caches to find image classes? 641 bool dex_cache_image_class_lookup_required_; 642 // Number of times we've searched dex caches for a class. After a certain number of misses we move 643 // the classes into the class_table_ to avoid dex cache based searches. 644 Atomic<uint32_t> failed_dex_cache_class_lookups_; 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 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 // indexes into class_roots_. 673 // needs to be kept in sync with class_roots_descriptors_. 674 enum ClassRoot { 675 kJavaLangClass, 676 kJavaLangObject, 677 kClassArrayClass, 678 kObjectArrayClass, 679 kJavaLangString, 680 kJavaLangDexCache, 681 kJavaLangRefReference, 682 kJavaLangReflectArtField, 683 kJavaLangReflectArtMethod, 684 kJavaLangReflectProxy, 685 kJavaLangStringArrayClass, 686 kJavaLangReflectArtFieldArrayClass, 687 kJavaLangReflectArtMethodArrayClass, 688 kJavaLangClassLoader, 689 kJavaLangThrowable, 690 kJavaLangClassNotFoundException, 691 kJavaLangStackTraceElement, 692 kPrimitiveBoolean, 693 kPrimitiveByte, 694 kPrimitiveChar, 695 kPrimitiveDouble, 696 kPrimitiveFloat, 697 kPrimitiveInt, 698 kPrimitiveLong, 699 kPrimitiveShort, 700 kPrimitiveVoid, 701 kBooleanArrayClass, 702 kByteArrayClass, 703 kCharArrayClass, 704 kDoubleArrayClass, 705 kFloatArrayClass, 706 kIntArrayClass, 707 kLongArrayClass, 708 kShortArrayClass, 709 kJavaLangStackTraceElementArrayClass, 710 kClassRootsMax, 711 }; 712 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_; 713 714 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 715 716 void SetClassRoot(ClassRoot class_root, mirror::Class* klass) 717 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 718 719 static const char* class_roots_descriptors_[]; 720 721 const char* GetClassRootDescriptor(ClassRoot class_root) { 722 const char* descriptor = class_roots_descriptors_[class_root]; 723 CHECK(descriptor != NULL); 724 return descriptor; 725 } 726 727 // The interface table used by all arrays. 728 GcRoot<mirror::IfTable> array_iftable_; 729 730 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class 731 // descriptors for the sake of performing FindClass. 732 static constexpr size_t kFindArrayCacheSize = 16; 733 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize]; 734 size_t find_array_class_cache_next_victim_; 735 736 bool init_done_; 737 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_); 738 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 739 740 InternTable* intern_table_; 741 742 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single 743 // patch point within the image. TODO: make these proper relocations. 744 const void* portable_resolution_trampoline_; 745 const void* quick_resolution_trampoline_; 746 const void* portable_imt_conflict_trampoline_; 747 const void* quick_imt_conflict_trampoline_; 748 const void* quick_generic_jni_trampoline_; 749 const void* quick_to_interpreter_bridge_trampoline_; 750 751 friend class ImageWriter; // for GetClassRoots 752 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation 753 friend class ElfPatcher; // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation 754 friend class NoDex2OatTest; // for FindOpenedOatFileForDexFile 755 friend class NoPatchoatTest; // for FindOpenedOatFileForDexFile 756 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors); 757 FRIEND_TEST(mirror::DexCacheTest, Open); 758 FRIEND_TEST(ExceptionTest, FindExceptionHandler); 759 FRIEND_TEST(ObjectTest, AllocObjectArray); 760 DISALLOW_COPY_AND_ASSIGN(ClassLinker); 761}; 762 763} // namespace art 764 765#endif // ART_RUNTIME_CLASS_LINKER_H_ 766