blob: 2e2e8c3ef6a1846e3a4bdf6be4fb67536297df36 [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
Richard Uhler46cc64f2016-11-14 14:53:55 +000019#include <sstream>
20
Richard Uhler66d874d2015-01-15 09:37:19 -080021#include <sys/stat.h>
Andreas Gampe9186ced2016-12-12 14:28:21 -080022
Andreas Gampec15a2f42017-04-21 12:09:39 -070023#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080024#include "android-base/strings.h"
25
Richard Uhler66d874d2015-01-15 09:37:19 -080026#include "base/logging.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070027#include "base/stl_util.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010028#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "class_linker.h"
David Sehr97c381e2017-02-01 15:09:58 -080030#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "gc/heap.h"
32#include "gc/space/image_space.h"
33#include "image.h"
34#include "oat.h"
35#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070037#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000039#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040
41namespace art {
42
Richard Uhler69bcf2c2017-01-24 10:25:21 +000043using android::base::StringPrintf;
44
Narayan Kamath8943c1d2016-05-02 13:14:48 +010045std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
46 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000047 case OatFileAssistant::kOatCannotOpen:
48 stream << "kOatCannotOpen";
49 break;
50 case OatFileAssistant::kOatDexOutOfDate:
51 stream << "kOatDexOutOfDate";
52 break;
53 case OatFileAssistant::kOatBootImageOutOfDate:
54 stream << "kOatBootImageOutOfDate";
55 break;
56 case OatFileAssistant::kOatRelocationOutOfDate:
57 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010058 break;
59 case OatFileAssistant::kOatUpToDate:
60 stream << "kOatUpToDate";
61 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010062 default:
63 UNREACHABLE();
64 }
65
66 return stream;
67}
68
Richard Uhler66d874d2015-01-15 09:37:19 -080069OatFileAssistant::OatFileAssistant(const char* dex_location,
70 const InstructionSet isa,
71 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000072 : isa_(isa),
73 load_executable_(load_executable),
74 odex_(this, /*is_oat_location*/ false),
75 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070076 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000077
78 // Try to get the realpath for the dex location.
79 //
80 // This is OK with respect to dalvik cache naming scheme because we never
81 // generate oat files starting from symlinks which go into dalvik cache.
82 // (recall that the oat files in dalvik cache are encoded by replacing '/'
83 // with '@' in the path).
84 // The boot image oat files (which are symlinked in dalvik-cache) are not
85 // loaded via the oat file assistant.
86 //
87 // The only case when the dex location may resolve to a different path
88 // is for secondary dex files (e.g. /data/user/0 symlinks to /data/data and
89 // the app is free to create its own internal layout). Related to this it is
90 // worthwhile to mention that installd resolves the secondary dex location
91 // before calling dex2oat.
92 UniqueCPtr<const char[]> dex_location_real(realpath(dex_location, nullptr));
93 if (dex_location_real != nullptr) {
94 dex_location_.assign(dex_location_real.get());
95 } else {
96 // If we can't get the realpath of the location there's not much point in trying to move on.
97 PLOG(ERROR) << "Could not get the realpath of dex_location " << dex_location;
98 return;
99 }
Richard Uhler740eec92015-10-15 15:12:23 -0700100
Richard Uhler66d874d2015-01-15 09:37:19 -0800101 if (load_executable_ && isa != kRuntimeISA) {
102 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
103 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
104 load_executable_ = false;
105 }
106
Richard Uhler743bf362016-04-19 15:39:37 -0700107 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700108 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700109 std::string odex_file_name;
110 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
111 odex_.Reset(odex_file_name);
112 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700113 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
114 }
115
Richard Uhler743bf362016-04-19 15:39:37 -0700116 // Get the oat filename.
Calin Juravle357c66d2017-05-04 01:57:17 +0000117 std::string oat_file_name;
118 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
119 oat_.Reset(oat_file_name);
Richard Uhlerd684f522016-04-19 13:24:41 -0700120 } else {
Calin Juravle357c66d2017-05-04 01:57:17 +0000121 LOG(WARNING) << "Failed to determine oat file name for dex location "
Calin Juravle9bfc6bb2017-05-04 00:13:50 +0000122 << dex_location_ << ": " << error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +0000123 }
124
125 // Check if the dex directory is writable.
126 // This will be needed in most uses of OatFileAssistant and so it's OK to
127 // compute it eagerly. (the only use which will not make use of it is
128 // OatFileAssistant::GetStatusDump())
129 size_t pos = dex_location_.rfind('/');
130 if (pos == std::string::npos) {
131 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
132 } else {
133 std::string parent = dex_location_.substr(0, pos);
134 if (access(parent.c_str(), W_OK) == 0) {
135 dex_parent_writable_ = true;
136 } else {
137 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700138 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800139 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800140}
141
142OatFileAssistant::~OatFileAssistant() {
143 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700144 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100145 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800146 }
147}
148
149bool OatFileAssistant::IsInBootClassPath() {
150 // Note: We check the current boot class path, regardless of the ISA
151 // specified by the user. This is okay, because the boot class path should
152 // be the same for all ISAs.
153 // TODO: Can we verify the boot class path is the same for all ISAs?
154 Runtime* runtime = Runtime::Current();
155 ClassLinker* class_linker = runtime->GetClassLinker();
156 const auto& boot_class_path = class_linker->GetBootClassPath();
157 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700158 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800159 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
160 return true;
161 }
162 }
163 return false;
164}
165
166bool OatFileAssistant::Lock(std::string* error_msg) {
167 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700168 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800169
Calin Juravle357c66d2017-05-04 01:57:17 +0000170 // Note the lock will only succeed for secondary dex files and in test
171 // environment.
172 //
173 // The lock *will fail* for all primary apks in a production environment.
174 // The app does not have permissions to create locks next to its dex location
175 // (be it system, data or vendor parition). We also cannot use the odex or
176 // oat location for the same reasoning.
177 //
178 // This is best effort and if it fails it's unlikely that we will be able
179 // to generate oat files anyway.
180 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800181
Richard Uhler581f4e92015-05-07 10:19:35 -0700182 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100183 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800184 return false;
185 }
186 return true;
187}
188
Richard Uhler7225a8d2016-11-22 10:12:03 +0000189int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000190 OatFileInfo& info = GetBestInfo();
191 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000192 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
193 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800194 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000195 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800196}
197
Richard Uhlerf4b34872016-04-13 11:03:46 -0700198// Figure out the currently specified compile filter option in the runtime.
199// Returns true on success, false if the compiler filter is invalid, in which
200// case error_msg describes the problem.
201static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
202 std::string* error_msg) {
203 CHECK(filter != nullptr);
204 CHECK(error_msg != nullptr);
205
Calin Juravle357c66d2017-05-04 01:57:17 +0000206 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700207 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
208 if (option.starts_with("--compiler-filter=")) {
209 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
210 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
211 *error_msg = std::string("Unknown --compiler-filter value: ")
212 + std::string(compiler_filter_string);
213 return false;
214 }
215 }
216 }
217 return true;
218}
219
Richard Uhler01be6812016-05-17 10:34:52 -0700220bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000221 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700222}
223
Richard Uhler1e860612016-03-30 12:17:55 -0700224OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700225OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700226 CompilerFilter::Filter target;
227 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
228 return kUpdateNotAttempted;
229 }
230
Richard Uhler88bc6732016-11-14 14:38:03 +0000231 OatFileInfo& info = GetBestInfo();
232 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000233 case kNoDexOptNeeded:
234 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000235
Richard Uhler7225a8d2016-11-22 10:12:03 +0000236 // TODO: For now, don't bother with all the different ways we can call
237 // dex2oat to generate the oat file. Always generate the oat file as if it
238 // were kDex2OatFromScratch.
239 case kDex2OatFromScratch:
240 case kDex2OatForBootImage:
241 case kDex2OatForRelocation:
242 case kDex2OatForFilter:
Calin Juravle07c6d722017-06-07 17:06:12 +0000243 return GenerateOatFileNoChecks(info, target, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800244 }
245 UNREACHABLE();
246}
247
248std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000249 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800250}
251
Richard Uhler46cc64f2016-11-14 14:53:55 +0000252std::string OatFileAssistant::GetStatusDump() {
253 std::ostringstream status;
254 bool oat_file_exists = false;
255 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000256 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000257 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000258 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000259
Richard Uhler46cc64f2016-11-14 14:53:55 +0000260 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000261 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
262 const OatFile* file = oat_.GetFile();
263 if (file == nullptr) {
264 // If the file is null even though the status is not kOatCannotOpen, it
265 // means we must have a vdex file with no corresponding oat file. In
266 // this case we cannot determine the compilation filter. Indicate that
267 // we have only the vdex file instead.
268 status << "vdex-only";
269 } else {
270 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
271 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000272 }
273
Richard Uhler03bc6592016-11-22 09:42:04 +0000274 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000275 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000276 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000277
Richard Uhler46cc64f2016-11-14 14:53:55 +0000278 odex_file_exists = true;
279 if (oat_file_exists) {
280 status << "] ";
281 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000282 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
283 const OatFile* file = odex_.GetFile();
284 if (file == nullptr) {
285 status << "vdex-only";
286 } else {
287 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
288 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000289 }
290
291 if (!oat_file_exists && !odex_file_exists) {
292 status << "invalid[";
293 }
294
295 status << "]";
296 return status.str();
297}
298
Richard Uhler66d874d2015-01-15 09:37:19 -0800299std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
300 const OatFile& oat_file, const char* dex_location) {
301 std::vector<std::unique_ptr<const DexFile>> dex_files;
302
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000303 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800304 std::string error_msg;
305 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700306 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800307 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700308 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800309 return std::vector<std::unique_ptr<const DexFile>>();
310 }
311
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700312 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800313 if (dex_file.get() == nullptr) {
314 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
315 return std::vector<std::unique_ptr<const DexFile>>();
316 }
317 dex_files.push_back(std::move(dex_file));
318
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000319 // Load the rest of the multidex entries
Andreas Gampe90e34042015-04-27 20:01:52 -0700320 for (size_t i = 1; ; i++) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000321 std::string multidex_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
322 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700323 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000324 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800325 break;
326 }
327
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700328 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800329 if (dex_file.get() == nullptr) {
330 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
331 return std::vector<std::unique_ptr<const DexFile>>();
332 }
333 dex_files.push_back(std::move(dex_file));
334 }
335 return dex_files;
336}
337
Richard Uhler9b994ea2015-06-24 08:44:19 -0700338bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000339 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700340 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000341 // GetRequiredDexChecksums.
342 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700343 return has_original_dex_files_;
344}
345
Richard Uhler95abd042015-03-24 09:51:28 -0700346OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700347 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800348}
349
Richard Uhler95abd042015-03-24 09:51:28 -0700350OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700351 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800352}
353
Richard Uhler2f27abd2017-01-31 14:02:34 +0000354bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000355 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
356 if (required_dex_checksums == nullptr) {
357 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
358 return true;
359 }
360
361 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
362 if (required_dex_checksums->size() != number_of_dex_files) {
363 *error_msg = StringPrintf("expected %zu dex files but found %u",
364 required_dex_checksums->size(),
365 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000366 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800367 }
368
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000369 for (uint32_t i = 0; i < number_of_dex_files; i++) {
370 uint32_t expected_checksum = (*required_dex_checksums)[i];
371 uint32_t actual_checksum = file.GetLocationChecksum(i);
372 if (expected_checksum != actual_checksum) {
373 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
374 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
375 "Expected: %u, actual: %u",
376 dex.c_str(),
377 expected_checksum,
378 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000379 return false;
380 }
381 }
382
Richard Uhler2f27abd2017-01-31 14:02:34 +0000383 return true;
384}
385
386bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000387 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
388 if (required_dex_checksums == nullptr) {
389 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
390 return true;
391 }
392
393 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
394 if (required_dex_checksums->size() != number_of_dex_files) {
395 *error_msg = StringPrintf("expected %zu dex files but found %u",
396 required_dex_checksums->size(),
397 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000398 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800399 }
400
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000401 for (uint32_t i = 0; i < number_of_dex_files; i++) {
402 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
403 uint32_t expected_checksum = (*required_dex_checksums)[i];
404 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
405 if (oat_dex_file == nullptr) {
406 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
407 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800408 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000409 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
410 if (expected_checksum != actual_checksum) {
411 VLOG(oat) << "Dex checksum does not match for dex: " << dex
412 << ". Expected: " << expected_checksum
413 << ", Actual: " << actual_checksum;
414 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800415 }
416 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000417 return true;
418}
419
420OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
421 // Verify the ART_USE_READ_BARRIER state.
422 // TODO: Don't fully reject files due to read barrier state. If they contain
423 // compiled code and are otherwise okay, we should return something like
424 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
425 // barrier state doesn't matter.
426 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
427 constexpr bool kRuntimeIsCC = kUseReadBarrier;
428 if (is_cc != kRuntimeIsCC) {
429 return kOatCannotOpen;
430 }
431
432 // Verify the dex checksum.
433 std::string error_msg;
434 if (kIsVdexEnabled) {
435 VdexFile* vdex = file.GetVdexFile();
436 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
437 LOG(ERROR) << error_msg;
438 return kOatDexOutOfDate;
439 }
440 } else {
441 if (!DexChecksumUpToDate(file, &error_msg)) {
442 LOG(ERROR) << error_msg;
443 return kOatDexOutOfDate;
444 }
445 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800446
Andreas Gampe29d38e72016-03-23 15:31:51 +0000447 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000448
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000450 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
451 const ImageInfo* image_info = GetImageInfo();
452 if (image_info == nullptr) {
453 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000454
Richard Uhler76f5cb62016-04-04 13:30:16 -0700455 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000456 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700457 }
458
459 // If there is no original dex file to fall back to, grudgingly accept
460 // the oat file. This could technically lead to crashes, but there's no
461 // way we could find a better oat file to use for this dex location,
462 // and it's better than being stuck in a boot loop with no way out.
463 // The problem will hopefully resolve itself the next time the runtime
464 // starts up.
465 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
466 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000467 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000468 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000469 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000470 }
471 } else {
472 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800473 }
474
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100475 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000476 if (!file.IsPic()) {
477 const ImageInfo* image_info = GetImageInfo();
478 if (image_info == nullptr) {
479 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000480 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000481 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700482
Andreas Gampe29d38e72016-03-23 15:31:51 +0000483 // Verify the oat_data_begin recorded for the image in the oat file matches
484 // the actual oat_data_begin for boot.oat in the image.
485 const OatHeader& oat_header = file.GetOatHeader();
486 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
487 if (oat_data_begin != image_info->oat_data_begin) {
488 VLOG(oat) << file.GetLocation() <<
489 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
490 << " does not match actual image oat_data_begin ("
491 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000492 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000493 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700494
Andreas Gampe29d38e72016-03-23 15:31:51 +0000495 // Verify the oat_patch_delta recorded for the image in the oat file matches
496 // the actual oat_patch_delta for the image.
497 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
498 if (oat_patch_delta != image_info->patch_delta) {
499 VLOG(oat) << file.GetLocation() <<
500 ": Oat file image patch delta (" << oat_patch_delta << ")"
501 << " does not match actual image patch delta ("
502 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000503 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000504 }
505 } else {
506 // Oat files compiled in PIC mode do not require relocation.
507 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
508 }
509 } else {
510 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800511 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700512 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800513}
514
Calin Juravle357c66d2017-05-04 01:57:17 +0000515static bool DexLocationToOdexNames(const std::string& location,
516 InstructionSet isa,
517 std::string* odex_filename,
518 std::string* oat_dir,
519 std::string* isa_dir,
520 std::string* error_msg) {
521 CHECK(odex_filename != nullptr);
522 CHECK(error_msg != nullptr);
523
524 // The odex file name is formed by replacing the dex_location extension with
525 // .odex and inserting an oat/<isa> directory. For example:
526 // location = /foo/bar/baz.jar
527 // odex_location = /foo/bar/oat/<isa>/baz.odex
528
529 // Find the directory portion of the dex location and add the oat/<isa>
530 // directory.
531 size_t pos = location.rfind('/');
532 if (pos == std::string::npos) {
533 *error_msg = "Dex location " + location + " has no directory.";
534 return false;
535 }
536 std::string dir = location.substr(0, pos+1);
537 // Add the oat directory.
538 dir += "oat";
539 if (oat_dir != nullptr) {
540 *oat_dir = dir;
541 }
542 // Add the isa directory
543 dir += "/" + std::string(GetInstructionSetString(isa));
544 if (isa_dir != nullptr) {
545 *isa_dir = dir;
546 }
547
548 // Get the base part of the file without the extension.
549 std::string file = location.substr(pos+1);
550 pos = file.rfind('.');
551 if (pos == std::string::npos) {
552 *error_msg = "Dex location " + location + " has no extension.";
553 return false;
554 }
555 std::string base = file.substr(0, pos);
556
557 *odex_filename = dir + "/" + base + ".odex";
558 return true;
559}
560
561// Prepare a subcomponent of the odex directory.
562// (i.e. create and set the expected permissions on the path `dir`).
563static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
564 struct stat dir_stat;
565 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
566 // The directory exists. Check if it is indeed a directory.
567 if (!S_ISDIR(dir_stat.st_mode)) {
568 *error_msg = dir + " is not a dir";
569 return false;
570 } else {
571 // The dir is already on disk.
572 return true;
573 }
574 }
575
576 // Failed to stat. We need to create the directory.
577 if (errno != ENOENT) {
578 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
579 return false;
580 }
581
582 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
583 if (mkdir(dir.c_str(), mode) != 0) {
584 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
585 return false;
586 }
587 if (chmod(dir.c_str(), mode) != 0) {
588 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
589 return false;
590 }
591 return true;
592}
593
594// Prepares the odex directory for the given dex location.
595static bool PrepareOdexDirectories(const std::string& dex_location,
596 const std::string& expected_odex_location,
597 InstructionSet isa,
598 std::string* error_msg) {
599 std::string actual_odex_location;
600 std::string oat_dir;
601 std::string isa_dir;
602 if (!DexLocationToOdexNames(
603 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
604 return false;
605 }
606 DCHECK_EQ(expected_odex_location, actual_odex_location);
607
608 if (!PrepareDirectory(oat_dir, error_msg)) {
609 return false;
610 }
611 if (!PrepareDirectory(isa_dir, error_msg)) {
612 return false;
613 }
614 return true;
615}
616
617OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle07c6d722017-06-07 17:06:12 +0000618 OatFileAssistant::OatFileInfo& info, CompilerFilter::Filter filter, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800619 CHECK(error_msg != nullptr);
620
Richard Uhler8327cf72015-10-13 16:34:59 -0700621 Runtime* runtime = Runtime::Current();
622 if (!runtime->IsDex2OatEnabled()) {
623 *error_msg = "Generation of oat file for dex location " + dex_location_
624 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700625 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700626 }
627
Calin Juravle357c66d2017-05-04 01:57:17 +0000628 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700629 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800630 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700631 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800632 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000633 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700634 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800635
Richard Uhler66d874d2015-01-15 09:37:19 -0800636 // dex2oat ignores missing dex files and doesn't report an error.
637 // Check explicitly here so we can detect the error properly.
638 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000639 struct stat dex_path_stat;
640 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
641 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700642 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800643 }
644
Calin Juravle357c66d2017-05-04 01:57:17 +0000645 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
646 if (!info.IsOatLocation()) {
647 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
648 return kUpdateNotAttempted;
649 }
650 }
651
652 // Set the permissions for the oat and the vdex files.
653 // The user always gets read and write while the group and others propagate
654 // the reading access of the original dex file.
655 mode_t file_mode = S_IRUSR | S_IWUSR |
656 (dex_path_stat.st_mode & S_IRGRP) |
657 (dex_path_stat.st_mode & S_IROTH);
658
David Brazdil7b49e6c2016-09-01 11:06:18 +0100659 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
660 if (vdex_file.get() == nullptr) {
661 *error_msg = "Generation of oat file " + oat_file_name
662 + " not attempted because the vdex file " + vdex_file_name
663 + " could not be opened.";
664 return kUpdateNotAttempted;
665 }
666
Calin Juravle357c66d2017-05-04 01:57:17 +0000667 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100668 *error_msg = "Generation of oat file " + oat_file_name
669 + " not attempted because the vdex file " + vdex_file_name
670 + " could not be made world readable.";
671 return kUpdateNotAttempted;
672 }
673
674 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700675 if (oat_file.get() == nullptr) {
676 *error_msg = "Generation of oat file " + oat_file_name
677 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700678 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700679 }
680
Calin Juravle357c66d2017-05-04 01:57:17 +0000681 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700682 *error_msg = "Generation of oat file " + oat_file_name
683 + " not attempted because the oat file could not be made world readable.";
684 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700685 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700686 }
687
688 std::vector<std::string> args;
689 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000690 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700691 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
692 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000693 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhler8327cf72015-10-13 16:34:59 -0700694
Richard Uhler66d874d2015-01-15 09:37:19 -0800695 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100696 // Manually delete the oat and vdex files. This ensures there is no garbage
697 // left over if the process unexpectedly died.
698 vdex_file->Erase();
699 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700700 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100701 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700702 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700703 }
704
David Brazdil7b49e6c2016-09-01 11:06:18 +0100705 if (vdex_file->FlushCloseOrErase() != 0) {
706 *error_msg = "Unable to close vdex file " + vdex_file_name;
707 unlink(vdex_file_name.c_str());
708 return kUpdateFailed;
709 }
710
Richard Uhler8327cf72015-10-13 16:34:59 -0700711 if (oat_file->FlushCloseOrErase() != 0) {
712 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100713 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700714 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800715 }
716
Calin Juravle357c66d2017-05-04 01:57:17 +0000717 // Mark that the odex file has changed and we should try to reload.
718 info.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700719 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800720}
721
722bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
723 std::string* error_msg) {
724 Runtime* runtime = Runtime::Current();
725 std::string image_location = ImageLocation();
726 if (image_location.empty()) {
727 *error_msg = "No image location found for Dex2Oat.";
728 return false;
729 }
730
731 std::vector<std::string> argv;
732 argv.push_back(runtime->GetCompilerExecutable());
733 argv.push_back("--runtime-arg");
734 argv.push_back("-classpath");
735 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700736 std::string class_path = runtime->GetClassPathString();
737 if (class_path == "") {
738 class_path = OatFile::kSpecialSharedLibrary;
739 }
740 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000741 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200742 argv.push_back("--debuggable");
743 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700744 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800745
746 if (!runtime->IsVerificationEnabled()) {
747 argv.push_back("--compiler-filter=verify-none");
748 }
749
750 if (runtime->MustRelocateIfPossible()) {
751 argv.push_back("--runtime-arg");
752 argv.push_back("-Xrelocate");
753 } else {
754 argv.push_back("--runtime-arg");
755 argv.push_back("-Xnorelocate");
756 }
757
758 if (!kIsTargetBuild) {
759 argv.push_back("--host");
760 }
761
762 argv.push_back("--boot-image=" + image_location);
763
764 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
765 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
766
767 argv.insert(argv.end(), args.begin(), args.end());
768
Andreas Gampe9186ced2016-12-12 14:28:21 -0800769 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800770 return Exec(argv, error_msg);
771}
772
Richard Uhlerb81881d2016-04-19 13:08:04 -0700773bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
774 InstructionSet isa,
775 std::string* odex_filename,
776 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000777 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800778}
779
Richard Uhlerb81881d2016-04-19 13:08:04 -0700780bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
781 InstructionSet isa,
782 std::string* oat_filename,
783 std::string* error_msg) {
784 CHECK(oat_filename != nullptr);
785 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800786
Richard Uhler55b58b62016-08-12 09:05:13 -0700787 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
788 if (cache_dir.empty()) {
789 *error_msg = "Dalvik cache directory does not exist";
790 return false;
791 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700792
793 // TODO: The oat file assistant should be the definitive place for
794 // determining the oat file name from the dex location, not
795 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700796 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800797}
798
Richard Uhler66d874d2015-01-15 09:37:19 -0800799std::string OatFileAssistant::ImageLocation() {
800 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000801 const std::vector<gc::space::ImageSpace*>& image_spaces =
802 runtime->GetHeap()->GetBootImageSpaces();
803 if (image_spaces.empty()) {
804 return "";
805 }
806 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800807}
808
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000809const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
810 if (!required_dex_checksums_attempted_) {
811 required_dex_checksums_attempted_ = true;
812 required_dex_checksums_found_ = false;
813 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800814 std::string error_msg;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000815 if (DexFile::GetMultiDexChecksums(dex_location_.c_str(),
816 &cached_required_dex_checksums_,
817 &error_msg)) {
818 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700819 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800820 } else {
821 // This can happen if the original dex file has been stripped from the
822 // apk.
823 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700824 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800825
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000826 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700827 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800828 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000829 required_dex_checksums_found_ = true;
830 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
831 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
832 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
833 if (odex_dex_file == nullptr) {
834 required_dex_checksums_found_ = false;
835 break;
836 }
837 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800838 }
839 }
840 }
841 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000842 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800843}
844
Richard Uhler95e09672017-02-22 11:37:41 +0000845std::unique_ptr<OatFileAssistant::ImageInfo>
846OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
847 CHECK(error_msg != nullptr);
848
Richard Uhler95e09672017-02-22 11:37:41 +0000849 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000850 std::unique_ptr<ImageInfo> info(new ImageInfo());
851 info->location = runtime->GetImageLocation();
852
853 std::unique_ptr<ImageHeader> image_header(
854 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
855 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000856 return nullptr;
857 }
858
Richard Uhler8f104862017-02-22 12:34:01 +0000859 info->oat_checksum = image_header->GetOatChecksum();
860 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
861 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000862 return info;
863}
864
Richard Uhler66d874d2015-01-15 09:37:19 -0800865const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
866 if (!image_info_load_attempted_) {
867 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000868 std::string error_msg;
869 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
870 if (cached_image_info_ == nullptr) {
871 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800872 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800873 }
Richard Uhler95e09672017-02-22 11:37:41 +0000874 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800875}
876
Richard Uhler88bc6732016-11-14 14:38:03 +0000877OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000878 // TODO(calin): Document the side effects of class loading when
879 // running dalvikvm command line.
880 if (dex_parent_writable_) {
881 // If the parent of the dex file is writable it means that we can
882 // create the odex file. In this case we unconditionally pick the odex
883 // as the best oat file. This corresponds to the regular use case when
884 // apps gets installed or when they load private, secondary dex file.
885 // For apps on the system partition the odex location will not be
886 // writable and thus the oat location might be more up to date.
887 return odex_;
888 }
889
890 // We cannot write to the odex location. This must be a system app.
891
892 // If the oat location is usable take it.
893 if (oat_.IsUseable()) {
894 return oat_;
895 }
896
897 // The oat file is not usable but the odex file might be up to date.
898 // This is an indication that we are dealing with an up to date prebuilt
899 // (that doesn't need relocation).
900 if (odex_.Status() == kOatUpToDate) {
901 return odex_;
902 }
903
904 // The oat file is not usable and the odex file is not up to date.
905 // However we have access to the original dex file which means we can make
906 // the oat location up to date.
907 if (HasOriginalDexFiles()) {
908 return oat_;
909 }
910
911 // We got into the worst situation here:
912 // - the oat location is not usable
913 // - the prebuild odex location is not up to date
914 // - and we don't have the original dex file anymore (stripped).
915 // Pick the odex if it exists, or the oat if not.
916 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000917}
918
Andreas Gampea463b6a2016-08-12 21:53:32 -0700919std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800920 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100921 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800922 if (art_file.empty()) {
923 return nullptr;
924 }
925 std::string error_msg;
926 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700927 std::unique_ptr<gc::space::ImageSpace> ret =
928 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800929 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800930 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
931 }
932 return ret;
933}
934
Richard Uhler88bc6732016-11-14 14:38:03 +0000935OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
936 bool is_oat_location)
937 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700938{}
939
Richard Uhler88bc6732016-11-14 14:38:03 +0000940bool OatFileAssistant::OatFileInfo::IsOatLocation() {
941 return is_oat_location_;
942}
943
Richard Uhler743bf362016-04-19 15:39:37 -0700944const std::string* OatFileAssistant::OatFileInfo::Filename() {
945 return filename_provided_ ? &filename_ : nullptr;
946}
947
Richard Uhler03bc6592016-11-22 09:42:04 +0000948bool OatFileAssistant::OatFileInfo::IsUseable() {
949 switch (Status()) {
950 case kOatCannotOpen:
951 case kOatDexOutOfDate:
952 case kOatBootImageOutOfDate: return false;
953
954 case kOatRelocationOutOfDate:
955 case kOatUpToDate: return true;
956 }
957 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700958}
959
960OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
961 if (!status_attempted_) {
962 status_attempted_ = true;
963 const OatFile* file = GetFile();
964 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000965 // Check to see if there is a vdex file we can make use of.
966 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700967 std::string vdex_filename = GetVdexFilename(filename_);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000968 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
969 /*writeable*/false,
970 /*low_4gb*/false,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100971 /*unquicken*/false,
Richard Uhler2f27abd2017-01-31 14:02:34 +0000972 &error_msg);
973 if (vdex == nullptr) {
974 status_ = kOatCannotOpen;
975 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
976 } else {
977 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
978 // The vdex file does not contain enough information to determine
979 // whether it is up to date with respect to the boot image, so we
980 // assume it is out of date.
981 VLOG(oat) << error_msg;
982 status_ = kOatBootImageOutOfDate;
983 } else {
984 status_ = kOatDexOutOfDate;
985 }
986 }
Richard Uhler743bf362016-04-19 15:39:37 -0700987 } else {
988 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700989 VLOG(oat) << file->GetLocation() << " is " << status_
990 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700991 }
992 }
993 return status_;
994}
995
Richard Uhler70a84262016-11-08 16:51:51 +0000996OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
997 CompilerFilter::Filter target, bool profile_changed) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100998 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000999 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +00001000
Richard Uhler7225a8d2016-11-22 10:12:03 +00001001 if (filter_okay && Status() == kOatUpToDate) {
1002 // The oat file is in good shape as is.
1003 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +00001004 }
1005
Richard Uhler7225a8d2016-11-22 10:12:03 +00001006 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1007 // If no compilation is desired, then it doesn't matter if the oat
1008 // file needs relocation. It's in good shape as is.
1009 return kNoDexOptNeeded;
1010 }
1011
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001012 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1013 return kDex2OatForRelocation;
Richard Uhler7225a8d2016-11-22 10:12:03 +00001014 }
1015
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001016 if (IsUseable()) {
1017 return kDex2OatForFilter;
1018 }
1019
1020 if (Status() == kOatBootImageOutOfDate) {
1021 return kDex2OatForBootImage;
1022 }
1023
1024 if (oat_file_assistant_->HasOriginalDexFiles()) {
1025 return kDex2OatFromScratch;
1026 } else {
1027 // Otherwise there is nothing we can do, even if we want to.
1028 return kNoDexOptNeeded;
1029 }
Richard Uhler70a84262016-11-08 16:51:51 +00001030}
1031
Richard Uhler743bf362016-04-19 15:39:37 -07001032const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1033 CHECK(!file_released_) << "GetFile called after oat file released.";
1034 if (!load_attempted_) {
1035 load_attempted_ = true;
1036 if (filename_provided_) {
1037 std::string error_msg;
1038 file_.reset(OatFile::Open(filename_.c_str(),
1039 filename_.c_str(),
1040 nullptr,
1041 nullptr,
1042 oat_file_assistant_->load_executable_,
1043 /*low_4gb*/false,
1044 oat_file_assistant_->dex_location_.c_str(),
1045 &error_msg));
1046 if (file_.get() == nullptr) {
1047 VLOG(oat) << "OatFileAssistant test for existing oat file "
1048 << filename_ << ": " << error_msg;
1049 }
1050 }
1051 }
1052 return file_.get();
1053}
1054
1055bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
1056 CompilerFilter::Filter target, bool profile_changed) {
1057 const OatFile* file = GetFile();
1058 if (file == nullptr) {
1059 return false;
1060 }
1061
1062 CompilerFilter::Filter current = file->GetCompilerFilter();
1063 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1064 VLOG(oat) << "Compiler filter not okay because Profile changed";
1065 return false;
1066 }
1067 return CompilerFilter::IsAsGoodAs(current, target);
1068}
1069
1070bool OatFileAssistant::OatFileInfo::IsExecutable() {
1071 const OatFile* file = GetFile();
1072 return (file != nullptr && file->IsExecutable());
1073}
1074
Richard Uhler743bf362016-04-19 15:39:37 -07001075void OatFileAssistant::OatFileInfo::Reset() {
1076 load_attempted_ = false;
1077 file_.reset();
1078 status_attempted_ = false;
1079}
1080
1081void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
1082 filename_provided_ = true;
1083 filename_ = filename;
1084 Reset();
1085}
1086
1087std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1088 file_released_ = true;
1089 return std::move(file_);
1090}
1091
Richard Uhler70a84262016-11-08 16:51:51 +00001092std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001093 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001094 return ReleaseFile();
1095 }
1096
1097 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1098 << " attempting to fall back to interpreting oat file instead.";
1099
Richard Uhler03bc6592016-11-22 09:42:04 +00001100 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001101 return ReleaseFile();
1102 }
1103
Richard Uhler03bc6592016-11-22 09:42:04 +00001104 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001105 // We are loading an oat file for runtime use that needs relocation.
1106 // Reload the file non-executable to ensure that we interpret out of the
1107 // dex code in the oat file rather than trying to execute the unrelocated
1108 // compiled code.
1109 oat_file_assistant_->load_executable_ = false;
1110 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001111 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001112 CHECK(!IsExecutable());
1113 return ReleaseFile();
1114 }
1115 }
1116 return std::unique_ptr<OatFile>();
1117}
Richard Uhler66d874d2015-01-15 09:37:19 -08001118} // namespace art