blob: 4820feb56cc127758af833d3a3c376e150a4d06b [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_assistant.h"
18
Richard Uhler46cc64f2016-11-14 14:53:55 +000019#include <sstream>
20
Richard Uhler66d874d2015-01-15 09:37:19 -080021#include <sys/stat.h>
Andreas Gampe9186ced2016-12-12 14:28:21 -080022
Andreas Gampec15a2f42017-04-21 12:09:39 -070023#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080024#include "android-base/strings.h"
25
Richard Uhler66d874d2015-01-15 09:37:19 -080026#include "base/logging.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070027#include "base/stl_util.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010028#include "compiler_filter.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "class_linker.h"
David Sehr97c381e2017-02-01 15:09:58 -080030#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "gc/heap.h"
32#include "gc/space/image_space.h"
33#include "image.h"
34#include "oat.h"
35#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070037#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000039#include "vdex_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040
41namespace art {
42
Richard Uhler69bcf2c2017-01-24 10:25:21 +000043using android::base::StringPrintf;
44
Narayan Kamath8943c1d2016-05-02 13:14:48 +010045std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
46 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000047 case OatFileAssistant::kOatCannotOpen:
48 stream << "kOatCannotOpen";
49 break;
50 case OatFileAssistant::kOatDexOutOfDate:
51 stream << "kOatDexOutOfDate";
52 break;
53 case OatFileAssistant::kOatBootImageOutOfDate:
54 stream << "kOatBootImageOutOfDate";
55 break;
56 case OatFileAssistant::kOatRelocationOutOfDate:
57 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010058 break;
59 case OatFileAssistant::kOatUpToDate:
60 stream << "kOatUpToDate";
61 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010062 default:
63 UNREACHABLE();
64 }
65
66 return stream;
67}
68
Richard Uhler66d874d2015-01-15 09:37:19 -080069OatFileAssistant::OatFileAssistant(const char* dex_location,
70 const InstructionSet isa,
71 bool load_executable)
Richard Uhler88bc6732016-11-14 14:38:03 +000072 : isa_(isa),
73 load_executable_(load_executable),
74 odex_(this, /*is_oat_location*/ false),
75 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070076 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000077
78 // Try to get the realpath for the dex location.
79 //
80 // This is OK with respect to dalvik cache naming scheme because we never
81 // generate oat files starting from symlinks which go into dalvik cache.
82 // (recall that the oat files in dalvik cache are encoded by replacing '/'
83 // with '@' in the path).
84 // The boot image oat files (which are symlinked in dalvik-cache) are not
85 // loaded via the oat file assistant.
86 //
87 // The only case when the dex location may resolve to a different path
88 // is for secondary dex files (e.g. /data/user/0 symlinks to /data/data and
89 // the app is free to create its own internal layout). Related to this it is
90 // worthwhile to mention that installd resolves the secondary dex location
91 // before calling dex2oat.
92 UniqueCPtr<const char[]> dex_location_real(realpath(dex_location, nullptr));
93 if (dex_location_real != nullptr) {
94 dex_location_.assign(dex_location_real.get());
95 } else {
96 // If we can't get the realpath of the location there's not much point in trying to move on.
97 PLOG(ERROR) << "Could not get the realpath of dex_location " << dex_location;
98 return;
99 }
Richard Uhler740eec92015-10-15 15:12:23 -0700100
Richard Uhler66d874d2015-01-15 09:37:19 -0800101 if (load_executable_ && isa != kRuntimeISA) {
102 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
103 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
104 load_executable_ = false;
105 }
106
Richard Uhler743bf362016-04-19 15:39:37 -0700107 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700108 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700109 std::string odex_file_name;
110 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
111 odex_.Reset(odex_file_name);
112 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700113 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
114 }
115
Richard Uhler743bf362016-04-19 15:39:37 -0700116 // Get the oat filename.
Calin Juravle357c66d2017-05-04 01:57:17 +0000117 std::string oat_file_name;
118 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
119 oat_.Reset(oat_file_name);
Richard Uhlerd684f522016-04-19 13:24:41 -0700120 } else {
Calin Juravle357c66d2017-05-04 01:57:17 +0000121 LOG(WARNING) << "Failed to determine oat file name for dex location "
Calin Juravle9bfc6bb2017-05-04 00:13:50 +0000122 << dex_location_ << ": " << error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +0000123 }
124
125 // Check if the dex directory is writable.
126 // This will be needed in most uses of OatFileAssistant and so it's OK to
127 // compute it eagerly. (the only use which will not make use of it is
128 // OatFileAssistant::GetStatusDump())
129 size_t pos = dex_location_.rfind('/');
130 if (pos == std::string::npos) {
131 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
132 } else {
133 std::string parent = dex_location_.substr(0, pos);
134 if (access(parent.c_str(), W_OK) == 0) {
135 dex_parent_writable_ = true;
136 } else {
137 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700138 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800139 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800140}
141
142OatFileAssistant::~OatFileAssistant() {
143 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100144 if (flock_.get() != nullptr) {
145 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800146 }
147}
148
149bool OatFileAssistant::IsInBootClassPath() {
150 // Note: We check the current boot class path, regardless of the ISA
151 // specified by the user. This is okay, because the boot class path should
152 // be the same for all ISAs.
153 // TODO: Can we verify the boot class path is the same for all ISAs?
154 Runtime* runtime = Runtime::Current();
155 ClassLinker* class_linker = runtime->GetClassLinker();
156 const auto& boot_class_path = class_linker->GetBootClassPath();
157 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700158 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800159 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
160 return true;
161 }
162 }
163 return false;
164}
165
166bool OatFileAssistant::Lock(std::string* error_msg) {
167 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100168 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800169
Calin Juravle357c66d2017-05-04 01:57:17 +0000170 // Note the lock will only succeed for secondary dex files and in test
171 // environment.
172 //
173 // The lock *will fail* for all primary apks in a production environment.
174 // The app does not have permissions to create locks next to its dex location
175 // (be it system, data or vendor parition). We also cannot use the odex or
176 // oat location for the same reasoning.
177 //
178 // This is best effort and if it fails it's unlikely that we will be able
179 // to generate oat files anyway.
180 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800181
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100182 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
183 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100184 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800185 return false;
186 }
187 return true;
188}
189
Richard Uhler7225a8d2016-11-22 10:12:03 +0000190int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, bool profile_changed) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000191 OatFileInfo& info = GetBestInfo();
192 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000193 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
194 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800195 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000196 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800197}
198
Richard Uhlerf4b34872016-04-13 11:03:46 -0700199// Figure out the currently specified compile filter option in the runtime.
200// Returns true on success, false if the compiler filter is invalid, in which
201// case error_msg describes the problem.
202static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
203 std::string* error_msg) {
204 CHECK(filter != nullptr);
205 CHECK(error_msg != nullptr);
206
Calin Juravle357c66d2017-05-04 01:57:17 +0000207 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700208 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
209 if (option.starts_with("--compiler-filter=")) {
210 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
211 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
212 *error_msg = std::string("Unknown --compiler-filter value: ")
213 + std::string(compiler_filter_string);
214 return false;
215 }
216 }
217 }
218 return true;
219}
220
Richard Uhler01be6812016-05-17 10:34:52 -0700221bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000222 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700223}
224
Richard Uhler1e860612016-03-30 12:17:55 -0700225OatFileAssistant::ResultOfAttemptToUpdate
Richard Uhlerd1472a22016-04-15 15:18:56 -0700226OatFileAssistant::MakeUpToDate(bool profile_changed, std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700227 CompilerFilter::Filter target;
228 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
229 return kUpdateNotAttempted;
230 }
231
Richard Uhler88bc6732016-11-14 14:38:03 +0000232 OatFileInfo& info = GetBestInfo();
233 switch (info.GetDexOptNeeded(target, profile_changed)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000234 case kNoDexOptNeeded:
235 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000236
Richard Uhler7225a8d2016-11-22 10:12:03 +0000237 // TODO: For now, don't bother with all the different ways we can call
238 // dex2oat to generate the oat file. Always generate the oat file as if it
239 // were kDex2OatFromScratch.
240 case kDex2OatFromScratch:
241 case kDex2OatForBootImage:
242 case kDex2OatForRelocation:
243 case kDex2OatForFilter:
Calin Juravle07c6d722017-06-07 17:06:12 +0000244 return GenerateOatFileNoChecks(info, target, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800245 }
246 UNREACHABLE();
247}
248
249std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000250 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800251}
252
Richard Uhler46cc64f2016-11-14 14:53:55 +0000253std::string OatFileAssistant::GetStatusDump() {
254 std::ostringstream status;
255 bool oat_file_exists = false;
256 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000257 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000258 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000259 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000260
Richard Uhler46cc64f2016-11-14 14:53:55 +0000261 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000262 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
263 const OatFile* file = oat_.GetFile();
264 if (file == nullptr) {
265 // If the file is null even though the status is not kOatCannotOpen, it
266 // means we must have a vdex file with no corresponding oat file. In
267 // this case we cannot determine the compilation filter. Indicate that
268 // we have only the vdex file instead.
269 status << "vdex-only";
270 } else {
271 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
272 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000273 }
274
Richard Uhler03bc6592016-11-22 09:42:04 +0000275 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000276 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000277 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000278
Richard Uhler46cc64f2016-11-14 14:53:55 +0000279 odex_file_exists = true;
280 if (oat_file_exists) {
281 status << "] ";
282 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000283 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
284 const OatFile* file = odex_.GetFile();
285 if (file == nullptr) {
286 status << "vdex-only";
287 } else {
288 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
289 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000290 }
291
292 if (!oat_file_exists && !odex_file_exists) {
293 status << "invalid[";
294 }
295
296 status << "]";
297 return status.str();
298}
299
Richard Uhler66d874d2015-01-15 09:37:19 -0800300std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
301 const OatFile& oat_file, const char* dex_location) {
302 std::vector<std::unique_ptr<const DexFile>> dex_files;
303
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000304 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800305 std::string error_msg;
306 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Richard Uhler9a37efc2016-08-05 16:32:55 -0700307 dex_location, nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800308 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700309 LOG(WARNING) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800310 return std::vector<std::unique_ptr<const DexFile>>();
311 }
312
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700313 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800314 if (dex_file.get() == nullptr) {
315 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
316 return std::vector<std::unique_ptr<const DexFile>>();
317 }
318 dex_files.push_back(std::move(dex_file));
319
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000320 // Load the rest of the multidex entries
Andreas Gampe90e34042015-04-27 20:01:52 -0700321 for (size_t i = 1; ; i++) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000322 std::string multidex_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
323 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700324 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000325 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800326 break;
327 }
328
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700329 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800330 if (dex_file.get() == nullptr) {
331 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
332 return std::vector<std::unique_ptr<const DexFile>>();
333 }
334 dex_files.push_back(std::move(dex_file));
335 }
336 return dex_files;
337}
338
Richard Uhler9b994ea2015-06-24 08:44:19 -0700339bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000340 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700341 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000342 // GetRequiredDexChecksums.
343 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700344 return has_original_dex_files_;
345}
346
Richard Uhler95abd042015-03-24 09:51:28 -0700347OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700348 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800349}
350
Richard Uhler95abd042015-03-24 09:51:28 -0700351OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700352 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800353}
354
Richard Uhler2f27abd2017-01-31 14:02:34 +0000355bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000356 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
357 if (required_dex_checksums == nullptr) {
358 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
359 return true;
360 }
361
362 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
363 if (required_dex_checksums->size() != number_of_dex_files) {
364 *error_msg = StringPrintf("expected %zu dex files but found %u",
365 required_dex_checksums->size(),
366 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000367 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800368 }
369
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000370 for (uint32_t i = 0; i < number_of_dex_files; i++) {
371 uint32_t expected_checksum = (*required_dex_checksums)[i];
372 uint32_t actual_checksum = file.GetLocationChecksum(i);
373 if (expected_checksum != actual_checksum) {
374 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
375 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
376 "Expected: %u, actual: %u",
377 dex.c_str(),
378 expected_checksum,
379 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000380 return false;
381 }
382 }
383
Richard Uhler2f27abd2017-01-31 14:02:34 +0000384 return true;
385}
386
387bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000388 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
389 if (required_dex_checksums == nullptr) {
390 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
391 return true;
392 }
393
394 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
395 if (required_dex_checksums->size() != number_of_dex_files) {
396 *error_msg = StringPrintf("expected %zu dex files but found %u",
397 required_dex_checksums->size(),
398 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000399 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800400 }
401
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000402 for (uint32_t i = 0; i < number_of_dex_files; i++) {
403 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
404 uint32_t expected_checksum = (*required_dex_checksums)[i];
405 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
406 if (oat_dex_file == nullptr) {
407 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
408 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800409 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000410 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
411 if (expected_checksum != actual_checksum) {
412 VLOG(oat) << "Dex checksum does not match for dex: " << dex
413 << ". Expected: " << expected_checksum
414 << ", Actual: " << actual_checksum;
415 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800416 }
417 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000418 return true;
419}
420
421OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
422 // Verify the ART_USE_READ_BARRIER state.
423 // TODO: Don't fully reject files due to read barrier state. If they contain
424 // compiled code and are otherwise okay, we should return something like
425 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
426 // barrier state doesn't matter.
427 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
428 constexpr bool kRuntimeIsCC = kUseReadBarrier;
429 if (is_cc != kRuntimeIsCC) {
430 return kOatCannotOpen;
431 }
432
433 // Verify the dex checksum.
434 std::string error_msg;
435 if (kIsVdexEnabled) {
436 VdexFile* vdex = file.GetVdexFile();
437 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
438 LOG(ERROR) << error_msg;
439 return kOatDexOutOfDate;
440 }
441 } else {
442 if (!DexChecksumUpToDate(file, &error_msg)) {
443 LOG(ERROR) << error_msg;
444 return kOatDexOutOfDate;
445 }
446 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800447
Andreas Gampe29d38e72016-03-23 15:31:51 +0000448 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000449
Richard Uhler66d874d2015-01-15 09:37:19 -0800450 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000451 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
452 const ImageInfo* image_info = GetImageInfo();
453 if (image_info == nullptr) {
454 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000455
Richard Uhler76f5cb62016-04-04 13:30:16 -0700456 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000457 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700458 }
459
460 // If there is no original dex file to fall back to, grudgingly accept
461 // the oat file. This could technically lead to crashes, but there's no
462 // way we could find a better oat file to use for this dex location,
463 // and it's better than being stuck in a boot loop with no way out.
464 // The problem will hopefully resolve itself the next time the runtime
465 // starts up.
466 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
467 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000468 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000469 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000470 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000471 }
472 } else {
473 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800474 }
475
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100476 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000477 if (!file.IsPic()) {
478 const ImageInfo* image_info = GetImageInfo();
479 if (image_info == nullptr) {
480 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000481 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000482 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700483
Andreas Gampe29d38e72016-03-23 15:31:51 +0000484 // Verify the oat_data_begin recorded for the image in the oat file matches
485 // the actual oat_data_begin for boot.oat in the image.
486 const OatHeader& oat_header = file.GetOatHeader();
487 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
488 if (oat_data_begin != image_info->oat_data_begin) {
489 VLOG(oat) << file.GetLocation() <<
490 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
491 << " does not match actual image oat_data_begin ("
492 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000493 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700495
Andreas Gampe29d38e72016-03-23 15:31:51 +0000496 // Verify the oat_patch_delta recorded for the image in the oat file matches
497 // the actual oat_patch_delta for the image.
498 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
499 if (oat_patch_delta != image_info->patch_delta) {
500 VLOG(oat) << file.GetLocation() <<
501 ": Oat file image patch delta (" << oat_patch_delta << ")"
502 << " does not match actual image patch delta ("
503 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000504 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000505 }
506 } else {
507 // Oat files compiled in PIC mode do not require relocation.
508 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
509 }
510 } else {
511 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800512 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700513 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800514}
515
Calin Juravle357c66d2017-05-04 01:57:17 +0000516static bool DexLocationToOdexNames(const std::string& location,
517 InstructionSet isa,
518 std::string* odex_filename,
519 std::string* oat_dir,
520 std::string* isa_dir,
521 std::string* error_msg) {
522 CHECK(odex_filename != nullptr);
523 CHECK(error_msg != nullptr);
524
525 // The odex file name is formed by replacing the dex_location extension with
526 // .odex and inserting an oat/<isa> directory. For example:
527 // location = /foo/bar/baz.jar
528 // odex_location = /foo/bar/oat/<isa>/baz.odex
529
530 // Find the directory portion of the dex location and add the oat/<isa>
531 // directory.
532 size_t pos = location.rfind('/');
533 if (pos == std::string::npos) {
534 *error_msg = "Dex location " + location + " has no directory.";
535 return false;
536 }
537 std::string dir = location.substr(0, pos+1);
538 // Add the oat directory.
539 dir += "oat";
540 if (oat_dir != nullptr) {
541 *oat_dir = dir;
542 }
543 // Add the isa directory
544 dir += "/" + std::string(GetInstructionSetString(isa));
545 if (isa_dir != nullptr) {
546 *isa_dir = dir;
547 }
548
549 // Get the base part of the file without the extension.
550 std::string file = location.substr(pos+1);
551 pos = file.rfind('.');
552 if (pos == std::string::npos) {
553 *error_msg = "Dex location " + location + " has no extension.";
554 return false;
555 }
556 std::string base = file.substr(0, pos);
557
558 *odex_filename = dir + "/" + base + ".odex";
559 return true;
560}
561
562// Prepare a subcomponent of the odex directory.
563// (i.e. create and set the expected permissions on the path `dir`).
564static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
565 struct stat dir_stat;
566 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
567 // The directory exists. Check if it is indeed a directory.
568 if (!S_ISDIR(dir_stat.st_mode)) {
569 *error_msg = dir + " is not a dir";
570 return false;
571 } else {
572 // The dir is already on disk.
573 return true;
574 }
575 }
576
577 // Failed to stat. We need to create the directory.
578 if (errno != ENOENT) {
579 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
580 return false;
581 }
582
583 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
584 if (mkdir(dir.c_str(), mode) != 0) {
585 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
586 return false;
587 }
588 if (chmod(dir.c_str(), mode) != 0) {
589 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
590 return false;
591 }
592 return true;
593}
594
595// Prepares the odex directory for the given dex location.
596static bool PrepareOdexDirectories(const std::string& dex_location,
597 const std::string& expected_odex_location,
598 InstructionSet isa,
599 std::string* error_msg) {
600 std::string actual_odex_location;
601 std::string oat_dir;
602 std::string isa_dir;
603 if (!DexLocationToOdexNames(
604 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
605 return false;
606 }
607 DCHECK_EQ(expected_odex_location, actual_odex_location);
608
609 if (!PrepareDirectory(oat_dir, error_msg)) {
610 return false;
611 }
612 if (!PrepareDirectory(isa_dir, error_msg)) {
613 return false;
614 }
615 return true;
616}
617
618OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle07c6d722017-06-07 17:06:12 +0000619 OatFileAssistant::OatFileInfo& info, CompilerFilter::Filter filter, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800620 CHECK(error_msg != nullptr);
621
Richard Uhler8327cf72015-10-13 16:34:59 -0700622 Runtime* runtime = Runtime::Current();
623 if (!runtime->IsDex2OatEnabled()) {
624 *error_msg = "Generation of oat file for dex location " + dex_location_
625 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700626 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700627 }
628
Calin Juravle357c66d2017-05-04 01:57:17 +0000629 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700630 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800631 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700632 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800633 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000634 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700635 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800636
Richard Uhler66d874d2015-01-15 09:37:19 -0800637 // dex2oat ignores missing dex files and doesn't report an error.
638 // Check explicitly here so we can detect the error properly.
639 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000640 struct stat dex_path_stat;
641 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
642 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700643 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800644 }
645
Calin Juravle357c66d2017-05-04 01:57:17 +0000646 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
647 if (!info.IsOatLocation()) {
648 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
649 return kUpdateNotAttempted;
650 }
651 }
652
653 // Set the permissions for the oat and the vdex files.
654 // The user always gets read and write while the group and others propagate
655 // the reading access of the original dex file.
656 mode_t file_mode = S_IRUSR | S_IWUSR |
657 (dex_path_stat.st_mode & S_IRGRP) |
658 (dex_path_stat.st_mode & S_IROTH);
659
David Brazdil7b49e6c2016-09-01 11:06:18 +0100660 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
661 if (vdex_file.get() == nullptr) {
662 *error_msg = "Generation of oat file " + oat_file_name
663 + " not attempted because the vdex file " + vdex_file_name
664 + " could not be opened.";
665 return kUpdateNotAttempted;
666 }
667
Calin Juravle357c66d2017-05-04 01:57:17 +0000668 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100669 *error_msg = "Generation of oat file " + oat_file_name
670 + " not attempted because the vdex file " + vdex_file_name
671 + " could not be made world readable.";
672 return kUpdateNotAttempted;
673 }
674
675 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700676 if (oat_file.get() == nullptr) {
677 *error_msg = "Generation of oat file " + oat_file_name
678 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700679 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700680 }
681
Calin Juravle357c66d2017-05-04 01:57:17 +0000682 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700683 *error_msg = "Generation of oat file " + oat_file_name
684 + " not attempted because the oat file could not be made world readable.";
685 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700686 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700687 }
688
689 std::vector<std::string> args;
690 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000691 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700692 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
693 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000694 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhler8327cf72015-10-13 16:34:59 -0700695
Richard Uhler66d874d2015-01-15 09:37:19 -0800696 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100697 // Manually delete the oat and vdex files. This ensures there is no garbage
698 // left over if the process unexpectedly died.
699 vdex_file->Erase();
700 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700701 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100702 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700703 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700704 }
705
David Brazdil7b49e6c2016-09-01 11:06:18 +0100706 if (vdex_file->FlushCloseOrErase() != 0) {
707 *error_msg = "Unable to close vdex file " + vdex_file_name;
708 unlink(vdex_file_name.c_str());
709 return kUpdateFailed;
710 }
711
Richard Uhler8327cf72015-10-13 16:34:59 -0700712 if (oat_file->FlushCloseOrErase() != 0) {
713 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100714 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700715 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800716 }
717
Calin Juravle357c66d2017-05-04 01:57:17 +0000718 // Mark that the odex file has changed and we should try to reload.
719 info.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700720 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800721}
722
723bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
724 std::string* error_msg) {
725 Runtime* runtime = Runtime::Current();
726 std::string image_location = ImageLocation();
727 if (image_location.empty()) {
728 *error_msg = "No image location found for Dex2Oat.";
729 return false;
730 }
731
732 std::vector<std::string> argv;
733 argv.push_back(runtime->GetCompilerExecutable());
734 argv.push_back("--runtime-arg");
735 argv.push_back("-classpath");
736 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700737 std::string class_path = runtime->GetClassPathString();
738 if (class_path == "") {
739 class_path = OatFile::kSpecialSharedLibrary;
740 }
741 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000742 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200743 argv.push_back("--debuggable");
744 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700745 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800746
747 if (!runtime->IsVerificationEnabled()) {
748 argv.push_back("--compiler-filter=verify-none");
749 }
750
751 if (runtime->MustRelocateIfPossible()) {
752 argv.push_back("--runtime-arg");
753 argv.push_back("-Xrelocate");
754 } else {
755 argv.push_back("--runtime-arg");
756 argv.push_back("-Xnorelocate");
757 }
758
759 if (!kIsTargetBuild) {
760 argv.push_back("--host");
761 }
762
763 argv.push_back("--boot-image=" + image_location);
764
765 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
766 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
767
768 argv.insert(argv.end(), args.begin(), args.end());
769
Andreas Gampe9186ced2016-12-12 14:28:21 -0800770 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800771 return Exec(argv, error_msg);
772}
773
Richard Uhlerb81881d2016-04-19 13:08:04 -0700774bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
775 InstructionSet isa,
776 std::string* odex_filename,
777 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000778 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800779}
780
Richard Uhlerb81881d2016-04-19 13:08:04 -0700781bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
782 InstructionSet isa,
783 std::string* oat_filename,
784 std::string* error_msg) {
785 CHECK(oat_filename != nullptr);
786 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800787
Richard Uhler55b58b62016-08-12 09:05:13 -0700788 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
789 if (cache_dir.empty()) {
790 *error_msg = "Dalvik cache directory does not exist";
791 return false;
792 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700793
794 // TODO: The oat file assistant should be the definitive place for
795 // determining the oat file name from the dex location, not
796 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700797 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800798}
799
Richard Uhler66d874d2015-01-15 09:37:19 -0800800std::string OatFileAssistant::ImageLocation() {
801 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000802 const std::vector<gc::space::ImageSpace*>& image_spaces =
803 runtime->GetHeap()->GetBootImageSpaces();
804 if (image_spaces.empty()) {
805 return "";
806 }
807 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800808}
809
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000810const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
811 if (!required_dex_checksums_attempted_) {
812 required_dex_checksums_attempted_ = true;
813 required_dex_checksums_found_ = false;
814 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800815 std::string error_msg;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000816 if (DexFile::GetMultiDexChecksums(dex_location_.c_str(),
817 &cached_required_dex_checksums_,
818 &error_msg)) {
819 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700820 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800821 } else {
822 // This can happen if the original dex file has been stripped from the
823 // apk.
824 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700825 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800826
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000827 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700828 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800829 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000830 required_dex_checksums_found_ = true;
831 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
832 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
833 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
834 if (odex_dex_file == nullptr) {
835 required_dex_checksums_found_ = false;
836 break;
837 }
838 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800839 }
840 }
841 }
842 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000843 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800844}
845
Richard Uhler95e09672017-02-22 11:37:41 +0000846std::unique_ptr<OatFileAssistant::ImageInfo>
847OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
848 CHECK(error_msg != nullptr);
849
Richard Uhler95e09672017-02-22 11:37:41 +0000850 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000851 std::unique_ptr<ImageInfo> info(new ImageInfo());
852 info->location = runtime->GetImageLocation();
853
854 std::unique_ptr<ImageHeader> image_header(
855 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
856 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000857 return nullptr;
858 }
859
Richard Uhler8f104862017-02-22 12:34:01 +0000860 info->oat_checksum = image_header->GetOatChecksum();
861 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
862 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000863 return info;
864}
865
Richard Uhler66d874d2015-01-15 09:37:19 -0800866const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
867 if (!image_info_load_attempted_) {
868 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000869 std::string error_msg;
870 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
871 if (cached_image_info_ == nullptr) {
872 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800873 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800874 }
Richard Uhler95e09672017-02-22 11:37:41 +0000875 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800876}
877
Richard Uhler88bc6732016-11-14 14:38:03 +0000878OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000879 // TODO(calin): Document the side effects of class loading when
880 // running dalvikvm command line.
881 if (dex_parent_writable_) {
882 // If the parent of the dex file is writable it means that we can
883 // create the odex file. In this case we unconditionally pick the odex
884 // as the best oat file. This corresponds to the regular use case when
885 // apps gets installed or when they load private, secondary dex file.
886 // For apps on the system partition the odex location will not be
887 // writable and thus the oat location might be more up to date.
888 return odex_;
889 }
890
891 // We cannot write to the odex location. This must be a system app.
892
893 // If the oat location is usable take it.
894 if (oat_.IsUseable()) {
895 return oat_;
896 }
897
898 // The oat file is not usable but the odex file might be up to date.
899 // This is an indication that we are dealing with an up to date prebuilt
900 // (that doesn't need relocation).
901 if (odex_.Status() == kOatUpToDate) {
902 return odex_;
903 }
904
905 // The oat file is not usable and the odex file is not up to date.
906 // However we have access to the original dex file which means we can make
907 // the oat location up to date.
908 if (HasOriginalDexFiles()) {
909 return oat_;
910 }
911
912 // We got into the worst situation here:
913 // - the oat location is not usable
914 // - the prebuild odex location is not up to date
915 // - and we don't have the original dex file anymore (stripped).
916 // Pick the odex if it exists, or the oat if not.
917 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000918}
919
Andreas Gampea463b6a2016-08-12 21:53:32 -0700920std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800921 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100922 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800923 if (art_file.empty()) {
924 return nullptr;
925 }
926 std::string error_msg;
927 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700928 std::unique_ptr<gc::space::ImageSpace> ret =
929 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800930 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800931 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
932 }
933 return ret;
934}
935
Richard Uhler88bc6732016-11-14 14:38:03 +0000936OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
937 bool is_oat_location)
938 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700939{}
940
Richard Uhler88bc6732016-11-14 14:38:03 +0000941bool OatFileAssistant::OatFileInfo::IsOatLocation() {
942 return is_oat_location_;
943}
944
Richard Uhler743bf362016-04-19 15:39:37 -0700945const std::string* OatFileAssistant::OatFileInfo::Filename() {
946 return filename_provided_ ? &filename_ : nullptr;
947}
948
Richard Uhler03bc6592016-11-22 09:42:04 +0000949bool OatFileAssistant::OatFileInfo::IsUseable() {
950 switch (Status()) {
951 case kOatCannotOpen:
952 case kOatDexOutOfDate:
953 case kOatBootImageOutOfDate: return false;
954
955 case kOatRelocationOutOfDate:
956 case kOatUpToDate: return true;
957 }
958 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700959}
960
961OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
962 if (!status_attempted_) {
963 status_attempted_ = true;
964 const OatFile* file = GetFile();
965 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000966 // Check to see if there is a vdex file we can make use of.
967 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700968 std::string vdex_filename = GetVdexFilename(filename_);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000969 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
970 /*writeable*/false,
971 /*low_4gb*/false,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100972 /*unquicken*/false,
Richard Uhler2f27abd2017-01-31 14:02:34 +0000973 &error_msg);
974 if (vdex == nullptr) {
975 status_ = kOatCannotOpen;
976 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
977 } else {
978 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
979 // The vdex file does not contain enough information to determine
980 // whether it is up to date with respect to the boot image, so we
981 // assume it is out of date.
982 VLOG(oat) << error_msg;
983 status_ = kOatBootImageOutOfDate;
984 } else {
985 status_ = kOatDexOutOfDate;
986 }
987 }
Richard Uhler743bf362016-04-19 15:39:37 -0700988 } else {
989 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -0700990 VLOG(oat) << file->GetLocation() << " is " << status_
991 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -0700992 }
993 }
994 return status_;
995}
996
Richard Uhler70a84262016-11-08 16:51:51 +0000997OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
998 CompilerFilter::Filter target, bool profile_changed) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100999 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +00001000 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +00001001
Richard Uhler7225a8d2016-11-22 10:12:03 +00001002 if (filter_okay && Status() == kOatUpToDate) {
1003 // The oat file is in good shape as is.
1004 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +00001005 }
1006
Richard Uhler7225a8d2016-11-22 10:12:03 +00001007 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1008 // If no compilation is desired, then it doesn't matter if the oat
1009 // file needs relocation. It's in good shape as is.
1010 return kNoDexOptNeeded;
1011 }
1012
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001013 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1014 return kDex2OatForRelocation;
Richard Uhler7225a8d2016-11-22 10:12:03 +00001015 }
1016
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001017 if (IsUseable()) {
1018 return kDex2OatForFilter;
1019 }
1020
1021 if (Status() == kOatBootImageOutOfDate) {
1022 return kDex2OatForBootImage;
1023 }
1024
1025 if (oat_file_assistant_->HasOriginalDexFiles()) {
1026 return kDex2OatFromScratch;
1027 } else {
1028 // Otherwise there is nothing we can do, even if we want to.
1029 return kNoDexOptNeeded;
1030 }
Richard Uhler70a84262016-11-08 16:51:51 +00001031}
1032
Richard Uhler743bf362016-04-19 15:39:37 -07001033const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1034 CHECK(!file_released_) << "GetFile called after oat file released.";
1035 if (!load_attempted_) {
1036 load_attempted_ = true;
1037 if (filename_provided_) {
1038 std::string error_msg;
1039 file_.reset(OatFile::Open(filename_.c_str(),
1040 filename_.c_str(),
1041 nullptr,
1042 nullptr,
1043 oat_file_assistant_->load_executable_,
1044 /*low_4gb*/false,
1045 oat_file_assistant_->dex_location_.c_str(),
1046 &error_msg));
1047 if (file_.get() == nullptr) {
1048 VLOG(oat) << "OatFileAssistant test for existing oat file "
1049 << filename_ << ": " << error_msg;
1050 }
1051 }
1052 }
1053 return file_.get();
1054}
1055
1056bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
1057 CompilerFilter::Filter target, bool profile_changed) {
1058 const OatFile* file = GetFile();
1059 if (file == nullptr) {
1060 return false;
1061 }
1062
1063 CompilerFilter::Filter current = file->GetCompilerFilter();
1064 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1065 VLOG(oat) << "Compiler filter not okay because Profile changed";
1066 return false;
1067 }
1068 return CompilerFilter::IsAsGoodAs(current, target);
1069}
1070
1071bool OatFileAssistant::OatFileInfo::IsExecutable() {
1072 const OatFile* file = GetFile();
1073 return (file != nullptr && file->IsExecutable());
1074}
1075
Richard Uhler743bf362016-04-19 15:39:37 -07001076void OatFileAssistant::OatFileInfo::Reset() {
1077 load_attempted_ = false;
1078 file_.reset();
1079 status_attempted_ = false;
1080}
1081
1082void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
1083 filename_provided_ = true;
1084 filename_ = filename;
1085 Reset();
1086}
1087
1088std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1089 file_released_ = true;
1090 return std::move(file_);
1091}
1092
Richard Uhler70a84262016-11-08 16:51:51 +00001093std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001094 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001095 return ReleaseFile();
1096 }
1097
1098 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1099 << " attempting to fall back to interpreting oat file instead.";
1100
Richard Uhler03bc6592016-11-22 09:42:04 +00001101 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001102 return ReleaseFile();
1103 }
1104
Richard Uhler03bc6592016-11-22 09:42:04 +00001105 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001106 // We are loading an oat file for runtime use that needs relocation.
1107 // Reload the file non-executable to ensure that we interpret out of the
1108 // dex code in the oat file rather than trying to execute the unrelocated
1109 // compiled code.
1110 oat_file_assistant_->load_executable_ = false;
1111 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001112 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001113 CHECK(!IsExecutable());
1114 return ReleaseFile();
1115 }
1116 }
1117 return std::unique_ptr<OatFile>();
1118}
Richard Uhler66d874d2015-01-15 09:37:19 -08001119} // namespace art