blob: ab1389af56a81635bc6ac9caff4825991a6718b2 [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
Richard Uhler66d874d2015-01-15 09:37:19 -0800114 if (load_executable_ && isa != kRuntimeISA) {
115 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
116 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
117 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
159OatFileAssistant::~OatFileAssistant() {
160 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100161 if (flock_.get() != nullptr) {
162 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800163 }
164}
165
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700166bool OatFileAssistant::UseFdToReadFiles() {
167 return zip_fd_ >= 0;
168}
169
Richard Uhler66d874d2015-01-15 09:37:19 -0800170bool OatFileAssistant::IsInBootClassPath() {
171 // Note: We check the current boot class path, regardless of the ISA
172 // specified by the user. This is okay, because the boot class path should
173 // be the same for all ISAs.
174 // TODO: Can we verify the boot class path is the same for all ISAs?
175 Runtime* runtime = Runtime::Current();
176 ClassLinker* class_linker = runtime->GetClassLinker();
177 const auto& boot_class_path = class_linker->GetBootClassPath();
178 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700179 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800180 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
181 return true;
182 }
183 }
184 return false;
185}
186
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700187int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
188 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700189 bool downgrade,
David Brazdil89821862019-03-19 13:57:43 +0000190 ClassLoaderContext* class_loader_context,
191 const std::vector<int>& context_fds) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000192 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700193 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
194 profile_changed,
195 downgrade,
David Brazdil89821862019-03-19 13:57:43 +0000196 class_loader_context,
197 context_fds);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000198 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
199 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800200 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000201 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800202}
203
Richard Uhler01be6812016-05-17 10:34:52 -0700204bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000205 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700206}
207
Richard Uhler66d874d2015-01-15 09:37:19 -0800208std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000209 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800210}
211
Richard Uhler46cc64f2016-11-14 14:53:55 +0000212std::string OatFileAssistant::GetStatusDump() {
213 std::ostringstream status;
214 bool oat_file_exists = false;
215 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000216 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000217 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000218 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000219
Richard Uhler46cc64f2016-11-14 14:53:55 +0000220 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000221 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
222 const OatFile* file = oat_.GetFile();
223 if (file == nullptr) {
224 // If the file is null even though the status is not kOatCannotOpen, it
225 // means we must have a vdex file with no corresponding oat file. In
226 // this case we cannot determine the compilation filter. Indicate that
227 // we have only the vdex file instead.
228 status << "vdex-only";
229 } else {
230 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
231 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000232 }
233
Richard Uhler03bc6592016-11-22 09:42:04 +0000234 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000235 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000236 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000237
Richard Uhler46cc64f2016-11-14 14:53:55 +0000238 odex_file_exists = true;
239 if (oat_file_exists) {
240 status << "] ";
241 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000242 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
243 const OatFile* file = odex_.GetFile();
244 if (file == nullptr) {
245 status << "vdex-only";
246 } else {
247 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
248 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000249 }
250
251 if (!oat_file_exists && !odex_file_exists) {
252 status << "invalid[";
253 }
254
255 status << "]";
256 return status.str();
257}
258
Richard Uhler66d874d2015-01-15 09:37:19 -0800259std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700260 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800261 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700262 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
263 return dex_files;
264 } else {
265 return std::vector<std::unique_ptr<const DexFile>>();
266 }
267}
Richard Uhler66d874d2015-01-15 09:37:19 -0800268
Calin Juravle87e2cb62017-06-13 21:48:45 -0700269bool OatFileAssistant::LoadDexFiles(
270 const OatFile &oat_file,
271 const std::string& dex_location,
272 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000273 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800274 std::string error_msg;
Andreas Gampeb40d3612018-06-26 15:49:42 -0700275 const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700276 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800277 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700278 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700279 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800280 }
281
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700282 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800283 if (dex_file.get() == nullptr) {
284 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700285 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800286 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700287 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800288
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000289 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700290 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700291 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000292 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700293 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000294 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800295 break;
296 }
297
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700298 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800299 if (dex_file.get() == nullptr) {
300 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700301 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800302 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700303 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800304 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700305 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800306}
307
Richard Uhler9b994ea2015-06-24 08:44:19 -0700308bool OatFileAssistant::HasOriginalDexFiles() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700309 ScopedTrace trace("HasOriginalDexFiles");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000310 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700311 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000312 // GetRequiredDexChecksums.
313 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700314 return has_original_dex_files_;
315}
316
Richard Uhler95abd042015-03-24 09:51:28 -0700317OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700318 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800319}
320
Richard Uhler95abd042015-03-24 09:51:28 -0700321OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700322 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800323}
324
Richard Uhler2f27abd2017-01-31 14:02:34 +0000325bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700326 ScopedTrace trace("DexChecksumUpToDate(vdex)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000327 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
328 if (required_dex_checksums == nullptr) {
329 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
330 return true;
331 }
332
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000333 uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000334 if (required_dex_checksums->size() != number_of_dex_files) {
335 *error_msg = StringPrintf("expected %zu dex files but found %u",
336 required_dex_checksums->size(),
337 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000338 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800339 }
340
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000341 for (uint32_t i = 0; i < number_of_dex_files; i++) {
342 uint32_t expected_checksum = (*required_dex_checksums)[i];
343 uint32_t actual_checksum = file.GetLocationChecksum(i);
344 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700345 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000346 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
347 "Expected: %u, actual: %u",
348 dex.c_str(),
349 expected_checksum,
350 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000351 return false;
352 }
353 }
354
Richard Uhler2f27abd2017-01-31 14:02:34 +0000355 return true;
356}
357
358bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700359 ScopedTrace trace("DexChecksumUpToDate(oat)");
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000360 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
361 if (required_dex_checksums == nullptr) {
362 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
363 return true;
364 }
365
366 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
367 if (required_dex_checksums->size() != number_of_dex_files) {
368 *error_msg = StringPrintf("expected %zu dex files but found %u",
369 required_dex_checksums->size(),
370 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000371 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 }
373
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000374 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700375 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000376 uint32_t expected_checksum = (*required_dex_checksums)[i];
Andreas Gampeb40d3612018-06-26 15:49:42 -0700377 const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000378 if (oat_dex_file == nullptr) {
379 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
380 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000382 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
383 if (expected_checksum != actual_checksum) {
384 VLOG(oat) << "Dex checksum does not match for dex: " << dex
385 << ". Expected: " << expected_checksum
386 << ", Actual: " << actual_checksum;
387 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800388 }
389 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000390 return true;
391}
392
393OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
394 // Verify the ART_USE_READ_BARRIER state.
395 // TODO: Don't fully reject files due to read barrier state. If they contain
396 // compiled code and are otherwise okay, we should return something like
397 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
398 // barrier state doesn't matter.
399 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
400 constexpr bool kRuntimeIsCC = kUseReadBarrier;
401 if (is_cc != kRuntimeIsCC) {
402 return kOatCannotOpen;
403 }
404
405 // Verify the dex checksum.
406 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000407 VdexFile* vdex = file.GetVdexFile();
408 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
409 LOG(ERROR) << error_msg;
410 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000411 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800412
Andreas Gampe29d38e72016-03-23 15:31:51 +0000413 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000414
Richard Uhler66d874d2015-01-15 09:37:19 -0800415 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000416 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
Vladimir Markobcd99be2019-03-22 16:21:31 +0000417 if (!ValidateBootClassPathChecksums(file)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000418 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000419 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000420 }
421 } else {
422 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800423 }
424
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000425 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
426 DCHECK(required_dex_checksums_attempted_);
427 if (only_load_system_executable_ &&
428 !LocationIsOnSystem(file.GetLocation().c_str()) &&
429 file.ContainsDexCode() &&
430 zip_file_only_contains_uncompressed_dex_) {
431 LOG(ERROR) << "Not loading "
432 << dex_location_
433 << ": oat file has dex code, but APK has uncompressed dex code";
434 return kOatDexOutOfDate;
435 }
436
Richard Uhlere8109f72016-04-18 10:40:50 -0700437 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800438}
439
David Brazdil35a3f6a2019-03-04 15:59:06 +0000440bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
441 InstructionSet isa,
442 /* out */ uint32_t* location_checksum,
443 /* out */ std::string* dex_location,
444 /* out */ std::string* vdex_filename) {
445 uint32_t checksum = adler32(0L, Z_NULL, 0);
446 for (const DexFile::Header* header : headers) {
447 checksum = adler32_combine(checksum,
448 header->checksum_,
449 header->file_size_ - DexFile::kNumNonChecksumBytes);
450 }
451 *location_checksum = checksum;
452
453 const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
454 if (data_dir.empty() || Runtime::Current()->IsZygote()) {
455 *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
456 return false;
457 }
458 *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
459
460 std::string odex_filename;
461 std::string error_msg;
462 if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
463 LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
464 return false;
465 }
466
467 *vdex_filename = GetVdexFilename(odex_filename);
468 return true;
469}
470
471bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
472 DCHECK(basename.find('/') == std::string::npos);
473 // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
474 if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
475 !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) ||
476 !android::base::EndsWith(basename, kVdexExtension)) {
477 return false;
478 }
479 // Check that all characters between the prefix and extension are decimal digits.
480 for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
481 if (!std::isdigit(basename[i])) {
482 return false;
483 }
484 }
485 return true;
486}
487
Calin Juravle357c66d2017-05-04 01:57:17 +0000488static bool DexLocationToOdexNames(const std::string& location,
489 InstructionSet isa,
490 std::string* odex_filename,
491 std::string* oat_dir,
492 std::string* isa_dir,
493 std::string* error_msg) {
494 CHECK(odex_filename != nullptr);
495 CHECK(error_msg != nullptr);
496
497 // The odex file name is formed by replacing the dex_location extension with
498 // .odex and inserting an oat/<isa> directory. For example:
499 // location = /foo/bar/baz.jar
500 // odex_location = /foo/bar/oat/<isa>/baz.odex
501
502 // Find the directory portion of the dex location and add the oat/<isa>
503 // directory.
504 size_t pos = location.rfind('/');
505 if (pos == std::string::npos) {
506 *error_msg = "Dex location " + location + " has no directory.";
507 return false;
508 }
509 std::string dir = location.substr(0, pos+1);
510 // Add the oat directory.
511 dir += "oat";
512 if (oat_dir != nullptr) {
513 *oat_dir = dir;
514 }
515 // Add the isa directory
516 dir += "/" + std::string(GetInstructionSetString(isa));
517 if (isa_dir != nullptr) {
518 *isa_dir = dir;
519 }
520
521 // Get the base part of the file without the extension.
522 std::string file = location.substr(pos+1);
523 pos = file.rfind('.');
524 if (pos == std::string::npos) {
525 *error_msg = "Dex location " + location + " has no extension.";
526 return false;
527 }
528 std::string base = file.substr(0, pos);
529
530 *odex_filename = dir + "/" + base + ".odex";
531 return true;
532}
533
Richard Uhlerb81881d2016-04-19 13:08:04 -0700534bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
535 InstructionSet isa,
536 std::string* odex_filename,
537 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000538 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800539}
540
Richard Uhlerb81881d2016-04-19 13:08:04 -0700541bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
542 InstructionSet isa,
543 std::string* oat_filename,
544 std::string* error_msg) {
545 CHECK(oat_filename != nullptr);
546 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800547
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700548 // If ANDROID_DATA is not set, return false instead of aborting.
549 // This can occur for preopt when using a class loader context.
Roland Levillain2e3cb542019-04-05 18:00:04 +0100550 if (GetAndroidDataSafe(error_msg).empty()) {
Mathieu Chartier9e423af2018-05-14 10:08:29 -0700551 *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
552 return false;
553 }
554
Richard Uhler55b58b62016-08-12 09:05:13 -0700555 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
556 if (cache_dir.empty()) {
557 *error_msg = "Dalvik cache directory does not exist";
558 return false;
559 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700560
561 // TODO: The oat file assistant should be the definitive place for
562 // determining the oat file name from the dex location, not
563 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700564 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800565}
566
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000567const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
568 if (!required_dex_checksums_attempted_) {
569 required_dex_checksums_attempted_ = true;
570 required_dex_checksums_found_ = false;
571 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800572 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800573 const ArtDexFileLoader dex_file_loader;
574 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
575 &cached_required_dex_checksums_,
576 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000577 zip_fd_,
578 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000579 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700580 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800581 } else {
582 // This can happen if the original dex file has been stripped from the
583 // apk.
584 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700585 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800586
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000587 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700588 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800589 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000590 required_dex_checksums_found_ = true;
591 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700592 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Andreas Gampeb40d3612018-06-26 15:49:42 -0700593 const OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000594 if (odex_dex_file == nullptr) {
595 required_dex_checksums_found_ = false;
596 break;
597 }
598 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800599 }
600 }
601 }
602 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000603 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800604}
605
Vladimir Markobcd99be2019-03-22 16:21:31 +0000606bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
607 // Get the BCP from the oat file.
608 const char* oat_boot_class_path =
609 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
610 if (oat_boot_class_path == nullptr) {
611 return false;
612 }
613
614 // Check that the oat BCP is a prefix of current BCP locations and count components.
615 Runtime* runtime = Runtime::Current();
616 size_t component_count = 0u;
617 std::string_view remaining_bcp(oat_boot_class_path);
618 bool bcp_ok = false;
619 for (const std::string& location : runtime->GetBootClassPathLocations()) {
620 if (!StartsWith(remaining_bcp, location)) {
621 break;
622 }
623 remaining_bcp.remove_prefix(location.size());
624 ++component_count;
625 if (remaining_bcp.empty()) {
626 bcp_ok = true;
627 break;
628 }
629 if (!StartsWith(remaining_bcp, ":")) {
630 break;
631 }
632 remaining_bcp.remove_prefix(1u);
633 }
634 if (!bcp_ok) {
635 return false;
636 }
637
638 // Get the checksums.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000639 const char* oat_boot_class_path_checksums =
640 oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
Vladimir Markof3d88a82018-12-21 16:38:47 +0000641 if (oat_boot_class_path_checksums == nullptr) {
642 return false;
643 }
Vladimir Marko0ace5632018-12-14 11:11:47 +0000644
Vladimir Markobcd99be2019-03-22 16:21:31 +0000645 // Retrieve checksums for this portion of the BCP if we do not have them cached.
646 if (cached_boot_class_path_checksum_component_count_ != component_count) {
647 ArrayRef<const std::string> boot_class_path(runtime->GetBootClassPath());
Richard Uhler95e09672017-02-22 11:37:41 +0000648 std::string error_msg;
Vladimir Markobcd99be2019-03-22 16:21:31 +0000649 std::string boot_class_path_checksums = gc::space::ImageSpace::GetBootClassPathChecksums(
650 boot_class_path.SubArray(/* pos= */ 0u, component_count),
651 runtime->GetImageLocation(),
652 isa_,
653 runtime->GetImageSpaceLoadingOrder(),
654 &error_msg);
655 if (boot_class_path_checksums.empty()) {
656 VLOG(oat) << "No image for oat image checksum to match against.";
657
658 if (HasOriginalDexFiles()) {
659 return false;
660 }
661
662 // If there is no original dex file to fall back to, grudgingly accept
663 // the oat file. This could technically lead to crashes, but there's no
664 // way we could find a better oat file to use for this dex location,
665 // and it's better than being stuck in a boot loop with no way out.
666 // The problem will hopefully resolve itself the next time the runtime
667 // starts up.
668 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
669 << "Allow oat file use. This is potentially dangerous.";
670
671 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800672 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000673 cached_boot_class_path_checksum_component_count_ = component_count;
674 cached_boot_class_path_checksums_ = boot_class_path_checksums;
Richard Uhler66d874d2015-01-15 09:37:19 -0800675 }
Vladimir Markobcd99be2019-03-22 16:21:31 +0000676
677 // Compare the checksums.
678 return cached_boot_class_path_checksums_ == oat_boot_class_path_checksums;
Richard Uhler66d874d2015-01-15 09:37:19 -0800679}
680
Richard Uhler88bc6732016-11-14 14:38:03 +0000681OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700682 ScopedTrace trace("GetBestInfo");
Calin Juravle357c66d2017-05-04 01:57:17 +0000683 // TODO(calin): Document the side effects of class loading when
684 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700685 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000686 // If the parent of the dex file is writable it means that we can
687 // create the odex file. In this case we unconditionally pick the odex
688 // as the best oat file. This corresponds to the regular use case when
689 // apps gets installed or when they load private, secondary dex file.
690 // For apps on the system partition the odex location will not be
691 // writable and thus the oat location might be more up to date.
692 return odex_;
693 }
694
695 // We cannot write to the odex location. This must be a system app.
696
697 // If the oat location is usable take it.
698 if (oat_.IsUseable()) {
699 return oat_;
700 }
701
702 // The oat file is not usable but the odex file might be up to date.
703 // This is an indication that we are dealing with an up to date prebuilt
704 // (that doesn't need relocation).
705 if (odex_.Status() == kOatUpToDate) {
706 return odex_;
707 }
708
709 // The oat file is not usable and the odex file is not up to date.
710 // However we have access to the original dex file which means we can make
711 // the oat location up to date.
712 if (HasOriginalDexFiles()) {
713 return oat_;
714 }
715
716 // We got into the worst situation here:
717 // - the oat location is not usable
718 // - the prebuild odex location is not up to date
719 // - and we don't have the original dex file anymore (stripped).
720 // Pick the odex if it exists, or the oat if not.
721 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000722}
723
Andreas Gampea463b6a2016-08-12 21:53:32 -0700724std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800725 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100726 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800727 if (art_file.empty()) {
728 return nullptr;
729 }
730 std::string error_msg;
731 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700732 std::unique_ptr<gc::space::ImageSpace> ret =
733 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800734 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800735 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
736 }
737 return ret;
738}
739
Richard Uhler88bc6732016-11-14 14:38:03 +0000740OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
741 bool is_oat_location)
742 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700743{}
744
Richard Uhler88bc6732016-11-14 14:38:03 +0000745bool OatFileAssistant::OatFileInfo::IsOatLocation() {
746 return is_oat_location_;
747}
748
Richard Uhler743bf362016-04-19 15:39:37 -0700749const std::string* OatFileAssistant::OatFileInfo::Filename() {
750 return filename_provided_ ? &filename_ : nullptr;
751}
752
Richard Uhler03bc6592016-11-22 09:42:04 +0000753bool OatFileAssistant::OatFileInfo::IsUseable() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700754 ScopedTrace trace("IsUseable");
Richard Uhler03bc6592016-11-22 09:42:04 +0000755 switch (Status()) {
756 case kOatCannotOpen:
757 case kOatDexOutOfDate:
758 case kOatBootImageOutOfDate: return false;
759
Richard Uhler03bc6592016-11-22 09:42:04 +0000760 case kOatUpToDate: return true;
761 }
762 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700763}
764
765OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700766 ScopedTrace trace("Status");
Richard Uhler743bf362016-04-19 15:39:37 -0700767 if (!status_attempted_) {
768 status_attempted_ = true;
769 const OatFile* file = GetFile();
770 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000771 // Check to see if there is a vdex file we can make use of.
772 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700773 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700774 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700775 if (use_fd_) {
776 if (vdex_fd_ >= 0) {
777 struct stat s;
778 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
779 if (rc == -1) {
780 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
781 } else {
782 vdex = VdexFile::Open(vdex_fd_,
783 s.st_size,
784 vdex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100785 /*writable=*/ false,
786 /*low_4gb=*/ false,
787 /*unquicken=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700788 &error_msg);
789 }
790 }
791 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700792 vdex = VdexFile::Open(vdex_filename,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700793 /*writable=*/ false,
794 /*low_4gb=*/ false,
795 /*unquicken=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700796 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700797 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000798 if (vdex == nullptr) {
799 status_ = kOatCannotOpen;
800 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
801 } else {
802 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
803 // The vdex file does not contain enough information to determine
804 // whether it is up to date with respect to the boot image, so we
805 // assume it is out of date.
806 VLOG(oat) << error_msg;
807 status_ = kOatBootImageOutOfDate;
808 } else {
809 status_ = kOatDexOutOfDate;
810 }
811 }
Richard Uhler743bf362016-04-19 15:39:37 -0700812 } else {
813 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700814 VLOG(oat) << file->GetLocation() << " is " << status_
815 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700816 }
817 }
818 return status_;
819}
820
Richard Uhler70a84262016-11-08 16:51:51 +0000821OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -0700822 CompilerFilter::Filter target,
823 bool profile_changed,
824 bool downgrade,
David Brazdil89821862019-03-19 13:57:43 +0000825 ClassLoaderContext* context,
826 const std::vector<int>& context_fds) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700827
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700828 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
David Brazdil89821862019-03-19 13:57:43 +0000829 bool class_loader_context_okay = ClassLoaderContextIsOkay(context, context_fds);
Richard Uhler70a84262016-11-08 16:51:51 +0000830
Calin Juravle44e5efa2017-09-12 00:54:26 -0700831 // Only check the filter and relocation if the class loader context is ok.
832 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
833 if (class_loader_context_okay) {
834 if (filter_okay && Status() == kOatUpToDate) {
835 // The oat file is in good shape as is.
836 return kNoDexOptNeeded;
837 }
Richard Uhler70a84262016-11-08 16:51:51 +0000838
Calin Juravle44e5efa2017-09-12 00:54:26 -0700839 if (IsUseable()) {
840 return kDex2OatForFilter;
841 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100842
Calin Juravle44e5efa2017-09-12 00:54:26 -0700843 if (Status() == kOatBootImageOutOfDate) {
844 return kDex2OatForBootImage;
845 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100846 }
847
848 if (oat_file_assistant_->HasOriginalDexFiles()) {
849 return kDex2OatFromScratch;
850 } else {
851 // Otherwise there is nothing we can do, even if we want to.
852 return kNoDexOptNeeded;
853 }
Richard Uhler70a84262016-11-08 16:51:51 +0000854}
855
Richard Uhler743bf362016-04-19 15:39:37 -0700856const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
857 CHECK(!file_released_) << "GetFile called after oat file released.";
858 if (!load_attempted_) {
859 load_attempted_ = true;
860 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +0000861 bool executable = oat_file_assistant_->load_executable_;
862 if (executable && oat_file_assistant_->only_load_system_executable_) {
863 executable = LocationIsOnSystem(filename_.c_str());
864 }
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000865 VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
Richard Uhler743bf362016-04-19 15:39:37 -0700866 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700867 if (use_fd_) {
868 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100869 file_.reset(OatFile::Open(zip_fd_,
870 vdex_fd_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700871 oat_fd_,
872 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000873 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100874 /*low_4gb=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700875 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100876 /*reservation=*/ nullptr,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700877 &error_msg));
878 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700879 } else {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100880 file_.reset(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100881 filename_.c_str(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700882 filename_.c_str(),
Nicolas Geoffray29742602017-12-14 10:09:03 +0000883 executable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100884 /*low_4gb=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700885 oat_file_assistant_->dex_location_.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100886 /*reservation=*/ nullptr,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700887 &error_msg));
888 }
Richard Uhler743bf362016-04-19 15:39:37 -0700889 if (file_.get() == nullptr) {
890 VLOG(oat) << "OatFileAssistant test for existing oat file "
891 << filename_ << ": " << error_msg;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000892 } else {
893 VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
Richard Uhler743bf362016-04-19 15:39:37 -0700894 }
895 }
896 }
897 return file_.get();
898}
899
900bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700901 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -0700902 const OatFile* file = GetFile();
903 if (file == nullptr) {
904 return false;
905 }
906
907 CompilerFilter::Filter current = file->GetCompilerFilter();
908 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
909 VLOG(oat) << "Compiler filter not okay because Profile changed";
910 return false;
911 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700912 return downgrade ? !CompilerFilter::IsBetter(current, target) :
913 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -0700914}
915
David Brazdil89821862019-03-19 13:57:43 +0000916bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context,
917 const std::vector<int>& context_fds) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700918 if (context == nullptr) {
919 VLOG(oat) << "ClassLoaderContext check ignored: null context";
920 return true;
921 }
922
923 const OatFile* file = GetFile();
924 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -0700925 // No oat file means we have nothing to verify.
926 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700927 }
928
Calin Juravle20c46442017-09-12 00:54:26 -0700929 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -0700930 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -0700931 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -0700932 : "";
933
David Brazdil89821862019-03-19 13:57:43 +0000934 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir, context_fds)) {
Calin Juravle44e5efa2017-09-12 00:54:26 -0700935 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
936 return false;
937 }
938
Mathieu Chartieradc90862018-05-11 13:03:06 -0700939 const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) !=
940 ClassLoaderContext::VerificationResult::kMismatch;
Calin Juravle44e5efa2017-09-12 00:54:26 -0700941 if (!result) {
942 VLOG(oat) << "ClassLoaderContext check failed. Context was "
943 << file->GetClassLoaderContext()
944 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
945 }
946 return result;
947}
948
Richard Uhler743bf362016-04-19 15:39:37 -0700949bool OatFileAssistant::OatFileInfo::IsExecutable() {
950 const OatFile* file = GetFile();
951 return (file != nullptr && file->IsExecutable());
952}
953
Richard Uhler743bf362016-04-19 15:39:37 -0700954void OatFileAssistant::OatFileInfo::Reset() {
955 load_attempted_ = false;
956 file_.reset();
957 status_attempted_ = false;
958}
959
Nicolas Geoffray30025092018-04-19 14:43:29 +0100960void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
961 bool use_fd,
962 int zip_fd,
963 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700964 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -0700965 filename_provided_ = true;
966 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700967 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100968 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700969 vdex_fd_ = vdex_fd;
970 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -0700971 Reset();
972}
973
974std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
975 file_released_ = true;
976 return std::move(file_);
977}
978
Richard Uhler70a84262016-11-08 16:51:51 +0000979std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700980 ScopedTrace trace("ReleaseFileForUse");
Richard Uhler3e580bc2016-11-08 16:23:07 +0000981 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000982 return ReleaseFile();
983 }
984
985 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
986 << " attempting to fall back to interpreting oat file instead.";
987
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800988 switch (Status()) {
989 case kOatBootImageOutOfDate:
Andreas Gampe8f81de52018-03-06 08:39:21 -0800990 // OutOfDate may be either a mismatched image, or a missing image.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800991 if (oat_file_assistant_->HasOriginalDexFiles()) {
Andreas Gampe8f81de52018-03-06 08:39:21 -0800992 // If there are original dex files, it is better to use them (to avoid a potential
993 // quickening mismatch because the boot image changed).
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800994 break;
995 }
Andreas Gampe8f81de52018-03-06 08:39:21 -0800996 // If we do not accept the oat file, we may not have access to dex bytecode at all. Grudgingly
997 // go forward.
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800998 FALLTHROUGH_INTENDED;
999
Andreas Gampe9ea84d02018-03-02 09:32:03 -08001000 case kOatUpToDate:
1001 case kOatCannotOpen:
1002 case kOatDexOutOfDate:
1003 break;
Richard Uhler70a84262016-11-08 16:51:51 +00001004 }
Andreas Gampe9ea84d02018-03-02 09:32:03 -08001005
Richard Uhler70a84262016-11-08 16:51:51 +00001006 return std::unique_ptr<OatFile>();
1007}
Calin Juravle5f9a8012018-02-12 20:27:46 -08001008
1009// TODO(calin): we could provide a more refined status here
1010// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
1011// track more experiments but adds extra complexity.
1012void OatFileAssistant::GetOptimizationStatus(
1013 const std::string& filename,
1014 InstructionSet isa,
1015 std::string* out_compilation_filter,
1016 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001017 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
1018 // non-executable and check the status manually.
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001019 OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false);
Calin Juravle5f9a8012018-02-12 20:27:46 -08001020 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1021
1022 if (oat_file == nullptr) {
1023 *out_compilation_filter = "run-from-apk";
1024 *out_compilation_reason = "unknown";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001025 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001026 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001027
1028 OatStatus status = oat_file_assistant.GivenOatFileStatus(*oat_file);
1029 const char* reason = oat_file->GetCompilationReason();
1030 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1031 switch (status) {
1032 case OatStatus::kOatUpToDate:
1033 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
1034 return;
1035
1036 case kOatCannotOpen: // This should never happen, but be robust.
1037 *out_compilation_filter = "error";
1038 *out_compilation_reason = "error";
1039 return;
1040
1041 // kOatBootImageOutOfDate - The oat file is up to date with respect to the
1042 // dex file, but is out of date with respect to the boot image.
1043 case kOatBootImageOutOfDate:
1044 FALLTHROUGH_INTENDED;
1045 case kOatDexOutOfDate:
1046 if (oat_file_assistant.HasOriginalDexFiles()) {
1047 *out_compilation_filter = "run-from-apk-fallback";
1048 } else {
1049 *out_compilation_filter = "run-from-vdex-fallback";
1050 }
1051 return;
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001052 }
1053 LOG(FATAL) << "Unreachable";
1054 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001055}
1056
Richard Uhler66d874d2015-01-15 09:37:19 -08001057} // namespace art