blob: 4b0783fb037dc2b227a75c761b1e54d16aca56de [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;;
112 }
113
Calin Juravle099f8d62017-09-05 19:04:04 -0700114 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700115
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100116 if (load_executable_ && isa != kRuntimeISA) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800117 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100118 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800119 load_executable_ = false;
120 }
121
Richard Uhler743bf362016-04-19 15:39:37 -0700122 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700123 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700124 std::string odex_file_name;
125 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100126 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000127 std::string vdex_file_name = GetVdexFilename(odex_file_name);
128 vdex_for_odex_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700129 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700130 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
131 }
132
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700133 if (!UseFdToReadFiles()) {
134 // Get the oat filename.
135 std::string oat_file_name;
136 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100137 oat_.Reset(oat_file_name, /*use_fd=*/ false);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000138 std::string vdex_file_name = GetVdexFilename(oat_file_name);
139 vdex_for_oat_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700140 } else {
141 LOG(WARNING) << "Failed to determine oat file name for dex location "
142 << dex_location_ << ": " << error_msg;
143 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000144 }
145
146 // Check if the dex directory is writable.
147 // This will be needed in most uses of OatFileAssistant and so it's OK to
148 // compute it eagerly. (the only use which will not make use of it is
149 // OatFileAssistant::GetStatusDump())
150 size_t pos = dex_location_.rfind('/');
151 if (pos == std::string::npos) {
152 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700153 } else if (!UseFdToReadFiles()) {
154 // We cannot test for parent access when using file descriptors. That's ok
155 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000156 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700157 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000158 dex_parent_writable_ = true;
159 } else {
160 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700161 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800162 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800163}
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,
Stefania Halace9818dd2020-02-14 17:58:26 +0000187 ClassLoaderContext* class_loader_context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000188 const std::vector<int>& context_fds,
189 bool profile_changed,
190 bool downgrade) {
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,
Stefania Halace9818dd2020-02-14 17:58:26 +0000193 class_loader_context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000194 context_fds,
195 profile_changed,
196 downgrade);
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
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100307bool OatFileAssistant::HasDexFiles() {
308 ScopedTrace trace("HasDexFiles");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000309 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700310 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000311 // GetRequiredDexChecksums.
312 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700313 return has_original_dex_files_;
314}
315
Richard Uhler95abd042015-03-24 09:51:28 -0700316OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700317 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800318}
319
Richard Uhler95abd042015-03-24 09:51:28 -0700320OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700321 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800322}
323
Richard Uhler2f27abd2017-01-31 14:02:34 +0000324bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700325 ScopedTrace trace("DexChecksumUpToDate(vdex)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000326 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
327 if (required_dex_checksums == nullptr) {
328 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
329 return true;
330 }
331
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000332 uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000333 if (required_dex_checksums->size() != number_of_dex_files) {
334 *error_msg = StringPrintf("expected %zu dex files but found %u",
335 required_dex_checksums->size(),
336 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000337 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800338 }
339
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000340 for (uint32_t i = 0; i < number_of_dex_files; i++) {
341 uint32_t expected_checksum = (*required_dex_checksums)[i];
342 uint32_t actual_checksum = file.GetLocationChecksum(i);
343 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700344 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000345 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
346 "Expected: %u, actual: %u",
347 dex.c_str(),
348 expected_checksum,
349 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000350 return false;
351 }
352 }
353
Richard Uhler2f27abd2017-01-31 14:02:34 +0000354 return true;
355}
356
357bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700358 ScopedTrace trace("DexChecksumUpToDate(oat)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000359 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
360 if (required_dex_checksums == nullptr) {
361 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
362 return true;
363 }
364
365 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
366 if (required_dex_checksums->size() != number_of_dex_files) {
367 *error_msg = StringPrintf("expected %zu dex files but found %u",
368 required_dex_checksums->size(),
369 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000370 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800371 }
372
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000373 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700374 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000375 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700376 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000377 if (oat_dex_file == nullptr) {
378 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
379 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800380 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000381 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
382 if (expected_checksum != actual_checksum) {
383 VLOG(oat) << "Dex checksum does not match for dex: " << dex
384 << ". Expected: " << expected_checksum
385 << ", Actual: " << actual_checksum;
386 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800387 }
388 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000389 return true;
390}
391
392OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
393 // Verify the ART_USE_READ_BARRIER state.
394 // TODO: Don't fully reject files due to read barrier state. If they contain
395 // compiled code and are otherwise okay, we should return something like
396 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
397 // barrier state doesn't matter.
398 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
399 constexpr bool kRuntimeIsCC = kUseReadBarrier;
400 if (is_cc != kRuntimeIsCC) {
401 return kOatCannotOpen;
402 }
403
404 // Verify the dex checksum.
405 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000406 VdexFile* vdex = file.GetVdexFile();
407 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
408 LOG(ERROR) << error_msg;
409 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000410 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800411
Andreas Gampe29d38e72016-03-23 15:31:51 +0000412 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000413
Richard Uhler66d874d2015-01-15 09:37:19 -0800414 // Verify the image checksum
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000415 if (file.IsBackedByVdexOnly()) {
416 VLOG(oat) << "Image checksum test skipped for vdex file " << file.GetLocation();
417 } else if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000418 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000419 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000420 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000421 }
422 } else {
423 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800424 }
425
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000426 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
427 DCHECK(required_dex_checksums_attempted_);
428 if (only_load_system_executable_ &&
429 !LocationIsOnSystem(file.GetLocation().c_str()) &&
430 file.ContainsDexCode() &&
431 zip_file_only_contains_uncompressed_dex_) {
432 LOG(ERROR) << "Not loading "
433 << dex_location_
434 << ": oat file has dex code, but APK has uncompressed dex code";
435 return kOatDexOutOfDate;
436 }
437
Richard Uhlere8109f72016-04-18 10:40:50 -0700438 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800439}
440
David Brazdil35a3f6a2019-03-04 15:59:06 +0000441bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
442 InstructionSet isa,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000443 /* out */ std::string* dex_location,
444 /* out */ std::string* vdex_filename) {
445 uint32_t checksum = adler32(0L, Z_NULL, 0);
446 for (const DexFile::Header* header : headers) {
447 checksum = adler32_combine(checksum,
448 header->checksum_,
449 header->file_size_ - DexFile::kNumNonChecksumBytes);
450 }
David Brazdil35a3f6a2019-03-04 15:59:06 +0000451
452 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
453 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
454 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
455 return false;
456 }
457 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
458
459 std::string odex_filename;
460 std::string error_msg;
461 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
462 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
463 return false;
464 }
465
466 *vdex_filename = GetVdexFilename(odex_filename);
467 return true;
468}
469
470bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
471 DCHECK(basename.find('/') == std::string::npos);
472 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
473 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
474 !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) ||
475 !android::base::EndsWith(basename, kVdexExtension)) {
476 return false;
477 }
478 // Check that all characters between the prefix and extension are decimal digits.
479 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
480 if (!std::isdigit(basename[i])) {
481 return false;
482 }
483 }
484 return true;
485}
486
Orion Hodsonb6f88572021-02-10 13:59:18 +0000487bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
488 InstructionSet isa,
489 std::string* odex_filename,
490 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000491 CHECK(odex_filename != nullptr);
492 CHECK(error_msg != nullptr);
493
494 // The odex file name is formed by replacing the dex_location extension with
495 // .odex and inserting an oat/<isa> directory. For example:
496 // location = /foo/bar/baz.jar
497 // odex_location = /foo/bar/oat/<isa>/baz.odex
498
499 // Find the directory portion of the dex location and add the oat/<isa>
500 // directory.
501 size_t pos = location.rfind('/');
502 if (pos == std::string::npos) {
503 *error_msg = "Dex location " + location + " has no directory.";
504 return false;
505 }
506 std::string dir = location.substr(0, pos+1);
507 // Add the oat directory.
508 dir += "oat";
Orion Hodsonb6f88572021-02-10 13:59:18 +0000509
Calin Juravle357c66d2017-05-04 01:57:17 +0000510 // Add the isa directory
511 dir += "/" + std::string(GetInstructionSetString(isa));
Calin Juravle357c66d2017-05-04 01:57:17 +0000512
513 // Get the base part of the file without the extension.
514 std::string file = location.substr(pos+1);
515 pos = file.rfind('.');
516 if (pos == std::string::npos) {
517 *error_msg = "Dex location " + location + " has no extension.";
518 return false;
519 }
520 std::string base = file.substr(0, pos);
521
522 *odex_filename = dir + "/" + base + ".odex";
523 return true;
524}
525
Richard Uhlerb81881d2016-04-19 13:08:04 -0700526bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
527 InstructionSet isa,
528 std::string* oat_filename,
529 std::string* error_msg) {
530 CHECK(oat_filename != nullptr);
531 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800532
Orion Hodsonb6f88572021-02-10 13:59:18 +0000533 // Check if `location` could have an oat file in the ART APEX data directory. If so, and the
534 // file exists, use it.
535 std::string apex_data_file = GetApexDataOdexFilename(location, isa);
536 if (!apex_data_file.empty() && OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) {
537 *oat_filename = apex_data_file;
538 return true;
539 }
540
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700541 // If ANDROID_DATA is not set, return false instead of aborting.
542 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100543 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700544 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
545 return false;
546 }
547
Orion Hodsonfa81f712021-01-13 12:27:21 +0000548 std::string dalvik_cache;
549 bool have_android_data = false;
550 bool dalvik_cache_exists = false;
551 bool is_global_cache = false;
552 GetDalvikCache(GetInstructionSetString(isa),
553 /*create_if_absent=*/ true,
554 &dalvik_cache,
555 &have_android_data,
556 &dalvik_cache_exists,
557 &is_global_cache);
558 if (!dalvik_cache_exists) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700559 *error_msg = "Dalvik cache directory does not exist";
560 return false;
561 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700562
563 // TODO: The oat file assistant should be the definitive place for
564 // determining the oat file name from the dex location, not
565 // GetDalvikCacheFilename.
Orion Hodsonfa81f712021-01-13 12:27:21 +0000566 return GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800567}
568
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000569const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
570 if (!required_dex_checksums_attempted_) {
571 required_dex_checksums_attempted_ = true;
572 required_dex_checksums_found_ = false;
573 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800574 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800575 const ArtDexFileLoader dex_file_loader;
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800576 std::vector<std::string> dex_locations_ignored;
David Sehr013fd802018-01-11 22:55:24 -0800577 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
578 &cached_required_dex_checksums_,
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800579 &dex_locations_ignored,
David Sehr013fd802018-01-11 22:55:24 -0800580 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000581 zip_fd_,
582 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000583 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700584 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800585 } else {
Calin Juravle5ff23932020-12-11 18:26:14 -0800586 // The only valid case here is for APKs without dex files.
587 required_dex_checksums_found_ = false;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700588 has_original_dex_files_ = false;
Calin Juravle5ff23932020-12-11 18:26:14 -0800589 VLOG(oat) << "Could not get required checksum: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800590 }
591 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000592 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800593}
594
Vladimir Markobcd99be2019-03-22 16:21:31 +0000595bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100596 // Get the checksums and the BCP from the oat file.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000597 const char* oat_boot_class_path_checksums =
598 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100599 const char* oat_boot_class_path =
600 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
601 if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) {
Vladimir Markof3d88a82018-12-21 16:38:47 +0000602 return false;
603 }
Vladimir Marko436c6f52019-07-25 14:50:14 +0100604 std::string_view oat_boot_class_path_checksums_view(oat_boot_class_path_checksums);
605 std::string_view oat_boot_class_path_view(oat_boot_class_path);
606 if (oat_boot_class_path_view == cached_boot_class_path_ &&
607 oat_boot_class_path_checksums_view == cached_boot_class_path_checksums_) {
608 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800609 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000610
Vladimir Marko436c6f52019-07-25 14:50:14 +0100611 Runtime* runtime = Runtime::Current();
612 std::string error_msg;
613 bool result = gc::space::ImageSpace::VerifyBootClassPathChecksums(
614 oat_boot_class_path_checksums_view,
615 oat_boot_class_path_view,
616 runtime->GetImageLocation(),
617 ArrayRef<const std::string>(runtime->GetBootClassPathLocations()),
618 ArrayRef<const std::string>(runtime->GetBootClassPath()),
619 isa_,
Vladimir Marko436c6f52019-07-25 14:50:14 +0100620 &error_msg);
621 if (!result) {
622 VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation()
623 << " error: " << error_msg;
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100624 return false;
Vladimir Marko436c6f52019-07-25 14:50:14 +0100625 }
626
627 // This checksum has been validated, so save it.
628 cached_boot_class_path_ = oat_boot_class_path_view;
629 cached_boot_class_path_checksums_ = oat_boot_class_path_checksums_view;
630 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800631}
632
Richard Uhler88bc6732016-11-14 14:38:03 +0000633OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700634 ScopedTrace trace("GetBestInfo");
Calin Juravle357c66d2017-05-04 01:57:17 +0000635 // TODO(calin): Document the side effects of class loading when
636 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700637 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000638 // If the parent of the dex file is writable it means that we can
639 // create the odex file. In this case we unconditionally pick the odex
640 // as the best oat file. This corresponds to the regular use case when
641 // apps gets installed or when they load private, secondary dex file.
642 // For apps on the system partition the odex location will not be
643 // writable and thus the oat location might be more up to date.
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000644
645 // If the odex is not useable, and we have a useable vdex, return the vdex
646 // instead.
647 if (!odex_.IsUseable() && vdex_for_odex_.IsUseable()) {
648 return vdex_for_odex_;
649 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000650 return odex_;
651 }
652
653 // We cannot write to the odex location. This must be a system app.
654
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000655 // If the oat location is useable take it.
Calin Juravle357c66d2017-05-04 01:57:17 +0000656 if (oat_.IsUseable()) {
657 return oat_;
658 }
659
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000660 // The oat file is not useable but the odex file might be up to date.
Calin Juravle357c66d2017-05-04 01:57:17 +0000661 // This is an indication that we are dealing with an up to date prebuilt
662 // (that doesn't need relocation).
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000663 if (odex_.IsUseable()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000664 return odex_;
665 }
666
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000667 // Look for a useable vdex file.
668 if (vdex_for_oat_.IsUseable()) {
669 return vdex_for_oat_;
670 }
671 if (vdex_for_odex_.IsUseable()) {
672 return vdex_for_odex_;
673 }
674
Calin Juravle357c66d2017-05-04 01:57:17 +0000675 // We got into the worst situation here:
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000676 // - the oat location is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000677 // - the prebuild odex location is not up to date
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000678 // - the vdex-only file is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000679 // - and we don't have the original dex file anymore (stripped).
680 // Pick the odex if it exists, or the oat if not.
681 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000682}
683
Andreas Gampea463b6a2016-08-12 21:53:32 -0700684std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800685 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100686 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800687 if (art_file.empty()) {
688 return nullptr;
689 }
690 std::string error_msg;
691 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700692 std::unique_ptr<gc::space::ImageSpace> ret =
693 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800694 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800695 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
696 }
697 return ret;
698}
699
Richard Uhler88bc6732016-11-14 14:38:03 +0000700OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
701 bool is_oat_location)
702 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700703{}
704
Richard Uhler88bc6732016-11-14 14:38:03 +0000705bool OatFileAssistant::OatFileInfo::IsOatLocation() {
706 return is_oat_location_;
707}
708
Richard Uhler743bf362016-04-19 15:39:37 -0700709const std::string* OatFileAssistant::OatFileInfo::Filename() {
710 return filename_provided_ ? &filename_ : nullptr;
711}
712
Richard Uhler03bc6592016-11-22 09:42:04 +0000713bool OatFileAssistant::OatFileInfo::IsUseable() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700714 ScopedTrace trace("IsUseable");
Richard Uhler03bc6592016-11-22 09:42:04 +0000715 switch (Status()) {
716 case kOatCannotOpen:
717 case kOatDexOutOfDate:
718 case kOatBootImageOutOfDate: return false;
719
Richard Uhler03bc6592016-11-22 09:42:04 +0000720 case kOatUpToDate: return true;
721 }
722 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700723}
724
725OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700726 ScopedTrace trace("Status");
Richard Uhler743bf362016-04-19 15:39:37 -0700727 if (!status_attempted_) {
728 status_attempted_ = true;
729 const OatFile* file = GetFile();
730 if (file == nullptr) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000731 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700732 } else {
733 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700734 VLOG(oat) << file->GetLocation() << " is " << status_
735 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700736 }
737 }
738 return status_;
739}
740
Richard Uhler70a84262016-11-08 16:51:51 +0000741OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700742 CompilerFilter::Filter target,
Stefania Halace9818dd2020-02-14 17:58:26 +0000743 ClassLoaderContext* context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000744 const std::vector<int>& context_fds,
745 bool profile_changed,
746 bool downgrade) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700747
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700748 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
David Brazdil89821862019-03-19 13:57:43 +0000749 bool class_loader_context_okay = ClassLoaderContextIsOkay(context, context_fds);
Richard Uhler70a84262016-11-08 16:51:51 +0000750
Calin Juravle44e5efa2017-09-12 00:54:26 -0700751 // Only check the filter and relocation if the class loader context is ok.
752 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
753 if (class_loader_context_okay) {
754 if (filter_okay && Status() == kOatUpToDate) {
755 // The oat file is in good shape as is.
756 return kNoDexOptNeeded;
757 }
Richard Uhler70a84262016-11-08 16:51:51 +0000758
Calin Juravle44e5efa2017-09-12 00:54:26 -0700759 if (IsUseable()) {
760 return kDex2OatForFilter;
761 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100762
Calin Juravle44e5efa2017-09-12 00:54:26 -0700763 if (Status() == kOatBootImageOutOfDate) {
764 return kDex2OatForBootImage;
765 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100766 }
767
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100768 if (oat_file_assistant_->HasDexFiles()) {
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100769 return kDex2OatFromScratch;
770 } else {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100771 // No dex file, there is nothing we need to do.
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100772 return kNoDexOptNeeded;
773 }
Richard Uhler70a84262016-11-08 16:51:51 +0000774}
775
Richard Uhler743bf362016-04-19 15:39:37 -0700776const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
777 CHECK(!file_released_) << "GetFile called after oat file released.";
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000778 if (load_attempted_) {
779 return file_.get();
780 }
781 load_attempted_ = true;
782 if (!filename_provided_) {
783 return nullptr;
784 }
785
786 std::string error_msg;
787 bool executable = oat_file_assistant_->load_executable_;
788 if (android::base::EndsWith(filename_, kVdexExtension)) {
789 executable = false;
790 // Check to see if there is a vdex file we can make use of.
791 std::unique_ptr<VdexFile> vdex;
792 if (use_fd_) {
793 if (vdex_fd_ >= 0) {
794 struct stat s;
795 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
796 if (rc == -1) {
797 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
798 } else {
799 vdex = VdexFile::Open(vdex_fd_,
800 s.st_size,
801 filename_,
802 /*writable=*/ false,
803 /*low_4gb=*/ false,
804 /*unquicken=*/ false,
805 &error_msg);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700806 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000807 }
808 } else {
809 vdex = VdexFile::Open(filename_,
810 /*writable=*/ false,
811 /*low_4gb=*/ false,
812 /*unquicken=*/ false,
813 &error_msg);
814 }
815 if (vdex == nullptr) {
816 VLOG(oat) << "unable to open vdex file " << filename_ << ": " << error_msg;
817 } else {
818 file_.reset(OatFile::OpenFromVdex(zip_fd_,
819 std::move(vdex),
820 oat_file_assistant_->dex_location_,
821 &error_msg));
822 }
823 } else {
824 if (executable && oat_file_assistant_->only_load_system_executable_) {
825 executable = LocationIsOnSystem(filename_.c_str());
826 }
827 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
828 if (use_fd_) {
829 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
830 ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_,
831 /*size=*/ 1u);
832 file_.reset(OatFile::Open(zip_fd_,
833 vdex_fd_,
834 oat_fd_,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700835 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000836 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100837 /*low_4gb=*/ false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000838 dex_locations,
839 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700840 &error_msg));
841 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000842 } else {
843 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
844 filename_.c_str(),
845 filename_.c_str(),
846 executable,
847 /*low_4gb=*/ false,
848 oat_file_assistant_->dex_location_,
849 &error_msg));
Richard Uhler743bf362016-04-19 15:39:37 -0700850 }
851 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000852 if (file_.get() == nullptr) {
853 VLOG(oat) << "OatFileAssistant test for existing oat file "
854 << filename_
855 << ": " << error_msg;
856 } else {
857 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
858 }
Richard Uhler743bf362016-04-19 15:39:37 -0700859 return file_.get();
860}
861
862bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700863 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700864 const OatFile* file = GetFile();
865 if (file == nullptr) {
866 return false;
867 }
868
869 CompilerFilter::Filter current = file->GetCompilerFilter();
870 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
871 VLOG(oat) << "Compiler filter not okay because Profile changed";
872 return false;
873 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700874 return downgrade ? !CompilerFilter::IsBetter(current, target) :
875 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700876}
877
David Brazdil89821862019-03-19 13:57:43 +0000878bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context,
879 const std::vector<int>& context_fds) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700880 const OatFile* file = GetFile();
881 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -0700882 // No oat file means we have nothing to verify.
883 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700884 }
885
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000886 if (file->IsBackedByVdexOnly()) {
887 // Only a vdex file, we don't depend on the class loader context.
888 return true;
889 }
890
Calin Juravleaf322422020-02-11 13:45:53 -0800891 if (!CompilerFilter::IsVerificationEnabled(file->GetCompilerFilter())) {
892 // If verification is not enabled we don't need to verify the class loader context and we
893 // assume it's ok.
894 return true;
895 }
896
Calin Juravle0a5cad32020-02-14 20:29:26 +0000897
Calin Juravleaf322422020-02-11 13:45:53 -0800898 if (context == nullptr) {
Calin Juravle0a5cad32020-02-14 20:29:26 +0000899 // TODO(calin): stop using null for the unkown contexts.
900 // b/148494302 introduces runtime encoding for unknown context which will make this possible.
901 VLOG(oat) << "ClassLoaderContext check failed: uknown(null) context";
902 return false;
Calin Juravleaf322422020-02-11 13:45:53 -0800903 }
904
Calin Juravle20c46442017-09-12 00:54:26 -0700905 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -0700906 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -0700907 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -0700908 : "";
909
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800910 if (!context->OpenDexFiles(classpath_dir, context_fds, /*only_read_checksums*/ true)) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700911 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
912 return false;
913 }
914
Mathieu Chartieradc90862018-05-11 13:03:06 -0700915 const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
916 ClassLoaderContext::VerificationResult::kMismatch;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700917 if (!result) {
918 VLOG(oat) << "ClassLoaderContext check failed. Context was "
919 << file->GetClassLoaderContext()
920 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
921 }
922 return result;
923}
924
Richard Uhler743bf362016-04-19 15:39:37 -0700925bool OatFileAssistant::OatFileInfo::IsExecutable() {
926 const OatFile* file = GetFile();
927 return (file != nullptr && file->IsExecutable());
928}
929
Richard Uhler743bf362016-04-19 15:39:37 -0700930void OatFileAssistant::OatFileInfo::Reset() {
931 load_attempted_ = false;
932 file_.reset();
933 status_attempted_ = false;
934}
935
Nicolas Geoffray30025092018-04-19 14:43:29 +0100936void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
937 bool use_fd,
938 int zip_fd,
939 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700940 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700941 filename_provided_ = true;
942 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700943 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100944 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700945 vdex_fd_ = vdex_fd;
946 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700947 Reset();
948}
949
950std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
951 file_released_ = true;
952 return std::move(file_);
953}
954
Richard Uhler70a84262016-11-08 16:51:51 +0000955std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700956 ScopedTrace trace("ReleaseFileForUse");
Richard Uhler3e580bc2016-11-08 16:23:07 +0000957 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000958 return ReleaseFile();
959 }
960
Richard Uhler70a84262016-11-08 16:51:51 +0000961 return std::unique_ptr<OatFile>();
962}
Calin Juravle5f9a8012018-02-12 20:27:46 -0800963
964// TODO(calin): we could provide a more refined status here
965// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
966// track more experiments but adds extra complexity.
967void OatFileAssistant::GetOptimizationStatus(
968 const std::string& filename,
969 InstructionSet isa,
970 std::string* out_compilation_filter,
971 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700972 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
973 // non-executable and check the status manually.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100974 OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false);
Calin Juravle98071152021-01-27 18:41:58 -0800975 std::string out_odex_location; // unused
976 std::string out_odex_status; // unused
977 oat_file_assistant.GetOptimizationStatus(
978 &out_odex_location,
979 out_compilation_filter,
980 out_compilation_reason,
981 &out_odex_status);
982}
983
984void OatFileAssistant::GetOptimizationStatus(
985 std::string* out_odex_location,
986 std::string* out_compilation_filter,
987 std::string* out_compilation_reason,
988 std::string* out_odex_status) {
989 OatFileInfo& oat_file_info = GetBestInfo();
990 const OatFile* oat_file = GetBestInfo().GetFile();
Calin Juravle5f9a8012018-02-12 20:27:46 -0800991
992 if (oat_file == nullptr) {
Calin Juravle98071152021-01-27 18:41:58 -0800993 *out_odex_location = "error";
Calin Juravle5f9a8012018-02-12 20:27:46 -0800994 *out_compilation_filter = "run-from-apk";
995 *out_compilation_reason = "unknown";
Calin Juravle98071152021-01-27 18:41:58 -0800996 // This mostly happens when we cannot open the oat file.
997 // Note that it's different than kOatCannotOpen.
998 // TODO: The design of getting the BestInfo is not ideal,
999 // as it's not very clear what's the difference between
1000 // a nullptr and kOatcannotOpen. The logic should be revised
1001 // and improved.
1002 *out_odex_status = "io-error-no-oat";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001003 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001004 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001005
Calin Juravle98071152021-01-27 18:41:58 -08001006 *out_odex_location = oat_file->GetLocation();
1007 OatStatus status = oat_file_info.Status();
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001008 const char* reason = oat_file->GetCompilationReason();
1009 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1010 switch (status) {
Calin Juravle98071152021-01-27 18:41:58 -08001011 case kOatUpToDate:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001012 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
Calin Juravle98071152021-01-27 18:41:58 -08001013 *out_odex_status = "up-to-date";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001014 return;
1015
1016 case kOatCannotOpen: // This should never happen, but be robust.
1017 *out_compilation_filter = "error";
1018 *out_compilation_reason = "error";
Calin Juravle98071152021-01-27 18:41:58 -08001019 // This mostly happens when we cannot open the vdex file,
1020 // or the file is corrupt.
1021 *out_odex_status = "io-error-or-corruption";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001022 return;
1023
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001024 case kOatBootImageOutOfDate:
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +01001025 *out_compilation_filter = "run-from-apk-fallback";
Calin Juravle98071152021-01-27 18:41:58 -08001026 *out_odex_status = "boot-image-more-recent";
1027 return;
1028
1029 case kOatDexOutOfDate:
1030 *out_compilation_filter = "run-from-apk-fallback";
1031 *out_odex_status = "apk-more-recent";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001032 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001033 }
1034 LOG(FATAL) << "Unreachable";
1035 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001036}
1037
Richard Uhler66d874d2015-01-15 09:37:19 -08001038} // namespace art