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