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