1 /*
2  * Copyright (C) 2015 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_OAT_FILE_MANAGER_H_
18 #define ART_RUNTIME_OAT_FILE_MANAGER_H_
19 
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "base/macros.h"
27 #include "base/mutex.h"
28 #include "jni.h"
29 
30 namespace art {
31 
32 namespace gc {
33 namespace space {
34 class ImageSpace;
35 }  // namespace space
36 }  // namespace gc
37 
38 class DexFile;
39 class OatFile;
40 
41 // Class for dealing with oat file management.
42 //
43 // This class knows about all the loaded oat files and provides utility functions. The oat file
44 // pointers returned from functions are always valid.
45 class OatFileManager {
46  public:
OatFileManager()47   OatFileManager() : have_non_pic_oat_file_(false) {}
48   ~OatFileManager();
49 
50   // Add an oat file to the internal accounting, std::aborts if there already exists an oat file
51   // with the same base address. Returns the oat file pointer from oat_file.
52   const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file)
53       REQUIRES(!Locks::oat_file_manager_lock_);
54 
55   void UnRegisterAndDeleteOatFile(const OatFile* oat_file)
56       REQUIRES(!Locks::oat_file_manager_lock_);
57 
58   // Find the first opened oat file with the same location, returns null if there are none.
59   const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const
60       REQUIRES(!Locks::oat_file_manager_lock_);
61 
62   // Find the oat file which contains a dex files with the given dex base location,
63   // returns null if there are none.
64   const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const
65       REQUIRES(!Locks::oat_file_manager_lock_);
66 
67   // Returns true if we have a non pic oat file.
HaveNonPicOatFile()68   bool HaveNonPicOatFile() const {
69     return have_non_pic_oat_file_;
70   }
71 
72   // Returns the boot image oat files.
73   std::vector<const OatFile*> GetBootOatFiles() const;
74 
75   // Returns the first non-image oat file in the class path.
76   const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_);
77 
78   // Returns the oat files for the images, registers the oat files.
79   // Takes ownership of the imagespace's underlying oat files.
80   std::vector<const OatFile*> RegisterImageOatFiles(std::vector<gc::space::ImageSpace*> spaces)
81       REQUIRES(!Locks::oat_file_manager_lock_);
82 
83   // Finds or creates the oat file holding dex_location. Then loads and returns
84   // all corresponding dex files (there may be more than one dex file loaded
85   // in the case of multidex).
86   // This may return the original, unquickened dex files if the oat file could
87   // not be generated.
88   //
89   // Returns an empty vector if the dex files could not be loaded. In this
90   // case, there will be at least one error message returned describing why no
91   // dex files could not be loaded. The 'error_msgs' argument must not be
92   // null, regardless of whether there is an error or not.
93   //
94   // This method should not be called with the mutator_lock_ held, because it
95   // could end up starving GC if we need to generate or relocate any oat
96   // files.
97   std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
98       const char* dex_location,
99       jobject class_loader,
100       jobjectArray dex_elements,
101       /*out*/ const OatFile** out_oat_file,
102       /*out*/ std::vector<std::string>* error_msgs)
103       REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
104 
105   void DumpForSigQuit(std::ostream& os);
106 
107  private:
108   // Check that the shared libraries in the given oat file match those in the given class loader and
109   // dex elements. If the class loader is null or we do not support one of the class loaders in the
110   // chain, compare against all non-boot oat files instead. If the shared libraries are not ok,
111   // check for duplicate class definitions of the given oat file against the oat files (either from
112   // the class loader and dex elements if possible or all non-boot oat files otherwise).
113   // Return true if there are any class definition collisions in the oat_file.
114   bool HasCollisions(const OatFile* oat_file,
115                      jobject class_loader,
116                      jobjectArray dex_elements,
117                      /*out*/ std::string* error_msg) const
118       REQUIRES(!Locks::oat_file_manager_lock_);
119 
120   const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
121       REQUIRES(Locks::oat_file_manager_lock_);
122 
123   std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
124   bool have_non_pic_oat_file_;
125 
126   DISALLOW_COPY_AND_ASSIGN(OatFileManager);
127 };
128 
129 }  // namespace art
130 
131 #endif  // ART_RUNTIME_OAT_FILE_MANAGER_H_
132