blob: 245ae3662220f4735ee1b02efb341ba4862cee82 [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
Eric Holkc7ac91b2021-02-04 21:44:01 +000027#include "base/compiler_filter.h"
David Sehr891a50e2017-10-27 17:01:07 -070028#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070030#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070032#include "base/stl_util.h"
Vladimir Markobcd99be2019-03-22 16:21:31 +000033#include "base/string_view_cpp20.h"
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -070034#include "base/systrace.h"
David Sehrc431b9d2018-03-02 12:01:51 -080035#include "base/utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "class_linker.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000037#include "class_loader_context.h"
David Sehr013fd802018-01-11 22:55:24 -080038#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080039#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080040#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041#include "gc/heap.h"
42#include "gc/space/image_space.h"
43#include "image.h"
44#include "oat.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080045#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070046#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000047#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080048
49namespace art {
50
Richard Uhler69bcf2c2017-01-24 10:25:21 +000051using android::base::StringPrintf;
52
David Brazdil35a3f6a2019-03-04 15:59:06 +000053static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@";
54static constexpr const char* kVdexExtension = ".vdex";
55
Narayan Kamath8943c1d2016-05-02 13:14:48 +010056std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
57 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000058 case OatFileAssistant::kOatCannotOpen:
59 stream << "kOatCannotOpen";
60 break;
61 case OatFileAssistant::kOatDexOutOfDate:
62 stream << "kOatDexOutOfDate";
63 break;
64 case OatFileAssistant::kOatBootImageOutOfDate:
65 stream << "kOatBootImageOutOfDate";
66 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010067 case OatFileAssistant::kOatUpToDate:
68 stream << "kOatUpToDate";
69 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010070 default:
71 UNREACHABLE();
72 }
73
74 return stream;
75}
76
Richard Uhler66d874d2015-01-15 09:37:19 -080077OatFileAssistant::OatFileAssistant(const char* dex_location,
78 const InstructionSet isa,
Nicolas Geoffray29742602017-12-14 10:09:03 +000079 bool load_executable,
80 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070081 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000082 isa,
83 load_executable,
84 only_load_system_executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010085 /*vdex_fd=*/ -1,
86 /*oat_fd=*/ -1,
87 /*zip_fd=*/ -1) {}
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070088
89
90OatFileAssistant::OatFileAssistant(const char* dex_location,
91 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070092 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000093 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070094 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070095 int oat_fd,
96 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000097 : isa_(isa),
98 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +000099 only_load_system_executable_(only_load_system_executable),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700100 odex_(this, /*is_oat_location=*/ false),
101 oat_(this, /*is_oat_location=*/ true),
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000102 vdex_for_odex_(this, /*is_oat_location=*/ false),
103 vdex_for_oat_(this, /*is_oat_location=*/ true),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700104 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -0700105 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +0000106
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700107 if (zip_fd < 0) {
108 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
109 << " oat_fd=" << oat_fd;
110 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
111 << " vdex_fd=" << vdex_fd;;
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000112 CHECK(!UseFdToReadFiles());
113 } else {
114 CHECK(UseFdToReadFiles());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700115 }
116
Calin Juravle099f8d62017-09-05 19:04:04 -0700117 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700118
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100119 if (load_executable_ && isa != kRuntimeISA) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800120 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100121 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800122 load_executable_ = false;
123 }
124
Richard Uhler743bf362016-04-19 15:39:37 -0700125 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700126 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700127 std::string odex_file_name;
128 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100129 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000130 std::string vdex_file_name = GetVdexFilename(odex_file_name);
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000131 // We dup FDs as the odex_ will claim ownership.
132 vdex_for_odex_.Reset(vdex_file_name,
133 UseFdToReadFiles(),
134 DupCloexec(zip_fd),
135 DupCloexec(vdex_fd),
136 DupCloexec(oat_fd));
Richard Uhler743bf362016-04-19 15:39:37 -0700137 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700138 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
139 }
140
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700141 if (!UseFdToReadFiles()) {
142 // Get the oat filename.
143 std::string oat_file_name;
144 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100145 oat_.Reset(oat_file_name, /*use_fd=*/ false);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000146 std::string vdex_file_name = GetVdexFilename(oat_file_name);
147 vdex_for_oat_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700148 } else {
149 LOG(WARNING) << "Failed to determine oat file name for dex location "
150 << dex_location_ << ": " << error_msg;
151 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000152 }
153
154 // Check if the dex directory is writable.
155 // This will be needed in most uses of OatFileAssistant and so it's OK to
156 // compute it eagerly. (the only use which will not make use of it is
157 // OatFileAssistant::GetStatusDump())
158 size_t pos = dex_location_.rfind('/');
159 if (pos == std::string::npos) {
160 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700161 } else if (!UseFdToReadFiles()) {
162 // We cannot test for parent access when using file descriptors. That's ok
163 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000164 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700165 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000166 dex_parent_writable_ = true;
167 } else {
168 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700169 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800170 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800171}
172
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700173bool OatFileAssistant::UseFdToReadFiles() {
174 return zip_fd_ >= 0;
175}
176
Richard Uhler66d874d2015-01-15 09:37:19 -0800177bool OatFileAssistant::IsInBootClassPath() {
178 // Note: We check the current boot class path, regardless of the ISA
179 // specified by the user. This is okay, because the boot class path should
180 // be the same for all ISAs.
181 // TODO: Can we verify the boot class path is the same for all ISAs?
182 Runtime* runtime = Runtime::Current();
183 ClassLinker* class_linker = runtime->GetClassLinker();
184 const auto& boot_class_path = class_linker->GetBootClassPath();
185 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700186 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800187 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
188 return true;
189 }
190 }
191 return false;
192}
193
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700194int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
Stefania Halace9818dd2020-02-14 17:58:26 +0000195 ClassLoaderContext* class_loader_context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000196 const std::vector<int>& context_fds,
197 bool profile_changed,
198 bool downgrade) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000199 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700200 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
Stefania Halace9818dd2020-02-14 17:58:26 +0000201 class_loader_context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000202 context_fds,
203 profile_changed,
204 downgrade);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000205 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
206 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800207 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000208 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800209}
210
Richard Uhler01be6812016-05-17 10:34:52 -0700211bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000212 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700213}
214
Richard Uhler66d874d2015-01-15 09:37:19 -0800215std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000216 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800217}
218
Richard Uhler46cc64f2016-11-14 14:53:55 +0000219std::string OatFileAssistant::GetStatusDump() {
220 std::ostringstream status;
221 bool oat_file_exists = false;
222 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000223 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000224 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000225 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000226
Richard Uhler46cc64f2016-11-14 14:53:55 +0000227 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000228 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
229 const OatFile* file = oat_.GetFile();
230 if (file == nullptr) {
231 // If the file is null even though the status is not kOatCannotOpen, it
232 // means we must have a vdex file with no corresponding oat file. In
233 // this case we cannot determine the compilation filter. Indicate that
234 // we have only the vdex file instead.
235 status << "vdex-only";
236 } else {
237 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
238 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000239 }
240
Richard Uhler03bc6592016-11-22 09:42:04 +0000241 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000242 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000243 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000244
Richard Uhler46cc64f2016-11-14 14:53:55 +0000245 odex_file_exists = true;
246 if (oat_file_exists) {
247 status << "] ";
248 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000249 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
250 const OatFile* file = odex_.GetFile();
251 if (file == nullptr) {
252 status << "vdex-only";
253 } else {
254 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
255 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000256 }
257
258 if (!oat_file_exists && !odex_file_exists) {
259 status << "invalid[";
260 }
261
262 status << "]";
263 return status.str();
264}
265
Richard Uhler66d874d2015-01-15 09:37:19 -0800266std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700267 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800268 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700269 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
270 return dex_files;
271 } else {
272 return std::vector<std::unique_ptr<const DexFile>>();
273 }
274}
Richard Uhler66d874d2015-01-15 09:37:19 -0800275
Calin Juravle87e2cb62017-06-13 21:48:45 -0700276bool OatFileAssistant::LoadDexFiles(
277 const OatFile &oat_file,
278 const std::string& dex_location,
279 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000280 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800281 std::string error_msg;
Andreas Gampeb40d3612018-06-26 15:49:42 -0700282 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700283 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800284 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700285 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700286 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 }
288
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700289 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800290 if (dex_file.get() == nullptr) {
291 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700292 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800293 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700294 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800295
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000296 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700297 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700298 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000299 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700300 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000301 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800302 break;
303 }
304
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700305 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800306 if (dex_file.get() == nullptr) {
307 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700308 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800309 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700310 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800311 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700312 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800313}
314
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100315bool OatFileAssistant::HasDexFiles() {
316 ScopedTrace trace("HasDexFiles");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000317 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700318 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000319 // GetRequiredDexChecksums.
320 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700321 return has_original_dex_files_;
322}
323
Richard Uhler95abd042015-03-24 09:51:28 -0700324OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700325 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800326}
327
Richard Uhler95abd042015-03-24 09:51:28 -0700328OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700329 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800330}
331
Richard Uhler2f27abd2017-01-31 14:02:34 +0000332bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700333 ScopedTrace trace("DexChecksumUpToDate(vdex)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000334 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
335 if (required_dex_checksums == nullptr) {
336 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
337 return true;
338 }
339
Nicolas Geoffraya129d8a2021-03-18 22:23:04 +0000340 uint32_t number_of_dex_files = file.GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000341 if (required_dex_checksums->size() != number_of_dex_files) {
342 *error_msg = StringPrintf("expected %zu dex files but found %u",
343 required_dex_checksums->size(),
344 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000345 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800346 }
347
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000348 for (uint32_t i = 0; i < number_of_dex_files; i++) {
349 uint32_t expected_checksum = (*required_dex_checksums)[i];
350 uint32_t actual_checksum = file.GetLocationChecksum(i);
351 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700352 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000353 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
354 "Expected: %u, actual: %u",
355 dex.c_str(),
356 expected_checksum,
357 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000358 return false;
359 }
360 }
361
Richard Uhler2f27abd2017-01-31 14:02:34 +0000362 return true;
363}
364
365bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700366 ScopedTrace trace("DexChecksumUpToDate(oat)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000367 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
368 if (required_dex_checksums == nullptr) {
369 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
370 return true;
371 }
372
373 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
374 if (required_dex_checksums->size() != number_of_dex_files) {
375 *error_msg = StringPrintf("expected %zu dex files but found %u",
376 required_dex_checksums->size(),
377 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000378 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800379 }
380
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000381 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700382 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000383 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700384 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000385 if (oat_dex_file == nullptr) {
386 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
387 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800388 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000389 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
390 if (expected_checksum != actual_checksum) {
391 VLOG(oat) << "Dex checksum does not match for dex: " << dex
392 << ". Expected: " << expected_checksum
393 << ", Actual: " << actual_checksum;
394 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800395 }
396 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000397 return true;
398}
399
400OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
401 // Verify the ART_USE_READ_BARRIER state.
402 // TODO: Don't fully reject files due to read barrier state. If they contain
403 // compiled code and are otherwise okay, we should return something like
404 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
405 // barrier state doesn't matter.
406 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
407 constexpr bool kRuntimeIsCC = kUseReadBarrier;
408 if (is_cc != kRuntimeIsCC) {
409 return kOatCannotOpen;
410 }
411
412 // Verify the dex checksum.
413 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000414 VdexFile* vdex = file.GetVdexFile();
415 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
416 LOG(ERROR) << error_msg;
417 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000418 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800419
Andreas Gampe29d38e72016-03-23 15:31:51 +0000420 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000421
Richard Uhler66d874d2015-01-15 09:37:19 -0800422 // Verify the image checksum
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000423 if (file.IsBackedByVdexOnly()) {
424 VLOG(oat) << "Image checksum test skipped for vdex file " << file.GetLocation();
425 } else if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000426 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000427 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000428 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000429 }
430 } else {
431 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800432 }
433
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000434 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
435 DCHECK(required_dex_checksums_attempted_);
436 if (only_load_system_executable_ &&
437 !LocationIsOnSystem(file.GetLocation().c_str()) &&
438 file.ContainsDexCode() &&
439 zip_file_only_contains_uncompressed_dex_) {
440 LOG(ERROR) << "Not loading "
441 << dex_location_
442 << ": oat file has dex code, but APK has uncompressed dex code";
443 return kOatDexOutOfDate;
444 }
445
Richard Uhlere8109f72016-04-18 10:40:50 -0700446 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800447}
448
David Brazdil35a3f6a2019-03-04 15:59:06 +0000449bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
450 InstructionSet isa,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000451 /* out */ std::string* dex_location,
452 /* out */ std::string* vdex_filename) {
453 uint32_t checksum = adler32(0L, Z_NULL, 0);
454 for (const DexFile::Header* header : headers) {
455 checksum = adler32_combine(checksum,
456 header->checksum_,
457 header->file_size_ - DexFile::kNumNonChecksumBytes);
458 }
David Brazdil35a3f6a2019-03-04 15:59:06 +0000459
460 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
461 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
462 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
463 return false;
464 }
465 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
466
467 std::string odex_filename;
468 std::string error_msg;
469 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
470 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
471 return false;
472 }
473
474 *vdex_filename = GetVdexFilename(odex_filename);
475 return true;
476}
477
478bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
479 DCHECK(basename.find('/') == std::string::npos);
480 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
481 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
482 !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) ||
483 !android::base::EndsWith(basename, kVdexExtension)) {
484 return false;
485 }
486 // Check that all characters between the prefix and extension are decimal digits.
487 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
488 if (!std::isdigit(basename[i])) {
489 return false;
490 }
491 }
492 return true;
493}
494
Orion Hodsonb6f88572021-02-10 13:59:18 +0000495bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
496 InstructionSet isa,
497 std::string* odex_filename,
498 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000499 CHECK(odex_filename != nullptr);
500 CHECK(error_msg != nullptr);
501
502 // The odex file name is formed by replacing the dex_location extension with
503 // .odex and inserting an oat/<isa> directory. For example:
504 // location = /foo/bar/baz.jar
505 // odex_location = /foo/bar/oat/<isa>/baz.odex
506
507 // Find the directory portion of the dex location and add the oat/<isa>
508 // directory.
509 size_t pos = location.rfind('/');
510 if (pos == std::string::npos) {
511 *error_msg = "Dex location " + location + " has no directory.";
512 return false;
513 }
514 std::string dir = location.substr(0, pos+1);
515 // Add the oat directory.
516 dir += "oat";
Orion Hodsonb6f88572021-02-10 13:59:18 +0000517
Calin Juravle357c66d2017-05-04 01:57:17 +0000518 // Add the isa directory
519 dir += "/" + std::string(GetInstructionSetString(isa));
Calin Juravle357c66d2017-05-04 01:57:17 +0000520
521 // Get the base part of the file without the extension.
522 std::string file = location.substr(pos+1);
523 pos = file.rfind('.');
524 if (pos == std::string::npos) {
525 *error_msg = "Dex location " + location + " has no extension.";
526 return false;
527 }
528 std::string base = file.substr(0, pos);
529
530 *odex_filename = dir + "/" + base + ".odex";
531 return true;
532}
533
Richard Uhlerb81881d2016-04-19 13:08:04 -0700534bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
535 InstructionSet isa,
536 std::string* oat_filename,
537 std::string* error_msg) {
538 CHECK(oat_filename != nullptr);
539 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800540
Orion Hodsonb6f88572021-02-10 13:59:18 +0000541 // Check if `location` could have an oat file in the ART APEX data directory. If so, and the
542 // file exists, use it.
Orion Hodsone5276da2021-02-27 18:04:41 +0000543 const std::string apex_data_file = GetApexDataOdexFilename(location, isa);
544 if (!apex_data_file.empty()) {
545 if (OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) {
546 *oat_filename = apex_data_file;
547 return true;
548 } else if (errno != ENOENT) {
549 PLOG(ERROR) << "Could not check odex file " << apex_data_file;
550 }
Orion Hodsonb6f88572021-02-10 13:59:18 +0000551 }
552
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700553 // If ANDROID_DATA is not set, return false instead of aborting.
554 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100555 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700556 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
557 return false;
558 }
559
Orion Hodsonfa81f712021-01-13 12:27:21 +0000560 std::string dalvik_cache;
561 bool have_android_data = false;
562 bool dalvik_cache_exists = false;
563 bool is_global_cache = false;
564 GetDalvikCache(GetInstructionSetString(isa),
565 /*create_if_absent=*/ true,
566 &dalvik_cache,
567 &have_android_data,
568 &dalvik_cache_exists,
569 &is_global_cache);
570 if (!dalvik_cache_exists) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700571 *error_msg = "Dalvik cache directory does not exist";
572 return false;
573 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700574
575 // TODO: The oat file assistant should be the definitive place for
576 // determining the oat file name from the dex location, not
577 // GetDalvikCacheFilename.
Orion Hodsonfa81f712021-01-13 12:27:21 +0000578 return GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800579}
580
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000581const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
582 if (!required_dex_checksums_attempted_) {
583 required_dex_checksums_attempted_ = true;
584 required_dex_checksums_found_ = false;
585 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800586 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800587 const ArtDexFileLoader dex_file_loader;
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800588 std::vector<std::string> dex_locations_ignored;
David Sehr013fd802018-01-11 22:55:24 -0800589 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
590 &cached_required_dex_checksums_,
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800591 &dex_locations_ignored,
David Sehr013fd802018-01-11 22:55:24 -0800592 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000593 zip_fd_,
594 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000595 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700596 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800597 } else {
Calin Juravle5ff23932020-12-11 18:26:14 -0800598 // The only valid case here is for APKs without dex files.
599 required_dex_checksums_found_ = false;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700600 has_original_dex_files_ = false;
Calin Juravle5ff23932020-12-11 18:26:14 -0800601 VLOG(oat) << "Could not get required checksum: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800602 }
603 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000604 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800605}
606
Vladimir Markobcd99be2019-03-22 16:21:31 +0000607bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100608 // Get the checksums and the BCP from the oat file.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000609 const char* oat_boot_class_path_checksums =
610 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100611 const char* oat_boot_class_path =
612 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
613 if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) {
Vladimir Markof3d88a82018-12-21 16:38:47 +0000614 return false;
615 }
Vladimir Marko436c6f52019-07-25 14:50:14 +0100616 std::string_view oat_boot_class_path_checksums_view(oat_boot_class_path_checksums);
617 std::string_view oat_boot_class_path_view(oat_boot_class_path);
618 if (oat_boot_class_path_view == cached_boot_class_path_ &&
619 oat_boot_class_path_checksums_view == cached_boot_class_path_checksums_) {
620 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800621 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000622
Vladimir Marko436c6f52019-07-25 14:50:14 +0100623 Runtime* runtime = Runtime::Current();
624 std::string error_msg;
625 bool result = gc::space::ImageSpace::VerifyBootClassPathChecksums(
626 oat_boot_class_path_checksums_view,
627 oat_boot_class_path_view,
628 runtime->GetImageLocation(),
629 ArrayRef<const std::string>(runtime->GetBootClassPathLocations()),
630 ArrayRef<const std::string>(runtime->GetBootClassPath()),
631 isa_,
Vladimir Marko436c6f52019-07-25 14:50:14 +0100632 &error_msg);
633 if (!result) {
634 VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation()
635 << " error: " << error_msg;
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100636 return false;
Vladimir Marko436c6f52019-07-25 14:50:14 +0100637 }
638
639 // This checksum has been validated, so save it.
640 cached_boot_class_path_ = oat_boot_class_path_view;
641 cached_boot_class_path_checksums_ = oat_boot_class_path_checksums_view;
642 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800643}
644
Richard Uhler88bc6732016-11-14 14:38:03 +0000645OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700646 ScopedTrace trace("GetBestInfo");
Calin Juravle357c66d2017-05-04 01:57:17 +0000647 // TODO(calin): Document the side effects of class loading when
648 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700649 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000650 // If the parent of the dex file is writable it means that we can
651 // create the odex file. In this case we unconditionally pick the odex
652 // as the best oat file. This corresponds to the regular use case when
653 // apps gets installed or when they load private, secondary dex file.
654 // For apps on the system partition the odex location will not be
655 // writable and thus the oat location might be more up to date.
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000656
657 // If the odex is not useable, and we have a useable vdex, return the vdex
658 // instead.
659 if (!odex_.IsUseable() && vdex_for_odex_.IsUseable()) {
660 return vdex_for_odex_;
661 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000662 return odex_;
663 }
664
665 // We cannot write to the odex location. This must be a system app.
666
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000667 // If the oat location is useable take it.
Calin Juravle357c66d2017-05-04 01:57:17 +0000668 if (oat_.IsUseable()) {
669 return oat_;
670 }
671
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000672 // The oat file is not useable but the odex file might be up to date.
Calin Juravle357c66d2017-05-04 01:57:17 +0000673 // This is an indication that we are dealing with an up to date prebuilt
674 // (that doesn't need relocation).
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000675 if (odex_.IsUseable()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000676 return odex_;
677 }
678
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000679 // Look for a useable vdex file.
680 if (vdex_for_oat_.IsUseable()) {
681 return vdex_for_oat_;
682 }
683 if (vdex_for_odex_.IsUseable()) {
684 return vdex_for_odex_;
685 }
686
Calin Juravle357c66d2017-05-04 01:57:17 +0000687 // We got into the worst situation here:
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000688 // - the oat location is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000689 // - the prebuild odex location is not up to date
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000690 // - the vdex-only file is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000691 // - and we don't have the original dex file anymore (stripped).
692 // Pick the odex if it exists, or the oat if not.
693 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000694}
695
Andreas Gampea463b6a2016-08-12 21:53:32 -0700696std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800697 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100698 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800699 if (art_file.empty()) {
700 return nullptr;
701 }
702 std::string error_msg;
703 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700704 std::unique_ptr<gc::space::ImageSpace> ret =
705 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800706 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800707 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
708 }
709 return ret;
710}
711
Richard Uhler88bc6732016-11-14 14:38:03 +0000712OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
713 bool is_oat_location)
714 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700715{}
716
Richard Uhler88bc6732016-11-14 14:38:03 +0000717bool OatFileAssistant::OatFileInfo::IsOatLocation() {
718 return is_oat_location_;
719}
720
Richard Uhler743bf362016-04-19 15:39:37 -0700721const std::string* OatFileAssistant::OatFileInfo::Filename() {
722 return filename_provided_ ? &filename_ : nullptr;
723}
724
Richard Uhler03bc6592016-11-22 09:42:04 +0000725bool OatFileAssistant::OatFileInfo::IsUseable() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700726 ScopedTrace trace("IsUseable");
Richard Uhler03bc6592016-11-22 09:42:04 +0000727 switch (Status()) {
728 case kOatCannotOpen:
729 case kOatDexOutOfDate:
730 case kOatBootImageOutOfDate: return false;
731
Richard Uhler03bc6592016-11-22 09:42:04 +0000732 case kOatUpToDate: return true;
733 }
734 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700735}
736
737OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700738 ScopedTrace trace("Status");
Richard Uhler743bf362016-04-19 15:39:37 -0700739 if (!status_attempted_) {
740 status_attempted_ = true;
741 const OatFile* file = GetFile();
742 if (file == nullptr) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000743 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700744 } else {
745 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700746 VLOG(oat) << file->GetLocation() << " is " << status_
747 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700748 }
749 }
750 return status_;
751}
752
Richard Uhler70a84262016-11-08 16:51:51 +0000753OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700754 CompilerFilter::Filter target,
Stefania Halace9818dd2020-02-14 17:58:26 +0000755 ClassLoaderContext* context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000756 const std::vector<int>& context_fds,
757 bool profile_changed,
758 bool downgrade) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700759
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700760 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
David Brazdil89821862019-03-19 13:57:43 +0000761 bool class_loader_context_okay = ClassLoaderContextIsOkay(context, context_fds);
Richard Uhler70a84262016-11-08 16:51:51 +0000762
Calin Juravle44e5efa2017-09-12 00:54:26 -0700763 // Only check the filter and relocation if the class loader context is ok.
764 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
765 if (class_loader_context_okay) {
766 if (filter_okay && Status() == kOatUpToDate) {
767 // The oat file is in good shape as is.
768 return kNoDexOptNeeded;
769 }
Richard Uhler70a84262016-11-08 16:51:51 +0000770
Calin Juravle44e5efa2017-09-12 00:54:26 -0700771 if (IsUseable()) {
772 return kDex2OatForFilter;
773 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100774
Calin Juravle44e5efa2017-09-12 00:54:26 -0700775 if (Status() == kOatBootImageOutOfDate) {
776 return kDex2OatForBootImage;
777 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100778 }
779
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100780 if (oat_file_assistant_->HasDexFiles()) {
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100781 return kDex2OatFromScratch;
782 } else {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100783 // No dex file, there is nothing we need to do.
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100784 return kNoDexOptNeeded;
785 }
Richard Uhler70a84262016-11-08 16:51:51 +0000786}
787
Richard Uhler743bf362016-04-19 15:39:37 -0700788const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
789 CHECK(!file_released_) << "GetFile called after oat file released.";
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000790 if (load_attempted_) {
791 return file_.get();
792 }
793 load_attempted_ = true;
794 if (!filename_provided_) {
795 return nullptr;
796 }
797
798 std::string error_msg;
799 bool executable = oat_file_assistant_->load_executable_;
800 if (android::base::EndsWith(filename_, kVdexExtension)) {
801 executable = false;
802 // Check to see if there is a vdex file we can make use of.
803 std::unique_ptr<VdexFile> vdex;
804 if (use_fd_) {
805 if (vdex_fd_ >= 0) {
806 struct stat s;
807 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
808 if (rc == -1) {
809 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
810 } else {
811 vdex = VdexFile::Open(vdex_fd_,
812 s.st_size,
813 filename_,
814 /*writable=*/ false,
815 /*low_4gb=*/ false,
816 /*unquicken=*/ false,
817 &error_msg);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700818 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000819 }
820 } else {
821 vdex = VdexFile::Open(filename_,
822 /*writable=*/ false,
823 /*low_4gb=*/ false,
824 /*unquicken=*/ false,
825 &error_msg);
826 }
827 if (vdex == nullptr) {
828 VLOG(oat) << "unable to open vdex file " << filename_ << ": " << error_msg;
829 } else {
830 file_.reset(OatFile::OpenFromVdex(zip_fd_,
831 std::move(vdex),
832 oat_file_assistant_->dex_location_,
833 &error_msg));
834 }
835 } else {
836 if (executable && oat_file_assistant_->only_load_system_executable_) {
837 executable = LocationIsOnSystem(filename_.c_str());
838 }
839 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
840 if (use_fd_) {
841 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
842 ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_,
843 /*size=*/ 1u);
844 file_.reset(OatFile::Open(zip_fd_,
845 vdex_fd_,
846 oat_fd_,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700847 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000848 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100849 /*low_4gb=*/ false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000850 dex_locations,
851 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700852 &error_msg));
853 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000854 } else {
855 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
856 filename_.c_str(),
857 filename_.c_str(),
858 executable,
859 /*low_4gb=*/ false,
860 oat_file_assistant_->dex_location_,
861 &error_msg));
Richard Uhler743bf362016-04-19 15:39:37 -0700862 }
863 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000864 if (file_.get() == nullptr) {
865 VLOG(oat) << "OatFileAssistant test for existing oat file "
866 << filename_
867 << ": " << error_msg;
868 } else {
869 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
870 }
Richard Uhler743bf362016-04-19 15:39:37 -0700871 return file_.get();
872}
873
874bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700875 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700876 const OatFile* file = GetFile();
877 if (file == nullptr) {
878 return false;
879 }
880
881 CompilerFilter::Filter current = file->GetCompilerFilter();
882 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
883 VLOG(oat) << "Compiler filter not okay because Profile changed";
884 return false;
885 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700886 return downgrade ? !CompilerFilter::IsBetter(current, target) :
887 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700888}
889
David Brazdil89821862019-03-19 13:57:43 +0000890bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context,
891 const std::vector<int>& context_fds) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700892 const OatFile* file = GetFile();
893 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -0700894 // No oat file means we have nothing to verify.
895 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700896 }
897
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000898 if (file->IsBackedByVdexOnly()) {
899 // Only a vdex file, we don't depend on the class loader context.
900 return true;
901 }
902
Calin Juravleaf322422020-02-11 13:45:53 -0800903 if (!CompilerFilter::IsVerificationEnabled(file->GetCompilerFilter())) {
904 // If verification is not enabled we don't need to verify the class loader context and we
905 // assume it's ok.
906 return true;
907 }
908
Calin Juravle0a5cad32020-02-14 20:29:26 +0000909
Calin Juravleaf322422020-02-11 13:45:53 -0800910 if (context == nullptr) {
Calin Juravle0a5cad32020-02-14 20:29:26 +0000911 // TODO(calin): stop using null for the unkown contexts.
912 // b/148494302 introduces runtime encoding for unknown context which will make this possible.
913 VLOG(oat) << "ClassLoaderContext check failed: uknown(null) context";
914 return false;
Calin Juravleaf322422020-02-11 13:45:53 -0800915 }
916
Calin Juravle20c46442017-09-12 00:54:26 -0700917 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -0700918 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -0700919 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -0700920 : "";
921
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800922 if (!context->OpenDexFiles(classpath_dir, context_fds, /*only_read_checksums*/ true)) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700923 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
924 return false;
925 }
926
Mathieu Chartieradc90862018-05-11 13:03:06 -0700927 const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
928 ClassLoaderContext::VerificationResult::kMismatch;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700929 if (!result) {
930 VLOG(oat) << "ClassLoaderContext check failed. Context was "
931 << file->GetClassLoaderContext()
932 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
933 }
934 return result;
935}
936
Richard Uhler743bf362016-04-19 15:39:37 -0700937bool OatFileAssistant::OatFileInfo::IsExecutable() {
938 const OatFile* file = GetFile();
939 return (file != nullptr && file->IsExecutable());
940}
941
Richard Uhler743bf362016-04-19 15:39:37 -0700942void OatFileAssistant::OatFileInfo::Reset() {
943 load_attempted_ = false;
944 file_.reset();
945 status_attempted_ = false;
946}
947
Nicolas Geoffray30025092018-04-19 14:43:29 +0100948void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
949 bool use_fd,
950 int zip_fd,
951 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700952 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700953 filename_provided_ = true;
954 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700955 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100956 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700957 vdex_fd_ = vdex_fd;
958 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700959 Reset();
960}
961
962std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
963 file_released_ = true;
964 return std::move(file_);
965}
966
Richard Uhler70a84262016-11-08 16:51:51 +0000967std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700968 ScopedTrace trace("ReleaseFileForUse");
Richard Uhler3e580bc2016-11-08 16:23:07 +0000969 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000970 return ReleaseFile();
971 }
972
Richard Uhler70a84262016-11-08 16:51:51 +0000973 return std::unique_ptr<OatFile>();
974}
Calin Juravle5f9a8012018-02-12 20:27:46 -0800975
976// TODO(calin): we could provide a more refined status here
977// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
978// track more experiments but adds extra complexity.
979void OatFileAssistant::GetOptimizationStatus(
980 const std::string& filename,
981 InstructionSet isa,
982 std::string* out_compilation_filter,
983 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700984 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
985 // non-executable and check the status manually.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100986 OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false);
Calin Juravle98071152021-01-27 18:41:58 -0800987 std::string out_odex_location; // unused
988 std::string out_odex_status; // unused
989 oat_file_assistant.GetOptimizationStatus(
990 &out_odex_location,
991 out_compilation_filter,
992 out_compilation_reason,
993 &out_odex_status);
994}
995
996void OatFileAssistant::GetOptimizationStatus(
997 std::string* out_odex_location,
998 std::string* out_compilation_filter,
999 std::string* out_compilation_reason,
1000 std::string* out_odex_status) {
1001 OatFileInfo& oat_file_info = GetBestInfo();
1002 const OatFile* oat_file = GetBestInfo().GetFile();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001003
1004 if (oat_file == nullptr) {
Calin Juravle98071152021-01-27 18:41:58 -08001005 *out_odex_location = "error";
Calin Juravle5f9a8012018-02-12 20:27:46 -08001006 *out_compilation_filter = "run-from-apk";
1007 *out_compilation_reason = "unknown";
Calin Juravle98071152021-01-27 18:41:58 -08001008 // This mostly happens when we cannot open the oat file.
1009 // Note that it's different than kOatCannotOpen.
1010 // TODO: The design of getting the BestInfo is not ideal,
1011 // as it's not very clear what's the difference between
1012 // a nullptr and kOatcannotOpen. The logic should be revised
1013 // and improved.
1014 *out_odex_status = "io-error-no-oat";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001015 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001016 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001017
Calin Juravle98071152021-01-27 18:41:58 -08001018 *out_odex_location = oat_file->GetLocation();
1019 OatStatus status = oat_file_info.Status();
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001020 const char* reason = oat_file->GetCompilationReason();
1021 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1022 switch (status) {
Calin Juravle98071152021-01-27 18:41:58 -08001023 case kOatUpToDate:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001024 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
Calin Juravle98071152021-01-27 18:41:58 -08001025 *out_odex_status = "up-to-date";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001026 return;
1027
1028 case kOatCannotOpen: // This should never happen, but be robust.
1029 *out_compilation_filter = "error";
1030 *out_compilation_reason = "error";
Calin Juravle98071152021-01-27 18:41:58 -08001031 // This mostly happens when we cannot open the vdex file,
1032 // or the file is corrupt.
1033 *out_odex_status = "io-error-or-corruption";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001034 return;
1035
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001036 case kOatBootImageOutOfDate:
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +01001037 *out_compilation_filter = "run-from-apk-fallback";
Calin Juravle98071152021-01-27 18:41:58 -08001038 *out_odex_status = "boot-image-more-recent";
1039 return;
1040
1041 case kOatDexOutOfDate:
1042 *out_compilation_filter = "run-from-apk-fallback";
1043 *out_odex_status = "apk-more-recent";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001044 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001045 }
1046 LOG(FATAL) << "Unreachable";
1047 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001048}
1049
Richard Uhler66d874d2015-01-15 09:37:19 -08001050} // namespace art