blob: 2eaf966ec6ac5d4734c4ca358d43596f127f5862 [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 Uhler66d874d2015-01-15 09:37:19 -080019#include <sys/stat.h>
Jiakai Zhang78d86b52022-05-26 17:24:17 +010020
21#include <memory>
Jiakai Zhangc372e792022-11-23 10:57:22 +000022#include <optional>
Jiakai Zhang78d86b52022-05-26 17:24:17 +010023#include <sstream>
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010024#include <vector>
Andreas Gampe9186ced2016-12-12 14:28:21 -080025
Nicolas Geoffray525fa422021-04-19 07:50:35 +000026#include "android-base/file.h"
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +010027#include "android-base/logging.h"
Martin Stjernholmef6ac1d2023-05-11 20:57:47 +010028#include "android-base/properties.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070029#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080030#include "android-base/strings.h"
Jiakai Zhang78d86b52022-05-26 17:24:17 +010031#include "arch/instruction_set.h"
32#include "base/array_ref.h"
Eric Holkc7ac91b2021-02-04 21:44:01 +000033#include "base/compiler_filter.h"
David Sehr891a50e2017-10-27 17:01:07 -070034#include "base/file_utils.h"
Jiakai Zhang0187b182023-07-31 11:01:45 +010035#include "base/globals.h"
Andreas Gampe57943812017-12-06 21:39:13 -080036#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070037#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080038#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070039#include "base/stl_util.h"
Vladimir Markobcd99be2019-03-22 16:21:31 +000040#include "base/string_view_cpp20.h"
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -070041#include "base/systrace.h"
David Sehrc431b9d2018-03-02 12:01:51 -080042#include "base/utils.h"
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +010043#include "base/zip_archive.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080044#include "class_linker.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000045#include "class_loader_context.h"
David Sehr013fd802018-01-11 22:55:24 -080046#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080048#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080049#include "gc/heap.h"
50#include "gc/space/image_space.h"
51#include "image.h"
52#include "oat.h"
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010053#include "oat_file_assistant_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080054#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070055#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000056#include "vdex_file.h"
Jiakai Zhang78d86b52022-05-26 17:24:17 +010057#include "zlib.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080058
Dmitrii Ishcheikin689cca82024-01-22 13:47:24 +000059namespace art HIDDEN {
Richard Uhler66d874d2015-01-15 09:37:19 -080060
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010061using ::android::base::ConsumePrefix;
62using ::android::base::StringPrintf;
Richard Uhler69bcf2c2017-01-24 10:25:21 +000063
David Brazdil35a3f6a2019-03-04 15:59:06 +000064static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@";
65static constexpr const char* kVdexExtension = ".vdex";
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +010066static constexpr const char* kDmExtension = ".dm";
David Brazdil35a3f6a2019-03-04 15:59:06 +000067
Stefano Cianciulliba87ab52023-03-30 12:35:16 +000068std::ostream& operator<<(std::ostream& stream, const OatFileAssistant::OatStatus status) {
Narayan Kamath8943c1d2016-05-02 13:14:48 +010069 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000070 case OatFileAssistant::kOatCannotOpen:
71 stream << "kOatCannotOpen";
72 break;
73 case OatFileAssistant::kOatDexOutOfDate:
74 stream << "kOatDexOutOfDate";
75 break;
76 case OatFileAssistant::kOatBootImageOutOfDate:
77 stream << "kOatBootImageOutOfDate";
78 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010079 case OatFileAssistant::kOatUpToDate:
80 stream << "kOatUpToDate";
81 break;
Nicolas Geoffray525fa422021-04-19 07:50:35 +000082 case OatFileAssistant::kOatContextOutOfDate:
83 stream << "kOaContextOutOfDate";
84 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010085 }
86
87 return stream;
88}
89
Richard Uhler66d874d2015-01-15 09:37:19 -080090OatFileAssistant::OatFileAssistant(const char* dex_location,
91 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +000092 ClassLoaderContext* context,
Nicolas Geoffray29742602017-12-14 10:09:03 +000093 bool load_executable,
Jiakai Zhang78d86b52022-05-26 17:24:17 +010094 bool only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010095 OatFileAssistantContext* ofa_context)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070096 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000097 isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +000098 context,
Nicolas Geoffray29742602017-12-14 10:09:03 +000099 load_executable,
Orion Hodson094b1cf2021-06-08 09:28:28 +0100100 only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100101 ofa_context,
102 /*vdex_fd=*/-1,
103 /*oat_fd=*/-1,
104 /*zip_fd=*/-1) {}
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700105
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700106OatFileAssistant::OatFileAssistant(const char* dex_location,
107 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000108 ClassLoaderContext* context,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700109 bool load_executable,
Orion Hodson094b1cf2021-06-08 09:28:28 +0100110 bool only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100111 OatFileAssistantContext* ofa_context,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700112 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700113 int oat_fd,
114 int zip_fd)
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000115 : context_(context),
116 isa_(isa),
Richard Uhler88bc6732016-11-14 14:38:03 +0000117 load_executable_(load_executable),
Orion Hodson094b1cf2021-06-08 09:28:28 +0100118 only_load_trusted_executable_(only_load_trusted_executable),
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100119 odex_(this, /*is_oat_location=*/false),
120 oat_(this, /*is_oat_location=*/true),
121 vdex_for_odex_(this, /*is_oat_location=*/false),
122 vdex_for_oat_(this, /*is_oat_location=*/true),
123 dm_for_odex_(this, /*is_oat_location=*/false),
124 dm_for_oat_(this, /*is_oat_location=*/true),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700125 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -0700126 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Santiago Aboy Solanes6cdabe12022-02-18 15:27:43 +0000127 CHECK_IMPLIES(load_executable, context != nullptr) << "Loading executable without a context";
Calin Juravle357c66d2017-05-04 01:57:17 +0000128
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700129 if (zip_fd < 0) {
130 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000131 << " oat_fd=" << oat_fd;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700132 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000133 << " vdex_fd=" << vdex_fd;
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000134 CHECK(!UseFdToReadFiles());
135 } else {
136 CHECK(UseFdToReadFiles());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700137 }
138
Calin Juravle099f8d62017-09-05 19:04:04 -0700139 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700140
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100141 Runtime* runtime = Runtime::Current();
142
143 if (load_executable_ && runtime == nullptr) {
144 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
145 << "but no active runtime is found. Will not attempt to load executable.";
146 load_executable_ = false;
147 }
148
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100149 if (load_executable_ && isa != kRuntimeISA) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800150 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000151 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800152 load_executable_ = false;
153 }
154
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100155 if (ofa_context == nullptr) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100156 CHECK(runtime != nullptr) << "runtime_options is not provided, and no active runtime is found.";
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100157 ofa_context_ = std::make_unique<OatFileAssistantContext>(runtime);
158 } else {
159 ofa_context_ = ofa_context;
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100160 }
161
162 if (runtime == nullptr) {
163 // We need `MemMap` for mapping files. We don't have to initialize it when there is a runtime
164 // because the runtime initializes it.
165 MemMap::Init();
166 }
167
Richard Uhler743bf362016-04-19 15:39:37 -0700168 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700169 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700170 std::string odex_file_name;
171 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100172 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000173 std::string vdex_file_name = GetVdexFilename(odex_file_name);
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000174 // We dup FDs as the odex_ will claim ownership.
175 vdex_for_odex_.Reset(vdex_file_name,
176 UseFdToReadFiles(),
177 DupCloexec(zip_fd),
178 DupCloexec(vdex_fd),
179 DupCloexec(oat_fd));
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100180
181 std::string dm_file_name = GetDmFilename(dex_location_);
182 dm_for_odex_.Reset(dm_file_name,
183 UseFdToReadFiles(),
184 DupCloexec(zip_fd),
185 DupCloexec(vdex_fd),
186 DupCloexec(oat_fd));
Richard Uhler743bf362016-04-19 15:39:37 -0700187 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700188 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
189 }
190
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700191 if (!UseFdToReadFiles()) {
192 // Get the oat filename.
193 std::string oat_file_name;
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100194 if (DexLocationToOatFilename(dex_location_,
195 isa_,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100196 GetRuntimeOptions().deny_art_apex_data_files,
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100197 &oat_file_name,
198 &error_msg)) {
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000199 oat_.Reset(oat_file_name, /*use_fd=*/false);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000200 std::string vdex_file_name = GetVdexFilename(oat_file_name);
201 vdex_for_oat_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100202 std::string dm_file_name = GetDmFilename(dex_location);
203 dm_for_oat_.Reset(dm_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Jiakai Zhang0187b182023-07-31 11:01:45 +0100204 } else if (kIsTargetAndroid) {
205 // No need to warn on host. We are probably in oatdump, where we only need OatFileAssistant to
206 // validate BCP checksums.
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000207 LOG(WARNING) << "Failed to determine oat file name for dex location " << dex_location_ << ": "
208 << error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700209 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000210 }
Jiakai Zhang3796e882023-11-03 13:46:47 +0000211
212 // Check if the dex directory is writable.
213 // This will be needed in most uses of OatFileAssistant and so it's OK to
214 // compute it eagerly. (the only use which will not make use of it is
215 // OatFileAssistant::GetStatusDump())
216 size_t pos = dex_location_.rfind('/');
217 if (pos == std::string::npos) {
218 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
219 } else if (!UseFdToReadFiles()) {
220 // We cannot test for parent access when using file descriptors. That's ok
221 // because in this case we will always pick the odex file anyway.
222 std::string parent = dex_location_.substr(0, pos);
223 if (access(parent.c_str(), W_OK) == 0) {
224 dex_parent_writable_ = true;
225 } else {
226 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
227 }
228 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800229}
230
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100231std::unique_ptr<OatFileAssistant> OatFileAssistant::Create(
232 const std::string& filename,
233 const std::string& isa_str,
Jiakai Zhangb4593412022-10-28 11:50:26 +0100234 const std::optional<std::string>& context_str,
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100235 bool load_executable,
236 bool only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100237 OatFileAssistantContext* ofa_context,
Jiakai Zhang865006d2022-07-01 21:09:10 +0100238 /*out*/ std::unique_ptr<ClassLoaderContext>* context,
239 /*out*/ std::string* error_msg) {
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100240 InstructionSet isa = GetInstructionSetFromString(isa_str.c_str());
241 if (isa == InstructionSet::kNone) {
242 *error_msg = StringPrintf("Instruction set '%s' is invalid", isa_str.c_str());
243 return nullptr;
244 }
245
Jiakai Zhangb4593412022-10-28 11:50:26 +0100246 std::unique_ptr<ClassLoaderContext> tmp_context = nullptr;
247 if (context_str.has_value()) {
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000248 tmp_context = ClassLoaderContext::Create(context_str.value());
Jiakai Zhangb4593412022-10-28 11:50:26 +0100249 if (tmp_context == nullptr) {
250 *error_msg = StringPrintf("Class loader context '%s' is invalid", context_str->c_str());
251 return nullptr;
252 }
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100253
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000254 if (!tmp_context->OpenDexFiles(android::base::Dirname(filename),
Jiakai Zhangb4593412022-10-28 11:50:26 +0100255 /*context_fds=*/{},
256 /*only_read_checksums=*/true)) {
257 *error_msg =
258 StringPrintf("Failed to load class loader context files for '%s' with context '%s'",
259 filename.c_str(),
260 context_str->c_str());
261 return nullptr;
262 }
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100263 }
264
265 auto assistant = std::make_unique<OatFileAssistant>(filename.c_str(),
266 isa,
Jiakai Zhang865006d2022-07-01 21:09:10 +0100267 tmp_context.get(),
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100268 load_executable,
269 only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100270 ofa_context);
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100271
Jiakai Zhang865006d2022-07-01 21:09:10 +0100272 *context = std::move(tmp_context);
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100273 return assistant;
274}
275
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000276bool OatFileAssistant::UseFdToReadFiles() { return zip_fd_ >= 0; }
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700277
Richard Uhler66d874d2015-01-15 09:37:19 -0800278bool OatFileAssistant::IsInBootClassPath() {
279 // Note: We check the current boot class path, regardless of the ISA
280 // specified by the user. This is okay, because the boot class path should
281 // be the same for all ISAs.
282 // TODO: Can we verify the boot class path is the same for all ISAs?
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100283 for (const std::string& boot_class_path_location :
284 GetRuntimeOptions().boot_class_path_locations) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100285 if (boot_class_path_location == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800286 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
287 return true;
288 }
289 }
290 return false;
291}
292
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100293OatFileAssistant::DexOptTrigger OatFileAssistant::GetDexOptTrigger(
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100294 CompilerFilter::Filter target_compiler_filter, bool profile_changed, bool downgrade) {
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100295 if (downgrade) {
296 // The caller's intention is to downgrade the compiler filter. We should only re-compile if the
297 // target compiler filter is worse than the current one.
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100298 return DexOptTrigger{.targetFilterIsWorse = true};
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100299 }
300
301 // This is the usual case. The caller's intention is to see if a better oat file can be generated.
Jiakai Zhang70517252023-01-31 15:00:53 +0800302 DexOptTrigger dexopt_trigger{
303 .targetFilterIsBetter = true, .primaryBootImageBecomesUsable = true, .needExtraction = true};
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100304 if (profile_changed && CompilerFilter::DependsOnProfile(target_compiler_filter)) {
305 // Since the profile has been changed, we should re-compile even if the compilation does not
306 // make the compiler filter better.
307 dexopt_trigger.targetFilterIsSame = true;
308 }
309 return dexopt_trigger;
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100310}
311
312int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000313 bool profile_changed,
314 bool downgrade) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000315 OatFileInfo& info = GetBestInfo();
Martin Stjernholm430efa22023-12-19 18:18:51 +0000316 if (info.CheckDisableCompactDex()) { // TODO(b/256664509): Clean this up.
317 VLOG(oat) << "Should recompile: disable cdex";
Martin Stjernholmef6ac1d2023-05-11 20:57:47 +0100318 return kDex2OatFromScratch;
319 }
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100320 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(
321 target_compiler_filter, GetDexOptTrigger(target_compiler_filter, profile_changed, downgrade));
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100322 if (dexopt_needed != kNoDexOptNeeded && (&info == &dm_for_oat_ || &info == &dm_for_odex_)) {
323 // The usable vdex file is in the DM file. This information cannot be encoded in the integer.
324 // Return kDex2OatFromScratch so that neither the vdex in the "oat" location nor the vdex in the
325 // "odex" location will be picked by installd.
326 return kDex2OatFromScratch;
327 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000328 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
329 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800330 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000331 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800332}
333
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100334bool OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
335 DexOptTrigger dexopt_trigger,
336 /*out*/ DexOptStatus* dexopt_status) {
337 OatFileInfo& info = GetBestInfo();
Martin Stjernholm430efa22023-12-19 18:18:51 +0000338 if (info.CheckDisableCompactDex()) { // TODO(b/256664509): Clean this up.
Martin Stjernholmef6ac1d2023-05-11 20:57:47 +0100339 dexopt_status->location_ = kLocationNoneOrError;
340 return true;
341 }
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100342 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target_compiler_filter, dexopt_trigger);
Jiakai Zhang1d090ef2023-12-19 18:28:53 +0000343 dexopt_status->location_ = GetLocation(info);
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100344 return dexopt_needed != kNoDexOptNeeded;
345}
346
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000347bool OatFileAssistant::IsUpToDate() { return GetBestInfo().Status() == kOatUpToDate; }
Richard Uhler01be6812016-05-17 10:34:52 -0700348
Richard Uhler66d874d2015-01-15 09:37:19 -0800349std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000350 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800351}
352
Richard Uhler46cc64f2016-11-14 14:53:55 +0000353std::string OatFileAssistant::GetStatusDump() {
354 std::ostringstream status;
355 bool oat_file_exists = false;
356 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000357 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000358 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000359 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000360
Richard Uhler46cc64f2016-11-14 14:53:55 +0000361 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000362 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
363 const OatFile* file = oat_.GetFile();
364 if (file == nullptr) {
365 // If the file is null even though the status is not kOatCannotOpen, it
366 // means we must have a vdex file with no corresponding oat file. In
367 // this case we cannot determine the compilation filter. Indicate that
368 // we have only the vdex file instead.
369 status << "vdex-only";
370 } else {
371 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
372 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000373 }
374
Richard Uhler03bc6592016-11-22 09:42:04 +0000375 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000376 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000377 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000378
Richard Uhler46cc64f2016-11-14 14:53:55 +0000379 odex_file_exists = true;
380 if (oat_file_exists) {
381 status << "] ";
382 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000383 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
384 const OatFile* file = odex_.GetFile();
385 if (file == nullptr) {
386 status << "vdex-only";
387 } else {
388 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
389 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000390 }
391
392 if (!oat_file_exists && !odex_file_exists) {
393 status << "invalid[";
394 }
395
396 status << "]";
397 return status.str();
398}
399
Richard Uhler66d874d2015-01-15 09:37:19 -0800400std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000401 const OatFile& oat_file, const char* dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800402 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700403 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
404 return dex_files;
405 } else {
406 return std::vector<std::unique_ptr<const DexFile>>();
407 }
408}
Richard Uhler66d874d2015-01-15 09:37:19 -0800409
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000410bool OatFileAssistant::LoadDexFiles(const OatFile& oat_file,
411 const std::string& dex_location,
412 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000413 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800414 std::string error_msg;
David Srbecky5945a7f2023-07-20 13:21:46 +0000415 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_location.c_str(), &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800416 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700417 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700418 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 }
420
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700421 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800422 if (dex_file.get() == nullptr) {
423 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700424 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800425 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700426 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800427
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000428 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700429 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700430 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
David Srbecky5945a7f2023-07-20 13:21:46 +0000431 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700432 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000433 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800434 break;
435 }
436
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700437 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800438 if (dex_file.get() == nullptr) {
439 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700440 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800441 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700442 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800443 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700444 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800445}
446
Jiakai Zhangc372e792022-11-23 10:57:22 +0000447std::optional<bool> OatFileAssistant::HasDexFiles(std::string* error_msg) {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100448 ScopedTrace trace("HasDexFiles");
David Srbeckyb1c6f572023-06-15 15:48:20 +0100449 std::optional<std::uint32_t> checksum;
450 if (!GetRequiredDexChecksum(&checksum, error_msg)) {
Jiakai Zhangc372e792022-11-23 10:57:22 +0000451 return std::nullopt;
452 }
David Srbeckyb1c6f572023-06-15 15:48:20 +0100453 return checksum.has_value();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700454}
455
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000456OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { return odex_.Status(); }
Richard Uhler66d874d2015-01-15 09:37:19 -0800457
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000458OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { return oat_.Status(); }
Richard Uhler66d874d2015-01-15 09:37:19 -0800459
Nicolas Geoffray6d2b6ba2022-12-05 11:41:43 +0000460bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
461 if (!file.ContainsDexCode()) {
462 // We've already checked during oat file creation that the dex files loaded
463 // from external files have the same checksums as the ones in the vdex file.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000464 return true;
465 }
Nicolas Geoffray6d2b6ba2022-12-05 11:41:43 +0000466 ScopedTrace trace("DexChecksumUpToDate");
David Srbeckyb1c6f572023-06-15 15:48:20 +0100467 std::optional<std::uint32_t> dex_checksum;
468 if (!GetRequiredDexChecksum(&dex_checksum, error_msg)) {
Jiakai Zhang003d2912022-11-24 17:27:07 +0000469 return false;
Jiakai Zhangc372e792022-11-23 10:57:22 +0000470 }
David Srbeckyb1c6f572023-06-15 15:48:20 +0100471 if (!dex_checksum.has_value()) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000472 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
473 return true;
474 }
475
David Srbeckyb1c6f572023-06-15 15:48:20 +0100476 std::vector<const OatDexFile*> oat_dex_files;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000477 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000478 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700479 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
David Srbecky5945a7f2023-07-20 13:21:46 +0000480 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000481 if (oat_dex_file == nullptr) {
482 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
483 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 }
David Srbeckyb1c6f572023-06-15 15:48:20 +0100485 oat_dex_files.push_back(oat_dex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 }
David Srbeckyb1c6f572023-06-15 15:48:20 +0100487 uint32_t oat_checksum = DexFileLoader::GetMultiDexChecksum(oat_dex_files);
488
489 CHECK(dex_checksum.has_value());
490 if (dex_checksum != oat_checksum) {
491 VLOG(oat) << "Checksum does not match: " << std::hex << file.GetLocation() << " ("
492 << oat_checksum << ") vs " << dex_location_ << " (" << *dex_checksum << ")";
493 return false;
494 }
495
Richard Uhler2f27abd2017-01-31 14:02:34 +0000496 return true;
497}
498
499OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
500 // Verify the ART_USE_READ_BARRIER state.
501 // TODO: Don't fully reject files due to read barrier state. If they contain
502 // compiled code and are otherwise okay, we should return something like
503 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
504 // barrier state doesn't matter.
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000505 if (file.GetOatHeader().IsConcurrentCopying() != gUseReadBarrier) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000506 return kOatCannotOpen;
507 }
508
509 // Verify the dex checksum.
510 std::string error_msg;
Nicolas Geoffray6d2b6ba2022-12-05 11:41:43 +0000511 if (!DexChecksumUpToDate(file, &error_msg)) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000512 LOG(ERROR) << error_msg;
513 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000514 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800515
Andreas Gampe29d38e72016-03-23 15:31:51 +0000516 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000517
Richard Uhler66d874d2015-01-15 09:37:19 -0800518 // Verify the image checksum
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000519 if (file.IsBackedByVdexOnly()) {
520 VLOG(oat) << "Image checksum test skipped for vdex file " << file.GetLocation();
521 } else if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000522 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000523 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000524 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000525 }
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100526 if (!gc::space::ImageSpace::ValidateApexVersions(
Nicolas Geoffray3aab3cf2023-02-12 22:22:46 +0000527 file.GetOatHeader(),
528 GetOatFileAssistantContext()->GetApexVersions(),
529 file.GetLocation(),
530 &error_msg)) {
Jiakai Zhang57a45be2022-02-07 16:12:57 +0000531 VLOG(oat) << error_msg;
Nicolas Geoffrayd3c45c22021-04-27 13:21:28 +0100532 return kOatBootImageOutOfDate;
533 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000534 } else {
535 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800536 }
537
Jiakai Zhang70517252023-01-31 15:00:53 +0800538 // The constraint is only enforced if the zip has uncompressed dex code.
Orion Hodson094b1cf2021-06-08 09:28:28 +0100539 if (only_load_trusted_executable_ &&
Jiakai Zhang70517252023-01-31 15:00:53 +0800540 !LocationIsTrusted(file.GetLocation(), !GetRuntimeOptions().deny_art_apex_data_files) &&
541 file.ContainsDexCode() && ZipFileOnlyContainsUncompressedDex()) {
542 LOG(ERROR) << "Not loading " << dex_location_
543 << ": oat file has dex code, but APK has uncompressed dex code";
544 return kOatDexOutOfDate;
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000545 }
546
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000547 if (!ClassLoaderContextIsOkay(file)) {
548 return kOatContextOutOfDate;
549 }
550
Richard Uhlere8109f72016-04-18 10:40:50 -0700551 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800552}
553
David Brazdil35a3f6a2019-03-04 15:59:06 +0000554bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
555 InstructionSet isa,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000556 /* out */ std::string* dex_location,
557 /* out */ std::string* vdex_filename) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100558 // Normally, OatFileAssistant should not assume that there is an active runtime. However, we
559 // reference the runtime here. This is okay because we are in a static function that is unrelated
560 // to other parts of OatFileAssistant.
561 DCHECK(Runtime::Current() != nullptr);
562
David Brazdil35a3f6a2019-03-04 15:59:06 +0000563 uint32_t checksum = adler32(0L, Z_NULL, 0);
564 for (const DexFile::Header* header : headers) {
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000565 checksum = adler32_combine(
566 checksum, header->checksum_, header->file_size_ - DexFile::kNumNonChecksumBytes);
David Brazdil35a3f6a2019-03-04 15:59:06 +0000567 }
David Brazdil35a3f6a2019-03-04 15:59:06 +0000568
569 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
570 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
571 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
572 return false;
573 }
574 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
575
576 std::string odex_filename;
577 std::string error_msg;
578 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
579 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
580 return false;
581 }
582
583 *vdex_filename = GetVdexFilename(odex_filename);
584 return true;
585}
586
587bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
588 DCHECK(basename.find('/') == std::string::npos);
589 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
590 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000591 !android::base::StartsWith(basename, kAnonymousDexPrefix) ||
David Brazdil35a3f6a2019-03-04 15:59:06 +0000592 !android::base::EndsWith(basename, kVdexExtension)) {
593 return false;
594 }
595 // Check that all characters between the prefix and extension are decimal digits.
596 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
597 if (!std::isdigit(basename[i])) {
598 return false;
599 }
600 }
601 return true;
602}
603
Orion Hodsonb6f88572021-02-10 13:59:18 +0000604bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
605 InstructionSet isa,
606 std::string* odex_filename,
607 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000608 CHECK(odex_filename != nullptr);
609 CHECK(error_msg != nullptr);
610
Jiakai Zhang7fa70862021-09-15 08:56:56 +0000611 // For a DEX file on /apex, check if there is an odex file on /system. If so, and the file exists,
612 // use it.
613 if (LocationIsOnApex(location)) {
614 const std::string system_file = GetSystemOdexFilenameForApex(location, isa);
615 if (OS::FileExists(system_file.c_str(), /*check_file_type=*/true)) {
616 *odex_filename = system_file;
617 return true;
618 } else if (errno != ENOENT) {
619 PLOG(ERROR) << "Could not check odex file " << system_file;
620 }
621 }
622
Calin Juravle357c66d2017-05-04 01:57:17 +0000623 // The odex file name is formed by replacing the dex_location extension with
624 // .odex and inserting an oat/<isa> directory. For example:
625 // location = /foo/bar/baz.jar
626 // odex_location = /foo/bar/oat/<isa>/baz.odex
627
628 // Find the directory portion of the dex location and add the oat/<isa>
629 // directory.
630 size_t pos = location.rfind('/');
631 if (pos == std::string::npos) {
632 *error_msg = "Dex location " + location + " has no directory.";
633 return false;
634 }
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000635 std::string dir = location.substr(0, pos + 1);
Calin Juravle357c66d2017-05-04 01:57:17 +0000636 // Add the oat directory.
637 dir += "oat";
Orion Hodsonb6f88572021-02-10 13:59:18 +0000638
Calin Juravle357c66d2017-05-04 01:57:17 +0000639 // Add the isa directory
640 dir += "/" + std::string(GetInstructionSetString(isa));
Calin Juravle357c66d2017-05-04 01:57:17 +0000641
642 // Get the base part of the file without the extension.
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000643 std::string file = location.substr(pos + 1);
Calin Juravle357c66d2017-05-04 01:57:17 +0000644 pos = file.rfind('.');
Jiakai Zhang07b39962023-08-09 14:58:30 +0100645 std::string base = pos != std::string::npos ? file.substr(0, pos) : file;
Calin Juravle357c66d2017-05-04 01:57:17 +0000646
647 *odex_filename = dir + "/" + base + ".odex";
648 return true;
649}
650
Richard Uhlerb81881d2016-04-19 13:08:04 -0700651bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
652 InstructionSet isa,
653 std::string* oat_filename,
654 std::string* error_msg) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100655 DCHECK(Runtime::Current() != nullptr);
656 return DexLocationToOatFilename(
657 location, isa, Runtime::Current()->DenyArtApexDataFiles(), oat_filename, error_msg);
658}
659
660bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
661 InstructionSet isa,
662 bool deny_art_apex_data_files,
663 std::string* oat_filename,
664 std::string* error_msg) {
Richard Uhlerb81881d2016-04-19 13:08:04 -0700665 CHECK(oat_filename != nullptr);
666 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800667
Orion Hodsonb6f88572021-02-10 13:59:18 +0000668 // Check if `location` could have an oat file in the ART APEX data directory. If so, and the
669 // file exists, use it.
Orion Hodsone5276da2021-02-27 18:04:41 +0000670 const std::string apex_data_file = GetApexDataOdexFilename(location, isa);
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100671 if (!apex_data_file.empty() && !deny_art_apex_data_files) {
Orion Hodsone5276da2021-02-27 18:04:41 +0000672 if (OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) {
673 *oat_filename = apex_data_file;
674 return true;
675 } else if (errno != ENOENT) {
676 PLOG(ERROR) << "Could not check odex file " << apex_data_file;
677 }
Orion Hodsonb6f88572021-02-10 13:59:18 +0000678 }
679
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700680 // If ANDROID_DATA is not set, return false instead of aborting.
681 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100682 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700683 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
684 return false;
685 }
686
Orion Hodsonfa81f712021-01-13 12:27:21 +0000687 std::string dalvik_cache;
688 bool have_android_data = false;
689 bool dalvik_cache_exists = false;
690 bool is_global_cache = false;
691 GetDalvikCache(GetInstructionSetString(isa),
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000692 /*create_if_absent=*/true,
693 &dalvik_cache,
694 &have_android_data,
695 &dalvik_cache_exists,
696 &is_global_cache);
Orion Hodsonfa81f712021-01-13 12:27:21 +0000697 if (!dalvik_cache_exists) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700698 *error_msg = "Dalvik cache directory does not exist";
699 return false;
700 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700701
702 // TODO: The oat file assistant should be the definitive place for
703 // determining the oat file name from the dex location, not
704 // GetDalvikCacheFilename.
Orion Hodsonfa81f712021-01-13 12:27:21 +0000705 return GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800706}
707
David Srbeckyb1c6f572023-06-15 15:48:20 +0100708bool OatFileAssistant::GetRequiredDexChecksum(std::optional<uint32_t>* checksum,
709 std::string* error) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000710 if (!required_dex_checksums_attempted_) {
711 required_dex_checksums_attempted_ = true;
Jiakai Zhangc372e792022-11-23 10:57:22 +0000712
David Srbecky15cee1f2023-07-03 17:36:29 +0100713 File file(zip_fd_, /*check_usage=*/false);
714 ArtDexFileLoader dex_loader(&file, dex_location_);
David Srbeckyb1c6f572023-06-15 15:48:20 +0100715 std::optional<uint32_t> checksum2;
716 std::string error2;
717 if (dex_loader.GetMultiDexChecksum(
718 &checksum2, &error2, &zip_file_only_contains_uncompressed_dex_)) {
719 cached_required_dex_checksums_ = checksum2;
720 cached_required_dex_checksums_error_ = std::nullopt;
721 } else {
722 cached_required_dex_checksums_ = std::nullopt;
723 cached_required_dex_checksums_error_ = error2;
Richard Uhler66d874d2015-01-15 09:37:19 -0800724 }
David Srbecky15cee1f2023-07-03 17:36:29 +0100725 file.Release(); // Don't close the file yet (we have only read the checksum).
Richard Uhler66d874d2015-01-15 09:37:19 -0800726 }
Jiakai Zhangc372e792022-11-23 10:57:22 +0000727
David Srbeckyb1c6f572023-06-15 15:48:20 +0100728 if (cached_required_dex_checksums_error_.has_value()) {
729 *error = cached_required_dex_checksums_error_.value();
730 DCHECK(!error->empty());
731 return false;
Jiakai Zhangc372e792022-11-23 10:57:22 +0000732 }
David Srbeckyb1c6f572023-06-15 15:48:20 +0100733
734 if (!cached_required_dex_checksums_.has_value()) {
735 // The only valid case here is for APKs without dex files.
736 VLOG(oat) << "No dex file found in " << dex_location_;
737 }
738 *checksum = cached_required_dex_checksums_;
739 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800740}
741
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100742bool OatFileAssistant::ValidateBootClassPathChecksums(OatFileAssistantContext* ofa_context,
743 InstructionSet isa,
744 std::string_view oat_checksums,
745 std::string_view oat_boot_class_path,
746 /*out*/ std::string* error_msg) {
747 const std::vector<std::string>& bcp_locations =
748 ofa_context->GetRuntimeOptions().boot_class_path_locations;
749
750 if (oat_checksums.empty() || oat_boot_class_path.empty()) {
751 *error_msg = oat_checksums.empty() ? "Empty checksums" : "Empty boot class path";
752 return false;
753 }
754
755 size_t oat_bcp_size = gc::space::ImageSpace::CheckAndCountBCPComponents(
756 oat_boot_class_path, ArrayRef<const std::string>(bcp_locations), error_msg);
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100757 if (oat_bcp_size == static_cast<size_t>(-1)) {
758 DCHECK(!error_msg->empty());
759 return false;
760 }
Theodore Duboisb7a86cb2022-10-25 17:42:56 -0700761 DCHECK_LE(oat_bcp_size, bcp_locations.size());
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100762
763 size_t bcp_index = 0;
764 size_t boot_image_index = 0;
765 bool found_d = false;
766
767 while (bcp_index < oat_bcp_size) {
768 static_assert(gc::space::ImageSpace::kImageChecksumPrefix == 'i', "Format prefix check");
769 static_assert(gc::space::ImageSpace::kDexFileChecksumPrefix == 'd', "Format prefix check");
770 if (StartsWith(oat_checksums, "i") && !found_d) {
771 const std::vector<OatFileAssistantContext::BootImageInfo>& boot_image_info_list =
772 ofa_context->GetBootImageInfoList(isa);
773 if (boot_image_index >= boot_image_info_list.size()) {
774 *error_msg = StringPrintf("Missing boot image for %s, remaining checksums: %s",
775 bcp_locations[bcp_index].c_str(),
776 std::string(oat_checksums).c_str());
777 return false;
778 }
779
780 const OatFileAssistantContext::BootImageInfo& boot_image_info =
781 boot_image_info_list[boot_image_index];
782 if (!ConsumePrefix(&oat_checksums, boot_image_info.checksum)) {
783 *error_msg = StringPrintf("Image checksum mismatch, expected %s to start with %s",
784 std::string(oat_checksums).c_str(),
785 boot_image_info.checksum.c_str());
786 return false;
787 }
788
789 bcp_index += boot_image_info.component_count;
790 boot_image_index++;
791 } else if (StartsWith(oat_checksums, "d")) {
792 found_d = true;
793 const std::vector<std::string>* bcp_checksums =
794 ofa_context->GetBcpChecksums(bcp_index, error_msg);
795 if (bcp_checksums == nullptr) {
796 return false;
797 }
798 oat_checksums.remove_prefix(1u);
799 for (const std::string& checksum : *bcp_checksums) {
800 if (!ConsumePrefix(&oat_checksums, checksum)) {
801 *error_msg = StringPrintf(
802 "Dex checksum mismatch for bootclasspath file %s, expected %s to start with %s",
803 bcp_locations[bcp_index].c_str(),
804 std::string(oat_checksums).c_str(),
805 checksum.c_str());
806 return false;
807 }
808 }
809
810 bcp_index++;
811 } else {
812 *error_msg = StringPrintf("Unexpected checksums, expected %s to start with %s",
813 std::string(oat_checksums).c_str(),
814 found_d ? "'d'" : "'i' or 'd'");
815 return false;
816 }
817
818 if (bcp_index < oat_bcp_size) {
819 if (!ConsumePrefix(&oat_checksums, ":")) {
820 if (oat_checksums.empty()) {
821 *error_msg =
822 StringPrintf("Checksum too short, missing %zu components", oat_bcp_size - bcp_index);
823 } else {
824 *error_msg = StringPrintf("Missing ':' separator at start of %s",
825 std::string(oat_checksums).c_str());
826 }
827 return false;
828 }
829 }
830 }
831
832 if (!oat_checksums.empty()) {
833 *error_msg =
834 StringPrintf("Checksum too long, unexpected tail: %s", std::string(oat_checksums).c_str());
835 return false;
836 }
837
838 return true;
839}
840
Vladimir Markobcd99be2019-03-22 16:21:31 +0000841bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100842 // Get the checksums and the BCP from the oat file.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000843 const char* oat_boot_class_path_checksums =
844 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100845 const char* oat_boot_class_path =
846 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
847 if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) {
Vladimir Markof3d88a82018-12-21 16:38:47 +0000848 return false;
849 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000850
Vladimir Marko436c6f52019-07-25 14:50:14 +0100851 std::string error_msg;
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100852 bool result = ValidateBootClassPathChecksums(GetOatFileAssistantContext(),
853 isa_,
854 oat_boot_class_path_checksums,
855 oat_boot_class_path,
856 &error_msg);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100857 if (!result) {
858 VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation()
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000859 << " error: " << error_msg;
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100860 return false;
Vladimir Marko436c6f52019-07-25 14:50:14 +0100861 }
862
Vladimir Marko436c6f52019-07-25 14:50:14 +0100863 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800864}
865
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100866bool OatFileAssistant::IsPrimaryBootImageUsable() {
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100867 return !GetOatFileAssistantContext()->GetBootImageInfoList(isa_).empty();
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100868}
869
Richard Uhler88bc6732016-11-14 14:38:03 +0000870OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700871 ScopedTrace trace("GetBestInfo");
Jiakai Zhang3796e882023-11-03 13:46:47 +0000872 // TODO(calin): Document the side effects of class loading when
873 // running dalvikvm command line.
874 if (dex_parent_writable_ || UseFdToReadFiles()) {
875 // If the parent of the dex file is writable it means that we can
876 // create the odex file. In this case we unconditionally pick the odex
877 // as the best oat file. This corresponds to the regular use case when
878 // apps gets installed or when they load private, secondary dex file.
879 // For apps on the system partition the odex location will not be
880 // writable and thus the oat location might be more up to date.
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000881
Jiakai Zhang3796e882023-11-03 13:46:47 +0000882 // If the odex is not useable, and we have a useable vdex, return the vdex
883 // instead.
884 VLOG(oat) << ART_FORMAT("GetBestInfo checking odex next to the dex file ({})",
885 odex_.DisplayFilename());
886 if (!odex_.IsUseable()) {
887 VLOG(oat) << ART_FORMAT("GetBestInfo checking vdex next to the dex file ({})",
888 vdex_for_odex_.DisplayFilename());
889 if (vdex_for_odex_.IsUseable()) {
890 return vdex_for_odex_;
891 }
892 VLOG(oat) << ART_FORMAT("GetBestInfo checking dm ({})", dm_for_odex_.DisplayFilename());
893 if (dm_for_odex_.IsUseable()) {
894 return dm_for_odex_;
895 }
896 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000897 return odex_;
898 }
899
Jiakai Zhang3796e882023-11-03 13:46:47 +0000900 // We cannot write to the odex location. This must be a system app.
901
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000902 // If the oat location is useable take it.
Jiakai Zhangb09e9572023-05-03 17:37:39 +0100903 VLOG(oat) << ART_FORMAT("GetBestInfo checking odex in dalvik-cache ({})", oat_.DisplayFilename());
Calin Juravle357c66d2017-05-04 01:57:17 +0000904 if (oat_.IsUseable()) {
905 return oat_;
906 }
907
Jiakai Zhang3796e882023-11-03 13:46:47 +0000908 // The oat file is not useable but the odex file might be up to date.
909 // This is an indication that we are dealing with an up to date prebuilt
910 // (that doesn't need relocation).
911 VLOG(oat) << ART_FORMAT("GetBestInfo checking odex next to the dex file ({})",
912 odex_.DisplayFilename());
913 if (odex_.IsUseable()) {
914 return odex_;
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000915 }
Jiakai Zhang3796e882023-11-03 13:46:47 +0000916
917 // Look for a useable vdex file.
Nicolas Geoffray4f6801e2023-06-29 15:36:42 +0100918 VLOG(oat) << ART_FORMAT("GetBestInfo checking vdex in dalvik-cache ({})",
919 vdex_for_oat_.DisplayFilename());
920 if (vdex_for_oat_.IsUseable()) {
921 return vdex_for_oat_;
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100922 }
Jiakai Zhang3796e882023-11-03 13:46:47 +0000923 VLOG(oat) << ART_FORMAT("GetBestInfo checking vdex next to the dex file ({})",
924 vdex_for_odex_.DisplayFilename());
925 if (vdex_for_odex_.IsUseable()) {
926 return vdex_for_odex_;
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100927 }
Nicolas Geoffray4f6801e2023-06-29 15:36:42 +0100928 VLOG(oat) << ART_FORMAT("GetBestInfo checking dm ({})", dm_for_oat_.DisplayFilename());
929 if (dm_for_oat_.IsUseable()) {
930 return dm_for_oat_;
931 }
Jiakai Zhang3796e882023-11-03 13:46:47 +0000932 // TODO(jiakaiz): Is this the same as above?
933 VLOG(oat) << ART_FORMAT("GetBestInfo checking dm ({})", dm_for_odex_.DisplayFilename());
934 if (dm_for_odex_.IsUseable()) {
935 return dm_for_odex_;
936 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000937
Jiakai Zhang3796e882023-11-03 13:46:47 +0000938 // We got into the worst situation here:
939 // - the oat location is not useable
940 // - the prebuild odex location is not up to date
941 // - the vdex-only file is not useable
942 // - and we don't have the original dex file anymore (stripped).
943 // Pick the odex if it exists, or the oat if not.
Jiakai Zhangb09e9572023-05-03 17:37:39 +0100944 VLOG(oat) << "GetBestInfo no usable artifacts";
Calin Juravle357c66d2017-05-04 01:57:17 +0000945 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000946}
947
Andreas Gampea463b6a2016-08-12 21:53:32 -0700948std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800949 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100950 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800951 if (art_file.empty()) {
952 return nullptr;
953 }
954 std::string error_msg;
955 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700956 std::unique_ptr<gc::space::ImageSpace> ret =
957 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800958 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800959 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
960 }
961 return ret;
962}
963
Richard Uhler88bc6732016-11-14 14:38:03 +0000964OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
965 bool is_oat_location)
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000966 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location) {}
Richard Uhler743bf362016-04-19 15:39:37 -0700967
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000968bool OatFileAssistant::OatFileInfo::IsOatLocation() { return is_oat_location_; }
Richard Uhler88bc6732016-11-14 14:38:03 +0000969
Richard Uhler743bf362016-04-19 15:39:37 -0700970const std::string* OatFileAssistant::OatFileInfo::Filename() {
971 return filename_provided_ ? &filename_ : nullptr;
972}
973
Jiakai Zhangb09e9572023-05-03 17:37:39 +0100974const char* OatFileAssistant::OatFileInfo::DisplayFilename() {
975 return filename_provided_ ? filename_.c_str() : "unknown";
976}
977
Richard Uhler03bc6592016-11-22 09:42:04 +0000978bool OatFileAssistant::OatFileInfo::IsUseable() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700979 ScopedTrace trace("IsUseable");
Richard Uhler03bc6592016-11-22 09:42:04 +0000980 switch (Status()) {
981 case kOatCannotOpen:
982 case kOatDexOutOfDate:
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000983 case kOatContextOutOfDate:
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000984 case kOatBootImageOutOfDate:
985 return false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000986
Stefano Cianciulliba87ab52023-03-30 12:35:16 +0000987 case kOatUpToDate:
988 return true;
Richard Uhler03bc6592016-11-22 09:42:04 +0000989 }
990 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700991}
992
993OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700994 ScopedTrace trace("Status");
Richard Uhler743bf362016-04-19 15:39:37 -0700995 if (!status_attempted_) {
996 status_attempted_ = true;
997 const OatFile* file = GetFile();
998 if (file == nullptr) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000999 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -07001000 } else {
1001 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001002 VLOG(oat) << file->GetLocation() << " is " << status_ << " with filter "
1003 << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001004 }
1005 }
1006 return status_;
1007}
1008
Richard Uhler70a84262016-11-08 16:51:51 +00001009OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001010 CompilerFilter::Filter target_compiler_filter, const DexOptTrigger dexopt_trigger) {
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001011 if (IsUseable()) {
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001012 return ShouldRecompileForFilter(target_compiler_filter, dexopt_trigger) ? kDex2OatForFilter :
1013 kNoDexOptNeeded;
1014 }
1015
1016 // In this case, the oat file is not usable. If the caller doesn't seek for a better compiler
1017 // filter (e.g., the caller wants to downgrade), then we should not recompile.
1018 if (!dexopt_trigger.targetFilterIsBetter) {
1019 return kNoDexOptNeeded;
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001020 }
Richard Uhler70a84262016-11-08 16:51:51 +00001021
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001022 if (Status() == kOatBootImageOutOfDate) {
1023 return kDex2OatForBootImage;
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001024 }
1025
Jiakai Zhangc372e792022-11-23 10:57:22 +00001026 std::string error_msg;
1027 std::optional<bool> has_dex_files = oat_file_assistant_->HasDexFiles(&error_msg);
1028 if (has_dex_files.has_value()) {
1029 if (*has_dex_files) {
1030 return kDex2OatFromScratch;
1031 } else {
1032 // No dex file, so there is nothing we need to do.
1033 return kNoDexOptNeeded;
1034 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001035 } else {
Jiakai Zhangc372e792022-11-23 10:57:22 +00001036 // Unable to open the dex file, so there is nothing we can do.
1037 LOG(WARNING) << error_msg;
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001038 return kNoDexOptNeeded;
1039 }
Richard Uhler70a84262016-11-08 16:51:51 +00001040}
1041
Richard Uhler743bf362016-04-19 15:39:37 -07001042const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1043 CHECK(!file_released_) << "GetFile called after oat file released.";
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001044 if (load_attempted_) {
1045 return file_.get();
1046 }
1047 load_attempted_ = true;
1048 if (!filename_provided_) {
1049 return nullptr;
1050 }
1051
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001052 if (LocationIsOnArtApexData(filename_) &&
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +01001053 oat_file_assistant_->GetRuntimeOptions().deny_art_apex_data_files) {
Orion Hodson971068d2021-06-30 21:17:53 +01001054 LOG(WARNING) << "OatFileAssistant rejected file " << filename_
1055 << ": ART apexdata is untrusted.";
1056 return nullptr;
1057 }
1058
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001059 std::string error_msg;
1060 bool executable = oat_file_assistant_->load_executable_;
1061 if (android::base::EndsWith(filename_, kVdexExtension)) {
1062 executable = false;
1063 // Check to see if there is a vdex file we can make use of.
1064 std::unique_ptr<VdexFile> vdex;
1065 if (use_fd_) {
1066 if (vdex_fd_ >= 0) {
1067 struct stat s;
1068 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1069 if (rc == -1) {
1070 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1071 } else {
1072 vdex = VdexFile::Open(vdex_fd_,
1073 s.st_size,
1074 filename_,
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001075 /*writable=*/false,
1076 /*low_4gb=*/false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001077 &error_msg);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001078 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001079 }
1080 } else {
1081 vdex = VdexFile::Open(filename_,
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001082 /*writable=*/false,
1083 /*low_4gb=*/false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001084 &error_msg);
1085 }
1086 if (vdex == nullptr) {
1087 VLOG(oat) << "unable to open vdex file " << filename_ << ": " << error_msg;
1088 } else {
1089 file_.reset(OatFile::OpenFromVdex(zip_fd_,
1090 std::move(vdex),
1091 oat_file_assistant_->dex_location_,
Nicolas Geoffray0f6af5e2023-01-30 14:29:11 +00001092 oat_file_assistant_->context_,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001093 &error_msg));
1094 }
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +01001095 } else if (android::base::EndsWith(filename_, kDmExtension)) {
1096 executable = false;
1097 // Check to see if there is a vdex file we can make use of.
1098 std::unique_ptr<ZipArchive> dm_file(ZipArchive::Open(filename_.c_str(), &error_msg));
1099 if (dm_file != nullptr) {
1100 std::unique_ptr<VdexFile> vdex(VdexFile::OpenFromDm(filename_, *dm_file));
1101 if (vdex != nullptr) {
1102 file_.reset(OatFile::OpenFromVdex(zip_fd_,
1103 std::move(vdex),
1104 oat_file_assistant_->dex_location_,
Nicolas Geoffray0f6af5e2023-01-30 14:29:11 +00001105 oat_file_assistant_->context_,
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +01001106 &error_msg));
1107 }
1108 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001109 } else {
Orion Hodson094b1cf2021-06-08 09:28:28 +01001110 if (executable && oat_file_assistant_->only_load_trusted_executable_) {
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001111 executable = LocationIsTrusted(filename_, /*trust_art_apex_data_files=*/true);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001112 }
1113 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
1114 if (use_fd_) {
1115 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1116 ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_,
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001117 /*size=*/1u);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001118 file_.reset(OatFile::Open(zip_fd_,
1119 vdex_fd_,
1120 oat_fd_,
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001121 filename_,
Nicolas Geoffray29742602017-12-14 10:09:03 +00001122 executable,
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001123 /*low_4gb=*/false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001124 dex_locations,
Stefano Cianciullia5955182023-10-26 09:21:59 +00001125 /*dex_files=*/{},
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001126 /*reservation=*/nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001127 &error_msg));
1128 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001129 } else {
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001130 file_.reset(OatFile::Open(/*zip_fd=*/-1,
1131 filename_,
1132 filename_,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001133 executable,
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001134 /*low_4gb=*/false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001135 oat_file_assistant_->dex_location_,
1136 &error_msg));
Richard Uhler743bf362016-04-19 15:39:37 -07001137 }
1138 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001139 if (file_.get() == nullptr) {
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001140 VLOG(oat) << "OatFileAssistant test for existing oat file " << filename_ << ": " << error_msg;
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001141 } else {
1142 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
1143 }
Richard Uhler743bf362016-04-19 15:39:37 -07001144 return file_.get();
1145}
1146
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001147bool OatFileAssistant::OatFileInfo::ShouldRecompileForFilter(CompilerFilter::Filter target,
1148 const DexOptTrigger dexopt_trigger) {
Richard Uhler743bf362016-04-19 15:39:37 -07001149 const OatFile* file = GetFile();
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001150 DCHECK(file != nullptr);
Richard Uhler743bf362016-04-19 15:39:37 -07001151
1152 CompilerFilter::Filter current = file->GetCompilerFilter();
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001153 if (dexopt_trigger.targetFilterIsBetter && CompilerFilter::IsBetter(target, current)) {
Jiakai Zhangb09e9572023-05-03 17:37:39 +01001154 VLOG(oat) << ART_FORMAT("Should recompile: targetFilterIsBetter (current: {}, target: {})",
1155 CompilerFilter::NameOfFilter(current),
1156 CompilerFilter::NameOfFilter(target));
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001157 return true;
1158 }
1159 if (dexopt_trigger.targetFilterIsSame && current == target) {
Jiakai Zhangb09e9572023-05-03 17:37:39 +01001160 VLOG(oat) << ART_FORMAT("Should recompile: targetFilterIsSame (current: {}, target: {})",
1161 CompilerFilter::NameOfFilter(current),
1162 CompilerFilter::NameOfFilter(target));
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001163 return true;
1164 }
1165 if (dexopt_trigger.targetFilterIsWorse && CompilerFilter::IsBetter(current, target)) {
Jiakai Zhangb09e9572023-05-03 17:37:39 +01001166 VLOG(oat) << ART_FORMAT("Should recompile: targetFilterIsWorse (current: {}, target: {})",
1167 CompilerFilter::NameOfFilter(current),
1168 CompilerFilter::NameOfFilter(target));
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001169 return true;
Richard Uhler743bf362016-04-19 15:39:37 -07001170 }
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001171
Jiakai Zhang98e1ddd2023-08-02 17:51:22 +01001172 // Don't regress the compiler filter for the triggers handled below.
1173 if (CompilerFilter::IsBetter(current, target)) {
1174 VLOG(oat) << "Should not recompile: current filter is better";
1175 return false;
1176 }
1177
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001178 if (dexopt_trigger.primaryBootImageBecomesUsable &&
Jiakai Zhang98e1ddd2023-08-02 17:51:22 +01001179 CompilerFilter::IsAotCompilationEnabled(current)) {
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001180 // If the oat file has been compiled without an image, and the runtime is
1181 // now running with an image loaded from disk, return that we need to
1182 // re-compile. The recompilation will generate a better oat file, and with an app
1183 // image for profile guided compilation.
Jiakai Zhang98e1ddd2023-08-02 17:51:22 +01001184 // However, don't recompile for "verify". Although verification depends on the boot image, the
1185 // penalty of being verified without a boot image is low. Consider the case where a dex file
1186 // is verified by "ab-ota", we don't want it to be re-verified by "boot-after-ota".
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001187 const char* oat_boot_class_path_checksums =
1188 file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
1189 if (oat_boot_class_path_checksums != nullptr &&
1190 !StartsWith(oat_boot_class_path_checksums, "i") &&
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001191 oat_file_assistant_->IsPrimaryBootImageUsable()) {
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001192 DCHECK(!file->GetOatHeader().RequiresImage());
Jiakai Zhangb09e9572023-05-03 17:37:39 +01001193 VLOG(oat) << "Should recompile: primaryBootImageBecomesUsable";
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001194 return true;
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001195 }
1196 }
1197
Jiakai Zhang70517252023-01-31 15:00:53 +08001198 if (dexopt_trigger.needExtraction && !file->ContainsDexCode() &&
1199 !oat_file_assistant_->ZipFileOnlyContainsUncompressedDex()) {
Jiakai Zhangb09e9572023-05-03 17:37:39 +01001200 VLOG(oat) << "Should recompile: needExtraction";
Jiakai Zhang70517252023-01-31 15:00:53 +08001201 return true;
1202 }
1203
Jiakai Zhangb09e9572023-05-03 17:37:39 +01001204 VLOG(oat) << "Should not recompile";
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001205 return false;
Richard Uhler743bf362016-04-19 15:39:37 -07001206}
1207
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001208bool OatFileAssistant::ClassLoaderContextIsOkay(const OatFile& oat_file) const {
Jiakai Zhangb4593412022-10-28 11:50:26 +01001209 if (context_ == nullptr) {
1210 // The caller requests to skip the check.
1211 return true;
1212 }
1213
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001214 if (oat_file.IsBackedByVdexOnly()) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001215 // Only a vdex file, we don't depend on the class loader context.
1216 return true;
1217 }
1218
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001219 if (!CompilerFilter::IsVerificationEnabled(oat_file.GetCompilerFilter())) {
Calin Juravleaf322422020-02-11 13:45:53 -08001220 // If verification is not enabled we don't need to verify the class loader context and we
1221 // assume it's ok.
1222 return true;
1223 }
1224
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001225 ClassLoaderContext::VerificationResult matches =
1226 context_->VerifyClassLoaderContextMatch(oat_file.GetClassLoaderContext(),
1227 /*verify_names=*/true,
1228 /*verify_checksums=*/true);
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001229 if (matches == ClassLoaderContext::VerificationResult::kMismatch) {
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001230 VLOG(oat) << "ClassLoaderContext check failed. Context was " << oat_file.GetClassLoaderContext()
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001231 << ". The expected context is "
1232 << context_->EncodeContextForOatFile(android::base::Dirname(dex_location_));
1233 return false;
David Srbecky656fdcd2021-04-17 16:47:01 +00001234 }
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001235 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001236}
1237
Richard Uhler743bf362016-04-19 15:39:37 -07001238bool OatFileAssistant::OatFileInfo::IsExecutable() {
1239 const OatFile* file = GetFile();
1240 return (file != nullptr && file->IsExecutable());
1241}
1242
Richard Uhler743bf362016-04-19 15:39:37 -07001243void OatFileAssistant::OatFileInfo::Reset() {
1244 load_attempted_ = false;
1245 file_.reset();
1246 status_attempted_ = false;
1247}
1248
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001249void OatFileAssistant::OatFileInfo::Reset(
1250 const std::string& filename, bool use_fd, int zip_fd, int vdex_fd, int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001251 filename_provided_ = true;
1252 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001253 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +01001254 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001255 vdex_fd_ = vdex_fd;
1256 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001257 Reset();
1258}
1259
1260std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1261 file_released_ = true;
1262 return std::move(file_);
1263}
1264
Richard Uhler70a84262016-11-08 16:51:51 +00001265std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -07001266 ScopedTrace trace("ReleaseFileForUse");
Richard Uhler3e580bc2016-11-08 16:23:07 +00001267 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001268 return ReleaseFile();
1269 }
1270
Richard Uhler70a84262016-11-08 16:51:51 +00001271 return std::unique_ptr<OatFile>();
1272}
Calin Juravle5f9a8012018-02-12 20:27:46 -08001273
Martin Stjernholm430efa22023-12-19 18:18:51 +00001274// Check if we should reject vdex containing cdex code as part of the cdex
1275// deprecation.
Martin Stjernholmef6ac1d2023-05-11 20:57:47 +01001276// TODO(b/256664509): Clean this up.
Martin Stjernholm430efa22023-12-19 18:18:51 +00001277bool OatFileAssistant::OatFileInfo::CheckDisableCompactDex() {
Martin Stjernholmef6ac1d2023-05-11 20:57:47 +01001278 const OatFile* oat_file = GetFile();
1279 if (oat_file == nullptr) {
1280 return false;
1281 }
1282 const VdexFile* vdex_file = oat_file->GetVdexFile();
1283 return vdex_file != nullptr && vdex_file->HasDexSection() &&
1284 !vdex_file->HasOnlyStandardDexFiles();
1285}
1286
Calin Juravle5f9a8012018-02-12 20:27:46 -08001287// TODO(calin): we could provide a more refined status here
1288// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
1289// track more experiments but adds extra complexity.
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001290void OatFileAssistant::GetOptimizationStatus(const std::string& filename,
1291 InstructionSet isa,
1292 std::string* out_compilation_filter,
1293 std::string* out_compilation_reason,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +01001294 OatFileAssistantContext* ofa_context) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001295 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
1296 // non-executable and check the status manually.
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001297 OatFileAssistant oat_file_assistant(filename.c_str(),
1298 isa,
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001299 /*context=*/nullptr,
1300 /*load_executable=*/false,
1301 /*only_load_trusted_executable=*/false,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +01001302 ofa_context);
Calin Juravle98071152021-01-27 18:41:58 -08001303 std::string out_odex_location; // unused
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001304 std::string out_odex_status; // unused
Jiakai Zhang1d090ef2023-12-19 18:28:53 +00001305 Location out_location; // unused
1306 oat_file_assistant.GetOptimizationStatus(&out_odex_location,
1307 out_compilation_filter,
1308 out_compilation_reason,
1309 &out_odex_status,
1310 &out_location);
Calin Juravle98071152021-01-27 18:41:58 -08001311}
1312
Stefano Cianciulliba87ab52023-03-30 12:35:16 +00001313void OatFileAssistant::GetOptimizationStatus(std::string* out_odex_location,
1314 std::string* out_compilation_filter,
1315 std::string* out_compilation_reason,
Jiakai Zhang1d090ef2023-12-19 18:28:53 +00001316 std::string* out_odex_status,
1317 Location* out_location) {
Calin Juravle98071152021-01-27 18:41:58 -08001318 OatFileInfo& oat_file_info = GetBestInfo();
Santiago Aboy Solanes6fd477a2023-08-11 11:11:43 +01001319 const OatFile* oat_file = oat_file_info.GetFile();
Jiakai Zhang1d090ef2023-12-19 18:28:53 +00001320 *out_location = GetLocation(oat_file_info);
Calin Juravle5f9a8012018-02-12 20:27:46 -08001321
1322 if (oat_file == nullptr) {
Jiakai Zhang36fca052023-04-13 16:22:31 +01001323 std::string error_msg;
1324 std::optional<bool> has_dex_files = HasDexFiles(&error_msg);
1325 if (!has_dex_files.has_value()) {
1326 *out_odex_location = "error";
1327 *out_compilation_filter = "unknown";
1328 *out_compilation_reason = "unknown";
1329 // This happens when we cannot open the APK/JAR.
1330 *out_odex_status = "io-error-no-apk";
1331 } else if (!has_dex_files.value()) {
1332 *out_odex_location = "none";
1333 *out_compilation_filter = "unknown";
1334 *out_compilation_reason = "unknown";
1335 // This happens when the APK/JAR doesn't contain any DEX file.
1336 *out_odex_status = "no-dex-code";
1337 } else {
1338 *out_odex_location = "error";
1339 *out_compilation_filter = "run-from-apk";
1340 *out_compilation_reason = "unknown";
1341 // This mostly happens when we cannot open the oat file.
1342 // Note that it's different than kOatCannotOpen.
1343 // TODO: The design of getting the BestInfo is not ideal, as it's not very clear what's the
1344 // difference between a nullptr and kOatcannotOpen. The logic should be revised and improved.
1345 *out_odex_status = "io-error-no-oat";
1346 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001347 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001348 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001349
Calin Juravle98071152021-01-27 18:41:58 -08001350 *out_odex_location = oat_file->GetLocation();
1351 OatStatus status = oat_file_info.Status();
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001352 const char* reason = oat_file->GetCompilationReason();
1353 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +01001354
1355 // If the oat file is invalid, the vdex file will be picked, so the status is `kOatUpToDate`. If
1356 // the vdex file is also invalid, then either `oat_file` is nullptr, or `status` is
1357 // `kOatDexOutOfDate`.
1358 DCHECK(status == kOatUpToDate || status == kOatDexOutOfDate);
1359
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001360 switch (status) {
Calin Juravle98071152021-01-27 18:41:58 -08001361 case kOatUpToDate:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001362 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
Calin Juravle98071152021-01-27 18:41:58 -08001363 *out_odex_status = "up-to-date";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001364 return;
1365
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +01001366 case kOatCannotOpen:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001367 case kOatBootImageOutOfDate:
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001368 case kOatContextOutOfDate:
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +01001369 // These should never happen, but be robust.
1370 *out_compilation_filter = "unexpected";
1371 *out_compilation_reason = "unexpected";
1372 *out_odex_status = "unexpected";
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001373 return;
1374
Calin Juravle98071152021-01-27 18:41:58 -08001375 case kOatDexOutOfDate:
1376 *out_compilation_filter = "run-from-apk-fallback";
1377 *out_odex_status = "apk-more-recent";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001378 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001379 }
1380 LOG(FATAL) << "Unreachable";
1381 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001382}
1383
Jiakai Zhang70517252023-01-31 15:00:53 +08001384bool OatFileAssistant::ZipFileOnlyContainsUncompressedDex() {
1385 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
David Srbeckyb1c6f572023-06-15 15:48:20 +01001386 std::optional<uint32_t> checksum;
Jiakai Zhang70517252023-01-31 15:00:53 +08001387 std::string error_msg;
David Srbeckyb1c6f572023-06-15 15:48:20 +01001388 if (!GetRequiredDexChecksum(&checksum, &error_msg)) {
Jiakai Zhang70517252023-01-31 15:00:53 +08001389 LOG(ERROR) << error_msg;
1390 }
1391 return zip_file_only_contains_uncompressed_dex_;
1392}
1393
Jiakai Zhang1d090ef2023-12-19 18:28:53 +00001394OatFileAssistant::Location OatFileAssistant::GetLocation(OatFileInfo& info) {
1395 if (info.IsUseable()) {
1396 if (&info == &dm_for_oat_ || &info == &dm_for_odex_) {
1397 return kLocationDm;
1398 } else if (info.IsOatLocation()) {
1399 return kLocationOat;
1400 } else {
1401 return kLocationOdex;
1402 }
1403 } else {
1404 return kLocationNoneOrError;
1405 }
1406}
1407
Richard Uhler66d874d2015-01-15 09:37:19 -08001408} // namespace art