blob: 603bbbf8bd685eaf8c88c28b115cdd4cf4e960cd [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
Richard Uhler66d874d2015-01-15 09:37:19 -080026#include "base/logging.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010027#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080028#include "class_linker.h"
David Sehr97c381e2017-02-01 15:09:58 -080029#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "gc/heap.h"
31#include "gc/space/image_space.h"
32#include "image.h"
33#include "oat.h"
34#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000038#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080039
40namespace art {
41
Richard Uhler69bcf2c2017-01-24 10:25:21 +000042using android::base::StringPrintf;
43
Narayan Kamath8943c1d2016-05-02 13:14:48 +010044std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
45 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000046 case OatFileAssistant::kOatCannotOpen:
47 stream << "kOatCannotOpen";
48 break;
49 case OatFileAssistant::kOatDexOutOfDate:
50 stream << "kOatDexOutOfDate";
51 break;
52 case OatFileAssistant::kOatBootImageOutOfDate:
53 stream << "kOatBootImageOutOfDate";
54 break;
55 case OatFileAssistant::kOatRelocationOutOfDate:
56 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010057 break;
58 case OatFileAssistant::kOatUpToDate:
59 stream << "kOatUpToDate";
60 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010061 default:
62 UNREACHABLE();
63 }
64
65 return stream;
66}
67
Richard Uhler66d874d2015-01-15 09:37:19 -080068OatFileAssistant::OatFileAssistant(const char* dex_location,
69 const InstructionSet isa,
70 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000071 : isa_(isa),
72 load_executable_(load_executable),
73 odex_(this, /*is_oat_location*/ false),
74 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070075 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000076
77 // Try to get the realpath for the dex location.
78 //
79 // This is OK with respect to dalvik cache naming scheme because we never
80 // generate oat files starting from symlinks which go into dalvik cache.
81 // (recall that the oat files in dalvik cache are encoded by replacing '/'
82 // with '@' in the path).
83 // The boot image oat files (which are symlinked in dalvik-cache) are not
84 // loaded via the oat file assistant.
85 //
86 // The only case when the dex location may resolve to a different path
87 // is for secondary dex files (e.g. /data/user/0 symlinks to /data/data and
88 // the app is free to create its own internal layout). Related to this it is
89 // worthwhile to mention that installd resolves the secondary dex location
90 // before calling dex2oat.
91 UniqueCPtr<const char[]> dex_location_real(realpath(dex_location, nullptr));
92 if (dex_location_real != nullptr) {
93 dex_location_.assign(dex_location_real.get());
94 } else {
95 // If we can't get the realpath of the location there's not much point in trying to move on.
96 PLOG(ERROR) << "Could not get the realpath of dex_location " << dex_location;
97 return;
98 }
Richard Uhler740eec92015-10-15 15:12:23 -070099
Richard Uhler66d874d2015-01-15 09:37:19 -0800100 if (load_executable_ && isa != kRuntimeISA) {
101 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
102 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
103 load_executable_ = false;
104 }
105
Richard Uhler743bf362016-04-19 15:39:37 -0700106 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700107 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700108 std::string odex_file_name;
109 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
110 odex_.Reset(odex_file_name);
111 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700112 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
113 }
114
Richard Uhler743bf362016-04-19 15:39:37 -0700115 // Get the oat filename.
Calin Juravle357c66d2017-05-04 01:57:17 +0000116 std::string oat_file_name;
117 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
118 oat_.Reset(oat_file_name);
Richard Uhlerd684f522016-04-19 13:24:41 -0700119 } else {
Calin Juravle357c66d2017-05-04 01:57:17 +0000120 LOG(WARNING) << "Failed to determine oat file name for dex location "
Calin Juravle9bfc6bb2017-05-04 00:13:50 +0000121 << dex_location_ << ": " << error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +0000122 }
123
124 // Check if the dex directory is writable.
125 // This will be needed in most uses of OatFileAssistant and so it's OK to
126 // compute it eagerly. (the only use which will not make use of it is
127 // OatFileAssistant::GetStatusDump())
128 size_t pos = dex_location_.rfind('/');
129 if (pos == std::string::npos) {
130 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
131 } else {
132 std::string parent = dex_location_.substr(0, pos);
133 if (access(parent.c_str(), W_OK) == 0) {
134 dex_parent_writable_ = true;
135 } else {
136 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700137 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800138 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800139}
140
141OatFileAssistant::~OatFileAssistant() {
142 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700143 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100144 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800145 }
146}
147
148bool OatFileAssistant::IsInBootClassPath() {
149 // Note: We check the current boot class path, regardless of the ISA
150 // specified by the user. This is okay, because the boot class path should
151 // be the same for all ISAs.
152 // TODO: Can we verify the boot class path is the same for all ISAs?
153 Runtime* runtime = Runtime::Current();
154 ClassLinker* class_linker = runtime->GetClassLinker();
155 const auto& boot_class_path = class_linker->GetBootClassPath();
156 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700157 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800158 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
159 return true;
160 }
161 }
162 return false;
163}
164
165bool OatFileAssistant::Lock(std::string* error_msg) {
166 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700167 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800168
Calin Juravle357c66d2017-05-04 01:57:17 +0000169 // Note the lock will only succeed for secondary dex files and in test
170 // environment.
171 //
172 // The lock *will fail* for all primary apks in a production environment.
173 // The app does not have permissions to create locks next to its dex location
174 // (be it system, data or vendor parition). We also cannot use the odex or
175 // oat location for the same reasoning.
176 //
177 // This is best effort and if it fails it's unlikely that we will be able
178 // to generate oat files anyway.
179 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800180
Richard Uhler581f4e92015-05-07 10:19:35 -0700181 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100182 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800183 return false;
184 }
185 return true;
186}
187
Richard Uhler7225a8d2016-11-22 10:12:03 +0000188int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000189 OatFileInfo& info = GetBestInfo();
190 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000191 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
192 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800193 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000194 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800195}
196
Richard Uhlerf4b34872016-04-13 11:03:46 -0700197// Figure out the currently specified compile filter option in the runtime.
198// Returns true on success, false if the compiler filter is invalid, in which
199// case error_msg describes the problem.
200static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
201 std::string* error_msg) {
202 CHECK(filter != nullptr);
203 CHECK(error_msg != nullptr);
204
Calin Juravle357c66d2017-05-04 01:57:17 +0000205 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700206 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
207 if (option.starts_with("--compiler-filter=")) {
208 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
209 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
210 *error_msg = std::string("Unknown --compiler-filter value: ")
211 + std::string(compiler_filter_string);
212 return false;
213 }
214 }
215 }
216 return true;
217}
218
Richard Uhler01be6812016-05-17 10:34:52 -0700219bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000220 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700221}
222
Richard Uhler1e860612016-03-30 12:17:55 -0700223OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700224OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700225 CompilerFilter::Filter target;
226 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
227 return kUpdateNotAttempted;
228 }
229
Richard Uhler88bc6732016-11-14 14:38:03 +0000230 OatFileInfo& info = GetBestInfo();
231 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000232 case kNoDexOptNeeded:
233 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000234
Richard Uhler7225a8d2016-11-22 10:12:03 +0000235 // TODO: For now, don't bother with all the different ways we can call
236 // dex2oat to generate the oat file. Always generate the oat file as if it
237 // were kDex2OatFromScratch.
238 case kDex2OatFromScratch:
239 case kDex2OatForBootImage:
240 case kDex2OatForRelocation:
241 case kDex2OatForFilter:
Calin Juravle357c66d2017-05-04 01:57:17 +0000242 return GenerateOatFileNoChecks(info, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800243 }
244 UNREACHABLE();
245}
246
247std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000248 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800249}
250
Richard Uhler46cc64f2016-11-14 14:53:55 +0000251std::string OatFileAssistant::GetStatusDump() {
252 std::ostringstream status;
253 bool oat_file_exists = false;
254 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000255 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000256 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000257 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000258
Richard Uhler46cc64f2016-11-14 14:53:55 +0000259 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000260 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
261 const OatFile* file = oat_.GetFile();
262 if (file == nullptr) {
263 // If the file is null even though the status is not kOatCannotOpen, it
264 // means we must have a vdex file with no corresponding oat file. In
265 // this case we cannot determine the compilation filter. Indicate that
266 // we have only the vdex file instead.
267 status << "vdex-only";
268 } else {
269 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
270 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000271 }
272
Richard Uhler03bc6592016-11-22 09:42:04 +0000273 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000274 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000275 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000276
Richard Uhler46cc64f2016-11-14 14:53:55 +0000277 odex_file_exists = true;
278 if (oat_file_exists) {
279 status << "] ";
280 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000281 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
282 const OatFile* file = odex_.GetFile();
283 if (file == nullptr) {
284 status << "vdex-only";
285 } else {
286 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
287 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000288 }
289
290 if (!oat_file_exists && !odex_file_exists) {
291 status << "invalid[";
292 }
293
294 status << "]";
295 return status.str();
296}
297
Richard Uhler66d874d2015-01-15 09:37:19 -0800298std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
299 const OatFile& oat_file, const char* dex_location) {
300 std::vector<std::unique_ptr<const DexFile>> dex_files;
301
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000302 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 std::string error_msg;
304 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700305 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800306 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700307 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800308 return std::vector<std::unique_ptr<const DexFile>>();
309 }
310
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700311 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800312 if (dex_file.get() == nullptr) {
313 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
314 return std::vector<std::unique_ptr<const DexFile>>();
315 }
316 dex_files.push_back(std::move(dex_file));
317
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000318 // Load the rest of the multidex entries
Andreas Gampe90e34042015-04-27 20:01:52 -0700319 for (size_t i = 1; ; i++) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000320 std::string multidex_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
321 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700322 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000323 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800324 break;
325 }
326
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700327 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800328 if (dex_file.get() == nullptr) {
329 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
330 return std::vector<std::unique_ptr<const DexFile>>();
331 }
332 dex_files.push_back(std::move(dex_file));
333 }
334 return dex_files;
335}
336
Richard Uhler9b994ea2015-06-24 08:44:19 -0700337bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000338 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700339 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000340 // GetRequiredDexChecksums.
341 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700342 return has_original_dex_files_;
343}
344
Richard Uhler95abd042015-03-24 09:51:28 -0700345OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700346 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800347}
348
Richard Uhler95abd042015-03-24 09:51:28 -0700349OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700350 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800351}
352
Richard Uhler2f27abd2017-01-31 14:02:34 +0000353bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000354 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
355 if (required_dex_checksums == nullptr) {
356 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
357 return true;
358 }
359
360 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
361 if (required_dex_checksums->size() != number_of_dex_files) {
362 *error_msg = StringPrintf("expected %zu dex files but found %u",
363 required_dex_checksums->size(),
364 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000365 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800366 }
367
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000368 for (uint32_t i = 0; i < number_of_dex_files; i++) {
369 uint32_t expected_checksum = (*required_dex_checksums)[i];
370 uint32_t actual_checksum = file.GetLocationChecksum(i);
371 if (expected_checksum != actual_checksum) {
372 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
373 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
374 "Expected: %u, actual: %u",
375 dex.c_str(),
376 expected_checksum,
377 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000378 return false;
379 }
380 }
381
Richard Uhler2f27abd2017-01-31 14:02:34 +0000382 return true;
383}
384
385bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000386 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
387 if (required_dex_checksums == nullptr) {
388 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
389 return true;
390 }
391
392 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
393 if (required_dex_checksums->size() != number_of_dex_files) {
394 *error_msg = StringPrintf("expected %zu dex files but found %u",
395 required_dex_checksums->size(),
396 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000397 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800398 }
399
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000400 for (uint32_t i = 0; i < number_of_dex_files; i++) {
401 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
402 uint32_t expected_checksum = (*required_dex_checksums)[i];
403 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
404 if (oat_dex_file == nullptr) {
405 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
406 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800407 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000408 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
409 if (expected_checksum != actual_checksum) {
410 VLOG(oat) << "Dex checksum does not match for dex: " << dex
411 << ". Expected: " << expected_checksum
412 << ", Actual: " << actual_checksum;
413 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800414 }
415 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000416 return true;
417}
418
419OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
420 // Verify the ART_USE_READ_BARRIER state.
421 // TODO: Don't fully reject files due to read barrier state. If they contain
422 // compiled code and are otherwise okay, we should return something like
423 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
424 // barrier state doesn't matter.
425 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
426 constexpr bool kRuntimeIsCC = kUseReadBarrier;
427 if (is_cc != kRuntimeIsCC) {
428 return kOatCannotOpen;
429 }
430
431 // Verify the dex checksum.
432 std::string error_msg;
433 if (kIsVdexEnabled) {
434 VdexFile* vdex = file.GetVdexFile();
435 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
436 LOG(ERROR) << error_msg;
437 return kOatDexOutOfDate;
438 }
439 } else {
440 if (!DexChecksumUpToDate(file, &error_msg)) {
441 LOG(ERROR) << error_msg;
442 return kOatDexOutOfDate;
443 }
444 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800445
Andreas Gampe29d38e72016-03-23 15:31:51 +0000446 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000447
Richard Uhler66d874d2015-01-15 09:37:19 -0800448 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000449 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
450 const ImageInfo* image_info = GetImageInfo();
451 if (image_info == nullptr) {
452 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000453
Richard Uhler76f5cb62016-04-04 13:30:16 -0700454 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000455 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700456 }
457
458 // If there is no original dex file to fall back to, grudgingly accept
459 // the oat file. This could technically lead to crashes, but there's no
460 // way we could find a better oat file to use for this dex location,
461 // and it's better than being stuck in a boot loop with no way out.
462 // The problem will hopefully resolve itself the next time the runtime
463 // starts up.
464 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
465 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000466 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000467 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000468 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000469 }
470 } else {
471 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800472 }
473
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100474 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000475 if (!file.IsPic()) {
476 const ImageInfo* image_info = GetImageInfo();
477 if (image_info == nullptr) {
478 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000479 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000480 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700481
Andreas Gampe29d38e72016-03-23 15:31:51 +0000482 // Verify the oat_data_begin recorded for the image in the oat file matches
483 // the actual oat_data_begin for boot.oat in the image.
484 const OatHeader& oat_header = file.GetOatHeader();
485 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
486 if (oat_data_begin != image_info->oat_data_begin) {
487 VLOG(oat) << file.GetLocation() <<
488 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
489 << " does not match actual image oat_data_begin ("
490 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000491 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000492 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700493
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 // Verify the oat_patch_delta recorded for the image in the oat file matches
495 // the actual oat_patch_delta for the image.
496 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
497 if (oat_patch_delta != image_info->patch_delta) {
498 VLOG(oat) << file.GetLocation() <<
499 ": Oat file image patch delta (" << oat_patch_delta << ")"
500 << " does not match actual image patch delta ("
501 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000502 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000503 }
504 } else {
505 // Oat files compiled in PIC mode do not require relocation.
506 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
507 }
508 } else {
509 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800510 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700511 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800512}
513
Calin Juravle357c66d2017-05-04 01:57:17 +0000514static bool DexLocationToOdexNames(const std::string& location,
515 InstructionSet isa,
516 std::string* odex_filename,
517 std::string* oat_dir,
518 std::string* isa_dir,
519 std::string* error_msg) {
520 CHECK(odex_filename != nullptr);
521 CHECK(error_msg != nullptr);
522
523 // The odex file name is formed by replacing the dex_location extension with
524 // .odex and inserting an oat/<isa> directory. For example:
525 // location = /foo/bar/baz.jar
526 // odex_location = /foo/bar/oat/<isa>/baz.odex
527
528 // Find the directory portion of the dex location and add the oat/<isa>
529 // directory.
530 size_t pos = location.rfind('/');
531 if (pos == std::string::npos) {
532 *error_msg = "Dex location " + location + " has no directory.";
533 return false;
534 }
535 std::string dir = location.substr(0, pos+1);
536 // Add the oat directory.
537 dir += "oat";
538 if (oat_dir != nullptr) {
539 *oat_dir = dir;
540 }
541 // Add the isa directory
542 dir += "/" + std::string(GetInstructionSetString(isa));
543 if (isa_dir != nullptr) {
544 *isa_dir = dir;
545 }
546
547 // Get the base part of the file without the extension.
548 std::string file = location.substr(pos+1);
549 pos = file.rfind('.');
550 if (pos == std::string::npos) {
551 *error_msg = "Dex location " + location + " has no extension.";
552 return false;
553 }
554 std::string base = file.substr(0, pos);
555
556 *odex_filename = dir + "/" + base + ".odex";
557 return true;
558}
559
560// Prepare a subcomponent of the odex directory.
561// (i.e. create and set the expected permissions on the path `dir`).
562static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
563 struct stat dir_stat;
564 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
565 // The directory exists. Check if it is indeed a directory.
566 if (!S_ISDIR(dir_stat.st_mode)) {
567 *error_msg = dir + " is not a dir";
568 return false;
569 } else {
570 // The dir is already on disk.
571 return true;
572 }
573 }
574
575 // Failed to stat. We need to create the directory.
576 if (errno != ENOENT) {
577 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
578 return false;
579 }
580
581 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
582 if (mkdir(dir.c_str(), mode) != 0) {
583 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
584 return false;
585 }
586 if (chmod(dir.c_str(), mode) != 0) {
587 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
588 return false;
589 }
590 return true;
591}
592
593// Prepares the odex directory for the given dex location.
594static bool PrepareOdexDirectories(const std::string& dex_location,
595 const std::string& expected_odex_location,
596 InstructionSet isa,
597 std::string* error_msg) {
598 std::string actual_odex_location;
599 std::string oat_dir;
600 std::string isa_dir;
601 if (!DexLocationToOdexNames(
602 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
603 return false;
604 }
605 DCHECK_EQ(expected_odex_location, actual_odex_location);
606
607 if (!PrepareDirectory(oat_dir, error_msg)) {
608 return false;
609 }
610 if (!PrepareDirectory(isa_dir, error_msg)) {
611 return false;
612 }
613 return true;
614}
615
616OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
617 OatFileAssistant::OatFileInfo& info, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800618 CHECK(error_msg != nullptr);
619
Richard Uhler8327cf72015-10-13 16:34:59 -0700620 Runtime* runtime = Runtime::Current();
621 if (!runtime->IsDex2OatEnabled()) {
622 *error_msg = "Generation of oat file for dex location " + dex_location_
623 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700624 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700625 }
626
Calin Juravle357c66d2017-05-04 01:57:17 +0000627 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700628 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800629 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700630 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800631 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000632 const std::string& oat_file_name = *info.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100633 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800634
Richard Uhler66d874d2015-01-15 09:37:19 -0800635 // dex2oat ignores missing dex files and doesn't report an error.
636 // Check explicitly here so we can detect the error properly.
637 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000638 struct stat dex_path_stat;
639 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
640 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700641 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800642 }
643
Calin Juravle357c66d2017-05-04 01:57:17 +0000644 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
645 if (!info.IsOatLocation()) {
646 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
647 return kUpdateNotAttempted;
648 }
649 }
650
651 // Set the permissions for the oat and the vdex files.
652 // The user always gets read and write while the group and others propagate
653 // the reading access of the original dex file.
654 mode_t file_mode = S_IRUSR | S_IWUSR |
655 (dex_path_stat.st_mode & S_IRGRP) |
656 (dex_path_stat.st_mode & S_IROTH);
657
David Brazdil7b49e6c2016-09-01 11:06:18 +0100658 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
659 if (vdex_file.get() == nullptr) {
660 *error_msg = "Generation of oat file " + oat_file_name
661 + " not attempted because the vdex file " + vdex_file_name
662 + " could not be opened.";
663 return kUpdateNotAttempted;
664 }
665
Calin Juravle357c66d2017-05-04 01:57:17 +0000666 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100667 *error_msg = "Generation of oat file " + oat_file_name
668 + " not attempted because the vdex file " + vdex_file_name
669 + " could not be made world readable.";
670 return kUpdateNotAttempted;
671 }
672
673 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700674 if (oat_file.get() == nullptr) {
675 *error_msg = "Generation of oat file " + oat_file_name
676 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700677 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700678 }
679
Calin Juravle357c66d2017-05-04 01:57:17 +0000680 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700681 *error_msg = "Generation of oat file " + oat_file_name
682 + " not attempted because the oat file could not be made world readable.";
683 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700684 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700685 }
686
687 std::vector<std::string> args;
688 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000689 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700690 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
691 args.push_back("--oat-location=" + oat_file_name);
692
Richard Uhler66d874d2015-01-15 09:37:19 -0800693 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100694 // Manually delete the oat and vdex files. This ensures there is no garbage
695 // left over if the process unexpectedly died.
696 vdex_file->Erase();
697 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700698 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100699 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700700 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700701 }
702
David Brazdil7b49e6c2016-09-01 11:06:18 +0100703 if (vdex_file->FlushCloseOrErase() != 0) {
704 *error_msg = "Unable to close vdex file " + vdex_file_name;
705 unlink(vdex_file_name.c_str());
706 return kUpdateFailed;
707 }
708
Richard Uhler8327cf72015-10-13 16:34:59 -0700709 if (oat_file->FlushCloseOrErase() != 0) {
710 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100711 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700712 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800713 }
714
Calin Juravle357c66d2017-05-04 01:57:17 +0000715 // Mark that the odex file has changed and we should try to reload.
716 info.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700717 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800718}
719
720bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
721 std::string* error_msg) {
722 Runtime* runtime = Runtime::Current();
723 std::string image_location = ImageLocation();
724 if (image_location.empty()) {
725 *error_msg = "No image location found for Dex2Oat.";
726 return false;
727 }
728
729 std::vector<std::string> argv;
730 argv.push_back(runtime->GetCompilerExecutable());
731 argv.push_back("--runtime-arg");
732 argv.push_back("-classpath");
733 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700734 std::string class_path = runtime->GetClassPathString();
735 if (class_path == "") {
736 class_path = OatFile::kSpecialSharedLibrary;
737 }
738 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000739 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200740 argv.push_back("--debuggable");
741 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700742 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800743
744 if (!runtime->IsVerificationEnabled()) {
745 argv.push_back("--compiler-filter=verify-none");
746 }
747
748 if (runtime->MustRelocateIfPossible()) {
749 argv.push_back("--runtime-arg");
750 argv.push_back("-Xrelocate");
751 } else {
752 argv.push_back("--runtime-arg");
753 argv.push_back("-Xnorelocate");
754 }
755
756 if (!kIsTargetBuild) {
757 argv.push_back("--host");
758 }
759
760 argv.push_back("--boot-image=" + image_location);
761
762 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
763 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
764
765 argv.insert(argv.end(), args.begin(), args.end());
766
Andreas Gampe9186ced2016-12-12 14:28:21 -0800767 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800768 return Exec(argv, error_msg);
769}
770
Richard Uhlerb81881d2016-04-19 13:08:04 -0700771bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
772 InstructionSet isa,
773 std::string* odex_filename,
774 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000775 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800776}
777
Richard Uhlerb81881d2016-04-19 13:08:04 -0700778bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
779 InstructionSet isa,
780 std::string* oat_filename,
781 std::string* error_msg) {
782 CHECK(oat_filename != nullptr);
783 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800784
Richard Uhler55b58b62016-08-12 09:05:13 -0700785 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
786 if (cache_dir.empty()) {
787 *error_msg = "Dalvik cache directory does not exist";
788 return false;
789 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700790
791 // TODO: The oat file assistant should be the definitive place for
792 // determining the oat file name from the dex location, not
793 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700794 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800795}
796
Richard Uhler66d874d2015-01-15 09:37:19 -0800797std::string OatFileAssistant::ImageLocation() {
798 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000799 const std::vector<gc::space::ImageSpace*>& image_spaces =
800 runtime->GetHeap()->GetBootImageSpaces();
801 if (image_spaces.empty()) {
802 return "";
803 }
804 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800805}
806
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000807const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
808 if (!required_dex_checksums_attempted_) {
809 required_dex_checksums_attempted_ = true;
810 required_dex_checksums_found_ = false;
811 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800812 std::string error_msg;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000813 if (DexFile::GetMultiDexChecksums(dex_location_.c_str(),
814 &cached_required_dex_checksums_,
815 &error_msg)) {
816 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700817 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800818 } else {
819 // This can happen if the original dex file has been stripped from the
820 // apk.
821 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700822 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800823
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000824 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700825 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800826 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000827 required_dex_checksums_found_ = true;
828 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
829 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
830 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
831 if (odex_dex_file == nullptr) {
832 required_dex_checksums_found_ = false;
833 break;
834 }
835 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800836 }
837 }
838 }
839 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000840 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800841}
842
Richard Uhler95e09672017-02-22 11:37:41 +0000843std::unique_ptr<OatFileAssistant::ImageInfo>
844OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
845 CHECK(error_msg != nullptr);
846
Richard Uhler95e09672017-02-22 11:37:41 +0000847 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000848 std::unique_ptr<ImageInfo> info(new ImageInfo());
849 info->location = runtime->GetImageLocation();
850
851 std::unique_ptr<ImageHeader> image_header(
852 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
853 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000854 return nullptr;
855 }
856
Richard Uhler8f104862017-02-22 12:34:01 +0000857 info->oat_checksum = image_header->GetOatChecksum();
858 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
859 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000860 return info;
861}
862
Richard Uhler66d874d2015-01-15 09:37:19 -0800863const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
864 if (!image_info_load_attempted_) {
865 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000866 std::string error_msg;
867 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
868 if (cached_image_info_ == nullptr) {
869 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800870 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800871 }
Richard Uhler95e09672017-02-22 11:37:41 +0000872 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800873}
874
Richard Uhler88bc6732016-11-14 14:38:03 +0000875OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000876 // TODO(calin): Document the side effects of class loading when
877 // running dalvikvm command line.
878 if (dex_parent_writable_) {
879 // If the parent of the dex file is writable it means that we can
880 // create the odex file. In this case we unconditionally pick the odex
881 // as the best oat file. This corresponds to the regular use case when
882 // apps gets installed or when they load private, secondary dex file.
883 // For apps on the system partition the odex location will not be
884 // writable and thus the oat location might be more up to date.
885 return odex_;
886 }
887
888 // We cannot write to the odex location. This must be a system app.
889
890 // If the oat location is usable take it.
891 if (oat_.IsUseable()) {
892 return oat_;
893 }
894
895 // The oat file is not usable but the odex file might be up to date.
896 // This is an indication that we are dealing with an up to date prebuilt
897 // (that doesn't need relocation).
898 if (odex_.Status() == kOatUpToDate) {
899 return odex_;
900 }
901
902 // The oat file is not usable and the odex file is not up to date.
903 // However we have access to the original dex file which means we can make
904 // the oat location up to date.
905 if (HasOriginalDexFiles()) {
906 return oat_;
907 }
908
909 // We got into the worst situation here:
910 // - the oat location is not usable
911 // - the prebuild odex location is not up to date
912 // - and we don't have the original dex file anymore (stripped).
913 // Pick the odex if it exists, or the oat if not.
914 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000915}
916
Andreas Gampea463b6a2016-08-12 21:53:32 -0700917std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800918 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100919 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800920 if (art_file.empty()) {
921 return nullptr;
922 }
923 std::string error_msg;
924 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700925 std::unique_ptr<gc::space::ImageSpace> ret =
926 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800927 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800928 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
929 }
930 return ret;
931}
932
Richard Uhler88bc6732016-11-14 14:38:03 +0000933OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
934 bool is_oat_location)
935 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700936{}
937
Richard Uhler88bc6732016-11-14 14:38:03 +0000938bool OatFileAssistant::OatFileInfo::IsOatLocation() {
939 return is_oat_location_;
940}
941
Richard Uhler743bf362016-04-19 15:39:37 -0700942const std::string* OatFileAssistant::OatFileInfo::Filename() {
943 return filename_provided_ ? &filename_ : nullptr;
944}
945
Richard Uhler03bc6592016-11-22 09:42:04 +0000946bool OatFileAssistant::OatFileInfo::IsUseable() {
947 switch (Status()) {
948 case kOatCannotOpen:
949 case kOatDexOutOfDate:
950 case kOatBootImageOutOfDate: return false;
951
952 case kOatRelocationOutOfDate:
953 case kOatUpToDate: return true;
954 }
955 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700956}
957
958OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
959 if (!status_attempted_) {
960 status_attempted_ = true;
961 const OatFile* file = GetFile();
962 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000963 // Check to see if there is a vdex file we can make use of.
964 std::string error_msg;
965 std::string vdex_filename = ReplaceFileExtension(filename_, "vdex");
966 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
967 /*writeable*/false,
968 /*low_4gb*/false,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100969 /*unquicken*/false,
Richard Uhler2f27abd2017-01-31 14:02:34 +0000970 &error_msg);
971 if (vdex == nullptr) {
972 status_ = kOatCannotOpen;
973 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
974 } else {
975 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
976 // The vdex file does not contain enough information to determine
977 // whether it is up to date with respect to the boot image, so we
978 // assume it is out of date.
979 VLOG(oat) << error_msg;
980 status_ = kOatBootImageOutOfDate;
981 } else {
982 status_ = kOatDexOutOfDate;
983 }
984 }
Richard Uhler743bf362016-04-19 15:39:37 -0700985 } else {
986 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700987 VLOG(oat) << file->GetLocation() << " is " << status_
988 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700989 }
990 }
991 return status_;
992}
993
Richard Uhler70a84262016-11-08 16:51:51 +0000994OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
995 CompilerFilter::Filter target, bool profile_changed) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100996 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000997 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000998
Richard Uhler7225a8d2016-11-22 10:12:03 +0000999 if (filter_okay && Status() == kOatUpToDate) {
1000 // The oat file is in good shape as is.
1001 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +00001002 }
1003
Richard Uhler7225a8d2016-11-22 10:12:03 +00001004 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1005 // If no compilation is desired, then it doesn't matter if the oat
1006 // file needs relocation. It's in good shape as is.
1007 return kNoDexOptNeeded;
1008 }
1009
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001010 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1011 return kDex2OatForRelocation;
Richard Uhler7225a8d2016-11-22 10:12:03 +00001012 }
1013
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001014 if (IsUseable()) {
1015 return kDex2OatForFilter;
1016 }
1017
1018 if (Status() == kOatBootImageOutOfDate) {
1019 return kDex2OatForBootImage;
1020 }
1021
1022 if (oat_file_assistant_->HasOriginalDexFiles()) {
1023 return kDex2OatFromScratch;
1024 } else {
1025 // Otherwise there is nothing we can do, even if we want to.
1026 return kNoDexOptNeeded;
1027 }
Richard Uhler70a84262016-11-08 16:51:51 +00001028}
1029
Richard Uhler743bf362016-04-19 15:39:37 -07001030const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1031 CHECK(!file_released_) << "GetFile called after oat file released.";
1032 if (!load_attempted_) {
1033 load_attempted_ = true;
1034 if (filename_provided_) {
1035 std::string error_msg;
1036 file_.reset(OatFile::Open(filename_.c_str(),
1037 filename_.c_str(),
1038 nullptr,
1039 nullptr,
1040 oat_file_assistant_->load_executable_,
1041 /*low_4gb*/false,
1042 oat_file_assistant_->dex_location_.c_str(),
1043 &error_msg));
1044 if (file_.get() == nullptr) {
1045 VLOG(oat) << "OatFileAssistant test for existing oat file "
1046 << filename_ << ": " << error_msg;
1047 }
1048 }
1049 }
1050 return file_.get();
1051}
1052
1053bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
1054 CompilerFilter::Filter target, bool profile_changed) {
1055 const OatFile* file = GetFile();
1056 if (file == nullptr) {
1057 return false;
1058 }
1059
1060 CompilerFilter::Filter current = file->GetCompilerFilter();
1061 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1062 VLOG(oat) << "Compiler filter not okay because Profile changed";
1063 return false;
1064 }
1065 return CompilerFilter::IsAsGoodAs(current, target);
1066}
1067
1068bool OatFileAssistant::OatFileInfo::IsExecutable() {
1069 const OatFile* file = GetFile();
1070 return (file != nullptr && file->IsExecutable());
1071}
1072
Richard Uhler743bf362016-04-19 15:39:37 -07001073void OatFileAssistant::OatFileInfo::Reset() {
1074 load_attempted_ = false;
1075 file_.reset();
1076 status_attempted_ = false;
1077}
1078
1079void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
1080 filename_provided_ = true;
1081 filename_ = filename;
1082 Reset();
1083}
1084
1085std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1086 file_released_ = true;
1087 return std::move(file_);
1088}
1089
Richard Uhler70a84262016-11-08 16:51:51 +00001090std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001091 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001092 return ReleaseFile();
1093 }
1094
1095 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1096 << " attempting to fall back to interpreting oat file instead.";
1097
Richard Uhler03bc6592016-11-22 09:42:04 +00001098 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001099 return ReleaseFile();
1100 }
1101
Richard Uhler03bc6592016-11-22 09:42:04 +00001102 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001103 // We are loading an oat file for runtime use that needs relocation.
1104 // Reload the file non-executable to ensure that we interpret out of the
1105 // dex code in the oat file rather than trying to execute the unrelocated
1106 // compiled code.
1107 oat_file_assistant_->load_executable_ = false;
1108 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001109 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001110 CHECK(!IsExecutable());
1111 return ReleaseFile();
1112 }
1113 }
1114 return std::unique_ptr<OatFile>();
1115}
Richard Uhler66d874d2015-01-15 09:37:19 -08001116} // namespace art