blob: 20f9aaa011d6d7dd1dc61c33aba028ae2cb0d747 [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_assistant.h"
18
Richard Uhler46cc64f2016-11-14 14:53:55 +000019#include <sstream>
20
Richard Uhler66d874d2015-01-15 09:37:19 -080021#include <sys/stat.h>
Andreas Gampe9186ced2016-12-12 14:28:21 -080022
Andreas Gampec15a2f42017-04-21 12:09:39 -070023#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080024#include "android-base/strings.h"
25
David Sehr891a50e2017-10-27 17:01:07 -070026#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080027#include "base/logging.h" // For VLOG.
Andreas Gampe5678db52017-06-08 14:11:18 -070028#include "base/stl_util.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "class_linker.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "compiler_filter.h"
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,
Nicolas Geoffray29742602017-12-14 10:09:03 +000074 bool load_executable,
75 bool only_load_system_executable)
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070076 : OatFileAssistant(dex_location,
Nicolas Geoffray29742602017-12-14 10:09:03 +000077 isa,
78 load_executable,
79 only_load_system_executable,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070080 -1 /* vdex_fd */,
81 -1 /* oat_fd */,
82 -1 /* zip_fd */) {}
83
84
85OatFileAssistant::OatFileAssistant(const char* dex_location,
86 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070087 bool load_executable,
Nicolas Geoffray29742602017-12-14 10:09:03 +000088 bool only_load_system_executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070089 int vdex_fd,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070090 int oat_fd,
91 int zip_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000092 : isa_(isa),
93 load_executable_(load_executable),
Nicolas Geoffray29742602017-12-14 10:09:03 +000094 only_load_system_executable_(only_load_system_executable),
Richard Uhler88bc6732016-11-14 14:38:03 +000095 odex_(this, /*is_oat_location*/ false),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -070096 oat_(this, /*is_oat_location*/ true),
97 zip_fd_(zip_fd) {
Richard Uhler740eec92015-10-15 15:12:23 -070098 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000099
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700100 if (zip_fd < 0) {
101 CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
102 << " oat_fd=" << oat_fd;
103 CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
104 << " vdex_fd=" << vdex_fd;;
105 }
106
Calin Juravle099f8d62017-09-05 19:04:04 -0700107 dex_location_.assign(dex_location);
Richard Uhler740eec92015-10-15 15:12:23 -0700108
Richard Uhler66d874d2015-01-15 09:37:19 -0800109 if (load_executable_ && isa != kRuntimeISA) {
110 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
111 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
112 load_executable_ = false;
113 }
114
Richard Uhler743bf362016-04-19 15:39:37 -0700115 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700116 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700117 std::string odex_file_name;
118 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700119 odex_.Reset(odex_file_name, UseFdToReadFiles(), vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700120 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700121 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
122 }
123
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700124 if (!UseFdToReadFiles()) {
125 // Get the oat filename.
126 std::string oat_file_name;
127 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
128 oat_.Reset(oat_file_name, false /* use_fd */);
129 } else {
130 LOG(WARNING) << "Failed to determine oat file name for dex location "
131 << dex_location_ << ": " << error_msg;
132 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000133 }
134
135 // Check if the dex directory is writable.
136 // This will be needed in most uses of OatFileAssistant and so it's OK to
137 // compute it eagerly. (the only use which will not make use of it is
138 // OatFileAssistant::GetStatusDump())
139 size_t pos = dex_location_.rfind('/');
140 if (pos == std::string::npos) {
141 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700142 } else if (!UseFdToReadFiles()) {
143 // We cannot test for parent access when using file descriptors. That's ok
144 // because in this case we will always pick the odex file anyway.
Calin Juravle357c66d2017-05-04 01:57:17 +0000145 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700146 if (access(parent.c_str(), W_OK) == 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000147 dex_parent_writable_ = true;
148 } else {
149 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700150 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800151 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800152}
153
154OatFileAssistant::~OatFileAssistant() {
155 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100156 if (flock_.get() != nullptr) {
157 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800158 }
159}
160
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700161bool OatFileAssistant::UseFdToReadFiles() {
162 return zip_fd_ >= 0;
163}
164
Richard Uhler66d874d2015-01-15 09:37:19 -0800165bool OatFileAssistant::IsInBootClassPath() {
166 // Note: We check the current boot class path, regardless of the ISA
167 // specified by the user. This is okay, because the boot class path should
168 // be the same for all ISAs.
169 // TODO: Can we verify the boot class path is the same for all ISAs?
170 Runtime* runtime = Runtime::Current();
171 ClassLinker* class_linker = runtime->GetClassLinker();
172 const auto& boot_class_path = class_linker->GetBootClassPath();
173 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700174 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800175 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
176 return true;
177 }
178 }
179 return false;
180}
181
182bool OatFileAssistant::Lock(std::string* error_msg) {
183 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100184 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800185
Calin Juravle357c66d2017-05-04 01:57:17 +0000186 // Note the lock will only succeed for secondary dex files and in test
187 // environment.
188 //
189 // The lock *will fail* for all primary apks in a production environment.
190 // The app does not have permissions to create locks next to its dex location
191 // (be it system, data or vendor parition). We also cannot use the odex or
192 // oat location for the same reasoning.
193 //
194 // This is best effort and if it fails it's unlikely that we will be able
195 // to generate oat files anyway.
196 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800197
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100198 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
199 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100200 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800201 return false;
202 }
203 return true;
204}
205
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700206int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
207 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700208 bool downgrade,
209 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000210 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700211 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
212 profile_changed,
213 downgrade,
214 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000215 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
216 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800217 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000218 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800219}
220
Richard Uhlerf4b34872016-04-13 11:03:46 -0700221// Figure out the currently specified compile filter option in the runtime.
222// Returns true on success, false if the compiler filter is invalid, in which
223// case error_msg describes the problem.
224static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
225 std::string* error_msg) {
226 CHECK(filter != nullptr);
227 CHECK(error_msg != nullptr);
228
Calin Juravle357c66d2017-05-04 01:57:17 +0000229 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700230 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
231 if (option.starts_with("--compiler-filter=")) {
232 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
233 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
234 *error_msg = std::string("Unknown --compiler-filter value: ")
235 + std::string(compiler_filter_string);
236 return false;
237 }
238 }
239 }
240 return true;
241}
242
Richard Uhler01be6812016-05-17 10:34:52 -0700243bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000244 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700245}
246
Richard Uhler1e860612016-03-30 12:17:55 -0700247OatFileAssistant::ResultOfAttemptToUpdate
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700248OatFileAssistant::MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700249 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700250 std::string* error_msg) {
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700251 // The method doesn't use zip_fd_ and directly opens dex files at dex_locations_.
252 CHECK_EQ(-1, zip_fd_) << "MakeUpToDate should not be called with zip_fd";
253
Richard Uhlerf4b34872016-04-13 11:03:46 -0700254 CompilerFilter::Filter target;
255 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
256 return kUpdateNotAttempted;
257 }
258
Richard Uhler88bc6732016-11-14 14:38:03 +0000259 OatFileInfo& info = GetBestInfo();
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700260 // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291.
261 // This is actually not trivial in the current logic as it will interact with the collision
262 // check:
263 // - currently, if the context does not match but we have no collisions we still accept the
264 // oat file.
265 // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make
266 // the oat code up to date the collision check becomes useless.
267 // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files
268 // loaded in other processes). So it boils down to how far do we want to complicate
269 // 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 -0700270 switch (info.GetDexOptNeeded(
271 target, profile_changed, /*downgrade*/ false, class_loader_context)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000272 case kNoDexOptNeeded:
273 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000274
Richard Uhler7225a8d2016-11-22 10:12:03 +0000275 // TODO: For now, don't bother with all the different ways we can call
276 // dex2oat to generate the oat file. Always generate the oat file as if it
277 // were kDex2OatFromScratch.
278 case kDex2OatFromScratch:
279 case kDex2OatForBootImage:
280 case kDex2OatForRelocation:
281 case kDex2OatForFilter:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700282 return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800283 }
284 UNREACHABLE();
285}
286
287std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000288 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800289}
290
Richard Uhler46cc64f2016-11-14 14:53:55 +0000291std::string OatFileAssistant::GetStatusDump() {
292 std::ostringstream status;
293 bool oat_file_exists = false;
294 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000295 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000296 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000297 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000298
Richard Uhler46cc64f2016-11-14 14:53:55 +0000299 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000300 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
301 const OatFile* file = oat_.GetFile();
302 if (file == nullptr) {
303 // If the file is null even though the status is not kOatCannotOpen, it
304 // means we must have a vdex file with no corresponding oat file. In
305 // this case we cannot determine the compilation filter. Indicate that
306 // we have only the vdex file instead.
307 status << "vdex-only";
308 } else {
309 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
310 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000311 }
312
Richard Uhler03bc6592016-11-22 09:42:04 +0000313 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000314 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000315 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000316
Richard Uhler46cc64f2016-11-14 14:53:55 +0000317 odex_file_exists = true;
318 if (oat_file_exists) {
319 status << "] ";
320 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000321 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
322 const OatFile* file = odex_.GetFile();
323 if (file == nullptr) {
324 status << "vdex-only";
325 } else {
326 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
327 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000328 }
329
330 if (!oat_file_exists && !odex_file_exists) {
331 status << "invalid[";
332 }
333
334 status << "]";
335 return status.str();
336}
337
Richard Uhler66d874d2015-01-15 09:37:19 -0800338std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700339 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800340 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700341 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
342 return dex_files;
343 } else {
344 return std::vector<std::unique_ptr<const DexFile>>();
345 }
346}
Richard Uhler66d874d2015-01-15 09:37:19 -0800347
Calin Juravle87e2cb62017-06-13 21:48:45 -0700348bool OatFileAssistant::LoadDexFiles(
349 const OatFile &oat_file,
350 const std::string& dex_location,
351 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000352 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800353 std::string error_msg;
354 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700355 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800356 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700357 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700358 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800359 }
360
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700361 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800362 if (dex_file.get() == nullptr) {
363 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700364 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800365 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700366 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800367
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000368 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700369 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700370 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000371 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700372 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000373 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 break;
375 }
376
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700377 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 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700384 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800385}
386
Richard Uhler9b994ea2015-06-24 08:44:19 -0700387bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000388 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700389 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000390 // GetRequiredDexChecksums.
391 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700392 return has_original_dex_files_;
393}
394
Richard Uhler95abd042015-03-24 09:51:28 -0700395OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700396 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800397}
398
Richard Uhler95abd042015-03-24 09:51:28 -0700399OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700400 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800401}
402
Richard Uhler2f27abd2017-01-31 14:02:34 +0000403bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000404 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
405 if (required_dex_checksums == nullptr) {
406 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
407 return true;
408 }
409
410 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
411 if (required_dex_checksums->size() != number_of_dex_files) {
412 *error_msg = StringPrintf("expected %zu dex files but found %u",
413 required_dex_checksums->size(),
414 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000415 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800416 }
417
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000418 for (uint32_t i = 0; i < number_of_dex_files; i++) {
419 uint32_t expected_checksum = (*required_dex_checksums)[i];
420 uint32_t actual_checksum = file.GetLocationChecksum(i);
421 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700422 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000423 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
424 "Expected: %u, actual: %u",
425 dex.c_str(),
426 expected_checksum,
427 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000428 return false;
429 }
430 }
431
Richard Uhler2f27abd2017-01-31 14:02:34 +0000432 return true;
433}
434
435bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000436 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
437 if (required_dex_checksums == nullptr) {
438 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
439 return true;
440 }
441
442 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
443 if (required_dex_checksums->size() != number_of_dex_files) {
444 *error_msg = StringPrintf("expected %zu dex files but found %u",
445 required_dex_checksums->size(),
446 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000447 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800448 }
449
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000450 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700451 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000452 uint32_t expected_checksum = (*required_dex_checksums)[i];
453 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
454 if (oat_dex_file == nullptr) {
455 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
456 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800457 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000458 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
459 if (expected_checksum != actual_checksum) {
460 VLOG(oat) << "Dex checksum does not match for dex: " << dex
461 << ". Expected: " << expected_checksum
462 << ", Actual: " << actual_checksum;
463 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800464 }
465 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000466 return true;
467}
468
469OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
470 // Verify the ART_USE_READ_BARRIER state.
471 // TODO: Don't fully reject files due to read barrier state. If they contain
472 // compiled code and are otherwise okay, we should return something like
473 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
474 // barrier state doesn't matter.
475 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
476 constexpr bool kRuntimeIsCC = kUseReadBarrier;
477 if (is_cc != kRuntimeIsCC) {
478 return kOatCannotOpen;
479 }
480
481 // Verify the dex checksum.
482 std::string error_msg;
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000483 VdexFile* vdex = file.GetVdexFile();
484 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
485 LOG(ERROR) << error_msg;
486 return kOatDexOutOfDate;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000487 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800488
Andreas Gampe29d38e72016-03-23 15:31:51 +0000489 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000490
Richard Uhler66d874d2015-01-15 09:37:19 -0800491 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000492 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
493 const ImageInfo* image_info = GetImageInfo();
494 if (image_info == nullptr) {
495 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000496
Richard Uhler76f5cb62016-04-04 13:30:16 -0700497 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000498 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700499 }
500
501 // If there is no original dex file to fall back to, grudgingly accept
502 // the oat file. This could technically lead to crashes, but there's no
503 // way we could find a better oat file to use for this dex location,
504 // and it's better than being stuck in a boot loop with no way out.
505 // The problem will hopefully resolve itself the next time the runtime
506 // starts up.
507 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
508 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000509 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000510 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000511 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000512 }
513 } else {
514 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800515 }
516
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100517 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000518 if (!file.IsPic()) {
519 const ImageInfo* image_info = GetImageInfo();
520 if (image_info == nullptr) {
521 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000522 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000523 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700524
Andreas Gampe29d38e72016-03-23 15:31:51 +0000525 // Verify the oat_data_begin recorded for the image in the oat file matches
526 // the actual oat_data_begin for boot.oat in the image.
527 const OatHeader& oat_header = file.GetOatHeader();
528 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
529 if (oat_data_begin != image_info->oat_data_begin) {
530 VLOG(oat) << file.GetLocation() <<
531 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
532 << " does not match actual image oat_data_begin ("
533 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000534 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000535 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700536
Andreas Gampe29d38e72016-03-23 15:31:51 +0000537 // Verify the oat_patch_delta recorded for the image in the oat file matches
538 // the actual oat_patch_delta for the image.
539 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
540 if (oat_patch_delta != image_info->patch_delta) {
541 VLOG(oat) << file.GetLocation() <<
542 ": Oat file image patch delta (" << oat_patch_delta << ")"
543 << " does not match actual image patch delta ("
544 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000545 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000546 }
547 } else {
548 // Oat files compiled in PIC mode do not require relocation.
549 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
550 }
551 } else {
552 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800553 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700554 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800555}
556
Calin Juravle357c66d2017-05-04 01:57:17 +0000557static bool DexLocationToOdexNames(const std::string& location,
558 InstructionSet isa,
559 std::string* odex_filename,
560 std::string* oat_dir,
561 std::string* isa_dir,
562 std::string* error_msg) {
563 CHECK(odex_filename != nullptr);
564 CHECK(error_msg != nullptr);
565
566 // The odex file name is formed by replacing the dex_location extension with
567 // .odex and inserting an oat/<isa> directory. For example:
568 // location = /foo/bar/baz.jar
569 // odex_location = /foo/bar/oat/<isa>/baz.odex
570
571 // Find the directory portion of the dex location and add the oat/<isa>
572 // directory.
573 size_t pos = location.rfind('/');
574 if (pos == std::string::npos) {
575 *error_msg = "Dex location " + location + " has no directory.";
576 return false;
577 }
578 std::string dir = location.substr(0, pos+1);
579 // Add the oat directory.
580 dir += "oat";
581 if (oat_dir != nullptr) {
582 *oat_dir = dir;
583 }
584 // Add the isa directory
585 dir += "/" + std::string(GetInstructionSetString(isa));
586 if (isa_dir != nullptr) {
587 *isa_dir = dir;
588 }
589
590 // Get the base part of the file without the extension.
591 std::string file = location.substr(pos+1);
592 pos = file.rfind('.');
593 if (pos == std::string::npos) {
594 *error_msg = "Dex location " + location + " has no extension.";
595 return false;
596 }
597 std::string base = file.substr(0, pos);
598
599 *odex_filename = dir + "/" + base + ".odex";
600 return true;
601}
602
603// Prepare a subcomponent of the odex directory.
604// (i.e. create and set the expected permissions on the path `dir`).
605static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
606 struct stat dir_stat;
607 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
608 // The directory exists. Check if it is indeed a directory.
609 if (!S_ISDIR(dir_stat.st_mode)) {
610 *error_msg = dir + " is not a dir";
611 return false;
612 } else {
613 // The dir is already on disk.
614 return true;
615 }
616 }
617
618 // Failed to stat. We need to create the directory.
619 if (errno != ENOENT) {
620 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
621 return false;
622 }
623
624 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
625 if (mkdir(dir.c_str(), mode) != 0) {
626 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
627 return false;
628 }
629 if (chmod(dir.c_str(), mode) != 0) {
630 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
631 return false;
632 }
633 return true;
634}
635
636// Prepares the odex directory for the given dex location.
637static bool PrepareOdexDirectories(const std::string& dex_location,
638 const std::string& expected_odex_location,
639 InstructionSet isa,
640 std::string* error_msg) {
641 std::string actual_odex_location;
642 std::string oat_dir;
643 std::string isa_dir;
644 if (!DexLocationToOdexNames(
645 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
646 return false;
647 }
648 DCHECK_EQ(expected_odex_location, actual_odex_location);
649
650 if (!PrepareDirectory(oat_dir, error_msg)) {
651 return false;
652 }
653 if (!PrepareDirectory(isa_dir, error_msg)) {
654 return false;
655 }
656 return true;
657}
658
Calin Juravled4215bb2017-09-20 11:54:21 -0700659class Dex2oatFileWrapper {
660 public:
661 explicit Dex2oatFileWrapper(File* file)
662 : file_(file),
663 unlink_file_at_destruction_(true) {
664 }
665
666 ~Dex2oatFileWrapper() {
667 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
668 file_->Erase(/*unlink*/ true);
669 }
670 }
671
672 File* GetFile() { return file_.get(); }
673
674 void DisableUnlinkAtDestruction() {
675 unlink_file_at_destruction_ = false;
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800676 }
Calin Juravled4215bb2017-09-20 11:54:21 -0700677
678 private:
679 std::unique_ptr<File> file_;
680 bool unlink_file_at_destruction_;
681};
682
Calin Juravle357c66d2017-05-04 01:57:17 +0000683OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700684 OatFileAssistant::OatFileInfo& info,
685 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700686 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700687 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800688 CHECK(error_msg != nullptr);
689
Richard Uhler8327cf72015-10-13 16:34:59 -0700690 Runtime* runtime = Runtime::Current();
691 if (!runtime->IsDex2OatEnabled()) {
692 *error_msg = "Generation of oat file for dex location " + dex_location_
693 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700694 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700695 }
696
Calin Juravle357c66d2017-05-04 01:57:17 +0000697 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700698 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800699 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700700 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800701 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000702 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700703 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800704
Richard Uhler66d874d2015-01-15 09:37:19 -0800705 // dex2oat ignores missing dex files and doesn't report an error.
706 // Check explicitly here so we can detect the error properly.
707 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000708 struct stat dex_path_stat;
709 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
710 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700711 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800712 }
713
Calin Juravle357c66d2017-05-04 01:57:17 +0000714 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
715 if (!info.IsOatLocation()) {
716 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
717 return kUpdateNotAttempted;
718 }
719 }
720
721 // Set the permissions for the oat and the vdex files.
722 // The user always gets read and write while the group and others propagate
723 // the reading access of the original dex file.
724 mode_t file_mode = S_IRUSR | S_IWUSR |
725 (dex_path_stat.st_mode & S_IRGRP) |
726 (dex_path_stat.st_mode & S_IROTH);
727
Calin Juravled4215bb2017-09-20 11:54:21 -0700728 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
729 File* vdex_file = vdex_file_wrapper.GetFile();
730 if (vdex_file == nullptr) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100731 *error_msg = "Generation of oat file " + oat_file_name
732 + " not attempted because the vdex file " + vdex_file_name
733 + " could not be opened.";
734 return kUpdateNotAttempted;
735 }
736
Calin Juravle357c66d2017-05-04 01:57:17 +0000737 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100738 *error_msg = "Generation of oat file " + oat_file_name
739 + " not attempted because the vdex file " + vdex_file_name
740 + " could not be made world readable.";
741 return kUpdateNotAttempted;
742 }
743
Calin Juravled4215bb2017-09-20 11:54:21 -0700744 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
745 File* oat_file = oat_file_wrapper.GetFile();
746 if (oat_file == nullptr) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700747 *error_msg = "Generation of oat file " + oat_file_name
748 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700749 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700750 }
751
Calin Juravle357c66d2017-05-04 01:57:17 +0000752 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700753 *error_msg = "Generation of oat file " + oat_file_name
754 + " not attempted because the oat file could not be made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700755 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700756 }
757
758 std::vector<std::string> args;
759 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000760 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700761 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
762 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000763 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700764 const std::string dex2oat_context = class_loader_context == nullptr
765 ? OatFile::kSpecialSharedLibrary
766 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
767 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700768
Richard Uhler66d874d2015-01-15 09:37:19 -0800769 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700770 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700771 }
772
David Brazdil7b49e6c2016-09-01 11:06:18 +0100773 if (vdex_file->FlushCloseOrErase() != 0) {
774 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100775 return kUpdateFailed;
776 }
777
Richard Uhler8327cf72015-10-13 16:34:59 -0700778 if (oat_file->FlushCloseOrErase() != 0) {
779 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700780 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800781 }
782
Calin Juravle357c66d2017-05-04 01:57:17 +0000783 // Mark that the odex file has changed and we should try to reload.
784 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700785 // We have compiled successfully. Disable the auto-unlink.
786 vdex_file_wrapper.DisableUnlinkAtDestruction();
787 oat_file_wrapper.DisableUnlinkAtDestruction();
788
Richard Uhler1e860612016-03-30 12:17:55 -0700789 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800790}
791
792bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
793 std::string* error_msg) {
794 Runtime* runtime = Runtime::Current();
795 std::string image_location = ImageLocation();
796 if (image_location.empty()) {
797 *error_msg = "No image location found for Dex2Oat.";
798 return false;
799 }
800
801 std::vector<std::string> argv;
802 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000803 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200804 argv.push_back("--debuggable");
805 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700806 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800807
808 if (!runtime->IsVerificationEnabled()) {
809 argv.push_back("--compiler-filter=verify-none");
810 }
811
812 if (runtime->MustRelocateIfPossible()) {
813 argv.push_back("--runtime-arg");
814 argv.push_back("-Xrelocate");
815 } else {
816 argv.push_back("--runtime-arg");
817 argv.push_back("-Xnorelocate");
818 }
819
820 if (!kIsTargetBuild) {
821 argv.push_back("--host");
822 }
823
824 argv.push_back("--boot-image=" + image_location);
825
826 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
827 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
828
829 argv.insert(argv.end(), args.begin(), args.end());
830
Andreas Gampe9186ced2016-12-12 14:28:21 -0800831 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800832 return Exec(argv, error_msg);
833}
834
Richard Uhlerb81881d2016-04-19 13:08:04 -0700835bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
836 InstructionSet isa,
837 std::string* odex_filename,
838 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000839 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800840}
841
Richard Uhlerb81881d2016-04-19 13:08:04 -0700842bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
843 InstructionSet isa,
844 std::string* oat_filename,
845 std::string* error_msg) {
846 CHECK(oat_filename != nullptr);
847 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800848
Richard Uhler55b58b62016-08-12 09:05:13 -0700849 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
850 if (cache_dir.empty()) {
851 *error_msg = "Dalvik cache directory does not exist";
852 return false;
853 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700854
855 // TODO: The oat file assistant should be the definitive place for
856 // determining the oat file name from the dex location, not
857 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700858 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800859}
860
Richard Uhler66d874d2015-01-15 09:37:19 -0800861std::string OatFileAssistant::ImageLocation() {
862 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000863 const std::vector<gc::space::ImageSpace*>& image_spaces =
864 runtime->GetHeap()->GetBootImageSpaces();
865 if (image_spaces.empty()) {
866 return "";
867 }
868 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800869}
870
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000871const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
872 if (!required_dex_checksums_attempted_) {
873 required_dex_checksums_attempted_ = true;
874 required_dex_checksums_found_ = false;
875 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800876 std::string error_msg;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700877 if (DexFileLoader::GetMultiDexChecksums(dex_location_.c_str(),
878 &cached_required_dex_checksums_,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700879 &error_msg,
880 zip_fd_)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000881 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700882 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800883 } else {
884 // This can happen if the original dex file has been stripped from the
885 // apk.
886 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700887 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800888
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000889 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700890 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800891 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000892 required_dex_checksums_found_ = true;
893 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700894 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000895 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
896 if (odex_dex_file == nullptr) {
897 required_dex_checksums_found_ = false;
898 break;
899 }
900 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800901 }
902 }
903 }
904 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000905 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800906}
907
Richard Uhler95e09672017-02-22 11:37:41 +0000908std::unique_ptr<OatFileAssistant::ImageInfo>
909OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
910 CHECK(error_msg != nullptr);
911
Richard Uhler95e09672017-02-22 11:37:41 +0000912 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000913 std::unique_ptr<ImageInfo> info(new ImageInfo());
914 info->location = runtime->GetImageLocation();
915
916 std::unique_ptr<ImageHeader> image_header(
917 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
918 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000919 return nullptr;
920 }
921
Richard Uhler8f104862017-02-22 12:34:01 +0000922 info->oat_checksum = image_header->GetOatChecksum();
923 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
924 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000925 return info;
926}
927
Richard Uhler66d874d2015-01-15 09:37:19 -0800928const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
929 if (!image_info_load_attempted_) {
930 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000931 std::string error_msg;
932 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
933 if (cached_image_info_ == nullptr) {
934 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800935 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800936 }
Richard Uhler95e09672017-02-22 11:37:41 +0000937 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800938}
939
Richard Uhler88bc6732016-11-14 14:38:03 +0000940OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000941 // TODO(calin): Document the side effects of class loading when
942 // running dalvikvm command line.
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700943 if (dex_parent_writable_ || UseFdToReadFiles()) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000944 // If the parent of the dex file is writable it means that we can
945 // create the odex file. In this case we unconditionally pick the odex
946 // as the best oat file. This corresponds to the regular use case when
947 // apps gets installed or when they load private, secondary dex file.
948 // For apps on the system partition the odex location will not be
949 // writable and thus the oat location might be more up to date.
950 return odex_;
951 }
952
953 // We cannot write to the odex location. This must be a system app.
954
955 // If the oat location is usable take it.
956 if (oat_.IsUseable()) {
957 return oat_;
958 }
959
960 // The oat file is not usable but the odex file might be up to date.
961 // This is an indication that we are dealing with an up to date prebuilt
962 // (that doesn't need relocation).
963 if (odex_.Status() == kOatUpToDate) {
964 return odex_;
965 }
966
967 // The oat file is not usable and the odex file is not up to date.
968 // However we have access to the original dex file which means we can make
969 // the oat location up to date.
970 if (HasOriginalDexFiles()) {
971 return oat_;
972 }
973
974 // We got into the worst situation here:
975 // - the oat location is not usable
976 // - the prebuild odex location is not up to date
977 // - and we don't have the original dex file anymore (stripped).
978 // Pick the odex if it exists, or the oat if not.
979 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000980}
981
Andreas Gampea463b6a2016-08-12 21:53:32 -0700982std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800983 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100984 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800985 if (art_file.empty()) {
986 return nullptr;
987 }
988 std::string error_msg;
989 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700990 std::unique_ptr<gc::space::ImageSpace> ret =
991 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800992 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800993 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
994 }
995 return ret;
996}
997
Richard Uhler88bc6732016-11-14 14:38:03 +0000998OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
999 bool is_oat_location)
1000 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -07001001{}
1002
Richard Uhler88bc6732016-11-14 14:38:03 +00001003bool OatFileAssistant::OatFileInfo::IsOatLocation() {
1004 return is_oat_location_;
1005}
1006
Richard Uhler743bf362016-04-19 15:39:37 -07001007const std::string* OatFileAssistant::OatFileInfo::Filename() {
1008 return filename_provided_ ? &filename_ : nullptr;
1009}
1010
Richard Uhler03bc6592016-11-22 09:42:04 +00001011bool OatFileAssistant::OatFileInfo::IsUseable() {
1012 switch (Status()) {
1013 case kOatCannotOpen:
1014 case kOatDexOutOfDate:
1015 case kOatBootImageOutOfDate: return false;
1016
1017 case kOatRelocationOutOfDate:
1018 case kOatUpToDate: return true;
1019 }
1020 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001021}
1022
1023OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1024 if (!status_attempted_) {
1025 status_attempted_ = true;
1026 const OatFile* file = GetFile();
1027 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001028 // Check to see if there is a vdex file we can make use of.
1029 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001030 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001031 std::unique_ptr<VdexFile> vdex;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001032 if (use_fd_) {
1033 if (vdex_fd_ >= 0) {
1034 struct stat s;
1035 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1036 if (rc == -1) {
1037 error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1038 } else {
1039 vdex = VdexFile::Open(vdex_fd_,
1040 s.st_size,
1041 vdex_filename,
1042 false /*writable*/,
1043 false /*low_4gb*/,
1044 false /* unquicken */,
1045 &error_msg);
1046 }
1047 }
1048 } else {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001049 vdex = VdexFile::Open(vdex_filename,
1050 false /*writeable*/,
1051 false /*low_4gb*/,
1052 false /*unquicken*/,
1053 &error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001054 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001055 if (vdex == nullptr) {
1056 status_ = kOatCannotOpen;
1057 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1058 } else {
1059 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1060 // The vdex file does not contain enough information to determine
1061 // whether it is up to date with respect to the boot image, so we
1062 // assume it is out of date.
1063 VLOG(oat) << error_msg;
1064 status_ = kOatBootImageOutOfDate;
1065 } else {
1066 status_ = kOatDexOutOfDate;
1067 }
1068 }
Richard Uhler743bf362016-04-19 15:39:37 -07001069 } else {
1070 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001071 VLOG(oat) << file->GetLocation() << " is " << status_
1072 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001073 }
1074 }
1075 return status_;
1076}
1077
Richard Uhler70a84262016-11-08 16:51:51 +00001078OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001079 CompilerFilter::Filter target,
1080 bool profile_changed,
1081 bool downgrade,
1082 ClassLoaderContext* context) {
1083
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001084 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001085 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001086 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001087
Calin Juravle44e5efa2017-09-12 00:54:26 -07001088 // Only check the filter and relocation if the class loader context is ok.
1089 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1090 if (class_loader_context_okay) {
1091 if (filter_okay && Status() == kOatUpToDate) {
1092 // The oat file is in good shape as is.
1093 return kNoDexOptNeeded;
1094 }
Richard Uhler70a84262016-11-08 16:51:51 +00001095
Calin Juravle44e5efa2017-09-12 00:54:26 -07001096 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1097 // If no compilation is desired, then it doesn't matter if the oat
1098 // file needs relocation. It's in good shape as is.
1099 return kNoDexOptNeeded;
1100 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001101
Calin Juravle44e5efa2017-09-12 00:54:26 -07001102 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1103 return kDex2OatForRelocation;
1104 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001105
Calin Juravle44e5efa2017-09-12 00:54:26 -07001106 if (IsUseable()) {
1107 return kDex2OatForFilter;
1108 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001109
Calin Juravle44e5efa2017-09-12 00:54:26 -07001110 if (Status() == kOatBootImageOutOfDate) {
1111 return kDex2OatForBootImage;
1112 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001113 }
1114
1115 if (oat_file_assistant_->HasOriginalDexFiles()) {
1116 return kDex2OatFromScratch;
1117 } else {
1118 // Otherwise there is nothing we can do, even if we want to.
1119 return kNoDexOptNeeded;
1120 }
Richard Uhler70a84262016-11-08 16:51:51 +00001121}
1122
Richard Uhler743bf362016-04-19 15:39:37 -07001123const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1124 CHECK(!file_released_) << "GetFile called after oat file released.";
1125 if (!load_attempted_) {
1126 load_attempted_ = true;
1127 if (filename_provided_) {
Nicolas Geoffray29742602017-12-14 10:09:03 +00001128 bool executable = oat_file_assistant_->load_executable_;
1129 if (executable && oat_file_assistant_->only_load_system_executable_) {
1130 executable = LocationIsOnSystem(filename_.c_str());
1131 }
Richard Uhler743bf362016-04-19 15:39:37 -07001132 std::string error_msg;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001133 if (use_fd_) {
1134 if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1135 file_.reset(OatFile::Open(vdex_fd_,
1136 oat_fd_,
1137 filename_.c_str(),
1138 nullptr,
1139 nullptr,
Nicolas Geoffray29742602017-12-14 10:09:03 +00001140 executable,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001141 false /* low_4gb */,
1142 oat_file_assistant_->dex_location_.c_str(),
1143 &error_msg));
1144 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001145 } else {
1146 file_.reset(OatFile::Open(filename_.c_str(),
1147 filename_.c_str(),
1148 nullptr,
1149 nullptr,
Nicolas Geoffray29742602017-12-14 10:09:03 +00001150 executable,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001151 false /* low_4gb */,
1152 oat_file_assistant_->dex_location_.c_str(),
1153 &error_msg));
1154 }
Richard Uhler743bf362016-04-19 15:39:37 -07001155 if (file_.get() == nullptr) {
1156 VLOG(oat) << "OatFileAssistant test for existing oat file "
1157 << filename_ << ": " << error_msg;
1158 }
1159 }
1160 }
1161 return file_.get();
1162}
1163
1164bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001165 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001166 const OatFile* file = GetFile();
1167 if (file == nullptr) {
1168 return false;
1169 }
1170
1171 CompilerFilter::Filter current = file->GetCompilerFilter();
1172 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1173 VLOG(oat) << "Compiler filter not okay because Profile changed";
1174 return false;
1175 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001176 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1177 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001178}
1179
Calin Juravle44e5efa2017-09-12 00:54:26 -07001180bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1181 if (context == nullptr) {
1182 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1183 return true;
1184 }
1185
1186 const OatFile* file = GetFile();
1187 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001188 // No oat file means we have nothing to verify.
1189 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001190 }
1191
Calin Juravle20c46442017-09-12 00:54:26 -07001192 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001193 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001194 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001195 : "";
1196
1197 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1198 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1199 return false;
1200 }
1201
1202 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext());
1203 if (!result) {
1204 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1205 << file->GetClassLoaderContext()
1206 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1207 }
1208 return result;
1209}
1210
Richard Uhler743bf362016-04-19 15:39:37 -07001211bool OatFileAssistant::OatFileInfo::IsExecutable() {
1212 const OatFile* file = GetFile();
1213 return (file != nullptr && file->IsExecutable());
1214}
1215
Richard Uhler743bf362016-04-19 15:39:37 -07001216void OatFileAssistant::OatFileInfo::Reset() {
1217 load_attempted_ = false;
1218 file_.reset();
1219 status_attempted_ = false;
1220}
1221
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001222void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, bool use_fd, int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001223 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001224 filename_provided_ = true;
1225 filename_ = filename;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -07001226 use_fd_ = use_fd;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001227 vdex_fd_ = vdex_fd;
1228 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001229 Reset();
1230}
1231
1232std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1233 file_released_ = true;
1234 return std::move(file_);
1235}
1236
Richard Uhler70a84262016-11-08 16:51:51 +00001237std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001238 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001239 return ReleaseFile();
1240 }
1241
1242 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1243 << " attempting to fall back to interpreting oat file instead.";
1244
Richard Uhler03bc6592016-11-22 09:42:04 +00001245 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001246 return ReleaseFile();
1247 }
1248
Richard Uhler03bc6592016-11-22 09:42:04 +00001249 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001250 // We are loading an oat file for runtime use that needs relocation.
1251 // Reload the file non-executable to ensure that we interpret out of the
1252 // dex code in the oat file rather than trying to execute the unrelocated
1253 // compiled code.
1254 oat_file_assistant_->load_executable_ = false;
1255 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001256 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001257 CHECK(!IsExecutable());
1258 return ReleaseFile();
1259 }
1260 }
1261 return std::unique_ptr<OatFile>();
1262}
Richard Uhler66d874d2015-01-15 09:37:19 -08001263} // namespace art