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 | |
| 25 | #include "arch/instruction_set.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame^] | 26 | #include "base/os.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 27 | #include "base/scoped_flock.h" |
| 28 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 29 | #include "compiler_filter.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: |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 51 | // The default compile filter to use when optimizing dex file at load time if they |
| 52 | // are out of date. |
| 53 | static const CompilerFilter::Filter kDefaultCompilerFilterForDexLoading = |
| 54 | CompilerFilter::kQuicken; |
| 55 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 56 | enum DexOptNeeded { |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 57 | // No dexopt should (or can) be done to update the apk/jar. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 58 | // Matches Java: dalvik.system.DexFile.NO_DEXOPT_NEEDED = 0 |
| 59 | kNoDexOptNeeded = 0, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 60 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 61 | // dex2oat should be run to update the apk/jar from scratch. |
| 62 | // Matches Java: dalvik.system.DexFile.DEX2OAT_FROM_SCRATCH = 1 |
| 63 | kDex2OatFromScratch = 1, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [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 boot image. |
| 67 | // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_BOOT_IMAGE |
| 68 | kDex2OatForBootImage = 2, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 69 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 70 | // dex2oat should be run to update the apk/jar because the existing code |
| 71 | // is out of date with respect to the target compiler filter. |
| 72 | // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_FILTER |
| 73 | kDex2OatForFilter = 3, |
| 74 | |
| 75 | // dex2oat should be run to update the apk/jar because the existing code |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 76 | // is not relocated to match the boot image. |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 77 | // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_RELOCATION |
| 78 | kDex2OatForRelocation = 4, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | enum OatStatus { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 82 | // kOatCannotOpen - The oat file cannot be opened, because it does not |
| 83 | // exist, is unreadable, or otherwise corrupted. |
| 84 | kOatCannotOpen, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 85 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 86 | // kOatDexOutOfDate - The oat file is out of date with respect to the dex file. |
| 87 | kOatDexOutOfDate, |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 88 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 89 | // kOatBootImageOutOfDate - The oat file is up to date with respect to the |
| 90 | // dex file, but is out of date with respect to the boot image. |
| 91 | kOatBootImageOutOfDate, |
| 92 | |
| 93 | // kOatRelocationOutOfDate - The oat file is up to date with respect to |
| 94 | // the dex file and boot image, but contains compiled code that has the |
| 95 | // wrong patch delta with respect to the boot image. Patchoat should be |
| 96 | // run on the oat file to update the patch delta of the compiled code to |
| 97 | // match the boot image. |
| 98 | kOatRelocationOutOfDate, |
| 99 | |
| 100 | // kOatUpToDate - The oat file is completely up to date with respect to |
| 101 | // the dex file and boot image. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 102 | kOatUpToDate, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | // Constructs an OatFileAssistant object to assist the oat file |
| 106 | // corresponding to the given dex location with the target instruction set. |
| 107 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 108 | // The dex_location must not be null and should remain available and |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 109 | // unchanged for the duration of the lifetime of the OatFileAssistant object. |
| 110 | // Typically the dex_location is the absolute path to the original, |
| 111 | // un-optimized dex file. |
| 112 | // |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 113 | // Note: Currently the dex_location must have an extension. |
| 114 | // TODO: Relax this restriction? |
| 115 | // |
| 116 | // The isa should be either the 32 bit or 64 bit variant for the current |
| 117 | // device. For example, on an arm device, use arm or arm64. An oat file can |
| 118 | // be loaded executable only if the ISA matches the current runtime. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 119 | // |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 120 | // load_executable should be true if the caller intends to try and load |
| 121 | // executable code for this dex location. |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 122 | // |
| 123 | // only_load_system_executable should be true if the caller intends to have |
| 124 | // only oat files from /system loaded executable. |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 125 | OatFileAssistant(const char* dex_location, |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 126 | const InstructionSet isa, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 127 | bool load_executable, |
| 128 | bool only_load_system_executable = false); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 129 | |
| 130 | // Similar to this(const char*, const InstructionSet, bool), however, if a valid zip_fd is |
| 131 | // provided, vdex, oat, and zip files will be read from vdex_fd, oat_fd and zip_fd respectively. |
| 132 | // Otherwise, dex_location will be used to construct necessary filenames. |
| 133 | OatFileAssistant(const char* dex_location, |
| 134 | const InstructionSet isa, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 135 | bool load_executable, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 136 | bool only_load_system_executable, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 137 | int vdex_fd, |
| 138 | int oat_fd, |
| 139 | int zip_fd); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 140 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 141 | ~OatFileAssistant(); |
| 142 | |
| 143 | // Returns true if the dex location refers to an element of the boot class |
| 144 | // path. |
| 145 | bool IsInBootClassPath(); |
| 146 | |
| 147 | // Obtains a lock on the target oat file. |
| 148 | // Only one OatFileAssistant object can hold the lock for a target oat file |
| 149 | // at a time. The Lock is released automatically when the OatFileAssistant |
| 150 | // object goes out of scope. The Lock() method must not be called if the |
| 151 | // lock has already been acquired. |
| 152 | // |
| 153 | // Returns true on success. |
| 154 | // Returns false on error, in which case error_msg will contain more |
| 155 | // information on the error. |
| 156 | // |
| 157 | // The 'error_msg' argument must not be null. |
| 158 | // |
| 159 | // This is intended to be used to avoid race conditions when multiple |
| 160 | // processes generate oat files, such as when a foreground Activity and |
| 161 | // a background Service both use DexClassLoaders pointing to the same dex |
| 162 | // file. |
| 163 | bool Lock(std::string* error_msg); |
| 164 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 165 | // 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] | 166 | // dex location. If "downgrade" is set to false, it verifies if the current |
| 167 | // compiler filter is at least as good as an oat file generated with the |
| 168 | // given compiler filter otherwise, if its set to true, it checks whether |
| 169 | // the oat file generated with the target filter will be downgraded as |
| 170 | // compared to the current state. For example, if the current compiler filter is |
| 171 | // quicken, and target filter is verify, it will recommend to dexopt, while |
| 172 | // if the target filter is speed profile, it will recommend to keep it in its |
| 173 | // current state. |
| 174 | // profile_changed should be true to indicate the profile has recently changed |
| 175 | // for this dex location. |
| 176 | // If the purpose of the dexopt is to downgrade the compiler filter, |
| 177 | // set downgrade to true. |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 178 | // Returns a positive status code if the status refers to the oat file in |
| 179 | // the oat location. Returns a negative status code if the status refers to |
| 180 | // the oat file in the odex location. |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 181 | int GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter, |
| 182 | bool profile_changed = false, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 183 | bool downgrade = false, |
| 184 | ClassLoaderContext* context = nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 185 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 186 | // Returns true if there is up-to-date code for this dex location, |
| 187 | // irrespective of the compiler filter of the up-to-date code. |
| 188 | bool IsUpToDate(); |
| 189 | |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 190 | // Return code used when attempting to generate updated code. |
| 191 | enum ResultOfAttemptToUpdate { |
| 192 | kUpdateFailed, // We tried making the code up to date, but |
| 193 | // encountered an unexpected failure. |
| 194 | kUpdateNotAttempted, // We wanted to update the code, but determined we |
| 195 | // should not make the attempt. |
| 196 | kUpdateSucceeded // We successfully made the code up to date |
| 197 | // (possibly by doing nothing). |
| 198 | }; |
| 199 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 200 | // Attempts to generate or relocate the oat file as needed to make it up to |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 201 | // date based on the current runtime and compiler options. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 202 | // profile_changed should be true to indicate the profile has recently |
| 203 | // changed for this dex location. |
| 204 | // |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 205 | // If the dex files need to be made up to date, class_loader_context will be |
| 206 | // passed to dex2oat. |
| 207 | // |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 208 | // Returns the result of attempting to update the code. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 209 | // |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 210 | // If the result is not kUpdateSucceeded, the value of error_msg will be set |
| 211 | // to a string describing why there was a failure or the update was not |
| 212 | // attempted. error_msg must not be null. |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 213 | ResultOfAttemptToUpdate MakeUpToDate(bool profile_changed, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 214 | ClassLoaderContext* class_loader_context, |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 215 | std::string* error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 216 | |
| 217 | // Returns an oat file that can be used for loading dex files. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 218 | // Returns null if no suitable oat file was found. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 219 | // |
| 220 | // After this call, no other methods of the OatFileAssistant should be |
| 221 | // called, because access to the loaded oat file has been taken away from |
| 222 | // the OatFileAssistant object. |
| 223 | std::unique_ptr<OatFile> GetBestOatFile(); |
| 224 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 225 | // Returns a human readable description of the status of the code for the |
| 226 | // dex file. The returned description is for debugging purposes only. |
| 227 | std::string GetStatusDump(); |
| 228 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 229 | // Computes the optimization status of the given dex file. The result is |
| 230 | // returned via the two output parameters. |
| 231 | // - out_compilation_filter: the level of optimizations (compiler filter) |
| 232 | // - out_compilation_reason: the optimization reason. The reason might |
| 233 | // be "unknown" if the compiler artifacts were not annotated during optimizations. |
| 234 | // |
| 235 | // This method will try to mimic the runtime effect of loading the dex file. |
| 236 | // For example, if there is no usable oat file, the compiler filter will be set |
| 237 | // to "run-from-apk". |
| 238 | static void GetOptimizationStatus(const std::string& filename, |
| 239 | InstructionSet isa, |
| 240 | std::string* out_compilation_filter, |
| 241 | std::string* out_compilation_reason); |
| 242 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 243 | // Open and returns an image space associated with the oat file. |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 244 | static std::unique_ptr<gc::space::ImageSpace> OpenImageSpace(const OatFile* oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 245 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 246 | // Loads the dex files in the given oat file for the given dex location. |
| 247 | // The oat file should be up to date for the given dex location. |
| 248 | // This loads multiple dex files in the case of multidex. |
| 249 | // Returns an empty vector if no dex files for that location could be loaded |
| 250 | // from the oat file. |
| 251 | // |
| 252 | // The caller is responsible for freeing the dex_files returned, if any. The |
| 253 | // dex_files will only remain valid as long as the oat_file is valid. |
| 254 | static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles( |
| 255 | const OatFile& oat_file, const char* dex_location); |
| 256 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 257 | // Same as `std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(...)` with the difference: |
| 258 | // - puts the dex files in the given vector |
| 259 | // - returns whether or not all dex files were successfully opened |
| 260 | static bool LoadDexFiles(const OatFile& oat_file, |
| 261 | const std::string& dex_location, |
| 262 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files); |
| 263 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 264 | // Returns true if there are dex files in the original dex location that can |
| 265 | // be compiled with dex2oat for this dex location. |
| 266 | // Returns false if there is no original dex file, or if the original dex |
| 267 | // file is an apk/zip without a classes.dex entry. |
| 268 | bool HasOriginalDexFiles(); |
| 269 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 270 | // If the dex file has been installed with a compiled oat file alongside |
| 271 | // it, the compiled oat file will have the extension .odex, and is referred |
| 272 | // to as the odex file. It is called odex for legacy reasons; the file is |
| 273 | // really an oat file. The odex file will often, but not always, have a |
| 274 | // patch delta of 0 and need to be relocated before use for the purposes of |
| 275 | // ASLR. The odex file is treated as if it were read-only. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 276 | // |
| 277 | // Returns the status of the odex file for the dex location. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 278 | OatStatus OdexFileStatus(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 279 | |
| 280 | // When the dex files is compiled on the target device, the oat file is the |
| 281 | // result. The oat file will have been relocated to some |
| 282 | // (possibly-out-of-date) offset for ASLR. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 283 | // |
| 284 | // Returns the status of the oat file for the dex location. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 285 | OatStatus OatFileStatus(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 286 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 287 | // Executes dex2oat using the current runtime configuration overridden with |
| 288 | // the given arguments. This does not check to see if dex2oat is enabled in |
| 289 | // the runtime configuration. |
| 290 | // Returns true on success. |
| 291 | // |
| 292 | // If there is a failure, the value of error_msg will be set to a string |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 293 | // describing why there was failure. error_msg must not be null. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 294 | // |
| 295 | // TODO: The OatFileAssistant probably isn't the right place to have this |
| 296 | // function. |
| 297 | static bool Dex2Oat(const std::vector<std::string>& args, std::string* error_msg); |
| 298 | |
| 299 | // Constructs the odex file name for the given dex location. |
| 300 | // Returns true on success, in which case odex_filename is set to the odex |
| 301 | // file name. |
Richard Uhler | e8e48ae | 2016-04-19 12:41:04 -0700 | [diff] [blame] | 302 | // Returns false on error, in which case error_msg describes the error and |
| 303 | // odex_filename is not changed. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 304 | // Neither odex_filename nor error_msg may be null. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 305 | static bool DexLocationToOdexFilename(const std::string& location, |
| 306 | InstructionSet isa, |
| 307 | std::string* odex_filename, |
| 308 | std::string* error_msg); |
| 309 | |
| 310 | // Constructs the oat file name for the given dex location. |
| 311 | // Returns true on success, in which case oat_filename is set to the oat |
| 312 | // file name. |
| 313 | // Returns false on error, in which case error_msg describes the error and |
| 314 | // oat_filename is not changed. |
| 315 | // Neither oat_filename nor error_msg may be null. |
| 316 | static bool DexLocationToOatFilename(const std::string& location, |
| 317 | InstructionSet isa, |
| 318 | std::string* oat_filename, |
| 319 | std::string* error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 320 | |
| 321 | private: |
| 322 | struct ImageInfo { |
| 323 | uint32_t oat_checksum = 0; |
| 324 | uintptr_t oat_data_begin = 0; |
| 325 | int32_t patch_delta = 0; |
| 326 | std::string location; |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 327 | |
| 328 | static std::unique_ptr<ImageInfo> GetRuntimeImageInfo(InstructionSet isa, |
| 329 | std::string* error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 330 | }; |
| 331 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 332 | class OatFileInfo { |
| 333 | public: |
| 334 | // Initially the info is for no file in particular. It will treat the |
| 335 | // file as out of date until Reset is called with a real filename to use |
| 336 | // the cache for. |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 337 | // Pass true for is_oat_location if the information associated with this |
| 338 | // OatFileInfo is for the oat location, as opposed to the odex location. |
| 339 | OatFileInfo(OatFileAssistant* oat_file_assistant, bool is_oat_location); |
| 340 | |
| 341 | bool IsOatLocation(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 342 | |
| 343 | const std::string* Filename(); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 344 | |
| 345 | // Returns true if this oat file can be used for running code. The oat |
| 346 | // file can be used for running code as long as it is not out of date with |
| 347 | // respect to the dex code or boot image. An oat file that is out of date |
| 348 | // with respect to relocation is considered useable, because it's possible |
| 349 | // to interpret the dex code rather than run the unrelocated compiled |
| 350 | // code. |
| 351 | bool IsUseable(); |
| 352 | |
| 353 | // Returns the status of this oat file. |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 354 | OatStatus Status(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 355 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 356 | // Return the DexOptNeeded value for this oat file with respect to the |
| 357 | // given target_compilation_filter. |
| 358 | // profile_changed should be true to indicate the profile has recently |
| 359 | // changed for this dex location. |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 360 | // downgrade should be true if the purpose of dexopt is to downgrade the |
| 361 | // compiler filter. |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 362 | DexOptNeeded GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter, |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 363 | bool profile_changed, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 364 | bool downgrade, |
| 365 | ClassLoaderContext* context); |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 366 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 367 | // Returns the loaded file. |
| 368 | // Loads the file if needed. Returns null if the file failed to load. |
| 369 | // The caller shouldn't clean up or free the returned pointer. |
| 370 | const OatFile* GetFile(); |
| 371 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 372 | // Returns true if the file is opened executable. |
| 373 | bool IsExecutable(); |
| 374 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 375 | // Clear any cached information about the file that depends on the |
| 376 | // contents of the file. This does not reset the provided filename. |
| 377 | void Reset(); |
| 378 | |
| 379 | // Clear any cached information and switch to getting info about the oat |
| 380 | // file with the given filename. |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 381 | void Reset(const std::string& filename, bool use_fd, int vdex_fd = -1, int oat_fd = -1); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 382 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 383 | // Release the loaded oat file for runtime use. |
| 384 | // Returns null if the oat file hasn't been loaded or is out of date. |
| 385 | // Ensures the returned file is not loaded executable if it has unuseable |
| 386 | // compiled code. |
| 387 | // |
| 388 | // After this call, no other methods of the OatFileInfo should be |
| 389 | // called, because access to the loaded oat file has been taken away from |
| 390 | // the OatFileInfo object. |
| 391 | std::unique_ptr<OatFile> ReleaseFileForUse(); |
| 392 | |
| 393 | private: |
| 394 | // Returns true if the compiler filter used to generate the file is at |
| 395 | // least as good as the given target filter. profile_changed should be |
| 396 | // true to indicate the profile has recently changed for this dex |
| 397 | // location. |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 398 | // downgrade should be true if the purpose of dexopt is to downgrade the |
| 399 | // compiler filter. |
| 400 | bool CompilerFilterIsOkay(CompilerFilter::Filter target, bool profile_changed, bool downgrade); |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 401 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 402 | bool ClassLoaderContextIsOkay(ClassLoaderContext* context); |
| 403 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 404 | // Release the loaded oat file. |
| 405 | // Returns null if the oat file hasn't been loaded. |
| 406 | // |
| 407 | // After this call, no other methods of the OatFileInfo should be |
| 408 | // called, because access to the loaded oat file has been taken away from |
| 409 | // the OatFileInfo object. |
| 410 | std::unique_ptr<OatFile> ReleaseFile(); |
| 411 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 412 | OatFileAssistant* oat_file_assistant_; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 413 | const bool is_oat_location_; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 414 | |
| 415 | bool filename_provided_ = false; |
| 416 | std::string filename_; |
| 417 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 418 | int oat_fd_ = -1; |
| 419 | int vdex_fd_ = -1; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 420 | bool use_fd_ = false; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 421 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 422 | bool load_attempted_ = false; |
| 423 | std::unique_ptr<OatFile> file_; |
| 424 | |
| 425 | bool status_attempted_ = false; |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 426 | OatStatus status_ = OatStatus::kOatCannotOpen; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 427 | |
| 428 | // For debugging only. |
| 429 | // If this flag is set, the file has been released to the user and the |
| 430 | // OatFileInfo object is in a bad state and should no longer be used. |
| 431 | bool file_released_ = false; |
| 432 | }; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 433 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 434 | // Generate the oat file for the given info from the dex file using the |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 435 | // current runtime compiler options, the specified filter and class loader |
| 436 | // context. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 437 | // This does not check the current status before attempting to generate the |
| 438 | // oat file. |
| 439 | // |
| 440 | // If the result is not kUpdateSucceeded, the value of error_msg will be set |
| 441 | // to a string describing why there was a failure or the update was not |
| 442 | // attempted. error_msg must not be null. |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 443 | ResultOfAttemptToUpdate GenerateOatFileNoChecks(OatFileInfo& info, |
| 444 | CompilerFilter::Filter target, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 445 | const ClassLoaderContext* class_loader_context, |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 446 | std::string* error_msg); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 447 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 448 | // Return info for the best oat file. |
| 449 | OatFileInfo& GetBestInfo(); |
| 450 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 451 | // Returns true when vdex/oat/odex files should be read from file descriptors. |
| 452 | // The method checks the value of zip_fd_, and if the value is valid, returns |
| 453 | // true. This is required to have a deterministic behavior around how different |
| 454 | // files are being read. |
| 455 | bool UseFdToReadFiles(); |
| 456 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 457 | // Returns true if the dex checksums in the given vdex file are up to date |
| 458 | // with respect to the dex location. If the dex checksums are not up to |
| 459 | // date, error_msg is updated with a message describing the problem. |
| 460 | bool DexChecksumUpToDate(const VdexFile& file, std::string* error_msg); |
| 461 | |
| 462 | // Returns true if the dex checksums in the given oat file are up to date |
| 463 | // with respect to the dex location. If the dex checksums are not up to |
| 464 | // date, error_msg is updated with a message describing the problem. |
| 465 | bool DexChecksumUpToDate(const OatFile& file, std::string* error_msg); |
| 466 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 467 | // Return the status for a given opened oat file with respect to the dex |
| 468 | // location. |
| 469 | OatStatus GivenOatFileStatus(const OatFile& file); |
| 470 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 471 | // Returns the current image location. |
| 472 | // Returns an empty string if the image location could not be retrieved. |
| 473 | // |
| 474 | // TODO: This method should belong with an image file manager, not |
| 475 | // the oat file assistant. |
| 476 | static std::string ImageLocation(); |
| 477 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 478 | // Gets the dex checksums required for an up-to-date oat file. |
| 479 | // Returns cached_required_dex_checksums if the required checksums were |
| 480 | // located. Returns null if the required checksums were not found. The |
| 481 | // caller shouldn't clean up or free the returned pointer. This sets the |
| 482 | // has_original_dex_files_ field to true if the checksums were found for the |
| 483 | // dex_location_ dex file. |
| 484 | const std::vector<uint32_t>* GetRequiredDexChecksums(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 485 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 486 | // Returns the loaded image info. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 487 | // Loads the image info if needed. Returns null if the image info failed |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 488 | // to load. |
| 489 | // The caller shouldn't clean up or free the returned pointer. |
| 490 | const ImageInfo* GetImageInfo(); |
| 491 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 492 | // To implement Lock(), we lock a dummy file where the oat file would go |
| 493 | // (adding ".flock" to the target file name) and retain the lock for the |
| 494 | // remaining lifetime of the OatFileAssistant object. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 495 | ScopedFlock flock_; |
| 496 | |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 497 | std::string dex_location_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 498 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 499 | // Whether or not the parent directory of the dex file is writable. |
| 500 | bool dex_parent_writable_ = false; |
| 501 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 502 | // In a properly constructed OatFileAssistant object, isa_ should be either |
| 503 | // the 32 or 64 bit variant for the current device. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 504 | const InstructionSet isa_ = InstructionSet::kNone; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 505 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 506 | // Whether we will attempt to load oat files executable. |
| 507 | bool load_executable_ = false; |
| 508 | |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 509 | // Whether only oat files on /system are loaded executable. |
| 510 | const bool only_load_system_executable_ = false; |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 511 | // Whether the potential zip file only contains uncompressed dex. |
| 512 | // Will be set during GetRequiredDexChecksums. |
| 513 | bool zip_file_only_contains_uncompressed_dex_ = true; |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 514 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 515 | // Cached value of the required dex checksums. |
| 516 | // This should be accessed only by the GetRequiredDexChecksums() method. |
| 517 | std::vector<uint32_t> cached_required_dex_checksums_; |
| 518 | bool required_dex_checksums_attempted_ = false; |
| 519 | bool required_dex_checksums_found_; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 520 | bool has_original_dex_files_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 521 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 522 | OatFileInfo odex_; |
| 523 | OatFileInfo oat_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 524 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 525 | // File descriptor corresponding to apk, dex file, or zip. |
| 526 | int zip_fd_; |
| 527 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 528 | // Cached value of the image info. |
| 529 | // Use the GetImageInfo method rather than accessing these directly. |
| 530 | // TODO: The image info should probably be moved out of the oat file |
| 531 | // assistant to an image file manager. |
| 532 | bool image_info_load_attempted_ = false; |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 533 | std::unique_ptr<ImageInfo> cached_image_info_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 534 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 535 | friend class OatFileAssistantTest; |
| 536 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 537 | DISALLOW_COPY_AND_ASSIGN(OatFileAssistant); |
| 538 | }; |
| 539 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 540 | std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status); |
| 541 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 542 | } // namespace art |
| 543 | |
| 544 | #endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_ |