blob: f8a734148a40aa91db3c56cbcef57bd6c5dc83eb [file] [log] [blame]
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001/*
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>
Mathieu Chartiere58991b2015-10-13 07:59:34 -070021#include <set>
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070022#include <string>
Mathieu Chartiere58991b2015-10-13 07:59:34 -070023#include <unordered_map>
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070024#include <vector>
25
Nicolas Geoffray0744d722021-04-19 08:46:32 +010026#include "base/compiler_filter.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080027#include "base/locks.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070028#include "base/macros.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080029#include "jni.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070030
31namespace art {
32
33namespace gc {
34namespace space {
35class ImageSpace;
36} // namespace space
37} // namespace gc
38
Calin Juravle27e0d1f2017-07-26 00:16:07 -070039class ClassLoaderContext;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070040class DexFile;
David Brazdil7126c5b2019-03-05 00:02:51 +000041class MemMap;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070042class OatFile;
David Brazdil331a5e12019-04-01 22:46:16 +000043class ThreadPool;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070044
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.
49class OatFileManager {
50 public:
Vladimir Markob0b68cf2017-11-14 18:11:50 +000051 OatFileManager();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070052 ~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
Mathieu Chartiere58991b2015-10-13 07:59:34 -070059 void UnRegisterAndDeleteOatFile(const OatFile* oat_file)
60 REQUIRES(!Locks::oat_file_manager_lock_);
61
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070062 // 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
Calin Juravle0b791272016-04-18 16:38:27 +010066 // 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
Jeff Haodcdc85b2015-12-04 14:06:18 -080071 // Returns the boot image oat files.
72 std::vector<const OatFile*> GetBootOatFiles() const;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070073
Nicolas Geoffray0744d722021-04-19 08:46:32 +010074 // Fetches information from the primary oat file.
75 bool GetPrimaryOatFileInfo(std::string* compilation_reason,
76 CompilerFilter::Filter* compiler_filter)
77 const REQUIRES(!Locks::oat_file_manager_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070078
Jeff Haodcdc85b2015-12-04 14:06:18 -080079 // Returns the oat files for the images, registers the oat files.
80 // Takes ownership of the imagespace's underlying oat files.
Stephen Hines48ba1972018-09-24 13:35:54 -070081 std::vector<const OatFile*> RegisterImageOatFiles(
82 const std::vector<gc::space::ImageSpace*>& spaces)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070083 REQUIRES(!Locks::oat_file_manager_lock_);
84
85 // Finds or creates the oat file holding dex_location. Then loads and returns
86 // all corresponding dex files (there may be more than one dex file loaded
87 // in the case of multidex).
88 // This may return the original, unquickened dex files if the oat file could
89 // not be generated.
90 //
91 // Returns an empty vector if the dex files could not be loaded. In this
92 // case, there will be at least one error message returned describing why no
93 // dex files could not be loaded. The 'error_msgs' argument must not be
94 // null, regardless of whether there is an error or not.
95 //
96 // This method should not be called with the mutator_lock_ held, because it
97 // could end up starving GC if we need to generate or relocate any oat
98 // files.
99 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
100 const char* dex_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800101 jobject class_loader,
102 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700103 /*out*/ const OatFile** out_oat_file,
104 /*out*/ std::vector<std::string>* error_msgs)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700105 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
106
David Brazdil7126c5b2019-03-05 00:02:51 +0000107 // Opens dex files provided in `dex_mem_maps` and attempts to find an anonymous
108 // vdex file created during a previous load attempt. If found, will initialize
109 // an instance of OatFile to back the DexFiles and preverify them using the
110 // vdex's VerifierDeps.
111 //
112 // Returns an empty vector if the dex files could not be loaded. In this
113 // case, there will be at least one error message returned describing why no
114 // dex files could not be loaded. The 'error_msgs' argument must not be
115 // null, regardless of whether there is an error or not.
116 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
117 std::vector<MemMap>&& dex_mem_maps,
118 jobject class_loader,
119 jobjectArray dex_elements,
120 /*out*/ const OatFile** out_oat_file,
121 /*out*/ std::vector<std::string>* error_msgs)
122 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
123
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000124 void DumpForSigQuit(std::ostream& os);
125
Orion Hodson094b1cf2021-06-08 09:28:28 +0100126 void SetOnlyUseTrustedOatFiles();
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100127
David Brazdil331a5e12019-04-01 22:46:16 +0000128 // Spawn a background thread which verifies all classes in the given dex files.
129 void RunBackgroundVerification(const std::vector<const DexFile*>& dex_files,
Nicolas Geoffray9bc364b2021-03-29 10:41:39 +0100130 jobject class_loader);
David Brazdil331a5e12019-04-01 22:46:16 +0000131
132 // Wait for thread pool workers to be created. This is used during shutdown as
133 // threads are not allowed to attach while runtime is in shutdown lock.
134 void WaitForWorkersToBeCreated();
135
136 // If allocated, delete a thread pool of background verification threads.
137 void DeleteThreadPool();
138
139 // Wait for all background verification tasks to finish. This is only used by tests.
140 void WaitForBackgroundVerificationTasks();
141
David Brazdil35a3f6a2019-03-04 15:59:06 +0000142 // Maximum number of anonymous vdex files kept in the process' data folder.
143 static constexpr size_t kAnonymousVdexCacheSize = 8u;
144
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700145 private:
David Brazdil7126c5b2019-03-05 00:02:51 +0000146 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat_Impl(
147 std::vector<MemMap>&& dex_mem_maps,
148 jobject class_loader,
149 jobjectArray dex_elements,
150 /*out*/ const OatFile** out_oat_file,
151 /*out*/ std::vector<std::string>* error_msgs)
152 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
153
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700154 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
155 REQUIRES(Locks::oat_file_manager_lock_);
156
Mathieu Chartieradc90862018-05-11 13:03:06 -0700157 // Return true if we should attempt to load the app image.
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100158 bool ShouldLoadAppImage(const OatFile* source_oat_file) const;
Mathieu Chartieradc90862018-05-11 13:03:06 -0700159
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700160 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
Nicolas Geoffray29742602017-12-14 10:09:03 +0000161
162 // Only use the compiled code in an OAT file when the file is on /system. If the OAT file
163 // is not on /system, don't load it "executable".
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100164 bool only_use_system_oat_files_;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000165
David Brazdil331a5e12019-04-01 22:46:16 +0000166 // Single-thread pool used to run the verifier in the background.
167 std::unique_ptr<ThreadPool> verification_thread_pool_;
168
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700169 DISALLOW_COPY_AND_ASSIGN(OatFileManager);
170};
171
172} // namespace art
173
174#endif // ART_RUNTIME_OAT_FILE_MANAGER_H_