blob: a8f84a2bfac06d37412ae10b1081c0ffbfd93da2 [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#include "oat_file_assistant.h"
18
19#include <fcntl.h>
20#ifdef __linux__
21#include <sys/sendfile.h>
22#else
23#include <sys/socket.h>
24#endif
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include <set>
30
31#include "base/logging.h"
32#include "base/stringprintf.h"
33#include "class_linker.h"
34#include "gc/heap.h"
35#include "gc/space/image_space.h"
36#include "image.h"
37#include "oat.h"
38#include "os.h"
39#include "profiler.h"
40#include "runtime.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080041#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080042#include "ScopedFd.h"
43#include "utils.h"
44
45namespace art {
46
47OatFileAssistant::OatFileAssistant(const char* dex_location,
48 const InstructionSet isa,
49 bool load_executable)
50 : OatFileAssistant(dex_location, nullptr, isa, load_executable, nullptr) { }
51
52OatFileAssistant::OatFileAssistant(const char* dex_location,
53 const char* oat_location,
54 const InstructionSet isa,
55 bool load_executable)
56 : OatFileAssistant(dex_location, oat_location, isa, load_executable, nullptr) { }
57
58OatFileAssistant::OatFileAssistant(const char* dex_location,
59 const InstructionSet isa,
60 bool load_executable,
61 const char* package_name)
62 : OatFileAssistant(dex_location, nullptr, isa, load_executable, package_name) { }
63
64OatFileAssistant::OatFileAssistant(const char* dex_location,
65 const char* oat_location,
66 const InstructionSet isa,
67 bool load_executable,
68 const char* package_name)
Richard Uhler740eec92015-10-15 15:12:23 -070069 : isa_(isa), package_name_(package_name), load_executable_(load_executable) {
70 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
71 dex_location_.assign(dex_location);
72
Richard Uhler66d874d2015-01-15 09:37:19 -080073 if (load_executable_ && isa != kRuntimeISA) {
74 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
75 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
76 load_executable_ = false;
77 }
78
79 // If the user gave a target oat location, save that as the cached oat
80 // location now so we won't try to construct the default location later.
81 if (oat_location != nullptr) {
82 cached_oat_file_name_ = std::string(oat_location);
83 cached_oat_file_name_attempted_ = true;
84 cached_oat_file_name_found_ = true;
85 }
86
87 // If there is no package name given, we will not be able to find any
88 // profiles associated with this dex location. Preemptively mark that to
89 // be the case, rather than trying to find and load the profiles later.
90 // Similarly, if profiling is disabled.
91 if (package_name == nullptr
92 || !Runtime::Current()->GetProfilerOptions().IsEnabled()) {
93 profile_load_attempted_ = true;
94 profile_load_succeeded_ = false;
95 old_profile_load_attempted_ = true;
96 old_profile_load_succeeded_ = false;
97 }
98}
99
100OatFileAssistant::~OatFileAssistant() {
101 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700102 if (flock_.HasFile()) {
103 TEMP_FAILURE_RETRY(unlink(flock_.GetFile()->GetPath().c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800104 }
105}
106
107bool OatFileAssistant::IsInBootClassPath() {
108 // Note: We check the current boot class path, regardless of the ISA
109 // specified by the user. This is okay, because the boot class path should
110 // be the same for all ISAs.
111 // TODO: Can we verify the boot class path is the same for all ISAs?
112 Runtime* runtime = Runtime::Current();
113 ClassLinker* class_linker = runtime->GetClassLinker();
114 const auto& boot_class_path = class_linker->GetBootClassPath();
115 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700116 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800117 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
118 return true;
119 }
120 }
121 return false;
122}
123
124bool OatFileAssistant::Lock(std::string* error_msg) {
125 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700126 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800127
128 if (OatFileName() == nullptr) {
129 *error_msg = "Failed to determine lock file";
130 return false;
131 }
132 std::string lock_file_name = *OatFileName() + ".flock";
133
Richard Uhler581f4e92015-05-07 10:19:35 -0700134 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800135 TEMP_FAILURE_RETRY(unlink(lock_file_name.c_str()));
136 return false;
137 }
138 return true;
139}
140
Richard Uhler95abd042015-03-24 09:51:28 -0700141OatFileAssistant::DexOptNeeded OatFileAssistant::GetDexOptNeeded() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800142 // TODO: If the profiling code is ever restored, it's worth considering
143 // whether we should check to see if the profile is out of date here.
144
Richard Uhler95abd042015-03-24 09:51:28 -0700145 if (OatFileIsUpToDate() || OdexFileIsUpToDate()) {
146 return kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800147 }
Richard Uhler95abd042015-03-24 09:51:28 -0700148
149 if (OdexFileNeedsRelocation()) {
150 return kPatchOatNeeded;
151 }
152
153 if (OatFileNeedsRelocation()) {
154 return kSelfPatchOatNeeded;
155 }
156
Richard Uhler9b994ea2015-06-24 08:44:19 -0700157 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800158}
159
160bool OatFileAssistant::MakeUpToDate(std::string* error_msg) {
Richard Uhler95abd042015-03-24 09:51:28 -0700161 switch (GetDexOptNeeded()) {
162 case kNoDexOptNeeded: return true;
163 case kDex2OatNeeded: return GenerateOatFile(error_msg);
164 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
165 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800166 }
167 UNREACHABLE();
168}
169
170std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700171 // The best oat files are, in descending order of bestness:
172 // 1. Properly relocated files. These may be opened executable.
173 // 2. Not out-of-date files that are already opened non-executable.
174 // 3. Not out-of-date files that we must reopen non-executable.
175
Richard Uhler66d874d2015-01-15 09:37:19 -0800176 if (OatFileIsUpToDate()) {
177 oat_file_released_ = true;
178 return std::move(cached_oat_file_);
179 }
180
181 if (OdexFileIsUpToDate()) {
182 oat_file_released_ = true;
183 return std::move(cached_odex_file_);
184 }
185
Richard Uhler5f946da2015-07-17 12:28:32 -0700186 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
187 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800188
Richard Uhler5f946da2015-07-17 12:28:32 -0700189 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
190 oat_file_released_ = true;
191 return std::move(cached_oat_file_);
192 }
193
194 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
195 oat_file_released_ = true;
196 return std::move(cached_odex_file_);
197 }
198
199 if (!OatFileIsOutOfDate()) {
200 load_executable_ = false;
201 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800202 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700203 CHECK(!OatFileIsExecutable());
204 oat_file_released_ = true;
205 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800206 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700207 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800208
Richard Uhler5f946da2015-07-17 12:28:32 -0700209 if (!OdexFileIsOutOfDate()) {
210 load_executable_ = false;
211 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800212 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700213 CHECK(!OdexFileIsExecutable());
214 oat_file_released_ = true;
215 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800216 }
217 }
218
219 return std::unique_ptr<OatFile>();
220}
221
222std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
223 const OatFile& oat_file, const char* dex_location) {
224 std::vector<std::unique_ptr<const DexFile>> dex_files;
225
226 // Load the primary dex file.
227 std::string error_msg;
228 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
229 dex_location, nullptr, false);
230 if (oat_dex_file == nullptr) {
231 LOG(WARNING) << "Attempt to load out-of-date oat file "
232 << oat_file.GetLocation() << " for dex location " << dex_location;
233 return std::vector<std::unique_ptr<const DexFile>>();
234 }
235
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700236 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800237 if (dex_file.get() == nullptr) {
238 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
239 return std::vector<std::unique_ptr<const DexFile>>();
240 }
241 dex_files.push_back(std::move(dex_file));
242
243 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700244 for (size_t i = 1; ; i++) {
245 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800246 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700247 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800248 // There are no more secondary dex files to load.
249 break;
250 }
251
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700252 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800253 if (dex_file.get() == nullptr) {
254 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
255 return std::vector<std::unique_ptr<const DexFile>>();
256 }
257 dex_files.push_back(std::move(dex_file));
258 }
259 return dex_files;
260}
261
Richard Uhler9b994ea2015-06-24 08:44:19 -0700262bool OatFileAssistant::HasOriginalDexFiles() {
263 // Ensure GetRequiredDexChecksum has been run so that
264 // has_original_dex_files_ is initialized. We don't care about the result of
265 // GetRequiredDexChecksum.
266 GetRequiredDexChecksum();
267 return has_original_dex_files_;
268}
269
Richard Uhler66d874d2015-01-15 09:37:19 -0800270const std::string* OatFileAssistant::OdexFileName() {
271 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800272 cached_odex_file_name_attempted_ = true;
273
274 std::string error_msg;
275 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700276 dex_location_, isa_, &cached_odex_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800277 if (!cached_odex_file_name_found_) {
278 // If we can't figure out the odex file, we treat it as if the odex
279 // file was inaccessible.
280 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
281 }
282 }
283 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
284}
285
286bool OatFileAssistant::OdexFileExists() {
287 return GetOdexFile() != nullptr;
288}
289
Richard Uhler95abd042015-03-24 09:51:28 -0700290OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800291 if (OdexFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700292 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800293 }
294 if (OdexFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700295 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 }
Richard Uhler95abd042015-03-24 09:51:28 -0700297 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800298}
299
300bool OatFileAssistant::OdexFileIsOutOfDate() {
301 if (!odex_file_is_out_of_date_attempted_) {
302 odex_file_is_out_of_date_attempted_ = true;
303 const OatFile* odex_file = GetOdexFile();
304 if (odex_file == nullptr) {
305 cached_odex_file_is_out_of_date_ = true;
306 } else {
307 cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file);
308 }
309 }
310 return cached_odex_file_is_out_of_date_;
311}
312
313bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700314 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800315}
316
317bool OatFileAssistant::OdexFileIsUpToDate() {
318 if (!odex_file_is_up_to_date_attempted_) {
319 odex_file_is_up_to_date_attempted_ = true;
320 const OatFile* odex_file = GetOdexFile();
321 if (odex_file == nullptr) {
322 cached_odex_file_is_up_to_date_ = false;
323 } else {
324 cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
325 }
326 }
327 return cached_odex_file_is_up_to_date_;
328}
329
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800330std::string OatFileAssistant::ArtFileName(const OatFile* oat_file) const {
331 const std::string oat_file_location = oat_file->GetLocation();
332 // Replace extension with .art
333 const size_t last_ext = oat_file_location.find_last_of('.');
334 if (last_ext == std::string::npos) {
335 LOG(ERROR) << "No extension in oat file " << oat_file_location;
336 return std::string();
337 }
338 return oat_file_location.substr(0, last_ext) + ".art";
339}
340
Richard Uhler66d874d2015-01-15 09:37:19 -0800341const std::string* OatFileAssistant::OatFileName() {
342 if (!cached_oat_file_name_attempted_) {
343 cached_oat_file_name_attempted_ = true;
344
345 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800346 // TODO: The oat file assistant should be the definitive place for
347 // determining the oat file name from the dex location, not
348 // GetDalvikCacheFilename.
349 std::string cache_dir = StringPrintf("%s%s",
350 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
351 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700352 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700353 cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800354 if (!cached_oat_file_name_found_) {
355 // If we can't determine the oat file name, we treat the oat file as
356 // inaccessible.
357 LOG(WARNING) << "Failed to determine oat file name for dex location "
358 << dex_location_ << ": " << error_msg;
359 }
360 }
361 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
362}
363
364bool OatFileAssistant::OatFileExists() {
365 return GetOatFile() != nullptr;
366}
367
Richard Uhler95abd042015-03-24 09:51:28 -0700368OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 if (OatFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700370 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800371 }
372 if (OatFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700373 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 }
Richard Uhler95abd042015-03-24 09:51:28 -0700375 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800376}
377
378bool OatFileAssistant::OatFileIsOutOfDate() {
379 if (!oat_file_is_out_of_date_attempted_) {
380 oat_file_is_out_of_date_attempted_ = true;
381 const OatFile* oat_file = GetOatFile();
382 if (oat_file == nullptr) {
383 cached_oat_file_is_out_of_date_ = true;
384 } else {
385 cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file);
386 }
387 }
388 return cached_oat_file_is_out_of_date_;
389}
390
391bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700392 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800393}
394
395bool OatFileAssistant::OatFileIsUpToDate() {
396 if (!oat_file_is_up_to_date_attempted_) {
397 oat_file_is_up_to_date_attempted_ = true;
398 const OatFile* oat_file = GetOatFile();
399 if (oat_file == nullptr) {
400 cached_oat_file_is_up_to_date_ = false;
401 } else {
402 cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
403 }
404 }
405 return cached_oat_file_is_up_to_date_;
406}
407
Richard Uhler95abd042015-03-24 09:51:28 -0700408OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800409 // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which
410 // is more work than we need to do. If performance becomes a concern, and
411 // this method is actually called, this should be fixed.
412 if (GivenOatFileIsOutOfDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700413 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800414 }
415 if (GivenOatFileIsUpToDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700416 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800417 }
Richard Uhler95abd042015-03-24 09:51:28 -0700418 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800419}
420
421bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) {
422 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700423 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800424 // what we provide, which verifies the primary dex checksum for us.
425 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
426 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700427 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700428 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800429 return true;
430 }
431
432 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700433 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800434 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700435 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800436 const OatFile::OatDexFile* secondary_oat_dex_file
437 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700438 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800439 // There are no more secondary dex files to check.
440 break;
441 }
442
443 std::string error_msg;
444 uint32_t expected_secondary_checksum = 0;
445 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700446 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800447 uint32_t actual_secondary_checksum
448 = secondary_oat_dex_file->GetDexFileLocationChecksum();
449 if (expected_secondary_checksum != actual_secondary_checksum) {
450 VLOG(oat) << "Dex checksum does not match for secondary dex: "
451 << secondary_dex_location
452 << ". Expected: " << expected_secondary_checksum
453 << ", Actual: " << actual_secondary_checksum;
Richard Uhler67ff7d12015-05-14 13:21:13 -0700454 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800455 }
456 } else {
457 // If we can't get the checksum for the secondary location, we assume
458 // the dex checksum is up to date for this and all other secondary dex
459 // files.
460 break;
461 }
462 }
463
464 // Verify the image checksum
465 const ImageInfo* image_info = GetImageInfo();
466 if (image_info == nullptr) {
467 VLOG(oat) << "No image for oat image checksum to match against.";
468 return true;
469 }
470
471 if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
472 VLOG(oat) << "Oat image checksum does not match image checksum.";
473 return true;
474 }
475
476 // The checksums are all good; the dex file is not out of date.
477 return false;
478}
479
480bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700481 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800482}
483
484bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
485 if (GivenOatFileIsOutOfDate(file)) {
486 return false;
487 }
488
489 if (file.IsPic()) {
490 return true;
491 }
492
493 const ImageInfo* image_info = GetImageInfo();
494 if (image_info == nullptr) {
Richard Uhlerf7f798c2015-05-11 09:36:57 -0700495 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800496 return false;
497 }
498
499 // Verify the oat_data_begin recorded for the image in the oat file matches
500 // the actual oat_data_begin for boot.oat in the image.
501 const OatHeader& oat_header = file.GetOatHeader();
502 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
503 if (oat_data_begin != image_info->oat_data_begin) {
504 VLOG(oat) << file.GetLocation() <<
505 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
506 << " does not match actual image oat_data_begin ("
507 << image_info->oat_data_begin << ")";
508 return false;
509 }
510
511 // Verify the oat_patch_delta recorded for the image in the oat file matches
512 // the actual oat_patch_delta for the image.
513 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
514 if (oat_patch_delta != image_info->patch_delta) {
515 VLOG(oat) << file.GetLocation() <<
516 ": Oat file image patch delta (" << oat_patch_delta << ")"
517 << " does not match actual image patch delta ("
518 << image_info->patch_delta << ")";
519 return false;
520 }
521 return true;
522}
523
524bool OatFileAssistant::ProfileExists() {
525 return GetProfile() != nullptr;
526}
527
528bool OatFileAssistant::OldProfileExists() {
529 return GetOldProfile() != nullptr;
530}
531
532// TODO: The IsProfileChangeSignificant implementation was copied from likely
533// bit-rotted code.
534bool OatFileAssistant::IsProfileChangeSignificant() {
535 ProfileFile* profile = GetProfile();
536 if (profile == nullptr) {
537 return false;
538 }
539
540 ProfileFile* old_profile = GetOldProfile();
541 if (old_profile == nullptr) {
542 return false;
543 }
544
545 // TODO: The following code to compare two profile files should live with
546 // the rest of the profiler code, not the oat file assistant code.
547
548 // A change in profile is considered significant if X% (change_thr property)
549 // of the top K% (compile_thr property) samples has changed.
550 const ProfilerOptions& options = Runtime::Current()->GetProfilerOptions();
551 const double top_k_threshold = options.GetTopKThreshold();
552 const double change_threshold = options.GetTopKChangeThreshold();
553 std::set<std::string> top_k, old_top_k;
554 profile->GetTopKSamples(top_k, top_k_threshold);
555 old_profile->GetTopKSamples(old_top_k, top_k_threshold);
556 std::set<std::string> diff;
557 std::set_difference(top_k.begin(), top_k.end(), old_top_k.begin(),
558 old_top_k.end(), std::inserter(diff, diff.end()));
559
560 // TODO: consider using the usedPercentage instead of the plain diff count.
561 double change_percent = 100.0 * static_cast<double>(diff.size())
562 / static_cast<double>(top_k.size());
563 std::set<std::string>::iterator end = diff.end();
564 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
565 VLOG(oat) << "Profile new in topK: " << *it;
566 }
567
568 if (change_percent > change_threshold) {
569 VLOG(oat) << "Oat File Assistant: Profile for " << dex_location_
570 << "has changed significantly: (top "
571 << top_k_threshold << "% samples changed in proportion of "
572 << change_percent << "%)";
573 return true;
574 }
575 return false;
576}
577
578// TODO: The CopyProfileFile implementation was copied from likely bit-rotted
579// code.
580void OatFileAssistant::CopyProfileFile() {
581 if (!ProfileExists()) {
582 return;
583 }
584
585 std::string profile_name = ProfileFileName();
586 std::string old_profile_name = OldProfileFileName();
587
588 ScopedFd src(open(old_profile_name.c_str(), O_RDONLY));
589 if (src.get() == -1) {
590 PLOG(WARNING) << "Failed to open profile file " << old_profile_name
591 << ". My uid:gid is " << getuid() << ":" << getgid();
592 return;
593 }
594
595 struct stat stat_src;
596 if (fstat(src.get(), &stat_src) == -1) {
597 PLOG(WARNING) << "Failed to get stats for profile file " << old_profile_name
598 << ". My uid:gid is " << getuid() << ":" << getgid();
599 return;
600 }
601
602 // Create the copy with rw------- (only accessible by system)
603 ScopedFd dst(open(profile_name.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0600));
604 if (dst.get() == -1) {
605 PLOG(WARNING) << "Failed to create/write prev profile file " << profile_name
606 << ". My uid:gid is " << getuid() << ":" << getgid();
607 return;
608 }
609
610#ifdef __linux__
611 if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
612#else
613 off_t len;
614 if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) {
615#endif
616 PLOG(WARNING) << "Failed to copy profile file " << old_profile_name
617 << " to " << profile_name << ". My uid:gid is " << getuid()
618 << ":" << getgid();
619 }
620}
621
Richard Uhler95abd042015-03-24 09:51:28 -0700622bool OatFileAssistant::RelocateOatFile(const std::string* input_file,
623 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800624 CHECK(error_msg != nullptr);
625
Richard Uhler95abd042015-03-24 09:51:28 -0700626 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700627 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700628 + " not attempted because the input file name could not be determined.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800629 return false;
630 }
Richard Uhler95abd042015-03-24 09:51:28 -0700631 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800632
633 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700634 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800635 + " not attempted because the oat file name could not be determined.";
636 return false;
637 }
638 const std::string& oat_file_name = *OatFileName();
639
640 const ImageInfo* image_info = GetImageInfo();
641 Runtime* runtime = Runtime::Current();
642 if (image_info == nullptr) {
643 *error_msg = "Patching of oat file " + oat_file_name
644 + " not attempted because no image location was found.";
645 return false;
646 }
647
648 if (!runtime->IsDex2OatEnabled()) {
649 *error_msg = "Patching of oat file " + oat_file_name
650 + " not attempted because dex2oat is disabled";
651 return false;
652 }
653
654 std::vector<std::string> argv;
655 argv.push_back(runtime->GetPatchoatExecutable());
656 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700657 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800658 argv.push_back("--output-oat-file=" + oat_file_name);
659 argv.push_back("--patched-image-location=" + image_info->location);
660
661 std::string command_line(Join(argv, ' '));
662 if (!Exec(argv, error_msg)) {
663 // Manually delete the file. This ensures there is no garbage left over if
664 // the process unexpectedly died.
665 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
666 return false;
667 }
668
669 // Mark that the oat file has changed and we should try to reload.
670 ClearOatFileCache();
671 return true;
672}
673
674bool OatFileAssistant::GenerateOatFile(std::string* error_msg) {
675 CHECK(error_msg != nullptr);
676
Richard Uhler8327cf72015-10-13 16:34:59 -0700677 Runtime* runtime = Runtime::Current();
678 if (!runtime->IsDex2OatEnabled()) {
679 *error_msg = "Generation of oat file for dex location " + dex_location_
680 + " not attempted because dex2oat is disabled.";
681 return false;
682 }
683
Richard Uhler66d874d2015-01-15 09:37:19 -0800684 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700685 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800686 + " not attempted because the oat file name could not be determined.";
687 return false;
688 }
689 const std::string& oat_file_name = *OatFileName();
690
Richard Uhler66d874d2015-01-15 09:37:19 -0800691 // dex2oat ignores missing dex files and doesn't report an error.
692 // Check explicitly here so we can detect the error properly.
693 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700694 if (!OS::FileExists(dex_location_.c_str())) {
695 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800696 return false;
697 }
698
Richard Uhler8327cf72015-10-13 16:34:59 -0700699 std::unique_ptr<File> oat_file;
700 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
701 if (oat_file.get() == nullptr) {
702 *error_msg = "Generation of oat file " + oat_file_name
703 + " not attempted because the oat file could not be created.";
704 return false;
705 }
706
707 if (fchmod(oat_file->Fd(), 0644) != 0) {
708 *error_msg = "Generation of oat file " + oat_file_name
709 + " not attempted because the oat file could not be made world readable.";
710 oat_file->Erase();
711 return false;
712 }
713
714 std::vector<std::string> args;
715 args.push_back("--dex-file=" + dex_location_);
716 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
717 args.push_back("--oat-location=" + oat_file_name);
718
Richard Uhler66d874d2015-01-15 09:37:19 -0800719 if (!Dex2Oat(args, error_msg)) {
720 // Manually delete the file. This ensures there is no garbage left over if
721 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700722 oat_file->Erase();
723 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
724 return false;
725 }
726
727 if (oat_file->FlushCloseOrErase() != 0) {
728 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler66d874d2015-01-15 09:37:19 -0800729 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
730 return false;
731 }
732
733 // Mark that the oat file has changed and we should try to reload.
734 ClearOatFileCache();
735 return true;
736}
737
738bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
739 std::string* error_msg) {
740 Runtime* runtime = Runtime::Current();
741 std::string image_location = ImageLocation();
742 if (image_location.empty()) {
743 *error_msg = "No image location found for Dex2Oat.";
744 return false;
745 }
746
747 std::vector<std::string> argv;
748 argv.push_back(runtime->GetCompilerExecutable());
749 argv.push_back("--runtime-arg");
750 argv.push_back("-classpath");
751 argv.push_back("--runtime-arg");
752 argv.push_back(runtime->GetClassPathString());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100753 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200754 argv.push_back("--debuggable");
755 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700756 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800757
758 if (!runtime->IsVerificationEnabled()) {
759 argv.push_back("--compiler-filter=verify-none");
760 }
761
762 if (runtime->MustRelocateIfPossible()) {
763 argv.push_back("--runtime-arg");
764 argv.push_back("-Xrelocate");
765 } else {
766 argv.push_back("--runtime-arg");
767 argv.push_back("-Xnorelocate");
768 }
769
770 if (!kIsTargetBuild) {
771 argv.push_back("--host");
772 }
773
774 argv.push_back("--boot-image=" + image_location);
775
776 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
777 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
778
779 argv.insert(argv.end(), args.begin(), args.end());
780
781 std::string command_line(Join(argv, ' '));
782 return Exec(argv, error_msg);
783}
784
785bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
786 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
787 CHECK(odex_filename != nullptr);
788 CHECK(error_msg != nullptr);
789
790 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700791 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800792 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700793 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800794
Richard Uhler63434112015-03-16 14:32:16 -0700795 // Find the directory portion of the dex location and add the oat/<isa>
796 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800797 size_t pos = location.rfind('/');
798 if (pos == std::string::npos) {
799 *error_msg = "Dex location " + location + " has no directory.";
800 return false;
801 }
802 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700803 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800804
805 // Find the file portion of the dex location.
806 std::string file;
807 if (pos == std::string::npos) {
808 file = location;
809 } else {
810 file = location.substr(pos+1);
811 }
812
813 // Get the base part of the file without the extension.
814 pos = file.rfind('.');
815 if (pos == std::string::npos) {
816 *error_msg = "Dex location " + location + " has no extension.";
817 return false;
818 }
819 std::string base = file.substr(0, pos);
820
821 *odex_filename = dir + "/" + base + ".odex";
822 return true;
823}
824
825std::string OatFileAssistant::DalvikCacheDirectory() {
826 // Note: We don't cache this, because it will only be called once by
827 // OatFileName, and we don't care about the performance of the profiling
828 // code, which isn't used in practice.
829
830 // TODO: The work done in GetDalvikCache is overkill for what we need.
831 // Ideally a new API for getting the DalvikCacheDirectory the way we want
832 // (without existence testing, creation, or death) is provided with the rest
833 // of the GetDalvikCache family of functions. Until such an API is in place,
834 // we use GetDalvikCache to avoid duplicating the logic for determining the
835 // dalvik cache directory.
836 std::string result;
837 bool have_android_data;
838 bool dalvik_cache_exists;
839 bool is_global_cache;
840 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
841 return result;
842}
843
844std::string OatFileAssistant::ProfileFileName() {
845 if (package_name_ != nullptr) {
846 return DalvikCacheDirectory() + std::string("profiles/") + package_name_;
847 }
848 return "";
849}
850
851std::string OatFileAssistant::OldProfileFileName() {
852 std::string profile_name = ProfileFileName();
853 if (profile_name.empty()) {
854 return "";
855 }
856 return profile_name + "@old";
857}
858
859std::string OatFileAssistant::ImageLocation() {
860 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000861 const std::vector<gc::space::ImageSpace*>& image_spaces =
862 runtime->GetHeap()->GetBootImageSpaces();
863 if (image_spaces.empty()) {
864 return "";
865 }
866 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800867}
868
869const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700870 if (!required_dex_checksum_attempted_) {
871 required_dex_checksum_attempted_ = true;
872 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800873 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700874 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700875 required_dex_checksum_found_ = true;
876 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800877 } else {
878 // This can happen if the original dex file has been stripped from the
879 // apk.
880 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700881 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800882
883 // Get the checksum from the odex if we can.
884 const OatFile* odex_file = GetOdexFile();
885 if (odex_file != nullptr) {
886 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700887 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800888 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700889 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
890 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800891 }
892 }
893 }
894 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700895 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800896}
897
898const OatFile* OatFileAssistant::GetOdexFile() {
899 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
900 if (!odex_file_load_attempted_) {
901 odex_file_load_attempted_ = true;
902 if (OdexFileName() != nullptr) {
903 const std::string& odex_file_name = *OdexFileName();
904 std::string error_msg;
905 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
906 odex_file_name.c_str(), nullptr, nullptr, load_executable_,
Richard Uhler740eec92015-10-15 15:12:23 -0700907 dex_location_.c_str(), &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800908 if (cached_odex_file_.get() == nullptr) {
909 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
910 << odex_file_name << ": " << error_msg;
911 }
912 }
913 }
914 return cached_odex_file_.get();
915}
916
Richard Uhler5f946da2015-07-17 12:28:32 -0700917bool OatFileAssistant::OdexFileIsExecutable() {
918 const OatFile* odex_file = GetOdexFile();
919 return (odex_file != nullptr && odex_file->IsExecutable());
920}
921
Richard Uhler66d874d2015-01-15 09:37:19 -0800922void OatFileAssistant::ClearOdexFileCache() {
923 odex_file_load_attempted_ = false;
924 cached_odex_file_.reset();
925 odex_file_is_out_of_date_attempted_ = false;
926 odex_file_is_up_to_date_attempted_ = false;
927}
928
929const OatFile* OatFileAssistant::GetOatFile() {
930 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
931 if (!oat_file_load_attempted_) {
932 oat_file_load_attempted_ = true;
933 if (OatFileName() != nullptr) {
934 const std::string& oat_file_name = *OatFileName();
935 std::string error_msg;
936 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Richard Uhlere5fed032015-03-18 08:21:11 -0700937 oat_file_name.c_str(), nullptr, nullptr, load_executable_,
Richard Uhler740eec92015-10-15 15:12:23 -0700938 dex_location_.c_str(), &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800939 if (cached_oat_file_.get() == nullptr) {
940 VLOG(oat) << "OatFileAssistant test for existing oat file "
941 << oat_file_name << ": " << error_msg;
942 }
943 }
944 }
945 return cached_oat_file_.get();
946}
947
Richard Uhler5f946da2015-07-17 12:28:32 -0700948bool OatFileAssistant::OatFileIsExecutable() {
949 const OatFile* oat_file = GetOatFile();
950 return (oat_file != nullptr && oat_file->IsExecutable());
951}
952
Richard Uhler66d874d2015-01-15 09:37:19 -0800953void OatFileAssistant::ClearOatFileCache() {
954 oat_file_load_attempted_ = false;
955 cached_oat_file_.reset();
956 oat_file_is_out_of_date_attempted_ = false;
957 oat_file_is_up_to_date_attempted_ = false;
958}
959
960const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
961 if (!image_info_load_attempted_) {
962 image_info_load_attempted_ = true;
963
964 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800965 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
966 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800967 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800968
969 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800970 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800971 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800972 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
973 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800974 cached_image_info_.patch_delta = image_header.GetPatchDelta();
975 } else {
976 std::unique_ptr<ImageHeader> image_header(
977 gc::space::ImageSpace::ReadImageHeaderOrDie(
978 cached_image_info_.location.c_str(), isa_));
979 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800980 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
981 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800982 cached_image_info_.patch_delta = image_header->GetPatchDelta();
983 }
984 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800985 image_info_load_succeeded_ = (!image_spaces.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -0800986 }
987 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
988}
989
990ProfileFile* OatFileAssistant::GetProfile() {
991 if (!profile_load_attempted_) {
992 CHECK(package_name_ != nullptr)
993 << "pakage_name_ is nullptr: "
994 << "profile_load_attempted_ should have been true";
995 profile_load_attempted_ = true;
996 std::string profile_name = ProfileFileName();
997 if (!profile_name.empty()) {
998 profile_load_succeeded_ = cached_profile_.LoadFile(profile_name);
999 }
1000 }
1001 return profile_load_succeeded_ ? &cached_profile_ : nullptr;
1002}
1003
1004ProfileFile* OatFileAssistant::GetOldProfile() {
1005 if (!old_profile_load_attempted_) {
1006 CHECK(package_name_ != nullptr)
1007 << "pakage_name_ is nullptr: "
1008 << "old_profile_load_attempted_ should have been true";
1009 old_profile_load_attempted_ = true;
1010 std::string old_profile_name = OldProfileFileName();
1011 if (!old_profile_name.empty()) {
1012 old_profile_load_succeeded_ = cached_old_profile_.LoadFile(old_profile_name);
1013 }
1014 }
1015 return old_profile_load_succeeded_ ? &cached_old_profile_ : nullptr;
1016}
1017
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001018gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
1019 DCHECK(oat_file != nullptr);
1020 std::string art_file = ArtFileName(oat_file);
1021 if (art_file.empty()) {
1022 return nullptr;
1023 }
1024 std::string error_msg;
1025 ScopedObjectAccess soa(Thread::Current());
1026 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
1027 oat_file,
1028 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001029 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001030 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1031 }
1032 return ret;
1033}
1034
Richard Uhler66d874d2015-01-15 09:37:19 -08001035} // namespace art
1036