blob: 241102ea83593946b39613a7348d7cfc3939f3d8 [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>
Andreas Gampe9186ced2016-12-12 14:28:21 -080022
Andreas Gampec15a2f42017-04-21 12:09:39 -070023#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080024#include "android-base/strings.h"
25
David Sehr891a50e2017-10-27 17:01:07 -070026#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080027#include "base/logging.h" // For VLOG.
Andreas Gampebd3e1ce2018-03-15 17:45:03 -070028#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070030#include "base/stl_util.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080032#include "class_linker.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "compiler_filter.h"
David Sehr013fd802018-01-11 22:55:24 -080034#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080036#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "gc/heap.h"
38#include "gc/space/image_space.h"
39#include "image.h"
40#include "oat.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000043#include "vdex_file.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070044#include "class_loader_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080045
46namespace art {
47
Richard Uhler69bcf2c2017-01-24 10:25:21 +000048using android::base::StringPrintf;
49
Narayan Kamath8943c1d2016-05-02 13:14:48 +010050std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
51 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000052 case OatFileAssistant::kOatCannotOpen:
53 stream << "kOatCannotOpen";
54 break;
55 case OatFileAssistant::kOatDexOutOfDate:
56 stream << "kOatDexOutOfDate";
57 break;
58 case OatFileAssistant::kOatBootImageOutOfDate:
59 stream << "kOatBootImageOutOfDate";
60 break;
61 case OatFileAssistant::kOatRelocationOutOfDate:
62 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010063 break;
64 case OatFileAssistant::kOatUpToDate:
65 stream << "kOatUpToDate";
66 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010067 default:
68 UNREACHABLE();
69 }
70
71 return stream;
72}
73
Richard Uhler66d874d2015-01-15 09:37:19 -080074OatFileAssistant::OatFileAssistant(const char* dex_location,
75 const InstructionSet isa,
Nicolas Geoffray29742602017-12-14 10:09:03 +000076 bool load_executable,
77 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070078 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000079 isa,
80 load_executable,
81 only_load_system_executable,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070082 -1 /* vdex_fd */,
83 -1 /* oat_fd */,
84 -1 /* zip_fd */) {}
85
86
87OatFileAssistant::OatFileAssistant(const char* dex_location,
88 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070089 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000090 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070091 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070092 int oat_fd,
93 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000094 : isa_(isa),
95 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +000096 only_load_system_executable_(only_load_system_executable),
Richard Uhler88bc6732016-11-14 14:38:03 +000097 odex_(this, /*is_oat_location*/ false),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070098 oat_(this, /*is_oat_location*/ true),
99 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -0700100 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +0000101
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700102 if (zip_fd < 0) {
103 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
104 << " oat_fd=" << oat_fd;
105 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
106 << " vdex_fd=" << vdex_fd;;
107 }
108
Calin Juravle099f8d62017-09-05 19:04:04 -0700109 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700110
Richard Uhler66d874d2015-01-15 09:37:19 -0800111 if (load_executable_ && isa != kRuntimeISA) {
112 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
113 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
114 load_executable_ = false;
115 }
116
Richard Uhler743bf362016-04-19 15:39:37 -0700117 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700118 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700119 std::string odex_file_name;
120 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Nicolas Geoffray30025092018-04-19 14:43:29 +0100121 odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700122 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700123 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
124 }
125
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700126 if (!UseFdToReadFiles()) {
127 // Get the oat filename.
128 std::string oat_file_name;
129 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
130 oat_.Reset(oat_file_name, false /* use_fd */);
131 } else {
132 LOG(WARNING) << "Failed to determine oat file name for dex location "
133 << dex_location_ << ": " << error_msg;
134 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000135 }
136
137 // Check if the dex directory is writable.
138 // This will be needed in most uses of OatFileAssistant and so it's OK to
139 // compute it eagerly. (the only use which will not make use of it is
140 // OatFileAssistant::GetStatusDump())
141 size_t pos = dex_location_.rfind('/');
142 if (pos == std::string::npos) {
143 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700144 } else if (!UseFdToReadFiles()) {
145 // We cannot test for parent access when using file descriptors. That's ok
146 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000147 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700148 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000149 dex_parent_writable_ = true;
150 } else {
151 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700152 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800153 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800154}
155
156OatFileAssistant::~OatFileAssistant() {
157 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100158 if (flock_.get() != nullptr) {
159 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800160 }
161}
162
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700163bool OatFileAssistant::UseFdToReadFiles() {
164 return zip_fd_ >= 0;
165}
166
Richard Uhler66d874d2015-01-15 09:37:19 -0800167bool OatFileAssistant::IsInBootClassPath() {
168 // Note: We check the current boot class path, regardless of the ISA
169 // specified by the user. This is okay, because the boot class path should
170 // be the same for all ISAs.
171 // TODO: Can we verify the boot class path is the same for all ISAs?
172 Runtime* runtime = Runtime::Current();
173 ClassLinker* class_linker = runtime->GetClassLinker();
174 const auto& boot_class_path = class_linker->GetBootClassPath();
175 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700176 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800177 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
178 return true;
179 }
180 }
181 return false;
182}
183
184bool OatFileAssistant::Lock(std::string* error_msg) {
185 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100186 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800187
Calin Juravle357c66d2017-05-04 01:57:17 +0000188 // Note the lock will only succeed for secondary dex files and in test
189 // environment.
190 //
191 // The lock *will fail* for all primary apks in a production environment.
192 // The app does not have permissions to create locks next to its dex location
193 // (be it system, data or vendor parition). We also cannot use the odex or
194 // oat location for the same reasoning.
195 //
196 // This is best effort and if it fails it's unlikely that we will be able
197 // to generate oat files anyway.
198 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800199
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100200 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
201 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100202 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800203 return false;
204 }
205 return true;
206}
207
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700208int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
209 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700210 bool downgrade,
211 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000212 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700213 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
214 profile_changed,
215 downgrade,
216 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000217 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
218 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800219 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000220 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800221}
222
Richard Uhlerf4b34872016-04-13 11:03:46 -0700223// Figure out the currently specified compile filter option in the runtime.
224// Returns true on success, false if the compiler filter is invalid, in which
225// case error_msg describes the problem.
226static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
227 std::string* error_msg) {
228 CHECK(filter != nullptr);
229 CHECK(error_msg != nullptr);
230
Calin Juravle357c66d2017-05-04 01:57:17 +0000231 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700232 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
233 if (option.starts_with("--compiler-filter=")) {
234 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
235 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
236 *error_msg = std::string("Unknown --compiler-filter value: ")
237 + std::string(compiler_filter_string);
238 return false;
239 }
240 }
241 }
242 return true;
243}
244
Richard Uhler01be6812016-05-17 10:34:52 -0700245bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000246 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700247}
248
Richard Uhler1e860612016-03-30 12:17:55 -0700249OatFileAssistant::ResultOfAttemptToUpdate
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700250OatFileAssistant::MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700251 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700252 std::string* error_msg) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700253 // The method doesn't use zip_fd_ and directly opens dex files at dex_locations_.
254 CHECK_EQ(-1, zip_fd_) << "MakeUpToDate should not be called with zip_fd";
255
Richard Uhlerf4b34872016-04-13 11:03:46 -0700256 CompilerFilter::Filter target;
257 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
258 return kUpdateNotAttempted;
259 }
260
Richard Uhler88bc6732016-11-14 14:38:03 +0000261 OatFileInfo& info = GetBestInfo();
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700262 // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291.
263 // This is actually not trivial in the current logic as it will interact with the collision
264 // check:
265 // - currently, if the context does not match but we have no collisions we still accept the
266 // oat file.
267 // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make
268 // the oat code up to date the collision check becomes useless.
269 // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files
270 // loaded in other processes). So it boils down to how far do we want to complicate
271 // the logic in order to enable the use of oat files. Maybe its time to try simplify it.
Calin Juravle44e5efa2017-09-12 00:54:26 -0700272 switch (info.GetDexOptNeeded(
273 target, profile_changed, /*downgrade*/ false, class_loader_context)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000274 case kNoDexOptNeeded:
275 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000276
Richard Uhler7225a8d2016-11-22 10:12:03 +0000277 // TODO: For now, don't bother with all the different ways we can call
278 // dex2oat to generate the oat file. Always generate the oat file as if it
279 // were kDex2OatFromScratch.
280 case kDex2OatFromScratch:
281 case kDex2OatForBootImage:
282 case kDex2OatForRelocation:
283 case kDex2OatForFilter:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700284 return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 }
286 UNREACHABLE();
287}
288
289std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000290 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800291}
292
Richard Uhler46cc64f2016-11-14 14:53:55 +0000293std::string OatFileAssistant::GetStatusDump() {
294 std::ostringstream status;
295 bool oat_file_exists = false;
296 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000297 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000298 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000299 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000300
Richard Uhler46cc64f2016-11-14 14:53:55 +0000301 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000302 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
303 const OatFile* file = oat_.GetFile();
304 if (file == nullptr) {
305 // If the file is null even though the status is not kOatCannotOpen, it
306 // means we must have a vdex file with no corresponding oat file. In
307 // this case we cannot determine the compilation filter. Indicate that
308 // we have only the vdex file instead.
309 status << "vdex-only";
310 } else {
311 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
312 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000313 }
314
Richard Uhler03bc6592016-11-22 09:42:04 +0000315 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000316 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000317 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000318
Richard Uhler46cc64f2016-11-14 14:53:55 +0000319 odex_file_exists = true;
320 if (oat_file_exists) {
321 status << "] ";
322 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000323 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
324 const OatFile* file = odex_.GetFile();
325 if (file == nullptr) {
326 status << "vdex-only";
327 } else {
328 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
329 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000330 }
331
332 if (!oat_file_exists && !odex_file_exists) {
333 status << "invalid[";
334 }
335
336 status << "]";
337 return status.str();
338}
339
Richard Uhler66d874d2015-01-15 09:37:19 -0800340std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700341 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800342 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700343 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
344 return dex_files;
345 } else {
346 return std::vector<std::unique_ptr<const DexFile>>();
347 }
348}
Richard Uhler66d874d2015-01-15 09:37:19 -0800349
Calin Juravle87e2cb62017-06-13 21:48:45 -0700350bool OatFileAssistant::LoadDexFiles(
351 const OatFile &oat_file,
352 const std::string& dex_location,
353 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000354 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800355 std::string error_msg;
356 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700357 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800358 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700359 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700360 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800361 }
362
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700363 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800364 if (dex_file.get() == nullptr) {
365 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700366 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800367 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700368 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800369
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000370 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700371 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700372 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000373 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700374 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000375 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800376 break;
377 }
378
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700379 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800380 if (dex_file.get() == nullptr) {
381 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700382 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800383 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700384 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800385 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700386 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800387}
388
Richard Uhler9b994ea2015-06-24 08:44:19 -0700389bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000390 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700391 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000392 // GetRequiredDexChecksums.
393 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700394 return has_original_dex_files_;
395}
396
Richard Uhler95abd042015-03-24 09:51:28 -0700397OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700398 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800399}
400
Richard Uhler95abd042015-03-24 09:51:28 -0700401OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700402 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800403}
404
Richard Uhler2f27abd2017-01-31 14:02:34 +0000405bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000406 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
407 if (required_dex_checksums == nullptr) {
408 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
409 return true;
410 }
411
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000412 uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles();
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000413 if (required_dex_checksums->size() != number_of_dex_files) {
414 *error_msg = StringPrintf("expected %zu dex files but found %u",
415 required_dex_checksums->size(),
416 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000417 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800418 }
419
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000420 for (uint32_t i = 0; i < number_of_dex_files; i++) {
421 uint32_t expected_checksum = (*required_dex_checksums)[i];
422 uint32_t actual_checksum = file.GetLocationChecksum(i);
423 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700424 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000425 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
426 "Expected: %u, actual: %u",
427 dex.c_str(),
428 expected_checksum,
429 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000430 return false;
431 }
432 }
433
Richard Uhler2f27abd2017-01-31 14:02:34 +0000434 return true;
435}
436
437bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000438 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
439 if (required_dex_checksums == nullptr) {
440 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
441 return true;
442 }
443
444 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
445 if (required_dex_checksums->size() != number_of_dex_files) {
446 *error_msg = StringPrintf("expected %zu dex files but found %u",
447 required_dex_checksums->size(),
448 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000449 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800450 }
451
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000452 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700453 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000454 uint32_t expected_checksum = (*required_dex_checksums)[i];
455 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
456 if (oat_dex_file == nullptr) {
457 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
458 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000460 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
461 if (expected_checksum != actual_checksum) {
462 VLOG(oat) << "Dex checksum does not match for dex: " << dex
463 << ". Expected: " << expected_checksum
464 << ", Actual: " << actual_checksum;
465 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800466 }
467 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000468 return true;
469}
470
471OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
472 // Verify the ART_USE_READ_BARRIER state.
473 // TODO: Don't fully reject files due to read barrier state. If they contain
474 // compiled code and are otherwise okay, we should return something like
475 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
476 // barrier state doesn't matter.
477 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
478 constexpr bool kRuntimeIsCC = kUseReadBarrier;
479 if (is_cc != kRuntimeIsCC) {
480 return kOatCannotOpen;
481 }
482
483 // Verify the dex checksum.
484 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000485 VdexFile* vdex = file.GetVdexFile();
486 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
487 LOG(ERROR) << error_msg;
488 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000489 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800490
Andreas Gampe29d38e72016-03-23 15:31:51 +0000491 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000492
Richard Uhler66d874d2015-01-15 09:37:19 -0800493 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
495 const ImageInfo* image_info = GetImageInfo();
496 if (image_info == nullptr) {
497 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000498
Richard Uhler76f5cb62016-04-04 13:30:16 -0700499 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000500 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700501 }
502
503 // If there is no original dex file to fall back to, grudgingly accept
504 // the oat file. This could technically lead to crashes, but there's no
505 // way we could find a better oat file to use for this dex location,
506 // and it's better than being stuck in a boot loop with no way out.
507 // The problem will hopefully resolve itself the next time the runtime
508 // starts up.
509 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
510 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000511 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000512 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000513 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000514 }
515 } else {
516 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800517 }
518
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000519 // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
520 DCHECK(required_dex_checksums_attempted_);
521 if (only_load_system_executable_ &&
522 !LocationIsOnSystem(file.GetLocation().c_str()) &&
523 file.ContainsDexCode() &&
524 zip_file_only_contains_uncompressed_dex_) {
525 LOG(ERROR) << "Not loading "
526 << dex_location_
527 << ": oat file has dex code, but APK has uncompressed dex code";
528 return kOatDexOutOfDate;
529 }
530
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100531 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000532 if (!file.IsPic()) {
533 const ImageInfo* image_info = GetImageInfo();
534 if (image_info == nullptr) {
535 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000536 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000537 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700538
Andreas Gampe29d38e72016-03-23 15:31:51 +0000539 // Verify the oat_data_begin recorded for the image in the oat file matches
540 // the actual oat_data_begin for boot.oat in the image.
541 const OatHeader& oat_header = file.GetOatHeader();
542 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
543 if (oat_data_begin != image_info->oat_data_begin) {
544 VLOG(oat) << file.GetLocation() <<
545 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
546 << " does not match actual image oat_data_begin ("
547 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000548 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000549 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700550
Andreas Gampe29d38e72016-03-23 15:31:51 +0000551 // Verify the oat_patch_delta recorded for the image in the oat file matches
552 // the actual oat_patch_delta for the image.
553 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
554 if (oat_patch_delta != image_info->patch_delta) {
555 VLOG(oat) << file.GetLocation() <<
556 ": Oat file image patch delta (" << oat_patch_delta << ")"
557 << " does not match actual image patch delta ("
558 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000559 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000560 }
561 } else {
562 // Oat files compiled in PIC mode do not require relocation.
563 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
564 }
565 } else {
566 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800567 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700568 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800569}
570
Calin Juravle357c66d2017-05-04 01:57:17 +0000571static bool DexLocationToOdexNames(const std::string& location,
572 InstructionSet isa,
573 std::string* odex_filename,
574 std::string* oat_dir,
575 std::string* isa_dir,
576 std::string* error_msg) {
577 CHECK(odex_filename != nullptr);
578 CHECK(error_msg != nullptr);
579
580 // The odex file name is formed by replacing the dex_location extension with
581 // .odex and inserting an oat/<isa> directory. For example:
582 // location = /foo/bar/baz.jar
583 // odex_location = /foo/bar/oat/<isa>/baz.odex
584
585 // Find the directory portion of the dex location and add the oat/<isa>
586 // directory.
587 size_t pos = location.rfind('/');
588 if (pos == std::string::npos) {
589 *error_msg = "Dex location " + location + " has no directory.";
590 return false;
591 }
592 std::string dir = location.substr(0, pos+1);
593 // Add the oat directory.
594 dir += "oat";
595 if (oat_dir != nullptr) {
596 *oat_dir = dir;
597 }
598 // Add the isa directory
599 dir += "/" + std::string(GetInstructionSetString(isa));
600 if (isa_dir != nullptr) {
601 *isa_dir = dir;
602 }
603
604 // Get the base part of the file without the extension.
605 std::string file = location.substr(pos+1);
606 pos = file.rfind('.');
607 if (pos == std::string::npos) {
608 *error_msg = "Dex location " + location + " has no extension.";
609 return false;
610 }
611 std::string base = file.substr(0, pos);
612
613 *odex_filename = dir + "/" + base + ".odex";
614 return true;
615}
616
617// Prepare a subcomponent of the odex directory.
618// (i.e. create and set the expected permissions on the path `dir`).
619static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
620 struct stat dir_stat;
621 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
622 // The directory exists. Check if it is indeed a directory.
623 if (!S_ISDIR(dir_stat.st_mode)) {
624 *error_msg = dir + " is not a dir";
625 return false;
626 } else {
627 // The dir is already on disk.
628 return true;
629 }
630 }
631
632 // Failed to stat. We need to create the directory.
633 if (errno != ENOENT) {
634 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
635 return false;
636 }
637
638 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
639 if (mkdir(dir.c_str(), mode) != 0) {
640 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
641 return false;
642 }
643 if (chmod(dir.c_str(), mode) != 0) {
644 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
645 return false;
646 }
647 return true;
648}
649
650// Prepares the odex directory for the given dex location.
651static bool PrepareOdexDirectories(const std::string& dex_location,
652 const std::string& expected_odex_location,
653 InstructionSet isa,
654 std::string* error_msg) {
655 std::string actual_odex_location;
656 std::string oat_dir;
657 std::string isa_dir;
658 if (!DexLocationToOdexNames(
659 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
660 return false;
661 }
662 DCHECK_EQ(expected_odex_location, actual_odex_location);
663
664 if (!PrepareDirectory(oat_dir, error_msg)) {
665 return false;
666 }
667 if (!PrepareDirectory(isa_dir, error_msg)) {
668 return false;
669 }
670 return true;
671}
672
Calin Juravled4215bb2017-09-20 11:54:21 -0700673class Dex2oatFileWrapper {
674 public:
675 explicit Dex2oatFileWrapper(File* file)
676 : file_(file),
677 unlink_file_at_destruction_(true) {
678 }
679
680 ~Dex2oatFileWrapper() {
681 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
682 file_->Erase(/*unlink*/ true);
683 }
684 }
685
686 File* GetFile() { return file_.get(); }
687
688 void DisableUnlinkAtDestruction() {
689 unlink_file_at_destruction_ = false;
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800690 }
Calin Juravled4215bb2017-09-20 11:54:21 -0700691
692 private:
693 std::unique_ptr<File> file_;
694 bool unlink_file_at_destruction_;
695};
696
Calin Juravle357c66d2017-05-04 01:57:17 +0000697OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700698 OatFileAssistant::OatFileInfo& info,
699 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700700 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700701 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800702 CHECK(error_msg != nullptr);
703
Richard Uhler8327cf72015-10-13 16:34:59 -0700704 Runtime* runtime = Runtime::Current();
705 if (!runtime->IsDex2OatEnabled()) {
706 *error_msg = "Generation of oat file for dex location " + dex_location_
707 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700708 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700709 }
710
Calin Juravle357c66d2017-05-04 01:57:17 +0000711 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700712 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800713 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700714 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800715 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000716 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700717 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800718
Richard Uhler66d874d2015-01-15 09:37:19 -0800719 // dex2oat ignores missing dex files and doesn't report an error.
720 // Check explicitly here so we can detect the error properly.
721 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000722 struct stat dex_path_stat;
723 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
724 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700725 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800726 }
727
Calin Juravle357c66d2017-05-04 01:57:17 +0000728 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
729 if (!info.IsOatLocation()) {
730 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
731 return kUpdateNotAttempted;
732 }
733 }
734
735 // Set the permissions for the oat and the vdex files.
736 // The user always gets read and write while the group and others propagate
737 // the reading access of the original dex file.
738 mode_t file_mode = S_IRUSR | S_IWUSR |
739 (dex_path_stat.st_mode & S_IRGRP) |
740 (dex_path_stat.st_mode & S_IROTH);
741
Calin Juravled4215bb2017-09-20 11:54:21 -0700742 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
743 File* vdex_file = vdex_file_wrapper.GetFile();
744 if (vdex_file == nullptr) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100745 *error_msg = "Generation of oat file " + oat_file_name
746 + " not attempted because the vdex file " + vdex_file_name
747 + " could not be opened.";
748 return kUpdateNotAttempted;
749 }
750
Calin Juravle357c66d2017-05-04 01:57:17 +0000751 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100752 *error_msg = "Generation of oat file " + oat_file_name
753 + " not attempted because the vdex file " + vdex_file_name
754 + " could not be made world readable.";
755 return kUpdateNotAttempted;
756 }
757
Calin Juravled4215bb2017-09-20 11:54:21 -0700758 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
759 File* oat_file = oat_file_wrapper.GetFile();
760 if (oat_file == nullptr) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700761 *error_msg = "Generation of oat file " + oat_file_name
762 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700763 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700764 }
765
Calin Juravle357c66d2017-05-04 01:57:17 +0000766 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700767 *error_msg = "Generation of oat file " + oat_file_name
768 + " not attempted because the oat file could not be made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700769 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700770 }
771
772 std::vector<std::string> args;
773 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000774 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700775 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
776 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000777 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700778 const std::string dex2oat_context = class_loader_context == nullptr
779 ? OatFile::kSpecialSharedLibrary
780 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
781 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700782
Richard Uhler66d874d2015-01-15 09:37:19 -0800783 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700784 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700785 }
786
David Brazdil7b49e6c2016-09-01 11:06:18 +0100787 if (vdex_file->FlushCloseOrErase() != 0) {
788 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100789 return kUpdateFailed;
790 }
791
Richard Uhler8327cf72015-10-13 16:34:59 -0700792 if (oat_file->FlushCloseOrErase() != 0) {
793 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700794 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800795 }
796
Calin Juravle357c66d2017-05-04 01:57:17 +0000797 // Mark that the odex file has changed and we should try to reload.
798 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700799 // We have compiled successfully. Disable the auto-unlink.
800 vdex_file_wrapper.DisableUnlinkAtDestruction();
801 oat_file_wrapper.DisableUnlinkAtDestruction();
802
Richard Uhler1e860612016-03-30 12:17:55 -0700803 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800804}
805
806bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
807 std::string* error_msg) {
808 Runtime* runtime = Runtime::Current();
809 std::string image_location = ImageLocation();
810 if (image_location.empty()) {
811 *error_msg = "No image location found for Dex2Oat.";
812 return false;
813 }
814
815 std::vector<std::string> argv;
816 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000817 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200818 argv.push_back("--debuggable");
819 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700820 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800821
822 if (!runtime->IsVerificationEnabled()) {
823 argv.push_back("--compiler-filter=verify-none");
824 }
825
826 if (runtime->MustRelocateIfPossible()) {
827 argv.push_back("--runtime-arg");
828 argv.push_back("-Xrelocate");
829 } else {
830 argv.push_back("--runtime-arg");
831 argv.push_back("-Xnorelocate");
832 }
833
834 if (!kIsTargetBuild) {
835 argv.push_back("--host");
836 }
837
838 argv.push_back("--boot-image=" + image_location);
839
840 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
841 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
842
843 argv.insert(argv.end(), args.begin(), args.end());
844
Andreas Gampe9186ced2016-12-12 14:28:21 -0800845 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800846 return Exec(argv, error_msg);
847}
848
Richard Uhlerb81881d2016-04-19 13:08:04 -0700849bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
850 InstructionSet isa,
851 std::string* odex_filename,
852 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000853 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800854}
855
Richard Uhlerb81881d2016-04-19 13:08:04 -0700856bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
857 InstructionSet isa,
858 std::string* oat_filename,
859 std::string* error_msg) {
860 CHECK(oat_filename != nullptr);
861 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800862
Richard Uhler55b58b62016-08-12 09:05:13 -0700863 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
864 if (cache_dir.empty()) {
865 *error_msg = "Dalvik cache directory does not exist";
866 return false;
867 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700868
869 // TODO: The oat file assistant should be the definitive place for
870 // determining the oat file name from the dex location, not
871 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700872 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800873}
874
Richard Uhler66d874d2015-01-15 09:37:19 -0800875std::string OatFileAssistant::ImageLocation() {
876 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000877 const std::vector<gc::space::ImageSpace*>& image_spaces =
878 runtime->GetHeap()->GetBootImageSpaces();
879 if (image_spaces.empty()) {
880 return "";
881 }
882 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800883}
884
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000885const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
886 if (!required_dex_checksums_attempted_) {
887 required_dex_checksums_attempted_ = true;
888 required_dex_checksums_found_ = false;
889 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800890 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800891 const ArtDexFileLoader dex_file_loader;
892 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
893 &cached_required_dex_checksums_,
894 &error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000895 zip_fd_,
896 &zip_file_only_contains_uncompressed_dex_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000897 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700898 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800899 } else {
900 // This can happen if the original dex file has been stripped from the
901 // apk.
902 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700903 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800904
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000905 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700906 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800907 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000908 required_dex_checksums_found_ = true;
909 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700910 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000911 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
912 if (odex_dex_file == nullptr) {
913 required_dex_checksums_found_ = false;
914 break;
915 }
916 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800917 }
918 }
919 }
920 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000921 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800922}
923
Richard Uhler95e09672017-02-22 11:37:41 +0000924std::unique_ptr<OatFileAssistant::ImageInfo>
925OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
926 CHECK(error_msg != nullptr);
927
Richard Uhler95e09672017-02-22 11:37:41 +0000928 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000929 std::unique_ptr<ImageInfo> info(new ImageInfo());
930 info->location = runtime->GetImageLocation();
931
932 std::unique_ptr<ImageHeader> image_header(
933 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
934 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000935 return nullptr;
936 }
937
Richard Uhler8f104862017-02-22 12:34:01 +0000938 info->oat_checksum = image_header->GetOatChecksum();
939 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
940 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000941 return info;
942}
943
Richard Uhler66d874d2015-01-15 09:37:19 -0800944const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
945 if (!image_info_load_attempted_) {
946 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000947 std::string error_msg;
948 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
949 if (cached_image_info_ == nullptr) {
950 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800951 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800952 }
Richard Uhler95e09672017-02-22 11:37:41 +0000953 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800954}
955
Richard Uhler88bc6732016-11-14 14:38:03 +0000956OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000957 // TODO(calin): Document the side effects of class loading when
958 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700959 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000960 // If the parent of the dex file is writable it means that we can
961 // create the odex file. In this case we unconditionally pick the odex
962 // as the best oat file. This corresponds to the regular use case when
963 // apps gets installed or when they load private, secondary dex file.
964 // For apps on the system partition the odex location will not be
965 // writable and thus the oat location might be more up to date.
966 return odex_;
967 }
968
969 // We cannot write to the odex location. This must be a system app.
970
971 // If the oat location is usable take it.
972 if (oat_.IsUseable()) {
973 return oat_;
974 }
975
976 // The oat file is not usable but the odex file might be up to date.
977 // This is an indication that we are dealing with an up to date prebuilt
978 // (that doesn't need relocation).
979 if (odex_.Status() == kOatUpToDate) {
980 return odex_;
981 }
982
983 // The oat file is not usable and the odex file is not up to date.
984 // However we have access to the original dex file which means we can make
985 // the oat location up to date.
986 if (HasOriginalDexFiles()) {
987 return oat_;
988 }
989
990 // We got into the worst situation here:
991 // - the oat location is not usable
992 // - the prebuild odex location is not up to date
993 // - and we don't have the original dex file anymore (stripped).
994 // Pick the odex if it exists, or the oat if not.
995 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000996}
997
Andreas Gampea463b6a2016-08-12 21:53:32 -0700998std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800999 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001000 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001001 if (art_file.empty()) {
1002 return nullptr;
1003 }
1004 std::string error_msg;
1005 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -07001006 std::unique_ptr<gc::space::ImageSpace> ret =
1007 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001008 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001009 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1010 }
1011 return ret;
1012}
1013
Richard Uhler88bc6732016-11-14 14:38:03 +00001014OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
1015 bool is_oat_location)
1016 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -07001017{}
1018
Richard Uhler88bc6732016-11-14 14:38:03 +00001019bool OatFileAssistant::OatFileInfo::IsOatLocation() {
1020 return is_oat_location_;
1021}
1022
Richard Uhler743bf362016-04-19 15:39:37 -07001023const std::string* OatFileAssistant::OatFileInfo::Filename() {
1024 return filename_provided_ ? &filename_ : nullptr;
1025}
1026
Richard Uhler03bc6592016-11-22 09:42:04 +00001027bool OatFileAssistant::OatFileInfo::IsUseable() {
1028 switch (Status()) {
1029 case kOatCannotOpen:
1030 case kOatDexOutOfDate:
1031 case kOatBootImageOutOfDate: return false;
1032
1033 case kOatRelocationOutOfDate:
1034 case kOatUpToDate: return true;
1035 }
1036 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001037}
1038
1039OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1040 if (!status_attempted_) {
1041 status_attempted_ = true;
1042 const OatFile* file = GetFile();
1043 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001044 // Check to see if there is a vdex file we can make use of.
1045 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001046 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001047 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001048 if (use_fd_) {
1049 if (vdex_fd_ >= 0) {
1050 struct stat s;
1051 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1052 if (rc == -1) {
1053 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1054 } else {
1055 vdex = VdexFile::Open(vdex_fd_,
1056 s.st_size,
1057 vdex_filename,
1058 false /*writable*/,
1059 false /*low_4gb*/,
1060 false /* unquicken */,
1061 &error_msg);
1062 }
1063 }
1064 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001065 vdex = VdexFile::Open(vdex_filename,
1066 false /*writeable*/,
1067 false /*low_4gb*/,
1068 false /*unquicken*/,
1069 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001070 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001071 if (vdex == nullptr) {
1072 status_ = kOatCannotOpen;
1073 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1074 } else {
1075 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1076 // The vdex file does not contain enough information to determine
1077 // whether it is up to date with respect to the boot image, so we
1078 // assume it is out of date.
1079 VLOG(oat) << error_msg;
1080 status_ = kOatBootImageOutOfDate;
1081 } else {
1082 status_ = kOatDexOutOfDate;
1083 }
1084 }
Richard Uhler743bf362016-04-19 15:39:37 -07001085 } else {
1086 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001087 VLOG(oat) << file->GetLocation() << " is " << status_
1088 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001089 }
1090 }
1091 return status_;
1092}
1093
Richard Uhler70a84262016-11-08 16:51:51 +00001094OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001095 CompilerFilter::Filter target,
1096 bool profile_changed,
1097 bool downgrade,
1098 ClassLoaderContext* context) {
1099
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001100 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001101 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001102 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001103
Calin Juravle44e5efa2017-09-12 00:54:26 -07001104 // Only check the filter and relocation if the class loader context is ok.
1105 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1106 if (class_loader_context_okay) {
1107 if (filter_okay && Status() == kOatUpToDate) {
1108 // The oat file is in good shape as is.
1109 return kNoDexOptNeeded;
1110 }
Richard Uhler70a84262016-11-08 16:51:51 +00001111
Calin Juravle44e5efa2017-09-12 00:54:26 -07001112 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1113 // If no compilation is desired, then it doesn't matter if the oat
1114 // file needs relocation. It's in good shape as is.
1115 return kNoDexOptNeeded;
1116 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001117
Calin Juravle44e5efa2017-09-12 00:54:26 -07001118 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1119 return kDex2OatForRelocation;
1120 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001121
Calin Juravle44e5efa2017-09-12 00:54:26 -07001122 if (IsUseable()) {
1123 return kDex2OatForFilter;
1124 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001125
Calin Juravle44e5efa2017-09-12 00:54:26 -07001126 if (Status() == kOatBootImageOutOfDate) {
1127 return kDex2OatForBootImage;
1128 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001129 }
1130
1131 if (oat_file_assistant_->HasOriginalDexFiles()) {
1132 return kDex2OatFromScratch;
1133 } else {
1134 // Otherwise there is nothing we can do, even if we want to.
1135 return kNoDexOptNeeded;
1136 }
Richard Uhler70a84262016-11-08 16:51:51 +00001137}
1138
Richard Uhler743bf362016-04-19 15:39:37 -07001139const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1140 CHECK(!file_released_) << "GetFile called after oat file released.";
1141 if (!load_attempted_) {
1142 load_attempted_ = true;
1143 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +00001144 bool executable = oat_file_assistant_->load_executable_;
1145 if (executable && oat_file_assistant_->only_load_system_executable_) {
1146 executable = LocationIsOnSystem(filename_.c_str());
1147 }
Richard Uhler743bf362016-04-19 15:39:37 -07001148 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001149 if (use_fd_) {
1150 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
Nicolas Geoffray30025092018-04-19 14:43:29 +01001151 file_.reset(OatFile::Open(zip_fd_,
1152 vdex_fd_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001153 oat_fd_,
1154 filename_.c_str(),
1155 nullptr,
1156 nullptr,
Nicolas Geoffray29742602017-12-14 10:09:03 +00001157 executable,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001158 false /* low_4gb */,
1159 oat_file_assistant_->dex_location_.c_str(),
1160 &error_msg));
1161 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001162 } else {
Nicolas Geoffray30025092018-04-19 14:43:29 +01001163 file_.reset(OatFile::Open(/* zip_fd */ -1,
1164 filename_.c_str(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001165 filename_.c_str(),
1166 nullptr,
1167 nullptr,
Nicolas Geoffray29742602017-12-14 10:09:03 +00001168 executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001169 false /* low_4gb */,
1170 oat_file_assistant_->dex_location_.c_str(),
1171 &error_msg));
1172 }
Richard Uhler743bf362016-04-19 15:39:37 -07001173 if (file_.get() == nullptr) {
1174 VLOG(oat) << "OatFileAssistant test for existing oat file "
1175 << filename_ << ": " << error_msg;
1176 }
1177 }
1178 }
1179 return file_.get();
1180}
1181
1182bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001183 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001184 const OatFile* file = GetFile();
1185 if (file == nullptr) {
1186 return false;
1187 }
1188
1189 CompilerFilter::Filter current = file->GetCompilerFilter();
1190 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1191 VLOG(oat) << "Compiler filter not okay because Profile changed";
1192 return false;
1193 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001194 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1195 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001196}
1197
Calin Juravle44e5efa2017-09-12 00:54:26 -07001198bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1199 if (context == nullptr) {
1200 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1201 return true;
1202 }
1203
1204 const OatFile* file = GetFile();
1205 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001206 // No oat file means we have nothing to verify.
1207 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001208 }
1209
Calin Juravle20c46442017-09-12 00:54:26 -07001210 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001211 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001212 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001213 : "";
1214
1215 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1216 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1217 return false;
1218 }
1219
Vladimir Markod8860b42018-05-02 14:58:12 +01001220
1221 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) ==
1222 ClassLoaderContext::VerificationResult::kVerifies;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001223 if (!result) {
1224 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1225 << file->GetClassLoaderContext()
1226 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1227 }
1228 return result;
1229}
1230
Richard Uhler743bf362016-04-19 15:39:37 -07001231bool OatFileAssistant::OatFileInfo::IsExecutable() {
1232 const OatFile* file = GetFile();
1233 return (file != nullptr && file->IsExecutable());
1234}
1235
Richard Uhler743bf362016-04-19 15:39:37 -07001236void OatFileAssistant::OatFileInfo::Reset() {
1237 load_attempted_ = false;
1238 file_.reset();
1239 status_attempted_ = false;
1240}
1241
Nicolas Geoffray30025092018-04-19 14:43:29 +01001242void OatFileAssistant::OatFileInfo::Reset(const std::string& filename,
1243 bool use_fd,
1244 int zip_fd,
1245 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001246 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001247 filename_provided_ = true;
1248 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001249 use_fd_ = use_fd;
Nicolas Geoffray30025092018-04-19 14:43:29 +01001250 zip_fd_ = zip_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001251 vdex_fd_ = vdex_fd;
1252 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001253 Reset();
1254}
1255
1256std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1257 file_released_ = true;
1258 return std::move(file_);
1259}
1260
Richard Uhler70a84262016-11-08 16:51:51 +00001261std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001262 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001263 return ReleaseFile();
1264 }
1265
1266 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1267 << " attempting to fall back to interpreting oat file instead.";
1268
Richard Uhler03bc6592016-11-22 09:42:04 +00001269 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001270 return ReleaseFile();
1271 }
1272
Andreas Gampe9ea84d02018-03-02 09:32:03 -08001273 switch (Status()) {
1274 case kOatBootImageOutOfDate:
Andreas Gampe8f81de52018-03-06 08:39:21 -08001275 // OutOfDate may be either a mismatched image, or a missing image.
Andreas Gampe9ea84d02018-03-02 09:32:03 -08001276 if (oat_file_assistant_->HasOriginalDexFiles()) {
Andreas Gampe8f81de52018-03-06 08:39:21 -08001277 // If there are original dex files, it is better to use them (to avoid a potential
1278 // quickening mismatch because the boot image changed).
Andreas Gampe9ea84d02018-03-02 09:32:03 -08001279 break;
1280 }
Andreas Gampe8f81de52018-03-06 08:39:21 -08001281 // If we do not accept the oat file, we may not have access to dex bytecode at all. Grudgingly
1282 // go forward.
Andreas Gampe9ea84d02018-03-02 09:32:03 -08001283 FALLTHROUGH_INTENDED;
1284
1285 case kOatRelocationOutOfDate:
1286 // We are loading an oat file for runtime use that needs relocation.
1287 // Reload the file non-executable to ensure that we interpret out of the
1288 // dex code in the oat file rather than trying to execute the unrelocated
1289 // compiled code.
1290 oat_file_assistant_->load_executable_ = false;
1291 Reset();
1292 if (IsUseable()) {
1293 CHECK(!IsExecutable());
1294 return ReleaseFile();
1295 }
1296 break;
1297
1298 case kOatUpToDate:
1299 case kOatCannotOpen:
1300 case kOatDexOutOfDate:
1301 break;
Richard Uhler70a84262016-11-08 16:51:51 +00001302 }
Andreas Gampe9ea84d02018-03-02 09:32:03 -08001303
Richard Uhler70a84262016-11-08 16:51:51 +00001304 return std::unique_ptr<OatFile>();
1305}
Calin Juravle5f9a8012018-02-12 20:27:46 -08001306
1307// TODO(calin): we could provide a more refined status here
1308// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
1309// track more experiments but adds extra complexity.
1310void OatFileAssistant::GetOptimizationStatus(
1311 const std::string& filename,
1312 InstructionSet isa,
1313 std::string* out_compilation_filter,
1314 std::string* out_compilation_reason) {
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001315 // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
1316 // non-executable and check the status manually.
1317 OatFileAssistant oat_file_assistant(filename.c_str(), isa, false /* load_executable */);
Calin Juravle5f9a8012018-02-12 20:27:46 -08001318 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1319
1320 if (oat_file == nullptr) {
1321 *out_compilation_filter = "run-from-apk";
1322 *out_compilation_reason = "unknown";
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001323 return;
Calin Juravle5f9a8012018-02-12 20:27:46 -08001324 }
Andreas Gampebd3e1ce2018-03-15 17:45:03 -07001325
1326 OatStatus status = oat_file_assistant.GivenOatFileStatus(*oat_file);
1327 const char* reason = oat_file->GetCompilationReason();
1328 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1329 switch (status) {
1330 case OatStatus::kOatUpToDate:
1331 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
1332 return;
1333
1334 case kOatCannotOpen: // This should never happen, but be robust.
1335 *out_compilation_filter = "error";
1336 *out_compilation_reason = "error";
1337 return;
1338
1339 // kOatBootImageOutOfDate - The oat file is up to date with respect to the
1340 // dex file, but is out of date with respect to the boot image.
1341 case kOatBootImageOutOfDate:
1342 FALLTHROUGH_INTENDED;
1343 case kOatDexOutOfDate:
1344 if (oat_file_assistant.HasOriginalDexFiles()) {
1345 *out_compilation_filter = "run-from-apk-fallback";
1346 } else {
1347 *out_compilation_filter = "run-from-vdex-fallback";
1348 }
1349 return;
1350
1351 case kOatRelocationOutOfDate:
1352 // On relocation-out-of-date, we'd run the dex code.
1353 *out_compilation_filter = "run-from-vdex-fallback";
1354 return;
1355 }
1356 LOG(FATAL) << "Unreachable";
1357 UNREACHABLE();
Calin Juravle5f9a8012018-02-12 20:27:46 -08001358}
1359
Richard Uhler66d874d2015-01-15 09:37:19 -08001360} // namespace art