• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/compiler_filter.h"
27 #include "base/locks.h"
28 #include "base/macros.h"
29 #include "jni.h"
30 
31 namespace art {
32 
33 namespace gc {
34 namespace space {
35 class ImageSpace;
36 }  // namespace space
37 }  // namespace gc
38 
39 class ClassLoaderContext;
40 class DexFile;
41 class MemMap;
42 class OatFile;
43 class ThreadPool;
44 
45 // Class for dealing with oat file management.
46 //
47 // This class knows about all the loaded oat files and provides utility functions. The oat file
48 // pointers returned from functions are always valid.
49 class OatFileManager {
50  public:
51   OatFileManager();
52   ~OatFileManager();
53 
54   // Add an oat file to the internal accounting, std::aborts if there already exists an oat file
55   // with the same base address. Returns the oat file pointer from oat_file.
56   const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file)
57       REQUIRES(!Locks::oat_file_manager_lock_);
58 
59   void UnRegisterAndDeleteOatFile(const OatFile* oat_file)
60       REQUIRES(!Locks::oat_file_manager_lock_);
61 
62   // Find the first opened oat file with the same location, returns null if there are none.
63   const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const
64       REQUIRES(!Locks::oat_file_manager_lock_);
65 
66   // Find the oat file which contains a dex files with the given dex base location,
67   // returns null if there are none.
68   const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const
69       REQUIRES(!Locks::oat_file_manager_lock_);
70 
71   // Returns the boot image oat files.
72   std::vector<const OatFile*> GetBootOatFiles() const;
73 
74   // Returns the oat files for the images, registers the oat files.
75   // Takes ownership of the imagespace's underlying oat files.
76   std::vector<const OatFile*> RegisterImageOatFiles(
77       const std::vector<gc::space::ImageSpace*>& spaces)
78       REQUIRES(!Locks::oat_file_manager_lock_);
79 
80   // Finds or creates the oat file holding dex_location. Then loads and returns
81   // all corresponding dex files (there may be more than one dex file loaded
82   // in the case of multidex).
83   // This may return the original, unquickened dex files if the oat file could
84   // not be generated.
85   //
86   // Returns an empty vector if the dex files could not be loaded. In this
87   // case, there will be at least one error message returned describing why no
88   // dex files could not be loaded. The 'error_msgs' argument must not be
89   // null, regardless of whether there is an error or not.
90   //
91   // This method should not be called with the mutator_lock_ held, because it
92   // could end up starving GC if we need to generate or relocate any oat
93   // files.
94   std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
95       const char* dex_location,
96       jobject class_loader,
97       jobjectArray dex_elements,
98       /*out*/ const OatFile** out_oat_file,
99       /*out*/ std::vector<std::string>* error_msgs)
100       REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
101 
102   // Opens dex files provided in `dex_mem_maps` and attempts to find an anonymous
103   // vdex file created during a previous load attempt. If found, will initialize
104   // an instance of OatFile to back the DexFiles and preverify them using the
105   // vdex's VerifierDeps.
106   //
107   // Returns an empty vector if the dex files could not be loaded. In this
108   // case, there will be at least one error message returned describing why no
109   // dex files could not be loaded. The 'error_msgs' argument must not be
110   // null, regardless of whether there is an error or not.
111   std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
112       std::vector<MemMap>&& dex_mem_maps,
113       jobject class_loader,
114       jobjectArray dex_elements,
115       /*out*/ const OatFile** out_oat_file,
116       /*out*/ std::vector<std::string>* error_msgs)
117       REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
118 
119   void DumpForSigQuit(std::ostream& os);
120 
121   void SetOnlyUseTrustedOatFiles();
122 
123   // Spawn a background thread which verifies all classes in the given dex files.
124   void RunBackgroundVerification(const std::vector<const DexFile*>& dex_files,
125                                  jobject class_loader);
126 
127   // Wait for thread pool workers to be created. This is used during shutdown as
128   // threads are not allowed to attach while runtime is in shutdown lock.
129   void WaitForWorkersToBeCreated();
130 
131   // If allocated, delete a thread pool of background verification threads.
132   void DeleteThreadPool();
133 
134   // Wait for all background verification tasks to finish. This is only used by tests.
135   void WaitForBackgroundVerificationTasks();
136 
137   // Maximum number of anonymous vdex files kept in the process' data folder.
138   static constexpr size_t kAnonymousVdexCacheSize = 8u;
139 
140  private:
141   std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat_Impl(
142       std::vector<MemMap>&& dex_mem_maps,
143       jobject class_loader,
144       jobjectArray dex_elements,
145       /*out*/ const OatFile** out_oat_file,
146       /*out*/ std::vector<std::string>* error_msgs)
147       REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
148 
149   const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
150       REQUIRES(Locks::oat_file_manager_lock_);
151 
152   // Return true if we should attempt to load the app image.
153   bool ShouldLoadAppImage(const OatFile* source_oat_file) const;
154 
155   std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
156 
157   // Only use the compiled code in an OAT file when the file is on /system. If the OAT file
158   // is not on /system, don't load it "executable".
159   bool only_use_system_oat_files_;
160 
161   // Single-thread pool used to run the verifier in the background.
162   std::unique_ptr<ThreadPool> verification_thread_pool_;
163 
164   DISALLOW_COPY_AND_ASSIGN(OatFileManager);
165 };
166 
167 }  // namespace art
168 
169 #endif  // ART_RUNTIME_OAT_FILE_MANAGER_H_
170