blob: bb52c752fe727803707699886a5856a14c8b94b4 [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) {
Richard Uhler70a84262016-11-08 16:51:51 +0000140 if (!oat_.IsOutOfDate()) {
141 DexOptNeeded dexopt_needed = oat_.GetDexOptNeeded(target, profile_changed);
142 if (dexopt_needed == kPatchOatNeeded) {
143 dexopt_needed = kSelfPatchOatNeeded;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000144 }
Richard Uhler70a84262016-11-08 16:51:51 +0000145 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800146 }
Richard Uhler70a84262016-11-08 16:51:51 +0000147 return odex_.GetDexOptNeeded(target, profile_changed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800148}
149
Richard Uhlerf4b34872016-04-13 11:03:46 -0700150// Figure out the currently specified compile filter option in the runtime.
151// Returns true on success, false if the compiler filter is invalid, in which
152// case error_msg describes the problem.
153static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
154 std::string* error_msg) {
155 CHECK(filter != nullptr);
156 CHECK(error_msg != nullptr);
157
158 *filter = CompilerFilter::kDefaultCompilerFilter;
159 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
160 if (option.starts_with("--compiler-filter=")) {
161 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
162 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
163 *error_msg = std::string("Unknown --compiler-filter value: ")
164 + std::string(compiler_filter_string);
165 return false;
166 }
167 }
168 }
169 return true;
170}
171
Richard Uhler01be6812016-05-17 10:34:52 -0700172bool OatFileAssistant::IsUpToDate() {
173 return OatFileIsUpToDate() || OdexFileIsUpToDate();
174}
175
Richard Uhler1e860612016-03-30 12:17:55 -0700176OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700177OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700178 CompilerFilter::Filter target;
179 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
180 return kUpdateNotAttempted;
181 }
182
Richard Uhlerd1472a22016-04-15 15:18:56 -0700183 switch (GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700184 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700185 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler743bf362016-04-19 15:39:37 -0700186 case kPatchOatNeeded: return RelocateOatFile(odex_.Filename(), error_msg);
187 case kSelfPatchOatNeeded: return RelocateOatFile(oat_.Filename(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800188 }
189 UNREACHABLE();
190}
191
192std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler743bf362016-04-19 15:39:37 -0700193 if (!oat_.IsOutOfDate()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000194 return oat_.ReleaseFileForUse();
Richard Uhler5f946da2015-07-17 12:28:32 -0700195 }
Richard Uhler70a84262016-11-08 16:51:51 +0000196 return odex_.ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800197}
198
199std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
200 const OatFile& oat_file, const char* dex_location) {
201 std::vector<std::unique_ptr<const DexFile>> dex_files;
202
203 // Load the primary dex file.
204 std::string error_msg;
205 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700206 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800207 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700208 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800209 return std::vector<std::unique_ptr<const DexFile>>();
210 }
211
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700212 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800213 if (dex_file.get() == nullptr) {
214 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
215 return std::vector<std::unique_ptr<const DexFile>>();
216 }
217 dex_files.push_back(std::move(dex_file));
218
219 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700220 for (size_t i = 1; ; i++) {
221 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700222 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700223 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800224 // There are no more secondary dex files to load.
225 break;
226 }
227
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700228 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800229 if (dex_file.get() == nullptr) {
230 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
231 return std::vector<std::unique_ptr<const DexFile>>();
232 }
233 dex_files.push_back(std::move(dex_file));
234 }
235 return dex_files;
236}
237
Richard Uhler9b994ea2015-06-24 08:44:19 -0700238bool OatFileAssistant::HasOriginalDexFiles() {
239 // Ensure GetRequiredDexChecksum has been run so that
240 // has_original_dex_files_ is initialized. We don't care about the result of
241 // GetRequiredDexChecksum.
242 GetRequiredDexChecksum();
243 return has_original_dex_files_;
244}
245
Richard Uhler66d874d2015-01-15 09:37:19 -0800246const std::string* OatFileAssistant::OdexFileName() {
Richard Uhler743bf362016-04-19 15:39:37 -0700247 return odex_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800248}
249
250bool OatFileAssistant::OdexFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700251 return odex_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800252}
253
Richard Uhler95abd042015-03-24 09:51:28 -0700254OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700255 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800256}
257
258bool OatFileAssistant::OdexFileIsOutOfDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700259 return odex_.IsOutOfDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800260}
261
262bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler743bf362016-04-19 15:39:37 -0700263 return odex_.NeedsRelocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800264}
265
266bool OatFileAssistant::OdexFileIsUpToDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700267 return odex_.IsUpToDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800268}
269
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100270CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
Richard Uhler743bf362016-04-19 15:39:37 -0700271 return odex_.CompilerFilter();
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100272}
Richard Uhler4c7f1932016-04-15 15:51:21 -0700273
Richard Uhler66d874d2015-01-15 09:37:19 -0800274const std::string* OatFileAssistant::OatFileName() {
Richard Uhler743bf362016-04-19 15:39:37 -0700275 return oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800276}
277
278bool OatFileAssistant::OatFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700279 return oat_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800280}
281
Richard Uhler95abd042015-03-24 09:51:28 -0700282OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700283 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800284}
285
286bool OatFileAssistant::OatFileIsOutOfDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700287 return oat_.IsOutOfDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800288}
289
290bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler743bf362016-04-19 15:39:37 -0700291 return oat_.NeedsRelocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800292}
293
294bool OatFileAssistant::OatFileIsUpToDate() {
Richard Uhler743bf362016-04-19 15:39:37 -0700295 return oat_.IsUpToDate();
Richard Uhler66d874d2015-01-15 09:37:19 -0800296}
297
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100298CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
Richard Uhler743bf362016-04-19 15:39:37 -0700299 return oat_.CompilerFilter();
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100300}
301
Richard Uhler95abd042015-03-24 09:51:28 -0700302OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700304 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800305 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700306 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800307 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
308 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700309 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700310 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700311 VLOG(oat) << error_msg;
Richard Uhlere8109f72016-04-18 10:40:50 -0700312 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800313 }
314
315 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700316 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700317 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800318 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700319 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700320 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800321 // There are no more secondary dex files to check.
322 break;
323 }
324
Richard Uhler66d874d2015-01-15 09:37:19 -0800325 uint32_t expected_secondary_checksum = 0;
326 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700327 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800328 uint32_t actual_secondary_checksum
329 = secondary_oat_dex_file->GetDexFileLocationChecksum();
330 if (expected_secondary_checksum != actual_secondary_checksum) {
331 VLOG(oat) << "Dex checksum does not match for secondary dex: "
332 << secondary_dex_location
333 << ". Expected: " << expected_secondary_checksum
334 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700335 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800336 }
337 } else {
338 // If we can't get the checksum for the secondary location, we assume
339 // the dex checksum is up to date for this and all other secondary dex
340 // files.
341 break;
342 }
343 }
344
Andreas Gampe29d38e72016-03-23 15:31:51 +0000345 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000346
Richard Uhler66d874d2015-01-15 09:37:19 -0800347 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000348 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
349 const ImageInfo* image_info = GetImageInfo();
350 if (image_info == nullptr) {
351 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000352
Richard Uhler76f5cb62016-04-04 13:30:16 -0700353 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700354 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700355 }
356
357 // If there is no original dex file to fall back to, grudgingly accept
358 // the oat file. This could technically lead to crashes, but there's no
359 // way we could find a better oat file to use for this dex location,
360 // and it's better than being stuck in a boot loop with no way out.
361 // The problem will hopefully resolve itself the next time the runtime
362 // starts up.
363 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
364 << "Allow oat file use. This is potentially dangerous.";
365 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
366 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000367 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700368 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000369 }
370 } else {
371 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 }
373
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100374 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000375 if (!file.IsPic()) {
376 const ImageInfo* image_info = GetImageInfo();
377 if (image_info == nullptr) {
378 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700379 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000380 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700381
Andreas Gampe29d38e72016-03-23 15:31:51 +0000382 // Verify the oat_data_begin recorded for the image in the oat file matches
383 // the actual oat_data_begin for boot.oat in the image.
384 const OatHeader& oat_header = file.GetOatHeader();
385 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
386 if (oat_data_begin != image_info->oat_data_begin) {
387 VLOG(oat) << file.GetLocation() <<
388 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
389 << " does not match actual image oat_data_begin ("
390 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700391 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000392 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700393
Andreas Gampe29d38e72016-03-23 15:31:51 +0000394 // Verify the oat_patch_delta recorded for the image in the oat file matches
395 // the actual oat_patch_delta for the image.
396 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
397 if (oat_patch_delta != image_info->patch_delta) {
398 VLOG(oat) << file.GetLocation() <<
399 ": Oat file image patch delta (" << oat_patch_delta << ")"
400 << " does not match actual image patch delta ("
401 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700402 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000403 }
404 } else {
405 // Oat files compiled in PIC mode do not require relocation.
406 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
407 }
408 } else {
409 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800410 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700411 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800412}
413
Richard Uhler1e860612016-03-30 12:17:55 -0700414OatFileAssistant::ResultOfAttemptToUpdate
415OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800416 CHECK(error_msg != nullptr);
417
Richard Uhler95abd042015-03-24 09:51:28 -0700418 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700419 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700420 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700421 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800422 }
Richard Uhler95abd042015-03-24 09:51:28 -0700423 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800424
Richard Uhler743bf362016-04-19 15:39:37 -0700425 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700426 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800427 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700428 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800429 }
Richard Uhler743bf362016-04-19 15:39:37 -0700430 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800431
432 const ImageInfo* image_info = GetImageInfo();
433 Runtime* runtime = Runtime::Current();
434 if (image_info == nullptr) {
435 *error_msg = "Patching of oat file " + oat_file_name
436 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700437 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800438 }
439
440 if (!runtime->IsDex2OatEnabled()) {
441 *error_msg = "Patching of oat file " + oat_file_name
442 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700443 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800444 }
445
446 std::vector<std::string> argv;
447 argv.push_back(runtime->GetPatchoatExecutable());
448 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700449 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800450 argv.push_back("--output-oat-file=" + oat_file_name);
451 argv.push_back("--patched-image-location=" + image_info->location);
452
453 std::string command_line(Join(argv, ' '));
454 if (!Exec(argv, error_msg)) {
455 // Manually delete the file. This ensures there is no garbage left over if
456 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100457 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700458 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 }
460
461 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700462 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700463 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800464}
465
Richard Uhler1e860612016-03-30 12:17:55 -0700466OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700467OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800468 CHECK(error_msg != nullptr);
469
Richard Uhler8327cf72015-10-13 16:34:59 -0700470 Runtime* runtime = Runtime::Current();
471 if (!runtime->IsDex2OatEnabled()) {
472 *error_msg = "Generation of oat file for dex location " + dex_location_
473 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700474 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700475 }
476
Richard Uhler743bf362016-04-19 15:39:37 -0700477 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700478 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800479 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700480 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800481 }
Richard Uhler743bf362016-04-19 15:39:37 -0700482 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100483 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800484
Richard Uhler66d874d2015-01-15 09:37:19 -0800485 // dex2oat ignores missing dex files and doesn't report an error.
486 // Check explicitly here so we can detect the error properly.
487 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700488 if (!OS::FileExists(dex_location_.c_str())) {
489 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700490 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800491 }
492
David Brazdil7b49e6c2016-09-01 11:06:18 +0100493 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
494 if (vdex_file.get() == nullptr) {
495 *error_msg = "Generation of oat file " + oat_file_name
496 + " not attempted because the vdex file " + vdex_file_name
497 + " could not be opened.";
498 return kUpdateNotAttempted;
499 }
500
501 if (fchmod(vdex_file->Fd(), 0644) != 0) {
502 *error_msg = "Generation of oat file " + oat_file_name
503 + " not attempted because the vdex file " + vdex_file_name
504 + " could not be made world readable.";
505 return kUpdateNotAttempted;
506 }
507
508 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700509 if (oat_file.get() == nullptr) {
510 *error_msg = "Generation of oat file " + oat_file_name
511 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700512 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700513 }
514
515 if (fchmod(oat_file->Fd(), 0644) != 0) {
516 *error_msg = "Generation of oat file " + oat_file_name
517 + " not attempted because the oat file could not be made world readable.";
518 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700519 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700520 }
521
522 std::vector<std::string> args;
523 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000524 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700525 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
526 args.push_back("--oat-location=" + oat_file_name);
527
Richard Uhler66d874d2015-01-15 09:37:19 -0800528 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100529 // Manually delete the oat and vdex files. This ensures there is no garbage
530 // left over if the process unexpectedly died.
531 vdex_file->Erase();
532 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700533 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100534 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700535 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700536 }
537
David Brazdil7b49e6c2016-09-01 11:06:18 +0100538 if (vdex_file->FlushCloseOrErase() != 0) {
539 *error_msg = "Unable to close vdex file " + vdex_file_name;
540 unlink(vdex_file_name.c_str());
541 return kUpdateFailed;
542 }
543
Richard Uhler8327cf72015-10-13 16:34:59 -0700544 if (oat_file->FlushCloseOrErase() != 0) {
545 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100546 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700547 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800548 }
549
550 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700551 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700552 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800553}
554
555bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
556 std::string* error_msg) {
557 Runtime* runtime = Runtime::Current();
558 std::string image_location = ImageLocation();
559 if (image_location.empty()) {
560 *error_msg = "No image location found for Dex2Oat.";
561 return false;
562 }
563
564 std::vector<std::string> argv;
565 argv.push_back(runtime->GetCompilerExecutable());
566 argv.push_back("--runtime-arg");
567 argv.push_back("-classpath");
568 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700569 std::string class_path = runtime->GetClassPathString();
570 if (class_path == "") {
571 class_path = OatFile::kSpecialSharedLibrary;
572 }
573 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100574 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200575 argv.push_back("--debuggable");
576 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700577 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800578
579 if (!runtime->IsVerificationEnabled()) {
580 argv.push_back("--compiler-filter=verify-none");
581 }
582
583 if (runtime->MustRelocateIfPossible()) {
584 argv.push_back("--runtime-arg");
585 argv.push_back("-Xrelocate");
586 } else {
587 argv.push_back("--runtime-arg");
588 argv.push_back("-Xnorelocate");
589 }
590
591 if (!kIsTargetBuild) {
592 argv.push_back("--host");
593 }
594
595 argv.push_back("--boot-image=" + image_location);
596
597 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
598 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
599
600 argv.insert(argv.end(), args.begin(), args.end());
601
602 std::string command_line(Join(argv, ' '));
603 return Exec(argv, error_msg);
604}
605
Richard Uhlerb81881d2016-04-19 13:08:04 -0700606bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
607 InstructionSet isa,
608 std::string* odex_filename,
609 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800610 CHECK(odex_filename != nullptr);
611 CHECK(error_msg != nullptr);
612
613 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700614 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800615 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700616 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800617
Richard Uhler63434112015-03-16 14:32:16 -0700618 // Find the directory portion of the dex location and add the oat/<isa>
619 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800620 size_t pos = location.rfind('/');
621 if (pos == std::string::npos) {
622 *error_msg = "Dex location " + location + " has no directory.";
623 return false;
624 }
625 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700626 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800627
628 // Find the file portion of the dex location.
629 std::string file;
630 if (pos == std::string::npos) {
631 file = location;
632 } else {
633 file = location.substr(pos+1);
634 }
635
636 // Get the base part of the file without the extension.
637 pos = file.rfind('.');
638 if (pos == std::string::npos) {
639 *error_msg = "Dex location " + location + " has no extension.";
640 return false;
641 }
642 std::string base = file.substr(0, pos);
643
644 *odex_filename = dir + "/" + base + ".odex";
645 return true;
646}
647
Richard Uhlerb81881d2016-04-19 13:08:04 -0700648bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
649 InstructionSet isa,
650 std::string* oat_filename,
651 std::string* error_msg) {
652 CHECK(oat_filename != nullptr);
653 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800654
Richard Uhler55b58b62016-08-12 09:05:13 -0700655 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
656 if (cache_dir.empty()) {
657 *error_msg = "Dalvik cache directory does not exist";
658 return false;
659 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700660
661 // TODO: The oat file assistant should be the definitive place for
662 // determining the oat file name from the dex location, not
663 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700664 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800665}
666
Richard Uhler66d874d2015-01-15 09:37:19 -0800667std::string OatFileAssistant::ImageLocation() {
668 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000669 const std::vector<gc::space::ImageSpace*>& image_spaces =
670 runtime->GetHeap()->GetBootImageSpaces();
671 if (image_spaces.empty()) {
672 return "";
673 }
674 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800675}
676
677const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700678 if (!required_dex_checksum_attempted_) {
679 required_dex_checksum_attempted_ = true;
680 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800681 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700682 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700683 required_dex_checksum_found_ = true;
684 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800685 } else {
686 // This can happen if the original dex file has been stripped from the
687 // apk.
688 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700689 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800690
691 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700692 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800693 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700694 const OatFile::OatDexFile* odex_dex_file
695 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800696 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700697 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
698 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800699 }
700 }
701 }
702 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700703 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800704}
705
Richard Uhler66d874d2015-01-15 09:37:19 -0800706const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
707 if (!image_info_load_attempted_) {
708 image_info_load_attempted_ = true;
709
710 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800711 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
712 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800713 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800714
715 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800716 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800717 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800718 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
719 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800720 cached_image_info_.patch_delta = image_header.GetPatchDelta();
721 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700722 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800723 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700724 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
725 isa_,
726 &error_msg));
727 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800728 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800729 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
730 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800731 cached_image_info_.patch_delta = image_header->GetPatchDelta();
732 }
733 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800734 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700735
Jeff Haofd336c32016-04-07 19:46:31 -0700736 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800737 }
738 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
739}
740
Jeff Haob11ffb72016-04-07 15:40:54 -0700741// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700742uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700743 uint32_t checksum = 0;
744 std::vector<gc::space::ImageSpace*> image_spaces =
745 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700746 if (isa == kRuntimeISA) {
747 for (gc::space::ImageSpace* image_space : image_spaces) {
748 checksum ^= image_space->GetImageHeader().GetOatChecksum();
749 }
750 } else {
751 for (gc::space::ImageSpace* image_space : image_spaces) {
752 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700753 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700754 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700755 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
756 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700757 checksum ^= image_header->GetOatChecksum();
758 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700759 }
760 return checksum;
761}
762
763uint32_t OatFileAssistant::GetCombinedImageChecksum() {
764 if (!image_info_load_attempted_) {
765 GetImageInfo();
766 }
767 return combined_image_checksum_;
768}
769
Andreas Gampea463b6a2016-08-12 21:53:32 -0700770std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800771 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100772 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800773 if (art_file.empty()) {
774 return nullptr;
775 }
776 std::string error_msg;
777 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700778 std::unique_ptr<gc::space::ImageSpace> ret =
779 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800780 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800781 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
782 }
783 return ret;
784}
785
Richard Uhler743bf362016-04-19 15:39:37 -0700786OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant)
787 : oat_file_assistant_(oat_file_assistant)
788{}
789
790const std::string* OatFileAssistant::OatFileInfo::Filename() {
791 return filename_provided_ ? &filename_ : nullptr;
792}
793
794bool OatFileAssistant::OatFileInfo::Exists() {
795 return GetFile() != nullptr;
796}
797
798OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
799 if (!status_attempted_) {
800 status_attempted_ = true;
801 const OatFile* file = GetFile();
802 if (file == nullptr) {
803 status_ = kOatOutOfDate;
804 } else {
805 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700806 VLOG(oat) << file->GetLocation() << " is " << status_
807 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700808 }
809 }
810 return status_;
811}
812
813bool OatFileAssistant::OatFileInfo::IsOutOfDate() {
814 return Status() == kOatOutOfDate;
815}
816
817bool OatFileAssistant::OatFileInfo::NeedsRelocation() {
818 return Status() == kOatNeedsRelocation;
819}
820
821bool OatFileAssistant::OatFileInfo::IsUpToDate() {
822 return Status() == kOatUpToDate;
823}
824
825CompilerFilter::Filter OatFileAssistant::OatFileInfo::CompilerFilter() {
826 const OatFile* file = GetFile();
827 CHECK(file != nullptr);
828 return file->GetCompilerFilter();
829}
830
Richard Uhler70a84262016-11-08 16:51:51 +0000831OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
832 CompilerFilter::Filter target, bool profile_changed) {
833 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
834
835 // See if the oat file is in good shape as is.
836 bool okay = CompilerFilterIsOkay(target, profile_changed);
837 if (okay) {
838 if (compilation_desired) {
839 if (IsUpToDate()) {
840 return kNoDexOptNeeded;
841 }
842 } else {
843 if (!IsOutOfDate()) {
844 return kNoDexOptNeeded;
845 }
846 }
847 }
848
849 // See if we can get an up-to-date file by running patchoat.
850 if (compilation_desired && okay && NeedsRelocation() && HasPatchInfo()) {
851 return kPatchOatNeeded;
852 }
853
854 // We can only run dex2oat if there are original dex files.
855 return oat_file_assistant_->HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
856}
857
Richard Uhler743bf362016-04-19 15:39:37 -0700858const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
859 CHECK(!file_released_) << "GetFile called after oat file released.";
860 if (!load_attempted_) {
861 load_attempted_ = true;
862 if (filename_provided_) {
863 std::string error_msg;
864 file_.reset(OatFile::Open(filename_.c_str(),
865 filename_.c_str(),
866 nullptr,
867 nullptr,
868 oat_file_assistant_->load_executable_,
869 /*low_4gb*/false,
870 oat_file_assistant_->dex_location_.c_str(),
871 &error_msg));
872 if (file_.get() == nullptr) {
873 VLOG(oat) << "OatFileAssistant test for existing oat file "
874 << filename_ << ": " << error_msg;
875 }
876 }
877 }
878 return file_.get();
879}
880
881bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
882 CompilerFilter::Filter target, bool profile_changed) {
883 const OatFile* file = GetFile();
884 if (file == nullptr) {
885 return false;
886 }
887
888 CompilerFilter::Filter current = file->GetCompilerFilter();
889 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
890 VLOG(oat) << "Compiler filter not okay because Profile changed";
891 return false;
892 }
893 return CompilerFilter::IsAsGoodAs(current, target);
894}
895
896bool OatFileAssistant::OatFileInfo::IsExecutable() {
897 const OatFile* file = GetFile();
898 return (file != nullptr && file->IsExecutable());
899}
900
901bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
902 const OatFile* file = GetFile();
903 return (file != nullptr && file->HasPatchInfo());
904}
905
906void OatFileAssistant::OatFileInfo::Reset() {
907 load_attempted_ = false;
908 file_.reset();
909 status_attempted_ = false;
910}
911
912void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
913 filename_provided_ = true;
914 filename_ = filename;
915 Reset();
916}
917
918std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
919 file_released_ = true;
920 return std::move(file_);
921}
922
Richard Uhler70a84262016-11-08 16:51:51 +0000923std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
924 if (IsUpToDate()) {
925 return ReleaseFile();
926 }
927
928 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
929 << " attempting to fall back to interpreting oat file instead.";
930
931 if (!IsOutOfDate() && !IsExecutable()) {
932 return ReleaseFile();
933 }
934
935 if (!IsOutOfDate()) {
936 // We are loading an oat file for runtime use that needs relocation.
937 // Reload the file non-executable to ensure that we interpret out of the
938 // dex code in the oat file rather than trying to execute the unrelocated
939 // compiled code.
940 oat_file_assistant_->load_executable_ = false;
941 Reset();
942 if (!IsOutOfDate()) {
943 CHECK(!IsExecutable());
944 return ReleaseFile();
945 }
946 }
947 return std::unique_ptr<OatFile>();
948}
Richard Uhler66d874d2015-01-15 09:37:19 -0800949} // namespace art
950