blob: c243cc3a54cc059277ffbf9470047a37a40e2ee3 [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
Eric Holkc7ac91b2021-02-04 21:44:01 +000025#include "base/compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080026#include "arch/instruction_set.h"
David Sehrc431b9d2018-03-02 12:01:51 -080027#include "base/os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080028#include "base/scoped_flock.h"
29#include "base/unix_file/fd_file.h"
Calin Juravle44e5efa2017-09-12 00:54:26 -070030#include "class_loader_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "oat_file.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:
Richard Uhler95abd042015-03-24 09:51:28 -070051 enum DexOptNeeded {
Richard Uhler7225a8d2016-11-22 10:12:03 +000052 // No dexopt should (or can) be done to update the apk/jar.
Richard Uhler95abd042015-03-24 09:51:28 -070053 // Matches Java: dalvik.system.DexFile.NO_DEXOPT_NEEDED = 0
54 kNoDexOptNeeded = 0,
Richard Uhler66d874d2015-01-15 09:37:19 -080055
Richard Uhler7225a8d2016-11-22 10:12:03 +000056 // dex2oat should be run to update the apk/jar from scratch.
57 // Matches Java: dalvik.system.DexFile.DEX2OAT_FROM_SCRATCH = 1
58 kDex2OatFromScratch = 1,
Richard Uhler66d874d2015-01-15 09:37:19 -080059
Richard Uhler7225a8d2016-11-22 10:12:03 +000060 // dex2oat should be run to update the apk/jar because the existing code
61 // is out of date with respect to the boot image.
62 // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_BOOT_IMAGE
63 kDex2OatForBootImage = 2,
Richard Uhler95abd042015-03-24 09:51:28 -070064
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 target compiler filter.
67 // Matches Java: dalvik.system.DexFile.DEX2OAT_FOR_FILTER
68 kDex2OatForFilter = 3,
Richard Uhler95abd042015-03-24 09:51:28 -070069 };
70
71 enum OatStatus {
Richard Uhler03bc6592016-11-22 09:42:04 +000072 // kOatCannotOpen - The oat file cannot be opened, because it does not
73 // exist, is unreadable, or otherwise corrupted.
74 kOatCannotOpen,
Richard Uhler95abd042015-03-24 09:51:28 -070075
Richard Uhler03bc6592016-11-22 09:42:04 +000076 // kOatDexOutOfDate - The oat file is out of date with respect to the dex file.
77 kOatDexOutOfDate,
Richard Uhler95abd042015-03-24 09:51:28 -070078
Richard Uhler03bc6592016-11-22 09:42:04 +000079 // kOatBootImageOutOfDate - The oat file is up to date with respect to the
80 // dex file, but is out of date with respect to the boot image.
81 kOatBootImageOutOfDate,
82
Nicolas Geoffray525fa422021-04-19 07:50:35 +000083 // kOatContextOutOfDate - The context in the oat file is out of date with
84 // respect to the class loader context.
85 kOatContextOutOfDate,
86
Richard Uhler03bc6592016-11-22 09:42:04 +000087 // kOatUpToDate - The oat file is completely up to date with respect to
88 // the dex file and boot image.
Richard Uhler95abd042015-03-24 09:51:28 -070089 kOatUpToDate,
Richard Uhler66d874d2015-01-15 09:37:19 -080090 };
91
92 // Constructs an OatFileAssistant object to assist the oat file
93 // corresponding to the given dex location with the target instruction set.
94 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -070095 // The dex_location must not be null and should remain available and
Richard Uhler66d874d2015-01-15 09:37:19 -080096 // unchanged for the duration of the lifetime of the OatFileAssistant object.
97 // Typically the dex_location is the absolute path to the original,
98 // un-optimized dex file.
99 //
Richard Uhler66d874d2015-01-15 09:37:19 -0800100 // Note: Currently the dex_location must have an extension.
101 // TODO: Relax this restriction?
102 //
103 // The isa should be either the 32 bit or 64 bit variant for the current
104 // device. For example, on an arm device, use arm or arm64. An oat file can
105 // be loaded executable only if the ISA matches the current runtime.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000106 //
Andreas Gampe29d38e72016-03-23 15:31:51 +0000107 // load_executable should be true if the caller intends to try and load
108 // executable code for this dex location.
Nicolas Geoffray29742602017-12-14 10:09:03 +0000109 //
Orion Hodson094b1cf2021-06-08 09:28:28 +0100110 // only_load_trusted_executable should be true if the caller intends to have
111 // only oat files from trusted locations loaded executable. See IsTrustedLocation() for
112 // details on trusted locations.
Calin Juravleb077e152016-02-18 18:47:37 +0000113 OatFileAssistant(const char* dex_location,
Calin Juravleb077e152016-02-18 18:47:37 +0000114 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000115 ClassLoaderContext* context,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000116 bool load_executable,
Orion Hodson094b1cf2021-06-08 09:28:28 +0100117 bool only_load_trusted_executable = false);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700118
119 // Similar to this(const char*, const InstructionSet, bool), however, if a valid zip_fd is
120 // provided, vdex, oat, and zip files will be read from vdex_fd, oat_fd and zip_fd respectively.
121 // Otherwise, dex_location will be used to construct necessary filenames.
122 OatFileAssistant(const char* dex_location,
123 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000124 ClassLoaderContext* context,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700125 bool load_executable,
Orion Hodson094b1cf2021-06-08 09:28:28 +0100126 bool only_load_trusted_executable,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700127 int vdex_fd,
128 int oat_fd,
129 int zip_fd);
Richard Uhler66d874d2015-01-15 09:37:19 -0800130
Richard Uhler66d874d2015-01-15 09:37:19 -0800131 // Returns true if the dex location refers to an element of the boot class
132 // path.
133 bool IsInBootClassPath();
134
Richard Uhler95abd042015-03-24 09:51:28 -0700135 // Return what action needs to be taken to produce up-to-date code for this
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700136 // dex location. If "downgrade" is set to false, it verifies if the current
137 // compiler filter is at least as good as an oat file generated with the
138 // given compiler filter otherwise, if its set to true, it checks whether
139 // the oat file generated with the target filter will be downgraded as
140 // compared to the current state. For example, if the current compiler filter is
141 // quicken, and target filter is verify, it will recommend to dexopt, while
142 // if the target filter is speed profile, it will recommend to keep it in its
143 // current state.
144 // profile_changed should be true to indicate the profile has recently changed
145 // for this dex location.
146 // If the purpose of the dexopt is to downgrade the compiler filter,
147 // set downgrade to true.
Richard Uhler7225a8d2016-11-22 10:12:03 +0000148 // Returns a positive status code if the status refers to the oat file in
149 // the oat location. Returns a negative status code if the status refers to
150 // the oat file in the odex location.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700151 int GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
152 bool profile_changed = false,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000153 bool downgrade = false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800154
Richard Uhler01be6812016-05-17 10:34:52 -0700155 // Returns true if there is up-to-date code for this dex location,
156 // irrespective of the compiler filter of the up-to-date code.
157 bool IsUpToDate();
158
Richard Uhler66d874d2015-01-15 09:37:19 -0800159 // Returns an oat file that can be used for loading dex files.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700160 // Returns null if no suitable oat file was found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800161 //
162 // After this call, no other methods of the OatFileAssistant should be
163 // called, because access to the loaded oat file has been taken away from
164 // the OatFileAssistant object.
165 std::unique_ptr<OatFile> GetBestOatFile();
166
Richard Uhler46cc64f2016-11-14 14:53:55 +0000167 // Returns a human readable description of the status of the code for the
168 // dex file. The returned description is for debugging purposes only.
169 std::string GetStatusDump();
170
Calin Juravle5f9a8012018-02-12 20:27:46 -0800171 // Computes the optimization status of the given dex file. The result is
172 // returned via the two output parameters.
Calin Juravle98071152021-01-27 18:41:58 -0800173 // - out_odex_location: the location of the (best) odex that will be used
174 // for loading. See GetBestInfo().
Calin Juravle5f9a8012018-02-12 20:27:46 -0800175 // - out_compilation_filter: the level of optimizations (compiler filter)
176 // - out_compilation_reason: the optimization reason. The reason might
177 // be "unknown" if the compiler artifacts were not annotated during optimizations.
Calin Juravle98071152021-01-27 18:41:58 -0800178 // - out_odex_status: a human readable refined status of the validity of the odex file.
179 // E.g. up-to-date, boot-image-more-recent, apk-more-recent.
Calin Juravle5f9a8012018-02-12 20:27:46 -0800180 //
181 // This method will try to mimic the runtime effect of loading the dex file.
182 // For example, if there is no usable oat file, the compiler filter will be set
183 // to "run-from-apk".
Calin Juravle98071152021-01-27 18:41:58 -0800184 void GetOptimizationStatus(std::string* out_odex_location,
185 std::string* out_compilation_filter,
186 std::string* out_compilation_reason,
187 std::string* out_odex_status);
188
Calin Juravle5f9a8012018-02-12 20:27:46 -0800189 static void GetOptimizationStatus(const std::string& filename,
190 InstructionSet isa,
191 std::string* out_compilation_filter,
192 std::string* out_compilation_reason);
193
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800194 // Open and returns an image space associated with the oat file.
Andreas Gampea463b6a2016-08-12 21:53:32 -0700195 static std::unique_ptr<gc::space::ImageSpace> OpenImageSpace(const OatFile* oat_file);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800196
Richard Uhler66d874d2015-01-15 09:37:19 -0800197 // Loads the dex files in the given oat file for the given dex location.
198 // The oat file should be up to date for the given dex location.
199 // This loads multiple dex files in the case of multidex.
200 // Returns an empty vector if no dex files for that location could be loaded
201 // from the oat file.
202 //
203 // The caller is responsible for freeing the dex_files returned, if any. The
204 // dex_files will only remain valid as long as the oat_file is valid.
205 static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(
206 const OatFile& oat_file, const char* dex_location);
207
Calin Juravle87e2cb62017-06-13 21:48:45 -0700208 // Same as `std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(...)` with the difference:
209 // - puts the dex files in the given vector
210 // - returns whether or not all dex files were successfully opened
211 static bool LoadDexFiles(const OatFile& oat_file,
212 const std::string& dex_location,
213 std::vector<std::unique_ptr<const DexFile>>* out_dex_files);
214
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100215 // Returns whether this is an apk/zip wit a classes.dex entry.
216 bool HasDexFiles();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700217
Richard Uhler63434112015-03-16 14:32:16 -0700218 // If the dex file has been installed with a compiled oat file alongside
219 // it, the compiled oat file will have the extension .odex, and is referred
220 // to as the odex file. It is called odex for legacy reasons; the file is
221 // really an oat file. The odex file will often, but not always, have a
222 // patch delta of 0 and need to be relocated before use for the purposes of
223 // ASLR. The odex file is treated as if it were read-only.
Richard Uhler03bc6592016-11-22 09:42:04 +0000224 //
225 // Returns the status of the odex file for the dex location.
Richard Uhler95abd042015-03-24 09:51:28 -0700226 OatStatus OdexFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800227
228 // When the dex files is compiled on the target device, the oat file is the
229 // result. The oat file will have been relocated to some
230 // (possibly-out-of-date) offset for ASLR.
Richard Uhler03bc6592016-11-22 09:42:04 +0000231 //
232 // Returns the status of the oat file for the dex location.
Richard Uhler95abd042015-03-24 09:51:28 -0700233 OatStatus OatFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800234
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000235 OatStatus GetBestStatus() {
236 return GetBestInfo().Status();
237 }
238
Richard Uhler66d874d2015-01-15 09:37:19 -0800239 // Constructs the odex file name for the given dex location.
240 // Returns true on success, in which case odex_filename is set to the odex
241 // file name.
Richard Uhlere8e48ae2016-04-19 12:41:04 -0700242 // Returns false on error, in which case error_msg describes the error and
243 // odex_filename is not changed.
Richard Uhler66d874d2015-01-15 09:37:19 -0800244 // Neither odex_filename nor error_msg may be null.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700245 static bool DexLocationToOdexFilename(const std::string& location,
246 InstructionSet isa,
247 std::string* odex_filename,
248 std::string* error_msg);
249
250 // Constructs the oat file name for the given dex location.
251 // Returns true on success, in which case oat_filename is set to the oat
252 // file name.
253 // Returns false on error, in which case error_msg describes the error and
254 // oat_filename is not changed.
255 // Neither oat_filename nor error_msg may be null.
256 static bool DexLocationToOatFilename(const std::string& location,
257 InstructionSet isa,
258 std::string* oat_filename,
259 std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800260
Nicolas Geoffray88566952021-02-16 15:51:18 +0000261 // Computes the dex location and vdex filename. If the data directory of the process
David Brazdil35a3f6a2019-03-04 15:59:06 +0000262 // is known, creates an absolute path in that directory and tries to infer path
263 // of a corresponding vdex file. Otherwise only creates a basename dex_location
264 // from the combined checksums. Returns true if all out-arguments have been set.
265 static bool AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& dex_headers,
266 InstructionSet isa,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000267 /* out */ std::string* dex_location,
268 /* out */ std::string* vdex_filename);
269
270 // Returns true if a filename (given as basename) is a name of a vdex for
271 // anonymous dex file(s) created by AnonymousDexVdexLocation.
272 static bool IsAnonymousVdexBasename(const std::string& basename);
273
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000274 bool ClassLoaderContextIsOkay(const OatFile& oat_file) const;
275
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 private:
Richard Uhler743bf362016-04-19 15:39:37 -0700277 class OatFileInfo {
278 public:
279 // Initially the info is for no file in particular. It will treat the
280 // file as out of date until Reset is called with a real filename to use
281 // the cache for.
Richard Uhler88bc6732016-11-14 14:38:03 +0000282 // Pass true for is_oat_location if the information associated with this
283 // OatFileInfo is for the oat location, as opposed to the odex location.
284 OatFileInfo(OatFileAssistant* oat_file_assistant, bool is_oat_location);
285
286 bool IsOatLocation();
Richard Uhler743bf362016-04-19 15:39:37 -0700287
288 const std::string* Filename();
Richard Uhler03bc6592016-11-22 09:42:04 +0000289
290 // Returns true if this oat file can be used for running code. The oat
291 // file can be used for running code as long as it is not out of date with
292 // respect to the dex code or boot image. An oat file that is out of date
293 // with respect to relocation is considered useable, because it's possible
294 // to interpret the dex code rather than run the unrelocated compiled
295 // code.
296 bool IsUseable();
297
298 // Returns the status of this oat file.
Richard Uhler743bf362016-04-19 15:39:37 -0700299 OatStatus Status();
Richard Uhler743bf362016-04-19 15:39:37 -0700300
Richard Uhler70a84262016-11-08 16:51:51 +0000301 // Return the DexOptNeeded value for this oat file with respect to the
302 // given target_compilation_filter.
303 // profile_changed should be true to indicate the profile has recently
304 // changed for this dex location.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700305 // downgrade should be true if the purpose of dexopt is to downgrade the
306 // compiler filter.
Richard Uhler70a84262016-11-08 16:51:51 +0000307 DexOptNeeded GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000308 bool profile_changed,
309 bool downgrade);
Richard Uhler70a84262016-11-08 16:51:51 +0000310
Richard Uhler743bf362016-04-19 15:39:37 -0700311 // Returns the loaded file.
312 // Loads the file if needed. Returns null if the file failed to load.
313 // The caller shouldn't clean up or free the returned pointer.
314 const OatFile* GetFile();
315
Richard Uhler743bf362016-04-19 15:39:37 -0700316 // Returns true if the file is opened executable.
317 bool IsExecutable();
318
Richard Uhler743bf362016-04-19 15:39:37 -0700319 // Clear any cached information about the file that depends on the
320 // contents of the file. This does not reset the provided filename.
321 void Reset();
322
323 // Clear any cached information and switch to getting info about the oat
324 // file with the given filename.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100325 void Reset(const std::string& filename,
326 bool use_fd,
327 int zip_fd = -1,
328 int vdex_fd = -1,
329 int oat_fd = -1);
Richard Uhler743bf362016-04-19 15:39:37 -0700330
Richard Uhler70a84262016-11-08 16:51:51 +0000331 // Release the loaded oat file for runtime use.
332 // Returns null if the oat file hasn't been loaded or is out of date.
333 // Ensures the returned file is not loaded executable if it has unuseable
334 // compiled code.
335 //
336 // After this call, no other methods of the OatFileInfo should be
337 // called, because access to the loaded oat file has been taken away from
338 // the OatFileInfo object.
339 std::unique_ptr<OatFile> ReleaseFileForUse();
340
341 private:
342 // Returns true if the compiler filter used to generate the file is at
343 // least as good as the given target filter. profile_changed should be
344 // true to indicate the profile has recently changed for this dex
345 // location.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700346 // downgrade should be true if the purpose of dexopt is to downgrade the
347 // compiler filter.
348 bool CompilerFilterIsOkay(CompilerFilter::Filter target, bool profile_changed, bool downgrade);
Richard Uhler70a84262016-11-08 16:51:51 +0000349
Richard Uhler743bf362016-04-19 15:39:37 -0700350 // Release the loaded oat file.
351 // Returns null if the oat file hasn't been loaded.
352 //
353 // After this call, no other methods of the OatFileInfo should be
354 // called, because access to the loaded oat file has been taken away from
355 // the OatFileInfo object.
356 std::unique_ptr<OatFile> ReleaseFile();
357
Richard Uhler743bf362016-04-19 15:39:37 -0700358 OatFileAssistant* oat_file_assistant_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000359 const bool is_oat_location_;
Richard Uhler743bf362016-04-19 15:39:37 -0700360
361 bool filename_provided_ = false;
362 std::string filename_;
363
Nicolas Geoffray30025092018-04-19 14:43:29 +0100364 int zip_fd_ = -1;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700365 int oat_fd_ = -1;
366 int vdex_fd_ = -1;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700367 bool use_fd_ = false;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700368
Richard Uhler743bf362016-04-19 15:39:37 -0700369 bool load_attempted_ = false;
370 std::unique_ptr<OatFile> file_;
371
372 bool status_attempted_ = false;
Andreas Gamped9911ee2017-03-27 13:27:24 -0700373 OatStatus status_ = OatStatus::kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700374
375 // For debugging only.
376 // If this flag is set, the file has been released to the user and the
377 // OatFileInfo object is in a bad state and should no longer be used.
378 bool file_released_ = false;
379 };
Richard Uhler66d874d2015-01-15 09:37:19 -0800380
Richard Uhler88bc6732016-11-14 14:38:03 +0000381 // Return info for the best oat file.
382 OatFileInfo& GetBestInfo();
383
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700384 // Returns true when vdex/oat/odex files should be read from file descriptors.
385 // The method checks the value of zip_fd_, and if the value is valid, returns
386 // true. This is required to have a deterministic behavior around how different
387 // files are being read.
388 bool UseFdToReadFiles();
389
Richard Uhler2f27abd2017-01-31 14:02:34 +0000390 // Returns true if the dex checksums in the given vdex file are up to date
391 // with respect to the dex location. If the dex checksums are not up to
392 // date, error_msg is updated with a message describing the problem.
393 bool DexChecksumUpToDate(const VdexFile& file, std::string* error_msg);
394
395 // Returns true if the dex checksums in the given oat file are up to date
396 // with respect to the dex location. If the dex checksums are not up to
397 // date, error_msg is updated with a message describing the problem.
398 bool DexChecksumUpToDate(const OatFile& file, std::string* error_msg);
399
Richard Uhler03bc6592016-11-22 09:42:04 +0000400 // Return the status for a given opened oat file with respect to the dex
401 // location.
402 OatStatus GivenOatFileStatus(const OatFile& file);
403
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000404 // Gets the dex checksums required for an up-to-date oat file.
405 // Returns cached_required_dex_checksums if the required checksums were
406 // located. Returns null if the required checksums were not found. The
407 // caller shouldn't clean up or free the returned pointer. This sets the
408 // has_original_dex_files_ field to true if the checksums were found for the
409 // dex_location_ dex file.
410 const std::vector<uint32_t>* GetRequiredDexChecksums();
Richard Uhler66d874d2015-01-15 09:37:19 -0800411
Vladimir Markobcd99be2019-03-22 16:21:31 +0000412 // Validates the boot class path checksum of an OatFile.
413 bool ValidateBootClassPathChecksums(const OatFile& oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800414
Richard Uhler740eec92015-10-15 15:12:23 -0700415 std::string dex_location_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800416
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000417 ClassLoaderContext* context_;
418
Calin Juravle357c66d2017-05-04 01:57:17 +0000419 // Whether or not the parent directory of the dex file is writable.
420 bool dex_parent_writable_ = false;
421
Richard Uhler66d874d2015-01-15 09:37:19 -0800422 // In a properly constructed OatFileAssistant object, isa_ should be either
423 // the 32 or 64 bit variant for the current device.
Vladimir Marko33bff252017-11-01 14:35:42 +0000424 const InstructionSet isa_ = InstructionSet::kNone;
Richard Uhler66d874d2015-01-15 09:37:19 -0800425
Richard Uhler66d874d2015-01-15 09:37:19 -0800426 // Whether we will attempt to load oat files executable.
427 bool load_executable_ = false;
428
Orion Hodson094b1cf2021-06-08 09:28:28 +0100429 // Whether only oat files from trusted locations are loaded executable.
430 const bool only_load_trusted_executable_ = false;
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000431 // Whether the potential zip file only contains uncompressed dex.
432 // Will be set during GetRequiredDexChecksums.
433 bool zip_file_only_contains_uncompressed_dex_ = true;
Nicolas Geoffray29742602017-12-14 10:09:03 +0000434
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000435 // Cached value of the required dex checksums.
436 // This should be accessed only by the GetRequiredDexChecksums() method.
437 std::vector<uint32_t> cached_required_dex_checksums_;
438 bool required_dex_checksums_attempted_ = false;
439 bool required_dex_checksums_found_;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700440 bool has_original_dex_files_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800441
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000442 // The AOT-compiled file of an app when the APK of the app is in /data.
Richard Uhler743bf362016-04-19 15:39:37 -0700443 OatFileInfo odex_;
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000444 // The AOT-compiled file of an app when the APK of the app is on a read-only partition
445 // (for example /system).
Richard Uhler743bf362016-04-19 15:39:37 -0700446 OatFileInfo oat_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800447
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000448 // The vdex-only file next to `odex_` when `odex_' cannot be used (for example
449 // it is out of date).
450 OatFileInfo vdex_for_odex_;
451 // The vdex-only file next to 'oat_` when `oat_' cannot be used (for example
452 // it is out of date).
453 OatFileInfo vdex_for_oat_;
454
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100455 // The vdex-only file next to the apk.
456 OatFileInfo dm_for_odex_;
457 OatFileInfo dm_for_oat_;
458
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700459 // File descriptor corresponding to apk, dex file, or zip.
460 int zip_fd_;
461
Vladimir Marko436c6f52019-07-25 14:50:14 +0100462 std::string cached_boot_class_path_;
Vladimir Markobcd99be2019-03-22 16:21:31 +0000463 std::string cached_boot_class_path_checksums_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800464
Calin Juravle357c66d2017-05-04 01:57:17 +0000465 friend class OatFileAssistantTest;
466
Richard Uhler66d874d2015-01-15 09:37:19 -0800467 DISALLOW_COPY_AND_ASSIGN(OatFileAssistant);
468};
469
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100470std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status);
471
Richard Uhler66d874d2015-01-15 09:37:19 -0800472} // namespace art
473
474#endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_