blob: 218c490b352244a0d2a6184fb48c4f645477c7a4 [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,
Andreas Gampe29d38e72016-03-23 15:31:51 +000067 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080068 bool load_executable)
Andreas Gampe29d38e72016-03-23 15:31:51 +000069 : OatFileAssistant(dex_location, nullptr, isa, profile_changed, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000070{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080071
72OatFileAssistant::OatFileAssistant(const char* dex_location,
73 const char* oat_location,
74 const InstructionSet isa,
Andreas Gampe29d38e72016-03-23 15:31:51 +000075 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080076 bool load_executable)
Andreas Gampe29d38e72016-03-23 15:31:51 +000077 : isa_(isa), profile_changed_(profile_changed), load_executable_(load_executable) {
Richard Uhler740eec92015-10-15 15:12:23 -070078 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
79 dex_location_.assign(dex_location);
80
Richard Uhler66d874d2015-01-15 09:37:19 -080081 if (load_executable_ && isa != kRuntimeISA) {
82 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
83 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
84 load_executable_ = false;
85 }
86
87 // If the user gave a target oat location, save that as the cached oat
88 // location now so we won't try to construct the default location later.
89 if (oat_location != nullptr) {
90 cached_oat_file_name_ = std::string(oat_location);
91 cached_oat_file_name_attempted_ = true;
92 cached_oat_file_name_found_ = true;
93 }
Richard Uhler66d874d2015-01-15 09:37:19 -080094}
95
96OatFileAssistant::~OatFileAssistant() {
97 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -070098 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010099 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800100 }
101}
102
103bool OatFileAssistant::IsInBootClassPath() {
104 // Note: We check the current boot class path, regardless of the ISA
105 // specified by the user. This is okay, because the boot class path should
106 // be the same for all ISAs.
107 // TODO: Can we verify the boot class path is the same for all ISAs?
108 Runtime* runtime = Runtime::Current();
109 ClassLinker* class_linker = runtime->GetClassLinker();
110 const auto& boot_class_path = class_linker->GetBootClassPath();
111 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700112 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800113 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
114 return true;
115 }
116 }
117 return false;
118}
119
120bool OatFileAssistant::Lock(std::string* error_msg) {
121 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700122 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800123
124 if (OatFileName() == nullptr) {
125 *error_msg = "Failed to determine lock file";
126 return false;
127 }
128 std::string lock_file_name = *OatFileName() + ".flock";
129
Richard Uhler581f4e92015-05-07 10:19:35 -0700130 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100131 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800132 return false;
133 }
134 return true;
135}
136
Andreas Gampe29d38e72016-03-23 15:31:51 +0000137bool OatFileAssistant::OatFileCompilerFilterIsOkay(CompilerFilter::Filter target) {
138 const OatFile* oat_file = GetOatFile();
139 if (oat_file != nullptr) {
140 CompilerFilter::Filter current = oat_file->GetCompilerFilter();
141 return CompilerFilter::IsAsGoodAs(current, target);
142 }
143 return false;
Calin Juravleb077e152016-02-18 18:47:37 +0000144}
Richard Uhler66d874d2015-01-15 09:37:19 -0800145
Andreas Gampe29d38e72016-03-23 15:31:51 +0000146bool OatFileAssistant::OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target) {
147 const OatFile* odex_file = GetOdexFile();
148 if (odex_file != nullptr) {
149 CompilerFilter::Filter current = odex_file->GetCompilerFilter();
150 return CompilerFilter::IsAsGoodAs(current, target);
151 }
152 return false;
153}
154
155OatFileAssistant::DexOptNeeded OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target) {
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100156 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000157
158 // See if the oat file is in good shape as is.
159 bool oat_okay = OatFileCompilerFilterIsOkay(target);
160 if (oat_okay) {
161 if (compilation_desired) {
162 if (OatFileIsUpToDate()) {
163 return kNoDexOptNeeded;
164 }
165 } else {
166 if (!OatFileIsOutOfDate()) {
167 return kNoDexOptNeeded;
168 }
169 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800170 }
Richard Uhler95abd042015-03-24 09:51:28 -0700171
Andreas Gampe29d38e72016-03-23 15:31:51 +0000172 // See if the odex file is in good shape as is.
173 bool odex_okay = OdexFileCompilerFilterIsOkay(target);
174 if (odex_okay) {
175 if (compilation_desired) {
176 if (OdexFileIsUpToDate()) {
177 return kNoDexOptNeeded;
178 }
179 } else {
180 if (!OdexFileIsOutOfDate()) {
181 return kNoDexOptNeeded;
182 }
183 }
Richard Uhler95abd042015-03-24 09:51:28 -0700184 }
185
Andreas Gampe29d38e72016-03-23 15:31:51 +0000186 // See if we can get an up-to-date file by running patchoat.
187 if (compilation_desired) {
Richard Uhlerd1537b52016-03-29 13:27:41 -0700188 if (odex_okay && OdexFileNeedsRelocation() && OdexFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000189 return kPatchOatNeeded;
190 }
191
Richard Uhlerd1537b52016-03-29 13:27:41 -0700192 if (oat_okay && OatFileNeedsRelocation() && OatFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000193 return kSelfPatchOatNeeded;
194 }
Richard Uhler95abd042015-03-24 09:51:28 -0700195 }
196
Andreas Gampe29d38e72016-03-23 15:31:51 +0000197 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700198 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800199}
200
Richard Uhlerf4b34872016-04-13 11:03:46 -0700201// Figure out the currently specified compile filter option in the runtime.
202// Returns true on success, false if the compiler filter is invalid, in which
203// case error_msg describes the problem.
204static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
205 std::string* error_msg) {
206 CHECK(filter != nullptr);
207 CHECK(error_msg != nullptr);
208
209 *filter = CompilerFilter::kDefaultCompilerFilter;
210 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
211 if (option.starts_with("--compiler-filter=")) {
212 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
213 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
214 *error_msg = std::string("Unknown --compiler-filter value: ")
215 + std::string(compiler_filter_string);
216 return false;
217 }
218 }
219 }
220 return true;
221}
222
Richard Uhler01be6812016-05-17 10:34:52 -0700223bool OatFileAssistant::IsUpToDate() {
224 return OatFileIsUpToDate() || OdexFileIsUpToDate();
225}
226
Richard Uhler1e860612016-03-30 12:17:55 -0700227OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700228OatFileAssistant::MakeUpToDate(std::string* error_msg) {
229 CompilerFilter::Filter target;
230 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
231 return kUpdateNotAttempted;
232 }
233
Andreas Gampe29d38e72016-03-23 15:31:51 +0000234 switch (GetDexOptNeeded(target)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700235 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700236 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700237 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
238 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800239 }
240 UNREACHABLE();
241}
242
243std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700244 // The best oat files are, in descending order of bestness:
245 // 1. Properly relocated files. These may be opened executable.
246 // 2. Not out-of-date files that are already opened non-executable.
247 // 3. Not out-of-date files that we must reopen non-executable.
248
Richard Uhler66d874d2015-01-15 09:37:19 -0800249 if (OatFileIsUpToDate()) {
250 oat_file_released_ = true;
251 return std::move(cached_oat_file_);
252 }
253
254 if (OdexFileIsUpToDate()) {
255 oat_file_released_ = true;
256 return std::move(cached_odex_file_);
257 }
258
Richard Uhler5f946da2015-07-17 12:28:32 -0700259 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
260 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800261
Richard Uhler5f946da2015-07-17 12:28:32 -0700262 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
263 oat_file_released_ = true;
264 return std::move(cached_oat_file_);
265 }
266
267 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
268 oat_file_released_ = true;
269 return std::move(cached_odex_file_);
270 }
271
272 if (!OatFileIsOutOfDate()) {
273 load_executable_ = false;
274 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800275 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700276 CHECK(!OatFileIsExecutable());
277 oat_file_released_ = true;
278 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800279 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700280 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800281
Richard Uhler5f946da2015-07-17 12:28:32 -0700282 if (!OdexFileIsOutOfDate()) {
283 load_executable_ = false;
284 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700286 CHECK(!OdexFileIsExecutable());
287 oat_file_released_ = true;
288 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800289 }
290 }
291
292 return std::unique_ptr<OatFile>();
293}
294
295std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
296 const OatFile& oat_file, const char* dex_location) {
297 std::vector<std::unique_ptr<const DexFile>> dex_files;
298
299 // Load the primary dex file.
300 std::string error_msg;
301 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
302 dex_location, nullptr, false);
303 if (oat_dex_file == nullptr) {
304 LOG(WARNING) << "Attempt to load out-of-date oat file "
305 << oat_file.GetLocation() << " for dex location " << dex_location;
306 return std::vector<std::unique_ptr<const DexFile>>();
307 }
308
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700309 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800310 if (dex_file.get() == nullptr) {
311 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
312 return std::vector<std::unique_ptr<const DexFile>>();
313 }
314 dex_files.push_back(std::move(dex_file));
315
316 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700317 for (size_t i = 1; ; i++) {
318 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800319 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700320 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800321 // There are no more secondary dex files to load.
322 break;
323 }
324
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700325 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800326 if (dex_file.get() == nullptr) {
327 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
328 return std::vector<std::unique_ptr<const DexFile>>();
329 }
330 dex_files.push_back(std::move(dex_file));
331 }
332 return dex_files;
333}
334
Richard Uhler9b994ea2015-06-24 08:44:19 -0700335bool OatFileAssistant::HasOriginalDexFiles() {
336 // Ensure GetRequiredDexChecksum has been run so that
337 // has_original_dex_files_ is initialized. We don't care about the result of
338 // GetRequiredDexChecksum.
339 GetRequiredDexChecksum();
340 return has_original_dex_files_;
341}
342
Richard Uhler66d874d2015-01-15 09:37:19 -0800343const std::string* OatFileAssistant::OdexFileName() {
344 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800345 cached_odex_file_name_attempted_ = true;
346
347 std::string error_msg;
348 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700349 dex_location_, isa_, &cached_odex_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800350 if (!cached_odex_file_name_found_) {
351 // If we can't figure out the odex file, we treat it as if the odex
352 // file was inaccessible.
353 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
354 }
355 }
356 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
357}
358
359bool OatFileAssistant::OdexFileExists() {
360 return GetOdexFile() != nullptr;
361}
362
Richard Uhler95abd042015-03-24 09:51:28 -0700363OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800364 if (OdexFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700365 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800366 }
367 if (OdexFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700368 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 }
Richard Uhler95abd042015-03-24 09:51:28 -0700370 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800371}
372
373bool OatFileAssistant::OdexFileIsOutOfDate() {
374 if (!odex_file_is_out_of_date_attempted_) {
375 odex_file_is_out_of_date_attempted_ = true;
376 const OatFile* odex_file = GetOdexFile();
377 if (odex_file == nullptr) {
378 cached_odex_file_is_out_of_date_ = true;
379 } else {
380 cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file);
381 }
382 }
383 return cached_odex_file_is_out_of_date_;
384}
385
386bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700387 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800388}
389
390bool OatFileAssistant::OdexFileIsUpToDate() {
391 if (!odex_file_is_up_to_date_attempted_) {
392 odex_file_is_up_to_date_attempted_ = true;
393 const OatFile* odex_file = GetOdexFile();
394 if (odex_file == nullptr) {
395 cached_odex_file_is_up_to_date_ = false;
396 } else {
397 cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
398 }
399 }
400 return cached_odex_file_is_up_to_date_;
401}
402
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100403CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
404 const OatFile* odex_file = GetOdexFile();
405 CHECK(odex_file != nullptr);
406
407 return odex_file->GetCompilerFilter();
408}
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800409std::string OatFileAssistant::ArtFileName(const OatFile* oat_file) const {
410 const std::string oat_file_location = oat_file->GetLocation();
411 // Replace extension with .art
412 const size_t last_ext = oat_file_location.find_last_of('.');
413 if (last_ext == std::string::npos) {
414 LOG(ERROR) << "No extension in oat file " << oat_file_location;
415 return std::string();
416 }
417 return oat_file_location.substr(0, last_ext) + ".art";
418}
419
Richard Uhler66d874d2015-01-15 09:37:19 -0800420const std::string* OatFileAssistant::OatFileName() {
421 if (!cached_oat_file_name_attempted_) {
422 cached_oat_file_name_attempted_ = true;
423
424 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800425 // TODO: The oat file assistant should be the definitive place for
426 // determining the oat file name from the dex location, not
427 // GetDalvikCacheFilename.
428 std::string cache_dir = StringPrintf("%s%s",
429 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
430 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700431 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700432 cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800433 if (!cached_oat_file_name_found_) {
434 // If we can't determine the oat file name, we treat the oat file as
435 // inaccessible.
436 LOG(WARNING) << "Failed to determine oat file name for dex location "
437 << dex_location_ << ": " << error_msg;
438 }
439 }
440 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
441}
442
443bool OatFileAssistant::OatFileExists() {
444 return GetOatFile() != nullptr;
445}
446
Richard Uhler95abd042015-03-24 09:51:28 -0700447OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800448 if (OatFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700449 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800450 }
451 if (OatFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700452 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800453 }
Richard Uhler95abd042015-03-24 09:51:28 -0700454 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800455}
456
457bool OatFileAssistant::OatFileIsOutOfDate() {
458 if (!oat_file_is_out_of_date_attempted_) {
459 oat_file_is_out_of_date_attempted_ = true;
460 const OatFile* oat_file = GetOatFile();
461 if (oat_file == nullptr) {
462 cached_oat_file_is_out_of_date_ = true;
463 } else {
464 cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file);
465 }
466 }
467 return cached_oat_file_is_out_of_date_;
468}
469
470bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700471 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800472}
473
474bool OatFileAssistant::OatFileIsUpToDate() {
475 if (!oat_file_is_up_to_date_attempted_) {
476 oat_file_is_up_to_date_attempted_ = true;
477 const OatFile* oat_file = GetOatFile();
478 if (oat_file == nullptr) {
479 cached_oat_file_is_up_to_date_ = false;
480 } else {
481 cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
482 }
483 }
484 return cached_oat_file_is_up_to_date_;
485}
486
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100487CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
488 const OatFile* oat_file = GetOatFile();
489 CHECK(oat_file != nullptr);
490
491 return oat_file->GetCompilerFilter();
492}
493
Richard Uhler95abd042015-03-24 09:51:28 -0700494OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800495 // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which
496 // is more work than we need to do. If performance becomes a concern, and
497 // this method is actually called, this should be fixed.
498 if (GivenOatFileIsOutOfDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700499 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800500 }
501 if (GivenOatFileIsUpToDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700502 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800503 }
Richard Uhler95abd042015-03-24 09:51:28 -0700504 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800505}
506
507bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) {
508 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700509 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800510 // what we provide, which verifies the primary dex checksum for us.
511 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
512 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700513 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700514 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800515 return true;
516 }
517
518 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700519 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800520 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700521 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800522 const OatFile::OatDexFile* secondary_oat_dex_file
523 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700524 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800525 // There are no more secondary dex files to check.
526 break;
527 }
528
529 std::string error_msg;
530 uint32_t expected_secondary_checksum = 0;
531 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700532 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800533 uint32_t actual_secondary_checksum
534 = secondary_oat_dex_file->GetDexFileLocationChecksum();
535 if (expected_secondary_checksum != actual_secondary_checksum) {
536 VLOG(oat) << "Dex checksum does not match for secondary dex: "
537 << secondary_dex_location
538 << ". Expected: " << expected_secondary_checksum
539 << ", Actual: " << actual_secondary_checksum;
Richard Uhler67ff7d12015-05-14 13:21:13 -0700540 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800541 }
542 } else {
543 // If we can't get the checksum for the secondary location, we assume
544 // the dex checksum is up to date for this and all other secondary dex
545 // files.
546 break;
547 }
548 }
549
Andreas Gampe29d38e72016-03-23 15:31:51 +0000550 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
551 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000552
Richard Uhler66d874d2015-01-15 09:37:19 -0800553 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000554 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
555 const ImageInfo* image_info = GetImageInfo();
556 if (image_info == nullptr) {
557 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000558
Richard Uhler76f5cb62016-04-04 13:30:16 -0700559 if (HasOriginalDexFiles()) {
560 return true;
561 }
562
563 // If there is no original dex file to fall back to, grudgingly accept
564 // the oat file. This could technically lead to crashes, but there's no
565 // way we could find a better oat file to use for this dex location,
566 // and it's better than being stuck in a boot loop with no way out.
567 // The problem will hopefully resolve itself the next time the runtime
568 // starts up.
569 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
570 << "Allow oat file use. This is potentially dangerous.";
571 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
572 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000573 VLOG(oat) << "Oat image checksum does not match image checksum.";
574 return true;
575 }
576 } else {
577 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800578 }
579
Andreas Gampe29d38e72016-03-23 15:31:51 +0000580 // Verify the profile hasn't changed recently.
581 // TODO: Move this check to OatFileCompilerFilterIsOkay? Nothing bad should
582 // happen if we use an oat file compiled with an out-of-date profile.
583 if (CompilerFilter::DependsOnProfile(current_compiler_filter)) {
584 if (profile_changed_) {
585 VLOG(oat) << "The profile has changed recently.";
586 return true;
587 }
588 } else {
589 VLOG(oat) << "Profile check skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800590 }
591
Andreas Gampe29d38e72016-03-23 15:31:51 +0000592 // Everything looks good; the dex file is not out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800593 return false;
594}
595
596bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700597 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800598}
599
600bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
601 if (GivenOatFileIsOutOfDate(file)) {
602 return false;
603 }
604
Andreas Gampe29d38e72016-03-23 15:31:51 +0000605 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
Richard Uhlera62d2f02016-03-18 15:05:30 -0700606
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100607 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000608 if (!file.IsPic()) {
609 const ImageInfo* image_info = GetImageInfo();
610 if (image_info == nullptr) {
611 VLOG(oat) << "No image to check oat relocation against.";
612 return false;
613 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700614
Andreas Gampe29d38e72016-03-23 15:31:51 +0000615 // Verify the oat_data_begin recorded for the image in the oat file matches
616 // the actual oat_data_begin for boot.oat in the image.
617 const OatHeader& oat_header = file.GetOatHeader();
618 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
619 if (oat_data_begin != image_info->oat_data_begin) {
620 VLOG(oat) << file.GetLocation() <<
621 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
622 << " does not match actual image oat_data_begin ("
623 << image_info->oat_data_begin << ")";
624 return false;
625 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700626
Andreas Gampe29d38e72016-03-23 15:31:51 +0000627 // Verify the oat_patch_delta recorded for the image in the oat file matches
628 // the actual oat_patch_delta for the image.
629 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
630 if (oat_patch_delta != image_info->patch_delta) {
631 VLOG(oat) << file.GetLocation() <<
632 ": Oat file image patch delta (" << oat_patch_delta << ")"
633 << " does not match actual image patch delta ("
634 << image_info->patch_delta << ")";
635 return false;
636 }
637 } else {
638 // Oat files compiled in PIC mode do not require relocation.
639 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
640 }
641 } else {
642 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800643 }
644 return true;
645}
646
Richard Uhler1e860612016-03-30 12:17:55 -0700647OatFileAssistant::ResultOfAttemptToUpdate
648OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800649 CHECK(error_msg != nullptr);
650
Richard Uhler95abd042015-03-24 09:51:28 -0700651 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700652 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700653 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700654 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800655 }
Richard Uhler95abd042015-03-24 09:51:28 -0700656 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800657
658 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700659 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800660 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700661 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800662 }
663 const std::string& oat_file_name = *OatFileName();
664
665 const ImageInfo* image_info = GetImageInfo();
666 Runtime* runtime = Runtime::Current();
667 if (image_info == nullptr) {
668 *error_msg = "Patching of oat file " + oat_file_name
669 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700670 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800671 }
672
673 if (!runtime->IsDex2OatEnabled()) {
674 *error_msg = "Patching of oat file " + oat_file_name
675 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700676 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800677 }
678
679 std::vector<std::string> argv;
680 argv.push_back(runtime->GetPatchoatExecutable());
681 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700682 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800683 argv.push_back("--output-oat-file=" + oat_file_name);
684 argv.push_back("--patched-image-location=" + image_info->location);
685
686 std::string command_line(Join(argv, ' '));
687 if (!Exec(argv, error_msg)) {
688 // Manually delete the file. This ensures there is no garbage left over if
689 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100690 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700691 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800692 }
693
694 // Mark that the oat file has changed and we should try to reload.
695 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700696 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800697}
698
Richard Uhler1e860612016-03-30 12:17:55 -0700699OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700700OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800701 CHECK(error_msg != nullptr);
702
Richard Uhler8327cf72015-10-13 16:34:59 -0700703 Runtime* runtime = Runtime::Current();
704 if (!runtime->IsDex2OatEnabled()) {
705 *error_msg = "Generation of oat file for dex location " + dex_location_
706 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700707 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700708 }
709
Richard Uhler66d874d2015-01-15 09:37:19 -0800710 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700711 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800712 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700713 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800714 }
715 const std::string& oat_file_name = *OatFileName();
716
Richard Uhler66d874d2015-01-15 09:37:19 -0800717 // dex2oat ignores missing dex files and doesn't report an error.
718 // Check explicitly here so we can detect the error properly.
719 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700720 if (!OS::FileExists(dex_location_.c_str())) {
721 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700722 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800723 }
724
Richard Uhler8327cf72015-10-13 16:34:59 -0700725 std::unique_ptr<File> oat_file;
726 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
727 if (oat_file.get() == nullptr) {
728 *error_msg = "Generation of oat file " + oat_file_name
729 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700730 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700731 }
732
733 if (fchmod(oat_file->Fd(), 0644) != 0) {
734 *error_msg = "Generation of oat file " + oat_file_name
735 + " not attempted because the oat file could not be made world readable.";
736 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700737 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700738 }
739
740 std::vector<std::string> args;
741 args.push_back("--dex-file=" + dex_location_);
742 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
743 args.push_back("--oat-location=" + oat_file_name);
744
Richard Uhler66d874d2015-01-15 09:37:19 -0800745 if (!Dex2Oat(args, error_msg)) {
746 // Manually delete the file. This ensures there is no garbage left over if
747 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700748 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100749 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700750 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700751 }
752
753 if (oat_file->FlushCloseOrErase() != 0) {
754 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100755 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700756 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800757 }
758
759 // Mark that the oat file has changed and we should try to reload.
760 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700761 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800762}
763
764bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
765 std::string* error_msg) {
766 Runtime* runtime = Runtime::Current();
767 std::string image_location = ImageLocation();
768 if (image_location.empty()) {
769 *error_msg = "No image location found for Dex2Oat.";
770 return false;
771 }
772
773 std::vector<std::string> argv;
774 argv.push_back(runtime->GetCompilerExecutable());
775 argv.push_back("--runtime-arg");
776 argv.push_back("-classpath");
777 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700778 std::string class_path = runtime->GetClassPathString();
779 if (class_path == "") {
780 class_path = OatFile::kSpecialSharedLibrary;
781 }
782 argv.push_back(class_path);
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100783 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200784 argv.push_back("--debuggable");
785 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700786 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800787
788 if (!runtime->IsVerificationEnabled()) {
789 argv.push_back("--compiler-filter=verify-none");
790 }
791
792 if (runtime->MustRelocateIfPossible()) {
793 argv.push_back("--runtime-arg");
794 argv.push_back("-Xrelocate");
795 } else {
796 argv.push_back("--runtime-arg");
797 argv.push_back("-Xnorelocate");
798 }
799
800 if (!kIsTargetBuild) {
801 argv.push_back("--host");
802 }
803
804 argv.push_back("--boot-image=" + image_location);
805
806 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
807 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
808
809 argv.insert(argv.end(), args.begin(), args.end());
810
811 std::string command_line(Join(argv, ' '));
812 return Exec(argv, error_msg);
813}
814
815bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
816 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
817 CHECK(odex_filename != nullptr);
818 CHECK(error_msg != nullptr);
819
820 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700821 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800822 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700823 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800824
Richard Uhler63434112015-03-16 14:32:16 -0700825 // Find the directory portion of the dex location and add the oat/<isa>
826 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800827 size_t pos = location.rfind('/');
828 if (pos == std::string::npos) {
829 *error_msg = "Dex location " + location + " has no directory.";
830 return false;
831 }
832 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700833 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800834
835 // Find the file portion of the dex location.
836 std::string file;
837 if (pos == std::string::npos) {
838 file = location;
839 } else {
840 file = location.substr(pos+1);
841 }
842
843 // Get the base part of the file without the extension.
844 pos = file.rfind('.');
845 if (pos == std::string::npos) {
846 *error_msg = "Dex location " + location + " has no extension.";
847 return false;
848 }
849 std::string base = file.substr(0, pos);
850
851 *odex_filename = dir + "/" + base + ".odex";
852 return true;
853}
854
855std::string OatFileAssistant::DalvikCacheDirectory() {
856 // Note: We don't cache this, because it will only be called once by
Andreas Gampe29d38e72016-03-23 15:31:51 +0000857 // OatFileName.
Richard Uhler66d874d2015-01-15 09:37:19 -0800858
859 // TODO: The work done in GetDalvikCache is overkill for what we need.
860 // Ideally a new API for getting the DalvikCacheDirectory the way we want
861 // (without existence testing, creation, or death) is provided with the rest
862 // of the GetDalvikCache family of functions. Until such an API is in place,
863 // we use GetDalvikCache to avoid duplicating the logic for determining the
864 // dalvik cache directory.
865 std::string result;
866 bool have_android_data;
867 bool dalvik_cache_exists;
868 bool is_global_cache;
869 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
870 return result;
871}
872
Richard Uhler66d874d2015-01-15 09:37:19 -0800873std::string OatFileAssistant::ImageLocation() {
874 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000875 const std::vector<gc::space::ImageSpace*>& image_spaces =
876 runtime->GetHeap()->GetBootImageSpaces();
877 if (image_spaces.empty()) {
878 return "";
879 }
880 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800881}
882
883const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700884 if (!required_dex_checksum_attempted_) {
885 required_dex_checksum_attempted_ = true;
886 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800887 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700888 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700889 required_dex_checksum_found_ = true;
890 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800891 } else {
892 // This can happen if the original dex file has been stripped from the
893 // apk.
894 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700895 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800896
897 // Get the checksum from the odex if we can.
898 const OatFile* odex_file = GetOdexFile();
899 if (odex_file != nullptr) {
900 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700901 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800902 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700903 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
904 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800905 }
906 }
907 }
908 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700909 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800910}
911
912const OatFile* OatFileAssistant::GetOdexFile() {
913 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
914 if (!odex_file_load_attempted_) {
915 odex_file_load_attempted_ = true;
916 if (OdexFileName() != nullptr) {
917 const std::string& odex_file_name = *OdexFileName();
918 std::string error_msg;
919 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800920 odex_file_name.c_str(),
921 nullptr,
922 nullptr,
923 load_executable_,
924 /*low_4gb*/false,
925 dex_location_.c_str(),
926 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800927 if (cached_odex_file_.get() == nullptr) {
928 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
929 << odex_file_name << ": " << error_msg;
930 }
931 }
932 }
933 return cached_odex_file_.get();
934}
935
Richard Uhler5f946da2015-07-17 12:28:32 -0700936bool OatFileAssistant::OdexFileIsExecutable() {
937 const OatFile* odex_file = GetOdexFile();
938 return (odex_file != nullptr && odex_file->IsExecutable());
939}
940
Richard Uhlerd1537b52016-03-29 13:27:41 -0700941bool OatFileAssistant::OdexFileHasPatchInfo() {
942 const OatFile* odex_file = GetOdexFile();
943 return (odex_file != nullptr && odex_file->HasPatchInfo());
944}
945
Richard Uhler66d874d2015-01-15 09:37:19 -0800946void OatFileAssistant::ClearOdexFileCache() {
947 odex_file_load_attempted_ = false;
948 cached_odex_file_.reset();
949 odex_file_is_out_of_date_attempted_ = false;
950 odex_file_is_up_to_date_attempted_ = false;
951}
952
953const OatFile* OatFileAssistant::GetOatFile() {
954 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
955 if (!oat_file_load_attempted_) {
956 oat_file_load_attempted_ = true;
957 if (OatFileName() != nullptr) {
958 const std::string& oat_file_name = *OatFileName();
959 std::string error_msg;
960 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800961 oat_file_name.c_str(),
962 nullptr,
963 nullptr,
964 load_executable_,
965 /*low_4gb*/false,
966 dex_location_.c_str(),
967 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800968 if (cached_oat_file_.get() == nullptr) {
969 VLOG(oat) << "OatFileAssistant test for existing oat file "
970 << oat_file_name << ": " << error_msg;
971 }
972 }
973 }
974 return cached_oat_file_.get();
975}
976
Richard Uhler5f946da2015-07-17 12:28:32 -0700977bool OatFileAssistant::OatFileIsExecutable() {
978 const OatFile* oat_file = GetOatFile();
979 return (oat_file != nullptr && oat_file->IsExecutable());
980}
981
Richard Uhlerd1537b52016-03-29 13:27:41 -0700982bool OatFileAssistant::OatFileHasPatchInfo() {
983 const OatFile* oat_file = GetOatFile();
984 return (oat_file != nullptr && oat_file->HasPatchInfo());
985}
986
Richard Uhler66d874d2015-01-15 09:37:19 -0800987void OatFileAssistant::ClearOatFileCache() {
988 oat_file_load_attempted_ = false;
989 cached_oat_file_.reset();
990 oat_file_is_out_of_date_attempted_ = false;
991 oat_file_is_up_to_date_attempted_ = false;
992}
993
994const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
995 if (!image_info_load_attempted_) {
996 image_info_load_attempted_ = true;
997
998 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800999 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
1000 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001001 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -08001002
1003 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001004 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -08001005 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -08001006 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
1007 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -08001008 cached_image_info_.patch_delta = image_header.GetPatchDelta();
1009 } else {
1010 std::unique_ptr<ImageHeader> image_header(
Jeff Haob11ffb72016-04-07 15:40:54 -07001011 gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_));
Richard Uhler66d874d2015-01-15 09:37:19 -08001012 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -08001013 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
1014 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -08001015 cached_image_info_.patch_delta = image_header->GetPatchDelta();
1016 }
1017 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001018 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -07001019
Jeff Haofd336c32016-04-07 19:46:31 -07001020 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -08001021 }
1022 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
1023}
1024
Jeff Haob11ffb72016-04-07 15:40:54 -07001025// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -07001026uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -07001027 uint32_t checksum = 0;
1028 std::vector<gc::space::ImageSpace*> image_spaces =
1029 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -07001030 if (isa == kRuntimeISA) {
1031 for (gc::space::ImageSpace* image_space : image_spaces) {
1032 checksum ^= image_space->GetImageHeader().GetOatChecksum();
1033 }
1034 } else {
1035 for (gc::space::ImageSpace* image_space : image_spaces) {
1036 std::string location = image_space->GetImageLocation();
1037 std::unique_ptr<ImageHeader> image_header(
1038 gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa));
1039 checksum ^= image_header->GetOatChecksum();
1040 }
Jeff Haob11ffb72016-04-07 15:40:54 -07001041 }
1042 return checksum;
1043}
1044
1045uint32_t OatFileAssistant::GetCombinedImageChecksum() {
1046 if (!image_info_load_attempted_) {
1047 GetImageInfo();
1048 }
1049 return combined_image_checksum_;
1050}
1051
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001052gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
1053 DCHECK(oat_file != nullptr);
1054 std::string art_file = ArtFileName(oat_file);
1055 if (art_file.empty()) {
1056 return nullptr;
1057 }
1058 std::string error_msg;
1059 ScopedObjectAccess soa(Thread::Current());
1060 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
1061 oat_file,
1062 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001063 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001064 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1065 }
1066 return ret;
1067}
1068
Richard Uhler66d874d2015-01-15 09:37:19 -08001069} // namespace art
1070