blob: 67c8e65b28952f305805045b21c94bb6ad3bd2c8 [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,
Richard Uhlera62d2f02016-03-18 15:05:30 -070048 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080049 bool load_executable)
Richard Uhlera62d2f02016-03-18 15:05:30 -070050 : 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,
Richard Uhlera62d2f02016-03-18 15:05:30 -070056 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080057 bool load_executable)
Richard Uhlera62d2f02016-03-18 15:05:30 -070058 : 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()) {
80 TEMP_FAILURE_RETRY(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)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800112 TEMP_FAILURE_RETRY(unlink(lock_file_name.c_str()));
113 return false;
114 }
115 return true;
116}
117
Richard Uhlera62d2f02016-03-18 15:05:30 -0700118bool 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
Richard Uhlera62d2f02016-03-18 15:05:30 -0700127bool 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
Richard Uhlera62d2f02016-03-18 15:05:30 -0700153 // 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
Richard Uhlera62d2f02016-03-18 15:05:30 -0700167 // See if we can get an up-to-date file by running patchoat.
168 if (compilation_desired) {
169 if (odex_okay && OdexFileNeedsRelocation()) {
170 // TODO: don't return kPatchOatNeeded if the odex file contains no
171 // patch information.
172 return kPatchOatNeeded;
173 }
174
175 if (oat_okay && OatFileNeedsRelocation()) {
176 // TODO: don't return kSelfPatchOatNeeded if the oat file contains no
177 // patch information.
178 return kSelfPatchOatNeeded;
179 }
Richard Uhler95abd042015-03-24 09:51:28 -0700180 }
181
Richard Uhlera62d2f02016-03-18 15:05:30 -0700182 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700183 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800184}
185
Richard Uhlera62d2f02016-03-18 15:05:30 -0700186bool OatFileAssistant::MakeUpToDate(CompilerFilter::Filter target, std::string* error_msg) {
187 switch (GetDexOptNeeded(target)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700188 case kNoDexOptNeeded: return true;
Richard Uhlera62d2f02016-03-18 15:05:30 -0700189 case kDex2OatNeeded: return GenerateOatFile(target, error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700190 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
191 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800192 }
193 UNREACHABLE();
194}
195
196std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700197 // The best oat files are, in descending order of bestness:
198 // 1. Properly relocated files. These may be opened executable.
199 // 2. Not out-of-date files that are already opened non-executable.
200 // 3. Not out-of-date files that we must reopen non-executable.
201
Richard Uhler66d874d2015-01-15 09:37:19 -0800202 if (OatFileIsUpToDate()) {
203 oat_file_released_ = true;
204 return std::move(cached_oat_file_);
205 }
206
207 if (OdexFileIsUpToDate()) {
208 oat_file_released_ = true;
209 return std::move(cached_odex_file_);
210 }
211
Richard Uhler5f946da2015-07-17 12:28:32 -0700212 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
213 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800214
Richard Uhler5f946da2015-07-17 12:28:32 -0700215 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
216 oat_file_released_ = true;
217 return std::move(cached_oat_file_);
218 }
219
220 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
221 oat_file_released_ = true;
222 return std::move(cached_odex_file_);
223 }
224
225 if (!OatFileIsOutOfDate()) {
226 load_executable_ = false;
227 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800228 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700229 CHECK(!OatFileIsExecutable());
230 oat_file_released_ = true;
231 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800232 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700233 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800234
Richard Uhler5f946da2015-07-17 12:28:32 -0700235 if (!OdexFileIsOutOfDate()) {
236 load_executable_ = false;
237 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800238 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700239 CHECK(!OdexFileIsExecutable());
240 oat_file_released_ = true;
241 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800242 }
243 }
244
245 return std::unique_ptr<OatFile>();
246}
247
248std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
249 const OatFile& oat_file, const char* dex_location) {
250 std::vector<std::unique_ptr<const DexFile>> dex_files;
251
252 // Load the primary dex file.
253 std::string error_msg;
254 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
255 dex_location, nullptr, false);
256 if (oat_dex_file == nullptr) {
257 LOG(WARNING) << "Attempt to load out-of-date oat file "
258 << oat_file.GetLocation() << " for dex location " << dex_location;
259 return std::vector<std::unique_ptr<const DexFile>>();
260 }
261
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700262 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800263 if (dex_file.get() == nullptr) {
264 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
265 return std::vector<std::unique_ptr<const DexFile>>();
266 }
267 dex_files.push_back(std::move(dex_file));
268
269 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700270 for (size_t i = 1; ; i++) {
271 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800272 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700273 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800274 // There are no more secondary dex files to load.
275 break;
276 }
277
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700278 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800279 if (dex_file.get() == nullptr) {
280 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
281 return std::vector<std::unique_ptr<const DexFile>>();
282 }
283 dex_files.push_back(std::move(dex_file));
284 }
285 return dex_files;
286}
287
Richard Uhler9b994ea2015-06-24 08:44:19 -0700288bool OatFileAssistant::HasOriginalDexFiles() {
289 // Ensure GetRequiredDexChecksum has been run so that
290 // has_original_dex_files_ is initialized. We don't care about the result of
291 // GetRequiredDexChecksum.
292 GetRequiredDexChecksum();
293 return has_original_dex_files_;
294}
295
Richard Uhler66d874d2015-01-15 09:37:19 -0800296const std::string* OatFileAssistant::OdexFileName() {
297 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800298 cached_odex_file_name_attempted_ = true;
299
300 std::string error_msg;
301 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700302 dex_location_, isa_, &cached_odex_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 if (!cached_odex_file_name_found_) {
304 // If we can't figure out the odex file, we treat it as if the odex
305 // file was inaccessible.
306 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
307 }
308 }
309 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
310}
311
312bool OatFileAssistant::OdexFileExists() {
313 return GetOdexFile() != nullptr;
314}
315
Richard Uhler95abd042015-03-24 09:51:28 -0700316OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800317 if (OdexFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700318 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800319 }
320 if (OdexFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700321 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 }
Richard Uhler95abd042015-03-24 09:51:28 -0700323 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800324}
325
326bool OatFileAssistant::OdexFileIsOutOfDate() {
327 if (!odex_file_is_out_of_date_attempted_) {
328 odex_file_is_out_of_date_attempted_ = true;
329 const OatFile* odex_file = GetOdexFile();
330 if (odex_file == nullptr) {
331 cached_odex_file_is_out_of_date_ = true;
332 } else {
333 cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file);
334 }
335 }
336 return cached_odex_file_is_out_of_date_;
337}
338
339bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700340 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800341}
342
343bool OatFileAssistant::OdexFileIsUpToDate() {
344 if (!odex_file_is_up_to_date_attempted_) {
345 odex_file_is_up_to_date_attempted_ = true;
346 const OatFile* odex_file = GetOdexFile();
347 if (odex_file == nullptr) {
348 cached_odex_file_is_up_to_date_ = false;
349 } else {
350 cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
351 }
352 }
353 return cached_odex_file_is_up_to_date_;
354}
355
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800356std::string OatFileAssistant::ArtFileName(const OatFile* oat_file) const {
357 const std::string oat_file_location = oat_file->GetLocation();
358 // Replace extension with .art
359 const size_t last_ext = oat_file_location.find_last_of('.');
360 if (last_ext == std::string::npos) {
361 LOG(ERROR) << "No extension in oat file " << oat_file_location;
362 return std::string();
363 }
364 return oat_file_location.substr(0, last_ext) + ".art";
365}
366
Richard Uhler66d874d2015-01-15 09:37:19 -0800367const std::string* OatFileAssistant::OatFileName() {
368 if (!cached_oat_file_name_attempted_) {
369 cached_oat_file_name_attempted_ = true;
370
371 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 // TODO: The oat file assistant should be the definitive place for
373 // determining the oat file name from the dex location, not
374 // GetDalvikCacheFilename.
375 std::string cache_dir = StringPrintf("%s%s",
376 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
377 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700378 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700379 cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800380 if (!cached_oat_file_name_found_) {
381 // If we can't determine the oat file name, we treat the oat file as
382 // inaccessible.
383 LOG(WARNING) << "Failed to determine oat file name for dex location "
384 << dex_location_ << ": " << error_msg;
385 }
386 }
387 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
388}
389
390bool OatFileAssistant::OatFileExists() {
391 return GetOatFile() != nullptr;
392}
393
Richard Uhler95abd042015-03-24 09:51:28 -0700394OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800395 if (OatFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700396 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800397 }
398 if (OatFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700399 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800400 }
Richard Uhler95abd042015-03-24 09:51:28 -0700401 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800402}
403
404bool OatFileAssistant::OatFileIsOutOfDate() {
405 if (!oat_file_is_out_of_date_attempted_) {
406 oat_file_is_out_of_date_attempted_ = true;
407 const OatFile* oat_file = GetOatFile();
408 if (oat_file == nullptr) {
409 cached_oat_file_is_out_of_date_ = true;
410 } else {
411 cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file);
412 }
413 }
414 return cached_oat_file_is_out_of_date_;
415}
416
417bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700418 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800419}
420
421bool OatFileAssistant::OatFileIsUpToDate() {
422 if (!oat_file_is_up_to_date_attempted_) {
423 oat_file_is_up_to_date_attempted_ = true;
424 const OatFile* oat_file = GetOatFile();
425 if (oat_file == nullptr) {
426 cached_oat_file_is_up_to_date_ = false;
427 } else {
428 cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
429 }
430 }
431 return cached_oat_file_is_up_to_date_;
432}
433
Richard Uhler95abd042015-03-24 09:51:28 -0700434OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800435 // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which
436 // is more work than we need to do. If performance becomes a concern, and
437 // this method is actually called, this should be fixed.
438 if (GivenOatFileIsOutOfDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700439 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800440 }
441 if (GivenOatFileIsUpToDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700442 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800443 }
Richard Uhler95abd042015-03-24 09:51:28 -0700444 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800445}
446
447bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) {
448 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700449 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800450 // what we provide, which verifies the primary dex checksum for us.
451 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
452 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700453 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700454 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800455 return true;
456 }
457
458 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700459 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800460 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700461 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800462 const OatFile::OatDexFile* secondary_oat_dex_file
463 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700464 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800465 // There are no more secondary dex files to check.
466 break;
467 }
468
469 std::string error_msg;
470 uint32_t expected_secondary_checksum = 0;
471 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700472 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800473 uint32_t actual_secondary_checksum
474 = secondary_oat_dex_file->GetDexFileLocationChecksum();
475 if (expected_secondary_checksum != actual_secondary_checksum) {
476 VLOG(oat) << "Dex checksum does not match for secondary dex: "
477 << secondary_dex_location
478 << ". Expected: " << expected_secondary_checksum
479 << ", Actual: " << actual_secondary_checksum;
Richard Uhler67ff7d12015-05-14 13:21:13 -0700480 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800481 }
482 } else {
483 // If we can't get the checksum for the secondary location, we assume
484 // the dex checksum is up to date for this and all other secondary dex
485 // files.
486 break;
487 }
488 }
489
Richard Uhlera62d2f02016-03-18 15:05:30 -0700490 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
491 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000492
Richard Uhler66d874d2015-01-15 09:37:19 -0800493 // Verify the image checksum
Richard Uhlera62d2f02016-03-18 15:05:30 -0700494 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
495 const ImageInfo* image_info = GetImageInfo();
496 if (image_info == nullptr) {
497 VLOG(oat) << "No image for oat image checksum to match against.";
498 return true;
499 }
500
501 if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
502 VLOG(oat) << "Oat image checksum does not match image checksum.";
503 return true;
504 }
505 } else {
506 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800507 }
508
Richard Uhlera62d2f02016-03-18 15:05:30 -0700509 // Verify the profile hasn't changed recently.
510 // TODO: Move this check to OatFileCompilerFilterIsOkay? Nothing bad should
511 // happen if we use an oat file compiled with an out-of-date profile.
512 if (CompilerFilter::DependsOnProfile(current_compiler_filter)) {
513 if (profile_changed_) {
514 VLOG(oat) << "The profile has changed recently.";
515 return true;
516 }
517 } else {
518 VLOG(oat) << "Profile check skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800519 }
520
Richard Uhlera62d2f02016-03-18 15:05:30 -0700521 // Everything looks good; the dex file is not out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800522 return false;
523}
524
525bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700526 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800527}
528
529bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
530 if (GivenOatFileIsOutOfDate(file)) {
531 return false;
532 }
533
Richard Uhlera62d2f02016-03-18 15:05:30 -0700534 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
535
536 // Don't consider an oat file as up-to-date with compiled code unless
537 // compilation is enabled. This ensures we fall back to a non-executable oat
538 // file if there is no compiled code.
539 if (!CompilerFilter::IsCompilationEnabled(current_compiler_filter)) {
540 VLOG(oat) << "Compilation not enabled for " << current_compiler_filter;
541 return false;
542 }
543
544 if (!file.IsPic()) {
545 const ImageInfo* image_info = GetImageInfo();
546 if (image_info == nullptr) {
547 VLOG(oat) << "No image to check oat relocation against.";
548 return false;
549 }
550
551 // Verify the oat_data_begin recorded for the image in the oat file matches
552 // the actual oat_data_begin for boot.oat in the image.
553 const OatHeader& oat_header = file.GetOatHeader();
554 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
555 if (oat_data_begin != image_info->oat_data_begin) {
556 VLOG(oat) << file.GetLocation() <<
557 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
558 << " does not match actual image oat_data_begin ("
559 << image_info->oat_data_begin << ")";
560 return false;
561 }
562
563 // Verify the oat_patch_delta recorded for the image in the oat file matches
564 // the actual oat_patch_delta for the image.
565 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
566 if (oat_patch_delta != image_info->patch_delta) {
567 VLOG(oat) << file.GetLocation() <<
568 ": Oat file image patch delta (" << oat_patch_delta << ")"
569 << " does not match actual image patch delta ("
570 << image_info->patch_delta << ")";
571 return false;
572 }
573 } else {
David Brazdilce4b0ba2016-01-28 15:05:49 +0000574 // Oat files compiled in PIC mode do not require relocation and extract-only
575 // oat files do not contain any compiled code. Skip the relocation test.
Richard Uhlera62d2f02016-03-18 15:05:30 -0700576 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
Richard Uhler66d874d2015-01-15 09:37:19 -0800577 }
578 return true;
579}
580
Richard Uhler95abd042015-03-24 09:51:28 -0700581bool OatFileAssistant::RelocateOatFile(const std::string* input_file,
582 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800583 CHECK(error_msg != nullptr);
584
Richard Uhler95abd042015-03-24 09:51:28 -0700585 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700586 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700587 + " not attempted because the input file name could not be determined.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800588 return false;
589 }
Richard Uhler95abd042015-03-24 09:51:28 -0700590 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800591
592 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700593 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800594 + " not attempted because the oat file name could not be determined.";
595 return false;
596 }
597 const std::string& oat_file_name = *OatFileName();
598
599 const ImageInfo* image_info = GetImageInfo();
600 Runtime* runtime = Runtime::Current();
601 if (image_info == nullptr) {
602 *error_msg = "Patching of oat file " + oat_file_name
603 + " not attempted because no image location was found.";
604 return false;
605 }
606
607 if (!runtime->IsDex2OatEnabled()) {
608 *error_msg = "Patching of oat file " + oat_file_name
609 + " not attempted because dex2oat is disabled";
610 return false;
611 }
612
613 std::vector<std::string> argv;
614 argv.push_back(runtime->GetPatchoatExecutable());
615 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700616 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800617 argv.push_back("--output-oat-file=" + oat_file_name);
618 argv.push_back("--patched-image-location=" + image_info->location);
619
620 std::string command_line(Join(argv, ' '));
621 if (!Exec(argv, error_msg)) {
622 // Manually delete the file. This ensures there is no garbage left over if
623 // the process unexpectedly died.
624 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
625 return false;
626 }
627
628 // Mark that the oat file has changed and we should try to reload.
629 ClearOatFileCache();
630 return true;
631}
632
Richard Uhlera62d2f02016-03-18 15:05:30 -0700633bool OatFileAssistant::GenerateOatFile(CompilerFilter::Filter target, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800634 CHECK(error_msg != nullptr);
635
Richard Uhler8327cf72015-10-13 16:34:59 -0700636 Runtime* runtime = Runtime::Current();
637 if (!runtime->IsDex2OatEnabled()) {
638 *error_msg = "Generation of oat file for dex location " + dex_location_
639 + " not attempted because dex2oat is disabled.";
640 return false;
641 }
642
Richard Uhler66d874d2015-01-15 09:37:19 -0800643 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700644 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800645 + " not attempted because the oat file name could not be determined.";
646 return false;
647 }
648 const std::string& oat_file_name = *OatFileName();
649
Richard Uhler66d874d2015-01-15 09:37:19 -0800650 // dex2oat ignores missing dex files and doesn't report an error.
651 // Check explicitly here so we can detect the error properly.
652 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700653 if (!OS::FileExists(dex_location_.c_str())) {
654 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800655 return false;
656 }
657
Richard Uhler8327cf72015-10-13 16:34:59 -0700658 std::unique_ptr<File> oat_file;
659 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
660 if (oat_file.get() == nullptr) {
661 *error_msg = "Generation of oat file " + oat_file_name
662 + " not attempted because the oat file could not be created.";
663 return false;
664 }
665
666 if (fchmod(oat_file->Fd(), 0644) != 0) {
667 *error_msg = "Generation of oat file " + oat_file_name
668 + " not attempted because the oat file could not be made world readable.";
669 oat_file->Erase();
670 return false;
671 }
672
673 std::vector<std::string> args;
674 args.push_back("--dex-file=" + dex_location_);
675 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
676 args.push_back("--oat-location=" + oat_file_name);
Richard Uhlera62d2f02016-03-18 15:05:30 -0700677 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(target));
Richard Uhler8327cf72015-10-13 16:34:59 -0700678
Richard Uhler66d874d2015-01-15 09:37:19 -0800679 if (!Dex2Oat(args, error_msg)) {
680 // Manually delete the file. This ensures there is no garbage left over if
681 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700682 oat_file->Erase();
683 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
684 return false;
685 }
686
687 if (oat_file->FlushCloseOrErase() != 0) {
688 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler66d874d2015-01-15 09:37:19 -0800689 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
690 return false;
691 }
692
693 // Mark that the oat file has changed and we should try to reload.
694 ClearOatFileCache();
695 return true;
696}
697
698bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
699 std::string* error_msg) {
700 Runtime* runtime = Runtime::Current();
701 std::string image_location = ImageLocation();
702 if (image_location.empty()) {
703 *error_msg = "No image location found for Dex2Oat.";
704 return false;
705 }
706
707 std::vector<std::string> argv;
708 argv.push_back(runtime->GetCompilerExecutable());
709 argv.push_back("--runtime-arg");
710 argv.push_back("-classpath");
711 argv.push_back("--runtime-arg");
712 argv.push_back(runtime->GetClassPathString());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100713 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200714 argv.push_back("--debuggable");
715 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700716 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800717
718 if (!runtime->IsVerificationEnabled()) {
719 argv.push_back("--compiler-filter=verify-none");
720 }
721
722 if (runtime->MustRelocateIfPossible()) {
723 argv.push_back("--runtime-arg");
724 argv.push_back("-Xrelocate");
725 } else {
726 argv.push_back("--runtime-arg");
727 argv.push_back("-Xnorelocate");
728 }
729
730 if (!kIsTargetBuild) {
731 argv.push_back("--host");
732 }
733
734 argv.push_back("--boot-image=" + image_location);
735
736 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
737 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
738
739 argv.insert(argv.end(), args.begin(), args.end());
740
741 std::string command_line(Join(argv, ' '));
742 return Exec(argv, error_msg);
743}
744
745bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
746 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
747 CHECK(odex_filename != nullptr);
748 CHECK(error_msg != nullptr);
749
750 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700751 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800752 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700753 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800754
Richard Uhler63434112015-03-16 14:32:16 -0700755 // Find the directory portion of the dex location and add the oat/<isa>
756 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800757 size_t pos = location.rfind('/');
758 if (pos == std::string::npos) {
759 *error_msg = "Dex location " + location + " has no directory.";
760 return false;
761 }
762 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700763 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800764
765 // Find the file portion of the dex location.
766 std::string file;
767 if (pos == std::string::npos) {
768 file = location;
769 } else {
770 file = location.substr(pos+1);
771 }
772
773 // Get the base part of the file without the extension.
774 pos = file.rfind('.');
775 if (pos == std::string::npos) {
776 *error_msg = "Dex location " + location + " has no extension.";
777 return false;
778 }
779 std::string base = file.substr(0, pos);
780
781 *odex_filename = dir + "/" + base + ".odex";
782 return true;
783}
784
785std::string OatFileAssistant::DalvikCacheDirectory() {
786 // Note: We don't cache this, because it will only be called once by
Richard Uhlera62d2f02016-03-18 15:05:30 -0700787 // OatFileName.
Richard Uhler66d874d2015-01-15 09:37:19 -0800788
789 // TODO: The work done in GetDalvikCache is overkill for what we need.
790 // Ideally a new API for getting the DalvikCacheDirectory the way we want
791 // (without existence testing, creation, or death) is provided with the rest
792 // of the GetDalvikCache family of functions. Until such an API is in place,
793 // we use GetDalvikCache to avoid duplicating the logic for determining the
794 // dalvik cache directory.
795 std::string result;
796 bool have_android_data;
797 bool dalvik_cache_exists;
798 bool is_global_cache;
799 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
800 return result;
801}
802
Richard Uhler66d874d2015-01-15 09:37:19 -0800803std::string OatFileAssistant::ImageLocation() {
804 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000805 const std::vector<gc::space::ImageSpace*>& image_spaces =
806 runtime->GetHeap()->GetBootImageSpaces();
807 if (image_spaces.empty()) {
808 return "";
809 }
810 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800811}
812
813const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700814 if (!required_dex_checksum_attempted_) {
815 required_dex_checksum_attempted_ = true;
816 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800817 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700818 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700819 required_dex_checksum_found_ = true;
820 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800821 } else {
822 // This can happen if the original dex file has been stripped from the
823 // apk.
824 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700825 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800826
827 // Get the checksum from the odex if we can.
828 const OatFile* odex_file = GetOdexFile();
829 if (odex_file != nullptr) {
830 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700831 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800832 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700833 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
834 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800835 }
836 }
837 }
838 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700839 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800840}
841
842const OatFile* OatFileAssistant::GetOdexFile() {
843 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
844 if (!odex_file_load_attempted_) {
845 odex_file_load_attempted_ = true;
846 if (OdexFileName() != nullptr) {
847 const std::string& odex_file_name = *OdexFileName();
848 std::string error_msg;
849 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800850 odex_file_name.c_str(),
851 nullptr,
852 nullptr,
853 load_executable_,
854 /*low_4gb*/false,
855 dex_location_.c_str(),
856 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800857 if (cached_odex_file_.get() == nullptr) {
858 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
859 << odex_file_name << ": " << error_msg;
860 }
861 }
862 }
863 return cached_odex_file_.get();
864}
865
Richard Uhler5f946da2015-07-17 12:28:32 -0700866bool OatFileAssistant::OdexFileIsExecutable() {
867 const OatFile* odex_file = GetOdexFile();
868 return (odex_file != nullptr && odex_file->IsExecutable());
869}
870
Richard Uhler66d874d2015-01-15 09:37:19 -0800871void OatFileAssistant::ClearOdexFileCache() {
872 odex_file_load_attempted_ = false;
873 cached_odex_file_.reset();
874 odex_file_is_out_of_date_attempted_ = false;
875 odex_file_is_up_to_date_attempted_ = false;
876}
877
878const OatFile* OatFileAssistant::GetOatFile() {
879 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
880 if (!oat_file_load_attempted_) {
881 oat_file_load_attempted_ = true;
882 if (OatFileName() != nullptr) {
883 const std::string& oat_file_name = *OatFileName();
884 std::string error_msg;
885 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800886 oat_file_name.c_str(),
887 nullptr,
888 nullptr,
889 load_executable_,
890 /*low_4gb*/false,
891 dex_location_.c_str(),
892 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800893 if (cached_oat_file_.get() == nullptr) {
894 VLOG(oat) << "OatFileAssistant test for existing oat file "
895 << oat_file_name << ": " << error_msg;
896 }
897 }
898 }
899 return cached_oat_file_.get();
900}
901
Richard Uhler5f946da2015-07-17 12:28:32 -0700902bool OatFileAssistant::OatFileIsExecutable() {
903 const OatFile* oat_file = GetOatFile();
904 return (oat_file != nullptr && oat_file->IsExecutable());
905}
906
Richard Uhler66d874d2015-01-15 09:37:19 -0800907void OatFileAssistant::ClearOatFileCache() {
908 oat_file_load_attempted_ = false;
909 cached_oat_file_.reset();
910 oat_file_is_out_of_date_attempted_ = false;
911 oat_file_is_up_to_date_attempted_ = false;
912}
913
914const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
915 if (!image_info_load_attempted_) {
916 image_info_load_attempted_ = true;
917
918 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800919 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
920 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800921 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800922
923 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800924 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800925 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800926 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
927 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800928 cached_image_info_.patch_delta = image_header.GetPatchDelta();
929 } else {
930 std::unique_ptr<ImageHeader> image_header(
931 gc::space::ImageSpace::ReadImageHeaderOrDie(
932 cached_image_info_.location.c_str(), isa_));
933 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800934 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
935 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800936 cached_image_info_.patch_delta = image_header->GetPatchDelta();
937 }
938 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800939 image_info_load_succeeded_ = (!image_spaces.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -0800940 }
941 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
942}
943
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800944gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
945 DCHECK(oat_file != nullptr);
946 std::string art_file = ArtFileName(oat_file);
947 if (art_file.empty()) {
948 return nullptr;
949 }
950 std::string error_msg;
951 ScopedObjectAccess soa(Thread::Current());
952 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
953 oat_file,
954 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800955 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800956 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
957 }
958 return ret;
959}
960
Richard Uhler66d874d2015-01-15 09:37:19 -0800961} // namespace art
962