blob: c1901587df047f65fea8bab064708347bf53c804 [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
Richard Uhler66d874d2015-01-15 09:37:19 -080019#include <sys/stat.h>
Richard Uhler66d874d2015-01-15 09:37:19 -080020#include "base/logging.h"
21#include "base/stringprintf.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010022#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080023#include "class_linker.h"
24#include "gc/heap.h"
25#include "gc/space/image_space.h"
26#include "image.h"
27#include "oat.h"
28#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "runtime.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080030#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "utils.h"
32
33namespace art {
34
Narayan Kamath8943c1d2016-05-02 13:14:48 +010035std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
36 switch (status) {
37 case OatFileAssistant::kOatOutOfDate:
38 stream << "kOatOutOfDate";
39 break;
40 case OatFileAssistant::kOatUpToDate:
41 stream << "kOatUpToDate";
42 break;
43 case OatFileAssistant::kOatNeedsRelocation:
44 stream << "kOatNeedsRelocation";
45 break;
46 default:
47 UNREACHABLE();
48 }
49
50 return stream;
51}
52
Richard Uhler66d874d2015-01-15 09:37:19 -080053OatFileAssistant::OatFileAssistant(const char* dex_location,
54 const InstructionSet isa,
55 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070056 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000057{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080058
59OatFileAssistant::OatFileAssistant(const char* dex_location,
60 const char* oat_location,
61 const InstructionSet isa,
62 bool load_executable)
Richard Uhler743bf362016-04-19 15:39:37 -070063 : isa_(isa), load_executable_(load_executable), odex_(this), oat_(this) {
Richard Uhler740eec92015-10-15 15:12:23 -070064 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
65 dex_location_.assign(dex_location);
66
Richard Uhler66d874d2015-01-15 09:37:19 -080067 if (load_executable_ && isa != kRuntimeISA) {
68 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
69 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
70 load_executable_ = false;
71 }
72
Richard Uhler743bf362016-04-19 15:39:37 -070073 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070074 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070075 std::string odex_file_name;
76 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
77 odex_.Reset(odex_file_name);
78 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070079 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
80 }
81
Richard Uhler743bf362016-04-19 15:39:37 -070082 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080083 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070084 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -070085 } else {
Richard Uhler743bf362016-04-19 15:39:37 -070086 std::string oat_file_name;
87 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
88 oat_.Reset(oat_file_name);
89 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070090 LOG(WARNING) << "Failed to determine oat file name for dex location "
91 << dex_location_ << ": " << error_msg;
92 }
Richard Uhler66d874d2015-01-15 09:37:19 -080093 }
Richard Uhler66d874d2015-01-15 09:37:19 -080094}
95
96OatFileAssistant::~OatFileAssistant() {
97 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -070098 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010099 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800100 }
101}
102
103bool OatFileAssistant::IsInBootClassPath() {
104 // Note: We check the current boot class path, regardless of the ISA
105 // specified by the user. This is okay, because the boot class path should
106 // be the same for all ISAs.
107 // TODO: Can we verify the boot class path is the same for all ISAs?
108 Runtime* runtime = Runtime::Current();
109 ClassLinker* class_linker = runtime->GetClassLinker();
110 const auto& boot_class_path = class_linker->GetBootClassPath();
111 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700112 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800113 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
114 return true;
115 }
116 }
117 return false;
118}
119
120bool OatFileAssistant::Lock(std::string* error_msg) {
121 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700122 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800123
Richard Uhler743bf362016-04-19 15:39:37 -0700124 const std::string* oat_file_name = oat_.Filename();
125 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800126 *error_msg = "Failed to determine lock file";
127 return false;
128 }
Richard Uhler743bf362016-04-19 15:39:37 -0700129 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800130
Richard Uhler581f4e92015-05-07 10:19:35 -0700131 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100132 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800133 return false;
134 }
135 return true;
136}
137
Richard Uhlerd1472a22016-04-15 15:18:56 -0700138OatFileAssistant::DexOptNeeded
Richard Uhler743bf362016-04-19 15:39:37 -0700139OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100140 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000141
142 // See if the oat file is in good shape as is.
Richard Uhler743bf362016-04-19 15:39:37 -0700143 bool oat_okay = oat_.CompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000144 if (oat_okay) {
145 if (compilation_desired) {
Richard Uhler743bf362016-04-19 15:39:37 -0700146 if (oat_.IsUpToDate()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000147 return kNoDexOptNeeded;
148 }
149 } else {
Richard Uhler743bf362016-04-19 15:39:37 -0700150 if (!oat_.IsOutOfDate()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000151 return kNoDexOptNeeded;
152 }
153 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800154 }
Richard Uhler95abd042015-03-24 09:51:28 -0700155
Andreas Gampe29d38e72016-03-23 15:31:51 +0000156 // See if the odex file is in good shape as is.
Richard Uhler743bf362016-04-19 15:39:37 -0700157 bool odex_okay = odex_.CompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000158 if (odex_okay) {
159 if (compilation_desired) {
Richard Uhler743bf362016-04-19 15:39:37 -0700160 if (odex_.IsUpToDate()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000161 return kNoDexOptNeeded;
162 }
163 } else {
Richard Uhler743bf362016-04-19 15:39:37 -0700164 if (!odex_.IsOutOfDate()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000165 return kNoDexOptNeeded;
166 }
167 }
Richard Uhler95abd042015-03-24 09:51:28 -0700168 }
169
Andreas Gampe29d38e72016-03-23 15:31:51 +0000170 // See if we can get an up-to-date file by running patchoat.
171 if (compilation_desired) {
Richard Uhler743bf362016-04-19 15:39:37 -0700172 if (odex_okay && odex_.NeedsRelocation() && odex_.HasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000173 return kPatchOatNeeded;
174 }
175
Richard Uhler743bf362016-04-19 15:39:37 -0700176 if (oat_okay && oat_.NeedsRelocation() && oat_.HasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000177 return kSelfPatchOatNeeded;
178 }
Richard Uhler95abd042015-03-24 09:51:28 -0700179 }
180
Andreas Gampe29d38e72016-03-23 15:31:51 +0000181 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700182 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800183}
184
Richard Uhlerf4b34872016-04-13 11:03:46 -0700185// Figure out the currently specified compile filter option in the runtime.
186// Returns true on success, false if the compiler filter is invalid, in which
187// case error_msg describes the problem.
188static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
189 std::string* error_msg) {
190 CHECK(filter != nullptr);
191 CHECK(error_msg != nullptr);
192
193 *filter = CompilerFilter::kDefaultCompilerFilter;
194 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
195 if (option.starts_with("--compiler-filter=")) {
196 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
197 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
198 *error_msg = std::string("Unknown --compiler-filter value: ")
199 + std::string(compiler_filter_string);
200 return false;
201 }
202 }
203 }
204 return true;
205}
206
Richard Uhler01be6812016-05-17 10:34:52 -0700207bool OatFileAssistant::IsUpToDate() {
208 return OatFileIsUpToDate() || OdexFileIsUpToDate();
209}
210
Richard Uhler1e860612016-03-30 12:17:55 -0700211OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700212OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700213 CompilerFilter::Filter target;
214 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
215 return kUpdateNotAttempted;
216 }
217
Richard Uhlerd1472a22016-04-15 15:18:56 -0700218 switch (GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700219 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700220 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler743bf362016-04-19 15:39:37 -0700221 case kPatchOatNeeded: return RelocateOatFile(odex_.Filename(), error_msg);
222 case kSelfPatchOatNeeded: return RelocateOatFile(oat_.Filename(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800223 }
224 UNREACHABLE();
225}
226
227std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700228 // The best oat files are, in descending order of bestness:
229 // 1. Properly relocated files. These may be opened executable.
230 // 2. Not out-of-date files that are already opened non-executable.
231 // 3. Not out-of-date files that we must reopen non-executable.
232
Richard Uhler743bf362016-04-19 15:39:37 -0700233 if (oat_.IsUpToDate()) {
234 return oat_.ReleaseFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800235 }
236
Richard Uhler743bf362016-04-19 15:39:37 -0700237 if (odex_.IsUpToDate()) {
238 return odex_.ReleaseFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800239 }
240
Richard Uhler5f946da2015-07-17 12:28:32 -0700241 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
242 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800243
Richard Uhler743bf362016-04-19 15:39:37 -0700244 if (!oat_.IsOutOfDate() && !oat_.IsExecutable()) {
245 return oat_.ReleaseFile();
Richard Uhler5f946da2015-07-17 12:28:32 -0700246 }
247
Richard Uhler743bf362016-04-19 15:39:37 -0700248 if (!odex_.IsOutOfDate() && !odex_.IsExecutable()) {
249 return odex_.ReleaseFile();
Richard Uhler5f946da2015-07-17 12:28:32 -0700250 }
251
Richard Uhler743bf362016-04-19 15:39:37 -0700252 if (!oat_.IsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700253 load_executable_ = false;
Richard Uhler743bf362016-04-19 15:39:37 -0700254 oat_.Reset();
255 if (!oat_.IsOutOfDate()) {
256 CHECK(!oat_.IsExecutable());
257 return oat_.ReleaseFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800258 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700259 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800260
Richard Uhler743bf362016-04-19 15:39:37 -0700261 if (!odex_.IsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700262 load_executable_ = false;
Richard Uhler743bf362016-04-19 15:39:37 -0700263 odex_.Reset();
264 if (!odex_.IsOutOfDate()) {
265 CHECK(!odex_.IsExecutable());
266 return odex_.ReleaseFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800267 }
268 }
269
270 return std::unique_ptr<OatFile>();
271}
272
273std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
274 const OatFile& oat_file, const char* dex_location) {
275 std::vector<std::unique_ptr<const DexFile>> dex_files;
276
277 // Load the primary dex file.
278 std::string error_msg;
279 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700280 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800281 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700282 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800283 return std::vector<std::unique_ptr<const DexFile>>();
284 }
285
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700286 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 if (dex_file.get() == nullptr) {
288 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
289 return std::vector<std::unique_ptr<const DexFile>>();
290 }
291 dex_files.push_back(std::move(dex_file));
292
293 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700294 for (size_t i = 1; ; i++) {
295 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700296 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700297 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800298 // There are no more secondary dex files to load.
299 break;
300 }
301
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700302 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 if (dex_file.get() == nullptr) {
304 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
305 return std::vector<std::unique_ptr<const DexFile>>();
306 }
307 dex_files.push_back(std::move(dex_file));
308 }
309 return dex_files;
310}
311
Richard Uhler9b994ea2015-06-24 08:44:19 -0700312bool OatFileAssistant::HasOriginalDexFiles() {
313 // Ensure GetRequiredDexChecksum has been run so that
314 // has_original_dex_files_ is initialized. We don't care about the result of
315 // GetRequiredDexChecksum.
316 GetRequiredDexChecksum();
317 return has_original_dex_files_;
318}
319
Richard Uhler66d874d2015-01-15 09:37:19 -0800320const std::string* OatFileAssistant::OdexFileName() {
Richard Uhler743bf362016-04-19 15:39:37 -0700321 return odex_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800322}
323
324bool OatFileAssistant::OdexFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700325 return odex_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800326}
327
Richard Uhler95abd042015-03-24 09:51:28 -0700328OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700329 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800330}
331
332bool OatFileAssistant::OdexFileIsOutOfDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700333 return odex_.IsOutOfDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800334}
335
336bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler743bf362016-04-19 15:39:37 -0700337 return odex_.NeedsRelocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800338}
339
340bool OatFileAssistant::OdexFileIsUpToDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700341 return odex_.IsUpToDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800342}
343
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100344CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
Richard Uhler743bf362016-04-19 15:39:37 -0700345 return odex_.CompilerFilter();
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100346}
Richard Uhler4c7f1932016-04-15 15:51:21 -0700347
348static std::string ArtFileName(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800349 const std::string oat_file_location = oat_file->GetLocation();
350 // Replace extension with .art
351 const size_t last_ext = oat_file_location.find_last_of('.');
352 if (last_ext == std::string::npos) {
353 LOG(ERROR) << "No extension in oat file " << oat_file_location;
354 return std::string();
355 }
356 return oat_file_location.substr(0, last_ext) + ".art";
357}
358
Richard Uhler66d874d2015-01-15 09:37:19 -0800359const std::string* OatFileAssistant::OatFileName() {
Richard Uhler743bf362016-04-19 15:39:37 -0700360 return oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800361}
362
363bool OatFileAssistant::OatFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700364 return oat_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800365}
366
Richard Uhler95abd042015-03-24 09:51:28 -0700367OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700368 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800369}
370
371bool OatFileAssistant::OatFileIsOutOfDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700372 return oat_.IsOutOfDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800373}
374
375bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler743bf362016-04-19 15:39:37 -0700376 return oat_.NeedsRelocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800377}
378
379bool OatFileAssistant::OatFileIsUpToDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700380 return oat_.IsUpToDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800381}
382
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100383CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
Richard Uhler743bf362016-04-19 15:39:37 -0700384 return oat_.CompilerFilter();
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100385}
386
Richard Uhler95abd042015-03-24 09:51:28 -0700387OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800388 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700389 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800390 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700391 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800392 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
393 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700394 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700395 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700396 VLOG(oat) << error_msg;
Richard Uhlere8109f72016-04-18 10:40:50 -0700397 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800398 }
399
400 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700401 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700402 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800403 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700404 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700405 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800406 // There are no more secondary dex files to check.
407 break;
408 }
409
Richard Uhler66d874d2015-01-15 09:37:19 -0800410 uint32_t expected_secondary_checksum = 0;
411 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700412 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800413 uint32_t actual_secondary_checksum
414 = secondary_oat_dex_file->GetDexFileLocationChecksum();
415 if (expected_secondary_checksum != actual_secondary_checksum) {
416 VLOG(oat) << "Dex checksum does not match for secondary dex: "
417 << secondary_dex_location
418 << ". Expected: " << expected_secondary_checksum
419 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700420 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800421 }
422 } else {
423 // If we can't get the checksum for the secondary location, we assume
424 // the dex checksum is up to date for this and all other secondary dex
425 // files.
426 break;
427 }
428 }
429
Andreas Gampe29d38e72016-03-23 15:31:51 +0000430 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000431
Richard Uhler66d874d2015-01-15 09:37:19 -0800432 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000433 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
434 const ImageInfo* image_info = GetImageInfo();
435 if (image_info == nullptr) {
436 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000437
Richard Uhler76f5cb62016-04-04 13:30:16 -0700438 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700439 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700440 }
441
442 // If there is no original dex file to fall back to, grudgingly accept
443 // the oat file. This could technically lead to crashes, but there's no
444 // way we could find a better oat file to use for this dex location,
445 // and it's better than being stuck in a boot loop with no way out.
446 // The problem will hopefully resolve itself the next time the runtime
447 // starts up.
448 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
449 << "Allow oat file use. This is potentially dangerous.";
450 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
451 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000452 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700453 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000454 }
455 } else {
456 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800457 }
458
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100459 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000460 if (!file.IsPic()) {
461 const ImageInfo* image_info = GetImageInfo();
462 if (image_info == nullptr) {
463 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700464 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000465 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700466
Andreas Gampe29d38e72016-03-23 15:31:51 +0000467 // Verify the oat_data_begin recorded for the image in the oat file matches
468 // the actual oat_data_begin for boot.oat in the image.
469 const OatHeader& oat_header = file.GetOatHeader();
470 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
471 if (oat_data_begin != image_info->oat_data_begin) {
472 VLOG(oat) << file.GetLocation() <<
473 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
474 << " does not match actual image oat_data_begin ("
475 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700476 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000477 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700478
Andreas Gampe29d38e72016-03-23 15:31:51 +0000479 // Verify the oat_patch_delta recorded for the image in the oat file matches
480 // the actual oat_patch_delta for the image.
481 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
482 if (oat_patch_delta != image_info->patch_delta) {
483 VLOG(oat) << file.GetLocation() <<
484 ": Oat file image patch delta (" << oat_patch_delta << ")"
485 << " does not match actual image patch delta ("
486 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700487 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000488 }
489 } else {
490 // Oat files compiled in PIC mode do not require relocation.
491 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
492 }
493 } else {
494 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800495 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700496 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800497}
498
Richard Uhler1e860612016-03-30 12:17:55 -0700499OatFileAssistant::ResultOfAttemptToUpdate
500OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800501 CHECK(error_msg != nullptr);
502
Richard Uhler95abd042015-03-24 09:51:28 -0700503 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700504 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700505 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700506 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800507 }
Richard Uhler95abd042015-03-24 09:51:28 -0700508 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800509
Richard Uhler743bf362016-04-19 15:39:37 -0700510 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700511 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800512 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700513 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800514 }
Richard Uhler743bf362016-04-19 15:39:37 -0700515 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800516
517 const ImageInfo* image_info = GetImageInfo();
518 Runtime* runtime = Runtime::Current();
519 if (image_info == nullptr) {
520 *error_msg = "Patching of oat file " + oat_file_name
521 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700522 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800523 }
524
525 if (!runtime->IsDex2OatEnabled()) {
526 *error_msg = "Patching of oat file " + oat_file_name
527 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700528 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800529 }
530
531 std::vector<std::string> argv;
532 argv.push_back(runtime->GetPatchoatExecutable());
533 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700534 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800535 argv.push_back("--output-oat-file=" + oat_file_name);
536 argv.push_back("--patched-image-location=" + image_info->location);
537
538 std::string command_line(Join(argv, ' '));
539 if (!Exec(argv, error_msg)) {
540 // Manually delete the file. This ensures there is no garbage left over if
541 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100542 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700543 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800544 }
545
546 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700547 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700548 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800549}
550
Richard Uhler1e860612016-03-30 12:17:55 -0700551OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700552OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800553 CHECK(error_msg != nullptr);
554
Richard Uhler8327cf72015-10-13 16:34:59 -0700555 Runtime* runtime = Runtime::Current();
556 if (!runtime->IsDex2OatEnabled()) {
557 *error_msg = "Generation of oat file for dex location " + dex_location_
558 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700559 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700560 }
561
Richard Uhler743bf362016-04-19 15:39:37 -0700562 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700563 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800564 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700565 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800566 }
Richard Uhler743bf362016-04-19 15:39:37 -0700567 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800568
Richard Uhler66d874d2015-01-15 09:37:19 -0800569 // dex2oat ignores missing dex files and doesn't report an error.
570 // Check explicitly here so we can detect the error properly.
571 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700572 if (!OS::FileExists(dex_location_.c_str())) {
573 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700574 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800575 }
576
Richard Uhler8327cf72015-10-13 16:34:59 -0700577 std::unique_ptr<File> oat_file;
578 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
579 if (oat_file.get() == nullptr) {
580 *error_msg = "Generation of oat file " + oat_file_name
581 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700582 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700583 }
584
585 if (fchmod(oat_file->Fd(), 0644) != 0) {
586 *error_msg = "Generation of oat file " + oat_file_name
587 + " not attempted because the oat file could not be made world readable.";
588 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700589 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700590 }
591
592 std::vector<std::string> args;
593 args.push_back("--dex-file=" + dex_location_);
594 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
595 args.push_back("--oat-location=" + oat_file_name);
596
Richard Uhler66d874d2015-01-15 09:37:19 -0800597 if (!Dex2Oat(args, error_msg)) {
598 // Manually delete the file. This ensures there is no garbage left over if
599 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700600 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100601 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700602 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700603 }
604
605 if (oat_file->FlushCloseOrErase() != 0) {
606 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100607 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700608 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800609 }
610
611 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700612 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700613 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800614}
615
616bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
617 std::string* error_msg) {
618 Runtime* runtime = Runtime::Current();
619 std::string image_location = ImageLocation();
620 if (image_location.empty()) {
621 *error_msg = "No image location found for Dex2Oat.";
622 return false;
623 }
624
625 std::vector<std::string> argv;
626 argv.push_back(runtime->GetCompilerExecutable());
627 argv.push_back("--runtime-arg");
628 argv.push_back("-classpath");
629 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700630 std::string class_path = runtime->GetClassPathString();
631 if (class_path == "") {
632 class_path = OatFile::kSpecialSharedLibrary;
633 }
634 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100635 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200636 argv.push_back("--debuggable");
637 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700638 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800639
640 if (!runtime->IsVerificationEnabled()) {
641 argv.push_back("--compiler-filter=verify-none");
642 }
643
644 if (runtime->MustRelocateIfPossible()) {
645 argv.push_back("--runtime-arg");
646 argv.push_back("-Xrelocate");
647 } else {
648 argv.push_back("--runtime-arg");
649 argv.push_back("-Xnorelocate");
650 }
651
652 if (!kIsTargetBuild) {
653 argv.push_back("--host");
654 }
655
656 argv.push_back("--boot-image=" + image_location);
657
658 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
659 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
660
661 argv.insert(argv.end(), args.begin(), args.end());
662
663 std::string command_line(Join(argv, ' '));
664 return Exec(argv, error_msg);
665}
666
Richard Uhlerb81881d2016-04-19 13:08:04 -0700667bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
668 InstructionSet isa,
669 std::string* odex_filename,
670 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800671 CHECK(odex_filename != nullptr);
672 CHECK(error_msg != nullptr);
673
674 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700675 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800676 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700677 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800678
Richard Uhler63434112015-03-16 14:32:16 -0700679 // Find the directory portion of the dex location and add the oat/<isa>
680 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800681 size_t pos = location.rfind('/');
682 if (pos == std::string::npos) {
683 *error_msg = "Dex location " + location + " has no directory.";
684 return false;
685 }
686 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700687 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800688
689 // Find the file portion of the dex location.
690 std::string file;
691 if (pos == std::string::npos) {
692 file = location;
693 } else {
694 file = location.substr(pos+1);
695 }
696
697 // Get the base part of the file without the extension.
698 pos = file.rfind('.');
699 if (pos == std::string::npos) {
700 *error_msg = "Dex location " + location + " has no extension.";
701 return false;
702 }
703 std::string base = file.substr(0, pos);
704
705 *odex_filename = dir + "/" + base + ".odex";
706 return true;
707}
708
Richard Uhlerb81881d2016-04-19 13:08:04 -0700709bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
710 InstructionSet isa,
711 std::string* oat_filename,
712 std::string* error_msg) {
713 CHECK(oat_filename != nullptr);
714 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800715
716 // TODO: The work done in GetDalvikCache is overkill for what we need.
717 // Ideally a new API for getting the DalvikCacheDirectory the way we want
718 // (without existence testing, creation, or death) is provided with the rest
719 // of the GetDalvikCache family of functions. Until such an API is in place,
720 // we use GetDalvikCache to avoid duplicating the logic for determining the
721 // dalvik cache directory.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700722 std::string dalvik_cache_dir;
723 bool ignored;
724 GetDalvikCache("", false, &dalvik_cache_dir, &ignored, &ignored, &ignored);
725
726 // TODO: The oat file assistant should be the definitive place for
727 // determining the oat file name from the dex location, not
728 // GetDalvikCacheFilename.
729 std::string cache_dir = StringPrintf("%s%s",
730 dalvik_cache_dir.c_str(), GetInstructionSetString(isa));
731 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800732}
733
Richard Uhler66d874d2015-01-15 09:37:19 -0800734std::string OatFileAssistant::ImageLocation() {
735 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000736 const std::vector<gc::space::ImageSpace*>& image_spaces =
737 runtime->GetHeap()->GetBootImageSpaces();
738 if (image_spaces.empty()) {
739 return "";
740 }
741 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800742}
743
744const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700745 if (!required_dex_checksum_attempted_) {
746 required_dex_checksum_attempted_ = true;
747 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800748 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700749 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700750 required_dex_checksum_found_ = true;
751 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800752 } else {
753 // This can happen if the original dex file has been stripped from the
754 // apk.
755 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700756 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800757
758 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700759 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800760 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700761 const OatFile::OatDexFile* odex_dex_file
762 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800763 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700764 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
765 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800766 }
767 }
768 }
769 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700770 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800771}
772
Richard Uhler66d874d2015-01-15 09:37:19 -0800773const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
774 if (!image_info_load_attempted_) {
775 image_info_load_attempted_ = true;
776
777 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800778 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
779 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800780 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800781
782 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800783 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800784 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800785 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
786 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800787 cached_image_info_.patch_delta = image_header.GetPatchDelta();
788 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700789 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800790 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700791 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
792 isa_,
793 &error_msg));
794 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800795 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800796 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
797 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800798 cached_image_info_.patch_delta = image_header->GetPatchDelta();
799 }
800 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800801 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700802
Jeff Haofd336c32016-04-07 19:46:31 -0700803 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800804 }
805 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
806}
807
Jeff Haob11ffb72016-04-07 15:40:54 -0700808// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700809uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700810 uint32_t checksum = 0;
811 std::vector<gc::space::ImageSpace*> image_spaces =
812 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700813 if (isa == kRuntimeISA) {
814 for (gc::space::ImageSpace* image_space : image_spaces) {
815 checksum ^= image_space->GetImageHeader().GetOatChecksum();
816 }
817 } else {
818 for (gc::space::ImageSpace* image_space : image_spaces) {
819 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700820 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700821 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700822 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
823 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700824 checksum ^= image_header->GetOatChecksum();
825 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700826 }
827 return checksum;
828}
829
830uint32_t OatFileAssistant::GetCombinedImageChecksum() {
831 if (!image_info_load_attempted_) {
832 GetImageInfo();
833 }
834 return combined_image_checksum_;
835}
836
Andreas Gampea463b6a2016-08-12 21:53:32 -0700837std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800838 DCHECK(oat_file != nullptr);
839 std::string art_file = ArtFileName(oat_file);
840 if (art_file.empty()) {
841 return nullptr;
842 }
843 std::string error_msg;
844 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700845 std::unique_ptr<gc::space::ImageSpace> ret =
846 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800847 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800848 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
849 }
850 return ret;
851}
852
Richard Uhler743bf362016-04-19 15:39:37 -0700853OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant)
854 : oat_file_assistant_(oat_file_assistant)
855{}
856
857const std::string* OatFileAssistant::OatFileInfo::Filename() {
858 return filename_provided_ ? &filename_ : nullptr;
859}
860
861bool OatFileAssistant::OatFileInfo::Exists() {
862 return GetFile() != nullptr;
863}
864
865OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
866 if (!status_attempted_) {
867 status_attempted_ = true;
868 const OatFile* file = GetFile();
869 if (file == nullptr) {
870 status_ = kOatOutOfDate;
871 } else {
872 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700873 VLOG(oat) << file->GetLocation() << " is " << status_
874 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700875 }
876 }
877 return status_;
878}
879
880bool OatFileAssistant::OatFileInfo::IsOutOfDate() {
881 return Status() == kOatOutOfDate;
882}
883
884bool OatFileAssistant::OatFileInfo::NeedsRelocation() {
885 return Status() == kOatNeedsRelocation;
886}
887
888bool OatFileAssistant::OatFileInfo::IsUpToDate() {
889 return Status() == kOatUpToDate;
890}
891
892CompilerFilter::Filter OatFileAssistant::OatFileInfo::CompilerFilter() {
893 const OatFile* file = GetFile();
894 CHECK(file != nullptr);
895 return file->GetCompilerFilter();
896}
897
898const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
899 CHECK(!file_released_) << "GetFile called after oat file released.";
900 if (!load_attempted_) {
901 load_attempted_ = true;
902 if (filename_provided_) {
903 std::string error_msg;
904 file_.reset(OatFile::Open(filename_.c_str(),
905 filename_.c_str(),
906 nullptr,
907 nullptr,
908 oat_file_assistant_->load_executable_,
909 /*low_4gb*/false,
910 oat_file_assistant_->dex_location_.c_str(),
911 &error_msg));
912 if (file_.get() == nullptr) {
913 VLOG(oat) << "OatFileAssistant test for existing oat file "
914 << filename_ << ": " << error_msg;
915 }
916 }
917 }
918 return file_.get();
919}
920
921bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
922 CompilerFilter::Filter target, bool profile_changed) {
923 const OatFile* file = GetFile();
924 if (file == nullptr) {
925 return false;
926 }
927
928 CompilerFilter::Filter current = file->GetCompilerFilter();
929 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
930 VLOG(oat) << "Compiler filter not okay because Profile changed";
931 return false;
932 }
933 return CompilerFilter::IsAsGoodAs(current, target);
934}
935
936bool OatFileAssistant::OatFileInfo::IsExecutable() {
937 const OatFile* file = GetFile();
938 return (file != nullptr && file->IsExecutable());
939}
940
941bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
942 const OatFile* file = GetFile();
943 return (file != nullptr && file->HasPatchInfo());
944}
945
946void OatFileAssistant::OatFileInfo::Reset() {
947 load_attempted_ = false;
948 file_.reset();
949 status_attempted_ = false;
950}
951
952void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
953 filename_provided_ = true;
954 filename_ = filename;
955 Reset();
956}
957
958std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
959 file_released_ = true;
960 return std::move(file_);
961}
962
Richard Uhler66d874d2015-01-15 09:37:19 -0800963} // namespace art
964