blob: 713e2f3fa98e08ea29ed0cf63866c4c1d230366f [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_assistant.h"
18
19#include <fcntl.h>
20#ifdef __linux__
21#include <sys/sendfile.h>
22#else
23#include <sys/socket.h>
24#endif
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include <set>
30
31#include "base/logging.h"
32#include "base/stringprintf.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010033#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "class_linker.h"
35#include "gc/heap.h"
36#include "gc/space/image_space.h"
37#include "image.h"
38#include "oat.h"
39#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040#include "runtime.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080041#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080042#include "ScopedFd.h"
43#include "utils.h"
44
45namespace art {
46
Narayan Kamath8943c1d2016-05-02 13:14:48 +010047std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
48 switch (status) {
49 case OatFileAssistant::kOatOutOfDate:
50 stream << "kOatOutOfDate";
51 break;
52 case OatFileAssistant::kOatUpToDate:
53 stream << "kOatUpToDate";
54 break;
55 case OatFileAssistant::kOatNeedsRelocation:
56 stream << "kOatNeedsRelocation";
57 break;
58 default:
59 UNREACHABLE();
60 }
61
62 return stream;
63}
64
Richard Uhler66d874d2015-01-15 09:37:19 -080065OatFileAssistant::OatFileAssistant(const char* dex_location,
66 const InstructionSet isa,
Andreas Gampe29d38e72016-03-23 15:31:51 +000067 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080068 bool load_executable)
Andreas Gampe29d38e72016-03-23 15:31:51 +000069 : OatFileAssistant(dex_location, nullptr, isa, profile_changed, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000070{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080071
72OatFileAssistant::OatFileAssistant(const char* dex_location,
73 const char* oat_location,
74 const InstructionSet isa,
Andreas Gampe29d38e72016-03-23 15:31:51 +000075 bool profile_changed,
Richard Uhler66d874d2015-01-15 09:37:19 -080076 bool load_executable)
Andreas Gampe29d38e72016-03-23 15:31:51 +000077 : isa_(isa), profile_changed_(profile_changed), load_executable_(load_executable) {
Richard Uhler740eec92015-10-15 15:12:23 -070078 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
79 dex_location_.assign(dex_location);
80
Richard Uhler66d874d2015-01-15 09:37:19 -080081 if (load_executable_ && isa != kRuntimeISA) {
82 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
83 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
84 load_executable_ = false;
85 }
86
87 // If the user gave a target oat location, save that as the cached oat
88 // location now so we won't try to construct the default location later.
89 if (oat_location != nullptr) {
90 cached_oat_file_name_ = std::string(oat_location);
91 cached_oat_file_name_attempted_ = true;
92 cached_oat_file_name_found_ = true;
93 }
Richard Uhler66d874d2015-01-15 09:37:19 -080094}
95
96OatFileAssistant::~OatFileAssistant() {
97 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -070098 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010099 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800100 }
101}
102
103bool OatFileAssistant::IsInBootClassPath() {
104 // Note: We check the current boot class path, regardless of the ISA
105 // specified by the user. This is okay, because the boot class path should
106 // be the same for all ISAs.
107 // TODO: Can we verify the boot class path is the same for all ISAs?
108 Runtime* runtime = Runtime::Current();
109 ClassLinker* class_linker = runtime->GetClassLinker();
110 const auto& boot_class_path = class_linker->GetBootClassPath();
111 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700112 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800113 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
114 return true;
115 }
116 }
117 return false;
118}
119
120bool OatFileAssistant::Lock(std::string* error_msg) {
121 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700122 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800123
124 if (OatFileName() == nullptr) {
125 *error_msg = "Failed to determine lock file";
126 return false;
127 }
128 std::string lock_file_name = *OatFileName() + ".flock";
129
Richard Uhler581f4e92015-05-07 10:19:35 -0700130 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100131 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800132 return false;
133 }
134 return true;
135}
136
Andreas Gampe29d38e72016-03-23 15:31:51 +0000137bool OatFileAssistant::OatFileCompilerFilterIsOkay(CompilerFilter::Filter target) {
138 const OatFile* oat_file = GetOatFile();
139 if (oat_file != nullptr) {
140 CompilerFilter::Filter current = oat_file->GetCompilerFilter();
141 return CompilerFilter::IsAsGoodAs(current, target);
142 }
143 return false;
Calin Juravleb077e152016-02-18 18:47:37 +0000144}
Richard Uhler66d874d2015-01-15 09:37:19 -0800145
Andreas Gampe29d38e72016-03-23 15:31:51 +0000146bool OatFileAssistant::OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target) {
147 const OatFile* odex_file = GetOdexFile();
148 if (odex_file != nullptr) {
149 CompilerFilter::Filter current = odex_file->GetCompilerFilter();
150 return CompilerFilter::IsAsGoodAs(current, target);
151 }
152 return false;
153}
154
155OatFileAssistant::DexOptNeeded OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target) {
156 bool compilation_desired = CompilerFilter::IsCompilationEnabled(target);
157
158 // See if the oat file is in good shape as is.
159 bool oat_okay = OatFileCompilerFilterIsOkay(target);
160 if (oat_okay) {
161 if (compilation_desired) {
162 if (OatFileIsUpToDate()) {
163 return kNoDexOptNeeded;
164 }
165 } else {
166 if (!OatFileIsOutOfDate()) {
167 return kNoDexOptNeeded;
168 }
169 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800170 }
Richard Uhler95abd042015-03-24 09:51:28 -0700171
Andreas Gampe29d38e72016-03-23 15:31:51 +0000172 // See if the odex file is in good shape as is.
173 bool odex_okay = OdexFileCompilerFilterIsOkay(target);
174 if (odex_okay) {
175 if (compilation_desired) {
176 if (OdexFileIsUpToDate()) {
177 return kNoDexOptNeeded;
178 }
179 } else {
180 if (!OdexFileIsOutOfDate()) {
181 return kNoDexOptNeeded;
182 }
183 }
Richard Uhler95abd042015-03-24 09:51:28 -0700184 }
185
Andreas Gampe29d38e72016-03-23 15:31:51 +0000186 // See if we can get an up-to-date file by running patchoat.
187 if (compilation_desired) {
Richard Uhlerd1537b52016-03-29 13:27:41 -0700188 if (odex_okay && OdexFileNeedsRelocation() && OdexFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000189 return kPatchOatNeeded;
190 }
191
Richard Uhlerd1537b52016-03-29 13:27:41 -0700192 if (oat_okay && OatFileNeedsRelocation() && OatFileHasPatchInfo()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000193 return kSelfPatchOatNeeded;
194 }
Richard Uhler95abd042015-03-24 09:51:28 -0700195 }
196
Andreas Gampe29d38e72016-03-23 15:31:51 +0000197 // We can only run dex2oat if there are original dex files.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700198 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800199}
200
Richard Uhlerf4b34872016-04-13 11:03:46 -0700201// Figure out the currently specified compile filter option in the runtime.
202// Returns true on success, false if the compiler filter is invalid, in which
203// case error_msg describes the problem.
204static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
205 std::string* error_msg) {
206 CHECK(filter != nullptr);
207 CHECK(error_msg != nullptr);
208
209 *filter = CompilerFilter::kDefaultCompilerFilter;
210 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
211 if (option.starts_with("--compiler-filter=")) {
212 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
213 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
214 *error_msg = std::string("Unknown --compiler-filter value: ")
215 + std::string(compiler_filter_string);
216 return false;
217 }
218 }
219 }
220 return true;
221}
222
Richard Uhler1e860612016-03-30 12:17:55 -0700223OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700224OatFileAssistant::MakeUpToDate(std::string* error_msg) {
225 CompilerFilter::Filter target;
226 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
227 return kUpdateNotAttempted;
228 }
229
Andreas Gampe29d38e72016-03-23 15:31:51 +0000230 switch (GetDexOptNeeded(target)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700231 case kNoDexOptNeeded: return kUpdateSucceeded;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700232 case kDex2OatNeeded: return GenerateOatFile(error_msg);
Richard Uhler95abd042015-03-24 09:51:28 -0700233 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
234 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800235 }
236 UNREACHABLE();
237}
238
239std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700240 // The best oat files are, in descending order of bestness:
241 // 1. Properly relocated files. These may be opened executable.
242 // 2. Not out-of-date files that are already opened non-executable.
243 // 3. Not out-of-date files that we must reopen non-executable.
244
Richard Uhler66d874d2015-01-15 09:37:19 -0800245 if (OatFileIsUpToDate()) {
246 oat_file_released_ = true;
247 return std::move(cached_oat_file_);
248 }
249
250 if (OdexFileIsUpToDate()) {
251 oat_file_released_ = true;
252 return std::move(cached_odex_file_);
253 }
254
Richard Uhler5f946da2015-07-17 12:28:32 -0700255 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
256 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800257
Richard Uhler5f946da2015-07-17 12:28:32 -0700258 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
259 oat_file_released_ = true;
260 return std::move(cached_oat_file_);
261 }
262
263 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
264 oat_file_released_ = true;
265 return std::move(cached_odex_file_);
266 }
267
268 if (!OatFileIsOutOfDate()) {
269 load_executable_ = false;
270 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700272 CHECK(!OatFileIsExecutable());
273 oat_file_released_ = true;
274 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800275 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700276 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800277
Richard Uhler5f946da2015-07-17 12:28:32 -0700278 if (!OdexFileIsOutOfDate()) {
279 load_executable_ = false;
280 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800281 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700282 CHECK(!OdexFileIsExecutable());
283 oat_file_released_ = true;
284 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 }
286 }
287
288 return std::unique_ptr<OatFile>();
289}
290
291std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
292 const OatFile& oat_file, const char* dex_location) {
293 std::vector<std::unique_ptr<const DexFile>> dex_files;
294
295 // Load the primary dex file.
296 std::string error_msg;
297 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
298 dex_location, nullptr, false);
299 if (oat_dex_file == nullptr) {
300 LOG(WARNING) << "Attempt to load out-of-date oat file "
301 << oat_file.GetLocation() << " for dex location " << dex_location;
302 return std::vector<std::unique_ptr<const DexFile>>();
303 }
304
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700305 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800306 if (dex_file.get() == nullptr) {
307 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
308 return std::vector<std::unique_ptr<const DexFile>>();
309 }
310 dex_files.push_back(std::move(dex_file));
311
312 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700313 for (size_t i = 1; ; i++) {
314 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800315 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700316 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800317 // There are no more secondary dex files to load.
318 break;
319 }
320
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700321 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 if (dex_file.get() == nullptr) {
323 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
324 return std::vector<std::unique_ptr<const DexFile>>();
325 }
326 dex_files.push_back(std::move(dex_file));
327 }
328 return dex_files;
329}
330
Richard Uhler9b994ea2015-06-24 08:44:19 -0700331bool OatFileAssistant::HasOriginalDexFiles() {
332 // Ensure GetRequiredDexChecksum has been run so that
333 // has_original_dex_files_ is initialized. We don't care about the result of
334 // GetRequiredDexChecksum.
335 GetRequiredDexChecksum();
336 return has_original_dex_files_;
337}
338
Richard Uhler66d874d2015-01-15 09:37:19 -0800339const std::string* OatFileAssistant::OdexFileName() {
340 if (!cached_odex_file_name_attempted_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800341 cached_odex_file_name_attempted_ = true;
342
343 std::string error_msg;
344 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700345 dex_location_, isa_, &cached_odex_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800346 if (!cached_odex_file_name_found_) {
347 // If we can't figure out the odex file, we treat it as if the odex
348 // file was inaccessible.
349 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
350 }
351 }
352 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
353}
354
355bool OatFileAssistant::OdexFileExists() {
356 return GetOdexFile() != nullptr;
357}
358
Richard Uhler95abd042015-03-24 09:51:28 -0700359OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800360 if (OdexFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700361 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800362 }
363 if (OdexFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700364 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800365 }
Richard Uhler95abd042015-03-24 09:51:28 -0700366 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800367}
368
369bool OatFileAssistant::OdexFileIsOutOfDate() {
370 if (!odex_file_is_out_of_date_attempted_) {
371 odex_file_is_out_of_date_attempted_ = true;
372 const OatFile* odex_file = GetOdexFile();
373 if (odex_file == nullptr) {
374 cached_odex_file_is_out_of_date_ = true;
375 } else {
376 cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file);
377 }
378 }
379 return cached_odex_file_is_out_of_date_;
380}
381
382bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700383 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800384}
385
386bool OatFileAssistant::OdexFileIsUpToDate() {
387 if (!odex_file_is_up_to_date_attempted_) {
388 odex_file_is_up_to_date_attempted_ = true;
389 const OatFile* odex_file = GetOdexFile();
390 if (odex_file == nullptr) {
391 cached_odex_file_is_up_to_date_ = false;
392 } else {
393 cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
394 }
395 }
396 return cached_odex_file_is_up_to_date_;
397}
398
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100399CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() {
400 const OatFile* odex_file = GetOdexFile();
401 CHECK(odex_file != nullptr);
402
403 return odex_file->GetCompilerFilter();
404}
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800405std::string OatFileAssistant::ArtFileName(const OatFile* oat_file) const {
406 const std::string oat_file_location = oat_file->GetLocation();
407 // Replace extension with .art
408 const size_t last_ext = oat_file_location.find_last_of('.');
409 if (last_ext == std::string::npos) {
410 LOG(ERROR) << "No extension in oat file " << oat_file_location;
411 return std::string();
412 }
413 return oat_file_location.substr(0, last_ext) + ".art";
414}
415
Richard Uhler66d874d2015-01-15 09:37:19 -0800416const std::string* OatFileAssistant::OatFileName() {
417 if (!cached_oat_file_name_attempted_) {
418 cached_oat_file_name_attempted_ = true;
419
420 // Compute the oat file name from the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800421 // TODO: The oat file assistant should be the definitive place for
422 // determining the oat file name from the dex location, not
423 // GetDalvikCacheFilename.
424 std::string cache_dir = StringPrintf("%s%s",
425 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
426 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700427 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700428 cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800429 if (!cached_oat_file_name_found_) {
430 // If we can't determine the oat file name, we treat the oat file as
431 // inaccessible.
432 LOG(WARNING) << "Failed to determine oat file name for dex location "
433 << dex_location_ << ": " << error_msg;
434 }
435 }
436 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
437}
438
439bool OatFileAssistant::OatFileExists() {
440 return GetOatFile() != nullptr;
441}
442
Richard Uhler95abd042015-03-24 09:51:28 -0700443OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800444 if (OatFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700445 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800446 }
447 if (OatFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700448 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 }
Richard Uhler95abd042015-03-24 09:51:28 -0700450 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800451}
452
453bool OatFileAssistant::OatFileIsOutOfDate() {
454 if (!oat_file_is_out_of_date_attempted_) {
455 oat_file_is_out_of_date_attempted_ = true;
456 const OatFile* oat_file = GetOatFile();
457 if (oat_file == nullptr) {
458 cached_oat_file_is_out_of_date_ = true;
459 } else {
460 cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file);
461 }
462 }
463 return cached_oat_file_is_out_of_date_;
464}
465
466bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700467 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800468}
469
470bool OatFileAssistant::OatFileIsUpToDate() {
471 if (!oat_file_is_up_to_date_attempted_) {
472 oat_file_is_up_to_date_attempted_ = true;
473 const OatFile* oat_file = GetOatFile();
474 if (oat_file == nullptr) {
475 cached_oat_file_is_up_to_date_ = false;
476 } else {
477 cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
478 }
479 }
480 return cached_oat_file_is_up_to_date_;
481}
482
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100483CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() {
484 const OatFile* oat_file = GetOatFile();
485 CHECK(oat_file != nullptr);
486
487 return oat_file->GetCompilerFilter();
488}
489
Richard Uhler95abd042015-03-24 09:51:28 -0700490OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800491 // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which
492 // is more work than we need to do. If performance becomes a concern, and
493 // this method is actually called, this should be fixed.
494 if (GivenOatFileIsOutOfDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700495 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800496 }
497 if (GivenOatFileIsUpToDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700498 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800499 }
Richard Uhler95abd042015-03-24 09:51:28 -0700500 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800501}
502
503bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) {
504 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700505 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800506 // what we provide, which verifies the primary dex checksum for us.
507 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
508 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700509 dex_location_.c_str(), dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700510 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800511 return true;
512 }
513
514 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700515 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800516 std::string secondary_dex_location
Richard Uhler740eec92015-10-15 15:12:23 -0700517 = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800518 const OatFile::OatDexFile* secondary_oat_dex_file
519 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700520 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800521 // There are no more secondary dex files to check.
522 break;
523 }
524
525 std::string error_msg;
526 uint32_t expected_secondary_checksum = 0;
527 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700528 &expected_secondary_checksum, &error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800529 uint32_t actual_secondary_checksum
530 = secondary_oat_dex_file->GetDexFileLocationChecksum();
531 if (expected_secondary_checksum != actual_secondary_checksum) {
532 VLOG(oat) << "Dex checksum does not match for secondary dex: "
533 << secondary_dex_location
534 << ". Expected: " << expected_secondary_checksum
535 << ", Actual: " << actual_secondary_checksum;
Richard Uhler67ff7d12015-05-14 13:21:13 -0700536 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800537 }
538 } else {
539 // If we can't get the checksum for the secondary location, we assume
540 // the dex checksum is up to date for this and all other secondary dex
541 // files.
542 break;
543 }
544 }
545
Andreas Gampe29d38e72016-03-23 15:31:51 +0000546 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
547 VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter;
David Brazdilce4b0ba2016-01-28 15:05:49 +0000548
Richard Uhler66d874d2015-01-15 09:37:19 -0800549 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000550 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
551 const ImageInfo* image_info = GetImageInfo();
552 if (image_info == nullptr) {
553 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000554
Richard Uhler76f5cb62016-04-04 13:30:16 -0700555 if (HasOriginalDexFiles()) {
556 return true;
557 }
558
559 // If there is no original dex file to fall back to, grudgingly accept
560 // the oat file. This could technically lead to crashes, but there's no
561 // way we could find a better oat file to use for this dex location,
562 // and it's better than being stuck in a boot loop with no way out.
563 // The problem will hopefully resolve itself the next time the runtime
564 // starts up.
565 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
566 << "Allow oat file use. This is potentially dangerous.";
567 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
568 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000569 VLOG(oat) << "Oat image checksum does not match image checksum.";
570 return true;
571 }
572 } else {
573 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800574 }
575
Andreas Gampe29d38e72016-03-23 15:31:51 +0000576 // Verify the profile hasn't changed recently.
577 // TODO: Move this check to OatFileCompilerFilterIsOkay? Nothing bad should
578 // happen if we use an oat file compiled with an out-of-date profile.
579 if (CompilerFilter::DependsOnProfile(current_compiler_filter)) {
580 if (profile_changed_) {
581 VLOG(oat) << "The profile has changed recently.";
582 return true;
583 }
584 } else {
585 VLOG(oat) << "Profile check skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800586 }
587
Andreas Gampe29d38e72016-03-23 15:31:51 +0000588 // Everything looks good; the dex file is not out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -0800589 return false;
590}
591
592bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700593 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800594}
595
596bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
597 if (GivenOatFileIsOutOfDate(file)) {
598 return false;
599 }
600
Andreas Gampe29d38e72016-03-23 15:31:51 +0000601 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
Richard Uhlera62d2f02016-03-18 15:05:30 -0700602
Andreas Gampe29d38e72016-03-23 15:31:51 +0000603 if (CompilerFilter::IsCompilationEnabled(current_compiler_filter)) {
604 if (!file.IsPic()) {
605 const ImageInfo* image_info = GetImageInfo();
606 if (image_info == nullptr) {
607 VLOG(oat) << "No image to check oat relocation against.";
608 return false;
609 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700610
Andreas Gampe29d38e72016-03-23 15:31:51 +0000611 // Verify the oat_data_begin recorded for the image in the oat file matches
612 // the actual oat_data_begin for boot.oat in the image.
613 const OatHeader& oat_header = file.GetOatHeader();
614 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
615 if (oat_data_begin != image_info->oat_data_begin) {
616 VLOG(oat) << file.GetLocation() <<
617 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
618 << " does not match actual image oat_data_begin ("
619 << image_info->oat_data_begin << ")";
620 return false;
621 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700622
Andreas Gampe29d38e72016-03-23 15:31:51 +0000623 // Verify the oat_patch_delta recorded for the image in the oat file matches
624 // the actual oat_patch_delta for the image.
625 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
626 if (oat_patch_delta != image_info->patch_delta) {
627 VLOG(oat) << file.GetLocation() <<
628 ": Oat file image patch delta (" << oat_patch_delta << ")"
629 << " does not match actual image patch delta ("
630 << image_info->patch_delta << ")";
631 return false;
632 }
633 } else {
634 // Oat files compiled in PIC mode do not require relocation.
635 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
636 }
637 } else {
638 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800639 }
640 return true;
641}
642
Richard Uhler1e860612016-03-30 12:17:55 -0700643OatFileAssistant::ResultOfAttemptToUpdate
644OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800645 CHECK(error_msg != nullptr);
646
Richard Uhler95abd042015-03-24 09:51:28 -0700647 if (input_file == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700648 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler95abd042015-03-24 09:51:28 -0700649 + " not attempted because the input file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700650 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800651 }
Richard Uhler95abd042015-03-24 09:51:28 -0700652 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800653
654 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700655 *error_msg = "Patching of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800656 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700657 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800658 }
659 const std::string& oat_file_name = *OatFileName();
660
661 const ImageInfo* image_info = GetImageInfo();
662 Runtime* runtime = Runtime::Current();
663 if (image_info == nullptr) {
664 *error_msg = "Patching of oat file " + oat_file_name
665 + " not attempted because no image location was found.";
Richard Uhler1e860612016-03-30 12:17:55 -0700666 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800667 }
668
669 if (!runtime->IsDex2OatEnabled()) {
670 *error_msg = "Patching of oat file " + oat_file_name
671 + " not attempted because dex2oat is disabled";
Richard Uhler1e860612016-03-30 12:17:55 -0700672 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800673 }
674
675 std::vector<std::string> argv;
676 argv.push_back(runtime->GetPatchoatExecutable());
677 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700678 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800679 argv.push_back("--output-oat-file=" + oat_file_name);
680 argv.push_back("--patched-image-location=" + image_info->location);
681
682 std::string command_line(Join(argv, ' '));
683 if (!Exec(argv, error_msg)) {
684 // Manually delete the file. This ensures there is no garbage left over if
685 // the process unexpectedly died.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100686 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700687 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800688 }
689
690 // Mark that the oat file has changed and we should try to reload.
691 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700692 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800693}
694
Richard Uhler1e860612016-03-30 12:17:55 -0700695OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700696OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800697 CHECK(error_msg != nullptr);
698
Richard Uhler8327cf72015-10-13 16:34:59 -0700699 Runtime* runtime = Runtime::Current();
700 if (!runtime->IsDex2OatEnabled()) {
701 *error_msg = "Generation of oat file for dex location " + dex_location_
702 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700703 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700704 }
705
Richard Uhler66d874d2015-01-15 09:37:19 -0800706 if (OatFileName() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700707 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800708 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700709 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800710 }
711 const std::string& oat_file_name = *OatFileName();
712
Richard Uhler66d874d2015-01-15 09:37:19 -0800713 // dex2oat ignores missing dex files and doesn't report an error.
714 // Check explicitly here so we can detect the error properly.
715 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700716 if (!OS::FileExists(dex_location_.c_str())) {
717 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700718 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800719 }
720
Richard Uhler8327cf72015-10-13 16:34:59 -0700721 std::unique_ptr<File> oat_file;
722 oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
723 if (oat_file.get() == nullptr) {
724 *error_msg = "Generation of oat file " + oat_file_name
725 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700726 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700727 }
728
729 if (fchmod(oat_file->Fd(), 0644) != 0) {
730 *error_msg = "Generation of oat file " + oat_file_name
731 + " not attempted because the oat file could not be made world readable.";
732 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700733 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700734 }
735
736 std::vector<std::string> args;
737 args.push_back("--dex-file=" + dex_location_);
738 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
739 args.push_back("--oat-location=" + oat_file_name);
740
Richard Uhler66d874d2015-01-15 09:37:19 -0800741 if (!Dex2Oat(args, error_msg)) {
742 // Manually delete the file. This ensures there is no garbage left over if
743 // the process unexpectedly died.
Richard Uhler8327cf72015-10-13 16:34:59 -0700744 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100745 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700746 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700747 }
748
749 if (oat_file->FlushCloseOrErase() != 0) {
750 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100751 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700752 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800753 }
754
755 // Mark that the oat file has changed and we should try to reload.
756 ClearOatFileCache();
Richard Uhler1e860612016-03-30 12:17:55 -0700757 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800758}
759
760bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
761 std::string* error_msg) {
762 Runtime* runtime = Runtime::Current();
763 std::string image_location = ImageLocation();
764 if (image_location.empty()) {
765 *error_msg = "No image location found for Dex2Oat.";
766 return false;
767 }
768
769 std::vector<std::string> argv;
770 argv.push_back(runtime->GetCompilerExecutable());
771 argv.push_back("--runtime-arg");
772 argv.push_back("-classpath");
773 argv.push_back("--runtime-arg");
774 argv.push_back(runtime->GetClassPathString());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100775 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200776 argv.push_back("--debuggable");
777 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700778 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800779
780 if (!runtime->IsVerificationEnabled()) {
781 argv.push_back("--compiler-filter=verify-none");
782 }
783
784 if (runtime->MustRelocateIfPossible()) {
785 argv.push_back("--runtime-arg");
786 argv.push_back("-Xrelocate");
787 } else {
788 argv.push_back("--runtime-arg");
789 argv.push_back("-Xnorelocate");
790 }
791
792 if (!kIsTargetBuild) {
793 argv.push_back("--host");
794 }
795
796 argv.push_back("--boot-image=" + image_location);
797
798 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
799 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
800
801 argv.insert(argv.end(), args.begin(), args.end());
802
803 std::string command_line(Join(argv, ' '));
804 return Exec(argv, error_msg);
805}
806
807bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
808 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
809 CHECK(odex_filename != nullptr);
810 CHECK(error_msg != nullptr);
811
812 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700813 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800814 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700815 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800816
Richard Uhler63434112015-03-16 14:32:16 -0700817 // Find the directory portion of the dex location and add the oat/<isa>
818 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800819 size_t pos = location.rfind('/');
820 if (pos == std::string::npos) {
821 *error_msg = "Dex location " + location + " has no directory.";
822 return false;
823 }
824 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700825 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800826
827 // Find the file portion of the dex location.
828 std::string file;
829 if (pos == std::string::npos) {
830 file = location;
831 } else {
832 file = location.substr(pos+1);
833 }
834
835 // Get the base part of the file without the extension.
836 pos = file.rfind('.');
837 if (pos == std::string::npos) {
838 *error_msg = "Dex location " + location + " has no extension.";
839 return false;
840 }
841 std::string base = file.substr(0, pos);
842
843 *odex_filename = dir + "/" + base + ".odex";
844 return true;
845}
846
847std::string OatFileAssistant::DalvikCacheDirectory() {
848 // Note: We don't cache this, because it will only be called once by
Andreas Gampe29d38e72016-03-23 15:31:51 +0000849 // OatFileName.
Richard Uhler66d874d2015-01-15 09:37:19 -0800850
851 // TODO: The work done in GetDalvikCache is overkill for what we need.
852 // Ideally a new API for getting the DalvikCacheDirectory the way we want
853 // (without existence testing, creation, or death) is provided with the rest
854 // of the GetDalvikCache family of functions. Until such an API is in place,
855 // we use GetDalvikCache to avoid duplicating the logic for determining the
856 // dalvik cache directory.
857 std::string result;
858 bool have_android_data;
859 bool dalvik_cache_exists;
860 bool is_global_cache;
861 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
862 return result;
863}
864
Richard Uhler66d874d2015-01-15 09:37:19 -0800865std::string OatFileAssistant::ImageLocation() {
866 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000867 const std::vector<gc::space::ImageSpace*>& image_spaces =
868 runtime->GetHeap()->GetBootImageSpaces();
869 if (image_spaces.empty()) {
870 return "";
871 }
872 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800873}
874
875const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700876 if (!required_dex_checksum_attempted_) {
877 required_dex_checksum_attempted_ = true;
878 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800879 std::string error_msg;
Richard Uhler740eec92015-10-15 15:12:23 -0700880 if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700881 required_dex_checksum_found_ = true;
882 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800883 } else {
884 // This can happen if the original dex file has been stripped from the
885 // apk.
886 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700887 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800888
889 // Get the checksum from the odex if we can.
890 const OatFile* odex_file = GetOdexFile();
891 if (odex_file != nullptr) {
892 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
Richard Uhler740eec92015-10-15 15:12:23 -0700893 dex_location_.c_str(), nullptr, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800894 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700895 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
896 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800897 }
898 }
899 }
900 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700901 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800902}
903
904const OatFile* OatFileAssistant::GetOdexFile() {
905 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
906 if (!odex_file_load_attempted_) {
907 odex_file_load_attempted_ = true;
908 if (OdexFileName() != nullptr) {
909 const std::string& odex_file_name = *OdexFileName();
910 std::string error_msg;
911 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800912 odex_file_name.c_str(),
913 nullptr,
914 nullptr,
915 load_executable_,
916 /*low_4gb*/false,
917 dex_location_.c_str(),
918 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800919 if (cached_odex_file_.get() == nullptr) {
920 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
921 << odex_file_name << ": " << error_msg;
922 }
923 }
924 }
925 return cached_odex_file_.get();
926}
927
Richard Uhler5f946da2015-07-17 12:28:32 -0700928bool OatFileAssistant::OdexFileIsExecutable() {
929 const OatFile* odex_file = GetOdexFile();
930 return (odex_file != nullptr && odex_file->IsExecutable());
931}
932
Richard Uhlerd1537b52016-03-29 13:27:41 -0700933bool OatFileAssistant::OdexFileHasPatchInfo() {
934 const OatFile* odex_file = GetOdexFile();
935 return (odex_file != nullptr && odex_file->HasPatchInfo());
936}
937
Richard Uhler66d874d2015-01-15 09:37:19 -0800938void OatFileAssistant::ClearOdexFileCache() {
939 odex_file_load_attempted_ = false;
940 cached_odex_file_.reset();
941 odex_file_is_out_of_date_attempted_ = false;
942 odex_file_is_up_to_date_attempted_ = false;
943}
944
945const OatFile* OatFileAssistant::GetOatFile() {
946 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
947 if (!oat_file_load_attempted_) {
948 oat_file_load_attempted_ = true;
949 if (OatFileName() != nullptr) {
950 const std::string& oat_file_name = *OatFileName();
951 std::string error_msg;
952 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800953 oat_file_name.c_str(),
954 nullptr,
955 nullptr,
956 load_executable_,
957 /*low_4gb*/false,
958 dex_location_.c_str(),
959 &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800960 if (cached_oat_file_.get() == nullptr) {
961 VLOG(oat) << "OatFileAssistant test for existing oat file "
962 << oat_file_name << ": " << error_msg;
963 }
964 }
965 }
966 return cached_oat_file_.get();
967}
968
Richard Uhler5f946da2015-07-17 12:28:32 -0700969bool OatFileAssistant::OatFileIsExecutable() {
970 const OatFile* oat_file = GetOatFile();
971 return (oat_file != nullptr && oat_file->IsExecutable());
972}
973
Richard Uhlerd1537b52016-03-29 13:27:41 -0700974bool OatFileAssistant::OatFileHasPatchInfo() {
975 const OatFile* oat_file = GetOatFile();
976 return (oat_file != nullptr && oat_file->HasPatchInfo());
977}
978
Richard Uhler66d874d2015-01-15 09:37:19 -0800979void OatFileAssistant::ClearOatFileCache() {
980 oat_file_load_attempted_ = false;
981 cached_oat_file_.reset();
982 oat_file_is_out_of_date_attempted_ = false;
983 oat_file_is_up_to_date_attempted_ = false;
984}
985
986const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
987 if (!image_info_load_attempted_) {
988 image_info_load_attempted_ = true;
989
990 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800991 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
992 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800993 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800994
995 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800996 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800997 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800998 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
999 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -08001000 cached_image_info_.patch_delta = image_header.GetPatchDelta();
1001 } else {
1002 std::unique_ptr<ImageHeader> image_header(
Jeff Haob11ffb72016-04-07 15:40:54 -07001003 gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_));
Richard Uhler66d874d2015-01-15 09:37:19 -08001004 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -08001005 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
1006 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -08001007 cached_image_info_.patch_delta = image_header->GetPatchDelta();
1008 }
1009 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001010 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -07001011
Jeff Haofd336c32016-04-07 19:46:31 -07001012 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -08001013 }
1014 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
1015}
1016
Jeff Haob11ffb72016-04-07 15:40:54 -07001017// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -07001018uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -07001019 uint32_t checksum = 0;
1020 std::vector<gc::space::ImageSpace*> image_spaces =
1021 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -07001022 if (isa == kRuntimeISA) {
1023 for (gc::space::ImageSpace* image_space : image_spaces) {
1024 checksum ^= image_space->GetImageHeader().GetOatChecksum();
1025 }
1026 } else {
1027 for (gc::space::ImageSpace* image_space : image_spaces) {
1028 std::string location = image_space->GetImageLocation();
1029 std::unique_ptr<ImageHeader> image_header(
1030 gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa));
1031 checksum ^= image_header->GetOatChecksum();
1032 }
Jeff Haob11ffb72016-04-07 15:40:54 -07001033 }
1034 return checksum;
1035}
1036
1037uint32_t OatFileAssistant::GetCombinedImageChecksum() {
1038 if (!image_info_load_attempted_) {
1039 GetImageInfo();
1040 }
1041 return combined_image_checksum_;
1042}
1043
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001044gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
1045 DCHECK(oat_file != nullptr);
1046 std::string art_file = ArtFileName(oat_file);
1047 if (art_file.empty()) {
1048 return nullptr;
1049 }
1050 std::string error_msg;
1051 ScopedObjectAccess soa(Thread::Current());
1052 gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(),
1053 oat_file,
1054 &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001055 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001056 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1057 }
1058 return ret;
1059}
1060
Richard Uhler66d874d2015-01-15 09:37:19 -08001061} // namespace art
1062