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" |
| 26 | #include "base/scoped_flock.h" |
| 27 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 28 | #include "compiler_filter.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 29 | #include "oat_file.h" |
| 30 | #include "os.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 34 | namespace gc { |
| 35 | namespace space { |
| 36 | class ImageSpace; |
| 37 | } // namespace space |
| 38 | } // namespace gc |
| 39 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 40 | // Class for assisting with oat file management. |
| 41 | // |
| 42 | // This class collects common utilities for determining the status of an oat |
| 43 | // file on the device, updating the oat file, and loading the oat file. |
| 44 | // |
| 45 | // The oat file assistant is intended to be used with dex locations not on the |
| 46 | // boot class path. See the IsInBootClassPath method for a way to check if the |
| 47 | // dex location is in the boot class path. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 48 | class OatFileAssistant { |
| 49 | public: |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 50 | enum DexOptNeeded { |
| 51 | // kNoDexOptNeeded - The code for this dex location is up to date and can |
| 52 | // be used as is. |
| 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 | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 56 | // kDex2OatNeeded - In order to make the code for this dex location up to |
| 57 | // date, dex2oat must be run on the dex file. |
| 58 | // Matches Java: dalvik.system.DexFile.DEX2OAT_NEEDED = 1 |
| 59 | kDex2OatNeeded = 1, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 60 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 61 | // kPatchOatNeeded - In order to make the code for this dex location up to |
| 62 | // date, patchoat must be run on the odex file. |
| 63 | // Matches Java: dalvik.system.DexFile.PATCHOAT_NEEDED = 2 |
| 64 | kPatchOatNeeded = 2, |
| 65 | |
| 66 | // kSelfPatchOatNeeded - In order to make the code for this dex location |
| 67 | // up to date, patchoat must be run on the oat file. |
| 68 | // Matches Java: dalvik.system.DexFile.SELF_PATCHOAT_NEEDED = 3 |
| 69 | kSelfPatchOatNeeded = 3, |
| 70 | }; |
| 71 | |
| 72 | enum OatStatus { |
| 73 | // kOatOutOfDate - An oat file is said to be out of date if the file does |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 74 | // not exist, is out of date with respect to the dex file or boot image, |
| 75 | // or does not meet the target compilation type. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 76 | kOatOutOfDate, |
| 77 | |
| 78 | // kOatNeedsRelocation - An oat file is said to need relocation if the |
| 79 | // code is up to date, but not yet properly relocated for address space |
| 80 | // layout randomization (ASLR). In this case, the oat file is neither |
| 81 | // "out of date" nor "up to date". |
| 82 | kOatNeedsRelocation, |
| 83 | |
| 84 | // kOatUpToDate - An oat file is said to be up to date if it is not out of |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 85 | // date and has been properly relocated for the purposes of ASLR. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 86 | kOatUpToDate, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | // Constructs an OatFileAssistant object to assist the oat file |
| 90 | // corresponding to the given dex location with the target instruction set. |
| 91 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 92 | // The dex_location must not be null and should remain available and |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 93 | // unchanged for the duration of the lifetime of the OatFileAssistant object. |
| 94 | // Typically the dex_location is the absolute path to the original, |
| 95 | // un-optimized dex file. |
| 96 | // |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 97 | // Note: Currently the dex_location must have an extension. |
| 98 | // TODO: Relax this restriction? |
| 99 | // |
| 100 | // The isa should be either the 32 bit or 64 bit variant for the current |
| 101 | // device. For example, on an arm device, use arm or arm64. An oat file can |
| 102 | // be loaded executable only if the ISA matches the current runtime. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 103 | // |
| 104 | // profile_changed should be true if the profile has recently changed |
| 105 | // for this dex location. |
| 106 | // |
| 107 | // load_executable should be true if the caller intends to try and load |
| 108 | // executable code for this dex location. |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 109 | OatFileAssistant(const char* dex_location, |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 110 | const InstructionSet isa, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 111 | bool profile_changed, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 112 | bool load_executable); |
| 113 | |
| 114 | // Constructs an OatFileAssistant, providing an explicit target oat_location |
| 115 | // to use instead of the standard oat location. |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 116 | OatFileAssistant(const char* dex_location, |
| 117 | const char* oat_location, |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 118 | const InstructionSet isa, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 119 | bool profile_changed, |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 120 | bool load_executable); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 121 | |
| 122 | ~OatFileAssistant(); |
| 123 | |
| 124 | // Returns true if the dex location refers to an element of the boot class |
| 125 | // path. |
| 126 | bool IsInBootClassPath(); |
| 127 | |
| 128 | // Obtains a lock on the target oat file. |
| 129 | // Only one OatFileAssistant object can hold the lock for a target oat file |
| 130 | // at a time. The Lock is released automatically when the OatFileAssistant |
| 131 | // object goes out of scope. The Lock() method must not be called if the |
| 132 | // lock has already been acquired. |
| 133 | // |
| 134 | // Returns true on success. |
| 135 | // Returns false on error, in which case error_msg will contain more |
| 136 | // information on the error. |
| 137 | // |
| 138 | // The 'error_msg' argument must not be null. |
| 139 | // |
| 140 | // This is intended to be used to avoid race conditions when multiple |
| 141 | // processes generate oat files, such as when a foreground Activity and |
| 142 | // a background Service both use DexClassLoaders pointing to the same dex |
| 143 | // file. |
| 144 | bool Lock(std::string* error_msg); |
| 145 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 146 | // Return what action needs to be taken to produce up-to-date code for this |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 147 | // dex location that is at least as good as an oat file generated with the |
| 148 | // given compiler filter. |
| 149 | DexOptNeeded GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 150 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 151 | // Returns true if there is up-to-date code for this dex location, |
| 152 | // irrespective of the compiler filter of the up-to-date code. |
| 153 | bool IsUpToDate(); |
| 154 | |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 155 | // Return code used when attempting to generate updated code. |
| 156 | enum ResultOfAttemptToUpdate { |
| 157 | kUpdateFailed, // We tried making the code up to date, but |
| 158 | // encountered an unexpected failure. |
| 159 | kUpdateNotAttempted, // We wanted to update the code, but determined we |
| 160 | // should not make the attempt. |
| 161 | kUpdateSucceeded // We successfully made the code up to date |
| 162 | // (possibly by doing nothing). |
| 163 | }; |
| 164 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 165 | // 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] | 166 | // date based on the current runtime and compiler options. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 167 | // |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 168 | // If the result is not kUpdateSucceeded, the value of error_msg will be set |
| 169 | // to a string describing why there was a failure or the update was not |
| 170 | // attempted. error_msg must not be null. |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 171 | ResultOfAttemptToUpdate MakeUpToDate(std::string* error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 172 | |
| 173 | // Returns an oat file that can be used for loading dex files. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 174 | // Returns null if no suitable oat file was found. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 175 | // |
| 176 | // After this call, no other methods of the OatFileAssistant should be |
| 177 | // called, because access to the loaded oat file has been taken away from |
| 178 | // the OatFileAssistant object. |
| 179 | std::unique_ptr<OatFile> GetBestOatFile(); |
| 180 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 181 | // Open and returns an image space associated with the oat file. |
| 182 | gc::space::ImageSpace* OpenImageSpace(const OatFile* oat_file); |
| 183 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 184 | // Loads the dex files in the given oat file for the given dex location. |
| 185 | // The oat file should be up to date for the given dex location. |
| 186 | // This loads multiple dex files in the case of multidex. |
| 187 | // Returns an empty vector if no dex files for that location could be loaded |
| 188 | // from the oat file. |
| 189 | // |
| 190 | // The caller is responsible for freeing the dex_files returned, if any. The |
| 191 | // dex_files will only remain valid as long as the oat_file is valid. |
| 192 | static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles( |
| 193 | const OatFile& oat_file, const char* dex_location); |
| 194 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 195 | // Returns true if there are dex files in the original dex location that can |
| 196 | // be compiled with dex2oat for this dex location. |
| 197 | // Returns false if there is no original dex file, or if the original dex |
| 198 | // file is an apk/zip without a classes.dex entry. |
| 199 | bool HasOriginalDexFiles(); |
| 200 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 201 | // If the dex file has been installed with a compiled oat file alongside |
| 202 | // it, the compiled oat file will have the extension .odex, and is referred |
| 203 | // to as the odex file. It is called odex for legacy reasons; the file is |
| 204 | // really an oat file. The odex file will often, but not always, have a |
| 205 | // patch delta of 0 and need to be relocated before use for the purposes of |
| 206 | // ASLR. The odex file is treated as if it were read-only. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 207 | // These methods return the location and status of the odex file for the dex |
| 208 | // location. |
| 209 | // Notes: |
| 210 | // * OdexFileName may return null if the odex file name could not be |
| 211 | // determined. |
| 212 | const std::string* OdexFileName(); |
| 213 | bool OdexFileExists(); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 214 | OatStatus OdexFileStatus(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 215 | bool OdexFileIsOutOfDate(); |
| 216 | bool OdexFileNeedsRelocation(); |
| 217 | bool OdexFileIsUpToDate(); |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 218 | // Must only be called if the associated odex file exists, i.e, if |
| 219 | // |OdexFileExists() == true|. |
| 220 | CompilerFilter::Filter OdexFileCompilerFilter(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 221 | |
| 222 | // When the dex files is compiled on the target device, the oat file is the |
| 223 | // result. The oat file will have been relocated to some |
| 224 | // (possibly-out-of-date) offset for ASLR. |
| 225 | // These methods return the location and status of the target oat file for |
| 226 | // the dex location. |
| 227 | // |
| 228 | // Notes: |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 229 | // * OatFileName may return null if the oat file name could not be |
| 230 | // determined. |
| 231 | const std::string* OatFileName(); |
| 232 | bool OatFileExists(); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 233 | OatStatus OatFileStatus(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 234 | bool OatFileIsOutOfDate(); |
| 235 | bool OatFileNeedsRelocation(); |
| 236 | bool OatFileIsUpToDate(); |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 237 | // Must only be called if the associated oat file exists, i.e, if |
| 238 | // |OatFileExists() == true|. |
| 239 | CompilerFilter::Filter OatFileCompilerFilter(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 240 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 241 | // Return image file name. Does not cache since it relies on the oat file. |
| 242 | std::string ArtFileName(const OatFile* oat_file) const; |
| 243 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 244 | // These methods return the status for a given opened oat file with respect |
| 245 | // to the dex location. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 246 | OatStatus GivenOatFileStatus(const OatFile& file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 247 | bool GivenOatFileIsOutOfDate(const OatFile& file); |
| 248 | bool GivenOatFileNeedsRelocation(const OatFile& file); |
| 249 | bool GivenOatFileIsUpToDate(const OatFile& file); |
| 250 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 251 | // Generates the oat file by relocation from the named input file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 252 | // This does not check the current status before attempting to relocate the |
| 253 | // oat file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 254 | // |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 255 | // If the result is not kUpdateSucceeded, the value of error_msg will be set |
| 256 | // to a string describing why there was a failure or the update was not |
| 257 | // attempted. error_msg must not be null. |
| 258 | ResultOfAttemptToUpdate RelocateOatFile(const std::string* input_file, std::string* error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 259 | |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 260 | // Generate the oat file from the dex file using the current runtime |
| 261 | // compiler options. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 262 | // This does not check the current status before attempting to generate the |
| 263 | // oat file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 264 | // |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 265 | // If the result is not kUpdateSucceeded, the value of error_msg will be set |
| 266 | // to a string describing why there was a failure or the update was not |
| 267 | // attempted. error_msg must not be null. |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 268 | ResultOfAttemptToUpdate GenerateOatFile(std::string* error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 269 | |
| 270 | // Executes dex2oat using the current runtime configuration overridden with |
| 271 | // the given arguments. This does not check to see if dex2oat is enabled in |
| 272 | // the runtime configuration. |
| 273 | // Returns true on success. |
| 274 | // |
| 275 | // 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] | 276 | // describing why there was failure. error_msg must not be null. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 277 | // |
| 278 | // TODO: The OatFileAssistant probably isn't the right place to have this |
| 279 | // function. |
| 280 | static bool Dex2Oat(const std::vector<std::string>& args, std::string* error_msg); |
| 281 | |
| 282 | // Constructs the odex file name for the given dex location. |
| 283 | // Returns true on success, in which case odex_filename is set to the odex |
| 284 | // file name. |
| 285 | // Returns false on error, in which case error_msg describes the error. |
| 286 | // Neither odex_filename nor error_msg may be null. |
| 287 | static bool DexFilenameToOdexFilename(const std::string& location, |
| 288 | InstructionSet isa, std::string* odex_filename, std::string* error_msg); |
| 289 | |
Jeff Hao | fd336c3 | 2016-04-07 19:46:31 -0700 | [diff] [blame] | 290 | static uint32_t CalculateCombinedImageChecksum(InstructionSet isa = kRuntimeISA); |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 291 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 292 | private: |
| 293 | struct ImageInfo { |
| 294 | uint32_t oat_checksum = 0; |
| 295 | uintptr_t oat_data_begin = 0; |
| 296 | int32_t patch_delta = 0; |
| 297 | std::string location; |
| 298 | }; |
| 299 | |
| 300 | // Returns the path to the dalvik cache directory. |
| 301 | // Does not check existence of the cache or try to create it. |
| 302 | // Includes the trailing slash. |
| 303 | // Returns an empty string if we can't get the dalvik cache directory path. |
| 304 | std::string DalvikCacheDirectory(); |
| 305 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 306 | // Returns the current image location. |
| 307 | // Returns an empty string if the image location could not be retrieved. |
| 308 | // |
| 309 | // TODO: This method should belong with an image file manager, not |
| 310 | // the oat file assistant. |
| 311 | static std::string ImageLocation(); |
| 312 | |
| 313 | // Gets the dex checksum required for an up-to-date oat file. |
| 314 | // Returns dex_checksum if a required checksum was located. Returns |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 315 | // null if the required checksum was not found. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 316 | // The caller shouldn't clean up or free the returned pointer. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 317 | // This sets the has_original_dex_files_ field to true if a checksum was |
| 318 | // found for the dex_location_ dex file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 319 | const uint32_t* GetRequiredDexChecksum(); |
| 320 | |
| 321 | // Returns the loaded odex file. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 322 | // Loads the file if needed. Returns null if the file failed to load. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 323 | // The caller shouldn't clean up or free the returned pointer. |
| 324 | const OatFile* GetOdexFile(); |
| 325 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 326 | // Returns true if the compiler filter used to generate the odex file is at |
| 327 | // least as good as the given target filter. |
| 328 | bool OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target); |
| 329 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 330 | // Returns true if the odex file is opened executable. |
| 331 | bool OdexFileIsExecutable(); |
| 332 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 333 | // Returns true if the odex file has patch info required to run patchoat. |
| 334 | bool OdexFileHasPatchInfo(); |
| 335 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 336 | // Clear any cached information about the odex file that depends on the |
| 337 | // contents of the file. |
| 338 | void ClearOdexFileCache(); |
| 339 | |
| 340 | // Returns the loaded oat file. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 341 | // Loads the file if needed. Returns null if the file failed to load. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 342 | // The caller shouldn't clean up or free the returned pointer. |
| 343 | const OatFile* GetOatFile(); |
| 344 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 345 | // Returns true if the compiler filter used to generate the oat file is at |
| 346 | // least as good as the given target filter. |
| 347 | bool OatFileCompilerFilterIsOkay(CompilerFilter::Filter target); |
| 348 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 349 | // Returns true if the oat file is opened executable. |
| 350 | bool OatFileIsExecutable(); |
| 351 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 352 | // Returns true if the oat file has patch info required to run patchoat. |
| 353 | bool OatFileHasPatchInfo(); |
| 354 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 355 | // Clear any cached information about the oat file that depends on the |
| 356 | // contents of the file. |
| 357 | void ClearOatFileCache(); |
| 358 | |
| 359 | // Returns the loaded image info. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 360 | // 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] | 361 | // to load. |
| 362 | // The caller shouldn't clean up or free the returned pointer. |
| 363 | const ImageInfo* GetImageInfo(); |
| 364 | |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 365 | uint32_t GetCombinedImageChecksum(); |
| 366 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 367 | // To implement Lock(), we lock a dummy file where the oat file would go |
| 368 | // (adding ".flock" to the target file name) and retain the lock for the |
| 369 | // remaining lifetime of the OatFileAssistant object. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 370 | ScopedFlock flock_; |
| 371 | |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 372 | std::string dex_location_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 373 | |
| 374 | // In a properly constructed OatFileAssistant object, isa_ should be either |
| 375 | // the 32 or 64 bit variant for the current device. |
| 376 | const InstructionSet isa_ = kNone; |
| 377 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 378 | // Whether the profile has recently changed. |
| 379 | bool profile_changed_ = false; |
| 380 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 381 | // Whether we will attempt to load oat files executable. |
| 382 | bool load_executable_ = false; |
| 383 | |
| 384 | // Cached value of the required dex checksum. |
| 385 | // This should be accessed only by the GetRequiredDexChecksum() method. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 386 | uint32_t cached_required_dex_checksum_; |
| 387 | bool required_dex_checksum_attempted_ = false; |
| 388 | bool required_dex_checksum_found_; |
| 389 | bool has_original_dex_files_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 390 | |
| 391 | // Cached value of the odex file name. |
| 392 | // This should be accessed only by the OdexFileName() method. |
| 393 | bool cached_odex_file_name_attempted_ = false; |
| 394 | bool cached_odex_file_name_found_; |
| 395 | std::string cached_odex_file_name_; |
| 396 | |
| 397 | // Cached value of the loaded odex file. |
| 398 | // Use the GetOdexFile method rather than accessing this directly, unless you |
| 399 | // know the odex file isn't out of date. |
| 400 | bool odex_file_load_attempted_ = false; |
| 401 | std::unique_ptr<OatFile> cached_odex_file_; |
| 402 | |
| 403 | // Cached results for OdexFileIsOutOfDate |
| 404 | bool odex_file_is_out_of_date_attempted_ = false; |
| 405 | bool cached_odex_file_is_out_of_date_; |
| 406 | |
| 407 | // Cached results for OdexFileIsUpToDate |
| 408 | bool odex_file_is_up_to_date_attempted_ = false; |
| 409 | bool cached_odex_file_is_up_to_date_; |
| 410 | |
| 411 | // Cached value of the oat file name. |
| 412 | // This should be accessed only by the OatFileName() method. |
| 413 | bool cached_oat_file_name_attempted_ = false; |
| 414 | bool cached_oat_file_name_found_; |
| 415 | std::string cached_oat_file_name_; |
| 416 | |
Richard Uhler | b361d94 | 2015-05-07 10:52:28 -0700 | [diff] [blame] | 417 | // Cached value of the loaded oat file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 418 | // Use the GetOatFile method rather than accessing this directly, unless you |
Richard Uhler | b361d94 | 2015-05-07 10:52:28 -0700 | [diff] [blame] | 419 | // know the oat file isn't out of date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 420 | bool oat_file_load_attempted_ = false; |
| 421 | std::unique_ptr<OatFile> cached_oat_file_; |
| 422 | |
| 423 | // Cached results for OatFileIsOutOfDate |
| 424 | bool oat_file_is_out_of_date_attempted_ = false; |
| 425 | bool cached_oat_file_is_out_of_date_; |
| 426 | |
| 427 | // Cached results for OatFileIsUpToDate |
| 428 | bool oat_file_is_up_to_date_attempted_ = false; |
| 429 | bool cached_oat_file_is_up_to_date_; |
| 430 | |
| 431 | // Cached value of the image info. |
| 432 | // Use the GetImageInfo method rather than accessing these directly. |
| 433 | // TODO: The image info should probably be moved out of the oat file |
| 434 | // assistant to an image file manager. |
| 435 | bool image_info_load_attempted_ = false; |
| 436 | bool image_info_load_succeeded_ = false; |
| 437 | ImageInfo cached_image_info_; |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 438 | uint32_t combined_image_checksum_ = 0; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 439 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 440 | // For debugging only. |
| 441 | // If this flag is set, the oat or odex file has been released to the user |
| 442 | // of the OatFileAssistant object and the OatFileAssistant object is in a |
| 443 | // bad state and should no longer be used. |
| 444 | bool oat_file_released_ = false; |
| 445 | |
| 446 | DISALLOW_COPY_AND_ASSIGN(OatFileAssistant); |
| 447 | }; |
| 448 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 449 | std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status); |
| 450 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 451 | } // namespace art |
| 452 | |
| 453 | #endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_ |