blob: c8766578c43ed92deced38dae2a28eb001a752d1 [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(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700301 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800302 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700303 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
304 return dex_files;
305 } else {
306 return std::vector<std::unique_ptr<const DexFile>>();
307 }
308}
Richard Uhler66d874d2015-01-15 09:37:19 -0800309
Calin Juravle87e2cb62017-06-13 21:48:45 -0700310bool OatFileAssistant::LoadDexFiles(
311 const OatFile &oat_file,
312 const std::string& dex_location,
313 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000314 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800315 std::string error_msg;
316 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700317 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800318 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700319 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700320 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800321 }
322
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700323 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800324 if (dex_file.get() == nullptr) {
325 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700326 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800327 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700328 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800329
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000330 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700331 for (size_t i = 1;; i++) {
332 std::string multidex_dex_location = DexFile::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000333 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700334 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000335 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800336 break;
337 }
338
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700339 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800340 if (dex_file.get() == nullptr) {
341 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700342 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800343 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700344 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800345 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700346 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800347}
348
Richard Uhler9b994ea2015-06-24 08:44:19 -0700349bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000350 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700351 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000352 // GetRequiredDexChecksums.
353 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700354 return has_original_dex_files_;
355}
356
Richard Uhler95abd042015-03-24 09:51:28 -0700357OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700358 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800359}
360
Richard Uhler95abd042015-03-24 09:51:28 -0700361OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700362 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800363}
364
Richard Uhler2f27abd2017-01-31 14:02:34 +0000365bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000366 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
367 if (required_dex_checksums == nullptr) {
368 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
369 return true;
370 }
371
372 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
373 if (required_dex_checksums->size() != number_of_dex_files) {
374 *error_msg = StringPrintf("expected %zu dex files but found %u",
375 required_dex_checksums->size(),
376 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000377 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800378 }
379
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000380 for (uint32_t i = 0; i < number_of_dex_files; i++) {
381 uint32_t expected_checksum = (*required_dex_checksums)[i];
382 uint32_t actual_checksum = file.GetLocationChecksum(i);
383 if (expected_checksum != actual_checksum) {
384 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
385 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
386 "Expected: %u, actual: %u",
387 dex.c_str(),
388 expected_checksum,
389 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000390 return false;
391 }
392 }
393
Richard Uhler2f27abd2017-01-31 14:02:34 +0000394 return true;
395}
396
397bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000398 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
399 if (required_dex_checksums == nullptr) {
400 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
401 return true;
402 }
403
404 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
405 if (required_dex_checksums->size() != number_of_dex_files) {
406 *error_msg = StringPrintf("expected %zu dex files but found %u",
407 required_dex_checksums->size(),
408 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000409 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800410 }
411
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000412 for (uint32_t i = 0; i < number_of_dex_files; i++) {
413 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
414 uint32_t expected_checksum = (*required_dex_checksums)[i];
415 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
416 if (oat_dex_file == nullptr) {
417 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
418 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000420 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
421 if (expected_checksum != actual_checksum) {
422 VLOG(oat) << "Dex checksum does not match for dex: " << dex
423 << ". Expected: " << expected_checksum
424 << ", Actual: " << actual_checksum;
425 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800426 }
427 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000428 return true;
429}
430
431OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
432 // Verify the ART_USE_READ_BARRIER state.
433 // TODO: Don't fully reject files due to read barrier state. If they contain
434 // compiled code and are otherwise okay, we should return something like
435 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
436 // barrier state doesn't matter.
437 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
438 constexpr bool kRuntimeIsCC = kUseReadBarrier;
439 if (is_cc != kRuntimeIsCC) {
440 return kOatCannotOpen;
441 }
442
443 // Verify the dex checksum.
444 std::string error_msg;
445 if (kIsVdexEnabled) {
446 VdexFile* vdex = file.GetVdexFile();
447 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
448 LOG(ERROR) << error_msg;
449 return kOatDexOutOfDate;
450 }
451 } else {
452 if (!DexChecksumUpToDate(file, &error_msg)) {
453 LOG(ERROR) << error_msg;
454 return kOatDexOutOfDate;
455 }
456 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800457
Andreas Gampe29d38e72016-03-23 15:31:51 +0000458 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000459
Richard Uhler66d874d2015-01-15 09:37:19 -0800460 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000461 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
462 const ImageInfo* image_info = GetImageInfo();
463 if (image_info == nullptr) {
464 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000465
Richard Uhler76f5cb62016-04-04 13:30:16 -0700466 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000467 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700468 }
469
470 // If there is no original dex file to fall back to, grudgingly accept
471 // the oat file. This could technically lead to crashes, but there's no
472 // way we could find a better oat file to use for this dex location,
473 // and it's better than being stuck in a boot loop with no way out.
474 // The problem will hopefully resolve itself the next time the runtime
475 // starts up.
476 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
477 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000478 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000479 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000480 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000481 }
482 } else {
483 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 }
485
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100486 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000487 if (!file.IsPic()) {
488 const ImageInfo* image_info = GetImageInfo();
489 if (image_info == nullptr) {
490 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000491 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000492 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700493
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 // Verify the oat_data_begin recorded for the image in the oat file matches
495 // the actual oat_data_begin for boot.oat in the image.
496 const OatHeader& oat_header = file.GetOatHeader();
497 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
498 if (oat_data_begin != image_info->oat_data_begin) {
499 VLOG(oat) << file.GetLocation() <<
500 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
501 << " does not match actual image oat_data_begin ("
502 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000503 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000504 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700505
Andreas Gampe29d38e72016-03-23 15:31:51 +0000506 // Verify the oat_patch_delta recorded for the image in the oat file matches
507 // the actual oat_patch_delta for the image.
508 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
509 if (oat_patch_delta != image_info->patch_delta) {
510 VLOG(oat) << file.GetLocation() <<
511 ": Oat file image patch delta (" << oat_patch_delta << ")"
512 << " does not match actual image patch delta ("
513 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000514 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000515 }
516 } else {
517 // Oat files compiled in PIC mode do not require relocation.
518 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
519 }
520 } else {
521 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800522 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700523 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800524}
525
Calin Juravle357c66d2017-05-04 01:57:17 +0000526static bool DexLocationToOdexNames(const std::string& location,
527 InstructionSet isa,
528 std::string* odex_filename,
529 std::string* oat_dir,
530 std::string* isa_dir,
531 std::string* error_msg) {
532 CHECK(odex_filename != nullptr);
533 CHECK(error_msg != nullptr);
534
535 // The odex file name is formed by replacing the dex_location extension with
536 // .odex and inserting an oat/<isa> directory. For example:
537 // location = /foo/bar/baz.jar
538 // odex_location = /foo/bar/oat/<isa>/baz.odex
539
540 // Find the directory portion of the dex location and add the oat/<isa>
541 // directory.
542 size_t pos = location.rfind('/');
543 if (pos == std::string::npos) {
544 *error_msg = "Dex location " + location + " has no directory.";
545 return false;
546 }
547 std::string dir = location.substr(0, pos+1);
548 // Add the oat directory.
549 dir += "oat";
550 if (oat_dir != nullptr) {
551 *oat_dir = dir;
552 }
553 // Add the isa directory
554 dir += "/" + std::string(GetInstructionSetString(isa));
555 if (isa_dir != nullptr) {
556 *isa_dir = dir;
557 }
558
559 // Get the base part of the file without the extension.
560 std::string file = location.substr(pos+1);
561 pos = file.rfind('.');
562 if (pos == std::string::npos) {
563 *error_msg = "Dex location " + location + " has no extension.";
564 return false;
565 }
566 std::string base = file.substr(0, pos);
567
568 *odex_filename = dir + "/" + base + ".odex";
569 return true;
570}
571
572// Prepare a subcomponent of the odex directory.
573// (i.e. create and set the expected permissions on the path `dir`).
574static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
575 struct stat dir_stat;
576 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
577 // The directory exists. Check if it is indeed a directory.
578 if (!S_ISDIR(dir_stat.st_mode)) {
579 *error_msg = dir + " is not a dir";
580 return false;
581 } else {
582 // The dir is already on disk.
583 return true;
584 }
585 }
586
587 // Failed to stat. We need to create the directory.
588 if (errno != ENOENT) {
589 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
590 return false;
591 }
592
593 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
594 if (mkdir(dir.c_str(), mode) != 0) {
595 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
596 return false;
597 }
598 if (chmod(dir.c_str(), mode) != 0) {
599 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
600 return false;
601 }
602 return true;
603}
604
605// Prepares the odex directory for the given dex location.
606static bool PrepareOdexDirectories(const std::string& dex_location,
607 const std::string& expected_odex_location,
608 InstructionSet isa,
609 std::string* error_msg) {
610 std::string actual_odex_location;
611 std::string oat_dir;
612 std::string isa_dir;
613 if (!DexLocationToOdexNames(
614 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
615 return false;
616 }
617 DCHECK_EQ(expected_odex_location, actual_odex_location);
618
619 if (!PrepareDirectory(oat_dir, error_msg)) {
620 return false;
621 }
622 if (!PrepareDirectory(isa_dir, error_msg)) {
623 return false;
624 }
625 return true;
626}
627
628OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle07c6d722017-06-07 17:06:12 +0000629 OatFileAssistant::OatFileInfo& info, CompilerFilter::Filter filter, std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800630 CHECK(error_msg != nullptr);
631
Richard Uhler8327cf72015-10-13 16:34:59 -0700632 Runtime* runtime = Runtime::Current();
633 if (!runtime->IsDex2OatEnabled()) {
634 *error_msg = "Generation of oat file for dex location " + dex_location_
635 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700636 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700637 }
638
Calin Juravle357c66d2017-05-04 01:57:17 +0000639 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700640 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800641 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700642 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800643 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000644 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700645 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800646
Richard Uhler66d874d2015-01-15 09:37:19 -0800647 // dex2oat ignores missing dex files and doesn't report an error.
648 // Check explicitly here so we can detect the error properly.
649 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000650 struct stat dex_path_stat;
651 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
652 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700653 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800654 }
655
Calin Juravle357c66d2017-05-04 01:57:17 +0000656 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
657 if (!info.IsOatLocation()) {
658 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
659 return kUpdateNotAttempted;
660 }
661 }
662
663 // Set the permissions for the oat and the vdex files.
664 // The user always gets read and write while the group and others propagate
665 // the reading access of the original dex file.
666 mode_t file_mode = S_IRUSR | S_IWUSR |
667 (dex_path_stat.st_mode & S_IRGRP) |
668 (dex_path_stat.st_mode & S_IROTH);
669
David Brazdil7b49e6c2016-09-01 11:06:18 +0100670 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str()));
671 if (vdex_file.get() == nullptr) {
672 *error_msg = "Generation of oat file " + oat_file_name
673 + " not attempted because the vdex file " + vdex_file_name
674 + " could not be opened.";
675 return kUpdateNotAttempted;
676 }
677
Calin Juravle357c66d2017-05-04 01:57:17 +0000678 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100679 *error_msg = "Generation of oat file " + oat_file_name
680 + " not attempted because the vdex file " + vdex_file_name
681 + " could not be made world readable.";
682 return kUpdateNotAttempted;
683 }
684
685 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_file_name.c_str()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700686 if (oat_file.get() == nullptr) {
687 *error_msg = "Generation of oat file " + oat_file_name
688 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700689 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700690 }
691
Calin Juravle357c66d2017-05-04 01:57:17 +0000692 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700693 *error_msg = "Generation of oat file " + oat_file_name
694 + " not attempted because the oat file could not be made world readable.";
695 oat_file->Erase();
Richard Uhler1e860612016-03-30 12:17:55 -0700696 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700697 }
698
699 std::vector<std::string> args;
700 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000701 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700702 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
703 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000704 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhler8327cf72015-10-13 16:34:59 -0700705
Richard Uhler66d874d2015-01-15 09:37:19 -0800706 if (!Dex2Oat(args, error_msg)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100707 // Manually delete the oat and vdex files. This ensures there is no garbage
708 // left over if the process unexpectedly died.
709 vdex_file->Erase();
710 unlink(vdex_file_name.c_str());
Richard Uhler8327cf72015-10-13 16:34:59 -0700711 oat_file->Erase();
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100712 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700713 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700714 }
715
David Brazdil7b49e6c2016-09-01 11:06:18 +0100716 if (vdex_file->FlushCloseOrErase() != 0) {
717 *error_msg = "Unable to close vdex file " + vdex_file_name;
718 unlink(vdex_file_name.c_str());
719 return kUpdateFailed;
720 }
721
Richard Uhler8327cf72015-10-13 16:34:59 -0700722 if (oat_file->FlushCloseOrErase() != 0) {
723 *error_msg = "Unable to close oat file " + oat_file_name;
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100724 unlink(oat_file_name.c_str());
Richard Uhler1e860612016-03-30 12:17:55 -0700725 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800726 }
727
Calin Juravle357c66d2017-05-04 01:57:17 +0000728 // Mark that the odex file has changed and we should try to reload.
729 info.Reset();
Richard Uhler1e860612016-03-30 12:17:55 -0700730 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800731}
732
733bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
734 std::string* error_msg) {
735 Runtime* runtime = Runtime::Current();
736 std::string image_location = ImageLocation();
737 if (image_location.empty()) {
738 *error_msg = "No image location found for Dex2Oat.";
739 return false;
740 }
741
742 std::vector<std::string> argv;
743 argv.push_back(runtime->GetCompilerExecutable());
744 argv.push_back("--runtime-arg");
745 argv.push_back("-classpath");
746 argv.push_back("--runtime-arg");
Jeff Haof0192c82016-03-28 20:39:50 -0700747 std::string class_path = runtime->GetClassPathString();
748 if (class_path == "") {
749 class_path = OatFile::kSpecialSharedLibrary;
750 }
751 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000752 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200753 argv.push_back("--debuggable");
754 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700755 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800756
757 if (!runtime->IsVerificationEnabled()) {
758 argv.push_back("--compiler-filter=verify-none");
759 }
760
761 if (runtime->MustRelocateIfPossible()) {
762 argv.push_back("--runtime-arg");
763 argv.push_back("-Xrelocate");
764 } else {
765 argv.push_back("--runtime-arg");
766 argv.push_back("-Xnorelocate");
767 }
768
769 if (!kIsTargetBuild) {
770 argv.push_back("--host");
771 }
772
773 argv.push_back("--boot-image=" + image_location);
774
775 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
776 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
777
778 argv.insert(argv.end(), args.begin(), args.end());
779
Andreas Gampe9186ced2016-12-12 14:28:21 -0800780 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800781 return Exec(argv, error_msg);
782}
783
Richard Uhlerb81881d2016-04-19 13:08:04 -0700784bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
785 InstructionSet isa,
786 std::string* odex_filename,
787 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000788 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800789}
790
Richard Uhlerb81881d2016-04-19 13:08:04 -0700791bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
792 InstructionSet isa,
793 std::string* oat_filename,
794 std::string* error_msg) {
795 CHECK(oat_filename != nullptr);
796 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800797
Richard Uhler55b58b62016-08-12 09:05:13 -0700798 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
799 if (cache_dir.empty()) {
800 *error_msg = "Dalvik cache directory does not exist";
801 return false;
802 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700803
804 // TODO: The oat file assistant should be the definitive place for
805 // determining the oat file name from the dex location, not
806 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700807 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800808}
809
Richard Uhler66d874d2015-01-15 09:37:19 -0800810std::string OatFileAssistant::ImageLocation() {
811 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000812 const std::vector<gc::space::ImageSpace*>& image_spaces =
813 runtime->GetHeap()->GetBootImageSpaces();
814 if (image_spaces.empty()) {
815 return "";
816 }
817 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800818}
819
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000820const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
821 if (!required_dex_checksums_attempted_) {
822 required_dex_checksums_attempted_ = true;
823 required_dex_checksums_found_ = false;
824 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800825 std::string error_msg;
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000826 if (DexFile::GetMultiDexChecksums(dex_location_.c_str(),
827 &cached_required_dex_checksums_,
828 &error_msg)) {
829 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700830 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800831 } else {
832 // This can happen if the original dex file has been stripped from the
833 // apk.
834 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700835 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800836
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000837 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700838 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800839 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000840 required_dex_checksums_found_ = true;
841 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
842 std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str());
843 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
844 if (odex_dex_file == nullptr) {
845 required_dex_checksums_found_ = false;
846 break;
847 }
848 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800849 }
850 }
851 }
852 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000853 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800854}
855
Richard Uhler95e09672017-02-22 11:37:41 +0000856std::unique_ptr<OatFileAssistant::ImageInfo>
857OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
858 CHECK(error_msg != nullptr);
859
Richard Uhler95e09672017-02-22 11:37:41 +0000860 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000861 std::unique_ptr<ImageInfo> info(new ImageInfo());
862 info->location = runtime->GetImageLocation();
863
864 std::unique_ptr<ImageHeader> image_header(
865 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
866 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000867 return nullptr;
868 }
869
Richard Uhler8f104862017-02-22 12:34:01 +0000870 info->oat_checksum = image_header->GetOatChecksum();
871 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
872 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000873 return info;
874}
875
Richard Uhler66d874d2015-01-15 09:37:19 -0800876const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
877 if (!image_info_load_attempted_) {
878 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000879 std::string error_msg;
880 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
881 if (cached_image_info_ == nullptr) {
882 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800883 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800884 }
Richard Uhler95e09672017-02-22 11:37:41 +0000885 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800886}
887
Richard Uhler88bc6732016-11-14 14:38:03 +0000888OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000889 // TODO(calin): Document the side effects of class loading when
890 // running dalvikvm command line.
891 if (dex_parent_writable_) {
892 // If the parent of the dex file is writable it means that we can
893 // create the odex file. In this case we unconditionally pick the odex
894 // as the best oat file. This corresponds to the regular use case when
895 // apps gets installed or when they load private, secondary dex file.
896 // For apps on the system partition the odex location will not be
897 // writable and thus the oat location might be more up to date.
898 return odex_;
899 }
900
901 // We cannot write to the odex location. This must be a system app.
902
903 // If the oat location is usable take it.
904 if (oat_.IsUseable()) {
905 return oat_;
906 }
907
908 // The oat file is not usable but the odex file might be up to date.
909 // This is an indication that we are dealing with an up to date prebuilt
910 // (that doesn't need relocation).
911 if (odex_.Status() == kOatUpToDate) {
912 return odex_;
913 }
914
915 // The oat file is not usable and the odex file is not up to date.
916 // However we have access to the original dex file which means we can make
917 // the oat location up to date.
918 if (HasOriginalDexFiles()) {
919 return oat_;
920 }
921
922 // We got into the worst situation here:
923 // - the oat location is not usable
924 // - the prebuild odex location is not up to date
925 // - and we don't have the original dex file anymore (stripped).
926 // Pick the odex if it exists, or the oat if not.
927 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000928}
929
Andreas Gampea463b6a2016-08-12 21:53:32 -0700930std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800931 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100932 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800933 if (art_file.empty()) {
934 return nullptr;
935 }
936 std::string error_msg;
937 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700938 std::unique_ptr<gc::space::ImageSpace> ret =
939 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800940 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800941 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
942 }
943 return ret;
944}
945
Richard Uhler88bc6732016-11-14 14:38:03 +0000946OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
947 bool is_oat_location)
948 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700949{}
950
Richard Uhler88bc6732016-11-14 14:38:03 +0000951bool OatFileAssistant::OatFileInfo::IsOatLocation() {
952 return is_oat_location_;
953}
954
Richard Uhler743bf362016-04-19 15:39:37 -0700955const std::string* OatFileAssistant::OatFileInfo::Filename() {
956 return filename_provided_ ? &filename_ : nullptr;
957}
958
Richard Uhler03bc6592016-11-22 09:42:04 +0000959bool OatFileAssistant::OatFileInfo::IsUseable() {
960 switch (Status()) {
961 case kOatCannotOpen:
962 case kOatDexOutOfDate:
963 case kOatBootImageOutOfDate: return false;
964
965 case kOatRelocationOutOfDate:
966 case kOatUpToDate: return true;
967 }
968 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -0700969}
970
971OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
972 if (!status_attempted_) {
973 status_attempted_ = true;
974 const OatFile* file = GetFile();
975 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000976 // Check to see if there is a vdex file we can make use of.
977 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -0700978 std::string vdex_filename = GetVdexFilename(filename_);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000979 std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename,
980 /*writeable*/false,
981 /*low_4gb*/false,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100982 /*unquicken*/false,
Richard Uhler2f27abd2017-01-31 14:02:34 +0000983 &error_msg);
984 if (vdex == nullptr) {
985 status_ = kOatCannotOpen;
986 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
987 } else {
988 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
989 // The vdex file does not contain enough information to determine
990 // whether it is up to date with respect to the boot image, so we
991 // assume it is out of date.
992 VLOG(oat) << error_msg;
993 status_ = kOatBootImageOutOfDate;
994 } else {
995 status_ = kOatDexOutOfDate;
996 }
997 }
Richard Uhler743bf362016-04-19 15:39:37 -0700998 } else {
999 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001000 VLOG(oat) << file->GetLocation() << " is " << status_
1001 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001002 }
1003 }
1004 return status_;
1005}
1006
Richard Uhler70a84262016-11-08 16:51:51 +00001007OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
1008 CompilerFilter::Filter target, bool profile_changed) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001009 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Richard Uhler7225a8d2016-11-22 10:12:03 +00001010 bool filter_okay = CompilerFilterIsOkay(target, profile_changed);
Richard Uhler70a84262016-11-08 16:51:51 +00001011
Richard Uhler7225a8d2016-11-22 10:12:03 +00001012 if (filter_okay && Status() == kOatUpToDate) {
1013 // The oat file is in good shape as is.
1014 return kNoDexOptNeeded;
Richard Uhler70a84262016-11-08 16:51:51 +00001015 }
1016
Richard Uhler7225a8d2016-11-22 10:12:03 +00001017 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1018 // If no compilation is desired, then it doesn't matter if the oat
1019 // file needs relocation. It's in good shape as is.
1020 return kNoDexOptNeeded;
1021 }
1022
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001023 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1024 return kDex2OatForRelocation;
Richard Uhler7225a8d2016-11-22 10:12:03 +00001025 }
1026
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001027 if (IsUseable()) {
1028 return kDex2OatForFilter;
1029 }
1030
1031 if (Status() == kOatBootImageOutOfDate) {
1032 return kDex2OatForBootImage;
1033 }
1034
1035 if (oat_file_assistant_->HasOriginalDexFiles()) {
1036 return kDex2OatFromScratch;
1037 } else {
1038 // Otherwise there is nothing we can do, even if we want to.
1039 return kNoDexOptNeeded;
1040 }
Richard Uhler70a84262016-11-08 16:51:51 +00001041}
1042
Richard Uhler743bf362016-04-19 15:39:37 -07001043const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1044 CHECK(!file_released_) << "GetFile called after oat file released.";
1045 if (!load_attempted_) {
1046 load_attempted_ = true;
1047 if (filename_provided_) {
1048 std::string error_msg;
1049 file_.reset(OatFile::Open(filename_.c_str(),
1050 filename_.c_str(),
1051 nullptr,
1052 nullptr,
1053 oat_file_assistant_->load_executable_,
1054 /*low_4gb*/false,
1055 oat_file_assistant_->dex_location_.c_str(),
1056 &error_msg));
1057 if (file_.get() == nullptr) {
1058 VLOG(oat) << "OatFileAssistant test for existing oat file "
1059 << filename_ << ": " << error_msg;
1060 }
1061 }
1062 }
1063 return file_.get();
1064}
1065
1066bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
1067 CompilerFilter::Filter target, bool profile_changed) {
1068 const OatFile* file = GetFile();
1069 if (file == nullptr) {
1070 return false;
1071 }
1072
1073 CompilerFilter::Filter current = file->GetCompilerFilter();
1074 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1075 VLOG(oat) << "Compiler filter not okay because Profile changed";
1076 return false;
1077 }
1078 return CompilerFilter::IsAsGoodAs(current, target);
1079}
1080
1081bool OatFileAssistant::OatFileInfo::IsExecutable() {
1082 const OatFile* file = GetFile();
1083 return (file != nullptr && file->IsExecutable());
1084}
1085
Richard Uhler743bf362016-04-19 15:39:37 -07001086void OatFileAssistant::OatFileInfo::Reset() {
1087 load_attempted_ = false;
1088 file_.reset();
1089 status_attempted_ = false;
1090}
1091
1092void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) {
1093 filename_provided_ = true;
1094 filename_ = filename;
1095 Reset();
1096}
1097
1098std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1099 file_released_ = true;
1100 return std::move(file_);
1101}
1102
Richard Uhler70a84262016-11-08 16:51:51 +00001103std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001104 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001105 return ReleaseFile();
1106 }
1107
1108 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1109 << " attempting to fall back to interpreting oat file instead.";
1110
Richard Uhler03bc6592016-11-22 09:42:04 +00001111 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001112 return ReleaseFile();
1113 }
1114
Richard Uhler03bc6592016-11-22 09:42:04 +00001115 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001116 // We are loading an oat file for runtime use that needs relocation.
1117 // Reload the file non-executable to ensure that we interpret out of the
1118 // dex code in the oat file rather than trying to execute the unrelocated
1119 // compiled code.
1120 oat_file_assistant_->load_executable_ = false;
1121 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001122 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001123 CHECK(!IsExecutable());
1124 return ReleaseFile();
1125 }
1126 }
1127 return std::unique_ptr<OatFile>();
1128}
Richard Uhler66d874d2015-01-15 09:37:19 -08001129} // namespace art