blob: 7b45bca94676727329703f5e27139b396bd22244 [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.
47//
48// TODO: All the profiling related code is old and untested. It should either
49// be restored and tested, or removed.
50class OatFileAssistant {
51 public:
Richard Uhler95abd042015-03-24 09:51:28 -070052 enum DexOptNeeded {
53 // kNoDexOptNeeded - The code for this dex location is up to date and can
54 // be used as is.
55 // Matches Java: dalvik.system.DexFile.NO_DEXOPT_NEEDED = 0
56 kNoDexOptNeeded = 0,
Richard Uhler66d874d2015-01-15 09:37:19 -080057
Richard Uhler95abd042015-03-24 09:51:28 -070058 // kDex2OatNeeded - In order to make the code for this dex location up to
59 // date, dex2oat must be run on the dex file.
60 // Matches Java: dalvik.system.DexFile.DEX2OAT_NEEDED = 1
61 kDex2OatNeeded = 1,
Richard Uhler66d874d2015-01-15 09:37:19 -080062
Richard Uhler95abd042015-03-24 09:51:28 -070063 // kPatchOatNeeded - In order to make the code for this dex location up to
64 // date, patchoat must be run on the odex file.
65 // Matches Java: dalvik.system.DexFile.PATCHOAT_NEEDED = 2
66 kPatchOatNeeded = 2,
67
68 // kSelfPatchOatNeeded - In order to make the code for this dex location
69 // up to date, patchoat must be run on the oat file.
70 // Matches Java: dalvik.system.DexFile.SELF_PATCHOAT_NEEDED = 3
71 kSelfPatchOatNeeded = 3,
72 };
73
74 enum OatStatus {
75 // kOatOutOfDate - An oat file is said to be out of date if the file does
76 // not exist, or is out of date with respect to the dex file or boot
77 // image.
78 kOatOutOfDate,
79
80 // kOatNeedsRelocation - An oat file is said to need relocation if the
81 // code is up to date, but not yet properly relocated for address space
82 // layout randomization (ASLR). In this case, the oat file is neither
83 // "out of date" nor "up to date".
84 kOatNeedsRelocation,
85
86 // kOatUpToDate - An oat file is said to be up to date if it is not out of
Richard Uhler66d874d2015-01-15 09:37:19 -080087 // date and has been properly relocated for the purposes of ASLR.
Richard Uhler95abd042015-03-24 09:51:28 -070088 kOatUpToDate,
Richard Uhler66d874d2015-01-15 09:37:19 -080089 };
90
91 // Constructs an OatFileAssistant object to assist the oat file
92 // corresponding to the given dex location with the target instruction set.
93 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 // The dex_location must not be null and should remain available and
Richard Uhler66d874d2015-01-15 09:37:19 -080095 // unchanged for the duration of the lifetime of the OatFileAssistant object.
96 // Typically the dex_location is the absolute path to the original,
97 // un-optimized dex file.
98 //
Richard Uhler66d874d2015-01-15 09:37:19 -080099 // Note: Currently the dex_location must have an extension.
100 // TODO: Relax this restriction?
101 //
102 // The isa should be either the 32 bit or 64 bit variant for the current
103 // device. For example, on an arm device, use arm or arm64. An oat file can
104 // be loaded executable only if the ISA matches the current runtime.
105 OatFileAssistant(const char* dex_location, const InstructionSet isa,
106 bool load_executable);
107
108 // Constructs an OatFileAssistant, providing an explicit target oat_location
109 // to use instead of the standard oat location.
110 OatFileAssistant(const char* dex_location, const char* oat_location,
111 const InstructionSet isa, bool load_executable);
112
113 // Constructs an OatFileAssistant, providing an additional package_name used
114 // solely for the purpose of locating profile files.
115 //
116 // TODO: Why is the name of the profile file based on the package name and
117 // not the dex location? If there is no technical reason the dex_location
118 // can't be used, we should prefer that instead.
119 OatFileAssistant(const char* dex_location, const InstructionSet isa,
120 bool load_executable, const char* package_name);
121
122 // Constructs an OatFileAssistant with user specified oat location and a
123 // package name.
124 OatFileAssistant(const char* dex_location, const char* oat_location,
125 const InstructionSet isa, bool load_executable,
126 const char* package_name);
127
128 ~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
153 // dex location.
154 DexOptNeeded GetDexOptNeeded();
Richard Uhler66d874d2015-01-15 09:37:19 -0800155
156 // Attempts to generate or relocate the oat file as needed to make it up to
157 // date.
158 // Returns true on success.
159 //
160 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700161 // describing why there was failure. error_msg must not be null.
Richard Uhler66d874d2015-01-15 09:37:19 -0800162 bool MakeUpToDate(std::string* error_msg);
163
164 // Returns an oat file that can be used for loading dex files.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700165 // Returns null if no suitable oat file was found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800166 //
167 // After this call, no other methods of the OatFileAssistant should be
168 // called, because access to the loaded oat file has been taken away from
169 // the OatFileAssistant object.
170 std::unique_ptr<OatFile> GetBestOatFile();
171
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800172 // Open and returns an image space associated with the oat file.
173 gc::space::ImageSpace* OpenImageSpace(const OatFile* oat_file);
174
Richard Uhler66d874d2015-01-15 09:37:19 -0800175 // Loads the dex files in the given oat file for the given dex location.
176 // The oat file should be up to date for the given dex location.
177 // This loads multiple dex files in the case of multidex.
178 // Returns an empty vector if no dex files for that location could be loaded
179 // from the oat file.
180 //
181 // The caller is responsible for freeing the dex_files returned, if any. The
182 // dex_files will only remain valid as long as the oat_file is valid.
183 static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(
184 const OatFile& oat_file, const char* dex_location);
185
Richard Uhler9b994ea2015-06-24 08:44:19 -0700186 // Returns true if there are dex files in the original dex location that can
187 // be compiled with dex2oat for this dex location.
188 // Returns false if there is no original dex file, or if the original dex
189 // file is an apk/zip without a classes.dex entry.
190 bool HasOriginalDexFiles();
191
Richard Uhler63434112015-03-16 14:32:16 -0700192 // If the dex file has been installed with a compiled oat file alongside
193 // it, the compiled oat file will have the extension .odex, and is referred
194 // to as the odex file. It is called odex for legacy reasons; the file is
195 // really an oat file. The odex file will often, but not always, have a
196 // patch delta of 0 and need to be relocated before use for the purposes of
197 // ASLR. The odex file is treated as if it were read-only.
Richard Uhler66d874d2015-01-15 09:37:19 -0800198 // These methods return the location and status of the odex file for the dex
199 // location.
200 // Notes:
201 // * OdexFileName may return null if the odex file name could not be
202 // determined.
203 const std::string* OdexFileName();
204 bool OdexFileExists();
Richard Uhler95abd042015-03-24 09:51:28 -0700205 OatStatus OdexFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800206 bool OdexFileIsOutOfDate();
207 bool OdexFileNeedsRelocation();
208 bool OdexFileIsUpToDate();
209
210 // When the dex files is compiled on the target device, the oat file is the
211 // result. The oat file will have been relocated to some
212 // (possibly-out-of-date) offset for ASLR.
213 // These methods return the location and status of the target oat file for
214 // the dex location.
215 //
216 // Notes:
Richard Uhler66d874d2015-01-15 09:37:19 -0800217 // * OatFileName may return null if the oat file name could not be
218 // determined.
219 const std::string* OatFileName();
220 bool OatFileExists();
Richard Uhler95abd042015-03-24 09:51:28 -0700221 OatStatus OatFileStatus();
Richard Uhler66d874d2015-01-15 09:37:19 -0800222 bool OatFileIsOutOfDate();
223 bool OatFileNeedsRelocation();
224 bool OatFileIsUpToDate();
225
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800226 // Return image file name. Does not cache since it relies on the oat file.
227 std::string ArtFileName(const OatFile* oat_file) const;
228
Richard Uhler66d874d2015-01-15 09:37:19 -0800229 // These methods return the status for a given opened oat file with respect
230 // to the dex location.
Richard Uhler95abd042015-03-24 09:51:28 -0700231 OatStatus GivenOatFileStatus(const OatFile& file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800232 bool GivenOatFileIsOutOfDate(const OatFile& file);
233 bool GivenOatFileNeedsRelocation(const OatFile& file);
234 bool GivenOatFileIsUpToDate(const OatFile& file);
235
236 // Returns true if there is an accessible profile associated with the dex
237 // location.
238 // This returns false if profiling is disabled.
239 bool ProfileExists();
240
241 // The old profile is a file containing a previous snapshot of profiling
242 // information associated with the dex file code. This is used to track how
243 // the profiling information has changed over time.
244 //
245 // Returns true if there is an accessible old profile associated with the
246 // dex location.
247 // This returns false if profiling is disabled.
248 bool OldProfileExists();
249
250 // Returns true if there has been a significant change between the old
251 // profile and the current profile.
252 // This returns false if profiling is disabled.
253 bool IsProfileChangeSignificant();
254
255 // Copy the current profile to the old profile location.
256 void CopyProfileFile();
257
Richard Uhler95abd042015-03-24 09:51:28 -0700258 // Generates the oat file by relocation from the named input file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800259 // This does not check the current status before attempting to relocate the
260 // oat file.
261 // Returns true on success.
262 // This will fail if dex2oat is not enabled in the current runtime.
263 //
264 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700265 // describing why there was failure. error_msg must not be null.
Richard Uhler95abd042015-03-24 09:51:28 -0700266 bool RelocateOatFile(const std::string* input_file, std::string* error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800267
268 // Generate the oat file from the dex file.
269 // This does not check the current status before attempting to generate the
270 // oat file.
271 // Returns true on success.
272 // This will fail if dex2oat is not enabled in the current runtime.
273 //
274 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700275 // describing why there was failure. error_msg must not be null.
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 bool GenerateOatFile(std::string* error_msg);
277
278 // Executes dex2oat using the current runtime configuration overridden with
279 // the given arguments. This does not check to see if dex2oat is enabled in
280 // the runtime configuration.
281 // Returns true on success.
282 //
283 // If there is a failure, the value of error_msg will be set to a string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700284 // describing why there was failure. error_msg must not be null.
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 //
286 // TODO: The OatFileAssistant probably isn't the right place to have this
287 // function.
288 static bool Dex2Oat(const std::vector<std::string>& args, std::string* error_msg);
289
290 // Constructs the odex file name for the given dex location.
291 // Returns true on success, in which case odex_filename is set to the odex
292 // file name.
293 // Returns false on error, in which case error_msg describes the error.
294 // Neither odex_filename nor error_msg may be null.
295 static bool DexFilenameToOdexFilename(const std::string& location,
296 InstructionSet isa, std::string* odex_filename, std::string* error_msg);
297
298 private:
299 struct ImageInfo {
300 uint32_t oat_checksum = 0;
301 uintptr_t oat_data_begin = 0;
302 int32_t patch_delta = 0;
303 std::string location;
304 };
305
306 // Returns the path to the dalvik cache directory.
307 // Does not check existence of the cache or try to create it.
308 // Includes the trailing slash.
309 // Returns an empty string if we can't get the dalvik cache directory path.
310 std::string DalvikCacheDirectory();
311
312 // Constructs the filename for the profile file.
313 // Returns an empty string if we do not have the necessary information to
314 // construct the filename.
315 std::string ProfileFileName();
316
317 // Constructs the filename for the old profile file.
318 // Returns an empty string if we do not have the necessary information to
319 // construct the filename.
320 std::string OldProfileFileName();
321
322 // Returns the current image location.
323 // Returns an empty string if the image location could not be retrieved.
324 //
325 // TODO: This method should belong with an image file manager, not
326 // the oat file assistant.
327 static std::string ImageLocation();
328
329 // Gets the dex checksum required for an up-to-date oat file.
330 // Returns dex_checksum if a required checksum was located. Returns
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700331 // null if the required checksum was not found.
Richard Uhler66d874d2015-01-15 09:37:19 -0800332 // The caller shouldn't clean up or free the returned pointer.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700333 // This sets the has_original_dex_files_ field to true if a checksum was
334 // found for the dex_location_ dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800335 const uint32_t* GetRequiredDexChecksum();
336
337 // Returns the loaded odex file.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700338 // Loads the file if needed. Returns null if the file failed to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800339 // The caller shouldn't clean up or free the returned pointer.
340 const OatFile* GetOdexFile();
341
Richard Uhler5f946da2015-07-17 12:28:32 -0700342 // Returns true if the odex file is opened executable.
343 bool OdexFileIsExecutable();
344
Richard Uhler66d874d2015-01-15 09:37:19 -0800345 // Clear any cached information about the odex file that depends on the
346 // contents of the file.
347 void ClearOdexFileCache();
348
349 // Returns the loaded oat file.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700350 // Loads the file if needed. Returns null if the file failed to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800351 // The caller shouldn't clean up or free the returned pointer.
352 const OatFile* GetOatFile();
353
Richard Uhler5f946da2015-07-17 12:28:32 -0700354 // Returns true if the oat file is opened executable.
355 bool OatFileIsExecutable();
356
Richard Uhler66d874d2015-01-15 09:37:19 -0800357 // Clear any cached information about the oat file that depends on the
358 // contents of the file.
359 void ClearOatFileCache();
360
361 // Returns the loaded image info.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700362 // Loads the image info if needed. Returns null if the image info failed
Richard Uhler66d874d2015-01-15 09:37:19 -0800363 // to load.
364 // The caller shouldn't clean up or free the returned pointer.
365 const ImageInfo* GetImageInfo();
366
367 // Returns the loaded profile.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700368 // Loads the profile if needed. Returns null if the profile failed
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 // to load.
370 // The caller shouldn't clean up or free the returned pointer.
371 ProfileFile* GetProfile();
372
373 // Returns the loaded old profile.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700374 // Loads the old profile if needed. Returns null if the old profile
Richard Uhler66d874d2015-01-15 09:37:19 -0800375 // failed to load.
376 // The caller shouldn't clean up or free the returned pointer.
377 ProfileFile* GetOldProfile();
378
379 // To implement Lock(), we lock a dummy file where the oat file would go
380 // (adding ".flock" to the target file name) and retain the lock for the
381 // remaining lifetime of the OatFileAssistant object.
Richard Uhler66d874d2015-01-15 09:37:19 -0800382 ScopedFlock flock_;
383
Richard Uhler740eec92015-10-15 15:12:23 -0700384 std::string dex_location_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800385
386 // In a properly constructed OatFileAssistant object, isa_ should be either
387 // the 32 or 64 bit variant for the current device.
388 const InstructionSet isa_ = kNone;
389
390 // The package name, used solely to find the profile file.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700391 // This may be null in a properly constructed object. In this case,
Richard Uhler66d874d2015-01-15 09:37:19 -0800392 // profile_load_attempted_ and old_profile_load_attempted_ will be true, and
393 // profile_load_succeeded_ and old_profile_load_succeeded_ will be false.
394 const char* package_name_ = nullptr;
395
396 // Whether we will attempt to load oat files executable.
397 bool load_executable_ = false;
398
399 // Cached value of the required dex checksum.
400 // This should be accessed only by the GetRequiredDexChecksum() method.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700401 uint32_t cached_required_dex_checksum_;
402 bool required_dex_checksum_attempted_ = false;
403 bool required_dex_checksum_found_;
404 bool has_original_dex_files_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800405
406 // Cached value of the odex file name.
407 // This should be accessed only by the OdexFileName() method.
408 bool cached_odex_file_name_attempted_ = false;
409 bool cached_odex_file_name_found_;
410 std::string cached_odex_file_name_;
411
412 // Cached value of the loaded odex file.
413 // Use the GetOdexFile method rather than accessing this directly, unless you
414 // know the odex file isn't out of date.
415 bool odex_file_load_attempted_ = false;
416 std::unique_ptr<OatFile> cached_odex_file_;
417
418 // Cached results for OdexFileIsOutOfDate
419 bool odex_file_is_out_of_date_attempted_ = false;
420 bool cached_odex_file_is_out_of_date_;
421
422 // Cached results for OdexFileIsUpToDate
423 bool odex_file_is_up_to_date_attempted_ = false;
424 bool cached_odex_file_is_up_to_date_;
425
426 // Cached value of the oat file name.
427 // This should be accessed only by the OatFileName() method.
428 bool cached_oat_file_name_attempted_ = false;
429 bool cached_oat_file_name_found_;
430 std::string cached_oat_file_name_;
431
Richard Uhlerb361d942015-05-07 10:52:28 -0700432 // Cached value of the loaded oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800433 // Use the GetOatFile method rather than accessing this directly, unless you
Richard Uhlerb361d942015-05-07 10:52:28 -0700434 // know the oat file isn't out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800435 bool oat_file_load_attempted_ = false;
436 std::unique_ptr<OatFile> cached_oat_file_;
437
438 // Cached results for OatFileIsOutOfDate
439 bool oat_file_is_out_of_date_attempted_ = false;
440 bool cached_oat_file_is_out_of_date_;
441
442 // Cached results for OatFileIsUpToDate
443 bool oat_file_is_up_to_date_attempted_ = false;
444 bool cached_oat_file_is_up_to_date_;
445
446 // Cached value of the image info.
447 // Use the GetImageInfo method rather than accessing these directly.
448 // TODO: The image info should probably be moved out of the oat file
449 // assistant to an image file manager.
450 bool image_info_load_attempted_ = false;
451 bool image_info_load_succeeded_ = false;
452 ImageInfo cached_image_info_;
453
454 // Cached value of the profile file.
455 // Use the GetProfile method rather than accessing these directly.
456 bool profile_load_attempted_ = false;
457 bool profile_load_succeeded_ = false;
458 ProfileFile cached_profile_;
459
460 // Cached value of the profile file.
461 // Use the GetOldProfile method rather than accessing these directly.
462 bool old_profile_load_attempted_ = false;
463 bool old_profile_load_succeeded_ = false;
464 ProfileFile cached_old_profile_;
465
466 // For debugging only.
467 // If this flag is set, the oat or odex file has been released to the user
468 // of the OatFileAssistant object and the OatFileAssistant object is in a
469 // bad state and should no longer be used.
470 bool oat_file_released_ = false;
471
472 DISALLOW_COPY_AND_ASSIGN(OatFileAssistant);
473};
474
475} // namespace art
476
477#endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_