blob: 73ca19a3630b549342a23262fe7ce77df6a32c90 [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,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070075 bool load_executable)
76 : OatFileAssistant(dex_location,
77 isa, load_executable,
78 -1 /* vdex_fd */,
79 -1 /* oat_fd */,
80 -1 /* zip_fd */) {}
81
82
83OatFileAssistant::OatFileAssistant(const char* dex_location,
84 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070085 bool load_executable,
86 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070087 int oat_fd,
88 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000089 : isa_(isa),
90 load_executable_(load_executable),
91 odex_(this, /*is_oat_location*/ false),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070092 oat_(this, /*is_oat_location*/ true),
93 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -070094 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000095
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070096 if (zip_fd < 0) {
97 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
98 << " oat_fd=" << oat_fd;
99 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
100 << " vdex_fd=" << vdex_fd;;
101 }
102
Calin Juravle099f8d62017-09-05 19:04:04 -0700103 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700104
Richard Uhler66d874d2015-01-15 09:37:19 -0800105 if (load_executable_ && isa != kRuntimeISA) {
106 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
107 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
108 load_executable_ = false;
109 }
110
Richard Uhler743bf362016-04-19 15:39:37 -0700111 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700112 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700113 std::string odex_file_name;
114 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700115 odex_.Reset(odex_file_name, UseFdToReadFiles(), vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700116 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700117 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
118 }
119
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700120 if (!UseFdToReadFiles()) {
121 // Get the oat filename.
122 std::string oat_file_name;
123 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
124 oat_.Reset(oat_file_name, false /* use_fd */);
125 } else {
126 LOG(WARNING) << "Failed to determine oat file name for dex location "
127 << dex_location_ << ": " << error_msg;
128 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000129 }
130
131 // Check if the dex directory is writable.
132 // This will be needed in most uses of OatFileAssistant and so it's OK to
133 // compute it eagerly. (the only use which will not make use of it is
134 // OatFileAssistant::GetStatusDump())
135 size_t pos = dex_location_.rfind('/');
136 if (pos == std::string::npos) {
137 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700138 } else if (!UseFdToReadFiles()) {
139 // We cannot test for parent access when using file descriptors. That's ok
140 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000141 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700142 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000143 dex_parent_writable_ = true;
144 } else {
145 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700146 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800147 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800148}
149
150OatFileAssistant::~OatFileAssistant() {
151 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100152 if (flock_.get() != nullptr) {
153 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800154 }
155}
156
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700157bool OatFileAssistant::UseFdToReadFiles() {
158 return zip_fd_ >= 0;
159}
160
Richard Uhler66d874d2015-01-15 09:37:19 -0800161bool OatFileAssistant::IsInBootClassPath() {
162 // Note: We check the current boot class path, regardless of the ISA
163 // specified by the user. This is okay, because the boot class path should
164 // be the same for all ISAs.
165 // TODO: Can we verify the boot class path is the same for all ISAs?
166 Runtime* runtime = Runtime::Current();
167 ClassLinker* class_linker = runtime->GetClassLinker();
168 const auto& boot_class_path = class_linker->GetBootClassPath();
169 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700170 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800171 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
172 return true;
173 }
174 }
175 return false;
176}
177
178bool OatFileAssistant::Lock(std::string* error_msg) {
179 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100180 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800181
Calin Juravle357c66d2017-05-04 01:57:17 +0000182 // Note the lock will only succeed for secondary dex files and in test
183 // environment.
184 //
185 // The lock *will fail* for all primary apks in a production environment.
186 // The app does not have permissions to create locks next to its dex location
187 // (be it system, data or vendor parition). We also cannot use the odex or
188 // oat location for the same reasoning.
189 //
190 // This is best effort and if it fails it's unlikely that we will be able
191 // to generate oat files anyway.
192 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800193
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100194 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
195 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100196 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800197 return false;
198 }
199 return true;
200}
201
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700202int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
203 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700204 bool downgrade,
205 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000206 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700207 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
208 profile_changed,
209 downgrade,
210 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000211 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
212 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800213 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000214 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800215}
216
Richard Uhlerf4b34872016-04-13 11:03:46 -0700217// Figure out the currently specified compile filter option in the runtime.
218// Returns true on success, false if the compiler filter is invalid, in which
219// case error_msg describes the problem.
220static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
221 std::string* error_msg) {
222 CHECK(filter != nullptr);
223 CHECK(error_msg != nullptr);
224
Calin Juravle357c66d2017-05-04 01:57:17 +0000225 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700226 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
227 if (option.starts_with("--compiler-filter=")) {
228 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
229 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
230 *error_msg = std::string("Unknown --compiler-filter value: ")
231 + std::string(compiler_filter_string);
232 return false;
233 }
234 }
235 }
236 return true;
237}
238
Richard Uhler01be6812016-05-17 10:34:52 -0700239bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000240 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700241}
242
Richard Uhler1e860612016-03-30 12:17:55 -0700243OatFileAssistant::ResultOfAttemptToUpdate
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700244OatFileAssistant::MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700245 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700246 std::string* error_msg) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700247 // The method doesn't use zip_fd_ and directly opens dex files at dex_locations_.
248 CHECK_EQ(-1, zip_fd_) << "MakeUpToDate should not be called with zip_fd";
249
Richard Uhlerf4b34872016-04-13 11:03:46 -0700250 CompilerFilter::Filter target;
251 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
252 return kUpdateNotAttempted;
253 }
254
Richard Uhler88bc6732016-11-14 14:38:03 +0000255 OatFileInfo& info = GetBestInfo();
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700256 // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291.
257 // This is actually not trivial in the current logic as it will interact with the collision
258 // check:
259 // - currently, if the context does not match but we have no collisions we still accept the
260 // oat file.
261 // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make
262 // the oat code up to date the collision check becomes useless.
263 // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files
264 // loaded in other processes). So it boils down to how far do we want to complicate
265 // 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 -0700266 switch (info.GetDexOptNeeded(
267 target, profile_changed, /*downgrade*/ false, class_loader_context)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000268 case kNoDexOptNeeded:
269 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000270
Richard Uhler7225a8d2016-11-22 10:12:03 +0000271 // TODO: For now, don't bother with all the different ways we can call
272 // dex2oat to generate the oat file. Always generate the oat file as if it
273 // were kDex2OatFromScratch.
274 case kDex2OatFromScratch:
275 case kDex2OatForBootImage:
276 case kDex2OatForRelocation:
277 case kDex2OatForFilter:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700278 return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800279 }
280 UNREACHABLE();
281}
282
283std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000284 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800285}
286
Richard Uhler46cc64f2016-11-14 14:53:55 +0000287std::string OatFileAssistant::GetStatusDump() {
288 std::ostringstream status;
289 bool oat_file_exists = false;
290 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000291 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000292 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000293 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000294
Richard Uhler46cc64f2016-11-14 14:53:55 +0000295 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000296 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
297 const OatFile* file = oat_.GetFile();
298 if (file == nullptr) {
299 // If the file is null even though the status is not kOatCannotOpen, it
300 // means we must have a vdex file with no corresponding oat file. In
301 // this case we cannot determine the compilation filter. Indicate that
302 // we have only the vdex file instead.
303 status << "vdex-only";
304 } else {
305 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
306 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000307 }
308
Richard Uhler03bc6592016-11-22 09:42:04 +0000309 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000310 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000311 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000312
Richard Uhler46cc64f2016-11-14 14:53:55 +0000313 odex_file_exists = true;
314 if (oat_file_exists) {
315 status << "] ";
316 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000317 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
318 const OatFile* file = odex_.GetFile();
319 if (file == nullptr) {
320 status << "vdex-only";
321 } else {
322 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
323 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000324 }
325
326 if (!oat_file_exists && !odex_file_exists) {
327 status << "invalid[";
328 }
329
330 status << "]";
331 return status.str();
332}
333
Richard Uhler66d874d2015-01-15 09:37:19 -0800334std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700335 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800336 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700337 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
338 return dex_files;
339 } else {
340 return std::vector<std::unique_ptr<const DexFile>>();
341 }
342}
Richard Uhler66d874d2015-01-15 09:37:19 -0800343
Calin Juravle87e2cb62017-06-13 21:48:45 -0700344bool OatFileAssistant::LoadDexFiles(
345 const OatFile &oat_file,
346 const std::string& dex_location,
347 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000348 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800349 std::string error_msg;
350 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700351 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800352 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700353 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700354 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800355 }
356
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700357 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800358 if (dex_file.get() == nullptr) {
359 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700360 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800361 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700362 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800363
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000364 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700365 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700366 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000367 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700368 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000369 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800370 break;
371 }
372
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700373 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 if (dex_file.get() == nullptr) {
375 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700376 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700378 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800379 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700380 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800381}
382
Richard Uhler9b994ea2015-06-24 08:44:19 -0700383bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000384 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700385 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000386 // GetRequiredDexChecksums.
387 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700388 return has_original_dex_files_;
389}
390
Richard Uhler95abd042015-03-24 09:51:28 -0700391OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700392 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800393}
394
Richard Uhler95abd042015-03-24 09:51:28 -0700395OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700396 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800397}
398
Richard Uhler2f27abd2017-01-31 14:02:34 +0000399bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000400 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
401 if (required_dex_checksums == nullptr) {
402 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
403 return true;
404 }
405
406 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
407 if (required_dex_checksums->size() != number_of_dex_files) {
408 *error_msg = StringPrintf("expected %zu dex files but found %u",
409 required_dex_checksums->size(),
410 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000411 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800412 }
413
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000414 for (uint32_t i = 0; i < number_of_dex_files; i++) {
415 uint32_t expected_checksum = (*required_dex_checksums)[i];
416 uint32_t actual_checksum = file.GetLocationChecksum(i);
417 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700418 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000419 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
420 "Expected: %u, actual: %u",
421 dex.c_str(),
422 expected_checksum,
423 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000424 return false;
425 }
426 }
427
Richard Uhler2f27abd2017-01-31 14:02:34 +0000428 return true;
429}
430
431bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000432 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
433 if (required_dex_checksums == nullptr) {
434 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
435 return true;
436 }
437
438 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
439 if (required_dex_checksums->size() != number_of_dex_files) {
440 *error_msg = StringPrintf("expected %zu dex files but found %u",
441 required_dex_checksums->size(),
442 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000443 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800444 }
445
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000446 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700447 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000448 uint32_t expected_checksum = (*required_dex_checksums)[i];
449 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
450 if (oat_dex_file == nullptr) {
451 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
452 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800453 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000454 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
455 if (expected_checksum != actual_checksum) {
456 VLOG(oat) << "Dex checksum does not match for dex: " << dex
457 << ". Expected: " << expected_checksum
458 << ", Actual: " << actual_checksum;
459 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800460 }
461 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000462 return true;
463}
464
465OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
466 // Verify the ART_USE_READ_BARRIER state.
467 // TODO: Don't fully reject files due to read barrier state. If they contain
468 // compiled code and are otherwise okay, we should return something like
469 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
470 // barrier state doesn't matter.
471 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
472 constexpr bool kRuntimeIsCC = kUseReadBarrier;
473 if (is_cc != kRuntimeIsCC) {
474 return kOatCannotOpen;
475 }
476
477 // Verify the dex checksum.
478 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000479 VdexFile* vdex = file.GetVdexFile();
480 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
481 LOG(ERROR) << error_msg;
482 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000483 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800484
Andreas Gampe29d38e72016-03-23 15:31:51 +0000485 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000486
Richard Uhler66d874d2015-01-15 09:37:19 -0800487 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000488 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
489 const ImageInfo* image_info = GetImageInfo();
490 if (image_info == nullptr) {
491 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000492
Richard Uhler76f5cb62016-04-04 13:30:16 -0700493 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000494 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700495 }
496
497 // If there is no original dex file to fall back to, grudgingly accept
498 // the oat file. This could technically lead to crashes, but there's no
499 // way we could find a better oat file to use for this dex location,
500 // and it's better than being stuck in a boot loop with no way out.
501 // The problem will hopefully resolve itself the next time the runtime
502 // starts up.
503 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
504 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000505 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000506 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000507 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000508 }
509 } else {
510 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800511 }
512
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100513 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000514 if (!file.IsPic()) {
515 const ImageInfo* image_info = GetImageInfo();
516 if (image_info == nullptr) {
517 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000518 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000519 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700520
Andreas Gampe29d38e72016-03-23 15:31:51 +0000521 // Verify the oat_data_begin recorded for the image in the oat file matches
522 // the actual oat_data_begin for boot.oat in the image.
523 const OatHeader& oat_header = file.GetOatHeader();
524 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
525 if (oat_data_begin != image_info->oat_data_begin) {
526 VLOG(oat) << file.GetLocation() <<
527 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
528 << " does not match actual image oat_data_begin ("
529 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000530 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000531 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700532
Andreas Gampe29d38e72016-03-23 15:31:51 +0000533 // Verify the oat_patch_delta recorded for the image in the oat file matches
534 // the actual oat_patch_delta for the image.
535 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
536 if (oat_patch_delta != image_info->patch_delta) {
537 VLOG(oat) << file.GetLocation() <<
538 ": Oat file image patch delta (" << oat_patch_delta << ")"
539 << " does not match actual image patch delta ("
540 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000541 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000542 }
543 } else {
544 // Oat files compiled in PIC mode do not require relocation.
545 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
546 }
547 } else {
548 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800549 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700550 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800551}
552
Calin Juravle357c66d2017-05-04 01:57:17 +0000553static bool DexLocationToOdexNames(const std::string& location,
554 InstructionSet isa,
555 std::string* odex_filename,
556 std::string* oat_dir,
557 std::string* isa_dir,
558 std::string* error_msg) {
559 CHECK(odex_filename != nullptr);
560 CHECK(error_msg != nullptr);
561
562 // The odex file name is formed by replacing the dex_location extension with
563 // .odex and inserting an oat/<isa> directory. For example:
564 // location = /foo/bar/baz.jar
565 // odex_location = /foo/bar/oat/<isa>/baz.odex
566
567 // Find the directory portion of the dex location and add the oat/<isa>
568 // directory.
569 size_t pos = location.rfind('/');
570 if (pos == std::string::npos) {
571 *error_msg = "Dex location " + location + " has no directory.";
572 return false;
573 }
574 std::string dir = location.substr(0, pos+1);
575 // Add the oat directory.
576 dir += "oat";
577 if (oat_dir != nullptr) {
578 *oat_dir = dir;
579 }
580 // Add the isa directory
581 dir += "/" + std::string(GetInstructionSetString(isa));
582 if (isa_dir != nullptr) {
583 *isa_dir = dir;
584 }
585
586 // Get the base part of the file without the extension.
587 std::string file = location.substr(pos+1);
588 pos = file.rfind('.');
589 if (pos == std::string::npos) {
590 *error_msg = "Dex location " + location + " has no extension.";
591 return false;
592 }
593 std::string base = file.substr(0, pos);
594
595 *odex_filename = dir + "/" + base + ".odex";
596 return true;
597}
598
599// Prepare a subcomponent of the odex directory.
600// (i.e. create and set the expected permissions on the path `dir`).
601static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
602 struct stat dir_stat;
603 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
604 // The directory exists. Check if it is indeed a directory.
605 if (!S_ISDIR(dir_stat.st_mode)) {
606 *error_msg = dir + " is not a dir";
607 return false;
608 } else {
609 // The dir is already on disk.
610 return true;
611 }
612 }
613
614 // Failed to stat. We need to create the directory.
615 if (errno != ENOENT) {
616 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
617 return false;
618 }
619
620 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
621 if (mkdir(dir.c_str(), mode) != 0) {
622 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
623 return false;
624 }
625 if (chmod(dir.c_str(), mode) != 0) {
626 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
627 return false;
628 }
629 return true;
630}
631
632// Prepares the odex directory for the given dex location.
633static bool PrepareOdexDirectories(const std::string& dex_location,
634 const std::string& expected_odex_location,
635 InstructionSet isa,
636 std::string* error_msg) {
637 std::string actual_odex_location;
638 std::string oat_dir;
639 std::string isa_dir;
640 if (!DexLocationToOdexNames(
641 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
642 return false;
643 }
644 DCHECK_EQ(expected_odex_location, actual_odex_location);
645
646 if (!PrepareDirectory(oat_dir, error_msg)) {
647 return false;
648 }
649 if (!PrepareDirectory(isa_dir, error_msg)) {
650 return false;
651 }
652 return true;
653}
654
Calin Juravled4215bb2017-09-20 11:54:21 -0700655class Dex2oatFileWrapper {
656 public:
657 explicit Dex2oatFileWrapper(File* file)
658 : file_(file),
659 unlink_file_at_destruction_(true) {
660 }
661
662 ~Dex2oatFileWrapper() {
663 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
664 file_->Erase(/*unlink*/ true);
665 }
666 }
667
668 File* GetFile() { return file_.get(); }
669
670 void DisableUnlinkAtDestruction() {
671 unlink_file_at_destruction_ = false;
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800672 }
Calin Juravled4215bb2017-09-20 11:54:21 -0700673
674 private:
675 std::unique_ptr<File> file_;
676 bool unlink_file_at_destruction_;
677};
678
Calin Juravle357c66d2017-05-04 01:57:17 +0000679OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700680 OatFileAssistant::OatFileInfo& info,
681 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700682 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700683 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800684 CHECK(error_msg != nullptr);
685
Richard Uhler8327cf72015-10-13 16:34:59 -0700686 Runtime* runtime = Runtime::Current();
687 if (!runtime->IsDex2OatEnabled()) {
688 *error_msg = "Generation of oat file for dex location " + dex_location_
689 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700690 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700691 }
692
Calin Juravle357c66d2017-05-04 01:57:17 +0000693 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700694 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800695 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700696 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800697 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000698 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700699 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800700
Richard Uhler66d874d2015-01-15 09:37:19 -0800701 // dex2oat ignores missing dex files and doesn't report an error.
702 // Check explicitly here so we can detect the error properly.
703 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000704 struct stat dex_path_stat;
705 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
706 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700707 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800708 }
709
Calin Juravle357c66d2017-05-04 01:57:17 +0000710 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
711 if (!info.IsOatLocation()) {
712 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
713 return kUpdateNotAttempted;
714 }
715 }
716
717 // Set the permissions for the oat and the vdex files.
718 // The user always gets read and write while the group and others propagate
719 // the reading access of the original dex file.
720 mode_t file_mode = S_IRUSR | S_IWUSR |
721 (dex_path_stat.st_mode & S_IRGRP) |
722 (dex_path_stat.st_mode & S_IROTH);
723
Calin Juravled4215bb2017-09-20 11:54:21 -0700724 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
725 File* vdex_file = vdex_file_wrapper.GetFile();
726 if (vdex_file == nullptr) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100727 *error_msg = "Generation of oat file " + oat_file_name
728 + " not attempted because the vdex file " + vdex_file_name
729 + " could not be opened.";
730 return kUpdateNotAttempted;
731 }
732
Calin Juravle357c66d2017-05-04 01:57:17 +0000733 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100734 *error_msg = "Generation of oat file " + oat_file_name
735 + " not attempted because the vdex file " + vdex_file_name
736 + " could not be made world readable.";
737 return kUpdateNotAttempted;
738 }
739
Calin Juravled4215bb2017-09-20 11:54:21 -0700740 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
741 File* oat_file = oat_file_wrapper.GetFile();
742 if (oat_file == nullptr) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700743 *error_msg = "Generation of oat file " + oat_file_name
744 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700745 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700746 }
747
Calin Juravle357c66d2017-05-04 01:57:17 +0000748 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700749 *error_msg = "Generation of oat file " + oat_file_name
750 + " not attempted because the oat file could not be made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700751 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700752 }
753
754 std::vector<std::string> args;
755 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000756 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700757 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
758 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000759 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700760 const std::string dex2oat_context = class_loader_context == nullptr
761 ? OatFile::kSpecialSharedLibrary
762 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
763 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700764
Richard Uhler66d874d2015-01-15 09:37:19 -0800765 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700766 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700767 }
768
David Brazdil7b49e6c2016-09-01 11:06:18 +0100769 if (vdex_file->FlushCloseOrErase() != 0) {
770 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100771 return kUpdateFailed;
772 }
773
Richard Uhler8327cf72015-10-13 16:34:59 -0700774 if (oat_file->FlushCloseOrErase() != 0) {
775 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700776 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800777 }
778
Calin Juravle357c66d2017-05-04 01:57:17 +0000779 // Mark that the odex file has changed and we should try to reload.
780 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700781 // We have compiled successfully. Disable the auto-unlink.
782 vdex_file_wrapper.DisableUnlinkAtDestruction();
783 oat_file_wrapper.DisableUnlinkAtDestruction();
784
Richard Uhler1e860612016-03-30 12:17:55 -0700785 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800786}
787
788bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
789 std::string* error_msg) {
790 Runtime* runtime = Runtime::Current();
791 std::string image_location = ImageLocation();
792 if (image_location.empty()) {
793 *error_msg = "No image location found for Dex2Oat.";
794 return false;
795 }
796
797 std::vector<std::string> argv;
798 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000799 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200800 argv.push_back("--debuggable");
801 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700802 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800803
804 if (!runtime->IsVerificationEnabled()) {
805 argv.push_back("--compiler-filter=verify-none");
806 }
807
808 if (runtime->MustRelocateIfPossible()) {
809 argv.push_back("--runtime-arg");
810 argv.push_back("-Xrelocate");
811 } else {
812 argv.push_back("--runtime-arg");
813 argv.push_back("-Xnorelocate");
814 }
815
816 if (!kIsTargetBuild) {
817 argv.push_back("--host");
818 }
819
820 argv.push_back("--boot-image=" + image_location);
821
822 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
823 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
824
825 argv.insert(argv.end(), args.begin(), args.end());
826
Andreas Gampe9186ced2016-12-12 14:28:21 -0800827 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800828 return Exec(argv, error_msg);
829}
830
Richard Uhlerb81881d2016-04-19 13:08:04 -0700831bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
832 InstructionSet isa,
833 std::string* odex_filename,
834 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000835 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800836}
837
Richard Uhlerb81881d2016-04-19 13:08:04 -0700838bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
839 InstructionSet isa,
840 std::string* oat_filename,
841 std::string* error_msg) {
842 CHECK(oat_filename != nullptr);
843 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800844
Richard Uhler55b58b62016-08-12 09:05:13 -0700845 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
846 if (cache_dir.empty()) {
847 *error_msg = "Dalvik cache directory does not exist";
848 return false;
849 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700850
851 // TODO: The oat file assistant should be the definitive place for
852 // determining the oat file name from the dex location, not
853 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700854 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800855}
856
Richard Uhler66d874d2015-01-15 09:37:19 -0800857std::string OatFileAssistant::ImageLocation() {
858 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000859 const std::vector<gc::space::ImageSpace*>& image_spaces =
860 runtime->GetHeap()->GetBootImageSpaces();
861 if (image_spaces.empty()) {
862 return "";
863 }
864 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800865}
866
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000867const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
868 if (!required_dex_checksums_attempted_) {
869 required_dex_checksums_attempted_ = true;
870 required_dex_checksums_found_ = false;
871 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800872 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800873 const ArtDexFileLoader dex_file_loader;
874 if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(),
875 &cached_required_dex_checksums_,
876 &error_msg,
877 zip_fd_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000878 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700879 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800880 } else {
881 // This can happen if the original dex file has been stripped from the
882 // apk.
883 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700884 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800885
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000886 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700887 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800888 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000889 required_dex_checksums_found_ = true;
890 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700891 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000892 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
893 if (odex_dex_file == nullptr) {
894 required_dex_checksums_found_ = false;
895 break;
896 }
897 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800898 }
899 }
900 }
901 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000902 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800903}
904
Richard Uhler95e09672017-02-22 11:37:41 +0000905std::unique_ptr<OatFileAssistant::ImageInfo>
906OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
907 CHECK(error_msg != nullptr);
908
Richard Uhler95e09672017-02-22 11:37:41 +0000909 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000910 std::unique_ptr<ImageInfo> info(new ImageInfo());
911 info->location = runtime->GetImageLocation();
912
913 std::unique_ptr<ImageHeader> image_header(
914 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
915 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000916 return nullptr;
917 }
918
Richard Uhler8f104862017-02-22 12:34:01 +0000919 info->oat_checksum = image_header->GetOatChecksum();
920 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
921 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000922 return info;
923}
924
Richard Uhler66d874d2015-01-15 09:37:19 -0800925const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
926 if (!image_info_load_attempted_) {
927 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000928 std::string error_msg;
929 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
930 if (cached_image_info_ == nullptr) {
931 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800932 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800933 }
Richard Uhler95e09672017-02-22 11:37:41 +0000934 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800935}
936
Richard Uhler88bc6732016-11-14 14:38:03 +0000937OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000938 // TODO(calin): Document the side effects of class loading when
939 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700940 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000941 // If the parent of the dex file is writable it means that we can
942 // create the odex file. In this case we unconditionally pick the odex
943 // as the best oat file. This corresponds to the regular use case when
944 // apps gets installed or when they load private, secondary dex file.
945 // For apps on the system partition the odex location will not be
946 // writable and thus the oat location might be more up to date.
947 return odex_;
948 }
949
950 // We cannot write to the odex location. This must be a system app.
951
952 // If the oat location is usable take it.
953 if (oat_.IsUseable()) {
954 return oat_;
955 }
956
957 // The oat file is not usable but the odex file might be up to date.
958 // This is an indication that we are dealing with an up to date prebuilt
959 // (that doesn't need relocation).
960 if (odex_.Status() == kOatUpToDate) {
961 return odex_;
962 }
963
964 // The oat file is not usable and the odex file is not up to date.
965 // However we have access to the original dex file which means we can make
966 // the oat location up to date.
967 if (HasOriginalDexFiles()) {
968 return oat_;
969 }
970
971 // We got into the worst situation here:
972 // - the oat location is not usable
973 // - the prebuild odex location is not up to date
974 // - and we don't have the original dex file anymore (stripped).
975 // Pick the odex if it exists, or the oat if not.
976 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000977}
978
Andreas Gampea463b6a2016-08-12 21:53:32 -0700979std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800980 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100981 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800982 if (art_file.empty()) {
983 return nullptr;
984 }
985 std::string error_msg;
986 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700987 std::unique_ptr<gc::space::ImageSpace> ret =
988 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800989 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800990 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
991 }
992 return ret;
993}
994
Richard Uhler88bc6732016-11-14 14:38:03 +0000995OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
996 bool is_oat_location)
997 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700998{}
999
Richard Uhler88bc6732016-11-14 14:38:03 +00001000bool OatFileAssistant::OatFileInfo::IsOatLocation() {
1001 return is_oat_location_;
1002}
1003
Richard Uhler743bf362016-04-19 15:39:37 -07001004const std::string* OatFileAssistant::OatFileInfo::Filename() {
1005 return filename_provided_ ? &filename_ : nullptr;
1006}
1007
Richard Uhler03bc6592016-11-22 09:42:04 +00001008bool OatFileAssistant::OatFileInfo::IsUseable() {
1009 switch (Status()) {
1010 case kOatCannotOpen:
1011 case kOatDexOutOfDate:
1012 case kOatBootImageOutOfDate: return false;
1013
1014 case kOatRelocationOutOfDate:
1015 case kOatUpToDate: return true;
1016 }
1017 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001018}
1019
1020OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1021 if (!status_attempted_) {
1022 status_attempted_ = true;
1023 const OatFile* file = GetFile();
1024 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001025 // Check to see if there is a vdex file we can make use of.
1026 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001027 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001028 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001029 if (use_fd_) {
1030 if (vdex_fd_ >= 0) {
1031 struct stat s;
1032 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1033 if (rc == -1) {
1034 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1035 } else {
1036 vdex = VdexFile::Open(vdex_fd_,
1037 s.st_size,
1038 vdex_filename,
1039 false /*writable*/,
1040 false /*low_4gb*/,
1041 false /* unquicken */,
1042 &error_msg);
1043 }
1044 }
1045 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001046 vdex = VdexFile::Open(vdex_filename,
1047 false /*writeable*/,
1048 false /*low_4gb*/,
1049 false /*unquicken*/,
1050 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001051 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001052 if (vdex == nullptr) {
1053 status_ = kOatCannotOpen;
1054 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1055 } else {
1056 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1057 // The vdex file does not contain enough information to determine
1058 // whether it is up to date with respect to the boot image, so we
1059 // assume it is out of date.
1060 VLOG(oat) << error_msg;
1061 status_ = kOatBootImageOutOfDate;
1062 } else {
1063 status_ = kOatDexOutOfDate;
1064 }
1065 }
Richard Uhler743bf362016-04-19 15:39:37 -07001066 } else {
1067 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001068 VLOG(oat) << file->GetLocation() << " is " << status_
1069 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001070 }
1071 }
1072 return status_;
1073}
1074
Richard Uhler70a84262016-11-08 16:51:51 +00001075OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001076 CompilerFilter::Filter target,
1077 bool profile_changed,
1078 bool downgrade,
1079 ClassLoaderContext* context) {
1080
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001081 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001082 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001083 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001084
Calin Juravle44e5efa2017-09-12 00:54:26 -07001085 // Only check the filter and relocation if the class loader context is ok.
1086 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1087 if (class_loader_context_okay) {
1088 if (filter_okay && Status() == kOatUpToDate) {
1089 // The oat file is in good shape as is.
1090 return kNoDexOptNeeded;
1091 }
Richard Uhler70a84262016-11-08 16:51:51 +00001092
Calin Juravle44e5efa2017-09-12 00:54:26 -07001093 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1094 // If no compilation is desired, then it doesn't matter if the oat
1095 // file needs relocation. It's in good shape as is.
1096 return kNoDexOptNeeded;
1097 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001098
Calin Juravle44e5efa2017-09-12 00:54:26 -07001099 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1100 return kDex2OatForRelocation;
1101 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001102
Calin Juravle44e5efa2017-09-12 00:54:26 -07001103 if (IsUseable()) {
1104 return kDex2OatForFilter;
1105 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001106
Calin Juravle44e5efa2017-09-12 00:54:26 -07001107 if (Status() == kOatBootImageOutOfDate) {
1108 return kDex2OatForBootImage;
1109 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001110 }
1111
1112 if (oat_file_assistant_->HasOriginalDexFiles()) {
1113 return kDex2OatFromScratch;
1114 } else {
1115 // Otherwise there is nothing we can do, even if we want to.
1116 return kNoDexOptNeeded;
1117 }
Richard Uhler70a84262016-11-08 16:51:51 +00001118}
1119
Richard Uhler743bf362016-04-19 15:39:37 -07001120const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1121 CHECK(!file_released_) << "GetFile called after oat file released.";
1122 if (!load_attempted_) {
1123 load_attempted_ = true;
1124 if (filename_provided_) {
1125 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001126 if (use_fd_) {
1127 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1128 file_.reset(OatFile::Open(vdex_fd_,
1129 oat_fd_,
1130 filename_.c_str(),
1131 nullptr,
1132 nullptr,
1133 oat_file_assistant_->load_executable_,
1134 false /* low_4gb */,
1135 oat_file_assistant_->dex_location_.c_str(),
1136 &error_msg));
1137 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001138 } else {
1139 file_.reset(OatFile::Open(filename_.c_str(),
1140 filename_.c_str(),
1141 nullptr,
1142 nullptr,
1143 oat_file_assistant_->load_executable_,
1144 false /* low_4gb */,
1145 oat_file_assistant_->dex_location_.c_str(),
1146 &error_msg));
1147 }
Richard Uhler743bf362016-04-19 15:39:37 -07001148 if (file_.get() == nullptr) {
1149 VLOG(oat) << "OatFileAssistant test for existing oat file "
1150 << filename_ << ": " << error_msg;
1151 }
1152 }
1153 }
1154 return file_.get();
1155}
1156
1157bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001158 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001159 const OatFile* file = GetFile();
1160 if (file == nullptr) {
1161 return false;
1162 }
1163
1164 CompilerFilter::Filter current = file->GetCompilerFilter();
1165 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1166 VLOG(oat) << "Compiler filter not okay because Profile changed";
1167 return false;
1168 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001169 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1170 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001171}
1172
Calin Juravle44e5efa2017-09-12 00:54:26 -07001173bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1174 if (context == nullptr) {
1175 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1176 return true;
1177 }
1178
1179 const OatFile* file = GetFile();
1180 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001181 // No oat file means we have nothing to verify.
1182 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001183 }
1184
Calin Juravle20c46442017-09-12 00:54:26 -07001185 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001186 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001187 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001188 : "";
1189
1190 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1191 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1192 return false;
1193 }
1194
1195 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext());
1196 if (!result) {
1197 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1198 << file->GetClassLoaderContext()
1199 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1200 }
1201 return result;
1202}
1203
Richard Uhler743bf362016-04-19 15:39:37 -07001204bool OatFileAssistant::OatFileInfo::IsExecutable() {
1205 const OatFile* file = GetFile();
1206 return (file != nullptr && file->IsExecutable());
1207}
1208
Richard Uhler743bf362016-04-19 15:39:37 -07001209void OatFileAssistant::OatFileInfo::Reset() {
1210 load_attempted_ = false;
1211 file_.reset();
1212 status_attempted_ = false;
1213}
1214
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001215void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, bool use_fd, int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001216 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001217 filename_provided_ = true;
1218 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001219 use_fd_ = use_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001220 vdex_fd_ = vdex_fd;
1221 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001222 Reset();
1223}
1224
1225std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1226 file_released_ = true;
1227 return std::move(file_);
1228}
1229
Richard Uhler70a84262016-11-08 16:51:51 +00001230std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001231 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001232 return ReleaseFile();
1233 }
1234
1235 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1236 << " attempting to fall back to interpreting oat file instead.";
1237
Richard Uhler03bc6592016-11-22 09:42:04 +00001238 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001239 return ReleaseFile();
1240 }
1241
Richard Uhler03bc6592016-11-22 09:42:04 +00001242 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001243 // We are loading an oat file for runtime use that needs relocation.
1244 // Reload the file non-executable to ensure that we interpret out of the
1245 // dex code in the oat file rather than trying to execute the unrelocated
1246 // compiled code.
1247 oat_file_assistant_->load_executable_ = false;
1248 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001249 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001250 CHECK(!IsExecutable());
1251 return ReleaseFile();
1252 }
1253 }
1254 return std::unique_ptr<OatFile>();
1255}
Richard Uhler66d874d2015-01-15 09:37:19 -08001256} // namespace art