blob: 7f7b1b5b060153347eee5ff5de318f621d4dcd4b [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"
26#include "base/stringprintf.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"
29#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"
37
38namespace art {
39
Narayan Kamath8943c1d2016-05-02 13:14:48 +010040std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
41 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000042 case OatFileAssistant::kOatCannotOpen:
43 stream << "kOatCannotOpen";
44 break;
45 case OatFileAssistant::kOatDexOutOfDate:
46 stream << "kOatDexOutOfDate";
47 break;
48 case OatFileAssistant::kOatBootImageOutOfDate:
49 stream << "kOatBootImageOutOfDate";
50 break;
51 case OatFileAssistant::kOatRelocationOutOfDate:
52 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010053 break;
54 case OatFileAssistant::kOatUpToDate:
55 stream << "kOatUpToDate";
56 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010057 default:
58 UNREACHABLE();
59 }
60
61 return stream;
62}
63
Richard Uhler66d874d2015-01-15 09:37:19 -080064OatFileAssistant::OatFileAssistant(const char* dex_location,
65 const InstructionSet isa,
66 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070067 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000068{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080069
70OatFileAssistant::OatFileAssistant(const char* dex_location,
71 const char* oat_location,
72 const InstructionSet isa,
73 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000074 : isa_(isa),
75 load_executable_(load_executable),
76 odex_(this, /*is_oat_location*/ false),
77 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070078 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
79 dex_location_.assign(dex_location);
80
Richard Uhler66d874d2015-01-15 09:37:19 -080081 if (load_executable_ && isa != kRuntimeISA) {
82 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
83 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
84 load_executable_ = false;
85 }
86
Richard Uhler743bf362016-04-19 15:39:37 -070087 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070088 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070089 std::string odex_file_name;
90 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
91 odex_.Reset(odex_file_name);
92 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070093 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
94 }
95
Richard Uhler743bf362016-04-19 15:39:37 -070096 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080097 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070098 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -070099 } else {
Richard Uhler743bf362016-04-19 15:39:37 -0700100 std::string oat_file_name;
101 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
102 oat_.Reset(oat_file_name);
103 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700104 LOG(WARNING) << "Failed to determine oat file name for dex location "
105 << dex_location_ << ": " << error_msg;
106 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800107 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800108}
109
110OatFileAssistant::~OatFileAssistant() {
111 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700112 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100113 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800114 }
115}
116
117bool OatFileAssistant::IsInBootClassPath() {
118 // Note: We check the current boot class path, regardless of the ISA
119 // specified by the user. This is okay, because the boot class path should
120 // be the same for all ISAs.
121 // TODO: Can we verify the boot class path is the same for all ISAs?
122 Runtime* runtime = Runtime::Current();
123 ClassLinker* class_linker = runtime->GetClassLinker();
124 const auto& boot_class_path = class_linker->GetBootClassPath();
125 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700126 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800127 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
128 return true;
129 }
130 }
131 return false;
132}
133
134bool OatFileAssistant::Lock(std::string* error_msg) {
135 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700136 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800137
Richard Uhler743bf362016-04-19 15:39:37 -0700138 const std::string* oat_file_name = oat_.Filename();
139 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800140 *error_msg = "Failed to determine lock file";
141 return false;
142 }
Richard Uhler743bf362016-04-19 15:39:37 -0700143 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800144
Richard Uhler581f4e92015-05-07 10:19:35 -0700145 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100146 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800147 return false;
148 }
149 return true;
150}
151
Richard Uhler7225a8d2016-11-22 10:12:03 +0000152int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000153 OatFileInfo& info = GetBestInfo();
154 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000155 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
156 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800157 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000158 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800159}
160
Richard Uhlerf4b34872016-04-13 11:03:46 -0700161// Figure out the currently specified compile filter option in the runtime.
162// Returns true on success, false if the compiler filter is invalid, in which
163// case error_msg describes the problem.
164static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
165 std::string* error_msg) {
166 CHECK(filter != nullptr);
167 CHECK(error_msg != nullptr);
168
169 *filter = CompilerFilter::kDefaultCompilerFilter;
170 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
171 if (option.starts_with("--compiler-filter=")) {
172 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
173 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
174 *error_msg = std::string("Unknown --compiler-filter value: ")
175 + std::string(compiler_filter_string);
176 return false;
177 }
178 }
179 }
180 return true;
181}
182
Richard Uhler01be6812016-05-17 10:34:52 -0700183bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000184 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700185}
186
Richard Uhler1e860612016-03-30 12:17:55 -0700187OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700188OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700189 CompilerFilter::Filter target;
190 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
191 return kUpdateNotAttempted;
192 }
193
Richard Uhler88bc6732016-11-14 14:38:03 +0000194 OatFileInfo& info = GetBestInfo();
195 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000196 case kNoDexOptNeeded:
197 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000198
Richard Uhler7225a8d2016-11-22 10:12:03 +0000199 // TODO: For now, don't bother with all the different ways we can call
200 // dex2oat to generate the oat file. Always generate the oat file as if it
201 // were kDex2OatFromScratch.
202 case kDex2OatFromScratch:
203 case kDex2OatForBootImage:
204 case kDex2OatForRelocation:
205 case kDex2OatForFilter:
206 return GenerateOatFile(error_msg);
207
208 case kPatchoatForRelocation: {
209 return RelocateOatFile(info.Filename(), error_msg);
210 }
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) {
224 // If we can open the file, neither Filename nor GetFile should return null.
225 CHECK(oat_.Filename() != nullptr);
226 CHECK(oat_.GetFile() != nullptr);
227
Richard Uhler46cc64f2016-11-14 14:53:55 +0000228 oat_file_exists = true;
229 status << *oat_.Filename() << " [compilation_filter=";
Richard Uhler03bc6592016-11-22 09:42:04 +0000230 status << CompilerFilter::NameOfFilter(oat_.GetFile()->GetCompilerFilter());
Richard Uhler46cc64f2016-11-14 14:53:55 +0000231 status << ", status=" << oat_.Status();
232 }
233
Richard Uhler03bc6592016-11-22 09:42:04 +0000234 if (odex_.Status() != kOatCannotOpen) {
235 // If we can open the file, neither Filename nor GetFile should return null.
236 CHECK(odex_.Filename() != nullptr);
237 CHECK(odex_.GetFile() != nullptr);
238
Richard Uhler46cc64f2016-11-14 14:53:55 +0000239 odex_file_exists = true;
240 if (oat_file_exists) {
241 status << "] ";
242 }
243 status << *odex_.Filename() << " [compilation_filter=";
Richard Uhler03bc6592016-11-22 09:42:04 +0000244 status << CompilerFilter::NameOfFilter(odex_.GetFile()->GetCompilerFilter());
Richard Uhler46cc64f2016-11-14 14:53:55 +0000245 status << ", status=" << odex_.Status();
246 }
247
248 if (!oat_file_exists && !odex_file_exists) {
249 status << "invalid[";
250 }
251
252 status << "]";
253 return status.str();
254}
255
Richard Uhler66d874d2015-01-15 09:37:19 -0800256std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
257 const OatFile& oat_file, const char* dex_location) {
258 std::vector<std::unique_ptr<const DexFile>> dex_files;
259
260 // Load the primary dex file.
261 std::string error_msg;
262 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700263 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800264 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700265 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800266 return std::vector<std::unique_ptr<const DexFile>>();
267 }
268
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700269 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800270 if (dex_file.get() == nullptr) {
271 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
272 return std::vector<std::unique_ptr<const DexFile>>();
273 }
274 dex_files.push_back(std::move(dex_file));
275
276 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700277 for (size_t i = 1; ; i++) {
278 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700279 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700280 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800281 // There are no more secondary dex files to load.
282 break;
283 }
284
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700285 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800286 if (dex_file.get() == nullptr) {
287 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
288 return std::vector<std::unique_ptr<const DexFile>>();
289 }
290 dex_files.push_back(std::move(dex_file));
291 }
292 return dex_files;
293}
294
Richard Uhler9b994ea2015-06-24 08:44:19 -0700295bool OatFileAssistant::HasOriginalDexFiles() {
296 // Ensure GetRequiredDexChecksum has been run so that
297 // has_original_dex_files_ is initialized. We don't care about the result of
298 // GetRequiredDexChecksum.
299 GetRequiredDexChecksum();
300 return has_original_dex_files_;
301}
302
Richard Uhler95abd042015-03-24 09:51:28 -0700303OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700304 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800305}
306
Richard Uhler95abd042015-03-24 09:51:28 -0700307OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700308 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800309}
310
Richard Uhler95abd042015-03-24 09:51:28 -0700311OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800312 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700313 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800314 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700315 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800316 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
317 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700318 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700319 if (oat_dex_file == nullptr) {
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000320 LOG(ERROR) << error_msg;
Richard Uhler03bc6592016-11-22 09:42:04 +0000321 return kOatDexOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 }
323
324 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700325 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700326 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800327 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700328 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700329 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800330 // There are no more secondary dex files to check.
331 break;
332 }
333
Richard Uhler66d874d2015-01-15 09:37:19 -0800334 uint32_t expected_secondary_checksum = 0;
335 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700336 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800337 uint32_t actual_secondary_checksum
338 = secondary_oat_dex_file->GetDexFileLocationChecksum();
339 if (expected_secondary_checksum != actual_secondary_checksum) {
340 VLOG(oat) << "Dex checksum does not match for secondary dex: "
341 << secondary_dex_location
342 << ". Expected: " << expected_secondary_checksum
343 << ", Actual: " << actual_secondary_checksum;
Richard Uhler03bc6592016-11-22 09:42:04 +0000344 return kOatDexOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800345 }
346 } else {
347 // If we can't get the checksum for the secondary location, we assume
348 // the dex checksum is up to date for this and all other secondary dex
349 // files.
350 break;
351 }
352 }
353
Andreas Gampe29d38e72016-03-23 15:31:51 +0000354 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000355
Richard Uhler66d874d2015-01-15 09:37:19 -0800356 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000357 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
358 const ImageInfo* image_info = GetImageInfo();
359 if (image_info == nullptr) {
360 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000361
Richard Uhler76f5cb62016-04-04 13:30:16 -0700362 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000363 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700364 }
365
366 // If there is no original dex file to fall back to, grudgingly accept
367 // the oat file. This could technically lead to crashes, but there's no
368 // way we could find a better oat file to use for this dex location,
369 // and it's better than being stuck in a boot loop with no way out.
370 // The problem will hopefully resolve itself the next time the runtime
371 // starts up.
372 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
373 << "Allow oat file use. This is potentially dangerous.";
374 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
375 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000376 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000377 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000378 }
379 } else {
380 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 }
382
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100383 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000384 if (!file.IsPic()) {
385 const ImageInfo* image_info = GetImageInfo();
386 if (image_info == nullptr) {
387 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000388 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000389 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700390
Andreas Gampe29d38e72016-03-23 15:31:51 +0000391 // Verify the oat_data_begin recorded for the image in the oat file matches
392 // the actual oat_data_begin for boot.oat in the image.
393 const OatHeader& oat_header = file.GetOatHeader();
394 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
395 if (oat_data_begin != image_info->oat_data_begin) {
396 VLOG(oat) << file.GetLocation() <<
397 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
398 << " does not match actual image oat_data_begin ("
399 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000400 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000401 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700402
Andreas Gampe29d38e72016-03-23 15:31:51 +0000403 // Verify the oat_patch_delta recorded for the image in the oat file matches
404 // the actual oat_patch_delta for the image.
405 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
406 if (oat_patch_delta != image_info->patch_delta) {
407 VLOG(oat) << file.GetLocation() <<
408 ": Oat file image patch delta (" << oat_patch_delta << ")"
409 << " does not match actual image patch delta ("
410 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000411 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000412 }
413 } else {
414 // Oat files compiled in PIC mode do not require relocation.
415 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
416 }
417 } else {
418 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700420 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800421}
422
Richard Uhler1e860612016-03-30 12:17:55 -0700423OatFileAssistant::ResultOfAttemptToUpdate
424OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800425 CHECK(error_msg != nullptr);
426
Richard Uhler95abd042015-03-24 09:51:28 -0700427 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700428 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700429 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700430 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800431 }
Richard Uhler95abd042015-03-24 09:51:28 -0700432 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800433
Richard Uhler743bf362016-04-19 15:39:37 -0700434 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700435 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800436 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700437 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800438 }
Richard Uhler743bf362016-04-19 15:39:37 -0700439 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800440
441 const ImageInfo* image_info = GetImageInfo();
442 Runtime* runtime = Runtime::Current();
443 if (image_info == nullptr) {
444 *error_msg = "Patching of oat file " + oat_file_name
445 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700446 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800447 }
448
449 if (!runtime->IsDex2OatEnabled()) {
450 *error_msg = "Patching of oat file " + oat_file_name
451 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700452 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800453 }
454
455 std::vector<std::string> argv;
456 argv.push_back(runtime->GetPatchoatExecutable());
457 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700458 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 argv.push_back("--output-oat-file=" + oat_file_name);
460 argv.push_back("--patched-image-location=" + image_info->location);
461
Andreas Gampe9186ced2016-12-12 14:28:21 -0800462 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800463 if (!Exec(argv, error_msg)) {
464 // Manually delete the file. This ensures there is no garbage left over if
465 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100466 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700467 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800468 }
469
470 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700471 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700472 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800473}
474
Richard Uhler1e860612016-03-30 12:17:55 -0700475OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700476OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800477 CHECK(error_msg != nullptr);
478
Richard Uhler8327cf72015-10-13 16:34:59 -0700479 Runtime* runtime = Runtime::Current();
480 if (!runtime->IsDex2OatEnabled()) {
481 *error_msg = "Generation of oat file for dex location " + dex_location_
482 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700483 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700484 }
485
Richard Uhler743bf362016-04-19 15:39:37 -0700486 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700487 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800488 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700489 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800490 }
Richard Uhler743bf362016-04-19 15:39:37 -0700491 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100492 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800493
Richard Uhler66d874d2015-01-15 09:37:19 -0800494 // dex2oat ignores missing dex files and doesn't report an error.
495 // Check explicitly here so we can detect the error properly.
496 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700497 if (!OS::FileExists(dex_location_.c_str())) {
498 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700499 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800500 }
501
David Brazdil7b49e6c2016-09-01 11:06:18 +0100502 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
503 if (vdex_file.get() == nullptr) {
504 *error_msg = "Generation of oat file " + oat_file_name
505 + " not attempted because the vdex file " + vdex_file_name
506 + " could not be opened.";
507 return kUpdateNotAttempted;
508 }
509
510 if (fchmod(vdex_file->Fd(), 0644) != 0) {
511 *error_msg = "Generation of oat file " + oat_file_name
512 + " not attempted because the vdex file " + vdex_file_name
513 + " could not be made world readable.";
514 return kUpdateNotAttempted;
515 }
516
517 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700518 if (oat_file.get() == nullptr) {
519 *error_msg = "Generation of oat file " + oat_file_name
520 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700521 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700522 }
523
524 if (fchmod(oat_file->Fd(), 0644) != 0) {
525 *error_msg = "Generation of oat file " + oat_file_name
526 + " not attempted because the oat file could not be made world readable.";
527 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700528 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700529 }
530
531 std::vector<std::string> args;
532 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000533 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700534 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
535 args.push_back("--oat-location=" + oat_file_name);
536
Richard Uhler66d874d2015-01-15 09:37:19 -0800537 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100538 // Manually delete the oat and vdex files. This ensures there is no garbage
539 // left over if the process unexpectedly died.
540 vdex_file->Erase();
541 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700542 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100543 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700544 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700545 }
546
David Brazdil7b49e6c2016-09-01 11:06:18 +0100547 if (vdex_file->FlushCloseOrErase() != 0) {
548 *error_msg = "Unable to close vdex file " + vdex_file_name;
549 unlink(vdex_file_name.c_str());
550 return kUpdateFailed;
551 }
552
Richard Uhler8327cf72015-10-13 16:34:59 -0700553 if (oat_file->FlushCloseOrErase() != 0) {
554 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100555 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700556 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800557 }
558
559 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700560 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700561 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800562}
563
564bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
565 std::string* error_msg) {
566 Runtime* runtime = Runtime::Current();
567 std::string image_location = ImageLocation();
568 if (image_location.empty()) {
569 *error_msg = "No image location found for Dex2Oat.";
570 return false;
571 }
572
573 std::vector<std::string> argv;
574 argv.push_back(runtime->GetCompilerExecutable());
575 argv.push_back("--runtime-arg");
576 argv.push_back("-classpath");
577 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700578 std::string class_path = runtime->GetClassPathString();
579 if (class_path == "") {
580 class_path = OatFile::kSpecialSharedLibrary;
581 }
582 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100583 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200584 argv.push_back("--debuggable");
585 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700586 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800587
588 if (!runtime->IsVerificationEnabled()) {
589 argv.push_back("--compiler-filter=verify-none");
590 }
591
592 if (runtime->MustRelocateIfPossible()) {
593 argv.push_back("--runtime-arg");
594 argv.push_back("-Xrelocate");
595 } else {
596 argv.push_back("--runtime-arg");
597 argv.push_back("-Xnorelocate");
598 }
599
600 if (!kIsTargetBuild) {
601 argv.push_back("--host");
602 }
603
604 argv.push_back("--boot-image=" + image_location);
605
606 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
607 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
608
609 argv.insert(argv.end(), args.begin(), args.end());
610
Andreas Gampe9186ced2016-12-12 14:28:21 -0800611 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800612 return Exec(argv, error_msg);
613}
614
Richard Uhlerb81881d2016-04-19 13:08:04 -0700615bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
616 InstructionSet isa,
617 std::string* odex_filename,
618 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800619 CHECK(odex_filename != nullptr);
620 CHECK(error_msg != nullptr);
621
622 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700623 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800624 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700625 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800626
Richard Uhler63434112015-03-16 14:32:16 -0700627 // Find the directory portion of the dex location and add the oat/<isa>
628 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800629 size_t pos = location.rfind('/');
630 if (pos == std::string::npos) {
631 *error_msg = "Dex location " + location + " has no directory.";
632 return false;
633 }
634 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700635 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800636
637 // Find the file portion of the dex location.
638 std::string file;
639 if (pos == std::string::npos) {
640 file = location;
641 } else {
642 file = location.substr(pos+1);
643 }
644
645 // Get the base part of the file without the extension.
646 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
686const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700687 if (!required_dex_checksum_attempted_) {
688 required_dex_checksum_attempted_ = true;
689 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800690 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700691 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700692 required_dex_checksum_found_ = true;
693 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800694 } else {
695 // This can happen if the original dex file has been stripped from the
696 // apk.
697 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700698 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800699
700 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700701 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800702 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700703 const OatFile::OatDexFile* odex_dex_file
704 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800705 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700706 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
707 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800708 }
709 }
710 }
711 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700712 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800713}
714
Richard Uhler66d874d2015-01-15 09:37:19 -0800715const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
716 if (!image_info_load_attempted_) {
717 image_info_load_attempted_ = true;
718
719 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800720 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
721 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800722 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800723
724 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800725 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800726 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800727 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
728 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800729 cached_image_info_.patch_delta = image_header.GetPatchDelta();
730 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700731 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800732 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700733 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
734 isa_,
735 &error_msg));
736 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800737 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800738 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
739 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800740 cached_image_info_.patch_delta = image_header->GetPatchDelta();
741 }
742 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800743 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700744
Jeff Haofd336c32016-04-07 19:46:31 -0700745 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800746 }
747 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
748}
749
Jeff Haob11ffb72016-04-07 15:40:54 -0700750// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700751uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700752 uint32_t checksum = 0;
753 std::vector<gc::space::ImageSpace*> image_spaces =
754 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700755 if (isa == kRuntimeISA) {
756 for (gc::space::ImageSpace* image_space : image_spaces) {
757 checksum ^= image_space->GetImageHeader().GetOatChecksum();
758 }
759 } else {
760 for (gc::space::ImageSpace* image_space : image_spaces) {
761 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700762 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700763 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700764 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
765 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700766 checksum ^= image_header->GetOatChecksum();
767 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700768 }
769 return checksum;
770}
771
772uint32_t OatFileAssistant::GetCombinedImageChecksum() {
773 if (!image_info_load_attempted_) {
774 GetImageInfo();
775 }
776 return combined_image_checksum_;
777}
778
Richard Uhler88bc6732016-11-14 14:38:03 +0000779OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000780 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
781 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000782}
783
Andreas Gampea463b6a2016-08-12 21:53:32 -0700784std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800785 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100786 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800787 if (art_file.empty()) {
788 return nullptr;
789 }
790 std::string error_msg;
791 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700792 std::unique_ptr<gc::space::ImageSpace> ret =
793 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800794 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800795 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
796 }
797 return ret;
798}
799
Richard Uhler88bc6732016-11-14 14:38:03 +0000800OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
801 bool is_oat_location)
802 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700803{}
804
Richard Uhler88bc6732016-11-14 14:38:03 +0000805bool OatFileAssistant::OatFileInfo::IsOatLocation() {
806 return is_oat_location_;
807}
808
Richard Uhler743bf362016-04-19 15:39:37 -0700809const std::string* OatFileAssistant::OatFileInfo::Filename() {
810 return filename_provided_ ? &filename_ : nullptr;
811}
812
Richard Uhler03bc6592016-11-22 09:42:04 +0000813bool OatFileAssistant::OatFileInfo::IsUseable() {
814 switch (Status()) {
815 case kOatCannotOpen:
816 case kOatDexOutOfDate:
817 case kOatBootImageOutOfDate: return false;
818
819 case kOatRelocationOutOfDate:
820 case kOatUpToDate: return true;
821 }
822 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700823}
824
825OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
826 if (!status_attempted_) {
827 status_attempted_ = true;
828 const OatFile* file = GetFile();
829 if (file == nullptr) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000830 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700831 } else {
832 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700833 VLOG(oat) << file->GetLocation() << " is " << status_
834 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700835 }
836 }
837 return status_;
838}
839
Richard Uhler70a84262016-11-08 16:51:51 +0000840OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
841 CompilerFilter::Filter target, bool profile_changed) {
842 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000843 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000844
Richard Uhler7225a8d2016-11-22 10:12:03 +0000845 if (filter_okay && Status() == kOatUpToDate) {
846 // The oat file is in good shape as is.
847 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000848 }
849
Richard Uhler7225a8d2016-11-22 10:12:03 +0000850 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
851 // If no compilation is desired, then it doesn't matter if the oat
852 // file needs relocation. It's in good shape as is.
853 return kNoDexOptNeeded;
854 }
855
856 if (filter_okay && Status() == kOatRelocationOutOfDate && HasPatchInfo()) {
857 return kPatchoatForRelocation;
858 }
859
860 if (oat_file_assistant_->HasOriginalDexFiles()) {
861 // Run dex2oat for relocation if we didn't have the patch info necessary
862 // to use patchoat.
863 if (filter_okay && Status() == kOatRelocationOutOfDate) {
864 return kDex2OatForRelocation;
865 }
866
867 if (IsUseable()) {
868 return kDex2OatForFilter;
869 }
870
871 if (Status() == kOatBootImageOutOfDate) {
872 return kDex2OatForBootImage;
873 }
874
875 return kDex2OatFromScratch;
876 }
877
878 // Otherwise there is nothing we can do, even if we want to.
879 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000880}
881
Richard Uhler743bf362016-04-19 15:39:37 -0700882const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
883 CHECK(!file_released_) << "GetFile called after oat file released.";
884 if (!load_attempted_) {
885 load_attempted_ = true;
886 if (filename_provided_) {
887 std::string error_msg;
888 file_.reset(OatFile::Open(filename_.c_str(),
889 filename_.c_str(),
890 nullptr,
891 nullptr,
892 oat_file_assistant_->load_executable_,
893 /*low_4gb*/false,
894 oat_file_assistant_->dex_location_.c_str(),
895 &error_msg));
896 if (file_.get() == nullptr) {
897 VLOG(oat) << "OatFileAssistant test for existing oat file "
898 << filename_ << ": " << error_msg;
899 }
900 }
901 }
902 return file_.get();
903}
904
905bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
906 CompilerFilter::Filter target, bool profile_changed) {
907 const OatFile* file = GetFile();
908 if (file == nullptr) {
909 return false;
910 }
911
912 CompilerFilter::Filter current = file->GetCompilerFilter();
913 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
914 VLOG(oat) << "Compiler filter not okay because Profile changed";
915 return false;
916 }
917 return CompilerFilter::IsAsGoodAs(current, target);
918}
919
920bool OatFileAssistant::OatFileInfo::IsExecutable() {
921 const OatFile* file = GetFile();
922 return (file != nullptr && file->IsExecutable());
923}
924
925bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
926 const OatFile* file = GetFile();
927 return (file != nullptr && file->HasPatchInfo());
928}
929
930void OatFileAssistant::OatFileInfo::Reset() {
931 load_attempted_ = false;
932 file_.reset();
933 status_attempted_ = false;
934}
935
936void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
937 filename_provided_ = true;
938 filename_ = filename;
939 Reset();
940}
941
942std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
943 file_released_ = true;
944 return std::move(file_);
945}
946
Richard Uhler70a84262016-11-08 16:51:51 +0000947std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000948 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000949 return ReleaseFile();
950 }
951
952 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
953 << " attempting to fall back to interpreting oat file instead.";
954
Richard Uhler03bc6592016-11-22 09:42:04 +0000955 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000956 return ReleaseFile();
957 }
958
Richard Uhler03bc6592016-11-22 09:42:04 +0000959 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000960 // We are loading an oat file for runtime use that needs relocation.
961 // Reload the file non-executable to ensure that we interpret out of the
962 // dex code in the oat file rather than trying to execute the unrelocated
963 // compiled code.
964 oat_file_assistant_->load_executable_ = false;
965 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +0000966 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000967 CHECK(!IsExecutable());
968 return ReleaseFile();
969 }
970 }
971 return std::unique_ptr<OatFile>();
972}
Richard Uhler66d874d2015-01-15 09:37:19 -0800973} // namespace art
974