blob: a7be73a84933d8c5f9950a40ed2191f5d619e829 [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
Andreas Gampec15a2f42017-04-21 12:09:39 -070023#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080024#include "android-base/strings.h"
25
Richard Uhler66d874d2015-01-15 09:37:19 -080026#include "base/logging.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010027#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080028#include "class_linker.h"
David Sehr97c381e2017-02-01 15:09:58 -080029#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "gc/heap.h"
31#include "gc/space/image_space.h"
32#include "image.h"
33#include "oat.h"
34#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000038#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080039
40namespace art {
41
Richard Uhler69bcf2c2017-01-24 10:25:21 +000042using android::base::StringPrintf;
43
Narayan Kamath8943c1d2016-05-02 13:14:48 +010044std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
45 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000046 case OatFileAssistant::kOatCannotOpen:
47 stream << "kOatCannotOpen";
48 break;
49 case OatFileAssistant::kOatDexOutOfDate:
50 stream << "kOatDexOutOfDate";
51 break;
52 case OatFileAssistant::kOatBootImageOutOfDate:
53 stream << "kOatBootImageOutOfDate";
54 break;
55 case OatFileAssistant::kOatRelocationOutOfDate:
56 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010057 break;
58 case OatFileAssistant::kOatUpToDate:
59 stream << "kOatUpToDate";
60 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010061 default:
62 UNREACHABLE();
63 }
64
65 return stream;
66}
67
Richard Uhler66d874d2015-01-15 09:37:19 -080068OatFileAssistant::OatFileAssistant(const char* dex_location,
69 const InstructionSet isa,
70 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070071 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000072{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080073
74OatFileAssistant::OatFileAssistant(const char* dex_location,
75 const char* oat_location,
76 const InstructionSet isa,
77 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000078 : isa_(isa),
79 load_executable_(load_executable),
80 odex_(this, /*is_oat_location*/ false),
81 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070082 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
83 dex_location_.assign(dex_location);
84
Richard Uhler66d874d2015-01-15 09:37:19 -080085 if (load_executable_ && isa != kRuntimeISA) {
86 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
87 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
88 load_executable_ = false;
89 }
90
Richard Uhler743bf362016-04-19 15:39:37 -070091 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070092 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070093 std::string odex_file_name;
94 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
95 odex_.Reset(odex_file_name);
96 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070097 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
98 }
99
Richard Uhler743bf362016-04-19 15:39:37 -0700100 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -0800101 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -0700102 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -0700103 } else {
Richard Uhler743bf362016-04-19 15:39:37 -0700104 std::string oat_file_name;
105 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
106 oat_.Reset(oat_file_name);
107 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700108 LOG(WARNING) << "Failed to determine oat file name for dex location "
109 << dex_location_ << ": " << error_msg;
110 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800111 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800112}
113
114OatFileAssistant::~OatFileAssistant() {
115 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700116 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100117 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800118 }
119}
120
121bool OatFileAssistant::IsInBootClassPath() {
122 // Note: We check the current boot class path, regardless of the ISA
123 // specified by the user. This is okay, because the boot class path should
124 // be the same for all ISAs.
125 // TODO: Can we verify the boot class path is the same for all ISAs?
126 Runtime* runtime = Runtime::Current();
127 ClassLinker* class_linker = runtime->GetClassLinker();
128 const auto& boot_class_path = class_linker->GetBootClassPath();
129 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700130 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800131 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
132 return true;
133 }
134 }
135 return false;
136}
137
138bool OatFileAssistant::Lock(std::string* error_msg) {
139 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700140 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800141
Richard Uhler743bf362016-04-19 15:39:37 -0700142 const std::string* oat_file_name = oat_.Filename();
143 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800144 *error_msg = "Failed to determine lock file";
145 return false;
146 }
Richard Uhler743bf362016-04-19 15:39:37 -0700147 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800148
Richard Uhler581f4e92015-05-07 10:19:35 -0700149 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100150 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800151 return false;
152 }
153 return true;
154}
155
Richard Uhler7225a8d2016-11-22 10:12:03 +0000156int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000157 OatFileInfo& info = GetBestInfo();
158 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000159 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
160 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800161 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000162 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800163}
164
Richard Uhlerf4b34872016-04-13 11:03:46 -0700165// Figure out the currently specified compile filter option in the runtime.
166// Returns true on success, false if the compiler filter is invalid, in which
167// case error_msg describes the problem.
168static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
169 std::string* error_msg) {
170 CHECK(filter != nullptr);
171 CHECK(error_msg != nullptr);
172
173 *filter = CompilerFilter::kDefaultCompilerFilter;
174 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
175 if (option.starts_with("--compiler-filter=")) {
176 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
177 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
178 *error_msg = std::string("Unknown --compiler-filter value: ")
179 + std::string(compiler_filter_string);
180 return false;
181 }
182 }
183 }
184 return true;
185}
186
Richard Uhler01be6812016-05-17 10:34:52 -0700187bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000188 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700189}
190
Richard Uhler1e860612016-03-30 12:17:55 -0700191OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700192OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700193 CompilerFilter::Filter target;
194 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
195 return kUpdateNotAttempted;
196 }
197
Richard Uhler88bc6732016-11-14 14:38:03 +0000198 OatFileInfo& info = GetBestInfo();
199 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000200 case kNoDexOptNeeded:
201 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000202
Richard Uhler7225a8d2016-11-22 10:12:03 +0000203 // TODO: For now, don't bother with all the different ways we can call
204 // dex2oat to generate the oat file. Always generate the oat file as if it
205 // were kDex2OatFromScratch.
206 case kDex2OatFromScratch:
207 case kDex2OatForBootImage:
208 case kDex2OatForRelocation:
209 case kDex2OatForFilter:
210 return GenerateOatFile(error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800211 }
212 UNREACHABLE();
213}
214
215std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000216 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800217}
218
Richard Uhler46cc64f2016-11-14 14:53:55 +0000219std::string OatFileAssistant::GetStatusDump() {
220 std::ostringstream status;
221 bool oat_file_exists = false;
222 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000223 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000224 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000225 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000226
Richard Uhler46cc64f2016-11-14 14:53:55 +0000227 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000228 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
229 const OatFile* file = oat_.GetFile();
230 if (file == nullptr) {
231 // If the file is null even though the status is not kOatCannotOpen, it
232 // means we must have a vdex file with no corresponding oat file. In
233 // this case we cannot determine the compilation filter. Indicate that
234 // we have only the vdex file instead.
235 status << "vdex-only";
236 } else {
237 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
238 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000239 }
240
Richard Uhler03bc6592016-11-22 09:42:04 +0000241 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000242 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000243 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000244
Richard Uhler46cc64f2016-11-14 14:53:55 +0000245 odex_file_exists = true;
246 if (oat_file_exists) {
247 status << "] ";
248 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000249 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
250 const OatFile* file = odex_.GetFile();
251 if (file == nullptr) {
252 status << "vdex-only";
253 } else {
254 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
255 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000256 }
257
258 if (!oat_file_exists && !odex_file_exists) {
259 status << "invalid[";
260 }
261
262 status << "]";
263 return status.str();
264}
265
Richard Uhler66d874d2015-01-15 09:37:19 -0800266std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
267 const OatFile& oat_file, const char* dex_location) {
268 std::vector<std::unique_ptr<const DexFile>> dex_files;
269
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000270 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 std::string error_msg;
272 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700273 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800274 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700275 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 return std::vector<std::unique_ptr<const DexFile>>();
277 }
278
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700279 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800280 if (dex_file.get() == nullptr) {
281 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
282 return std::vector<std::unique_ptr<const DexFile>>();
283 }
284 dex_files.push_back(std::move(dex_file));
285
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000286 // Load the rest of the multidex entries
Andreas Gampe90e34042015-04-27 20:01:52 -0700287 for (size_t i = 1; ; i++) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000288 std::string multidex_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
289 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700290 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000291 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800292 break;
293 }
294
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700295 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 if (dex_file.get() == nullptr) {
297 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
298 return std::vector<std::unique_ptr<const DexFile>>();
299 }
300 dex_files.push_back(std::move(dex_file));
301 }
302 return dex_files;
303}
304
Richard Uhler9b994ea2015-06-24 08:44:19 -0700305bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000306 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700307 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000308 // GetRequiredDexChecksums.
309 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700310 return has_original_dex_files_;
311}
312
Richard Uhler95abd042015-03-24 09:51:28 -0700313OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700314 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800315}
316
Richard Uhler95abd042015-03-24 09:51:28 -0700317OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700318 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800319}
320
Richard Uhler2f27abd2017-01-31 14:02:34 +0000321bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000322 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
323 if (required_dex_checksums == nullptr) {
324 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
325 return true;
326 }
327
328 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
329 if (required_dex_checksums->size() != number_of_dex_files) {
330 *error_msg = StringPrintf("expected %zu dex files but found %u",
331 required_dex_checksums->size(),
332 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000333 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800334 }
335
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000336 for (uint32_t i = 0; i < number_of_dex_files; i++) {
337 uint32_t expected_checksum = (*required_dex_checksums)[i];
338 uint32_t actual_checksum = file.GetLocationChecksum(i);
339 if (expected_checksum != actual_checksum) {
340 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
341 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
342 "Expected: %u, actual: %u",
343 dex.c_str(),
344 expected_checksum,
345 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000346 return false;
347 }
348 }
349
Richard Uhler2f27abd2017-01-31 14:02:34 +0000350 return true;
351}
352
353bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000354 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
355 if (required_dex_checksums == nullptr) {
356 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
357 return true;
358 }
359
360 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
361 if (required_dex_checksums->size() != number_of_dex_files) {
362 *error_msg = StringPrintf("expected %zu dex files but found %u",
363 required_dex_checksums->size(),
364 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000365 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800366 }
367
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000368 for (uint32_t i = 0; i < number_of_dex_files; i++) {
369 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
370 uint32_t expected_checksum = (*required_dex_checksums)[i];
371 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
372 if (oat_dex_file == nullptr) {
373 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
374 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800375 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000376 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
377 if (expected_checksum != actual_checksum) {
378 VLOG(oat) << "Dex checksum does not match for dex: " << dex
379 << ". Expected: " << expected_checksum
380 << ", Actual: " << actual_checksum;
381 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800382 }
383 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000384 return true;
385}
386
387OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
388 // Verify the ART_USE_READ_BARRIER state.
389 // TODO: Don't fully reject files due to read barrier state. If they contain
390 // compiled code and are otherwise okay, we should return something like
391 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
392 // barrier state doesn't matter.
393 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
394 constexpr bool kRuntimeIsCC = kUseReadBarrier;
395 if (is_cc != kRuntimeIsCC) {
396 return kOatCannotOpen;
397 }
398
399 // Verify the dex checksum.
400 std::string error_msg;
401 if (kIsVdexEnabled) {
402 VdexFile* vdex = file.GetVdexFile();
403 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
404 LOG(ERROR) << error_msg;
405 return kOatDexOutOfDate;
406 }
407 } else {
408 if (!DexChecksumUpToDate(file, &error_msg)) {
409 LOG(ERROR) << error_msg;
410 return kOatDexOutOfDate;
411 }
412 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800413
Andreas Gampe29d38e72016-03-23 15:31:51 +0000414 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000415
Richard Uhler66d874d2015-01-15 09:37:19 -0800416 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000417 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
418 const ImageInfo* image_info = GetImageInfo();
419 if (image_info == nullptr) {
420 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000421
Richard Uhler76f5cb62016-04-04 13:30:16 -0700422 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000423 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700424 }
425
426 // If there is no original dex file to fall back to, grudgingly accept
427 // the oat file. This could technically lead to crashes, but there's no
428 // way we could find a better oat file to use for this dex location,
429 // and it's better than being stuck in a boot loop with no way out.
430 // The problem will hopefully resolve itself the next time the runtime
431 // starts up.
432 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
433 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000434 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000435 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000436 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000437 }
438 } else {
439 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800440 }
441
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100442 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000443 if (!file.IsPic()) {
444 const ImageInfo* image_info = GetImageInfo();
445 if (image_info == nullptr) {
446 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000447 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000448 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700449
Andreas Gampe29d38e72016-03-23 15:31:51 +0000450 // Verify the oat_data_begin recorded for the image in the oat file matches
451 // the actual oat_data_begin for boot.oat in the image.
452 const OatHeader& oat_header = file.GetOatHeader();
453 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
454 if (oat_data_begin != image_info->oat_data_begin) {
455 VLOG(oat) << file.GetLocation() <<
456 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
457 << " does not match actual image oat_data_begin ("
458 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000459 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000460 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700461
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 // Verify the oat_patch_delta recorded for the image in the oat file matches
463 // the actual oat_patch_delta for the image.
464 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
465 if (oat_patch_delta != image_info->patch_delta) {
466 VLOG(oat) << file.GetLocation() <<
467 ": Oat file image patch delta (" << oat_patch_delta << ")"
468 << " does not match actual image patch delta ("
469 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000470 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000471 }
472 } else {
473 // Oat files compiled in PIC mode do not require relocation.
474 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
475 }
476 } else {
477 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800478 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700479 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800480}
481
Richard Uhler1e860612016-03-30 12:17:55 -0700482OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700483OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 CHECK(error_msg != nullptr);
485
Richard Uhler8327cf72015-10-13 16:34:59 -0700486 Runtime* runtime = Runtime::Current();
487 if (!runtime->IsDex2OatEnabled()) {
488 *error_msg = "Generation of oat file for dex location " + dex_location_
489 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700490 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700491 }
492
Richard Uhler743bf362016-04-19 15:39:37 -0700493 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700494 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800495 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700496 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 }
Richard Uhler743bf362016-04-19 15:39:37 -0700498 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100499 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800500
Richard Uhler66d874d2015-01-15 09:37:19 -0800501 // dex2oat ignores missing dex files and doesn't report an error.
502 // Check explicitly here so we can detect the error properly.
503 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700504 if (!OS::FileExists(dex_location_.c_str())) {
505 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700506 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800507 }
508
David Brazdil7b49e6c2016-09-01 11:06:18 +0100509 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
510 if (vdex_file.get() == nullptr) {
511 *error_msg = "Generation of oat file " + oat_file_name
512 + " not attempted because the vdex file " + vdex_file_name
513 + " could not be opened.";
514 return kUpdateNotAttempted;
515 }
516
517 if (fchmod(vdex_file->Fd(), 0644) != 0) {
518 *error_msg = "Generation of oat file " + oat_file_name
519 + " not attempted because the vdex file " + vdex_file_name
520 + " could not be made world readable.";
521 return kUpdateNotAttempted;
522 }
523
524 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700525 if (oat_file.get() == nullptr) {
526 *error_msg = "Generation of oat file " + oat_file_name
527 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700528 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700529 }
530
531 if (fchmod(oat_file->Fd(), 0644) != 0) {
532 *error_msg = "Generation of oat file " + oat_file_name
533 + " not attempted because the oat file could not be made world readable.";
534 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700535 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700536 }
537
538 std::vector<std::string> args;
539 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000540 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700541 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
542 args.push_back("--oat-location=" + oat_file_name);
543
Richard Uhler66d874d2015-01-15 09:37:19 -0800544 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100545 // Manually delete the oat and vdex files. This ensures there is no garbage
546 // left over if the process unexpectedly died.
547 vdex_file->Erase();
548 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700549 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100550 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700551 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700552 }
553
David Brazdil7b49e6c2016-09-01 11:06:18 +0100554 if (vdex_file->FlushCloseOrErase() != 0) {
555 *error_msg = "Unable to close vdex file " + vdex_file_name;
556 unlink(vdex_file_name.c_str());
557 return kUpdateFailed;
558 }
559
Richard Uhler8327cf72015-10-13 16:34:59 -0700560 if (oat_file->FlushCloseOrErase() != 0) {
561 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100562 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700563 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800564 }
565
566 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700567 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700568 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800569}
570
571bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
572 std::string* error_msg) {
573 Runtime* runtime = Runtime::Current();
574 std::string image_location = ImageLocation();
575 if (image_location.empty()) {
576 *error_msg = "No image location found for Dex2Oat.";
577 return false;
578 }
579
580 std::vector<std::string> argv;
581 argv.push_back(runtime->GetCompilerExecutable());
582 argv.push_back("--runtime-arg");
583 argv.push_back("-classpath");
584 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700585 std::string class_path = runtime->GetClassPathString();
586 if (class_path == "") {
587 class_path = OatFile::kSpecialSharedLibrary;
588 }
589 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000590 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200591 argv.push_back("--debuggable");
592 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700593 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800594
595 if (!runtime->IsVerificationEnabled()) {
596 argv.push_back("--compiler-filter=verify-none");
597 }
598
599 if (runtime->MustRelocateIfPossible()) {
600 argv.push_back("--runtime-arg");
601 argv.push_back("-Xrelocate");
602 } else {
603 argv.push_back("--runtime-arg");
604 argv.push_back("-Xnorelocate");
605 }
606
607 if (!kIsTargetBuild) {
608 argv.push_back("--host");
609 }
610
611 argv.push_back("--boot-image=" + image_location);
612
613 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
614 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
615
616 argv.insert(argv.end(), args.begin(), args.end());
617
Andreas Gampe9186ced2016-12-12 14:28:21 -0800618 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800619 return Exec(argv, error_msg);
620}
621
Richard Uhlerb81881d2016-04-19 13:08:04 -0700622bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
623 InstructionSet isa,
624 std::string* odex_filename,
625 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800626 CHECK(odex_filename != nullptr);
627 CHECK(error_msg != nullptr);
628
629 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700630 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800631 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700632 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800633
Richard Uhler63434112015-03-16 14:32:16 -0700634 // Find the directory portion of the dex location and add the oat/<isa>
635 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800636 size_t pos = location.rfind('/');
637 if (pos == std::string::npos) {
638 *error_msg = "Dex location " + location + " has no directory.";
639 return false;
640 }
641 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700642 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800643
Richard Uhler66d874d2015-01-15 09:37:19 -0800644 // Get the base part of the file without the extension.
Richard Uhlerc91b5a22017-03-27 16:53:39 +0100645 std::string file = location.substr(pos+1);
Richard Uhler66d874d2015-01-15 09:37:19 -0800646 pos = file.rfind('.');
647 if (pos == std::string::npos) {
648 *error_msg = "Dex location " + location + " has no extension.";
649 return false;
650 }
651 std::string base = file.substr(0, pos);
652
653 *odex_filename = dir + "/" + base + ".odex";
654 return true;
655}
656
Richard Uhlerb81881d2016-04-19 13:08:04 -0700657bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
658 InstructionSet isa,
659 std::string* oat_filename,
660 std::string* error_msg) {
661 CHECK(oat_filename != nullptr);
662 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800663
Richard Uhler55b58b62016-08-12 09:05:13 -0700664 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
665 if (cache_dir.empty()) {
666 *error_msg = "Dalvik cache directory does not exist";
667 return false;
668 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700669
670 // TODO: The oat file assistant should be the definitive place for
671 // determining the oat file name from the dex location, not
672 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700673 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800674}
675
Richard Uhler66d874d2015-01-15 09:37:19 -0800676std::string OatFileAssistant::ImageLocation() {
677 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000678 const std::vector<gc::space::ImageSpace*>& image_spaces =
679 runtime->GetHeap()->GetBootImageSpaces();
680 if (image_spaces.empty()) {
681 return "";
682 }
683 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800684}
685
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000686const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
687 if (!required_dex_checksums_attempted_) {
688 required_dex_checksums_attempted_ = true;
689 required_dex_checksums_found_ = false;
690 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800691 std::string error_msg;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000692 if (DexFile::GetMultiDexChecksums(dex_location_.c_str(),
693 &cached_required_dex_checksums_,
694 &error_msg)) {
695 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700696 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800697 } else {
698 // This can happen if the original dex file has been stripped from the
699 // apk.
700 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700701 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800702
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000703 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700704 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800705 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000706 required_dex_checksums_found_ = true;
707 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
708 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
709 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
710 if (odex_dex_file == nullptr) {
711 required_dex_checksums_found_ = false;
712 break;
713 }
714 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800715 }
716 }
717 }
718 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000719 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800720}
721
Richard Uhler95e09672017-02-22 11:37:41 +0000722std::unique_ptr<OatFileAssistant::ImageInfo>
723OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
724 CHECK(error_msg != nullptr);
725
Richard Uhler95e09672017-02-22 11:37:41 +0000726 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000727 std::unique_ptr<ImageInfo> info(new ImageInfo());
728 info->location = runtime->GetImageLocation();
729
730 std::unique_ptr<ImageHeader> image_header(
731 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
732 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000733 return nullptr;
734 }
735
Richard Uhler8f104862017-02-22 12:34:01 +0000736 info->oat_checksum = image_header->GetOatChecksum();
737 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
738 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000739 return info;
740}
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;
Richard Uhler95e09672017-02-22 11:37:41 +0000745 std::string error_msg;
746 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
747 if (cached_image_info_ == nullptr) {
748 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800749 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800750 }
Richard Uhler95e09672017-02-22 11:37:41 +0000751 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800752}
753
Richard Uhler88bc6732016-11-14 14:38:03 +0000754OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000755 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
756 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000757}
758
Andreas Gampea463b6a2016-08-12 21:53:32 -0700759std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800760 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100761 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800762 if (art_file.empty()) {
763 return nullptr;
764 }
765 std::string error_msg;
766 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700767 std::unique_ptr<gc::space::ImageSpace> ret =
768 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800769 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800770 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
771 }
772 return ret;
773}
774
Richard Uhler88bc6732016-11-14 14:38:03 +0000775OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
776 bool is_oat_location)
777 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700778{}
779
Richard Uhler88bc6732016-11-14 14:38:03 +0000780bool OatFileAssistant::OatFileInfo::IsOatLocation() {
781 return is_oat_location_;
782}
783
Richard Uhler743bf362016-04-19 15:39:37 -0700784const std::string* OatFileAssistant::OatFileInfo::Filename() {
785 return filename_provided_ ? &filename_ : nullptr;
786}
787
Richard Uhler03bc6592016-11-22 09:42:04 +0000788bool OatFileAssistant::OatFileInfo::IsUseable() {
789 switch (Status()) {
790 case kOatCannotOpen:
791 case kOatDexOutOfDate:
792 case kOatBootImageOutOfDate: return false;
793
794 case kOatRelocationOutOfDate:
795 case kOatUpToDate: return true;
796 }
797 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700798}
799
800OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
801 if (!status_attempted_) {
802 status_attempted_ = true;
803 const OatFile* file = GetFile();
804 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000805 // Check to see if there is a vdex file we can make use of.
806 std::string error_msg;
807 std::string vdex_filename = ReplaceFileExtension(filename_, "vdex");
808 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
809 /*writeable*/false,
810 /*low_4gb*/false,
811 &error_msg);
812 if (vdex == nullptr) {
813 status_ = kOatCannotOpen;
814 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
815 } else {
816 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
817 // The vdex file does not contain enough information to determine
818 // whether it is up to date with respect to the boot image, so we
819 // assume it is out of date.
820 VLOG(oat) << error_msg;
821 status_ = kOatBootImageOutOfDate;
822 } else {
823 status_ = kOatDexOutOfDate;
824 }
825 }
Richard Uhler743bf362016-04-19 15:39:37 -0700826 } else {
827 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700828 VLOG(oat) << file->GetLocation() << " is " << status_
829 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700830 }
831 }
832 return status_;
833}
834
Richard Uhler70a84262016-11-08 16:51:51 +0000835OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
836 CompilerFilter::Filter target, bool profile_changed) {
837 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000838 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000839
Richard Uhler7225a8d2016-11-22 10:12:03 +0000840 if (filter_okay && Status() == kOatUpToDate) {
841 // The oat file is in good shape as is.
842 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000843 }
844
Richard Uhler7225a8d2016-11-22 10:12:03 +0000845 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
846 // If no compilation is desired, then it doesn't matter if the oat
847 // file needs relocation. It's in good shape as is.
848 return kNoDexOptNeeded;
849 }
850
Richard Uhler7225a8d2016-11-22 10:12:03 +0000851 if (oat_file_assistant_->HasOriginalDexFiles()) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000852 if (filter_okay && Status() == kOatRelocationOutOfDate) {
853 return kDex2OatForRelocation;
854 }
855
856 if (IsUseable()) {
857 return kDex2OatForFilter;
858 }
859
860 if (Status() == kOatBootImageOutOfDate) {
861 return kDex2OatForBootImage;
862 }
863
864 return kDex2OatFromScratch;
865 }
866
867 // Otherwise there is nothing we can do, even if we want to.
868 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000869}
870
Richard Uhler743bf362016-04-19 15:39:37 -0700871const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
872 CHECK(!file_released_) << "GetFile called after oat file released.";
873 if (!load_attempted_) {
874 load_attempted_ = true;
875 if (filename_provided_) {
876 std::string error_msg;
877 file_.reset(OatFile::Open(filename_.c_str(),
878 filename_.c_str(),
879 nullptr,
880 nullptr,
881 oat_file_assistant_->load_executable_,
882 /*low_4gb*/false,
883 oat_file_assistant_->dex_location_.c_str(),
884 &error_msg));
885 if (file_.get() == nullptr) {
886 VLOG(oat) << "OatFileAssistant test for existing oat file "
887 << filename_ << ": " << error_msg;
888 }
889 }
890 }
891 return file_.get();
892}
893
894bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
895 CompilerFilter::Filter target, bool profile_changed) {
896 const OatFile* file = GetFile();
897 if (file == nullptr) {
898 return false;
899 }
900
901 CompilerFilter::Filter current = file->GetCompilerFilter();
902 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
903 VLOG(oat) << "Compiler filter not okay because Profile changed";
904 return false;
905 }
906 return CompilerFilter::IsAsGoodAs(current, target);
907}
908
909bool OatFileAssistant::OatFileInfo::IsExecutable() {
910 const OatFile* file = GetFile();
911 return (file != nullptr && file->IsExecutable());
912}
913
Richard Uhler743bf362016-04-19 15:39:37 -0700914void OatFileAssistant::OatFileInfo::Reset() {
915 load_attempted_ = false;
916 file_.reset();
917 status_attempted_ = false;
918}
919
920void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
921 filename_provided_ = true;
922 filename_ = filename;
923 Reset();
924}
925
926std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
927 file_released_ = true;
928 return std::move(file_);
929}
930
Richard Uhler70a84262016-11-08 16:51:51 +0000931std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000932 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000933 return ReleaseFile();
934 }
935
936 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
937 << " attempting to fall back to interpreting oat file instead.";
938
Richard Uhler03bc6592016-11-22 09:42:04 +0000939 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000940 return ReleaseFile();
941 }
942
Richard Uhler03bc6592016-11-22 09:42:04 +0000943 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000944 // We are loading an oat file for runtime use that needs relocation.
945 // Reload the file non-executable to ensure that we interpret out of the
946 // dex code in the oat file rather than trying to execute the unrelocated
947 // compiled code.
948 oat_file_assistant_->load_executable_ = false;
949 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +0000950 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000951 CHECK(!IsExecutable());
952 return ReleaseFile();
953 }
954 }
955 return std::unique_ptr<OatFile>();
956}
Richard Uhler66d874d2015-01-15 09:37:19 -0800957} // namespace art