blob: c4ad98d5fa0aa6c2204f93c703c5ceb81c782bf8 [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 Uhler88bc6732016-11-14 14:38:03 +000065 : isa_(isa),
66 load_executable_(load_executable),
67 odex_(this, /*is_oat_location*/ false),
68 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070069 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
70 dex_location_.assign(dex_location);
71
Richard Uhler66d874d2015-01-15 09:37:19 -080072 if (load_executable_ && isa != kRuntimeISA) {
73 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
74 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
75 load_executable_ = false;
76 }
77
Richard Uhler743bf362016-04-19 15:39:37 -070078 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070079 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070080 std::string odex_file_name;
81 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
82 odex_.Reset(odex_file_name);
83 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070084 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
85 }
86
Richard Uhler743bf362016-04-19 15:39:37 -070087 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080088 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070089 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -070090 } else {
Richard Uhler743bf362016-04-19 15:39:37 -070091 std::string oat_file_name;
92 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
93 oat_.Reset(oat_file_name);
94 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070095 LOG(WARNING) << "Failed to determine oat file name for dex location "
96 << dex_location_ << ": " << error_msg;
97 }
Richard Uhler66d874d2015-01-15 09:37:19 -080098 }
Richard Uhler66d874d2015-01-15 09:37:19 -080099}
100
101OatFileAssistant::~OatFileAssistant() {
102 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700103 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100104 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800105 }
106}
107
108bool OatFileAssistant::IsInBootClassPath() {
109 // Note: We check the current boot class path, regardless of the ISA
110 // specified by the user. This is okay, because the boot class path should
111 // be the same for all ISAs.
112 // TODO: Can we verify the boot class path is the same for all ISAs?
113 Runtime* runtime = Runtime::Current();
114 ClassLinker* class_linker = runtime->GetClassLinker();
115 const auto& boot_class_path = class_linker->GetBootClassPath();
116 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700117 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800118 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
119 return true;
120 }
121 }
122 return false;
123}
124
125bool OatFileAssistant::Lock(std::string* error_msg) {
126 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700127 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800128
Richard Uhler743bf362016-04-19 15:39:37 -0700129 const std::string* oat_file_name = oat_.Filename();
130 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800131 *error_msg = "Failed to determine lock file";
132 return false;
133 }
Richard Uhler743bf362016-04-19 15:39:37 -0700134 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800135
Richard Uhler581f4e92015-05-07 10:19:35 -0700136 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100137 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800138 return false;
139 }
140 return true;
141}
142
Richard Uhlerd1472a22016-04-15 15:18:56 -0700143OatFileAssistant::DexOptNeeded
Richard Uhler743bf362016-04-19 15:39:37 -0700144OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000145 OatFileInfo& info = GetBestInfo();
146 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
147 if (dexopt_needed == kPatchOatNeeded && info.IsOatLocation()) {
148 dexopt_needed = kSelfPatchOatNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800149 }
Richard Uhler88bc6732016-11-14 14:38:03 +0000150 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800151}
152
Richard Uhlerf4b34872016-04-13 11:03:46 -0700153// Figure out the currently specified compile filter option in the runtime.
154// Returns true on success, false if the compiler filter is invalid, in which
155// case error_msg describes the problem.
156static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
157 std::string* error_msg) {
158 CHECK(filter != nullptr);
159 CHECK(error_msg != nullptr);
160
161 *filter = CompilerFilter::kDefaultCompilerFilter;
162 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
163 if (option.starts_with("--compiler-filter=")) {
164 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
165 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
166 *error_msg = std::string("Unknown --compiler-filter value: ")
167 + std::string(compiler_filter_string);
168 return false;
169 }
170 }
171 }
172 return true;
173}
174
Richard Uhler01be6812016-05-17 10:34:52 -0700175bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000176 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700177}
178
Richard Uhler1e860612016-03-30 12:17:55 -0700179OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700180OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700181 CompilerFilter::Filter target;
182 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
183 return kUpdateNotAttempted;
184 }
185
Richard Uhler88bc6732016-11-14 14:38:03 +0000186 OatFileInfo& info = GetBestInfo();
187 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700188 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700189 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler88bc6732016-11-14 14:38:03 +0000190 case kPatchOatNeeded: return RelocateOatFile(info.Filename(), error_msg);
191
192 // kSelfPatchOatNeeded will never be returned by GetDexOptNeeded for an
193 // individual OatFileInfo.
194 case kSelfPatchOatNeeded: UNREACHABLE();
Richard Uhler66d874d2015-01-15 09:37:19 -0800195 }
196 UNREACHABLE();
197}
198
199std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000200 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800201}
202
Richard Uhler46cc64f2016-11-14 14:53:55 +0000203std::string OatFileAssistant::GetStatusDump() {
204 std::ostringstream status;
205 bool oat_file_exists = false;
206 bool odex_file_exists = false;
207 if (oat_.Exists()) {
208 oat_file_exists = true;
209 status << *oat_.Filename() << " [compilation_filter=";
210 status << CompilerFilter::NameOfFilter(oat_.CompilerFilter());
211 status << ", status=" << oat_.Status();
212 }
213
214 if (odex_.Exists()) {
215 odex_file_exists = true;
216 if (oat_file_exists) {
217 status << "] ";
218 }
219 status << *odex_.Filename() << " [compilation_filter=";
220 status << CompilerFilter::NameOfFilter(odex_.CompilerFilter());
221 status << ", status=" << odex_.Status();
222 }
223
224 if (!oat_file_exists && !odex_file_exists) {
225 status << "invalid[";
226 }
227
228 status << "]";
229 return status.str();
230}
231
Richard Uhler66d874d2015-01-15 09:37:19 -0800232std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
233 const OatFile& oat_file, const char* dex_location) {
234 std::vector<std::unique_ptr<const DexFile>> dex_files;
235
236 // Load the primary dex file.
237 std::string error_msg;
238 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700239 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800240 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700241 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800242 return std::vector<std::unique_ptr<const DexFile>>();
243 }
244
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700245 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800246 if (dex_file.get() == nullptr) {
247 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
248 return std::vector<std::unique_ptr<const DexFile>>();
249 }
250 dex_files.push_back(std::move(dex_file));
251
252 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700253 for (size_t i = 1; ; i++) {
254 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700255 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700256 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800257 // There are no more secondary dex files to load.
258 break;
259 }
260
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700261 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800262 if (dex_file.get() == nullptr) {
263 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
264 return std::vector<std::unique_ptr<const DexFile>>();
265 }
266 dex_files.push_back(std::move(dex_file));
267 }
268 return dex_files;
269}
270
Richard Uhler9b994ea2015-06-24 08:44:19 -0700271bool OatFileAssistant::HasOriginalDexFiles() {
272 // Ensure GetRequiredDexChecksum has been run so that
273 // has_original_dex_files_ is initialized. We don't care about the result of
274 // GetRequiredDexChecksum.
275 GetRequiredDexChecksum();
276 return has_original_dex_files_;
277}
278
Richard Uhler66d874d2015-01-15 09:37:19 -0800279bool OatFileAssistant::OdexFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700280 return odex_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800281}
282
Richard Uhler95abd042015-03-24 09:51:28 -0700283OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700284 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800285}
286
Richard Uhler66d874d2015-01-15 09:37:19 -0800287bool OatFileAssistant::OatFileExists() {
Richard Uhler743bf362016-04-19 15:39:37 -0700288 return oat_.Exists();
Richard Uhler66d874d2015-01-15 09:37:19 -0800289}
290
Richard Uhler95abd042015-03-24 09:51:28 -0700291OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700292 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800293}
294
Richard Uhler95abd042015-03-24 09:51:28 -0700295OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700297 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800298 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700299 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800300 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
301 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700302 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700303 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700304 VLOG(oat) << error_msg;
Richard Uhlere8109f72016-04-18 10:40:50 -0700305 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800306 }
307
308 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700309 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700310 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800311 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700312 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700313 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800314 // There are no more secondary dex files to check.
315 break;
316 }
317
Richard Uhler66d874d2015-01-15 09:37:19 -0800318 uint32_t expected_secondary_checksum = 0;
319 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700320 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800321 uint32_t actual_secondary_checksum
322 = secondary_oat_dex_file->GetDexFileLocationChecksum();
323 if (expected_secondary_checksum != actual_secondary_checksum) {
324 VLOG(oat) << "Dex checksum does not match for secondary dex: "
325 << secondary_dex_location
326 << ". Expected: " << expected_secondary_checksum
327 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700328 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800329 }
330 } else {
331 // If we can't get the checksum for the secondary location, we assume
332 // the dex checksum is up to date for this and all other secondary dex
333 // files.
334 break;
335 }
336 }
337
Andreas Gampe29d38e72016-03-23 15:31:51 +0000338 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000339
Richard Uhler66d874d2015-01-15 09:37:19 -0800340 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000341 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
342 const ImageInfo* image_info = GetImageInfo();
343 if (image_info == nullptr) {
344 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000345
Richard Uhler76f5cb62016-04-04 13:30:16 -0700346 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700347 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700348 }
349
350 // If there is no original dex file to fall back to, grudgingly accept
351 // the oat file. This could technically lead to crashes, but there's no
352 // way we could find a better oat file to use for this dex location,
353 // and it's better than being stuck in a boot loop with no way out.
354 // The problem will hopefully resolve itself the next time the runtime
355 // starts up.
356 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
357 << "Allow oat file use. This is potentially dangerous.";
358 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
359 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000360 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700361 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000362 }
363 } else {
364 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800365 }
366
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100367 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000368 if (!file.IsPic()) {
369 const ImageInfo* image_info = GetImageInfo();
370 if (image_info == nullptr) {
371 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700372 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000373 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700374
Andreas Gampe29d38e72016-03-23 15:31:51 +0000375 // Verify the oat_data_begin recorded for the image in the oat file matches
376 // the actual oat_data_begin for boot.oat in the image.
377 const OatHeader& oat_header = file.GetOatHeader();
378 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
379 if (oat_data_begin != image_info->oat_data_begin) {
380 VLOG(oat) << file.GetLocation() <<
381 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
382 << " does not match actual image oat_data_begin ("
383 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700384 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000385 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700386
Andreas Gampe29d38e72016-03-23 15:31:51 +0000387 // Verify the oat_patch_delta recorded for the image in the oat file matches
388 // the actual oat_patch_delta for the image.
389 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
390 if (oat_patch_delta != image_info->patch_delta) {
391 VLOG(oat) << file.GetLocation() <<
392 ": Oat file image patch delta (" << oat_patch_delta << ")"
393 << " does not match actual image patch delta ("
394 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700395 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000396 }
397 } else {
398 // Oat files compiled in PIC mode do not require relocation.
399 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
400 }
401 } else {
402 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800403 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700404 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800405}
406
Richard Uhler1e860612016-03-30 12:17:55 -0700407OatFileAssistant::ResultOfAttemptToUpdate
408OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800409 CHECK(error_msg != nullptr);
410
Richard Uhler95abd042015-03-24 09:51:28 -0700411 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700412 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700413 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700414 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800415 }
Richard Uhler95abd042015-03-24 09:51:28 -0700416 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800417
Richard Uhler743bf362016-04-19 15:39:37 -0700418 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700419 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800420 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700421 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800422 }
Richard Uhler743bf362016-04-19 15:39:37 -0700423 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800424
425 const ImageInfo* image_info = GetImageInfo();
426 Runtime* runtime = Runtime::Current();
427 if (image_info == nullptr) {
428 *error_msg = "Patching of oat file " + oat_file_name
429 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700430 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800431 }
432
433 if (!runtime->IsDex2OatEnabled()) {
434 *error_msg = "Patching of oat file " + oat_file_name
435 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700436 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800437 }
438
439 std::vector<std::string> argv;
440 argv.push_back(runtime->GetPatchoatExecutable());
441 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700442 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800443 argv.push_back("--output-oat-file=" + oat_file_name);
444 argv.push_back("--patched-image-location=" + image_info->location);
445
446 std::string command_line(Join(argv, ' '));
447 if (!Exec(argv, error_msg)) {
448 // Manually delete the file. This ensures there is no garbage left over if
449 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100450 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700451 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800452 }
453
454 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700455 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700456 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800457}
458
Richard Uhler1e860612016-03-30 12:17:55 -0700459OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700460OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800461 CHECK(error_msg != nullptr);
462
Richard Uhler8327cf72015-10-13 16:34:59 -0700463 Runtime* runtime = Runtime::Current();
464 if (!runtime->IsDex2OatEnabled()) {
465 *error_msg = "Generation of oat file for dex location " + dex_location_
466 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700467 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700468 }
469
Richard Uhler743bf362016-04-19 15:39:37 -0700470 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700471 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800472 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700473 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800474 }
Richard Uhler743bf362016-04-19 15:39:37 -0700475 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100476 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800477
Richard Uhler66d874d2015-01-15 09:37:19 -0800478 // dex2oat ignores missing dex files and doesn't report an error.
479 // Check explicitly here so we can detect the error properly.
480 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700481 if (!OS::FileExists(dex_location_.c_str())) {
482 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700483 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 }
485
David Brazdil7b49e6c2016-09-01 11:06:18 +0100486 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
487 if (vdex_file.get() == nullptr) {
488 *error_msg = "Generation of oat file " + oat_file_name
489 + " not attempted because the vdex file " + vdex_file_name
490 + " could not be opened.";
491 return kUpdateNotAttempted;
492 }
493
494 if (fchmod(vdex_file->Fd(), 0644) != 0) {
495 *error_msg = "Generation of oat file " + oat_file_name
496 + " not attempted because the vdex file " + vdex_file_name
497 + " could not be made world readable.";
498 return kUpdateNotAttempted;
499 }
500
501 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700502 if (oat_file.get() == nullptr) {
503 *error_msg = "Generation of oat file " + oat_file_name
504 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700505 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700506 }
507
508 if (fchmod(oat_file->Fd(), 0644) != 0) {
509 *error_msg = "Generation of oat file " + oat_file_name
510 + " not attempted because the oat file could not be made world readable.";
511 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700512 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700513 }
514
515 std::vector<std::string> args;
516 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000517 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700518 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
519 args.push_back("--oat-location=" + oat_file_name);
520
Richard Uhler66d874d2015-01-15 09:37:19 -0800521 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100522 // Manually delete the oat and vdex files. This ensures there is no garbage
523 // left over if the process unexpectedly died.
524 vdex_file->Erase();
525 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700526 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100527 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700528 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700529 }
530
David Brazdil7b49e6c2016-09-01 11:06:18 +0100531 if (vdex_file->FlushCloseOrErase() != 0) {
532 *error_msg = "Unable to close vdex file " + vdex_file_name;
533 unlink(vdex_file_name.c_str());
534 return kUpdateFailed;
535 }
536
Richard Uhler8327cf72015-10-13 16:34:59 -0700537 if (oat_file->FlushCloseOrErase() != 0) {
538 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100539 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700540 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800541 }
542
543 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700544 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700545 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800546}
547
548bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
549 std::string* error_msg) {
550 Runtime* runtime = Runtime::Current();
551 std::string image_location = ImageLocation();
552 if (image_location.empty()) {
553 *error_msg = "No image location found for Dex2Oat.";
554 return false;
555 }
556
557 std::vector<std::string> argv;
558 argv.push_back(runtime->GetCompilerExecutable());
559 argv.push_back("--runtime-arg");
560 argv.push_back("-classpath");
561 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700562 std::string class_path = runtime->GetClassPathString();
563 if (class_path == "") {
564 class_path = OatFile::kSpecialSharedLibrary;
565 }
566 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100567 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200568 argv.push_back("--debuggable");
569 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700570 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800571
572 if (!runtime->IsVerificationEnabled()) {
573 argv.push_back("--compiler-filter=verify-none");
574 }
575
576 if (runtime->MustRelocateIfPossible()) {
577 argv.push_back("--runtime-arg");
578 argv.push_back("-Xrelocate");
579 } else {
580 argv.push_back("--runtime-arg");
581 argv.push_back("-Xnorelocate");
582 }
583
584 if (!kIsTargetBuild) {
585 argv.push_back("--host");
586 }
587
588 argv.push_back("--boot-image=" + image_location);
589
590 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
591 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
592
593 argv.insert(argv.end(), args.begin(), args.end());
594
595 std::string command_line(Join(argv, ' '));
596 return Exec(argv, error_msg);
597}
598
Richard Uhlerb81881d2016-04-19 13:08:04 -0700599bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
600 InstructionSet isa,
601 std::string* odex_filename,
602 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800603 CHECK(odex_filename != nullptr);
604 CHECK(error_msg != nullptr);
605
606 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700607 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800608 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700609 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800610
Richard Uhler63434112015-03-16 14:32:16 -0700611 // Find the directory portion of the dex location and add the oat/<isa>
612 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800613 size_t pos = location.rfind('/');
614 if (pos == std::string::npos) {
615 *error_msg = "Dex location " + location + " has no directory.";
616 return false;
617 }
618 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700619 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800620
621 // Find the file portion of the dex location.
622 std::string file;
623 if (pos == std::string::npos) {
624 file = location;
625 } else {
626 file = location.substr(pos+1);
627 }
628
629 // Get the base part of the file without the extension.
630 pos = file.rfind('.');
631 if (pos == std::string::npos) {
632 *error_msg = "Dex location " + location + " has no extension.";
633 return false;
634 }
635 std::string base = file.substr(0, pos);
636
637 *odex_filename = dir + "/" + base + ".odex";
638 return true;
639}
640
Richard Uhlerb81881d2016-04-19 13:08:04 -0700641bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
642 InstructionSet isa,
643 std::string* oat_filename,
644 std::string* error_msg) {
645 CHECK(oat_filename != nullptr);
646 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800647
Richard Uhler55b58b62016-08-12 09:05:13 -0700648 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
649 if (cache_dir.empty()) {
650 *error_msg = "Dalvik cache directory does not exist";
651 return false;
652 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700653
654 // TODO: The oat file assistant should be the definitive place for
655 // determining the oat file name from the dex location, not
656 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700657 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800658}
659
Richard Uhler66d874d2015-01-15 09:37:19 -0800660std::string OatFileAssistant::ImageLocation() {
661 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000662 const std::vector<gc::space::ImageSpace*>& image_spaces =
663 runtime->GetHeap()->GetBootImageSpaces();
664 if (image_spaces.empty()) {
665 return "";
666 }
667 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800668}
669
670const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700671 if (!required_dex_checksum_attempted_) {
672 required_dex_checksum_attempted_ = true;
673 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800674 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700675 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700676 required_dex_checksum_found_ = true;
677 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800678 } else {
679 // This can happen if the original dex file has been stripped from the
680 // apk.
681 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700682 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800683
684 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700685 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800686 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700687 const OatFile::OatDexFile* odex_dex_file
688 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800689 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700690 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
691 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800692 }
693 }
694 }
695 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700696 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800697}
698
Richard Uhler66d874d2015-01-15 09:37:19 -0800699const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
700 if (!image_info_load_attempted_) {
701 image_info_load_attempted_ = true;
702
703 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800704 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
705 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800706 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800707
708 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800709 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800710 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800711 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
712 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800713 cached_image_info_.patch_delta = image_header.GetPatchDelta();
714 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700715 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800716 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700717 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
718 isa_,
719 &error_msg));
720 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800721 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800722 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
723 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800724 cached_image_info_.patch_delta = image_header->GetPatchDelta();
725 }
726 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800727 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700728
Jeff Haofd336c32016-04-07 19:46:31 -0700729 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800730 }
731 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
732}
733
Jeff Haob11ffb72016-04-07 15:40:54 -0700734// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700735uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700736 uint32_t checksum = 0;
737 std::vector<gc::space::ImageSpace*> image_spaces =
738 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700739 if (isa == kRuntimeISA) {
740 for (gc::space::ImageSpace* image_space : image_spaces) {
741 checksum ^= image_space->GetImageHeader().GetOatChecksum();
742 }
743 } else {
744 for (gc::space::ImageSpace* image_space : image_spaces) {
745 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700746 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700747 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700748 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
749 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700750 checksum ^= image_header->GetOatChecksum();
751 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700752 }
753 return checksum;
754}
755
756uint32_t OatFileAssistant::GetCombinedImageChecksum() {
757 if (!image_info_load_attempted_) {
758 GetImageInfo();
759 }
760 return combined_image_checksum_;
761}
762
Richard Uhler88bc6732016-11-14 14:38:03 +0000763OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
764 return oat_.Status() != kOatOutOfDate ? oat_ : odex_;
765}
766
Andreas Gampea463b6a2016-08-12 21:53:32 -0700767std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800768 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100769 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800770 if (art_file.empty()) {
771 return nullptr;
772 }
773 std::string error_msg;
774 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700775 std::unique_ptr<gc::space::ImageSpace> ret =
776 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800777 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800778 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
779 }
780 return ret;
781}
782
Richard Uhler88bc6732016-11-14 14:38:03 +0000783OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
784 bool is_oat_location)
785 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700786{}
787
Richard Uhler88bc6732016-11-14 14:38:03 +0000788bool OatFileAssistant::OatFileInfo::IsOatLocation() {
789 return is_oat_location_;
790}
791
Richard Uhler743bf362016-04-19 15:39:37 -0700792const std::string* OatFileAssistant::OatFileInfo::Filename() {
793 return filename_provided_ ? &filename_ : nullptr;
794}
795
796bool OatFileAssistant::OatFileInfo::Exists() {
797 return GetFile() != nullptr;
798}
799
800OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
801 if (!status_attempted_) {
802 status_attempted_ = true;
803 const OatFile* file = GetFile();
804 if (file == nullptr) {
805 status_ = kOatOutOfDate;
806 } else {
807 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700808 VLOG(oat) << file->GetLocation() << " is " << status_
809 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700810 }
811 }
812 return status_;
813}
814
Richard Uhler743bf362016-04-19 15:39:37 -0700815CompilerFilter::Filter OatFileAssistant::OatFileInfo::CompilerFilter() {
816 const OatFile* file = GetFile();
817 CHECK(file != nullptr);
818 return file->GetCompilerFilter();
819}
820
Richard Uhler70a84262016-11-08 16:51:51 +0000821OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
822 CompilerFilter::Filter target, bool profile_changed) {
823 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
824
Richard Uhler3e580bc2016-11-08 16:23:07 +0000825 if (CompilerFilterIsOkay(target, profile_changed)) {
826 if (Status() == kOatUpToDate) {
827 // The oat file is in good shape as is.
828 return kNoDexOptNeeded;
829 }
830
831 if (Status() == kOatNeedsRelocation) {
832 if (!compilation_desired) {
833 // If no compilation is desired, then it doesn't matter if the oat
834 // file needs relocation. It's in good shape as is.
Richard Uhler70a84262016-11-08 16:51:51 +0000835 return kNoDexOptNeeded;
836 }
Richard Uhler3e580bc2016-11-08 16:23:07 +0000837
838 if (compilation_desired && HasPatchInfo()) {
839 // Relocate if we can.
840 return kPatchOatNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000841 }
842 }
843 }
844
Richard Uhler70a84262016-11-08 16:51:51 +0000845 // We can only run dex2oat if there are original dex files.
846 return oat_file_assistant_->HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
847}
848
Richard Uhler743bf362016-04-19 15:39:37 -0700849const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
850 CHECK(!file_released_) << "GetFile called after oat file released.";
851 if (!load_attempted_) {
852 load_attempted_ = true;
853 if (filename_provided_) {
854 std::string error_msg;
855 file_.reset(OatFile::Open(filename_.c_str(),
856 filename_.c_str(),
857 nullptr,
858 nullptr,
859 oat_file_assistant_->load_executable_,
860 /*low_4gb*/false,
861 oat_file_assistant_->dex_location_.c_str(),
862 &error_msg));
863 if (file_.get() == nullptr) {
864 VLOG(oat) << "OatFileAssistant test for existing oat file "
865 << filename_ << ": " << error_msg;
866 }
867 }
868 }
869 return file_.get();
870}
871
872bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
873 CompilerFilter::Filter target, bool profile_changed) {
874 const OatFile* file = GetFile();
875 if (file == nullptr) {
876 return false;
877 }
878
879 CompilerFilter::Filter current = file->GetCompilerFilter();
880 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
881 VLOG(oat) << "Compiler filter not okay because Profile changed";
882 return false;
883 }
884 return CompilerFilter::IsAsGoodAs(current, target);
885}
886
887bool OatFileAssistant::OatFileInfo::IsExecutable() {
888 const OatFile* file = GetFile();
889 return (file != nullptr && file->IsExecutable());
890}
891
892bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
893 const OatFile* file = GetFile();
894 return (file != nullptr && file->HasPatchInfo());
895}
896
897void OatFileAssistant::OatFileInfo::Reset() {
898 load_attempted_ = false;
899 file_.reset();
900 status_attempted_ = false;
901}
902
903void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
904 filename_provided_ = true;
905 filename_ = filename;
906 Reset();
907}
908
909std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
910 file_released_ = true;
911 return std::move(file_);
912}
913
Richard Uhler70a84262016-11-08 16:51:51 +0000914std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000915 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000916 return ReleaseFile();
917 }
918
919 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
920 << " attempting to fall back to interpreting oat file instead.";
921
Richard Uhler3e580bc2016-11-08 16:23:07 +0000922 if (Status() == kOatNeedsRelocation && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000923 return ReleaseFile();
924 }
925
Richard Uhler3e580bc2016-11-08 16:23:07 +0000926 if (Status() == kOatNeedsRelocation) {
Richard Uhler70a84262016-11-08 16:51:51 +0000927 // We are loading an oat file for runtime use that needs relocation.
928 // Reload the file non-executable to ensure that we interpret out of the
929 // dex code in the oat file rather than trying to execute the unrelocated
930 // compiled code.
931 oat_file_assistant_->load_executable_ = false;
932 Reset();
Richard Uhler3e580bc2016-11-08 16:23:07 +0000933 if (Status() != kOatOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000934 CHECK(!IsExecutable());
935 return ReleaseFile();
936 }
937 }
938 return std::unique_ptr<OatFile>();
939}
Richard Uhler66d874d2015-01-15 09:37:19 -0800940} // namespace art
941