Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 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> |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 21 | #include <set> |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 22 | #include <string> |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 23 | #include <unordered_map> |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Nicolas Geoffray | 0744d72 | 2021-04-19 08:46:32 +0100 | [diff] [blame] | 26 | #include "base/compiler_filter.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 27 | #include "base/locks.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 28 | #include "base/macros.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 29 | #include "jni.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | namespace gc { |
| 34 | namespace space { |
| 35 | class ImageSpace; |
| 36 | } // namespace space |
| 37 | } // namespace gc |
| 38 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 39 | class ClassLoaderContext; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 40 | class DexFile; |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 41 | class MemMap; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 42 | class OatFile; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 43 | class ThreadPool; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 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: |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 51 | OatFileManager(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 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. |
Nicolas Geoffray | ea55f3d | 2021-09-07 12:16:56 +0100 | [diff] [blame] | 56 | // The `in_memory` parameter is whether the oat file is not present on disk, |
| 57 | // but only in memory (for example files created with memfd). |
| 58 | const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file, bool in_memory = false) |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 59 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 60 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 61 | void UnRegisterAndDeleteOatFile(const OatFile* oat_file) |
| 62 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 63 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 64 | // Find the first opened oat file with the same location, returns null if there are none. |
| 65 | const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const |
| 66 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 67 | |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 68 | // Find the oat file which contains a dex files with the given dex base location, |
| 69 | // returns null if there are none. |
| 70 | const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const |
| 71 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 72 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 73 | // Returns the boot image oat files. |
| 74 | std::vector<const OatFile*> GetBootOatFiles() const; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 75 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 76 | // Returns the oat files for the images, registers the oat files. |
| 77 | // Takes ownership of the imagespace's underlying oat files. |
Stephen Hines | 48ba197 | 2018-09-24 13:35:54 -0700 | [diff] [blame] | 78 | std::vector<const OatFile*> RegisterImageOatFiles( |
| 79 | const std::vector<gc::space::ImageSpace*>& spaces) |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 80 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 81 | |
| 82 | // Finds or creates the oat file holding dex_location. Then loads and returns |
| 83 | // all corresponding dex files (there may be more than one dex file loaded |
| 84 | // in the case of multidex). |
| 85 | // This may return the original, unquickened dex files if the oat file could |
| 86 | // not be generated. |
| 87 | // |
| 88 | // Returns an empty vector if the dex files could not be loaded. In this |
| 89 | // case, there will be at least one error message returned describing why no |
| 90 | // dex files could not be loaded. The 'error_msgs' argument must not be |
| 91 | // null, regardless of whether there is an error or not. |
| 92 | // |
| 93 | // This method should not be called with the mutator_lock_ held, because it |
| 94 | // could end up starving GC if we need to generate or relocate any oat |
| 95 | // files. |
| 96 | std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat( |
| 97 | const char* dex_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 98 | jobject class_loader, |
| 99 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 100 | /*out*/ const OatFile** out_oat_file, |
| 101 | /*out*/ std::vector<std::string>* error_msgs) |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 102 | REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_); |
| 103 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 104 | // Opens dex files provided in `dex_mem_maps` and attempts to find an anonymous |
| 105 | // vdex file created during a previous load attempt. If found, will initialize |
| 106 | // an instance of OatFile to back the DexFiles and preverify them using the |
| 107 | // vdex's VerifierDeps. |
| 108 | // |
| 109 | // Returns an empty vector if the dex files could not be loaded. In this |
| 110 | // case, there will be at least one error message returned describing why no |
| 111 | // dex files could not be loaded. The 'error_msgs' argument must not be |
| 112 | // null, regardless of whether there is an error or not. |
| 113 | std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat( |
| 114 | std::vector<MemMap>&& dex_mem_maps, |
| 115 | jobject class_loader, |
| 116 | jobjectArray dex_elements, |
| 117 | /*out*/ const OatFile** out_oat_file, |
| 118 | /*out*/ std::vector<std::string>* error_msgs) |
| 119 | REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_); |
| 120 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 121 | void DumpForSigQuit(std::ostream& os); |
| 122 | |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 123 | void SetOnlyUseTrustedOatFiles(); |
Nicolas Geoffray | ea55f3d | 2021-09-07 12:16:56 +0100 | [diff] [blame] | 124 | void ClearOnlyUseTrustedOatFiles(); |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 125 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 126 | // Spawn a background thread which verifies all classes in the given dex files. |
| 127 | void RunBackgroundVerification(const std::vector<const DexFile*>& dex_files, |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 128 | jobject class_loader); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 129 | |
| 130 | // Wait for thread pool workers to be created. This is used during shutdown as |
| 131 | // threads are not allowed to attach while runtime is in shutdown lock. |
| 132 | void WaitForWorkersToBeCreated(); |
| 133 | |
| 134 | // If allocated, delete a thread pool of background verification threads. |
| 135 | void DeleteThreadPool(); |
| 136 | |
| 137 | // Wait for all background verification tasks to finish. This is only used by tests. |
| 138 | void WaitForBackgroundVerificationTasks(); |
| 139 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 140 | // Maximum number of anonymous vdex files kept in the process' data folder. |
| 141 | static constexpr size_t kAnonymousVdexCacheSize = 8u; |
| 142 | |
Nicolas Geoffray | c25a9f9 | 2021-12-13 17:22:43 +0000 | [diff] [blame] | 143 | bool ContainsPc(const void* pc) REQUIRES(!Locks::oat_file_manager_lock_); |
| 144 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 145 | private: |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 146 | 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 Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 154 | const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const |
| 155 | REQUIRES(Locks::oat_file_manager_lock_); |
| 156 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 157 | // Return true if we should attempt to load the app image. |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 158 | bool ShouldLoadAppImage(const OatFile* source_oat_file) const; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 159 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 160 | std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_); |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 161 | |
| 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 Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 164 | bool only_use_system_oat_files_; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 165 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 166 | // Single-thread pool used to run the verifier in the background. |
| 167 | std::unique_ptr<ThreadPool> verification_thread_pool_; |
| 168 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 169 | DISALLOW_COPY_AND_ASSIGN(OatFileManager); |
| 170 | }; |
| 171 | |
| 172 | } // namespace art |
| 173 | |
| 174 | #endif // ART_RUNTIME_OAT_FILE_MANAGER_H_ |