Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_ASSISTANT_H_ |
| 18 | #define ART_RUNTIME_OAT_FILE_ASSISTANT_H_ |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <memory> |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 22 | #include <sstream> |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 25 | #include "base/compiler_filter.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 26 | #include "arch/instruction_set.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 27 | #include "base/os.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 28 | #include "base/scoped_flock.h" |
| 29 | #include "base/unix_file/fd_file.h" |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 30 | #include "class_loader_context.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 31 | #include "oat_file.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 35 | namespace gc { |
| 36 | namespace space { |
| 37 | class ImageSpace; |
| 38 | } // namespace space |
| 39 | } // namespace gc |
| 40 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 41 | // Class for assisting with oat file management. |
| 42 | // |
| 43 | // This class collects common utilities for determining the status of an oat |
| 44 | // file on the device, updating the oat file, and loading the oat file. |
| 45 | // |
| 46 | // The oat file assistant is intended to be used with dex locations not on the |
| 47 | // boot class path. See the IsInBootClassPath method for a way to check if the |
| 48 | // dex location is in the boot class path. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 49 | class OatFileAssistant { |
| 50 | public: |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 51 | enum DexOptNeeded { |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 52 | // No dexopt should (or can) be done to update the apk/jar. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 53 | // Matches Java: dalvik.system.DexFile.NO_DEXOPT_NEEDED = 0 |
| 54 | kNoDexOptNeeded = 0, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 55 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 56 | // dex2oat should be run to update the apk/jar from scratch. |
| 57 | // Matches Java: dalvik.system.DexFile.DEX2OAT_FROM_SCRATCH = 1 |
| 58 | kDex2OatFromScratch = 1, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 59 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 60 | // dex2oat should be run to update the apk/jar because the existing code |
| 61 | // is out of date with respect to the boot image. |
| 62 | // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_BOOT_IMAGE |
| 63 | kDex2OatForBootImage = 2, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 64 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 65 | // dex2oat should be run to update the apk/jar because the existing code |
| 66 | // is out of date with respect to the target compiler filter. |
| 67 | // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_FILTER |
| 68 | kDex2OatForFilter = 3, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | enum OatStatus { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 72 | // kOatCannotOpen - The oat file cannot be opened, because it does not |
| 73 | // exist, is unreadable, or otherwise corrupted. |
| 74 | kOatCannotOpen, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 75 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 76 | // kOatDexOutOfDate - The oat file is out of date with respect to the dex file. |
| 77 | kOatDexOutOfDate, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 78 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 79 | // kOatBootImageOutOfDate - The oat file is up to date with respect to the |
| 80 | // dex file, but is out of date with respect to the boot image. |
| 81 | kOatBootImageOutOfDate, |
| 82 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 83 | // kOatUpToDate - The oat file is completely up to date with respect to |
| 84 | // the dex file and boot image. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 85 | kOatUpToDate, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | // Constructs an OatFileAssistant object to assist the oat file |
| 89 | // corresponding to the given dex location with the target instruction set. |
| 90 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 91 | // The dex_location must not be null and should remain available and |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 92 | // unchanged for the duration of the lifetime of the OatFileAssistant object. |
| 93 | // Typically the dex_location is the absolute path to the original, |
| 94 | // un-optimized dex file. |
| 95 | // |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 96 | // Note: Currently the dex_location must have an extension. |
| 97 | // TODO: Relax this restriction? |
| 98 | // |
| 99 | // The isa should be either the 32 bit or 64 bit variant for the current |
| 100 | // device. For example, on an arm device, use arm or arm64. An oat file can |
| 101 | // be loaded executable only if the ISA matches the current runtime. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 102 | // |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 103 | // load_executable should be true if the caller intends to try and load |
| 104 | // executable code for this dex location. |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 105 | // |
| 106 | // only_load_system_executable should be true if the caller intends to have |
| 107 | // only oat files from /system loaded executable. |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 108 | OatFileAssistant(const char* dex_location, |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 109 | const InstructionSet isa, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 110 | bool load_executable, |
| 111 | bool only_load_system_executable = false); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 112 | |
| 113 | // Similar to this(const char*, const InstructionSet, bool), however, if a valid zip_fd is |
| 114 | // provided, vdex, oat, and zip files will be read from vdex_fd, oat_fd and zip_fd respectively. |
| 115 | // Otherwise, dex_location will be used to construct necessary filenames. |
| 116 | OatFileAssistant(const char* dex_location, |
| 117 | const InstructionSet isa, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 118 | bool load_executable, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 119 | bool only_load_system_executable, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 120 | int vdex_fd, |
| 121 | int oat_fd, |
| 122 | int zip_fd); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 123 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 124 | // Returns true if the dex location refers to an element of the boot class |
| 125 | // path. |
| 126 | bool IsInBootClassPath(); |
| 127 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 128 | // Return what action needs to be taken to produce up-to-date code for this |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 129 | // dex location. If "downgrade" is set to false, it verifies if the current |
| 130 | // compiler filter is at least as good as an oat file generated with the |
| 131 | // given compiler filter otherwise, if its set to true, it checks whether |
| 132 | // the oat file generated with the target filter will be downgraded as |
| 133 | // compared to the current state. For example, if the current compiler filter is |
| 134 | // quicken, and target filter is verify, it will recommend to dexopt, while |
| 135 | // if the target filter is speed profile, it will recommend to keep it in its |
| 136 | // current state. |
| 137 | // profile_changed should be true to indicate the profile has recently changed |
| 138 | // for this dex location. |
| 139 | // If the purpose of the dexopt is to downgrade the compiler filter, |
| 140 | // set downgrade to true. |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 141 | // Returns a positive status code if the status refers to the oat file in |
| 142 | // the oat location. Returns a negative status code if the status refers to |
| 143 | // the oat file in the odex location. |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 144 | int GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter, |
Calin Juravle | 0a5cad3 | 2020-02-14 20:29:26 +0000 | [diff] [blame] | 145 | ClassLoaderContext* context, |
| 146 | const std::vector<int>& context_fds, |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 147 | bool profile_changed = false, |
Calin Juravle | 0a5cad3 | 2020-02-14 20:29:26 +0000 | [diff] [blame] | 148 | bool downgrade = false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 149 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 150 | // Returns true if there is up-to-date code for this dex location, |
| 151 | // irrespective of the compiler filter of the up-to-date code. |
| 152 | bool IsUpToDate(); |
| 153 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 154 | // Returns an oat file that can be used for loading dex files. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 155 | // Returns null if no suitable oat file was found. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 156 | // |
| 157 | // After this call, no other methods of the OatFileAssistant should be |
| 158 | // called, because access to the loaded oat file has been taken away from |
| 159 | // the OatFileAssistant object. |
| 160 | std::unique_ptr<OatFile> GetBestOatFile(); |
| 161 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 162 | // Returns a human readable description of the status of the code for the |
| 163 | // dex file. The returned description is for debugging purposes only. |
| 164 | std::string GetStatusDump(); |
| 165 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 166 | // Computes the optimization status of the given dex file. The result is |
| 167 | // returned via the two output parameters. |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 168 | // - out_odex_location: the location of the (best) odex that will be used |
| 169 | // for loading. See GetBestInfo(). |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 170 | // - out_compilation_filter: the level of optimizations (compiler filter) |
| 171 | // - out_compilation_reason: the optimization reason. The reason might |
| 172 | // be "unknown" if the compiler artifacts were not annotated during optimizations. |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 173 | // - out_odex_status: a human readable refined status of the validity of the odex file. |
| 174 | // E.g. up-to-date, boot-image-more-recent, apk-more-recent. |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 175 | // |
| 176 | // This method will try to mimic the runtime effect of loading the dex file. |
| 177 | // For example, if there is no usable oat file, the compiler filter will be set |
| 178 | // to "run-from-apk". |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 179 | void GetOptimizationStatus(std::string* out_odex_location, |
| 180 | std::string* out_compilation_filter, |
| 181 | std::string* out_compilation_reason, |
| 182 | std::string* out_odex_status); |
| 183 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 184 | static void GetOptimizationStatus(const std::string& filename, |
| 185 | InstructionSet isa, |
| 186 | std::string* out_compilation_filter, |
| 187 | std::string* out_compilation_reason); |
| 188 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 189 | // Open and returns an image space associated with the oat file. |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 190 | static std::unique_ptr<gc::space::ImageSpace> OpenImageSpace(const OatFile* oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 191 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 192 | // Loads the dex files in the given oat file for the given dex location. |
| 193 | // The oat file should be up to date for the given dex location. |
| 194 | // This loads multiple dex files in the case of multidex. |
| 195 | // Returns an empty vector if no dex files for that location could be loaded |
| 196 | // from the oat file. |
| 197 | // |
| 198 | // The caller is responsible for freeing the dex_files returned, if any. The |
| 199 | // dex_files will only remain valid as long as the oat_file is valid. |
| 200 | static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles( |
| 201 | const OatFile& oat_file, const char* dex_location); |
| 202 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 203 | // Same as `std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(...)` with the difference: |
| 204 | // - puts the dex files in the given vector |
| 205 | // - returns whether or not all dex files were successfully opened |
| 206 | static bool LoadDexFiles(const OatFile& oat_file, |
| 207 | const std::string& dex_location, |
| 208 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files); |
| 209 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 210 | // Returns whether this is an apk/zip wit a classes.dex entry. |
| 211 | bool HasDexFiles(); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 212 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 213 | // If the dex file has been installed with a compiled oat file alongside |
| 214 | // it, the compiled oat file will have the extension .odex, and is referred |
| 215 | // to as the odex file. It is called odex for legacy reasons; the file is |
| 216 | // really an oat file. The odex file will often, but not always, have a |
| 217 | // patch delta of 0 and need to be relocated before use for the purposes of |
| 218 | // ASLR. The odex file is treated as if it were read-only. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 219 | // |
| 220 | // Returns the status of the odex file for the dex location. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 221 | OatStatus OdexFileStatus(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 222 | |
| 223 | // When the dex files is compiled on the target device, the oat file is the |
| 224 | // result. The oat file will have been relocated to some |
| 225 | // (possibly-out-of-date) offset for ASLR. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 226 | // |
| 227 | // Returns the status of the oat file for the dex location. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 228 | OatStatus OatFileStatus(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 229 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 230 | // Constructs the odex file name for the given dex location. |
| 231 | // Returns true on success, in which case odex_filename is set to the odex |
| 232 | // file name. |
Richard Uhler | e8e48ae | 2016-04-19 12:41:04 -0700 | [diff] [blame] | 233 | // Returns false on error, in which case error_msg describes the error and |
| 234 | // odex_filename is not changed. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 235 | // Neither odex_filename nor error_msg may be null. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 236 | static bool DexLocationToOdexFilename(const std::string& location, |
| 237 | InstructionSet isa, |
| 238 | std::string* odex_filename, |
| 239 | std::string* error_msg); |
| 240 | |
| 241 | // Constructs the oat file name for the given dex location. |
| 242 | // Returns true on success, in which case oat_filename is set to the oat |
| 243 | // file name. |
| 244 | // Returns false on error, in which case error_msg describes the error and |
| 245 | // oat_filename is not changed. |
| 246 | // Neither oat_filename nor error_msg may be null. |
| 247 | static bool DexLocationToOatFilename(const std::string& location, |
| 248 | InstructionSet isa, |
| 249 | std::string* oat_filename, |
| 250 | std::string* error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 251 | |
Nicolas Geoffray | 8856695 | 2021-02-16 15:51:18 +0000 | [diff] [blame] | 252 | // Computes the dex location and vdex filename. If the data directory of the process |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 253 | // is known, creates an absolute path in that directory and tries to infer path |
| 254 | // of a corresponding vdex file. Otherwise only creates a basename dex_location |
| 255 | // from the combined checksums. Returns true if all out-arguments have been set. |
| 256 | static bool AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& dex_headers, |
| 257 | InstructionSet isa, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 258 | /* out */ std::string* dex_location, |
| 259 | /* out */ std::string* vdex_filename); |
| 260 | |
| 261 | // Returns true if a filename (given as basename) is a name of a vdex for |
| 262 | // anonymous dex file(s) created by AnonymousDexVdexLocation. |
| 263 | static bool IsAnonymousVdexBasename(const std::string& basename); |
| 264 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 265 | private: |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 266 | class OatFileInfo { |
| 267 | public: |
| 268 | // Initially the info is for no file in particular. It will treat the |
| 269 | // file as out of date until Reset is called with a real filename to use |
| 270 | // the cache for. |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 271 | // Pass true for is_oat_location if the information associated with this |
| 272 | // OatFileInfo is for the oat location, as opposed to the odex location. |
| 273 | OatFileInfo(OatFileAssistant* oat_file_assistant, bool is_oat_location); |
| 274 | |
| 275 | bool IsOatLocation(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 276 | |
| 277 | const std::string* Filename(); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 278 | |
| 279 | // Returns true if this oat file can be used for running code. The oat |
| 280 | // file can be used for running code as long as it is not out of date with |
| 281 | // respect to the dex code or boot image. An oat file that is out of date |
| 282 | // with respect to relocation is considered useable, because it's possible |
| 283 | // to interpret the dex code rather than run the unrelocated compiled |
| 284 | // code. |
| 285 | bool IsUseable(); |
| 286 | |
| 287 | // Returns the status of this oat file. |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 288 | OatStatus Status(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 289 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 290 | // Return the DexOptNeeded value for this oat file with respect to the |
| 291 | // given target_compilation_filter. |
| 292 | // profile_changed should be true to indicate the profile has recently |
| 293 | // changed for this dex location. |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 294 | // downgrade should be true if the purpose of dexopt is to downgrade the |
| 295 | // compiler filter. |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 296 | DexOptNeeded GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter, |
Stefania Halac | e9818dd | 2020-02-14 17:58:26 +0000 | [diff] [blame] | 297 | ClassLoaderContext* context, |
Calin Juravle | 0a5cad3 | 2020-02-14 20:29:26 +0000 | [diff] [blame] | 298 | const std::vector<int>& context_fds, |
| 299 | bool profile_changed, |
| 300 | bool downgrade); |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 301 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 302 | // Returns the loaded file. |
| 303 | // Loads the file if needed. Returns null if the file failed to load. |
| 304 | // The caller shouldn't clean up or free the returned pointer. |
| 305 | const OatFile* GetFile(); |
| 306 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 307 | // Returns true if the file is opened executable. |
| 308 | bool IsExecutable(); |
| 309 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 310 | // Clear any cached information about the file that depends on the |
| 311 | // contents of the file. This does not reset the provided filename. |
| 312 | void Reset(); |
| 313 | |
| 314 | // Clear any cached information and switch to getting info about the oat |
| 315 | // file with the given filename. |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 316 | void Reset(const std::string& filename, |
| 317 | bool use_fd, |
| 318 | int zip_fd = -1, |
| 319 | int vdex_fd = -1, |
| 320 | int oat_fd = -1); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 321 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 322 | // Release the loaded oat file for runtime use. |
| 323 | // Returns null if the oat file hasn't been loaded or is out of date. |
| 324 | // Ensures the returned file is not loaded executable if it has unuseable |
| 325 | // compiled code. |
| 326 | // |
| 327 | // After this call, no other methods of the OatFileInfo should be |
| 328 | // called, because access to the loaded oat file has been taken away from |
| 329 | // the OatFileInfo object. |
| 330 | std::unique_ptr<OatFile> ReleaseFileForUse(); |
| 331 | |
| 332 | private: |
| 333 | // Returns true if the compiler filter used to generate the file is at |
| 334 | // least as good as the given target filter. profile_changed should be |
| 335 | // true to indicate the profile has recently changed for this dex |
| 336 | // location. |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 337 | // downgrade should be true if the purpose of dexopt is to downgrade the |
| 338 | // compiler filter. |
| 339 | bool CompilerFilterIsOkay(CompilerFilter::Filter target, bool profile_changed, bool downgrade); |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 340 | |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 341 | bool ClassLoaderContextIsOkay(ClassLoaderContext* context, const std::vector<int>& context_fds); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 342 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 343 | // Release the loaded oat file. |
| 344 | // Returns null if the oat file hasn't been loaded. |
| 345 | // |
| 346 | // After this call, no other methods of the OatFileInfo should be |
| 347 | // called, because access to the loaded oat file has been taken away from |
| 348 | // the OatFileInfo object. |
| 349 | std::unique_ptr<OatFile> ReleaseFile(); |
| 350 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 351 | OatFileAssistant* oat_file_assistant_; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 352 | const bool is_oat_location_; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 353 | |
| 354 | bool filename_provided_ = false; |
| 355 | std::string filename_; |
| 356 | |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 357 | int zip_fd_ = -1; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 358 | int oat_fd_ = -1; |
| 359 | int vdex_fd_ = -1; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 360 | bool use_fd_ = false; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 361 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 362 | bool load_attempted_ = false; |
| 363 | std::unique_ptr<OatFile> file_; |
| 364 | |
| 365 | bool status_attempted_ = false; |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 366 | OatStatus status_ = OatStatus::kOatCannotOpen; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 367 | |
| 368 | // For debugging only. |
| 369 | // If this flag is set, the file has been released to the user and the |
| 370 | // OatFileInfo object is in a bad state and should no longer be used. |
| 371 | bool file_released_ = false; |
| 372 | }; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 373 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 374 | // Return info for the best oat file. |
| 375 | OatFileInfo& GetBestInfo(); |
| 376 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 377 | // Returns true when vdex/oat/odex files should be read from file descriptors. |
| 378 | // The method checks the value of zip_fd_, and if the value is valid, returns |
| 379 | // true. This is required to have a deterministic behavior around how different |
| 380 | // files are being read. |
| 381 | bool UseFdToReadFiles(); |
| 382 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 383 | // Returns true if the dex checksums in the given vdex file are up to date |
| 384 | // with respect to the dex location. If the dex checksums are not up to |
| 385 | // date, error_msg is updated with a message describing the problem. |
| 386 | bool DexChecksumUpToDate(const VdexFile& file, std::string* error_msg); |
| 387 | |
| 388 | // Returns true if the dex checksums in the given oat file are up to date |
| 389 | // with respect to the dex location. If the dex checksums are not up to |
| 390 | // date, error_msg is updated with a message describing the problem. |
| 391 | bool DexChecksumUpToDate(const OatFile& file, std::string* error_msg); |
| 392 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 393 | // Return the status for a given opened oat file with respect to the dex |
| 394 | // location. |
| 395 | OatStatus GivenOatFileStatus(const OatFile& file); |
| 396 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 397 | // Gets the dex checksums required for an up-to-date oat file. |
| 398 | // Returns cached_required_dex_checksums if the required checksums were |
| 399 | // located. Returns null if the required checksums were not found. The |
| 400 | // caller shouldn't clean up or free the returned pointer. This sets the |
| 401 | // has_original_dex_files_ field to true if the checksums were found for the |
| 402 | // dex_location_ dex file. |
| 403 | const std::vector<uint32_t>* GetRequiredDexChecksums(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 404 | |
Vladimir Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 405 | // Validates the boot class path checksum of an OatFile. |
| 406 | bool ValidateBootClassPathChecksums(const OatFile& oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 407 | |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 408 | std::string dex_location_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 409 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 410 | // Whether or not the parent directory of the dex file is writable. |
| 411 | bool dex_parent_writable_ = false; |
| 412 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 413 | // In a properly constructed OatFileAssistant object, isa_ should be either |
| 414 | // the 32 or 64 bit variant for the current device. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 415 | const InstructionSet isa_ = InstructionSet::kNone; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 416 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 417 | // Whether we will attempt to load oat files executable. |
| 418 | bool load_executable_ = false; |
| 419 | |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 420 | // Whether only oat files on /system are loaded executable. |
| 421 | const bool only_load_system_executable_ = false; |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 422 | // Whether the potential zip file only contains uncompressed dex. |
| 423 | // Will be set during GetRequiredDexChecksums. |
| 424 | bool zip_file_only_contains_uncompressed_dex_ = true; |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 425 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 426 | // Cached value of the required dex checksums. |
| 427 | // This should be accessed only by the GetRequiredDexChecksums() method. |
| 428 | std::vector<uint32_t> cached_required_dex_checksums_; |
| 429 | bool required_dex_checksums_attempted_ = false; |
| 430 | bool required_dex_checksums_found_; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 431 | bool has_original_dex_files_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 432 | |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 433 | // The AOT-compiled file of an app when the APK of the app is in /data. |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 434 | OatFileInfo odex_; |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 435 | // The AOT-compiled file of an app when the APK of the app is on a read-only partition |
| 436 | // (for example /system). |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 437 | OatFileInfo oat_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 438 | |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 439 | // The vdex-only file next to `odex_` when `odex_' cannot be used (for example |
| 440 | // it is out of date). |
| 441 | OatFileInfo vdex_for_odex_; |
| 442 | // The vdex-only file next to 'oat_` when `oat_' cannot be used (for example |
| 443 | // it is out of date). |
| 444 | OatFileInfo vdex_for_oat_; |
| 445 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 446 | // File descriptor corresponding to apk, dex file, or zip. |
| 447 | int zip_fd_; |
| 448 | |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 449 | std::string cached_boot_class_path_; |
Vladimir Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 450 | std::string cached_boot_class_path_checksums_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 451 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 452 | friend class OatFileAssistantTest; |
| 453 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 454 | DISALLOW_COPY_AND_ASSIGN(OatFileAssistant); |
| 455 | }; |
| 456 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 457 | std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status); |
| 458 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 459 | } // namespace art |
| 460 | |
| 461 | #endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_ |