blob: 67281231bf91762763da62c0c183b8b1d4fca6ff [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
84 // If the user gave a target oat location, save that as the cached oat
85 // location now so we won't try to construct the default location later.
86 if (oat_location != nullptr) {
87 cached_oat_file_name_ = std::string(oat_location);
88 cached_oat_file_name_attempted_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -080089 }
Richard Uhler66d874d2015-01-15 09:37:19 -080090}
91
92OatFileAssistant::~OatFileAssistant() {
93 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -070094 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010095 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -080096 }
97}
98
99bool OatFileAssistant::IsInBootClassPath() {
100 // Note: We check the current boot class path, regardless of the ISA
101 // specified by the user. This is okay, because the boot class path should
102 // be the same for all ISAs.
103 // TODO: Can we verify the boot class path is the same for all ISAs?
104 Runtime* runtime = Runtime::Current();
105 ClassLinker* class_linker = runtime->GetClassLinker();
106 const auto& boot_class_path = class_linker->GetBootClassPath();
107 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700108 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800109 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
110 return true;
111 }
112 }
113 return false;
114}
115
116bool OatFileAssistant::Lock(std::string* error_msg) {
117 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700118 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800119
120 if (OatFileName() == nullptr) {
121 *error_msg = "Failed to determine lock file";
122 return false;
123 }
124 std::string lock_file_name = *OatFileName() + ".flock";
125
Richard Uhler581f4e92015-05-07 10:19:35 -0700126 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100127 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800128 return false;
129 }
130 return true;
131}
132
Richard Uhlerd1472a22016-04-15 15:18:56 -0700133static bool GivenOatFileCompilerFilterIsOkay(const OatFile& oat_file,
134 CompilerFilter::Filter target,
135 bool profile_changed) {
136 CompilerFilter::Filter current = oat_file.GetCompilerFilter();
137
138 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
139 VLOG(oat) << "Compiler filter not okay because Profile changed";
140 return false;
141 }
142 return CompilerFilter::IsAsGoodAs(current, target);
143}
144
145bool OatFileAssistant::OatFileCompilerFilterIsOkay(CompilerFilter::Filter target,
146 bool profile_changed) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000147 const OatFile* oat_file = GetOatFile();
148 if (oat_file != nullptr) {
Richard Uhlerd1472a22016-04-15 15:18:56 -0700149 return GivenOatFileCompilerFilterIsOkay(*oat_file, target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000150 }
151 return false;
Calin Juravleb077e152016-02-18 18:47:37 +0000152}
Richard Uhler66d874d2015-01-15 09:37:19 -0800153
Richard Uhlerd1472a22016-04-15 15:18:56 -0700154bool OatFileAssistant::OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target,
155 bool profile_changed) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000156 const OatFile* odex_file = GetOdexFile();
157 if (odex_file != nullptr) {
Richard Uhlerd1472a22016-04-15 15:18:56 -0700158 return GivenOatFileCompilerFilterIsOkay(*odex_file, target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000159 }
160 return false;
161}
162
Richard Uhlerd1472a22016-04-15 15:18:56 -0700163OatFileAssistant::DexOptNeeded
164OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
165 bool profile_changed) {
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100166 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000167
168 // See if the oat file is in good shape as is.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700169 bool oat_okay = OatFileCompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000170 if (oat_okay) {
171 if (compilation_desired) {
172 if (OatFileIsUpToDate()) {
173 return kNoDexOptNeeded;
174 }
175 } else {
176 if (!OatFileIsOutOfDate()) {
177 return kNoDexOptNeeded;
178 }
179 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800180 }
Richard Uhler95abd042015-03-24 09:51:28 -0700181
Andreas Gampe29d38e72016-03-23 15:31:51 +0000182 // See if the odex file is in good shape as is.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700183 bool odex_okay = OdexFileCompilerFilterIsOkay(target, profile_changed);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000184 if (odex_okay) {
185 if (compilation_desired) {
186 if (OdexFileIsUpToDate()) {
187 return kNoDexOptNeeded;
188 }
189 } else {
190 if (!OdexFileIsOutOfDate()) {
191 return kNoDexOptNeeded;
192 }
193 }
Richard Uhler95abd042015-03-24 09:51:28 -0700194 }
195
Andreas Gampe29d38e72016-03-23 15:31:51 +0000196 // See if we can get an up-to-date file by running patchoat.
197 if (compilation_desired) {
Richard Uhlerd1537b52016-03-29 13:27:41 -0700198 if (odex_okay && OdexFileNeedsRelocation() && OdexFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000199 return kPatchOatNeeded;
200 }
201
Richard Uhlerd1537b52016-03-29 13:27:41 -0700202 if (oat_okay && OatFileNeedsRelocation() && OatFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000203 return kSelfPatchOatNeeded;
204 }
Richard Uhler95abd042015-03-24 09:51:28 -0700205 }
206
Andreas Gampe29d38e72016-03-23 15:31:51 +0000207 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700208 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800209}
210
Richard Uhlerf4b34872016-04-13 11:03:46 -0700211// Figure out the currently specified compile filter option in the runtime.
212// Returns true on success, false if the compiler filter is invalid, in which
213// case error_msg describes the problem.
214static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
215 std::string* error_msg) {
216 CHECK(filter != nullptr);
217 CHECK(error_msg != nullptr);
218
219 *filter = CompilerFilter::kDefaultCompilerFilter;
220 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
221 if (option.starts_with("--compiler-filter=")) {
222 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
223 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
224 *error_msg = std::string("Unknown --compiler-filter value: ")
225 + std::string(compiler_filter_string);
226 return false;
227 }
228 }
229 }
230 return true;
231}
232
Richard Uhler01be6812016-05-17 10:34:52 -0700233bool OatFileAssistant::IsUpToDate() {
234 return OatFileIsUpToDate() || OdexFileIsUpToDate();
235}
236
Richard Uhler1e860612016-03-30 12:17:55 -0700237OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700238OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700239 CompilerFilter::Filter target;
240 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
241 return kUpdateNotAttempted;
242 }
243
Richard Uhlerd1472a22016-04-15 15:18:56 -0700244 switch (GetDexOptNeeded(target, profile_changed)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700245 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700246 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700247 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
248 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800249 }
250 UNREACHABLE();
251}
252
253std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700254 // The best oat files are, in descending order of bestness:
255 // 1. Properly relocated files. These may be opened executable.
256 // 2. Not out-of-date files that are already opened non-executable.
257 // 3. Not out-of-date files that we must reopen non-executable.
258
Richard Uhler66d874d2015-01-15 09:37:19 -0800259 if (OatFileIsUpToDate()) {
260 oat_file_released_ = true;
261 return std::move(cached_oat_file_);
262 }
263
264 if (OdexFileIsUpToDate()) {
265 oat_file_released_ = true;
266 return std::move(cached_odex_file_);
267 }
268
Richard Uhler5f946da2015-07-17 12:28:32 -0700269 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
270 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800271
Richard Uhler5f946da2015-07-17 12:28:32 -0700272 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
273 oat_file_released_ = true;
274 return std::move(cached_oat_file_);
275 }
276
277 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
278 oat_file_released_ = true;
279 return std::move(cached_odex_file_);
280 }
281
282 if (!OatFileIsOutOfDate()) {
283 load_executable_ = false;
284 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700286 CHECK(!OatFileIsExecutable());
287 oat_file_released_ = true;
288 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800289 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700290 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800291
Richard Uhler5f946da2015-07-17 12:28:32 -0700292 if (!OdexFileIsOutOfDate()) {
293 load_executable_ = false;
294 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800295 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700296 CHECK(!OdexFileIsExecutable());
297 oat_file_released_ = true;
298 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800299 }
300 }
301
302 return std::unique_ptr<OatFile>();
303}
304
305std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
306 const OatFile& oat_file, const char* dex_location) {
307 std::vector<std::unique_ptr<const DexFile>> dex_files;
308
309 // Load the primary dex file.
310 std::string error_msg;
311 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
312 dex_location, nullptr, false);
313 if (oat_dex_file == nullptr) {
314 LOG(WARNING) << "Attempt to load out-of-date oat file "
315 << oat_file.GetLocation() << " for dex location " << dex_location;
316 return std::vector<std::unique_ptr<const DexFile>>();
317 }
318
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700319 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800320 if (dex_file.get() == nullptr) {
321 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
322 return std::vector<std::unique_ptr<const DexFile>>();
323 }
324 dex_files.push_back(std::move(dex_file));
325
326 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700327 for (size_t i = 1; ; i++) {
328 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800329 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700330 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800331 // There are no more secondary dex files to load.
332 break;
333 }
334
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700335 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800336 if (dex_file.get() == nullptr) {
337 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
338 return std::vector<std::unique_ptr<const DexFile>>();
339 }
340 dex_files.push_back(std::move(dex_file));
341 }
342 return dex_files;
343}
344
Richard Uhler9b994ea2015-06-24 08:44:19 -0700345bool OatFileAssistant::HasOriginalDexFiles() {
346 // Ensure GetRequiredDexChecksum has been run so that
347 // has_original_dex_files_ is initialized. We don't care about the result of
348 // GetRequiredDexChecksum.
349 GetRequiredDexChecksum();
350 return has_original_dex_files_;
351}
352
Richard Uhler66d874d2015-01-15 09:37:19 -0800353const std::string* OatFileAssistant::OdexFileName() {
354 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800355 cached_odex_file_name_attempted_ = true;
356
357 std::string error_msg;
Richard Uhlere8e48ae2016-04-19 12:41:04 -0700358 if (!DexFilenameToOdexFilename(dex_location_, isa_, &cached_odex_file_name_, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800359 // If we can't figure out the odex file, we treat it as if the odex
360 // file was inaccessible.
361 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
362 }
363 }
Richard Uhlere8e48ae2016-04-19 12:41:04 -0700364 return cached_odex_file_name_.empty() ? nullptr : &cached_odex_file_name_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800365}
366
367bool OatFileAssistant::OdexFileExists() {
368 return GetOdexFile() != nullptr;
369}
370
Richard Uhler95abd042015-03-24 09:51:28 -0700371OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700372 if (!odex_file_status_attempted_) {
373 odex_file_status_attempted_ = true;
374 const OatFile* odex_file = GetOdexFile();
375 if (odex_file == nullptr) {
376 cached_odex_file_status_ = kOatOutOfDate;
377 } else {
378 cached_odex_file_status_ = GivenOatFileStatus(*odex_file);
379 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800380 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700381 return cached_odex_file_status_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800382}
383
384bool OatFileAssistant::OdexFileIsOutOfDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700385 return OdexFileStatus() == kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800386}
387
388bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700389 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800390}
391
392bool OatFileAssistant::OdexFileIsUpToDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700393 return OdexFileStatus() == kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800394}
395
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100396CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
397 const OatFile* odex_file = GetOdexFile();
398 CHECK(odex_file != nullptr);
399
400 return odex_file->GetCompilerFilter();
401}
Richard Uhler4c7f1932016-04-15 15:51:21 -0700402
403static std::string ArtFileName(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800404 const std::string oat_file_location = oat_file->GetLocation();
405 // Replace extension with .art
406 const size_t last_ext = oat_file_location.find_last_of('.');
407 if (last_ext == std::string::npos) {
408 LOG(ERROR) << "No extension in oat file " << oat_file_location;
409 return std::string();
410 }
411 return oat_file_location.substr(0, last_ext) + ".art";
412}
413
Richard Uhler66d874d2015-01-15 09:37:19 -0800414const std::string* OatFileAssistant::OatFileName() {
415 if (!cached_oat_file_name_attempted_) {
416 cached_oat_file_name_attempted_ = true;
417
418 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 // TODO: The oat file assistant should be the definitive place for
420 // determining the oat file name from the dex location, not
421 // GetDalvikCacheFilename.
422 std::string cache_dir = StringPrintf("%s%s",
423 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
424 std::string error_msg;
Richard Uhlere8e48ae2016-04-19 12:41:04 -0700425 if (!GetDalvikCacheFilename(dex_location_.c_str(),
426 cache_dir.c_str(), &cached_oat_file_name_, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800427 // If we can't determine the oat file name, we treat the oat file as
428 // inaccessible.
429 LOG(WARNING) << "Failed to determine oat file name for dex location "
430 << dex_location_ << ": " << error_msg;
431 }
432 }
Richard Uhlere8e48ae2016-04-19 12:41:04 -0700433 return cached_oat_file_name_.empty() ? nullptr : &cached_oat_file_name_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800434}
435
436bool OatFileAssistant::OatFileExists() {
437 return GetOatFile() != nullptr;
438}
439
Richard Uhler95abd042015-03-24 09:51:28 -0700440OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700441 if (!oat_file_status_attempted_) {
442 oat_file_status_attempted_ = true;
443 const OatFile* oat_file = GetOatFile();
444 if (oat_file == nullptr) {
445 cached_oat_file_status_ = kOatOutOfDate;
446 } else {
447 cached_oat_file_status_ = GivenOatFileStatus(*oat_file);
448 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700450 return cached_oat_file_status_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800451}
452
453bool OatFileAssistant::OatFileIsOutOfDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700454 return OatFileStatus() == kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800455}
456
457bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700458 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800459}
460
461bool OatFileAssistant::OatFileIsUpToDate() {
Richard Uhlere8109f72016-04-18 10:40:50 -0700462 return OatFileStatus() == kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800463}
464
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100465CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
466 const OatFile* oat_file = GetOatFile();
467 CHECK(oat_file != nullptr);
468
469 return oat_file->GetCompilerFilter();
470}
471
Richard Uhler95abd042015-03-24 09:51:28 -0700472OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800473 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700474 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800475 // what we provide, which verifies the primary dex checksum for us.
476 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
477 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700478 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700479 if (oat_dex_file == nullptr) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700480 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800481 }
482
483 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700484 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800485 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700486 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800487 const OatFile::OatDexFile* secondary_oat_dex_file
488 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700489 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800490 // There are no more secondary dex files to check.
491 break;
492 }
493
494 std::string error_msg;
495 uint32_t expected_secondary_checksum = 0;
496 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700497 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800498 uint32_t actual_secondary_checksum
499 = secondary_oat_dex_file->GetDexFileLocationChecksum();
500 if (expected_secondary_checksum != actual_secondary_checksum) {
501 VLOG(oat) << "Dex checksum does not match for secondary dex: "
502 << secondary_dex_location
503 << ". Expected: " << expected_secondary_checksum
504 << ", Actual: " << actual_secondary_checksum;
Richard Uhlere8109f72016-04-18 10:40:50 -0700505 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800506 }
507 } else {
508 // If we can't get the checksum for the secondary location, we assume
509 // the dex checksum is up to date for this and all other secondary dex
510 // files.
511 break;
512 }
513 }
514
Andreas Gampe29d38e72016-03-23 15:31:51 +0000515 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
516 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000517
Richard Uhler66d874d2015-01-15 09:37:19 -0800518 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000519 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
520 const ImageInfo* image_info = GetImageInfo();
521 if (image_info == nullptr) {
522 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000523
Richard Uhler76f5cb62016-04-04 13:30:16 -0700524 if (HasOriginalDexFiles()) {
Richard Uhlere8109f72016-04-18 10:40:50 -0700525 return kOatOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700526 }
527
528 // If there is no original dex file to fall back to, grudgingly accept
529 // the oat file. This could technically lead to crashes, but there's no
530 // way we could find a better oat file to use for this dex location,
531 // and it's better than being stuck in a boot loop with no way out.
532 // The problem will hopefully resolve itself the next time the runtime
533 // starts up.
534 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
535 << "Allow oat file use. This is potentially dangerous.";
536 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
537 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000538 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700539 return kOatOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000540 }
541 } else {
542 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800543 }
544
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100545 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000546 if (!file.IsPic()) {
547 const ImageInfo* image_info = GetImageInfo();
548 if (image_info == nullptr) {
549 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhlere8109f72016-04-18 10:40:50 -0700550 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000551 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700552
Andreas Gampe29d38e72016-03-23 15:31:51 +0000553 // Verify the oat_data_begin recorded for the image in the oat file matches
554 // the actual oat_data_begin for boot.oat in the image.
555 const OatHeader& oat_header = file.GetOatHeader();
556 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
557 if (oat_data_begin != image_info->oat_data_begin) {
558 VLOG(oat) << file.GetLocation() <<
559 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
560 << " does not match actual image oat_data_begin ("
561 << image_info->oat_data_begin << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700562 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000563 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700564
Andreas Gampe29d38e72016-03-23 15:31:51 +0000565 // Verify the oat_patch_delta recorded for the image in the oat file matches
566 // the actual oat_patch_delta for the image.
567 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
568 if (oat_patch_delta != image_info->patch_delta) {
569 VLOG(oat) << file.GetLocation() <<
570 ": Oat file image patch delta (" << oat_patch_delta << ")"
571 << " does not match actual image patch delta ("
572 << image_info->patch_delta << ")";
Richard Uhlere8109f72016-04-18 10:40:50 -0700573 return kOatNeedsRelocation;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000574 }
575 } else {
576 // Oat files compiled in PIC mode do not require relocation.
577 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
578 }
579 } else {
580 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800581 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700582 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800583}
584
Richard Uhler1e860612016-03-30 12:17:55 -0700585OatFileAssistant::ResultOfAttemptToUpdate
586OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800587 CHECK(error_msg != nullptr);
588
Richard Uhler95abd042015-03-24 09:51:28 -0700589 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700590 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700591 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700592 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800593 }
Richard Uhler95abd042015-03-24 09:51:28 -0700594 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800595
596 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700597 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800598 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700599 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800600 }
601 const std::string& oat_file_name = *OatFileName();
602
603 const ImageInfo* image_info = GetImageInfo();
604 Runtime* runtime = Runtime::Current();
605 if (image_info == nullptr) {
606 *error_msg = "Patching of oat file " + oat_file_name
607 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700608 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800609 }
610
611 if (!runtime->IsDex2OatEnabled()) {
612 *error_msg = "Patching of oat file " + oat_file_name
613 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700614 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800615 }
616
617 std::vector<std::string> argv;
618 argv.push_back(runtime->GetPatchoatExecutable());
619 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700620 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800621 argv.push_back("--output-oat-file=" + oat_file_name);
622 argv.push_back("--patched-image-location=" + image_info->location);
623
624 std::string command_line(Join(argv, ' '));
625 if (!Exec(argv, error_msg)) {
626 // Manually delete the file. This ensures there is no garbage left over if
627 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100628 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700629 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800630 }
631
632 // Mark that the oat file has changed and we should try to reload.
633 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700634 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800635}
636
Richard Uhler1e860612016-03-30 12:17:55 -0700637OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700638OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800639 CHECK(error_msg != nullptr);
640
Richard Uhler8327cf72015-10-13 16:34:59 -0700641 Runtime* runtime = Runtime::Current();
642 if (!runtime->IsDex2OatEnabled()) {
643 *error_msg = "Generation of oat file for dex location " + dex_location_
644 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700645 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700646 }
647
Richard Uhler66d874d2015-01-15 09:37:19 -0800648 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700649 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800650 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700651 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800652 }
653 const std::string& oat_file_name = *OatFileName();
654
Richard Uhler66d874d2015-01-15 09:37:19 -0800655 // dex2oat ignores missing dex files and doesn't report an error.
656 // Check explicitly here so we can detect the error properly.
657 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700658 if (!OS::FileExists(dex_location_.c_str())) {
659 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700660 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800661 }
662
Richard Uhler8327cf72015-10-13 16:34:59 -0700663 std::unique_ptr<File> oat_file;
664 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
665 if (oat_file.get() == nullptr) {
666 *error_msg = "Generation of oat file " + oat_file_name
667 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700668 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700669 }
670
671 if (fchmod(oat_file->Fd(), 0644) != 0) {
672 *error_msg = "Generation of oat file " + oat_file_name
673 + " not attempted because the oat file could not be made world readable.";
674 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700675 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700676 }
677
678 std::vector<std::string> args;
679 args.push_back("--dex-file=" + dex_location_);
680 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
681 args.push_back("--oat-location=" + oat_file_name);
682
Richard Uhler66d874d2015-01-15 09:37:19 -0800683 if (!Dex2Oat(args, error_msg)) {
684 // Manually delete the file. This ensures there is no garbage left over if
685 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700686 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100687 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700688 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700689 }
690
691 if (oat_file->FlushCloseOrErase() != 0) {
692 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100693 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700694 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800695 }
696
697 // Mark that the oat file has changed and we should try to reload.
698 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700699 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800700}
701
702bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
703 std::string* error_msg) {
704 Runtime* runtime = Runtime::Current();
705 std::string image_location = ImageLocation();
706 if (image_location.empty()) {
707 *error_msg = "No image location found for Dex2Oat.";
708 return false;
709 }
710
711 std::vector<std::string> argv;
712 argv.push_back(runtime->GetCompilerExecutable());
713 argv.push_back("--runtime-arg");
714 argv.push_back("-classpath");
715 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700716 std::string class_path = runtime->GetClassPathString();
717 if (class_path == "") {
718 class_path = OatFile::kSpecialSharedLibrary;
719 }
720 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100721 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200722 argv.push_back("--debuggable");
723 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700724 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800725
726 if (!runtime->IsVerificationEnabled()) {
727 argv.push_back("--compiler-filter=verify-none");
728 }
729
730 if (runtime->MustRelocateIfPossible()) {
731 argv.push_back("--runtime-arg");
732 argv.push_back("-Xrelocate");
733 } else {
734 argv.push_back("--runtime-arg");
735 argv.push_back("-Xnorelocate");
736 }
737
738 if (!kIsTargetBuild) {
739 argv.push_back("--host");
740 }
741
742 argv.push_back("--boot-image=" + image_location);
743
744 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
745 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
746
747 argv.insert(argv.end(), args.begin(), args.end());
748
749 std::string command_line(Join(argv, ' '));
750 return Exec(argv, error_msg);
751}
752
753bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
754 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
755 CHECK(odex_filename != nullptr);
756 CHECK(error_msg != nullptr);
757
758 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700759 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800760 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700761 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800762
Richard Uhler63434112015-03-16 14:32:16 -0700763 // Find the directory portion of the dex location and add the oat/<isa>
764 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800765 size_t pos = location.rfind('/');
766 if (pos == std::string::npos) {
767 *error_msg = "Dex location " + location + " has no directory.";
768 return false;
769 }
770 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700771 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800772
773 // Find the file portion of the dex location.
774 std::string file;
775 if (pos == std::string::npos) {
776 file = location;
777 } else {
778 file = location.substr(pos+1);
779 }
780
781 // Get the base part of the file without the extension.
782 pos = file.rfind('.');
783 if (pos == std::string::npos) {
784 *error_msg = "Dex location " + location + " has no extension.";
785 return false;
786 }
787 std::string base = file.substr(0, pos);
788
789 *odex_filename = dir + "/" + base + ".odex";
790 return true;
791}
792
793std::string OatFileAssistant::DalvikCacheDirectory() {
794 // Note: We don't cache this, because it will only be called once by
Andreas Gampe29d38e72016-03-23 15:31:51 +0000795 // OatFileName.
Richard Uhler66d874d2015-01-15 09:37:19 -0800796
797 // TODO: The work done in GetDalvikCache is overkill for what we need.
798 // Ideally a new API for getting the DalvikCacheDirectory the way we want
799 // (without existence testing, creation, or death) is provided with the rest
800 // of the GetDalvikCache family of functions. Until such an API is in place,
801 // we use GetDalvikCache to avoid duplicating the logic for determining the
802 // dalvik cache directory.
803 std::string result;
804 bool have_android_data;
805 bool dalvik_cache_exists;
806 bool is_global_cache;
807 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
808 return result;
809}
810
Richard Uhler66d874d2015-01-15 09:37:19 -0800811std::string OatFileAssistant::ImageLocation() {
812 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000813 const std::vector<gc::space::ImageSpace*>& image_spaces =
814 runtime->GetHeap()->GetBootImageSpaces();
815 if (image_spaces.empty()) {
816 return "";
817 }
818 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800819}
820
821const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700822 if (!required_dex_checksum_attempted_) {
823 required_dex_checksum_attempted_ = true;
824 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800825 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700826 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700827 required_dex_checksum_found_ = true;
828 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800829 } else {
830 // This can happen if the original dex file has been stripped from the
831 // apk.
832 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700833 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800834
835 // Get the checksum from the odex if we can.
836 const OatFile* odex_file = GetOdexFile();
837 if (odex_file != nullptr) {
838 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700839 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800840 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700841 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
842 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800843 }
844 }
845 }
846 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700847 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800848}
849
850const OatFile* OatFileAssistant::GetOdexFile() {
851 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
852 if (!odex_file_load_attempted_) {
853 odex_file_load_attempted_ = true;
854 if (OdexFileName() != nullptr) {
855 const std::string& odex_file_name = *OdexFileName();
856 std::string error_msg;
857 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800858 odex_file_name.c_str(),
859 nullptr,
860 nullptr,
861 load_executable_,
862 /*low_4gb*/false,
863 dex_location_.c_str(),
864 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800865 if (cached_odex_file_.get() == nullptr) {
866 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
867 << odex_file_name << ": " << error_msg;
868 }
869 }
870 }
871 return cached_odex_file_.get();
872}
873
Richard Uhler5f946da2015-07-17 12:28:32 -0700874bool OatFileAssistant::OdexFileIsExecutable() {
875 const OatFile* odex_file = GetOdexFile();
876 return (odex_file != nullptr && odex_file->IsExecutable());
877}
878
Richard Uhlerd1537b52016-03-29 13:27:41 -0700879bool OatFileAssistant::OdexFileHasPatchInfo() {
880 const OatFile* odex_file = GetOdexFile();
881 return (odex_file != nullptr && odex_file->HasPatchInfo());
882}
883
Richard Uhler66d874d2015-01-15 09:37:19 -0800884void OatFileAssistant::ClearOdexFileCache() {
885 odex_file_load_attempted_ = false;
886 cached_odex_file_.reset();
Richard Uhlere8109f72016-04-18 10:40:50 -0700887 odex_file_status_attempted_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800888}
889
890const OatFile* OatFileAssistant::GetOatFile() {
891 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
892 if (!oat_file_load_attempted_) {
893 oat_file_load_attempted_ = true;
894 if (OatFileName() != nullptr) {
895 const std::string& oat_file_name = *OatFileName();
896 std::string error_msg;
897 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800898 oat_file_name.c_str(),
899 nullptr,
900 nullptr,
901 load_executable_,
902 /*low_4gb*/false,
903 dex_location_.c_str(),
904 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800905 if (cached_oat_file_.get() == nullptr) {
906 VLOG(oat) << "OatFileAssistant test for existing oat file "
907 << oat_file_name << ": " << error_msg;
908 }
909 }
910 }
911 return cached_oat_file_.get();
912}
913
Richard Uhler5f946da2015-07-17 12:28:32 -0700914bool OatFileAssistant::OatFileIsExecutable() {
915 const OatFile* oat_file = GetOatFile();
916 return (oat_file != nullptr && oat_file->IsExecutable());
917}
918
Richard Uhlerd1537b52016-03-29 13:27:41 -0700919bool OatFileAssistant::OatFileHasPatchInfo() {
920 const OatFile* oat_file = GetOatFile();
921 return (oat_file != nullptr && oat_file->HasPatchInfo());
922}
923
Richard Uhler66d874d2015-01-15 09:37:19 -0800924void OatFileAssistant::ClearOatFileCache() {
925 oat_file_load_attempted_ = false;
926 cached_oat_file_.reset();
Richard Uhlere8109f72016-04-18 10:40:50 -0700927 oat_file_status_attempted_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800928}
929
930const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
931 if (!image_info_load_attempted_) {
932 image_info_load_attempted_ = true;
933
934 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800935 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
936 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800937 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800938
939 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800940 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800941 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800942 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
943 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800944 cached_image_info_.patch_delta = image_header.GetPatchDelta();
945 } else {
946 std::unique_ptr<ImageHeader> image_header(
Jeff Haob11ffb72016-04-07 15:40:54 -0700947 gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_));
Richard Uhler66d874d2015-01-15 09:37:19 -0800948 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800949 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
950 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800951 cached_image_info_.patch_delta = image_header->GetPatchDelta();
952 }
953 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800954 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700955
Jeff Haofd336c32016-04-07 19:46:31 -0700956 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800957 }
958 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
959}
960
Jeff Haob11ffb72016-04-07 15:40:54 -0700961// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700962uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700963 uint32_t checksum = 0;
964 std::vector<gc::space::ImageSpace*> image_spaces =
965 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700966 if (isa == kRuntimeISA) {
967 for (gc::space::ImageSpace* image_space : image_spaces) {
968 checksum ^= image_space->GetImageHeader().GetOatChecksum();
969 }
970 } else {
971 for (gc::space::ImageSpace* image_space : image_spaces) {
972 std::string location = image_space->GetImageLocation();
973 std::unique_ptr<ImageHeader> image_header(
974 gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa));
975 checksum ^= image_header->GetOatChecksum();
976 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700977 }
978 return checksum;
979}
980
981uint32_t OatFileAssistant::GetCombinedImageChecksum() {
982 if (!image_info_load_attempted_) {
983 GetImageInfo();
984 }
985 return combined_image_checksum_;
986}
987
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800988gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
989 DCHECK(oat_file != nullptr);
990 std::string art_file = ArtFileName(oat_file);
991 if (art_file.empty()) {
992 return nullptr;
993 }
994 std::string error_msg;
995 ScopedObjectAccess soa(Thread::Current());
996 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
997 oat_file,
998 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800999 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001000 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1001 }
1002 return ret;
1003}
1004
Richard Uhler66d874d2015-01-15 09:37:19 -08001005} // namespace art
1006