blob: fd58907fc8ff5ae7401ec3ec78ffd7ccd04fe429 [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(
280 dex_location, nullptr, false);
281 if (oat_dex_file == nullptr) {
282 LOG(WARNING) << "Attempt to load out-of-date oat file "
283 << oat_file.GetLocation() << " for dex location " << dex_location;
284 return std::vector<std::unique_ptr<const DexFile>>();
285 }
286
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700287 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800288 if (dex_file.get() == nullptr) {
289 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
290 return std::vector<std::unique_ptr<const DexFile>>();
291 }
292 dex_files.push_back(std::move(dex_file));
293
294 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700295 for (size_t i = 1; ; i++) {
296 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800297 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700298 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800299 // There are no more secondary dex files to load.
300 break;
301 }
302
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700303 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800304 if (dex_file.get() == nullptr) {
305 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
306 return std::vector<std::unique_ptr<const DexFile>>();
307 }
308 dex_files.push_back(std::move(dex_file));
309 }
310 return dex_files;
311}
312
Richard Uhler9b994ea2015-06-24 08:44:19 -0700313bool OatFileAssistant::HasOriginalDexFiles() {
314 // Ensure GetRequiredDexChecksum has been run so that
315 // has_original_dex_files_ is initialized. We don't care about the result of
316 // GetRequiredDexChecksum.
317 GetRequiredDexChecksum();
318 return has_original_dex_files_;
319}
320
Richard Uhler66d874d2015-01-15 09:37:19 -0800321const std::string* OatFileAssistant::OdexFileName() {
Richard Uhler743bf362016-04-19 15:39:37 -0700322 return odex_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800323}
324
325bool OatFileAssistant::OdexFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700326 return odex_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800327}
328
Richard Uhler95abd042015-03-24 09:51:28 -0700329OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700330 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800331}
332
333bool OatFileAssistant::OdexFileIsOutOfDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700334 return odex_.IsOutOfDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800335}
336
337bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler743bf362016-04-19 15:39:37 -0700338 return odex_.NeedsRelocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800339}
340
341bool OatFileAssistant::OdexFileIsUpToDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700342 return odex_.IsUpToDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800343}
344
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100345CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
Richard Uhler743bf362016-04-19 15:39:37 -0700346 return odex_.CompilerFilter();
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100347}
Richard Uhler4c7f1932016-04-15 15:51:21 -0700348
349static std::string ArtFileName(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800350 const std::string oat_file_location = oat_file->GetLocation();
351 // Replace extension with .art
352 const size_t last_ext = oat_file_location.find_last_of('.');
353 if (last_ext == std::string::npos) {
354 LOG(ERROR) << "No extension in oat file " << oat_file_location;
355 return std::string();
356 }
357 return oat_file_location.substr(0, last_ext) + ".art";
358}
359
Richard Uhler66d874d2015-01-15 09:37:19 -0800360const std::string* OatFileAssistant::OatFileName() {
Richard Uhler743bf362016-04-19 15:39:37 -0700361 return oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800362}
363
364bool OatFileAssistant::OatFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700365 return oat_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800366}
367
Richard Uhler95abd042015-03-24 09:51:28 -0700368OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700369 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800370}
371
372bool OatFileAssistant::OatFileIsOutOfDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700373 return oat_.IsOutOfDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800374}
375
376bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler743bf362016-04-19 15:39:37 -0700377 return oat_.NeedsRelocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800378}
379
380bool OatFileAssistant::OatFileIsUpToDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700381 return oat_.IsUpToDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800382}
383
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100384CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
Richard Uhler743bf362016-04-19 15:39:37 -0700385 return oat_.CompilerFilter();
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100386}
387
Richard Uhler95abd042015-03-24 09:51:28 -0700388OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800389 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700390 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800391 // what we provide, which verifies the primary dex checksum for us.
392 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
393 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700394 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700395 if (oat_dex_file == nullptr) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700396 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800397 }
398
399 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700400 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800401 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700402 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800403 const OatFile::OatDexFile* secondary_oat_dex_file
404 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
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
410 std::string error_msg;
411 uint32_t expected_secondary_checksum = 0;
412 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700413 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800414 uint32_t actual_secondary_checksum
415 = secondary_oat_dex_file->GetDexFileLocationChecksum();
416 if (expected_secondary_checksum != actual_secondary_checksum) {
417 VLOG(oat) << "Dex checksum does not match for secondary dex: "
418 << secondary_dex_location
419 << ". Expected: " << expected_secondary_checksum
420 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700421 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800422 }
423 } else {
424 // If we can't get the checksum for the secondary location, we assume
425 // the dex checksum is up to date for this and all other secondary dex
426 // files.
427 break;
428 }
429 }
430
Andreas Gampe29d38e72016-03-23 15:31:51 +0000431 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
432 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000433
Richard Uhler66d874d2015-01-15 09:37:19 -0800434 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000435 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
436 const ImageInfo* image_info = GetImageInfo();
437 if (image_info == nullptr) {
438 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000439
Richard Uhler76f5cb62016-04-04 13:30:16 -0700440 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700441 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700442 }
443
444 // If there is no original dex file to fall back to, grudgingly accept
445 // the oat file. This could technically lead to crashes, but there's no
446 // way we could find a better oat file to use for this dex location,
447 // and it's better than being stuck in a boot loop with no way out.
448 // The problem will hopefully resolve itself the next time the runtime
449 // starts up.
450 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
451 << "Allow oat file use. This is potentially dangerous.";
452 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
453 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000454 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700455 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000456 }
457 } else {
458 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 }
460
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100461 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 if (!file.IsPic()) {
463 const ImageInfo* image_info = GetImageInfo();
464 if (image_info == nullptr) {
465 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700466 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000467 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700468
Andreas Gampe29d38e72016-03-23 15:31:51 +0000469 // Verify the oat_data_begin recorded for the image in the oat file matches
470 // the actual oat_data_begin for boot.oat in the image.
471 const OatHeader& oat_header = file.GetOatHeader();
472 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
473 if (oat_data_begin != image_info->oat_data_begin) {
474 VLOG(oat) << file.GetLocation() <<
475 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
476 << " does not match actual image oat_data_begin ("
477 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700478 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000479 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700480
Andreas Gampe29d38e72016-03-23 15:31:51 +0000481 // Verify the oat_patch_delta recorded for the image in the oat file matches
482 // the actual oat_patch_delta for the image.
483 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
484 if (oat_patch_delta != image_info->patch_delta) {
485 VLOG(oat) << file.GetLocation() <<
486 ": Oat file image patch delta (" << oat_patch_delta << ")"
487 << " does not match actual image patch delta ("
488 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700489 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000490 }
491 } else {
492 // Oat files compiled in PIC mode do not require relocation.
493 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
494 }
495 } else {
496 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700498 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800499}
500
Richard Uhler1e860612016-03-30 12:17:55 -0700501OatFileAssistant::ResultOfAttemptToUpdate
502OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800503 CHECK(error_msg != nullptr);
504
Richard Uhler95abd042015-03-24 09:51:28 -0700505 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700506 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700507 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700508 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800509 }
Richard Uhler95abd042015-03-24 09:51:28 -0700510 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800511
Richard Uhler743bf362016-04-19 15:39:37 -0700512 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700513 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800514 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700515 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800516 }
Richard Uhler743bf362016-04-19 15:39:37 -0700517 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800518
519 const ImageInfo* image_info = GetImageInfo();
520 Runtime* runtime = Runtime::Current();
521 if (image_info == nullptr) {
522 *error_msg = "Patching of oat file " + oat_file_name
523 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700524 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800525 }
526
527 if (!runtime->IsDex2OatEnabled()) {
528 *error_msg = "Patching of oat file " + oat_file_name
529 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700530 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800531 }
532
533 std::vector<std::string> argv;
534 argv.push_back(runtime->GetPatchoatExecutable());
535 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700536 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800537 argv.push_back("--output-oat-file=" + oat_file_name);
538 argv.push_back("--patched-image-location=" + image_info->location);
539
540 std::string command_line(Join(argv, ' '));
541 if (!Exec(argv, error_msg)) {
542 // Manually delete the file. This ensures there is no garbage left over if
543 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100544 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700545 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800546 }
547
548 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700549 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700550 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800551}
552
Richard Uhler1e860612016-03-30 12:17:55 -0700553OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700554OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800555 CHECK(error_msg != nullptr);
556
Richard Uhler8327cf72015-10-13 16:34:59 -0700557 Runtime* runtime = Runtime::Current();
558 if (!runtime->IsDex2OatEnabled()) {
559 *error_msg = "Generation of oat file for dex location " + dex_location_
560 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700561 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700562 }
563
Richard Uhler743bf362016-04-19 15:39:37 -0700564 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700565 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800566 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700567 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800568 }
Richard Uhler743bf362016-04-19 15:39:37 -0700569 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800570
Richard Uhler66d874d2015-01-15 09:37:19 -0800571 // dex2oat ignores missing dex files and doesn't report an error.
572 // Check explicitly here so we can detect the error properly.
573 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700574 if (!OS::FileExists(dex_location_.c_str())) {
575 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700576 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800577 }
578
Richard Uhler8327cf72015-10-13 16:34:59 -0700579 std::unique_ptr<File> oat_file;
580 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
581 if (oat_file.get() == nullptr) {
582 *error_msg = "Generation of oat file " + oat_file_name
583 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700584 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700585 }
586
587 if (fchmod(oat_file->Fd(), 0644) != 0) {
588 *error_msg = "Generation of oat file " + oat_file_name
589 + " not attempted because the oat file could not be made world readable.";
590 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700591 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700592 }
593
594 std::vector<std::string> args;
595 args.push_back("--dex-file=" + dex_location_);
596 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
597 args.push_back("--oat-location=" + oat_file_name);
598
Richard Uhler66d874d2015-01-15 09:37:19 -0800599 if (!Dex2Oat(args, error_msg)) {
600 // Manually delete the file. This ensures there is no garbage left over if
601 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700602 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100603 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700604 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700605 }
606
607 if (oat_file->FlushCloseOrErase() != 0) {
608 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100609 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700610 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800611 }
612
613 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700614 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700615 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800616}
617
618bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
619 std::string* error_msg) {
620 Runtime* runtime = Runtime::Current();
621 std::string image_location = ImageLocation();
622 if (image_location.empty()) {
623 *error_msg = "No image location found for Dex2Oat.";
624 return false;
625 }
626
627 std::vector<std::string> argv;
628 argv.push_back(runtime->GetCompilerExecutable());
629 argv.push_back("--runtime-arg");
630 argv.push_back("-classpath");
631 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700632 std::string class_path = runtime->GetClassPathString();
633 if (class_path == "") {
634 class_path = OatFile::kSpecialSharedLibrary;
635 }
636 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100637 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200638 argv.push_back("--debuggable");
639 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700640 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800641
642 if (!runtime->IsVerificationEnabled()) {
643 argv.push_back("--compiler-filter=verify-none");
644 }
645
646 if (runtime->MustRelocateIfPossible()) {
647 argv.push_back("--runtime-arg");
648 argv.push_back("-Xrelocate");
649 } else {
650 argv.push_back("--runtime-arg");
651 argv.push_back("-Xnorelocate");
652 }
653
654 if (!kIsTargetBuild) {
655 argv.push_back("--host");
656 }
657
658 argv.push_back("--boot-image=" + image_location);
659
660 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
661 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
662
663 argv.insert(argv.end(), args.begin(), args.end());
664
665 std::string command_line(Join(argv, ' '));
666 return Exec(argv, error_msg);
667}
668
Richard Uhlerb81881d2016-04-19 13:08:04 -0700669bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
670 InstructionSet isa,
671 std::string* odex_filename,
672 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800673 CHECK(odex_filename != nullptr);
674 CHECK(error_msg != nullptr);
675
676 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700677 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800678 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700679 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800680
Richard Uhler63434112015-03-16 14:32:16 -0700681 // Find the directory portion of the dex location and add the oat/<isa>
682 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800683 size_t pos = location.rfind('/');
684 if (pos == std::string::npos) {
685 *error_msg = "Dex location " + location + " has no directory.";
686 return false;
687 }
688 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700689 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800690
691 // Find the file portion of the dex location.
692 std::string file;
693 if (pos == std::string::npos) {
694 file = location;
695 } else {
696 file = location.substr(pos+1);
697 }
698
699 // Get the base part of the file without the extension.
700 pos = file.rfind('.');
701 if (pos == std::string::npos) {
702 *error_msg = "Dex location " + location + " has no extension.";
703 return false;
704 }
705 std::string base = file.substr(0, pos);
706
707 *odex_filename = dir + "/" + base + ".odex";
708 return true;
709}
710
Richard Uhlerb81881d2016-04-19 13:08:04 -0700711bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
712 InstructionSet isa,
713 std::string* oat_filename,
714 std::string* error_msg) {
715 CHECK(oat_filename != nullptr);
716 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800717
718 // TODO: The work done in GetDalvikCache is overkill for what we need.
719 // Ideally a new API for getting the DalvikCacheDirectory the way we want
720 // (without existence testing, creation, or death) is provided with the rest
721 // of the GetDalvikCache family of functions. Until such an API is in place,
722 // we use GetDalvikCache to avoid duplicating the logic for determining the
723 // dalvik cache directory.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700724 std::string dalvik_cache_dir;
725 bool ignored;
726 GetDalvikCache("", false, &dalvik_cache_dir, &ignored, &ignored, &ignored);
727
728 // TODO: The oat file assistant should be the definitive place for
729 // determining the oat file name from the dex location, not
730 // GetDalvikCacheFilename.
731 std::string cache_dir = StringPrintf("%s%s",
732 dalvik_cache_dir.c_str(), GetInstructionSetString(isa));
733 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800734}
735
Richard Uhler66d874d2015-01-15 09:37:19 -0800736std::string OatFileAssistant::ImageLocation() {
737 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000738 const std::vector<gc::space::ImageSpace*>& image_spaces =
739 runtime->GetHeap()->GetBootImageSpaces();
740 if (image_spaces.empty()) {
741 return "";
742 }
743 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800744}
745
746const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700747 if (!required_dex_checksum_attempted_) {
748 required_dex_checksum_attempted_ = true;
749 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800750 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700751 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700752 required_dex_checksum_found_ = true;
753 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800754 } else {
755 // This can happen if the original dex file has been stripped from the
756 // apk.
757 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700758 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800759
760 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700761 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800762 if (odex_file != nullptr) {
763 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700764 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800765 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700766 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
767 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800768 }
769 }
770 }
771 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700772 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800773}
774
Richard Uhler66d874d2015-01-15 09:37:19 -0800775const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
776 if (!image_info_load_attempted_) {
777 image_info_load_attempted_ = true;
778
779 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800780 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
781 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800782 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800783
784 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800785 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800786 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800787 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
788 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800789 cached_image_info_.patch_delta = image_header.GetPatchDelta();
790 } else {
791 std::unique_ptr<ImageHeader> image_header(
Jeff Haob11ffb72016-04-07 15:40:54 -0700792 gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_));
Richard Uhler66d874d2015-01-15 09:37:19 -0800793 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800794 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
795 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800796 cached_image_info_.patch_delta = image_header->GetPatchDelta();
797 }
798 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800799 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700800
Jeff Haofd336c32016-04-07 19:46:31 -0700801 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800802 }
803 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
804}
805
Jeff Haob11ffb72016-04-07 15:40:54 -0700806// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700807uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700808 uint32_t checksum = 0;
809 std::vector<gc::space::ImageSpace*> image_spaces =
810 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700811 if (isa == kRuntimeISA) {
812 for (gc::space::ImageSpace* image_space : image_spaces) {
813 checksum ^= image_space->GetImageHeader().GetOatChecksum();
814 }
815 } else {
816 for (gc::space::ImageSpace* image_space : image_spaces) {
817 std::string location = image_space->GetImageLocation();
818 std::unique_ptr<ImageHeader> image_header(
819 gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa));
820 checksum ^= image_header->GetOatChecksum();
821 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700822 }
823 return checksum;
824}
825
826uint32_t OatFileAssistant::GetCombinedImageChecksum() {
827 if (!image_info_load_attempted_) {
828 GetImageInfo();
829 }
830 return combined_image_checksum_;
831}
832
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800833gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
834 DCHECK(oat_file != nullptr);
835 std::string art_file = ArtFileName(oat_file);
836 if (art_file.empty()) {
837 return nullptr;
838 }
839 std::string error_msg;
840 ScopedObjectAccess soa(Thread::Current());
841 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
842 oat_file,
843 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800844 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800845 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
846 }
847 return ret;
848}
849
Richard Uhler743bf362016-04-19 15:39:37 -0700850OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant)
851 : oat_file_assistant_(oat_file_assistant)
852{}
853
854const std::string* OatFileAssistant::OatFileInfo::Filename() {
855 return filename_provided_ ? &filename_ : nullptr;
856}
857
858bool OatFileAssistant::OatFileInfo::Exists() {
859 return GetFile() != nullptr;
860}
861
862OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
863 if (!status_attempted_) {
864 status_attempted_ = true;
865 const OatFile* file = GetFile();
866 if (file == nullptr) {
867 status_ = kOatOutOfDate;
868 } else {
869 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
870 }
871 }
872 return status_;
873}
874
875bool OatFileAssistant::OatFileInfo::IsOutOfDate() {
876 return Status() == kOatOutOfDate;
877}
878
879bool OatFileAssistant::OatFileInfo::NeedsRelocation() {
880 return Status() == kOatNeedsRelocation;
881}
882
883bool OatFileAssistant::OatFileInfo::IsUpToDate() {
884 return Status() == kOatUpToDate;
885}
886
887CompilerFilter::Filter OatFileAssistant::OatFileInfo::CompilerFilter() {
888 const OatFile* file = GetFile();
889 CHECK(file != nullptr);
890 return file->GetCompilerFilter();
891}
892
893const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
894 CHECK(!file_released_) << "GetFile called after oat file released.";
895 if (!load_attempted_) {
896 load_attempted_ = true;
897 if (filename_provided_) {
898 std::string error_msg;
899 file_.reset(OatFile::Open(filename_.c_str(),
900 filename_.c_str(),
901 nullptr,
902 nullptr,
903 oat_file_assistant_->load_executable_,
904 /*low_4gb*/false,
905 oat_file_assistant_->dex_location_.c_str(),
906 &error_msg));
907 if (file_.get() == nullptr) {
908 VLOG(oat) << "OatFileAssistant test for existing oat file "
909 << filename_ << ": " << error_msg;
910 }
911 }
912 }
913 return file_.get();
914}
915
916bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
917 CompilerFilter::Filter target, bool profile_changed) {
918 const OatFile* file = GetFile();
919 if (file == nullptr) {
920 return false;
921 }
922
923 CompilerFilter::Filter current = file->GetCompilerFilter();
924 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
925 VLOG(oat) << "Compiler filter not okay because Profile changed";
926 return false;
927 }
928 return CompilerFilter::IsAsGoodAs(current, target);
929}
930
931bool OatFileAssistant::OatFileInfo::IsExecutable() {
932 const OatFile* file = GetFile();
933 return (file != nullptr && file->IsExecutable());
934}
935
936bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
937 const OatFile* file = GetFile();
938 return (file != nullptr && file->HasPatchInfo());
939}
940
941void OatFileAssistant::OatFileInfo::Reset() {
942 load_attempted_ = false;
943 file_.reset();
944 status_attempted_ = false;
945}
946
947void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
948 filename_provided_ = true;
949 filename_ = filename;
950 Reset();
951}
952
953std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
954 file_released_ = true;
955 return std::move(file_);
956}
957
Richard Uhler66d874d2015-01-15 09:37:19 -0800958} // namespace art
959