blob: 893aea2ab9dc87dbdbac488f6d9b6565d608239f [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"
27#include "oat_file.h"
28#include "os.h"
29#include "profiler.h"
30
31namespace art {
32
Mathieu Chartierfbc31082016-01-24 11:59:56 -080033namespace gc {
34namespace space {
35class ImageSpace;
36} // namespace space
37} // namespace gc
38
Richard Uhler66d874d2015-01-15 09:37:19 -080039// Class for assisting with oat file management.
40//
41// This class collects common utilities for determining the status of an oat
42// file on the device, updating the oat file, and loading the oat file.
43//
44// The oat file assistant is intended to be used with dex locations not on the
45// boot class path. See the IsInBootClassPath method for a way to check if the
46// dex location is in the boot class path.
Richard Uhler66d874d2015-01-15 09:37:19 -080047class OatFileAssistant {
48 public:
Richard Uhler95abd042015-03-24 09:51:28 -070049 enum DexOptNeeded {
50 // kNoDexOptNeeded - The code for this dex location is up to date and can
51 // be used as is.
52 // Matches Java: dalvik.system.DexFile.NO_DEXOPT_NEEDED = 0
53 kNoDexOptNeeded = 0,
Richard Uhler66d874d2015-01-15 09:37:19 -080054
Richard Uhler95abd042015-03-24 09:51:28 -070055 // kDex2OatNeeded - In order to make the code for this dex location up to
56 // date, dex2oat must be run on the dex file.
57 // Matches Java: dalvik.system.DexFile.DEX2OAT_NEEDED = 1
58 kDex2OatNeeded = 1,
Richard Uhler66d874d2015-01-15 09:37:19 -080059
Richard Uhler95abd042015-03-24 09:51:28 -070060 // kPatchOatNeeded - In order to make the code for this dex location up to
61 // date, patchoat must be run on the odex file.
62 // Matches Java: dalvik.system.DexFile.PATCHOAT_NEEDED = 2
63 kPatchOatNeeded = 2,
64
65 // kSelfPatchOatNeeded - In order to make the code for this dex location
66 // up to date, patchoat must be run on the oat file.
67 // Matches Java: dalvik.system.DexFile.SELF_PATCHOAT_NEEDED = 3
68 kSelfPatchOatNeeded = 3,
69 };
70
71 enum OatStatus {
72 // kOatOutOfDate - An oat file is said to be out of date if the file does
Calin Juravleb077e152016-02-18 18:47:37 +000073 // not exist, is out of date with respect to the dex file or boot image,
74 // or does not meet the target compilation type.
Richard Uhler95abd042015-03-24 09:51:28 -070075 kOatOutOfDate,
76
77 // kOatNeedsRelocation - An oat file is said to need relocation if the
78 // code is up to date, but not yet properly relocated for address space
79 // layout randomization (ASLR). In this case, the oat file is neither
80 // "out of date" nor "up to date".
81 kOatNeedsRelocation,
82
83 // kOatUpToDate - An oat file is said to be up to date if it is not out of
Richard Uhler66d874d2015-01-15 09:37:19 -080084 // date and has been properly relocated for the purposes of ASLR.
Richard Uhler95abd042015-03-24 09:51:28 -070085 kOatUpToDate,
Richard Uhler66d874d2015-01-15 09:37:19 -080086 };
87
Calin Juravleb077e152016-02-18 18:47:37 +000088 // Represents the different compilation types of oat files that OatFileAssitant
89 // and external GetDexOptNeeded callers care about.
90 // Note: these should be able to be used as part of a mask.
91 enum CompilationType {
92 // Matches Java: dalvik.system.DexFile.COMPILATION_TYPE_FULL = 1
93 kFullCompilation = 1,
94
95 // Matches Java: dalvik.system.DexFile.COMPILATION_TYPE_PROFILE_GUIDE = 2
96 kProfileGuideCompilation = 2,
97
98 // Matches Java: dalvik.system.DexFile.COMPILATION_TYPE_EXTRACT_ONLY = 4
99 kExtractOnly = 4,
100 };
101
Richard Uhler66d874d2015-01-15 09:37:19 -0800102 // Constructs an OatFileAssistant object to assist the oat file
103 // corresponding to the given dex location with the target instruction set.
104 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700105 // The dex_location must not be null and should remain available and
Richard Uhler66d874d2015-01-15 09:37:19 -0800106 // unchanged for the duration of the lifetime of the OatFileAssistant object.
107 // Typically the dex_location is the absolute path to the original,
108 // un-optimized dex file.
109 //
Richard Uhler66d874d2015-01-15 09:37:19 -0800110 // Note: Currently the dex_location must have an extension.
111 // TODO: Relax this restriction?
112 //
Calin Juravleb077e152016-02-18 18:47:37 +0000113 // The target compilation type specifies a set of CompilationTypes that
114 // should be considered up to date. An oat file compiled in a way not
115 // included in the set is considered out of date. For example, to consider
116 // otherwise up-to-date fully compiled and profile-guide compiled oat
117 // files as up to date, but to consider extract-only files as out of date,
118 // specify: (kFullCompilation | kProfileGuideCompilation).
119 //
Richard Uhler66d874d2015-01-15 09:37:19 -0800120 // The isa should be either the 32 bit or 64 bit variant for the current
121 // device. For example, on an arm device, use arm or arm64. An oat file can
122 // be loaded executable only if the ISA matches the current runtime.
Calin Juravleb077e152016-02-18 18:47:37 +0000123 OatFileAssistant(const char* dex_location,
124 int target_compilation_type_mask,
125 const InstructionSet isa,
Richard Uhler66d874d2015-01-15 09:37:19 -0800126 bool load_executable);
127
128 // Constructs an OatFileAssistant, providing an explicit target oat_location
129 // to use instead of the standard oat location.
Calin Juravleb077e152016-02-18 18:47:37 +0000130 OatFileAssistant(const char* dex_location,
131 const char* oat_location,
132 int target_compilation_type_mask,
133 const InstructionSet isa,
134 bool load_executable);
Richard Uhler66d874d2015-01-15 09:37:19 -0800135
136 ~OatFileAssistant();
137
138 // Returns true if the dex location refers to an element of the boot class
139 // path.
140 bool IsInBootClassPath();
141
142 // Obtains a lock on the target oat file.
143 // Only one OatFileAssistant object can hold the lock for a target oat file
144 // at a time. The Lock is released automatically when the OatFileAssistant
145 // object goes out of scope. The Lock() method must not be called if the
146 // lock has already been acquired.
147 //
148 // Returns true on success.
149 // Returns false on error, in which case error_msg will contain more
150 // information on the error.
151 //
152 // The 'error_msg' argument must not be null.
153 //
154 // This is intended to be used to avoid race conditions when multiple
155 // processes generate oat files, such as when a foreground Activity and
156 // a background Service both use DexClassLoaders pointing to the same dex
157 // file.
158 bool Lock(std::string* error_msg);
159
Richard Uhler95abd042015-03-24 09:51:28 -0700160 // Return what action needs to be taken to produce up-to-date code for this
161 // dex location.
162 DexOptNeeded GetDexOptNeeded();
Richard Uhler66d874d2015-01-15 09:37:19 -0800163
164 // Attempts to generate or relocate the oat file as needed to make it up to
165 // date.
166 // Returns true on success.
167 //
168 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700169 // describing why there was failure. error_msg must not be null.
Richard Uhler66d874d2015-01-15 09:37:19 -0800170 bool MakeUpToDate(std::string* error_msg);
171
172 // Returns an oat file that can be used for loading dex files.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700173 // Returns null if no suitable oat file was found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800174 //
175 // After this call, no other methods of the OatFileAssistant should be
176 // called, because access to the loaded oat file has been taken away from
177 // the OatFileAssistant object.
178 std::unique_ptr<OatFile> GetBestOatFile();
179
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800180 // Open and returns an image space associated with the oat file.
181 gc::space::ImageSpace* OpenImageSpace(const OatFile* oat_file);
182
Richard Uhler66d874d2015-01-15 09:37:19 -0800183 // Loads the dex files in the given oat file for the given dex location.
184 // The oat file should be up to date for the given dex location.
185 // This loads multiple dex files in the case of multidex.
186 // Returns an empty vector if no dex files for that location could be loaded
187 // from the oat file.
188 //
189 // The caller is responsible for freeing the dex_files returned, if any. The
190 // dex_files will only remain valid as long as the oat_file is valid.
191 static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(
192 const OatFile& oat_file, const char* dex_location);
193
Richard Uhler9b994ea2015-06-24 08:44:19 -0700194 // Returns true if there are dex files in the original dex location that can
195 // be compiled with dex2oat for this dex location.
196 // Returns false if there is no original dex file, or if the original dex
197 // file is an apk/zip without a classes.dex entry.
198 bool HasOriginalDexFiles();
199
Richard Uhler63434112015-03-16 14:32:16 -0700200 // If the dex file has been installed with a compiled oat file alongside
201 // it, the compiled oat file will have the extension .odex, and is referred
202 // to as the odex file. It is called odex for legacy reasons; the file is
203 // really an oat file. The odex file will often, but not always, have a
204 // patch delta of 0 and need to be relocated before use for the purposes of
205 // ASLR. The odex file is treated as if it were read-only.
Richard Uhler66d874d2015-01-15 09:37:19 -0800206 // These methods return the location and status of the odex file for the dex
207 // location.
208 // Notes:
209 // * OdexFileName may return null if the odex file name could not be
210 // determined.
211 const std::string* OdexFileName();
212 bool OdexFileExists();
Richard Uhler95abd042015-03-24 09:51:28 -0700213 OatStatus OdexFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800214 bool OdexFileIsOutOfDate();
215 bool OdexFileNeedsRelocation();
216 bool OdexFileIsUpToDate();
217
218 // When the dex files is compiled on the target device, the oat file is the
219 // result. The oat file will have been relocated to some
220 // (possibly-out-of-date) offset for ASLR.
221 // These methods return the location and status of the target oat file for
222 // the dex location.
223 //
224 // Notes:
Richard Uhler66d874d2015-01-15 09:37:19 -0800225 // * OatFileName may return null if the oat file name could not be
226 // determined.
227 const std::string* OatFileName();
228 bool OatFileExists();
Richard Uhler95abd042015-03-24 09:51:28 -0700229 OatStatus OatFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800230 bool OatFileIsOutOfDate();
231 bool OatFileNeedsRelocation();
232 bool OatFileIsUpToDate();
233
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800234 // Return image file name. Does not cache since it relies on the oat file.
235 std::string ArtFileName(const OatFile* oat_file) const;
236
Richard Uhler66d874d2015-01-15 09:37:19 -0800237 // These methods return the status for a given opened oat file with respect
238 // to the dex location.
Richard Uhler95abd042015-03-24 09:51:28 -0700239 OatStatus GivenOatFileStatus(const OatFile& file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800240 bool GivenOatFileIsOutOfDate(const OatFile& file);
241 bool GivenOatFileNeedsRelocation(const OatFile& file);
242 bool GivenOatFileIsUpToDate(const OatFile& file);
243
Richard Uhler95abd042015-03-24 09:51:28 -0700244 // Generates the oat file by relocation from the named input file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800245 // This does not check the current status before attempting to relocate the
246 // oat file.
247 // Returns true on success.
248 // This will fail if dex2oat is not enabled in the current runtime.
249 //
250 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700251 // describing why there was failure. error_msg must not be null.
Richard Uhler95abd042015-03-24 09:51:28 -0700252 bool RelocateOatFile(const std::string* input_file, std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800253
254 // Generate the oat file from the dex file.
255 // This does not check the current status before attempting to generate the
256 // oat file.
257 // Returns true on success.
258 // This will fail if dex2oat is not enabled in the current runtime.
259 //
260 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700261 // describing why there was failure. error_msg must not be null.
Richard Uhler66d874d2015-01-15 09:37:19 -0800262 bool GenerateOatFile(std::string* error_msg);
263
264 // Executes dex2oat using the current runtime configuration overridden with
265 // the given arguments. This does not check to see if dex2oat is enabled in
266 // the runtime configuration.
267 // Returns true on success.
268 //
269 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700270 // describing why there was failure. error_msg must not be null.
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 //
272 // TODO: The OatFileAssistant probably isn't the right place to have this
273 // function.
274 static bool Dex2Oat(const std::vector<std::string>& args, std::string* error_msg);
275
276 // Constructs the odex file name for the given dex location.
277 // Returns true on success, in which case odex_filename is set to the odex
278 // file name.
279 // Returns false on error, in which case error_msg describes the error.
280 // Neither odex_filename nor error_msg may be null.
281 static bool DexFilenameToOdexFilename(const std::string& location,
282 InstructionSet isa, std::string* odex_filename, std::string* error_msg);
283
284 private:
285 struct ImageInfo {
286 uint32_t oat_checksum = 0;
287 uintptr_t oat_data_begin = 0;
288 int32_t patch_delta = 0;
289 std::string location;
290 };
291
292 // Returns the path to the dalvik cache directory.
293 // Does not check existence of the cache or try to create it.
294 // Includes the trailing slash.
295 // Returns an empty string if we can't get the dalvik cache directory path.
296 std::string DalvikCacheDirectory();
297
Richard Uhler66d874d2015-01-15 09:37:19 -0800298 // Returns the current image location.
299 // Returns an empty string if the image location could not be retrieved.
300 //
301 // TODO: This method should belong with an image file manager, not
302 // the oat file assistant.
303 static std::string ImageLocation();
304
305 // Gets the dex checksum required for an up-to-date oat file.
306 // Returns dex_checksum if a required checksum was located. Returns
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700307 // null if the required checksum was not found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800308 // The caller shouldn't clean up or free the returned pointer.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700309 // This sets the has_original_dex_files_ field to true if a checksum was
310 // found for the dex_location_ dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800311 const uint32_t* GetRequiredDexChecksum();
312
313 // Returns the loaded odex file.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700314 // Loads the file if needed. Returns null if the file failed to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800315 // The caller shouldn't clean up or free the returned pointer.
316 const OatFile* GetOdexFile();
317
Richard Uhler5f946da2015-07-17 12:28:32 -0700318 // Returns true if the odex file is opened executable.
319 bool OdexFileIsExecutable();
320
Richard Uhler66d874d2015-01-15 09:37:19 -0800321 // Clear any cached information about the odex file that depends on the
322 // contents of the file.
323 void ClearOdexFileCache();
324
325 // Returns the loaded oat file.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700326 // Loads the file if needed. Returns null if the file failed to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800327 // The caller shouldn't clean up or free the returned pointer.
328 const OatFile* GetOatFile();
329
Richard Uhler5f946da2015-07-17 12:28:32 -0700330 // Returns true if the oat file is opened executable.
331 bool OatFileIsExecutable();
332
Richard Uhler66d874d2015-01-15 09:37:19 -0800333 // Clear any cached information about the oat file that depends on the
334 // contents of the file.
335 void ClearOatFileCache();
336
337 // Returns the loaded image info.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700338 // Loads the image info if needed. Returns null if the image info failed
Richard Uhler66d874d2015-01-15 09:37:19 -0800339 // to load.
340 // The caller shouldn't clean up or free the returned pointer.
341 const ImageInfo* GetImageInfo();
342
Richard Uhler66d874d2015-01-15 09:37:19 -0800343 // To implement Lock(), we lock a dummy file where the oat file would go
344 // (adding ".flock" to the target file name) and retain the lock for the
345 // remaining lifetime of the OatFileAssistant object.
Richard Uhler66d874d2015-01-15 09:37:19 -0800346 ScopedFlock flock_;
347
Richard Uhler740eec92015-10-15 15:12:23 -0700348 std::string dex_location_;
Calin Juravleb077e152016-02-18 18:47:37 +0000349 const int target_compilation_type_mask_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800350
351 // In a properly constructed OatFileAssistant object, isa_ should be either
352 // the 32 or 64 bit variant for the current device.
353 const InstructionSet isa_ = kNone;
354
Richard Uhler66d874d2015-01-15 09:37:19 -0800355 // Whether we will attempt to load oat files executable.
356 bool load_executable_ = false;
357
358 // Cached value of the required dex checksum.
359 // This should be accessed only by the GetRequiredDexChecksum() method.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700360 uint32_t cached_required_dex_checksum_;
361 bool required_dex_checksum_attempted_ = false;
362 bool required_dex_checksum_found_;
363 bool has_original_dex_files_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800364
365 // Cached value of the odex file name.
366 // This should be accessed only by the OdexFileName() method.
367 bool cached_odex_file_name_attempted_ = false;
368 bool cached_odex_file_name_found_;
369 std::string cached_odex_file_name_;
370
371 // Cached value of the loaded odex file.
372 // Use the GetOdexFile method rather than accessing this directly, unless you
373 // know the odex file isn't out of date.
374 bool odex_file_load_attempted_ = false;
375 std::unique_ptr<OatFile> cached_odex_file_;
376
377 // Cached results for OdexFileIsOutOfDate
378 bool odex_file_is_out_of_date_attempted_ = false;
379 bool cached_odex_file_is_out_of_date_;
380
381 // Cached results for OdexFileIsUpToDate
382 bool odex_file_is_up_to_date_attempted_ = false;
383 bool cached_odex_file_is_up_to_date_;
384
385 // Cached value of the oat file name.
386 // This should be accessed only by the OatFileName() method.
387 bool cached_oat_file_name_attempted_ = false;
388 bool cached_oat_file_name_found_;
389 std::string cached_oat_file_name_;
390
Richard Uhlerb361d942015-05-07 10:52:28 -0700391 // Cached value of the loaded oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800392 // Use the GetOatFile method rather than accessing this directly, unless you
Richard Uhlerb361d942015-05-07 10:52:28 -0700393 // know the oat file isn't out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800394 bool oat_file_load_attempted_ = false;
395 std::unique_ptr<OatFile> cached_oat_file_;
396
397 // Cached results for OatFileIsOutOfDate
398 bool oat_file_is_out_of_date_attempted_ = false;
399 bool cached_oat_file_is_out_of_date_;
400
401 // Cached results for OatFileIsUpToDate
402 bool oat_file_is_up_to_date_attempted_ = false;
403 bool cached_oat_file_is_up_to_date_;
404
405 // Cached value of the image info.
406 // Use the GetImageInfo method rather than accessing these directly.
407 // TODO: The image info should probably be moved out of the oat file
408 // assistant to an image file manager.
409 bool image_info_load_attempted_ = false;
410 bool image_info_load_succeeded_ = false;
411 ImageInfo cached_image_info_;
412
Richard Uhler66d874d2015-01-15 09:37:19 -0800413 // For debugging only.
414 // If this flag is set, the oat or odex file has been released to the user
415 // of the OatFileAssistant object and the OatFileAssistant object is in a
416 // bad state and should no longer be used.
417 bool oat_file_released_ = false;
418
419 DISALLOW_COPY_AND_ASSIGN(OatFileAssistant);
420};
421
422} // namespace art
423
424#endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_