blob: 8a23457470ba600760591d511458a134aa09e00b [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 Uhler46cc64f2016-11-14 14:53:55 +000019#include <sstream>
20
Richard Uhler66d874d2015-01-15 09:37:19 -080021#include <sys/stat.h>
Andreas Gampe9186ced2016-12-12 14:28:21 -080022
23#include "android-base/strings.h"
24
Richard Uhler66d874d2015-01-15 09:37:19 -080025#include "base/logging.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010026#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080027#include "class_linker.h"
28#include "gc/heap.h"
29#include "gc/space/image_space.h"
30#include "image.h"
31#include "oat.h"
32#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "utils.h"
Richard Uhler402089a2017-01-18 08:39:23 +000036#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037
38namespace art {
39
Narayan Kamath8943c1d2016-05-02 13:14:48 +010040std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
41 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000042 case OatFileAssistant::kOatCannotOpen:
43 stream << "kOatCannotOpen";
44 break;
45 case OatFileAssistant::kOatDexOutOfDate:
46 stream << "kOatDexOutOfDate";
47 break;
48 case OatFileAssistant::kOatBootImageOutOfDate:
49 stream << "kOatBootImageOutOfDate";
50 break;
51 case OatFileAssistant::kOatRelocationOutOfDate:
52 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010053 break;
54 case OatFileAssistant::kOatUpToDate:
55 stream << "kOatUpToDate";
56 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010057 default:
58 UNREACHABLE();
59 }
60
61 return stream;
62}
63
Richard Uhler66d874d2015-01-15 09:37:19 -080064OatFileAssistant::OatFileAssistant(const char* dex_location,
65 const InstructionSet isa,
66 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070067 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000068{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080069
70OatFileAssistant::OatFileAssistant(const char* dex_location,
71 const char* oat_location,
72 const InstructionSet isa,
73 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000074 : isa_(isa),
75 load_executable_(load_executable),
76 odex_(this, /*is_oat_location*/ false),
77 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070078 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
79 dex_location_.assign(dex_location);
80
Richard Uhler66d874d2015-01-15 09:37:19 -080081 if (load_executable_ && isa != kRuntimeISA) {
82 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
83 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
84 load_executable_ = false;
85 }
86
Richard Uhler743bf362016-04-19 15:39:37 -070087 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070088 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070089 std::string odex_file_name;
90 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
91 odex_.Reset(odex_file_name);
92 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070093 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
94 }
95
Richard Uhler743bf362016-04-19 15:39:37 -070096 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080097 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070098 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -070099 } else {
Richard Uhler743bf362016-04-19 15:39:37 -0700100 std::string oat_file_name;
101 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
102 oat_.Reset(oat_file_name);
103 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700104 LOG(WARNING) << "Failed to determine oat file name for dex location "
105 << dex_location_ << ": " << error_msg;
106 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800107 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800108}
109
110OatFileAssistant::~OatFileAssistant() {
111 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700112 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100113 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800114 }
115}
116
117bool OatFileAssistant::IsInBootClassPath() {
118 // Note: We check the current boot class path, regardless of the ISA
119 // specified by the user. This is okay, because the boot class path should
120 // be the same for all ISAs.
121 // TODO: Can we verify the boot class path is the same for all ISAs?
122 Runtime* runtime = Runtime::Current();
123 ClassLinker* class_linker = runtime->GetClassLinker();
124 const auto& boot_class_path = class_linker->GetBootClassPath();
125 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700126 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800127 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
128 return true;
129 }
130 }
131 return false;
132}
133
134bool OatFileAssistant::Lock(std::string* error_msg) {
135 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700136 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800137
Richard Uhler743bf362016-04-19 15:39:37 -0700138 const std::string* oat_file_name = oat_.Filename();
139 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800140 *error_msg = "Failed to determine lock file";
141 return false;
142 }
Richard Uhler743bf362016-04-19 15:39:37 -0700143 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800144
Richard Uhler581f4e92015-05-07 10:19:35 -0700145 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100146 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800147 return false;
148 }
149 return true;
150}
151
Richard Uhler7225a8d2016-11-22 10:12:03 +0000152int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000153 OatFileInfo& info = GetBestInfo();
154 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000155 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
156 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800157 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000158 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800159}
160
Richard Uhlerf4b34872016-04-13 11:03:46 -0700161// Figure out the currently specified compile filter option in the runtime.
162// Returns true on success, false if the compiler filter is invalid, in which
163// case error_msg describes the problem.
164static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
165 std::string* error_msg) {
166 CHECK(filter != nullptr);
167 CHECK(error_msg != nullptr);
168
169 *filter = CompilerFilter::kDefaultCompilerFilter;
170 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
171 if (option.starts_with("--compiler-filter=")) {
172 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
173 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
174 *error_msg = std::string("Unknown --compiler-filter value: ")
175 + std::string(compiler_filter_string);
176 return false;
177 }
178 }
179 }
180 return true;
181}
182
Richard Uhler01be6812016-05-17 10:34:52 -0700183bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000184 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700185}
186
Richard Uhler1e860612016-03-30 12:17:55 -0700187OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700188OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700189 CompilerFilter::Filter target;
190 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
191 return kUpdateNotAttempted;
192 }
193
Richard Uhler88bc6732016-11-14 14:38:03 +0000194 OatFileInfo& info = GetBestInfo();
195 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000196 case kNoDexOptNeeded:
197 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000198
Richard Uhler7225a8d2016-11-22 10:12:03 +0000199 // TODO: For now, don't bother with all the different ways we can call
200 // dex2oat to generate the oat file. Always generate the oat file as if it
201 // were kDex2OatFromScratch.
202 case kDex2OatFromScratch:
203 case kDex2OatForBootImage:
204 case kDex2OatForRelocation:
205 case kDex2OatForFilter:
206 return GenerateOatFile(error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800207 }
208 UNREACHABLE();
209}
210
211std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000212 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800213}
214
Richard Uhler46cc64f2016-11-14 14:53:55 +0000215std::string OatFileAssistant::GetStatusDump() {
216 std::ostringstream status;
217 bool oat_file_exists = false;
218 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000219 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler402089a2017-01-18 08:39:23 +0000220 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000221 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000222
Richard Uhler46cc64f2016-11-14 14:53:55 +0000223 oat_file_exists = true;
Richard Uhler402089a2017-01-18 08:39:23 +0000224 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
225 const OatFile* file = oat_.GetFile();
226 if (file == nullptr) {
227 // If the file is null even though the status is not kOatCannotOpen, it
228 // means we must have a vdex file with no corresponding oat file. In
229 // this case we cannot determine the compilation filter. Indicate that
230 // we have only the vdex file instead.
231 status << "vdex-only";
232 } else {
233 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
234 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000235 }
236
Richard Uhler03bc6592016-11-22 09:42:04 +0000237 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler402089a2017-01-18 08:39:23 +0000238 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000239 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000240
Richard Uhler46cc64f2016-11-14 14:53:55 +0000241 odex_file_exists = true;
242 if (oat_file_exists) {
243 status << "] ";
244 }
Richard Uhler402089a2017-01-18 08:39:23 +0000245 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
246 const OatFile* file = odex_.GetFile();
247 if (file == nullptr) {
248 status << "vdex-only";
249 } else {
250 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
251 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000252 }
253
254 if (!oat_file_exists && !odex_file_exists) {
255 status << "invalid[";
256 }
257
258 status << "]";
259 return status.str();
260}
261
Richard Uhler66d874d2015-01-15 09:37:19 -0800262std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
263 const OatFile& oat_file, const char* dex_location) {
264 std::vector<std::unique_ptr<const DexFile>> dex_files;
265
266 // Load the primary dex file.
267 std::string error_msg;
268 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700269 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800270 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700271 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800272 return std::vector<std::unique_ptr<const DexFile>>();
273 }
274
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700275 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 if (dex_file.get() == nullptr) {
277 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
278 return std::vector<std::unique_ptr<const DexFile>>();
279 }
280 dex_files.push_back(std::move(dex_file));
281
282 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700283 for (size_t i = 1; ; i++) {
284 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700285 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700286 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 // There are no more secondary dex files to load.
288 break;
289 }
290
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700291 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800292 if (dex_file.get() == nullptr) {
293 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
294 return std::vector<std::unique_ptr<const DexFile>>();
295 }
296 dex_files.push_back(std::move(dex_file));
297 }
298 return dex_files;
299}
300
Richard Uhler9b994ea2015-06-24 08:44:19 -0700301bool OatFileAssistant::HasOriginalDexFiles() {
302 // Ensure GetRequiredDexChecksum has been run so that
303 // has_original_dex_files_ is initialized. We don't care about the result of
304 // GetRequiredDexChecksum.
305 GetRequiredDexChecksum();
306 return has_original_dex_files_;
307}
308
Richard Uhler95abd042015-03-24 09:51:28 -0700309OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700310 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800311}
312
Richard Uhler95abd042015-03-24 09:51:28 -0700313OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700314 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800315}
316
Richard Uhler402089a2017-01-18 08:39:23 +0000317bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
318 // TODO: Use GetRequiredDexChecksum to get secondary checksums as well, not
319 // just the primary. Because otherwise we may fail to see a secondary
320 // checksum failure in the case when the original (multidex) files are
321 // stripped but we have a newer odex file.
322 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
323 if (dex_checksum_pointer != nullptr) {
324 uint32_t actual_checksum = file.GetLocationChecksum(0);
325 if (*dex_checksum_pointer != actual_checksum) {
326 VLOG(oat) << "Dex checksum does not match for primary dex: " << dex_location_
327 << ". Expected: " << *dex_checksum_pointer
328 << ", Actual: " << actual_checksum;
329 return false;
330 }
Andreas Gampef8cd8902017-01-18 16:05:01 -0800331 }
332
Richard Uhler402089a2017-01-18 08:39:23 +0000333 // Verify the dex checksums for any secondary multidex files
334 for (uint32_t i = 1; i < file.GetHeader().GetNumberOfDexFiles(); i++) {
335 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
336 uint32_t expected_secondary_checksum = 0;
337 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
338 &expected_secondary_checksum,
339 error_msg)) {
340 uint32_t actual_secondary_checksum = file.GetLocationChecksum(i);
341 if (expected_secondary_checksum != actual_secondary_checksum) {
342 VLOG(oat) << "Dex checksum does not match for secondary dex: "
343 << secondary_dex_location
344 << ". Expected: " << expected_secondary_checksum
345 << ", Actual: " << actual_secondary_checksum;
346 return false;
347 }
348 } else {
349 // If we can't get the checksum for the secondary location, we assume
350 // the dex checksum is up to date for this and all other secondary dex
351 // files.
352 break;
353 }
354 }
355 return true;
356}
357
358bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700359 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800360 // what we provide, which verifies the primary dex checksum for us.
361 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
362 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler402089a2017-01-18 08:39:23 +0000363 dex_location_.c_str(), dex_checksum_pointer, error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700364 if (oat_dex_file == nullptr) {
Richard Uhler402089a2017-01-18 08:39:23 +0000365 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800366 }
367
368 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700369 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700370 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800371 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700372 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700373 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 // There are no more secondary dex files to check.
375 break;
376 }
377
Richard Uhler66d874d2015-01-15 09:37:19 -0800378 uint32_t expected_secondary_checksum = 0;
379 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Richard Uhler402089a2017-01-18 08:39:23 +0000380 &expected_secondary_checksum, error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 uint32_t actual_secondary_checksum
382 = secondary_oat_dex_file->GetDexFileLocationChecksum();
383 if (expected_secondary_checksum != actual_secondary_checksum) {
384 VLOG(oat) << "Dex checksum does not match for secondary dex: "
385 << secondary_dex_location
386 << ". Expected: " << expected_secondary_checksum
387 << ", Actual: " << actual_secondary_checksum;
Richard Uhler402089a2017-01-18 08:39:23 +0000388 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800389 }
390 } else {
391 // If we can't get the checksum for the secondary location, we assume
392 // the dex checksum is up to date for this and all other secondary dex
393 // files.
394 break;
395 }
396 }
Richard Uhler402089a2017-01-18 08:39:23 +0000397 return true;
398}
399
400OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
401 // Verify the ART_USE_READ_BARRIER state.
402 // TODO: Don't fully reject files due to read barrier state. If they contain
403 // compiled code and are otherwise okay, we should return something like
404 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
405 // barrier state doesn't matter.
406 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
407 constexpr bool kRuntimeIsCC = kUseReadBarrier;
408 if (is_cc != kRuntimeIsCC) {
409 return kOatCannotOpen;
410 }
411
412 // Verify the dex checksum.
413 std::string error_msg;
414 if (kIsVdexEnabled) {
415 VdexFile* vdex = file.GetVdexFile();
416 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
417 LOG(ERROR) << error_msg;
418 return kOatDexOutOfDate;
419 }
420 } else {
421 if (!DexChecksumUpToDate(file, &error_msg)) {
422 LOG(ERROR) << error_msg;
423 return kOatDexOutOfDate;
424 }
425 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800426
Andreas Gampe29d38e72016-03-23 15:31:51 +0000427 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000428
Richard Uhler66d874d2015-01-15 09:37:19 -0800429 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000430 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
431 const ImageInfo* image_info = GetImageInfo();
432 if (image_info == nullptr) {
433 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000434
Richard Uhler76f5cb62016-04-04 13:30:16 -0700435 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000436 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700437 }
438
439 // If there is no original dex file to fall back to, grudgingly accept
440 // the oat file. This could technically lead to crashes, but there's no
441 // way we could find a better oat file to use for this dex location,
442 // and it's better than being stuck in a boot loop with no way out.
443 // The problem will hopefully resolve itself the next time the runtime
444 // starts up.
445 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
446 << "Allow oat file use. This is potentially dangerous.";
447 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
448 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000449 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000450 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000451 }
452 } else {
453 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800454 }
455
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100456 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000457 if (!file.IsPic()) {
458 const ImageInfo* image_info = GetImageInfo();
459 if (image_info == nullptr) {
460 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000461 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700463
Andreas Gampe29d38e72016-03-23 15:31:51 +0000464 // Verify the oat_data_begin recorded for the image in the oat file matches
465 // the actual oat_data_begin for boot.oat in the image.
466 const OatHeader& oat_header = file.GetOatHeader();
467 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
468 if (oat_data_begin != image_info->oat_data_begin) {
469 VLOG(oat) << file.GetLocation() <<
470 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
471 << " does not match actual image oat_data_begin ("
472 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000473 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000474 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700475
Andreas Gampe29d38e72016-03-23 15:31:51 +0000476 // Verify the oat_patch_delta recorded for the image in the oat file matches
477 // the actual oat_patch_delta for the image.
478 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
479 if (oat_patch_delta != image_info->patch_delta) {
480 VLOG(oat) << file.GetLocation() <<
481 ": Oat file image patch delta (" << oat_patch_delta << ")"
482 << " does not match actual image patch delta ("
483 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000484 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000485 }
486 } else {
487 // Oat files compiled in PIC mode do not require relocation.
488 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
489 }
490 } else {
491 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800492 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700493 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800494}
495
Richard Uhler1e860612016-03-30 12:17:55 -0700496OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700497OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800498 CHECK(error_msg != nullptr);
499
Richard Uhler8327cf72015-10-13 16:34:59 -0700500 Runtime* runtime = Runtime::Current();
501 if (!runtime->IsDex2OatEnabled()) {
502 *error_msg = "Generation of oat file for dex location " + dex_location_
503 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700504 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700505 }
506
Richard Uhler743bf362016-04-19 15:39:37 -0700507 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700508 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800509 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700510 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800511 }
Richard Uhler743bf362016-04-19 15:39:37 -0700512 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100513 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800514
Richard Uhler66d874d2015-01-15 09:37:19 -0800515 // dex2oat ignores missing dex files and doesn't report an error.
516 // Check explicitly here so we can detect the error properly.
517 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700518 if (!OS::FileExists(dex_location_.c_str())) {
519 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700520 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800521 }
522
David Brazdil7b49e6c2016-09-01 11:06:18 +0100523 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
524 if (vdex_file.get() == nullptr) {
525 *error_msg = "Generation of oat file " + oat_file_name
526 + " not attempted because the vdex file " + vdex_file_name
527 + " could not be opened.";
528 return kUpdateNotAttempted;
529 }
530
531 if (fchmod(vdex_file->Fd(), 0644) != 0) {
532 *error_msg = "Generation of oat file " + oat_file_name
533 + " not attempted because the vdex file " + vdex_file_name
534 + " could not be made world readable.";
535 return kUpdateNotAttempted;
536 }
537
538 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700539 if (oat_file.get() == nullptr) {
540 *error_msg = "Generation of oat file " + oat_file_name
541 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700542 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700543 }
544
545 if (fchmod(oat_file->Fd(), 0644) != 0) {
546 *error_msg = "Generation of oat file " + oat_file_name
547 + " not attempted because the oat file could not be made world readable.";
548 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700549 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700550 }
551
552 std::vector<std::string> args;
553 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000554 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700555 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
556 args.push_back("--oat-location=" + oat_file_name);
557
Richard Uhler66d874d2015-01-15 09:37:19 -0800558 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100559 // Manually delete the oat and vdex files. This ensures there is no garbage
560 // left over if the process unexpectedly died.
561 vdex_file->Erase();
562 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700563 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100564 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700565 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700566 }
567
David Brazdil7b49e6c2016-09-01 11:06:18 +0100568 if (vdex_file->FlushCloseOrErase() != 0) {
569 *error_msg = "Unable to close vdex file " + vdex_file_name;
570 unlink(vdex_file_name.c_str());
571 return kUpdateFailed;
572 }
573
Richard Uhler8327cf72015-10-13 16:34:59 -0700574 if (oat_file->FlushCloseOrErase() != 0) {
575 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100576 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700577 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800578 }
579
580 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700581 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700582 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800583}
584
585bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
586 std::string* error_msg) {
587 Runtime* runtime = Runtime::Current();
588 std::string image_location = ImageLocation();
589 if (image_location.empty()) {
590 *error_msg = "No image location found for Dex2Oat.";
591 return false;
592 }
593
594 std::vector<std::string> argv;
595 argv.push_back(runtime->GetCompilerExecutable());
596 argv.push_back("--runtime-arg");
597 argv.push_back("-classpath");
598 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700599 std::string class_path = runtime->GetClassPathString();
600 if (class_path == "") {
601 class_path = OatFile::kSpecialSharedLibrary;
602 }
603 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100604 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200605 argv.push_back("--debuggable");
606 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700607 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800608
609 if (!runtime->IsVerificationEnabled()) {
610 argv.push_back("--compiler-filter=verify-none");
611 }
612
613 if (runtime->MustRelocateIfPossible()) {
614 argv.push_back("--runtime-arg");
615 argv.push_back("-Xrelocate");
616 } else {
617 argv.push_back("--runtime-arg");
618 argv.push_back("-Xnorelocate");
619 }
620
621 if (!kIsTargetBuild) {
622 argv.push_back("--host");
623 }
624
625 argv.push_back("--boot-image=" + image_location);
626
627 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
628 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
629
630 argv.insert(argv.end(), args.begin(), args.end());
631
Andreas Gampe9186ced2016-12-12 14:28:21 -0800632 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800633 return Exec(argv, error_msg);
634}
635
Richard Uhlerb81881d2016-04-19 13:08:04 -0700636bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
637 InstructionSet isa,
638 std::string* odex_filename,
639 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800640 CHECK(odex_filename != nullptr);
641 CHECK(error_msg != nullptr);
642
643 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700644 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800645 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700646 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800647
Richard Uhler63434112015-03-16 14:32:16 -0700648 // Find the directory portion of the dex location and add the oat/<isa>
649 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800650 size_t pos = location.rfind('/');
651 if (pos == std::string::npos) {
652 *error_msg = "Dex location " + location + " has no directory.";
653 return false;
654 }
655 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700656 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800657
658 // Find the file portion of the dex location.
659 std::string file;
660 if (pos == std::string::npos) {
661 file = location;
662 } else {
663 file = location.substr(pos+1);
664 }
665
666 // Get the base part of the file without the extension.
667 pos = file.rfind('.');
668 if (pos == std::string::npos) {
669 *error_msg = "Dex location " + location + " has no extension.";
670 return false;
671 }
672 std::string base = file.substr(0, pos);
673
674 *odex_filename = dir + "/" + base + ".odex";
675 return true;
676}
677
Richard Uhlerb81881d2016-04-19 13:08:04 -0700678bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
679 InstructionSet isa,
680 std::string* oat_filename,
681 std::string* error_msg) {
682 CHECK(oat_filename != nullptr);
683 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800684
Richard Uhler55b58b62016-08-12 09:05:13 -0700685 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
686 if (cache_dir.empty()) {
687 *error_msg = "Dalvik cache directory does not exist";
688 return false;
689 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700690
691 // TODO: The oat file assistant should be the definitive place for
692 // determining the oat file name from the dex location, not
693 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700694 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800695}
696
Richard Uhler66d874d2015-01-15 09:37:19 -0800697std::string OatFileAssistant::ImageLocation() {
698 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000699 const std::vector<gc::space::ImageSpace*>& image_spaces =
700 runtime->GetHeap()->GetBootImageSpaces();
701 if (image_spaces.empty()) {
702 return "";
703 }
704 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800705}
706
707const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700708 if (!required_dex_checksum_attempted_) {
709 required_dex_checksum_attempted_ = true;
710 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800711 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700712 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700713 required_dex_checksum_found_ = true;
714 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800715 } else {
716 // This can happen if the original dex file has been stripped from the
717 // apk.
718 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700719 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800720
721 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700722 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800723 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700724 const OatFile::OatDexFile* odex_dex_file
725 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800726 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700727 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
728 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800729 }
730 }
731 }
732 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700733 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800734}
735
Richard Uhler66d874d2015-01-15 09:37:19 -0800736const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
737 if (!image_info_load_attempted_) {
738 image_info_load_attempted_ = true;
739
740 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800741 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
742 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800743 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800744
745 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800746 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800747 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800748 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
749 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800750 cached_image_info_.patch_delta = image_header.GetPatchDelta();
751 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700752 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800753 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700754 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
755 isa_,
756 &error_msg));
757 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800758 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800759 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
760 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800761 cached_image_info_.patch_delta = image_header->GetPatchDelta();
762 }
763 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800764 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700765
Jeff Haofd336c32016-04-07 19:46:31 -0700766 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800767 }
768 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
769}
770
Jeff Haob11ffb72016-04-07 15:40:54 -0700771// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700772uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700773 uint32_t checksum = 0;
774 std::vector<gc::space::ImageSpace*> image_spaces =
775 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700776 if (isa == kRuntimeISA) {
777 for (gc::space::ImageSpace* image_space : image_spaces) {
778 checksum ^= image_space->GetImageHeader().GetOatChecksum();
779 }
780 } else {
781 for (gc::space::ImageSpace* image_space : image_spaces) {
782 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700783 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700784 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700785 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
786 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700787 checksum ^= image_header->GetOatChecksum();
788 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700789 }
790 return checksum;
791}
792
793uint32_t OatFileAssistant::GetCombinedImageChecksum() {
794 if (!image_info_load_attempted_) {
795 GetImageInfo();
796 }
797 return combined_image_checksum_;
798}
799
Richard Uhler88bc6732016-11-14 14:38:03 +0000800OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000801 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
802 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000803}
804
Andreas Gampea463b6a2016-08-12 21:53:32 -0700805std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800806 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100807 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800808 if (art_file.empty()) {
809 return nullptr;
810 }
811 std::string error_msg;
812 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700813 std::unique_ptr<gc::space::ImageSpace> ret =
814 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800815 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800816 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
817 }
818 return ret;
819}
820
Richard Uhler88bc6732016-11-14 14:38:03 +0000821OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
822 bool is_oat_location)
823 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700824{}
825
Richard Uhler88bc6732016-11-14 14:38:03 +0000826bool OatFileAssistant::OatFileInfo::IsOatLocation() {
827 return is_oat_location_;
828}
829
Richard Uhler743bf362016-04-19 15:39:37 -0700830const std::string* OatFileAssistant::OatFileInfo::Filename() {
831 return filename_provided_ ? &filename_ : nullptr;
832}
833
Richard Uhler03bc6592016-11-22 09:42:04 +0000834bool OatFileAssistant::OatFileInfo::IsUseable() {
835 switch (Status()) {
836 case kOatCannotOpen:
837 case kOatDexOutOfDate:
838 case kOatBootImageOutOfDate: return false;
839
840 case kOatRelocationOutOfDate:
841 case kOatUpToDate: return true;
842 }
843 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700844}
845
846OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
847 if (!status_attempted_) {
848 status_attempted_ = true;
849 const OatFile* file = GetFile();
850 if (file == nullptr) {
Richard Uhler402089a2017-01-18 08:39:23 +0000851 // Check to see if there is a vdex file we can make use of.
852 std::string error_msg;
853 std::string vdex_filename = ReplaceFileExtension(filename_, "vdex");
854 std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_filename,
855 /*writeable*/false,
856 /*low_4gb*/false,
857 &error_msg));
858 if (vdex == nullptr) {
859 status_ = kOatCannotOpen;
860 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
861 } else {
862 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
863 // The vdex file does not contain enough information to determine
864 // whether it is up to date with respect to the boot image, so we
865 // assume it is out of date.
866 VLOG(oat) << error_msg;
867 status_ = kOatBootImageOutOfDate;
868 } else {
869 status_ = kOatDexOutOfDate;
870 }
871 }
Richard Uhler743bf362016-04-19 15:39:37 -0700872 } else {
873 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700874 VLOG(oat) << file->GetLocation() << " is " << status_
875 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700876 }
877 }
878 return status_;
879}
880
Richard Uhler70a84262016-11-08 16:51:51 +0000881OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
882 CompilerFilter::Filter target, bool profile_changed) {
883 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000884 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000885
Richard Uhler7225a8d2016-11-22 10:12:03 +0000886 if (filter_okay && Status() == kOatUpToDate) {
887 // The oat file is in good shape as is.
888 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000889 }
890
Richard Uhler7225a8d2016-11-22 10:12:03 +0000891 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
892 // If no compilation is desired, then it doesn't matter if the oat
893 // file needs relocation. It's in good shape as is.
894 return kNoDexOptNeeded;
895 }
896
Richard Uhler7225a8d2016-11-22 10:12:03 +0000897 if (oat_file_assistant_->HasOriginalDexFiles()) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000898 if (filter_okay && Status() == kOatRelocationOutOfDate) {
899 return kDex2OatForRelocation;
900 }
901
902 if (IsUseable()) {
903 return kDex2OatForFilter;
904 }
905
906 if (Status() == kOatBootImageOutOfDate) {
907 return kDex2OatForBootImage;
908 }
909
910 return kDex2OatFromScratch;
911 }
912
913 // Otherwise there is nothing we can do, even if we want to.
914 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000915}
916
Richard Uhler743bf362016-04-19 15:39:37 -0700917const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
918 CHECK(!file_released_) << "GetFile called after oat file released.";
919 if (!load_attempted_) {
920 load_attempted_ = true;
921 if (filename_provided_) {
922 std::string error_msg;
923 file_.reset(OatFile::Open(filename_.c_str(),
924 filename_.c_str(),
925 nullptr,
926 nullptr,
927 oat_file_assistant_->load_executable_,
928 /*low_4gb*/false,
929 oat_file_assistant_->dex_location_.c_str(),
930 &error_msg));
931 if (file_.get() == nullptr) {
932 VLOG(oat) << "OatFileAssistant test for existing oat file "
933 << filename_ << ": " << error_msg;
934 }
935 }
936 }
937 return file_.get();
938}
939
940bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
941 CompilerFilter::Filter target, bool profile_changed) {
942 const OatFile* file = GetFile();
943 if (file == nullptr) {
944 return false;
945 }
946
947 CompilerFilter::Filter current = file->GetCompilerFilter();
948 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
949 VLOG(oat) << "Compiler filter not okay because Profile changed";
950 return false;
951 }
952 return CompilerFilter::IsAsGoodAs(current, target);
953}
954
955bool OatFileAssistant::OatFileInfo::IsExecutable() {
956 const OatFile* file = GetFile();
957 return (file != nullptr && file->IsExecutable());
958}
959
Richard Uhler743bf362016-04-19 15:39:37 -0700960void OatFileAssistant::OatFileInfo::Reset() {
961 load_attempted_ = false;
962 file_.reset();
963 status_attempted_ = false;
964}
965
966void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
967 filename_provided_ = true;
968 filename_ = filename;
969 Reset();
970}
971
972std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
973 file_released_ = true;
974 return std::move(file_);
975}
976
Richard Uhler70a84262016-11-08 16:51:51 +0000977std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000978 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000979 return ReleaseFile();
980 }
981
982 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
983 << " attempting to fall back to interpreting oat file instead.";
984
Richard Uhler03bc6592016-11-22 09:42:04 +0000985 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000986 return ReleaseFile();
987 }
988
Richard Uhler03bc6592016-11-22 09:42:04 +0000989 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000990 // We are loading an oat file for runtime use that needs relocation.
991 // Reload the file non-executable to ensure that we interpret out of the
992 // dex code in the oat file rather than trying to execute the unrelocated
993 // compiled code.
994 oat_file_assistant_->load_executable_ = false;
995 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +0000996 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000997 CHECK(!IsExecutable());
998 return ReleaseFile();
999 }
1000 }
1001 return std::unique_ptr<OatFile>();
1002}
Richard Uhler66d874d2015-01-15 09:37:19 -08001003} // namespace art