blob: 80ac01f41a2af5108c1106acff229aa1fcb9a94d [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.";
Vladimir Marko0ace5632018-12-14 11:11:47 +0000422 } else if (!image_info->ValidateBootClassPathChecksums(file)) {
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
Vladimir Marko0ace5632018-12-14 11:11:47 +0000563bool OatFileAssistant::ImageInfo::ValidateBootClassPathChecksums(const OatFile& oat_file) const {
564 const char* oat_boot_class_path_checksums =
565 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Markof3d88a82018-12-21 16:38:47 +0000566 if (oat_boot_class_path_checksums == nullptr) {
567 return false;
568 }
569 // The checksums can be either the same or a prefix of the expected checksums,
570 // ending before the ':' delimiter.
571 size_t length = strlen(oat_boot_class_path_checksums);
572 if (length > boot_class_path_checksums.length() ||
573 (length < boot_class_path_checksums.length() && boot_class_path_checksums[length] != ':')) {
574 return false;
575 }
576 return boot_class_path_checksums.compare(0u, length, oat_boot_class_path_checksums) == 0;
Vladimir Marko0ace5632018-12-14 11:11:47 +0000577}
578
Richard Uhler95e09672017-02-22 11:37:41 +0000579std::unique_ptr<OatFileAssistant::ImageInfo>
580OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
581 CHECK(error_msg != nullptr);
582
Richard Uhler95e09672017-02-22 11:37:41 +0000583 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000584 std::unique_ptr<ImageInfo> info(new ImageInfo());
585 info->location = runtime->GetImageLocation();
Vladimir Marko0ace5632018-12-14 11:11:47 +0000586 info->boot_class_path_checksums = gc::space::ImageSpace::GetBootClassPathChecksums(
587 runtime->GetBootClassPath(), info->location, isa, error_msg);
588 if (info->boot_class_path_checksums.empty()) {
Richard Uhler95e09672017-02-22 11:37:41 +0000589 return nullptr;
590 }
Richard Uhler95e09672017-02-22 11:37:41 +0000591 return info;
592}
593
Richard Uhler66d874d2015-01-15 09:37:19 -0800594const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
595 if (!image_info_load_attempted_) {
596 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000597 std::string error_msg;
598 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
599 if (cached_image_info_ == nullptr) {
600 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800601 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800602 }
Richard Uhler95e09672017-02-22 11:37:41 +0000603 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800604}
605
Richard Uhler88bc6732016-11-14 14:38:03 +0000606OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000607 // TODO(calin): Document the side effects of class loading when
608 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700609 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000610 // If the parent of the dex file is writable it means that we can
611 // create the odex file. In this case we unconditionally pick the odex
612 // as the best oat file. This corresponds to the regular use case when
613 // apps gets installed or when they load private, secondary dex file.
614 // For apps on the system partition the odex location will not be
615 // writable and thus the oat location might be more up to date.
616 return odex_;
617 }
618
619 // We cannot write to the odex location. This must be a system app.
620
621 // If the oat location is usable take it.
622 if (oat_.IsUseable()) {
623 return oat_;
624 }
625
626 // The oat file is not usable but the odex file might be up to date.
627 // This is an indication that we are dealing with an up to date prebuilt
628 // (that doesn't need relocation).
629 if (odex_.Status() == kOatUpToDate) {
630 return odex_;
631 }
632
633 // The oat file is not usable and the odex file is not up to date.
634 // However we have access to the original dex file which means we can make
635 // the oat location up to date.
636 if (HasOriginalDexFiles()) {
637 return oat_;
638 }
639
640 // We got into the worst situation here:
641 // - the oat location is not usable
642 // - the prebuild odex location is not up to date
643 // - and we don't have the original dex file anymore (stripped).
644 // Pick the odex if it exists, or the oat if not.
645 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000646}
647
Andreas Gampea463b6a2016-08-12 21:53:32 -0700648std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800649 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100650 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800651 if (art_file.empty()) {
652 return nullptr;
653 }
654 std::string error_msg;
655 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700656 std::unique_ptr<gc::space::ImageSpace> ret =
657 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800658 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800659 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
660 }
661 return ret;
662}
663
Richard Uhler88bc6732016-11-14 14:38:03 +0000664OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
665 bool is_oat_location)
666 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700667{}
668
Richard Uhler88bc6732016-11-14 14:38:03 +0000669bool OatFileAssistant::OatFileInfo::IsOatLocation() {
670 return is_oat_location_;
671}
672
Richard Uhler743bf362016-04-19 15:39:37 -0700673const std::string* OatFileAssistant::OatFileInfo::Filename() {
674 return filename_provided_ ? &filename_ : nullptr;
675}
676
Richard Uhler03bc6592016-11-22 09:42:04 +0000677bool OatFileAssistant::OatFileInfo::IsUseable() {
678 switch (Status()) {
679 case kOatCannotOpen:
680 case kOatDexOutOfDate:
681 case kOatBootImageOutOfDate: return false;
682
Richard Uhler03bc6592016-11-22 09:42:04 +0000683 case kOatUpToDate: return true;
684 }
685 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700686}
687
688OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
689 if (!status_attempted_) {
690 status_attempted_ = true;
691 const OatFile* file = GetFile();
692 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000693 // Check to see if there is a vdex file we can make use of.
694 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700695 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700696 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700697 if (use_fd_) {
698 if (vdex_fd_ >= 0) {
699 struct stat s;
700 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
701 if (rc == -1) {
702 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
703 } else {
704 vdex = VdexFile::Open(vdex_fd_,
705 s.st_size,
706 vdex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100707 /*writable=*/ false,
708 /*low_4gb=*/ false,
709 /*unquicken=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700710 &error_msg);
711 }
712 }
713 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700714 vdex = VdexFile::Open(vdex_filename,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700715 /*writable=*/ false,
716 /*low_4gb=*/ false,
717 /*unquicken=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700718 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700719 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000720 if (vdex == nullptr) {
721 status_ = kOatCannotOpen;
722 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
723 } else {
724 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
725 // The vdex file does not contain enough information to determine
726 // whether it is up to date with respect to the boot image, so we
727 // assume it is out of date.
728 VLOG(oat) << error_msg;
729 status_ = kOatBootImageOutOfDate;
730 } else {
731 status_ = kOatDexOutOfDate;
732 }
733 }
Richard Uhler743bf362016-04-19 15:39:37 -0700734 } else {
735 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700736 VLOG(oat) << file->GetLocation() << " is " << status_
737 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700738 }
739 }
740 return status_;
741}
742
Richard Uhler70a84262016-11-08 16:51:51 +0000743OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700744 CompilerFilter::Filter target,
745 bool profile_changed,
746 bool downgrade,
747 ClassLoaderContext* context) {
748
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700749 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -0700750 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +0000751
Calin Juravle44e5efa2017-09-12 00:54:26 -0700752 // Only check the filter and relocation if the class loader context is ok.
753 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
754 if (class_loader_context_okay) {
755 if (filter_okay && Status() == kOatUpToDate) {
756 // The oat file is in good shape as is.
757 return kNoDexOptNeeded;
758 }
Richard Uhler70a84262016-11-08 16:51:51 +0000759
Calin Juravle44e5efa2017-09-12 00:54:26 -0700760 if (IsUseable()) {
761 return kDex2OatForFilter;
762 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100763
Calin Juravle44e5efa2017-09-12 00:54:26 -0700764 if (Status() == kOatBootImageOutOfDate) {
765 return kDex2OatForBootImage;
766 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100767 }
768
769 if (oat_file_assistant_->HasOriginalDexFiles()) {
770 return kDex2OatFromScratch;
771 } else {
772 // Otherwise there is nothing we can do, even if we want to.
773 return kNoDexOptNeeded;
774 }
Richard Uhler70a84262016-11-08 16:51:51 +0000775}
776
Richard Uhler743bf362016-04-19 15:39:37 -0700777const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
778 CHECK(!file_released_) << "GetFile called after oat file released.";
779 if (!load_attempted_) {
780 load_attempted_ = true;
781 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +0000782 bool executable = oat_file_assistant_->load_executable_;
783 if (executable && oat_file_assistant_->only_load_system_executable_) {
784 executable = LocationIsOnSystem(filename_.c_str());
785 }
Richard Uhler743bf362016-04-19 15:39:37 -0700786 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700787 if (use_fd_) {
788 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100789 file_.reset(OatFile::Open(zip_fd_,
790 vdex_fd_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700791 oat_fd_,
792 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000793 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100794 /*low_4gb=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700795 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100796 /*reservation=*/ nullptr,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700797 &error_msg));
798 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700799 } else {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100800 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100801 filename_.c_str(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700802 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000803 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100804 /*low_4gb=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700805 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100806 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700807 &error_msg));
808 }
Richard Uhler743bf362016-04-19 15:39:37 -0700809 if (file_.get() == nullptr) {
810 VLOG(oat) << "OatFileAssistant test for existing oat file "
811 << filename_ << ": " << error_msg;
812 }
813 }
814 }
815 return file_.get();
816}
817
818bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700819 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700820 const OatFile* file = GetFile();
821 if (file == nullptr) {
822 return false;
823 }
824
825 CompilerFilter::Filter current = file->GetCompilerFilter();
826 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
827 VLOG(oat) << "Compiler filter not okay because Profile changed";
828 return false;
829 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700830 return downgrade ? !CompilerFilter::IsBetter(current, target) :
831 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700832}
833
Calin Juravle44e5efa2017-09-12 00:54:26 -0700834bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
835 if (context == nullptr) {
836 VLOG(oat) << "ClassLoaderContext check ignored: null context";
837 return true;
838 }
839
840 const OatFile* file = GetFile();
841 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -0700842 // No oat file means we have nothing to verify.
843 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700844 }
845
Calin Juravle20c46442017-09-12 00:54:26 -0700846 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -0700847 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -0700848 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -0700849 : "";
850
851 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
852 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
853 return false;
854 }
855
Mathieu Chartieradc90862018-05-11 13:03:06 -0700856
857 const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
858 ClassLoaderContext::VerificationResult::kMismatch;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700859 if (!result) {
860 VLOG(oat) << "ClassLoaderContext check failed. Context was "
861 << file->GetClassLoaderContext()
862 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
863 }
864 return result;
865}
866
Richard Uhler743bf362016-04-19 15:39:37 -0700867bool OatFileAssistant::OatFileInfo::IsExecutable() {
868 const OatFile* file = GetFile();
869 return (file != nullptr && file->IsExecutable());
870}
871
Richard Uhler743bf362016-04-19 15:39:37 -0700872void OatFileAssistant::OatFileInfo::Reset() {
873 load_attempted_ = false;
874 file_.reset();
875 status_attempted_ = false;
876}
877
Nicolas Geoffray30025092018-04-19 14:43:29 +0100878void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
879 bool use_fd,
880 int zip_fd,
881 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700882 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700883 filename_provided_ = true;
884 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700885 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100886 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700887 vdex_fd_ = vdex_fd;
888 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700889 Reset();
890}
891
892std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
893 file_released_ = true;
894 return std::move(file_);
895}
896
Richard Uhler70a84262016-11-08 16:51:51 +0000897std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000898 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000899 return ReleaseFile();
900 }
901
902 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
903 << " attempting to fall back to interpreting oat file instead.";
904
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800905 switch (Status()) {
906 case kOatBootImageOutOfDate:
Andreas Gampe8f81de52018-03-06 08:39:21 -0800907 // OutOfDate may be either a mismatched image, or a missing image.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800908 if (oat_file_assistant_->HasOriginalDexFiles()) {
Andreas Gampe8f81de52018-03-06 08:39:21 -0800909 // If there are original dex files, it is better to use them (to avoid a potential
910 // quickening mismatch because the boot image changed).
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800911 break;
912 }
Andreas Gampe8f81de52018-03-06 08:39:21 -0800913 // If we do not accept the oat file, we may not have access to dex bytecode at all. Grudgingly
914 // go forward.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800915 FALLTHROUGH_INTENDED;
916
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800917 case kOatUpToDate:
918 case kOatCannotOpen:
919 case kOatDexOutOfDate:
920 break;
Richard Uhler70a84262016-11-08 16:51:51 +0000921 }
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800922
Richard Uhler70a84262016-11-08 16:51:51 +0000923 return std::unique_ptr<OatFile>();
924}
Calin Juravle5f9a8012018-02-12 20:27:46 -0800925
926// TODO(calin): we could provide a more refined status here
927// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
928// track more experiments but adds extra complexity.
929void OatFileAssistant::GetOptimizationStatus(
930 const std::string& filename,
931 InstructionSet isa,
932 std::string* out_compilation_filter,
933 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700934 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
935 // non-executable and check the status manually.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100936 OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false);
Calin Juravle5f9a8012018-02-12 20:27:46 -0800937 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
938
939 if (oat_file == nullptr) {
940 *out_compilation_filter = "run-from-apk";
941 *out_compilation_reason = "unknown";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700942 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -0800943 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700944
945 OatStatus status = oat_file_assistant.GivenOatFileStatus(*oat_file);
946 const char* reason = oat_file->GetCompilationReason();
947 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
948 switch (status) {
949 case OatStatus::kOatUpToDate:
950 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
951 return;
952
953 case kOatCannotOpen: // This should never happen, but be robust.
954 *out_compilation_filter = "error";
955 *out_compilation_reason = "error";
956 return;
957
958 // kOatBootImageOutOfDate - The oat file is up to date with respect to the
959 // dex file, but is out of date with respect to the boot image.
960 case kOatBootImageOutOfDate:
961 FALLTHROUGH_INTENDED;
962 case kOatDexOutOfDate:
963 if (oat_file_assistant.HasOriginalDexFiles()) {
964 *out_compilation_filter = "run-from-apk-fallback";
965 } else {
966 *out_compilation_filter = "run-from-vdex-fallback";
967 }
968 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700969 }
970 LOG(FATAL) << "Unreachable";
971 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -0800972}
973
Richard Uhler66d874d2015-01-15 09:37:19 -0800974} // namespace art