blob: f12a5e774269498c0480f1a0c4fb26680dbcde27 [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_assistant.h"
18
Richard Uhler46cc64f2016-11-14 14:53:55 +000019#include <sstream>
20
Richard Uhler66d874d2015-01-15 09:37:19 -080021#include <sys/stat.h>
Andreas Gampe9186ced2016-12-12 14:28:21 -080022
23#include "android-base/strings.h"
24
Richard Uhler66d874d2015-01-15 09:37:19 -080025#include "base/logging.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010026#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080027#include "class_linker.h"
28#include "gc/heap.h"
29#include "gc/space/image_space.h"
30#include "image.h"
31#include "oat.h"
32#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "utils.h"
36
37namespace art {
38
Narayan Kamath8943c1d2016-05-02 13:14:48 +010039std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
40 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000041 case OatFileAssistant::kOatCannotOpen:
42 stream << "kOatCannotOpen";
43 break;
44 case OatFileAssistant::kOatDexOutOfDate:
45 stream << "kOatDexOutOfDate";
46 break;
47 case OatFileAssistant::kOatBootImageOutOfDate:
48 stream << "kOatBootImageOutOfDate";
49 break;
50 case OatFileAssistant::kOatRelocationOutOfDate:
51 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010052 break;
53 case OatFileAssistant::kOatUpToDate:
54 stream << "kOatUpToDate";
55 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010056 default:
57 UNREACHABLE();
58 }
59
60 return stream;
61}
62
Richard Uhler66d874d2015-01-15 09:37:19 -080063OatFileAssistant::OatFileAssistant(const char* dex_location,
64 const InstructionSet isa,
65 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070066 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000067{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080068
69OatFileAssistant::OatFileAssistant(const char* dex_location,
70 const char* oat_location,
71 const InstructionSet isa,
72 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000073 : isa_(isa),
74 load_executable_(load_executable),
75 odex_(this, /*is_oat_location*/ false),
76 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070077 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
78 dex_location_.assign(dex_location);
79
Richard Uhler66d874d2015-01-15 09:37:19 -080080 if (load_executable_ && isa != kRuntimeISA) {
81 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
82 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
83 load_executable_ = false;
84 }
85
Richard Uhler743bf362016-04-19 15:39:37 -070086 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070087 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070088 std::string odex_file_name;
89 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
90 odex_.Reset(odex_file_name);
91 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070092 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
93 }
94
Richard Uhler743bf362016-04-19 15:39:37 -070095 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080096 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070097 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -070098 } else {
Richard Uhler743bf362016-04-19 15:39:37 -070099 std::string oat_file_name;
100 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
101 oat_.Reset(oat_file_name);
102 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700103 LOG(WARNING) << "Failed to determine oat file name for dex location "
104 << dex_location_ << ": " << error_msg;
105 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800106 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800107}
108
109OatFileAssistant::~OatFileAssistant() {
110 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700111 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100112 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800113 }
114}
115
116bool OatFileAssistant::IsInBootClassPath() {
117 // Note: We check the current boot class path, regardless of the ISA
118 // specified by the user. This is okay, because the boot class path should
119 // be the same for all ISAs.
120 // TODO: Can we verify the boot class path is the same for all ISAs?
121 Runtime* runtime = Runtime::Current();
122 ClassLinker* class_linker = runtime->GetClassLinker();
123 const auto& boot_class_path = class_linker->GetBootClassPath();
124 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700125 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800126 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
127 return true;
128 }
129 }
130 return false;
131}
132
133bool OatFileAssistant::Lock(std::string* error_msg) {
134 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700135 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800136
Richard Uhler743bf362016-04-19 15:39:37 -0700137 const std::string* oat_file_name = oat_.Filename();
138 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800139 *error_msg = "Failed to determine lock file";
140 return false;
141 }
Richard Uhler743bf362016-04-19 15:39:37 -0700142 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800143
Richard Uhler581f4e92015-05-07 10:19:35 -0700144 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100145 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800146 return false;
147 }
148 return true;
149}
150
Richard Uhler7225a8d2016-11-22 10:12:03 +0000151int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000152 OatFileInfo& info = GetBestInfo();
153 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000154 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
155 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800156 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000157 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800158}
159
Richard Uhlerf4b34872016-04-13 11:03:46 -0700160// Figure out the currently specified compile filter option in the runtime.
161// Returns true on success, false if the compiler filter is invalid, in which
162// case error_msg describes the problem.
163static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
164 std::string* error_msg) {
165 CHECK(filter != nullptr);
166 CHECK(error_msg != nullptr);
167
168 *filter = CompilerFilter::kDefaultCompilerFilter;
169 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
170 if (option.starts_with("--compiler-filter=")) {
171 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
172 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
173 *error_msg = std::string("Unknown --compiler-filter value: ")
174 + std::string(compiler_filter_string);
175 return false;
176 }
177 }
178 }
179 return true;
180}
181
Richard Uhler01be6812016-05-17 10:34:52 -0700182bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000183 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700184}
185
Richard Uhler1e860612016-03-30 12:17:55 -0700186OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700187OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700188 CompilerFilter::Filter target;
189 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
190 return kUpdateNotAttempted;
191 }
192
Richard Uhler88bc6732016-11-14 14:38:03 +0000193 OatFileInfo& info = GetBestInfo();
194 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000195 case kNoDexOptNeeded:
196 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000197
Richard Uhler7225a8d2016-11-22 10:12:03 +0000198 // TODO: For now, don't bother with all the different ways we can call
199 // dex2oat to generate the oat file. Always generate the oat file as if it
200 // were kDex2OatFromScratch.
201 case kDex2OatFromScratch:
202 case kDex2OatForBootImage:
203 case kDex2OatForRelocation:
204 case kDex2OatForFilter:
205 return GenerateOatFile(error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800206 }
207 UNREACHABLE();
208}
209
210std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000211 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800212}
213
Richard Uhler46cc64f2016-11-14 14:53:55 +0000214std::string OatFileAssistant::GetStatusDump() {
215 std::ostringstream status;
216 bool oat_file_exists = false;
217 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000218 if (oat_.Status() != kOatCannotOpen) {
219 // If we can open the file, neither Filename nor GetFile should return null.
220 CHECK(oat_.Filename() != nullptr);
221 CHECK(oat_.GetFile() != nullptr);
222
Richard Uhler46cc64f2016-11-14 14:53:55 +0000223 oat_file_exists = true;
224 status << *oat_.Filename() << " [compilation_filter=";
Richard Uhler03bc6592016-11-22 09:42:04 +0000225 status << CompilerFilter::NameOfFilter(oat_.GetFile()->GetCompilerFilter());
Richard Uhler46cc64f2016-11-14 14:53:55 +0000226 status << ", status=" << oat_.Status();
227 }
228
Richard Uhler03bc6592016-11-22 09:42:04 +0000229 if (odex_.Status() != kOatCannotOpen) {
230 // If we can open the file, neither Filename nor GetFile should return null.
231 CHECK(odex_.Filename() != nullptr);
232 CHECK(odex_.GetFile() != nullptr);
233
Richard Uhler46cc64f2016-11-14 14:53:55 +0000234 odex_file_exists = true;
235 if (oat_file_exists) {
236 status << "] ";
237 }
238 status << *odex_.Filename() << " [compilation_filter=";
Richard Uhler03bc6592016-11-22 09:42:04 +0000239 status << CompilerFilter::NameOfFilter(odex_.GetFile()->GetCompilerFilter());
Richard Uhler46cc64f2016-11-14 14:53:55 +0000240 status << ", status=" << odex_.Status();
241 }
242
243 if (!oat_file_exists && !odex_file_exists) {
244 status << "invalid[";
245 }
246
247 status << "]";
248 return status.str();
249}
250
Richard Uhler66d874d2015-01-15 09:37:19 -0800251std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
252 const OatFile& oat_file, const char* dex_location) {
253 std::vector<std::unique_ptr<const DexFile>> dex_files;
254
255 // Load the primary dex file.
256 std::string error_msg;
257 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700258 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800259 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700260 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800261 return std::vector<std::unique_ptr<const DexFile>>();
262 }
263
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700264 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800265 if (dex_file.get() == nullptr) {
266 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
267 return std::vector<std::unique_ptr<const DexFile>>();
268 }
269 dex_files.push_back(std::move(dex_file));
270
271 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700272 for (size_t i = 1; ; i++) {
273 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700274 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700275 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 // There are no more secondary dex files to load.
277 break;
278 }
279
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700280 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800281 if (dex_file.get() == nullptr) {
282 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
283 return std::vector<std::unique_ptr<const DexFile>>();
284 }
285 dex_files.push_back(std::move(dex_file));
286 }
287 return dex_files;
288}
289
Richard Uhler9b994ea2015-06-24 08:44:19 -0700290bool OatFileAssistant::HasOriginalDexFiles() {
291 // Ensure GetRequiredDexChecksum has been run so that
292 // has_original_dex_files_ is initialized. We don't care about the result of
293 // GetRequiredDexChecksum.
294 GetRequiredDexChecksum();
295 return has_original_dex_files_;
296}
297
Richard Uhler95abd042015-03-24 09:51:28 -0700298OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700299 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800300}
301
Richard Uhler95abd042015-03-24 09:51:28 -0700302OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700303 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800304}
305
Richard Uhler95abd042015-03-24 09:51:28 -0700306OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800307 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700308 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800309 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700310 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800311 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
312 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700313 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700314 if (oat_dex_file == nullptr) {
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000315 LOG(ERROR) << error_msg;
Richard Uhler03bc6592016-11-22 09:42:04 +0000316 return kOatDexOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800317 }
318
319 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700320 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700321 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700323 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700324 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800325 // There are no more secondary dex files to check.
326 break;
327 }
328
Richard Uhler66d874d2015-01-15 09:37:19 -0800329 uint32_t expected_secondary_checksum = 0;
330 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700331 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800332 uint32_t actual_secondary_checksum
333 = secondary_oat_dex_file->GetDexFileLocationChecksum();
334 if (expected_secondary_checksum != actual_secondary_checksum) {
335 VLOG(oat) << "Dex checksum does not match for secondary dex: "
336 << secondary_dex_location
337 << ". Expected: " << expected_secondary_checksum
338 << ", Actual: " << actual_secondary_checksum;
Richard Uhler03bc6592016-11-22 09:42:04 +0000339 return kOatDexOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800340 }
341 } else {
342 // If we can't get the checksum for the secondary location, we assume
343 // the dex checksum is up to date for this and all other secondary dex
344 // files.
345 break;
346 }
347 }
348
Andreas Gampe29d38e72016-03-23 15:31:51 +0000349 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000350
Richard Uhler66d874d2015-01-15 09:37:19 -0800351 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000352 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
353 const ImageInfo* image_info = GetImageInfo();
354 if (image_info == nullptr) {
355 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000356
Richard Uhler76f5cb62016-04-04 13:30:16 -0700357 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000358 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700359 }
360
361 // If there is no original dex file to fall back to, grudgingly accept
362 // the oat file. This could technically lead to crashes, but there's no
363 // way we could find a better oat file to use for this dex location,
364 // and it's better than being stuck in a boot loop with no way out.
365 // The problem will hopefully resolve itself the next time the runtime
366 // starts up.
367 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
368 << "Allow oat file use. This is potentially dangerous.";
369 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
370 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000371 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000372 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000373 }
374 } else {
375 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800376 }
377
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100378 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000379 if (!file.IsPic()) {
380 const ImageInfo* image_info = GetImageInfo();
381 if (image_info == nullptr) {
382 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000383 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000384 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700385
Andreas Gampe29d38e72016-03-23 15:31:51 +0000386 // Verify the oat_data_begin recorded for the image in the oat file matches
387 // the actual oat_data_begin for boot.oat in the image.
388 const OatHeader& oat_header = file.GetOatHeader();
389 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
390 if (oat_data_begin != image_info->oat_data_begin) {
391 VLOG(oat) << file.GetLocation() <<
392 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
393 << " does not match actual image oat_data_begin ("
394 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000395 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000396 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700397
Andreas Gampe29d38e72016-03-23 15:31:51 +0000398 // Verify the oat_patch_delta recorded for the image in the oat file matches
399 // the actual oat_patch_delta for the image.
400 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
401 if (oat_patch_delta != image_info->patch_delta) {
402 VLOG(oat) << file.GetLocation() <<
403 ": Oat file image patch delta (" << oat_patch_delta << ")"
404 << " does not match actual image patch delta ("
405 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000406 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000407 }
408 } else {
409 // Oat files compiled in PIC mode do not require relocation.
410 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
411 }
412 } else {
413 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800414 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700415 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800416}
417
Richard Uhler1e860612016-03-30 12:17:55 -0700418OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700419OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800420 CHECK(error_msg != nullptr);
421
Richard Uhler8327cf72015-10-13 16:34:59 -0700422 Runtime* runtime = Runtime::Current();
423 if (!runtime->IsDex2OatEnabled()) {
424 *error_msg = "Generation of oat file for dex location " + dex_location_
425 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700426 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700427 }
428
Richard Uhler743bf362016-04-19 15:39:37 -0700429 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700430 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800431 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700432 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800433 }
Richard Uhler743bf362016-04-19 15:39:37 -0700434 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100435 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800436
Richard Uhler66d874d2015-01-15 09:37:19 -0800437 // dex2oat ignores missing dex files and doesn't report an error.
438 // Check explicitly here so we can detect the error properly.
439 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700440 if (!OS::FileExists(dex_location_.c_str())) {
441 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700442 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800443 }
444
David Brazdil7b49e6c2016-09-01 11:06:18 +0100445 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
446 if (vdex_file.get() == nullptr) {
447 *error_msg = "Generation of oat file " + oat_file_name
448 + " not attempted because the vdex file " + vdex_file_name
449 + " could not be opened.";
450 return kUpdateNotAttempted;
451 }
452
453 if (fchmod(vdex_file->Fd(), 0644) != 0) {
454 *error_msg = "Generation of oat file " + oat_file_name
455 + " not attempted because the vdex file " + vdex_file_name
456 + " could not be made world readable.";
457 return kUpdateNotAttempted;
458 }
459
460 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700461 if (oat_file.get() == nullptr) {
462 *error_msg = "Generation of oat file " + oat_file_name
463 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700464 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700465 }
466
467 if (fchmod(oat_file->Fd(), 0644) != 0) {
468 *error_msg = "Generation of oat file " + oat_file_name
469 + " not attempted because the oat file could not be made world readable.";
470 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700471 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700472 }
473
474 std::vector<std::string> args;
475 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000476 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700477 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
478 args.push_back("--oat-location=" + oat_file_name);
479
Richard Uhler66d874d2015-01-15 09:37:19 -0800480 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100481 // Manually delete the oat and vdex files. This ensures there is no garbage
482 // left over if the process unexpectedly died.
483 vdex_file->Erase();
484 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700485 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100486 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700487 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700488 }
489
David Brazdil7b49e6c2016-09-01 11:06:18 +0100490 if (vdex_file->FlushCloseOrErase() != 0) {
491 *error_msg = "Unable to close vdex file " + vdex_file_name;
492 unlink(vdex_file_name.c_str());
493 return kUpdateFailed;
494 }
495
Richard Uhler8327cf72015-10-13 16:34:59 -0700496 if (oat_file->FlushCloseOrErase() != 0) {
497 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100498 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700499 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800500 }
501
502 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700503 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700504 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800505}
506
507bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
508 std::string* error_msg) {
509 Runtime* runtime = Runtime::Current();
510 std::string image_location = ImageLocation();
511 if (image_location.empty()) {
512 *error_msg = "No image location found for Dex2Oat.";
513 return false;
514 }
515
516 std::vector<std::string> argv;
517 argv.push_back(runtime->GetCompilerExecutable());
518 argv.push_back("--runtime-arg");
519 argv.push_back("-classpath");
520 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700521 std::string class_path = runtime->GetClassPathString();
522 if (class_path == "") {
523 class_path = OatFile::kSpecialSharedLibrary;
524 }
525 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100526 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200527 argv.push_back("--debuggable");
528 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700529 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800530
531 if (!runtime->IsVerificationEnabled()) {
532 argv.push_back("--compiler-filter=verify-none");
533 }
534
535 if (runtime->MustRelocateIfPossible()) {
536 argv.push_back("--runtime-arg");
537 argv.push_back("-Xrelocate");
538 } else {
539 argv.push_back("--runtime-arg");
540 argv.push_back("-Xnorelocate");
541 }
542
543 if (!kIsTargetBuild) {
544 argv.push_back("--host");
545 }
546
547 argv.push_back("--boot-image=" + image_location);
548
549 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
550 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
551
552 argv.insert(argv.end(), args.begin(), args.end());
553
Andreas Gampe9186ced2016-12-12 14:28:21 -0800554 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800555 return Exec(argv, error_msg);
556}
557
Richard Uhlerb81881d2016-04-19 13:08:04 -0700558bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
559 InstructionSet isa,
560 std::string* odex_filename,
561 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800562 CHECK(odex_filename != nullptr);
563 CHECK(error_msg != nullptr);
564
565 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700566 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800567 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700568 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800569
Richard Uhler63434112015-03-16 14:32:16 -0700570 // Find the directory portion of the dex location and add the oat/<isa>
571 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800572 size_t pos = location.rfind('/');
573 if (pos == std::string::npos) {
574 *error_msg = "Dex location " + location + " has no directory.";
575 return false;
576 }
577 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700578 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800579
580 // Find the file portion of the dex location.
581 std::string file;
582 if (pos == std::string::npos) {
583 file = location;
584 } else {
585 file = location.substr(pos+1);
586 }
587
588 // Get the base part of the file without the extension.
589 pos = file.rfind('.');
590 if (pos == std::string::npos) {
591 *error_msg = "Dex location " + location + " has no extension.";
592 return false;
593 }
594 std::string base = file.substr(0, pos);
595
596 *odex_filename = dir + "/" + base + ".odex";
597 return true;
598}
599
Richard Uhlerb81881d2016-04-19 13:08:04 -0700600bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
601 InstructionSet isa,
602 std::string* oat_filename,
603 std::string* error_msg) {
604 CHECK(oat_filename != nullptr);
605 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800606
Richard Uhler55b58b62016-08-12 09:05:13 -0700607 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
608 if (cache_dir.empty()) {
609 *error_msg = "Dalvik cache directory does not exist";
610 return false;
611 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700612
613 // TODO: The oat file assistant should be the definitive place for
614 // determining the oat file name from the dex location, not
615 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700616 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800617}
618
Richard Uhler66d874d2015-01-15 09:37:19 -0800619std::string OatFileAssistant::ImageLocation() {
620 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000621 const std::vector<gc::space::ImageSpace*>& image_spaces =
622 runtime->GetHeap()->GetBootImageSpaces();
623 if (image_spaces.empty()) {
624 return "";
625 }
626 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800627}
628
629const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700630 if (!required_dex_checksum_attempted_) {
631 required_dex_checksum_attempted_ = true;
632 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800633 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700634 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700635 required_dex_checksum_found_ = true;
636 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800637 } else {
638 // This can happen if the original dex file has been stripped from the
639 // apk.
640 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700641 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800642
643 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700644 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800645 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700646 const OatFile::OatDexFile* odex_dex_file
647 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800648 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700649 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
650 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800651 }
652 }
653 }
654 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700655 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800656}
657
Richard Uhler66d874d2015-01-15 09:37:19 -0800658const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
659 if (!image_info_load_attempted_) {
660 image_info_load_attempted_ = true;
661
662 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800663 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
664 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800665 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800666
667 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800668 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800669 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800670 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
671 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800672 cached_image_info_.patch_delta = image_header.GetPatchDelta();
673 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700674 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800675 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700676 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
677 isa_,
678 &error_msg));
679 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800680 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800681 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
682 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800683 cached_image_info_.patch_delta = image_header->GetPatchDelta();
684 }
685 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800686 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700687
Jeff Haofd336c32016-04-07 19:46:31 -0700688 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800689 }
690 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
691}
692
Jeff Haob11ffb72016-04-07 15:40:54 -0700693// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700694uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700695 uint32_t checksum = 0;
696 std::vector<gc::space::ImageSpace*> image_spaces =
697 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700698 if (isa == kRuntimeISA) {
699 for (gc::space::ImageSpace* image_space : image_spaces) {
700 checksum ^= image_space->GetImageHeader().GetOatChecksum();
701 }
702 } else {
703 for (gc::space::ImageSpace* image_space : image_spaces) {
704 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700705 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700706 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700707 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
708 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700709 checksum ^= image_header->GetOatChecksum();
710 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700711 }
712 return checksum;
713}
714
715uint32_t OatFileAssistant::GetCombinedImageChecksum() {
716 if (!image_info_load_attempted_) {
717 GetImageInfo();
718 }
719 return combined_image_checksum_;
720}
721
Richard Uhler88bc6732016-11-14 14:38:03 +0000722OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000723 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
724 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000725}
726
Andreas Gampea463b6a2016-08-12 21:53:32 -0700727std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800728 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100729 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800730 if (art_file.empty()) {
731 return nullptr;
732 }
733 std::string error_msg;
734 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700735 std::unique_ptr<gc::space::ImageSpace> ret =
736 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800737 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800738 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
739 }
740 return ret;
741}
742
Richard Uhler88bc6732016-11-14 14:38:03 +0000743OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
744 bool is_oat_location)
745 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700746{}
747
Richard Uhler88bc6732016-11-14 14:38:03 +0000748bool OatFileAssistant::OatFileInfo::IsOatLocation() {
749 return is_oat_location_;
750}
751
Richard Uhler743bf362016-04-19 15:39:37 -0700752const std::string* OatFileAssistant::OatFileInfo::Filename() {
753 return filename_provided_ ? &filename_ : nullptr;
754}
755
Richard Uhler03bc6592016-11-22 09:42:04 +0000756bool OatFileAssistant::OatFileInfo::IsUseable() {
757 switch (Status()) {
758 case kOatCannotOpen:
759 case kOatDexOutOfDate:
760 case kOatBootImageOutOfDate: return false;
761
762 case kOatRelocationOutOfDate:
763 case kOatUpToDate: return true;
764 }
765 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700766}
767
768OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
769 if (!status_attempted_) {
770 status_attempted_ = true;
771 const OatFile* file = GetFile();
772 if (file == nullptr) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000773 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700774 } else {
775 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700776 VLOG(oat) << file->GetLocation() << " is " << status_
777 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700778 }
779 }
780 return status_;
781}
782
Richard Uhler70a84262016-11-08 16:51:51 +0000783OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
784 CompilerFilter::Filter target, bool profile_changed) {
785 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000786 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000787
Richard Uhler7225a8d2016-11-22 10:12:03 +0000788 if (filter_okay && Status() == kOatUpToDate) {
789 // The oat file is in good shape as is.
790 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000791 }
792
Richard Uhler7225a8d2016-11-22 10:12:03 +0000793 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
794 // If no compilation is desired, then it doesn't matter if the oat
795 // file needs relocation. It's in good shape as is.
796 return kNoDexOptNeeded;
797 }
798
Richard Uhler7225a8d2016-11-22 10:12:03 +0000799 if (oat_file_assistant_->HasOriginalDexFiles()) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000800 if (filter_okay && Status() == kOatRelocationOutOfDate) {
801 return kDex2OatForRelocation;
802 }
803
804 if (IsUseable()) {
805 return kDex2OatForFilter;
806 }
807
808 if (Status() == kOatBootImageOutOfDate) {
809 return kDex2OatForBootImage;
810 }
811
812 return kDex2OatFromScratch;
813 }
814
815 // Otherwise there is nothing we can do, even if we want to.
816 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000817}
818
Richard Uhler743bf362016-04-19 15:39:37 -0700819const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
820 CHECK(!file_released_) << "GetFile called after oat file released.";
821 if (!load_attempted_) {
822 load_attempted_ = true;
823 if (filename_provided_) {
824 std::string error_msg;
825 file_.reset(OatFile::Open(filename_.c_str(),
826 filename_.c_str(),
827 nullptr,
828 nullptr,
829 oat_file_assistant_->load_executable_,
830 /*low_4gb*/false,
831 oat_file_assistant_->dex_location_.c_str(),
832 &error_msg));
833 if (file_.get() == nullptr) {
834 VLOG(oat) << "OatFileAssistant test for existing oat file "
835 << filename_ << ": " << error_msg;
836 }
837 }
838 }
839 return file_.get();
840}
841
842bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
843 CompilerFilter::Filter target, bool profile_changed) {
844 const OatFile* file = GetFile();
845 if (file == nullptr) {
846 return false;
847 }
848
849 CompilerFilter::Filter current = file->GetCompilerFilter();
850 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
851 VLOG(oat) << "Compiler filter not okay because Profile changed";
852 return false;
853 }
854 return CompilerFilter::IsAsGoodAs(current, target);
855}
856
857bool OatFileAssistant::OatFileInfo::IsExecutable() {
858 const OatFile* file = GetFile();
859 return (file != nullptr && file->IsExecutable());
860}
861
Richard Uhler743bf362016-04-19 15:39:37 -0700862void OatFileAssistant::OatFileInfo::Reset() {
863 load_attempted_ = false;
864 file_.reset();
865 status_attempted_ = false;
866}
867
868void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
869 filename_provided_ = true;
870 filename_ = filename;
871 Reset();
872}
873
874std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
875 file_released_ = true;
876 return std::move(file_);
877}
878
Richard Uhler70a84262016-11-08 16:51:51 +0000879std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000880 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000881 return ReleaseFile();
882 }
883
884 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
885 << " attempting to fall back to interpreting oat file instead.";
886
Richard Uhler03bc6592016-11-22 09:42:04 +0000887 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000888 return ReleaseFile();
889 }
890
Richard Uhler03bc6592016-11-22 09:42:04 +0000891 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000892 // We are loading an oat file for runtime use that needs relocation.
893 // Reload the file non-executable to ensure that we interpret out of the
894 // dex code in the oat file rather than trying to execute the unrelocated
895 // compiled code.
896 oat_file_assistant_->load_executable_ = false;
897 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +0000898 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000899 CHECK(!IsExecutable());
900 return ReleaseFile();
901 }
902 }
903 return std::unique_ptr<OatFile>();
904}
Richard Uhler66d874d2015-01-15 09:37:19 -0800905} // namespace art
906