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