blob: 85f4a478684ca7eff69297d045bb4a8429c500fd [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>
22#include <string>
23
24#include "arch/instruction_set.h"
25#include "base/scoped_flock.h"
26#include "base/unix_file/fd_file.h"
Andreas Gampe29d38e72016-03-23 15:31:51 +000027#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080028#include "oat_file.h"
29#include "os.h"
30#include "profiler.h"
31
32namespace art {
33
Mathieu Chartierfbc31082016-01-24 11:59:56 -080034namespace gc {
35namespace space {
36class ImageSpace;
37} // namespace space
38} // namespace gc
39
Richard Uhler66d874d2015-01-15 09:37:19 -080040// 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 Uhler66d874d2015-01-15 09:37:19 -080048class OatFileAssistant {
49 public:
Richard Uhler95abd042015-03-24 09:51:28 -070050 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 Uhler66d874d2015-01-15 09:37:19 -080055
Richard Uhler95abd042015-03-24 09:51:28 -070056 // 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 Uhler66d874d2015-01-15 09:37:19 -080060
Richard Uhler95abd042015-03-24 09:51:28 -070061 // 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 Juravleb077e152016-02-18 18:47:37 +000074 // 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 Uhler95abd042015-03-24 09:51:28 -070076 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 Uhler66d874d2015-01-15 09:37:19 -080085 // date and has been properly relocated for the purposes of ASLR.
Richard Uhler95abd042015-03-24 09:51:28 -070086 kOatUpToDate,
Richard Uhler66d874d2015-01-15 09:37:19 -080087 };
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 Chartier2cebb242015-04-21 16:50:40 -070092 // The dex_location must not be null and should remain available and
Richard Uhler66d874d2015-01-15 09:37:19 -080093 // 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 Uhler66d874d2015-01-15 09:37:19 -080097 // 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 Gampe29d38e72016-03-23 15:31:51 +0000103 //
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 Juravleb077e152016-02-18 18:47:37 +0000109 OatFileAssistant(const char* dex_location,
Calin Juravleb077e152016-02-18 18:47:37 +0000110 const InstructionSet isa,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000111 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -0800112 bool load_executable);
113
114 // Constructs an OatFileAssistant, providing an explicit target oat_location
115 // to use instead of the standard oat location.
Calin Juravleb077e152016-02-18 18:47:37 +0000116 OatFileAssistant(const char* dex_location,
117 const char* oat_location,
Calin Juravleb077e152016-02-18 18:47:37 +0000118 const InstructionSet isa,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000119 bool profile_changed,
Calin Juravleb077e152016-02-18 18:47:37 +0000120 bool load_executable);
Richard Uhler66d874d2015-01-15 09:37:19 -0800121
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 Uhler95abd042015-03-24 09:51:28 -0700146 // Return what action needs to be taken to produce up-to-date code for this
Andreas Gampe29d38e72016-03-23 15:31:51 +0000147 // 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 Uhler66d874d2015-01-15 09:37:19 -0800150
Richard Uhler1e860612016-03-30 12:17:55 -0700151 // Return code used when attempting to generate updated code.
152 enum ResultOfAttemptToUpdate {
153 kUpdateFailed, // We tried making the code up to date, but
154 // encountered an unexpected failure.
155 kUpdateNotAttempted, // We wanted to update the code, but determined we
156 // should not make the attempt.
157 kUpdateSucceeded // We successfully made the code up to date
158 // (possibly by doing nothing).
159 };
160
Richard Uhler66d874d2015-01-15 09:37:19 -0800161 // Attempts to generate or relocate the oat file as needed to make it up to
Richard Uhlerf4b34872016-04-13 11:03:46 -0700162 // date based on the current runtime and compiler options.
Richard Uhler66d874d2015-01-15 09:37:19 -0800163 //
Richard Uhler1e860612016-03-30 12:17:55 -0700164 // If the result is not kUpdateSucceeded, the value of error_msg will be set
165 // to a string describing why there was a failure or the update was not
166 // attempted. error_msg must not be null.
Richard Uhlerf4b34872016-04-13 11:03:46 -0700167 ResultOfAttemptToUpdate MakeUpToDate(std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800168
169 // Returns an oat file that can be used for loading dex files.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700170 // Returns null if no suitable oat file was found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800171 //
172 // After this call, no other methods of the OatFileAssistant should be
173 // called, because access to the loaded oat file has been taken away from
174 // the OatFileAssistant object.
175 std::unique_ptr<OatFile> GetBestOatFile();
176
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800177 // Open and returns an image space associated with the oat file.
178 gc::space::ImageSpace* OpenImageSpace(const OatFile* oat_file);
179
Richard Uhler66d874d2015-01-15 09:37:19 -0800180 // Loads the dex files in the given oat file for the given dex location.
181 // The oat file should be up to date for the given dex location.
182 // This loads multiple dex files in the case of multidex.
183 // Returns an empty vector if no dex files for that location could be loaded
184 // from the oat file.
185 //
186 // The caller is responsible for freeing the dex_files returned, if any. The
187 // dex_files will only remain valid as long as the oat_file is valid.
188 static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(
189 const OatFile& oat_file, const char* dex_location);
190
Richard Uhler9b994ea2015-06-24 08:44:19 -0700191 // Returns true if there are dex files in the original dex location that can
192 // be compiled with dex2oat for this dex location.
193 // Returns false if there is no original dex file, or if the original dex
194 // file is an apk/zip without a classes.dex entry.
195 bool HasOriginalDexFiles();
196
Richard Uhler63434112015-03-16 14:32:16 -0700197 // If the dex file has been installed with a compiled oat file alongside
198 // it, the compiled oat file will have the extension .odex, and is referred
199 // to as the odex file. It is called odex for legacy reasons; the file is
200 // really an oat file. The odex file will often, but not always, have a
201 // patch delta of 0 and need to be relocated before use for the purposes of
202 // ASLR. The odex file is treated as if it were read-only.
Richard Uhler66d874d2015-01-15 09:37:19 -0800203 // These methods return the location and status of the odex file for the dex
204 // location.
205 // Notes:
206 // * OdexFileName may return null if the odex file name could not be
207 // determined.
208 const std::string* OdexFileName();
209 bool OdexFileExists();
Richard Uhler95abd042015-03-24 09:51:28 -0700210 OatStatus OdexFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800211 bool OdexFileIsOutOfDate();
212 bool OdexFileNeedsRelocation();
213 bool OdexFileIsUpToDate();
214
215 // When the dex files is compiled on the target device, the oat file is the
216 // result. The oat file will have been relocated to some
217 // (possibly-out-of-date) offset for ASLR.
218 // These methods return the location and status of the target oat file for
219 // the dex location.
220 //
221 // Notes:
Richard Uhler66d874d2015-01-15 09:37:19 -0800222 // * OatFileName may return null if the oat file name could not be
223 // determined.
224 const std::string* OatFileName();
225 bool OatFileExists();
Richard Uhler95abd042015-03-24 09:51:28 -0700226 OatStatus OatFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800227 bool OatFileIsOutOfDate();
228 bool OatFileNeedsRelocation();
229 bool OatFileIsUpToDate();
230
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800231 // Return image file name. Does not cache since it relies on the oat file.
232 std::string ArtFileName(const OatFile* oat_file) const;
233
Richard Uhler66d874d2015-01-15 09:37:19 -0800234 // These methods return the status for a given opened oat file with respect
235 // to the dex location.
Richard Uhler95abd042015-03-24 09:51:28 -0700236 OatStatus GivenOatFileStatus(const OatFile& file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800237 bool GivenOatFileIsOutOfDate(const OatFile& file);
238 bool GivenOatFileNeedsRelocation(const OatFile& file);
239 bool GivenOatFileIsUpToDate(const OatFile& file);
240
Richard Uhler95abd042015-03-24 09:51:28 -0700241 // Generates the oat file by relocation from the named input file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800242 // This does not check the current status before attempting to relocate the
243 // oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800244 //
Richard Uhler1e860612016-03-30 12:17:55 -0700245 // If the result is not kUpdateSucceeded, the value of error_msg will be set
246 // to a string describing why there was a failure or the update was not
247 // attempted. error_msg must not be null.
248 ResultOfAttemptToUpdate RelocateOatFile(const std::string* input_file, std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800249
Richard Uhlerf4b34872016-04-13 11:03:46 -0700250 // Generate the oat file from the dex file using the current runtime
251 // compiler options.
Richard Uhler66d874d2015-01-15 09:37:19 -0800252 // This does not check the current status before attempting to generate the
253 // oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800254 //
Richard Uhler1e860612016-03-30 12:17:55 -0700255 // 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.
Richard Uhlerf4b34872016-04-13 11:03:46 -0700258 ResultOfAttemptToUpdate GenerateOatFile(std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800259
260 // 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.
275 // Returns false on error, in which case error_msg describes the error.
276 // Neither odex_filename nor error_msg may be null.
277 static bool DexFilenameToOdexFilename(const std::string& location,
278 InstructionSet isa, std::string* odex_filename, std::string* error_msg);
279
Jeff Haofd336c32016-04-07 19:46:31 -0700280 static uint32_t CalculateCombinedImageChecksum(InstructionSet isa = kRuntimeISA);
Jeff Haob11ffb72016-04-07 15:40:54 -0700281
Richard Uhler66d874d2015-01-15 09:37:19 -0800282 private:
283 struct ImageInfo {
284 uint32_t oat_checksum = 0;
285 uintptr_t oat_data_begin = 0;
286 int32_t patch_delta = 0;
287 std::string location;
288 };
289
290 // Returns the path to the dalvik cache directory.
291 // Does not check existence of the cache or try to create it.
292 // Includes the trailing slash.
293 // Returns an empty string if we can't get the dalvik cache directory path.
294 std::string DalvikCacheDirectory();
295
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 // Returns the current image location.
297 // Returns an empty string if the image location could not be retrieved.
298 //
299 // TODO: This method should belong with an image file manager, not
300 // the oat file assistant.
301 static std::string ImageLocation();
302
303 // Gets the dex checksum required for an up-to-date oat file.
304 // Returns dex_checksum if a required checksum was located. Returns
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700305 // null if the required checksum was not found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800306 // The caller shouldn't clean up or free the returned pointer.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700307 // This sets the has_original_dex_files_ field to true if a checksum was
308 // found for the dex_location_ dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800309 const uint32_t* GetRequiredDexChecksum();
310
311 // Returns the loaded odex file.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700312 // Loads the file if needed. Returns null if the file failed to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800313 // The caller shouldn't clean up or free the returned pointer.
314 const OatFile* GetOdexFile();
315
Andreas Gampe29d38e72016-03-23 15:31:51 +0000316 // Returns true if the compiler filter used to generate the odex file is at
317 // least as good as the given target filter.
318 bool OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target);
319
Richard Uhler5f946da2015-07-17 12:28:32 -0700320 // Returns true if the odex file is opened executable.
321 bool OdexFileIsExecutable();
322
Richard Uhlerd1537b52016-03-29 13:27:41 -0700323 // Returns true if the odex file has patch info required to run patchoat.
324 bool OdexFileHasPatchInfo();
325
Richard Uhler66d874d2015-01-15 09:37:19 -0800326 // Clear any cached information about the odex file that depends on the
327 // contents of the file.
328 void ClearOdexFileCache();
329
330 // Returns the loaded oat file.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700331 // Loads the file if needed. Returns null if the file failed to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800332 // The caller shouldn't clean up or free the returned pointer.
333 const OatFile* GetOatFile();
334
Andreas Gampe29d38e72016-03-23 15:31:51 +0000335 // Returns true if the compiler filter used to generate the oat file is at
336 // least as good as the given target filter.
337 bool OatFileCompilerFilterIsOkay(CompilerFilter::Filter target);
338
Richard Uhler5f946da2015-07-17 12:28:32 -0700339 // Returns true if the oat file is opened executable.
340 bool OatFileIsExecutable();
341
Richard Uhlerd1537b52016-03-29 13:27:41 -0700342 // Returns true if the oat file has patch info required to run patchoat.
343 bool OatFileHasPatchInfo();
344
Richard Uhler66d874d2015-01-15 09:37:19 -0800345 // Clear any cached information about the oat file that depends on the
346 // contents of the file.
347 void ClearOatFileCache();
348
349 // Returns the loaded image info.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700350 // Loads the image info if needed. Returns null if the image info failed
Richard Uhler66d874d2015-01-15 09:37:19 -0800351 // to load.
352 // The caller shouldn't clean up or free the returned pointer.
353 const ImageInfo* GetImageInfo();
354
Jeff Haob11ffb72016-04-07 15:40:54 -0700355 uint32_t GetCombinedImageChecksum();
356
Richard Uhler66d874d2015-01-15 09:37:19 -0800357 // To implement Lock(), we lock a dummy file where the oat file would go
358 // (adding ".flock" to the target file name) and retain the lock for the
359 // remaining lifetime of the OatFileAssistant object.
Richard Uhler66d874d2015-01-15 09:37:19 -0800360 ScopedFlock flock_;
361
Richard Uhler740eec92015-10-15 15:12:23 -0700362 std::string dex_location_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800363
364 // In a properly constructed OatFileAssistant object, isa_ should be either
365 // the 32 or 64 bit variant for the current device.
366 const InstructionSet isa_ = kNone;
367
Andreas Gampe29d38e72016-03-23 15:31:51 +0000368 // Whether the profile has recently changed.
369 bool profile_changed_ = false;
370
Richard Uhler66d874d2015-01-15 09:37:19 -0800371 // Whether we will attempt to load oat files executable.
372 bool load_executable_ = false;
373
374 // Cached value of the required dex checksum.
375 // This should be accessed only by the GetRequiredDexChecksum() method.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700376 uint32_t cached_required_dex_checksum_;
377 bool required_dex_checksum_attempted_ = false;
378 bool required_dex_checksum_found_;
379 bool has_original_dex_files_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800380
381 // Cached value of the odex file name.
382 // This should be accessed only by the OdexFileName() method.
383 bool cached_odex_file_name_attempted_ = false;
384 bool cached_odex_file_name_found_;
385 std::string cached_odex_file_name_;
386
387 // Cached value of the loaded odex file.
388 // Use the GetOdexFile method rather than accessing this directly, unless you
389 // know the odex file isn't out of date.
390 bool odex_file_load_attempted_ = false;
391 std::unique_ptr<OatFile> cached_odex_file_;
392
393 // Cached results for OdexFileIsOutOfDate
394 bool odex_file_is_out_of_date_attempted_ = false;
395 bool cached_odex_file_is_out_of_date_;
396
397 // Cached results for OdexFileIsUpToDate
398 bool odex_file_is_up_to_date_attempted_ = false;
399 bool cached_odex_file_is_up_to_date_;
400
401 // Cached value of the oat file name.
402 // This should be accessed only by the OatFileName() method.
403 bool cached_oat_file_name_attempted_ = false;
404 bool cached_oat_file_name_found_;
405 std::string cached_oat_file_name_;
406
Richard Uhlerb361d942015-05-07 10:52:28 -0700407 // Cached value of the loaded oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800408 // Use the GetOatFile method rather than accessing this directly, unless you
Richard Uhlerb361d942015-05-07 10:52:28 -0700409 // know the oat file isn't out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800410 bool oat_file_load_attempted_ = false;
411 std::unique_ptr<OatFile> cached_oat_file_;
412
413 // Cached results for OatFileIsOutOfDate
414 bool oat_file_is_out_of_date_attempted_ = false;
415 bool cached_oat_file_is_out_of_date_;
416
417 // Cached results for OatFileIsUpToDate
418 bool oat_file_is_up_to_date_attempted_ = false;
419 bool cached_oat_file_is_up_to_date_;
420
421 // Cached value of the image info.
422 // Use the GetImageInfo method rather than accessing these directly.
423 // TODO: The image info should probably be moved out of the oat file
424 // assistant to an image file manager.
425 bool image_info_load_attempted_ = false;
426 bool image_info_load_succeeded_ = false;
427 ImageInfo cached_image_info_;
Jeff Haob11ffb72016-04-07 15:40:54 -0700428 uint32_t combined_image_checksum_ = 0;
Richard Uhler66d874d2015-01-15 09:37:19 -0800429
Richard Uhler66d874d2015-01-15 09:37:19 -0800430 // For debugging only.
431 // If this flag is set, the oat or odex file has been released to the user
432 // of the OatFileAssistant object and the OatFileAssistant object is in a
433 // bad state and should no longer be used.
434 bool oat_file_released_ = false;
435
436 DISALLOW_COPY_AND_ASSIGN(OatFileAssistant);
437};
438
439} // namespace art
440
441#endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_