blob: 0f74ca4b0266c6485819af50ca5fcd682257c7e3 [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
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 Kamath8943c1d2016-05-02 13:14:48 +010022#include <sstream>
Richard Uhler66d874d2015-01-15 09:37:19 -080023#include <string>
24
25#include "arch/instruction_set.h"
26#include "base/scoped_flock.h"
27#include "base/unix_file/fd_file.h"
Andreas Gampe29d38e72016-03-23 15:31:51 +000028#include "compiler_filter.h"
Calin Juravle44e5efa2017-09-12 00:54:26 -070029#include "class_loader_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "oat_file.h"
31#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080032
33namespace art {
34
Mathieu Chartierfbc31082016-01-24 11:59:56 -080035namespace gc {
36namespace space {
37class ImageSpace;
38} // namespace space
39} // namespace gc
40
Richard Uhler66d874d2015-01-15 09:37:19 -080041// 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 Uhler66d874d2015-01-15 09:37:19 -080049class OatFileAssistant {
50 public:
Calin Juravle357c66d2017-05-04 01:57:17 +000051 // 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 Uhler95abd042015-03-24 09:51:28 -070056 enum DexOptNeeded {
Richard Uhler7225a8d2016-11-22 10:12:03 +000057 // No dexopt should (or can) be done to update the apk/jar.
Richard Uhler95abd042015-03-24 09:51:28 -070058 // Matches Java: dalvik.system.DexFile.NO_DEXOPT_NEEDED = 0
59 kNoDexOptNeeded = 0,
Richard Uhler66d874d2015-01-15 09:37:19 -080060
Richard Uhler7225a8d2016-11-22 10:12:03 +000061 // 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 Uhler66d874d2015-01-15 09:37:19 -080064
Richard Uhler7225a8d2016-11-22 10:12:03 +000065 // 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 Uhler95abd042015-03-24 09:51:28 -070069
Richard Uhler7225a8d2016-11-22 10:12:03 +000070 // 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 Uhler5923b522016-12-08 09:48:01 +000076 // is not relocated to match the boot image.
Richard Uhler7225a8d2016-11-22 10:12:03 +000077 // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_RELOCATION
78 kDex2OatForRelocation = 4,
Richard Uhler95abd042015-03-24 09:51:28 -070079 };
80
81 enum OatStatus {
Richard Uhler03bc6592016-11-22 09:42:04 +000082 // kOatCannotOpen - The oat file cannot be opened, because it does not
83 // exist, is unreadable, or otherwise corrupted.
84 kOatCannotOpen,
Richard Uhler95abd042015-03-24 09:51:28 -070085
Richard Uhler03bc6592016-11-22 09:42:04 +000086 // kOatDexOutOfDate - The oat file is out of date with respect to the dex file.
87 kOatDexOutOfDate,
Richard Uhler95abd042015-03-24 09:51:28 -070088
Richard Uhler03bc6592016-11-22 09:42:04 +000089 // 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 Uhler95abd042015-03-24 09:51:28 -0700102 kOatUpToDate,
Richard Uhler66d874d2015-01-15 09:37:19 -0800103 };
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 Chartier2cebb242015-04-21 16:50:40 -0700108 // The dex_location must not be null and should remain available and
Richard Uhler66d874d2015-01-15 09:37:19 -0800109 // 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 Uhler66d874d2015-01-15 09:37:19 -0800113 // 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 Gampe29d38e72016-03-23 15:31:51 +0000119 //
Andreas Gampe29d38e72016-03-23 15:31:51 +0000120 // load_executable should be true if the caller intends to try and load
121 // executable code for this dex location.
Calin Juravleb077e152016-02-18 18:47:37 +0000122 OatFileAssistant(const char* dex_location,
Calin Juravleb077e152016-02-18 18:47:37 +0000123 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700124 bool load_executable,
125 int vdex_fd = -1,
126 int oat_fd = -1);
Richard Uhler66d874d2015-01-15 09:37:19 -0800127
Richard Uhler66d874d2015-01-15 09:37:19 -0800128 ~OatFileAssistant();
129
130 // Returns true if the dex location refers to an element of the boot class
131 // path.
132 bool IsInBootClassPath();
133
134 // Obtains a lock on the target oat file.
135 // Only one OatFileAssistant object can hold the lock for a target oat file
136 // at a time. The Lock is released automatically when the OatFileAssistant
137 // object goes out of scope. The Lock() method must not be called if the
138 // lock has already been acquired.
139 //
140 // Returns true on success.
141 // Returns false on error, in which case error_msg will contain more
142 // information on the error.
143 //
144 // The 'error_msg' argument must not be null.
145 //
146 // This is intended to be used to avoid race conditions when multiple
147 // processes generate oat files, such as when a foreground Activity and
148 // a background Service both use DexClassLoaders pointing to the same dex
149 // file.
150 bool Lock(std::string* error_msg);
151
Richard Uhler95abd042015-03-24 09:51:28 -0700152 // Return what action needs to be taken to produce up-to-date code for this
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700153 // dex location. If "downgrade" is set to false, it verifies if the current
154 // compiler filter is at least as good as an oat file generated with the
155 // given compiler filter otherwise, if its set to true, it checks whether
156 // the oat file generated with the target filter will be downgraded as
157 // compared to the current state. For example, if the current compiler filter is
158 // quicken, and target filter is verify, it will recommend to dexopt, while
159 // if the target filter is speed profile, it will recommend to keep it in its
160 // current state.
161 // profile_changed should be true to indicate the profile has recently changed
162 // for this dex location.
163 // If the purpose of the dexopt is to downgrade the compiler filter,
164 // set downgrade to true.
Richard Uhler7225a8d2016-11-22 10:12:03 +0000165 // Returns a positive status code if the status refers to the oat file in
166 // the oat location. Returns a negative status code if the status refers to
167 // the oat file in the odex location.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700168 int GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
169 bool profile_changed = false,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700170 bool downgrade = false,
171 ClassLoaderContext* context = nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800172
Richard Uhler01be6812016-05-17 10:34:52 -0700173 // Returns true if there is up-to-date code for this dex location,
174 // irrespective of the compiler filter of the up-to-date code.
175 bool IsUpToDate();
176
Richard Uhler1e860612016-03-30 12:17:55 -0700177 // Return code used when attempting to generate updated code.
178 enum ResultOfAttemptToUpdate {
179 kUpdateFailed, // We tried making the code up to date, but
180 // encountered an unexpected failure.
181 kUpdateNotAttempted, // We wanted to update the code, but determined we
182 // should not make the attempt.
183 kUpdateSucceeded // We successfully made the code up to date
184 // (possibly by doing nothing).
185 };
186
Richard Uhler66d874d2015-01-15 09:37:19 -0800187 // Attempts to generate or relocate the oat file as needed to make it up to
Richard Uhlerf4b34872016-04-13 11:03:46 -0700188 // date based on the current runtime and compiler options.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700189 // profile_changed should be true to indicate the profile has recently
190 // changed for this dex location.
191 //
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700192 // If the dex files need to be made up to date, class_loader_context will be
193 // passed to dex2oat.
194 //
Richard Uhlerd1472a22016-04-15 15:18:56 -0700195 // Returns the result of attempting to update the code.
Richard Uhler66d874d2015-01-15 09:37:19 -0800196 //
Richard Uhler1e860612016-03-30 12:17:55 -0700197 // If the result is not kUpdateSucceeded, the value of error_msg will be set
198 // to a string describing why there was a failure or the update was not
199 // attempted. error_msg must not be null.
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700200 ResultOfAttemptToUpdate MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700201 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700202 std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800203
204 // Returns an oat file that can be used for loading dex files.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700205 // Returns null if no suitable oat file was found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800206 //
207 // After this call, no other methods of the OatFileAssistant should be
208 // called, because access to the loaded oat file has been taken away from
209 // the OatFileAssistant object.
210 std::unique_ptr<OatFile> GetBestOatFile();
211
Richard Uhler46cc64f2016-11-14 14:53:55 +0000212 // Returns a human readable description of the status of the code for the
213 // dex file. The returned description is for debugging purposes only.
214 std::string GetStatusDump();
215
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800216 // Open and returns an image space associated with the oat file.
Andreas Gampea463b6a2016-08-12 21:53:32 -0700217 static std::unique_ptr<gc::space::ImageSpace> OpenImageSpace(const OatFile* oat_file);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800218
Richard Uhler66d874d2015-01-15 09:37:19 -0800219 // Loads the dex files in the given oat file for the given dex location.
220 // The oat file should be up to date for the given dex location.
221 // This loads multiple dex files in the case of multidex.
222 // Returns an empty vector if no dex files for that location could be loaded
223 // from the oat file.
224 //
225 // The caller is responsible for freeing the dex_files returned, if any. The
226 // dex_files will only remain valid as long as the oat_file is valid.
227 static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(
228 const OatFile& oat_file, const char* dex_location);
229
Calin Juravle87e2cb62017-06-13 21:48:45 -0700230 // Same as `std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(...)` with the difference:
231 // - puts the dex files in the given vector
232 // - returns whether or not all dex files were successfully opened
233 static bool LoadDexFiles(const OatFile& oat_file,
234 const std::string& dex_location,
235 std::vector<std::unique_ptr<const DexFile>>* out_dex_files);
236
Richard Uhler9b994ea2015-06-24 08:44:19 -0700237 // Returns true if there are dex files in the original dex location that can
238 // be compiled with dex2oat for this dex location.
239 // Returns false if there is no original dex file, or if the original dex
240 // file is an apk/zip without a classes.dex entry.
241 bool HasOriginalDexFiles();
242
Richard Uhler63434112015-03-16 14:32:16 -0700243 // If the dex file has been installed with a compiled oat file alongside
244 // it, the compiled oat file will have the extension .odex, and is referred
245 // to as the odex file. It is called odex for legacy reasons; the file is
246 // really an oat file. The odex file will often, but not always, have a
247 // patch delta of 0 and need to be relocated before use for the purposes of
248 // ASLR. The odex file is treated as if it were read-only.
Richard Uhler03bc6592016-11-22 09:42:04 +0000249 //
250 // Returns the status of the odex file for the dex location.
Richard Uhler95abd042015-03-24 09:51:28 -0700251 OatStatus OdexFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800252
253 // When the dex files is compiled on the target device, the oat file is the
254 // result. The oat file will have been relocated to some
255 // (possibly-out-of-date) offset for ASLR.
Richard Uhler03bc6592016-11-22 09:42:04 +0000256 //
257 // Returns the status of the oat file for the dex location.
Richard Uhler95abd042015-03-24 09:51:28 -0700258 OatStatus OatFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800259
Richard Uhler66d874d2015-01-15 09:37:19 -0800260 // Executes dex2oat using the current runtime configuration overridden with
261 // the given arguments. This does not check to see if dex2oat is enabled in
262 // the runtime configuration.
263 // Returns true on success.
264 //
265 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700266 // describing why there was failure. error_msg must not be null.
Richard Uhler66d874d2015-01-15 09:37:19 -0800267 //
268 // TODO: The OatFileAssistant probably isn't the right place to have this
269 // function.
270 static bool Dex2Oat(const std::vector<std::string>& args, std::string* error_msg);
271
272 // Constructs the odex file name for the given dex location.
273 // Returns true on success, in which case odex_filename is set to the odex
274 // file name.
Richard Uhlere8e48ae2016-04-19 12:41:04 -0700275 // Returns false on error, in which case error_msg describes the error and
276 // odex_filename is not changed.
Richard Uhler66d874d2015-01-15 09:37:19 -0800277 // Neither odex_filename nor error_msg may be null.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700278 static bool DexLocationToOdexFilename(const std::string& location,
279 InstructionSet isa,
280 std::string* odex_filename,
281 std::string* error_msg);
282
283 // Constructs the oat file name for the given dex location.
284 // Returns true on success, in which case oat_filename is set to the oat
285 // file name.
286 // Returns false on error, in which case error_msg describes the error and
287 // oat_filename is not changed.
288 // Neither oat_filename nor error_msg may be null.
289 static bool DexLocationToOatFilename(const std::string& location,
290 InstructionSet isa,
291 std::string* oat_filename,
292 std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800293
294 private:
295 struct ImageInfo {
296 uint32_t oat_checksum = 0;
297 uintptr_t oat_data_begin = 0;
298 int32_t patch_delta = 0;
299 std::string location;
Richard Uhler95e09672017-02-22 11:37:41 +0000300
301 static std::unique_ptr<ImageInfo> GetRuntimeImageInfo(InstructionSet isa,
302 std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 };
304
Richard Uhler743bf362016-04-19 15:39:37 -0700305 class OatFileInfo {
306 public:
307 // Initially the info is for no file in particular. It will treat the
308 // file as out of date until Reset is called with a real filename to use
309 // the cache for.
Richard Uhler88bc6732016-11-14 14:38:03 +0000310 // Pass true for is_oat_location if the information associated with this
311 // OatFileInfo is for the oat location, as opposed to the odex location.
312 OatFileInfo(OatFileAssistant* oat_file_assistant, bool is_oat_location);
313
314 bool IsOatLocation();
Richard Uhler743bf362016-04-19 15:39:37 -0700315
316 const std::string* Filename();
Richard Uhler03bc6592016-11-22 09:42:04 +0000317
318 // Returns true if this oat file can be used for running code. The oat
319 // file can be used for running code as long as it is not out of date with
320 // respect to the dex code or boot image. An oat file that is out of date
321 // with respect to relocation is considered useable, because it's possible
322 // to interpret the dex code rather than run the unrelocated compiled
323 // code.
324 bool IsUseable();
325
326 // Returns the status of this oat file.
Richard Uhler743bf362016-04-19 15:39:37 -0700327 OatStatus Status();
Richard Uhler743bf362016-04-19 15:39:37 -0700328
Richard Uhler70a84262016-11-08 16:51:51 +0000329 // Return the DexOptNeeded value for this oat file with respect to the
330 // given target_compilation_filter.
331 // profile_changed should be true to indicate the profile has recently
332 // changed for this dex location.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700333 // downgrade should be true if the purpose of dexopt is to downgrade the
334 // compiler filter.
Richard Uhler70a84262016-11-08 16:51:51 +0000335 DexOptNeeded GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700336 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700337 bool downgrade,
338 ClassLoaderContext* context);
Richard Uhler70a84262016-11-08 16:51:51 +0000339
Richard Uhler743bf362016-04-19 15:39:37 -0700340 // Returns the loaded file.
341 // Loads the file if needed. Returns null if the file failed to load.
342 // The caller shouldn't clean up or free the returned pointer.
343 const OatFile* GetFile();
344
Richard Uhler743bf362016-04-19 15:39:37 -0700345 // Returns true if the file is opened executable.
346 bool IsExecutable();
347
Richard Uhler743bf362016-04-19 15:39:37 -0700348 // Clear any cached information about the file that depends on the
349 // contents of the file. This does not reset the provided filename.
350 void Reset();
351
352 // Clear any cached information and switch to getting info about the oat
353 // file with the given filename.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700354 void Reset(const std::string& filename, int vdex_fd = -1, int oat_fd = -1);
Richard Uhler743bf362016-04-19 15:39:37 -0700355
Richard Uhler70a84262016-11-08 16:51:51 +0000356 // Release the loaded oat file for runtime use.
357 // Returns null if the oat file hasn't been loaded or is out of date.
358 // Ensures the returned file is not loaded executable if it has unuseable
359 // compiled code.
360 //
361 // After this call, no other methods of the OatFileInfo should be
362 // called, because access to the loaded oat file has been taken away from
363 // the OatFileInfo object.
364 std::unique_ptr<OatFile> ReleaseFileForUse();
365
366 private:
367 // Returns true if the compiler filter used to generate the file is at
368 // least as good as the given target filter. profile_changed should be
369 // true to indicate the profile has recently changed for this dex
370 // location.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700371 // downgrade should be true if the purpose of dexopt is to downgrade the
372 // compiler filter.
373 bool CompilerFilterIsOkay(CompilerFilter::Filter target, bool profile_changed, bool downgrade);
Richard Uhler70a84262016-11-08 16:51:51 +0000374
Calin Juravle44e5efa2017-09-12 00:54:26 -0700375 bool ClassLoaderContextIsOkay(ClassLoaderContext* context);
376
Richard Uhler743bf362016-04-19 15:39:37 -0700377 // Release the loaded oat file.
378 // Returns null if the oat file hasn't been loaded.
379 //
380 // After this call, no other methods of the OatFileInfo should be
381 // called, because access to the loaded oat file has been taken away from
382 // the OatFileInfo object.
383 std::unique_ptr<OatFile> ReleaseFile();
384
Richard Uhler743bf362016-04-19 15:39:37 -0700385 OatFileAssistant* oat_file_assistant_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000386 const bool is_oat_location_;
Richard Uhler743bf362016-04-19 15:39:37 -0700387
388 bool filename_provided_ = false;
389 std::string filename_;
390
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700391 int oat_fd_ = -1;
392 int vdex_fd_ = -1;
393
Richard Uhler743bf362016-04-19 15:39:37 -0700394 bool load_attempted_ = false;
395 std::unique_ptr<OatFile> file_;
396
397 bool status_attempted_ = false;
Andreas Gamped9911ee2017-03-27 13:27:24 -0700398 OatStatus status_ = OatStatus::kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700399
400 // For debugging only.
401 // If this flag is set, the file has been released to the user and the
402 // OatFileInfo object is in a bad state and should no longer be used.
403 bool file_released_ = false;
404 };
Richard Uhler66d874d2015-01-15 09:37:19 -0800405
Calin Juravle357c66d2017-05-04 01:57:17 +0000406 // Generate the oat file for the given info from the dex file using the
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700407 // current runtime compiler options, the specified filter and class loader
408 // context.
Calin Juravle357c66d2017-05-04 01:57:17 +0000409 // This does not check the current status before attempting to generate the
410 // oat file.
411 //
412 // If the result is not kUpdateSucceeded, the value of error_msg will be set
413 // to a string describing why there was a failure or the update was not
414 // attempted. error_msg must not be null.
Calin Juravle07c6d722017-06-07 17:06:12 +0000415 ResultOfAttemptToUpdate GenerateOatFileNoChecks(OatFileInfo& info,
416 CompilerFilter::Filter target,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700417 const ClassLoaderContext* class_loader_context,
Calin Juravle07c6d722017-06-07 17:06:12 +0000418 std::string* error_msg);
Calin Juravle357c66d2017-05-04 01:57:17 +0000419
Richard Uhler88bc6732016-11-14 14:38:03 +0000420 // Return info for the best oat file.
421 OatFileInfo& GetBestInfo();
422
Richard Uhler2f27abd2017-01-31 14:02:34 +0000423 // Returns true if the dex checksums in the given vdex file are up to date
424 // with respect to the dex location. If the dex checksums are not up to
425 // date, error_msg is updated with a message describing the problem.
426 bool DexChecksumUpToDate(const VdexFile& file, std::string* error_msg);
427
428 // Returns true if the dex checksums in the given oat file are up to date
429 // with respect to the dex location. If the dex checksums are not up to
430 // date, error_msg is updated with a message describing the problem.
431 bool DexChecksumUpToDate(const OatFile& file, std::string* error_msg);
432
Richard Uhler03bc6592016-11-22 09:42:04 +0000433 // Return the status for a given opened oat file with respect to the dex
434 // location.
435 OatStatus GivenOatFileStatus(const OatFile& file);
436
Richard Uhler66d874d2015-01-15 09:37:19 -0800437 // Returns the current image location.
438 // Returns an empty string if the image location could not be retrieved.
439 //
440 // TODO: This method should belong with an image file manager, not
441 // the oat file assistant.
442 static std::string ImageLocation();
443
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000444 // Gets the dex checksums required for an up-to-date oat file.
445 // Returns cached_required_dex_checksums if the required checksums were
446 // located. Returns null if the required checksums were not found. The
447 // caller shouldn't clean up or free the returned pointer. This sets the
448 // has_original_dex_files_ field to true if the checksums were found for the
449 // dex_location_ dex file.
450 const std::vector<uint32_t>* GetRequiredDexChecksums();
Richard Uhler66d874d2015-01-15 09:37:19 -0800451
Richard Uhler66d874d2015-01-15 09:37:19 -0800452 // Returns the loaded image info.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700453 // Loads the image info if needed. Returns null if the image info failed
Richard Uhler66d874d2015-01-15 09:37:19 -0800454 // to load.
455 // The caller shouldn't clean up or free the returned pointer.
456 const ImageInfo* GetImageInfo();
457
Richard Uhler66d874d2015-01-15 09:37:19 -0800458 // To implement Lock(), we lock a dummy file where the oat file would go
459 // (adding ".flock" to the target file name) and retain the lock for the
460 // remaining lifetime of the OatFileAssistant object.
Richard Uhler66d874d2015-01-15 09:37:19 -0800461 ScopedFlock flock_;
462
Richard Uhler740eec92015-10-15 15:12:23 -0700463 std::string dex_location_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800464
Calin Juravle357c66d2017-05-04 01:57:17 +0000465 // Whether or not the parent directory of the dex file is writable.
466 bool dex_parent_writable_ = false;
467
Richard Uhler66d874d2015-01-15 09:37:19 -0800468 // In a properly constructed OatFileAssistant object, isa_ should be either
469 // the 32 or 64 bit variant for the current device.
470 const InstructionSet isa_ = kNone;
471
Richard Uhler66d874d2015-01-15 09:37:19 -0800472 // Whether we will attempt to load oat files executable.
473 bool load_executable_ = false;
474
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000475 // Cached value of the required dex checksums.
476 // This should be accessed only by the GetRequiredDexChecksums() method.
477 std::vector<uint32_t> cached_required_dex_checksums_;
478 bool required_dex_checksums_attempted_ = false;
479 bool required_dex_checksums_found_;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700480 bool has_original_dex_files_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800481
Richard Uhler743bf362016-04-19 15:39:37 -0700482 OatFileInfo odex_;
483 OatFileInfo oat_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800484
485 // Cached value of the image info.
486 // Use the GetImageInfo method rather than accessing these directly.
487 // TODO: The image info should probably be moved out of the oat file
488 // assistant to an image file manager.
489 bool image_info_load_attempted_ = false;
Richard Uhler95e09672017-02-22 11:37:41 +0000490 std::unique_ptr<ImageInfo> cached_image_info_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800491
Calin Juravle357c66d2017-05-04 01:57:17 +0000492 friend class OatFileAssistantTest;
493
Richard Uhler66d874d2015-01-15 09:37:19 -0800494 DISALLOW_COPY_AND_ASSIGN(OatFileAssistant);
495};
496
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100497std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status);
498
Richard Uhler66d874d2015-01-15 09:37:19 -0800499} // namespace art
500
501#endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_