blob: 5ae2fc51b7e01845ee5618b697e9a695b471cc46 [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
23#include "android-base/strings.h"
24
Richard Uhler66d874d2015-01-15 09:37:19 -080025#include "base/logging.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010026#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080027#include "class_linker.h"
David Sehr97c381e2017-02-01 15:09:58 -080028#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "gc/heap.h"
30#include "gc/space/image_space.h"
31#include "image.h"
32#include "oat.h"
33#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000037#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038
39namespace art {
40
Richard Uhler69bcf2c2017-01-24 10:25:21 +000041using android::base::StringPrintf;
42
Narayan Kamath8943c1d2016-05-02 13:14:48 +010043std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
44 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000045 case OatFileAssistant::kOatCannotOpen:
46 stream << "kOatCannotOpen";
47 break;
48 case OatFileAssistant::kOatDexOutOfDate:
49 stream << "kOatDexOutOfDate";
50 break;
51 case OatFileAssistant::kOatBootImageOutOfDate:
52 stream << "kOatBootImageOutOfDate";
53 break;
54 case OatFileAssistant::kOatRelocationOutOfDate:
55 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010056 break;
57 case OatFileAssistant::kOatUpToDate:
58 stream << "kOatUpToDate";
59 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010060 default:
61 UNREACHABLE();
62 }
63
64 return stream;
65}
66
Richard Uhler66d874d2015-01-15 09:37:19 -080067OatFileAssistant::OatFileAssistant(const char* dex_location,
68 const InstructionSet isa,
69 bool load_executable)
Richard Uhlerd1472a22016-04-15 15:18:56 -070070 : OatFileAssistant(dex_location, nullptr, isa, load_executable)
Calin Juravleb077e152016-02-18 18:47:37 +000071{ }
Richard Uhler66d874d2015-01-15 09:37:19 -080072
73OatFileAssistant::OatFileAssistant(const char* dex_location,
74 const char* oat_location,
75 const InstructionSet isa,
76 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000077 : isa_(isa),
78 load_executable_(load_executable),
79 odex_(this, /*is_oat_location*/ false),
80 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070081 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
82 dex_location_.assign(dex_location);
83
Richard Uhler66d874d2015-01-15 09:37:19 -080084 if (load_executable_ && isa != kRuntimeISA) {
85 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
86 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
87 load_executable_ = false;
88 }
89
Richard Uhler743bf362016-04-19 15:39:37 -070090 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -070091 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -070092 std::string odex_file_name;
93 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
94 odex_.Reset(odex_file_name);
95 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -070096 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
97 }
98
Richard Uhler743bf362016-04-19 15:39:37 -070099 // Get the oat filename.
Richard Uhler66d874d2015-01-15 09:37:19 -0800100 if (oat_location != nullptr) {
Richard Uhler743bf362016-04-19 15:39:37 -0700101 oat_.Reset(oat_location);
Richard Uhlerd684f522016-04-19 13:24:41 -0700102 } else {
Richard Uhler743bf362016-04-19 15:39:37 -0700103 std::string oat_file_name;
104 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
105 oat_.Reset(oat_file_name);
106 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700107 LOG(WARNING) << "Failed to determine oat file name for dex location "
108 << dex_location_ << ": " << error_msg;
109 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800110 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800111}
112
113OatFileAssistant::~OatFileAssistant() {
114 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700115 if (flock_.HasFile()) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100116 unlink(flock_.GetFile()->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800117 }
118}
119
120bool OatFileAssistant::IsInBootClassPath() {
121 // Note: We check the current boot class path, regardless of the ISA
122 // specified by the user. This is okay, because the boot class path should
123 // be the same for all ISAs.
124 // TODO: Can we verify the boot class path is the same for all ISAs?
125 Runtime* runtime = Runtime::Current();
126 ClassLinker* class_linker = runtime->GetClassLinker();
127 const auto& boot_class_path = class_linker->GetBootClassPath();
128 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700129 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800130 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
131 return true;
132 }
133 }
134 return false;
135}
136
137bool OatFileAssistant::Lock(std::string* error_msg) {
138 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700139 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800140
Richard Uhler743bf362016-04-19 15:39:37 -0700141 const std::string* oat_file_name = oat_.Filename();
142 if (oat_file_name == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800143 *error_msg = "Failed to determine lock file";
144 return false;
145 }
Richard Uhler743bf362016-04-19 15:39:37 -0700146 std::string lock_file_name = *oat_file_name + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800147
Richard Uhler581f4e92015-05-07 10:19:35 -0700148 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100149 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800150 return false;
151 }
152 return true;
153}
154
Richard Uhler7225a8d2016-11-22 10:12:03 +0000155int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000156 OatFileInfo& info = GetBestInfo();
157 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000158 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
159 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800160 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000161 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800162}
163
Richard Uhlerf4b34872016-04-13 11:03:46 -0700164// Figure out the currently specified compile filter option in the runtime.
165// Returns true on success, false if the compiler filter is invalid, in which
166// case error_msg describes the problem.
167static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
168 std::string* error_msg) {
169 CHECK(filter != nullptr);
170 CHECK(error_msg != nullptr);
171
172 *filter = CompilerFilter::kDefaultCompilerFilter;
173 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
174 if (option.starts_with("--compiler-filter=")) {
175 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
176 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
177 *error_msg = std::string("Unknown --compiler-filter value: ")
178 + std::string(compiler_filter_string);
179 return false;
180 }
181 }
182 }
183 return true;
184}
185
Richard Uhler01be6812016-05-17 10:34:52 -0700186bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000187 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700188}
189
Richard Uhler1e860612016-03-30 12:17:55 -0700190OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700191OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700192 CompilerFilter::Filter target;
193 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
194 return kUpdateNotAttempted;
195 }
196
Richard Uhler88bc6732016-11-14 14:38:03 +0000197 OatFileInfo& info = GetBestInfo();
198 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000199 case kNoDexOptNeeded:
200 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000201
Richard Uhler7225a8d2016-11-22 10:12:03 +0000202 // TODO: For now, don't bother with all the different ways we can call
203 // dex2oat to generate the oat file. Always generate the oat file as if it
204 // were kDex2OatFromScratch.
205 case kDex2OatFromScratch:
206 case kDex2OatForBootImage:
207 case kDex2OatForRelocation:
208 case kDex2OatForFilter:
209 return GenerateOatFile(error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800210 }
211 UNREACHABLE();
212}
213
214std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000215 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800216}
217
Richard Uhler46cc64f2016-11-14 14:53:55 +0000218std::string OatFileAssistant::GetStatusDump() {
219 std::ostringstream status;
220 bool oat_file_exists = false;
221 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000222 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000223 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000224 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000225
Richard Uhler46cc64f2016-11-14 14:53:55 +0000226 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000227 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
228 const OatFile* file = oat_.GetFile();
229 if (file == nullptr) {
230 // If the file is null even though the status is not kOatCannotOpen, it
231 // means we must have a vdex file with no corresponding oat file. In
232 // this case we cannot determine the compilation filter. Indicate that
233 // we have only the vdex file instead.
234 status << "vdex-only";
235 } else {
236 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
237 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000238 }
239
Richard Uhler03bc6592016-11-22 09:42:04 +0000240 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000241 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000242 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000243
Richard Uhler46cc64f2016-11-14 14:53:55 +0000244 odex_file_exists = true;
245 if (oat_file_exists) {
246 status << "] ";
247 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000248 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
249 const OatFile* file = odex_.GetFile();
250 if (file == nullptr) {
251 status << "vdex-only";
252 } else {
253 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
254 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000255 }
256
257 if (!oat_file_exists && !odex_file_exists) {
258 status << "invalid[";
259 }
260
261 status << "]";
262 return status.str();
263}
264
Richard Uhler66d874d2015-01-15 09:37:19 -0800265std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
266 const OatFile& oat_file, const char* dex_location) {
267 std::vector<std::unique_ptr<const DexFile>> dex_files;
268
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000269 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800270 std::string error_msg;
271 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700272 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800273 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700274 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800275 return std::vector<std::unique_ptr<const DexFile>>();
276 }
277
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700278 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800279 if (dex_file.get() == nullptr) {
280 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
281 return std::vector<std::unique_ptr<const DexFile>>();
282 }
283 dex_files.push_back(std::move(dex_file));
284
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000285 // Load the rest of the multidex entries
Andreas Gampe90e34042015-04-27 20:01:52 -0700286 for (size_t i = 1; ; i++) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000287 std::string multidex_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
288 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700289 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000290 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800291 break;
292 }
293
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700294 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800295 if (dex_file.get() == nullptr) {
296 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
297 return std::vector<std::unique_ptr<const DexFile>>();
298 }
299 dex_files.push_back(std::move(dex_file));
300 }
301 return dex_files;
302}
303
Richard Uhler9b994ea2015-06-24 08:44:19 -0700304bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000305 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700306 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000307 // GetRequiredDexChecksums.
308 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700309 return has_original_dex_files_;
310}
311
Richard Uhler95abd042015-03-24 09:51:28 -0700312OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700313 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800314}
315
Richard Uhler95abd042015-03-24 09:51:28 -0700316OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700317 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800318}
319
Richard Uhler2f27abd2017-01-31 14:02:34 +0000320bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000321 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
322 if (required_dex_checksums == nullptr) {
323 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
324 return true;
325 }
326
327 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
328 if (required_dex_checksums->size() != number_of_dex_files) {
329 *error_msg = StringPrintf("expected %zu dex files but found %u",
330 required_dex_checksums->size(),
331 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000332 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800333 }
334
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000335 for (uint32_t i = 0; i < number_of_dex_files; i++) {
336 uint32_t expected_checksum = (*required_dex_checksums)[i];
337 uint32_t actual_checksum = file.GetLocationChecksum(i);
338 if (expected_checksum != actual_checksum) {
339 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
340 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
341 "Expected: %u, actual: %u",
342 dex.c_str(),
343 expected_checksum,
344 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000345 return false;
346 }
347 }
348
Richard Uhler2f27abd2017-01-31 14:02:34 +0000349 return true;
350}
351
352bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000353 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
354 if (required_dex_checksums == nullptr) {
355 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
356 return true;
357 }
358
359 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
360 if (required_dex_checksums->size() != number_of_dex_files) {
361 *error_msg = StringPrintf("expected %zu dex files but found %u",
362 required_dex_checksums->size(),
363 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000364 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800365 }
366
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000367 for (uint32_t i = 0; i < number_of_dex_files; i++) {
368 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
369 uint32_t expected_checksum = (*required_dex_checksums)[i];
370 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
371 if (oat_dex_file == nullptr) {
372 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
373 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000375 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
376 if (expected_checksum != actual_checksum) {
377 VLOG(oat) << "Dex checksum does not match for dex: " << dex
378 << ". Expected: " << expected_checksum
379 << ", Actual: " << actual_checksum;
380 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 }
382 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000383 return true;
384}
385
386OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
387 // Verify the ART_USE_READ_BARRIER state.
388 // TODO: Don't fully reject files due to read barrier state. If they contain
389 // compiled code and are otherwise okay, we should return something like
390 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
391 // barrier state doesn't matter.
392 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
393 constexpr bool kRuntimeIsCC = kUseReadBarrier;
394 if (is_cc != kRuntimeIsCC) {
395 return kOatCannotOpen;
396 }
397
398 // Verify the dex checksum.
399 std::string error_msg;
400 if (kIsVdexEnabled) {
401 VdexFile* vdex = file.GetVdexFile();
402 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
403 LOG(ERROR) << error_msg;
404 return kOatDexOutOfDate;
405 }
406 } else {
407 if (!DexChecksumUpToDate(file, &error_msg)) {
408 LOG(ERROR) << error_msg;
409 return kOatDexOutOfDate;
410 }
411 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800412
Andreas Gampe29d38e72016-03-23 15:31:51 +0000413 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000414
Richard Uhler66d874d2015-01-15 09:37:19 -0800415 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000416 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
417 const ImageInfo* image_info = GetImageInfo();
418 if (image_info == nullptr) {
419 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000420
Richard Uhler76f5cb62016-04-04 13:30:16 -0700421 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000422 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700423 }
424
425 // If there is no original dex file to fall back to, grudgingly accept
426 // the oat file. This could technically lead to crashes, but there's no
427 // way we could find a better oat file to use for this dex location,
428 // and it's better than being stuck in a boot loop with no way out.
429 // The problem will hopefully resolve itself the next time the runtime
430 // starts up.
431 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
432 << "Allow oat file use. This is potentially dangerous.";
433 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum()
434 != GetCombinedImageChecksum()) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000435 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000436 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000437 }
438 } else {
439 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800440 }
441
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100442 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000443 if (!file.IsPic()) {
444 const ImageInfo* image_info = GetImageInfo();
445 if (image_info == nullptr) {
446 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000447 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000448 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700449
Andreas Gampe29d38e72016-03-23 15:31:51 +0000450 // Verify the oat_data_begin recorded for the image in the oat file matches
451 // the actual oat_data_begin for boot.oat in the image.
452 const OatHeader& oat_header = file.GetOatHeader();
453 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
454 if (oat_data_begin != image_info->oat_data_begin) {
455 VLOG(oat) << file.GetLocation() <<
456 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
457 << " does not match actual image oat_data_begin ("
458 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000459 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000460 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700461
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 // Verify the oat_patch_delta recorded for the image in the oat file matches
463 // the actual oat_patch_delta for the image.
464 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
465 if (oat_patch_delta != image_info->patch_delta) {
466 VLOG(oat) << file.GetLocation() <<
467 ": Oat file image patch delta (" << oat_patch_delta << ")"
468 << " does not match actual image patch delta ("
469 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000470 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000471 }
472 } else {
473 // Oat files compiled in PIC mode do not require relocation.
474 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
475 }
476 } else {
477 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800478 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700479 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800480}
481
Richard Uhler1e860612016-03-30 12:17:55 -0700482OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700483OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 CHECK(error_msg != nullptr);
485
Richard Uhler8327cf72015-10-13 16:34:59 -0700486 Runtime* runtime = Runtime::Current();
487 if (!runtime->IsDex2OatEnabled()) {
488 *error_msg = "Generation of oat file for dex location " + dex_location_
489 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700490 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700491 }
492
Richard Uhler743bf362016-04-19 15:39:37 -0700493 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700494 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800495 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700496 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 }
Richard Uhler743bf362016-04-19 15:39:37 -0700498 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100499 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800500
Richard Uhler66d874d2015-01-15 09:37:19 -0800501 // dex2oat ignores missing dex files and doesn't report an error.
502 // Check explicitly here so we can detect the error properly.
503 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700504 if (!OS::FileExists(dex_location_.c_str())) {
505 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700506 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800507 }
508
David Brazdil7b49e6c2016-09-01 11:06:18 +0100509 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
510 if (vdex_file.get() == nullptr) {
511 *error_msg = "Generation of oat file " + oat_file_name
512 + " not attempted because the vdex file " + vdex_file_name
513 + " could not be opened.";
514 return kUpdateNotAttempted;
515 }
516
517 if (fchmod(vdex_file->Fd(), 0644) != 0) {
518 *error_msg = "Generation of oat file " + oat_file_name
519 + " not attempted because the vdex file " + vdex_file_name
520 + " could not be made world readable.";
521 return kUpdateNotAttempted;
522 }
523
524 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700525 if (oat_file.get() == nullptr) {
526 *error_msg = "Generation of oat file " + oat_file_name
527 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700528 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700529 }
530
531 if (fchmod(oat_file->Fd(), 0644) != 0) {
532 *error_msg = "Generation of oat file " + oat_file_name
533 + " not attempted because the oat file could not be made world readable.";
534 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700535 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700536 }
537
538 std::vector<std::string> args;
539 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000540 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700541 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
542 args.push_back("--oat-location=" + oat_file_name);
543
Richard Uhler66d874d2015-01-15 09:37:19 -0800544 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100545 // Manually delete the oat and vdex files. This ensures there is no garbage
546 // left over if the process unexpectedly died.
547 vdex_file->Erase();
548 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700549 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100550 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700551 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700552 }
553
David Brazdil7b49e6c2016-09-01 11:06:18 +0100554 if (vdex_file->FlushCloseOrErase() != 0) {
555 *error_msg = "Unable to close vdex file " + vdex_file_name;
556 unlink(vdex_file_name.c_str());
557 return kUpdateFailed;
558 }
559
Richard Uhler8327cf72015-10-13 16:34:59 -0700560 if (oat_file->FlushCloseOrErase() != 0) {
561 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100562 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700563 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800564 }
565
566 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700567 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700568 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800569}
570
571bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
572 std::string* error_msg) {
573 Runtime* runtime = Runtime::Current();
574 std::string image_location = ImageLocation();
575 if (image_location.empty()) {
576 *error_msg = "No image location found for Dex2Oat.";
577 return false;
578 }
579
580 std::vector<std::string> argv;
581 argv.push_back(runtime->GetCompilerExecutable());
582 argv.push_back("--runtime-arg");
583 argv.push_back("-classpath");
584 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700585 std::string class_path = runtime->GetClassPathString();
586 if (class_path == "") {
587 class_path = OatFile::kSpecialSharedLibrary;
588 }
589 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000590 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200591 argv.push_back("--debuggable");
592 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700593 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800594
595 if (!runtime->IsVerificationEnabled()) {
596 argv.push_back("--compiler-filter=verify-none");
597 }
598
599 if (runtime->MustRelocateIfPossible()) {
600 argv.push_back("--runtime-arg");
601 argv.push_back("-Xrelocate");
602 } else {
603 argv.push_back("--runtime-arg");
604 argv.push_back("-Xnorelocate");
605 }
606
607 if (!kIsTargetBuild) {
608 argv.push_back("--host");
609 }
610
611 argv.push_back("--boot-image=" + image_location);
612
613 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
614 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
615
616 argv.insert(argv.end(), args.begin(), args.end());
617
Andreas Gampe9186ced2016-12-12 14:28:21 -0800618 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800619 return Exec(argv, error_msg);
620}
621
Richard Uhlerb81881d2016-04-19 13:08:04 -0700622bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
623 InstructionSet isa,
624 std::string* odex_filename,
625 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800626 CHECK(odex_filename != nullptr);
627 CHECK(error_msg != nullptr);
628
629 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700630 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800631 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700632 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800633
Richard Uhler63434112015-03-16 14:32:16 -0700634 // Find the directory portion of the dex location and add the oat/<isa>
635 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800636 size_t pos = location.rfind('/');
637 if (pos == std::string::npos) {
638 *error_msg = "Dex location " + location + " has no directory.";
639 return false;
640 }
641 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700642 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800643
644 // Find the file portion of the dex location.
645 std::string file;
646 if (pos == std::string::npos) {
647 file = location;
648 } else {
649 file = location.substr(pos+1);
650 }
651
652 // Get the base part of the file without the extension.
653 pos = file.rfind('.');
654 if (pos == std::string::npos) {
655 *error_msg = "Dex location " + location + " has no extension.";
656 return false;
657 }
658 std::string base = file.substr(0, pos);
659
660 *odex_filename = dir + "/" + base + ".odex";
661 return true;
662}
663
Richard Uhlerb81881d2016-04-19 13:08:04 -0700664bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
665 InstructionSet isa,
666 std::string* oat_filename,
667 std::string* error_msg) {
668 CHECK(oat_filename != nullptr);
669 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800670
Richard Uhler55b58b62016-08-12 09:05:13 -0700671 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
672 if (cache_dir.empty()) {
673 *error_msg = "Dalvik cache directory does not exist";
674 return false;
675 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700676
677 // TODO: The oat file assistant should be the definitive place for
678 // determining the oat file name from the dex location, not
679 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700680 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800681}
682
Richard Uhler66d874d2015-01-15 09:37:19 -0800683std::string OatFileAssistant::ImageLocation() {
684 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000685 const std::vector<gc::space::ImageSpace*>& image_spaces =
686 runtime->GetHeap()->GetBootImageSpaces();
687 if (image_spaces.empty()) {
688 return "";
689 }
690 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800691}
692
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000693const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
694 if (!required_dex_checksums_attempted_) {
695 required_dex_checksums_attempted_ = true;
696 required_dex_checksums_found_ = false;
697 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800698 std::string error_msg;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000699 if (DexFile::GetMultiDexChecksums(dex_location_.c_str(),
700 &cached_required_dex_checksums_,
701 &error_msg)) {
702 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700703 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800704 } else {
705 // This can happen if the original dex file has been stripped from the
706 // apk.
707 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700708 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800709
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000710 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700711 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800712 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000713 required_dex_checksums_found_ = true;
714 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
715 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
716 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
717 if (odex_dex_file == nullptr) {
718 required_dex_checksums_found_ = false;
719 break;
720 }
721 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800722 }
723 }
724 }
725 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000726 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800727}
728
Richard Uhler66d874d2015-01-15 09:37:19 -0800729const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
730 if (!image_info_load_attempted_) {
731 image_info_load_attempted_ = true;
732
733 Runtime* runtime = Runtime::Current();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800734 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
735 if (!image_spaces.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800736 cached_image_info_.location = image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800737
738 if (isa_ == kRuntimeISA) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800739 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler66d874d2015-01-15 09:37:19 -0800740 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800741 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
742 image_header.GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800743 cached_image_info_.patch_delta = image_header.GetPatchDelta();
744 } else {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700745 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800746 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700747 gc::space::ImageSpace::ReadImageHeader(cached_image_info_.location.c_str(),
748 isa_,
749 &error_msg));
750 CHECK(image_header != nullptr) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800751 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800752 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(
753 image_header->GetOatDataBegin());
Richard Uhler66d874d2015-01-15 09:37:19 -0800754 cached_image_info_.patch_delta = image_header->GetPatchDelta();
755 }
756 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800757 image_info_load_succeeded_ = (!image_spaces.empty());
Jeff Haob11ffb72016-04-07 15:40:54 -0700758
Jeff Haofd336c32016-04-07 19:46:31 -0700759 combined_image_checksum_ = CalculateCombinedImageChecksum(isa_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800760 }
761 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
762}
763
Jeff Haob11ffb72016-04-07 15:40:54 -0700764// TODO: Use something better than xor.
Jeff Haofd336c32016-04-07 19:46:31 -0700765uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) {
Jeff Haob11ffb72016-04-07 15:40:54 -0700766 uint32_t checksum = 0;
767 std::vector<gc::space::ImageSpace*> image_spaces =
768 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haofd336c32016-04-07 19:46:31 -0700769 if (isa == kRuntimeISA) {
770 for (gc::space::ImageSpace* image_space : image_spaces) {
771 checksum ^= image_space->GetImageHeader().GetOatChecksum();
772 }
773 } else {
774 for (gc::space::ImageSpace* image_space : image_spaces) {
775 std::string location = image_space->GetImageLocation();
Andreas Gampea463b6a2016-08-12 21:53:32 -0700776 std::string error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700777 std::unique_ptr<ImageHeader> image_header(
Andreas Gampea463b6a2016-08-12 21:53:32 -0700778 gc::space::ImageSpace::ReadImageHeader(location.c_str(), isa, &error_msg));
779 CHECK(image_header != nullptr) << error_msg;
Jeff Haofd336c32016-04-07 19:46:31 -0700780 checksum ^= image_header->GetOatChecksum();
781 }
Jeff Haob11ffb72016-04-07 15:40:54 -0700782 }
783 return checksum;
784}
785
786uint32_t OatFileAssistant::GetCombinedImageChecksum() {
787 if (!image_info_load_attempted_) {
788 GetImageInfo();
789 }
790 return combined_image_checksum_;
791}
792
Richard Uhler88bc6732016-11-14 14:38:03 +0000793OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000794 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
795 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000796}
797
Andreas Gampea463b6a2016-08-12 21:53:32 -0700798std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800799 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100800 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800801 if (art_file.empty()) {
802 return nullptr;
803 }
804 std::string error_msg;
805 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700806 std::unique_ptr<gc::space::ImageSpace> ret =
807 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800808 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800809 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
810 }
811 return ret;
812}
813
Richard Uhler88bc6732016-11-14 14:38:03 +0000814OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
815 bool is_oat_location)
816 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700817{}
818
Richard Uhler88bc6732016-11-14 14:38:03 +0000819bool OatFileAssistant::OatFileInfo::IsOatLocation() {
820 return is_oat_location_;
821}
822
Richard Uhler743bf362016-04-19 15:39:37 -0700823const std::string* OatFileAssistant::OatFileInfo::Filename() {
824 return filename_provided_ ? &filename_ : nullptr;
825}
826
Richard Uhler03bc6592016-11-22 09:42:04 +0000827bool OatFileAssistant::OatFileInfo::IsUseable() {
828 switch (Status()) {
829 case kOatCannotOpen:
830 case kOatDexOutOfDate:
831 case kOatBootImageOutOfDate: return false;
832
833 case kOatRelocationOutOfDate:
834 case kOatUpToDate: return true;
835 }
836 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700837}
838
839OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
840 if (!status_attempted_) {
841 status_attempted_ = true;
842 const OatFile* file = GetFile();
843 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000844 // Check to see if there is a vdex file we can make use of.
845 std::string error_msg;
846 std::string vdex_filename = ReplaceFileExtension(filename_, "vdex");
847 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
848 /*writeable*/false,
849 /*low_4gb*/false,
850 &error_msg);
851 if (vdex == nullptr) {
852 status_ = kOatCannotOpen;
853 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
854 } else {
855 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
856 // The vdex file does not contain enough information to determine
857 // whether it is up to date with respect to the boot image, so we
858 // assume it is out of date.
859 VLOG(oat) << error_msg;
860 status_ = kOatBootImageOutOfDate;
861 } else {
862 status_ = kOatDexOutOfDate;
863 }
864 }
Richard Uhler743bf362016-04-19 15:39:37 -0700865 } else {
866 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700867 VLOG(oat) << file->GetLocation() << " is " << status_
868 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700869 }
870 }
871 return status_;
872}
873
Richard Uhler70a84262016-11-08 16:51:51 +0000874OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
875 CompilerFilter::Filter target, bool profile_changed) {
876 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000877 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000878
Richard Uhler7225a8d2016-11-22 10:12:03 +0000879 if (filter_okay && Status() == kOatUpToDate) {
880 // The oat file is in good shape as is.
881 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000882 }
883
Richard Uhler7225a8d2016-11-22 10:12:03 +0000884 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
885 // If no compilation is desired, then it doesn't matter if the oat
886 // file needs relocation. It's in good shape as is.
887 return kNoDexOptNeeded;
888 }
889
Richard Uhler7225a8d2016-11-22 10:12:03 +0000890 if (oat_file_assistant_->HasOriginalDexFiles()) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000891 if (filter_okay && Status() == kOatRelocationOutOfDate) {
892 return kDex2OatForRelocation;
893 }
894
895 if (IsUseable()) {
896 return kDex2OatForFilter;
897 }
898
899 if (Status() == kOatBootImageOutOfDate) {
900 return kDex2OatForBootImage;
901 }
902
903 return kDex2OatFromScratch;
904 }
905
906 // Otherwise there is nothing we can do, even if we want to.
907 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000908}
909
Richard Uhler743bf362016-04-19 15:39:37 -0700910const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
911 CHECK(!file_released_) << "GetFile called after oat file released.";
912 if (!load_attempted_) {
913 load_attempted_ = true;
914 if (filename_provided_) {
915 std::string error_msg;
916 file_.reset(OatFile::Open(filename_.c_str(),
917 filename_.c_str(),
918 nullptr,
919 nullptr,
920 oat_file_assistant_->load_executable_,
921 /*low_4gb*/false,
922 oat_file_assistant_->dex_location_.c_str(),
923 &error_msg));
924 if (file_.get() == nullptr) {
925 VLOG(oat) << "OatFileAssistant test for existing oat file "
926 << filename_ << ": " << error_msg;
927 }
928 }
929 }
930 return file_.get();
931}
932
933bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
934 CompilerFilter::Filter target, bool profile_changed) {
935 const OatFile* file = GetFile();
936 if (file == nullptr) {
937 return false;
938 }
939
940 CompilerFilter::Filter current = file->GetCompilerFilter();
941 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
942 VLOG(oat) << "Compiler filter not okay because Profile changed";
943 return false;
944 }
945 return CompilerFilter::IsAsGoodAs(current, target);
946}
947
948bool OatFileAssistant::OatFileInfo::IsExecutable() {
949 const OatFile* file = GetFile();
950 return (file != nullptr && file->IsExecutable());
951}
952
Richard Uhler743bf362016-04-19 15:39:37 -0700953void OatFileAssistant::OatFileInfo::Reset() {
954 load_attempted_ = false;
955 file_.reset();
956 status_attempted_ = false;
957}
958
959void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
960 filename_provided_ = true;
961 filename_ = filename;
962 Reset();
963}
964
965std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
966 file_released_ = true;
967 return std::move(file_);
968}
969
Richard Uhler70a84262016-11-08 16:51:51 +0000970std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000971 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000972 return ReleaseFile();
973 }
974
975 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
976 << " attempting to fall back to interpreting oat file instead.";
977
Richard Uhler03bc6592016-11-22 09:42:04 +0000978 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000979 return ReleaseFile();
980 }
981
Richard Uhler03bc6592016-11-22 09:42:04 +0000982 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000983 // We are loading an oat file for runtime use that needs relocation.
984 // Reload the file non-executable to ensure that we interpret out of the
985 // dex code in the oat file rather than trying to execute the unrelocated
986 // compiled code.
987 oat_file_assistant_->load_executable_ = false;
988 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +0000989 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000990 CHECK(!IsExecutable());
991 return ReleaseFile();
992 }
993 }
994 return std::unique_ptr<OatFile>();
995}
Richard Uhler66d874d2015-01-15 09:37:19 -0800996} // namespace art