blob: 1735045d6066eb2617cdd2a34312cc5941e9e7bc [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.";
Richard Uhler95e09672017-02-22 11:37:41 +0000433 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000434 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000435 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000436 }
437 } else {
438 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800439 }
440
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100441 if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000442 if (!file.IsPic()) {
443 const ImageInfo* image_info = GetImageInfo();
444 if (image_info == nullptr) {
445 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000446 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000447 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700448
Andreas Gampe29d38e72016-03-23 15:31:51 +0000449 // Verify the oat_data_begin recorded for the image in the oat file matches
450 // the actual oat_data_begin for boot.oat in the image.
451 const OatHeader& oat_header = file.GetOatHeader();
452 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
453 if (oat_data_begin != image_info->oat_data_begin) {
454 VLOG(oat) << file.GetLocation() <<
455 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
456 << " does not match actual image oat_data_begin ("
457 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000458 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000459 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700460
Andreas Gampe29d38e72016-03-23 15:31:51 +0000461 // Verify the oat_patch_delta recorded for the image in the oat file matches
462 // the actual oat_patch_delta for the image.
463 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
464 if (oat_patch_delta != image_info->patch_delta) {
465 VLOG(oat) << file.GetLocation() <<
466 ": Oat file image patch delta (" << oat_patch_delta << ")"
467 << " does not match actual image patch delta ("
468 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000469 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000470 }
471 } else {
472 // Oat files compiled in PIC mode do not require relocation.
473 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
474 }
475 } else {
476 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800477 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700478 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800479}
480
Richard Uhler1e860612016-03-30 12:17:55 -0700481OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerf4b34872016-04-13 11:03:46 -0700482OatFileAssistant::GenerateOatFile(std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800483 CHECK(error_msg != nullptr);
484
Richard Uhler8327cf72015-10-13 16:34:59 -0700485 Runtime* runtime = Runtime::Current();
486 if (!runtime->IsDex2OatEnabled()) {
487 *error_msg = "Generation of oat file for dex location " + dex_location_
488 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700489 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700490 }
491
Richard Uhler743bf362016-04-19 15:39:37 -0700492 if (oat_.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700493 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800494 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700495 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800496 }
Richard Uhler743bf362016-04-19 15:39:37 -0700497 const std::string& oat_file_name = *oat_.Filename();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100498 const std::string& vdex_file_name = ReplaceFileExtension(oat_file_name, "vdex");
Richard Uhler66d874d2015-01-15 09:37:19 -0800499
Richard Uhler66d874d2015-01-15 09:37:19 -0800500 // dex2oat ignores missing dex files and doesn't report an error.
501 // Check explicitly here so we can detect the error properly.
502 // TODO: Why does dex2oat behave that way?
Richard Uhler740eec92015-10-15 15:12:23 -0700503 if (!OS::FileExists(dex_location_.c_str())) {
504 *error_msg = "Dex location " + dex_location_ + " does not exists.";
Richard Uhler1e860612016-03-30 12:17:55 -0700505 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800506 }
507
David Brazdil7b49e6c2016-09-01 11:06:18 +0100508 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
509 if (vdex_file.get() == nullptr) {
510 *error_msg = "Generation of oat file " + oat_file_name
511 + " not attempted because the vdex file " + vdex_file_name
512 + " could not be opened.";
513 return kUpdateNotAttempted;
514 }
515
516 if (fchmod(vdex_file->Fd(), 0644) != 0) {
517 *error_msg = "Generation of oat file " + oat_file_name
518 + " not attempted because the vdex file " + vdex_file_name
519 + " could not be made world readable.";
520 return kUpdateNotAttempted;
521 }
522
523 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700524 if (oat_file.get() == nullptr) {
525 *error_msg = "Generation of oat file " + oat_file_name
526 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700527 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700528 }
529
530 if (fchmod(oat_file->Fd(), 0644) != 0) {
531 *error_msg = "Generation of oat file " + oat_file_name
532 + " not attempted because the oat file could not be made world readable.";
533 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700534 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700535 }
536
537 std::vector<std::string> args;
538 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000539 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700540 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
541 args.push_back("--oat-location=" + oat_file_name);
542
Richard Uhler66d874d2015-01-15 09:37:19 -0800543 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100544 // Manually delete the oat and vdex files. This ensures there is no garbage
545 // left over if the process unexpectedly died.
546 vdex_file->Erase();
547 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700548 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100549 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700550 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700551 }
552
David Brazdil7b49e6c2016-09-01 11:06:18 +0100553 if (vdex_file->FlushCloseOrErase() != 0) {
554 *error_msg = "Unable to close vdex file " + vdex_file_name;
555 unlink(vdex_file_name.c_str());
556 return kUpdateFailed;
557 }
558
Richard Uhler8327cf72015-10-13 16:34:59 -0700559 if (oat_file->FlushCloseOrErase() != 0) {
560 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100561 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700562 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800563 }
564
565 // Mark that the oat file has changed and we should try to reload.
Richard Uhler743bf362016-04-19 15:39:37 -0700566 oat_.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700567 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800568}
569
570bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
571 std::string* error_msg) {
572 Runtime* runtime = Runtime::Current();
573 std::string image_location = ImageLocation();
574 if (image_location.empty()) {
575 *error_msg = "No image location found for Dex2Oat.";
576 return false;
577 }
578
579 std::vector<std::string> argv;
580 argv.push_back(runtime->GetCompilerExecutable());
581 argv.push_back("--runtime-arg");
582 argv.push_back("-classpath");
583 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700584 std::string class_path = runtime->GetClassPathString();
585 if (class_path == "") {
586 class_path = OatFile::kSpecialSharedLibrary;
587 }
588 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000589 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200590 argv.push_back("--debuggable");
591 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700592 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800593
594 if (!runtime->IsVerificationEnabled()) {
595 argv.push_back("--compiler-filter=verify-none");
596 }
597
598 if (runtime->MustRelocateIfPossible()) {
599 argv.push_back("--runtime-arg");
600 argv.push_back("-Xrelocate");
601 } else {
602 argv.push_back("--runtime-arg");
603 argv.push_back("-Xnorelocate");
604 }
605
606 if (!kIsTargetBuild) {
607 argv.push_back("--host");
608 }
609
610 argv.push_back("--boot-image=" + image_location);
611
612 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
613 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
614
615 argv.insert(argv.end(), args.begin(), args.end());
616
Andreas Gampe9186ced2016-12-12 14:28:21 -0800617 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800618 return Exec(argv, error_msg);
619}
620
Richard Uhlerb81881d2016-04-19 13:08:04 -0700621bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
622 InstructionSet isa,
623 std::string* odex_filename,
624 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800625 CHECK(odex_filename != nullptr);
626 CHECK(error_msg != nullptr);
627
628 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700629 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800630 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700631 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800632
Richard Uhler63434112015-03-16 14:32:16 -0700633 // Find the directory portion of the dex location and add the oat/<isa>
634 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800635 size_t pos = location.rfind('/');
636 if (pos == std::string::npos) {
637 *error_msg = "Dex location " + location + " has no directory.";
638 return false;
639 }
640 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700641 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800642
643 // Find the file portion of the dex location.
644 std::string file;
645 if (pos == std::string::npos) {
646 file = location;
647 } else {
648 file = location.substr(pos+1);
649 }
650
651 // Get the base part of the file without the extension.
652 pos = file.rfind('.');
653 if (pos == std::string::npos) {
654 *error_msg = "Dex location " + location + " has no extension.";
655 return false;
656 }
657 std::string base = file.substr(0, pos);
658
659 *odex_filename = dir + "/" + base + ".odex";
660 return true;
661}
662
Richard Uhlerb81881d2016-04-19 13:08:04 -0700663bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
664 InstructionSet isa,
665 std::string* oat_filename,
666 std::string* error_msg) {
667 CHECK(oat_filename != nullptr);
668 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800669
Richard Uhler55b58b62016-08-12 09:05:13 -0700670 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
671 if (cache_dir.empty()) {
672 *error_msg = "Dalvik cache directory does not exist";
673 return false;
674 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700675
676 // TODO: The oat file assistant should be the definitive place for
677 // determining the oat file name from the dex location, not
678 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700679 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800680}
681
Richard Uhler66d874d2015-01-15 09:37:19 -0800682std::string OatFileAssistant::ImageLocation() {
683 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000684 const std::vector<gc::space::ImageSpace*>& image_spaces =
685 runtime->GetHeap()->GetBootImageSpaces();
686 if (image_spaces.empty()) {
687 return "";
688 }
689 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800690}
691
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000692const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
693 if (!required_dex_checksums_attempted_) {
694 required_dex_checksums_attempted_ = true;
695 required_dex_checksums_found_ = false;
696 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800697 std::string error_msg;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000698 if (DexFile::GetMultiDexChecksums(dex_location_.c_str(),
699 &cached_required_dex_checksums_,
700 &error_msg)) {
701 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700702 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800703 } else {
704 // This can happen if the original dex file has been stripped from the
705 // apk.
706 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700707 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800708
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000709 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700710 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800711 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000712 required_dex_checksums_found_ = true;
713 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
714 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
715 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
716 if (odex_dex_file == nullptr) {
717 required_dex_checksums_found_ = false;
718 break;
719 }
720 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800721 }
722 }
723 }
724 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000725 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800726}
727
Richard Uhler95e09672017-02-22 11:37:41 +0000728// TODO: Use something better than xor for the combined image checksum.
729std::unique_ptr<OatFileAssistant::ImageInfo>
730OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
731 CHECK(error_msg != nullptr);
732
733 // Use the currently loaded image to determine the image locations for all
734 // the image spaces, regardless of the isa requested. Otherwise we would
735 // need to read from the boot image's oat file to determine the rest of the
736 // image locations in the case of multi-image.
737 Runtime* runtime = Runtime::Current();
738 std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces();
739 if (image_spaces.empty()) {
740 *error_msg = "There are no boot image spaces";
741 return nullptr;
742 }
743
744 std::unique_ptr<ImageInfo> info(new ImageInfo());
745 info->location = image_spaces[0]->GetImageLocation();
746
747 // TODO: Special casing on isa == kRuntimeISA is presumably motivated by
748 // performance: 'it's faster to use an already loaded image header than read
749 // the image header from disk'. But the loaded image is not necessarily the
750 // same as kRuntimeISA, so this behavior is suspect (b/35659889).
751 if (isa == kRuntimeISA) {
752 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhlerbc26b722017-03-10 14:27:10 +0000753 info->oat_checksum = image_header.GetOatChecksum();
Richard Uhler95e09672017-02-22 11:37:41 +0000754 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin());
755 info->patch_delta = image_header.GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000756 } else {
757 std::unique_ptr<ImageHeader> image_header(
758 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
759 if (image_header == nullptr) {
760 return nullptr;
761 }
Richard Uhlerbc26b722017-03-10 14:27:10 +0000762 info->oat_checksum = image_header->GetOatChecksum();
Richard Uhler95e09672017-02-22 11:37:41 +0000763 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
764 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000765 }
766 return info;
767}
768
Richard Uhler66d874d2015-01-15 09:37:19 -0800769const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
770 if (!image_info_load_attempted_) {
771 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000772 std::string error_msg;
773 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
774 if (cached_image_info_ == nullptr) {
775 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800776 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800777 }
Richard Uhler95e09672017-02-22 11:37:41 +0000778 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800779}
780
Richard Uhler88bc6732016-11-14 14:38:03 +0000781OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Richard Uhler03bc6592016-11-22 09:42:04 +0000782 bool use_oat = oat_.IsUseable() || odex_.Status() == kOatCannotOpen;
783 return use_oat ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000784}
785
Andreas Gampea463b6a2016-08-12 21:53:32 -0700786std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800787 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100788 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800789 if (art_file.empty()) {
790 return nullptr;
791 }
792 std::string error_msg;
793 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700794 std::unique_ptr<gc::space::ImageSpace> ret =
795 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800796 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800797 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
798 }
799 return ret;
800}
801
Richard Uhler88bc6732016-11-14 14:38:03 +0000802OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
803 bool is_oat_location)
804 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700805{}
806
Richard Uhler88bc6732016-11-14 14:38:03 +0000807bool OatFileAssistant::OatFileInfo::IsOatLocation() {
808 return is_oat_location_;
809}
810
Richard Uhler743bf362016-04-19 15:39:37 -0700811const std::string* OatFileAssistant::OatFileInfo::Filename() {
812 return filename_provided_ ? &filename_ : nullptr;
813}
814
Richard Uhler03bc6592016-11-22 09:42:04 +0000815bool OatFileAssistant::OatFileInfo::IsUseable() {
816 switch (Status()) {
817 case kOatCannotOpen:
818 case kOatDexOutOfDate:
819 case kOatBootImageOutOfDate: return false;
820
821 case kOatRelocationOutOfDate:
822 case kOatUpToDate: return true;
823 }
824 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700825}
826
827OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
828 if (!status_attempted_) {
829 status_attempted_ = true;
830 const OatFile* file = GetFile();
831 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000832 // Check to see if there is a vdex file we can make use of.
833 std::string error_msg;
834 std::string vdex_filename = ReplaceFileExtension(filename_, "vdex");
835 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
836 /*writeable*/false,
837 /*low_4gb*/false,
838 &error_msg);
839 if (vdex == nullptr) {
840 status_ = kOatCannotOpen;
841 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
842 } else {
843 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
844 // The vdex file does not contain enough information to determine
845 // whether it is up to date with respect to the boot image, so we
846 // assume it is out of date.
847 VLOG(oat) << error_msg;
848 status_ = kOatBootImageOutOfDate;
849 } else {
850 status_ = kOatDexOutOfDate;
851 }
852 }
Richard Uhler743bf362016-04-19 15:39:37 -0700853 } else {
854 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700855 VLOG(oat) << file->GetLocation() << " is " << status_
856 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700857 }
858 }
859 return status_;
860}
861
Richard Uhler70a84262016-11-08 16:51:51 +0000862OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
863 CompilerFilter::Filter target, bool profile_changed) {
864 bool compilation_desired = CompilerFilter::IsBytecodeCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000865 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +0000866
Richard Uhler7225a8d2016-11-22 10:12:03 +0000867 if (filter_okay && Status() == kOatUpToDate) {
868 // The oat file is in good shape as is.
869 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000870 }
871
Richard Uhler7225a8d2016-11-22 10:12:03 +0000872 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
873 // If no compilation is desired, then it doesn't matter if the oat
874 // file needs relocation. It's in good shape as is.
875 return kNoDexOptNeeded;
876 }
877
Richard Uhler7225a8d2016-11-22 10:12:03 +0000878 if (oat_file_assistant_->HasOriginalDexFiles()) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000879 if (filter_okay && Status() == kOatRelocationOutOfDate) {
880 return kDex2OatForRelocation;
881 }
882
883 if (IsUseable()) {
884 return kDex2OatForFilter;
885 }
886
887 if (Status() == kOatBootImageOutOfDate) {
888 return kDex2OatForBootImage;
889 }
890
891 return kDex2OatFromScratch;
892 }
893
894 // Otherwise there is nothing we can do, even if we want to.
895 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +0000896}
897
Richard Uhler743bf362016-04-19 15:39:37 -0700898const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
899 CHECK(!file_released_) << "GetFile called after oat file released.";
900 if (!load_attempted_) {
901 load_attempted_ = true;
902 if (filename_provided_) {
903 std::string error_msg;
904 file_.reset(OatFile::Open(filename_.c_str(),
905 filename_.c_str(),
906 nullptr,
907 nullptr,
908 oat_file_assistant_->load_executable_,
909 /*low_4gb*/false,
910 oat_file_assistant_->dex_location_.c_str(),
911 &error_msg));
912 if (file_.get() == nullptr) {
913 VLOG(oat) << "OatFileAssistant test for existing oat file "
914 << filename_ << ": " << error_msg;
915 }
916 }
917 }
918 return file_.get();
919}
920
921bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
922 CompilerFilter::Filter target, bool profile_changed) {
923 const OatFile* file = GetFile();
924 if (file == nullptr) {
925 return false;
926 }
927
928 CompilerFilter::Filter current = file->GetCompilerFilter();
929 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
930 VLOG(oat) << "Compiler filter not okay because Profile changed";
931 return false;
932 }
933 return CompilerFilter::IsAsGoodAs(current, target);
934}
935
936bool OatFileAssistant::OatFileInfo::IsExecutable() {
937 const OatFile* file = GetFile();
938 return (file != nullptr && file->IsExecutable());
939}
940
Richard Uhler743bf362016-04-19 15:39:37 -0700941void OatFileAssistant::OatFileInfo::Reset() {
942 load_attempted_ = false;
943 file_.reset();
944 status_attempted_ = false;
945}
946
947void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
948 filename_provided_ = true;
949 filename_ = filename;
950 Reset();
951}
952
953std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
954 file_released_ = true;
955 return std::move(file_);
956}
957
Richard Uhler70a84262016-11-08 16:51:51 +0000958std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +0000959 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000960 return ReleaseFile();
961 }
962
963 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
964 << " attempting to fall back to interpreting oat file instead.";
965
Richard Uhler03bc6592016-11-22 09:42:04 +0000966 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000967 return ReleaseFile();
968 }
969
Richard Uhler03bc6592016-11-22 09:42:04 +0000970 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +0000971 // We are loading an oat file for runtime use that needs relocation.
972 // Reload the file non-executable to ensure that we interpret out of the
973 // dex code in the oat file rather than trying to execute the unrelocated
974 // compiled code.
975 oat_file_assistant_->load_executable_ = false;
976 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +0000977 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +0000978 CHECK(!IsExecutable());
979 return ReleaseFile();
980 }
981 }
982 return std::unique_ptr<OatFile>();
983}
Richard Uhler66d874d2015-01-15 09:37:19 -0800984} // namespace art