blob: 958b44048d316d1959c5408a9b89780bf924c806 [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
33// Class for assisting with oat file management.
34//
35// This class collects common utilities for determining the status of an oat
36// file on the device, updating the oat file, and loading the oat file.
37//
38// The oat file assistant is intended to be used with dex locations not on the
39// boot class path. See the IsInBootClassPath method for a way to check if the
40// dex location is in the boot class path.
41//
42// TODO: All the profiling related code is old and untested. It should either
43// be restored and tested, or removed.
44class OatFileAssistant {
45 public:
46 enum Status {
47 // kOutOfDate - An oat file is said to be out of date if the file does not
48 // exist, or is out of date with respect to the dex file or boot image.
49 kOutOfDate,
50
51 // kNeedsRelocation - An oat file is said to need relocation if the code
52 // is up to date, but not yet properly relocated for address space layout
53 // randomization (ASLR). In this case, the oat file is neither "out of
54 // date" nor "up to date".
55 kNeedsRelocation,
56
57 // kUpToDate - An oat file is said to be up to date if it is not out of
58 // date and has been properly relocated for the purposes of ASLR.
59 kUpToDate,
60 };
61
62 // Constructs an OatFileAssistant object to assist the oat file
63 // corresponding to the given dex location with the target instruction set.
64 //
65 // The dex_location must not be NULL and should remain available and
66 // unchanged for the duration of the lifetime of the OatFileAssistant object.
67 // Typically the dex_location is the absolute path to the original,
68 // un-optimized dex file.
69 //
70 //
71 // Note: Currently the dex_location must have an extension.
72 // TODO: Relax this restriction?
73 //
74 // The isa should be either the 32 bit or 64 bit variant for the current
75 // device. For example, on an arm device, use arm or arm64. An oat file can
76 // be loaded executable only if the ISA matches the current runtime.
77 OatFileAssistant(const char* dex_location, const InstructionSet isa,
78 bool load_executable);
79
80 // Constructs an OatFileAssistant, providing an explicit target oat_location
81 // to use instead of the standard oat location.
82 OatFileAssistant(const char* dex_location, const char* oat_location,
83 const InstructionSet isa, bool load_executable);
84
85 // Constructs an OatFileAssistant, providing an additional package_name used
86 // solely for the purpose of locating profile files.
87 //
88 // TODO: Why is the name of the profile file based on the package name and
89 // not the dex location? If there is no technical reason the dex_location
90 // can't be used, we should prefer that instead.
91 OatFileAssistant(const char* dex_location, const InstructionSet isa,
92 bool load_executable, const char* package_name);
93
94 // Constructs an OatFileAssistant with user specified oat location and a
95 // package name.
96 OatFileAssistant(const char* dex_location, const char* oat_location,
97 const InstructionSet isa, bool load_executable,
98 const char* package_name);
99
100 ~OatFileAssistant();
101
102 // Returns true if the dex location refers to an element of the boot class
103 // path.
104 bool IsInBootClassPath();
105
106 // Obtains a lock on the target oat file.
107 // Only one OatFileAssistant object can hold the lock for a target oat file
108 // at a time. The Lock is released automatically when the OatFileAssistant
109 // object goes out of scope. The Lock() method must not be called if the
110 // lock has already been acquired.
111 //
112 // Returns true on success.
113 // Returns false on error, in which case error_msg will contain more
114 // information on the error.
115 //
116 // The 'error_msg' argument must not be null.
117 //
118 // This is intended to be used to avoid race conditions when multiple
119 // processes generate oat files, such as when a foreground Activity and
120 // a background Service both use DexClassLoaders pointing to the same dex
121 // file.
122 bool Lock(std::string* error_msg);
123
124 // Returns the overall compilation status for the given dex location.
125 Status GetStatus();
126
127 // Attempts to generate or relocate the oat file as needed to make it up to
128 // date.
129 // Returns true on success.
130 //
131 // If there is a failure, the value of error_msg will be set to a string
132 // describing why there was failure. error_msg must not be nullptr.
133 bool MakeUpToDate(std::string* error_msg);
134
135 // Returns an oat file that can be used for loading dex files.
136 // Returns nullptr if no suitable oat file was found.
137 //
138 // After this call, no other methods of the OatFileAssistant should be
139 // called, because access to the loaded oat file has been taken away from
140 // the OatFileAssistant object.
141 std::unique_ptr<OatFile> GetBestOatFile();
142
143 // Loads the dex files in the given oat file for the given dex location.
144 // The oat file should be up to date for the given dex location.
145 // This loads multiple dex files in the case of multidex.
146 // Returns an empty vector if no dex files for that location could be loaded
147 // from the oat file.
148 //
149 // The caller is responsible for freeing the dex_files returned, if any. The
150 // dex_files will only remain valid as long as the oat_file is valid.
151 static std::vector<std::unique_ptr<const DexFile>> LoadDexFiles(
152 const OatFile& oat_file, const char* dex_location);
153
154 // If the dex file has been pre-compiled on the host, the compiled oat file
155 // will have the extension .odex, and is referred to as the odex file.
156 // It is called odex for legacy reasons; the file is really an oat file. The
157 // odex file will typically have a patch delta of 0 and need to be relocated
158 // before use for the purposes of ASLR.
159 // These methods return the location and status of the odex file for the dex
160 // location.
161 // Notes:
162 // * OdexFileName may return null if the odex file name could not be
163 // determined.
164 const std::string* OdexFileName();
165 bool OdexFileExists();
166 Status OdexFileStatus();
167 bool OdexFileIsOutOfDate();
168 bool OdexFileNeedsRelocation();
169 bool OdexFileIsUpToDate();
170
171 // When the dex files is compiled on the target device, the oat file is the
172 // result. The oat file will have been relocated to some
173 // (possibly-out-of-date) offset for ASLR.
174 // These methods return the location and status of the target oat file for
175 // the dex location.
176 //
177 // Notes:
178 // * To get the overall status of the compiled code for this dex_location,
179 // use the GetStatus() method, not the OatFileStatus() method.
180 // * OatFileName may return null if the oat file name could not be
181 // determined.
182 const std::string* OatFileName();
183 bool OatFileExists();
184 Status OatFileStatus();
185 bool OatFileIsOutOfDate();
186 bool OatFileNeedsRelocation();
187 bool OatFileIsUpToDate();
188
189 // These methods return the status for a given opened oat file with respect
190 // to the dex location.
191 Status GivenOatFileStatus(const OatFile& file);
192 bool GivenOatFileIsOutOfDate(const OatFile& file);
193 bool GivenOatFileNeedsRelocation(const OatFile& file);
194 bool GivenOatFileIsUpToDate(const OatFile& file);
195
196 // Returns true if there is an accessible profile associated with the dex
197 // location.
198 // This returns false if profiling is disabled.
199 bool ProfileExists();
200
201 // The old profile is a file containing a previous snapshot of profiling
202 // information associated with the dex file code. This is used to track how
203 // the profiling information has changed over time.
204 //
205 // Returns true if there is an accessible old profile associated with the
206 // dex location.
207 // This returns false if profiling is disabled.
208 bool OldProfileExists();
209
210 // Returns true if there has been a significant change between the old
211 // profile and the current profile.
212 // This returns false if profiling is disabled.
213 bool IsProfileChangeSignificant();
214
215 // Copy the current profile to the old profile location.
216 void CopyProfileFile();
217
218 // Generates the oat file by relocation from the odex file.
219 // This does not check the current status before attempting to relocate the
220 // oat file.
221 // Returns true on success.
222 // This will fail if dex2oat is not enabled in the current runtime.
223 //
224 // If there is a failure, the value of error_msg will be set to a string
225 // describing why there was failure. error_msg must not be nullptr.
226 bool RelocateOatFile(std::string* error_msg);
227
228 // Generate the oat file from the dex file.
229 // This does not check the current status before attempting to generate the
230 // oat file.
231 // Returns true on success.
232 // This will fail if dex2oat is not enabled in the current runtime.
233 //
234 // If there is a failure, the value of error_msg will be set to a string
235 // describing why there was failure. error_msg must not be nullptr.
236 bool GenerateOatFile(std::string* error_msg);
237
238 // Executes dex2oat using the current runtime configuration overridden with
239 // the given arguments. This does not check to see if dex2oat is enabled in
240 // the runtime configuration.
241 // Returns true on success.
242 //
243 // If there is a failure, the value of error_msg will be set to a string
244 // describing why there was failure. error_msg must not be nullptr.
245 //
246 // TODO: The OatFileAssistant probably isn't the right place to have this
247 // function.
248 static bool Dex2Oat(const std::vector<std::string>& args, std::string* error_msg);
249
250 // Constructs the odex file name for the given dex location.
251 // Returns true on success, in which case odex_filename is set to the odex
252 // file name.
253 // Returns false on error, in which case error_msg describes the error.
254 // Neither odex_filename nor error_msg may be null.
255 static bool DexFilenameToOdexFilename(const std::string& location,
256 InstructionSet isa, std::string* odex_filename, std::string* error_msg);
257
258 private:
259 struct ImageInfo {
260 uint32_t oat_checksum = 0;
261 uintptr_t oat_data_begin = 0;
262 int32_t patch_delta = 0;
263 std::string location;
264 };
265
266 // Returns the path to the dalvik cache directory.
267 // Does not check existence of the cache or try to create it.
268 // Includes the trailing slash.
269 // Returns an empty string if we can't get the dalvik cache directory path.
270 std::string DalvikCacheDirectory();
271
272 // Constructs the filename for the profile file.
273 // Returns an empty string if we do not have the necessary information to
274 // construct the filename.
275 std::string ProfileFileName();
276
277 // Constructs the filename for the old profile file.
278 // Returns an empty string if we do not have the necessary information to
279 // construct the filename.
280 std::string OldProfileFileName();
281
282 // Returns the current image location.
283 // Returns an empty string if the image location could not be retrieved.
284 //
285 // TODO: This method should belong with an image file manager, not
286 // the oat file assistant.
287 static std::string ImageLocation();
288
289 // Gets the dex checksum required for an up-to-date oat file.
290 // Returns dex_checksum if a required checksum was located. Returns
291 // nullptr if the required checksum was not found.
292 // The caller shouldn't clean up or free the returned pointer.
293 const uint32_t* GetRequiredDexChecksum();
294
295 // Returns the loaded odex file.
296 // Loads the file if needed. Returns nullptr if the file failed to load.
297 // The caller shouldn't clean up or free the returned pointer.
298 const OatFile* GetOdexFile();
299
300 // Clear any cached information about the odex file that depends on the
301 // contents of the file.
302 void ClearOdexFileCache();
303
304 // Returns the loaded oat file.
305 // Loads the file if needed. Returns nullptr if the file failed to load.
306 // The caller shouldn't clean up or free the returned pointer.
307 const OatFile* GetOatFile();
308
309 // Clear any cached information about the oat file that depends on the
310 // contents of the file.
311 void ClearOatFileCache();
312
313 // Returns the loaded image info.
314 // Loads the image info if needed. Returns nullptr if the image info failed
315 // to load.
316 // The caller shouldn't clean up or free the returned pointer.
317 const ImageInfo* GetImageInfo();
318
319 // Returns the loaded profile.
320 // Loads the profile if needed. Returns nullptr if the profile failed
321 // to load.
322 // The caller shouldn't clean up or free the returned pointer.
323 ProfileFile* GetProfile();
324
325 // Returns the loaded old profile.
326 // Loads the old profile if needed. Returns nullptr if the old profile
327 // failed to load.
328 // The caller shouldn't clean up or free the returned pointer.
329 ProfileFile* GetOldProfile();
330
331 // To implement Lock(), we lock a dummy file where the oat file would go
332 // (adding ".flock" to the target file name) and retain the lock for the
333 // remaining lifetime of the OatFileAssistant object.
334 std::unique_ptr<File> lock_file_;
335 ScopedFlock flock_;
336
337 // In a properly constructed OatFileAssistant object, dex_location_ should
338 // never be nullptr.
339 const char* dex_location_ = nullptr;
340
341 // In a properly constructed OatFileAssistant object, isa_ should be either
342 // the 32 or 64 bit variant for the current device.
343 const InstructionSet isa_ = kNone;
344
345 // The package name, used solely to find the profile file.
346 // This may be nullptr in a properly constructed object. In this case,
347 // profile_load_attempted_ and old_profile_load_attempted_ will be true, and
348 // profile_load_succeeded_ and old_profile_load_succeeded_ will be false.
349 const char* package_name_ = nullptr;
350
351 // Whether we will attempt to load oat files executable.
352 bool load_executable_ = false;
353
354 // Cached value of the required dex checksum.
355 // This should be accessed only by the GetRequiredDexChecksum() method.
356 uint32_t cached_required_dex_checksum;
357 bool required_dex_checksum_attempted = false;
358 bool required_dex_checksum_found;
359
360 // Cached value of the odex file name.
361 // This should be accessed only by the OdexFileName() method.
362 bool cached_odex_file_name_attempted_ = false;
363 bool cached_odex_file_name_found_;
364 std::string cached_odex_file_name_;
365
366 // Cached value of the loaded odex file.
367 // Use the GetOdexFile method rather than accessing this directly, unless you
368 // know the odex file isn't out of date.
369 bool odex_file_load_attempted_ = false;
370 std::unique_ptr<OatFile> cached_odex_file_;
371
372 // Cached results for OdexFileIsOutOfDate
373 bool odex_file_is_out_of_date_attempted_ = false;
374 bool cached_odex_file_is_out_of_date_;
375
376 // Cached results for OdexFileIsUpToDate
377 bool odex_file_is_up_to_date_attempted_ = false;
378 bool cached_odex_file_is_up_to_date_;
379
380 // Cached value of the oat file name.
381 // This should be accessed only by the OatFileName() method.
382 bool cached_oat_file_name_attempted_ = false;
383 bool cached_oat_file_name_found_;
384 std::string cached_oat_file_name_;
385
386 // Cached value of the loaded odex file.
387 // Use the GetOatFile method rather than accessing this directly, unless you
388 // know the odex file isn't out of date.
389 bool oat_file_load_attempted_ = false;
390 std::unique_ptr<OatFile> cached_oat_file_;
391
392 // Cached results for OatFileIsOutOfDate
393 bool oat_file_is_out_of_date_attempted_ = false;
394 bool cached_oat_file_is_out_of_date_;
395
396 // Cached results for OatFileIsUpToDate
397 bool oat_file_is_up_to_date_attempted_ = false;
398 bool cached_oat_file_is_up_to_date_;
399
400 // Cached value of the image info.
401 // Use the GetImageInfo method rather than accessing these directly.
402 // TODO: The image info should probably be moved out of the oat file
403 // assistant to an image file manager.
404 bool image_info_load_attempted_ = false;
405 bool image_info_load_succeeded_ = false;
406 ImageInfo cached_image_info_;
407
408 // Cached value of the profile file.
409 // Use the GetProfile method rather than accessing these directly.
410 bool profile_load_attempted_ = false;
411 bool profile_load_succeeded_ = false;
412 ProfileFile cached_profile_;
413
414 // Cached value of the profile file.
415 // Use the GetOldProfile method rather than accessing these directly.
416 bool old_profile_load_attempted_ = false;
417 bool old_profile_load_succeeded_ = false;
418 ProfileFile cached_old_profile_;
419
420 // For debugging only.
421 // If this flag is set, the oat or odex file has been released to the user
422 // of the OatFileAssistant object and the OatFileAssistant object is in a
423 // bad state and should no longer be used.
424 bool oat_file_released_ = false;
425
426 DISALLOW_COPY_AND_ASSIGN(OatFileAssistant);
427};
428
429} // namespace art
430
431#endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_