blob: 0170073e2902320c7da91c9f11f6b63db33a65c0 [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 Gampe5678db52017-06-08 14:11:18 -070028#include "base/stl_util.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "class_linker.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "compiler_filter.h"
David Sehr013fd802018-01-11 22:55:24 -080031#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080032#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080033#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "gc/heap.h"
35#include "gc/space/image_space.h"
36#include "image.h"
37#include "oat.h"
38#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080039#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070040#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000042#include "vdex_file.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070043#include "class_loader_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080044
45namespace art {
46
Richard Uhler69bcf2c2017-01-24 10:25:21 +000047using android::base::StringPrintf;
48
Narayan Kamath8943c1d2016-05-02 13:14:48 +010049std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
50 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000051 case OatFileAssistant::kOatCannotOpen:
52 stream << "kOatCannotOpen";
53 break;
54 case OatFileAssistant::kOatDexOutOfDate:
55 stream << "kOatDexOutOfDate";
56 break;
57 case OatFileAssistant::kOatBootImageOutOfDate:
58 stream << "kOatBootImageOutOfDate";
59 break;
60 case OatFileAssistant::kOatRelocationOutOfDate:
61 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010062 break;
63 case OatFileAssistant::kOatUpToDate:
64 stream << "kOatUpToDate";
65 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010066 default:
67 UNREACHABLE();
68 }
69
70 return stream;
71}
72
Richard Uhler66d874d2015-01-15 09:37:19 -080073OatFileAssistant::OatFileAssistant(const char* dex_location,
74 const InstructionSet isa,
Nicolas Geoffray29742602017-12-14 10:09:03 +000075 bool load_executable,
76 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070077 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000078 isa,
79 load_executable,
80 only_load_system_executable,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070081 -1 /* vdex_fd */,
82 -1 /* oat_fd */,
83 -1 /* zip_fd */) {}
84
85
86OatFileAssistant::OatFileAssistant(const char* dex_location,
87 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070088 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000089 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070090 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070091 int oat_fd,
92 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000093 : isa_(isa),
94 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +000095 only_load_system_executable_(only_load_system_executable),
Richard Uhler88bc6732016-11-14 14:38:03 +000096 odex_(this, /*is_oat_location*/ false),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070097 oat_(this, /*is_oat_location*/ true),
98 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -070099 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +0000100
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700101 if (zip_fd < 0) {
102 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
103 << " oat_fd=" << oat_fd;
104 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
105 << " vdex_fd=" << vdex_fd;;
106 }
107
Calin Juravle099f8d62017-09-05 19:04:04 -0700108 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700109
Richard Uhler66d874d2015-01-15 09:37:19 -0800110 if (load_executable_ && isa != kRuntimeISA) {
111 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
112 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
113 load_executable_ = false;
114 }
115
Richard Uhler743bf362016-04-19 15:39:37 -0700116 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700117 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700118 std::string odex_file_name;
119 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700120 odex_.Reset(odex_file_name, UseFdToReadFiles(), vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700121 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700122 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
123 }
124
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700125 if (!UseFdToReadFiles()) {
126 // Get the oat filename.
127 std::string oat_file_name;
128 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
129 oat_.Reset(oat_file_name, false /* use_fd */);
130 } else {
131 LOG(WARNING) << "Failed to determine oat file name for dex location "
132 << dex_location_ << ": " << error_msg;
133 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000134 }
135
136 // Check if the dex directory is writable.
137 // This will be needed in most uses of OatFileAssistant and so it's OK to
138 // compute it eagerly. (the only use which will not make use of it is
139 // OatFileAssistant::GetStatusDump())
140 size_t pos = dex_location_.rfind('/');
141 if (pos == std::string::npos) {
142 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700143 } else if (!UseFdToReadFiles()) {
144 // We cannot test for parent access when using file descriptors. That's ok
145 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000146 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700147 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000148 dex_parent_writable_ = true;
149 } else {
150 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700151 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800152 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800153}
154
155OatFileAssistant::~OatFileAssistant() {
156 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100157 if (flock_.get() != nullptr) {
158 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800159 }
160}
161
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700162bool OatFileAssistant::UseFdToReadFiles() {
163 return zip_fd_ >= 0;
164}
165
Richard Uhler66d874d2015-01-15 09:37:19 -0800166bool OatFileAssistant::IsInBootClassPath() {
167 // Note: We check the current boot class path, regardless of the ISA
168 // specified by the user. This is okay, because the boot class path should
169 // be the same for all ISAs.
170 // TODO: Can we verify the boot class path is the same for all ISAs?
171 Runtime* runtime = Runtime::Current();
172 ClassLinker* class_linker = runtime->GetClassLinker();
173 const auto& boot_class_path = class_linker->GetBootClassPath();
174 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700175 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800176 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
177 return true;
178 }
179 }
180 return false;
181}
182
183bool OatFileAssistant::Lock(std::string* error_msg) {
184 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100185 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800186
Calin Juravle357c66d2017-05-04 01:57:17 +0000187 // Note the lock will only succeed for secondary dex files and in test
188 // environment.
189 //
190 // The lock *will fail* for all primary apks in a production environment.
191 // The app does not have permissions to create locks next to its dex location
192 // (be it system, data or vendor parition). We also cannot use the odex or
193 // oat location for the same reasoning.
194 //
195 // This is best effort and if it fails it's unlikely that we will be able
196 // to generate oat files anyway.
197 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800198
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100199 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
200 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100201 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800202 return false;
203 }
204 return true;
205}
206
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700207int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
208 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700209 bool downgrade,
210 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000211 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700212 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
213 profile_changed,
214 downgrade,
215 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000216 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
217 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800218 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000219 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800220}
221
Richard Uhlerf4b34872016-04-13 11:03:46 -0700222// Figure out the currently specified compile filter option in the runtime.
223// Returns true on success, false if the compiler filter is invalid, in which
224// case error_msg describes the problem.
225static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
226 std::string* error_msg) {
227 CHECK(filter != nullptr);
228 CHECK(error_msg != nullptr);
229
Calin Juravle357c66d2017-05-04 01:57:17 +0000230 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700231 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
232 if (option.starts_with("--compiler-filter=")) {
233 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
234 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
235 *error_msg = std::string("Unknown --compiler-filter value: ")
236 + std::string(compiler_filter_string);
237 return false;
238 }
239 }
240 }
241 return true;
242}
243
Richard Uhler01be6812016-05-17 10:34:52 -0700244bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000245 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700246}
247
Richard Uhler1e860612016-03-30 12:17:55 -0700248OatFileAssistant::ResultOfAttemptToUpdate
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700249OatFileAssistant::MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700250 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700251 std::string* error_msg) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700252 // The method doesn't use zip_fd_ and directly opens dex files at dex_locations_.
253 CHECK_EQ(-1, zip_fd_) << "MakeUpToDate should not be called with zip_fd";
254
Richard Uhlerf4b34872016-04-13 11:03:46 -0700255 CompilerFilter::Filter target;
256 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
257 return kUpdateNotAttempted;
258 }
259
Richard Uhler88bc6732016-11-14 14:38:03 +0000260 OatFileInfo& info = GetBestInfo();
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700261 // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291.
262 // This is actually not trivial in the current logic as it will interact with the collision
263 // check:
264 // - currently, if the context does not match but we have no collisions we still accept the
265 // oat file.
266 // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make
267 // the oat code up to date the collision check becomes useless.
268 // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files
269 // loaded in other processes). So it boils down to how far do we want to complicate
270 // 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 -0700271 switch (info.GetDexOptNeeded(
272 target, profile_changed, /*downgrade*/ false, class_loader_context)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000273 case kNoDexOptNeeded:
274 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000275
Richard Uhler7225a8d2016-11-22 10:12:03 +0000276 // TODO: For now, don't bother with all the different ways we can call
277 // dex2oat to generate the oat file. Always generate the oat file as if it
278 // were kDex2OatFromScratch.
279 case kDex2OatFromScratch:
280 case kDex2OatForBootImage:
281 case kDex2OatForRelocation:
282 case kDex2OatForFilter:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700283 return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800284 }
285 UNREACHABLE();
286}
287
288std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000289 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800290}
291
Richard Uhler46cc64f2016-11-14 14:53:55 +0000292std::string OatFileAssistant::GetStatusDump() {
293 std::ostringstream status;
294 bool oat_file_exists = false;
295 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000296 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000297 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000298 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000299
Richard Uhler46cc64f2016-11-14 14:53:55 +0000300 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000301 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
302 const OatFile* file = oat_.GetFile();
303 if (file == nullptr) {
304 // If the file is null even though the status is not kOatCannotOpen, it
305 // means we must have a vdex file with no corresponding oat file. In
306 // this case we cannot determine the compilation filter. Indicate that
307 // we have only the vdex file instead.
308 status << "vdex-only";
309 } else {
310 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
311 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000312 }
313
Richard Uhler03bc6592016-11-22 09:42:04 +0000314 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000315 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000316 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000317
Richard Uhler46cc64f2016-11-14 14:53:55 +0000318 odex_file_exists = true;
319 if (oat_file_exists) {
320 status << "] ";
321 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000322 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
323 const OatFile* file = odex_.GetFile();
324 if (file == nullptr) {
325 status << "vdex-only";
326 } else {
327 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
328 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000329 }
330
331 if (!oat_file_exists && !odex_file_exists) {
332 status << "invalid[";
333 }
334
335 status << "]";
336 return status.str();
337}
338
Richard Uhler66d874d2015-01-15 09:37:19 -0800339std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700340 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800341 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700342 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
343 return dex_files;
344 } else {
345 return std::vector<std::unique_ptr<const DexFile>>();
346 }
347}
Richard Uhler66d874d2015-01-15 09:37:19 -0800348
Calin Juravle87e2cb62017-06-13 21:48:45 -0700349bool OatFileAssistant::LoadDexFiles(
350 const OatFile &oat_file,
351 const std::string& dex_location,
352 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000353 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800354 std::string error_msg;
355 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700356 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800357 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700358 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700359 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800360 }
361
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700362 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800363 if (dex_file.get() == nullptr) {
364 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700365 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800366 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700367 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800368
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000369 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700370 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700371 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000372 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700373 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000374 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800375 break;
376 }
377
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700378 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800379 if (dex_file.get() == nullptr) {
380 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700381 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800382 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700383 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800384 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700385 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800386}
387
Richard Uhler9b994ea2015-06-24 08:44:19 -0700388bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000389 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700390 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000391 // GetRequiredDexChecksums.
392 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700393 return has_original_dex_files_;
394}
395
Richard Uhler95abd042015-03-24 09:51:28 -0700396OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700397 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800398}
399
Richard Uhler95abd042015-03-24 09:51:28 -0700400OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700401 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800402}
403
Richard Uhler2f27abd2017-01-31 14:02:34 +0000404bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000405 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
406 if (required_dex_checksums == nullptr) {
407 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
408 return true;
409 }
410
411 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
412 if (required_dex_checksums->size() != number_of_dex_files) {
413 *error_msg = StringPrintf("expected %zu dex files but found %u",
414 required_dex_checksums->size(),
415 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000416 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800417 }
418
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000419 for (uint32_t i = 0; i < number_of_dex_files; i++) {
420 uint32_t expected_checksum = (*required_dex_checksums)[i];
421 uint32_t actual_checksum = file.GetLocationChecksum(i);
422 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700423 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000424 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
425 "Expected: %u, actual: %u",
426 dex.c_str(),
427 expected_checksum,
428 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000429 return false;
430 }
431 }
432
Richard Uhler2f27abd2017-01-31 14:02:34 +0000433 return true;
434}
435
436bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000437 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
438 if (required_dex_checksums == nullptr) {
439 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
440 return true;
441 }
442
443 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
444 if (required_dex_checksums->size() != number_of_dex_files) {
445 *error_msg = StringPrintf("expected %zu dex files but found %u",
446 required_dex_checksums->size(),
447 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000448 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 }
450
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000451 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700452 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000453 uint32_t expected_checksum = (*required_dex_checksums)[i];
454 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
455 if (oat_dex_file == nullptr) {
456 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
457 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800458 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000459 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
460 if (expected_checksum != actual_checksum) {
461 VLOG(oat) << "Dex checksum does not match for dex: " << dex
462 << ". Expected: " << expected_checksum
463 << ", Actual: " << actual_checksum;
464 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800465 }
466 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000467 return true;
468}
469
470OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
471 // Verify the ART_USE_READ_BARRIER state.
472 // TODO: Don't fully reject files due to read barrier state. If they contain
473 // compiled code and are otherwise okay, we should return something like
474 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
475 // barrier state doesn't matter.
476 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
477 constexpr bool kRuntimeIsCC = kUseReadBarrier;
478 if (is_cc != kRuntimeIsCC) {
479 return kOatCannotOpen;
480 }
481
482 // Verify the dex checksum.
483 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000484 VdexFile* vdex = file.GetVdexFile();
485 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
486 LOG(ERROR) << error_msg;
487 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000488 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800489
Andreas Gampe29d38e72016-03-23 15:31:51 +0000490 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000491
Richard Uhler66d874d2015-01-15 09:37:19 -0800492 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000493 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
494 const ImageInfo* image_info = GetImageInfo();
495 if (image_info == nullptr) {
496 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000497
Richard Uhler76f5cb62016-04-04 13:30:16 -0700498 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000499 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700500 }
501
502 // If there is no original dex file to fall back to, grudgingly accept
503 // the oat file. This could technically lead to crashes, but there's no
504 // way we could find a better oat file to use for this dex location,
505 // and it's better than being stuck in a boot loop with no way out.
506 // The problem will hopefully resolve itself the next time the runtime
507 // starts up.
508 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
509 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000510 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000511 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000512 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000513 }
514 } else {
515 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800516 }
517
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100518 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000519 if (!file.IsPic()) {
520 const ImageInfo* image_info = GetImageInfo();
521 if (image_info == nullptr) {
522 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000523 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000524 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700525
Andreas Gampe29d38e72016-03-23 15:31:51 +0000526 // Verify the oat_data_begin recorded for the image in the oat file matches
527 // the actual oat_data_begin for boot.oat in the image.
528 const OatHeader& oat_header = file.GetOatHeader();
529 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
530 if (oat_data_begin != image_info->oat_data_begin) {
531 VLOG(oat) << file.GetLocation() <<
532 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
533 << " does not match actual image oat_data_begin ("
534 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000535 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000536 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700537
Andreas Gampe29d38e72016-03-23 15:31:51 +0000538 // Verify the oat_patch_delta recorded for the image in the oat file matches
539 // the actual oat_patch_delta for the image.
540 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
541 if (oat_patch_delta != image_info->patch_delta) {
542 VLOG(oat) << file.GetLocation() <<
543 ": Oat file image patch delta (" << oat_patch_delta << ")"
544 << " does not match actual image patch delta ("
545 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000546 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000547 }
548 } else {
549 // Oat files compiled in PIC mode do not require relocation.
550 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
551 }
552 } else {
553 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800554 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700555 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800556}
557
Calin Juravle357c66d2017-05-04 01:57:17 +0000558static bool DexLocationToOdexNames(const std::string& location,
559 InstructionSet isa,
560 std::string* odex_filename,
561 std::string* oat_dir,
562 std::string* isa_dir,
563 std::string* error_msg) {
564 CHECK(odex_filename != nullptr);
565 CHECK(error_msg != nullptr);
566
567 // The odex file name is formed by replacing the dex_location extension with
568 // .odex and inserting an oat/<isa> directory. For example:
569 // location = /foo/bar/baz.jar
570 // odex_location = /foo/bar/oat/<isa>/baz.odex
571
572 // Find the directory portion of the dex location and add the oat/<isa>
573 // directory.
574 size_t pos = location.rfind('/');
575 if (pos == std::string::npos) {
576 *error_msg = "Dex location " + location + " has no directory.";
577 return false;
578 }
579 std::string dir = location.substr(0, pos+1);
580 // Add the oat directory.
581 dir += "oat";
582 if (oat_dir != nullptr) {
583 *oat_dir = dir;
584 }
585 // Add the isa directory
586 dir += "/" + std::string(GetInstructionSetString(isa));
587 if (isa_dir != nullptr) {
588 *isa_dir = dir;
589 }
590
591 // Get the base part of the file without the extension.
592 std::string file = location.substr(pos+1);
593 pos = file.rfind('.');
594 if (pos == std::string::npos) {
595 *error_msg = "Dex location " + location + " has no extension.";
596 return false;
597 }
598 std::string base = file.substr(0, pos);
599
600 *odex_filename = dir + "/" + base + ".odex";
601 return true;
602}
603
604// Prepare a subcomponent of the odex directory.
605// (i.e. create and set the expected permissions on the path `dir`).
606static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
607 struct stat dir_stat;
608 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
609 // The directory exists. Check if it is indeed a directory.
610 if (!S_ISDIR(dir_stat.st_mode)) {
611 *error_msg = dir + " is not a dir";
612 return false;
613 } else {
614 // The dir is already on disk.
615 return true;
616 }
617 }
618
619 // Failed to stat. We need to create the directory.
620 if (errno != ENOENT) {
621 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
622 return false;
623 }
624
625 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
626 if (mkdir(dir.c_str(), mode) != 0) {
627 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
628 return false;
629 }
630 if (chmod(dir.c_str(), mode) != 0) {
631 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
632 return false;
633 }
634 return true;
635}
636
637// Prepares the odex directory for the given dex location.
638static bool PrepareOdexDirectories(const std::string& dex_location,
639 const std::string& expected_odex_location,
640 InstructionSet isa,
641 std::string* error_msg) {
642 std::string actual_odex_location;
643 std::string oat_dir;
644 std::string isa_dir;
645 if (!DexLocationToOdexNames(
646 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
647 return false;
648 }
649 DCHECK_EQ(expected_odex_location, actual_odex_location);
650
651 if (!PrepareDirectory(oat_dir, error_msg)) {
652 return false;
653 }
654 if (!PrepareDirectory(isa_dir, error_msg)) {
655 return false;
656 }
657 return true;
658}
659
Calin Juravled4215bb2017-09-20 11:54:21 -0700660class Dex2oatFileWrapper {
661 public:
662 explicit Dex2oatFileWrapper(File* file)
663 : file_(file),
664 unlink_file_at_destruction_(true) {
665 }
666
667 ~Dex2oatFileWrapper() {
668 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
669 file_->Erase(/*unlink*/ true);
670 }
671 }
672
673 File* GetFile() { return file_.get(); }
674
675 void DisableUnlinkAtDestruction() {
676 unlink_file_at_destruction_ = false;
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800677 }
Calin Juravled4215bb2017-09-20 11:54:21 -0700678
679 private:
680 std::unique_ptr<File> file_;
681 bool unlink_file_at_destruction_;
682};
683
Calin Juravle357c66d2017-05-04 01:57:17 +0000684OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700685 OatFileAssistant::OatFileInfo& info,
686 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700687 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700688 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800689 CHECK(error_msg != nullptr);
690
Richard Uhler8327cf72015-10-13 16:34:59 -0700691 Runtime* runtime = Runtime::Current();
692 if (!runtime->IsDex2OatEnabled()) {
693 *error_msg = "Generation of oat file for dex location " + dex_location_
694 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700695 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700696 }
697
Calin Juravle357c66d2017-05-04 01:57:17 +0000698 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700699 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800700 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700701 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800702 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000703 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700704 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800705
Richard Uhler66d874d2015-01-15 09:37:19 -0800706 // dex2oat ignores missing dex files and doesn't report an error.
707 // Check explicitly here so we can detect the error properly.
708 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000709 struct stat dex_path_stat;
710 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
711 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700712 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800713 }
714
Calin Juravle357c66d2017-05-04 01:57:17 +0000715 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
716 if (!info.IsOatLocation()) {
717 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
718 return kUpdateNotAttempted;
719 }
720 }
721
722 // Set the permissions for the oat and the vdex files.
723 // The user always gets read and write while the group and others propagate
724 // the reading access of the original dex file.
725 mode_t file_mode = S_IRUSR | S_IWUSR |
726 (dex_path_stat.st_mode & S_IRGRP) |
727 (dex_path_stat.st_mode & S_IROTH);
728
Calin Juravled4215bb2017-09-20 11:54:21 -0700729 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
730 File* vdex_file = vdex_file_wrapper.GetFile();
731 if (vdex_file == nullptr) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100732 *error_msg = "Generation of oat file " + oat_file_name
733 + " not attempted because the vdex file " + vdex_file_name
734 + " could not be opened.";
735 return kUpdateNotAttempted;
736 }
737
Calin Juravle357c66d2017-05-04 01:57:17 +0000738 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100739 *error_msg = "Generation of oat file " + oat_file_name
740 + " not attempted because the vdex file " + vdex_file_name
741 + " could not be made world readable.";
742 return kUpdateNotAttempted;
743 }
744
Calin Juravled4215bb2017-09-20 11:54:21 -0700745 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
746 File* oat_file = oat_file_wrapper.GetFile();
747 if (oat_file == nullptr) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700748 *error_msg = "Generation of oat file " + oat_file_name
749 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700750 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700751 }
752
Calin Juravle357c66d2017-05-04 01:57:17 +0000753 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700754 *error_msg = "Generation of oat file " + oat_file_name
755 + " not attempted because the oat file could not be made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700756 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700757 }
758
759 std::vector<std::string> args;
760 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000761 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700762 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
763 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000764 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700765 const std::string dex2oat_context = class_loader_context == nullptr
766 ? OatFile::kSpecialSharedLibrary
767 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
768 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700769
Richard Uhler66d874d2015-01-15 09:37:19 -0800770 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700771 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700772 }
773
David Brazdil7b49e6c2016-09-01 11:06:18 +0100774 if (vdex_file->FlushCloseOrErase() != 0) {
775 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100776 return kUpdateFailed;
777 }
778
Richard Uhler8327cf72015-10-13 16:34:59 -0700779 if (oat_file->FlushCloseOrErase() != 0) {
780 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700781 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800782 }
783
Calin Juravle357c66d2017-05-04 01:57:17 +0000784 // Mark that the odex file has changed and we should try to reload.
785 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700786 // We have compiled successfully. Disable the auto-unlink.
787 vdex_file_wrapper.DisableUnlinkAtDestruction();
788 oat_file_wrapper.DisableUnlinkAtDestruction();
789
Richard Uhler1e860612016-03-30 12:17:55 -0700790 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800791}
792
793bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
794 std::string* error_msg) {
795 Runtime* runtime = Runtime::Current();
796 std::string image_location = ImageLocation();
797 if (image_location.empty()) {
798 *error_msg = "No image location found for Dex2Oat.";
799 return false;
800 }
801
802 std::vector<std::string> argv;
803 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000804 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200805 argv.push_back("--debuggable");
806 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700807 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800808
809 if (!runtime->IsVerificationEnabled()) {
810 argv.push_back("--compiler-filter=verify-none");
811 }
812
813 if (runtime->MustRelocateIfPossible()) {
814 argv.push_back("--runtime-arg");
815 argv.push_back("-Xrelocate");
816 } else {
817 argv.push_back("--runtime-arg");
818 argv.push_back("-Xnorelocate");
819 }
820
821 if (!kIsTargetBuild) {
822 argv.push_back("--host");
823 }
824
825 argv.push_back("--boot-image=" + image_location);
826
827 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
828 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
829
830 argv.insert(argv.end(), args.begin(), args.end());
831
Andreas Gampe9186ced2016-12-12 14:28:21 -0800832 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800833 return Exec(argv, error_msg);
834}
835
Richard Uhlerb81881d2016-04-19 13:08:04 -0700836bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
837 InstructionSet isa,
838 std::string* odex_filename,
839 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000840 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800841}
842
Richard Uhlerb81881d2016-04-19 13:08:04 -0700843bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
844 InstructionSet isa,
845 std::string* oat_filename,
846 std::string* error_msg) {
847 CHECK(oat_filename != nullptr);
848 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800849
Richard Uhler55b58b62016-08-12 09:05:13 -0700850 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
851 if (cache_dir.empty()) {
852 *error_msg = "Dalvik cache directory does not exist";
853 return false;
854 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700855
856 // TODO: The oat file assistant should be the definitive place for
857 // determining the oat file name from the dex location, not
858 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700859 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800860}
861
Richard Uhler66d874d2015-01-15 09:37:19 -0800862std::string OatFileAssistant::ImageLocation() {
863 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000864 const std::vector<gc::space::ImageSpace*>& image_spaces =
865 runtime->GetHeap()->GetBootImageSpaces();
866 if (image_spaces.empty()) {
867 return "";
868 }
869 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800870}
871
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000872const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
873 if (!required_dex_checksums_attempted_) {
874 required_dex_checksums_attempted_ = true;
875 required_dex_checksums_found_ = false;
876 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800877 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800878 const ArtDexFileLoader dex_file_loader;
879 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
880 &cached_required_dex_checksums_,
881 &error_msg,
882 zip_fd_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000883 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700884 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800885 } else {
886 // This can happen if the original dex file has been stripped from the
887 // apk.
888 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700889 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800890
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000891 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700892 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800893 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000894 required_dex_checksums_found_ = true;
895 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700896 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000897 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
898 if (odex_dex_file == nullptr) {
899 required_dex_checksums_found_ = false;
900 break;
901 }
902 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800903 }
904 }
905 }
906 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000907 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800908}
909
Richard Uhler95e09672017-02-22 11:37:41 +0000910std::unique_ptr<OatFileAssistant::ImageInfo>
911OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
912 CHECK(error_msg != nullptr);
913
Richard Uhler95e09672017-02-22 11:37:41 +0000914 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000915 std::unique_ptr<ImageInfo> info(new ImageInfo());
916 info->location = runtime->GetImageLocation();
917
918 std::unique_ptr<ImageHeader> image_header(
919 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
920 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000921 return nullptr;
922 }
923
Richard Uhler8f104862017-02-22 12:34:01 +0000924 info->oat_checksum = image_header->GetOatChecksum();
925 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
926 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000927 return info;
928}
929
Richard Uhler66d874d2015-01-15 09:37:19 -0800930const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
931 if (!image_info_load_attempted_) {
932 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000933 std::string error_msg;
934 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
935 if (cached_image_info_ == nullptr) {
936 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800937 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800938 }
Richard Uhler95e09672017-02-22 11:37:41 +0000939 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800940}
941
Richard Uhler88bc6732016-11-14 14:38:03 +0000942OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000943 // TODO(calin): Document the side effects of class loading when
944 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700945 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000946 // If the parent of the dex file is writable it means that we can
947 // create the odex file. In this case we unconditionally pick the odex
948 // as the best oat file. This corresponds to the regular use case when
949 // apps gets installed or when they load private, secondary dex file.
950 // For apps on the system partition the odex location will not be
951 // writable and thus the oat location might be more up to date.
952 return odex_;
953 }
954
955 // We cannot write to the odex location. This must be a system app.
956
957 // If the oat location is usable take it.
958 if (oat_.IsUseable()) {
959 return oat_;
960 }
961
962 // The oat file is not usable but the odex file might be up to date.
963 // This is an indication that we are dealing with an up to date prebuilt
964 // (that doesn't need relocation).
965 if (odex_.Status() == kOatUpToDate) {
966 return odex_;
967 }
968
969 // The oat file is not usable and the odex file is not up to date.
970 // However we have access to the original dex file which means we can make
971 // the oat location up to date.
972 if (HasOriginalDexFiles()) {
973 return oat_;
974 }
975
976 // We got into the worst situation here:
977 // - the oat location is not usable
978 // - the prebuild odex location is not up to date
979 // - and we don't have the original dex file anymore (stripped).
980 // Pick the odex if it exists, or the oat if not.
981 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000982}
983
Andreas Gampea463b6a2016-08-12 21:53:32 -0700984std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800985 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100986 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800987 if (art_file.empty()) {
988 return nullptr;
989 }
990 std::string error_msg;
991 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700992 std::unique_ptr<gc::space::ImageSpace> ret =
993 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800994 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800995 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
996 }
997 return ret;
998}
999
Richard Uhler88bc6732016-11-14 14:38:03 +00001000OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
1001 bool is_oat_location)
1002 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -07001003{}
1004
Richard Uhler88bc6732016-11-14 14:38:03 +00001005bool OatFileAssistant::OatFileInfo::IsOatLocation() {
1006 return is_oat_location_;
1007}
1008
Richard Uhler743bf362016-04-19 15:39:37 -07001009const std::string* OatFileAssistant::OatFileInfo::Filename() {
1010 return filename_provided_ ? &filename_ : nullptr;
1011}
1012
Richard Uhler03bc6592016-11-22 09:42:04 +00001013bool OatFileAssistant::OatFileInfo::IsUseable() {
1014 switch (Status()) {
1015 case kOatCannotOpen:
1016 case kOatDexOutOfDate:
1017 case kOatBootImageOutOfDate: return false;
1018
1019 case kOatRelocationOutOfDate:
1020 case kOatUpToDate: return true;
1021 }
1022 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001023}
1024
1025OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1026 if (!status_attempted_) {
1027 status_attempted_ = true;
1028 const OatFile* file = GetFile();
1029 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001030 // Check to see if there is a vdex file we can make use of.
1031 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001032 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001033 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001034 if (use_fd_) {
1035 if (vdex_fd_ >= 0) {
1036 struct stat s;
1037 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1038 if (rc == -1) {
1039 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1040 } else {
1041 vdex = VdexFile::Open(vdex_fd_,
1042 s.st_size,
1043 vdex_filename,
1044 false /*writable*/,
1045 false /*low_4gb*/,
1046 false /* unquicken */,
1047 &error_msg);
1048 }
1049 }
1050 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001051 vdex = VdexFile::Open(vdex_filename,
1052 false /*writeable*/,
1053 false /*low_4gb*/,
1054 false /*unquicken*/,
1055 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001056 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001057 if (vdex == nullptr) {
1058 status_ = kOatCannotOpen;
1059 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1060 } else {
1061 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1062 // The vdex file does not contain enough information to determine
1063 // whether it is up to date with respect to the boot image, so we
1064 // assume it is out of date.
1065 VLOG(oat) << error_msg;
1066 status_ = kOatBootImageOutOfDate;
1067 } else {
1068 status_ = kOatDexOutOfDate;
1069 }
1070 }
Richard Uhler743bf362016-04-19 15:39:37 -07001071 } else {
1072 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001073 VLOG(oat) << file->GetLocation() << " is " << status_
1074 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001075 }
1076 }
1077 return status_;
1078}
1079
Richard Uhler70a84262016-11-08 16:51:51 +00001080OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001081 CompilerFilter::Filter target,
1082 bool profile_changed,
1083 bool downgrade,
1084 ClassLoaderContext* context) {
1085
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001086 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001087 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001088 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001089
Calin Juravle44e5efa2017-09-12 00:54:26 -07001090 // Only check the filter and relocation if the class loader context is ok.
1091 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1092 if (class_loader_context_okay) {
1093 if (filter_okay && Status() == kOatUpToDate) {
1094 // The oat file is in good shape as is.
1095 return kNoDexOptNeeded;
1096 }
Richard Uhler70a84262016-11-08 16:51:51 +00001097
Calin Juravle44e5efa2017-09-12 00:54:26 -07001098 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1099 // If no compilation is desired, then it doesn't matter if the oat
1100 // file needs relocation. It's in good shape as is.
1101 return kNoDexOptNeeded;
1102 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001103
Calin Juravle44e5efa2017-09-12 00:54:26 -07001104 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1105 return kDex2OatForRelocation;
1106 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001107
Calin Juravle44e5efa2017-09-12 00:54:26 -07001108 if (IsUseable()) {
1109 return kDex2OatForFilter;
1110 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001111
Calin Juravle44e5efa2017-09-12 00:54:26 -07001112 if (Status() == kOatBootImageOutOfDate) {
1113 return kDex2OatForBootImage;
1114 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001115 }
1116
1117 if (oat_file_assistant_->HasOriginalDexFiles()) {
1118 return kDex2OatFromScratch;
1119 } else {
1120 // Otherwise there is nothing we can do, even if we want to.
1121 return kNoDexOptNeeded;
1122 }
Richard Uhler70a84262016-11-08 16:51:51 +00001123}
1124
Richard Uhler743bf362016-04-19 15:39:37 -07001125const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1126 CHECK(!file_released_) << "GetFile called after oat file released.";
1127 if (!load_attempted_) {
1128 load_attempted_ = true;
1129 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +00001130 bool executable = oat_file_assistant_->load_executable_;
1131 if (executable && oat_file_assistant_->only_load_system_executable_) {
1132 executable = LocationIsOnSystem(filename_.c_str());
1133 }
Richard Uhler743bf362016-04-19 15:39:37 -07001134 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001135 if (use_fd_) {
1136 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1137 file_.reset(OatFile::Open(vdex_fd_,
1138 oat_fd_,
1139 filename_.c_str(),
1140 nullptr,
1141 nullptr,
Nicolas Geoffray29742602017-12-14 10:09:03 +00001142 executable,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001143 false /* low_4gb */,
1144 oat_file_assistant_->dex_location_.c_str(),
1145 &error_msg));
1146 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001147 } else {
1148 file_.reset(OatFile::Open(filename_.c_str(),
1149 filename_.c_str(),
1150 nullptr,
1151 nullptr,
Nicolas Geoffray29742602017-12-14 10:09:03 +00001152 executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001153 false /* low_4gb */,
1154 oat_file_assistant_->dex_location_.c_str(),
1155 &error_msg));
1156 }
Richard Uhler743bf362016-04-19 15:39:37 -07001157 if (file_.get() == nullptr) {
1158 VLOG(oat) << "OatFileAssistant test for existing oat file "
1159 << filename_ << ": " << error_msg;
1160 }
1161 }
1162 }
1163 return file_.get();
1164}
1165
1166bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001167 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001168 const OatFile* file = GetFile();
1169 if (file == nullptr) {
1170 return false;
1171 }
1172
1173 CompilerFilter::Filter current = file->GetCompilerFilter();
1174 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1175 VLOG(oat) << "Compiler filter not okay because Profile changed";
1176 return false;
1177 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001178 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1179 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001180}
1181
Calin Juravle44e5efa2017-09-12 00:54:26 -07001182bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1183 if (context == nullptr) {
1184 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1185 return true;
1186 }
1187
1188 const OatFile* file = GetFile();
1189 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001190 // No oat file means we have nothing to verify.
1191 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001192 }
1193
Calin Juravle20c46442017-09-12 00:54:26 -07001194 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001195 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001196 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001197 : "";
1198
1199 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1200 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1201 return false;
1202 }
1203
1204 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext());
1205 if (!result) {
1206 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1207 << file->GetClassLoaderContext()
1208 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1209 }
1210 return result;
1211}
1212
Richard Uhler743bf362016-04-19 15:39:37 -07001213bool OatFileAssistant::OatFileInfo::IsExecutable() {
1214 const OatFile* file = GetFile();
1215 return (file != nullptr && file->IsExecutable());
1216}
1217
Richard Uhler743bf362016-04-19 15:39:37 -07001218void OatFileAssistant::OatFileInfo::Reset() {
1219 load_attempted_ = false;
1220 file_.reset();
1221 status_attempted_ = false;
1222}
1223
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001224void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, bool use_fd, int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001225 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001226 filename_provided_ = true;
1227 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001228 use_fd_ = use_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001229 vdex_fd_ = vdex_fd;
1230 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001231 Reset();
1232}
1233
1234std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1235 file_released_ = true;
1236 return std::move(file_);
1237}
1238
Richard Uhler70a84262016-11-08 16:51:51 +00001239std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001240 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001241 return ReleaseFile();
1242 }
1243
1244 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1245 << " attempting to fall back to interpreting oat file instead.";
1246
Richard Uhler03bc6592016-11-22 09:42:04 +00001247 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001248 return ReleaseFile();
1249 }
1250
Richard Uhler03bc6592016-11-22 09:42:04 +00001251 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001252 // We are loading an oat file for runtime use that needs relocation.
1253 // Reload the file non-executable to ensure that we interpret out of the
1254 // dex code in the oat file rather than trying to execute the unrelocated
1255 // compiled code.
1256 oat_file_assistant_->load_executable_ = false;
1257 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001258 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001259 CHECK(!IsExecutable());
1260 return ReleaseFile();
1261 }
1262 }
1263 return std::unique_ptr<OatFile>();
1264}
Calin Juravle5f9a8012018-02-12 20:27:46 -08001265
1266// TODO(calin): we could provide a more refined status here
1267// (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
1268// track more experiments but adds extra complexity.
1269void OatFileAssistant::GetOptimizationStatus(
1270 const std::string& filename,
1271 InstructionSet isa,
1272 std::string* out_compilation_filter,
1273 std::string* out_compilation_reason) {
1274 // Try to load the oat file as we would do at runtime.
1275 OatFileAssistant oat_file_assistant(filename.c_str(), isa, true /* load_executable */);
1276 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1277
1278 if (oat_file == nullptr) {
1279 *out_compilation_filter = "run-from-apk";
1280 *out_compilation_reason = "unknown";
1281 } else {
1282 *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
1283 const char* reason = oat_file->GetCompilationReason();
1284 *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1285 }
1286}
1287
Richard Uhler66d874d2015-01-15 09:37:19 -08001288} // namespace art