xref: /aosp_15_r20/art/runtime/oat/oat_file_manager.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_OAT_OAT_FILE_MANAGER_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_OAT_OAT_FILE_MANAGER_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <memory>
21*795d594fSAndroid Build Coastguard Worker #include <set>
22*795d594fSAndroid Build Coastguard Worker #include <string>
23*795d594fSAndroid Build Coastguard Worker #include <unordered_map>
24*795d594fSAndroid Build Coastguard Worker #include <vector>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include "base/compiler_filter.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
29*795d594fSAndroid Build Coastguard Worker #include "jni.h"
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker namespace gc {
34*795d594fSAndroid Build Coastguard Worker namespace space {
35*795d594fSAndroid Build Coastguard Worker class ImageSpace;
36*795d594fSAndroid Build Coastguard Worker }  // namespace space
37*795d594fSAndroid Build Coastguard Worker }  // namespace gc
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker class ClassLoaderContext;
40*795d594fSAndroid Build Coastguard Worker class DexFile;
41*795d594fSAndroid Build Coastguard Worker class MemMap;
42*795d594fSAndroid Build Coastguard Worker class OatFile;
43*795d594fSAndroid Build Coastguard Worker class ThreadPool;
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker // Class for dealing with oat file management.
46*795d594fSAndroid Build Coastguard Worker //
47*795d594fSAndroid Build Coastguard Worker // This class knows about all the loaded oat files and provides utility functions. The oat file
48*795d594fSAndroid Build Coastguard Worker // pointers returned from functions are always valid.
49*795d594fSAndroid Build Coastguard Worker class OatFileManager {
50*795d594fSAndroid Build Coastguard Worker  public:
51*795d594fSAndroid Build Coastguard Worker   OatFileManager();
52*795d594fSAndroid Build Coastguard Worker   ~OatFileManager();
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   // Add an oat file to the internal accounting, std::aborts if there already exists an oat file
55*795d594fSAndroid Build Coastguard Worker   // with the same base address. Returns the oat file pointer from oat_file.
56*795d594fSAndroid Build Coastguard Worker   // The `in_memory` parameter is whether the oat file is not present on disk,
57*795d594fSAndroid Build Coastguard Worker   // but only in memory (for example files created with memfd).
58*795d594fSAndroid Build Coastguard Worker   EXPORT const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file,
59*795d594fSAndroid Build Coastguard Worker                                         bool in_memory = false)
60*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_);
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker   void UnRegisterAndDeleteOatFile(const OatFile* oat_file)
63*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_);
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   // Find the first opened oat file with the same location, returns null if there are none.
66*795d594fSAndroid Build Coastguard Worker   EXPORT const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const
67*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_);
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker   // Find the oat file which contains a dex files with the given dex base location,
70*795d594fSAndroid Build Coastguard Worker   // returns null if there are none.
71*795d594fSAndroid Build Coastguard Worker   const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const
72*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_);
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker   // Returns the boot image oat files.
75*795d594fSAndroid Build Coastguard Worker   EXPORT std::vector<const OatFile*> GetBootOatFiles() const;
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   // Returns the oat files for the images, registers the oat files.
78*795d594fSAndroid Build Coastguard Worker   // Takes ownership of the imagespace's underlying oat files.
79*795d594fSAndroid Build Coastguard Worker   std::vector<const OatFile*> RegisterImageOatFiles(
80*795d594fSAndroid Build Coastguard Worker       const std::vector<gc::space::ImageSpace*>& spaces)
81*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_);
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker   // Finds or creates the oat file holding dex_location. Then loads and returns
84*795d594fSAndroid Build Coastguard Worker   // all corresponding dex files (there may be more than one dex file loaded
85*795d594fSAndroid Build Coastguard Worker   // in the case of multidex).
86*795d594fSAndroid Build Coastguard Worker   // This may return the original, unquickened dex files if the oat file could
87*795d594fSAndroid Build Coastguard Worker   // not be generated.
88*795d594fSAndroid Build Coastguard Worker   //
89*795d594fSAndroid Build Coastguard Worker   // Returns an empty vector if the dex files could not be loaded. In this
90*795d594fSAndroid Build Coastguard Worker   // case, there will be at least one error message returned describing why no
91*795d594fSAndroid Build Coastguard Worker   // dex files could not be loaded. The 'error_msgs' argument must not be
92*795d594fSAndroid Build Coastguard Worker   // null, regardless of whether there is an error or not.
93*795d594fSAndroid Build Coastguard Worker   //
94*795d594fSAndroid Build Coastguard Worker   // This method should not be called with the mutator_lock_ held, because it
95*795d594fSAndroid Build Coastguard Worker   // could end up starving GC if we need to generate or relocate any oat
96*795d594fSAndroid Build Coastguard Worker   // files.
97*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
98*795d594fSAndroid Build Coastguard Worker       const char* dex_location,
99*795d594fSAndroid Build Coastguard Worker       jobject class_loader,
100*795d594fSAndroid Build Coastguard Worker       jobjectArray dex_elements,
101*795d594fSAndroid Build Coastguard Worker       /*out*/ const OatFile** out_oat_file,
102*795d594fSAndroid Build Coastguard Worker       /*out*/ std::vector<std::string>* error_msgs)
103*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker   // Opens dex files provided in `dex_mem_maps` and attempts to find an anonymous
106*795d594fSAndroid Build Coastguard Worker   // vdex file created during a previous load attempt. If found, will initialize
107*795d594fSAndroid Build Coastguard Worker   // an instance of OatFile to back the DexFiles and preverify them using the
108*795d594fSAndroid Build Coastguard Worker   // vdex's VerifierDeps.
109*795d594fSAndroid Build Coastguard Worker   //
110*795d594fSAndroid Build Coastguard Worker   // Returns an empty vector if the dex files could not be loaded. In this
111*795d594fSAndroid Build Coastguard Worker   // case, there will be at least one error message returned describing why no
112*795d594fSAndroid Build Coastguard Worker   // dex files could not be loaded. The 'error_msgs' argument must not be
113*795d594fSAndroid Build Coastguard Worker   // null, regardless of whether there is an error or not.
114*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
115*795d594fSAndroid Build Coastguard Worker       std::vector<MemMap>&& dex_mem_maps,
116*795d594fSAndroid Build Coastguard Worker       jobject class_loader,
117*795d594fSAndroid Build Coastguard Worker       jobjectArray dex_elements,
118*795d594fSAndroid Build Coastguard Worker       /*out*/ const OatFile** out_oat_file,
119*795d594fSAndroid Build Coastguard Worker       /*out*/ std::vector<std::string>* error_msgs)
120*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker   void DumpForSigQuit(std::ostream& os);
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker   void SetOnlyUseTrustedOatFiles();
125*795d594fSAndroid Build Coastguard Worker   void ClearOnlyUseTrustedOatFiles();
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker   // Spawn a background thread which verifies all classes in the given dex files.
128*795d594fSAndroid Build Coastguard Worker   void RunBackgroundVerification(const std::vector<const DexFile*>& dex_files,
129*795d594fSAndroid Build Coastguard Worker                                  jobject class_loader);
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker   // Wait for thread pool workers to be created. This is used during shutdown as
132*795d594fSAndroid Build Coastguard Worker   // threads are not allowed to attach while runtime is in shutdown lock.
133*795d594fSAndroid Build Coastguard Worker   void WaitForWorkersToBeCreated();
134*795d594fSAndroid Build Coastguard Worker 
135*795d594fSAndroid Build Coastguard Worker   // If allocated, delete a thread pool of background verification threads.
136*795d594fSAndroid Build Coastguard Worker   void DeleteThreadPool();
137*795d594fSAndroid Build Coastguard Worker 
138*795d594fSAndroid Build Coastguard Worker   // Wait for any ongoing background verification tasks to finish.
139*795d594fSAndroid Build Coastguard Worker   EXPORT void WaitForBackgroundVerificationTasksToFinish();
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker   // Wait for all background verification tasks to finish. This is only used by tests.
142*795d594fSAndroid Build Coastguard Worker   EXPORT void WaitForBackgroundVerificationTasks();
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker   // Maximum number of anonymous vdex files kept in the process' data folder.
145*795d594fSAndroid Build Coastguard Worker   static constexpr size_t kAnonymousVdexCacheSize = 8u;
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker   bool ContainsPc(const void* pc) REQUIRES(!Locks::oat_file_manager_lock_);
148*795d594fSAndroid Build Coastguard Worker 
149*795d594fSAndroid Build Coastguard Worker  private:
150*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat_Impl(
151*795d594fSAndroid Build Coastguard Worker       std::vector<MemMap>&& dex_mem_maps,
152*795d594fSAndroid Build Coastguard Worker       jobject class_loader,
153*795d594fSAndroid Build Coastguard Worker       jobjectArray dex_elements,
154*795d594fSAndroid Build Coastguard Worker       /*out*/ const OatFile** out_oat_file,
155*795d594fSAndroid Build Coastguard Worker       /*out*/ std::vector<std::string>* error_msgs)
156*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
157*795d594fSAndroid Build Coastguard Worker 
158*795d594fSAndroid Build Coastguard Worker   const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
159*795d594fSAndroid Build Coastguard Worker       REQUIRES(Locks::oat_file_manager_lock_);
160*795d594fSAndroid Build Coastguard Worker 
161*795d594fSAndroid Build Coastguard Worker   // Return true if we should attempt to load the app image.
162*795d594fSAndroid Build Coastguard Worker   bool ShouldLoadAppImage() const;
163*795d594fSAndroid Build Coastguard Worker 
164*795d594fSAndroid Build Coastguard Worker   std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
165*795d594fSAndroid Build Coastguard Worker 
166*795d594fSAndroid Build Coastguard Worker   // Only use the compiled code in an OAT file when the file is on /system. If the OAT file
167*795d594fSAndroid Build Coastguard Worker   // is not on /system, don't load it "executable".
168*795d594fSAndroid Build Coastguard Worker   bool only_use_system_oat_files_;
169*795d594fSAndroid Build Coastguard Worker 
170*795d594fSAndroid Build Coastguard Worker   // Single-thread pool used to run the verifier in the background.
171*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<ThreadPool> verification_thread_pool_;
172*795d594fSAndroid Build Coastguard Worker 
173*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(OatFileManager);
174*795d594fSAndroid Build Coastguard Worker };
175*795d594fSAndroid Build Coastguard Worker 
176*795d594fSAndroid Build Coastguard Worker }  // namespace art
177*795d594fSAndroid Build Coastguard Worker 
178*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_OAT_OAT_FILE_MANAGER_H_
179