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