blob: 95e218943001771ae2e20fb092408a07b1269bb1 [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>
Richard Uhler66d874d2015-01-15 09:37:19 -080022#include "base/logging.h"
23#include "base/stringprintf.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010024#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080025#include "class_linker.h"
26#include "gc/heap.h"
27#include "gc/space/image_space.h"
28#include "image.h"
29#include "oat.h"
30#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070032#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "utils.h"
34
35namespace art {
36
Narayan Kamath8943c1d2016-05-02 13:14:48 +010037std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
38 switch (status) {
39 case OatFileAssistant::kOatOutOfDate:
40 stream << "kOatOutOfDate";
41 break;
42 case OatFileAssistant::kOatUpToDate:
43 stream << "kOatUpToDate";
44 break;
45 case OatFileAssistant::kOatNeedsRelocation:
46 stream << "kOatNeedsRelocation";
47 break;
48 default:
49 UNREACHABLE();
50 }
51
52 return stream;
53}
54
Richard Uhler66d874d2015-01-15 09:37:19 -080055OatFileAssistant::OatFileAssistant(const char* dex_location,
56 const InstructionSet isa,
57 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070058 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000059{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080060
61OatFileAssistant::OatFileAssistant(const char* dex_location,
62 const char* oat_location,
63 const InstructionSet isa,
64 bool load_executable)
Richard Uhler743bf362016-04-19 15:39:37 -070065 : isa_(isa), load_executable_(load_executable), odex_(this), oat_(this) {
Richard Uhler740eec92015-10-15 15:12:23 -070066 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
67 dex_location_.assign(dex_location);
68
Richard Uhler66d874d2015-01-15 09:37:19 -080069 if (load_executable_ && isa != kRuntimeISA) {
70 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
71 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
72 load_executable_ = false;
73 }
74
Richard Uhler743bf362016-04-19 15:39:37 -070075 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070076 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070077 std::string odex_file_name;
78 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
79 odex_.Reset(odex_file_name);
80 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070081 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
82 }
83
Richard Uhler743bf362016-04-19 15:39:37 -070084 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080085 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070086 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -070087 } else {
Richard Uhler743bf362016-04-19 15:39:37 -070088 std::string oat_file_name;
89 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
90 oat_.Reset(oat_file_name);
91 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070092 LOG(WARNING) << "Failed to determine oat file name for dex location "
93 << dex_location_ << ": " << error_msg;
94 }
Richard Uhler66d874d2015-01-15 09:37:19 -080095 }
Richard Uhler66d874d2015-01-15 09:37:19 -080096}
97
98OatFileAssistant::~OatFileAssistant() {
99 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700100 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100101 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800102 }
103}
104
105bool OatFileAssistant::IsInBootClassPath() {
106 // Note: We check the current boot class path, regardless of the ISA
107 // specified by the user. This is okay, because the boot class path should
108 // be the same for all ISAs.
109 // TODO: Can we verify the boot class path is the same for all ISAs?
110 Runtime* runtime = Runtime::Current();
111 ClassLinker* class_linker = runtime->GetClassLinker();
112 const auto& boot_class_path = class_linker->GetBootClassPath();
113 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700114 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800115 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
116 return true;
117 }
118 }
119 return false;
120}
121
122bool OatFileAssistant::Lock(std::string* error_msg) {
123 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700124 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800125
Richard Uhler743bf362016-04-19 15:39:37 -0700126 const std::string* oat_file_name = oat_.Filename();
127 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800128 *error_msg = "Failed to determine lock file";
129 return false;
130 }
Richard Uhler743bf362016-04-19 15:39:37 -0700131 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800132
Richard Uhler581f4e92015-05-07 10:19:35 -0700133 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100134 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800135 return false;
136 }
137 return true;
138}
139
Richard Uhlerd1472a22016-04-15 15:18:56 -0700140OatFileAssistant::DexOptNeeded
Richard Uhler743bf362016-04-19 15:39:37 -0700141OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000142 if (oat_.Status() != kOatOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000143 DexOptNeeded dexopt_needed = oat_.GetDexOptNeeded(target, profile_changed);
144 if (dexopt_needed == kPatchOatNeeded) {
145 dexopt_needed = kSelfPatchOatNeeded;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000146 }
Richard Uhler70a84262016-11-08 16:51:51 +0000147 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800148 }
Richard Uhler70a84262016-11-08 16:51:51 +0000149 return odex_.GetDexOptNeeded(target, profile_changed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800150}
151
Richard Uhlerf4b34872016-04-13 11:03:46 -0700152// Figure out the currently specified compile filter option in the runtime.
153// Returns true on success, false if the compiler filter is invalid, in which
154// case error_msg describes the problem.
155static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
156 std::string* error_msg) {
157 CHECK(filter != nullptr);
158 CHECK(error_msg != nullptr);
159
160 *filter = CompilerFilter::kDefaultCompilerFilter;
161 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
162 if (option.starts_with("--compiler-filter=")) {
163 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
164 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
165 *error_msg = std::string("Unknown --compiler-filter value: ")
166 + std::string(compiler_filter_string);
167 return false;
168 }
169 }
170 }
171 return true;
172}
173
Richard Uhler01be6812016-05-17 10:34:52 -0700174bool OatFileAssistant::IsUpToDate() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000175 return oat_.Status() == kOatUpToDate || odex_.Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700176}
177
Richard Uhler1e860612016-03-30 12:17:55 -0700178OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700179OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700180 CompilerFilter::Filter target;
181 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
182 return kUpdateNotAttempted;
183 }
184
Richard Uhlerd1472a22016-04-15 15:18:56 -0700185 switch (GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700186 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700187 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler743bf362016-04-19 15:39:37 -0700188 case kPatchOatNeeded: return RelocateOatFile(odex_.Filename(), error_msg);
189 case kSelfPatchOatNeeded: return RelocateOatFile(oat_.Filename(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800190 }
191 UNREACHABLE();
192}
193
194std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000195 if (oat_.Status() != kOatOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000196 return oat_.ReleaseFileForUse();
Richard Uhler5f946da2015-07-17 12:28:32 -0700197 }
Richard Uhler70a84262016-11-08 16:51:51 +0000198 return odex_.ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800199}
200
Richard Uhler46cc64f2016-11-14 14:53:55 +0000201std::string OatFileAssistant::GetStatusDump() {
202 std::ostringstream status;
203 bool oat_file_exists = false;
204 bool odex_file_exists = false;
205 if (oat_.Exists()) {
206 oat_file_exists = true;
207 status << *oat_.Filename() << " [compilation_filter=";
208 status << CompilerFilter::NameOfFilter(oat_.CompilerFilter());
209 status << ", status=" << oat_.Status();
210 }
211
212 if (odex_.Exists()) {
213 odex_file_exists = true;
214 if (oat_file_exists) {
215 status << "] ";
216 }
217 status << *odex_.Filename() << " [compilation_filter=";
218 status << CompilerFilter::NameOfFilter(odex_.CompilerFilter());
219 status << ", status=" << odex_.Status();
220 }
221
222 if (!oat_file_exists && !odex_file_exists) {
223 status << "invalid[";
224 }
225
226 status << "]";
227 return status.str();
228}
229
Richard Uhler66d874d2015-01-15 09:37:19 -0800230std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
231 const OatFile& oat_file, const char* dex_location) {
232 std::vector<std::unique_ptr<const DexFile>> dex_files;
233
234 // Load the primary dex file.
235 std::string error_msg;
236 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700237 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800238 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700239 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800240 return std::vector<std::unique_ptr<const DexFile>>();
241 }
242
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700243 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800244 if (dex_file.get() == nullptr) {
245 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
246 return std::vector<std::unique_ptr<const DexFile>>();
247 }
248 dex_files.push_back(std::move(dex_file));
249
250 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700251 for (size_t i = 1; ; i++) {
252 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700253 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700254 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800255 // There are no more secondary dex files to load.
256 break;
257 }
258
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700259 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800260 if (dex_file.get() == nullptr) {
261 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
262 return std::vector<std::unique_ptr<const DexFile>>();
263 }
264 dex_files.push_back(std::move(dex_file));
265 }
266 return dex_files;
267}
268
Richard Uhler9b994ea2015-06-24 08:44:19 -0700269bool OatFileAssistant::HasOriginalDexFiles() {
270 // Ensure GetRequiredDexChecksum has been run so that
271 // has_original_dex_files_ is initialized. We don't care about the result of
272 // GetRequiredDexChecksum.
273 GetRequiredDexChecksum();
274 return has_original_dex_files_;
275}
276
Richard Uhler66d874d2015-01-15 09:37:19 -0800277bool OatFileAssistant::OdexFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700278 return odex_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800279}
280
Richard Uhler95abd042015-03-24 09:51:28 -0700281OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700282 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800283}
284
Richard Uhler66d874d2015-01-15 09:37:19 -0800285bool OatFileAssistant::OatFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700286 return oat_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800287}
288
Richard Uhler95abd042015-03-24 09:51:28 -0700289OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700290 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800291}
292
Richard Uhler95abd042015-03-24 09:51:28 -0700293OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800294 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700295 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700297 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800298 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
299 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700300 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700301 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700302 VLOG(oat) << error_msg;
Richard Uhlere8109f72016-04-18 10:40:50 -0700303 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800304 }
305
306 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700307 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700308 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800309 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700310 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700311 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800312 // There are no more secondary dex files to check.
313 break;
314 }
315
Richard Uhler66d874d2015-01-15 09:37:19 -0800316 uint32_t expected_secondary_checksum = 0;
317 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700318 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800319 uint32_t actual_secondary_checksum
320 = secondary_oat_dex_file->GetDexFileLocationChecksum();
321 if (expected_secondary_checksum != actual_secondary_checksum) {
322 VLOG(oat) << "Dex checksum does not match for secondary dex: "
323 << secondary_dex_location
324 << ". Expected: " << expected_secondary_checksum
325 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700326 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800327 }
328 } else {
329 // If we can't get the checksum for the secondary location, we assume
330 // the dex checksum is up to date for this and all other secondary dex
331 // files.
332 break;
333 }
334 }
335
Andreas Gampe29d38e72016-03-23 15:31:51 +0000336 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000337
Richard Uhler66d874d2015-01-15 09:37:19 -0800338 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000339 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
340 const ImageInfo* image_info = GetImageInfo();
341 if (image_info == nullptr) {
342 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000343
Richard Uhler76f5cb62016-04-04 13:30:16 -0700344 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700345 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700346 }
347
348 // If there is no original dex file to fall back to, grudgingly accept
349 // the oat file. This could technically lead to crashes, but there's no
350 // way we could find a better oat file to use for this dex location,
351 // and it's better than being stuck in a boot loop with no way out.
352 // The problem will hopefully resolve itself the next time the runtime
353 // starts up.
354 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
355 << "Allow oat file use. This is potentially dangerous.";
356 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
357 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000358 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700359 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000360 }
361 } else {
362 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800363 }
364
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100365 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000366 if (!file.IsPic()) {
367 const ImageInfo* image_info = GetImageInfo();
368 if (image_info == nullptr) {
369 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700370 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000371 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700372
Andreas Gampe29d38e72016-03-23 15:31:51 +0000373 // Verify the oat_data_begin recorded for the image in the oat file matches
374 // the actual oat_data_begin for boot.oat in the image.
375 const OatHeader& oat_header = file.GetOatHeader();
376 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
377 if (oat_data_begin != image_info->oat_data_begin) {
378 VLOG(oat) << file.GetLocation() <<
379 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
380 << " does not match actual image oat_data_begin ("
381 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700382 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000383 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700384
Andreas Gampe29d38e72016-03-23 15:31:51 +0000385 // Verify the oat_patch_delta recorded for the image in the oat file matches
386 // the actual oat_patch_delta for the image.
387 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
388 if (oat_patch_delta != image_info->patch_delta) {
389 VLOG(oat) << file.GetLocation() <<
390 ": Oat file image patch delta (" << oat_patch_delta << ")"
391 << " does not match actual image patch delta ("
392 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700393 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000394 }
395 } else {
396 // Oat files compiled in PIC mode do not require relocation.
397 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
398 }
399 } else {
400 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800401 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700402 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800403}
404
Richard Uhler1e860612016-03-30 12:17:55 -0700405OatFileAssistant::ResultOfAttemptToUpdate
406OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800407 CHECK(error_msg != nullptr);
408
Richard Uhler95abd042015-03-24 09:51:28 -0700409 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700410 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700411 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700412 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800413 }
Richard Uhler95abd042015-03-24 09:51:28 -0700414 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800415
Richard Uhler743bf362016-04-19 15:39:37 -0700416 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700417 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800418 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700419 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800420 }
Richard Uhler743bf362016-04-19 15:39:37 -0700421 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800422
423 const ImageInfo* image_info = GetImageInfo();
424 Runtime* runtime = Runtime::Current();
425 if (image_info == nullptr) {
426 *error_msg = "Patching of oat file " + oat_file_name
427 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700428 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800429 }
430
431 if (!runtime->IsDex2OatEnabled()) {
432 *error_msg = "Patching of oat file " + oat_file_name
433 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700434 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800435 }
436
437 std::vector<std::string> argv;
438 argv.push_back(runtime->GetPatchoatExecutable());
439 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700440 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800441 argv.push_back("--output-oat-file=" + oat_file_name);
442 argv.push_back("--patched-image-location=" + image_info->location);
443
444 std::string command_line(Join(argv, ' '));
445 if (!Exec(argv, error_msg)) {
446 // Manually delete the file. This ensures there is no garbage left over if
447 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100448 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700449 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800450 }
451
452 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700453 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700454 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800455}
456
Richard Uhler1e860612016-03-30 12:17:55 -0700457OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700458OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 CHECK(error_msg != nullptr);
460
Richard Uhler8327cf72015-10-13 16:34:59 -0700461 Runtime* runtime = Runtime::Current();
462 if (!runtime->IsDex2OatEnabled()) {
463 *error_msg = "Generation of oat file for dex location " + dex_location_
464 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700465 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700466 }
467
Richard Uhler743bf362016-04-19 15:39:37 -0700468 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700469 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800470 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700471 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800472 }
Richard Uhler743bf362016-04-19 15:39:37 -0700473 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100474 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800475
Richard Uhler66d874d2015-01-15 09:37:19 -0800476 // dex2oat ignores missing dex files and doesn't report an error.
477 // Check explicitly here so we can detect the error properly.
478 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700479 if (!OS::FileExists(dex_location_.c_str())) {
480 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700481 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800482 }
483
David Brazdil7b49e6c2016-09-01 11:06:18 +0100484 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
485 if (vdex_file.get() == nullptr) {
486 *error_msg = "Generation of oat file " + oat_file_name
487 + " not attempted because the vdex file " + vdex_file_name
488 + " could not be opened.";
489 return kUpdateNotAttempted;
490 }
491
492 if (fchmod(vdex_file->Fd(), 0644) != 0) {
493 *error_msg = "Generation of oat file " + oat_file_name
494 + " not attempted because the vdex file " + vdex_file_name
495 + " could not be made world readable.";
496 return kUpdateNotAttempted;
497 }
498
499 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700500 if (oat_file.get() == nullptr) {
501 *error_msg = "Generation of oat file " + oat_file_name
502 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700503 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700504 }
505
506 if (fchmod(oat_file->Fd(), 0644) != 0) {
507 *error_msg = "Generation of oat file " + oat_file_name
508 + " not attempted because the oat file could not be made world readable.";
509 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700510 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700511 }
512
513 std::vector<std::string> args;
514 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000515 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700516 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
517 args.push_back("--oat-location=" + oat_file_name);
518
Richard Uhler66d874d2015-01-15 09:37:19 -0800519 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100520 // Manually delete the oat and vdex files. This ensures there is no garbage
521 // left over if the process unexpectedly died.
522 vdex_file->Erase();
523 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700524 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100525 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700526 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700527 }
528
David Brazdil7b49e6c2016-09-01 11:06:18 +0100529 if (vdex_file->FlushCloseOrErase() != 0) {
530 *error_msg = "Unable to close vdex file " + vdex_file_name;
531 unlink(vdex_file_name.c_str());
532 return kUpdateFailed;
533 }
534
Richard Uhler8327cf72015-10-13 16:34:59 -0700535 if (oat_file->FlushCloseOrErase() != 0) {
536 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100537 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700538 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800539 }
540
541 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700542 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700543 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800544}
545
546bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
547 std::string* error_msg) {
548 Runtime* runtime = Runtime::Current();
549 std::string image_location = ImageLocation();
550 if (image_location.empty()) {
551 *error_msg = "No image location found for Dex2Oat.";
552 return false;
553 }
554
555 std::vector<std::string> argv;
556 argv.push_back(runtime->GetCompilerExecutable());
557 argv.push_back("--runtime-arg");
558 argv.push_back("-classpath");
559 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700560 std::string class_path = runtime->GetClassPathString();
561 if (class_path == "") {
562 class_path = OatFile::kSpecialSharedLibrary;
563 }
564 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100565 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200566 argv.push_back("--debuggable");
567 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700568 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800569
570 if (!runtime->IsVerificationEnabled()) {
571 argv.push_back("--compiler-filter=verify-none");
572 }
573
574 if (runtime->MustRelocateIfPossible()) {
575 argv.push_back("--runtime-arg");
576 argv.push_back("-Xrelocate");
577 } else {
578 argv.push_back("--runtime-arg");
579 argv.push_back("-Xnorelocate");
580 }
581
582 if (!kIsTargetBuild) {
583 argv.push_back("--host");
584 }
585
586 argv.push_back("--boot-image=" + image_location);
587
588 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
589 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
590
591 argv.insert(argv.end(), args.begin(), args.end());
592
593 std::string command_line(Join(argv, ' '));
594 return Exec(argv, error_msg);
595}
596
Richard Uhlerb81881d2016-04-19 13:08:04 -0700597bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
598 InstructionSet isa,
599 std::string* odex_filename,
600 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800601 CHECK(odex_filename != nullptr);
602 CHECK(error_msg != nullptr);
603
604 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700605 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800606 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700607 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800608
Richard Uhler63434112015-03-16 14:32:16 -0700609 // Find the directory portion of the dex location and add the oat/<isa>
610 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800611 size_t pos = location.rfind('/');
612 if (pos == std::string::npos) {
613 *error_msg = "Dex location " + location + " has no directory.";
614 return false;
615 }
616 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700617 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800618
619 // Find the file portion of the dex location.
620 std::string file;
621 if (pos == std::string::npos) {
622 file = location;
623 } else {
624 file = location.substr(pos+1);
625 }
626
627 // Get the base part of the file without the extension.
628 pos = file.rfind('.');
629 if (pos == std::string::npos) {
630 *error_msg = "Dex location " + location + " has no extension.";
631 return false;
632 }
633 std::string base = file.substr(0, pos);
634
635 *odex_filename = dir + "/" + base + ".odex";
636 return true;
637}
638
Richard Uhlerb81881d2016-04-19 13:08:04 -0700639bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
640 InstructionSet isa,
641 std::string* oat_filename,
642 std::string* error_msg) {
643 CHECK(oat_filename != nullptr);
644 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800645
Richard Uhler55b58b62016-08-12 09:05:13 -0700646 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
647 if (cache_dir.empty()) {
648 *error_msg = "Dalvik cache directory does not exist";
649 return false;
650 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700651
652 // TODO: The oat file assistant should be the definitive place for
653 // determining the oat file name from the dex location, not
654 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700655 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800656}
657
Richard Uhler66d874d2015-01-15 09:37:19 -0800658std::string OatFileAssistant::ImageLocation() {
659 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000660 const std::vector<gc::space::ImageSpace*>& image_spaces =
661 runtime->GetHeap()->GetBootImageSpaces();
662 if (image_spaces.empty()) {
663 return "";
664 }
665 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800666}
667
668const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700669 if (!required_dex_checksum_attempted_) {
670 required_dex_checksum_attempted_ = true;
671 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800672 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700673 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700674 required_dex_checksum_found_ = true;
675 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800676 } else {
677 // This can happen if the original dex file has been stripped from the
678 // apk.
679 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700680 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800681
682 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700683 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800684 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700685 const OatFile::OatDexFile* odex_dex_file
686 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800687 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700688 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
689 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800690 }
691 }
692 }
693 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700694 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800695}
696
Richard Uhler66d874d2015-01-15 09:37:19 -0800697const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
698 if (!image_info_load_attempted_) {
699 image_info_load_attempted_ = true;
700
701 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800702 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
703 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800704 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800705
706 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800707 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800708 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800709 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
710 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800711 cached_image_info_.patch_delta = image_header.GetPatchDelta();
712 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700713 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800714 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700715 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
716 isa_,
717 &error_msg));
718 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800719 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800720 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
721 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800722 cached_image_info_.patch_delta = image_header->GetPatchDelta();
723 }
724 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800725 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700726
Jeff Haofd336c32016-04-07 19:46:31 -0700727 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800728 }
729 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
730}
731
Jeff Haob11ffb72016-04-07 15:40:54 -0700732// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700733uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700734 uint32_t checksum = 0;
735 std::vector<gc::space::ImageSpace*> image_spaces =
736 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700737 if (isa == kRuntimeISA) {
738 for (gc::space::ImageSpace* image_space : image_spaces) {
739 checksum ^= image_space->GetImageHeader().GetOatChecksum();
740 }
741 } else {
742 for (gc::space::ImageSpace* image_space : image_spaces) {
743 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700744 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700745 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700746 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
747 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700748 checksum ^= image_header->GetOatChecksum();
749 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700750 }
751 return checksum;
752}
753
754uint32_t OatFileAssistant::GetCombinedImageChecksum() {
755 if (!image_info_load_attempted_) {
756 GetImageInfo();
757 }
758 return combined_image_checksum_;
759}
760
Andreas Gampea463b6a2016-08-12 21:53:32 -0700761std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800762 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100763 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800764 if (art_file.empty()) {
765 return nullptr;
766 }
767 std::string error_msg;
768 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700769 std::unique_ptr<gc::space::ImageSpace> ret =
770 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800771 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800772 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
773 }
774 return ret;
775}
776
Richard Uhler743bf362016-04-19 15:39:37 -0700777OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant)
778 : oat_file_assistant_(oat_file_assistant)
779{}
780
781const std::string* OatFileAssistant::OatFileInfo::Filename() {
782 return filename_provided_ ? &filename_ : nullptr;
783}
784
785bool OatFileAssistant::OatFileInfo::Exists() {
786 return GetFile() != nullptr;
787}
788
789OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
790 if (!status_attempted_) {
791 status_attempted_ = true;
792 const OatFile* file = GetFile();
793 if (file == nullptr) {
794 status_ = kOatOutOfDate;
795 } else {
796 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700797 VLOG(oat) << file->GetLocation() << " is " << status_
798 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700799 }
800 }
801 return status_;
802}
803
Richard Uhler743bf362016-04-19 15:39:37 -0700804CompilerFilter::Filter OatFileAssistant::OatFileInfo::CompilerFilter() {
805 const OatFile* file = GetFile();
806 CHECK(file != nullptr);
807 return file->GetCompilerFilter();
808}
809
Richard Uhler70a84262016-11-08 16:51:51 +0000810OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
811 CompilerFilter::Filter target, bool profile_changed) {
812 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
813
Richard Uhler3e580bc2016-11-08 16:23:07 +0000814 if (CompilerFilterIsOkay(target, profile_changed)) {
815 if (Status() == kOatUpToDate) {
816 // The oat file is in good shape as is.
817 return kNoDexOptNeeded;
818 }
819
820 if (Status() == kOatNeedsRelocation) {
821 if (!compilation_desired) {
822 // If no compilation is desired, then it doesn't matter if the oat
823 // file needs relocation. It's in good shape as is.
Richard Uhler70a84262016-11-08 16:51:51 +0000824 return kNoDexOptNeeded;
825 }
Richard Uhler3e580bc2016-11-08 16:23:07 +0000826
827 if (compilation_desired && HasPatchInfo()) {
828 // Relocate if we can.
829 return kPatchOatNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000830 }
831 }
832 }
833
Richard Uhler70a84262016-11-08 16:51:51 +0000834 // We can only run dex2oat if there are original dex files.
835 return oat_file_assistant_->HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
836}
837
Richard Uhler743bf362016-04-19 15:39:37 -0700838const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
839 CHECK(!file_released_) << "GetFile called after oat file released.";
840 if (!load_attempted_) {
841 load_attempted_ = true;
842 if (filename_provided_) {
843 std::string error_msg;
844 file_.reset(OatFile::Open(filename_.c_str(),
845 filename_.c_str(),
846 nullptr,
847 nullptr,
848 oat_file_assistant_->load_executable_,
849 /*low_4gb*/false,
850 oat_file_assistant_->dex_location_.c_str(),
851 &error_msg));
852 if (file_.get() == nullptr) {
853 VLOG(oat) << "OatFileAssistant test for existing oat file "
854 << filename_ << ": " << error_msg;
855 }
856 }
857 }
858 return file_.get();
859}
860
861bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
862 CompilerFilter::Filter target, bool profile_changed) {
863 const OatFile* file = GetFile();
864 if (file == nullptr) {
865 return false;
866 }
867
868 CompilerFilter::Filter current = file->GetCompilerFilter();
869 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
870 VLOG(oat) << "Compiler filter not okay because Profile changed";
871 return false;
872 }
873 return CompilerFilter::IsAsGoodAs(current, target);
874}
875
876bool OatFileAssistant::OatFileInfo::IsExecutable() {
877 const OatFile* file = GetFile();
878 return (file != nullptr && file->IsExecutable());
879}
880
881bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
882 const OatFile* file = GetFile();
883 return (file != nullptr && file->HasPatchInfo());
884}
885
886void OatFileAssistant::OatFileInfo::Reset() {
887 load_attempted_ = false;
888 file_.reset();
889 status_attempted_ = false;
890}
891
892void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
893 filename_provided_ = true;
894 filename_ = filename;
895 Reset();
896}
897
898std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
899 file_released_ = true;
900 return std::move(file_);
901}
902
Richard Uhler70a84262016-11-08 16:51:51 +0000903std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000904 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000905 return ReleaseFile();
906 }
907
908 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
909 << " attempting to fall back to interpreting oat file instead.";
910
Richard Uhler3e580bc2016-11-08 16:23:07 +0000911 if (Status() == kOatNeedsRelocation && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000912 return ReleaseFile();
913 }
914
Richard Uhler3e580bc2016-11-08 16:23:07 +0000915 if (Status() == kOatNeedsRelocation) {
Richard Uhler70a84262016-11-08 16:51:51 +0000916 // We are loading an oat file for runtime use that needs relocation.
917 // Reload the file non-executable to ensure that we interpret out of the
918 // dex code in the oat file rather than trying to execute the unrelocated
919 // compiled code.
920 oat_file_assistant_->load_executable_ = false;
921 Reset();
Richard Uhler3e580bc2016-11-08 16:23:07 +0000922 if (Status() != kOatOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000923 CHECK(!IsExecutable());
924 return ReleaseFile();
925 }
926 }
927 return std::unique_ptr<OatFile>();
928}
Richard Uhler66d874d2015-01-15 09:37:19 -0800929} // namespace art
930