blob: ff00451343cfee6afe58ce35fd6347e42bf0607f [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 Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.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
Richard Uhler66d874d2015-01-15 09:37:19 -0800348const std::string* OatFileAssistant::OatFileName() {
Richard Uhler743bf362016-04-19 15:39:37 -0700349 return oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800350}
351
352bool OatFileAssistant::OatFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700353 return oat_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800354}
355
Richard Uhler95abd042015-03-24 09:51:28 -0700356OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700357 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800358}
359
360bool OatFileAssistant::OatFileIsOutOfDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700361 return oat_.IsOutOfDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800362}
363
364bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler743bf362016-04-19 15:39:37 -0700365 return oat_.NeedsRelocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800366}
367
368bool OatFileAssistant::OatFileIsUpToDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700369 return oat_.IsUpToDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800370}
371
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100372CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
Richard Uhler743bf362016-04-19 15:39:37 -0700373 return oat_.CompilerFilter();
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100374}
375
Richard Uhler95abd042015-03-24 09:51:28 -0700376OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700378 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800379 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700380 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
382 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700383 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700384 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700385 VLOG(oat) << error_msg;
Richard Uhlere8109f72016-04-18 10:40:50 -0700386 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800387 }
388
389 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700390 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700391 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800392 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700393 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700394 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800395 // There are no more secondary dex files to check.
396 break;
397 }
398
Richard Uhler66d874d2015-01-15 09:37:19 -0800399 uint32_t expected_secondary_checksum = 0;
400 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700401 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800402 uint32_t actual_secondary_checksum
403 = secondary_oat_dex_file->GetDexFileLocationChecksum();
404 if (expected_secondary_checksum != actual_secondary_checksum) {
405 VLOG(oat) << "Dex checksum does not match for secondary dex: "
406 << secondary_dex_location
407 << ". Expected: " << expected_secondary_checksum
408 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700409 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800410 }
411 } else {
412 // If we can't get the checksum for the secondary location, we assume
413 // the dex checksum is up to date for this and all other secondary dex
414 // files.
415 break;
416 }
417 }
418
Andreas Gampe29d38e72016-03-23 15:31:51 +0000419 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000420
Richard Uhler66d874d2015-01-15 09:37:19 -0800421 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000422 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
423 const ImageInfo* image_info = GetImageInfo();
424 if (image_info == nullptr) {
425 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000426
Richard Uhler76f5cb62016-04-04 13:30:16 -0700427 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700428 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700429 }
430
431 // If there is no original dex file to fall back to, grudgingly accept
432 // the oat file. This could technically lead to crashes, but there's no
433 // way we could find a better oat file to use for this dex location,
434 // and it's better than being stuck in a boot loop with no way out.
435 // The problem will hopefully resolve itself the next time the runtime
436 // starts up.
437 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
438 << "Allow oat file use. This is potentially dangerous.";
439 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
440 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000441 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700442 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000443 }
444 } else {
445 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800446 }
447
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100448 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000449 if (!file.IsPic()) {
450 const ImageInfo* image_info = GetImageInfo();
451 if (image_info == nullptr) {
452 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700453 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000454 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700455
Andreas Gampe29d38e72016-03-23 15:31:51 +0000456 // Verify the oat_data_begin recorded for the image in the oat file matches
457 // the actual oat_data_begin for boot.oat in the image.
458 const OatHeader& oat_header = file.GetOatHeader();
459 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
460 if (oat_data_begin != image_info->oat_data_begin) {
461 VLOG(oat) << file.GetLocation() <<
462 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
463 << " does not match actual image oat_data_begin ("
464 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700465 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000466 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700467
Andreas Gampe29d38e72016-03-23 15:31:51 +0000468 // Verify the oat_patch_delta recorded for the image in the oat file matches
469 // the actual oat_patch_delta for the image.
470 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
471 if (oat_patch_delta != image_info->patch_delta) {
472 VLOG(oat) << file.GetLocation() <<
473 ": Oat file image patch delta (" << oat_patch_delta << ")"
474 << " does not match actual image patch delta ("
475 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700476 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000477 }
478 } else {
479 // Oat files compiled in PIC mode do not require relocation.
480 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
481 }
482 } else {
483 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700485 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800486}
487
Richard Uhler1e860612016-03-30 12:17:55 -0700488OatFileAssistant::ResultOfAttemptToUpdate
489OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800490 CHECK(error_msg != nullptr);
491
Richard Uhler95abd042015-03-24 09:51:28 -0700492 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700493 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700494 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700495 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800496 }
Richard Uhler95abd042015-03-24 09:51:28 -0700497 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800498
Richard Uhler743bf362016-04-19 15:39:37 -0700499 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700500 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800501 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700502 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800503 }
Richard Uhler743bf362016-04-19 15:39:37 -0700504 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800505
506 const ImageInfo* image_info = GetImageInfo();
507 Runtime* runtime = Runtime::Current();
508 if (image_info == nullptr) {
509 *error_msg = "Patching of oat file " + oat_file_name
510 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700511 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800512 }
513
514 if (!runtime->IsDex2OatEnabled()) {
515 *error_msg = "Patching of oat file " + oat_file_name
516 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700517 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800518 }
519
520 std::vector<std::string> argv;
521 argv.push_back(runtime->GetPatchoatExecutable());
522 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700523 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800524 argv.push_back("--output-oat-file=" + oat_file_name);
525 argv.push_back("--patched-image-location=" + image_info->location);
526
527 std::string command_line(Join(argv, ' '));
528 if (!Exec(argv, error_msg)) {
529 // Manually delete the file. This ensures there is no garbage left over if
530 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100531 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700532 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800533 }
534
535 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700536 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700537 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800538}
539
Richard Uhler1e860612016-03-30 12:17:55 -0700540OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700541OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800542 CHECK(error_msg != nullptr);
543
Richard Uhler8327cf72015-10-13 16:34:59 -0700544 Runtime* runtime = Runtime::Current();
545 if (!runtime->IsDex2OatEnabled()) {
546 *error_msg = "Generation of oat file for dex location " + dex_location_
547 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700548 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700549 }
550
Richard Uhler743bf362016-04-19 15:39:37 -0700551 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700552 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800553 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700554 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800555 }
Richard Uhler743bf362016-04-19 15:39:37 -0700556 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100557 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800558
Richard Uhler66d874d2015-01-15 09:37:19 -0800559 // dex2oat ignores missing dex files and doesn't report an error.
560 // Check explicitly here so we can detect the error properly.
561 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700562 if (!OS::FileExists(dex_location_.c_str())) {
563 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700564 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800565 }
566
David Brazdil7b49e6c2016-09-01 11:06:18 +0100567 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
568 if (vdex_file.get() == nullptr) {
569 *error_msg = "Generation of oat file " + oat_file_name
570 + " not attempted because the vdex file " + vdex_file_name
571 + " could not be opened.";
572 return kUpdateNotAttempted;
573 }
574
575 if (fchmod(vdex_file->Fd(), 0644) != 0) {
576 *error_msg = "Generation of oat file " + oat_file_name
577 + " not attempted because the vdex file " + vdex_file_name
578 + " could not be made world readable.";
579 return kUpdateNotAttempted;
580 }
581
582 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700583 if (oat_file.get() == nullptr) {
584 *error_msg = "Generation of oat file " + oat_file_name
585 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700586 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700587 }
588
589 if (fchmod(oat_file->Fd(), 0644) != 0) {
590 *error_msg = "Generation of oat file " + oat_file_name
591 + " not attempted because the oat file could not be made world readable.";
592 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700593 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700594 }
595
596 std::vector<std::string> args;
597 args.push_back("--dex-file=" + dex_location_);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100598 args.push_back("--vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700599 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
600 args.push_back("--oat-location=" + oat_file_name);
601
Richard Uhler66d874d2015-01-15 09:37:19 -0800602 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100603 // Manually delete the oat and vdex files. This ensures there is no garbage
604 // left over if the process unexpectedly died.
605 vdex_file->Erase();
606 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700607 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100608 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700609 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700610 }
611
David Brazdil7b49e6c2016-09-01 11:06:18 +0100612 if (vdex_file->FlushCloseOrErase() != 0) {
613 *error_msg = "Unable to close vdex file " + vdex_file_name;
614 unlink(vdex_file_name.c_str());
615 return kUpdateFailed;
616 }
617
Richard Uhler8327cf72015-10-13 16:34:59 -0700618 if (oat_file->FlushCloseOrErase() != 0) {
619 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100620 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700621 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800622 }
623
624 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700625 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700626 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800627}
628
629bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
630 std::string* error_msg) {
631 Runtime* runtime = Runtime::Current();
632 std::string image_location = ImageLocation();
633 if (image_location.empty()) {
634 *error_msg = "No image location found for Dex2Oat.";
635 return false;
636 }
637
638 std::vector<std::string> argv;
639 argv.push_back(runtime->GetCompilerExecutable());
640 argv.push_back("--runtime-arg");
641 argv.push_back("-classpath");
642 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700643 std::string class_path = runtime->GetClassPathString();
644 if (class_path == "") {
645 class_path = OatFile::kSpecialSharedLibrary;
646 }
647 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100648 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200649 argv.push_back("--debuggable");
650 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700651 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800652
653 if (!runtime->IsVerificationEnabled()) {
654 argv.push_back("--compiler-filter=verify-none");
655 }
656
657 if (runtime->MustRelocateIfPossible()) {
658 argv.push_back("--runtime-arg");
659 argv.push_back("-Xrelocate");
660 } else {
661 argv.push_back("--runtime-arg");
662 argv.push_back("-Xnorelocate");
663 }
664
665 if (!kIsTargetBuild) {
666 argv.push_back("--host");
667 }
668
669 argv.push_back("--boot-image=" + image_location);
670
671 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
672 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
673
674 argv.insert(argv.end(), args.begin(), args.end());
675
676 std::string command_line(Join(argv, ' '));
677 return Exec(argv, error_msg);
678}
679
Richard Uhlerb81881d2016-04-19 13:08:04 -0700680bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
681 InstructionSet isa,
682 std::string* odex_filename,
683 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800684 CHECK(odex_filename != nullptr);
685 CHECK(error_msg != nullptr);
686
687 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700688 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800689 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700690 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800691
Richard Uhler63434112015-03-16 14:32:16 -0700692 // Find the directory portion of the dex location and add the oat/<isa>
693 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800694 size_t pos = location.rfind('/');
695 if (pos == std::string::npos) {
696 *error_msg = "Dex location " + location + " has no directory.";
697 return false;
698 }
699 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700700 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800701
702 // Find the file portion of the dex location.
703 std::string file;
704 if (pos == std::string::npos) {
705 file = location;
706 } else {
707 file = location.substr(pos+1);
708 }
709
710 // Get the base part of the file without the extension.
711 pos = file.rfind('.');
712 if (pos == std::string::npos) {
713 *error_msg = "Dex location " + location + " has no extension.";
714 return false;
715 }
716 std::string base = file.substr(0, pos);
717
718 *odex_filename = dir + "/" + base + ".odex";
719 return true;
720}
721
Richard Uhlerb81881d2016-04-19 13:08:04 -0700722bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
723 InstructionSet isa,
724 std::string* oat_filename,
725 std::string* error_msg) {
726 CHECK(oat_filename != nullptr);
727 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800728
Richard Uhler55b58b62016-08-12 09:05:13 -0700729 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
730 if (cache_dir.empty()) {
731 *error_msg = "Dalvik cache directory does not exist";
732 return false;
733 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700734
735 // TODO: The oat file assistant should be the definitive place for
736 // determining the oat file name from the dex location, not
737 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700738 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800739}
740
Richard Uhler66d874d2015-01-15 09:37:19 -0800741std::string OatFileAssistant::ImageLocation() {
742 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000743 const std::vector<gc::space::ImageSpace*>& image_spaces =
744 runtime->GetHeap()->GetBootImageSpaces();
745 if (image_spaces.empty()) {
746 return "";
747 }
748 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800749}
750
751const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700752 if (!required_dex_checksum_attempted_) {
753 required_dex_checksum_attempted_ = true;
754 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800755 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700756 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700757 required_dex_checksum_found_ = true;
758 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800759 } else {
760 // This can happen if the original dex file has been stripped from the
761 // apk.
762 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700763 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800764
765 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700766 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800767 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700768 const OatFile::OatDexFile* odex_dex_file
769 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800770 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700771 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
772 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800773 }
774 }
775 }
776 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700777 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800778}
779
Richard Uhler66d874d2015-01-15 09:37:19 -0800780const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
781 if (!image_info_load_attempted_) {
782 image_info_load_attempted_ = true;
783
784 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800785 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
786 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800787 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800788
789 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800790 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800791 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800792 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
793 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800794 cached_image_info_.patch_delta = image_header.GetPatchDelta();
795 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700796 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800797 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700798 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
799 isa_,
800 &error_msg));
801 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800802 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800803 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
804 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800805 cached_image_info_.patch_delta = image_header->GetPatchDelta();
806 }
807 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800808 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700809
Jeff Haofd336c32016-04-07 19:46:31 -0700810 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800811 }
812 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
813}
814
Jeff Haob11ffb72016-04-07 15:40:54 -0700815// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700816uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700817 uint32_t checksum = 0;
818 std::vector<gc::space::ImageSpace*> image_spaces =
819 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700820 if (isa == kRuntimeISA) {
821 for (gc::space::ImageSpace* image_space : image_spaces) {
822 checksum ^= image_space->GetImageHeader().GetOatChecksum();
823 }
824 } else {
825 for (gc::space::ImageSpace* image_space : image_spaces) {
826 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700827 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700828 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700829 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
830 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700831 checksum ^= image_header->GetOatChecksum();
832 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700833 }
834 return checksum;
835}
836
837uint32_t OatFileAssistant::GetCombinedImageChecksum() {
838 if (!image_info_load_attempted_) {
839 GetImageInfo();
840 }
841 return combined_image_checksum_;
842}
843
Andreas Gampea463b6a2016-08-12 21:53:32 -0700844std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800845 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100846 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800847 if (art_file.empty()) {
848 return nullptr;
849 }
850 std::string error_msg;
851 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700852 std::unique_ptr<gc::space::ImageSpace> ret =
853 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800854 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800855 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
856 }
857 return ret;
858}
859
Richard Uhler743bf362016-04-19 15:39:37 -0700860OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant)
861 : oat_file_assistant_(oat_file_assistant)
862{}
863
864const std::string* OatFileAssistant::OatFileInfo::Filename() {
865 return filename_provided_ ? &filename_ : nullptr;
866}
867
868bool OatFileAssistant::OatFileInfo::Exists() {
869 return GetFile() != nullptr;
870}
871
872OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
873 if (!status_attempted_) {
874 status_attempted_ = true;
875 const OatFile* file = GetFile();
876 if (file == nullptr) {
877 status_ = kOatOutOfDate;
878 } else {
879 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700880 VLOG(oat) << file->GetLocation() << " is " << status_
881 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700882 }
883 }
884 return status_;
885}
886
887bool OatFileAssistant::OatFileInfo::IsOutOfDate() {
888 return Status() == kOatOutOfDate;
889}
890
891bool OatFileAssistant::OatFileInfo::NeedsRelocation() {
892 return Status() == kOatNeedsRelocation;
893}
894
895bool OatFileAssistant::OatFileInfo::IsUpToDate() {
896 return Status() == kOatUpToDate;
897}
898
899CompilerFilter::Filter OatFileAssistant::OatFileInfo::CompilerFilter() {
900 const OatFile* file = GetFile();
901 CHECK(file != nullptr);
902 return file->GetCompilerFilter();
903}
904
905const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
906 CHECK(!file_released_) << "GetFile called after oat file released.";
907 if (!load_attempted_) {
908 load_attempted_ = true;
909 if (filename_provided_) {
910 std::string error_msg;
911 file_.reset(OatFile::Open(filename_.c_str(),
912 filename_.c_str(),
913 nullptr,
914 nullptr,
915 oat_file_assistant_->load_executable_,
916 /*low_4gb*/false,
917 oat_file_assistant_->dex_location_.c_str(),
918 &error_msg));
919 if (file_.get() == nullptr) {
920 VLOG(oat) << "OatFileAssistant test for existing oat file "
921 << filename_ << ": " << error_msg;
922 }
923 }
924 }
925 return file_.get();
926}
927
928bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
929 CompilerFilter::Filter target, bool profile_changed) {
930 const OatFile* file = GetFile();
931 if (file == nullptr) {
932 return false;
933 }
934
935 CompilerFilter::Filter current = file->GetCompilerFilter();
936 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
937 VLOG(oat) << "Compiler filter not okay because Profile changed";
938 return false;
939 }
940 return CompilerFilter::IsAsGoodAs(current, target);
941}
942
943bool OatFileAssistant::OatFileInfo::IsExecutable() {
944 const OatFile* file = GetFile();
945 return (file != nullptr && file->IsExecutable());
946}
947
948bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
949 const OatFile* file = GetFile();
950 return (file != nullptr && file->HasPatchInfo());
951}
952
953void OatFileAssistant::OatFileInfo::Reset() {
954 load_attempted_ = false;
955 file_.reset();
956 status_attempted_ = false;
957}
958
959void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
960 filename_provided_ = true;
961 filename_ = filename;
962 Reset();
963}
964
965std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
966 file_released_ = true;
967 return std::move(file_);
968}
969
Richard Uhler66d874d2015-01-15 09:37:19 -0800970} // namespace art
971