blob: 6197120d6337dfa9a78326168717e968142394d4 [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) {
Richard Uhler03bc6592016-11-22 09:42:04 +000039 case OatFileAssistant::kOatCannotOpen:
40 stream << "kOatCannotOpen";
41 break;
42 case OatFileAssistant::kOatDexOutOfDate:
43 stream << "kOatDexOutOfDate";
44 break;
45 case OatFileAssistant::kOatBootImageOutOfDate:
46 stream << "kOatBootImageOutOfDate";
47 break;
48 case OatFileAssistant::kOatRelocationOutOfDate:
49 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010050 break;
51 case OatFileAssistant::kOatUpToDate:
52 stream << "kOatUpToDate";
53 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010054 default:
55 UNREACHABLE();
56 }
57
58 return stream;
59}
60
Richard Uhler66d874d2015-01-15 09:37:19 -080061OatFileAssistant::OatFileAssistant(const char* dex_location,
62 const InstructionSet isa,
63 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070064 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000065{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080066
67OatFileAssistant::OatFileAssistant(const char* dex_location,
68 const char* oat_location,
69 const InstructionSet isa,
70 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000071 : isa_(isa),
72 load_executable_(load_executable),
73 odex_(this, /*is_oat_location*/ false),
74 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070075 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
76 dex_location_.assign(dex_location);
77
Richard Uhler66d874d2015-01-15 09:37:19 -080078 if (load_executable_ && isa != kRuntimeISA) {
79 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
80 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
81 load_executable_ = false;
82 }
83
Richard Uhler743bf362016-04-19 15:39:37 -070084 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070085 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070086 std::string odex_file_name;
87 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
88 odex_.Reset(odex_file_name);
89 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070090 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
91 }
92
Richard Uhler743bf362016-04-19 15:39:37 -070093 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -080094 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -070095 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -070096 } else {
Richard Uhler743bf362016-04-19 15:39:37 -070097 std::string oat_file_name;
98 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
99 oat_.Reset(oat_file_name);
100 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700101 LOG(WARNING) << "Failed to determine oat file name for dex location "
102 << dex_location_ << ": " << error_msg;
103 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800104 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800105}
106
107OatFileAssistant::~OatFileAssistant() {
108 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700109 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100110 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800111 }
112}
113
114bool OatFileAssistant::IsInBootClassPath() {
115 // Note: We check the current boot class path, regardless of the ISA
116 // specified by the user. This is okay, because the boot class path should
117 // be the same for all ISAs.
118 // TODO: Can we verify the boot class path is the same for all ISAs?
119 Runtime* runtime = Runtime::Current();
120 ClassLinker* class_linker = runtime->GetClassLinker();
121 const auto& boot_class_path = class_linker->GetBootClassPath();
122 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700123 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800124 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
125 return true;
126 }
127 }
128 return false;
129}
130
131bool OatFileAssistant::Lock(std::string* error_msg) {
132 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700133 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800134
Richard Uhler743bf362016-04-19 15:39:37 -0700135 const std::string* oat_file_name = oat_.Filename();
136 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800137 *error_msg = "Failed to determine lock file";
138 return false;
139 }
Richard Uhler743bf362016-04-19 15:39:37 -0700140 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800141
Richard Uhler581f4e92015-05-07 10:19:35 -0700142 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100143 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800144 return false;
145 }
146 return true;
147}
148
Richard Uhlerd1472a22016-04-15 15:18:56 -0700149OatFileAssistant::DexOptNeeded
Richard Uhler743bf362016-04-19 15:39:37 -0700150OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000151 OatFileInfo& info = GetBestInfo();
152 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
153 if (dexopt_needed == kPatchOatNeeded && info.IsOatLocation()) {
154 dexopt_needed = kSelfPatchOatNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800155 }
Richard Uhler88bc6732016-11-14 14:38:03 +0000156 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800157}
158
Richard Uhlerf4b34872016-04-13 11:03:46 -0700159// Figure out the currently specified compile filter option in the runtime.
160// Returns true on success, false if the compiler filter is invalid, in which
161// case error_msg describes the problem.
162static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
163 std::string* error_msg) {
164 CHECK(filter != nullptr);
165 CHECK(error_msg != nullptr);
166
167 *filter = CompilerFilter::kDefaultCompilerFilter;
168 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
169 if (option.starts_with("--compiler-filter=")) {
170 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
171 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
172 *error_msg = std::string("Unknown --compiler-filter value: ")
173 + std::string(compiler_filter_string);
174 return false;
175 }
176 }
177 }
178 return true;
179}
180
Richard Uhler01be6812016-05-17 10:34:52 -0700181bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000182 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700183}
184
Richard Uhler1e860612016-03-30 12:17:55 -0700185OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700186OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700187 CompilerFilter::Filter target;
188 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
189 return kUpdateNotAttempted;
190 }
191
Richard Uhler88bc6732016-11-14 14:38:03 +0000192 OatFileInfo& info = GetBestInfo();
193 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700194 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700195 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler88bc6732016-11-14 14:38:03 +0000196 case kPatchOatNeeded: return RelocateOatFile(info.Filename(), error_msg);
197
198 // kSelfPatchOatNeeded will never be returned by GetDexOptNeeded for an
199 // individual OatFileInfo.
200 case kSelfPatchOatNeeded: UNREACHABLE();
Richard Uhler66d874d2015-01-15 09:37:19 -0800201 }
202 UNREACHABLE();
203}
204
205std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000206 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800207}
208
Richard Uhler46cc64f2016-11-14 14:53:55 +0000209std::string OatFileAssistant::GetStatusDump() {
210 std::ostringstream status;
211 bool oat_file_exists = false;
212 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000213 if (oat_.Status() != kOatCannotOpen) {
214 // If we can open the file, neither Filename nor GetFile should return null.
215 CHECK(oat_.Filename() != nullptr);
216 CHECK(oat_.GetFile() != nullptr);
217
Richard Uhler46cc64f2016-11-14 14:53:55 +0000218 oat_file_exists = true;
219 status << *oat_.Filename() << " [compilation_filter=";
Richard Uhler03bc6592016-11-22 09:42:04 +0000220 status << CompilerFilter::NameOfFilter(oat_.GetFile()->GetCompilerFilter());
Richard Uhler46cc64f2016-11-14 14:53:55 +0000221 status << ", status=" << oat_.Status();
222 }
223
Richard Uhler03bc6592016-11-22 09:42:04 +0000224 if (odex_.Status() != kOatCannotOpen) {
225 // If we can open the file, neither Filename nor GetFile should return null.
226 CHECK(odex_.Filename() != nullptr);
227 CHECK(odex_.GetFile() != nullptr);
228
Richard Uhler46cc64f2016-11-14 14:53:55 +0000229 odex_file_exists = true;
230 if (oat_file_exists) {
231 status << "] ";
232 }
233 status << *odex_.Filename() << " [compilation_filter=";
Richard Uhler03bc6592016-11-22 09:42:04 +0000234 status << CompilerFilter::NameOfFilter(odex_.GetFile()->GetCompilerFilter());
Richard Uhler46cc64f2016-11-14 14:53:55 +0000235 status << ", status=" << odex_.Status();
236 }
237
238 if (!oat_file_exists && !odex_file_exists) {
239 status << "invalid[";
240 }
241
242 status << "]";
243 return status.str();
244}
245
Richard Uhler66d874d2015-01-15 09:37:19 -0800246std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
247 const OatFile& oat_file, const char* dex_location) {
248 std::vector<std::unique_ptr<const DexFile>> dex_files;
249
250 // Load the primary dex file.
251 std::string error_msg;
252 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700253 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800254 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700255 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800256 return std::vector<std::unique_ptr<const DexFile>>();
257 }
258
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700259 std::unique_ptr<const DexFile> 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 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700267 for (size_t i = 1; ; i++) {
268 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700269 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700270 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 // There are no more secondary dex files to load.
272 break;
273 }
274
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700275 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 if (dex_file.get() == nullptr) {
277 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
278 return std::vector<std::unique_ptr<const DexFile>>();
279 }
280 dex_files.push_back(std::move(dex_file));
281 }
282 return dex_files;
283}
284
Richard Uhler9b994ea2015-06-24 08:44:19 -0700285bool OatFileAssistant::HasOriginalDexFiles() {
286 // Ensure GetRequiredDexChecksum has been run so that
287 // has_original_dex_files_ is initialized. We don't care about the result of
288 // GetRequiredDexChecksum.
289 GetRequiredDexChecksum();
290 return has_original_dex_files_;
291}
292
Richard Uhler95abd042015-03-24 09:51:28 -0700293OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700294 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800295}
296
Richard Uhler95abd042015-03-24 09:51:28 -0700297OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700298 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800299}
300
Richard Uhler95abd042015-03-24 09:51:28 -0700301OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800302 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700303 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800304 // what we provide, which verifies the primary dex checksum for us.
Richard Uhler9a37efc2016-08-05 16:32:55 -0700305 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800306 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
307 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700308 dex_location_.c_str(), dex_checksum_pointer, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700309 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700310 VLOG(oat) << error_msg;
Richard Uhler03bc6592016-11-22 09:42:04 +0000311 return kOatDexOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800312 }
313
314 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700315 for (size_t i = 1; ; i++) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700316 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800317 const OatFile::OatDexFile* secondary_oat_dex_file
Richard Uhler9a37efc2016-08-05 16:32:55 -0700318 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700319 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800320 // There are no more secondary dex files to check.
321 break;
322 }
323
Richard Uhler66d874d2015-01-15 09:37:19 -0800324 uint32_t expected_secondary_checksum = 0;
325 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700326 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800327 uint32_t actual_secondary_checksum
328 = secondary_oat_dex_file->GetDexFileLocationChecksum();
329 if (expected_secondary_checksum != actual_secondary_checksum) {
330 VLOG(oat) << "Dex checksum does not match for secondary dex: "
331 << secondary_dex_location
332 << ". Expected: " << expected_secondary_checksum
333 << ", Actual: " << actual_secondary_checksum;
Richard Uhler03bc6592016-11-22 09:42:04 +0000334 return kOatDexOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800335 }
336 } else {
337 // If we can't get the checksum for the secondary location, we assume
338 // the dex checksum is up to date for this and all other secondary dex
339 // files.
340 break;
341 }
342 }
343
Andreas Gampe29d38e72016-03-23 15:31:51 +0000344 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000345
Richard Uhler66d874d2015-01-15 09:37:19 -0800346 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000347 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
348 const ImageInfo* image_info = GetImageInfo();
349 if (image_info == nullptr) {
350 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000351
Richard Uhler76f5cb62016-04-04 13:30:16 -0700352 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000353 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700354 }
355
356 // If there is no original dex file to fall back to, grudgingly accept
357 // the oat file. This could technically lead to crashes, but there's no
358 // way we could find a better oat file to use for this dex location,
359 // and it's better than being stuck in a boot loop with no way out.
360 // The problem will hopefully resolve itself the next time the runtime
361 // starts up.
362 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
363 << "Allow oat file use. This is potentially dangerous.";
364 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
365 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000366 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000367 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000368 }
369 } else {
370 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800371 }
372
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100373 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000374 if (!file.IsPic()) {
375 const ImageInfo* image_info = GetImageInfo();
376 if (image_info == nullptr) {
377 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000378 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000379 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700380
Andreas Gampe29d38e72016-03-23 15:31:51 +0000381 // Verify the oat_data_begin recorded for the image in the oat file matches
382 // the actual oat_data_begin for boot.oat in the image.
383 const OatHeader& oat_header = file.GetOatHeader();
384 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
385 if (oat_data_begin != image_info->oat_data_begin) {
386 VLOG(oat) << file.GetLocation() <<
387 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
388 << " does not match actual image oat_data_begin ("
389 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000390 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000391 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700392
Andreas Gampe29d38e72016-03-23 15:31:51 +0000393 // Verify the oat_patch_delta recorded for the image in the oat file matches
394 // the actual oat_patch_delta for the image.
395 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
396 if (oat_patch_delta != image_info->patch_delta) {
397 VLOG(oat) << file.GetLocation() <<
398 ": Oat file image patch delta (" << oat_patch_delta << ")"
399 << " does not match actual image patch delta ("
400 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000401 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000402 }
403 } else {
404 // Oat files compiled in PIC mode do not require relocation.
405 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
406 }
407 } else {
408 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800409 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700410 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800411}
412
Richard Uhler1e860612016-03-30 12:17:55 -0700413OatFileAssistant::ResultOfAttemptToUpdate
414OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800415 CHECK(error_msg != nullptr);
416
Richard Uhler95abd042015-03-24 09:51:28 -0700417 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700418 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700419 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700420 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800421 }
Richard Uhler95abd042015-03-24 09:51:28 -0700422 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800423
Richard Uhler743bf362016-04-19 15:39:37 -0700424 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700425 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800426 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700427 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800428 }
Richard Uhler743bf362016-04-19 15:39:37 -0700429 const std::string& oat_file_name = *oat_.Filename();
Richard Uhler66d874d2015-01-15 09:37:19 -0800430
431 const ImageInfo* image_info = GetImageInfo();
432 Runtime* runtime = Runtime::Current();
433 if (image_info == nullptr) {
434 *error_msg = "Patching of oat file " + oat_file_name
435 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700436 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800437 }
438
439 if (!runtime->IsDex2OatEnabled()) {
440 *error_msg = "Patching of oat file " + oat_file_name
441 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700442 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800443 }
444
445 std::vector<std::string> argv;
446 argv.push_back(runtime->GetPatchoatExecutable());
447 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700448 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 argv.push_back("--output-oat-file=" + oat_file_name);
450 argv.push_back("--patched-image-location=" + image_info->location);
451
452 std::string command_line(Join(argv, ' '));
453 if (!Exec(argv, error_msg)) {
454 // Manually delete the file. This ensures there is no garbage left over if
455 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100456 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700457 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800458 }
459
460 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700461 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700462 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800463}
464
Richard Uhler1e860612016-03-30 12:17:55 -0700465OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700466OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800467 CHECK(error_msg != nullptr);
468
Richard Uhler8327cf72015-10-13 16:34:59 -0700469 Runtime* runtime = Runtime::Current();
470 if (!runtime->IsDex2OatEnabled()) {
471 *error_msg = "Generation of oat file for dex location " + dex_location_
472 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700473 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700474 }
475
Richard Uhler743bf362016-04-19 15:39:37 -0700476 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700477 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800478 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700479 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800480 }
Richard Uhler743bf362016-04-19 15:39:37 -0700481 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100482 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800483
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 // dex2oat ignores missing dex files and doesn't report an error.
485 // Check explicitly here so we can detect the error properly.
486 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700487 if (!OS::FileExists(dex_location_.c_str())) {
488 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700489 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800490 }
491
David Brazdil7b49e6c2016-09-01 11:06:18 +0100492 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
493 if (vdex_file.get() == nullptr) {
494 *error_msg = "Generation of oat file " + oat_file_name
495 + " not attempted because the vdex file " + vdex_file_name
496 + " could not be opened.";
497 return kUpdateNotAttempted;
498 }
499
500 if (fchmod(vdex_file->Fd(), 0644) != 0) {
501 *error_msg = "Generation of oat file " + oat_file_name
502 + " not attempted because the vdex file " + vdex_file_name
503 + " could not be made world readable.";
504 return kUpdateNotAttempted;
505 }
506
507 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700508 if (oat_file.get() == nullptr) {
509 *error_msg = "Generation of oat file " + oat_file_name
510 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700511 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700512 }
513
514 if (fchmod(oat_file->Fd(), 0644) != 0) {
515 *error_msg = "Generation of oat file " + oat_file_name
516 + " not attempted because the oat file could not be made world readable.";
517 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700518 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700519 }
520
521 std::vector<std::string> args;
522 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000523 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700524 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
525 args.push_back("--oat-location=" + oat_file_name);
526
Richard Uhler66d874d2015-01-15 09:37:19 -0800527 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100528 // Manually delete the oat and vdex files. This ensures there is no garbage
529 // left over if the process unexpectedly died.
530 vdex_file->Erase();
531 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700532 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100533 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700534 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700535 }
536
David Brazdil7b49e6c2016-09-01 11:06:18 +0100537 if (vdex_file->FlushCloseOrErase() != 0) {
538 *error_msg = "Unable to close vdex file " + vdex_file_name;
539 unlink(vdex_file_name.c_str());
540 return kUpdateFailed;
541 }
542
Richard Uhler8327cf72015-10-13 16:34:59 -0700543 if (oat_file->FlushCloseOrErase() != 0) {
544 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100545 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700546 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800547 }
548
549 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700550 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700551 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800552}
553
554bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
555 std::string* error_msg) {
556 Runtime* runtime = Runtime::Current();
557 std::string image_location = ImageLocation();
558 if (image_location.empty()) {
559 *error_msg = "No image location found for Dex2Oat.";
560 return false;
561 }
562
563 std::vector<std::string> argv;
564 argv.push_back(runtime->GetCompilerExecutable());
565 argv.push_back("--runtime-arg");
566 argv.push_back("-classpath");
567 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700568 std::string class_path = runtime->GetClassPathString();
569 if (class_path == "") {
570 class_path = OatFile::kSpecialSharedLibrary;
571 }
572 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100573 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200574 argv.push_back("--debuggable");
575 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700576 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800577
578 if (!runtime->IsVerificationEnabled()) {
579 argv.push_back("--compiler-filter=verify-none");
580 }
581
582 if (runtime->MustRelocateIfPossible()) {
583 argv.push_back("--runtime-arg");
584 argv.push_back("-Xrelocate");
585 } else {
586 argv.push_back("--runtime-arg");
587 argv.push_back("-Xnorelocate");
588 }
589
590 if (!kIsTargetBuild) {
591 argv.push_back("--host");
592 }
593
594 argv.push_back("--boot-image=" + image_location);
595
596 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
597 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
598
599 argv.insert(argv.end(), args.begin(), args.end());
600
601 std::string command_line(Join(argv, ' '));
602 return Exec(argv, error_msg);
603}
604
Richard Uhlerb81881d2016-04-19 13:08:04 -0700605bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
606 InstructionSet isa,
607 std::string* odex_filename,
608 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800609 CHECK(odex_filename != nullptr);
610 CHECK(error_msg != nullptr);
611
612 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700613 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800614 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700615 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800616
Richard Uhler63434112015-03-16 14:32:16 -0700617 // Find the directory portion of the dex location and add the oat/<isa>
618 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800619 size_t pos = location.rfind('/');
620 if (pos == std::string::npos) {
621 *error_msg = "Dex location " + location + " has no directory.";
622 return false;
623 }
624 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700625 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800626
627 // Find the file portion of the dex location.
628 std::string file;
629 if (pos == std::string::npos) {
630 file = location;
631 } else {
632 file = location.substr(pos+1);
633 }
634
635 // Get the base part of the file without the extension.
636 pos = file.rfind('.');
637 if (pos == std::string::npos) {
638 *error_msg = "Dex location " + location + " has no extension.";
639 return false;
640 }
641 std::string base = file.substr(0, pos);
642
643 *odex_filename = dir + "/" + base + ".odex";
644 return true;
645}
646
Richard Uhlerb81881d2016-04-19 13:08:04 -0700647bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
648 InstructionSet isa,
649 std::string* oat_filename,
650 std::string* error_msg) {
651 CHECK(oat_filename != nullptr);
652 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800653
Richard Uhler55b58b62016-08-12 09:05:13 -0700654 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
655 if (cache_dir.empty()) {
656 *error_msg = "Dalvik cache directory does not exist";
657 return false;
658 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700659
660 // TODO: The oat file assistant should be the definitive place for
661 // determining the oat file name from the dex location, not
662 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700663 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800664}
665
Richard Uhler66d874d2015-01-15 09:37:19 -0800666std::string OatFileAssistant::ImageLocation() {
667 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000668 const std::vector<gc::space::ImageSpace*>& image_spaces =
669 runtime->GetHeap()->GetBootImageSpaces();
670 if (image_spaces.empty()) {
671 return "";
672 }
673 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800674}
675
676const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700677 if (!required_dex_checksum_attempted_) {
678 required_dex_checksum_attempted_ = true;
679 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800680 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700681 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700682 required_dex_checksum_found_ = true;
683 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800684 } else {
685 // This can happen if the original dex file has been stripped from the
686 // apk.
687 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700688 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800689
690 // Get the checksum from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700691 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800692 if (odex_file != nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700693 const OatFile::OatDexFile* odex_dex_file
694 = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800695 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700696 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
697 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800698 }
699 }
700 }
701 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700702 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800703}
704
Richard Uhler66d874d2015-01-15 09:37:19 -0800705const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
706 if (!image_info_load_attempted_) {
707 image_info_load_attempted_ = true;
708
709 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800710 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
711 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800712 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800713
714 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800715 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800716 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800717 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
718 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800719 cached_image_info_.patch_delta = image_header.GetPatchDelta();
720 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700721 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800722 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700723 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
724 isa_,
725 &error_msg));
726 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800727 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800728 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
729 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800730 cached_image_info_.patch_delta = image_header->GetPatchDelta();
731 }
732 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800733 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700734
Jeff Haofd336c32016-04-07 19:46:31 -0700735 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800736 }
737 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
738}
739
Jeff Haob11ffb72016-04-07 15:40:54 -0700740// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700741uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700742 uint32_t checksum = 0;
743 std::vector<gc::space::ImageSpace*> image_spaces =
744 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700745 if (isa == kRuntimeISA) {
746 for (gc::space::ImageSpace* image_space : image_spaces) {
747 checksum ^= image_space->GetImageHeader().GetOatChecksum();
748 }
749 } else {
750 for (gc::space::ImageSpace* image_space : image_spaces) {
751 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700752 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700753 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700754 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
755 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700756 checksum ^= image_header->GetOatChecksum();
757 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700758 }
759 return checksum;
760}
761
762uint32_t OatFileAssistant::GetCombinedImageChecksum() {
763 if (!image_info_load_attempted_) {
764 GetImageInfo();
765 }
766 return combined_image_checksum_;
767}
768
Richard Uhler88bc6732016-11-14 14:38:03 +0000769OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000770 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
771 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000772}
773
Andreas Gampea463b6a2016-08-12 21:53:32 -0700774std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800775 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100776 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800777 if (art_file.empty()) {
778 return nullptr;
779 }
780 std::string error_msg;
781 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700782 std::unique_ptr<gc::space::ImageSpace> ret =
783 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800784 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800785 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
786 }
787 return ret;
788}
789
Richard Uhler88bc6732016-11-14 14:38:03 +0000790OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
791 bool is_oat_location)
792 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700793{}
794
Richard Uhler88bc6732016-11-14 14:38:03 +0000795bool OatFileAssistant::OatFileInfo::IsOatLocation() {
796 return is_oat_location_;
797}
798
Richard Uhler743bf362016-04-19 15:39:37 -0700799const std::string* OatFileAssistant::OatFileInfo::Filename() {
800 return filename_provided_ ? &filename_ : nullptr;
801}
802
Richard Uhler03bc6592016-11-22 09:42:04 +0000803bool OatFileAssistant::OatFileInfo::IsUseable() {
804 switch (Status()) {
805 case kOatCannotOpen:
806 case kOatDexOutOfDate:
807 case kOatBootImageOutOfDate: return false;
808
809 case kOatRelocationOutOfDate:
810 case kOatUpToDate: return true;
811 }
812 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700813}
814
815OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
816 if (!status_attempted_) {
817 status_attempted_ = true;
818 const OatFile* file = GetFile();
819 if (file == nullptr) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000820 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700821 } else {
822 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700823 VLOG(oat) << file->GetLocation() << " is " << status_
824 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700825 }
826 }
827 return status_;
828}
829
Richard Uhler70a84262016-11-08 16:51:51 +0000830OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
831 CompilerFilter::Filter target, bool profile_changed) {
832 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
833
Richard Uhler3e580bc2016-11-08 16:23:07 +0000834 if (CompilerFilterIsOkay(target, profile_changed)) {
835 if (Status() == kOatUpToDate) {
836 // The oat file is in good shape as is.
837 return kNoDexOptNeeded;
838 }
839
Richard Uhler03bc6592016-11-22 09:42:04 +0000840 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000841 if (!compilation_desired) {
842 // If no compilation is desired, then it doesn't matter if the oat
843 // file needs relocation. It's in good shape as is.
Richard Uhler70a84262016-11-08 16:51:51 +0000844 return kNoDexOptNeeded;
845 }
Richard Uhler3e580bc2016-11-08 16:23:07 +0000846
847 if (compilation_desired && HasPatchInfo()) {
848 // Relocate if we can.
849 return kPatchOatNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000850 }
851 }
852 }
853
Richard Uhler70a84262016-11-08 16:51:51 +0000854 // We can only run dex2oat if there are original dex files.
855 return oat_file_assistant_->HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
856}
857
Richard Uhler743bf362016-04-19 15:39:37 -0700858const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
859 CHECK(!file_released_) << "GetFile called after oat file released.";
860 if (!load_attempted_) {
861 load_attempted_ = true;
862 if (filename_provided_) {
863 std::string error_msg;
864 file_.reset(OatFile::Open(filename_.c_str(),
865 filename_.c_str(),
866 nullptr,
867 nullptr,
868 oat_file_assistant_->load_executable_,
869 /*low_4gb*/false,
870 oat_file_assistant_->dex_location_.c_str(),
871 &error_msg));
872 if (file_.get() == nullptr) {
873 VLOG(oat) << "OatFileAssistant test for existing oat file "
874 << filename_ << ": " << error_msg;
875 }
876 }
877 }
878 return file_.get();
879}
880
881bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
882 CompilerFilter::Filter target, bool profile_changed) {
883 const OatFile* file = GetFile();
884 if (file == nullptr) {
885 return false;
886 }
887
888 CompilerFilter::Filter current = file->GetCompilerFilter();
889 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
890 VLOG(oat) << "Compiler filter not okay because Profile changed";
891 return false;
892 }
893 return CompilerFilter::IsAsGoodAs(current, target);
894}
895
896bool OatFileAssistant::OatFileInfo::IsExecutable() {
897 const OatFile* file = GetFile();
898 return (file != nullptr && file->IsExecutable());
899}
900
901bool OatFileAssistant::OatFileInfo::HasPatchInfo() {
902 const OatFile* file = GetFile();
903 return (file != nullptr && file->HasPatchInfo());
904}
905
906void OatFileAssistant::OatFileInfo::Reset() {
907 load_attempted_ = false;
908 file_.reset();
909 status_attempted_ = false;
910}
911
912void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
913 filename_provided_ = true;
914 filename_ = filename;
915 Reset();
916}
917
918std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
919 file_released_ = true;
920 return std::move(file_);
921}
922
Richard Uhler70a84262016-11-08 16:51:51 +0000923std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000924 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000925 return ReleaseFile();
926 }
927
928 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
929 << " attempting to fall back to interpreting oat file instead.";
930
Richard Uhler03bc6592016-11-22 09:42:04 +0000931 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000932 return ReleaseFile();
933 }
934
Richard Uhler03bc6592016-11-22 09:42:04 +0000935 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000936 // We are loading an oat file for runtime use that needs relocation.
937 // Reload the file non-executable to ensure that we interpret out of the
938 // dex code in the oat file rather than trying to execute the unrelocated
939 // compiled code.
940 oat_file_assistant_->load_executable_ = false;
941 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +0000942 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000943 CHECK(!IsExecutable());
944 return ReleaseFile();
945 }
946 }
947 return std::unique_ptr<OatFile>();
948}
Richard Uhler66d874d2015-01-15 09:37:19 -0800949} // namespace art
950