blob: 9bb12032d77d9f1927fa0b2fada7155c171d652d [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>
David Brazdil35a3f6a2019-03-04 15:59:06 +000022#include "zlib.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080023
Andreas Gampec15a2f42017-04-21 12:09:39 -070024#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080025#include "android-base/strings.h"
26
David Sehr891a50e2017-10-27 17:01:07 -070027#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080028#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070029#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070031#include "base/stl_util.h"
Vladimir Markobcd99be2019-03-22 16:21:31 +000032#include "base/string_view_cpp20.h"
David Sehrc431b9d2018-03-02 12:01:51 -080033#include "base/utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "class_linker.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000035#include "class_loader_context.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "compiler_filter.h"
David Sehr013fd802018-01-11 22:55:24 -080037#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080038#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080039#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040#include "gc/heap.h"
41#include "gc/space/image_space.h"
42#include "image.h"
43#include "oat.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080044#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070045#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000046#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080047
48namespace art {
49
Richard Uhler69bcf2c2017-01-24 10:25:21 +000050using android::base::StringPrintf;
51
David Brazdil35a3f6a2019-03-04 15:59:06 +000052static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@";
53static constexpr const char* kVdexExtension = ".vdex";
54
Narayan Kamath8943c1d2016-05-02 13:14:48 +010055std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
56 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000057 case OatFileAssistant::kOatCannotOpen:
58 stream << "kOatCannotOpen";
59 break;
60 case OatFileAssistant::kOatDexOutOfDate:
61 stream << "kOatDexOutOfDate";
62 break;
63 case OatFileAssistant::kOatBootImageOutOfDate:
64 stream << "kOatBootImageOutOfDate";
65 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010066 case OatFileAssistant::kOatUpToDate:
67 stream << "kOatUpToDate";
68 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010069 default:
70 UNREACHABLE();
71 }
72
73 return stream;
74}
75
Richard Uhler66d874d2015-01-15 09:37:19 -080076OatFileAssistant::OatFileAssistant(const char* dex_location,
77 const InstructionSet isa,
Nicolas Geoffray29742602017-12-14 10:09:03 +000078 bool load_executable,
79 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070080 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000081 isa,
82 load_executable,
83 only_load_system_executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010084 /*vdex_fd=*/ -1,
85 /*oat_fd=*/ -1,
86 /*zip_fd=*/ -1) {}
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070087
88
89OatFileAssistant::OatFileAssistant(const char* dex_location,
90 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070091 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000092 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070093 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070094 int oat_fd,
95 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000096 : isa_(isa),
97 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +000098 only_load_system_executable_(only_load_system_executable),
Andreas Gampe98ea9d92018-10-19 14:06:15 -070099 odex_(this, /*is_oat_location=*/ false),
100 oat_(this, /*is_oat_location=*/ true),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700101 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -0700102 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +0000103
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700104 if (zip_fd < 0) {
105 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
106 << " oat_fd=" << oat_fd;
107 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
108 << " vdex_fd=" << vdex_fd;;
109 }
110
Calin Juravle099f8d62017-09-05 19:04:04 -0700111 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700112
Richard Uhler66d874d2015-01-15 09:37:19 -0800113 if (load_executable_ && isa != kRuntimeISA) {
114 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
115 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
116 load_executable_ = false;
117 }
118
Richard Uhler743bf362016-04-19 15:39:37 -0700119 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700120 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700121 std::string odex_file_name;
122 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100123 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700124 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700125 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
126 }
127
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700128 if (!UseFdToReadFiles()) {
129 // Get the oat filename.
130 std::string oat_file_name;
131 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100132 oat_.Reset(oat_file_name, /*use_fd=*/ false);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700133 } else {
134 LOG(WARNING) << "Failed to determine oat file name for dex location "
135 << dex_location_ << ": " << error_msg;
136 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000137 }
138
139 // Check if the dex directory is writable.
140 // This will be needed in most uses of OatFileAssistant and so it's OK to
141 // compute it eagerly. (the only use which will not make use of it is
142 // OatFileAssistant::GetStatusDump())
143 size_t pos = dex_location_.rfind('/');
144 if (pos == std::string::npos) {
145 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700146 } else if (!UseFdToReadFiles()) {
147 // We cannot test for parent access when using file descriptors. That's ok
148 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000149 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700150 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000151 dex_parent_writable_ = true;
152 } else {
153 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700154 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800155 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800156}
157
158OatFileAssistant::~OatFileAssistant() {
159 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100160 if (flock_.get() != nullptr) {
161 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800162 }
163}
164
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700165bool OatFileAssistant::UseFdToReadFiles() {
166 return zip_fd_ >= 0;
167}
168
Richard Uhler66d874d2015-01-15 09:37:19 -0800169bool OatFileAssistant::IsInBootClassPath() {
170 // Note: We check the current boot class path, regardless of the ISA
171 // specified by the user. This is okay, because the boot class path should
172 // be the same for all ISAs.
173 // TODO: Can we verify the boot class path is the same for all ISAs?
174 Runtime* runtime = Runtime::Current();
175 ClassLinker* class_linker = runtime->GetClassLinker();
176 const auto& boot_class_path = class_linker->GetBootClassPath();
177 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700178 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800179 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
180 return true;
181 }
182 }
183 return false;
184}
185
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700186int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
187 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700188 bool downgrade,
David Brazdil89821862019-03-19 13:57:43 +0000189 ClassLoaderContext* class_loader_context,
190 const std::vector<int>& context_fds) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000191 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700192 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
193 profile_changed,
194 downgrade,
David Brazdil89821862019-03-19 13:57:43 +0000195 class_loader_context,
196 context_fds);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000197 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
198 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800199 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000200 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800201}
202
Richard Uhler01be6812016-05-17 10:34:52 -0700203bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000204 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700205}
206
Richard Uhler66d874d2015-01-15 09:37:19 -0800207std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000208 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800209}
210
Richard Uhler46cc64f2016-11-14 14:53:55 +0000211std::string OatFileAssistant::GetStatusDump() {
212 std::ostringstream status;
213 bool oat_file_exists = false;
214 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000215 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000216 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000217 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000218
Richard Uhler46cc64f2016-11-14 14:53:55 +0000219 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000220 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
221 const OatFile* file = oat_.GetFile();
222 if (file == nullptr) {
223 // If the file is null even though the status is not kOatCannotOpen, it
224 // means we must have a vdex file with no corresponding oat file. In
225 // this case we cannot determine the compilation filter. Indicate that
226 // we have only the vdex file instead.
227 status << "vdex-only";
228 } else {
229 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
230 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000231 }
232
Richard Uhler03bc6592016-11-22 09:42:04 +0000233 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000234 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000235 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000236
Richard Uhler46cc64f2016-11-14 14:53:55 +0000237 odex_file_exists = true;
238 if (oat_file_exists) {
239 status << "] ";
240 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000241 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
242 const OatFile* file = odex_.GetFile();
243 if (file == nullptr) {
244 status << "vdex-only";
245 } else {
246 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
247 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000248 }
249
250 if (!oat_file_exists && !odex_file_exists) {
251 status << "invalid[";
252 }
253
254 status << "]";
255 return status.str();
256}
257
Richard Uhler66d874d2015-01-15 09:37:19 -0800258std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700259 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800260 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700261 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
262 return dex_files;
263 } else {
264 return std::vector<std::unique_ptr<const DexFile>>();
265 }
266}
Richard Uhler66d874d2015-01-15 09:37:19 -0800267
Calin Juravle87e2cb62017-06-13 21:48:45 -0700268bool OatFileAssistant::LoadDexFiles(
269 const OatFile &oat_file,
270 const std::string& dex_location,
271 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000272 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800273 std::string error_msg;
Andreas Gampeb40d3612018-06-26 15:49:42 -0700274 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700275 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700277 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700278 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800279 }
280
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700281 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800282 if (dex_file.get() == nullptr) {
283 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700284 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700286 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800287
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000288 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700289 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700290 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000291 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700292 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000293 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800294 break;
295 }
296
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700297 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800298 if (dex_file.get() == nullptr) {
299 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700300 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800301 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700302 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700304 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800305}
306
Richard Uhler9b994ea2015-06-24 08:44:19 -0700307bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000308 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700309 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000310 // GetRequiredDexChecksums.
311 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700312 return has_original_dex_files_;
313}
314
Richard Uhler95abd042015-03-24 09:51:28 -0700315OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700316 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800317}
318
Richard Uhler95abd042015-03-24 09:51:28 -0700319OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700320 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800321}
322
Richard Uhler2f27abd2017-01-31 14:02:34 +0000323bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000324 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
325 if (required_dex_checksums == nullptr) {
326 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
327 return true;
328 }
329
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000330 uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000331 if (required_dex_checksums->size() != number_of_dex_files) {
332 *error_msg = StringPrintf("expected %zu dex files but found %u",
333 required_dex_checksums->size(),
334 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000335 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800336 }
337
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000338 for (uint32_t i = 0; i < number_of_dex_files; i++) {
339 uint32_t expected_checksum = (*required_dex_checksums)[i];
340 uint32_t actual_checksum = file.GetLocationChecksum(i);
341 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700342 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000343 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
344 "Expected: %u, actual: %u",
345 dex.c_str(),
346 expected_checksum,
347 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000348 return false;
349 }
350 }
351
Richard Uhler2f27abd2017-01-31 14:02:34 +0000352 return true;
353}
354
355bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000356 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
357 if (required_dex_checksums == nullptr) {
358 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
359 return true;
360 }
361
362 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
363 if (required_dex_checksums->size() != number_of_dex_files) {
364 *error_msg = StringPrintf("expected %zu dex files but found %u",
365 required_dex_checksums->size(),
366 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000367 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800368 }
369
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000370 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700371 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000372 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700373 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000374 if (oat_dex_file == nullptr) {
375 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
376 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000378 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
379 if (expected_checksum != actual_checksum) {
380 VLOG(oat) << "Dex checksum does not match for dex: " << dex
381 << ". Expected: " << expected_checksum
382 << ", Actual: " << actual_checksum;
383 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800384 }
385 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000386 return true;
387}
388
389OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
390 // Verify the ART_USE_READ_BARRIER state.
391 // TODO: Don't fully reject files due to read barrier state. If they contain
392 // compiled code and are otherwise okay, we should return something like
393 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
394 // barrier state doesn't matter.
395 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
396 constexpr bool kRuntimeIsCC = kUseReadBarrier;
397 if (is_cc != kRuntimeIsCC) {
398 return kOatCannotOpen;
399 }
400
401 // Verify the dex checksum.
402 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000403 VdexFile* vdex = file.GetVdexFile();
404 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
405 LOG(ERROR) << error_msg;
406 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000407 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800408
Andreas Gampe29d38e72016-03-23 15:31:51 +0000409 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000410
Richard Uhler66d874d2015-01-15 09:37:19 -0800411 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000412 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000413 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000414 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000415 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000416 }
417 } else {
418 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 }
420
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000421 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
422 DCHECK(required_dex_checksums_attempted_);
423 if (only_load_system_executable_ &&
424 !LocationIsOnSystem(file.GetLocation().c_str()) &&
425 file.ContainsDexCode() &&
426 zip_file_only_contains_uncompressed_dex_) {
427 LOG(ERROR) << "Not loading "
428 << dex_location_
429 << ": oat file has dex code, but APK has uncompressed dex code";
430 return kOatDexOutOfDate;
431 }
432
Richard Uhlere8109f72016-04-18 10:40:50 -0700433 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800434}
435
David Brazdil35a3f6a2019-03-04 15:59:06 +0000436bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
437 InstructionSet isa,
438 /* out */ uint32_t* location_checksum,
439 /* out */ std::string* dex_location,
440 /* out */ std::string* vdex_filename) {
441 uint32_t checksum = adler32(0L, Z_NULL, 0);
442 for (const DexFile::Header* header : headers) {
443 checksum = adler32_combine(checksum,
444 header->checksum_,
445 header->file_size_ - DexFile::kNumNonChecksumBytes);
446 }
447 *location_checksum = checksum;
448
449 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
450 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
451 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
452 return false;
453 }
454 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
455
456 std::string odex_filename;
457 std::string error_msg;
458 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
459 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
460 return false;
461 }
462
463 *vdex_filename = GetVdexFilename(odex_filename);
464 return true;
465}
466
467bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
468 DCHECK(basename.find('/') == std::string::npos);
469 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
470 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
471 !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) ||
472 !android::base::EndsWith(basename, kVdexExtension)) {
473 return false;
474 }
475 // Check that all characters between the prefix and extension are decimal digits.
476 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
477 if (!std::isdigit(basename[i])) {
478 return false;
479 }
480 }
481 return true;
482}
483
Calin Juravle357c66d2017-05-04 01:57:17 +0000484static bool DexLocationToOdexNames(const std::string& location,
485 InstructionSet isa,
486 std::string* odex_filename,
487 std::string* oat_dir,
488 std::string* isa_dir,
489 std::string* error_msg) {
490 CHECK(odex_filename != nullptr);
491 CHECK(error_msg != nullptr);
492
493 // The odex file name is formed by replacing the dex_location extension with
494 // .odex and inserting an oat/<isa> directory. For example:
495 // location = /foo/bar/baz.jar
496 // odex_location = /foo/bar/oat/<isa>/baz.odex
497
498 // Find the directory portion of the dex location and add the oat/<isa>
499 // directory.
500 size_t pos = location.rfind('/');
501 if (pos == std::string::npos) {
502 *error_msg = "Dex location " + location + " has no directory.";
503 return false;
504 }
505 std::string dir = location.substr(0, pos+1);
506 // Add the oat directory.
507 dir += "oat";
508 if (oat_dir != nullptr) {
509 *oat_dir = dir;
510 }
511 // Add the isa directory
512 dir += "/" + std::string(GetInstructionSetString(isa));
513 if (isa_dir != nullptr) {
514 *isa_dir = dir;
515 }
516
517 // Get the base part of the file without the extension.
518 std::string file = location.substr(pos+1);
519 pos = file.rfind('.');
520 if (pos == std::string::npos) {
521 *error_msg = "Dex location " + location + " has no extension.";
522 return false;
523 }
524 std::string base = file.substr(0, pos);
525
526 *odex_filename = dir + "/" + base + ".odex";
527 return true;
528}
529
Richard Uhlerb81881d2016-04-19 13:08:04 -0700530bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
531 InstructionSet isa,
532 std::string* odex_filename,
533 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000534 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800535}
536
Richard Uhlerb81881d2016-04-19 13:08:04 -0700537bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
538 InstructionSet isa,
539 std::string* oat_filename,
540 std::string* error_msg) {
541 CHECK(oat_filename != nullptr);
542 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800543
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700544 // If ANDROID_DATA is not set, return false instead of aborting.
545 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100546 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700547 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
548 return false;
549 }
550
Richard Uhler55b58b62016-08-12 09:05:13 -0700551 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
552 if (cache_dir.empty()) {
553 *error_msg = "Dalvik cache directory does not exist";
554 return false;
555 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700556
557 // TODO: The oat file assistant should be the definitive place for
558 // determining the oat file name from the dex location, not
559 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700560 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800561}
562
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000563const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
564 if (!required_dex_checksums_attempted_) {
565 required_dex_checksums_attempted_ = true;
566 required_dex_checksums_found_ = false;
567 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800568 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800569 const ArtDexFileLoader dex_file_loader;
570 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
571 &cached_required_dex_checksums_,
572 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000573 zip_fd_,
574 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000575 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700576 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800577 } else {
578 // This can happen if the original dex file has been stripped from the
579 // apk.
580 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700581 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800582
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000583 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700584 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800585 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000586 required_dex_checksums_found_ = true;
587 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700588 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Andreas Gampeb40d3612018-06-26 15:49:42 -0700589 const OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000590 if (odex_dex_file == nullptr) {
591 required_dex_checksums_found_ = false;
592 break;
593 }
594 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800595 }
596 }
597 }
598 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000599 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800600}
601
Vladimir Markobcd99be2019-03-22 16:21:31 +0000602bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
603 // Get the BCP from the oat file.
604 const char* oat_boot_class_path =
605 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
606 if (oat_boot_class_path == nullptr) {
607 return false;
608 }
609
610 // Check that the oat BCP is a prefix of current BCP locations and count components.
611 Runtime* runtime = Runtime::Current();
612 size_t component_count = 0u;
613 std::string_view remaining_bcp(oat_boot_class_path);
614 bool bcp_ok = false;
615 for (const std::string& location : runtime->GetBootClassPathLocations()) {
616 if (!StartsWith(remaining_bcp, location)) {
617 break;
618 }
619 remaining_bcp.remove_prefix(location.size());
620 ++component_count;
621 if (remaining_bcp.empty()) {
622 bcp_ok = true;
623 break;
624 }
625 if (!StartsWith(remaining_bcp, ":")) {
626 break;
627 }
628 remaining_bcp.remove_prefix(1u);
629 }
630 if (!bcp_ok) {
631 return false;
632 }
633
634 // Get the checksums.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000635 const char* oat_boot_class_path_checksums =
636 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Markof3d88a82018-12-21 16:38:47 +0000637 if (oat_boot_class_path_checksums == nullptr) {
638 return false;
639 }
Vladimir Marko0ace5632018-12-14 11:11:47 +0000640
Vladimir Markobcd99be2019-03-22 16:21:31 +0000641 // Retrieve checksums for this portion of the BCP if we do not have them cached.
642 if (cached_boot_class_path_checksum_component_count_ != component_count) {
643 ArrayRef<const std::string> boot_class_path(runtime->GetBootClassPath());
Richard Uhler95e09672017-02-22 11:37:41 +0000644 std::string error_msg;
Vladimir Markobcd99be2019-03-22 16:21:31 +0000645 std::string boot_class_path_checksums = gc::space::ImageSpace::GetBootClassPathChecksums(
646 boot_class_path.SubArray(/* pos= */ 0u, component_count),
647 runtime->GetImageLocation(),
648 isa_,
649 runtime->GetImageSpaceLoadingOrder(),
650 &error_msg);
651 if (boot_class_path_checksums.empty()) {
652 VLOG(oat) << "No image for oat image checksum to match against.";
653
654 if (HasOriginalDexFiles()) {
655 return false;
656 }
657
658 // If there is no original dex file to fall back to, grudgingly accept
659 // the oat file. This could technically lead to crashes, but there's no
660 // way we could find a better oat file to use for this dex location,
661 // and it's better than being stuck in a boot loop with no way out.
662 // The problem will hopefully resolve itself the next time the runtime
663 // starts up.
664 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
665 << "Allow oat file use. This is potentially dangerous.";
666
667 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800668 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000669 cached_boot_class_path_checksum_component_count_ = component_count;
670 cached_boot_class_path_checksums_ = boot_class_path_checksums;
Richard Uhler66d874d2015-01-15 09:37:19 -0800671 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000672
673 // Compare the checksums.
674 return cached_boot_class_path_checksums_ == oat_boot_class_path_checksums;
Richard Uhler66d874d2015-01-15 09:37:19 -0800675}
676
Richard Uhler88bc6732016-11-14 14:38:03 +0000677OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000678 // TODO(calin): Document the side effects of class loading when
679 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700680 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000681 // If the parent of the dex file is writable it means that we can
682 // create the odex file. In this case we unconditionally pick the odex
683 // as the best oat file. This corresponds to the regular use case when
684 // apps gets installed or when they load private, secondary dex file.
685 // For apps on the system partition the odex location will not be
686 // writable and thus the oat location might be more up to date.
687 return odex_;
688 }
689
690 // We cannot write to the odex location. This must be a system app.
691
692 // If the oat location is usable take it.
693 if (oat_.IsUseable()) {
694 return oat_;
695 }
696
697 // The oat file is not usable but the odex file might be up to date.
698 // This is an indication that we are dealing with an up to date prebuilt
699 // (that doesn't need relocation).
700 if (odex_.Status() == kOatUpToDate) {
701 return odex_;
702 }
703
704 // The oat file is not usable and the odex file is not up to date.
705 // However we have access to the original dex file which means we can make
706 // the oat location up to date.
707 if (HasOriginalDexFiles()) {
708 return oat_;
709 }
710
711 // We got into the worst situation here:
712 // - the oat location is not usable
713 // - the prebuild odex location is not up to date
714 // - and we don't have the original dex file anymore (stripped).
715 // Pick the odex if it exists, or the oat if not.
716 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000717}
718
Andreas Gampea463b6a2016-08-12 21:53:32 -0700719std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800720 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100721 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800722 if (art_file.empty()) {
723 return nullptr;
724 }
725 std::string error_msg;
726 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700727 std::unique_ptr<gc::space::ImageSpace> ret =
728 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800729 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800730 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
731 }
732 return ret;
733}
734
Richard Uhler88bc6732016-11-14 14:38:03 +0000735OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
736 bool is_oat_location)
737 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700738{}
739
Richard Uhler88bc6732016-11-14 14:38:03 +0000740bool OatFileAssistant::OatFileInfo::IsOatLocation() {
741 return is_oat_location_;
742}
743
Richard Uhler743bf362016-04-19 15:39:37 -0700744const std::string* OatFileAssistant::OatFileInfo::Filename() {
745 return filename_provided_ ? &filename_ : nullptr;
746}
747
Richard Uhler03bc6592016-11-22 09:42:04 +0000748bool OatFileAssistant::OatFileInfo::IsUseable() {
749 switch (Status()) {
750 case kOatCannotOpen:
751 case kOatDexOutOfDate:
752 case kOatBootImageOutOfDate: return false;
753
Richard Uhler03bc6592016-11-22 09:42:04 +0000754 case kOatUpToDate: return true;
755 }
756 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700757}
758
759OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
760 if (!status_attempted_) {
761 status_attempted_ = true;
762 const OatFile* file = GetFile();
763 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000764 // Check to see if there is a vdex file we can make use of.
765 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700766 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700767 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700768 if (use_fd_) {
769 if (vdex_fd_ >= 0) {
770 struct stat s;
771 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
772 if (rc == -1) {
773 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
774 } else {
775 vdex = VdexFile::Open(vdex_fd_,
776 s.st_size,
777 vdex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100778 /*writable=*/ false,
779 /*low_4gb=*/ false,
780 /*unquicken=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700781 &error_msg);
782 }
783 }
784 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700785 vdex = VdexFile::Open(vdex_filename,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700786 /*writable=*/ false,
787 /*low_4gb=*/ false,
788 /*unquicken=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700789 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700790 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000791 if (vdex == nullptr) {
792 status_ = kOatCannotOpen;
793 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
794 } else {
795 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
796 // The vdex file does not contain enough information to determine
797 // whether it is up to date with respect to the boot image, so we
798 // assume it is out of date.
799 VLOG(oat) << error_msg;
800 status_ = kOatBootImageOutOfDate;
801 } else {
802 status_ = kOatDexOutOfDate;
803 }
804 }
Richard Uhler743bf362016-04-19 15:39:37 -0700805 } else {
806 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700807 VLOG(oat) << file->GetLocation() << " is " << status_
808 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700809 }
810 }
811 return status_;
812}
813
Richard Uhler70a84262016-11-08 16:51:51 +0000814OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700815 CompilerFilter::Filter target,
816 bool profile_changed,
817 bool downgrade,
David Brazdil89821862019-03-19 13:57:43 +0000818 ClassLoaderContext* context,
819 const std::vector<int>& context_fds) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700820
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700821 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
David Brazdil89821862019-03-19 13:57:43 +0000822 bool class_loader_context_okay = ClassLoaderContextIsOkay(context, context_fds);
Richard Uhler70a84262016-11-08 16:51:51 +0000823
Calin Juravle44e5efa2017-09-12 00:54:26 -0700824 // Only check the filter and relocation if the class loader context is ok.
825 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
826 if (class_loader_context_okay) {
827 if (filter_okay && Status() == kOatUpToDate) {
828 // The oat file is in good shape as is.
829 return kNoDexOptNeeded;
830 }
Richard Uhler70a84262016-11-08 16:51:51 +0000831
Calin Juravle44e5efa2017-09-12 00:54:26 -0700832 if (IsUseable()) {
833 return kDex2OatForFilter;
834 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100835
Calin Juravle44e5efa2017-09-12 00:54:26 -0700836 if (Status() == kOatBootImageOutOfDate) {
837 return kDex2OatForBootImage;
838 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100839 }
840
841 if (oat_file_assistant_->HasOriginalDexFiles()) {
842 return kDex2OatFromScratch;
843 } else {
844 // Otherwise there is nothing we can do, even if we want to.
845 return kNoDexOptNeeded;
846 }
Richard Uhler70a84262016-11-08 16:51:51 +0000847}
848
Richard Uhler743bf362016-04-19 15:39:37 -0700849const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
850 CHECK(!file_released_) << "GetFile called after oat file released.";
851 if (!load_attempted_) {
852 load_attempted_ = true;
853 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +0000854 bool executable = oat_file_assistant_->load_executable_;
855 if (executable && oat_file_assistant_->only_load_system_executable_) {
856 executable = LocationIsOnSystem(filename_.c_str());
857 }
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000858 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
Richard Uhler743bf362016-04-19 15:39:37 -0700859 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700860 if (use_fd_) {
861 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100862 file_.reset(OatFile::Open(zip_fd_,
863 vdex_fd_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700864 oat_fd_,
865 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000866 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100867 /*low_4gb=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700868 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100869 /*reservation=*/ nullptr,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700870 &error_msg));
871 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700872 } else {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100873 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100874 filename_.c_str(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700875 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000876 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100877 /*low_4gb=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700878 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100879 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700880 &error_msg));
881 }
Richard Uhler743bf362016-04-19 15:39:37 -0700882 if (file_.get() == nullptr) {
883 VLOG(oat) << "OatFileAssistant test for existing oat file "
884 << filename_ << ": " << error_msg;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000885 } else {
886 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
Richard Uhler743bf362016-04-19 15:39:37 -0700887 }
888 }
889 }
890 return file_.get();
891}
892
893bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700894 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700895 const OatFile* file = GetFile();
896 if (file == nullptr) {
897 return false;
898 }
899
900 CompilerFilter::Filter current = file->GetCompilerFilter();
901 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
902 VLOG(oat) << "Compiler filter not okay because Profile changed";
903 return false;
904 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700905 return downgrade ? !CompilerFilter::IsBetter(current, target) :
906 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700907}
908
David Brazdil89821862019-03-19 13:57:43 +0000909bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context,
910 const std::vector<int>& context_fds) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700911 if (context == nullptr) {
912 VLOG(oat) << "ClassLoaderContext check ignored: null context";
913 return true;
914 }
915
916 const OatFile* file = GetFile();
917 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -0700918 // No oat file means we have nothing to verify.
919 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700920 }
921
Calin Juravle20c46442017-09-12 00:54:26 -0700922 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -0700923 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -0700924 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -0700925 : "";
926
David Brazdil89821862019-03-19 13:57:43 +0000927 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir, context_fds)) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700928 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
929 return false;
930 }
931
Mathieu Chartieradc90862018-05-11 13:03:06 -0700932 const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
933 ClassLoaderContext::VerificationResult::kMismatch;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700934 if (!result) {
935 VLOG(oat) << "ClassLoaderContext check failed. Context was "
936 << file->GetClassLoaderContext()
937 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
938 }
939 return result;
940}
941
Richard Uhler743bf362016-04-19 15:39:37 -0700942bool OatFileAssistant::OatFileInfo::IsExecutable() {
943 const OatFile* file = GetFile();
944 return (file != nullptr && file->IsExecutable());
945}
946
Richard Uhler743bf362016-04-19 15:39:37 -0700947void OatFileAssistant::OatFileInfo::Reset() {
948 load_attempted_ = false;
949 file_.reset();
950 status_attempted_ = false;
951}
952
Nicolas Geoffray30025092018-04-19 14:43:29 +0100953void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
954 bool use_fd,
955 int zip_fd,
956 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700957 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700958 filename_provided_ = true;
959 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700960 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100961 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700962 vdex_fd_ = vdex_fd;
963 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700964 Reset();
965}
966
967std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
968 file_released_ = true;
969 return std::move(file_);
970}
971
Richard Uhler70a84262016-11-08 16:51:51 +0000972std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000973 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000974 return ReleaseFile();
975 }
976
977 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
978 << " attempting to fall back to interpreting oat file instead.";
979
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800980 switch (Status()) {
981 case kOatBootImageOutOfDate:
Andreas Gampe8f81de52018-03-06 08:39:21 -0800982 // OutOfDate may be either a mismatched image, or a missing image.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800983 if (oat_file_assistant_->HasOriginalDexFiles()) {
Andreas Gampe8f81de52018-03-06 08:39:21 -0800984 // If there are original dex files, it is better to use them (to avoid a potential
985 // quickening mismatch because the boot image changed).
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800986 break;
987 }
Andreas Gampe8f81de52018-03-06 08:39:21 -0800988 // If we do not accept the oat file, we may not have access to dex bytecode at all. Grudgingly
989 // go forward.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800990 FALLTHROUGH_INTENDED;
991
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800992 case kOatUpToDate:
993 case kOatCannotOpen:
994 case kOatDexOutOfDate:
995 break;
Richard Uhler70a84262016-11-08 16:51:51 +0000996 }
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800997
Richard Uhler70a84262016-11-08 16:51:51 +0000998 return std::unique_ptr<OatFile>();
999}
Calin Juravle5f9a8012018-02-12 20:27:46 -08001000
1001// TODO(calin): we could provide a more refined status here
1002// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
1003// track more experiments but adds extra complexity.
1004void OatFileAssistant::GetOptimizationStatus(
1005 const std::string& filename,
1006 InstructionSet isa,
1007 std::string* out_compilation_filter,
1008 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001009 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
1010 // non-executable and check the status manually.
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001011 OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false);
Calin Juravle5f9a8012018-02-12 20:27:46 -08001012 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1013
1014 if (oat_file == nullptr) {
1015 *out_compilation_filter = "run-from-apk";
1016 *out_compilation_reason = "unknown";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001017 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001018 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001019
1020 OatStatus status = oat_file_assistant.GivenOatFileStatus(*oat_file);
1021 const char* reason = oat_file->GetCompilationReason();
1022 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1023 switch (status) {
1024 case OatStatus::kOatUpToDate:
1025 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
1026 return;
1027
1028 case kOatCannotOpen: // This should never happen, but be robust.
1029 *out_compilation_filter = "error";
1030 *out_compilation_reason = "error";
1031 return;
1032
1033 // kOatBootImageOutOfDate - The oat file is up to date with respect to the
1034 // dex file, but is out of date with respect to the boot image.
1035 case kOatBootImageOutOfDate:
1036 FALLTHROUGH_INTENDED;
1037 case kOatDexOutOfDate:
1038 if (oat_file_assistant.HasOriginalDexFiles()) {
1039 *out_compilation_filter = "run-from-apk-fallback";
1040 } else {
1041 *out_compilation_filter = "run-from-vdex-fallback";
1042 }
1043 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001044 }
1045 LOG(FATAL) << "Unreachable";
1046 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001047}
1048
Richard Uhler66d874d2015-01-15 09:37:19 -08001049} // namespace art