blob: f2abcf99d386ede84db7fa31bc3305bbce3eb4cd [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
Richard Uhler63434112015-03-16 14:32:16 -0700154 // If the dex file has been installed with a compiled oat file alongside
155 // it, the compiled oat file will have the extension .odex, and is referred
156 // to as the odex file. It is called odex for legacy reasons; the file is
157 // really an oat file. The odex file will often, but not always, have a
158 // patch delta of 0 and need to be relocated before use for the purposes of
159 // ASLR. The odex file is treated as if it were read-only.
Richard Uhler66d874d2015-01-15 09:37:19 -0800160 // These methods return the location and status of the odex file for the dex
161 // location.
162 // Notes:
163 // * OdexFileName may return null if the odex file name could not be
164 // determined.
165 const std::string* OdexFileName();
166 bool OdexFileExists();
167 Status OdexFileStatus();
168 bool OdexFileIsOutOfDate();
169 bool OdexFileNeedsRelocation();
170 bool OdexFileIsUpToDate();
171
172 // When the dex files is compiled on the target device, the oat file is the
173 // result. The oat file will have been relocated to some
174 // (possibly-out-of-date) offset for ASLR.
175 // These methods return the location and status of the target oat file for
176 // the dex location.
177 //
178 // Notes:
179 // * To get the overall status of the compiled code for this dex_location,
180 // use the GetStatus() method, not the OatFileStatus() method.
181 // * OatFileName may return null if the oat file name could not be
182 // determined.
183 const std::string* OatFileName();
184 bool OatFileExists();
185 Status OatFileStatus();
186 bool OatFileIsOutOfDate();
187 bool OatFileNeedsRelocation();
188 bool OatFileIsUpToDate();
189
190 // These methods return the status for a given opened oat file with respect
191 // to the dex location.
192 Status GivenOatFileStatus(const OatFile& file);
193 bool GivenOatFileIsOutOfDate(const OatFile& file);
194 bool GivenOatFileNeedsRelocation(const OatFile& file);
195 bool GivenOatFileIsUpToDate(const OatFile& file);
196
197 // Returns true if there is an accessible profile associated with the dex
198 // location.
199 // This returns false if profiling is disabled.
200 bool ProfileExists();
201
202 // The old profile is a file containing a previous snapshot of profiling
203 // information associated with the dex file code. This is used to track how
204 // the profiling information has changed over time.
205 //
206 // Returns true if there is an accessible old profile associated with the
207 // dex location.
208 // This returns false if profiling is disabled.
209 bool OldProfileExists();
210
211 // Returns true if there has been a significant change between the old
212 // profile and the current profile.
213 // This returns false if profiling is disabled.
214 bool IsProfileChangeSignificant();
215
216 // Copy the current profile to the old profile location.
217 void CopyProfileFile();
218
219 // Generates the oat file by relocation from the odex file.
220 // This does not check the current status before attempting to relocate the
221 // oat file.
222 // Returns true on success.
223 // This will fail if dex2oat is not enabled in the current runtime.
224 //
225 // If there is a failure, the value of error_msg will be set to a string
226 // describing why there was failure. error_msg must not be nullptr.
227 bool RelocateOatFile(std::string* error_msg);
228
229 // Generate the oat file from the dex file.
230 // This does not check the current status before attempting to generate the
231 // oat file.
232 // Returns true on success.
233 // This will fail if dex2oat is not enabled in the current runtime.
234 //
235 // If there is a failure, the value of error_msg will be set to a string
236 // describing why there was failure. error_msg must not be nullptr.
237 bool GenerateOatFile(std::string* error_msg);
238
239 // Executes dex2oat using the current runtime configuration overridden with
240 // the given arguments. This does not check to see if dex2oat is enabled in
241 // the runtime configuration.
242 // Returns true on success.
243 //
244 // If there is a failure, the value of error_msg will be set to a string
245 // describing why there was failure. error_msg must not be nullptr.
246 //
247 // TODO: The OatFileAssistant probably isn't the right place to have this
248 // function.
249 static bool Dex2Oat(const std::vector<std::string>& args, std::string* error_msg);
250
251 // Constructs the odex file name for the given dex location.
252 // Returns true on success, in which case odex_filename is set to the odex
253 // file name.
254 // Returns false on error, in which case error_msg describes the error.
255 // Neither odex_filename nor error_msg may be null.
256 static bool DexFilenameToOdexFilename(const std::string& location,
257 InstructionSet isa, std::string* odex_filename, std::string* error_msg);
258
259 private:
260 struct ImageInfo {
261 uint32_t oat_checksum = 0;
262 uintptr_t oat_data_begin = 0;
263 int32_t patch_delta = 0;
264 std::string location;
265 };
266
267 // Returns the path to the dalvik cache directory.
268 // Does not check existence of the cache or try to create it.
269 // Includes the trailing slash.
270 // Returns an empty string if we can't get the dalvik cache directory path.
271 std::string DalvikCacheDirectory();
272
273 // Constructs the filename for the profile file.
274 // Returns an empty string if we do not have the necessary information to
275 // construct the filename.
276 std::string ProfileFileName();
277
278 // Constructs the filename for the old profile file.
279 // Returns an empty string if we do not have the necessary information to
280 // construct the filename.
281 std::string OldProfileFileName();
282
283 // Returns the current image location.
284 // Returns an empty string if the image location could not be retrieved.
285 //
286 // TODO: This method should belong with an image file manager, not
287 // the oat file assistant.
288 static std::string ImageLocation();
289
290 // Gets the dex checksum required for an up-to-date oat file.
291 // Returns dex_checksum if a required checksum was located. Returns
292 // nullptr if the required checksum was not found.
293 // The caller shouldn't clean up or free the returned pointer.
294 const uint32_t* GetRequiredDexChecksum();
295
296 // Returns the loaded odex file.
297 // Loads the file if needed. Returns nullptr if the file failed to load.
298 // The caller shouldn't clean up or free the returned pointer.
299 const OatFile* GetOdexFile();
300
301 // Clear any cached information about the odex file that depends on the
302 // contents of the file.
303 void ClearOdexFileCache();
304
305 // Returns the loaded oat file.
306 // Loads the file if needed. Returns nullptr if the file failed to load.
307 // The caller shouldn't clean up or free the returned pointer.
308 const OatFile* GetOatFile();
309
310 // Clear any cached information about the oat file that depends on the
311 // contents of the file.
312 void ClearOatFileCache();
313
314 // Returns the loaded image info.
315 // Loads the image info if needed. Returns nullptr if the image info failed
316 // to load.
317 // The caller shouldn't clean up or free the returned pointer.
318 const ImageInfo* GetImageInfo();
319
320 // Returns the loaded profile.
321 // Loads the profile if needed. Returns nullptr if the profile failed
322 // to load.
323 // The caller shouldn't clean up or free the returned pointer.
324 ProfileFile* GetProfile();
325
326 // Returns the loaded old profile.
327 // Loads the old profile if needed. Returns nullptr if the old profile
328 // failed to load.
329 // The caller shouldn't clean up or free the returned pointer.
330 ProfileFile* GetOldProfile();
331
332 // To implement Lock(), we lock a dummy file where the oat file would go
333 // (adding ".flock" to the target file name) and retain the lock for the
334 // remaining lifetime of the OatFileAssistant object.
335 std::unique_ptr<File> lock_file_;
336 ScopedFlock flock_;
337
338 // In a properly constructed OatFileAssistant object, dex_location_ should
339 // never be nullptr.
340 const char* dex_location_ = nullptr;
341
342 // In a properly constructed OatFileAssistant object, isa_ should be either
343 // the 32 or 64 bit variant for the current device.
344 const InstructionSet isa_ = kNone;
345
346 // The package name, used solely to find the profile file.
347 // This may be nullptr in a properly constructed object. In this case,
348 // profile_load_attempted_ and old_profile_load_attempted_ will be true, and
349 // profile_load_succeeded_ and old_profile_load_succeeded_ will be false.
350 const char* package_name_ = nullptr;
351
352 // Whether we will attempt to load oat files executable.
353 bool load_executable_ = false;
354
355 // Cached value of the required dex checksum.
356 // This should be accessed only by the GetRequiredDexChecksum() method.
357 uint32_t cached_required_dex_checksum;
358 bool required_dex_checksum_attempted = false;
359 bool required_dex_checksum_found;
360
361 // Cached value of the odex file name.
362 // This should be accessed only by the OdexFileName() method.
363 bool cached_odex_file_name_attempted_ = false;
364 bool cached_odex_file_name_found_;
365 std::string cached_odex_file_name_;
366
367 // Cached value of the loaded odex file.
368 // Use the GetOdexFile method rather than accessing this directly, unless you
369 // know the odex file isn't out of date.
370 bool odex_file_load_attempted_ = false;
371 std::unique_ptr<OatFile> cached_odex_file_;
372
373 // Cached results for OdexFileIsOutOfDate
374 bool odex_file_is_out_of_date_attempted_ = false;
375 bool cached_odex_file_is_out_of_date_;
376
377 // Cached results for OdexFileIsUpToDate
378 bool odex_file_is_up_to_date_attempted_ = false;
379 bool cached_odex_file_is_up_to_date_;
380
381 // Cached value of the oat file name.
382 // This should be accessed only by the OatFileName() method.
383 bool cached_oat_file_name_attempted_ = false;
384 bool cached_oat_file_name_found_;
385 std::string cached_oat_file_name_;
386
387 // Cached value of the loaded odex file.
388 // Use the GetOatFile method rather than accessing this directly, unless you
389 // know the odex file isn't out of date.
390 bool oat_file_load_attempted_ = false;
391 std::unique_ptr<OatFile> cached_oat_file_;
392
393 // Cached results for OatFileIsOutOfDate
394 bool oat_file_is_out_of_date_attempted_ = false;
395 bool cached_oat_file_is_out_of_date_;
396
397 // Cached results for OatFileIsUpToDate
398 bool oat_file_is_up_to_date_attempted_ = false;
399 bool cached_oat_file_is_up_to_date_;
400
401 // Cached value of the image info.
402 // Use the GetImageInfo method rather than accessing these directly.
403 // TODO: The image info should probably be moved out of the oat file
404 // assistant to an image file manager.
405 bool image_info_load_attempted_ = false;
406 bool image_info_load_succeeded_ = false;
407 ImageInfo cached_image_info_;
408
409 // Cached value of the profile file.
410 // Use the GetProfile method rather than accessing these directly.
411 bool profile_load_attempted_ = false;
412 bool profile_load_succeeded_ = false;
413 ProfileFile cached_profile_;
414
415 // Cached value of the profile file.
416 // Use the GetOldProfile method rather than accessing these directly.
417 bool old_profile_load_attempted_ = false;
418 bool old_profile_load_succeeded_ = false;
419 ProfileFile cached_old_profile_;
420
421 // For debugging only.
422 // If this flag is set, the oat or odex file has been released to the user
423 // of the OatFileAssistant object and the OatFileAssistant object is in a
424 // bad state and should no longer be used.
425 bool oat_file_released_ = false;
426
427 DISALLOW_COPY_AND_ASSIGN(OatFileAssistant);
428};
429
430} // namespace art
431
432#endif // ART_RUNTIME_OAT_FILE_ASSISTANT_H_