blob: b8a13da9b366cbdc21bb8296d93215da80a18573 [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"
Richard Uhler66d874d2015-01-15 09:37:19 -080027#include "base/logging.h"
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"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070031#include "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;
478 if (kIsVdexEnabled) {
479 VdexFile* vdex = file.GetVdexFile();
480 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
481 LOG(ERROR) << error_msg;
482 return kOatDexOutOfDate;
483 }
484 } else {
485 if (!DexChecksumUpToDate(file, &error_msg)) {
486 LOG(ERROR) << error_msg;
487 return kOatDexOutOfDate;
488 }
489 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800490
Andreas Gampe29d38e72016-03-23 15:31:51 +0000491 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000492
Richard Uhler66d874d2015-01-15 09:37:19 -0800493 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
495 const ImageInfo* image_info = GetImageInfo();
496 if (image_info == nullptr) {
497 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000498
Richard Uhler76f5cb62016-04-04 13:30:16 -0700499 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000500 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700501 }
502
503 // If there is no original dex file to fall back to, grudgingly accept
504 // the oat file. This could technically lead to crashes, but there's no
505 // way we could find a better oat file to use for this dex location,
506 // and it's better than being stuck in a boot loop with no way out.
507 // The problem will hopefully resolve itself the next time the runtime
508 // starts up.
509 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
510 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000511 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000512 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000513 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000514 }
515 } else {
516 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800517 }
518
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100519 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000520 if (!file.IsPic()) {
521 const ImageInfo* image_info = GetImageInfo();
522 if (image_info == nullptr) {
523 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000524 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000525 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700526
Andreas Gampe29d38e72016-03-23 15:31:51 +0000527 // Verify the oat_data_begin recorded for the image in the oat file matches
528 // the actual oat_data_begin for boot.oat in the image.
529 const OatHeader& oat_header = file.GetOatHeader();
530 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
531 if (oat_data_begin != image_info->oat_data_begin) {
532 VLOG(oat) << file.GetLocation() <<
533 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
534 << " does not match actual image oat_data_begin ("
535 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000536 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000537 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700538
Andreas Gampe29d38e72016-03-23 15:31:51 +0000539 // Verify the oat_patch_delta recorded for the image in the oat file matches
540 // the actual oat_patch_delta for the image.
541 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
542 if (oat_patch_delta != image_info->patch_delta) {
543 VLOG(oat) << file.GetLocation() <<
544 ": Oat file image patch delta (" << oat_patch_delta << ")"
545 << " does not match actual image patch delta ("
546 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000547 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000548 }
549 } else {
550 // Oat files compiled in PIC mode do not require relocation.
551 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
552 }
553 } else {
554 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800555 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700556 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800557}
558
Calin Juravle357c66d2017-05-04 01:57:17 +0000559static bool DexLocationToOdexNames(const std::string& location,
560 InstructionSet isa,
561 std::string* odex_filename,
562 std::string* oat_dir,
563 std::string* isa_dir,
564 std::string* error_msg) {
565 CHECK(odex_filename != nullptr);
566 CHECK(error_msg != nullptr);
567
568 // The odex file name is formed by replacing the dex_location extension with
569 // .odex and inserting an oat/<isa> directory. For example:
570 // location = /foo/bar/baz.jar
571 // odex_location = /foo/bar/oat/<isa>/baz.odex
572
573 // Find the directory portion of the dex location and add the oat/<isa>
574 // directory.
575 size_t pos = location.rfind('/');
576 if (pos == std::string::npos) {
577 *error_msg = "Dex location " + location + " has no directory.";
578 return false;
579 }
580 std::string dir = location.substr(0, pos+1);
581 // Add the oat directory.
582 dir += "oat";
583 if (oat_dir != nullptr) {
584 *oat_dir = dir;
585 }
586 // Add the isa directory
587 dir += "/" + std::string(GetInstructionSetString(isa));
588 if (isa_dir != nullptr) {
589 *isa_dir = dir;
590 }
591
592 // Get the base part of the file without the extension.
593 std::string file = location.substr(pos+1);
594 pos = file.rfind('.');
595 if (pos == std::string::npos) {
596 *error_msg = "Dex location " + location + " has no extension.";
597 return false;
598 }
599 std::string base = file.substr(0, pos);
600
601 *odex_filename = dir + "/" + base + ".odex";
602 return true;
603}
604
605// Prepare a subcomponent of the odex directory.
606// (i.e. create and set the expected permissions on the path `dir`).
607static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
608 struct stat dir_stat;
609 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
610 // The directory exists. Check if it is indeed a directory.
611 if (!S_ISDIR(dir_stat.st_mode)) {
612 *error_msg = dir + " is not a dir";
613 return false;
614 } else {
615 // The dir is already on disk.
616 return true;
617 }
618 }
619
620 // Failed to stat. We need to create the directory.
621 if (errno != ENOENT) {
622 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
623 return false;
624 }
625
626 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
627 if (mkdir(dir.c_str(), mode) != 0) {
628 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
629 return false;
630 }
631 if (chmod(dir.c_str(), mode) != 0) {
632 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
633 return false;
634 }
635 return true;
636}
637
638// Prepares the odex directory for the given dex location.
639static bool PrepareOdexDirectories(const std::string& dex_location,
640 const std::string& expected_odex_location,
641 InstructionSet isa,
642 std::string* error_msg) {
643 std::string actual_odex_location;
644 std::string oat_dir;
645 std::string isa_dir;
646 if (!DexLocationToOdexNames(
647 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
648 return false;
649 }
650 DCHECK_EQ(expected_odex_location, actual_odex_location);
651
652 if (!PrepareDirectory(oat_dir, error_msg)) {
653 return false;
654 }
655 if (!PrepareDirectory(isa_dir, error_msg)) {
656 return false;
657 }
658 return true;
659}
660
Calin Juravled4215bb2017-09-20 11:54:21 -0700661class Dex2oatFileWrapper {
662 public:
663 explicit Dex2oatFileWrapper(File* file)
664 : file_(file),
665 unlink_file_at_destruction_(true) {
666 }
667
668 ~Dex2oatFileWrapper() {
669 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
670 file_->Erase(/*unlink*/ true);
671 }
672 }
673
674 File* GetFile() { return file_.get(); }
675
676 void DisableUnlinkAtDestruction() {
677 unlink_file_at_destruction_ = false;
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800678 }
Calin Juravled4215bb2017-09-20 11:54:21 -0700679
680 private:
681 std::unique_ptr<File> file_;
682 bool unlink_file_at_destruction_;
683};
684
Calin Juravle357c66d2017-05-04 01:57:17 +0000685OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700686 OatFileAssistant::OatFileInfo& info,
687 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700688 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700689 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800690 CHECK(error_msg != nullptr);
691
Richard Uhler8327cf72015-10-13 16:34:59 -0700692 Runtime* runtime = Runtime::Current();
693 if (!runtime->IsDex2OatEnabled()) {
694 *error_msg = "Generation of oat file for dex location " + dex_location_
695 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700696 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700697 }
698
Calin Juravle357c66d2017-05-04 01:57:17 +0000699 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700700 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800701 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700702 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800703 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000704 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700705 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800706
Richard Uhler66d874d2015-01-15 09:37:19 -0800707 // dex2oat ignores missing dex files and doesn't report an error.
708 // Check explicitly here so we can detect the error properly.
709 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000710 struct stat dex_path_stat;
711 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
712 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700713 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800714 }
715
Calin Juravle357c66d2017-05-04 01:57:17 +0000716 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
717 if (!info.IsOatLocation()) {
718 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
719 return kUpdateNotAttempted;
720 }
721 }
722
723 // Set the permissions for the oat and the vdex files.
724 // The user always gets read and write while the group and others propagate
725 // the reading access of the original dex file.
726 mode_t file_mode = S_IRUSR | S_IWUSR |
727 (dex_path_stat.st_mode & S_IRGRP) |
728 (dex_path_stat.st_mode & S_IROTH);
729
Calin Juravled4215bb2017-09-20 11:54:21 -0700730 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
731 File* vdex_file = vdex_file_wrapper.GetFile();
732 if (vdex_file == nullptr) {
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 opened.";
736 return kUpdateNotAttempted;
737 }
738
Calin Juravle357c66d2017-05-04 01:57:17 +0000739 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100740 *error_msg = "Generation of oat file " + oat_file_name
741 + " not attempted because the vdex file " + vdex_file_name
742 + " could not be made world readable.";
743 return kUpdateNotAttempted;
744 }
745
Calin Juravled4215bb2017-09-20 11:54:21 -0700746 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
747 File* oat_file = oat_file_wrapper.GetFile();
748 if (oat_file == nullptr) {
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 created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700751 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700752 }
753
Calin Juravle357c66d2017-05-04 01:57:17 +0000754 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700755 *error_msg = "Generation of oat file " + oat_file_name
756 + " not attempted because the oat file could not be made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700757 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700758 }
759
760 std::vector<std::string> args;
761 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000762 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700763 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
764 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000765 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700766 const std::string dex2oat_context = class_loader_context == nullptr
767 ? OatFile::kSpecialSharedLibrary
768 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
769 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700770
Richard Uhler66d874d2015-01-15 09:37:19 -0800771 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700772 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700773 }
774
David Brazdil7b49e6c2016-09-01 11:06:18 +0100775 if (vdex_file->FlushCloseOrErase() != 0) {
776 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100777 return kUpdateFailed;
778 }
779
Richard Uhler8327cf72015-10-13 16:34:59 -0700780 if (oat_file->FlushCloseOrErase() != 0) {
781 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700782 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800783 }
784
Calin Juravle357c66d2017-05-04 01:57:17 +0000785 // Mark that the odex file has changed and we should try to reload.
786 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700787 // We have compiled successfully. Disable the auto-unlink.
788 vdex_file_wrapper.DisableUnlinkAtDestruction();
789 oat_file_wrapper.DisableUnlinkAtDestruction();
790
Richard Uhler1e860612016-03-30 12:17:55 -0700791 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800792}
793
794bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
795 std::string* error_msg) {
796 Runtime* runtime = Runtime::Current();
797 std::string image_location = ImageLocation();
798 if (image_location.empty()) {
799 *error_msg = "No image location found for Dex2Oat.";
800 return false;
801 }
802
803 std::vector<std::string> argv;
804 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000805 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200806 argv.push_back("--debuggable");
807 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700808 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800809
810 if (!runtime->IsVerificationEnabled()) {
811 argv.push_back("--compiler-filter=verify-none");
812 }
813
814 if (runtime->MustRelocateIfPossible()) {
815 argv.push_back("--runtime-arg");
816 argv.push_back("-Xrelocate");
817 } else {
818 argv.push_back("--runtime-arg");
819 argv.push_back("-Xnorelocate");
820 }
821
822 if (!kIsTargetBuild) {
823 argv.push_back("--host");
824 }
825
826 argv.push_back("--boot-image=" + image_location);
827
828 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
829 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
830
831 argv.insert(argv.end(), args.begin(), args.end());
832
Andreas Gampe9186ced2016-12-12 14:28:21 -0800833 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800834 return Exec(argv, error_msg);
835}
836
Richard Uhlerb81881d2016-04-19 13:08:04 -0700837bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
838 InstructionSet isa,
839 std::string* odex_filename,
840 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000841 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800842}
843
Richard Uhlerb81881d2016-04-19 13:08:04 -0700844bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
845 InstructionSet isa,
846 std::string* oat_filename,
847 std::string* error_msg) {
848 CHECK(oat_filename != nullptr);
849 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800850
Richard Uhler55b58b62016-08-12 09:05:13 -0700851 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
852 if (cache_dir.empty()) {
853 *error_msg = "Dalvik cache directory does not exist";
854 return false;
855 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700856
857 // TODO: The oat file assistant should be the definitive place for
858 // determining the oat file name from the dex location, not
859 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700860 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800861}
862
Richard Uhler66d874d2015-01-15 09:37:19 -0800863std::string OatFileAssistant::ImageLocation() {
864 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000865 const std::vector<gc::space::ImageSpace*>& image_spaces =
866 runtime->GetHeap()->GetBootImageSpaces();
867 if (image_spaces.empty()) {
868 return "";
869 }
870 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800871}
872
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000873const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
874 if (!required_dex_checksums_attempted_) {
875 required_dex_checksums_attempted_ = true;
876 required_dex_checksums_found_ = false;
877 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800878 std::string error_msg;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700879 if (DexFileLoader::GetMultiDexChecksums(dex_location_.c_str(),
880 &cached_required_dex_checksums_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700881 &error_msg,
882 zip_fd_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000883 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700884 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800885 } else {
886 // This can happen if the original dex file has been stripped from the
887 // apk.
888 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700889 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800890
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000891 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700892 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800893 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000894 required_dex_checksums_found_ = true;
895 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700896 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000897 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
898 if (odex_dex_file == nullptr) {
899 required_dex_checksums_found_ = false;
900 break;
901 }
902 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800903 }
904 }
905 }
906 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000907 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800908}
909
Richard Uhler95e09672017-02-22 11:37:41 +0000910std::unique_ptr<OatFileAssistant::ImageInfo>
911OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
912 CHECK(error_msg != nullptr);
913
Richard Uhler95e09672017-02-22 11:37:41 +0000914 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000915 std::unique_ptr<ImageInfo> info(new ImageInfo());
916 info->location = runtime->GetImageLocation();
917
918 std::unique_ptr<ImageHeader> image_header(
919 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
920 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000921 return nullptr;
922 }
923
Richard Uhler8f104862017-02-22 12:34:01 +0000924 info->oat_checksum = image_header->GetOatChecksum();
925 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
926 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000927 return info;
928}
929
Richard Uhler66d874d2015-01-15 09:37:19 -0800930const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
931 if (!image_info_load_attempted_) {
932 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000933 std::string error_msg;
934 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
935 if (cached_image_info_ == nullptr) {
936 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800937 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800938 }
Richard Uhler95e09672017-02-22 11:37:41 +0000939 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800940}
941
Richard Uhler88bc6732016-11-14 14:38:03 +0000942OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000943 // TODO(calin): Document the side effects of class loading when
944 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700945 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000946 // If the parent of the dex file is writable it means that we can
947 // create the odex file. In this case we unconditionally pick the odex
948 // as the best oat file. This corresponds to the regular use case when
949 // apps gets installed or when they load private, secondary dex file.
950 // For apps on the system partition the odex location will not be
951 // writable and thus the oat location might be more up to date.
952 return odex_;
953 }
954
955 // We cannot write to the odex location. This must be a system app.
956
957 // If the oat location is usable take it.
958 if (oat_.IsUseable()) {
959 return oat_;
960 }
961
962 // The oat file is not usable but the odex file might be up to date.
963 // This is an indication that we are dealing with an up to date prebuilt
964 // (that doesn't need relocation).
965 if (odex_.Status() == kOatUpToDate) {
966 return odex_;
967 }
968
969 // The oat file is not usable and the odex file is not up to date.
970 // However we have access to the original dex file which means we can make
971 // the oat location up to date.
972 if (HasOriginalDexFiles()) {
973 return oat_;
974 }
975
976 // We got into the worst situation here:
977 // - the oat location is not usable
978 // - the prebuild odex location is not up to date
979 // - and we don't have the original dex file anymore (stripped).
980 // Pick the odex if it exists, or the oat if not.
981 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000982}
983
Andreas Gampea463b6a2016-08-12 21:53:32 -0700984std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800985 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100986 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800987 if (art_file.empty()) {
988 return nullptr;
989 }
990 std::string error_msg;
991 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700992 std::unique_ptr<gc::space::ImageSpace> ret =
993 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800994 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800995 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
996 }
997 return ret;
998}
999
Richard Uhler88bc6732016-11-14 14:38:03 +00001000OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
1001 bool is_oat_location)
1002 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -07001003{}
1004
Richard Uhler88bc6732016-11-14 14:38:03 +00001005bool OatFileAssistant::OatFileInfo::IsOatLocation() {
1006 return is_oat_location_;
1007}
1008
Richard Uhler743bf362016-04-19 15:39:37 -07001009const std::string* OatFileAssistant::OatFileInfo::Filename() {
1010 return filename_provided_ ? &filename_ : nullptr;
1011}
1012
Richard Uhler03bc6592016-11-22 09:42:04 +00001013bool OatFileAssistant::OatFileInfo::IsUseable() {
1014 switch (Status()) {
1015 case kOatCannotOpen:
1016 case kOatDexOutOfDate:
1017 case kOatBootImageOutOfDate: return false;
1018
1019 case kOatRelocationOutOfDate:
1020 case kOatUpToDate: return true;
1021 }
1022 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001023}
1024
1025OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1026 if (!status_attempted_) {
1027 status_attempted_ = true;
1028 const OatFile* file = GetFile();
1029 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001030 // Check to see if there is a vdex file we can make use of.
1031 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001032 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001033 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001034 if (use_fd_) {
1035 if (vdex_fd_ >= 0) {
1036 struct stat s;
1037 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1038 if (rc == -1) {
1039 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1040 } else {
1041 vdex = VdexFile::Open(vdex_fd_,
1042 s.st_size,
1043 vdex_filename,
1044 false /*writable*/,
1045 false /*low_4gb*/,
1046 false /* unquicken */,
1047 &error_msg);
1048 }
1049 }
1050 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001051 vdex = VdexFile::Open(vdex_filename,
1052 false /*writeable*/,
1053 false /*low_4gb*/,
1054 false /*unquicken*/,
1055 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001056 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001057 if (vdex == nullptr) {
1058 status_ = kOatCannotOpen;
1059 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1060 } else {
1061 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1062 // The vdex file does not contain enough information to determine
1063 // whether it is up to date with respect to the boot image, so we
1064 // assume it is out of date.
1065 VLOG(oat) << error_msg;
1066 status_ = kOatBootImageOutOfDate;
1067 } else {
1068 status_ = kOatDexOutOfDate;
1069 }
1070 }
Richard Uhler743bf362016-04-19 15:39:37 -07001071 } else {
1072 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001073 VLOG(oat) << file->GetLocation() << " is " << status_
1074 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001075 }
1076 }
1077 return status_;
1078}
1079
Richard Uhler70a84262016-11-08 16:51:51 +00001080OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001081 CompilerFilter::Filter target,
1082 bool profile_changed,
1083 bool downgrade,
1084 ClassLoaderContext* context) {
1085
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001086 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001087 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001088 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001089
Calin Juravle44e5efa2017-09-12 00:54:26 -07001090 // Only check the filter and relocation if the class loader context is ok.
1091 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1092 if (class_loader_context_okay) {
1093 if (filter_okay && Status() == kOatUpToDate) {
1094 // The oat file is in good shape as is.
1095 return kNoDexOptNeeded;
1096 }
Richard Uhler70a84262016-11-08 16:51:51 +00001097
Calin Juravle44e5efa2017-09-12 00:54:26 -07001098 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1099 // If no compilation is desired, then it doesn't matter if the oat
1100 // file needs relocation. It's in good shape as is.
1101 return kNoDexOptNeeded;
1102 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001103
Calin Juravle44e5efa2017-09-12 00:54:26 -07001104 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1105 return kDex2OatForRelocation;
1106 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001107
Calin Juravle44e5efa2017-09-12 00:54:26 -07001108 if (IsUseable()) {
1109 return kDex2OatForFilter;
1110 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001111
Calin Juravle44e5efa2017-09-12 00:54:26 -07001112 if (Status() == kOatBootImageOutOfDate) {
1113 return kDex2OatForBootImage;
1114 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001115 }
1116
1117 if (oat_file_assistant_->HasOriginalDexFiles()) {
1118 return kDex2OatFromScratch;
1119 } else {
1120 // Otherwise there is nothing we can do, even if we want to.
1121 return kNoDexOptNeeded;
1122 }
Richard Uhler70a84262016-11-08 16:51:51 +00001123}
1124
Richard Uhler743bf362016-04-19 15:39:37 -07001125const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1126 CHECK(!file_released_) << "GetFile called after oat file released.";
1127 if (!load_attempted_) {
1128 load_attempted_ = true;
1129 if (filename_provided_) {
1130 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001131 if (use_fd_) {
1132 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1133 file_.reset(OatFile::Open(vdex_fd_,
1134 oat_fd_,
1135 filename_.c_str(),
1136 nullptr,
1137 nullptr,
1138 oat_file_assistant_->load_executable_,
1139 false /* low_4gb */,
1140 oat_file_assistant_->dex_location_.c_str(),
1141 &error_msg));
1142 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001143 } else {
1144 file_.reset(OatFile::Open(filename_.c_str(),
1145 filename_.c_str(),
1146 nullptr,
1147 nullptr,
1148 oat_file_assistant_->load_executable_,
1149 false /* low_4gb */,
1150 oat_file_assistant_->dex_location_.c_str(),
1151 &error_msg));
1152 }
Richard Uhler743bf362016-04-19 15:39:37 -07001153 if (file_.get() == nullptr) {
1154 VLOG(oat) << "OatFileAssistant test for existing oat file "
1155 << filename_ << ": " << error_msg;
1156 }
1157 }
1158 }
1159 return file_.get();
1160}
1161
1162bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001163 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001164 const OatFile* file = GetFile();
1165 if (file == nullptr) {
1166 return false;
1167 }
1168
1169 CompilerFilter::Filter current = file->GetCompilerFilter();
1170 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1171 VLOG(oat) << "Compiler filter not okay because Profile changed";
1172 return false;
1173 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001174 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1175 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001176}
1177
Calin Juravle44e5efa2017-09-12 00:54:26 -07001178bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1179 if (context == nullptr) {
1180 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1181 return true;
1182 }
1183
1184 const OatFile* file = GetFile();
1185 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001186 // No oat file means we have nothing to verify.
1187 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001188 }
1189
Calin Juravle20c46442017-09-12 00:54:26 -07001190 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001191 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001192 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001193 : "";
1194
1195 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1196 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1197 return false;
1198 }
1199
1200 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext());
1201 if (!result) {
1202 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1203 << file->GetClassLoaderContext()
1204 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1205 }
1206 return result;
1207}
1208
Richard Uhler743bf362016-04-19 15:39:37 -07001209bool OatFileAssistant::OatFileInfo::IsExecutable() {
1210 const OatFile* file = GetFile();
1211 return (file != nullptr && file->IsExecutable());
1212}
1213
Richard Uhler743bf362016-04-19 15:39:37 -07001214void OatFileAssistant::OatFileInfo::Reset() {
1215 load_attempted_ = false;
1216 file_.reset();
1217 status_attempted_ = false;
1218}
1219
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001220void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, bool use_fd, int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001221 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001222 filename_provided_ = true;
1223 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001224 use_fd_ = use_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001225 vdex_fd_ = vdex_fd;
1226 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001227 Reset();
1228}
1229
1230std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1231 file_released_ = true;
1232 return std::move(file_);
1233}
1234
Richard Uhler70a84262016-11-08 16:51:51 +00001235std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001236 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001237 return ReleaseFile();
1238 }
1239
1240 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1241 << " attempting to fall back to interpreting oat file instead.";
1242
Richard Uhler03bc6592016-11-22 09:42:04 +00001243 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001244 return ReleaseFile();
1245 }
1246
Richard Uhler03bc6592016-11-22 09:42:04 +00001247 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001248 // We are loading an oat file for runtime use that needs relocation.
1249 // Reload the file non-executable to ensure that we interpret out of the
1250 // dex code in the oat file rather than trying to execute the unrelocated
1251 // compiled code.
1252 oat_file_assistant_->load_executable_ = false;
1253 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001254 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001255 CHECK(!IsExecutable());
1256 return ReleaseFile();
1257 }
1258 }
1259 return std::unique_ptr<OatFile>();
1260}
Richard Uhler66d874d2015-01-15 09:37:19 -08001261} // namespace art