blob: 97254d223dcdc37b6ba43fea6d1ba3c7bc5b2c2e [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_assistant.h"
18
Richard Uhler46cc64f2016-11-14 14:53:55 +000019#include <sstream>
20
Richard Uhler66d874d2015-01-15 09:37:19 -080021#include <sys/stat.h>
David Brazdil35a3f6a2019-03-04 15:59:06 +000022#include "zlib.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080023
Andreas Gampec15a2f42017-04-21 12:09:39 -070024#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080025#include "android-base/strings.h"
26
David Sehr891a50e2017-10-27 17:01:07 -070027#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080028#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070029#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070031#include "base/stl_util.h"
Vladimir Markobcd99be2019-03-22 16:21:31 +000032#include "base/string_view_cpp20.h"
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -070033#include "base/systrace.h"
David Sehrc431b9d2018-03-02 12:01:51 -080034#include "base/utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "class_linker.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000036#include "class_loader_context.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "compiler_filter.h"
David Sehr013fd802018-01-11 22:55:24 -080038#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080039#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080040#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041#include "gc/heap.h"
42#include "gc/space/image_space.h"
43#include "image.h"
44#include "oat.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080045#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070046#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000047#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080048
49namespace art {
50
Richard Uhler69bcf2c2017-01-24 10:25:21 +000051using android::base::StringPrintf;
52
David Brazdil35a3f6a2019-03-04 15:59:06 +000053static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@";
54static constexpr const char* kVdexExtension = ".vdex";
55
Narayan Kamath8943c1d2016-05-02 13:14:48 +010056std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
57 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000058 case OatFileAssistant::kOatCannotOpen:
59 stream << "kOatCannotOpen";
60 break;
61 case OatFileAssistant::kOatDexOutOfDate:
62 stream << "kOatDexOutOfDate";
63 break;
64 case OatFileAssistant::kOatBootImageOutOfDate:
65 stream << "kOatBootImageOutOfDate";
66 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010067 case OatFileAssistant::kOatUpToDate:
68 stream << "kOatUpToDate";
69 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010070 default:
71 UNREACHABLE();
72 }
73
74 return stream;
75}
76
Richard Uhler66d874d2015-01-15 09:37:19 -080077OatFileAssistant::OatFileAssistant(const char* dex_location,
78 const InstructionSet isa,
Nicolas Geoffray29742602017-12-14 10:09:03 +000079 bool load_executable,
80 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070081 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000082 isa,
83 load_executable,
84 only_load_system_executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010085 /*vdex_fd=*/ -1,
86 /*oat_fd=*/ -1,
87 /*zip_fd=*/ -1) {}
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070088
89
90OatFileAssistant::OatFileAssistant(const char* dex_location,
91 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070092 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000093 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070094 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070095 int oat_fd,
96 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000097 : isa_(isa),
98 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +000099 only_load_system_executable_(only_load_system_executable),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700100 odex_(this, /*is_oat_location=*/ false),
101 oat_(this, /*is_oat_location=*/ true),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700102 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -0700103 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +0000104
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700105 if (zip_fd < 0) {
106 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
107 << " oat_fd=" << oat_fd;
108 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
109 << " vdex_fd=" << vdex_fd;;
110 }
111
Calin Juravle099f8d62017-09-05 19:04:04 -0700112 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700113
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100114 if (load_executable_ && isa != kRuntimeISA) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800115 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100116 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800117 load_executable_ = false;
118 }
119
Richard Uhler743bf362016-04-19 15:39:37 -0700120 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700121 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700122 std::string odex_file_name;
123 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100124 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700125 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700126 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
127 }
128
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700129 if (!UseFdToReadFiles()) {
130 // Get the oat filename.
131 std::string oat_file_name;
132 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100133 oat_.Reset(oat_file_name, /*use_fd=*/ false);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700134 } else {
135 LOG(WARNING) << "Failed to determine oat file name for dex location "
136 << dex_location_ << ": " << error_msg;
137 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000138 }
139
140 // Check if the dex directory is writable.
141 // This will be needed in most uses of OatFileAssistant and so it's OK to
142 // compute it eagerly. (the only use which will not make use of it is
143 // OatFileAssistant::GetStatusDump())
144 size_t pos = dex_location_.rfind('/');
145 if (pos == std::string::npos) {
146 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700147 } else if (!UseFdToReadFiles()) {
148 // We cannot test for parent access when using file descriptors. That's ok
149 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000150 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700151 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000152 dex_parent_writable_ = true;
153 } else {
154 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700155 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800156 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800157}
158
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700159bool OatFileAssistant::UseFdToReadFiles() {
160 return zip_fd_ >= 0;
161}
162
Richard Uhler66d874d2015-01-15 09:37:19 -0800163bool OatFileAssistant::IsInBootClassPath() {
164 // Note: We check the current boot class path, regardless of the ISA
165 // specified by the user. This is okay, because the boot class path should
166 // be the same for all ISAs.
167 // TODO: Can we verify the boot class path is the same for all ISAs?
168 Runtime* runtime = Runtime::Current();
169 ClassLinker* class_linker = runtime->GetClassLinker();
170 const auto& boot_class_path = class_linker->GetBootClassPath();
171 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700172 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800173 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
174 return true;
175 }
176 }
177 return false;
178}
179
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700180int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
Stefania Halace9818dd2020-02-14 17:58:26 +0000181 ClassLoaderContext* class_loader_context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000182 const std::vector<int>& context_fds,
183 bool profile_changed,
184 bool downgrade) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000185 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700186 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
Stefania Halace9818dd2020-02-14 17:58:26 +0000187 class_loader_context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000188 context_fds,
189 profile_changed,
190 downgrade);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000191 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
192 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800193 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000194 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800195}
196
Richard Uhler01be6812016-05-17 10:34:52 -0700197bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000198 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700199}
200
Richard Uhler66d874d2015-01-15 09:37:19 -0800201std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000202 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800203}
204
Richard Uhler46cc64f2016-11-14 14:53:55 +0000205std::string OatFileAssistant::GetStatusDump() {
206 std::ostringstream status;
207 bool oat_file_exists = false;
208 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000209 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000210 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000211 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000212
Richard Uhler46cc64f2016-11-14 14:53:55 +0000213 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000214 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
215 const OatFile* file = oat_.GetFile();
216 if (file == nullptr) {
217 // If the file is null even though the status is not kOatCannotOpen, it
218 // means we must have a vdex file with no corresponding oat file. In
219 // this case we cannot determine the compilation filter. Indicate that
220 // we have only the vdex file instead.
221 status << "vdex-only";
222 } else {
223 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
224 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000225 }
226
Richard Uhler03bc6592016-11-22 09:42:04 +0000227 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000228 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000229 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000230
Richard Uhler46cc64f2016-11-14 14:53:55 +0000231 odex_file_exists = true;
232 if (oat_file_exists) {
233 status << "] ";
234 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000235 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
236 const OatFile* file = odex_.GetFile();
237 if (file == nullptr) {
238 status << "vdex-only";
239 } else {
240 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
241 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000242 }
243
244 if (!oat_file_exists && !odex_file_exists) {
245 status << "invalid[";
246 }
247
248 status << "]";
249 return status.str();
250}
251
Richard Uhler66d874d2015-01-15 09:37:19 -0800252std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700253 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800254 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700255 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
256 return dex_files;
257 } else {
258 return std::vector<std::unique_ptr<const DexFile>>();
259 }
260}
Richard Uhler66d874d2015-01-15 09:37:19 -0800261
Calin Juravle87e2cb62017-06-13 21:48:45 -0700262bool OatFileAssistant::LoadDexFiles(
263 const OatFile &oat_file,
264 const std::string& dex_location,
265 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000266 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800267 std::string error_msg;
Andreas Gampeb40d3612018-06-26 15:49:42 -0700268 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700269 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800270 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700271 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700272 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800273 }
274
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700275 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 if (dex_file.get() == nullptr) {
277 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700278 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800279 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700280 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800281
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000282 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700283 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700284 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000285 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700286 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000287 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800288 break;
289 }
290
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700291 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800292 if (dex_file.get() == nullptr) {
293 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700294 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800295 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700296 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800297 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700298 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800299}
300
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100301bool OatFileAssistant::HasDexFiles() {
302 ScopedTrace trace("HasDexFiles");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000303 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700304 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000305 // GetRequiredDexChecksums.
306 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700307 return has_original_dex_files_;
308}
309
Richard Uhler95abd042015-03-24 09:51:28 -0700310OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700311 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800312}
313
Richard Uhler95abd042015-03-24 09:51:28 -0700314OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700315 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800316}
317
Richard Uhler2f27abd2017-01-31 14:02:34 +0000318bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700319 ScopedTrace trace("DexChecksumUpToDate(vdex)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000320 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
321 if (required_dex_checksums == nullptr) {
322 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
323 return true;
324 }
325
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000326 uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000327 if (required_dex_checksums->size() != number_of_dex_files) {
328 *error_msg = StringPrintf("expected %zu dex files but found %u",
329 required_dex_checksums->size(),
330 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000331 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800332 }
333
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000334 for (uint32_t i = 0; i < number_of_dex_files; i++) {
335 uint32_t expected_checksum = (*required_dex_checksums)[i];
336 uint32_t actual_checksum = file.GetLocationChecksum(i);
337 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700338 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000339 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
340 "Expected: %u, actual: %u",
341 dex.c_str(),
342 expected_checksum,
343 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000344 return false;
345 }
346 }
347
Richard Uhler2f27abd2017-01-31 14:02:34 +0000348 return true;
349}
350
351bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700352 ScopedTrace trace("DexChecksumUpToDate(oat)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000353 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
354 if (required_dex_checksums == nullptr) {
355 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
356 return true;
357 }
358
359 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
360 if (required_dex_checksums->size() != number_of_dex_files) {
361 *error_msg = StringPrintf("expected %zu dex files but found %u",
362 required_dex_checksums->size(),
363 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000364 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800365 }
366
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000367 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700368 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000369 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700370 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000371 if (oat_dex_file == nullptr) {
372 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
373 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000375 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
376 if (expected_checksum != actual_checksum) {
377 VLOG(oat) << "Dex checksum does not match for dex: " << dex
378 << ". Expected: " << expected_checksum
379 << ", Actual: " << actual_checksum;
380 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 }
382 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000383 return true;
384}
385
386OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
387 // Verify the ART_USE_READ_BARRIER state.
388 // TODO: Don't fully reject files due to read barrier state. If they contain
389 // compiled code and are otherwise okay, we should return something like
390 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
391 // barrier state doesn't matter.
392 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
393 constexpr bool kRuntimeIsCC = kUseReadBarrier;
394 if (is_cc != kRuntimeIsCC) {
395 return kOatCannotOpen;
396 }
397
398 // Verify the dex checksum.
399 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000400 VdexFile* vdex = file.GetVdexFile();
401 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
402 LOG(ERROR) << error_msg;
403 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000404 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800405
Andreas Gampe29d38e72016-03-23 15:31:51 +0000406 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000407
Richard Uhler66d874d2015-01-15 09:37:19 -0800408 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000409 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000410 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000411 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000412 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000413 }
414 } else {
415 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800416 }
417
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000418 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
419 DCHECK(required_dex_checksums_attempted_);
420 if (only_load_system_executable_ &&
421 !LocationIsOnSystem(file.GetLocation().c_str()) &&
422 file.ContainsDexCode() &&
423 zip_file_only_contains_uncompressed_dex_) {
424 LOG(ERROR) << "Not loading "
425 << dex_location_
426 << ": oat file has dex code, but APK has uncompressed dex code";
427 return kOatDexOutOfDate;
428 }
429
Richard Uhlere8109f72016-04-18 10:40:50 -0700430 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800431}
432
David Brazdil35a3f6a2019-03-04 15:59:06 +0000433bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
434 InstructionSet isa,
435 /* out */ uint32_t* location_checksum,
436 /* out */ std::string* dex_location,
437 /* out */ std::string* vdex_filename) {
438 uint32_t checksum = adler32(0L, Z_NULL, 0);
439 for (const DexFile::Header* header : headers) {
440 checksum = adler32_combine(checksum,
441 header->checksum_,
442 header->file_size_ - DexFile::kNumNonChecksumBytes);
443 }
444 *location_checksum = checksum;
445
446 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
447 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
448 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
449 return false;
450 }
451 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
452
453 std::string odex_filename;
454 std::string error_msg;
455 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
456 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
457 return false;
458 }
459
460 *vdex_filename = GetVdexFilename(odex_filename);
461 return true;
462}
463
464bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
465 DCHECK(basename.find('/') == std::string::npos);
466 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
467 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
468 !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) ||
469 !android::base::EndsWith(basename, kVdexExtension)) {
470 return false;
471 }
472 // Check that all characters between the prefix and extension are decimal digits.
473 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
474 if (!std::isdigit(basename[i])) {
475 return false;
476 }
477 }
478 return true;
479}
480
Calin Juravle357c66d2017-05-04 01:57:17 +0000481static bool DexLocationToOdexNames(const std::string& location,
482 InstructionSet isa,
483 std::string* odex_filename,
484 std::string* oat_dir,
485 std::string* isa_dir,
486 std::string* error_msg) {
487 CHECK(odex_filename != nullptr);
488 CHECK(error_msg != nullptr);
489
490 // The odex file name is formed by replacing the dex_location extension with
491 // .odex and inserting an oat/<isa> directory. For example:
492 // location = /foo/bar/baz.jar
493 // odex_location = /foo/bar/oat/<isa>/baz.odex
494
495 // Find the directory portion of the dex location and add the oat/<isa>
496 // directory.
497 size_t pos = location.rfind('/');
498 if (pos == std::string::npos) {
499 *error_msg = "Dex location " + location + " has no directory.";
500 return false;
501 }
502 std::string dir = location.substr(0, pos+1);
503 // Add the oat directory.
504 dir += "oat";
505 if (oat_dir != nullptr) {
506 *oat_dir = dir;
507 }
508 // Add the isa directory
509 dir += "/" + std::string(GetInstructionSetString(isa));
510 if (isa_dir != nullptr) {
511 *isa_dir = dir;
512 }
513
514 // Get the base part of the file without the extension.
515 std::string file = location.substr(pos+1);
516 pos = file.rfind('.');
517 if (pos == std::string::npos) {
518 *error_msg = "Dex location " + location + " has no extension.";
519 return false;
520 }
521 std::string base = file.substr(0, pos);
522
523 *odex_filename = dir + "/" + base + ".odex";
524 return true;
525}
526
Richard Uhlerb81881d2016-04-19 13:08:04 -0700527bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
528 InstructionSet isa,
529 std::string* odex_filename,
530 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000531 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800532}
533
Richard Uhlerb81881d2016-04-19 13:08:04 -0700534bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
535 InstructionSet isa,
536 std::string* oat_filename,
537 std::string* error_msg) {
538 CHECK(oat_filename != nullptr);
539 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800540
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700541 // If ANDROID_DATA is not set, return false instead of aborting.
542 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100543 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700544 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
545 return false;
546 }
547
Orion Hodsonfa81f712021-01-13 12:27:21 +0000548 std::string dalvik_cache;
549 bool have_android_data = false;
550 bool dalvik_cache_exists = false;
551 bool is_global_cache = false;
552 GetDalvikCache(GetInstructionSetString(isa),
553 /*create_if_absent=*/ true,
554 &dalvik_cache,
555 &have_android_data,
556 &dalvik_cache_exists,
557 &is_global_cache);
558 if (!dalvik_cache_exists) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700559 *error_msg = "Dalvik cache directory does not exist";
560 return false;
561 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700562
563 // TODO: The oat file assistant should be the definitive place for
564 // determining the oat file name from the dex location, not
565 // GetDalvikCacheFilename.
Orion Hodsonfa81f712021-01-13 12:27:21 +0000566 return GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800567}
568
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000569const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
570 if (!required_dex_checksums_attempted_) {
571 required_dex_checksums_attempted_ = true;
572 required_dex_checksums_found_ = false;
573 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800574 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800575 const ArtDexFileLoader dex_file_loader;
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800576 std::vector<std::string> dex_locations_ignored;
David Sehr013fd802018-01-11 22:55:24 -0800577 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
578 &cached_required_dex_checksums_,
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800579 &dex_locations_ignored,
David Sehr013fd802018-01-11 22:55:24 -0800580 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000581 zip_fd_,
582 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000583 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700584 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800585 } else {
Calin Juravle5ff23932020-12-11 18:26:14 -0800586 // The only valid case here is for APKs without dex files.
587 required_dex_checksums_found_ = false;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700588 has_original_dex_files_ = false;
Calin Juravle5ff23932020-12-11 18:26:14 -0800589 VLOG(oat) << "Could not get required checksum: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800590 }
591 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000592 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800593}
594
Vladimir Markobcd99be2019-03-22 16:21:31 +0000595bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100596 // Get the checksums and the BCP from the oat file.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000597 const char* oat_boot_class_path_checksums =
598 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100599 const char* oat_boot_class_path =
600 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
601 if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) {
Vladimir Markof3d88a82018-12-21 16:38:47 +0000602 return false;
603 }
Vladimir Marko436c6f52019-07-25 14:50:14 +0100604 std::string_view oat_boot_class_path_checksums_view(oat_boot_class_path_checksums);
605 std::string_view oat_boot_class_path_view(oat_boot_class_path);
606 if (oat_boot_class_path_view == cached_boot_class_path_ &&
607 oat_boot_class_path_checksums_view == cached_boot_class_path_checksums_) {
608 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800609 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000610
Vladimir Marko436c6f52019-07-25 14:50:14 +0100611 Runtime* runtime = Runtime::Current();
612 std::string error_msg;
613 bool result = gc::space::ImageSpace::VerifyBootClassPathChecksums(
614 oat_boot_class_path_checksums_view,
615 oat_boot_class_path_view,
616 runtime->GetImageLocation(),
617 ArrayRef<const std::string>(runtime->GetBootClassPathLocations()),
618 ArrayRef<const std::string>(runtime->GetBootClassPath()),
619 isa_,
Vladimir Marko436c6f52019-07-25 14:50:14 +0100620 &error_msg);
621 if (!result) {
622 VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation()
623 << " error: " << error_msg;
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100624 return false;
Vladimir Marko436c6f52019-07-25 14:50:14 +0100625 }
626
627 // This checksum has been validated, so save it.
628 cached_boot_class_path_ = oat_boot_class_path_view;
629 cached_boot_class_path_checksums_ = oat_boot_class_path_checksums_view;
630 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800631}
632
Richard Uhler88bc6732016-11-14 14:38:03 +0000633OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700634 ScopedTrace trace("GetBestInfo");
Calin Juravle357c66d2017-05-04 01:57:17 +0000635 // TODO(calin): Document the side effects of class loading when
636 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700637 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000638 // If the parent of the dex file is writable it means that we can
639 // create the odex file. In this case we unconditionally pick the odex
640 // as the best oat file. This corresponds to the regular use case when
641 // apps gets installed or when they load private, secondary dex file.
642 // For apps on the system partition the odex location will not be
643 // writable and thus the oat location might be more up to date.
644 return odex_;
645 }
646
647 // We cannot write to the odex location. This must be a system app.
648
649 // If the oat location is usable take it.
650 if (oat_.IsUseable()) {
651 return oat_;
652 }
653
654 // The oat file is not usable but the odex file might be up to date.
655 // This is an indication that we are dealing with an up to date prebuilt
656 // (that doesn't need relocation).
657 if (odex_.Status() == kOatUpToDate) {
658 return odex_;
659 }
660
Calin Juravle357c66d2017-05-04 01:57:17 +0000661 // We got into the worst situation here:
662 // - the oat location is not usable
663 // - the prebuild odex location is not up to date
664 // - and we don't have the original dex file anymore (stripped).
665 // Pick the odex if it exists, or the oat if not.
666 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000667}
668
Andreas Gampea463b6a2016-08-12 21:53:32 -0700669std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800670 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100671 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800672 if (art_file.empty()) {
673 return nullptr;
674 }
675 std::string error_msg;
676 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700677 std::unique_ptr<gc::space::ImageSpace> ret =
678 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800679 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800680 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
681 }
682 return ret;
683}
684
Richard Uhler88bc6732016-11-14 14:38:03 +0000685OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
686 bool is_oat_location)
687 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700688{}
689
Richard Uhler88bc6732016-11-14 14:38:03 +0000690bool OatFileAssistant::OatFileInfo::IsOatLocation() {
691 return is_oat_location_;
692}
693
Richard Uhler743bf362016-04-19 15:39:37 -0700694const std::string* OatFileAssistant::OatFileInfo::Filename() {
695 return filename_provided_ ? &filename_ : nullptr;
696}
697
Richard Uhler03bc6592016-11-22 09:42:04 +0000698bool OatFileAssistant::OatFileInfo::IsUseable() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700699 ScopedTrace trace("IsUseable");
Richard Uhler03bc6592016-11-22 09:42:04 +0000700 switch (Status()) {
701 case kOatCannotOpen:
702 case kOatDexOutOfDate:
703 case kOatBootImageOutOfDate: return false;
704
Richard Uhler03bc6592016-11-22 09:42:04 +0000705 case kOatUpToDate: return true;
706 }
707 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700708}
709
710OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700711 ScopedTrace trace("Status");
Richard Uhler743bf362016-04-19 15:39:37 -0700712 if (!status_attempted_) {
713 status_attempted_ = true;
714 const OatFile* file = GetFile();
715 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000716 // Check to see if there is a vdex file we can make use of.
717 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700718 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700719 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700720 if (use_fd_) {
721 if (vdex_fd_ >= 0) {
722 struct stat s;
723 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
724 if (rc == -1) {
725 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
726 } else {
727 vdex = VdexFile::Open(vdex_fd_,
728 s.st_size,
729 vdex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100730 /*writable=*/ false,
731 /*low_4gb=*/ false,
732 /*unquicken=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700733 &error_msg);
734 }
735 }
736 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700737 vdex = VdexFile::Open(vdex_filename,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700738 /*writable=*/ false,
739 /*low_4gb=*/ false,
740 /*unquicken=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700741 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700742 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000743 if (vdex == nullptr) {
744 status_ = kOatCannotOpen;
745 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
746 } else {
747 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
748 // The vdex file does not contain enough information to determine
749 // whether it is up to date with respect to the boot image, so we
750 // assume it is out of date.
751 VLOG(oat) << error_msg;
752 status_ = kOatBootImageOutOfDate;
753 } else {
754 status_ = kOatDexOutOfDate;
755 }
756 }
Richard Uhler743bf362016-04-19 15:39:37 -0700757 } else {
758 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700759 VLOG(oat) << file->GetLocation() << " is " << status_
760 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700761 }
762 }
763 return status_;
764}
765
Richard Uhler70a84262016-11-08 16:51:51 +0000766OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700767 CompilerFilter::Filter target,
Stefania Halace9818dd2020-02-14 17:58:26 +0000768 ClassLoaderContext* context,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000769 const std::vector<int>& context_fds,
770 bool profile_changed,
771 bool downgrade) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700772
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700773 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
David Brazdil89821862019-03-19 13:57:43 +0000774 bool class_loader_context_okay = ClassLoaderContextIsOkay(context, context_fds);
Richard Uhler70a84262016-11-08 16:51:51 +0000775
Calin Juravle44e5efa2017-09-12 00:54:26 -0700776 // Only check the filter and relocation if the class loader context is ok.
777 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
778 if (class_loader_context_okay) {
779 if (filter_okay && Status() == kOatUpToDate) {
780 // The oat file is in good shape as is.
781 return kNoDexOptNeeded;
782 }
Richard Uhler70a84262016-11-08 16:51:51 +0000783
Calin Juravle44e5efa2017-09-12 00:54:26 -0700784 if (IsUseable()) {
785 return kDex2OatForFilter;
786 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100787
Calin Juravle44e5efa2017-09-12 00:54:26 -0700788 if (Status() == kOatBootImageOutOfDate) {
789 return kDex2OatForBootImage;
790 }
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.";
803 if (!load_attempted_) {
804 load_attempted_ = true;
805 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +0000806 bool executable = oat_file_assistant_->load_executable_;
807 if (executable && oat_file_assistant_->only_load_system_executable_) {
808 executable = LocationIsOnSystem(filename_.c_str());
809 }
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000810 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
Richard Uhler743bf362016-04-19 15:39:37 -0700811 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700812 if (use_fd_) {
813 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
Vladimir Markob7bf8432019-12-03 13:18:50 +0000814 ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_,
815 /*size=*/ 1u);
Nicolas Geoffray30025092018-04-19 14:43:29 +0100816 file_.reset(OatFile::Open(zip_fd_,
817 vdex_fd_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700818 oat_fd_,
819 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000820 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100821 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000822 dex_locations,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100823 /*reservation=*/ nullptr,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700824 &error_msg));
825 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700826 } else {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100827 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100828 filename_.c_str(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700829 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000830 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100831 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000832 oat_file_assistant_->dex_location_,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700833 &error_msg));
834 }
Richard Uhler743bf362016-04-19 15:39:37 -0700835 if (file_.get() == nullptr) {
836 VLOG(oat) << "OatFileAssistant test for existing oat file "
837 << filename_ << ": " << error_msg;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000838 } else {
839 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
Richard Uhler743bf362016-04-19 15:39:37 -0700840 }
841 }
842 }
843 return file_.get();
844}
845
846bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700847 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700848 const OatFile* file = GetFile();
849 if (file == nullptr) {
850 return false;
851 }
852
853 CompilerFilter::Filter current = file->GetCompilerFilter();
854 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
855 VLOG(oat) << "Compiler filter not okay because Profile changed";
856 return false;
857 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700858 return downgrade ? !CompilerFilter::IsBetter(current, target) :
859 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700860}
861
David Brazdil89821862019-03-19 13:57:43 +0000862bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context,
863 const std::vector<int>& context_fds) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700864 const OatFile* file = GetFile();
865 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -0700866 // No oat file means we have nothing to verify.
867 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700868 }
869
Calin Juravleaf322422020-02-11 13:45:53 -0800870 if (!CompilerFilter::IsVerificationEnabled(file->GetCompilerFilter())) {
871 // If verification is not enabled we don't need to verify the class loader context and we
872 // assume it's ok.
873 return true;
874 }
875
Calin Juravle0a5cad32020-02-14 20:29:26 +0000876
Calin Juravleaf322422020-02-11 13:45:53 -0800877 if (context == nullptr) {
Calin Juravle0a5cad32020-02-14 20:29:26 +0000878 // TODO(calin): stop using null for the unkown contexts.
879 // b/148494302 introduces runtime encoding for unknown context which will make this possible.
880 VLOG(oat) << "ClassLoaderContext check failed: uknown(null) context";
881 return false;
Calin Juravleaf322422020-02-11 13:45:53 -0800882 }
883
Calin Juravle20c46442017-09-12 00:54:26 -0700884 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -0700885 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -0700886 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -0700887 : "";
888
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800889 if (!context->OpenDexFiles(classpath_dir, context_fds, /*only_read_checksums*/ true)) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700890 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
891 return false;
892 }
893
Mathieu Chartieradc90862018-05-11 13:03:06 -0700894 const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
895 ClassLoaderContext::VerificationResult::kMismatch;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700896 if (!result) {
897 VLOG(oat) << "ClassLoaderContext check failed. Context was "
898 << file->GetClassLoaderContext()
899 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
900 }
901 return result;
902}
903
Richard Uhler743bf362016-04-19 15:39:37 -0700904bool OatFileAssistant::OatFileInfo::IsExecutable() {
905 const OatFile* file = GetFile();
906 return (file != nullptr && file->IsExecutable());
907}
908
Richard Uhler743bf362016-04-19 15:39:37 -0700909void OatFileAssistant::OatFileInfo::Reset() {
910 load_attempted_ = false;
911 file_.reset();
912 status_attempted_ = false;
913}
914
Nicolas Geoffray30025092018-04-19 14:43:29 +0100915void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
916 bool use_fd,
917 int zip_fd,
918 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700919 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700920 filename_provided_ = true;
921 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700922 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100923 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700924 vdex_fd_ = vdex_fd;
925 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700926 Reset();
927}
928
929std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
930 file_released_ = true;
931 return std::move(file_);
932}
933
Richard Uhler70a84262016-11-08 16:51:51 +0000934std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700935 ScopedTrace trace("ReleaseFileForUse");
Richard Uhler3e580bc2016-11-08 16:23:07 +0000936 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000937 return ReleaseFile();
938 }
939
Richard Uhler70a84262016-11-08 16:51:51 +0000940 return std::unique_ptr<OatFile>();
941}
Calin Juravle5f9a8012018-02-12 20:27:46 -0800942
943// TODO(calin): we could provide a more refined status here
944// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
945// track more experiments but adds extra complexity.
946void OatFileAssistant::GetOptimizationStatus(
947 const std::string& filename,
948 InstructionSet isa,
949 std::string* out_compilation_filter,
950 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700951 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
952 // non-executable and check the status manually.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100953 OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false);
Calin Juravle98071152021-01-27 18:41:58 -0800954 std::string out_odex_location; // unused
955 std::string out_odex_status; // unused
956 oat_file_assistant.GetOptimizationStatus(
957 &out_odex_location,
958 out_compilation_filter,
959 out_compilation_reason,
960 &out_odex_status);
961}
962
963void OatFileAssistant::GetOptimizationStatus(
964 std::string* out_odex_location,
965 std::string* out_compilation_filter,
966 std::string* out_compilation_reason,
967 std::string* out_odex_status) {
968 OatFileInfo& oat_file_info = GetBestInfo();
969 const OatFile* oat_file = GetBestInfo().GetFile();
Calin Juravle5f9a8012018-02-12 20:27:46 -0800970
971 if (oat_file == nullptr) {
Calin Juravle98071152021-01-27 18:41:58 -0800972 *out_odex_location = "error";
Calin Juravle5f9a8012018-02-12 20:27:46 -0800973 *out_compilation_filter = "run-from-apk";
974 *out_compilation_reason = "unknown";
Calin Juravle98071152021-01-27 18:41:58 -0800975 // This mostly happens when we cannot open the oat file.
976 // Note that it's different than kOatCannotOpen.
977 // TODO: The design of getting the BestInfo is not ideal,
978 // as it's not very clear what's the difference between
979 // a nullptr and kOatcannotOpen. The logic should be revised
980 // and improved.
981 *out_odex_status = "io-error-no-oat";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700982 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -0800983 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700984
Calin Juravle98071152021-01-27 18:41:58 -0800985 *out_odex_location = oat_file->GetLocation();
986 OatStatus status = oat_file_info.Status();
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700987 const char* reason = oat_file->GetCompilationReason();
988 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
989 switch (status) {
Calin Juravle98071152021-01-27 18:41:58 -0800990 case kOatUpToDate:
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700991 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
Calin Juravle98071152021-01-27 18:41:58 -0800992 *out_odex_status = "up-to-date";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -0700993 return;
994
995 case kOatCannotOpen: // This should never happen, but be robust.
996 *out_compilation_filter = "error";
997 *out_compilation_reason = "error";
Calin Juravle98071152021-01-27 18:41:58 -0800998 // This mostly happens when we cannot open the vdex file,
999 // or the file is corrupt.
1000 *out_odex_status = "io-error-or-corruption";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001001 return;
1002
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001003 case kOatBootImageOutOfDate:
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +01001004 *out_compilation_filter = "run-from-apk-fallback";
Calin Juravle98071152021-01-27 18:41:58 -08001005 *out_odex_status = "boot-image-more-recent";
1006 return;
1007
1008 case kOatDexOutOfDate:
1009 *out_compilation_filter = "run-from-apk-fallback";
1010 *out_odex_status = "apk-more-recent";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001011 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001012 }
1013 LOG(FATAL) << "Unreachable";
1014 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001015}
1016
Richard Uhler66d874d2015-01-15 09:37:19 -08001017} // namespace art