blob: 240030cf5b892e6e42f6b5f26ff869e699c243fd [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 Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080032#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "gc/heap.h"
34#include "gc/space/image_space.h"
35#include "image.h"
36#include "oat.h"
37#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000041#include "vdex_file.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070042#include "class_loader_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080043
44namespace art {
45
Richard Uhler69bcf2c2017-01-24 10:25:21 +000046using android::base::StringPrintf;
47
Narayan Kamath8943c1d2016-05-02 13:14:48 +010048std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
49 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000050 case OatFileAssistant::kOatCannotOpen:
51 stream << "kOatCannotOpen";
52 break;
53 case OatFileAssistant::kOatDexOutOfDate:
54 stream << "kOatDexOutOfDate";
55 break;
56 case OatFileAssistant::kOatBootImageOutOfDate:
57 stream << "kOatBootImageOutOfDate";
58 break;
59 case OatFileAssistant::kOatRelocationOutOfDate:
60 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010061 break;
62 case OatFileAssistant::kOatUpToDate:
63 stream << "kOatUpToDate";
64 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010065 default:
66 UNREACHABLE();
67 }
68
69 return stream;
70}
71
Richard Uhler66d874d2015-01-15 09:37:19 -080072OatFileAssistant::OatFileAssistant(const char* dex_location,
73 const InstructionSet isa,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070074 bool load_executable)
75 : OatFileAssistant(dex_location,
76 isa, load_executable,
77 -1 /* vdex_fd */,
78 -1 /* oat_fd */,
79 -1 /* zip_fd */) {}
80
81
82OatFileAssistant::OatFileAssistant(const char* dex_location,
83 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070084 bool load_executable,
85 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070086 int oat_fd,
87 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000088 : isa_(isa),
89 load_executable_(load_executable),
90 odex_(this, /*is_oat_location*/ false),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070091 oat_(this, /*is_oat_location*/ true),
92 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -070093 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000094
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070095 if (zip_fd < 0) {
96 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
97 << " oat_fd=" << oat_fd;
98 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
99 << " vdex_fd=" << vdex_fd;;
100 }
101
Calin Juravle099f8d62017-09-05 19:04:04 -0700102 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700103
Richard Uhler66d874d2015-01-15 09:37:19 -0800104 if (load_executable_ && isa != kRuntimeISA) {
105 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
106 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
107 load_executable_ = false;
108 }
109
Richard Uhler743bf362016-04-19 15:39:37 -0700110 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700111 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700112 std::string odex_file_name;
113 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700114 odex_.Reset(odex_file_name, UseFdToReadFiles(), vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700115 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700116 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
117 }
118
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700119 if (!UseFdToReadFiles()) {
120 // Get the oat filename.
121 std::string oat_file_name;
122 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
123 oat_.Reset(oat_file_name, false /* use_fd */);
124 } else {
125 LOG(WARNING) << "Failed to determine oat file name for dex location "
126 << dex_location_ << ": " << error_msg;
127 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000128 }
129
130 // Check if the dex directory is writable.
131 // This will be needed in most uses of OatFileAssistant and so it's OK to
132 // compute it eagerly. (the only use which will not make use of it is
133 // OatFileAssistant::GetStatusDump())
134 size_t pos = dex_location_.rfind('/');
135 if (pos == std::string::npos) {
136 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700137 } else if (!UseFdToReadFiles()) {
138 // We cannot test for parent access when using file descriptors. That's ok
139 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000140 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700141 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000142 dex_parent_writable_ = true;
143 } else {
144 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700145 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800146 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800147}
148
149OatFileAssistant::~OatFileAssistant() {
150 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100151 if (flock_.get() != nullptr) {
152 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800153 }
154}
155
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700156bool OatFileAssistant::UseFdToReadFiles() {
157 return zip_fd_ >= 0;
158}
159
Richard Uhler66d874d2015-01-15 09:37:19 -0800160bool OatFileAssistant::IsInBootClassPath() {
161 // Note: We check the current boot class path, regardless of the ISA
162 // specified by the user. This is okay, because the boot class path should
163 // be the same for all ISAs.
164 // TODO: Can we verify the boot class path is the same for all ISAs?
165 Runtime* runtime = Runtime::Current();
166 ClassLinker* class_linker = runtime->GetClassLinker();
167 const auto& boot_class_path = class_linker->GetBootClassPath();
168 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700169 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800170 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
171 return true;
172 }
173 }
174 return false;
175}
176
177bool OatFileAssistant::Lock(std::string* error_msg) {
178 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100179 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800180
Calin Juravle357c66d2017-05-04 01:57:17 +0000181 // Note the lock will only succeed for secondary dex files and in test
182 // environment.
183 //
184 // The lock *will fail* for all primary apks in a production environment.
185 // The app does not have permissions to create locks next to its dex location
186 // (be it system, data or vendor parition). We also cannot use the odex or
187 // oat location for the same reasoning.
188 //
189 // This is best effort and if it fails it's unlikely that we will be able
190 // to generate oat files anyway.
191 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800192
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100193 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
194 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100195 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800196 return false;
197 }
198 return true;
199}
200
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700201int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
202 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700203 bool downgrade,
204 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000205 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700206 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
207 profile_changed,
208 downgrade,
209 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000210 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
211 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800212 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000213 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800214}
215
Richard Uhlerf4b34872016-04-13 11:03:46 -0700216// Figure out the currently specified compile filter option in the runtime.
217// Returns true on success, false if the compiler filter is invalid, in which
218// case error_msg describes the problem.
219static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
220 std::string* error_msg) {
221 CHECK(filter != nullptr);
222 CHECK(error_msg != nullptr);
223
Calin Juravle357c66d2017-05-04 01:57:17 +0000224 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700225 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
226 if (option.starts_with("--compiler-filter=")) {
227 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
228 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
229 *error_msg = std::string("Unknown --compiler-filter value: ")
230 + std::string(compiler_filter_string);
231 return false;
232 }
233 }
234 }
235 return true;
236}
237
Richard Uhler01be6812016-05-17 10:34:52 -0700238bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000239 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700240}
241
Richard Uhler1e860612016-03-30 12:17:55 -0700242OatFileAssistant::ResultOfAttemptToUpdate
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700243OatFileAssistant::MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700244 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700245 std::string* error_msg) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700246 // The method doesn't use zip_fd_ and directly opens dex files at dex_locations_.
247 CHECK_EQ(-1, zip_fd_) << "MakeUpToDate should not be called with zip_fd";
248
Richard Uhlerf4b34872016-04-13 11:03:46 -0700249 CompilerFilter::Filter target;
250 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
251 return kUpdateNotAttempted;
252 }
253
Richard Uhler88bc6732016-11-14 14:38:03 +0000254 OatFileInfo& info = GetBestInfo();
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700255 // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291.
256 // This is actually not trivial in the current logic as it will interact with the collision
257 // check:
258 // - currently, if the context does not match but we have no collisions we still accept the
259 // oat file.
260 // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make
261 // the oat code up to date the collision check becomes useless.
262 // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files
263 // loaded in other processes). So it boils down to how far do we want to complicate
264 // 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 -0700265 switch (info.GetDexOptNeeded(
266 target, profile_changed, /*downgrade*/ false, class_loader_context)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000267 case kNoDexOptNeeded:
268 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000269
Richard Uhler7225a8d2016-11-22 10:12:03 +0000270 // TODO: For now, don't bother with all the different ways we can call
271 // dex2oat to generate the oat file. Always generate the oat file as if it
272 // were kDex2OatFromScratch.
273 case kDex2OatFromScratch:
274 case kDex2OatForBootImage:
275 case kDex2OatForRelocation:
276 case kDex2OatForFilter:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700277 return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800278 }
279 UNREACHABLE();
280}
281
282std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000283 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800284}
285
Richard Uhler46cc64f2016-11-14 14:53:55 +0000286std::string OatFileAssistant::GetStatusDump() {
287 std::ostringstream status;
288 bool oat_file_exists = false;
289 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000290 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000291 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000292 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000293
Richard Uhler46cc64f2016-11-14 14:53:55 +0000294 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000295 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
296 const OatFile* file = oat_.GetFile();
297 if (file == nullptr) {
298 // If the file is null even though the status is not kOatCannotOpen, it
299 // means we must have a vdex file with no corresponding oat file. In
300 // this case we cannot determine the compilation filter. Indicate that
301 // we have only the vdex file instead.
302 status << "vdex-only";
303 } else {
304 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
305 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000306 }
307
Richard Uhler03bc6592016-11-22 09:42:04 +0000308 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000309 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000310 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000311
Richard Uhler46cc64f2016-11-14 14:53:55 +0000312 odex_file_exists = true;
313 if (oat_file_exists) {
314 status << "] ";
315 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000316 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
317 const OatFile* file = odex_.GetFile();
318 if (file == nullptr) {
319 status << "vdex-only";
320 } else {
321 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
322 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000323 }
324
325 if (!oat_file_exists && !odex_file_exists) {
326 status << "invalid[";
327 }
328
329 status << "]";
330 return status.str();
331}
332
Richard Uhler66d874d2015-01-15 09:37:19 -0800333std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700334 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800335 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700336 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
337 return dex_files;
338 } else {
339 return std::vector<std::unique_ptr<const DexFile>>();
340 }
341}
Richard Uhler66d874d2015-01-15 09:37:19 -0800342
Calin Juravle87e2cb62017-06-13 21:48:45 -0700343bool OatFileAssistant::LoadDexFiles(
344 const OatFile &oat_file,
345 const std::string& dex_location,
346 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000347 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800348 std::string error_msg;
349 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700350 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800351 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700352 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700353 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800354 }
355
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700356 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800357 if (dex_file.get() == nullptr) {
358 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700359 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800360 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700361 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800362
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000363 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700364 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700365 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000366 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700367 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000368 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 break;
370 }
371
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700372 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800373 if (dex_file.get() == nullptr) {
374 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700375 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800376 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700377 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800378 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700379 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800380}
381
Richard Uhler9b994ea2015-06-24 08:44:19 -0700382bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000383 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700384 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000385 // GetRequiredDexChecksums.
386 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700387 return has_original_dex_files_;
388}
389
Richard Uhler95abd042015-03-24 09:51:28 -0700390OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700391 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800392}
393
Richard Uhler95abd042015-03-24 09:51:28 -0700394OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700395 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800396}
397
Richard Uhler2f27abd2017-01-31 14:02:34 +0000398bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000399 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
400 if (required_dex_checksums == nullptr) {
401 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
402 return true;
403 }
404
405 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
406 if (required_dex_checksums->size() != number_of_dex_files) {
407 *error_msg = StringPrintf("expected %zu dex files but found %u",
408 required_dex_checksums->size(),
409 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000410 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800411 }
412
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000413 for (uint32_t i = 0; i < number_of_dex_files; i++) {
414 uint32_t expected_checksum = (*required_dex_checksums)[i];
415 uint32_t actual_checksum = file.GetLocationChecksum(i);
416 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700417 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000418 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
419 "Expected: %u, actual: %u",
420 dex.c_str(),
421 expected_checksum,
422 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000423 return false;
424 }
425 }
426
Richard Uhler2f27abd2017-01-31 14:02:34 +0000427 return true;
428}
429
430bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000431 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
432 if (required_dex_checksums == nullptr) {
433 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
434 return true;
435 }
436
437 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
438 if (required_dex_checksums->size() != number_of_dex_files) {
439 *error_msg = StringPrintf("expected %zu dex files but found %u",
440 required_dex_checksums->size(),
441 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000442 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800443 }
444
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000445 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700446 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000447 uint32_t expected_checksum = (*required_dex_checksums)[i];
448 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
449 if (oat_dex_file == nullptr) {
450 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
451 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800452 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000453 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
454 if (expected_checksum != actual_checksum) {
455 VLOG(oat) << "Dex checksum does not match for dex: " << dex
456 << ". Expected: " << expected_checksum
457 << ", Actual: " << actual_checksum;
458 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 }
460 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000461 return true;
462}
463
464OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
465 // Verify the ART_USE_READ_BARRIER state.
466 // TODO: Don't fully reject files due to read barrier state. If they contain
467 // compiled code and are otherwise okay, we should return something like
468 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
469 // barrier state doesn't matter.
470 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
471 constexpr bool kRuntimeIsCC = kUseReadBarrier;
472 if (is_cc != kRuntimeIsCC) {
473 return kOatCannotOpen;
474 }
475
476 // Verify the dex checksum.
477 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000478 VdexFile* vdex = file.GetVdexFile();
479 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
480 LOG(ERROR) << error_msg;
481 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000482 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800483
Andreas Gampe29d38e72016-03-23 15:31:51 +0000484 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000485
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000487 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
488 const ImageInfo* image_info = GetImageInfo();
489 if (image_info == nullptr) {
490 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000491
Richard Uhler76f5cb62016-04-04 13:30:16 -0700492 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000493 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700494 }
495
496 // If there is no original dex file to fall back to, grudgingly accept
497 // the oat file. This could technically lead to crashes, but there's no
498 // way we could find a better oat file to use for this dex location,
499 // and it's better than being stuck in a boot loop with no way out.
500 // The problem will hopefully resolve itself the next time the runtime
501 // starts up.
502 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
503 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000504 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000505 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000506 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000507 }
508 } else {
509 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800510 }
511
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100512 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000513 if (!file.IsPic()) {
514 const ImageInfo* image_info = GetImageInfo();
515 if (image_info == nullptr) {
516 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000517 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000518 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700519
Andreas Gampe29d38e72016-03-23 15:31:51 +0000520 // Verify the oat_data_begin recorded for the image in the oat file matches
521 // the actual oat_data_begin for boot.oat in the image.
522 const OatHeader& oat_header = file.GetOatHeader();
523 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
524 if (oat_data_begin != image_info->oat_data_begin) {
525 VLOG(oat) << file.GetLocation() <<
526 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
527 << " does not match actual image oat_data_begin ("
528 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000529 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000530 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700531
Andreas Gampe29d38e72016-03-23 15:31:51 +0000532 // Verify the oat_patch_delta recorded for the image in the oat file matches
533 // the actual oat_patch_delta for the image.
534 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
535 if (oat_patch_delta != image_info->patch_delta) {
536 VLOG(oat) << file.GetLocation() <<
537 ": Oat file image patch delta (" << oat_patch_delta << ")"
538 << " does not match actual image patch delta ("
539 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000540 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000541 }
542 } else {
543 // Oat files compiled in PIC mode do not require relocation.
544 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
545 }
546 } else {
547 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800548 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700549 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800550}
551
Calin Juravle357c66d2017-05-04 01:57:17 +0000552static bool DexLocationToOdexNames(const std::string& location,
553 InstructionSet isa,
554 std::string* odex_filename,
555 std::string* oat_dir,
556 std::string* isa_dir,
557 std::string* error_msg) {
558 CHECK(odex_filename != nullptr);
559 CHECK(error_msg != nullptr);
560
561 // The odex file name is formed by replacing the dex_location extension with
562 // .odex and inserting an oat/<isa> directory. For example:
563 // location = /foo/bar/baz.jar
564 // odex_location = /foo/bar/oat/<isa>/baz.odex
565
566 // Find the directory portion of the dex location and add the oat/<isa>
567 // directory.
568 size_t pos = location.rfind('/');
569 if (pos == std::string::npos) {
570 *error_msg = "Dex location " + location + " has no directory.";
571 return false;
572 }
573 std::string dir = location.substr(0, pos+1);
574 // Add the oat directory.
575 dir += "oat";
576 if (oat_dir != nullptr) {
577 *oat_dir = dir;
578 }
579 // Add the isa directory
580 dir += "/" + std::string(GetInstructionSetString(isa));
581 if (isa_dir != nullptr) {
582 *isa_dir = dir;
583 }
584
585 // Get the base part of the file without the extension.
586 std::string file = location.substr(pos+1);
587 pos = file.rfind('.');
588 if (pos == std::string::npos) {
589 *error_msg = "Dex location " + location + " has no extension.";
590 return false;
591 }
592 std::string base = file.substr(0, pos);
593
594 *odex_filename = dir + "/" + base + ".odex";
595 return true;
596}
597
598// Prepare a subcomponent of the odex directory.
599// (i.e. create and set the expected permissions on the path `dir`).
600static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
601 struct stat dir_stat;
602 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
603 // The directory exists. Check if it is indeed a directory.
604 if (!S_ISDIR(dir_stat.st_mode)) {
605 *error_msg = dir + " is not a dir";
606 return false;
607 } else {
608 // The dir is already on disk.
609 return true;
610 }
611 }
612
613 // Failed to stat. We need to create the directory.
614 if (errno != ENOENT) {
615 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
616 return false;
617 }
618
619 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
620 if (mkdir(dir.c_str(), mode) != 0) {
621 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
622 return false;
623 }
624 if (chmod(dir.c_str(), mode) != 0) {
625 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
626 return false;
627 }
628 return true;
629}
630
631// Prepares the odex directory for the given dex location.
632static bool PrepareOdexDirectories(const std::string& dex_location,
633 const std::string& expected_odex_location,
634 InstructionSet isa,
635 std::string* error_msg) {
636 std::string actual_odex_location;
637 std::string oat_dir;
638 std::string isa_dir;
639 if (!DexLocationToOdexNames(
640 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
641 return false;
642 }
643 DCHECK_EQ(expected_odex_location, actual_odex_location);
644
645 if (!PrepareDirectory(oat_dir, error_msg)) {
646 return false;
647 }
648 if (!PrepareDirectory(isa_dir, error_msg)) {
649 return false;
650 }
651 return true;
652}
653
Calin Juravled4215bb2017-09-20 11:54:21 -0700654class Dex2oatFileWrapper {
655 public:
656 explicit Dex2oatFileWrapper(File* file)
657 : file_(file),
658 unlink_file_at_destruction_(true) {
659 }
660
661 ~Dex2oatFileWrapper() {
662 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
663 file_->Erase(/*unlink*/ true);
664 }
665 }
666
667 File* GetFile() { return file_.get(); }
668
669 void DisableUnlinkAtDestruction() {
670 unlink_file_at_destruction_ = false;
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800671 }
Calin Juravled4215bb2017-09-20 11:54:21 -0700672
673 private:
674 std::unique_ptr<File> file_;
675 bool unlink_file_at_destruction_;
676};
677
Calin Juravle357c66d2017-05-04 01:57:17 +0000678OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700679 OatFileAssistant::OatFileInfo& info,
680 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700681 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700682 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800683 CHECK(error_msg != nullptr);
684
Richard Uhler8327cf72015-10-13 16:34:59 -0700685 Runtime* runtime = Runtime::Current();
686 if (!runtime->IsDex2OatEnabled()) {
687 *error_msg = "Generation of oat file for dex location " + dex_location_
688 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700689 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700690 }
691
Calin Juravle357c66d2017-05-04 01:57:17 +0000692 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700693 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800694 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700695 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800696 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000697 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700698 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800699
Richard Uhler66d874d2015-01-15 09:37:19 -0800700 // dex2oat ignores missing dex files and doesn't report an error.
701 // Check explicitly here so we can detect the error properly.
702 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000703 struct stat dex_path_stat;
704 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
705 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700706 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800707 }
708
Calin Juravle357c66d2017-05-04 01:57:17 +0000709 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
710 if (!info.IsOatLocation()) {
711 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
712 return kUpdateNotAttempted;
713 }
714 }
715
716 // Set the permissions for the oat and the vdex files.
717 // The user always gets read and write while the group and others propagate
718 // the reading access of the original dex file.
719 mode_t file_mode = S_IRUSR | S_IWUSR |
720 (dex_path_stat.st_mode & S_IRGRP) |
721 (dex_path_stat.st_mode & S_IROTH);
722
Calin Juravled4215bb2017-09-20 11:54:21 -0700723 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
724 File* vdex_file = vdex_file_wrapper.GetFile();
725 if (vdex_file == nullptr) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100726 *error_msg = "Generation of oat file " + oat_file_name
727 + " not attempted because the vdex file " + vdex_file_name
728 + " could not be opened.";
729 return kUpdateNotAttempted;
730 }
731
Calin Juravle357c66d2017-05-04 01:57:17 +0000732 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100733 *error_msg = "Generation of oat file " + oat_file_name
734 + " not attempted because the vdex file " + vdex_file_name
735 + " could not be made world readable.";
736 return kUpdateNotAttempted;
737 }
738
Calin Juravled4215bb2017-09-20 11:54:21 -0700739 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
740 File* oat_file = oat_file_wrapper.GetFile();
741 if (oat_file == nullptr) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700742 *error_msg = "Generation of oat file " + oat_file_name
743 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700744 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700745 }
746
Calin Juravle357c66d2017-05-04 01:57:17 +0000747 if (fchmod(oat_file->Fd(), file_mode) != 0) {
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 made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700750 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700751 }
752
753 std::vector<std::string> args;
754 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000755 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700756 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
757 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000758 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700759 const std::string dex2oat_context = class_loader_context == nullptr
760 ? OatFile::kSpecialSharedLibrary
761 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
762 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700763
Richard Uhler66d874d2015-01-15 09:37:19 -0800764 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700765 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700766 }
767
David Brazdil7b49e6c2016-09-01 11:06:18 +0100768 if (vdex_file->FlushCloseOrErase() != 0) {
769 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100770 return kUpdateFailed;
771 }
772
Richard Uhler8327cf72015-10-13 16:34:59 -0700773 if (oat_file->FlushCloseOrErase() != 0) {
774 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700775 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800776 }
777
Calin Juravle357c66d2017-05-04 01:57:17 +0000778 // Mark that the odex file has changed and we should try to reload.
779 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700780 // We have compiled successfully. Disable the auto-unlink.
781 vdex_file_wrapper.DisableUnlinkAtDestruction();
782 oat_file_wrapper.DisableUnlinkAtDestruction();
783
Richard Uhler1e860612016-03-30 12:17:55 -0700784 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800785}
786
787bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
788 std::string* error_msg) {
789 Runtime* runtime = Runtime::Current();
790 std::string image_location = ImageLocation();
791 if (image_location.empty()) {
792 *error_msg = "No image location found for Dex2Oat.";
793 return false;
794 }
795
796 std::vector<std::string> argv;
797 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000798 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200799 argv.push_back("--debuggable");
800 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700801 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800802
803 if (!runtime->IsVerificationEnabled()) {
804 argv.push_back("--compiler-filter=verify-none");
805 }
806
807 if (runtime->MustRelocateIfPossible()) {
808 argv.push_back("--runtime-arg");
809 argv.push_back("-Xrelocate");
810 } else {
811 argv.push_back("--runtime-arg");
812 argv.push_back("-Xnorelocate");
813 }
814
815 if (!kIsTargetBuild) {
816 argv.push_back("--host");
817 }
818
819 argv.push_back("--boot-image=" + image_location);
820
821 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
822 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
823
824 argv.insert(argv.end(), args.begin(), args.end());
825
Andreas Gampe9186ced2016-12-12 14:28:21 -0800826 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800827 return Exec(argv, error_msg);
828}
829
Richard Uhlerb81881d2016-04-19 13:08:04 -0700830bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
831 InstructionSet isa,
832 std::string* odex_filename,
833 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000834 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800835}
836
Richard Uhlerb81881d2016-04-19 13:08:04 -0700837bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
838 InstructionSet isa,
839 std::string* oat_filename,
840 std::string* error_msg) {
841 CHECK(oat_filename != nullptr);
842 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800843
Richard Uhler55b58b62016-08-12 09:05:13 -0700844 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
845 if (cache_dir.empty()) {
846 *error_msg = "Dalvik cache directory does not exist";
847 return false;
848 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700849
850 // TODO: The oat file assistant should be the definitive place for
851 // determining the oat file name from the dex location, not
852 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700853 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800854}
855
Richard Uhler66d874d2015-01-15 09:37:19 -0800856std::string OatFileAssistant::ImageLocation() {
857 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000858 const std::vector<gc::space::ImageSpace*>& image_spaces =
859 runtime->GetHeap()->GetBootImageSpaces();
860 if (image_spaces.empty()) {
861 return "";
862 }
863 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800864}
865
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000866const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
867 if (!required_dex_checksums_attempted_) {
868 required_dex_checksums_attempted_ = true;
869 required_dex_checksums_found_ = false;
870 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800871 std::string error_msg;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700872 if (DexFileLoader::GetMultiDexChecksums(dex_location_.c_str(),
873 &cached_required_dex_checksums_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700874 &error_msg,
875 zip_fd_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000876 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700877 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800878 } else {
879 // This can happen if the original dex file has been stripped from the
880 // apk.
881 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700882 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800883
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000884 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700885 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800886 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000887 required_dex_checksums_found_ = true;
888 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700889 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000890 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
891 if (odex_dex_file == nullptr) {
892 required_dex_checksums_found_ = false;
893 break;
894 }
895 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800896 }
897 }
898 }
899 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000900 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800901}
902
Richard Uhler95e09672017-02-22 11:37:41 +0000903std::unique_ptr<OatFileAssistant::ImageInfo>
904OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
905 CHECK(error_msg != nullptr);
906
Richard Uhler95e09672017-02-22 11:37:41 +0000907 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000908 std::unique_ptr<ImageInfo> info(new ImageInfo());
909 info->location = runtime->GetImageLocation();
910
911 std::unique_ptr<ImageHeader> image_header(
912 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
913 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000914 return nullptr;
915 }
916
Richard Uhler8f104862017-02-22 12:34:01 +0000917 info->oat_checksum = image_header->GetOatChecksum();
918 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
919 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000920 return info;
921}
922
Richard Uhler66d874d2015-01-15 09:37:19 -0800923const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
924 if (!image_info_load_attempted_) {
925 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000926 std::string error_msg;
927 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
928 if (cached_image_info_ == nullptr) {
929 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800930 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800931 }
Richard Uhler95e09672017-02-22 11:37:41 +0000932 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800933}
934
Richard Uhler88bc6732016-11-14 14:38:03 +0000935OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000936 // TODO(calin): Document the side effects of class loading when
937 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700938 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000939 // If the parent of the dex file is writable it means that we can
940 // create the odex file. In this case we unconditionally pick the odex
941 // as the best oat file. This corresponds to the regular use case when
942 // apps gets installed or when they load private, secondary dex file.
943 // For apps on the system partition the odex location will not be
944 // writable and thus the oat location might be more up to date.
945 return odex_;
946 }
947
948 // We cannot write to the odex location. This must be a system app.
949
950 // If the oat location is usable take it.
951 if (oat_.IsUseable()) {
952 return oat_;
953 }
954
955 // The oat file is not usable but the odex file might be up to date.
956 // This is an indication that we are dealing with an up to date prebuilt
957 // (that doesn't need relocation).
958 if (odex_.Status() == kOatUpToDate) {
959 return odex_;
960 }
961
962 // The oat file is not usable and the odex file is not up to date.
963 // However we have access to the original dex file which means we can make
964 // the oat location up to date.
965 if (HasOriginalDexFiles()) {
966 return oat_;
967 }
968
969 // We got into the worst situation here:
970 // - the oat location is not usable
971 // - the prebuild odex location is not up to date
972 // - and we don't have the original dex file anymore (stripped).
973 // Pick the odex if it exists, or the oat if not.
974 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000975}
976
Andreas Gampea463b6a2016-08-12 21:53:32 -0700977std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800978 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100979 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800980 if (art_file.empty()) {
981 return nullptr;
982 }
983 std::string error_msg;
984 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700985 std::unique_ptr<gc::space::ImageSpace> ret =
986 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800987 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800988 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
989 }
990 return ret;
991}
992
Richard Uhler88bc6732016-11-14 14:38:03 +0000993OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
994 bool is_oat_location)
995 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700996{}
997
Richard Uhler88bc6732016-11-14 14:38:03 +0000998bool OatFileAssistant::OatFileInfo::IsOatLocation() {
999 return is_oat_location_;
1000}
1001
Richard Uhler743bf362016-04-19 15:39:37 -07001002const std::string* OatFileAssistant::OatFileInfo::Filename() {
1003 return filename_provided_ ? &filename_ : nullptr;
1004}
1005
Richard Uhler03bc6592016-11-22 09:42:04 +00001006bool OatFileAssistant::OatFileInfo::IsUseable() {
1007 switch (Status()) {
1008 case kOatCannotOpen:
1009 case kOatDexOutOfDate:
1010 case kOatBootImageOutOfDate: return false;
1011
1012 case kOatRelocationOutOfDate:
1013 case kOatUpToDate: return true;
1014 }
1015 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001016}
1017
1018OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1019 if (!status_attempted_) {
1020 status_attempted_ = true;
1021 const OatFile* file = GetFile();
1022 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001023 // Check to see if there is a vdex file we can make use of.
1024 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001025 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001026 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001027 if (use_fd_) {
1028 if (vdex_fd_ >= 0) {
1029 struct stat s;
1030 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1031 if (rc == -1) {
1032 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1033 } else {
1034 vdex = VdexFile::Open(vdex_fd_,
1035 s.st_size,
1036 vdex_filename,
1037 false /*writable*/,
1038 false /*low_4gb*/,
1039 false /* unquicken */,
1040 &error_msg);
1041 }
1042 }
1043 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001044 vdex = VdexFile::Open(vdex_filename,
1045 false /*writeable*/,
1046 false /*low_4gb*/,
1047 false /*unquicken*/,
1048 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001049 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001050 if (vdex == nullptr) {
1051 status_ = kOatCannotOpen;
1052 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1053 } else {
1054 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1055 // The vdex file does not contain enough information to determine
1056 // whether it is up to date with respect to the boot image, so we
1057 // assume it is out of date.
1058 VLOG(oat) << error_msg;
1059 status_ = kOatBootImageOutOfDate;
1060 } else {
1061 status_ = kOatDexOutOfDate;
1062 }
1063 }
Richard Uhler743bf362016-04-19 15:39:37 -07001064 } else {
1065 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001066 VLOG(oat) << file->GetLocation() << " is " << status_
1067 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001068 }
1069 }
1070 return status_;
1071}
1072
Richard Uhler70a84262016-11-08 16:51:51 +00001073OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001074 CompilerFilter::Filter target,
1075 bool profile_changed,
1076 bool downgrade,
1077 ClassLoaderContext* context) {
1078
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001079 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001080 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001081 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001082
Calin Juravle44e5efa2017-09-12 00:54:26 -07001083 // Only check the filter and relocation if the class loader context is ok.
1084 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1085 if (class_loader_context_okay) {
1086 if (filter_okay && Status() == kOatUpToDate) {
1087 // The oat file is in good shape as is.
1088 return kNoDexOptNeeded;
1089 }
Richard Uhler70a84262016-11-08 16:51:51 +00001090
Calin Juravle44e5efa2017-09-12 00:54:26 -07001091 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1092 // If no compilation is desired, then it doesn't matter if the oat
1093 // file needs relocation. It's in good shape as is.
1094 return kNoDexOptNeeded;
1095 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001096
Calin Juravle44e5efa2017-09-12 00:54:26 -07001097 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1098 return kDex2OatForRelocation;
1099 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001100
Calin Juravle44e5efa2017-09-12 00:54:26 -07001101 if (IsUseable()) {
1102 return kDex2OatForFilter;
1103 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001104
Calin Juravle44e5efa2017-09-12 00:54:26 -07001105 if (Status() == kOatBootImageOutOfDate) {
1106 return kDex2OatForBootImage;
1107 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001108 }
1109
1110 if (oat_file_assistant_->HasOriginalDexFiles()) {
1111 return kDex2OatFromScratch;
1112 } else {
1113 // Otherwise there is nothing we can do, even if we want to.
1114 return kNoDexOptNeeded;
1115 }
Richard Uhler70a84262016-11-08 16:51:51 +00001116}
1117
Richard Uhler743bf362016-04-19 15:39:37 -07001118const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1119 CHECK(!file_released_) << "GetFile called after oat file released.";
1120 if (!load_attempted_) {
1121 load_attempted_ = true;
1122 if (filename_provided_) {
1123 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001124 if (use_fd_) {
1125 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1126 file_.reset(OatFile::Open(vdex_fd_,
1127 oat_fd_,
1128 filename_.c_str(),
1129 nullptr,
1130 nullptr,
1131 oat_file_assistant_->load_executable_,
1132 false /* low_4gb */,
1133 oat_file_assistant_->dex_location_.c_str(),
1134 &error_msg));
1135 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001136 } else {
1137 file_.reset(OatFile::Open(filename_.c_str(),
1138 filename_.c_str(),
1139 nullptr,
1140 nullptr,
1141 oat_file_assistant_->load_executable_,
1142 false /* low_4gb */,
1143 oat_file_assistant_->dex_location_.c_str(),
1144 &error_msg));
1145 }
Richard Uhler743bf362016-04-19 15:39:37 -07001146 if (file_.get() == nullptr) {
1147 VLOG(oat) << "OatFileAssistant test for existing oat file "
1148 << filename_ << ": " << error_msg;
1149 }
1150 }
1151 }
1152 return file_.get();
1153}
1154
1155bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001156 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001157 const OatFile* file = GetFile();
1158 if (file == nullptr) {
1159 return false;
1160 }
1161
1162 CompilerFilter::Filter current = file->GetCompilerFilter();
1163 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1164 VLOG(oat) << "Compiler filter not okay because Profile changed";
1165 return false;
1166 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001167 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1168 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001169}
1170
Calin Juravle44e5efa2017-09-12 00:54:26 -07001171bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1172 if (context == nullptr) {
1173 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1174 return true;
1175 }
1176
1177 const OatFile* file = GetFile();
1178 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001179 // No oat file means we have nothing to verify.
1180 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001181 }
1182
Calin Juravle20c46442017-09-12 00:54:26 -07001183 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001184 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001185 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001186 : "";
1187
1188 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1189 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1190 return false;
1191 }
1192
1193 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext());
1194 if (!result) {
1195 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1196 << file->GetClassLoaderContext()
1197 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1198 }
1199 return result;
1200}
1201
Richard Uhler743bf362016-04-19 15:39:37 -07001202bool OatFileAssistant::OatFileInfo::IsExecutable() {
1203 const OatFile* file = GetFile();
1204 return (file != nullptr && file->IsExecutable());
1205}
1206
Richard Uhler743bf362016-04-19 15:39:37 -07001207void OatFileAssistant::OatFileInfo::Reset() {
1208 load_attempted_ = false;
1209 file_.reset();
1210 status_attempted_ = false;
1211}
1212
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001213void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, bool use_fd, int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001214 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001215 filename_provided_ = true;
1216 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001217 use_fd_ = use_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001218 vdex_fd_ = vdex_fd;
1219 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001220 Reset();
1221}
1222
1223std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1224 file_released_ = true;
1225 return std::move(file_);
1226}
1227
Richard Uhler70a84262016-11-08 16:51:51 +00001228std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001229 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001230 return ReleaseFile();
1231 }
1232
1233 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1234 << " attempting to fall back to interpreting oat file instead.";
1235
Richard Uhler03bc6592016-11-22 09:42:04 +00001236 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001237 return ReleaseFile();
1238 }
1239
Richard Uhler03bc6592016-11-22 09:42:04 +00001240 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001241 // We are loading an oat file for runtime use that needs relocation.
1242 // Reload the file non-executable to ensure that we interpret out of the
1243 // dex code in the oat file rather than trying to execute the unrelocated
1244 // compiled code.
1245 oat_file_assistant_->load_executable_ = false;
1246 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001247 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001248 CHECK(!IsExecutable());
1249 return ReleaseFile();
1250 }
1251 }
1252 return std::unique_ptr<OatFile>();
1253}
Richard Uhler66d874d2015-01-15 09:37:19 -08001254} // namespace art