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_OAT_FILE_MANAGER_H_ 18 #define ART_RUNTIME_OAT_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 HIDDEN { 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 // 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 EXPORT const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file, 59 bool in_memory = false) 60 REQUIRES(!Locks::oat_file_manager_lock_); 61 62 void UnRegisterAndDeleteOatFile(const OatFile* oat_file) 63 REQUIRES(!Locks::oat_file_manager_lock_); 64 65 // Find the first opened oat file with the same location, returns null if there are none. 66 EXPORT const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const 67 REQUIRES(!Locks::oat_file_manager_lock_); 68 69 // Find the oat file which contains a dex files with the given dex base location, 70 // returns null if there are none. 71 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const 72 REQUIRES(!Locks::oat_file_manager_lock_); 73 74 // Returns the boot image oat files. 75 EXPORT std::vector<const OatFile*> GetBootOatFiles() const; 76 77 // Returns the oat files for the images, registers the oat files. 78 // Takes ownership of the imagespace's underlying oat files. 79 std::vector<const OatFile*> RegisterImageOatFiles( 80 const 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 // Opens dex files provided in `dex_mem_maps` and attempts to find an anonymous 106 // vdex file created during a previous load attempt. If found, will initialize 107 // an instance of OatFile to back the DexFiles and preverify them using the 108 // vdex's VerifierDeps. 109 // 110 // Returns an empty vector if the dex files could not be loaded. In this 111 // case, there will be at least one error message returned describing why no 112 // dex files could not be loaded. The 'error_msgs' argument must not be 113 // null, regardless of whether there is an error or not. 114 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat( 115 std::vector<MemMap>&& dex_mem_maps, 116 jobject class_loader, 117 jobjectArray dex_elements, 118 /*out*/ const OatFile** out_oat_file, 119 /*out*/ std::vector<std::string>* error_msgs) 120 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_); 121 122 void DumpForSigQuit(std::ostream& os); 123 124 void SetOnlyUseTrustedOatFiles(); 125 void ClearOnlyUseTrustedOatFiles(); 126 127 // Spawn a background thread which verifies all classes in the given dex files. 128 void RunBackgroundVerification(const std::vector<const DexFile*>& dex_files, 129 jobject class_loader); 130 131 // Wait for thread pool workers to be created. This is used during shutdown as 132 // threads are not allowed to attach while runtime is in shutdown lock. 133 void WaitForWorkersToBeCreated(); 134 135 // If allocated, delete a thread pool of background verification threads. 136 void DeleteThreadPool(); 137 138 // Wait for any ongoing background verification tasks to finish. 139 EXPORT void WaitForBackgroundVerificationTasksToFinish(); 140 141 // Wait for all background verification tasks to finish. This is only used by tests. 142 EXPORT void WaitForBackgroundVerificationTasks(); 143 144 // Maximum number of anonymous vdex files kept in the process' data folder. 145 static constexpr size_t kAnonymousVdexCacheSize = 8u; 146 147 bool ContainsPc(const void* pc) REQUIRES(!Locks::oat_file_manager_lock_); 148 149 private: 150 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat_Impl( 151 std::vector<MemMap>&& dex_mem_maps, 152 jobject class_loader, 153 jobjectArray dex_elements, 154 /*out*/ const OatFile** out_oat_file, 155 /*out*/ std::vector<std::string>* error_msgs) 156 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_); 157 158 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const 159 REQUIRES(Locks::oat_file_manager_lock_); 160 161 // Return true if we should attempt to load the app image. 162 bool ShouldLoadAppImage() const; 163 164 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_); 165 166 // Only use the compiled code in an OAT file when the file is on /system. If the OAT file 167 // is not on /system, don't load it "executable". 168 bool only_use_system_oat_files_; 169 170 // Single-thread pool used to run the verifier in the background. 171 std::unique_ptr<ThreadPool> verification_thread_pool_; 172 173 DISALLOW_COPY_AND_ASSIGN(OatFileManager); 174 }; 175 176 } // namespace art 177 178 #endif // ART_RUNTIME_OAT_OAT_FILE_MANAGER_H_ 179