blob: 07d7b5a0f24f83059b019248a7ab9ec29f754de4 [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 Uhler2f27abd2017-01-31 14:02:34 +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 Uhler2f27abd2017-01-31 14:02:34 +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 Uhler2f27abd2017-01-31 14:02:34 +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 Uhler2f27abd2017-01-31 14:02:34 +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 Uhler2f27abd2017-01-31 14:02:34 +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 Uhler2f27abd2017-01-31 14:02:34 +0000317bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
318 if (file.GetHeader().GetNumberOfDexFiles() <= 0) {
319 VLOG(oat) << "Vdex does not contain any dex files";
320 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800321 }
322
Richard Uhler2f27abd2017-01-31 14:02:34 +0000323 // TODO: Use GetRequiredDexChecksum to get secondary checksums as well, not
324 // just the primary. Because otherwise we may fail to see a secondary
325 // checksum failure in the case when the original (multidex) files are
326 // stripped but we have a newer odex file.
327 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
328 if (dex_checksum_pointer != nullptr) {
329 uint32_t actual_checksum = file.GetLocationChecksum(0);
330 if (*dex_checksum_pointer != actual_checksum) {
331 VLOG(oat) << "Dex checksum does not match for primary dex: " << dex_location_
332 << ". Expected: " << *dex_checksum_pointer
333 << ", Actual: " << actual_checksum;
334 return false;
335 }
336 }
337
338 // Verify the dex checksums for any secondary multidex files
339 for (uint32_t i = 1; i < file.GetHeader().GetNumberOfDexFiles(); i++) {
340 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
341 uint32_t expected_secondary_checksum = 0;
342 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
343 &expected_secondary_checksum,
344 error_msg)) {
345 uint32_t actual_secondary_checksum = file.GetLocationChecksum(i);
346 if (expected_secondary_checksum != actual_secondary_checksum) {
347 VLOG(oat) << "Dex checksum does not match for secondary dex: "
348 << secondary_dex_location
349 << ". Expected: " << expected_secondary_checksum
350 << ", Actual: " << actual_secondary_checksum;
351 return false;
352 }
353 } else {
354 // If we can't get the checksum for the secondary location, we assume
355 // the dex checksum is up to date for this and all other secondary dex
356 // files.
357 break;
358 }
359 }
360 return true;
361}
362
363bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700364 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800365 // what we provide, which verifies the primary dex checksum for us.
366 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
367 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler2f27abd2017-01-31 14:02:34 +0000368 dex_location_.c_str(), dex_checksum_pointer, error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 if (oat_dex_file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000370 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800371 }
372
373 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700374 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700375 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800376 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700377 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700378 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800379 // There are no more secondary dex files to check.
380 break;
381 }
382
Richard Uhler66d874d2015-01-15 09:37:19 -0800383 uint32_t expected_secondary_checksum = 0;
384 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Richard Uhler2f27abd2017-01-31 14:02:34 +0000385 &expected_secondary_checksum, error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800386 uint32_t actual_secondary_checksum
387 = secondary_oat_dex_file->GetDexFileLocationChecksum();
388 if (expected_secondary_checksum != actual_secondary_checksum) {
389 VLOG(oat) << "Dex checksum does not match for secondary dex: "
390 << secondary_dex_location
391 << ". Expected: " << expected_secondary_checksum
392 << ", Actual: " << actual_secondary_checksum;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000393 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800394 }
395 } else {
396 // If we can't get the checksum for the secondary location, we assume
397 // the dex checksum is up to date for this and all other secondary dex
398 // files.
399 break;
400 }
401 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000402 return true;
403}
404
405OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
406 // Verify the ART_USE_READ_BARRIER state.
407 // TODO: Don't fully reject files due to read barrier state. If they contain
408 // compiled code and are otherwise okay, we should return something like
409 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
410 // barrier state doesn't matter.
411 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
412 constexpr bool kRuntimeIsCC = kUseReadBarrier;
413 if (is_cc != kRuntimeIsCC) {
414 return kOatCannotOpen;
415 }
416
417 // Verify the dex checksum.
418 std::string error_msg;
419 if (kIsVdexEnabled) {
420 VdexFile* vdex = file.GetVdexFile();
421 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
422 LOG(ERROR) << error_msg;
423 return kOatDexOutOfDate;
424 }
425 } else {
426 if (!DexChecksumUpToDate(file, &error_msg)) {
427 LOG(ERROR) << error_msg;
428 return kOatDexOutOfDate;
429 }
430 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800431
Andreas Gampe29d38e72016-03-23 15:31:51 +0000432 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000433
Richard Uhler66d874d2015-01-15 09:37:19 -0800434 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000435 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
436 const ImageInfo* image_info = GetImageInfo();
437 if (image_info == nullptr) {
438 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000439
Richard Uhler76f5cb62016-04-04 13:30:16 -0700440 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000441 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700442 }
443
444 // If there is no original dex file to fall back to, grudgingly accept
445 // the oat file. This could technically lead to crashes, but there's no
446 // way we could find a better oat file to use for this dex location,
447 // and it's better than being stuck in a boot loop with no way out.
448 // The problem will hopefully resolve itself the next time the runtime
449 // starts up.
450 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
451 << "Allow oat file use. This is potentially dangerous.";
452 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
453 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000454 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000455 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000456 }
457 } else {
458 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 }
460
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100461 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 if (!file.IsPic()) {
463 const ImageInfo* image_info = GetImageInfo();
464 if (image_info == nullptr) {
465 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000466 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000467 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700468
Andreas Gampe29d38e72016-03-23 15:31:51 +0000469 // Verify the oat_data_begin recorded for the image in the oat file matches
470 // the actual oat_data_begin for boot.oat in the image.
471 const OatHeader& oat_header = file.GetOatHeader();
472 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
473 if (oat_data_begin != image_info->oat_data_begin) {
474 VLOG(oat) << file.GetLocation() <<
475 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
476 << " does not match actual image oat_data_begin ("
477 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000478 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000479 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700480
Andreas Gampe29d38e72016-03-23 15:31:51 +0000481 // Verify the oat_patch_delta recorded for the image in the oat file matches
482 // the actual oat_patch_delta for the image.
483 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
484 if (oat_patch_delta != image_info->patch_delta) {
485 VLOG(oat) << file.GetLocation() <<
486 ": Oat file image patch delta (" << oat_patch_delta << ")"
487 << " does not match actual image patch delta ("
488 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000489 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000490 }
491 } else {
492 // Oat files compiled in PIC mode do not require relocation.
493 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
494 }
495 } else {
496 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700498 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800499}
500
Richard Uhler1e860612016-03-30 12:17:55 -0700501OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700502OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800503 CHECK(error_msg != nullptr);
504
Richard Uhler8327cf72015-10-13 16:34:59 -0700505 Runtime* runtime = Runtime::Current();
506 if (!runtime->IsDex2OatEnabled()) {
507 *error_msg = "Generation of oat file for dex location " + dex_location_
508 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700509 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700510 }
511
Richard Uhler743bf362016-04-19 15:39:37 -0700512 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700513 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800514 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700515 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800516 }
Richard Uhler743bf362016-04-19 15:39:37 -0700517 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100518 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800519
Richard Uhler66d874d2015-01-15 09:37:19 -0800520 // dex2oat ignores missing dex files and doesn't report an error.
521 // Check explicitly here so we can detect the error properly.
522 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700523 if (!OS::FileExists(dex_location_.c_str())) {
524 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700525 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800526 }
527
David Brazdil7b49e6c2016-09-01 11:06:18 +0100528 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
529 if (vdex_file.get() == nullptr) {
530 *error_msg = "Generation of oat file " + oat_file_name
531 + " not attempted because the vdex file " + vdex_file_name
532 + " could not be opened.";
533 return kUpdateNotAttempted;
534 }
535
536 if (fchmod(vdex_file->Fd(), 0644) != 0) {
537 *error_msg = "Generation of oat file " + oat_file_name
538 + " not attempted because the vdex file " + vdex_file_name
539 + " could not be made world readable.";
540 return kUpdateNotAttempted;
541 }
542
543 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700544 if (oat_file.get() == nullptr) {
545 *error_msg = "Generation of oat file " + oat_file_name
546 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700547 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700548 }
549
550 if (fchmod(oat_file->Fd(), 0644) != 0) {
551 *error_msg = "Generation of oat file " + oat_file_name
552 + " not attempted because the oat file could not be made world readable.";
553 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700554 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700555 }
556
557 std::vector<std::string> args;
558 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000559 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700560 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
561 args.push_back("--oat-location=" + oat_file_name);
562
Richard Uhler66d874d2015-01-15 09:37:19 -0800563 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100564 // Manually delete the oat and vdex files. This ensures there is no garbage
565 // left over if the process unexpectedly died.
566 vdex_file->Erase();
567 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700568 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100569 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700570 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700571 }
572
David Brazdil7b49e6c2016-09-01 11:06:18 +0100573 if (vdex_file->FlushCloseOrErase() != 0) {
574 *error_msg = "Unable to close vdex file " + vdex_file_name;
575 unlink(vdex_file_name.c_str());
576 return kUpdateFailed;
577 }
578
Richard Uhler8327cf72015-10-13 16:34:59 -0700579 if (oat_file->FlushCloseOrErase() != 0) {
580 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100581 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700582 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800583 }
584
585 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700586 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700587 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800588}
589
590bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
591 std::string* error_msg) {
592 Runtime* runtime = Runtime::Current();
593 std::string image_location = ImageLocation();
594 if (image_location.empty()) {
595 *error_msg = "No image location found for Dex2Oat.";
596 return false;
597 }
598
599 std::vector<std::string> argv;
600 argv.push_back(runtime->GetCompilerExecutable());
601 argv.push_back("--runtime-arg");
602 argv.push_back("-classpath");
603 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700604 std::string class_path = runtime->GetClassPathString();
605 if (class_path == "") {
606 class_path = OatFile::kSpecialSharedLibrary;
607 }
608 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000609 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200610 argv.push_back("--debuggable");
611 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700612 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800613
614 if (!runtime->IsVerificationEnabled()) {
615 argv.push_back("--compiler-filter=verify-none");
616 }
617
618 if (runtime->MustRelocateIfPossible()) {
619 argv.push_back("--runtime-arg");
620 argv.push_back("-Xrelocate");
621 } else {
622 argv.push_back("--runtime-arg");
623 argv.push_back("-Xnorelocate");
624 }
625
626 if (!kIsTargetBuild) {
627 argv.push_back("--host");
628 }
629
630 argv.push_back("--boot-image=" + image_location);
631
632 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
633 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
634
635 argv.insert(argv.end(), args.begin(), args.end());
636
Andreas Gampe9186ced2016-12-12 14:28:21 -0800637 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800638 return Exec(argv, error_msg);
639}
640
Richard Uhlerb81881d2016-04-19 13:08:04 -0700641bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
642 InstructionSet isa,
643 std::string* odex_filename,
644 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800645 CHECK(odex_filename != nullptr);
646 CHECK(error_msg != nullptr);
647
648 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700649 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800650 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700651 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800652
Richard Uhler63434112015-03-16 14:32:16 -0700653 // Find the directory portion of the dex location and add the oat/<isa>
654 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800655 size_t pos = location.rfind('/');
656 if (pos == std::string::npos) {
657 *error_msg = "Dex location " + location + " has no directory.";
658 return false;
659 }
660 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700661 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800662
663 // Find the file portion of the dex location.
664 std::string file;
665 if (pos == std::string::npos) {
666 file = location;
667 } else {
668 file = location.substr(pos+1);
669 }
670
671 // Get the base part of the file without the extension.
672 pos = file.rfind('.');
673 if (pos == std::string::npos) {
674 *error_msg = "Dex location " + location + " has no extension.";
675 return false;
676 }
677 std::string base = file.substr(0, pos);
678
679 *odex_filename = dir + "/" + base + ".odex";
680 return true;
681}
682
Richard Uhlerb81881d2016-04-19 13:08:04 -0700683bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
684 InstructionSet isa,
685 std::string* oat_filename,
686 std::string* error_msg) {
687 CHECK(oat_filename != nullptr);
688 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800689
Richard Uhler55b58b62016-08-12 09:05:13 -0700690 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
691 if (cache_dir.empty()) {
692 *error_msg = "Dalvik cache directory does not exist";
693 return false;
694 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700695
696 // TODO: The oat file assistant should be the definitive place for
697 // determining the oat file name from the dex location, not
698 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700699 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800700}
701
Richard Uhler66d874d2015-01-15 09:37:19 -0800702std::string OatFileAssistant::ImageLocation() {
703 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000704 const std::vector<gc::space::ImageSpace*>& image_spaces =
705 runtime->GetHeap()->GetBootImageSpaces();
706 if (image_spaces.empty()) {
707 return "";
708 }
709 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800710}
711
712const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700713 if (!required_dex_checksum_attempted_) {
714 required_dex_checksum_attempted_ = true;
715 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800716 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700717 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700718 required_dex_checksum_found_ = true;
719 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800720 } else {
721 // This can happen if the original dex file has been stripped from the
722 // apk.
723 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700724 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800725
726 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700727 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800728 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700729 const OatFile::OatDexFile* odex_dex_file
730 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800731 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700732 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
733 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800734 }
735 }
736 }
737 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700738 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800739}
740
Richard Uhler66d874d2015-01-15 09:37:19 -0800741const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
742 if (!image_info_load_attempted_) {
743 image_info_load_attempted_ = true;
744
745 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800746 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
747 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800748 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800749
750 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800751 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800752 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800753 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
754 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800755 cached_image_info_.patch_delta = image_header.GetPatchDelta();
756 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700757 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800758 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700759 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
760 isa_,
761 &error_msg));
762 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800763 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800764 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
765 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800766 cached_image_info_.patch_delta = image_header->GetPatchDelta();
767 }
768 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800769 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700770
Jeff Haofd336c32016-04-07 19:46:31 -0700771 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800772 }
773 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
774}
775
Jeff Haob11ffb72016-04-07 15:40:54 -0700776// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700777uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700778 uint32_t checksum = 0;
779 std::vector<gc::space::ImageSpace*> image_spaces =
780 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700781 if (isa == kRuntimeISA) {
782 for (gc::space::ImageSpace* image_space : image_spaces) {
783 checksum ^= image_space->GetImageHeader().GetOatChecksum();
784 }
785 } else {
786 for (gc::space::ImageSpace* image_space : image_spaces) {
787 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700788 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700789 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700790 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
791 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700792 checksum ^= image_header->GetOatChecksum();
793 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700794 }
795 return checksum;
796}
797
798uint32_t OatFileAssistant::GetCombinedImageChecksum() {
799 if (!image_info_load_attempted_) {
800 GetImageInfo();
801 }
802 return combined_image_checksum_;
803}
804
Richard Uhler88bc6732016-11-14 14:38:03 +0000805OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000806 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
807 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000808}
809
Andreas Gampea463b6a2016-08-12 21:53:32 -0700810std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800811 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100812 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800813 if (art_file.empty()) {
814 return nullptr;
815 }
816 std::string error_msg;
817 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700818 std::unique_ptr<gc::space::ImageSpace> ret =
819 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800820 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800821 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
822 }
823 return ret;
824}
825
Richard Uhler88bc6732016-11-14 14:38:03 +0000826OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
827 bool is_oat_location)
828 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700829{}
830
Richard Uhler88bc6732016-11-14 14:38:03 +0000831bool OatFileAssistant::OatFileInfo::IsOatLocation() {
832 return is_oat_location_;
833}
834
Richard Uhler743bf362016-04-19 15:39:37 -0700835const std::string* OatFileAssistant::OatFileInfo::Filename() {
836 return filename_provided_ ? &filename_ : nullptr;
837}
838
Richard Uhler03bc6592016-11-22 09:42:04 +0000839bool OatFileAssistant::OatFileInfo::IsUseable() {
840 switch (Status()) {
841 case kOatCannotOpen:
842 case kOatDexOutOfDate:
843 case kOatBootImageOutOfDate: return false;
844
845 case kOatRelocationOutOfDate:
846 case kOatUpToDate: return true;
847 }
848 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700849}
850
851OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
852 if (!status_attempted_) {
853 status_attempted_ = true;
854 const OatFile* file = GetFile();
855 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000856 // Check to see if there is a vdex file we can make use of.
857 std::string error_msg;
858 std::string vdex_filename = ReplaceFileExtension(filename_, "vdex");
859 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
860 /*writeable*/false,
861 /*low_4gb*/false,
862 &error_msg);
863 if (vdex == nullptr) {
864 status_ = kOatCannotOpen;
865 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
866 } else {
867 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
868 // The vdex file does not contain enough information to determine
869 // whether it is up to date with respect to the boot image, so we
870 // assume it is out of date.
871 VLOG(oat) << error_msg;
872 status_ = kOatBootImageOutOfDate;
873 } else {
874 status_ = kOatDexOutOfDate;
875 }
876 }
Richard Uhler743bf362016-04-19 15:39:37 -0700877 } else {
878 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700879 VLOG(oat) << file->GetLocation() << " is " << status_
880 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700881 }
882 }
883 return status_;
884}
885
Richard Uhler70a84262016-11-08 16:51:51 +0000886OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
887 CompilerFilter::Filter target, bool profile_changed) {
888 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000889 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000890
Richard Uhler7225a8d2016-11-22 10:12:03 +0000891 if (filter_okay && Status() == kOatUpToDate) {
892 // The oat file is in good shape as is.
893 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000894 }
895
Richard Uhler7225a8d2016-11-22 10:12:03 +0000896 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
897 // If no compilation is desired, then it doesn't matter if the oat
898 // file needs relocation. It's in good shape as is.
899 return kNoDexOptNeeded;
900 }
901
Richard Uhler7225a8d2016-11-22 10:12:03 +0000902 if (oat_file_assistant_->HasOriginalDexFiles()) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000903 if (filter_okay && Status() == kOatRelocationOutOfDate) {
904 return kDex2OatForRelocation;
905 }
906
907 if (IsUseable()) {
908 return kDex2OatForFilter;
909 }
910
911 if (Status() == kOatBootImageOutOfDate) {
912 return kDex2OatForBootImage;
913 }
914
915 return kDex2OatFromScratch;
916 }
917
918 // Otherwise there is nothing we can do, even if we want to.
919 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000920}
921
Richard Uhler743bf362016-04-19 15:39:37 -0700922const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
923 CHECK(!file_released_) << "GetFile called after oat file released.";
924 if (!load_attempted_) {
925 load_attempted_ = true;
926 if (filename_provided_) {
927 std::string error_msg;
928 file_.reset(OatFile::Open(filename_.c_str(),
929 filename_.c_str(),
930 nullptr,
931 nullptr,
932 oat_file_assistant_->load_executable_,
933 /*low_4gb*/false,
934 oat_file_assistant_->dex_location_.c_str(),
935 &error_msg));
936 if (file_.get() == nullptr) {
937 VLOG(oat) << "OatFileAssistant test for existing oat file "
938 << filename_ << ": " << error_msg;
939 }
940 }
941 }
942 return file_.get();
943}
944
945bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
946 CompilerFilter::Filter target, bool profile_changed) {
947 const OatFile* file = GetFile();
948 if (file == nullptr) {
949 return false;
950 }
951
952 CompilerFilter::Filter current = file->GetCompilerFilter();
953 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
954 VLOG(oat) << "Compiler filter not okay because Profile changed";
955 return false;
956 }
957 return CompilerFilter::IsAsGoodAs(current, target);
958}
959
960bool OatFileAssistant::OatFileInfo::IsExecutable() {
961 const OatFile* file = GetFile();
962 return (file != nullptr && file->IsExecutable());
963}
964
Richard Uhler743bf362016-04-19 15:39:37 -0700965void OatFileAssistant::OatFileInfo::Reset() {
966 load_attempted_ = false;
967 file_.reset();
968 status_attempted_ = false;
969}
970
971void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
972 filename_provided_ = true;
973 filename_ = filename;
974 Reset();
975}
976
977std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
978 file_released_ = true;
979 return std::move(file_);
980}
981
Richard Uhler70a84262016-11-08 16:51:51 +0000982std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000983 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000984 return ReleaseFile();
985 }
986
987 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
988 << " attempting to fall back to interpreting oat file instead.";
989
Richard Uhler03bc6592016-11-22 09:42:04 +0000990 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000991 return ReleaseFile();
992 }
993
Richard Uhler03bc6592016-11-22 09:42:04 +0000994 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000995 // We are loading an oat file for runtime use that needs relocation.
996 // Reload the file non-executable to ensure that we interpret out of the
997 // dex code in the oat file rather than trying to execute the unrelocated
998 // compiled code.
999 oat_file_assistant_->load_executable_ = false;
1000 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001001 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001002 CHECK(!IsExecutable());
1003 return ReleaseFile();
1004 }
1005 }
1006 return std::unique_ptr<OatFile>();
1007}
Richard Uhler66d874d2015-01-15 09:37:19 -08001008} // namespace art