blob: 32e95e7459e4dffd090d346616748966273b652d [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 Uhler1e860612016-03-30 12:17:55 -0700182OatFileAssistant::ResultOfAttemptToUpdate
183OatFileAssistant::MakeUpToDate(CompilerFilter::Filter target, std::string* error_msg) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000184 switch (GetDexOptNeeded(target)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700185 case kNoDexOptNeeded: return kUpdateSucceeded;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000186 case kDex2OatNeeded: return GenerateOatFile(target, error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700187 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
188 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800189 }
190 UNREACHABLE();
191}
192
193std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700194 // The best oat files are, in descending order of bestness:
195 // 1. Properly relocated files. These may be opened executable.
196 // 2. Not out-of-date files that are already opened non-executable.
197 // 3. Not out-of-date files that we must reopen non-executable.
198
Richard Uhler66d874d2015-01-15 09:37:19 -0800199 if (OatFileIsUpToDate()) {
200 oat_file_released_ = true;
201 return std::move(cached_oat_file_);
202 }
203
204 if (OdexFileIsUpToDate()) {
205 oat_file_released_ = true;
206 return std::move(cached_odex_file_);
207 }
208
Richard Uhler5f946da2015-07-17 12:28:32 -0700209 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
210 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800211
Richard Uhler5f946da2015-07-17 12:28:32 -0700212 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
213 oat_file_released_ = true;
214 return std::move(cached_oat_file_);
215 }
216
217 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
218 oat_file_released_ = true;
219 return std::move(cached_odex_file_);
220 }
221
222 if (!OatFileIsOutOfDate()) {
223 load_executable_ = false;
224 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800225 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700226 CHECK(!OatFileIsExecutable());
227 oat_file_released_ = true;
228 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800229 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700230 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800231
Richard Uhler5f946da2015-07-17 12:28:32 -0700232 if (!OdexFileIsOutOfDate()) {
233 load_executable_ = false;
234 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800235 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700236 CHECK(!OdexFileIsExecutable());
237 oat_file_released_ = true;
238 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800239 }
240 }
241
242 return std::unique_ptr<OatFile>();
243}
244
245std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
246 const OatFile& oat_file, const char* dex_location) {
247 std::vector<std::unique_ptr<const DexFile>> dex_files;
248
249 // Load the primary dex file.
250 std::string error_msg;
251 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
252 dex_location, nullptr, false);
253 if (oat_dex_file == nullptr) {
254 LOG(WARNING) << "Attempt to load out-of-date oat file "
255 << oat_file.GetLocation() << " for dex location " << dex_location;
256 return std::vector<std::unique_ptr<const DexFile>>();
257 }
258
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700259 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800260 if (dex_file.get() == nullptr) {
261 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
262 return std::vector<std::unique_ptr<const DexFile>>();
263 }
264 dex_files.push_back(std::move(dex_file));
265
266 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700267 for (size_t i = 1; ; i++) {
268 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800269 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700270 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 // There are no more secondary dex files to load.
272 break;
273 }
274
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700275 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 if (dex_file.get() == nullptr) {
277 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
278 return std::vector<std::unique_ptr<const DexFile>>();
279 }
280 dex_files.push_back(std::move(dex_file));
281 }
282 return dex_files;
283}
284
Richard Uhler9b994ea2015-06-24 08:44:19 -0700285bool OatFileAssistant::HasOriginalDexFiles() {
286 // Ensure GetRequiredDexChecksum has been run so that
287 // has_original_dex_files_ is initialized. We don't care about the result of
288 // GetRequiredDexChecksum.
289 GetRequiredDexChecksum();
290 return has_original_dex_files_;
291}
292
Richard Uhler66d874d2015-01-15 09:37:19 -0800293const std::string* OatFileAssistant::OdexFileName() {
294 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800295 cached_odex_file_name_attempted_ = true;
296
297 std::string error_msg;
298 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700299 dex_location_, isa_, &cached_odex_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800300 if (!cached_odex_file_name_found_) {
301 // If we can't figure out the odex file, we treat it as if the odex
302 // file was inaccessible.
303 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
304 }
305 }
306 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
307}
308
309bool OatFileAssistant::OdexFileExists() {
310 return GetOdexFile() != nullptr;
311}
312
Richard Uhler95abd042015-03-24 09:51:28 -0700313OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800314 if (OdexFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700315 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800316 }
317 if (OdexFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700318 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800319 }
Richard Uhler95abd042015-03-24 09:51:28 -0700320 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800321}
322
323bool OatFileAssistant::OdexFileIsOutOfDate() {
324 if (!odex_file_is_out_of_date_attempted_) {
325 odex_file_is_out_of_date_attempted_ = true;
326 const OatFile* odex_file = GetOdexFile();
327 if (odex_file == nullptr) {
328 cached_odex_file_is_out_of_date_ = true;
329 } else {
330 cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file);
331 }
332 }
333 return cached_odex_file_is_out_of_date_;
334}
335
336bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700337 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800338}
339
340bool OatFileAssistant::OdexFileIsUpToDate() {
341 if (!odex_file_is_up_to_date_attempted_) {
342 odex_file_is_up_to_date_attempted_ = true;
343 const OatFile* odex_file = GetOdexFile();
344 if (odex_file == nullptr) {
345 cached_odex_file_is_up_to_date_ = false;
346 } else {
347 cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
348 }
349 }
350 return cached_odex_file_is_up_to_date_;
351}
352
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800353std::string OatFileAssistant::ArtFileName(const OatFile* oat_file) const {
354 const std::string oat_file_location = oat_file->GetLocation();
355 // Replace extension with .art
356 const size_t last_ext = oat_file_location.find_last_of('.');
357 if (last_ext == std::string::npos) {
358 LOG(ERROR) << "No extension in oat file " << oat_file_location;
359 return std::string();
360 }
361 return oat_file_location.substr(0, last_ext) + ".art";
362}
363
Richard Uhler66d874d2015-01-15 09:37:19 -0800364const std::string* OatFileAssistant::OatFileName() {
365 if (!cached_oat_file_name_attempted_) {
366 cached_oat_file_name_attempted_ = true;
367
368 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 // TODO: The oat file assistant should be the definitive place for
370 // determining the oat file name from the dex location, not
371 // GetDalvikCacheFilename.
372 std::string cache_dir = StringPrintf("%s%s",
373 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
374 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700375 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700376 cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 if (!cached_oat_file_name_found_) {
378 // If we can't determine the oat file name, we treat the oat file as
379 // inaccessible.
380 LOG(WARNING) << "Failed to determine oat file name for dex location "
381 << dex_location_ << ": " << error_msg;
382 }
383 }
384 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
385}
386
387bool OatFileAssistant::OatFileExists() {
388 return GetOatFile() != nullptr;
389}
390
Richard Uhler95abd042015-03-24 09:51:28 -0700391OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800392 if (OatFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700393 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800394 }
395 if (OatFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700396 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800397 }
Richard Uhler95abd042015-03-24 09:51:28 -0700398 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800399}
400
401bool OatFileAssistant::OatFileIsOutOfDate() {
402 if (!oat_file_is_out_of_date_attempted_) {
403 oat_file_is_out_of_date_attempted_ = true;
404 const OatFile* oat_file = GetOatFile();
405 if (oat_file == nullptr) {
406 cached_oat_file_is_out_of_date_ = true;
407 } else {
408 cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file);
409 }
410 }
411 return cached_oat_file_is_out_of_date_;
412}
413
414bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700415 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800416}
417
418bool OatFileAssistant::OatFileIsUpToDate() {
419 if (!oat_file_is_up_to_date_attempted_) {
420 oat_file_is_up_to_date_attempted_ = true;
421 const OatFile* oat_file = GetOatFile();
422 if (oat_file == nullptr) {
423 cached_oat_file_is_up_to_date_ = false;
424 } else {
425 cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
426 }
427 }
428 return cached_oat_file_is_up_to_date_;
429}
430
Richard Uhler95abd042015-03-24 09:51:28 -0700431OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800432 // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which
433 // is more work than we need to do. If performance becomes a concern, and
434 // this method is actually called, this should be fixed.
435 if (GivenOatFileIsOutOfDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700436 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800437 }
438 if (GivenOatFileIsUpToDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700439 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800440 }
Richard Uhler95abd042015-03-24 09:51:28 -0700441 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800442}
443
444bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) {
445 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700446 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800447 // what we provide, which verifies the primary dex checksum for us.
448 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
449 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700450 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700451 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800452 return true;
453 }
454
455 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700456 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800457 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700458 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 const OatFile::OatDexFile* secondary_oat_dex_file
460 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700461 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800462 // There are no more secondary dex files to check.
463 break;
464 }
465
466 std::string error_msg;
467 uint32_t expected_secondary_checksum = 0;
468 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700469 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800470 uint32_t actual_secondary_checksum
471 = secondary_oat_dex_file->GetDexFileLocationChecksum();
472 if (expected_secondary_checksum != actual_secondary_checksum) {
473 VLOG(oat) << "Dex checksum does not match for secondary dex: "
474 << secondary_dex_location
475 << ". Expected: " << expected_secondary_checksum
476 << ", Actual: " << actual_secondary_checksum;
Richard Uhler67ff7d12015-05-14 13:21:13 -0700477 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800478 }
479 } else {
480 // If we can't get the checksum for the secondary location, we assume
481 // the dex checksum is up to date for this and all other secondary dex
482 // files.
483 break;
484 }
485 }
486
Andreas Gampe29d38e72016-03-23 15:31:51 +0000487 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
488 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000489
Richard Uhler66d874d2015-01-15 09:37:19 -0800490 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000491 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
492 const ImageInfo* image_info = GetImageInfo();
493 if (image_info == nullptr) {
494 VLOG(oat) << "No image for oat image checksum to match against.";
495 return true;
496 }
497
498 if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
499 VLOG(oat) << "Oat image checksum does not match image checksum.";
500 return true;
501 }
502 } else {
503 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800504 }
505
Andreas Gampe29d38e72016-03-23 15:31:51 +0000506 // Verify the profile hasn't changed recently.
507 // TODO: Move this check to OatFileCompilerFilterIsOkay? Nothing bad should
508 // happen if we use an oat file compiled with an out-of-date profile.
509 if (CompilerFilter::DependsOnProfile(current_compiler_filter)) {
510 if (profile_changed_) {
511 VLOG(oat) << "The profile has changed recently.";
512 return true;
513 }
514 } else {
515 VLOG(oat) << "Profile check skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800516 }
517
Andreas Gampe29d38e72016-03-23 15:31:51 +0000518 // Everything looks good; the dex file is not out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800519 return false;
520}
521
522bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700523 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800524}
525
526bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
527 if (GivenOatFileIsOutOfDate(file)) {
528 return false;
529 }
530
Andreas Gampe29d38e72016-03-23 15:31:51 +0000531 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
Richard Uhlera62d2f02016-03-18 15:05:30 -0700532
Andreas Gampe29d38e72016-03-23 15:31:51 +0000533 if (CompilerFilter::IsCompilationEnabled(current_compiler_filter)) {
534 if (!file.IsPic()) {
535 const ImageInfo* image_info = GetImageInfo();
536 if (image_info == nullptr) {
537 VLOG(oat) << "No image to check oat relocation against.";
538 return false;
539 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700540
Andreas Gampe29d38e72016-03-23 15:31:51 +0000541 // Verify the oat_data_begin recorded for the image in the oat file matches
542 // the actual oat_data_begin for boot.oat in the image.
543 const OatHeader& oat_header = file.GetOatHeader();
544 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
545 if (oat_data_begin != image_info->oat_data_begin) {
546 VLOG(oat) << file.GetLocation() <<
547 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
548 << " does not match actual image oat_data_begin ("
549 << image_info->oat_data_begin << ")";
550 return false;
551 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700552
Andreas Gampe29d38e72016-03-23 15:31:51 +0000553 // Verify the oat_patch_delta recorded for the image in the oat file matches
554 // the actual oat_patch_delta for the image.
555 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
556 if (oat_patch_delta != image_info->patch_delta) {
557 VLOG(oat) << file.GetLocation() <<
558 ": Oat file image patch delta (" << oat_patch_delta << ")"
559 << " does not match actual image patch delta ("
560 << image_info->patch_delta << ")";
561 return false;
562 }
563 } else {
564 // Oat files compiled in PIC mode do not require relocation.
565 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
566 }
567 } else {
568 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800569 }
570 return true;
571}
572
Richard Uhler1e860612016-03-30 12:17:55 -0700573OatFileAssistant::ResultOfAttemptToUpdate
574OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800575 CHECK(error_msg != nullptr);
576
Richard Uhler95abd042015-03-24 09:51:28 -0700577 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700578 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700579 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700580 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800581 }
Richard Uhler95abd042015-03-24 09:51:28 -0700582 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800583
584 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700585 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800586 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700587 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800588 }
589 const std::string& oat_file_name = *OatFileName();
590
591 const ImageInfo* image_info = GetImageInfo();
592 Runtime* runtime = Runtime::Current();
593 if (image_info == nullptr) {
594 *error_msg = "Patching of oat file " + oat_file_name
595 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700596 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800597 }
598
599 if (!runtime->IsDex2OatEnabled()) {
600 *error_msg = "Patching of oat file " + oat_file_name
601 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700602 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800603 }
604
605 std::vector<std::string> argv;
606 argv.push_back(runtime->GetPatchoatExecutable());
607 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700608 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800609 argv.push_back("--output-oat-file=" + oat_file_name);
610 argv.push_back("--patched-image-location=" + image_info->location);
611
612 std::string command_line(Join(argv, ' '));
613 if (!Exec(argv, error_msg)) {
614 // Manually delete the file. This ensures there is no garbage left over if
615 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100616 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700617 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800618 }
619
620 // Mark that the oat file has changed and we should try to reload.
621 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700622 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800623}
624
Richard Uhler1e860612016-03-30 12:17:55 -0700625OatFileAssistant::ResultOfAttemptToUpdate
626OatFileAssistant::GenerateOatFile(CompilerFilter::Filter target, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800627 CHECK(error_msg != nullptr);
628
Richard Uhler8327cf72015-10-13 16:34:59 -0700629 Runtime* runtime = Runtime::Current();
630 if (!runtime->IsDex2OatEnabled()) {
631 *error_msg = "Generation of oat file for dex location " + dex_location_
632 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700633 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700634 }
635
Richard Uhler66d874d2015-01-15 09:37:19 -0800636 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700637 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800638 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700639 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800640 }
641 const std::string& oat_file_name = *OatFileName();
642
Richard Uhler66d874d2015-01-15 09:37:19 -0800643 // dex2oat ignores missing dex files and doesn't report an error.
644 // Check explicitly here so we can detect the error properly.
645 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700646 if (!OS::FileExists(dex_location_.c_str())) {
647 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700648 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800649 }
650
Richard Uhler8327cf72015-10-13 16:34:59 -0700651 std::unique_ptr<File> oat_file;
652 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
653 if (oat_file.get() == nullptr) {
654 *error_msg = "Generation of oat file " + oat_file_name
655 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700656 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700657 }
658
659 if (fchmod(oat_file->Fd(), 0644) != 0) {
660 *error_msg = "Generation of oat file " + oat_file_name
661 + " not attempted because the oat file could not be made world readable.";
662 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700663 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700664 }
665
666 std::vector<std::string> args;
667 args.push_back("--dex-file=" + dex_location_);
668 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
669 args.push_back("--oat-location=" + oat_file_name);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000670 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(target));
Richard Uhler8327cf72015-10-13 16:34:59 -0700671
Richard Uhler66d874d2015-01-15 09:37:19 -0800672 if (!Dex2Oat(args, error_msg)) {
673 // Manually delete the file. This ensures there is no garbage left over if
674 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700675 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100676 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700677 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700678 }
679
680 if (oat_file->FlushCloseOrErase() != 0) {
681 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100682 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700683 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800684 }
685
686 // Mark that the oat file has changed and we should try to reload.
687 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700688 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800689}
690
691bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
692 std::string* error_msg) {
693 Runtime* runtime = Runtime::Current();
694 std::string image_location = ImageLocation();
695 if (image_location.empty()) {
696 *error_msg = "No image location found for Dex2Oat.";
697 return false;
698 }
699
700 std::vector<std::string> argv;
701 argv.push_back(runtime->GetCompilerExecutable());
702 argv.push_back("--runtime-arg");
703 argv.push_back("-classpath");
704 argv.push_back("--runtime-arg");
705 argv.push_back(runtime->GetClassPathString());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100706 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200707 argv.push_back("--debuggable");
708 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700709 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800710
711 if (!runtime->IsVerificationEnabled()) {
712 argv.push_back("--compiler-filter=verify-none");
713 }
714
715 if (runtime->MustRelocateIfPossible()) {
716 argv.push_back("--runtime-arg");
717 argv.push_back("-Xrelocate");
718 } else {
719 argv.push_back("--runtime-arg");
720 argv.push_back("-Xnorelocate");
721 }
722
723 if (!kIsTargetBuild) {
724 argv.push_back("--host");
725 }
726
727 argv.push_back("--boot-image=" + image_location);
728
729 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
730 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
731
732 argv.insert(argv.end(), args.begin(), args.end());
733
734 std::string command_line(Join(argv, ' '));
735 return Exec(argv, error_msg);
736}
737
738bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
739 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
740 CHECK(odex_filename != nullptr);
741 CHECK(error_msg != nullptr);
742
743 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700744 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800745 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700746 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800747
Richard Uhler63434112015-03-16 14:32:16 -0700748 // Find the directory portion of the dex location and add the oat/<isa>
749 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800750 size_t pos = location.rfind('/');
751 if (pos == std::string::npos) {
752 *error_msg = "Dex location " + location + " has no directory.";
753 return false;
754 }
755 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700756 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800757
758 // Find the file portion of the dex location.
759 std::string file;
760 if (pos == std::string::npos) {
761 file = location;
762 } else {
763 file = location.substr(pos+1);
764 }
765
766 // Get the base part of the file without the extension.
767 pos = file.rfind('.');
768 if (pos == std::string::npos) {
769 *error_msg = "Dex location " + location + " has no extension.";
770 return false;
771 }
772 std::string base = file.substr(0, pos);
773
774 *odex_filename = dir + "/" + base + ".odex";
775 return true;
776}
777
778std::string OatFileAssistant::DalvikCacheDirectory() {
779 // Note: We don't cache this, because it will only be called once by
Andreas Gampe29d38e72016-03-23 15:31:51 +0000780 // OatFileName.
Richard Uhler66d874d2015-01-15 09:37:19 -0800781
782 // TODO: The work done in GetDalvikCache is overkill for what we need.
783 // Ideally a new API for getting the DalvikCacheDirectory the way we want
784 // (without existence testing, creation, or death) is provided with the rest
785 // of the GetDalvikCache family of functions. Until such an API is in place,
786 // we use GetDalvikCache to avoid duplicating the logic for determining the
787 // dalvik cache directory.
788 std::string result;
789 bool have_android_data;
790 bool dalvik_cache_exists;
791 bool is_global_cache;
792 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
793 return result;
794}
795
Richard Uhler66d874d2015-01-15 09:37:19 -0800796std::string OatFileAssistant::ImageLocation() {
797 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000798 const std::vector<gc::space::ImageSpace*>& image_spaces =
799 runtime->GetHeap()->GetBootImageSpaces();
800 if (image_spaces.empty()) {
801 return "";
802 }
803 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800804}
805
806const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700807 if (!required_dex_checksum_attempted_) {
808 required_dex_checksum_attempted_ = true;
809 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800810 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700811 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700812 required_dex_checksum_found_ = true;
813 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800814 } else {
815 // This can happen if the original dex file has been stripped from the
816 // apk.
817 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700818 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800819
820 // Get the checksum from the odex if we can.
821 const OatFile* odex_file = GetOdexFile();
822 if (odex_file != nullptr) {
823 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700824 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800825 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700826 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
827 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800828 }
829 }
830 }
831 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700832 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800833}
834
835const OatFile* OatFileAssistant::GetOdexFile() {
836 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
837 if (!odex_file_load_attempted_) {
838 odex_file_load_attempted_ = true;
839 if (OdexFileName() != nullptr) {
840 const std::string& odex_file_name = *OdexFileName();
841 std::string error_msg;
842 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800843 odex_file_name.c_str(),
844 nullptr,
845 nullptr,
846 load_executable_,
847 /*low_4gb*/false,
848 dex_location_.c_str(),
849 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800850 if (cached_odex_file_.get() == nullptr) {
851 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
852 << odex_file_name << ": " << error_msg;
853 }
854 }
855 }
856 return cached_odex_file_.get();
857}
858
Richard Uhler5f946da2015-07-17 12:28:32 -0700859bool OatFileAssistant::OdexFileIsExecutable() {
860 const OatFile* odex_file = GetOdexFile();
861 return (odex_file != nullptr && odex_file->IsExecutable());
862}
863
Richard Uhlerd1537b52016-03-29 13:27:41 -0700864bool OatFileAssistant::OdexFileHasPatchInfo() {
865 const OatFile* odex_file = GetOdexFile();
866 return (odex_file != nullptr && odex_file->HasPatchInfo());
867}
868
Richard Uhler66d874d2015-01-15 09:37:19 -0800869void OatFileAssistant::ClearOdexFileCache() {
870 odex_file_load_attempted_ = false;
871 cached_odex_file_.reset();
872 odex_file_is_out_of_date_attempted_ = false;
873 odex_file_is_up_to_date_attempted_ = false;
874}
875
876const OatFile* OatFileAssistant::GetOatFile() {
877 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
878 if (!oat_file_load_attempted_) {
879 oat_file_load_attempted_ = true;
880 if (OatFileName() != nullptr) {
881 const std::string& oat_file_name = *OatFileName();
882 std::string error_msg;
883 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800884 oat_file_name.c_str(),
885 nullptr,
886 nullptr,
887 load_executable_,
888 /*low_4gb*/false,
889 dex_location_.c_str(),
890 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800891 if (cached_oat_file_.get() == nullptr) {
892 VLOG(oat) << "OatFileAssistant test for existing oat file "
893 << oat_file_name << ": " << error_msg;
894 }
895 }
896 }
897 return cached_oat_file_.get();
898}
899
Richard Uhler5f946da2015-07-17 12:28:32 -0700900bool OatFileAssistant::OatFileIsExecutable() {
901 const OatFile* oat_file = GetOatFile();
902 return (oat_file != nullptr && oat_file->IsExecutable());
903}
904
Richard Uhlerd1537b52016-03-29 13:27:41 -0700905bool OatFileAssistant::OatFileHasPatchInfo() {
906 const OatFile* oat_file = GetOatFile();
907 return (oat_file != nullptr && oat_file->HasPatchInfo());
908}
909
Richard Uhler66d874d2015-01-15 09:37:19 -0800910void OatFileAssistant::ClearOatFileCache() {
911 oat_file_load_attempted_ = false;
912 cached_oat_file_.reset();
913 oat_file_is_out_of_date_attempted_ = false;
914 oat_file_is_up_to_date_attempted_ = false;
915}
916
917const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
918 if (!image_info_load_attempted_) {
919 image_info_load_attempted_ = true;
920
921 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800922 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
923 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800924 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800925
926 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800927 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800928 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800929 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
930 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800931 cached_image_info_.patch_delta = image_header.GetPatchDelta();
932 } else {
933 std::unique_ptr<ImageHeader> image_header(
934 gc::space::ImageSpace::ReadImageHeaderOrDie(
935 cached_image_info_.location.c_str(), isa_));
936 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800937 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
938 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800939 cached_image_info_.patch_delta = image_header->GetPatchDelta();
940 }
941 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800942 image_info_load_succeeded_ = (!image_spaces.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -0800943 }
944 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
945}
946
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800947gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
948 DCHECK(oat_file != nullptr);
949 std::string art_file = ArtFileName(oat_file);
950 if (art_file.empty()) {
951 return nullptr;
952 }
953 std::string error_msg;
954 ScopedObjectAccess soa(Thread::Current());
955 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
956 oat_file,
957 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800958 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800959 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
960 }
961 return ret;
962}
963
Richard Uhler66d874d2015-01-15 09:37:19 -0800964} // namespace art
965