blob: 8700a90276ffde98ccea9f21d7c39ed7f1ef92a5 [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
19#include <fcntl.h>
20#ifdef __linux__
21#include <sys/sendfile.h>
22#else
23#include <sys/socket.h>
24#endif
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include <set>
30
31#include "base/logging.h"
32#include "base/stringprintf.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010033#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "class_linker.h"
35#include "gc/heap.h"
36#include "gc/space/image_space.h"
37#include "image.h"
38#include "oat.h"
39#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040#include "runtime.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080041#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080042#include "utils.h"
43
44namespace art {
45
Narayan Kamath8943c1d2016-05-02 13:14:48 +010046std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
47 switch (status) {
48 case OatFileAssistant::kOatOutOfDate:
49 stream << "kOatOutOfDate";
50 break;
51 case OatFileAssistant::kOatUpToDate:
52 stream << "kOatUpToDate";
53 break;
54 case OatFileAssistant::kOatNeedsRelocation:
55 stream << "kOatNeedsRelocation";
56 break;
57 default:
58 UNREACHABLE();
59 }
60
61 return stream;
62}
63
Richard Uhler66d874d2015-01-15 09:37:19 -080064OatFileAssistant::OatFileAssistant(const char* dex_location,
65 const InstructionSet isa,
66 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070067 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000068{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080069
70OatFileAssistant::OatFileAssistant(const char* dex_location,
71 const char* oat_location,
72 const InstructionSet isa,
73 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070074 : isa_(isa), load_executable_(load_executable) {
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 Uhlerd684f522016-04-19 13:24:41 -070084 std::string error_msg;
85 if (!DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name_, &error_msg)) {
86 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
87 }
88
Richard Uhler66d874d2015-01-15 09:37:19 -080089 if (oat_location != nullptr) {
Richard Uhlerd684f522016-04-19 13:24:41 -070090 oat_file_name_ = std::string(oat_location);
91 } else {
92 if (!DexLocationToOatFilename(dex_location_, isa_, &oat_file_name_, &error_msg)) {
93 LOG(WARNING) << "Failed to determine oat file name for dex location "
94 << dex_location_ << ": " << error_msg;
95 }
Richard Uhler66d874d2015-01-15 09:37:19 -080096 }
Richard Uhler66d874d2015-01-15 09:37:19 -080097}
98
99OatFileAssistant::~OatFileAssistant() {
100 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700101 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100102 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800103 }
104}
105
106bool OatFileAssistant::IsInBootClassPath() {
107 // Note: We check the current boot class path, regardless of the ISA
108 // specified by the user. This is okay, because the boot class path should
109 // be the same for all ISAs.
110 // TODO: Can we verify the boot class path is the same for all ISAs?
111 Runtime* runtime = Runtime::Current();
112 ClassLinker* class_linker = runtime->GetClassLinker();
113 const auto& boot_class_path = class_linker->GetBootClassPath();
114 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700115 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800116 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
117 return true;
118 }
119 }
120 return false;
121}
122
123bool OatFileAssistant::Lock(std::string* error_msg) {
124 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700125 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800126
127 if (OatFileName() == nullptr) {
128 *error_msg = "Failed to determine lock file";
129 return false;
130 }
131 std::string lock_file_name = *OatFileName() + ".flock";
132
Richard Uhler581f4e92015-05-07 10:19:35 -0700133 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100134 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800135 return false;
136 }
137 return true;
138}
139
Richard Uhlerd1472a22016-04-15 15:18:56 -0700140static bool GivenOatFileCompilerFilterIsOkay(const OatFile& oat_file,
141 CompilerFilter::Filter target,
142 bool profile_changed) {
143 CompilerFilter::Filter current = oat_file.GetCompilerFilter();
144
145 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
146 VLOG(oat) << "Compiler filter not okay because Profile changed";
147 return false;
148 }
149 return CompilerFilter::IsAsGoodAs(current, target);
150}
151
152bool OatFileAssistant::OatFileCompilerFilterIsOkay(CompilerFilter::Filter target,
153 bool profile_changed) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000154 const OatFile* oat_file = GetOatFile();
155 if (oat_file != nullptr) {
Richard Uhlerd1472a22016-04-15 15:18:56 -0700156 return GivenOatFileCompilerFilterIsOkay(*oat_file, target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000157 }
158 return false;
Calin Juravleb077e152016-02-18 18:47:37 +0000159}
Richard Uhler66d874d2015-01-15 09:37:19 -0800160
Richard Uhlerd1472a22016-04-15 15:18:56 -0700161bool OatFileAssistant::OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target,
162 bool profile_changed) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000163 const OatFile* odex_file = GetOdexFile();
164 if (odex_file != nullptr) {
Richard Uhlerd1472a22016-04-15 15:18:56 -0700165 return GivenOatFileCompilerFilterIsOkay(*odex_file, target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000166 }
167 return false;
168}
169
Richard Uhlerd1472a22016-04-15 15:18:56 -0700170OatFileAssistant::DexOptNeeded
171OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
172 bool profile_changed) {
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100173 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000174
175 // See if the oat file is in good shape as is.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700176 bool oat_okay = OatFileCompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000177 if (oat_okay) {
178 if (compilation_desired) {
179 if (OatFileIsUpToDate()) {
180 return kNoDexOptNeeded;
181 }
182 } else {
183 if (!OatFileIsOutOfDate()) {
184 return kNoDexOptNeeded;
185 }
186 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800187 }
Richard Uhler95abd042015-03-24 09:51:28 -0700188
Andreas Gampe29d38e72016-03-23 15:31:51 +0000189 // See if the odex file is in good shape as is.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700190 bool odex_okay = OdexFileCompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000191 if (odex_okay) {
192 if (compilation_desired) {
193 if (OdexFileIsUpToDate()) {
194 return kNoDexOptNeeded;
195 }
196 } else {
197 if (!OdexFileIsOutOfDate()) {
198 return kNoDexOptNeeded;
199 }
200 }
Richard Uhler95abd042015-03-24 09:51:28 -0700201 }
202
Andreas Gampe29d38e72016-03-23 15:31:51 +0000203 // See if we can get an up-to-date file by running patchoat.
204 if (compilation_desired) {
Richard Uhlerd1537b52016-03-29 13:27:41 -0700205 if (odex_okay && OdexFileNeedsRelocation() && OdexFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000206 return kPatchOatNeeded;
207 }
208
Richard Uhlerd1537b52016-03-29 13:27:41 -0700209 if (oat_okay && OatFileNeedsRelocation() && OatFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000210 return kSelfPatchOatNeeded;
211 }
Richard Uhler95abd042015-03-24 09:51:28 -0700212 }
213
Andreas Gampe29d38e72016-03-23 15:31:51 +0000214 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700215 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800216}
217
Richard Uhlerf4b34872016-04-13 11:03:46 -0700218// Figure out the currently specified compile filter option in the runtime.
219// Returns true on success, false if the compiler filter is invalid, in which
220// case error_msg describes the problem.
221static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
222 std::string* error_msg) {
223 CHECK(filter != nullptr);
224 CHECK(error_msg != nullptr);
225
226 *filter = CompilerFilter::kDefaultCompilerFilter;
227 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
228 if (option.starts_with("--compiler-filter=")) {
229 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
230 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
231 *error_msg = std::string("Unknown --compiler-filter value: ")
232 + std::string(compiler_filter_string);
233 return false;
234 }
235 }
236 }
237 return true;
238}
239
Richard Uhler01be6812016-05-17 10:34:52 -0700240bool OatFileAssistant::IsUpToDate() {
241 return OatFileIsUpToDate() || OdexFileIsUpToDate();
242}
243
Richard Uhler1e860612016-03-30 12:17:55 -0700244OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700245OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700246 CompilerFilter::Filter target;
247 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
248 return kUpdateNotAttempted;
249 }
250
Richard Uhlerd1472a22016-04-15 15:18:56 -0700251 switch (GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700252 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700253 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700254 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
255 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800256 }
257 UNREACHABLE();
258}
259
260std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700261 // The best oat files are, in descending order of bestness:
262 // 1. Properly relocated files. These may be opened executable.
263 // 2. Not out-of-date files that are already opened non-executable.
264 // 3. Not out-of-date files that we must reopen non-executable.
265
Richard Uhler66d874d2015-01-15 09:37:19 -0800266 if (OatFileIsUpToDate()) {
267 oat_file_released_ = true;
268 return std::move(cached_oat_file_);
269 }
270
271 if (OdexFileIsUpToDate()) {
272 oat_file_released_ = true;
273 return std::move(cached_odex_file_);
274 }
275
Richard Uhler5f946da2015-07-17 12:28:32 -0700276 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
277 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800278
Richard Uhler5f946da2015-07-17 12:28:32 -0700279 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
280 oat_file_released_ = true;
281 return std::move(cached_oat_file_);
282 }
283
284 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
285 oat_file_released_ = true;
286 return std::move(cached_odex_file_);
287 }
288
289 if (!OatFileIsOutOfDate()) {
290 load_executable_ = false;
291 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800292 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700293 CHECK(!OatFileIsExecutable());
294 oat_file_released_ = true;
295 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700297 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800298
Richard Uhler5f946da2015-07-17 12:28:32 -0700299 if (!OdexFileIsOutOfDate()) {
300 load_executable_ = false;
301 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800302 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700303 CHECK(!OdexFileIsExecutable());
304 oat_file_released_ = true;
305 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800306 }
307 }
308
309 return std::unique_ptr<OatFile>();
310}
311
312std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
313 const OatFile& oat_file, const char* dex_location) {
314 std::vector<std::unique_ptr<const DexFile>> dex_files;
315
316 // Load the primary dex file.
317 std::string error_msg;
318 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
319 dex_location, nullptr, false);
320 if (oat_dex_file == nullptr) {
321 LOG(WARNING) << "Attempt to load out-of-date oat file "
322 << oat_file.GetLocation() << " for dex location " << dex_location;
323 return std::vector<std::unique_ptr<const DexFile>>();
324 }
325
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700326 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800327 if (dex_file.get() == nullptr) {
328 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
329 return std::vector<std::unique_ptr<const DexFile>>();
330 }
331 dex_files.push_back(std::move(dex_file));
332
333 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700334 for (size_t i = 1; ; i++) {
335 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800336 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700337 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800338 // There are no more secondary dex files to load.
339 break;
340 }
341
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700342 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800343 if (dex_file.get() == nullptr) {
344 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
345 return std::vector<std::unique_ptr<const DexFile>>();
346 }
347 dex_files.push_back(std::move(dex_file));
348 }
349 return dex_files;
350}
351
Richard Uhler9b994ea2015-06-24 08:44:19 -0700352bool OatFileAssistant::HasOriginalDexFiles() {
353 // Ensure GetRequiredDexChecksum has been run so that
354 // has_original_dex_files_ is initialized. We don't care about the result of
355 // GetRequiredDexChecksum.
356 GetRequiredDexChecksum();
357 return has_original_dex_files_;
358}
359
Richard Uhler66d874d2015-01-15 09:37:19 -0800360const std::string* OatFileAssistant::OdexFileName() {
Richard Uhlerd684f522016-04-19 13:24:41 -0700361 return odex_file_name_.empty() ? nullptr : &odex_file_name_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800362}
363
364bool OatFileAssistant::OdexFileExists() {
365 return GetOdexFile() != nullptr;
366}
367
Richard Uhler95abd042015-03-24 09:51:28 -0700368OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700369 if (!odex_file_status_attempted_) {
370 odex_file_status_attempted_ = true;
371 const OatFile* odex_file = GetOdexFile();
372 if (odex_file == nullptr) {
373 cached_odex_file_status_ = kOatOutOfDate;
374 } else {
375 cached_odex_file_status_ = GivenOatFileStatus(*odex_file);
376 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700378 return cached_odex_file_status_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800379}
380
381bool OatFileAssistant::OdexFileIsOutOfDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700382 return OdexFileStatus() == kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800383}
384
385bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700386 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800387}
388
389bool OatFileAssistant::OdexFileIsUpToDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700390 return OdexFileStatus() == kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800391}
392
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100393CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
394 const OatFile* odex_file = GetOdexFile();
395 CHECK(odex_file != nullptr);
396
397 return odex_file->GetCompilerFilter();
398}
Richard Uhler4c7f1932016-04-15 15:51:21 -0700399
400static std::string ArtFileName(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800401 const std::string oat_file_location = oat_file->GetLocation();
402 // Replace extension with .art
403 const size_t last_ext = oat_file_location.find_last_of('.');
404 if (last_ext == std::string::npos) {
405 LOG(ERROR) << "No extension in oat file " << oat_file_location;
406 return std::string();
407 }
408 return oat_file_location.substr(0, last_ext) + ".art";
409}
410
Richard Uhler66d874d2015-01-15 09:37:19 -0800411const std::string* OatFileAssistant::OatFileName() {
Richard Uhlerd684f522016-04-19 13:24:41 -0700412 return oat_file_name_.empty() ? nullptr : &oat_file_name_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800413}
414
415bool OatFileAssistant::OatFileExists() {
416 return GetOatFile() != nullptr;
417}
418
Richard Uhler95abd042015-03-24 09:51:28 -0700419OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700420 if (!oat_file_status_attempted_) {
421 oat_file_status_attempted_ = true;
422 const OatFile* oat_file = GetOatFile();
423 if (oat_file == nullptr) {
424 cached_oat_file_status_ = kOatOutOfDate;
425 } else {
426 cached_oat_file_status_ = GivenOatFileStatus(*oat_file);
427 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800428 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700429 return cached_oat_file_status_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800430}
431
432bool OatFileAssistant::OatFileIsOutOfDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700433 return OatFileStatus() == kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800434}
435
436bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700437 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800438}
439
440bool OatFileAssistant::OatFileIsUpToDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700441 return OatFileStatus() == kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800442}
443
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100444CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
445 const OatFile* oat_file = GetOatFile();
446 CHECK(oat_file != nullptr);
447
448 return oat_file->GetCompilerFilter();
449}
450
Richard Uhler95abd042015-03-24 09:51:28 -0700451OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800452 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700453 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800454 // what we provide, which verifies the primary dex checksum for us.
455 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
456 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700457 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700458 if (oat_dex_file == nullptr) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700459 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800460 }
461
462 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700463 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800464 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700465 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800466 const OatFile::OatDexFile* secondary_oat_dex_file
467 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700468 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800469 // There are no more secondary dex files to check.
470 break;
471 }
472
473 std::string error_msg;
474 uint32_t expected_secondary_checksum = 0;
475 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700476 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800477 uint32_t actual_secondary_checksum
478 = secondary_oat_dex_file->GetDexFileLocationChecksum();
479 if (expected_secondary_checksum != actual_secondary_checksum) {
480 VLOG(oat) << "Dex checksum does not match for secondary dex: "
481 << secondary_dex_location
482 << ". Expected: " << expected_secondary_checksum
483 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700484 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800485 }
486 } else {
487 // If we can't get the checksum for the secondary location, we assume
488 // the dex checksum is up to date for this and all other secondary dex
489 // files.
490 break;
491 }
492 }
493
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
495 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000496
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000498 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
499 const ImageInfo* image_info = GetImageInfo();
500 if (image_info == nullptr) {
501 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000502
Richard Uhler76f5cb62016-04-04 13:30:16 -0700503 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700504 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700505 }
506
507 // If there is no original dex file to fall back to, grudgingly accept
508 // the oat file. This could technically lead to crashes, but there's no
509 // way we could find a better oat file to use for this dex location,
510 // and it's better than being stuck in a boot loop with no way out.
511 // The problem will hopefully resolve itself the next time the runtime
512 // starts up.
513 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
514 << "Allow oat file use. This is potentially dangerous.";
515 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
516 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000517 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700518 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000519 }
520 } else {
521 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800522 }
523
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100524 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000525 if (!file.IsPic()) {
526 const ImageInfo* image_info = GetImageInfo();
527 if (image_info == nullptr) {
528 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700529 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000530 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700531
Andreas Gampe29d38e72016-03-23 15:31:51 +0000532 // Verify the oat_data_begin recorded for the image in the oat file matches
533 // the actual oat_data_begin for boot.oat in the image.
534 const OatHeader& oat_header = file.GetOatHeader();
535 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
536 if (oat_data_begin != image_info->oat_data_begin) {
537 VLOG(oat) << file.GetLocation() <<
538 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
539 << " does not match actual image oat_data_begin ("
540 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700541 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000542 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700543
Andreas Gampe29d38e72016-03-23 15:31:51 +0000544 // Verify the oat_patch_delta recorded for the image in the oat file matches
545 // the actual oat_patch_delta for the image.
546 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
547 if (oat_patch_delta != image_info->patch_delta) {
548 VLOG(oat) << file.GetLocation() <<
549 ": Oat file image patch delta (" << oat_patch_delta << ")"
550 << " does not match actual image patch delta ("
551 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700552 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000553 }
554 } else {
555 // Oat files compiled in PIC mode do not require relocation.
556 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
557 }
558 } else {
559 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800560 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700561 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800562}
563
Richard Uhler1e860612016-03-30 12:17:55 -0700564OatFileAssistant::ResultOfAttemptToUpdate
565OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800566 CHECK(error_msg != nullptr);
567
Richard Uhler95abd042015-03-24 09:51:28 -0700568 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700569 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700570 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700571 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800572 }
Richard Uhler95abd042015-03-24 09:51:28 -0700573 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800574
575 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700576 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800577 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700578 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800579 }
580 const std::string& oat_file_name = *OatFileName();
581
582 const ImageInfo* image_info = GetImageInfo();
583 Runtime* runtime = Runtime::Current();
584 if (image_info == nullptr) {
585 *error_msg = "Patching of oat file " + oat_file_name
586 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700587 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800588 }
589
590 if (!runtime->IsDex2OatEnabled()) {
591 *error_msg = "Patching of oat file " + oat_file_name
592 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700593 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800594 }
595
596 std::vector<std::string> argv;
597 argv.push_back(runtime->GetPatchoatExecutable());
598 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700599 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800600 argv.push_back("--output-oat-file=" + oat_file_name);
601 argv.push_back("--patched-image-location=" + image_info->location);
602
603 std::string command_line(Join(argv, ' '));
604 if (!Exec(argv, error_msg)) {
605 // Manually delete the file. This ensures there is no garbage left over if
606 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100607 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700608 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800609 }
610
611 // Mark that the oat file has changed and we should try to reload.
612 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700613 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800614}
615
Richard Uhler1e860612016-03-30 12:17:55 -0700616OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700617OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800618 CHECK(error_msg != nullptr);
619
Richard Uhler8327cf72015-10-13 16:34:59 -0700620 Runtime* runtime = Runtime::Current();
621 if (!runtime->IsDex2OatEnabled()) {
622 *error_msg = "Generation of oat file for dex location " + dex_location_
623 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700624 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700625 }
626
Richard Uhler66d874d2015-01-15 09:37:19 -0800627 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700628 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800629 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700630 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800631 }
632 const std::string& oat_file_name = *OatFileName();
633
Richard Uhler66d874d2015-01-15 09:37:19 -0800634 // dex2oat ignores missing dex files and doesn't report an error.
635 // Check explicitly here so we can detect the error properly.
636 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700637 if (!OS::FileExists(dex_location_.c_str())) {
638 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700639 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800640 }
641
Richard Uhler8327cf72015-10-13 16:34:59 -0700642 std::unique_ptr<File> oat_file;
643 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
644 if (oat_file.get() == nullptr) {
645 *error_msg = "Generation of oat file " + oat_file_name
646 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700647 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700648 }
649
650 if (fchmod(oat_file->Fd(), 0644) != 0) {
651 *error_msg = "Generation of oat file " + oat_file_name
652 + " not attempted because the oat file could not be made world readable.";
653 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700654 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700655 }
656
657 std::vector<std::string> args;
658 args.push_back("--dex-file=" + dex_location_);
659 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
660 args.push_back("--oat-location=" + oat_file_name);
661
Richard Uhler66d874d2015-01-15 09:37:19 -0800662 if (!Dex2Oat(args, error_msg)) {
663 // Manually delete the file. This ensures there is no garbage left over if
664 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700665 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100666 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700667 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700668 }
669
670 if (oat_file->FlushCloseOrErase() != 0) {
671 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100672 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700673 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800674 }
675
676 // Mark that the oat file has changed and we should try to reload.
677 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700678 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800679}
680
681bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
682 std::string* error_msg) {
683 Runtime* runtime = Runtime::Current();
684 std::string image_location = ImageLocation();
685 if (image_location.empty()) {
686 *error_msg = "No image location found for Dex2Oat.";
687 return false;
688 }
689
690 std::vector<std::string> argv;
691 argv.push_back(runtime->GetCompilerExecutable());
692 argv.push_back("--runtime-arg");
693 argv.push_back("-classpath");
694 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700695 std::string class_path = runtime->GetClassPathString();
696 if (class_path == "") {
697 class_path = OatFile::kSpecialSharedLibrary;
698 }
699 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100700 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200701 argv.push_back("--debuggable");
702 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700703 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800704
705 if (!runtime->IsVerificationEnabled()) {
706 argv.push_back("--compiler-filter=verify-none");
707 }
708
709 if (runtime->MustRelocateIfPossible()) {
710 argv.push_back("--runtime-arg");
711 argv.push_back("-Xrelocate");
712 } else {
713 argv.push_back("--runtime-arg");
714 argv.push_back("-Xnorelocate");
715 }
716
717 if (!kIsTargetBuild) {
718 argv.push_back("--host");
719 }
720
721 argv.push_back("--boot-image=" + image_location);
722
723 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
724 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
725
726 argv.insert(argv.end(), args.begin(), args.end());
727
728 std::string command_line(Join(argv, ' '));
729 return Exec(argv, error_msg);
730}
731
Richard Uhlerb81881d2016-04-19 13:08:04 -0700732bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
733 InstructionSet isa,
734 std::string* odex_filename,
735 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800736 CHECK(odex_filename != nullptr);
737 CHECK(error_msg != nullptr);
738
739 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700740 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800741 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700742 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800743
Richard Uhler63434112015-03-16 14:32:16 -0700744 // Find the directory portion of the dex location and add the oat/<isa>
745 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800746 size_t pos = location.rfind('/');
747 if (pos == std::string::npos) {
748 *error_msg = "Dex location " + location + " has no directory.";
749 return false;
750 }
751 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700752 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800753
754 // Find the file portion of the dex location.
755 std::string file;
756 if (pos == std::string::npos) {
757 file = location;
758 } else {
759 file = location.substr(pos+1);
760 }
761
762 // Get the base part of the file without the extension.
763 pos = file.rfind('.');
764 if (pos == std::string::npos) {
765 *error_msg = "Dex location " + location + " has no extension.";
766 return false;
767 }
768 std::string base = file.substr(0, pos);
769
770 *odex_filename = dir + "/" + base + ".odex";
771 return true;
772}
773
Richard Uhlerb81881d2016-04-19 13:08:04 -0700774bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
775 InstructionSet isa,
776 std::string* oat_filename,
777 std::string* error_msg) {
778 CHECK(oat_filename != nullptr);
779 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800780
781 // TODO: The work done in GetDalvikCache is overkill for what we need.
782 // Ideally a new API for getting the DalvikCacheDirectory the way we want
783 // (without existence testing, creation, or death) is provided with the rest
784 // of the GetDalvikCache family of functions. Until such an API is in place,
785 // we use GetDalvikCache to avoid duplicating the logic for determining the
786 // dalvik cache directory.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700787 std::string dalvik_cache_dir;
788 bool ignored;
789 GetDalvikCache("", false, &dalvik_cache_dir, &ignored, &ignored, &ignored);
790
791 // TODO: The oat file assistant should be the definitive place for
792 // determining the oat file name from the dex location, not
793 // GetDalvikCacheFilename.
794 std::string cache_dir = StringPrintf("%s%s",
795 dalvik_cache_dir.c_str(), GetInstructionSetString(isa));
796 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800797}
798
Richard Uhler66d874d2015-01-15 09:37:19 -0800799std::string OatFileAssistant::ImageLocation() {
800 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000801 const std::vector<gc::space::ImageSpace*>& image_spaces =
802 runtime->GetHeap()->GetBootImageSpaces();
803 if (image_spaces.empty()) {
804 return "";
805 }
806 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800807}
808
809const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700810 if (!required_dex_checksum_attempted_) {
811 required_dex_checksum_attempted_ = true;
812 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800813 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700814 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700815 required_dex_checksum_found_ = true;
816 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800817 } else {
818 // This can happen if the original dex file has been stripped from the
819 // apk.
820 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700821 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800822
823 // Get the checksum from the odex if we can.
824 const OatFile* odex_file = GetOdexFile();
825 if (odex_file != nullptr) {
826 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700827 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800828 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700829 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
830 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800831 }
832 }
833 }
834 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700835 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800836}
837
838const OatFile* OatFileAssistant::GetOdexFile() {
839 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
840 if (!odex_file_load_attempted_) {
841 odex_file_load_attempted_ = true;
842 if (OdexFileName() != nullptr) {
843 const std::string& odex_file_name = *OdexFileName();
844 std::string error_msg;
845 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800846 odex_file_name.c_str(),
847 nullptr,
848 nullptr,
849 load_executable_,
850 /*low_4gb*/false,
851 dex_location_.c_str(),
852 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800853 if (cached_odex_file_.get() == nullptr) {
854 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
855 << odex_file_name << ": " << error_msg;
856 }
857 }
858 }
859 return cached_odex_file_.get();
860}
861
Richard Uhler5f946da2015-07-17 12:28:32 -0700862bool OatFileAssistant::OdexFileIsExecutable() {
863 const OatFile* odex_file = GetOdexFile();
864 return (odex_file != nullptr && odex_file->IsExecutable());
865}
866
Richard Uhlerd1537b52016-03-29 13:27:41 -0700867bool OatFileAssistant::OdexFileHasPatchInfo() {
868 const OatFile* odex_file = GetOdexFile();
869 return (odex_file != nullptr && odex_file->HasPatchInfo());
870}
871
Richard Uhler66d874d2015-01-15 09:37:19 -0800872void OatFileAssistant::ClearOdexFileCache() {
873 odex_file_load_attempted_ = false;
874 cached_odex_file_.reset();
Richard Uhlere8109f72016-04-18 10:40:50 -0700875 odex_file_status_attempted_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800876}
877
878const OatFile* OatFileAssistant::GetOatFile() {
879 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
880 if (!oat_file_load_attempted_) {
881 oat_file_load_attempted_ = true;
882 if (OatFileName() != nullptr) {
883 const std::string& oat_file_name = *OatFileName();
884 std::string error_msg;
885 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800886 oat_file_name.c_str(),
887 nullptr,
888 nullptr,
889 load_executable_,
890 /*low_4gb*/false,
891 dex_location_.c_str(),
892 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800893 if (cached_oat_file_.get() == nullptr) {
894 VLOG(oat) << "OatFileAssistant test for existing oat file "
895 << oat_file_name << ": " << error_msg;
896 }
897 }
898 }
899 return cached_oat_file_.get();
900}
901
Richard Uhler5f946da2015-07-17 12:28:32 -0700902bool OatFileAssistant::OatFileIsExecutable() {
903 const OatFile* oat_file = GetOatFile();
904 return (oat_file != nullptr && oat_file->IsExecutable());
905}
906
Richard Uhlerd1537b52016-03-29 13:27:41 -0700907bool OatFileAssistant::OatFileHasPatchInfo() {
908 const OatFile* oat_file = GetOatFile();
909 return (oat_file != nullptr && oat_file->HasPatchInfo());
910}
911
Richard Uhler66d874d2015-01-15 09:37:19 -0800912void OatFileAssistant::ClearOatFileCache() {
913 oat_file_load_attempted_ = false;
914 cached_oat_file_.reset();
Richard Uhlere8109f72016-04-18 10:40:50 -0700915 oat_file_status_attempted_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800916}
917
918const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
919 if (!image_info_load_attempted_) {
920 image_info_load_attempted_ = true;
921
922 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800923 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
924 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800925 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800926
927 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800928 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800929 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800930 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
931 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800932 cached_image_info_.patch_delta = image_header.GetPatchDelta();
933 } else {
934 std::unique_ptr<ImageHeader> image_header(
Jeff Haob11ffb72016-04-07 15:40:54 -0700935 gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_));
Richard Uhler66d874d2015-01-15 09:37:19 -0800936 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800937 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
938 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800939 cached_image_info_.patch_delta = image_header->GetPatchDelta();
940 }
941 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800942 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700943
Jeff Haofd336c32016-04-07 19:46:31 -0700944 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800945 }
946 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
947}
948
Jeff Haob11ffb72016-04-07 15:40:54 -0700949// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700950uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700951 uint32_t checksum = 0;
952 std::vector<gc::space::ImageSpace*> image_spaces =
953 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700954 if (isa == kRuntimeISA) {
955 for (gc::space::ImageSpace* image_space : image_spaces) {
956 checksum ^= image_space->GetImageHeader().GetOatChecksum();
957 }
958 } else {
959 for (gc::space::ImageSpace* image_space : image_spaces) {
960 std::string location = image_space->GetImageLocation();
961 std::unique_ptr<ImageHeader> image_header(
962 gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa));
963 checksum ^= image_header->GetOatChecksum();
964 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700965 }
966 return checksum;
967}
968
969uint32_t OatFileAssistant::GetCombinedImageChecksum() {
970 if (!image_info_load_attempted_) {
971 GetImageInfo();
972 }
973 return combined_image_checksum_;
974}
975
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800976gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
977 DCHECK(oat_file != nullptr);
978 std::string art_file = ArtFileName(oat_file);
979 if (art_file.empty()) {
980 return nullptr;
981 }
982 std::string error_msg;
983 ScopedObjectAccess soa(Thread::Current());
984 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
985 oat_file,
986 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800987 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800988 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
989 }
990 return ret;
991}
992
Richard Uhler66d874d2015-01-15 09:37:19 -0800993} // namespace art
994