blob: 77cdd28d3a8c0c83216a6447893054fcf2cf2dbe [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"
David Sehr97c381e2017-02-01 15:09:58 -080028#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "gc/heap.h"
30#include "gc/space/image_space.h"
31#include "image.h"
32#include "oat.h"
33#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000037#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038
39namespace art {
40
Narayan Kamath8943c1d2016-05-02 13:14:48 +010041std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
42 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000043 case OatFileAssistant::kOatCannotOpen:
44 stream << "kOatCannotOpen";
45 break;
46 case OatFileAssistant::kOatDexOutOfDate:
47 stream << "kOatDexOutOfDate";
48 break;
49 case OatFileAssistant::kOatBootImageOutOfDate:
50 stream << "kOatBootImageOutOfDate";
51 break;
52 case OatFileAssistant::kOatRelocationOutOfDate:
53 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010054 break;
55 case OatFileAssistant::kOatUpToDate:
56 stream << "kOatUpToDate";
57 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010058 default:
59 UNREACHABLE();
60 }
61
62 return stream;
63}
64
Richard Uhler66d874d2015-01-15 09:37:19 -080065OatFileAssistant::OatFileAssistant(const char* dex_location,
66 const InstructionSet isa,
67 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070068 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000069{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080070
71OatFileAssistant::OatFileAssistant(const char* dex_location,
72 const char* oat_location,
73 const InstructionSet isa,
74 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000075 : isa_(isa),
76 load_executable_(load_executable),
77 odex_(this, /*is_oat_location*/ false),
78 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070079 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
80 dex_location_.assign(dex_location);
81
Richard Uhler66d874d2015-01-15 09:37:19 -080082 if (load_executable_ && isa != kRuntimeISA) {
83 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
84 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
85 load_executable_ = false;
86 }
87
Richard Uhler743bf362016-04-19 15:39:37 -070088 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070089 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070090 std::string odex_file_name;
91 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
92 odex_.Reset(odex_file_name);
93 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070094 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
95 }
96
Richard Uhler743bf362016-04-19 15:39:37 -070097 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080098 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070099 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -0700100 } else {
Richard Uhler743bf362016-04-19 15:39:37 -0700101 std::string oat_file_name;
102 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
103 oat_.Reset(oat_file_name);
104 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700105 LOG(WARNING) << "Failed to determine oat file name for dex location "
106 << dex_location_ << ": " << error_msg;
107 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800108 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800109}
110
111OatFileAssistant::~OatFileAssistant() {
112 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700113 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100114 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800115 }
116}
117
118bool OatFileAssistant::IsInBootClassPath() {
119 // Note: We check the current boot class path, regardless of the ISA
120 // specified by the user. This is okay, because the boot class path should
121 // be the same for all ISAs.
122 // TODO: Can we verify the boot class path is the same for all ISAs?
123 Runtime* runtime = Runtime::Current();
124 ClassLinker* class_linker = runtime->GetClassLinker();
125 const auto& boot_class_path = class_linker->GetBootClassPath();
126 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700127 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800128 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
129 return true;
130 }
131 }
132 return false;
133}
134
135bool OatFileAssistant::Lock(std::string* error_msg) {
136 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700137 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800138
Richard Uhler743bf362016-04-19 15:39:37 -0700139 const std::string* oat_file_name = oat_.Filename();
140 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800141 *error_msg = "Failed to determine lock file";
142 return false;
143 }
Richard Uhler743bf362016-04-19 15:39:37 -0700144 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800145
Richard Uhler581f4e92015-05-07 10:19:35 -0700146 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100147 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800148 return false;
149 }
150 return true;
151}
152
Richard Uhler7225a8d2016-11-22 10:12:03 +0000153int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000154 OatFileInfo& info = GetBestInfo();
155 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000156 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
157 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800158 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000159 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800160}
161
Richard Uhlerf4b34872016-04-13 11:03:46 -0700162// Figure out the currently specified compile filter option in the runtime.
163// Returns true on success, false if the compiler filter is invalid, in which
164// case error_msg describes the problem.
165static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
166 std::string* error_msg) {
167 CHECK(filter != nullptr);
168 CHECK(error_msg != nullptr);
169
170 *filter = CompilerFilter::kDefaultCompilerFilter;
171 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
172 if (option.starts_with("--compiler-filter=")) {
173 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
174 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
175 *error_msg = std::string("Unknown --compiler-filter value: ")
176 + std::string(compiler_filter_string);
177 return false;
178 }
179 }
180 }
181 return true;
182}
183
Richard Uhler01be6812016-05-17 10:34:52 -0700184bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000185 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700186}
187
Richard Uhler1e860612016-03-30 12:17:55 -0700188OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700189OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700190 CompilerFilter::Filter target;
191 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
192 return kUpdateNotAttempted;
193 }
194
Richard Uhler88bc6732016-11-14 14:38:03 +0000195 OatFileInfo& info = GetBestInfo();
196 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000197 case kNoDexOptNeeded:
198 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000199
Richard Uhler7225a8d2016-11-22 10:12:03 +0000200 // TODO: For now, don't bother with all the different ways we can call
201 // dex2oat to generate the oat file. Always generate the oat file as if it
202 // were kDex2OatFromScratch.
203 case kDex2OatFromScratch:
204 case kDex2OatForBootImage:
205 case kDex2OatForRelocation:
206 case kDex2OatForFilter:
207 return GenerateOatFile(error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800208 }
209 UNREACHABLE();
210}
211
212std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000213 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800214}
215
Richard Uhler46cc64f2016-11-14 14:53:55 +0000216std::string OatFileAssistant::GetStatusDump() {
217 std::ostringstream status;
218 bool oat_file_exists = false;
219 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000220 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000221 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000222 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000223
Richard Uhler46cc64f2016-11-14 14:53:55 +0000224 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000225 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
226 const OatFile* file = oat_.GetFile();
227 if (file == nullptr) {
228 // If the file is null even though the status is not kOatCannotOpen, it
229 // means we must have a vdex file with no corresponding oat file. In
230 // this case we cannot determine the compilation filter. Indicate that
231 // we have only the vdex file instead.
232 status << "vdex-only";
233 } else {
234 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
235 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000236 }
237
Richard Uhler03bc6592016-11-22 09:42:04 +0000238 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000239 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000240 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000241
Richard Uhler46cc64f2016-11-14 14:53:55 +0000242 odex_file_exists = true;
243 if (oat_file_exists) {
244 status << "] ";
245 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000246 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
247 const OatFile* file = odex_.GetFile();
248 if (file == nullptr) {
249 status << "vdex-only";
250 } else {
251 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
252 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000253 }
254
255 if (!oat_file_exists && !odex_file_exists) {
256 status << "invalid[";
257 }
258
259 status << "]";
260 return status.str();
261}
262
Richard Uhler66d874d2015-01-15 09:37:19 -0800263std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
264 const OatFile& oat_file, const char* dex_location) {
265 std::vector<std::unique_ptr<const DexFile>> dex_files;
266
267 // Load the primary dex file.
268 std::string error_msg;
269 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700270 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700272 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800273 return std::vector<std::unique_ptr<const DexFile>>();
274 }
275
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700276 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800277 if (dex_file.get() == nullptr) {
278 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
279 return std::vector<std::unique_ptr<const DexFile>>();
280 }
281 dex_files.push_back(std::move(dex_file));
282
283 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700284 for (size_t i = 1; ; i++) {
285 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700286 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700287 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800288 // There are no more secondary dex files to load.
289 break;
290 }
291
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700292 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800293 if (dex_file.get() == nullptr) {
294 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
295 return std::vector<std::unique_ptr<const DexFile>>();
296 }
297 dex_files.push_back(std::move(dex_file));
298 }
299 return dex_files;
300}
301
Richard Uhler9b994ea2015-06-24 08:44:19 -0700302bool OatFileAssistant::HasOriginalDexFiles() {
303 // Ensure GetRequiredDexChecksum has been run so that
304 // has_original_dex_files_ is initialized. We don't care about the result of
305 // GetRequiredDexChecksum.
306 GetRequiredDexChecksum();
307 return has_original_dex_files_;
308}
309
Richard Uhler95abd042015-03-24 09:51:28 -0700310OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700311 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800312}
313
Richard Uhler95abd042015-03-24 09:51:28 -0700314OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700315 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800316}
317
Richard Uhler2f27abd2017-01-31 14:02:34 +0000318bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
319 if (file.GetHeader().GetNumberOfDexFiles() <= 0) {
320 VLOG(oat) << "Vdex does not contain any dex files";
321 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800322 }
323
Richard Uhler2f27abd2017-01-31 14:02:34 +0000324 // TODO: Use GetRequiredDexChecksum to get secondary checksums as well, not
325 // just the primary. Because otherwise we may fail to see a secondary
326 // checksum failure in the case when the original (multidex) files are
327 // stripped but we have a newer odex file.
328 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
329 if (dex_checksum_pointer != nullptr) {
330 uint32_t actual_checksum = file.GetLocationChecksum(0);
331 if (*dex_checksum_pointer != actual_checksum) {
332 VLOG(oat) << "Dex checksum does not match for primary dex: " << dex_location_
333 << ". Expected: " << *dex_checksum_pointer
334 << ", Actual: " << actual_checksum;
335 return false;
336 }
337 }
338
339 // Verify the dex checksums for any secondary multidex files
340 for (uint32_t i = 1; i < file.GetHeader().GetNumberOfDexFiles(); i++) {
341 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
342 uint32_t expected_secondary_checksum = 0;
343 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
344 &expected_secondary_checksum,
345 error_msg)) {
346 uint32_t actual_secondary_checksum = file.GetLocationChecksum(i);
347 if (expected_secondary_checksum != actual_secondary_checksum) {
348 VLOG(oat) << "Dex checksum does not match for secondary dex: "
349 << secondary_dex_location
350 << ". Expected: " << expected_secondary_checksum
351 << ", Actual: " << actual_secondary_checksum;
352 return false;
353 }
354 } else {
355 // If we can't get the checksum for the secondary location, we assume
356 // the dex checksum is up to date for this and all other secondary dex
357 // files.
358 break;
359 }
360 }
361 return true;
362}
363
364bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700365 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800366 // what we provide, which verifies the primary dex checksum for us.
367 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
368 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler2f27abd2017-01-31 14:02:34 +0000369 dex_location_.c_str(), dex_checksum_pointer, error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700370 if (oat_dex_file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000371 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 }
373
374 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700375 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700376 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700378 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800380 // There are no more secondary dex files to check.
381 break;
382 }
383
Richard Uhler66d874d2015-01-15 09:37:19 -0800384 uint32_t expected_secondary_checksum = 0;
385 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Richard Uhler2f27abd2017-01-31 14:02:34 +0000386 &expected_secondary_checksum, error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800387 uint32_t actual_secondary_checksum
388 = secondary_oat_dex_file->GetDexFileLocationChecksum();
389 if (expected_secondary_checksum != actual_secondary_checksum) {
390 VLOG(oat) << "Dex checksum does not match for secondary dex: "
391 << secondary_dex_location
392 << ". Expected: " << expected_secondary_checksum
393 << ", Actual: " << actual_secondary_checksum;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000394 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800395 }
396 } else {
397 // If we can't get the checksum for the secondary location, we assume
398 // the dex checksum is up to date for this and all other secondary dex
399 // files.
400 break;
401 }
402 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000403 return true;
404}
405
406OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
407 // Verify the ART_USE_READ_BARRIER state.
408 // TODO: Don't fully reject files due to read barrier state. If they contain
409 // compiled code and are otherwise okay, we should return something like
410 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
411 // barrier state doesn't matter.
412 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
413 constexpr bool kRuntimeIsCC = kUseReadBarrier;
414 if (is_cc != kRuntimeIsCC) {
415 return kOatCannotOpen;
416 }
417
418 // Verify the dex checksum.
419 std::string error_msg;
420 if (kIsVdexEnabled) {
421 VdexFile* vdex = file.GetVdexFile();
422 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
423 LOG(ERROR) << error_msg;
424 return kOatDexOutOfDate;
425 }
426 } else {
427 if (!DexChecksumUpToDate(file, &error_msg)) {
428 LOG(ERROR) << error_msg;
429 return kOatDexOutOfDate;
430 }
431 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800432
Andreas Gampe29d38e72016-03-23 15:31:51 +0000433 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000434
Richard Uhler66d874d2015-01-15 09:37:19 -0800435 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000436 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
437 const ImageInfo* image_info = GetImageInfo();
438 if (image_info == nullptr) {
439 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000440
Richard Uhler76f5cb62016-04-04 13:30:16 -0700441 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000442 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700443 }
444
445 // If there is no original dex file to fall back to, grudgingly accept
446 // the oat file. This could technically lead to crashes, but there's no
447 // way we could find a better oat file to use for this dex location,
448 // and it's better than being stuck in a boot loop with no way out.
449 // The problem will hopefully resolve itself the next time the runtime
450 // starts up.
451 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
452 << "Allow oat file use. This is potentially dangerous.";
453 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
454 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000455 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000456 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000457 }
458 } else {
459 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800460 }
461
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100462 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000463 if (!file.IsPic()) {
464 const ImageInfo* image_info = GetImageInfo();
465 if (image_info == nullptr) {
466 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000467 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000468 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700469
Andreas Gampe29d38e72016-03-23 15:31:51 +0000470 // Verify the oat_data_begin recorded for the image in the oat file matches
471 // the actual oat_data_begin for boot.oat in the image.
472 const OatHeader& oat_header = file.GetOatHeader();
473 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
474 if (oat_data_begin != image_info->oat_data_begin) {
475 VLOG(oat) << file.GetLocation() <<
476 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
477 << " does not match actual image oat_data_begin ("
478 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000479 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000480 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700481
Andreas Gampe29d38e72016-03-23 15:31:51 +0000482 // Verify the oat_patch_delta recorded for the image in the oat file matches
483 // the actual oat_patch_delta for the image.
484 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
485 if (oat_patch_delta != image_info->patch_delta) {
486 VLOG(oat) << file.GetLocation() <<
487 ": Oat file image patch delta (" << oat_patch_delta << ")"
488 << " does not match actual image patch delta ("
489 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000490 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000491 }
492 } else {
493 // Oat files compiled in PIC mode do not require relocation.
494 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
495 }
496 } else {
497 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800498 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700499 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800500}
501
Richard Uhler1e860612016-03-30 12:17:55 -0700502OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700503OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800504 CHECK(error_msg != nullptr);
505
Richard Uhler8327cf72015-10-13 16:34:59 -0700506 Runtime* runtime = Runtime::Current();
507 if (!runtime->IsDex2OatEnabled()) {
508 *error_msg = "Generation of oat file for dex location " + dex_location_
509 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700510 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700511 }
512
Richard Uhler743bf362016-04-19 15:39:37 -0700513 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700514 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800515 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700516 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800517 }
Richard Uhler743bf362016-04-19 15:39:37 -0700518 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100519 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800520
Richard Uhler66d874d2015-01-15 09:37:19 -0800521 // dex2oat ignores missing dex files and doesn't report an error.
522 // Check explicitly here so we can detect the error properly.
523 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700524 if (!OS::FileExists(dex_location_.c_str())) {
525 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700526 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800527 }
528
David Brazdil7b49e6c2016-09-01 11:06:18 +0100529 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
530 if (vdex_file.get() == nullptr) {
531 *error_msg = "Generation of oat file " + oat_file_name
532 + " not attempted because the vdex file " + vdex_file_name
533 + " could not be opened.";
534 return kUpdateNotAttempted;
535 }
536
537 if (fchmod(vdex_file->Fd(), 0644) != 0) {
538 *error_msg = "Generation of oat file " + oat_file_name
539 + " not attempted because the vdex file " + vdex_file_name
540 + " could not be made world readable.";
541 return kUpdateNotAttempted;
542 }
543
544 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700545 if (oat_file.get() == nullptr) {
546 *error_msg = "Generation of oat file " + oat_file_name
547 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700548 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700549 }
550
551 if (fchmod(oat_file->Fd(), 0644) != 0) {
552 *error_msg = "Generation of oat file " + oat_file_name
553 + " not attempted because the oat file could not be made world readable.";
554 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700555 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700556 }
557
558 std::vector<std::string> args;
559 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000560 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700561 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
562 args.push_back("--oat-location=" + oat_file_name);
563
Richard Uhler66d874d2015-01-15 09:37:19 -0800564 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100565 // Manually delete the oat and vdex files. This ensures there is no garbage
566 // left over if the process unexpectedly died.
567 vdex_file->Erase();
568 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700569 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100570 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700571 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700572 }
573
David Brazdil7b49e6c2016-09-01 11:06:18 +0100574 if (vdex_file->FlushCloseOrErase() != 0) {
575 *error_msg = "Unable to close vdex file " + vdex_file_name;
576 unlink(vdex_file_name.c_str());
577 return kUpdateFailed;
578 }
579
Richard Uhler8327cf72015-10-13 16:34:59 -0700580 if (oat_file->FlushCloseOrErase() != 0) {
581 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100582 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700583 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800584 }
585
586 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700587 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700588 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800589}
590
591bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
592 std::string* error_msg) {
593 Runtime* runtime = Runtime::Current();
594 std::string image_location = ImageLocation();
595 if (image_location.empty()) {
596 *error_msg = "No image location found for Dex2Oat.";
597 return false;
598 }
599
600 std::vector<std::string> argv;
601 argv.push_back(runtime->GetCompilerExecutable());
602 argv.push_back("--runtime-arg");
603 argv.push_back("-classpath");
604 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700605 std::string class_path = runtime->GetClassPathString();
606 if (class_path == "") {
607 class_path = OatFile::kSpecialSharedLibrary;
608 }
609 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000610 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200611 argv.push_back("--debuggable");
612 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700613 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800614
615 if (!runtime->IsVerificationEnabled()) {
616 argv.push_back("--compiler-filter=verify-none");
617 }
618
619 if (runtime->MustRelocateIfPossible()) {
620 argv.push_back("--runtime-arg");
621 argv.push_back("-Xrelocate");
622 } else {
623 argv.push_back("--runtime-arg");
624 argv.push_back("-Xnorelocate");
625 }
626
627 if (!kIsTargetBuild) {
628 argv.push_back("--host");
629 }
630
631 argv.push_back("--boot-image=" + image_location);
632
633 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
634 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
635
636 argv.insert(argv.end(), args.begin(), args.end());
637
Andreas Gampe9186ced2016-12-12 14:28:21 -0800638 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800639 return Exec(argv, error_msg);
640}
641
Richard Uhlerb81881d2016-04-19 13:08:04 -0700642bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
643 InstructionSet isa,
644 std::string* odex_filename,
645 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800646 CHECK(odex_filename != nullptr);
647 CHECK(error_msg != nullptr);
648
649 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700650 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800651 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700652 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800653
Richard Uhler63434112015-03-16 14:32:16 -0700654 // Find the directory portion of the dex location and add the oat/<isa>
655 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800656 size_t pos = location.rfind('/');
657 if (pos == std::string::npos) {
658 *error_msg = "Dex location " + location + " has no directory.";
659 return false;
660 }
661 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700662 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800663
664 // Find the file portion of the dex location.
665 std::string file;
666 if (pos == std::string::npos) {
667 file = location;
668 } else {
669 file = location.substr(pos+1);
670 }
671
672 // Get the base part of the file without the extension.
673 pos = file.rfind('.');
674 if (pos == std::string::npos) {
675 *error_msg = "Dex location " + location + " has no extension.";
676 return false;
677 }
678 std::string base = file.substr(0, pos);
679
680 *odex_filename = dir + "/" + base + ".odex";
681 return true;
682}
683
Richard Uhlerb81881d2016-04-19 13:08:04 -0700684bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
685 InstructionSet isa,
686 std::string* oat_filename,
687 std::string* error_msg) {
688 CHECK(oat_filename != nullptr);
689 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800690
Richard Uhler55b58b62016-08-12 09:05:13 -0700691 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
692 if (cache_dir.empty()) {
693 *error_msg = "Dalvik cache directory does not exist";
694 return false;
695 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700696
697 // TODO: The oat file assistant should be the definitive place for
698 // determining the oat file name from the dex location, not
699 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700700 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800701}
702
Richard Uhler66d874d2015-01-15 09:37:19 -0800703std::string OatFileAssistant::ImageLocation() {
704 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000705 const std::vector<gc::space::ImageSpace*>& image_spaces =
706 runtime->GetHeap()->GetBootImageSpaces();
707 if (image_spaces.empty()) {
708 return "";
709 }
710 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800711}
712
713const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700714 if (!required_dex_checksum_attempted_) {
715 required_dex_checksum_attempted_ = true;
716 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800717 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700718 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700719 required_dex_checksum_found_ = true;
720 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800721 } else {
722 // This can happen if the original dex file has been stripped from the
723 // apk.
724 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700725 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800726
727 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700728 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800729 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700730 const OatFile::OatDexFile* odex_dex_file
731 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800732 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700733 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
734 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800735 }
736 }
737 }
738 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700739 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800740}
741
Richard Uhler66d874d2015-01-15 09:37:19 -0800742const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
743 if (!image_info_load_attempted_) {
744 image_info_load_attempted_ = true;
745
746 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800747 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
748 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800749 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800750
751 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800752 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800753 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800754 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
755 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800756 cached_image_info_.patch_delta = image_header.GetPatchDelta();
757 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700758 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800759 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700760 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
761 isa_,
762 &error_msg));
763 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800764 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800765 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
766 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800767 cached_image_info_.patch_delta = image_header->GetPatchDelta();
768 }
769 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800770 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700771
Jeff Haofd336c32016-04-07 19:46:31 -0700772 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800773 }
774 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
775}
776
Jeff Haob11ffb72016-04-07 15:40:54 -0700777// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700778uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700779 uint32_t checksum = 0;
780 std::vector<gc::space::ImageSpace*> image_spaces =
781 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700782 if (isa == kRuntimeISA) {
783 for (gc::space::ImageSpace* image_space : image_spaces) {
784 checksum ^= image_space->GetImageHeader().GetOatChecksum();
785 }
786 } else {
787 for (gc::space::ImageSpace* image_space : image_spaces) {
788 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700789 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700790 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700791 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
792 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700793 checksum ^= image_header->GetOatChecksum();
794 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700795 }
796 return checksum;
797}
798
799uint32_t OatFileAssistant::GetCombinedImageChecksum() {
800 if (!image_info_load_attempted_) {
801 GetImageInfo();
802 }
803 return combined_image_checksum_;
804}
805
Richard Uhler88bc6732016-11-14 14:38:03 +0000806OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000807 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
808 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000809}
810
Andreas Gampea463b6a2016-08-12 21:53:32 -0700811std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800812 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100813 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800814 if (art_file.empty()) {
815 return nullptr;
816 }
817 std::string error_msg;
818 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700819 std::unique_ptr<gc::space::ImageSpace> ret =
820 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800821 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800822 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
823 }
824 return ret;
825}
826
Richard Uhler88bc6732016-11-14 14:38:03 +0000827OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
828 bool is_oat_location)
829 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700830{}
831
Richard Uhler88bc6732016-11-14 14:38:03 +0000832bool OatFileAssistant::OatFileInfo::IsOatLocation() {
833 return is_oat_location_;
834}
835
Richard Uhler743bf362016-04-19 15:39:37 -0700836const std::string* OatFileAssistant::OatFileInfo::Filename() {
837 return filename_provided_ ? &filename_ : nullptr;
838}
839
Richard Uhler03bc6592016-11-22 09:42:04 +0000840bool OatFileAssistant::OatFileInfo::IsUseable() {
841 switch (Status()) {
842 case kOatCannotOpen:
843 case kOatDexOutOfDate:
844 case kOatBootImageOutOfDate: return false;
845
846 case kOatRelocationOutOfDate:
847 case kOatUpToDate: return true;
848 }
849 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700850}
851
852OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
853 if (!status_attempted_) {
854 status_attempted_ = true;
855 const OatFile* file = GetFile();
856 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000857 // Check to see if there is a vdex file we can make use of.
858 std::string error_msg;
859 std::string vdex_filename = ReplaceFileExtension(filename_, "vdex");
860 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
861 /*writeable*/false,
862 /*low_4gb*/false,
863 &error_msg);
864 if (vdex == nullptr) {
865 status_ = kOatCannotOpen;
866 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
867 } else {
868 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
869 // The vdex file does not contain enough information to determine
870 // whether it is up to date with respect to the boot image, so we
871 // assume it is out of date.
872 VLOG(oat) << error_msg;
873 status_ = kOatBootImageOutOfDate;
874 } else {
875 status_ = kOatDexOutOfDate;
876 }
877 }
Richard Uhler743bf362016-04-19 15:39:37 -0700878 } else {
879 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700880 VLOG(oat) << file->GetLocation() << " is " << status_
881 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700882 }
883 }
884 return status_;
885}
886
Richard Uhler70a84262016-11-08 16:51:51 +0000887OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
888 CompilerFilter::Filter target, bool profile_changed) {
889 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000890 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000891
Richard Uhler7225a8d2016-11-22 10:12:03 +0000892 if (filter_okay && Status() == kOatUpToDate) {
893 // The oat file is in good shape as is.
894 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000895 }
896
Richard Uhler7225a8d2016-11-22 10:12:03 +0000897 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
898 // If no compilation is desired, then it doesn't matter if the oat
899 // file needs relocation. It's in good shape as is.
900 return kNoDexOptNeeded;
901 }
902
Richard Uhler7225a8d2016-11-22 10:12:03 +0000903 if (oat_file_assistant_->HasOriginalDexFiles()) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000904 if (filter_okay && Status() == kOatRelocationOutOfDate) {
905 return kDex2OatForRelocation;
906 }
907
908 if (IsUseable()) {
909 return kDex2OatForFilter;
910 }
911
912 if (Status() == kOatBootImageOutOfDate) {
913 return kDex2OatForBootImage;
914 }
915
916 return kDex2OatFromScratch;
917 }
918
919 // Otherwise there is nothing we can do, even if we want to.
920 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000921}
922
Richard Uhler743bf362016-04-19 15:39:37 -0700923const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
924 CHECK(!file_released_) << "GetFile called after oat file released.";
925 if (!load_attempted_) {
926 load_attempted_ = true;
927 if (filename_provided_) {
928 std::string error_msg;
929 file_.reset(OatFile::Open(filename_.c_str(),
930 filename_.c_str(),
931 nullptr,
932 nullptr,
933 oat_file_assistant_->load_executable_,
934 /*low_4gb*/false,
935 oat_file_assistant_->dex_location_.c_str(),
936 &error_msg));
937 if (file_.get() == nullptr) {
938 VLOG(oat) << "OatFileAssistant test for existing oat file "
939 << filename_ << ": " << error_msg;
940 }
941 }
942 }
943 return file_.get();
944}
945
946bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
947 CompilerFilter::Filter target, bool profile_changed) {
948 const OatFile* file = GetFile();
949 if (file == nullptr) {
950 return false;
951 }
952
953 CompilerFilter::Filter current = file->GetCompilerFilter();
954 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
955 VLOG(oat) << "Compiler filter not okay because Profile changed";
956 return false;
957 }
958 return CompilerFilter::IsAsGoodAs(current, target);
959}
960
961bool OatFileAssistant::OatFileInfo::IsExecutable() {
962 const OatFile* file = GetFile();
963 return (file != nullptr && file->IsExecutable());
964}
965
Richard Uhler743bf362016-04-19 15:39:37 -0700966void OatFileAssistant::OatFileInfo::Reset() {
967 load_attempted_ = false;
968 file_.reset();
969 status_attempted_ = false;
970}
971
972void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
973 filename_provided_ = true;
974 filename_ = filename;
975 Reset();
976}
977
978std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
979 file_released_ = true;
980 return std::move(file_);
981}
982
Richard Uhler70a84262016-11-08 16:51:51 +0000983std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000984 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000985 return ReleaseFile();
986 }
987
988 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
989 << " attempting to fall back to interpreting oat file instead.";
990
Richard Uhler03bc6592016-11-22 09:42:04 +0000991 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000992 return ReleaseFile();
993 }
994
Richard Uhler03bc6592016-11-22 09:42:04 +0000995 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000996 // We are loading an oat file for runtime use that needs relocation.
997 // Reload the file non-executable to ensure that we interpret out of the
998 // dex code in the oat file rather than trying to execute the unrelocated
999 // compiled code.
1000 oat_file_assistant_->load_executable_ = false;
1001 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001002 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001003 CHECK(!IsExecutable());
1004 return ReleaseFile();
1005 }
1006 }
1007 return std::unique_ptr<OatFile>();
1008}
Richard Uhler66d874d2015-01-15 09:37:19 -08001009} // namespace art