blob: 74f56710f0df3686f00f48b2502e82eb2af6f30b [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"
Andreas Gampec15a2f42017-04-21 12:09:39 -070028#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080029#include "android-base/strings.h"
Jiakai Zhang78d86b52022-05-26 17:24:17 +010030#include "arch/instruction_set.h"
31#include "base/array_ref.h"
Eric Holkc7ac91b2021-02-04 21:44:01 +000032#include "base/compiler_filter.h"
David Sehr891a50e2017-10-27 17:01:07 -070033#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080034#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070035#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080036#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070037#include "base/stl_util.h"
Vladimir Markobcd99be2019-03-22 16:21:31 +000038#include "base/string_view_cpp20.h"
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -070039#include "base/systrace.h"
David Sehrc431b9d2018-03-02 12:01:51 -080040#include "base/utils.h"
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +010041#include "base/zip_archive.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080042#include "class_linker.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000043#include "class_loader_context.h"
David Sehr013fd802018-01-11 22:55:24 -080044#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080045#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080046#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080047#include "gc/heap.h"
48#include "gc/space/image_space.h"
49#include "image.h"
50#include "oat.h"
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010051#include "oat_file_assistant_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080052#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070053#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000054#include "vdex_file.h"
Jiakai Zhang78d86b52022-05-26 17:24:17 +010055#include "zlib.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080056
57namespace art {
58
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010059using ::android::base::ConsumePrefix;
60using ::android::base::StringPrintf;
Richard Uhler69bcf2c2017-01-24 10:25:21 +000061
David Brazdil35a3f6a2019-03-04 15:59:06 +000062static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@";
63static constexpr const char* kVdexExtension = ".vdex";
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +010064static constexpr const char* kDmExtension = ".dm";
David Brazdil35a3f6a2019-03-04 15:59:06 +000065
Narayan Kamath8943c1d2016-05-02 13:14:48 +010066std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
67 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000068 case OatFileAssistant::kOatCannotOpen:
69 stream << "kOatCannotOpen";
70 break;
71 case OatFileAssistant::kOatDexOutOfDate:
72 stream << "kOatDexOutOfDate";
73 break;
74 case OatFileAssistant::kOatBootImageOutOfDate:
75 stream << "kOatBootImageOutOfDate";
76 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010077 case OatFileAssistant::kOatUpToDate:
78 stream << "kOatUpToDate";
79 break;
Nicolas Geoffray525fa422021-04-19 07:50:35 +000080 case OatFileAssistant::kOatContextOutOfDate:
81 stream << "kOaContextOutOfDate";
82 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010083 }
84
85 return stream;
86}
87
Richard Uhler66d874d2015-01-15 09:37:19 -080088OatFileAssistant::OatFileAssistant(const char* dex_location,
89 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +000090 ClassLoaderContext* context,
Nicolas Geoffray29742602017-12-14 10:09:03 +000091 bool load_executable,
Jiakai Zhang78d86b52022-05-26 17:24:17 +010092 bool only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010093 OatFileAssistantContext* ofa_context)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070094 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000095 isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +000096 context,
Nicolas Geoffray29742602017-12-14 10:09:03 +000097 load_executable,
Orion Hodson094b1cf2021-06-08 09:28:28 +010098 only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +010099 ofa_context,
100 /*vdex_fd=*/-1,
101 /*oat_fd=*/-1,
102 /*zip_fd=*/-1) {}
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700103
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700104OatFileAssistant::OatFileAssistant(const char* dex_location,
105 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000106 ClassLoaderContext* context,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700107 bool load_executable,
Orion Hodson094b1cf2021-06-08 09:28:28 +0100108 bool only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100109 OatFileAssistantContext* ofa_context,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700110 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700111 int oat_fd,
112 int zip_fd)
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000113 : context_(context),
114 isa_(isa),
Richard Uhler88bc6732016-11-14 14:38:03 +0000115 load_executable_(load_executable),
Orion Hodson094b1cf2021-06-08 09:28:28 +0100116 only_load_trusted_executable_(only_load_trusted_executable),
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100117 odex_(this, /*is_oat_location=*/false),
118 oat_(this, /*is_oat_location=*/true),
119 vdex_for_odex_(this, /*is_oat_location=*/false),
120 vdex_for_oat_(this, /*is_oat_location=*/true),
121 dm_for_odex_(this, /*is_oat_location=*/false),
122 dm_for_oat_(this, /*is_oat_location=*/true),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700123 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -0700124 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Santiago Aboy Solanes6cdabe12022-02-18 15:27:43 +0000125 CHECK_IMPLIES(load_executable, context != nullptr) << "Loading executable without a context";
Calin Juravle357c66d2017-05-04 01:57:17 +0000126
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700127 if (zip_fd < 0) {
128 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
129 << " oat_fd=" << oat_fd;
130 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
131 << " vdex_fd=" << vdex_fd;;
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000132 CHECK(!UseFdToReadFiles());
133 } else {
134 CHECK(UseFdToReadFiles());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700135 }
136
Calin Juravle099f8d62017-09-05 19:04:04 -0700137 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700138
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100139 Runtime* runtime = Runtime::Current();
140
141 if (load_executable_ && runtime == nullptr) {
142 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
143 << "but no active runtime is found. Will not attempt to load executable.";
144 load_executable_ = false;
145 }
146
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100147 if (load_executable_ && isa != kRuntimeISA) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800148 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100149 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800150 load_executable_ = false;
151 }
152
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100153 if (ofa_context == nullptr) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100154 CHECK(runtime != nullptr) << "runtime_options is not provided, and no active runtime is found.";
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100155 ofa_context_ = std::make_unique<OatFileAssistantContext>(runtime);
156 } else {
157 ofa_context_ = ofa_context;
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100158 }
159
160 if (runtime == nullptr) {
161 // We need `MemMap` for mapping files. We don't have to initialize it when there is a runtime
162 // because the runtime initializes it.
163 MemMap::Init();
164 }
165
Richard Uhler743bf362016-04-19 15:39:37 -0700166 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700167 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700168 std::string odex_file_name;
169 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100170 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000171 std::string vdex_file_name = GetVdexFilename(odex_file_name);
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000172 // We dup FDs as the odex_ will claim ownership.
173 vdex_for_odex_.Reset(vdex_file_name,
174 UseFdToReadFiles(),
175 DupCloexec(zip_fd),
176 DupCloexec(vdex_fd),
177 DupCloexec(oat_fd));
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100178
179 std::string dm_file_name = GetDmFilename(dex_location_);
180 dm_for_odex_.Reset(dm_file_name,
181 UseFdToReadFiles(),
182 DupCloexec(zip_fd),
183 DupCloexec(vdex_fd),
184 DupCloexec(oat_fd));
Richard Uhler743bf362016-04-19 15:39:37 -0700185 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700186 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
187 }
188
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700189 if (!UseFdToReadFiles()) {
190 // Get the oat filename.
191 std::string oat_file_name;
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100192 if (DexLocationToOatFilename(dex_location_,
193 isa_,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100194 GetRuntimeOptions().deny_art_apex_data_files,
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100195 &oat_file_name,
196 &error_msg)) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100197 oat_.Reset(oat_file_name, /*use_fd=*/ false);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000198 std::string vdex_file_name = GetVdexFilename(oat_file_name);
199 vdex_for_oat_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100200 std::string dm_file_name = GetDmFilename(dex_location);
201 dm_for_oat_.Reset(dm_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700202 } else {
203 LOG(WARNING) << "Failed to determine oat file name for dex location "
204 << dex_location_ << ": " << error_msg;
205 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000206 }
207
208 // Check if the dex directory is writable.
209 // This will be needed in most uses of OatFileAssistant and so it's OK to
210 // compute it eagerly. (the only use which will not make use of it is
211 // OatFileAssistant::GetStatusDump())
212 size_t pos = dex_location_.rfind('/');
213 if (pos == std::string::npos) {
214 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700215 } else if (!UseFdToReadFiles()) {
216 // We cannot test for parent access when using file descriptors. That's ok
217 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000218 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700219 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000220 dex_parent_writable_ = true;
221 } else {
222 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700223 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800224 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800225}
226
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100227std::unique_ptr<OatFileAssistant> OatFileAssistant::Create(
228 const std::string& filename,
229 const std::string& isa_str,
Jiakai Zhangb4593412022-10-28 11:50:26 +0100230 const std::optional<std::string>& context_str,
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100231 bool load_executable,
232 bool only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100233 OatFileAssistantContext* ofa_context,
Jiakai Zhang865006d2022-07-01 21:09:10 +0100234 /*out*/ std::unique_ptr<ClassLoaderContext>* context,
235 /*out*/ std::string* error_msg) {
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100236 InstructionSet isa = GetInstructionSetFromString(isa_str.c_str());
237 if (isa == InstructionSet::kNone) {
238 *error_msg = StringPrintf("Instruction set '%s' is invalid", isa_str.c_str());
239 return nullptr;
240 }
241
Jiakai Zhangb4593412022-10-28 11:50:26 +0100242 std::unique_ptr<ClassLoaderContext> tmp_context = nullptr;
243 if (context_str.has_value()) {
244 tmp_context = ClassLoaderContext::Create(context_str->c_str());
245 if (tmp_context == nullptr) {
246 *error_msg = StringPrintf("Class loader context '%s' is invalid", context_str->c_str());
247 return nullptr;
248 }
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100249
Jiakai Zhangb4593412022-10-28 11:50:26 +0100250 if (!tmp_context->OpenDexFiles(android::base::Dirname(filename.c_str()),
251 /*context_fds=*/{},
252 /*only_read_checksums=*/true)) {
253 *error_msg =
254 StringPrintf("Failed to load class loader context files for '%s' with context '%s'",
255 filename.c_str(),
256 context_str->c_str());
257 return nullptr;
258 }
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100259 }
260
261 auto assistant = std::make_unique<OatFileAssistant>(filename.c_str(),
262 isa,
Jiakai Zhang865006d2022-07-01 21:09:10 +0100263 tmp_context.get(),
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100264 load_executable,
265 only_load_trusted_executable,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100266 ofa_context);
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100267
Jiakai Zhang865006d2022-07-01 21:09:10 +0100268 *context = std::move(tmp_context);
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +0100269 return assistant;
270}
271
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700272bool OatFileAssistant::UseFdToReadFiles() {
273 return zip_fd_ >= 0;
274}
275
Richard Uhler66d874d2015-01-15 09:37:19 -0800276bool OatFileAssistant::IsInBootClassPath() {
277 // Note: We check the current boot class path, regardless of the ISA
278 // specified by the user. This is okay, because the boot class path should
279 // be the same for all ISAs.
280 // TODO: Can we verify the boot class path is the same for all ISAs?
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100281 for (const std::string& boot_class_path_location :
282 GetRuntimeOptions().boot_class_path_locations) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100283 if (boot_class_path_location == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800284 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
285 return true;
286 }
287 }
288 return false;
289}
290
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100291OatFileAssistant::DexOptTrigger OatFileAssistant::GetDexOptTrigger(
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100292 CompilerFilter::Filter target_compiler_filter, bool profile_changed, bool downgrade) {
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100293 if (downgrade) {
294 // The caller's intention is to downgrade the compiler filter. We should only re-compile if the
295 // target compiler filter is worse than the current one.
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100296 return DexOptTrigger{.targetFilterIsWorse = true};
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100297 }
298
299 // This is the usual case. The caller's intention is to see if a better oat file can be generated.
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100300 DexOptTrigger dexopt_trigger{.targetFilterIsBetter = true, .primaryBootImageBecomesUsable = true};
301 if (profile_changed && CompilerFilter::DependsOnProfile(target_compiler_filter)) {
302 // Since the profile has been changed, we should re-compile even if the compilation does not
303 // make the compiler filter better.
304 dexopt_trigger.targetFilterIsSame = true;
305 }
306 return dexopt_trigger;
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100307}
308
309int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000310 bool profile_changed,
311 bool downgrade) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000312 OatFileInfo& info = GetBestInfo();
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100313 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(
314 target_compiler_filter, GetDexOptTrigger(target_compiler_filter, profile_changed, downgrade));
Jiakai Zhangb9617f12022-06-29 16:27:27 +0100315 if (dexopt_needed != kNoDexOptNeeded && (&info == &dm_for_oat_ || &info == &dm_for_odex_)) {
316 // The usable vdex file is in the DM file. This information cannot be encoded in the integer.
317 // Return kDex2OatFromScratch so that neither the vdex in the "oat" location nor the vdex in the
318 // "odex" location will be picked by installd.
319 return kDex2OatFromScratch;
320 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000321 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
322 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800323 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000324 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800325}
326
Jiakai Zhangdbfa7992022-06-24 23:14:24 +0100327bool OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
328 DexOptTrigger dexopt_trigger,
329 /*out*/ DexOptStatus* dexopt_status) {
330 OatFileInfo& info = GetBestInfo();
331 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target_compiler_filter, dexopt_trigger);
332 if (info.IsUseable()) {
333 if (&info == &dm_for_oat_ || &info == &dm_for_odex_) {
334 dexopt_status->location_ = kLocationDm;
335 } else if (info.IsOatLocation()) {
336 dexopt_status->location_ = kLocationOat;
337 } else {
338 dexopt_status->location_ = kLocationOdex;
339 }
340 } else {
341 dexopt_status->location_ = kLocationNoneOrError;
342 }
343 return dexopt_needed != kNoDexOptNeeded;
344}
345
Richard Uhler01be6812016-05-17 10:34:52 -0700346bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000347 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700348}
349
Richard Uhler66d874d2015-01-15 09:37:19 -0800350std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000351 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800352}
353
Richard Uhler46cc64f2016-11-14 14:53:55 +0000354std::string OatFileAssistant::GetStatusDump() {
355 std::ostringstream status;
356 bool oat_file_exists = false;
357 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000358 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000359 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000360 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000361
Richard Uhler46cc64f2016-11-14 14:53:55 +0000362 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000363 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
364 const OatFile* file = oat_.GetFile();
365 if (file == nullptr) {
366 // If the file is null even though the status is not kOatCannotOpen, it
367 // means we must have a vdex file with no corresponding oat file. In
368 // this case we cannot determine the compilation filter. Indicate that
369 // we have only the vdex file instead.
370 status << "vdex-only";
371 } else {
372 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
373 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000374 }
375
Richard Uhler03bc6592016-11-22 09:42:04 +0000376 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000377 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000378 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000379
Richard Uhler46cc64f2016-11-14 14:53:55 +0000380 odex_file_exists = true;
381 if (oat_file_exists) {
382 status << "] ";
383 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000384 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
385 const OatFile* file = odex_.GetFile();
386 if (file == nullptr) {
387 status << "vdex-only";
388 } else {
389 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
390 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000391 }
392
393 if (!oat_file_exists && !odex_file_exists) {
394 status << "invalid[";
395 }
396
397 status << "]";
398 return status.str();
399}
400
Richard Uhler66d874d2015-01-15 09:37:19 -0800401std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700402 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800403 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700404 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
405 return dex_files;
406 } else {
407 return std::vector<std::unique_ptr<const DexFile>>();
408 }
409}
Richard Uhler66d874d2015-01-15 09:37:19 -0800410
Calin Juravle87e2cb62017-06-13 21:48:45 -0700411bool OatFileAssistant::LoadDexFiles(
412 const OatFile &oat_file,
413 const std::string& dex_location,
414 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000415 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800416 std::string error_msg;
Andreas Gampeb40d3612018-06-26 15:49:42 -0700417 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700418 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700420 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700421 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800422 }
423
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700424 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800425 if (dex_file.get() == nullptr) {
426 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700427 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800428 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700429 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800430
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000431 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700432 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700433 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000434 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700435 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000436 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800437 break;
438 }
439
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700440 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800441 if (dex_file.get() == nullptr) {
442 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700443 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800444 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700445 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800446 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700447 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800448}
449
Jiakai Zhangc372e792022-11-23 10:57:22 +0000450std::optional<bool> OatFileAssistant::HasDexFiles(std::string* error_msg) {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100451 ScopedTrace trace("HasDexFiles");
Jiakai Zhangc372e792022-11-23 10:57:22 +0000452 const std::vector<std::uint32_t>* checksums = GetRequiredDexChecksums(error_msg);
453 if (checksums == nullptr) {
454 return std::nullopt;
455 }
456 return !checksums->empty();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700457}
458
Richard Uhler95abd042015-03-24 09:51:28 -0700459OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700460 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800461}
462
Richard Uhler95abd042015-03-24 09:51:28 -0700463OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700464 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800465}
466
Nicolas Geoffray6d2b6ba2022-12-05 11:41:43 +0000467bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
468 if (!file.ContainsDexCode()) {
469 // We've already checked during oat file creation that the dex files loaded
470 // from external files have the same checksums as the ones in the vdex file.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000471 return true;
472 }
Nicolas Geoffray6d2b6ba2022-12-05 11:41:43 +0000473 ScopedTrace trace("DexChecksumUpToDate");
Jiakai Zhangc372e792022-11-23 10:57:22 +0000474 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(error_msg);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000475 if (required_dex_checksums == nullptr) {
Jiakai Zhang003d2912022-11-24 17:27:07 +0000476 return false;
Jiakai Zhangc372e792022-11-23 10:57:22 +0000477 }
478 if (required_dex_checksums->empty()) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000479 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
480 return true;
481 }
482
483 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
484 if (required_dex_checksums->size() != number_of_dex_files) {
485 *error_msg = StringPrintf("expected %zu dex files but found %u",
486 required_dex_checksums->size(),
487 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000488 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800489 }
490
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000491 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700492 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000493 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700494 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000495 if (oat_dex_file == nullptr) {
496 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
497 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800498 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000499 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
500 if (expected_checksum != actual_checksum) {
501 VLOG(oat) << "Dex checksum does not match for dex: " << dex
502 << ". Expected: " << expected_checksum
503 << ", Actual: " << actual_checksum;
504 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800505 }
506 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000507 return true;
508}
509
510OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
511 // Verify the ART_USE_READ_BARRIER state.
512 // TODO: Don't fully reject files due to read barrier state. If they contain
513 // compiled code and are otherwise okay, we should return something like
514 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
515 // barrier state doesn't matter.
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000516 if (file.GetOatHeader().IsConcurrentCopying() != gUseReadBarrier) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000517 return kOatCannotOpen;
518 }
519
520 // Verify the dex checksum.
521 std::string error_msg;
Nicolas Geoffray6d2b6ba2022-12-05 11:41:43 +0000522 if (!DexChecksumUpToDate(file, &error_msg)) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000523 LOG(ERROR) << error_msg;
524 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000525 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800526
Andreas Gampe29d38e72016-03-23 15:31:51 +0000527 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000528
Richard Uhler66d874d2015-01-15 09:37:19 -0800529 // Verify the image checksum
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000530 if (file.IsBackedByVdexOnly()) {
531 VLOG(oat) << "Image checksum test skipped for vdex file " << file.GetLocation();
532 } else if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000533 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000534 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000535 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000536 }
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100537 if (!gc::space::ImageSpace::ValidateApexVersions(
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100538 file, GetOatFileAssistantContext()->GetApexVersions(), &error_msg)) {
Jiakai Zhang57a45be2022-02-07 16:12:57 +0000539 VLOG(oat) << error_msg;
Nicolas Geoffrayd3c45c22021-04-27 13:21:28 +0100540 return kOatBootImageOutOfDate;
541 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000542 } else {
543 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800544 }
545
Orion Hodson094b1cf2021-06-08 09:28:28 +0100546 if (only_load_trusted_executable_ &&
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100547 !LocationIsTrusted(file.GetLocation(),
548 !GetRuntimeOptions().deny_art_apex_data_files) &&
Nicolas Geoffray6d2b6ba2022-12-05 11:41:43 +0000549 file.ContainsDexCode()) {
550 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
551 GetRequiredDexChecksums(&error_msg);
552 // The constraint is only enforced if the zip has uncompressed dex code.
553 if (zip_file_only_contains_uncompressed_dex_) {
554 LOG(ERROR) << "Not loading "
555 << dex_location_
556 << ": oat file has dex code, but APK has uncompressed dex code";
557 return kOatDexOutOfDate;
558 }
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000559 }
560
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000561 if (!ClassLoaderContextIsOkay(file)) {
562 return kOatContextOutOfDate;
563 }
564
Richard Uhlere8109f72016-04-18 10:40:50 -0700565 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800566}
567
David Brazdil35a3f6a2019-03-04 15:59:06 +0000568bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
569 InstructionSet isa,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000570 /* out */ std::string* dex_location,
571 /* out */ std::string* vdex_filename) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100572 // Normally, OatFileAssistant should not assume that there is an active runtime. However, we
573 // reference the runtime here. This is okay because we are in a static function that is unrelated
574 // to other parts of OatFileAssistant.
575 DCHECK(Runtime::Current() != nullptr);
576
David Brazdil35a3f6a2019-03-04 15:59:06 +0000577 uint32_t checksum = adler32(0L, Z_NULL, 0);
578 for (const DexFile::Header* header : headers) {
579 checksum = adler32_combine(checksum,
580 header->checksum_,
581 header->file_size_ - DexFile::kNumNonChecksumBytes);
582 }
David Brazdil35a3f6a2019-03-04 15:59:06 +0000583
584 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
585 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
586 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
587 return false;
588 }
589 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
590
591 std::string odex_filename;
592 std::string error_msg;
593 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
594 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
595 return false;
596 }
597
598 *vdex_filename = GetVdexFilename(odex_filename);
599 return true;
600}
601
602bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
603 DCHECK(basename.find('/') == std::string::npos);
604 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
605 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
606 !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) ||
607 !android::base::EndsWith(basename, kVdexExtension)) {
608 return false;
609 }
610 // Check that all characters between the prefix and extension are decimal digits.
611 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
612 if (!std::isdigit(basename[i])) {
613 return false;
614 }
615 }
616 return true;
617}
618
Orion Hodsonb6f88572021-02-10 13:59:18 +0000619bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
620 InstructionSet isa,
621 std::string* odex_filename,
622 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000623 CHECK(odex_filename != nullptr);
624 CHECK(error_msg != nullptr);
625
Jiakai Zhang7fa70862021-09-15 08:56:56 +0000626 // For a DEX file on /apex, check if there is an odex file on /system. If so, and the file exists,
627 // use it.
628 if (LocationIsOnApex(location)) {
629 const std::string system_file = GetSystemOdexFilenameForApex(location, isa);
630 if (OS::FileExists(system_file.c_str(), /*check_file_type=*/true)) {
631 *odex_filename = system_file;
632 return true;
633 } else if (errno != ENOENT) {
634 PLOG(ERROR) << "Could not check odex file " << system_file;
635 }
636 }
637
Calin Juravle357c66d2017-05-04 01:57:17 +0000638 // The odex file name is formed by replacing the dex_location extension with
639 // .odex and inserting an oat/<isa> directory. For example:
640 // location = /foo/bar/baz.jar
641 // odex_location = /foo/bar/oat/<isa>/baz.odex
642
643 // Find the directory portion of the dex location and add the oat/<isa>
644 // directory.
645 size_t pos = location.rfind('/');
646 if (pos == std::string::npos) {
647 *error_msg = "Dex location " + location + " has no directory.";
648 return false;
649 }
650 std::string dir = location.substr(0, pos+1);
651 // Add the oat directory.
652 dir += "oat";
Orion Hodsonb6f88572021-02-10 13:59:18 +0000653
Calin Juravle357c66d2017-05-04 01:57:17 +0000654 // Add the isa directory
655 dir += "/" + std::string(GetInstructionSetString(isa));
Calin Juravle357c66d2017-05-04 01:57:17 +0000656
657 // Get the base part of the file without the extension.
658 std::string file = location.substr(pos+1);
659 pos = file.rfind('.');
660 if (pos == std::string::npos) {
661 *error_msg = "Dex location " + location + " has no extension.";
662 return false;
663 }
664 std::string base = file.substr(0, pos);
665
666 *odex_filename = dir + "/" + base + ".odex";
667 return true;
668}
669
Richard Uhlerb81881d2016-04-19 13:08:04 -0700670bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
671 InstructionSet isa,
672 std::string* oat_filename,
673 std::string* error_msg) {
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100674 DCHECK(Runtime::Current() != nullptr);
675 return DexLocationToOatFilename(
676 location, isa, Runtime::Current()->DenyArtApexDataFiles(), oat_filename, error_msg);
677}
678
679bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
680 InstructionSet isa,
681 bool deny_art_apex_data_files,
682 std::string* oat_filename,
683 std::string* error_msg) {
Richard Uhlerb81881d2016-04-19 13:08:04 -0700684 CHECK(oat_filename != nullptr);
685 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800686
Orion Hodsonb6f88572021-02-10 13:59:18 +0000687 // Check if `location` could have an oat file in the ART APEX data directory. If so, and the
688 // file exists, use it.
Orion Hodsone5276da2021-02-27 18:04:41 +0000689 const std::string apex_data_file = GetApexDataOdexFilename(location, isa);
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100690 if (!apex_data_file.empty() && !deny_art_apex_data_files) {
Orion Hodsone5276da2021-02-27 18:04:41 +0000691 if (OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) {
692 *oat_filename = apex_data_file;
693 return true;
694 } else if (errno != ENOENT) {
695 PLOG(ERROR) << "Could not check odex file " << apex_data_file;
696 }
Orion Hodsonb6f88572021-02-10 13:59:18 +0000697 }
698
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700699 // If ANDROID_DATA is not set, return false instead of aborting.
700 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100701 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700702 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
703 return false;
704 }
705
Orion Hodsonfa81f712021-01-13 12:27:21 +0000706 std::string dalvik_cache;
707 bool have_android_data = false;
708 bool dalvik_cache_exists = false;
709 bool is_global_cache = false;
710 GetDalvikCache(GetInstructionSetString(isa),
711 /*create_if_absent=*/ true,
712 &dalvik_cache,
713 &have_android_data,
714 &dalvik_cache_exists,
715 &is_global_cache);
716 if (!dalvik_cache_exists) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700717 *error_msg = "Dalvik cache directory does not exist";
718 return false;
719 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700720
721 // TODO: The oat file assistant should be the definitive place for
722 // determining the oat file name from the dex location, not
723 // GetDalvikCacheFilename.
Orion Hodsonfa81f712021-01-13 12:27:21 +0000724 return GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800725}
726
Jiakai Zhangc372e792022-11-23 10:57:22 +0000727const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums(std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000728 if (!required_dex_checksums_attempted_) {
729 required_dex_checksums_attempted_ = true;
Jiakai Zhangc372e792022-11-23 10:57:22 +0000730 std::vector<uint32_t> checksums;
David Sehr013fd802018-01-11 22:55:24 -0800731 const ArtDexFileLoader dex_file_loader;
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800732 std::vector<std::string> dex_locations_ignored;
David Sehr013fd802018-01-11 22:55:24 -0800733 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
Jiakai Zhangc372e792022-11-23 10:57:22 +0000734 &checksums,
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800735 &dex_locations_ignored,
Jiakai Zhangc372e792022-11-23 10:57:22 +0000736 &cached_required_dex_checksums_error_,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000737 zip_fd_,
738 &zip_file_only_contains_uncompressed_dex_)) {
Jiakai Zhangc372e792022-11-23 10:57:22 +0000739 if (checksums.empty()) {
740 // The only valid case here is for APKs without dex files.
741 VLOG(oat) << "No dex file found in " << dex_location_;
742 }
743
744 cached_required_dex_checksums_ = std::move(checksums);
Richard Uhler66d874d2015-01-15 09:37:19 -0800745 }
746 }
Jiakai Zhangc372e792022-11-23 10:57:22 +0000747
748 if (cached_required_dex_checksums_.has_value()) {
Jiakai Zhangfc7236d2022-12-01 14:45:57 +0000749 return &cached_required_dex_checksums_.value();
Jiakai Zhangc372e792022-11-23 10:57:22 +0000750 } else {
751 *error_msg = cached_required_dex_checksums_error_;
752 DCHECK(!error_msg->empty());
753 return nullptr;
754 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800755}
756
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100757bool OatFileAssistant::ValidateBootClassPathChecksums(OatFileAssistantContext* ofa_context,
758 InstructionSet isa,
759 std::string_view oat_checksums,
760 std::string_view oat_boot_class_path,
761 /*out*/ std::string* error_msg) {
762 const std::vector<std::string>& bcp_locations =
763 ofa_context->GetRuntimeOptions().boot_class_path_locations;
764
765 if (oat_checksums.empty() || oat_boot_class_path.empty()) {
766 *error_msg = oat_checksums.empty() ? "Empty checksums" : "Empty boot class path";
767 return false;
768 }
769
770 size_t oat_bcp_size = gc::space::ImageSpace::CheckAndCountBCPComponents(
771 oat_boot_class_path, ArrayRef<const std::string>(bcp_locations), error_msg);
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100772 if (oat_bcp_size == static_cast<size_t>(-1)) {
773 DCHECK(!error_msg->empty());
774 return false;
775 }
Theodore Duboisb7a86cb2022-10-25 17:42:56 -0700776 DCHECK_LE(oat_bcp_size, bcp_locations.size());
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100777
778 size_t bcp_index = 0;
779 size_t boot_image_index = 0;
780 bool found_d = false;
781
782 while (bcp_index < oat_bcp_size) {
783 static_assert(gc::space::ImageSpace::kImageChecksumPrefix == 'i', "Format prefix check");
784 static_assert(gc::space::ImageSpace::kDexFileChecksumPrefix == 'd', "Format prefix check");
785 if (StartsWith(oat_checksums, "i") && !found_d) {
786 const std::vector<OatFileAssistantContext::BootImageInfo>& boot_image_info_list =
787 ofa_context->GetBootImageInfoList(isa);
788 if (boot_image_index >= boot_image_info_list.size()) {
789 *error_msg = StringPrintf("Missing boot image for %s, remaining checksums: %s",
790 bcp_locations[bcp_index].c_str(),
791 std::string(oat_checksums).c_str());
792 return false;
793 }
794
795 const OatFileAssistantContext::BootImageInfo& boot_image_info =
796 boot_image_info_list[boot_image_index];
797 if (!ConsumePrefix(&oat_checksums, boot_image_info.checksum)) {
798 *error_msg = StringPrintf("Image checksum mismatch, expected %s to start with %s",
799 std::string(oat_checksums).c_str(),
800 boot_image_info.checksum.c_str());
801 return false;
802 }
803
804 bcp_index += boot_image_info.component_count;
805 boot_image_index++;
806 } else if (StartsWith(oat_checksums, "d")) {
807 found_d = true;
808 const std::vector<std::string>* bcp_checksums =
809 ofa_context->GetBcpChecksums(bcp_index, error_msg);
810 if (bcp_checksums == nullptr) {
811 return false;
812 }
813 oat_checksums.remove_prefix(1u);
814 for (const std::string& checksum : *bcp_checksums) {
815 if (!ConsumePrefix(&oat_checksums, checksum)) {
816 *error_msg = StringPrintf(
817 "Dex checksum mismatch for bootclasspath file %s, expected %s to start with %s",
818 bcp_locations[bcp_index].c_str(),
819 std::string(oat_checksums).c_str(),
820 checksum.c_str());
821 return false;
822 }
823 }
824
825 bcp_index++;
826 } else {
827 *error_msg = StringPrintf("Unexpected checksums, expected %s to start with %s",
828 std::string(oat_checksums).c_str(),
829 found_d ? "'d'" : "'i' or 'd'");
830 return false;
831 }
832
833 if (bcp_index < oat_bcp_size) {
834 if (!ConsumePrefix(&oat_checksums, ":")) {
835 if (oat_checksums.empty()) {
836 *error_msg =
837 StringPrintf("Checksum too short, missing %zu components", oat_bcp_size - bcp_index);
838 } else {
839 *error_msg = StringPrintf("Missing ':' separator at start of %s",
840 std::string(oat_checksums).c_str());
841 }
842 return false;
843 }
844 }
845 }
846
847 if (!oat_checksums.empty()) {
848 *error_msg =
849 StringPrintf("Checksum too long, unexpected tail: %s", std::string(oat_checksums).c_str());
850 return false;
851 }
852
853 return true;
854}
855
Vladimir Markobcd99be2019-03-22 16:21:31 +0000856bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100857 // Get the checksums and the BCP from the oat file.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000858 const char* oat_boot_class_path_checksums =
859 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100860 const char* oat_boot_class_path =
861 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
862 if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) {
Vladimir Markof3d88a82018-12-21 16:38:47 +0000863 return false;
864 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000865
Vladimir Marko436c6f52019-07-25 14:50:14 +0100866 std::string error_msg;
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100867 bool result = ValidateBootClassPathChecksums(GetOatFileAssistantContext(),
868 isa_,
869 oat_boot_class_path_checksums,
870 oat_boot_class_path,
871 &error_msg);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100872 if (!result) {
873 VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation()
874 << " error: " << error_msg;
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100875 return false;
Vladimir Marko436c6f52019-07-25 14:50:14 +0100876 }
877
Vladimir Marko436c6f52019-07-25 14:50:14 +0100878 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800879}
880
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100881bool OatFileAssistant::IsPrimaryBootImageUsable() {
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +0100882 return !GetOatFileAssistantContext()->GetBootImageInfoList(isa_).empty();
Jiakai Zhang78d86b52022-05-26 17:24:17 +0100883}
884
Richard Uhler88bc6732016-11-14 14:38:03 +0000885OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700886 ScopedTrace trace("GetBestInfo");
Calin Juravle357c66d2017-05-04 01:57:17 +0000887 // TODO(calin): Document the side effects of class loading when
888 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700889 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000890 // If the parent of the dex file is writable it means that we can
891 // create the odex file. In this case we unconditionally pick the odex
892 // as the best oat file. This corresponds to the regular use case when
893 // apps gets installed or when they load private, secondary dex file.
894 // For apps on the system partition the odex location will not be
895 // writable and thus the oat location might be more up to date.
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000896
897 // If the odex is not useable, and we have a useable vdex, return the vdex
898 // instead.
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100899 if (!odex_.IsUseable()) {
900 if (vdex_for_odex_.IsUseable()) {
901 return vdex_for_odex_;
902 } else if (dm_for_odex_.IsUseable()) {
903 return dm_for_odex_;
904 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000905 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000906 return odex_;
907 }
908
909 // We cannot write to the odex location. This must be a system app.
910
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000911 // If the oat location is useable take it.
Calin Juravle357c66d2017-05-04 01:57:17 +0000912 if (oat_.IsUseable()) {
913 return oat_;
914 }
915
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000916 // The oat file is not useable but the odex file might be up to date.
Calin Juravle357c66d2017-05-04 01:57:17 +0000917 // This is an indication that we are dealing with an up to date prebuilt
918 // (that doesn't need relocation).
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000919 if (odex_.IsUseable()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000920 return odex_;
921 }
922
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000923 // Look for a useable vdex file.
924 if (vdex_for_oat_.IsUseable()) {
925 return vdex_for_oat_;
926 }
927 if (vdex_for_odex_.IsUseable()) {
928 return vdex_for_odex_;
929 }
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100930 if (dm_for_oat_.IsUseable()) {
931 return dm_for_oat_;
932 }
933 if (dm_for_odex_.IsUseable()) {
934 return dm_for_odex_;
935 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000936
Calin Juravle357c66d2017-05-04 01:57:17 +0000937 // We got into the worst situation here:
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000938 // - the oat location is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000939 // - the prebuild odex location is not up to date
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000940 // - the vdex-only file is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000941 // - and we don't have the original dex file anymore (stripped).
942 // Pick the odex if it exists, or the oat if not.
943 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000944}
945
Andreas Gampea463b6a2016-08-12 21:53:32 -0700946std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800947 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100948 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800949 if (art_file.empty()) {
950 return nullptr;
951 }
952 std::string error_msg;
953 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700954 std::unique_ptr<gc::space::ImageSpace> ret =
955 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800956 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800957 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
958 }
959 return ret;
960}
961
Richard Uhler88bc6732016-11-14 14:38:03 +0000962OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
963 bool is_oat_location)
964 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700965{}
966
Richard Uhler88bc6732016-11-14 14:38:03 +0000967bool OatFileAssistant::OatFileInfo::IsOatLocation() {
968 return is_oat_location_;
969}
970
Richard Uhler743bf362016-04-19 15:39:37 -0700971const std::string* OatFileAssistant::OatFileInfo::Filename() {
972 return filename_provided_ ? &filename_ : nullptr;
973}
974
Richard Uhler03bc6592016-11-22 09:42:04 +0000975bool OatFileAssistant::OatFileInfo::IsUseable() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700976 ScopedTrace trace("IsUseable");
Richard Uhler03bc6592016-11-22 09:42:04 +0000977 switch (Status()) {
978 case kOatCannotOpen:
979 case kOatDexOutOfDate:
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000980 case kOatContextOutOfDate:
Richard Uhler03bc6592016-11-22 09:42:04 +0000981 case kOatBootImageOutOfDate: return false;
982
Richard Uhler03bc6592016-11-22 09:42:04 +0000983 case kOatUpToDate: return true;
984 }
985 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700986}
987
988OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700989 ScopedTrace trace("Status");
Richard Uhler743bf362016-04-19 15:39:37 -0700990 if (!status_attempted_) {
991 status_attempted_ = true;
992 const OatFile* file = GetFile();
993 if (file == nullptr) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000994 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700995 } else {
996 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700997 VLOG(oat) << file->GetLocation() << " is " << status_
998 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700999 }
1000 }
1001 return status_;
1002}
1003
Richard Uhler70a84262016-11-08 16:51:51 +00001004OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001005 CompilerFilter::Filter target_compiler_filter, const DexOptTrigger dexopt_trigger) {
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001006 if (IsUseable()) {
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001007 return ShouldRecompileForFilter(target_compiler_filter, dexopt_trigger) ? kDex2OatForFilter :
1008 kNoDexOptNeeded;
1009 }
1010
1011 // In this case, the oat file is not usable. If the caller doesn't seek for a better compiler
1012 // filter (e.g., the caller wants to downgrade), then we should not recompile.
1013 if (!dexopt_trigger.targetFilterIsBetter) {
1014 return kNoDexOptNeeded;
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001015 }
Richard Uhler70a84262016-11-08 16:51:51 +00001016
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001017 if (Status() == kOatBootImageOutOfDate) {
1018 return kDex2OatForBootImage;
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001019 }
1020
Jiakai Zhangc372e792022-11-23 10:57:22 +00001021 std::string error_msg;
1022 std::optional<bool> has_dex_files = oat_file_assistant_->HasDexFiles(&error_msg);
1023 if (has_dex_files.has_value()) {
1024 if (*has_dex_files) {
1025 return kDex2OatFromScratch;
1026 } else {
1027 // No dex file, so there is nothing we need to do.
1028 return kNoDexOptNeeded;
1029 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001030 } else {
Jiakai Zhangc372e792022-11-23 10:57:22 +00001031 // Unable to open the dex file, so there is nothing we can do.
1032 LOG(WARNING) << error_msg;
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001033 return kNoDexOptNeeded;
1034 }
Richard Uhler70a84262016-11-08 16:51:51 +00001035}
1036
Richard Uhler743bf362016-04-19 15:39:37 -07001037const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1038 CHECK(!file_released_) << "GetFile called after oat file released.";
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001039 if (load_attempted_) {
1040 return file_.get();
1041 }
1042 load_attempted_ = true;
1043 if (!filename_provided_) {
1044 return nullptr;
1045 }
1046
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001047 if (LocationIsOnArtApexData(filename_) &&
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +01001048 oat_file_assistant_->GetRuntimeOptions().deny_art_apex_data_files) {
Orion Hodson971068d2021-06-30 21:17:53 +01001049 LOG(WARNING) << "OatFileAssistant rejected file " << filename_
1050 << ": ART apexdata is untrusted.";
1051 return nullptr;
1052 }
1053
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001054 std::string error_msg;
1055 bool executable = oat_file_assistant_->load_executable_;
1056 if (android::base::EndsWith(filename_, kVdexExtension)) {
1057 executable = false;
1058 // Check to see if there is a vdex file we can make use of.
1059 std::unique_ptr<VdexFile> vdex;
1060 if (use_fd_) {
1061 if (vdex_fd_ >= 0) {
1062 struct stat s;
1063 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1064 if (rc == -1) {
1065 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1066 } else {
1067 vdex = VdexFile::Open(vdex_fd_,
1068 s.st_size,
1069 filename_,
1070 /*writable=*/ false,
1071 /*low_4gb=*/ false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001072 &error_msg);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001073 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001074 }
1075 } else {
1076 vdex = VdexFile::Open(filename_,
1077 /*writable=*/ false,
1078 /*low_4gb=*/ false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001079 &error_msg);
1080 }
1081 if (vdex == nullptr) {
1082 VLOG(oat) << "unable to open vdex file " << filename_ << ": " << error_msg;
1083 } else {
1084 file_.reset(OatFile::OpenFromVdex(zip_fd_,
1085 std::move(vdex),
1086 oat_file_assistant_->dex_location_,
Nicolas Geoffray0f6af5e2023-01-30 14:29:11 +00001087 oat_file_assistant_->context_,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001088 &error_msg));
1089 }
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +01001090 } else if (android::base::EndsWith(filename_, kDmExtension)) {
1091 executable = false;
1092 // Check to see if there is a vdex file we can make use of.
1093 std::unique_ptr<ZipArchive> dm_file(ZipArchive::Open(filename_.c_str(), &error_msg));
1094 if (dm_file != nullptr) {
1095 std::unique_ptr<VdexFile> vdex(VdexFile::OpenFromDm(filename_, *dm_file));
1096 if (vdex != nullptr) {
1097 file_.reset(OatFile::OpenFromVdex(zip_fd_,
1098 std::move(vdex),
1099 oat_file_assistant_->dex_location_,
Nicolas Geoffray0f6af5e2023-01-30 14:29:11 +00001100 oat_file_assistant_->context_,
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +01001101 &error_msg));
1102 }
1103 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001104 } else {
Orion Hodson094b1cf2021-06-08 09:28:28 +01001105 if (executable && oat_file_assistant_->only_load_trusted_executable_) {
Orion Hodson971068d2021-06-30 21:17:53 +01001106 executable = LocationIsTrusted(filename_, /*trust_art_apex_data_files=*/ true);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001107 }
1108 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
1109 if (use_fd_) {
1110 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1111 ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_,
1112 /*size=*/ 1u);
1113 file_.reset(OatFile::Open(zip_fd_,
1114 vdex_fd_,
1115 oat_fd_,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001116 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +00001117 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001118 /*low_4gb=*/ false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001119 dex_locations,
Victor Hsiehce9b9022021-07-21 10:44:06 -07001120 /*dex_fds=*/ ArrayRef<const int>(),
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001121 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001122 &error_msg));
1123 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001124 } else {
1125 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
1126 filename_.c_str(),
1127 filename_.c_str(),
1128 executable,
1129 /*low_4gb=*/ false,
1130 oat_file_assistant_->dex_location_,
1131 &error_msg));
Richard Uhler743bf362016-04-19 15:39:37 -07001132 }
1133 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001134 if (file_.get() == nullptr) {
1135 VLOG(oat) << "OatFileAssistant test for existing oat file "
1136 << filename_
1137 << ": " << error_msg;
1138 } else {
1139 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
1140 }
Richard Uhler743bf362016-04-19 15:39:37 -07001141 return file_.get();
1142}
1143
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001144bool OatFileAssistant::OatFileInfo::ShouldRecompileForFilter(CompilerFilter::Filter target,
1145 const DexOptTrigger dexopt_trigger) {
Richard Uhler743bf362016-04-19 15:39:37 -07001146 const OatFile* file = GetFile();
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001147 DCHECK(file != nullptr);
Richard Uhler743bf362016-04-19 15:39:37 -07001148
1149 CompilerFilter::Filter current = file->GetCompilerFilter();
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001150 if (dexopt_trigger.targetFilterIsBetter && CompilerFilter::IsBetter(target, current)) {
1151 return true;
1152 }
1153 if (dexopt_trigger.targetFilterIsSame && current == target) {
1154 return true;
1155 }
1156 if (dexopt_trigger.targetFilterIsWorse && CompilerFilter::IsBetter(current, target)) {
1157 return true;
Richard Uhler743bf362016-04-19 15:39:37 -07001158 }
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001159
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001160 if (dexopt_trigger.primaryBootImageBecomesUsable &&
1161 CompilerFilter::DependsOnImageChecksum(current)) {
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001162 // If the oat file has been compiled without an image, and the runtime is
1163 // now running with an image loaded from disk, return that we need to
1164 // re-compile. The recompilation will generate a better oat file, and with an app
1165 // image for profile guided compilation.
1166 const char* oat_boot_class_path_checksums =
1167 file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
1168 if (oat_boot_class_path_checksums != nullptr &&
1169 !StartsWith(oat_boot_class_path_checksums, "i") &&
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001170 oat_file_assistant_->IsPrimaryBootImageUsable()) {
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001171 DCHECK(!file->GetOatHeader().RequiresImage());
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001172 return true;
Nicolas Geoffraye7e026b2022-03-24 16:42:07 +00001173 }
1174 }
1175
Jiakai Zhangdbfa7992022-06-24 23:14:24 +01001176 return false;
Richard Uhler743bf362016-04-19 15:39:37 -07001177}
1178
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001179bool OatFileAssistant::ClassLoaderContextIsOkay(const OatFile& oat_file) const {
Jiakai Zhangb4593412022-10-28 11:50:26 +01001180 if (context_ == nullptr) {
1181 // The caller requests to skip the check.
1182 return true;
1183 }
1184
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001185 if (oat_file.IsBackedByVdexOnly()) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +00001186 // Only a vdex file, we don't depend on the class loader context.
1187 return true;
1188 }
1189
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001190 if (!CompilerFilter::IsVerificationEnabled(oat_file.GetCompilerFilter())) {
Calin Juravleaf322422020-02-11 13:45:53 -08001191 // If verification is not enabled we don't need to verify the class loader context and we
1192 // assume it's ok.
1193 return true;
1194 }
1195
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001196 ClassLoaderContext::VerificationResult matches = context_->VerifyClassLoaderContextMatch(
1197 oat_file.GetClassLoaderContext(),
1198 /*verify_names=*/ true,
1199 /*verify_checksums=*/ true);
1200 if (matches == ClassLoaderContext::VerificationResult::kMismatch) {
David Srbecky656fdcd2021-04-17 16:47:01 +00001201 VLOG(oat) << "ClassLoaderContext check failed. Context was "
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001202 << oat_file.GetClassLoaderContext()
1203 << ". The expected context is "
1204 << context_->EncodeContextForOatFile(android::base::Dirname(dex_location_));
1205 return false;
David Srbecky656fdcd2021-04-17 16:47:01 +00001206 }
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001207 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001208}
1209
Richard Uhler743bf362016-04-19 15:39:37 -07001210bool OatFileAssistant::OatFileInfo::IsExecutable() {
1211 const OatFile* file = GetFile();
1212 return (file != nullptr && file->IsExecutable());
1213}
1214
Richard Uhler743bf362016-04-19 15:39:37 -07001215void OatFileAssistant::OatFileInfo::Reset() {
1216 load_attempted_ = false;
1217 file_.reset();
1218 status_attempted_ = false;
1219}
1220
Nicolas Geoffray30025092018-04-19 14:43:29 +01001221void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
1222 bool use_fd,
1223 int zip_fd,
1224 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001225 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001226 filename_provided_ = true;
1227 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001228 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +01001229 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001230 vdex_fd_ = vdex_fd;
1231 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001232 Reset();
1233}
1234
1235std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1236 file_released_ = true;
1237 return std::move(file_);
1238}
1239
Richard Uhler70a84262016-11-08 16:51:51 +00001240std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -07001241 ScopedTrace trace("ReleaseFileForUse");
Richard Uhler3e580bc2016-11-08 16:23:07 +00001242 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001243 return ReleaseFile();
1244 }
1245
Richard Uhler70a84262016-11-08 16:51:51 +00001246 return std::unique_ptr<OatFile>();
1247}
Calin Juravle5f9a8012018-02-12 20:27:46 -08001248
1249// TODO(calin): we could provide a more refined status here
1250// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
1251// track more experiments but adds extra complexity.
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001252void OatFileAssistant::GetOptimizationStatus(const std::string& filename,
1253 InstructionSet isa,
1254 std::string* out_compilation_filter,
1255 std::string* out_compilation_reason,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +01001256 OatFileAssistantContext* ofa_context) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001257 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
1258 // non-executable and check the status manually.
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001259 OatFileAssistant oat_file_assistant(filename.c_str(),
1260 isa,
Jiakai Zhang78d86b52022-05-26 17:24:17 +01001261 /*context=*/nullptr,
1262 /*load_executable=*/false,
1263 /*only_load_trusted_executable=*/false,
Jiakai Zhang7a5a95f2022-08-07 01:12:24 +01001264 ofa_context);
Calin Juravle98071152021-01-27 18:41:58 -08001265 std::string out_odex_location; // unused
1266 std::string out_odex_status; // unused
1267 oat_file_assistant.GetOptimizationStatus(
1268 &out_odex_location,
1269 out_compilation_filter,
1270 out_compilation_reason,
1271 &out_odex_status);
1272}
1273
1274void OatFileAssistant::GetOptimizationStatus(
1275 std::string* out_odex_location,
1276 std::string* out_compilation_filter,
1277 std::string* out_compilation_reason,
1278 std::string* out_odex_status) {
1279 OatFileInfo& oat_file_info = GetBestInfo();
1280 const OatFile* oat_file = GetBestInfo().GetFile();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001281
1282 if (oat_file == nullptr) {
Calin Juravle98071152021-01-27 18:41:58 -08001283 *out_odex_location = "error";
Calin Juravle5f9a8012018-02-12 20:27:46 -08001284 *out_compilation_filter = "run-from-apk";
1285 *out_compilation_reason = "unknown";
Calin Juravle98071152021-01-27 18:41:58 -08001286 // This mostly happens when we cannot open the oat file.
1287 // Note that it's different than kOatCannotOpen.
1288 // TODO: The design of getting the BestInfo is not ideal,
1289 // as it's not very clear what's the difference between
1290 // a nullptr and kOatcannotOpen. The logic should be revised
1291 // and improved.
1292 *out_odex_status = "io-error-no-oat";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001293 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001294 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001295
Calin Juravle98071152021-01-27 18:41:58 -08001296 *out_odex_location = oat_file->GetLocation();
1297 OatStatus status = oat_file_info.Status();
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001298 const char* reason = oat_file->GetCompilationReason();
1299 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +01001300
1301 // If the oat file is invalid, the vdex file will be picked, so the status is `kOatUpToDate`. If
1302 // the vdex file is also invalid, then either `oat_file` is nullptr, or `status` is
1303 // `kOatDexOutOfDate`.
1304 DCHECK(status == kOatUpToDate || status == kOatDexOutOfDate);
1305
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001306 switch (status) {
Calin Juravle98071152021-01-27 18:41:58 -08001307 case kOatUpToDate:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001308 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
Calin Juravle98071152021-01-27 18:41:58 -08001309 *out_odex_status = "up-to-date";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001310 return;
1311
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +01001312 case kOatCannotOpen:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001313 case kOatBootImageOutOfDate:
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001314 case kOatContextOutOfDate:
Jiakai Zhang6c18fbd2022-06-24 19:01:45 +01001315 // These should never happen, but be robust.
1316 *out_compilation_filter = "unexpected";
1317 *out_compilation_reason = "unexpected";
1318 *out_odex_status = "unexpected";
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001319 return;
1320
Calin Juravle98071152021-01-27 18:41:58 -08001321 case kOatDexOutOfDate:
1322 *out_compilation_filter = "run-from-apk-fallback";
1323 *out_odex_status = "apk-more-recent";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001324 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001325 }
1326 LOG(FATAL) << "Unreachable";
1327 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001328}
1329
Richard Uhler66d874d2015-01-15 09:37:19 -08001330} // namespace art