blob: a508e87c8758936d45acf4ac900c7c4c823f5a5a [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"
33#include "class_linker.h"
34#include "gc/heap.h"
35#include "gc/space/image_space.h"
36#include "image.h"
37#include "oat.h"
38#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080039#include "runtime.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080040#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041#include "ScopedFd.h"
42#include "utils.h"
43
44namespace art {
45
46OatFileAssistant::OatFileAssistant(const char* dex_location,
47 const InstructionSet isa,
Andreas Gampe29d38e72016-03-23 15:31:51 +000048 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080049 bool load_executable)
Andreas Gampe29d38e72016-03-23 15:31:51 +000050 : OatFileAssistant(dex_location, nullptr, isa, profile_changed, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000051{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080052
53OatFileAssistant::OatFileAssistant(const char* dex_location,
54 const char* oat_location,
55 const InstructionSet isa,
Andreas Gampe29d38e72016-03-23 15:31:51 +000056 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080057 bool load_executable)
Andreas Gampe29d38e72016-03-23 15:31:51 +000058 : isa_(isa), profile_changed_(profile_changed), load_executable_(load_executable) {
Richard Uhler740eec92015-10-15 15:12:23 -070059 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
60 dex_location_.assign(dex_location);
61
Richard Uhler66d874d2015-01-15 09:37:19 -080062 if (load_executable_ && isa != kRuntimeISA) {
63 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
64 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
65 load_executable_ = false;
66 }
67
68 // If the user gave a target oat location, save that as the cached oat
69 // location now so we won't try to construct the default location later.
70 if (oat_location != nullptr) {
71 cached_oat_file_name_ = std::string(oat_location);
72 cached_oat_file_name_attempted_ = true;
73 cached_oat_file_name_found_ = true;
74 }
Richard Uhler66d874d2015-01-15 09:37:19 -080075}
76
77OatFileAssistant::~OatFileAssistant() {
78 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -070079 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010080 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -080081 }
82}
83
84bool OatFileAssistant::IsInBootClassPath() {
85 // Note: We check the current boot class path, regardless of the ISA
86 // specified by the user. This is okay, because the boot class path should
87 // be the same for all ISAs.
88 // TODO: Can we verify the boot class path is the same for all ISAs?
89 Runtime* runtime = Runtime::Current();
90 ClassLinker* class_linker = runtime->GetClassLinker();
91 const auto& boot_class_path = class_linker->GetBootClassPath();
92 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -070093 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -080094 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
95 return true;
96 }
97 }
98 return false;
99}
100
101bool OatFileAssistant::Lock(std::string* error_msg) {
102 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700103 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800104
105 if (OatFileName() == nullptr) {
106 *error_msg = "Failed to determine lock file";
107 return false;
108 }
109 std::string lock_file_name = *OatFileName() + ".flock";
110
Richard Uhler581f4e92015-05-07 10:19:35 -0700111 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100112 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800113 return false;
114 }
115 return true;
116}
117
Andreas Gampe29d38e72016-03-23 15:31:51 +0000118bool OatFileAssistant::OatFileCompilerFilterIsOkay(CompilerFilter::Filter target) {
119 const OatFile* oat_file = GetOatFile();
120 if (oat_file != nullptr) {
121 CompilerFilter::Filter current = oat_file->GetCompilerFilter();
122 return CompilerFilter::IsAsGoodAs(current, target);
123 }
124 return false;
Calin Juravleb077e152016-02-18 18:47:37 +0000125}
Richard Uhler66d874d2015-01-15 09:37:19 -0800126
Andreas Gampe29d38e72016-03-23 15:31:51 +0000127bool OatFileAssistant::OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target) {
128 const OatFile* odex_file = GetOdexFile();
129 if (odex_file != nullptr) {
130 CompilerFilter::Filter current = odex_file->GetCompilerFilter();
131 return CompilerFilter::IsAsGoodAs(current, target);
132 }
133 return false;
134}
135
136OatFileAssistant::DexOptNeeded OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target) {
137 bool compilation_desired = CompilerFilter::IsCompilationEnabled(target);
138
139 // See if the oat file is in good shape as is.
140 bool oat_okay = OatFileCompilerFilterIsOkay(target);
141 if (oat_okay) {
142 if (compilation_desired) {
143 if (OatFileIsUpToDate()) {
144 return kNoDexOptNeeded;
145 }
146 } else {
147 if (!OatFileIsOutOfDate()) {
148 return kNoDexOptNeeded;
149 }
150 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800151 }
Richard Uhler95abd042015-03-24 09:51:28 -0700152
Andreas Gampe29d38e72016-03-23 15:31:51 +0000153 // See if the odex file is in good shape as is.
154 bool odex_okay = OdexFileCompilerFilterIsOkay(target);
155 if (odex_okay) {
156 if (compilation_desired) {
157 if (OdexFileIsUpToDate()) {
158 return kNoDexOptNeeded;
159 }
160 } else {
161 if (!OdexFileIsOutOfDate()) {
162 return kNoDexOptNeeded;
163 }
164 }
Richard Uhler95abd042015-03-24 09:51:28 -0700165 }
166
Andreas Gampe29d38e72016-03-23 15:31:51 +0000167 // See if we can get an up-to-date file by running patchoat.
168 if (compilation_desired) {
Richard Uhlerd1537b52016-03-29 13:27:41 -0700169 if (odex_okay && OdexFileNeedsRelocation() && OdexFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000170 return kPatchOatNeeded;
171 }
172
Richard Uhlerd1537b52016-03-29 13:27:41 -0700173 if (oat_okay && OatFileNeedsRelocation() && OatFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000174 return kSelfPatchOatNeeded;
175 }
Richard Uhler95abd042015-03-24 09:51:28 -0700176 }
177
Andreas Gampe29d38e72016-03-23 15:31:51 +0000178 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700179 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800180}
181
Richard Uhlerf4b34872016-04-13 11:03:46 -0700182// Figure out the currently specified compile filter option in the runtime.
183// Returns true on success, false if the compiler filter is invalid, in which
184// case error_msg describes the problem.
185static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
186 std::string* error_msg) {
187 CHECK(filter != nullptr);
188 CHECK(error_msg != nullptr);
189
190 *filter = CompilerFilter::kDefaultCompilerFilter;
191 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
192 if (option.starts_with("--compiler-filter=")) {
193 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
194 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
195 *error_msg = std::string("Unknown --compiler-filter value: ")
196 + std::string(compiler_filter_string);
197 return false;
198 }
199 }
200 }
201 return true;
202}
203
Richard Uhler1e860612016-03-30 12:17:55 -0700204OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700205OatFileAssistant::MakeUpToDate(std::string* error_msg) {
206 CompilerFilter::Filter target;
207 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
208 return kUpdateNotAttempted;
209 }
210
Andreas Gampe29d38e72016-03-23 15:31:51 +0000211 switch (GetDexOptNeeded(target)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700212 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700213 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700214 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
215 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800216 }
217 UNREACHABLE();
218}
219
220std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700221 // The best oat files are, in descending order of bestness:
222 // 1. Properly relocated files. These may be opened executable.
223 // 2. Not out-of-date files that are already opened non-executable.
224 // 3. Not out-of-date files that we must reopen non-executable.
225
Richard Uhler66d874d2015-01-15 09:37:19 -0800226 if (OatFileIsUpToDate()) {
227 oat_file_released_ = true;
228 return std::move(cached_oat_file_);
229 }
230
231 if (OdexFileIsUpToDate()) {
232 oat_file_released_ = true;
233 return std::move(cached_odex_file_);
234 }
235
Richard Uhler5f946da2015-07-17 12:28:32 -0700236 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
237 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800238
Richard Uhler5f946da2015-07-17 12:28:32 -0700239 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
240 oat_file_released_ = true;
241 return std::move(cached_oat_file_);
242 }
243
244 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
245 oat_file_released_ = true;
246 return std::move(cached_odex_file_);
247 }
248
249 if (!OatFileIsOutOfDate()) {
250 load_executable_ = false;
251 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800252 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700253 CHECK(!OatFileIsExecutable());
254 oat_file_released_ = true;
255 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800256 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700257 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800258
Richard Uhler5f946da2015-07-17 12:28:32 -0700259 if (!OdexFileIsOutOfDate()) {
260 load_executable_ = false;
261 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800262 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700263 CHECK(!OdexFileIsExecutable());
264 oat_file_released_ = true;
265 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800266 }
267 }
268
269 return std::unique_ptr<OatFile>();
270}
271
272std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
273 const OatFile& oat_file, const char* dex_location) {
274 std::vector<std::unique_ptr<const DexFile>> dex_files;
275
276 // Load the primary dex file.
277 std::string error_msg;
278 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
279 dex_location, nullptr, false);
280 if (oat_dex_file == nullptr) {
281 LOG(WARNING) << "Attempt to load out-of-date oat file "
282 << oat_file.GetLocation() << " for dex location " << dex_location;
283 return std::vector<std::unique_ptr<const DexFile>>();
284 }
285
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700286 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 if (dex_file.get() == nullptr) {
288 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
289 return std::vector<std::unique_ptr<const DexFile>>();
290 }
291 dex_files.push_back(std::move(dex_file));
292
293 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700294 for (size_t i = 1; ; i++) {
295 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700297 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800298 // There are no more secondary dex files to load.
299 break;
300 }
301
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700302 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 if (dex_file.get() == nullptr) {
304 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
305 return std::vector<std::unique_ptr<const DexFile>>();
306 }
307 dex_files.push_back(std::move(dex_file));
308 }
309 return dex_files;
310}
311
Richard Uhler9b994ea2015-06-24 08:44:19 -0700312bool OatFileAssistant::HasOriginalDexFiles() {
313 // Ensure GetRequiredDexChecksum has been run so that
314 // has_original_dex_files_ is initialized. We don't care about the result of
315 // GetRequiredDexChecksum.
316 GetRequiredDexChecksum();
317 return has_original_dex_files_;
318}
319
Richard Uhler66d874d2015-01-15 09:37:19 -0800320const std::string* OatFileAssistant::OdexFileName() {
321 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 cached_odex_file_name_attempted_ = true;
323
324 std::string error_msg;
325 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700326 dex_location_, isa_, &cached_odex_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800327 if (!cached_odex_file_name_found_) {
328 // If we can't figure out the odex file, we treat it as if the odex
329 // file was inaccessible.
330 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
331 }
332 }
333 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
334}
335
336bool OatFileAssistant::OdexFileExists() {
337 return GetOdexFile() != nullptr;
338}
339
Richard Uhler95abd042015-03-24 09:51:28 -0700340OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800341 if (OdexFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700342 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800343 }
344 if (OdexFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700345 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800346 }
Richard Uhler95abd042015-03-24 09:51:28 -0700347 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800348}
349
350bool OatFileAssistant::OdexFileIsOutOfDate() {
351 if (!odex_file_is_out_of_date_attempted_) {
352 odex_file_is_out_of_date_attempted_ = true;
353 const OatFile* odex_file = GetOdexFile();
354 if (odex_file == nullptr) {
355 cached_odex_file_is_out_of_date_ = true;
356 } else {
357 cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file);
358 }
359 }
360 return cached_odex_file_is_out_of_date_;
361}
362
363bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700364 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800365}
366
367bool OatFileAssistant::OdexFileIsUpToDate() {
368 if (!odex_file_is_up_to_date_attempted_) {
369 odex_file_is_up_to_date_attempted_ = true;
370 const OatFile* odex_file = GetOdexFile();
371 if (odex_file == nullptr) {
372 cached_odex_file_is_up_to_date_ = false;
373 } else {
374 cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
375 }
376 }
377 return cached_odex_file_is_up_to_date_;
378}
379
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800380std::string OatFileAssistant::ArtFileName(const OatFile* oat_file) const {
381 const std::string oat_file_location = oat_file->GetLocation();
382 // Replace extension with .art
383 const size_t last_ext = oat_file_location.find_last_of('.');
384 if (last_ext == std::string::npos) {
385 LOG(ERROR) << "No extension in oat file " << oat_file_location;
386 return std::string();
387 }
388 return oat_file_location.substr(0, last_ext) + ".art";
389}
390
Richard Uhler66d874d2015-01-15 09:37:19 -0800391const std::string* OatFileAssistant::OatFileName() {
392 if (!cached_oat_file_name_attempted_) {
393 cached_oat_file_name_attempted_ = true;
394
395 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800396 // TODO: The oat file assistant should be the definitive place for
397 // determining the oat file name from the dex location, not
398 // GetDalvikCacheFilename.
399 std::string cache_dir = StringPrintf("%s%s",
400 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
401 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700402 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700403 cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800404 if (!cached_oat_file_name_found_) {
405 // If we can't determine the oat file name, we treat the oat file as
406 // inaccessible.
407 LOG(WARNING) << "Failed to determine oat file name for dex location "
408 << dex_location_ << ": " << error_msg;
409 }
410 }
411 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
412}
413
414bool OatFileAssistant::OatFileExists() {
415 return GetOatFile() != nullptr;
416}
417
Richard Uhler95abd042015-03-24 09:51:28 -0700418OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 if (OatFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700420 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800421 }
422 if (OatFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700423 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800424 }
Richard Uhler95abd042015-03-24 09:51:28 -0700425 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800426}
427
428bool OatFileAssistant::OatFileIsOutOfDate() {
429 if (!oat_file_is_out_of_date_attempted_) {
430 oat_file_is_out_of_date_attempted_ = true;
431 const OatFile* oat_file = GetOatFile();
432 if (oat_file == nullptr) {
433 cached_oat_file_is_out_of_date_ = true;
434 } else {
435 cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file);
436 }
437 }
438 return cached_oat_file_is_out_of_date_;
439}
440
441bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700442 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800443}
444
445bool OatFileAssistant::OatFileIsUpToDate() {
446 if (!oat_file_is_up_to_date_attempted_) {
447 oat_file_is_up_to_date_attempted_ = true;
448 const OatFile* oat_file = GetOatFile();
449 if (oat_file == nullptr) {
450 cached_oat_file_is_up_to_date_ = false;
451 } else {
452 cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
453 }
454 }
455 return cached_oat_file_is_up_to_date_;
456}
457
Richard Uhler95abd042015-03-24 09:51:28 -0700458OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which
460 // is more work than we need to do. If performance becomes a concern, and
461 // this method is actually called, this should be fixed.
462 if (GivenOatFileIsOutOfDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700463 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800464 }
465 if (GivenOatFileIsUpToDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700466 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800467 }
Richard Uhler95abd042015-03-24 09:51:28 -0700468 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800469}
470
471bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) {
472 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700473 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800474 // what we provide, which verifies the primary dex checksum for us.
475 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
476 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700477 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700478 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800479 return true;
480 }
481
482 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700483 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700485 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 const OatFile::OatDexFile* secondary_oat_dex_file
487 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700488 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800489 // There are no more secondary dex files to check.
490 break;
491 }
492
493 std::string error_msg;
494 uint32_t expected_secondary_checksum = 0;
495 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700496 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 uint32_t actual_secondary_checksum
498 = secondary_oat_dex_file->GetDexFileLocationChecksum();
499 if (expected_secondary_checksum != actual_secondary_checksum) {
500 VLOG(oat) << "Dex checksum does not match for secondary dex: "
501 << secondary_dex_location
502 << ". Expected: " << expected_secondary_checksum
503 << ", Actual: " << actual_secondary_checksum;
Richard Uhler67ff7d12015-05-14 13:21:13 -0700504 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800505 }
506 } else {
507 // If we can't get the checksum for the secondary location, we assume
508 // the dex checksum is up to date for this and all other secondary dex
509 // files.
510 break;
511 }
512 }
513
Andreas Gampe29d38e72016-03-23 15:31:51 +0000514 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
515 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000516
Richard Uhler66d874d2015-01-15 09:37:19 -0800517 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000518 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
519 const ImageInfo* image_info = GetImageInfo();
520 if (image_info == nullptr) {
521 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000522
Richard Uhler76f5cb62016-04-04 13:30:16 -0700523 if (HasOriginalDexFiles()) {
524 return true;
525 }
526
527 // If there is no original dex file to fall back to, grudgingly accept
528 // the oat file. This could technically lead to crashes, but there's no
529 // way we could find a better oat file to use for this dex location,
530 // and it's better than being stuck in a boot loop with no way out.
531 // The problem will hopefully resolve itself the next time the runtime
532 // starts up.
533 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
534 << "Allow oat file use. This is potentially dangerous.";
535 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
536 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000537 VLOG(oat) << "Oat image checksum does not match image checksum.";
538 return true;
539 }
540 } else {
541 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800542 }
543
Andreas Gampe29d38e72016-03-23 15:31:51 +0000544 // Verify the profile hasn't changed recently.
545 // TODO: Move this check to OatFileCompilerFilterIsOkay? Nothing bad should
546 // happen if we use an oat file compiled with an out-of-date profile.
547 if (CompilerFilter::DependsOnProfile(current_compiler_filter)) {
548 if (profile_changed_) {
549 VLOG(oat) << "The profile has changed recently.";
550 return true;
551 }
552 } else {
553 VLOG(oat) << "Profile check skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800554 }
555
Andreas Gampe29d38e72016-03-23 15:31:51 +0000556 // Everything looks good; the dex file is not out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800557 return false;
558}
559
560bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700561 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800562}
563
564bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
565 if (GivenOatFileIsOutOfDate(file)) {
566 return false;
567 }
568
Andreas Gampe29d38e72016-03-23 15:31:51 +0000569 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
Richard Uhlera62d2f02016-03-18 15:05:30 -0700570
Andreas Gampe29d38e72016-03-23 15:31:51 +0000571 if (CompilerFilter::IsCompilationEnabled(current_compiler_filter)) {
572 if (!file.IsPic()) {
573 const ImageInfo* image_info = GetImageInfo();
574 if (image_info == nullptr) {
575 VLOG(oat) << "No image to check oat relocation against.";
576 return false;
577 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700578
Andreas Gampe29d38e72016-03-23 15:31:51 +0000579 // Verify the oat_data_begin recorded for the image in the oat file matches
580 // the actual oat_data_begin for boot.oat in the image.
581 const OatHeader& oat_header = file.GetOatHeader();
582 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
583 if (oat_data_begin != image_info->oat_data_begin) {
584 VLOG(oat) << file.GetLocation() <<
585 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
586 << " does not match actual image oat_data_begin ("
587 << image_info->oat_data_begin << ")";
588 return false;
589 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700590
Andreas Gampe29d38e72016-03-23 15:31:51 +0000591 // Verify the oat_patch_delta recorded for the image in the oat file matches
592 // the actual oat_patch_delta for the image.
593 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
594 if (oat_patch_delta != image_info->patch_delta) {
595 VLOG(oat) << file.GetLocation() <<
596 ": Oat file image patch delta (" << oat_patch_delta << ")"
597 << " does not match actual image patch delta ("
598 << image_info->patch_delta << ")";
599 return false;
600 }
601 } else {
602 // Oat files compiled in PIC mode do not require relocation.
603 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
604 }
605 } else {
606 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800607 }
608 return true;
609}
610
Richard Uhler1e860612016-03-30 12:17:55 -0700611OatFileAssistant::ResultOfAttemptToUpdate
612OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800613 CHECK(error_msg != nullptr);
614
Richard Uhler95abd042015-03-24 09:51:28 -0700615 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700616 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700617 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700618 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800619 }
Richard Uhler95abd042015-03-24 09:51:28 -0700620 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800621
622 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700623 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800624 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700625 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800626 }
627 const std::string& oat_file_name = *OatFileName();
628
629 const ImageInfo* image_info = GetImageInfo();
630 Runtime* runtime = Runtime::Current();
631 if (image_info == nullptr) {
632 *error_msg = "Patching of oat file " + oat_file_name
633 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700634 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800635 }
636
637 if (!runtime->IsDex2OatEnabled()) {
638 *error_msg = "Patching of oat file " + oat_file_name
639 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700640 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800641 }
642
643 std::vector<std::string> argv;
644 argv.push_back(runtime->GetPatchoatExecutable());
645 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700646 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800647 argv.push_back("--output-oat-file=" + oat_file_name);
648 argv.push_back("--patched-image-location=" + image_info->location);
649
650 std::string command_line(Join(argv, ' '));
651 if (!Exec(argv, error_msg)) {
652 // Manually delete the file. This ensures there is no garbage left over if
653 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100654 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700655 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800656 }
657
658 // Mark that the oat file has changed and we should try to reload.
659 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700660 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800661}
662
Richard Uhler1e860612016-03-30 12:17:55 -0700663OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700664OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800665 CHECK(error_msg != nullptr);
666
Richard Uhler8327cf72015-10-13 16:34:59 -0700667 Runtime* runtime = Runtime::Current();
668 if (!runtime->IsDex2OatEnabled()) {
669 *error_msg = "Generation of oat file for dex location " + dex_location_
670 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700671 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700672 }
673
Richard Uhler66d874d2015-01-15 09:37:19 -0800674 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700675 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800676 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700677 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800678 }
679 const std::string& oat_file_name = *OatFileName();
680
Richard Uhler66d874d2015-01-15 09:37:19 -0800681 // dex2oat ignores missing dex files and doesn't report an error.
682 // Check explicitly here so we can detect the error properly.
683 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700684 if (!OS::FileExists(dex_location_.c_str())) {
685 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700686 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800687 }
688
Richard Uhler8327cf72015-10-13 16:34:59 -0700689 std::unique_ptr<File> oat_file;
690 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
691 if (oat_file.get() == nullptr) {
692 *error_msg = "Generation of oat file " + oat_file_name
693 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700694 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700695 }
696
697 if (fchmod(oat_file->Fd(), 0644) != 0) {
698 *error_msg = "Generation of oat file " + oat_file_name
699 + " not attempted because the oat file could not be made world readable.";
700 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700701 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700702 }
703
704 std::vector<std::string> args;
705 args.push_back("--dex-file=" + dex_location_);
706 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
707 args.push_back("--oat-location=" + oat_file_name);
708
Richard Uhler66d874d2015-01-15 09:37:19 -0800709 if (!Dex2Oat(args, error_msg)) {
710 // Manually delete the file. This ensures there is no garbage left over if
711 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700712 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100713 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700714 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700715 }
716
717 if (oat_file->FlushCloseOrErase() != 0) {
718 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100719 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700720 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800721 }
722
723 // Mark that the oat file has changed and we should try to reload.
724 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700725 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800726}
727
728bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
729 std::string* error_msg) {
730 Runtime* runtime = Runtime::Current();
731 std::string image_location = ImageLocation();
732 if (image_location.empty()) {
733 *error_msg = "No image location found for Dex2Oat.";
734 return false;
735 }
736
737 std::vector<std::string> argv;
738 argv.push_back(runtime->GetCompilerExecutable());
739 argv.push_back("--runtime-arg");
740 argv.push_back("-classpath");
741 argv.push_back("--runtime-arg");
742 argv.push_back(runtime->GetClassPathString());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100743 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200744 argv.push_back("--debuggable");
745 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700746 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800747
748 if (!runtime->IsVerificationEnabled()) {
749 argv.push_back("--compiler-filter=verify-none");
750 }
751
752 if (runtime->MustRelocateIfPossible()) {
753 argv.push_back("--runtime-arg");
754 argv.push_back("-Xrelocate");
755 } else {
756 argv.push_back("--runtime-arg");
757 argv.push_back("-Xnorelocate");
758 }
759
760 if (!kIsTargetBuild) {
761 argv.push_back("--host");
762 }
763
764 argv.push_back("--boot-image=" + image_location);
765
766 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
767 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
768
769 argv.insert(argv.end(), args.begin(), args.end());
770
771 std::string command_line(Join(argv, ' '));
772 return Exec(argv, error_msg);
773}
774
775bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
776 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
777 CHECK(odex_filename != nullptr);
778 CHECK(error_msg != nullptr);
779
780 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700781 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800782 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700783 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800784
Richard Uhler63434112015-03-16 14:32:16 -0700785 // Find the directory portion of the dex location and add the oat/<isa>
786 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800787 size_t pos = location.rfind('/');
788 if (pos == std::string::npos) {
789 *error_msg = "Dex location " + location + " has no directory.";
790 return false;
791 }
792 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700793 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800794
795 // Find the file portion of the dex location.
796 std::string file;
797 if (pos == std::string::npos) {
798 file = location;
799 } else {
800 file = location.substr(pos+1);
801 }
802
803 // Get the base part of the file without the extension.
804 pos = file.rfind('.');
805 if (pos == std::string::npos) {
806 *error_msg = "Dex location " + location + " has no extension.";
807 return false;
808 }
809 std::string base = file.substr(0, pos);
810
811 *odex_filename = dir + "/" + base + ".odex";
812 return true;
813}
814
815std::string OatFileAssistant::DalvikCacheDirectory() {
816 // Note: We don't cache this, because it will only be called once by
Andreas Gampe29d38e72016-03-23 15:31:51 +0000817 // OatFileName.
Richard Uhler66d874d2015-01-15 09:37:19 -0800818
819 // TODO: The work done in GetDalvikCache is overkill for what we need.
820 // Ideally a new API for getting the DalvikCacheDirectory the way we want
821 // (without existence testing, creation, or death) is provided with the rest
822 // of the GetDalvikCache family of functions. Until such an API is in place,
823 // we use GetDalvikCache to avoid duplicating the logic for determining the
824 // dalvik cache directory.
825 std::string result;
826 bool have_android_data;
827 bool dalvik_cache_exists;
828 bool is_global_cache;
829 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
830 return result;
831}
832
Richard Uhler66d874d2015-01-15 09:37:19 -0800833std::string OatFileAssistant::ImageLocation() {
834 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000835 const std::vector<gc::space::ImageSpace*>& image_spaces =
836 runtime->GetHeap()->GetBootImageSpaces();
837 if (image_spaces.empty()) {
838 return "";
839 }
840 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800841}
842
843const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700844 if (!required_dex_checksum_attempted_) {
845 required_dex_checksum_attempted_ = true;
846 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800847 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700848 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700849 required_dex_checksum_found_ = true;
850 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800851 } else {
852 // This can happen if the original dex file has been stripped from the
853 // apk.
854 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700855 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800856
857 // Get the checksum from the odex if we can.
858 const OatFile* odex_file = GetOdexFile();
859 if (odex_file != nullptr) {
860 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700861 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800862 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700863 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
864 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800865 }
866 }
867 }
868 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700869 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800870}
871
872const OatFile* OatFileAssistant::GetOdexFile() {
873 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
874 if (!odex_file_load_attempted_) {
875 odex_file_load_attempted_ = true;
876 if (OdexFileName() != nullptr) {
877 const std::string& odex_file_name = *OdexFileName();
878 std::string error_msg;
879 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800880 odex_file_name.c_str(),
881 nullptr,
882 nullptr,
883 load_executable_,
884 /*low_4gb*/false,
885 dex_location_.c_str(),
886 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800887 if (cached_odex_file_.get() == nullptr) {
888 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
889 << odex_file_name << ": " << error_msg;
890 }
891 }
892 }
893 return cached_odex_file_.get();
894}
895
Richard Uhler5f946da2015-07-17 12:28:32 -0700896bool OatFileAssistant::OdexFileIsExecutable() {
897 const OatFile* odex_file = GetOdexFile();
898 return (odex_file != nullptr && odex_file->IsExecutable());
899}
900
Richard Uhlerd1537b52016-03-29 13:27:41 -0700901bool OatFileAssistant::OdexFileHasPatchInfo() {
902 const OatFile* odex_file = GetOdexFile();
903 return (odex_file != nullptr && odex_file->HasPatchInfo());
904}
905
Richard Uhler66d874d2015-01-15 09:37:19 -0800906void OatFileAssistant::ClearOdexFileCache() {
907 odex_file_load_attempted_ = false;
908 cached_odex_file_.reset();
909 odex_file_is_out_of_date_attempted_ = false;
910 odex_file_is_up_to_date_attempted_ = false;
911}
912
913const OatFile* OatFileAssistant::GetOatFile() {
914 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
915 if (!oat_file_load_attempted_) {
916 oat_file_load_attempted_ = true;
917 if (OatFileName() != nullptr) {
918 const std::string& oat_file_name = *OatFileName();
919 std::string error_msg;
920 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800921 oat_file_name.c_str(),
922 nullptr,
923 nullptr,
924 load_executable_,
925 /*low_4gb*/false,
926 dex_location_.c_str(),
927 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800928 if (cached_oat_file_.get() == nullptr) {
929 VLOG(oat) << "OatFileAssistant test for existing oat file "
930 << oat_file_name << ": " << error_msg;
931 }
932 }
933 }
934 return cached_oat_file_.get();
935}
936
Richard Uhler5f946da2015-07-17 12:28:32 -0700937bool OatFileAssistant::OatFileIsExecutable() {
938 const OatFile* oat_file = GetOatFile();
939 return (oat_file != nullptr && oat_file->IsExecutable());
940}
941
Richard Uhlerd1537b52016-03-29 13:27:41 -0700942bool OatFileAssistant::OatFileHasPatchInfo() {
943 const OatFile* oat_file = GetOatFile();
944 return (oat_file != nullptr && oat_file->HasPatchInfo());
945}
946
Richard Uhler66d874d2015-01-15 09:37:19 -0800947void OatFileAssistant::ClearOatFileCache() {
948 oat_file_load_attempted_ = false;
949 cached_oat_file_.reset();
950 oat_file_is_out_of_date_attempted_ = false;
951 oat_file_is_up_to_date_attempted_ = false;
952}
953
954const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
955 if (!image_info_load_attempted_) {
956 image_info_load_attempted_ = true;
957
958 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800959 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
960 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800961 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800962
963 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800964 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800965 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800966 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
967 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800968 cached_image_info_.patch_delta = image_header.GetPatchDelta();
969 } else {
970 std::unique_ptr<ImageHeader> image_header(
Jeff Haob11ffb72016-04-07 15:40:54 -0700971 gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_));
Richard Uhler66d874d2015-01-15 09:37:19 -0800972 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800973 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
974 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800975 cached_image_info_.patch_delta = image_header->GetPatchDelta();
976 }
977 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800978 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700979
Jeff Haofd336c32016-04-07 19:46:31 -0700980 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800981 }
982 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
983}
984
Jeff Haob11ffb72016-04-07 15:40:54 -0700985// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700986uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700987 uint32_t checksum = 0;
988 std::vector<gc::space::ImageSpace*> image_spaces =
989 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700990 if (isa == kRuntimeISA) {
991 for (gc::space::ImageSpace* image_space : image_spaces) {
992 checksum ^= image_space->GetImageHeader().GetOatChecksum();
993 }
994 } else {
995 for (gc::space::ImageSpace* image_space : image_spaces) {
996 std::string location = image_space->GetImageLocation();
997 std::unique_ptr<ImageHeader> image_header(
998 gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa));
999 checksum ^= image_header->GetOatChecksum();
1000 }
Jeff Haob11ffb72016-04-07 15:40:54 -07001001 }
1002 return checksum;
1003}
1004
1005uint32_t OatFileAssistant::GetCombinedImageChecksum() {
1006 if (!image_info_load_attempted_) {
1007 GetImageInfo();
1008 }
1009 return combined_image_checksum_;
1010}
1011
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001012gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
1013 DCHECK(oat_file != nullptr);
1014 std::string art_file = ArtFileName(oat_file);
1015 if (art_file.empty()) {
1016 return nullptr;
1017 }
1018 std::string error_msg;
1019 ScopedObjectAccess soa(Thread::Current());
1020 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
1021 oat_file,
1022 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001023 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001024 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1025 }
1026 return ret;
1027}
1028
Richard Uhler66d874d2015-01-15 09:37:19 -08001029} // namespace art
1030