blob: 262c932766e5ef6b7bebf762bfbc803b3ea823ea [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
David Brazdilce4b0ba2016-01-28 15:05:49 +0000464 if (file.IsExtractOnly()) {
465 VLOG(oat) << "Oat file is extract-only. Image checksum test skipped.";
466 if (kIsDebugBuild) {
467 // Sanity check that no classes have compiled code. Does not test that
468 // the DEX code has not been quickened.
469 std::string error_msg;
470 for (const OatFile::OatDexFile* current : file.GetOatDexFiles()) {
David Brazdil13554952016-02-01 16:32:50 +0000471 std::unique_ptr<const DexFile> dex_file = current->OpenDexFile(&error_msg);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000472 DCHECK(dex_file != nullptr);
473 for (size_t i = 0, e = dex_file->NumClassDefs(); i < e; ++i) {
474 DCHECK_EQ(current->GetOatClass(i).GetType(), kOatClassNoneCompiled);
475 }
476 }
477 }
478 return false;
479 }
480
Richard Uhler66d874d2015-01-15 09:37:19 -0800481 // Verify the image checksum
482 const ImageInfo* image_info = GetImageInfo();
483 if (image_info == nullptr) {
484 VLOG(oat) << "No image for oat image checksum to match against.";
485 return true;
486 }
487
488 if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
489 VLOG(oat) << "Oat image checksum does not match image checksum.";
490 return true;
491 }
492
493 // The checksums are all good; the dex file is not out of date.
494 return false;
495}
496
497bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700498 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800499}
500
501bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
502 if (GivenOatFileIsOutOfDate(file)) {
503 return false;
504 }
505
David Brazdilce4b0ba2016-01-28 15:05:49 +0000506 if (file.IsPic() || file.IsExtractOnly()) {
507 // Oat files compiled in PIC mode do not require relocation and extract-only
508 // oat files do not contain any compiled code. Skip the relocation test.
509 VLOG(oat) << "Oat relocation test skipped.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800510 return true;
511 }
512
513 const ImageInfo* image_info = GetImageInfo();
514 if (image_info == nullptr) {
Richard Uhlerf7f798c2015-05-11 09:36:57 -0700515 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800516 return false;
517 }
518
519 // Verify the oat_data_begin recorded for the image in the oat file matches
520 // the actual oat_data_begin for boot.oat in the image.
521 const OatHeader& oat_header = file.GetOatHeader();
522 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
523 if (oat_data_begin != image_info->oat_data_begin) {
524 VLOG(oat) << file.GetLocation() <<
525 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
526 << " does not match actual image oat_data_begin ("
527 << image_info->oat_data_begin << ")";
528 return false;
529 }
530
531 // Verify the oat_patch_delta recorded for the image in the oat file matches
532 // the actual oat_patch_delta for the image.
533 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
534 if (oat_patch_delta != image_info->patch_delta) {
535 VLOG(oat) << file.GetLocation() <<
536 ": Oat file image patch delta (" << oat_patch_delta << ")"
537 << " does not match actual image patch delta ("
538 << image_info->patch_delta << ")";
539 return false;
540 }
541 return true;
542}
543
544bool OatFileAssistant::ProfileExists() {
545 return GetProfile() != nullptr;
546}
547
548bool OatFileAssistant::OldProfileExists() {
549 return GetOldProfile() != nullptr;
550}
551
552// TODO: The IsProfileChangeSignificant implementation was copied from likely
553// bit-rotted code.
554bool OatFileAssistant::IsProfileChangeSignificant() {
555 ProfileFile* profile = GetProfile();
556 if (profile == nullptr) {
557 return false;
558 }
559
560 ProfileFile* old_profile = GetOldProfile();
561 if (old_profile == nullptr) {
562 return false;
563 }
564
565 // TODO: The following code to compare two profile files should live with
566 // the rest of the profiler code, not the oat file assistant code.
567
568 // A change in profile is considered significant if X% (change_thr property)
569 // of the top K% (compile_thr property) samples has changed.
570 const ProfilerOptions& options = Runtime::Current()->GetProfilerOptions();
571 const double top_k_threshold = options.GetTopKThreshold();
572 const double change_threshold = options.GetTopKChangeThreshold();
573 std::set<std::string> top_k, old_top_k;
574 profile->GetTopKSamples(top_k, top_k_threshold);
575 old_profile->GetTopKSamples(old_top_k, top_k_threshold);
576 std::set<std::string> diff;
577 std::set_difference(top_k.begin(), top_k.end(), old_top_k.begin(),
578 old_top_k.end(), std::inserter(diff, diff.end()));
579
580 // TODO: consider using the usedPercentage instead of the plain diff count.
581 double change_percent = 100.0 * static_cast<double>(diff.size())
582 / static_cast<double>(top_k.size());
583 std::set<std::string>::iterator end = diff.end();
584 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
585 VLOG(oat) << "Profile new in topK: " << *it;
586 }
587
588 if (change_percent > change_threshold) {
589 VLOG(oat) << "Oat File Assistant: Profile for " << dex_location_
590 << "has changed significantly: (top "
591 << top_k_threshold << "% samples changed in proportion of "
592 << change_percent << "%)";
593 return true;
594 }
595 return false;
596}
597
598// TODO: The CopyProfileFile implementation was copied from likely bit-rotted
599// code.
600void OatFileAssistant::CopyProfileFile() {
601 if (!ProfileExists()) {
602 return;
603 }
604
605 std::string profile_name = ProfileFileName();
606 std::string old_profile_name = OldProfileFileName();
607
608 ScopedFd src(open(old_profile_name.c_str(), O_RDONLY));
609 if (src.get() == -1) {
610 PLOG(WARNING) << "Failed to open profile file " << old_profile_name
611 << ". My uid:gid is " << getuid() << ":" << getgid();
612 return;
613 }
614
615 struct stat stat_src;
616 if (fstat(src.get(), &stat_src) == -1) {
617 PLOG(WARNING) << "Failed to get stats for profile file " << old_profile_name
618 << ". My uid:gid is " << getuid() << ":" << getgid();
619 return;
620 }
621
622 // Create the copy with rw------- (only accessible by system)
623 ScopedFd dst(open(profile_name.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0600));
624 if (dst.get() == -1) {
625 PLOG(WARNING) << "Failed to create/write prev profile file " << profile_name
626 << ". My uid:gid is " << getuid() << ":" << getgid();
627 return;
628 }
629
630#ifdef __linux__
631 if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
632#else
633 off_t len;
634 if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) {
635#endif
636 PLOG(WARNING) << "Failed to copy profile file " << old_profile_name
637 << " to " << profile_name << ". My uid:gid is " << getuid()
638 << ":" << getgid();
639 }
640}
641
Richard Uhler95abd042015-03-24 09:51:28 -0700642bool OatFileAssistant::RelocateOatFile(const std::string* input_file,
643 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800644 CHECK(error_msg != nullptr);
645
Richard Uhler95abd042015-03-24 09:51:28 -0700646 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700647 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700648 + " not attempted because the input file name could not be determined.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800649 return false;
650 }
Richard Uhler95abd042015-03-24 09:51:28 -0700651 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800652
653 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700654 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800655 + " not attempted because the oat file name could not be determined.";
656 return false;
657 }
658 const std::string& oat_file_name = *OatFileName();
659
660 const ImageInfo* image_info = GetImageInfo();
661 Runtime* runtime = Runtime::Current();
662 if (image_info == nullptr) {
663 *error_msg = "Patching of oat file " + oat_file_name
664 + " not attempted because no image location was found.";
665 return false;
666 }
667
668 if (!runtime->IsDex2OatEnabled()) {
669 *error_msg = "Patching of oat file " + oat_file_name
670 + " not attempted because dex2oat is disabled";
671 return false;
672 }
673
674 std::vector<std::string> argv;
675 argv.push_back(runtime->GetPatchoatExecutable());
676 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700677 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800678 argv.push_back("--output-oat-file=" + oat_file_name);
679 argv.push_back("--patched-image-location=" + image_info->location);
680
681 std::string command_line(Join(argv, ' '));
682 if (!Exec(argv, error_msg)) {
683 // Manually delete the file. This ensures there is no garbage left over if
684 // the process unexpectedly died.
685 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
686 return false;
687 }
688
689 // Mark that the oat file has changed and we should try to reload.
690 ClearOatFileCache();
691 return true;
692}
693
694bool OatFileAssistant::GenerateOatFile(std::string* error_msg) {
695 CHECK(error_msg != nullptr);
696
Richard Uhler8327cf72015-10-13 16:34:59 -0700697 Runtime* runtime = Runtime::Current();
698 if (!runtime->IsDex2OatEnabled()) {
699 *error_msg = "Generation of oat file for dex location " + dex_location_
700 + " not attempted because dex2oat is disabled.";
701 return false;
702 }
703
Richard Uhler66d874d2015-01-15 09:37:19 -0800704 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700705 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800706 + " not attempted because the oat file name could not be determined.";
707 return false;
708 }
709 const std::string& oat_file_name = *OatFileName();
710
Richard Uhler66d874d2015-01-15 09:37:19 -0800711 // dex2oat ignores missing dex files and doesn't report an error.
712 // Check explicitly here so we can detect the error properly.
713 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700714 if (!OS::FileExists(dex_location_.c_str())) {
715 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800716 return false;
717 }
718
Richard Uhler8327cf72015-10-13 16:34:59 -0700719 std::unique_ptr<File> oat_file;
720 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
721 if (oat_file.get() == nullptr) {
722 *error_msg = "Generation of oat file " + oat_file_name
723 + " not attempted because the oat file could not be created.";
724 return false;
725 }
726
727 if (fchmod(oat_file->Fd(), 0644) != 0) {
728 *error_msg = "Generation of oat file " + oat_file_name
729 + " not attempted because the oat file could not be made world readable.";
730 oat_file->Erase();
731 return false;
732 }
733
734 std::vector<std::string> args;
735 args.push_back("--dex-file=" + dex_location_);
736 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
737 args.push_back("--oat-location=" + oat_file_name);
738
Richard Uhler66d874d2015-01-15 09:37:19 -0800739 if (!Dex2Oat(args, error_msg)) {
740 // Manually delete the file. This ensures there is no garbage left over if
741 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700742 oat_file->Erase();
743 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
744 return false;
745 }
746
747 if (oat_file->FlushCloseOrErase() != 0) {
748 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler66d874d2015-01-15 09:37:19 -0800749 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
750 return false;
751 }
752
753 // Mark that the oat file has changed and we should try to reload.
754 ClearOatFileCache();
755 return true;
756}
757
758bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
759 std::string* error_msg) {
760 Runtime* runtime = Runtime::Current();
761 std::string image_location = ImageLocation();
762 if (image_location.empty()) {
763 *error_msg = "No image location found for Dex2Oat.";
764 return false;
765 }
766
767 std::vector<std::string> argv;
768 argv.push_back(runtime->GetCompilerExecutable());
769 argv.push_back("--runtime-arg");
770 argv.push_back("-classpath");
771 argv.push_back("--runtime-arg");
772 argv.push_back(runtime->GetClassPathString());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100773 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200774 argv.push_back("--debuggable");
775 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700776 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800777
778 if (!runtime->IsVerificationEnabled()) {
779 argv.push_back("--compiler-filter=verify-none");
780 }
781
782 if (runtime->MustRelocateIfPossible()) {
783 argv.push_back("--runtime-arg");
784 argv.push_back("-Xrelocate");
785 } else {
786 argv.push_back("--runtime-arg");
787 argv.push_back("-Xnorelocate");
788 }
789
790 if (!kIsTargetBuild) {
791 argv.push_back("--host");
792 }
793
794 argv.push_back("--boot-image=" + image_location);
795
796 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
797 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
798
799 argv.insert(argv.end(), args.begin(), args.end());
800
801 std::string command_line(Join(argv, ' '));
802 return Exec(argv, error_msg);
803}
804
805bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
806 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
807 CHECK(odex_filename != nullptr);
808 CHECK(error_msg != nullptr);
809
810 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700811 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800812 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700813 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800814
Richard Uhler63434112015-03-16 14:32:16 -0700815 // Find the directory portion of the dex location and add the oat/<isa>
816 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800817 size_t pos = location.rfind('/');
818 if (pos == std::string::npos) {
819 *error_msg = "Dex location " + location + " has no directory.";
820 return false;
821 }
822 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700823 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800824
825 // Find the file portion of the dex location.
826 std::string file;
827 if (pos == std::string::npos) {
828 file = location;
829 } else {
830 file = location.substr(pos+1);
831 }
832
833 // Get the base part of the file without the extension.
834 pos = file.rfind('.');
835 if (pos == std::string::npos) {
836 *error_msg = "Dex location " + location + " has no extension.";
837 return false;
838 }
839 std::string base = file.substr(0, pos);
840
841 *odex_filename = dir + "/" + base + ".odex";
842 return true;
843}
844
845std::string OatFileAssistant::DalvikCacheDirectory() {
846 // Note: We don't cache this, because it will only be called once by
847 // OatFileName, and we don't care about the performance of the profiling
848 // code, which isn't used in practice.
849
850 // TODO: The work done in GetDalvikCache is overkill for what we need.
851 // Ideally a new API for getting the DalvikCacheDirectory the way we want
852 // (without existence testing, creation, or death) is provided with the rest
853 // of the GetDalvikCache family of functions. Until such an API is in place,
854 // we use GetDalvikCache to avoid duplicating the logic for determining the
855 // dalvik cache directory.
856 std::string result;
857 bool have_android_data;
858 bool dalvik_cache_exists;
859 bool is_global_cache;
860 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
861 return result;
862}
863
864std::string OatFileAssistant::ProfileFileName() {
865 if (package_name_ != nullptr) {
866 return DalvikCacheDirectory() + std::string("profiles/") + package_name_;
867 }
868 return "";
869}
870
871std::string OatFileAssistant::OldProfileFileName() {
872 std::string profile_name = ProfileFileName();
873 if (profile_name.empty()) {
874 return "";
875 }
876 return profile_name + "@old";
877}
878
879std::string OatFileAssistant::ImageLocation() {
880 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000881 const std::vector<gc::space::ImageSpace*>& image_spaces =
882 runtime->GetHeap()->GetBootImageSpaces();
883 if (image_spaces.empty()) {
884 return "";
885 }
886 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800887}
888
889const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700890 if (!required_dex_checksum_attempted_) {
891 required_dex_checksum_attempted_ = true;
892 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800893 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700894 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700895 required_dex_checksum_found_ = true;
896 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800897 } else {
898 // This can happen if the original dex file has been stripped from the
899 // apk.
900 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700901 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800902
903 // Get the checksum from the odex if we can.
904 const OatFile* odex_file = GetOdexFile();
905 if (odex_file != nullptr) {
906 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700907 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800908 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700909 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
910 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800911 }
912 }
913 }
914 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700915 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800916}
917
918const OatFile* OatFileAssistant::GetOdexFile() {
919 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
920 if (!odex_file_load_attempted_) {
921 odex_file_load_attempted_ = true;
922 if (OdexFileName() != nullptr) {
923 const std::string& odex_file_name = *OdexFileName();
924 std::string error_msg;
925 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
926 odex_file_name.c_str(), nullptr, nullptr, load_executable_,
Richard Uhler740eec92015-10-15 15:12:23 -0700927 dex_location_.c_str(), &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800928 if (cached_odex_file_.get() == nullptr) {
929 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
930 << odex_file_name << ": " << error_msg;
931 }
932 }
933 }
934 return cached_odex_file_.get();
935}
936
Richard Uhler5f946da2015-07-17 12:28:32 -0700937bool OatFileAssistant::OdexFileIsExecutable() {
938 const OatFile* odex_file = GetOdexFile();
939 return (odex_file != nullptr && odex_file->IsExecutable());
940}
941
Richard Uhler66d874d2015-01-15 09:37:19 -0800942void OatFileAssistant::ClearOdexFileCache() {
943 odex_file_load_attempted_ = false;
944 cached_odex_file_.reset();
945 odex_file_is_out_of_date_attempted_ = false;
946 odex_file_is_up_to_date_attempted_ = false;
947}
948
949const OatFile* OatFileAssistant::GetOatFile() {
950 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
951 if (!oat_file_load_attempted_) {
952 oat_file_load_attempted_ = true;
953 if (OatFileName() != nullptr) {
954 const std::string& oat_file_name = *OatFileName();
955 std::string error_msg;
956 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Richard Uhlere5fed032015-03-18 08:21:11 -0700957 oat_file_name.c_str(), nullptr, nullptr, load_executable_,
Richard Uhler740eec92015-10-15 15:12:23 -0700958 dex_location_.c_str(), &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800959 if (cached_oat_file_.get() == nullptr) {
960 VLOG(oat) << "OatFileAssistant test for existing oat file "
961 << oat_file_name << ": " << error_msg;
962 }
963 }
964 }
965 return cached_oat_file_.get();
966}
967
Richard Uhler5f946da2015-07-17 12:28:32 -0700968bool OatFileAssistant::OatFileIsExecutable() {
969 const OatFile* oat_file = GetOatFile();
970 return (oat_file != nullptr && oat_file->IsExecutable());
971}
972
Richard Uhler66d874d2015-01-15 09:37:19 -0800973void OatFileAssistant::ClearOatFileCache() {
974 oat_file_load_attempted_ = false;
975 cached_oat_file_.reset();
976 oat_file_is_out_of_date_attempted_ = false;
977 oat_file_is_up_to_date_attempted_ = false;
978}
979
980const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
981 if (!image_info_load_attempted_) {
982 image_info_load_attempted_ = true;
983
984 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800985 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
986 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800987 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800988
989 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800990 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800991 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800992 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
993 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800994 cached_image_info_.patch_delta = image_header.GetPatchDelta();
995 } else {
996 std::unique_ptr<ImageHeader> image_header(
997 gc::space::ImageSpace::ReadImageHeaderOrDie(
998 cached_image_info_.location.c_str(), isa_));
999 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -08001000 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
1001 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -08001002 cached_image_info_.patch_delta = image_header->GetPatchDelta();
1003 }
1004 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001005 image_info_load_succeeded_ = (!image_spaces.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001006 }
1007 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
1008}
1009
1010ProfileFile* OatFileAssistant::GetProfile() {
1011 if (!profile_load_attempted_) {
1012 CHECK(package_name_ != nullptr)
1013 << "pakage_name_ is nullptr: "
1014 << "profile_load_attempted_ should have been true";
1015 profile_load_attempted_ = true;
1016 std::string profile_name = ProfileFileName();
1017 if (!profile_name.empty()) {
1018 profile_load_succeeded_ = cached_profile_.LoadFile(profile_name);
1019 }
1020 }
1021 return profile_load_succeeded_ ? &cached_profile_ : nullptr;
1022}
1023
1024ProfileFile* OatFileAssistant::GetOldProfile() {
1025 if (!old_profile_load_attempted_) {
1026 CHECK(package_name_ != nullptr)
1027 << "pakage_name_ is nullptr: "
1028 << "old_profile_load_attempted_ should have been true";
1029 old_profile_load_attempted_ = true;
1030 std::string old_profile_name = OldProfileFileName();
1031 if (!old_profile_name.empty()) {
1032 old_profile_load_succeeded_ = cached_old_profile_.LoadFile(old_profile_name);
1033 }
1034 }
1035 return old_profile_load_succeeded_ ? &cached_old_profile_ : nullptr;
1036}
1037
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001038gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
1039 DCHECK(oat_file != nullptr);
1040 std::string art_file = ArtFileName(oat_file);
1041 if (art_file.empty()) {
1042 return nullptr;
1043 }
1044 std::string error_msg;
1045 ScopedObjectAccess soa(Thread::Current());
1046 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
1047 oat_file,
1048 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001049 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001050 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1051 }
1052 return ret;
1053}
1054
Richard Uhler66d874d2015-01-15 09:37:19 -08001055} // namespace art
1056