blob: 6f32b98466a050e8bbb9b7ad917f42c053d3c78e [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
David Sehr891a50e2017-10-27 17:01:07 -070026#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080027#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070028#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070030#include "base/stl_util.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080032#include "class_linker.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "compiler_filter.h"
David Sehr013fd802018-01-11 22:55:24 -080034#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080036#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "gc/heap.h"
38#include "gc/space/image_space.h"
39#include "image.h"
40#include "oat.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000043#include "vdex_file.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070044#include "class_loader_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080045
46namespace art {
47
Richard Uhler69bcf2c2017-01-24 10:25:21 +000048using android::base::StringPrintf;
49
Narayan Kamath8943c1d2016-05-02 13:14:48 +010050std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
51 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000052 case OatFileAssistant::kOatCannotOpen:
53 stream << "kOatCannotOpen";
54 break;
55 case OatFileAssistant::kOatDexOutOfDate:
56 stream << "kOatDexOutOfDate";
57 break;
58 case OatFileAssistant::kOatBootImageOutOfDate:
59 stream << "kOatBootImageOutOfDate";
60 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010061 case OatFileAssistant::kOatUpToDate:
62 stream << "kOatUpToDate";
63 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010064 default:
65 UNREACHABLE();
66 }
67
68 return stream;
69}
70
Richard Uhler66d874d2015-01-15 09:37:19 -080071OatFileAssistant::OatFileAssistant(const char* dex_location,
72 const InstructionSet isa,
Nicolas Geoffray29742602017-12-14 10:09:03 +000073 bool load_executable,
74 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070075 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000076 isa,
77 load_executable,
78 only_load_system_executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010079 /*vdex_fd=*/ -1,
80 /*oat_fd=*/ -1,
81 /*zip_fd=*/ -1) {}
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070082
83
84OatFileAssistant::OatFileAssistant(const char* dex_location,
85 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070086 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000087 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070088 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070089 int oat_fd,
90 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000091 : isa_(isa),
92 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +000093 only_load_system_executable_(only_load_system_executable),
Andreas Gampe98ea9d92018-10-19 14:06:15 -070094 odex_(this, /*is_oat_location=*/ false),
95 oat_(this, /*is_oat_location=*/ true),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070096 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -070097 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000098
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070099 if (zip_fd < 0) {
100 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
101 << " oat_fd=" << oat_fd;
102 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
103 << " vdex_fd=" << vdex_fd;;
104 }
105
Calin Juravle099f8d62017-09-05 19:04:04 -0700106 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700107
Richard Uhler66d874d2015-01-15 09:37:19 -0800108 if (load_executable_ && isa != kRuntimeISA) {
109 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
110 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
111 load_executable_ = false;
112 }
113
Richard Uhler743bf362016-04-19 15:39:37 -0700114 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700115 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700116 std::string odex_file_name;
117 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100118 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700119 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700120 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
121 }
122
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700123 if (!UseFdToReadFiles()) {
124 // Get the oat filename.
125 std::string oat_file_name;
126 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100127 oat_.Reset(oat_file_name, /*use_fd=*/ false);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700128 } else {
129 LOG(WARNING) << "Failed to determine oat file name for dex location "
130 << dex_location_ << ": " << error_msg;
131 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000132 }
133
134 // Check if the dex directory is writable.
135 // This will be needed in most uses of OatFileAssistant and so it's OK to
136 // compute it eagerly. (the only use which will not make use of it is
137 // OatFileAssistant::GetStatusDump())
138 size_t pos = dex_location_.rfind('/');
139 if (pos == std::string::npos) {
140 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700141 } else if (!UseFdToReadFiles()) {
142 // We cannot test for parent access when using file descriptors. That's ok
143 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000144 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700145 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000146 dex_parent_writable_ = true;
147 } else {
148 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700149 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800150 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800151}
152
153OatFileAssistant::~OatFileAssistant() {
154 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100155 if (flock_.get() != nullptr) {
156 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800157 }
158}
159
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700160bool OatFileAssistant::UseFdToReadFiles() {
161 return zip_fd_ >= 0;
162}
163
Richard Uhler66d874d2015-01-15 09:37:19 -0800164bool OatFileAssistant::IsInBootClassPath() {
165 // Note: We check the current boot class path, regardless of the ISA
166 // specified by the user. This is okay, because the boot class path should
167 // be the same for all ISAs.
168 // TODO: Can we verify the boot class path is the same for all ISAs?
169 Runtime* runtime = Runtime::Current();
170 ClassLinker* class_linker = runtime->GetClassLinker();
171 const auto& boot_class_path = class_linker->GetBootClassPath();
172 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700173 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800174 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
175 return true;
176 }
177 }
178 return false;
179}
180
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700181int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
182 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700183 bool downgrade,
184 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000185 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700186 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
187 profile_changed,
188 downgrade,
189 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000190 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
191 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800192 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000193 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800194}
195
Richard Uhler01be6812016-05-17 10:34:52 -0700196bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000197 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700198}
199
Richard Uhler66d874d2015-01-15 09:37:19 -0800200std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000201 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800202}
203
Richard Uhler46cc64f2016-11-14 14:53:55 +0000204std::string OatFileAssistant::GetStatusDump() {
205 std::ostringstream status;
206 bool oat_file_exists = false;
207 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000208 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000209 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000210 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000211
Richard Uhler46cc64f2016-11-14 14:53:55 +0000212 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000213 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
214 const OatFile* file = oat_.GetFile();
215 if (file == nullptr) {
216 // If the file is null even though the status is not kOatCannotOpen, it
217 // means we must have a vdex file with no corresponding oat file. In
218 // this case we cannot determine the compilation filter. Indicate that
219 // we have only the vdex file instead.
220 status << "vdex-only";
221 } else {
222 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
223 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000224 }
225
Richard Uhler03bc6592016-11-22 09:42:04 +0000226 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000227 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000228 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000229
Richard Uhler46cc64f2016-11-14 14:53:55 +0000230 odex_file_exists = true;
231 if (oat_file_exists) {
232 status << "] ";
233 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000234 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
235 const OatFile* file = odex_.GetFile();
236 if (file == nullptr) {
237 status << "vdex-only";
238 } else {
239 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
240 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000241 }
242
243 if (!oat_file_exists && !odex_file_exists) {
244 status << "invalid[";
245 }
246
247 status << "]";
248 return status.str();
249}
250
Richard Uhler66d874d2015-01-15 09:37:19 -0800251std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700252 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800253 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700254 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
255 return dex_files;
256 } else {
257 return std::vector<std::unique_ptr<const DexFile>>();
258 }
259}
Richard Uhler66d874d2015-01-15 09:37:19 -0800260
Calin Juravle87e2cb62017-06-13 21:48:45 -0700261bool OatFileAssistant::LoadDexFiles(
262 const OatFile &oat_file,
263 const std::string& dex_location,
264 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000265 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800266 std::string error_msg;
Andreas Gampeb40d3612018-06-26 15:49:42 -0700267 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700268 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800269 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700270 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700271 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800272 }
273
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700274 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800275 if (dex_file.get() == nullptr) {
276 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700277 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800278 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700279 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800280
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000281 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700282 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700283 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000284 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700285 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000286 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 break;
288 }
289
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700290 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800291 if (dex_file.get() == nullptr) {
292 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700293 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800294 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700295 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700297 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800298}
299
Richard Uhler9b994ea2015-06-24 08:44:19 -0700300bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000301 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700302 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000303 // GetRequiredDexChecksums.
304 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700305 return has_original_dex_files_;
306}
307
Richard Uhler95abd042015-03-24 09:51:28 -0700308OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700309 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800310}
311
Richard Uhler95abd042015-03-24 09:51:28 -0700312OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700313 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800314}
315
Richard Uhler2f27abd2017-01-31 14:02:34 +0000316bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000317 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
318 if (required_dex_checksums == nullptr) {
319 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
320 return true;
321 }
322
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000323 uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000324 if (required_dex_checksums->size() != number_of_dex_files) {
325 *error_msg = StringPrintf("expected %zu dex files but found %u",
326 required_dex_checksums->size(),
327 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000328 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800329 }
330
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000331 for (uint32_t i = 0; i < number_of_dex_files; i++) {
332 uint32_t expected_checksum = (*required_dex_checksums)[i];
333 uint32_t actual_checksum = file.GetLocationChecksum(i);
334 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700335 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000336 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
337 "Expected: %u, actual: %u",
338 dex.c_str(),
339 expected_checksum,
340 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000341 return false;
342 }
343 }
344
Richard Uhler2f27abd2017-01-31 14:02:34 +0000345 return true;
346}
347
348bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000349 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
350 if (required_dex_checksums == nullptr) {
351 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
352 return true;
353 }
354
355 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
356 if (required_dex_checksums->size() != number_of_dex_files) {
357 *error_msg = StringPrintf("expected %zu dex files but found %u",
358 required_dex_checksums->size(),
359 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000360 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800361 }
362
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000363 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700364 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000365 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700366 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000367 if (oat_dex_file == nullptr) {
368 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
369 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800370 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000371 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
372 if (expected_checksum != actual_checksum) {
373 VLOG(oat) << "Dex checksum does not match for dex: " << dex
374 << ". Expected: " << expected_checksum
375 << ", Actual: " << actual_checksum;
376 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 }
378 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000379 return true;
380}
381
382OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
383 // Verify the ART_USE_READ_BARRIER state.
384 // TODO: Don't fully reject files due to read barrier state. If they contain
385 // compiled code and are otherwise okay, we should return something like
386 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
387 // barrier state doesn't matter.
388 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
389 constexpr bool kRuntimeIsCC = kUseReadBarrier;
390 if (is_cc != kRuntimeIsCC) {
391 return kOatCannotOpen;
392 }
393
394 // Verify the dex checksum.
395 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000396 VdexFile* vdex = file.GetVdexFile();
397 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
398 LOG(ERROR) << error_msg;
399 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000400 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800401
Andreas Gampe29d38e72016-03-23 15:31:51 +0000402 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000403
Richard Uhler66d874d2015-01-15 09:37:19 -0800404 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000405 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
406 const ImageInfo* image_info = GetImageInfo();
407 if (image_info == nullptr) {
408 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000409
Richard Uhler76f5cb62016-04-04 13:30:16 -0700410 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000411 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700412 }
413
414 // If there is no original dex file to fall back to, grudgingly accept
415 // the oat file. This could technically lead to crashes, but there's no
416 // way we could find a better oat file to use for this dex location,
417 // and it's better than being stuck in a boot loop with no way out.
418 // The problem will hopefully resolve itself the next time the runtime
419 // starts up.
420 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
421 << "Allow oat file use. This is potentially dangerous.";
Andreas Gampefb83d762018-12-13 23:30:25 +0000422 } else if (file.GetOatHeader().GetBootImageChecksum() != image_info->boot_image_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000423 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000424 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000425 }
426 } else {
427 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800428 }
429
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000430 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
431 DCHECK(required_dex_checksums_attempted_);
432 if (only_load_system_executable_ &&
433 !LocationIsOnSystem(file.GetLocation().c_str()) &&
434 file.ContainsDexCode() &&
435 zip_file_only_contains_uncompressed_dex_) {
436 LOG(ERROR) << "Not loading "
437 << dex_location_
438 << ": oat file has dex code, but APK has uncompressed dex code";
439 return kOatDexOutOfDate;
440 }
441
Richard Uhlere8109f72016-04-18 10:40:50 -0700442 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800443}
444
Calin Juravle357c66d2017-05-04 01:57:17 +0000445static bool DexLocationToOdexNames(const std::string& location,
446 InstructionSet isa,
447 std::string* odex_filename,
448 std::string* oat_dir,
449 std::string* isa_dir,
450 std::string* error_msg) {
451 CHECK(odex_filename != nullptr);
452 CHECK(error_msg != nullptr);
453
454 // The odex file name is formed by replacing the dex_location extension with
455 // .odex and inserting an oat/<isa> directory. For example:
456 // location = /foo/bar/baz.jar
457 // odex_location = /foo/bar/oat/<isa>/baz.odex
458
459 // Find the directory portion of the dex location and add the oat/<isa>
460 // directory.
461 size_t pos = location.rfind('/');
462 if (pos == std::string::npos) {
463 *error_msg = "Dex location " + location + " has no directory.";
464 return false;
465 }
466 std::string dir = location.substr(0, pos+1);
467 // Add the oat directory.
468 dir += "oat";
469 if (oat_dir != nullptr) {
470 *oat_dir = dir;
471 }
472 // Add the isa directory
473 dir += "/" + std::string(GetInstructionSetString(isa));
474 if (isa_dir != nullptr) {
475 *isa_dir = dir;
476 }
477
478 // Get the base part of the file without the extension.
479 std::string file = location.substr(pos+1);
480 pos = file.rfind('.');
481 if (pos == std::string::npos) {
482 *error_msg = "Dex location " + location + " has no extension.";
483 return false;
484 }
485 std::string base = file.substr(0, pos);
486
487 *odex_filename = dir + "/" + base + ".odex";
488 return true;
489}
490
Richard Uhlerb81881d2016-04-19 13:08:04 -0700491bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
492 InstructionSet isa,
493 std::string* odex_filename,
494 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000495 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800496}
497
Richard Uhlerb81881d2016-04-19 13:08:04 -0700498bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
499 InstructionSet isa,
500 std::string* oat_filename,
501 std::string* error_msg) {
502 CHECK(oat_filename != nullptr);
503 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800504
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700505 // If ANDROID_DATA is not set, return false instead of aborting.
506 // This can occur for preopt when using a class loader context.
507 if (GetAndroidDataSafe(error_msg) == nullptr) {
508 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
509 return false;
510 }
511
Richard Uhler55b58b62016-08-12 09:05:13 -0700512 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
513 if (cache_dir.empty()) {
514 *error_msg = "Dalvik cache directory does not exist";
515 return false;
516 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700517
518 // TODO: The oat file assistant should be the definitive place for
519 // determining the oat file name from the dex location, not
520 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700521 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800522}
523
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000524const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
525 if (!required_dex_checksums_attempted_) {
526 required_dex_checksums_attempted_ = true;
527 required_dex_checksums_found_ = false;
528 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800529 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800530 const ArtDexFileLoader dex_file_loader;
531 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
532 &cached_required_dex_checksums_,
533 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000534 zip_fd_,
535 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000536 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700537 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800538 } else {
539 // This can happen if the original dex file has been stripped from the
540 // apk.
541 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700542 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800543
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000544 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700545 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800546 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000547 required_dex_checksums_found_ = true;
548 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700549 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Andreas Gampeb40d3612018-06-26 15:49:42 -0700550 const OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000551 if (odex_dex_file == nullptr) {
552 required_dex_checksums_found_ = false;
553 break;
554 }
555 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800556 }
557 }
558 }
559 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000560 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800561}
562
Richard Uhler95e09672017-02-22 11:37:41 +0000563std::unique_ptr<OatFileAssistant::ImageInfo>
564OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
565 CHECK(error_msg != nullptr);
566
Richard Uhler95e09672017-02-22 11:37:41 +0000567 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000568 std::unique_ptr<ImageInfo> info(new ImageInfo());
569 info->location = runtime->GetImageLocation();
Andreas Gampefb83d762018-12-13 23:30:25 +0000570
571 std::unique_ptr<ImageHeader> image_header(
572 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
573 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000574 return nullptr;
575 }
Andreas Gampefb83d762018-12-13 23:30:25 +0000576
577 info->boot_image_checksum = image_header->GetImageChecksum();
Richard Uhler95e09672017-02-22 11:37:41 +0000578 return info;
579}
580
Richard Uhler66d874d2015-01-15 09:37:19 -0800581const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
582 if (!image_info_load_attempted_) {
583 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000584 std::string error_msg;
585 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
586 if (cached_image_info_ == nullptr) {
587 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800588 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800589 }
Richard Uhler95e09672017-02-22 11:37:41 +0000590 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800591}
592
Richard Uhler88bc6732016-11-14 14:38:03 +0000593OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000594 // TODO(calin): Document the side effects of class loading when
595 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700596 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000597 // If the parent of the dex file is writable it means that we can
598 // create the odex file. In this case we unconditionally pick the odex
599 // as the best oat file. This corresponds to the regular use case when
600 // apps gets installed or when they load private, secondary dex file.
601 // For apps on the system partition the odex location will not be
602 // writable and thus the oat location might be more up to date.
603 return odex_;
604 }
605
606 // We cannot write to the odex location. This must be a system app.
607
608 // If the oat location is usable take it.
609 if (oat_.IsUseable()) {
610 return oat_;
611 }
612
613 // The oat file is not usable but the odex file might be up to date.
614 // This is an indication that we are dealing with an up to date prebuilt
615 // (that doesn't need relocation).
616 if (odex_.Status() == kOatUpToDate) {
617 return odex_;
618 }
619
620 // The oat file is not usable and the odex file is not up to date.
621 // However we have access to the original dex file which means we can make
622 // the oat location up to date.
623 if (HasOriginalDexFiles()) {
624 return oat_;
625 }
626
627 // We got into the worst situation here:
628 // - the oat location is not usable
629 // - the prebuild odex location is not up to date
630 // - and we don't have the original dex file anymore (stripped).
631 // Pick the odex if it exists, or the oat if not.
632 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000633}
634
Andreas Gampea463b6a2016-08-12 21:53:32 -0700635std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800636 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100637 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800638 if (art_file.empty()) {
639 return nullptr;
640 }
641 std::string error_msg;
642 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700643 std::unique_ptr<gc::space::ImageSpace> ret =
644 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800645 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800646 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
647 }
648 return ret;
649}
650
Richard Uhler88bc6732016-11-14 14:38:03 +0000651OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
652 bool is_oat_location)
653 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700654{}
655
Richard Uhler88bc6732016-11-14 14:38:03 +0000656bool OatFileAssistant::OatFileInfo::IsOatLocation() {
657 return is_oat_location_;
658}
659
Richard Uhler743bf362016-04-19 15:39:37 -0700660const std::string* OatFileAssistant::OatFileInfo::Filename() {
661 return filename_provided_ ? &filename_ : nullptr;
662}
663
Richard Uhler03bc6592016-11-22 09:42:04 +0000664bool OatFileAssistant::OatFileInfo::IsUseable() {
665 switch (Status()) {
666 case kOatCannotOpen:
667 case kOatDexOutOfDate:
668 case kOatBootImageOutOfDate: return false;
669
Richard Uhler03bc6592016-11-22 09:42:04 +0000670 case kOatUpToDate: return true;
671 }
672 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700673}
674
675OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
676 if (!status_attempted_) {
677 status_attempted_ = true;
678 const OatFile* file = GetFile();
679 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000680 // Check to see if there is a vdex file we can make use of.
681 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700682 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700683 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700684 if (use_fd_) {
685 if (vdex_fd_ >= 0) {
686 struct stat s;
687 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
688 if (rc == -1) {
689 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
690 } else {
691 vdex = VdexFile::Open(vdex_fd_,
692 s.st_size,
693 vdex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100694 /*writable=*/ false,
695 /*low_4gb=*/ false,
696 /*unquicken=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700697 &error_msg);
698 }
699 }
700 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700701 vdex = VdexFile::Open(vdex_filename,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700702 /*writable=*/ false,
703 /*low_4gb=*/ false,
704 /*unquicken=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700705 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700706 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000707 if (vdex == nullptr) {
708 status_ = kOatCannotOpen;
709 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
710 } else {
711 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
712 // The vdex file does not contain enough information to determine
713 // whether it is up to date with respect to the boot image, so we
714 // assume it is out of date.
715 VLOG(oat) << error_msg;
716 status_ = kOatBootImageOutOfDate;
717 } else {
718 status_ = kOatDexOutOfDate;
719 }
720 }
Richard Uhler743bf362016-04-19 15:39:37 -0700721 } else {
722 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700723 VLOG(oat) << file->GetLocation() << " is " << status_
724 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700725 }
726 }
727 return status_;
728}
729
Richard Uhler70a84262016-11-08 16:51:51 +0000730OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700731 CompilerFilter::Filter target,
732 bool profile_changed,
733 bool downgrade,
734 ClassLoaderContext* context) {
735
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700736 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -0700737 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +0000738
Calin Juravle44e5efa2017-09-12 00:54:26 -0700739 // Only check the filter and relocation if the class loader context is ok.
740 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
741 if (class_loader_context_okay) {
742 if (filter_okay && Status() == kOatUpToDate) {
743 // The oat file is in good shape as is.
744 return kNoDexOptNeeded;
745 }
Richard Uhler70a84262016-11-08 16:51:51 +0000746
Calin Juravle44e5efa2017-09-12 00:54:26 -0700747 if (IsUseable()) {
748 return kDex2OatForFilter;
749 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100750
Calin Juravle44e5efa2017-09-12 00:54:26 -0700751 if (Status() == kOatBootImageOutOfDate) {
752 return kDex2OatForBootImage;
753 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100754 }
755
756 if (oat_file_assistant_->HasOriginalDexFiles()) {
757 return kDex2OatFromScratch;
758 } else {
759 // Otherwise there is nothing we can do, even if we want to.
760 return kNoDexOptNeeded;
761 }
Richard Uhler70a84262016-11-08 16:51:51 +0000762}
763
Richard Uhler743bf362016-04-19 15:39:37 -0700764const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
765 CHECK(!file_released_) << "GetFile called after oat file released.";
766 if (!load_attempted_) {
767 load_attempted_ = true;
768 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +0000769 bool executable = oat_file_assistant_->load_executable_;
770 if (executable && oat_file_assistant_->only_load_system_executable_) {
771 executable = LocationIsOnSystem(filename_.c_str());
772 }
Richard Uhler743bf362016-04-19 15:39:37 -0700773 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700774 if (use_fd_) {
775 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100776 file_.reset(OatFile::Open(zip_fd_,
777 vdex_fd_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700778 oat_fd_,
779 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000780 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100781 /*low_4gb=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700782 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100783 /*reservation=*/ nullptr,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700784 &error_msg));
785 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700786 } else {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100787 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100788 filename_.c_str(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700789 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000790 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100791 /*low_4gb=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700792 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100793 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700794 &error_msg));
795 }
Richard Uhler743bf362016-04-19 15:39:37 -0700796 if (file_.get() == nullptr) {
797 VLOG(oat) << "OatFileAssistant test for existing oat file "
798 << filename_ << ": " << error_msg;
799 }
800 }
801 }
802 return file_.get();
803}
804
805bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700806 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700807 const OatFile* file = GetFile();
808 if (file == nullptr) {
809 return false;
810 }
811
812 CompilerFilter::Filter current = file->GetCompilerFilter();
813 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
814 VLOG(oat) << "Compiler filter not okay because Profile changed";
815 return false;
816 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700817 return downgrade ? !CompilerFilter::IsBetter(current, target) :
818 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700819}
820
Calin Juravle44e5efa2017-09-12 00:54:26 -0700821bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
822 if (context == nullptr) {
823 VLOG(oat) << "ClassLoaderContext check ignored: null context";
824 return true;
825 }
826
827 const OatFile* file = GetFile();
828 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -0700829 // No oat file means we have nothing to verify.
830 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700831 }
832
Calin Juravle20c46442017-09-12 00:54:26 -0700833 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -0700834 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -0700835 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -0700836 : "";
837
838 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
839 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
840 return false;
841 }
842
Mathieu Chartieradc90862018-05-11 13:03:06 -0700843
844 const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
845 ClassLoaderContext::VerificationResult::kMismatch;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700846 if (!result) {
847 VLOG(oat) << "ClassLoaderContext check failed. Context was "
848 << file->GetClassLoaderContext()
849 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
850 }
851 return result;
852}
853
Richard Uhler743bf362016-04-19 15:39:37 -0700854bool OatFileAssistant::OatFileInfo::IsExecutable() {
855 const OatFile* file = GetFile();
856 return (file != nullptr && file->IsExecutable());
857}
858
Richard Uhler743bf362016-04-19 15:39:37 -0700859void OatFileAssistant::OatFileInfo::Reset() {
860 load_attempted_ = false;
861 file_.reset();
862 status_attempted_ = false;
863}
864
Nicolas Geoffray30025092018-04-19 14:43:29 +0100865void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
866 bool use_fd,
867 int zip_fd,
868 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700869 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700870 filename_provided_ = true;
871 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700872 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100873 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700874 vdex_fd_ = vdex_fd;
875 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700876 Reset();
877}
878
879std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
880 file_released_ = true;
881 return std::move(file_);
882}
883
Richard Uhler70a84262016-11-08 16:51:51 +0000884std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000885 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000886 return ReleaseFile();
887 }
888
889 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
890 << " attempting to fall back to interpreting oat file instead.";
891
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800892 switch (Status()) {
893 case kOatBootImageOutOfDate:
Andreas Gampe8f81de52018-03-06 08:39:21 -0800894 // OutOfDate may be either a mismatched image, or a missing image.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800895 if (oat_file_assistant_->HasOriginalDexFiles()) {
Andreas Gampe8f81de52018-03-06 08:39:21 -0800896 // If there are original dex files, it is better to use them (to avoid a potential
897 // quickening mismatch because the boot image changed).
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800898 break;
899 }
Andreas Gampe8f81de52018-03-06 08:39:21 -0800900 // If we do not accept the oat file, we may not have access to dex bytecode at all. Grudgingly
901 // go forward.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800902 FALLTHROUGH_INTENDED;
903
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800904 case kOatUpToDate:
905 case kOatCannotOpen:
906 case kOatDexOutOfDate:
907 break;
Richard Uhler70a84262016-11-08 16:51:51 +0000908 }
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800909
Richard Uhler70a84262016-11-08 16:51:51 +0000910 return std::unique_ptr<OatFile>();
911}
Calin Juravle5f9a8012018-02-12 20:27:46 -0800912
913// TODO(calin): we could provide a more refined status here
914// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
915// track more experiments but adds extra complexity.
916void OatFileAssistant::GetOptimizationStatus(
917 const std::string& filename,
918 InstructionSet isa,
919 std::string* out_compilation_filter,
920 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700921 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
922 // non-executable and check the status manually.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100923 OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false);
Calin Juravle5f9a8012018-02-12 20:27:46 -0800924 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
925
926 if (oat_file == nullptr) {
927 *out_compilation_filter = "run-from-apk";
928 *out_compilation_reason = "unknown";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700929 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -0800930 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700931
932 OatStatus status = oat_file_assistant.GivenOatFileStatus(*oat_file);
933 const char* reason = oat_file->GetCompilationReason();
934 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
935 switch (status) {
936 case OatStatus::kOatUpToDate:
937 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
938 return;
939
940 case kOatCannotOpen: // This should never happen, but be robust.
941 *out_compilation_filter = "error";
942 *out_compilation_reason = "error";
943 return;
944
945 // kOatBootImageOutOfDate - The oat file is up to date with respect to the
946 // dex file, but is out of date with respect to the boot image.
947 case kOatBootImageOutOfDate:
948 FALLTHROUGH_INTENDED;
949 case kOatDexOutOfDate:
950 if (oat_file_assistant.HasOriginalDexFiles()) {
951 *out_compilation_filter = "run-from-apk-fallback";
952 } else {
953 *out_compilation_filter = "run-from-vdex-fallback";
954 }
955 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700956 }
957 LOG(FATAL) << "Unreachable";
958 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -0800959}
960
Richard Uhler66d874d2015-01-15 09:37:19 -0800961} // namespace art