blob: c74f19bab579c0455afea4f197183f7253613805 [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_assistant.h"
18
Richard Uhler46cc64f2016-11-14 14:53:55 +000019#include <sstream>
20
Richard Uhler66d874d2015-01-15 09:37:19 -080021#include <sys/stat.h>
David Brazdil35a3f6a2019-03-04 15:59:06 +000022#include "zlib.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080023
Nicolas Geoffray525fa422021-04-19 07:50:35 +000024#include "android-base/file.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070025#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080026#include "android-base/strings.h"
27
Eric Holkc7ac91b2021-02-04 21:44:01 +000028#include "base/compiler_filter.h"
David Sehr891a50e2017-10-27 17:01:07 -070029#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080030#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070031#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080032#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070033#include "base/stl_util.h"
Vladimir Markobcd99be2019-03-22 16:21:31 +000034#include "base/string_view_cpp20.h"
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -070035#include "base/systrace.h"
David Sehrc431b9d2018-03-02 12:01:51 -080036#include "base/utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "class_linker.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000038#include "class_loader_context.h"
David Sehr013fd802018-01-11 22:55:24 -080039#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080040#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080041#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080042#include "gc/heap.h"
43#include "gc/space/image_space.h"
44#include "image.h"
45#include "oat.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080046#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070047#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000048#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080049
50namespace art {
51
Richard Uhler69bcf2c2017-01-24 10:25:21 +000052using android::base::StringPrintf;
53
David Brazdil35a3f6a2019-03-04 15:59:06 +000054static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@";
55static constexpr const char* kVdexExtension = ".vdex";
56
Narayan Kamath8943c1d2016-05-02 13:14:48 +010057std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
58 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000059 case OatFileAssistant::kOatCannotOpen:
60 stream << "kOatCannotOpen";
61 break;
62 case OatFileAssistant::kOatDexOutOfDate:
63 stream << "kOatDexOutOfDate";
64 break;
65 case OatFileAssistant::kOatBootImageOutOfDate:
66 stream << "kOatBootImageOutOfDate";
67 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010068 case OatFileAssistant::kOatUpToDate:
69 stream << "kOatUpToDate";
70 break;
Nicolas Geoffray525fa422021-04-19 07:50:35 +000071 case OatFileAssistant::kOatContextOutOfDate:
72 stream << "kOaContextOutOfDate";
73 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010074 }
75
76 return stream;
77}
78
Richard Uhler66d874d2015-01-15 09:37:19 -080079OatFileAssistant::OatFileAssistant(const char* dex_location,
80 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +000081 ClassLoaderContext* context,
Nicolas Geoffray29742602017-12-14 10:09:03 +000082 bool load_executable,
83 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070084 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000085 isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +000086 context,
Nicolas Geoffray29742602017-12-14 10:09:03 +000087 load_executable,
88 only_load_system_executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010089 /*vdex_fd=*/ -1,
90 /*oat_fd=*/ -1,
91 /*zip_fd=*/ -1) {}
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070092
93
94OatFileAssistant::OatFileAssistant(const char* dex_location,
95 const InstructionSet isa,
Nicolas Geoffray525fa422021-04-19 07:50:35 +000096 ClassLoaderContext* context,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070097 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000098 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070099 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700100 int oat_fd,
101 int zip_fd)
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000102 : context_(context),
103 isa_(isa),
Richard Uhler88bc6732016-11-14 14:38:03 +0000104 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000105 only_load_system_executable_(only_load_system_executable),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700106 odex_(this, /*is_oat_location=*/ false),
107 oat_(this, /*is_oat_location=*/ true),
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000108 vdex_for_odex_(this, /*is_oat_location=*/ false),
109 vdex_for_oat_(this, /*is_oat_location=*/ true),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700110 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -0700111 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000112 CHECK(!load_executable || context != nullptr) << "Loading executable without a context";
Calin Juravle357c66d2017-05-04 01:57:17 +0000113
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700114 if (zip_fd < 0) {
115 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
116 << " oat_fd=" << oat_fd;
117 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
118 << " vdex_fd=" << vdex_fd;;
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000119 CHECK(!UseFdToReadFiles());
120 } else {
121 CHECK(UseFdToReadFiles());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700122 }
123
Calin Juravle099f8d62017-09-05 19:04:04 -0700124 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700125
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100126 if (load_executable_ && isa != kRuntimeISA) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800127 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100128 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800129 load_executable_ = false;
130 }
131
Richard Uhler743bf362016-04-19 15:39:37 -0700132 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700133 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700134 std::string odex_file_name;
135 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100136 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000137 std::string vdex_file_name = GetVdexFilename(odex_file_name);
Nicolas Geoffrayee1c9612021-03-02 13:43:20 +0000138 // We dup FDs as the odex_ will claim ownership.
139 vdex_for_odex_.Reset(vdex_file_name,
140 UseFdToReadFiles(),
141 DupCloexec(zip_fd),
142 DupCloexec(vdex_fd),
143 DupCloexec(oat_fd));
Richard Uhler743bf362016-04-19 15:39:37 -0700144 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700145 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
146 }
147
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700148 if (!UseFdToReadFiles()) {
149 // Get the oat filename.
150 std::string oat_file_name;
151 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100152 oat_.Reset(oat_file_name, /*use_fd=*/ false);
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000153 std::string vdex_file_name = GetVdexFilename(oat_file_name);
154 vdex_for_oat_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700155 } else {
156 LOG(WARNING) << "Failed to determine oat file name for dex location "
157 << dex_location_ << ": " << error_msg;
158 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000159 }
160
161 // Check if the dex directory is writable.
162 // This will be needed in most uses of OatFileAssistant and so it's OK to
163 // compute it eagerly. (the only use which will not make use of it is
164 // OatFileAssistant::GetStatusDump())
165 size_t pos = dex_location_.rfind('/');
166 if (pos == std::string::npos) {
167 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700168 } else if (!UseFdToReadFiles()) {
169 // We cannot test for parent access when using file descriptors. That's ok
170 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000171 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700172 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000173 dex_parent_writable_ = true;
174 } else {
175 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700176 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800177 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800178}
179
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700180bool OatFileAssistant::UseFdToReadFiles() {
181 return zip_fd_ >= 0;
182}
183
Richard Uhler66d874d2015-01-15 09:37:19 -0800184bool OatFileAssistant::IsInBootClassPath() {
185 // Note: We check the current boot class path, regardless of the ISA
186 // specified by the user. This is okay, because the boot class path should
187 // be the same for all ISAs.
188 // TODO: Can we verify the boot class path is the same for all ISAs?
189 Runtime* runtime = Runtime::Current();
190 ClassLinker* class_linker = runtime->GetClassLinker();
191 const auto& boot_class_path = class_linker->GetBootClassPath();
192 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700193 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800194 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
195 return true;
196 }
197 }
198 return false;
199}
200
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700201int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000202 bool profile_changed,
203 bool downgrade) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000204 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700205 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000206 profile_changed,
207 downgrade);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000208 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
209 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800210 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000211 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800212}
213
Richard Uhler01be6812016-05-17 10:34:52 -0700214bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000215 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700216}
217
Richard Uhler66d874d2015-01-15 09:37:19 -0800218std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000219 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800220}
221
Richard Uhler46cc64f2016-11-14 14:53:55 +0000222std::string OatFileAssistant::GetStatusDump() {
223 std::ostringstream status;
224 bool oat_file_exists = false;
225 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000226 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000227 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000228 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000229
Richard Uhler46cc64f2016-11-14 14:53:55 +0000230 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000231 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
232 const OatFile* file = oat_.GetFile();
233 if (file == nullptr) {
234 // If the file is null even though the status is not kOatCannotOpen, it
235 // means we must have a vdex file with no corresponding oat file. In
236 // this case we cannot determine the compilation filter. Indicate that
237 // we have only the vdex file instead.
238 status << "vdex-only";
239 } else {
240 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
241 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000242 }
243
Richard Uhler03bc6592016-11-22 09:42:04 +0000244 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000245 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000246 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000247
Richard Uhler46cc64f2016-11-14 14:53:55 +0000248 odex_file_exists = true;
249 if (oat_file_exists) {
250 status << "] ";
251 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000252 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
253 const OatFile* file = odex_.GetFile();
254 if (file == nullptr) {
255 status << "vdex-only";
256 } else {
257 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
258 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000259 }
260
261 if (!oat_file_exists && !odex_file_exists) {
262 status << "invalid[";
263 }
264
265 status << "]";
266 return status.str();
267}
268
Richard Uhler66d874d2015-01-15 09:37:19 -0800269std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700270 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700272 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
273 return dex_files;
274 } else {
275 return std::vector<std::unique_ptr<const DexFile>>();
276 }
277}
Richard Uhler66d874d2015-01-15 09:37:19 -0800278
Calin Juravle87e2cb62017-06-13 21:48:45 -0700279bool OatFileAssistant::LoadDexFiles(
280 const OatFile &oat_file,
281 const std::string& dex_location,
282 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000283 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800284 std::string error_msg;
Andreas Gampeb40d3612018-06-26 15:49:42 -0700285 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700286 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700288 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700289 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800290 }
291
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700292 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800293 if (dex_file.get() == nullptr) {
294 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700295 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700297 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800298
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000299 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700300 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700301 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000302 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700303 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000304 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800305 break;
306 }
307
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700308 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800309 if (dex_file.get() == nullptr) {
310 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700311 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800312 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700313 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800314 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700315 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800316}
317
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100318bool OatFileAssistant::HasDexFiles() {
319 ScopedTrace trace("HasDexFiles");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000320 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700321 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000322 // GetRequiredDexChecksums.
323 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700324 return has_original_dex_files_;
325}
326
Richard Uhler95abd042015-03-24 09:51:28 -0700327OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700328 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800329}
330
Richard Uhler95abd042015-03-24 09:51:28 -0700331OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700332 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800333}
334
Richard Uhler2f27abd2017-01-31 14:02:34 +0000335bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700336 ScopedTrace trace("DexChecksumUpToDate(vdex)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000337 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
338 if (required_dex_checksums == nullptr) {
339 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
340 return true;
341 }
342
Nicolas Geoffraya129d8a2021-03-18 22:23:04 +0000343 uint32_t number_of_dex_files = file.GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000344 if (required_dex_checksums->size() != number_of_dex_files) {
345 *error_msg = StringPrintf("expected %zu dex files but found %u",
346 required_dex_checksums->size(),
347 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000348 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800349 }
350
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000351 for (uint32_t i = 0; i < number_of_dex_files; i++) {
352 uint32_t expected_checksum = (*required_dex_checksums)[i];
353 uint32_t actual_checksum = file.GetLocationChecksum(i);
354 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700355 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000356 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
357 "Expected: %u, actual: %u",
358 dex.c_str(),
359 expected_checksum,
360 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000361 return false;
362 }
363 }
364
Richard Uhler2f27abd2017-01-31 14:02:34 +0000365 return true;
366}
367
368bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700369 ScopedTrace trace("DexChecksumUpToDate(oat)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000370 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
371 if (required_dex_checksums == nullptr) {
372 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
373 return true;
374 }
375
376 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
377 if (required_dex_checksums->size() != number_of_dex_files) {
378 *error_msg = StringPrintf("expected %zu dex files but found %u",
379 required_dex_checksums->size(),
380 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000381 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800382 }
383
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000384 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700385 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000386 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700387 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000388 if (oat_dex_file == nullptr) {
389 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
390 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800391 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000392 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
393 if (expected_checksum != actual_checksum) {
394 VLOG(oat) << "Dex checksum does not match for dex: " << dex
395 << ". Expected: " << expected_checksum
396 << ", Actual: " << actual_checksum;
397 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800398 }
399 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000400 return true;
401}
402
Nicolas Geoffrayd3c45c22021-04-27 13:21:28 +0100403static bool ValidateApexVersions(const OatFile& oat_file) {
404 const char* oat_apex_versions =
405 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kApexVersionsKey);
406 if (oat_apex_versions == nullptr) {
407 return false;
408 }
409 // Some dex files get compiled with a subset of the boot classpath (for
410 // example currently system server is compiled with DEX2OAT_BOOTCLASSPATH).
411 // For such cases, the oat apex versions will be a prefix of the runtime apex
412 // versions.
413 return android::base::StartsWith(Runtime::Current()->GetApexVersions(), oat_apex_versions);
414}
415
Richard Uhler2f27abd2017-01-31 14:02:34 +0000416OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
417 // Verify the ART_USE_READ_BARRIER state.
418 // TODO: Don't fully reject files due to read barrier state. If they contain
419 // compiled code and are otherwise okay, we should return something like
420 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
421 // barrier state doesn't matter.
422 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
423 constexpr bool kRuntimeIsCC = kUseReadBarrier;
424 if (is_cc != kRuntimeIsCC) {
425 return kOatCannotOpen;
426 }
427
428 // Verify the dex checksum.
429 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000430 VdexFile* vdex = file.GetVdexFile();
431 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
432 LOG(ERROR) << error_msg;
433 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000434 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800435
Andreas Gampe29d38e72016-03-23 15:31:51 +0000436 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000437
Richard Uhler66d874d2015-01-15 09:37:19 -0800438 // Verify the image checksum
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000439 if (file.IsBackedByVdexOnly()) {
440 VLOG(oat) << "Image checksum test skipped for vdex file " << file.GetLocation();
441 } else if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000442 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000443 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000444 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000445 }
Nicolas Geoffrayd3c45c22021-04-27 13:21:28 +0100446 if (!ValidateApexVersions(file)) {
447 VLOG(oat) << "Apex versions do not match.";
448 return kOatBootImageOutOfDate;
449 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000450 } else {
451 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800452 }
453
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000454 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
455 DCHECK(required_dex_checksums_attempted_);
456 if (only_load_system_executable_ &&
457 !LocationIsOnSystem(file.GetLocation().c_str()) &&
458 file.ContainsDexCode() &&
459 zip_file_only_contains_uncompressed_dex_) {
460 LOG(ERROR) << "Not loading "
461 << dex_location_
462 << ": oat file has dex code, but APK has uncompressed dex code";
463 return kOatDexOutOfDate;
464 }
465
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000466 if (!ClassLoaderContextIsOkay(file)) {
467 return kOatContextOutOfDate;
468 }
469
Richard Uhlere8109f72016-04-18 10:40:50 -0700470 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800471}
472
David Brazdil35a3f6a2019-03-04 15:59:06 +0000473bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
474 InstructionSet isa,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000475 /* out */ std::string* dex_location,
476 /* out */ std::string* vdex_filename) {
477 uint32_t checksum = adler32(0L, Z_NULL, 0);
478 for (const DexFile::Header* header : headers) {
479 checksum = adler32_combine(checksum,
480 header->checksum_,
481 header->file_size_ - DexFile::kNumNonChecksumBytes);
482 }
David Brazdil35a3f6a2019-03-04 15:59:06 +0000483
484 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
485 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
486 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
487 return false;
488 }
489 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
490
491 std::string odex_filename;
492 std::string error_msg;
493 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
494 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
495 return false;
496 }
497
498 *vdex_filename = GetVdexFilename(odex_filename);
499 return true;
500}
501
502bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
503 DCHECK(basename.find('/') == std::string::npos);
504 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
505 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
506 !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) ||
507 !android::base::EndsWith(basename, kVdexExtension)) {
508 return false;
509 }
510 // Check that all characters between the prefix and extension are decimal digits.
511 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
512 if (!std::isdigit(basename[i])) {
513 return false;
514 }
515 }
516 return true;
517}
518
Orion Hodsonb6f88572021-02-10 13:59:18 +0000519bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
520 InstructionSet isa,
521 std::string* odex_filename,
522 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000523 CHECK(odex_filename != nullptr);
524 CHECK(error_msg != nullptr);
525
526 // The odex file name is formed by replacing the dex_location extension with
527 // .odex and inserting an oat/<isa> directory. For example:
528 // location = /foo/bar/baz.jar
529 // odex_location = /foo/bar/oat/<isa>/baz.odex
530
531 // Find the directory portion of the dex location and add the oat/<isa>
532 // directory.
533 size_t pos = location.rfind('/');
534 if (pos == std::string::npos) {
535 *error_msg = "Dex location " + location + " has no directory.";
536 return false;
537 }
538 std::string dir = location.substr(0, pos+1);
539 // Add the oat directory.
540 dir += "oat";
Orion Hodsonb6f88572021-02-10 13:59:18 +0000541
Calin Juravle357c66d2017-05-04 01:57:17 +0000542 // Add the isa directory
543 dir += "/" + std::string(GetInstructionSetString(isa));
Calin Juravle357c66d2017-05-04 01:57:17 +0000544
545 // Get the base part of the file without the extension.
546 std::string file = location.substr(pos+1);
547 pos = file.rfind('.');
548 if (pos == std::string::npos) {
549 *error_msg = "Dex location " + location + " has no extension.";
550 return false;
551 }
552 std::string base = file.substr(0, pos);
553
554 *odex_filename = dir + "/" + base + ".odex";
555 return true;
556}
557
Richard Uhlerb81881d2016-04-19 13:08:04 -0700558bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
559 InstructionSet isa,
560 std::string* oat_filename,
561 std::string* error_msg) {
562 CHECK(oat_filename != nullptr);
563 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800564
Orion Hodsonb6f88572021-02-10 13:59:18 +0000565 // Check if `location` could have an oat file in the ART APEX data directory. If so, and the
566 // file exists, use it.
Orion Hodsone5276da2021-02-27 18:04:41 +0000567 const std::string apex_data_file = GetApexDataOdexFilename(location, isa);
568 if (!apex_data_file.empty()) {
569 if (OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) {
570 *oat_filename = apex_data_file;
571 return true;
572 } else if (errno != ENOENT) {
573 PLOG(ERROR) << "Could not check odex file " << apex_data_file;
574 }
Orion Hodsonb6f88572021-02-10 13:59:18 +0000575 }
576
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700577 // If ANDROID_DATA is not set, return false instead of aborting.
578 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100579 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700580 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
581 return false;
582 }
583
Orion Hodsonfa81f712021-01-13 12:27:21 +0000584 std::string dalvik_cache;
585 bool have_android_data = false;
586 bool dalvik_cache_exists = false;
587 bool is_global_cache = false;
588 GetDalvikCache(GetInstructionSetString(isa),
589 /*create_if_absent=*/ true,
590 &dalvik_cache,
591 &have_android_data,
592 &dalvik_cache_exists,
593 &is_global_cache);
594 if (!dalvik_cache_exists) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700595 *error_msg = "Dalvik cache directory does not exist";
596 return false;
597 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700598
599 // TODO: The oat file assistant should be the definitive place for
600 // determining the oat file name from the dex location, not
601 // GetDalvikCacheFilename.
Orion Hodsonfa81f712021-01-13 12:27:21 +0000602 return GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800603}
604
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000605const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
606 if (!required_dex_checksums_attempted_) {
607 required_dex_checksums_attempted_ = true;
608 required_dex_checksums_found_ = false;
609 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800610 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800611 const ArtDexFileLoader dex_file_loader;
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800612 std::vector<std::string> dex_locations_ignored;
David Sehr013fd802018-01-11 22:55:24 -0800613 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
614 &cached_required_dex_checksums_,
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800615 &dex_locations_ignored,
David Sehr013fd802018-01-11 22:55:24 -0800616 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000617 zip_fd_,
618 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000619 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700620 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800621 } else {
Calin Juravle5ff23932020-12-11 18:26:14 -0800622 // The only valid case here is for APKs without dex files.
623 required_dex_checksums_found_ = false;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700624 has_original_dex_files_ = false;
Calin Juravle5ff23932020-12-11 18:26:14 -0800625 VLOG(oat) << "Could not get required checksum: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800626 }
627 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000628 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800629}
630
Vladimir Markobcd99be2019-03-22 16:21:31 +0000631bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100632 // Get the checksums and the BCP from the oat file.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000633 const char* oat_boot_class_path_checksums =
634 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100635 const char* oat_boot_class_path =
636 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
637 if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) {
Vladimir Markof3d88a82018-12-21 16:38:47 +0000638 return false;
639 }
Vladimir Marko436c6f52019-07-25 14:50:14 +0100640 std::string_view oat_boot_class_path_checksums_view(oat_boot_class_path_checksums);
641 std::string_view oat_boot_class_path_view(oat_boot_class_path);
642 if (oat_boot_class_path_view == cached_boot_class_path_ &&
643 oat_boot_class_path_checksums_view == cached_boot_class_path_checksums_) {
644 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800645 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000646
Vladimir Marko436c6f52019-07-25 14:50:14 +0100647 Runtime* runtime = Runtime::Current();
648 std::string error_msg;
649 bool result = gc::space::ImageSpace::VerifyBootClassPathChecksums(
650 oat_boot_class_path_checksums_view,
651 oat_boot_class_path_view,
652 runtime->GetImageLocation(),
653 ArrayRef<const std::string>(runtime->GetBootClassPathLocations()),
654 ArrayRef<const std::string>(runtime->GetBootClassPath()),
655 isa_,
Vladimir Marko436c6f52019-07-25 14:50:14 +0100656 &error_msg);
657 if (!result) {
658 VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation()
659 << " error: " << error_msg;
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100660 return false;
Vladimir Marko436c6f52019-07-25 14:50:14 +0100661 }
662
663 // This checksum has been validated, so save it.
664 cached_boot_class_path_ = oat_boot_class_path_view;
665 cached_boot_class_path_checksums_ = oat_boot_class_path_checksums_view;
666 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800667}
668
Richard Uhler88bc6732016-11-14 14:38:03 +0000669OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700670 ScopedTrace trace("GetBestInfo");
Calin Juravle357c66d2017-05-04 01:57:17 +0000671 // TODO(calin): Document the side effects of class loading when
672 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700673 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000674 // If the parent of the dex file is writable it means that we can
675 // create the odex file. In this case we unconditionally pick the odex
676 // as the best oat file. This corresponds to the regular use case when
677 // apps gets installed or when they load private, secondary dex file.
678 // For apps on the system partition the odex location will not be
679 // writable and thus the oat location might be more up to date.
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000680
681 // If the odex is not useable, and we have a useable vdex, return the vdex
682 // instead.
683 if (!odex_.IsUseable() && vdex_for_odex_.IsUseable()) {
684 return vdex_for_odex_;
685 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000686 return odex_;
687 }
688
689 // We cannot write to the odex location. This must be a system app.
690
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000691 // If the oat location is useable take it.
Calin Juravle357c66d2017-05-04 01:57:17 +0000692 if (oat_.IsUseable()) {
693 return oat_;
694 }
695
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000696 // The oat file is not useable but the odex file might be up to date.
Calin Juravle357c66d2017-05-04 01:57:17 +0000697 // This is an indication that we are dealing with an up to date prebuilt
698 // (that doesn't need relocation).
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000699 if (odex_.IsUseable()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000700 return odex_;
701 }
702
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000703 // Look for a useable vdex file.
704 if (vdex_for_oat_.IsUseable()) {
705 return vdex_for_oat_;
706 }
707 if (vdex_for_odex_.IsUseable()) {
708 return vdex_for_odex_;
709 }
710
Calin Juravle357c66d2017-05-04 01:57:17 +0000711 // We got into the worst situation here:
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000712 // - the oat location is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000713 // - the prebuild odex location is not up to date
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000714 // - the vdex-only file is not useable
Calin Juravle357c66d2017-05-04 01:57:17 +0000715 // - and we don't have the original dex file anymore (stripped).
716 // Pick the odex if it exists, or the oat if not.
717 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000718}
719
Andreas Gampea463b6a2016-08-12 21:53:32 -0700720std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800721 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100722 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800723 if (art_file.empty()) {
724 return nullptr;
725 }
726 std::string error_msg;
727 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700728 std::unique_ptr<gc::space::ImageSpace> ret =
729 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800730 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800731 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
732 }
733 return ret;
734}
735
Richard Uhler88bc6732016-11-14 14:38:03 +0000736OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
737 bool is_oat_location)
738 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700739{}
740
Richard Uhler88bc6732016-11-14 14:38:03 +0000741bool OatFileAssistant::OatFileInfo::IsOatLocation() {
742 return is_oat_location_;
743}
744
Richard Uhler743bf362016-04-19 15:39:37 -0700745const std::string* OatFileAssistant::OatFileInfo::Filename() {
746 return filename_provided_ ? &filename_ : nullptr;
747}
748
Richard Uhler03bc6592016-11-22 09:42:04 +0000749bool OatFileAssistant::OatFileInfo::IsUseable() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700750 ScopedTrace trace("IsUseable");
Richard Uhler03bc6592016-11-22 09:42:04 +0000751 switch (Status()) {
752 case kOatCannotOpen:
753 case kOatDexOutOfDate:
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000754 case kOatContextOutOfDate:
Richard Uhler03bc6592016-11-22 09:42:04 +0000755 case kOatBootImageOutOfDate: return false;
756
Richard Uhler03bc6592016-11-22 09:42:04 +0000757 case kOatUpToDate: return true;
758 }
759 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700760}
761
762OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700763 ScopedTrace trace("Status");
Richard Uhler743bf362016-04-19 15:39:37 -0700764 if (!status_attempted_) {
765 status_attempted_ = true;
766 const OatFile* file = GetFile();
767 if (file == nullptr) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000768 status_ = kOatCannotOpen;
Richard Uhler743bf362016-04-19 15:39:37 -0700769 } else {
770 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700771 VLOG(oat) << file->GetLocation() << " is " << status_
772 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700773 }
774 }
775 return status_;
776}
777
Richard Uhler70a84262016-11-08 16:51:51 +0000778OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700779 CompilerFilter::Filter target,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000780 bool profile_changed,
781 bool downgrade) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700782
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000783 if (IsUseable()) {
784 return CompilerFilterIsOkay(target, profile_changed, downgrade)
785 ? kNoDexOptNeeded
786 : kDex2OatForFilter;
787 }
Richard Uhler70a84262016-11-08 16:51:51 +0000788
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000789 if (Status() == kOatBootImageOutOfDate) {
790 return kDex2OatForBootImage;
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100791 }
792
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100793 if (oat_file_assistant_->HasDexFiles()) {
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100794 return kDex2OatFromScratch;
795 } else {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100796 // No dex file, there is nothing we need to do.
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100797 return kNoDexOptNeeded;
798 }
Richard Uhler70a84262016-11-08 16:51:51 +0000799}
800
Richard Uhler743bf362016-04-19 15:39:37 -0700801const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
802 CHECK(!file_released_) << "GetFile called after oat file released.";
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000803 if (load_attempted_) {
804 return file_.get();
805 }
806 load_attempted_ = true;
807 if (!filename_provided_) {
808 return nullptr;
809 }
810
811 std::string error_msg;
812 bool executable = oat_file_assistant_->load_executable_;
813 if (android::base::EndsWith(filename_, kVdexExtension)) {
814 executable = false;
815 // Check to see if there is a vdex file we can make use of.
816 std::unique_ptr<VdexFile> vdex;
817 if (use_fd_) {
818 if (vdex_fd_ >= 0) {
819 struct stat s;
820 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
821 if (rc == -1) {
822 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
823 } else {
824 vdex = VdexFile::Open(vdex_fd_,
825 s.st_size,
826 filename_,
827 /*writable=*/ false,
828 /*low_4gb=*/ false,
829 /*unquicken=*/ false,
830 &error_msg);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700831 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000832 }
833 } else {
834 vdex = VdexFile::Open(filename_,
835 /*writable=*/ false,
836 /*low_4gb=*/ false,
837 /*unquicken=*/ false,
838 &error_msg);
839 }
840 if (vdex == nullptr) {
841 VLOG(oat) << "unable to open vdex file " << filename_ << ": " << error_msg;
842 } else {
843 file_.reset(OatFile::OpenFromVdex(zip_fd_,
844 std::move(vdex),
845 oat_file_assistant_->dex_location_,
846 &error_msg));
847 }
848 } else {
849 if (executable && oat_file_assistant_->only_load_system_executable_) {
850 executable = LocationIsOnSystem(filename_.c_str());
851 }
852 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
853 if (use_fd_) {
854 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
855 ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_,
856 /*size=*/ 1u);
857 file_.reset(OatFile::Open(zip_fd_,
858 vdex_fd_,
859 oat_fd_,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700860 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000861 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100862 /*low_4gb=*/ false,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000863 dex_locations,
864 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700865 &error_msg));
866 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000867 } else {
868 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
869 filename_.c_str(),
870 filename_.c_str(),
871 executable,
872 /*low_4gb=*/ false,
873 oat_file_assistant_->dex_location_,
874 &error_msg));
Richard Uhler743bf362016-04-19 15:39:37 -0700875 }
876 }
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000877 if (file_.get() == nullptr) {
878 VLOG(oat) << "OatFileAssistant test for existing oat file "
879 << filename_
880 << ": " << error_msg;
881 } else {
882 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
883 }
Richard Uhler743bf362016-04-19 15:39:37 -0700884 return file_.get();
885}
886
887bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700888 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700889 const OatFile* file = GetFile();
890 if (file == nullptr) {
891 return false;
892 }
893
894 CompilerFilter::Filter current = file->GetCompilerFilter();
895 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
896 VLOG(oat) << "Compiler filter not okay because Profile changed";
897 return false;
898 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700899 return downgrade ? !CompilerFilter::IsBetter(current, target) :
900 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700901}
902
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000903bool OatFileAssistant::ClassLoaderContextIsOkay(const OatFile& oat_file) const {
904 if (oat_file.IsBackedByVdexOnly()) {
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000905 // Only a vdex file, we don't depend on the class loader context.
906 return true;
907 }
908
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000909 if (!CompilerFilter::IsVerificationEnabled(oat_file.GetCompilerFilter())) {
Calin Juravleaf322422020-02-11 13:45:53 -0800910 // If verification is not enabled we don't need to verify the class loader context and we
911 // assume it's ok.
912 return true;
913 }
914
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000915 if (context_ == nullptr) {
916 // When no class loader context is provided (which happens for deprecated
917 // DexFile APIs), just assume it is OK.
918 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700919 }
David Srbecky656fdcd2021-04-17 16:47:01 +0000920
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000921 ClassLoaderContext::VerificationResult matches = context_->VerifyClassLoaderContextMatch(
922 oat_file.GetClassLoaderContext(),
923 /*verify_names=*/ true,
924 /*verify_checksums=*/ true);
925 if (matches == ClassLoaderContext::VerificationResult::kMismatch) {
David Srbecky656fdcd2021-04-17 16:47:01 +0000926 VLOG(oat) << "ClassLoaderContext check failed. Context was "
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000927 << oat_file.GetClassLoaderContext()
928 << ". The expected context is "
929 << context_->EncodeContextForOatFile(android::base::Dirname(dex_location_));
930 return false;
David Srbecky656fdcd2021-04-17 16:47:01 +0000931 }
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000932 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700933}
934
Richard Uhler743bf362016-04-19 15:39:37 -0700935bool OatFileAssistant::OatFileInfo::IsExecutable() {
936 const OatFile* file = GetFile();
937 return (file != nullptr && file->IsExecutable());
938}
939
Richard Uhler743bf362016-04-19 15:39:37 -0700940void OatFileAssistant::OatFileInfo::Reset() {
941 load_attempted_ = false;
942 file_.reset();
943 status_attempted_ = false;
944}
945
Nicolas Geoffray30025092018-04-19 14:43:29 +0100946void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
947 bool use_fd,
948 int zip_fd,
949 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700950 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700951 filename_provided_ = true;
952 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700953 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100954 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700955 vdex_fd_ = vdex_fd;
956 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700957 Reset();
958}
959
960std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
961 file_released_ = true;
962 return std::move(file_);
963}
964
Richard Uhler70a84262016-11-08 16:51:51 +0000965std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700966 ScopedTrace trace("ReleaseFileForUse");
Richard Uhler3e580bc2016-11-08 16:23:07 +0000967 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000968 return ReleaseFile();
969 }
970
Richard Uhler70a84262016-11-08 16:51:51 +0000971 return std::unique_ptr<OatFile>();
972}
Calin Juravle5f9a8012018-02-12 20:27:46 -0800973
974// TODO(calin): we could provide a more refined status here
975// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
976// track more experiments but adds extra complexity.
977void OatFileAssistant::GetOptimizationStatus(
978 const std::string& filename,
979 InstructionSet isa,
980 std::string* out_compilation_filter,
981 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700982 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
983 // non-executable and check the status manually.
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000984 OatFileAssistant oat_file_assistant(filename.c_str(),
985 isa,
986 /* context= */ nullptr,
987 /*load_executable=*/ false);
Calin Juravle98071152021-01-27 18:41:58 -0800988 std::string out_odex_location; // unused
989 std::string out_odex_status; // unused
990 oat_file_assistant.GetOptimizationStatus(
991 &out_odex_location,
992 out_compilation_filter,
993 out_compilation_reason,
994 &out_odex_status);
995}
996
997void OatFileAssistant::GetOptimizationStatus(
998 std::string* out_odex_location,
999 std::string* out_compilation_filter,
1000 std::string* out_compilation_reason,
1001 std::string* out_odex_status) {
1002 OatFileInfo& oat_file_info = GetBestInfo();
1003 const OatFile* oat_file = GetBestInfo().GetFile();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001004
1005 if (oat_file == nullptr) {
Calin Juravle98071152021-01-27 18:41:58 -08001006 *out_odex_location = "error";
Calin Juravle5f9a8012018-02-12 20:27:46 -08001007 *out_compilation_filter = "run-from-apk";
1008 *out_compilation_reason = "unknown";
Calin Juravle98071152021-01-27 18:41:58 -08001009 // This mostly happens when we cannot open the oat file.
1010 // Note that it's different than kOatCannotOpen.
1011 // TODO: The design of getting the BestInfo is not ideal,
1012 // as it's not very clear what's the difference between
1013 // a nullptr and kOatcannotOpen. The logic should be revised
1014 // and improved.
1015 *out_odex_status = "io-error-no-oat";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001016 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001017 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001018
Calin Juravle98071152021-01-27 18:41:58 -08001019 *out_odex_location = oat_file->GetLocation();
1020 OatStatus status = oat_file_info.Status();
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001021 const char* reason = oat_file->GetCompilationReason();
1022 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1023 switch (status) {
Calin Juravle98071152021-01-27 18:41:58 -08001024 case kOatUpToDate:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001025 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
Calin Juravle98071152021-01-27 18:41:58 -08001026 *out_odex_status = "up-to-date";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001027 return;
1028
1029 case kOatCannotOpen: // This should never happen, but be robust.
1030 *out_compilation_filter = "error";
1031 *out_compilation_reason = "error";
Calin Juravle98071152021-01-27 18:41:58 -08001032 // This mostly happens when we cannot open the vdex file,
1033 // or the file is corrupt.
1034 *out_odex_status = "io-error-or-corruption";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001035 return;
1036
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001037 case kOatBootImageOutOfDate:
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +01001038 *out_compilation_filter = "run-from-apk-fallback";
Calin Juravle98071152021-01-27 18:41:58 -08001039 *out_odex_status = "boot-image-more-recent";
1040 return;
1041
Nicolas Geoffray525fa422021-04-19 07:50:35 +00001042 case kOatContextOutOfDate:
1043 *out_compilation_filter = "run-from-apk-fallback";
1044 *out_odex_status = "context-mismatch";
1045 return;
1046
Calin Juravle98071152021-01-27 18:41:58 -08001047 case kOatDexOutOfDate:
1048 *out_compilation_filter = "run-from-apk-fallback";
1049 *out_odex_status = "apk-more-recent";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001050 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001051 }
1052 LOG(FATAL) << "Unreachable";
1053 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001054}
1055
Richard Uhler66d874d2015-01-15 09:37:19 -08001056} // namespace art