blob: 046c80e1df3116a02e749704879c591ddbab180f [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 "ScopedFd.h"
43#include "utils.h"
44
45namespace art {
46
Narayan Kamath8943c1d2016-05-02 13:14:48 +010047std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
48 switch (status) {
49 case OatFileAssistant::kOatOutOfDate:
50 stream << "kOatOutOfDate";
51 break;
52 case OatFileAssistant::kOatUpToDate:
53 stream << "kOatUpToDate";
54 break;
55 case OatFileAssistant::kOatNeedsRelocation:
56 stream << "kOatNeedsRelocation";
57 break;
58 default:
59 UNREACHABLE();
60 }
61
62 return stream;
63}
64
Richard Uhler66d874d2015-01-15 09:37:19 -080065OatFileAssistant::OatFileAssistant(const char* dex_location,
66 const InstructionSet isa,
67 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070068 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000069{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080070
71OatFileAssistant::OatFileAssistant(const char* dex_location,
72 const char* oat_location,
73 const InstructionSet isa,
74 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070075 : isa_(isa), load_executable_(load_executable) {
Richard Uhler740eec92015-10-15 15:12:23 -070076 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
77 dex_location_.assign(dex_location);
78
Richard Uhler66d874d2015-01-15 09:37:19 -080079 if (load_executable_ && isa != kRuntimeISA) {
80 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
81 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
82 load_executable_ = false;
83 }
84
85 // If the user gave a target oat location, save that as the cached oat
86 // location now so we won't try to construct the default location later.
87 if (oat_location != nullptr) {
88 cached_oat_file_name_ = std::string(oat_location);
89 cached_oat_file_name_attempted_ = true;
90 cached_oat_file_name_found_ = true;
91 }
Richard Uhler66d874d2015-01-15 09:37:19 -080092}
93
94OatFileAssistant::~OatFileAssistant() {
95 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -070096 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010097 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -080098 }
99}
100
101bool OatFileAssistant::IsInBootClassPath() {
102 // Note: We check the current boot class path, regardless of the ISA
103 // specified by the user. This is okay, because the boot class path should
104 // be the same for all ISAs.
105 // TODO: Can we verify the boot class path is the same for all ISAs?
106 Runtime* runtime = Runtime::Current();
107 ClassLinker* class_linker = runtime->GetClassLinker();
108 const auto& boot_class_path = class_linker->GetBootClassPath();
109 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700110 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800111 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
112 return true;
113 }
114 }
115 return false;
116}
117
118bool OatFileAssistant::Lock(std::string* error_msg) {
119 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700120 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800121
122 if (OatFileName() == nullptr) {
123 *error_msg = "Failed to determine lock file";
124 return false;
125 }
126 std::string lock_file_name = *OatFileName() + ".flock";
127
Richard Uhler581f4e92015-05-07 10:19:35 -0700128 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100129 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800130 return false;
131 }
132 return true;
133}
134
Richard Uhlerd1472a22016-04-15 15:18:56 -0700135static bool GivenOatFileCompilerFilterIsOkay(const OatFile& oat_file,
136 CompilerFilter::Filter target,
137 bool profile_changed) {
138 CompilerFilter::Filter current = oat_file.GetCompilerFilter();
139
140 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
141 VLOG(oat) << "Compiler filter not okay because Profile changed";
142 return false;
143 }
144 return CompilerFilter::IsAsGoodAs(current, target);
145}
146
147bool OatFileAssistant::OatFileCompilerFilterIsOkay(CompilerFilter::Filter target,
148 bool profile_changed) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000149 const OatFile* oat_file = GetOatFile();
150 if (oat_file != nullptr) {
Richard Uhlerd1472a22016-04-15 15:18:56 -0700151 return GivenOatFileCompilerFilterIsOkay(*oat_file, target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000152 }
153 return false;
Calin Juravleb077e152016-02-18 18:47:37 +0000154}
Richard Uhler66d874d2015-01-15 09:37:19 -0800155
Richard Uhlerd1472a22016-04-15 15:18:56 -0700156bool OatFileAssistant::OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target,
157 bool profile_changed) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000158 const OatFile* odex_file = GetOdexFile();
159 if (odex_file != nullptr) {
Richard Uhlerd1472a22016-04-15 15:18:56 -0700160 return GivenOatFileCompilerFilterIsOkay(*odex_file, target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000161 }
162 return false;
163}
164
Richard Uhlerd1472a22016-04-15 15:18:56 -0700165OatFileAssistant::DexOptNeeded
166OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
167 bool profile_changed) {
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100168 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000169
170 // See if the oat file is in good shape as is.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700171 bool oat_okay = OatFileCompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000172 if (oat_okay) {
173 if (compilation_desired) {
174 if (OatFileIsUpToDate()) {
175 return kNoDexOptNeeded;
176 }
177 } else {
178 if (!OatFileIsOutOfDate()) {
179 return kNoDexOptNeeded;
180 }
181 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800182 }
Richard Uhler95abd042015-03-24 09:51:28 -0700183
Andreas Gampe29d38e72016-03-23 15:31:51 +0000184 // See if the odex file is in good shape as is.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700185 bool odex_okay = OdexFileCompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000186 if (odex_okay) {
187 if (compilation_desired) {
188 if (OdexFileIsUpToDate()) {
189 return kNoDexOptNeeded;
190 }
191 } else {
192 if (!OdexFileIsOutOfDate()) {
193 return kNoDexOptNeeded;
194 }
195 }
Richard Uhler95abd042015-03-24 09:51:28 -0700196 }
197
Andreas Gampe29d38e72016-03-23 15:31:51 +0000198 // See if we can get an up-to-date file by running patchoat.
199 if (compilation_desired) {
Richard Uhlerd1537b52016-03-29 13:27:41 -0700200 if (odex_okay && OdexFileNeedsRelocation() && OdexFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000201 return kPatchOatNeeded;
202 }
203
Richard Uhlerd1537b52016-03-29 13:27:41 -0700204 if (oat_okay && OatFileNeedsRelocation() && OatFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000205 return kSelfPatchOatNeeded;
206 }
Richard Uhler95abd042015-03-24 09:51:28 -0700207 }
208
Andreas Gampe29d38e72016-03-23 15:31:51 +0000209 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700210 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800211}
212
Richard Uhlerf4b34872016-04-13 11:03:46 -0700213// Figure out the currently specified compile filter option in the runtime.
214// Returns true on success, false if the compiler filter is invalid, in which
215// case error_msg describes the problem.
216static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
217 std::string* error_msg) {
218 CHECK(filter != nullptr);
219 CHECK(error_msg != nullptr);
220
221 *filter = CompilerFilter::kDefaultCompilerFilter;
222 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
223 if (option.starts_with("--compiler-filter=")) {
224 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
225 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
226 *error_msg = std::string("Unknown --compiler-filter value: ")
227 + std::string(compiler_filter_string);
228 return false;
229 }
230 }
231 }
232 return true;
233}
234
Richard Uhler01be6812016-05-17 10:34:52 -0700235bool OatFileAssistant::IsUpToDate() {
236 return OatFileIsUpToDate() || OdexFileIsUpToDate();
237}
238
Richard Uhler1e860612016-03-30 12:17:55 -0700239OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700240OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700241 CompilerFilter::Filter target;
242 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
243 return kUpdateNotAttempted;
244 }
245
Richard Uhlerd1472a22016-04-15 15:18:56 -0700246 switch (GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700247 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700248 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700249 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
250 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800251 }
252 UNREACHABLE();
253}
254
255std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700256 // The best oat files are, in descending order of bestness:
257 // 1. Properly relocated files. These may be opened executable.
258 // 2. Not out-of-date files that are already opened non-executable.
259 // 3. Not out-of-date files that we must reopen non-executable.
260
Richard Uhler66d874d2015-01-15 09:37:19 -0800261 if (OatFileIsUpToDate()) {
262 oat_file_released_ = true;
263 return std::move(cached_oat_file_);
264 }
265
266 if (OdexFileIsUpToDate()) {
267 oat_file_released_ = true;
268 return std::move(cached_odex_file_);
269 }
270
Richard Uhler5f946da2015-07-17 12:28:32 -0700271 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
272 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800273
Richard Uhler5f946da2015-07-17 12:28:32 -0700274 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
275 oat_file_released_ = true;
276 return std::move(cached_oat_file_);
277 }
278
279 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
280 oat_file_released_ = true;
281 return std::move(cached_odex_file_);
282 }
283
284 if (!OatFileIsOutOfDate()) {
285 load_executable_ = false;
286 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700288 CHECK(!OatFileIsExecutable());
289 oat_file_released_ = true;
290 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800291 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700292 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800293
Richard Uhler5f946da2015-07-17 12:28:32 -0700294 if (!OdexFileIsOutOfDate()) {
295 load_executable_ = false;
296 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800297 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700298 CHECK(!OdexFileIsExecutable());
299 oat_file_released_ = true;
300 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800301 }
302 }
303
304 return std::unique_ptr<OatFile>();
305}
306
307std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
308 const OatFile& oat_file, const char* dex_location) {
309 std::vector<std::unique_ptr<const DexFile>> dex_files;
310
311 // Load the primary dex file.
312 std::string error_msg;
313 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
314 dex_location, nullptr, false);
315 if (oat_dex_file == nullptr) {
316 LOG(WARNING) << "Attempt to load out-of-date oat file "
317 << oat_file.GetLocation() << " for dex location " << dex_location;
318 return std::vector<std::unique_ptr<const DexFile>>();
319 }
320
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700321 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 if (dex_file.get() == nullptr) {
323 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
324 return std::vector<std::unique_ptr<const DexFile>>();
325 }
326 dex_files.push_back(std::move(dex_file));
327
328 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700329 for (size_t i = 1; ; i++) {
330 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800331 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700332 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800333 // There are no more secondary dex files to load.
334 break;
335 }
336
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700337 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800338 if (dex_file.get() == nullptr) {
339 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
340 return std::vector<std::unique_ptr<const DexFile>>();
341 }
342 dex_files.push_back(std::move(dex_file));
343 }
344 return dex_files;
345}
346
Richard Uhler9b994ea2015-06-24 08:44:19 -0700347bool OatFileAssistant::HasOriginalDexFiles() {
348 // Ensure GetRequiredDexChecksum has been run so that
349 // has_original_dex_files_ is initialized. We don't care about the result of
350 // GetRequiredDexChecksum.
351 GetRequiredDexChecksum();
352 return has_original_dex_files_;
353}
354
Richard Uhler66d874d2015-01-15 09:37:19 -0800355const std::string* OatFileAssistant::OdexFileName() {
356 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800357 cached_odex_file_name_attempted_ = true;
358
359 std::string error_msg;
360 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700361 dex_location_, isa_, &cached_odex_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800362 if (!cached_odex_file_name_found_) {
363 // If we can't figure out the odex file, we treat it as if the odex
364 // file was inaccessible.
365 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
366 }
367 }
368 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
369}
370
371bool OatFileAssistant::OdexFileExists() {
372 return GetOdexFile() != nullptr;
373}
374
Richard Uhler95abd042015-03-24 09:51:28 -0700375OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700376 if (!odex_file_status_attempted_) {
377 odex_file_status_attempted_ = true;
378 const OatFile* odex_file = GetOdexFile();
379 if (odex_file == nullptr) {
380 cached_odex_file_status_ = kOatOutOfDate;
381 } else {
382 cached_odex_file_status_ = GivenOatFileStatus(*odex_file);
383 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800384 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700385 return cached_odex_file_status_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800386}
387
388bool OatFileAssistant::OdexFileIsOutOfDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700389 return OdexFileStatus() == kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800390}
391
392bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700393 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800394}
395
396bool OatFileAssistant::OdexFileIsUpToDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700397 return OdexFileStatus() == kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800398}
399
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100400CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
401 const OatFile* odex_file = GetOdexFile();
402 CHECK(odex_file != nullptr);
403
404 return odex_file->GetCompilerFilter();
405}
Richard Uhler4c7f1932016-04-15 15:51:21 -0700406
407static std::string ArtFileName(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800408 const std::string oat_file_location = oat_file->GetLocation();
409 // Replace extension with .art
410 const size_t last_ext = oat_file_location.find_last_of('.');
411 if (last_ext == std::string::npos) {
412 LOG(ERROR) << "No extension in oat file " << oat_file_location;
413 return std::string();
414 }
415 return oat_file_location.substr(0, last_ext) + ".art";
416}
417
Richard Uhler66d874d2015-01-15 09:37:19 -0800418const std::string* OatFileAssistant::OatFileName() {
419 if (!cached_oat_file_name_attempted_) {
420 cached_oat_file_name_attempted_ = true;
421
422 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800423 // TODO: The oat file assistant should be the definitive place for
424 // determining the oat file name from the dex location, not
425 // GetDalvikCacheFilename.
426 std::string cache_dir = StringPrintf("%s%s",
427 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
428 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700429 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700430 cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800431 if (!cached_oat_file_name_found_) {
432 // If we can't determine the oat file name, we treat the oat file as
433 // inaccessible.
434 LOG(WARNING) << "Failed to determine oat file name for dex location "
435 << dex_location_ << ": " << error_msg;
436 }
437 }
438 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
439}
440
441bool OatFileAssistant::OatFileExists() {
442 return GetOatFile() != nullptr;
443}
444
Richard Uhler95abd042015-03-24 09:51:28 -0700445OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700446 if (!oat_file_status_attempted_) {
447 oat_file_status_attempted_ = true;
448 const OatFile* oat_file = GetOatFile();
449 if (oat_file == nullptr) {
450 cached_oat_file_status_ = kOatOutOfDate;
451 } else {
452 cached_oat_file_status_ = GivenOatFileStatus(*oat_file);
453 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800454 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700455 return cached_oat_file_status_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800456}
457
458bool OatFileAssistant::OatFileIsOutOfDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700459 return OatFileStatus() == kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800460}
461
462bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700463 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800464}
465
466bool OatFileAssistant::OatFileIsUpToDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700467 return OatFileStatus() == kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800468}
469
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100470CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
471 const OatFile* oat_file = GetOatFile();
472 CHECK(oat_file != nullptr);
473
474 return oat_file->GetCompilerFilter();
475}
476
Richard Uhler95abd042015-03-24 09:51:28 -0700477OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800478 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700479 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800480 // what we provide, which verifies the primary dex checksum for us.
481 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
482 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700483 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700484 if (oat_dex_file == nullptr) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700485 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 }
487
488 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700489 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800490 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700491 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800492 const OatFile::OatDexFile* secondary_oat_dex_file
493 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700494 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800495 // There are no more secondary dex files to check.
496 break;
497 }
498
499 std::string error_msg;
500 uint32_t expected_secondary_checksum = 0;
501 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700502 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800503 uint32_t actual_secondary_checksum
504 = secondary_oat_dex_file->GetDexFileLocationChecksum();
505 if (expected_secondary_checksum != actual_secondary_checksum) {
506 VLOG(oat) << "Dex checksum does not match for secondary dex: "
507 << secondary_dex_location
508 << ". Expected: " << expected_secondary_checksum
509 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700510 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800511 }
512 } else {
513 // If we can't get the checksum for the secondary location, we assume
514 // the dex checksum is up to date for this and all other secondary dex
515 // files.
516 break;
517 }
518 }
519
Andreas Gampe29d38e72016-03-23 15:31:51 +0000520 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
521 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000522
Richard Uhler66d874d2015-01-15 09:37:19 -0800523 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000524 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
525 const ImageInfo* image_info = GetImageInfo();
526 if (image_info == nullptr) {
527 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000528
Richard Uhler76f5cb62016-04-04 13:30:16 -0700529 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700530 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700531 }
532
533 // If there is no original dex file to fall back to, grudgingly accept
534 // the oat file. This could technically lead to crashes, but there's no
535 // way we could find a better oat file to use for this dex location,
536 // and it's better than being stuck in a boot loop with no way out.
537 // The problem will hopefully resolve itself the next time the runtime
538 // starts up.
539 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
540 << "Allow oat file use. This is potentially dangerous.";
541 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
542 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000543 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700544 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000545 }
546 } else {
547 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800548 }
549
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100550 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000551 if (!file.IsPic()) {
552 const ImageInfo* image_info = GetImageInfo();
553 if (image_info == nullptr) {
554 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700555 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000556 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700557
Andreas Gampe29d38e72016-03-23 15:31:51 +0000558 // Verify the oat_data_begin recorded for the image in the oat file matches
559 // the actual oat_data_begin for boot.oat in the image.
560 const OatHeader& oat_header = file.GetOatHeader();
561 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
562 if (oat_data_begin != image_info->oat_data_begin) {
563 VLOG(oat) << file.GetLocation() <<
564 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
565 << " does not match actual image oat_data_begin ("
566 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700567 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000568 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700569
Andreas Gampe29d38e72016-03-23 15:31:51 +0000570 // Verify the oat_patch_delta recorded for the image in the oat file matches
571 // the actual oat_patch_delta for the image.
572 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
573 if (oat_patch_delta != image_info->patch_delta) {
574 VLOG(oat) << file.GetLocation() <<
575 ": Oat file image patch delta (" << oat_patch_delta << ")"
576 << " does not match actual image patch delta ("
577 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700578 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000579 }
580 } else {
581 // Oat files compiled in PIC mode do not require relocation.
582 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
583 }
584 } else {
585 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800586 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700587 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800588}
589
Richard Uhler1e860612016-03-30 12:17:55 -0700590OatFileAssistant::ResultOfAttemptToUpdate
591OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800592 CHECK(error_msg != nullptr);
593
Richard Uhler95abd042015-03-24 09:51:28 -0700594 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700595 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700596 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700597 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800598 }
Richard Uhler95abd042015-03-24 09:51:28 -0700599 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800600
601 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700602 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800603 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700604 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800605 }
606 const std::string& oat_file_name = *OatFileName();
607
608 const ImageInfo* image_info = GetImageInfo();
609 Runtime* runtime = Runtime::Current();
610 if (image_info == nullptr) {
611 *error_msg = "Patching of oat file " + oat_file_name
612 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700613 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800614 }
615
616 if (!runtime->IsDex2OatEnabled()) {
617 *error_msg = "Patching of oat file " + oat_file_name
618 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700619 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800620 }
621
622 std::vector<std::string> argv;
623 argv.push_back(runtime->GetPatchoatExecutable());
624 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700625 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800626 argv.push_back("--output-oat-file=" + oat_file_name);
627 argv.push_back("--patched-image-location=" + image_info->location);
628
629 std::string command_line(Join(argv, ' '));
630 if (!Exec(argv, error_msg)) {
631 // Manually delete the file. This ensures there is no garbage left over if
632 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100633 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700634 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800635 }
636
637 // Mark that the oat file has changed and we should try to reload.
638 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700639 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800640}
641
Richard Uhler1e860612016-03-30 12:17:55 -0700642OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700643OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800644 CHECK(error_msg != nullptr);
645
Richard Uhler8327cf72015-10-13 16:34:59 -0700646 Runtime* runtime = Runtime::Current();
647 if (!runtime->IsDex2OatEnabled()) {
648 *error_msg = "Generation of oat file for dex location " + dex_location_
649 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700650 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700651 }
652
Richard Uhler66d874d2015-01-15 09:37:19 -0800653 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700654 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800655 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700656 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800657 }
658 const std::string& oat_file_name = *OatFileName();
659
Richard Uhler66d874d2015-01-15 09:37:19 -0800660 // dex2oat ignores missing dex files and doesn't report an error.
661 // Check explicitly here so we can detect the error properly.
662 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700663 if (!OS::FileExists(dex_location_.c_str())) {
664 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700665 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800666 }
667
Richard Uhler8327cf72015-10-13 16:34:59 -0700668 std::unique_ptr<File> oat_file;
669 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
670 if (oat_file.get() == nullptr) {
671 *error_msg = "Generation of oat file " + oat_file_name
672 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700673 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700674 }
675
676 if (fchmod(oat_file->Fd(), 0644) != 0) {
677 *error_msg = "Generation of oat file " + oat_file_name
678 + " not attempted because the oat file could not be made world readable.";
679 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700680 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700681 }
682
683 std::vector<std::string> args;
684 args.push_back("--dex-file=" + dex_location_);
685 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
686 args.push_back("--oat-location=" + oat_file_name);
687
Richard Uhler66d874d2015-01-15 09:37:19 -0800688 if (!Dex2Oat(args, error_msg)) {
689 // Manually delete the file. This ensures there is no garbage left over if
690 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700691 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100692 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700693 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700694 }
695
696 if (oat_file->FlushCloseOrErase() != 0) {
697 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100698 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700699 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800700 }
701
702 // Mark that the oat file has changed and we should try to reload.
703 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700704 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800705}
706
707bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
708 std::string* error_msg) {
709 Runtime* runtime = Runtime::Current();
710 std::string image_location = ImageLocation();
711 if (image_location.empty()) {
712 *error_msg = "No image location found for Dex2Oat.";
713 return false;
714 }
715
716 std::vector<std::string> argv;
717 argv.push_back(runtime->GetCompilerExecutable());
718 argv.push_back("--runtime-arg");
719 argv.push_back("-classpath");
720 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700721 std::string class_path = runtime->GetClassPathString();
722 if (class_path == "") {
723 class_path = OatFile::kSpecialSharedLibrary;
724 }
725 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100726 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200727 argv.push_back("--debuggable");
728 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700729 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800730
731 if (!runtime->IsVerificationEnabled()) {
732 argv.push_back("--compiler-filter=verify-none");
733 }
734
735 if (runtime->MustRelocateIfPossible()) {
736 argv.push_back("--runtime-arg");
737 argv.push_back("-Xrelocate");
738 } else {
739 argv.push_back("--runtime-arg");
740 argv.push_back("-Xnorelocate");
741 }
742
743 if (!kIsTargetBuild) {
744 argv.push_back("--host");
745 }
746
747 argv.push_back("--boot-image=" + image_location);
748
749 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
750 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
751
752 argv.insert(argv.end(), args.begin(), args.end());
753
754 std::string command_line(Join(argv, ' '));
755 return Exec(argv, error_msg);
756}
757
758bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
759 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
760 CHECK(odex_filename != nullptr);
761 CHECK(error_msg != nullptr);
762
763 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700764 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800765 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700766 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800767
Richard Uhler63434112015-03-16 14:32:16 -0700768 // Find the directory portion of the dex location and add the oat/<isa>
769 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800770 size_t pos = location.rfind('/');
771 if (pos == std::string::npos) {
772 *error_msg = "Dex location " + location + " has no directory.";
773 return false;
774 }
775 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700776 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800777
778 // Find the file portion of the dex location.
779 std::string file;
780 if (pos == std::string::npos) {
781 file = location;
782 } else {
783 file = location.substr(pos+1);
784 }
785
786 // Get the base part of the file without the extension.
787 pos = file.rfind('.');
788 if (pos == std::string::npos) {
789 *error_msg = "Dex location " + location + " has no extension.";
790 return false;
791 }
792 std::string base = file.substr(0, pos);
793
794 *odex_filename = dir + "/" + base + ".odex";
795 return true;
796}
797
798std::string OatFileAssistant::DalvikCacheDirectory() {
799 // Note: We don't cache this, because it will only be called once by
Andreas Gampe29d38e72016-03-23 15:31:51 +0000800 // OatFileName.
Richard Uhler66d874d2015-01-15 09:37:19 -0800801
802 // TODO: The work done in GetDalvikCache is overkill for what we need.
803 // Ideally a new API for getting the DalvikCacheDirectory the way we want
804 // (without existence testing, creation, or death) is provided with the rest
805 // of the GetDalvikCache family of functions. Until such an API is in place,
806 // we use GetDalvikCache to avoid duplicating the logic for determining the
807 // dalvik cache directory.
808 std::string result;
809 bool have_android_data;
810 bool dalvik_cache_exists;
811 bool is_global_cache;
812 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
813 return result;
814}
815
Richard Uhler66d874d2015-01-15 09:37:19 -0800816std::string OatFileAssistant::ImageLocation() {
817 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000818 const std::vector<gc::space::ImageSpace*>& image_spaces =
819 runtime->GetHeap()->GetBootImageSpaces();
820 if (image_spaces.empty()) {
821 return "";
822 }
823 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800824}
825
826const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700827 if (!required_dex_checksum_attempted_) {
828 required_dex_checksum_attempted_ = true;
829 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800830 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700831 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700832 required_dex_checksum_found_ = true;
833 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800834 } else {
835 // This can happen if the original dex file has been stripped from the
836 // apk.
837 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700838 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800839
840 // Get the checksum from the odex if we can.
841 const OatFile* odex_file = GetOdexFile();
842 if (odex_file != nullptr) {
843 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700844 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800845 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700846 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
847 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800848 }
849 }
850 }
851 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700852 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800853}
854
855const OatFile* OatFileAssistant::GetOdexFile() {
856 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
857 if (!odex_file_load_attempted_) {
858 odex_file_load_attempted_ = true;
859 if (OdexFileName() != nullptr) {
860 const std::string& odex_file_name = *OdexFileName();
861 std::string error_msg;
862 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800863 odex_file_name.c_str(),
864 nullptr,
865 nullptr,
866 load_executable_,
867 /*low_4gb*/false,
868 dex_location_.c_str(),
869 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800870 if (cached_odex_file_.get() == nullptr) {
871 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
872 << odex_file_name << ": " << error_msg;
873 }
874 }
875 }
876 return cached_odex_file_.get();
877}
878
Richard Uhler5f946da2015-07-17 12:28:32 -0700879bool OatFileAssistant::OdexFileIsExecutable() {
880 const OatFile* odex_file = GetOdexFile();
881 return (odex_file != nullptr && odex_file->IsExecutable());
882}
883
Richard Uhlerd1537b52016-03-29 13:27:41 -0700884bool OatFileAssistant::OdexFileHasPatchInfo() {
885 const OatFile* odex_file = GetOdexFile();
886 return (odex_file != nullptr && odex_file->HasPatchInfo());
887}
888
Richard Uhler66d874d2015-01-15 09:37:19 -0800889void OatFileAssistant::ClearOdexFileCache() {
890 odex_file_load_attempted_ = false;
891 cached_odex_file_.reset();
Richard Uhlere8109f72016-04-18 10:40:50 -0700892 odex_file_status_attempted_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800893}
894
895const OatFile* OatFileAssistant::GetOatFile() {
896 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
897 if (!oat_file_load_attempted_) {
898 oat_file_load_attempted_ = true;
899 if (OatFileName() != nullptr) {
900 const std::string& oat_file_name = *OatFileName();
901 std::string error_msg;
902 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800903 oat_file_name.c_str(),
904 nullptr,
905 nullptr,
906 load_executable_,
907 /*low_4gb*/false,
908 dex_location_.c_str(),
909 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800910 if (cached_oat_file_.get() == nullptr) {
911 VLOG(oat) << "OatFileAssistant test for existing oat file "
912 << oat_file_name << ": " << error_msg;
913 }
914 }
915 }
916 return cached_oat_file_.get();
917}
918
Richard Uhler5f946da2015-07-17 12:28:32 -0700919bool OatFileAssistant::OatFileIsExecutable() {
920 const OatFile* oat_file = GetOatFile();
921 return (oat_file != nullptr && oat_file->IsExecutable());
922}
923
Richard Uhlerd1537b52016-03-29 13:27:41 -0700924bool OatFileAssistant::OatFileHasPatchInfo() {
925 const OatFile* oat_file = GetOatFile();
926 return (oat_file != nullptr && oat_file->HasPatchInfo());
927}
928
Richard Uhler66d874d2015-01-15 09:37:19 -0800929void OatFileAssistant::ClearOatFileCache() {
930 oat_file_load_attempted_ = false;
931 cached_oat_file_.reset();
Richard Uhlere8109f72016-04-18 10:40:50 -0700932 oat_file_status_attempted_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800933}
934
935const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
936 if (!image_info_load_attempted_) {
937 image_info_load_attempted_ = true;
938
939 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800940 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
941 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800942 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800943
944 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800945 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800946 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800947 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
948 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800949 cached_image_info_.patch_delta = image_header.GetPatchDelta();
950 } else {
951 std::unique_ptr<ImageHeader> image_header(
Jeff Haob11ffb72016-04-07 15:40:54 -0700952 gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_));
Richard Uhler66d874d2015-01-15 09:37:19 -0800953 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800954 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
955 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800956 cached_image_info_.patch_delta = image_header->GetPatchDelta();
957 }
958 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800959 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700960
Jeff Haofd336c32016-04-07 19:46:31 -0700961 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800962 }
963 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
964}
965
Jeff Haob11ffb72016-04-07 15:40:54 -0700966// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700967uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700968 uint32_t checksum = 0;
969 std::vector<gc::space::ImageSpace*> image_spaces =
970 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700971 if (isa == kRuntimeISA) {
972 for (gc::space::ImageSpace* image_space : image_spaces) {
973 checksum ^= image_space->GetImageHeader().GetOatChecksum();
974 }
975 } else {
976 for (gc::space::ImageSpace* image_space : image_spaces) {
977 std::string location = image_space->GetImageLocation();
978 std::unique_ptr<ImageHeader> image_header(
979 gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa));
980 checksum ^= image_header->GetOatChecksum();
981 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700982 }
983 return checksum;
984}
985
986uint32_t OatFileAssistant::GetCombinedImageChecksum() {
987 if (!image_info_load_attempted_) {
988 GetImageInfo();
989 }
990 return combined_image_checksum_;
991}
992
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800993gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
994 DCHECK(oat_file != nullptr);
995 std::string art_file = ArtFileName(oat_file);
996 if (art_file.empty()) {
997 return nullptr;
998 }
999 std::string error_msg;
1000 ScopedObjectAccess soa(Thread::Current());
1001 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
1002 oat_file,
1003 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001004 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001005 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1006 }
1007 return ret;
1008}
1009
Richard Uhler66d874d2015-01-15 09:37:19 -08001010} // namespace art
1011