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