class_linker.h revision 64ad14dbe2225441fb7734bf6d89358d96692eea
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 InitWithoutImage(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 // Get compiled code for a method, return null if no code 348 // exists. This is unlike Get..OatCodeFor which will return a bridge 349 // or interpreter entrypoint. 350 const void* GetOatMethodQuickCodeFor(mirror::ArtMethod* method) 351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 352 const void* GetOatMethodPortableCodeFor(mirror::ArtMethod* method) 353 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 354 355 pid_t GetClassesLockOwner(); // For SignalCatcher. 356 pid_t GetDexLockOwner(); // For SignalCatcher. 357 358 const void* GetPortableResolutionTrampoline() const { 359 return portable_resolution_trampoline_; 360 } 361 362 const void* GetQuickGenericJniTrampoline() const { 363 return quick_generic_jni_trampoline_; 364 } 365 366 const void* GetQuickResolutionTrampoline() const { 367 return quick_resolution_trampoline_; 368 } 369 370 const void* GetPortableImtConflictTrampoline() const { 371 return portable_imt_conflict_trampoline_; 372 } 373 374 const void* GetQuickImtConflictTrampoline() const { 375 return quick_imt_conflict_trampoline_; 376 } 377 378 const void* GetQuickToInterpreterBridgeTrampoline() const { 379 return quick_to_interpreter_bridge_trampoline_; 380 } 381 382 InternTable* GetInternTable() const { 383 return intern_table_; 384 } 385 386 // Attempts to insert a class into a class table. Returns NULL if 387 // the class was inserted, otherwise returns an existing class with 388 // the same descriptor and ClassLoader. 389 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash) 390 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 392 393 // Special code to allocate an art method, use this instead of class->AllocObject. 394 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 395 396 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 397 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); 398 DCHECK(class_roots != NULL); 399 return class_roots; 400 } 401 402 private: 403 const OatFile::OatMethod FindOatMethodFor(mirror::ArtMethod* method, bool* found) 404 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 405 406 OatFile& GetImageOatFile(gc::space::ImageSpace* space) 407 LOCKS_EXCLUDED(dex_lock_) 408 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 409 410 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 411 412 // For early bootstrapping by Init 413 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size) 414 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 415 416 // Alloc* convenience functions to avoid needing to pass in mirror::Class* 417 // values that are known to the ClassLinker such as 418 // kObjectArrayClass and kJavaLangString etc. 419 mirror::Class* AllocClass(Thread* self, uint32_t class_size) 420 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 421 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file) 422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 423 mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 424 425 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type) 426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 427 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type) 428 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 429 430 431 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor, 432 Handle<mirror::ClassLoader> class_loader) 433 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 434 435 void AppendToBootClassPath(const DexFile& dex_file) 436 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 437 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 439 440 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, 441 mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map) 442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 443 444 // Precomputes size needed for Class, in the case of a non-temporary class this size must be 445 // sufficient to hold all static fields. 446 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, 447 const DexFile::ClassDef& dex_class_def); 448 449 void LoadClass(const DexFile& dex_file, 450 const DexFile::ClassDef& dex_class_def, 451 Handle<mirror::Class> klass, 452 mirror::ClassLoader* class_loader) 453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 454 void LoadClassMembers(const DexFile& dex_file, 455 const byte* class_data, 456 Handle<mirror::Class> klass, 457 mirror::ClassLoader* class_loader, 458 const OatFile::OatClass* oat_class) 459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 460 461 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, 462 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst) 463 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 464 465 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file, 466 const ClassDataItemIterator& dex_method, 467 Handle<mirror::Class> klass) 468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 469 470 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 471 472 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on 473 // error and sets found to false. 474 OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found) 475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 476 477 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache) 478 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_) 479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 480 bool IsDexFileRegisteredLocked(const DexFile& dex_file) 481 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_); 482 483 bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit, 484 bool can_init_parents) 485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 486 bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, 487 ObjectLock<mirror::Class>& lock); 488 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass) 489 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 490 491 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor, 492 Handle<mirror::ClassLoader> class_loader1, 493 Handle<mirror::ClassLoader> class_loader2) 494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 495 496 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method, 497 mirror::Class* klass1, 498 mirror::Class* klass2) 499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 500 501 bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, 502 Handle<mirror::ObjectArray<mirror::Class>> interfaces, 503 mirror::Class** new_class) 504 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 505 506 bool LinkSuperClass(Handle<mirror::Class> klass) 507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 508 509 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) 510 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 511 512 bool LinkMethods(Thread* self, Handle<mirror::Class> klass, 513 Handle<mirror::ObjectArray<mirror::Class>> interfaces) 514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 515 516 bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) 517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 518 519 bool LinkInterfaceMethods(Handle<mirror::Class> klass, 520 Handle<mirror::ObjectArray<mirror::Class>> interfaces) 521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 522 523 bool LinkStaticFields(Handle<mirror::Class> klass, size_t* class_size) 524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 525 bool LinkInstanceFields(Handle<mirror::Class> klass) 526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 527 bool LinkFields(Handle<mirror::Class> klass, bool is_static, size_t* class_size) 528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 529 void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class, 530 const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index) 531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 532 533 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) 534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 535 void CreateReferenceStaticOffsets(Handle<mirror::Class> klass) 536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 537 void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static, 538 uint32_t reference_offsets) 539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 540 541 // For use by ImageWriter to find DexCaches for its roots 542 ReaderWriterMutex* DexLock() 543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) { 544 return &dex_lock_; 545 } 546 size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) { 547 return dex_caches_.size(); 548 } 549 mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_); 550 551 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file) 552 LOCKS_EXCLUDED(dex_lock_) 553 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 554 555 // Find an opened oat file that contains dex_location. If oat_location is not nullptr, the file 556 // must have that location, else any oat location is accepted. 557 const OatFile* FindOpenedOatFile(const char* oat_location, const char* dex_location, 558 const uint32_t* const dex_location_checksum) 559 LOCKS_EXCLUDED(dex_lock_); 560 561 // Will open the oat file directly without relocating, even if we could/should do relocation. 562 const OatFile* FindOatFileFromOatLocation(const std::string& oat_location, 563 std::string* error_msg) 564 LOCKS_EXCLUDED(dex_lock_); 565 566 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) 567 LOCKS_EXCLUDED(dex_lock_); 568 569 const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location, 570 InstructionSet isa, 571 bool* already_opened, 572 bool* obsolete_file_cleanup_failed, 573 std::vector<std::string>* error_msg) 574 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 575 576 const OatFile* GetInterpretedOnlyOat(const std::string& oat_path, 577 InstructionSet isa, 578 std::string* error_msg); 579 580 const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output, 581 const std::string& image_location, InstructionSet isa, 582 std::string* error_msg) 583 LOCKS_EXCLUDED(Locks::mutator_lock_); 584 585 bool CheckOatFile(const OatFile* oat_file, InstructionSet isa, 586 bool* checksum_verified, std::string* error_msg); 587 int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa); 588 589 // Note: will not register the oat file. 590 const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location, 591 uint32_t dex_location_checksum, 592 const char* oat_location, 593 std::string* error_msg) 594 LOCKS_EXCLUDED(dex_lock_); 595 596 // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for 597 // the file to be written, which is assumed to be under a lock. 598 const OatFile* CreateOatFileForDexLocation(const char* dex_location, 599 int fd, const char* oat_location, 600 std::vector<std::string>* error_msgs) 601 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 602 603 // Finds an OatFile that contains a DexFile for the given a DexFile location. 604 // 605 // Note 1: this will not check open oat files, which are assumed to be stale when this is run. 606 // Note 2: Does not register the oat file. It is the caller's job to register if the file is to 607 // be kept. 608 const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* location, 609 const uint32_t* const location_checksum, 610 InstructionSet isa, 611 std::vector<std::string>* error_msgs, 612 bool* obsolete_file_cleanup_failed) 613 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 614 615 // verify an oat file with the given dex file. Will return false when the dex file could not be 616 // verified. Will return true otherwise. 617 bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location, 618 std::string* error_msg); 619 620 mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass, 621 mirror::Class* proxy_class) 622 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 623 mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass, 624 Handle<mirror::ArtMethod> prototype) 625 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 626 627 std::vector<const DexFile*> boot_class_path_; 628 629 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 630 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);; 631 std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_); 632 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_); 633 634 635 // multimap from a string hash code of a class descriptor to 636 // mirror::Class* instances. Results should be compared for a matching 637 // Class::descriptor_ and Class::class_loader_. 638 typedef std::multimap<size_t, GcRoot<mirror::Class>> Table; 639 // This contains strong roots. To enable concurrent root scanning of 640 // the class table, be careful to use a read barrier when accessing this. 641 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_); 642 std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_; 643 644 // Do we need to search dex caches to find image classes? 645 bool dex_cache_image_class_lookup_required_; 646 // Number of times we've searched dex caches for a class. After a certain number of misses we move 647 // the classes into the class_table_ to avoid dex cache based searches. 648 AtomicInteger failed_dex_cache_class_lookups_; 649 650 mirror::Class* LookupClassFromTableLocked(const char* descriptor, 651 const mirror::ClassLoader* class_loader, 652 size_t hash) 653 SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 654 655 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) 656 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 657 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 658 659 void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 660 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 661 mirror::Class* LookupClassFromImage(const char* descriptor) 662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 663 664 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved 665 // before returning it to the caller. Its the responsibility of the thread that placed the class 666 // in the table to make it resolved. The thread doing resolution must notify on the class' lock 667 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may 668 // retire a class, the version of the class in the table is returned and this may differ from 669 // the class passed in. 670 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass) 671 WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 672 673 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) 674 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 675 676 // indexes into class_roots_. 677 // needs to be kept in sync with class_roots_descriptors_. 678 enum ClassRoot { 679 kJavaLangClass, 680 kJavaLangObject, 681 kClassArrayClass, 682 kObjectArrayClass, 683 kJavaLangString, 684 kJavaLangDexCache, 685 kJavaLangRefReference, 686 kJavaLangReflectArtField, 687 kJavaLangReflectArtMethod, 688 kJavaLangReflectProxy, 689 kJavaLangStringArrayClass, 690 kJavaLangReflectArtFieldArrayClass, 691 kJavaLangReflectArtMethodArrayClass, 692 kJavaLangClassLoader, 693 kJavaLangThrowable, 694 kJavaLangClassNotFoundException, 695 kJavaLangStackTraceElement, 696 kPrimitiveBoolean, 697 kPrimitiveByte, 698 kPrimitiveChar, 699 kPrimitiveDouble, 700 kPrimitiveFloat, 701 kPrimitiveInt, 702 kPrimitiveLong, 703 kPrimitiveShort, 704 kPrimitiveVoid, 705 kBooleanArrayClass, 706 kByteArrayClass, 707 kCharArrayClass, 708 kDoubleArrayClass, 709 kFloatArrayClass, 710 kIntArrayClass, 711 kLongArrayClass, 712 kShortArrayClass, 713 kJavaLangStackTraceElementArrayClass, 714 kClassRootsMax, 715 }; 716 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_; 717 718 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 719 720 void SetClassRoot(ClassRoot class_root, mirror::Class* klass) 721 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 722 723 static const char* class_roots_descriptors_[]; 724 725 const char* GetClassRootDescriptor(ClassRoot class_root) { 726 const char* descriptor = class_roots_descriptors_[class_root]; 727 CHECK(descriptor != NULL); 728 return descriptor; 729 } 730 731 // The interface table used by all arrays. 732 GcRoot<mirror::IfTable> array_iftable_; 733 734 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class 735 // descriptors for the sake of performing FindClass. 736 static constexpr size_t kFindArrayCacheSize = 16; 737 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize]; 738 size_t find_array_class_cache_next_victim_; 739 740 bool init_done_; 741 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_); 742 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 743 744 InternTable* intern_table_; 745 746 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single 747 // patch point within the image. TODO: make these proper relocations. 748 const void* portable_resolution_trampoline_; 749 const void* quick_resolution_trampoline_; 750 const void* portable_imt_conflict_trampoline_; 751 const void* quick_imt_conflict_trampoline_; 752 const void* quick_generic_jni_trampoline_; 753 const void* quick_to_interpreter_bridge_trampoline_; 754 755 friend class ImageWriter; // for GetClassRoots 756 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation 757 friend class ElfPatcher; // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation 758 friend class NoDex2OatTest; // for FindOpenedOatFileForDexFile 759 friend class NoPatchoatTest; // for FindOpenedOatFileForDexFile 760 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors); 761 FRIEND_TEST(mirror::DexCacheTest, Open); 762 FRIEND_TEST(ExceptionTest, FindExceptionHandler); 763 FRIEND_TEST(ObjectTest, AllocObjectArray); 764 DISALLOW_COPY_AND_ASSIGN(ClassLinker); 765}; 766 767} // namespace art 768 769#endif // ART_RUNTIME_CLASS_LINKER_H_ 770