blob: 97b2aecd82b7975688022b8975911eb3abfa09d5 [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 Juravle357c66d2017-05-04 01:57:17 +0000102 // Try to get the realpath for the dex location.
103 //
104 // This is OK with respect to dalvik cache naming scheme because we never
105 // generate oat files starting from symlinks which go into dalvik cache.
106 // (recall that the oat files in dalvik cache are encoded by replacing '/'
107 // with '@' in the path).
108 // The boot image oat files (which are symlinked in dalvik-cache) are not
109 // loaded via the oat file assistant.
110 //
111 // The only case when the dex location may resolve to a different path
112 // is for secondary dex files (e.g. /data/user/0 symlinks to /data/data and
113 // the app is free to create its own internal layout). Related to this it is
114 // worthwhile to mention that installd resolves the secondary dex location
115 // before calling dex2oat.
116 UniqueCPtr<const char[]> dex_location_real(realpath(dex_location, nullptr));
117 if (dex_location_real != nullptr) {
118 dex_location_.assign(dex_location_real.get());
119 } else {
120 // If we can't get the realpath of the location there's not much point in trying to move on.
121 PLOG(ERROR) << "Could not get the realpath of dex_location " << dex_location;
122 return;
123 }
Richard Uhler740eec92015-10-15 15:12:23 -0700124
Richard Uhler66d874d2015-01-15 09:37:19 -0800125 if (load_executable_ && isa != kRuntimeISA) {
126 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
127 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
128 load_executable_ = false;
129 }
130
Richard Uhler743bf362016-04-19 15:39:37 -0700131 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700132 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700133 std::string odex_file_name;
134 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700135 odex_.Reset(odex_file_name, UseFdToReadFiles(), vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700136 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700137 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
138 }
139
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700140 if (!UseFdToReadFiles()) {
141 // Get the oat filename.
142 std::string oat_file_name;
143 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
144 oat_.Reset(oat_file_name, false /* use_fd */);
145 } else {
146 LOG(WARNING) << "Failed to determine oat file name for dex location "
147 << dex_location_ << ": " << error_msg;
148 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000149 }
150
151 // Check if the dex directory is writable.
152 // This will be needed in most uses of OatFileAssistant and so it's OK to
153 // compute it eagerly. (the only use which will not make use of it is
154 // OatFileAssistant::GetStatusDump())
155 size_t pos = dex_location_.rfind('/');
156 if (pos == std::string::npos) {
157 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700158 } else if (!UseFdToReadFiles()) {
159 // We cannot test for parent access when using file descriptors. That's ok
160 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000161 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700162 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000163 dex_parent_writable_ = true;
164 } else {
165 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700166 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800167 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800168}
169
170OatFileAssistant::~OatFileAssistant() {
171 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100172 if (flock_.get() != nullptr) {
173 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800174 }
175}
176
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700177bool OatFileAssistant::UseFdToReadFiles() {
178 return zip_fd_ >= 0;
179}
180
Richard Uhler66d874d2015-01-15 09:37:19 -0800181bool OatFileAssistant::IsInBootClassPath() {
182 // Note: We check the current boot class path, regardless of the ISA
183 // specified by the user. This is okay, because the boot class path should
184 // be the same for all ISAs.
185 // TODO: Can we verify the boot class path is the same for all ISAs?
186 Runtime* runtime = Runtime::Current();
187 ClassLinker* class_linker = runtime->GetClassLinker();
188 const auto& boot_class_path = class_linker->GetBootClassPath();
189 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700190 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800191 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
192 return true;
193 }
194 }
195 return false;
196}
197
198bool OatFileAssistant::Lock(std::string* error_msg) {
199 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100200 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800201
Calin Juravle357c66d2017-05-04 01:57:17 +0000202 // Note the lock will only succeed for secondary dex files and in test
203 // environment.
204 //
205 // The lock *will fail* for all primary apks in a production environment.
206 // The app does not have permissions to create locks next to its dex location
207 // (be it system, data or vendor parition). We also cannot use the odex or
208 // oat location for the same reasoning.
209 //
210 // This is best effort and if it fails it's unlikely that we will be able
211 // to generate oat files anyway.
212 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800213
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100214 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
215 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100216 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800217 return false;
218 }
219 return true;
220}
221
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700222int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
223 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700224 bool downgrade,
225 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000226 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700227 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
228 profile_changed,
229 downgrade,
230 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000231 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
232 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800233 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000234 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800235}
236
Richard Uhlerf4b34872016-04-13 11:03:46 -0700237// Figure out the currently specified compile filter option in the runtime.
238// Returns true on success, false if the compiler filter is invalid, in which
239// case error_msg describes the problem.
240static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
241 std::string* error_msg) {
242 CHECK(filter != nullptr);
243 CHECK(error_msg != nullptr);
244
Calin Juravle357c66d2017-05-04 01:57:17 +0000245 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700246 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
247 if (option.starts_with("--compiler-filter=")) {
248 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
249 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
250 *error_msg = std::string("Unknown --compiler-filter value: ")
251 + std::string(compiler_filter_string);
252 return false;
253 }
254 }
255 }
256 return true;
257}
258
Richard Uhler01be6812016-05-17 10:34:52 -0700259bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000260 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700261}
262
Richard Uhler1e860612016-03-30 12:17:55 -0700263OatFileAssistant::ResultOfAttemptToUpdate
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700264OatFileAssistant::MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700265 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700266 std::string* error_msg) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700267 // The method doesn't use zip_fd_ and directly opens dex files at dex_locations_.
268 CHECK_EQ(-1, zip_fd_) << "MakeUpToDate should not be called with zip_fd";
269
Richard Uhlerf4b34872016-04-13 11:03:46 -0700270 CompilerFilter::Filter target;
271 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
272 return kUpdateNotAttempted;
273 }
274
Richard Uhler88bc6732016-11-14 14:38:03 +0000275 OatFileInfo& info = GetBestInfo();
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700276 // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291.
277 // This is actually not trivial in the current logic as it will interact with the collision
278 // check:
279 // - currently, if the context does not match but we have no collisions we still accept the
280 // oat file.
281 // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make
282 // the oat code up to date the collision check becomes useless.
283 // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files
284 // loaded in other processes). So it boils down to how far do we want to complicate
285 // 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 -0700286 switch (info.GetDexOptNeeded(
287 target, profile_changed, /*downgrade*/ false, class_loader_context)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000288 case kNoDexOptNeeded:
289 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000290
Richard Uhler7225a8d2016-11-22 10:12:03 +0000291 // TODO: For now, don't bother with all the different ways we can call
292 // dex2oat to generate the oat file. Always generate the oat file as if it
293 // were kDex2OatFromScratch.
294 case kDex2OatFromScratch:
295 case kDex2OatForBootImage:
296 case kDex2OatForRelocation:
297 case kDex2OatForFilter:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700298 return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800299 }
300 UNREACHABLE();
301}
302
303std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000304 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800305}
306
Richard Uhler46cc64f2016-11-14 14:53:55 +0000307std::string OatFileAssistant::GetStatusDump() {
308 std::ostringstream status;
309 bool oat_file_exists = false;
310 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000311 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000312 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000313 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000314
Richard Uhler46cc64f2016-11-14 14:53:55 +0000315 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000316 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
317 const OatFile* file = oat_.GetFile();
318 if (file == nullptr) {
319 // If the file is null even though the status is not kOatCannotOpen, it
320 // means we must have a vdex file with no corresponding oat file. In
321 // this case we cannot determine the compilation filter. Indicate that
322 // we have only the vdex file instead.
323 status << "vdex-only";
324 } else {
325 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
326 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000327 }
328
Richard Uhler03bc6592016-11-22 09:42:04 +0000329 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000330 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000331 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000332
Richard Uhler46cc64f2016-11-14 14:53:55 +0000333 odex_file_exists = true;
334 if (oat_file_exists) {
335 status << "] ";
336 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000337 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
338 const OatFile* file = odex_.GetFile();
339 if (file == nullptr) {
340 status << "vdex-only";
341 } else {
342 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
343 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000344 }
345
346 if (!oat_file_exists && !odex_file_exists) {
347 status << "invalid[";
348 }
349
350 status << "]";
351 return status.str();
352}
353
Richard Uhler66d874d2015-01-15 09:37:19 -0800354std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700355 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800356 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700357 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
358 return dex_files;
359 } else {
360 return std::vector<std::unique_ptr<const DexFile>>();
361 }
362}
Richard Uhler66d874d2015-01-15 09:37:19 -0800363
Calin Juravle87e2cb62017-06-13 21:48:45 -0700364bool OatFileAssistant::LoadDexFiles(
365 const OatFile &oat_file,
366 const std::string& dex_location,
367 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000368 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 std::string error_msg;
370 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700371 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700373 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700374 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800375 }
376
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700377 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800378 if (dex_file.get() == nullptr) {
379 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700380 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700382 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800383
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000384 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700385 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700386 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000387 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700388 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000389 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800390 break;
391 }
392
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700393 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800394 if (dex_file.get() == nullptr) {
395 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700396 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800397 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700398 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800399 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700400 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800401}
402
Richard Uhler9b994ea2015-06-24 08:44:19 -0700403bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000404 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700405 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000406 // GetRequiredDexChecksums.
407 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700408 return has_original_dex_files_;
409}
410
Richard Uhler95abd042015-03-24 09:51:28 -0700411OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700412 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800413}
414
Richard Uhler95abd042015-03-24 09:51:28 -0700415OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700416 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800417}
418
Richard Uhler2f27abd2017-01-31 14:02:34 +0000419bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000420 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
421 if (required_dex_checksums == nullptr) {
422 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
423 return true;
424 }
425
426 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
427 if (required_dex_checksums->size() != number_of_dex_files) {
428 *error_msg = StringPrintf("expected %zu dex files but found %u",
429 required_dex_checksums->size(),
430 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000431 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800432 }
433
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000434 for (uint32_t i = 0; i < number_of_dex_files; i++) {
435 uint32_t expected_checksum = (*required_dex_checksums)[i];
436 uint32_t actual_checksum = file.GetLocationChecksum(i);
437 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700438 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000439 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
440 "Expected: %u, actual: %u",
441 dex.c_str(),
442 expected_checksum,
443 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000444 return false;
445 }
446 }
447
Richard Uhler2f27abd2017-01-31 14:02:34 +0000448 return true;
449}
450
451bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000452 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
453 if (required_dex_checksums == nullptr) {
454 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
455 return true;
456 }
457
458 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
459 if (required_dex_checksums->size() != number_of_dex_files) {
460 *error_msg = StringPrintf("expected %zu dex files but found %u",
461 required_dex_checksums->size(),
462 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000463 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800464 }
465
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000466 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700467 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000468 uint32_t expected_checksum = (*required_dex_checksums)[i];
469 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
470 if (oat_dex_file == nullptr) {
471 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
472 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800473 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000474 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
475 if (expected_checksum != actual_checksum) {
476 VLOG(oat) << "Dex checksum does not match for dex: " << dex
477 << ". Expected: " << expected_checksum
478 << ", Actual: " << actual_checksum;
479 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800480 }
481 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000482 return true;
483}
484
485OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
486 // Verify the ART_USE_READ_BARRIER state.
487 // TODO: Don't fully reject files due to read barrier state. If they contain
488 // compiled code and are otherwise okay, we should return something like
489 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
490 // barrier state doesn't matter.
491 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
492 constexpr bool kRuntimeIsCC = kUseReadBarrier;
493 if (is_cc != kRuntimeIsCC) {
494 return kOatCannotOpen;
495 }
496
497 // Verify the dex checksum.
498 std::string error_msg;
499 if (kIsVdexEnabled) {
500 VdexFile* vdex = file.GetVdexFile();
501 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
502 LOG(ERROR) << error_msg;
503 return kOatDexOutOfDate;
504 }
505 } else {
506 if (!DexChecksumUpToDate(file, &error_msg)) {
507 LOG(ERROR) << error_msg;
508 return kOatDexOutOfDate;
509 }
510 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800511
Andreas Gampe29d38e72016-03-23 15:31:51 +0000512 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000513
Richard Uhler66d874d2015-01-15 09:37:19 -0800514 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000515 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
516 const ImageInfo* image_info = GetImageInfo();
517 if (image_info == nullptr) {
518 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000519
Richard Uhler76f5cb62016-04-04 13:30:16 -0700520 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000521 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700522 }
523
524 // If there is no original dex file to fall back to, grudgingly accept
525 // the oat file. This could technically lead to crashes, but there's no
526 // way we could find a better oat file to use for this dex location,
527 // and it's better than being stuck in a boot loop with no way out.
528 // The problem will hopefully resolve itself the next time the runtime
529 // starts up.
530 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
531 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000532 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000533 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000534 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000535 }
536 } else {
537 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800538 }
539
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100540 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000541 if (!file.IsPic()) {
542 const ImageInfo* image_info = GetImageInfo();
543 if (image_info == nullptr) {
544 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000545 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000546 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700547
Andreas Gampe29d38e72016-03-23 15:31:51 +0000548 // Verify the oat_data_begin recorded for the image in the oat file matches
549 // the actual oat_data_begin for boot.oat in the image.
550 const OatHeader& oat_header = file.GetOatHeader();
551 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
552 if (oat_data_begin != image_info->oat_data_begin) {
553 VLOG(oat) << file.GetLocation() <<
554 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
555 << " does not match actual image oat_data_begin ("
556 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000557 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000558 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700559
Andreas Gampe29d38e72016-03-23 15:31:51 +0000560 // Verify the oat_patch_delta recorded for the image in the oat file matches
561 // the actual oat_patch_delta for the image.
562 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
563 if (oat_patch_delta != image_info->patch_delta) {
564 VLOG(oat) << file.GetLocation() <<
565 ": Oat file image patch delta (" << oat_patch_delta << ")"
566 << " does not match actual image patch delta ("
567 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000568 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000569 }
570 } else {
571 // Oat files compiled in PIC mode do not require relocation.
572 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
573 }
574 } else {
575 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800576 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700577 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800578}
579
Calin Juravle357c66d2017-05-04 01:57:17 +0000580static bool DexLocationToOdexNames(const std::string& location,
581 InstructionSet isa,
582 std::string* odex_filename,
583 std::string* oat_dir,
584 std::string* isa_dir,
585 std::string* error_msg) {
586 CHECK(odex_filename != nullptr);
587 CHECK(error_msg != nullptr);
588
589 // The odex file name is formed by replacing the dex_location extension with
590 // .odex and inserting an oat/<isa> directory. For example:
591 // location = /foo/bar/baz.jar
592 // odex_location = /foo/bar/oat/<isa>/baz.odex
593
594 // Find the directory portion of the dex location and add the oat/<isa>
595 // directory.
596 size_t pos = location.rfind('/');
597 if (pos == std::string::npos) {
598 *error_msg = "Dex location " + location + " has no directory.";
599 return false;
600 }
601 std::string dir = location.substr(0, pos+1);
602 // Add the oat directory.
603 dir += "oat";
604 if (oat_dir != nullptr) {
605 *oat_dir = dir;
606 }
607 // Add the isa directory
608 dir += "/" + std::string(GetInstructionSetString(isa));
609 if (isa_dir != nullptr) {
610 *isa_dir = dir;
611 }
612
613 // Get the base part of the file without the extension.
614 std::string file = location.substr(pos+1);
615 pos = file.rfind('.');
616 if (pos == std::string::npos) {
617 *error_msg = "Dex location " + location + " has no extension.";
618 return false;
619 }
620 std::string base = file.substr(0, pos);
621
622 *odex_filename = dir + "/" + base + ".odex";
623 return true;
624}
625
626// Prepare a subcomponent of the odex directory.
627// (i.e. create and set the expected permissions on the path `dir`).
628static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
629 struct stat dir_stat;
630 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
631 // The directory exists. Check if it is indeed a directory.
632 if (!S_ISDIR(dir_stat.st_mode)) {
633 *error_msg = dir + " is not a dir";
634 return false;
635 } else {
636 // The dir is already on disk.
637 return true;
638 }
639 }
640
641 // Failed to stat. We need to create the directory.
642 if (errno != ENOENT) {
643 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
644 return false;
645 }
646
647 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
648 if (mkdir(dir.c_str(), mode) != 0) {
649 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
650 return false;
651 }
652 if (chmod(dir.c_str(), mode) != 0) {
653 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
654 return false;
655 }
656 return true;
657}
658
659// Prepares the odex directory for the given dex location.
660static bool PrepareOdexDirectories(const std::string& dex_location,
661 const std::string& expected_odex_location,
662 InstructionSet isa,
663 std::string* error_msg) {
664 std::string actual_odex_location;
665 std::string oat_dir;
666 std::string isa_dir;
667 if (!DexLocationToOdexNames(
668 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
669 return false;
670 }
671 DCHECK_EQ(expected_odex_location, actual_odex_location);
672
673 if (!PrepareDirectory(oat_dir, error_msg)) {
674 return false;
675 }
676 if (!PrepareDirectory(isa_dir, error_msg)) {
677 return false;
678 }
679 return true;
680}
681
Calin Juravled4215bb2017-09-20 11:54:21 -0700682class Dex2oatFileWrapper {
683 public:
684 explicit Dex2oatFileWrapper(File* file)
685 : file_(file),
686 unlink_file_at_destruction_(true) {
687 }
688
689 ~Dex2oatFileWrapper() {
690 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
691 file_->Erase(/*unlink*/ true);
692 }
693 }
694
695 File* GetFile() { return file_.get(); }
696
697 void DisableUnlinkAtDestruction() {
698 unlink_file_at_destruction_ = false;
699 };
700
701 private:
702 std::unique_ptr<File> file_;
703 bool unlink_file_at_destruction_;
704};
705
Calin Juravle357c66d2017-05-04 01:57:17 +0000706OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700707 OatFileAssistant::OatFileInfo& info,
708 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700709 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700710 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800711 CHECK(error_msg != nullptr);
712
Richard Uhler8327cf72015-10-13 16:34:59 -0700713 Runtime* runtime = Runtime::Current();
714 if (!runtime->IsDex2OatEnabled()) {
715 *error_msg = "Generation of oat file for dex location " + dex_location_
716 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700717 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700718 }
719
Calin Juravle357c66d2017-05-04 01:57:17 +0000720 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700721 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800722 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700723 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800724 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000725 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700726 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800727
Richard Uhler66d874d2015-01-15 09:37:19 -0800728 // dex2oat ignores missing dex files and doesn't report an error.
729 // Check explicitly here so we can detect the error properly.
730 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000731 struct stat dex_path_stat;
732 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
733 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700734 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800735 }
736
Calin Juravle357c66d2017-05-04 01:57:17 +0000737 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
738 if (!info.IsOatLocation()) {
739 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
740 return kUpdateNotAttempted;
741 }
742 }
743
744 // Set the permissions for the oat and the vdex files.
745 // The user always gets read and write while the group and others propagate
746 // the reading access of the original dex file.
747 mode_t file_mode = S_IRUSR | S_IWUSR |
748 (dex_path_stat.st_mode & S_IRGRP) |
749 (dex_path_stat.st_mode & S_IROTH);
750
Calin Juravled4215bb2017-09-20 11:54:21 -0700751 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
752 File* vdex_file = vdex_file_wrapper.GetFile();
753 if (vdex_file == nullptr) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100754 *error_msg = "Generation of oat file " + oat_file_name
755 + " not attempted because the vdex file " + vdex_file_name
756 + " could not be opened.";
757 return kUpdateNotAttempted;
758 }
759
Calin Juravle357c66d2017-05-04 01:57:17 +0000760 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100761 *error_msg = "Generation of oat file " + oat_file_name
762 + " not attempted because the vdex file " + vdex_file_name
763 + " could not be made world readable.";
764 return kUpdateNotAttempted;
765 }
766
Calin Juravled4215bb2017-09-20 11:54:21 -0700767 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
768 File* oat_file = oat_file_wrapper.GetFile();
769 if (oat_file == nullptr) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700770 *error_msg = "Generation of oat file " + oat_file_name
771 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700772 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700773 }
774
Calin Juravle357c66d2017-05-04 01:57:17 +0000775 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700776 *error_msg = "Generation of oat file " + oat_file_name
777 + " not attempted because the oat file could not be made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700778 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700779 }
780
781 std::vector<std::string> args;
782 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000783 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700784 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
785 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000786 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700787 const std::string dex2oat_context = class_loader_context == nullptr
788 ? OatFile::kSpecialSharedLibrary
789 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
790 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700791
Richard Uhler66d874d2015-01-15 09:37:19 -0800792 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700793 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700794 }
795
David Brazdil7b49e6c2016-09-01 11:06:18 +0100796 if (vdex_file->FlushCloseOrErase() != 0) {
797 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100798 return kUpdateFailed;
799 }
800
Richard Uhler8327cf72015-10-13 16:34:59 -0700801 if (oat_file->FlushCloseOrErase() != 0) {
802 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700803 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800804 }
805
Calin Juravle357c66d2017-05-04 01:57:17 +0000806 // Mark that the odex file has changed and we should try to reload.
807 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700808 // We have compiled successfully. Disable the auto-unlink.
809 vdex_file_wrapper.DisableUnlinkAtDestruction();
810 oat_file_wrapper.DisableUnlinkAtDestruction();
811
Richard Uhler1e860612016-03-30 12:17:55 -0700812 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800813}
814
815bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
816 std::string* error_msg) {
817 Runtime* runtime = Runtime::Current();
818 std::string image_location = ImageLocation();
819 if (image_location.empty()) {
820 *error_msg = "No image location found for Dex2Oat.";
821 return false;
822 }
823
824 std::vector<std::string> argv;
825 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000826 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200827 argv.push_back("--debuggable");
828 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700829 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800830
831 if (!runtime->IsVerificationEnabled()) {
832 argv.push_back("--compiler-filter=verify-none");
833 }
834
835 if (runtime->MustRelocateIfPossible()) {
836 argv.push_back("--runtime-arg");
837 argv.push_back("-Xrelocate");
838 } else {
839 argv.push_back("--runtime-arg");
840 argv.push_back("-Xnorelocate");
841 }
842
843 if (!kIsTargetBuild) {
844 argv.push_back("--host");
845 }
846
847 argv.push_back("--boot-image=" + image_location);
848
849 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
850 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
851
852 argv.insert(argv.end(), args.begin(), args.end());
853
Andreas Gampe9186ced2016-12-12 14:28:21 -0800854 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800855 return Exec(argv, error_msg);
856}
857
Richard Uhlerb81881d2016-04-19 13:08:04 -0700858bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
859 InstructionSet isa,
860 std::string* odex_filename,
861 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000862 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800863}
864
Richard Uhlerb81881d2016-04-19 13:08:04 -0700865bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
866 InstructionSet isa,
867 std::string* oat_filename,
868 std::string* error_msg) {
869 CHECK(oat_filename != nullptr);
870 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800871
Richard Uhler55b58b62016-08-12 09:05:13 -0700872 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
873 if (cache_dir.empty()) {
874 *error_msg = "Dalvik cache directory does not exist";
875 return false;
876 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700877
878 // TODO: The oat file assistant should be the definitive place for
879 // determining the oat file name from the dex location, not
880 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700881 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800882}
883
Richard Uhler66d874d2015-01-15 09:37:19 -0800884std::string OatFileAssistant::ImageLocation() {
885 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000886 const std::vector<gc::space::ImageSpace*>& image_spaces =
887 runtime->GetHeap()->GetBootImageSpaces();
888 if (image_spaces.empty()) {
889 return "";
890 }
891 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800892}
893
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000894const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
895 if (!required_dex_checksums_attempted_) {
896 required_dex_checksums_attempted_ = true;
897 required_dex_checksums_found_ = false;
898 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800899 std::string error_msg;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700900 if (DexFileLoader::GetMultiDexChecksums(dex_location_.c_str(),
901 &cached_required_dex_checksums_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700902 &error_msg,
903 zip_fd_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000904 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700905 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800906 } else {
907 // This can happen if the original dex file has been stripped from the
908 // apk.
909 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700910 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800911
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000912 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700913 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800914 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000915 required_dex_checksums_found_ = true;
916 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700917 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000918 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
919 if (odex_dex_file == nullptr) {
920 required_dex_checksums_found_ = false;
921 break;
922 }
923 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800924 }
925 }
926 }
927 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000928 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800929}
930
Richard Uhler95e09672017-02-22 11:37:41 +0000931std::unique_ptr<OatFileAssistant::ImageInfo>
932OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
933 CHECK(error_msg != nullptr);
934
Richard Uhler95e09672017-02-22 11:37:41 +0000935 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000936 std::unique_ptr<ImageInfo> info(new ImageInfo());
937 info->location = runtime->GetImageLocation();
938
939 std::unique_ptr<ImageHeader> image_header(
940 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
941 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000942 return nullptr;
943 }
944
Richard Uhler8f104862017-02-22 12:34:01 +0000945 info->oat_checksum = image_header->GetOatChecksum();
946 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
947 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000948 return info;
949}
950
Richard Uhler66d874d2015-01-15 09:37:19 -0800951const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
952 if (!image_info_load_attempted_) {
953 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000954 std::string error_msg;
955 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
956 if (cached_image_info_ == nullptr) {
957 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800958 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800959 }
Richard Uhler95e09672017-02-22 11:37:41 +0000960 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800961}
962
Richard Uhler88bc6732016-11-14 14:38:03 +0000963OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000964 // TODO(calin): Document the side effects of class loading when
965 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700966 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000967 // If the parent of the dex file is writable it means that we can
968 // create the odex file. In this case we unconditionally pick the odex
969 // as the best oat file. This corresponds to the regular use case when
970 // apps gets installed or when they load private, secondary dex file.
971 // For apps on the system partition the odex location will not be
972 // writable and thus the oat location might be more up to date.
973 return odex_;
974 }
975
976 // We cannot write to the odex location. This must be a system app.
977
978 // If the oat location is usable take it.
979 if (oat_.IsUseable()) {
980 return oat_;
981 }
982
983 // The oat file is not usable but the odex file might be up to date.
984 // This is an indication that we are dealing with an up to date prebuilt
985 // (that doesn't need relocation).
986 if (odex_.Status() == kOatUpToDate) {
987 return odex_;
988 }
989
990 // The oat file is not usable and the odex file is not up to date.
991 // However we have access to the original dex file which means we can make
992 // the oat location up to date.
993 if (HasOriginalDexFiles()) {
994 return oat_;
995 }
996
997 // We got into the worst situation here:
998 // - the oat location is not usable
999 // - the prebuild odex location is not up to date
1000 // - and we don't have the original dex file anymore (stripped).
1001 // Pick the odex if it exists, or the oat if not.
1002 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +00001003}
1004
Andreas Gampea463b6a2016-08-12 21:53:32 -07001005std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001006 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001007 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001008 if (art_file.empty()) {
1009 return nullptr;
1010 }
1011 std::string error_msg;
1012 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -07001013 std::unique_ptr<gc::space::ImageSpace> ret =
1014 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -08001015 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001016 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
1017 }
1018 return ret;
1019}
1020
Richard Uhler88bc6732016-11-14 14:38:03 +00001021OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
1022 bool is_oat_location)
1023 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -07001024{}
1025
Richard Uhler88bc6732016-11-14 14:38:03 +00001026bool OatFileAssistant::OatFileInfo::IsOatLocation() {
1027 return is_oat_location_;
1028}
1029
Richard Uhler743bf362016-04-19 15:39:37 -07001030const std::string* OatFileAssistant::OatFileInfo::Filename() {
1031 return filename_provided_ ? &filename_ : nullptr;
1032}
1033
Richard Uhler03bc6592016-11-22 09:42:04 +00001034bool OatFileAssistant::OatFileInfo::IsUseable() {
1035 switch (Status()) {
1036 case kOatCannotOpen:
1037 case kOatDexOutOfDate:
1038 case kOatBootImageOutOfDate: return false;
1039
1040 case kOatRelocationOutOfDate:
1041 case kOatUpToDate: return true;
1042 }
1043 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001044}
1045
1046OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1047 if (!status_attempted_) {
1048 status_attempted_ = true;
1049 const OatFile* file = GetFile();
1050 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001051 // Check to see if there is a vdex file we can make use of.
1052 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001053 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001054 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001055 if (use_fd_) {
1056 if (vdex_fd_ >= 0) {
1057 struct stat s;
1058 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1059 if (rc == -1) {
1060 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1061 } else {
1062 vdex = VdexFile::Open(vdex_fd_,
1063 s.st_size,
1064 vdex_filename,
1065 false /*writable*/,
1066 false /*low_4gb*/,
1067 false /* unquicken */,
1068 &error_msg);
1069 }
1070 }
1071 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001072 vdex = VdexFile::Open(vdex_filename,
1073 false /*writeable*/,
1074 false /*low_4gb*/,
1075 false /*unquicken*/,
1076 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001077 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001078 if (vdex == nullptr) {
1079 status_ = kOatCannotOpen;
1080 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1081 } else {
1082 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1083 // The vdex file does not contain enough information to determine
1084 // whether it is up to date with respect to the boot image, so we
1085 // assume it is out of date.
1086 VLOG(oat) << error_msg;
1087 status_ = kOatBootImageOutOfDate;
1088 } else {
1089 status_ = kOatDexOutOfDate;
1090 }
1091 }
Richard Uhler743bf362016-04-19 15:39:37 -07001092 } else {
1093 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001094 VLOG(oat) << file->GetLocation() << " is " << status_
1095 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001096 }
1097 }
1098 return status_;
1099}
1100
Richard Uhler70a84262016-11-08 16:51:51 +00001101OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001102 CompilerFilter::Filter target,
1103 bool profile_changed,
1104 bool downgrade,
1105 ClassLoaderContext* context) {
1106
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001107 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001108 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001109 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001110
Calin Juravle44e5efa2017-09-12 00:54:26 -07001111 // Only check the filter and relocation if the class loader context is ok.
1112 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1113 if (class_loader_context_okay) {
1114 if (filter_okay && Status() == kOatUpToDate) {
1115 // The oat file is in good shape as is.
1116 return kNoDexOptNeeded;
1117 }
Richard Uhler70a84262016-11-08 16:51:51 +00001118
Calin Juravle44e5efa2017-09-12 00:54:26 -07001119 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1120 // If no compilation is desired, then it doesn't matter if the oat
1121 // file needs relocation. It's in good shape as is.
1122 return kNoDexOptNeeded;
1123 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001124
Calin Juravle44e5efa2017-09-12 00:54:26 -07001125 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1126 return kDex2OatForRelocation;
1127 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001128
Calin Juravle44e5efa2017-09-12 00:54:26 -07001129 if (IsUseable()) {
1130 return kDex2OatForFilter;
1131 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001132
Calin Juravle44e5efa2017-09-12 00:54:26 -07001133 if (Status() == kOatBootImageOutOfDate) {
1134 return kDex2OatForBootImage;
1135 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001136 }
1137
1138 if (oat_file_assistant_->HasOriginalDexFiles()) {
1139 return kDex2OatFromScratch;
1140 } else {
1141 // Otherwise there is nothing we can do, even if we want to.
1142 return kNoDexOptNeeded;
1143 }
Richard Uhler70a84262016-11-08 16:51:51 +00001144}
1145
Richard Uhler743bf362016-04-19 15:39:37 -07001146const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1147 CHECK(!file_released_) << "GetFile called after oat file released.";
1148 if (!load_attempted_) {
1149 load_attempted_ = true;
1150 if (filename_provided_) {
1151 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001152 if (use_fd_) {
1153 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1154 file_.reset(OatFile::Open(vdex_fd_,
1155 oat_fd_,
1156 filename_.c_str(),
1157 nullptr,
1158 nullptr,
1159 oat_file_assistant_->load_executable_,
1160 false /* low_4gb */,
1161 oat_file_assistant_->dex_location_.c_str(),
1162 &error_msg));
1163 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001164 } else {
1165 file_.reset(OatFile::Open(filename_.c_str(),
1166 filename_.c_str(),
1167 nullptr,
1168 nullptr,
1169 oat_file_assistant_->load_executable_,
1170 false /* low_4gb */,
1171 oat_file_assistant_->dex_location_.c_str(),
1172 &error_msg));
1173 }
Richard Uhler743bf362016-04-19 15:39:37 -07001174 if (file_.get() == nullptr) {
1175 VLOG(oat) << "OatFileAssistant test for existing oat file "
1176 << filename_ << ": " << error_msg;
1177 }
1178 }
1179 }
1180 return file_.get();
1181}
1182
1183bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001184 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001185 const OatFile* file = GetFile();
1186 if (file == nullptr) {
1187 return false;
1188 }
1189
1190 CompilerFilter::Filter current = file->GetCompilerFilter();
1191 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1192 VLOG(oat) << "Compiler filter not okay because Profile changed";
1193 return false;
1194 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001195 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1196 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001197}
1198
Calin Juravle44e5efa2017-09-12 00:54:26 -07001199bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1200 if (context == nullptr) {
1201 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1202 return true;
1203 }
1204
1205 const OatFile* file = GetFile();
1206 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001207 // No oat file means we have nothing to verify.
1208 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001209 }
1210
Calin Juravle20c46442017-09-12 00:54:26 -07001211 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001212 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001213 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001214 : "";
1215
1216 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1217 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1218 return false;
1219 }
1220
1221 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext());
1222 if (!result) {
1223 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1224 << file->GetClassLoaderContext()
1225 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1226 }
1227 return result;
1228}
1229
Richard Uhler743bf362016-04-19 15:39:37 -07001230bool OatFileAssistant::OatFileInfo::IsExecutable() {
1231 const OatFile* file = GetFile();
1232 return (file != nullptr && file->IsExecutable());
1233}
1234
Richard Uhler743bf362016-04-19 15:39:37 -07001235void OatFileAssistant::OatFileInfo::Reset() {
1236 load_attempted_ = false;
1237 file_.reset();
1238 status_attempted_ = false;
1239}
1240
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001241void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, bool use_fd, int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001242 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001243 filename_provided_ = true;
1244 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001245 use_fd_ = use_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001246 vdex_fd_ = vdex_fd;
1247 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001248 Reset();
1249}
1250
1251std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1252 file_released_ = true;
1253 return std::move(file_);
1254}
1255
Richard Uhler70a84262016-11-08 16:51:51 +00001256std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001257 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001258 return ReleaseFile();
1259 }
1260
1261 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1262 << " attempting to fall back to interpreting oat file instead.";
1263
Richard Uhler03bc6592016-11-22 09:42:04 +00001264 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001265 return ReleaseFile();
1266 }
1267
Richard Uhler03bc6592016-11-22 09:42:04 +00001268 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001269 // We are loading an oat file for runtime use that needs relocation.
1270 // Reload the file non-executable to ensure that we interpret out of the
1271 // dex code in the oat file rather than trying to execute the unrelocated
1272 // compiled code.
1273 oat_file_assistant_->load_executable_ = false;
1274 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001275 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001276 CHECK(!IsExecutable());
1277 return ReleaseFile();
1278 }
1279 }
1280 return std::unique_ptr<OatFile>();
1281}
Richard Uhler66d874d2015-01-15 09:37:19 -08001282} // namespace art