blob: fb612285121db128b07673866424e94ff22a71cf [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"
Richard Uhler66d874d2015-01-15 09:37:19 -080028#include "class_linker.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "compiler_filter.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070030#include "dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080031#include "exec_utils.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080032#include "gc/heap.h"
33#include "gc/space/image_space.h"
34#include "image.h"
35#include "oat.h"
36#include "os.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080039#include "utils.h"
Richard Uhler2f27abd2017-01-31 14:02:34 +000040#include "vdex_file.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070041#include "class_loader_context.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080042
43namespace art {
44
Richard Uhler69bcf2c2017-01-24 10:25:21 +000045using android::base::StringPrintf;
46
Narayan Kamath8943c1d2016-05-02 13:14:48 +010047std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) {
48 switch (status) {
Richard Uhler03bc6592016-11-22 09:42:04 +000049 case OatFileAssistant::kOatCannotOpen:
50 stream << "kOatCannotOpen";
51 break;
52 case OatFileAssistant::kOatDexOutOfDate:
53 stream << "kOatDexOutOfDate";
54 break;
55 case OatFileAssistant::kOatBootImageOutOfDate:
56 stream << "kOatBootImageOutOfDate";
57 break;
58 case OatFileAssistant::kOatRelocationOutOfDate:
59 stream << "kOatRelocationOutOfDate";
Narayan Kamath8943c1d2016-05-02 13:14:48 +010060 break;
61 case OatFileAssistant::kOatUpToDate:
62 stream << "kOatUpToDate";
63 break;
Narayan Kamath8943c1d2016-05-02 13:14:48 +010064 default:
65 UNREACHABLE();
66 }
67
68 return stream;
69}
70
Richard Uhler66d874d2015-01-15 09:37:19 -080071OatFileAssistant::OatFileAssistant(const char* dex_location,
72 const InstructionSet isa,
Shubham Ajmerab22dea02017-10-04 18:36:41 -070073 bool load_executable,
74 int vdex_fd,
75 int oat_fd)
Richard Uhler88bc6732016-11-14 14:38:03 +000076 : isa_(isa),
77 load_executable_(load_executable),
78 odex_(this, /*is_oat_location*/ false),
79 oat_(this, /*is_oat_location*/ true) {
Richard Uhler740eec92015-10-15 15:12:23 -070080 CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
Calin Juravle357c66d2017-05-04 01:57:17 +000081
82 // Try to get the realpath for the dex location.
83 //
84 // This is OK with respect to dalvik cache naming scheme because we never
85 // generate oat files starting from symlinks which go into dalvik cache.
86 // (recall that the oat files in dalvik cache are encoded by replacing '/'
87 // with '@' in the path).
88 // The boot image oat files (which are symlinked in dalvik-cache) are not
89 // loaded via the oat file assistant.
90 //
91 // The only case when the dex location may resolve to a different path
92 // is for secondary dex files (e.g. /data/user/0 symlinks to /data/data and
93 // the app is free to create its own internal layout). Related to this it is
94 // worthwhile to mention that installd resolves the secondary dex location
95 // before calling dex2oat.
96 UniqueCPtr<const char[]> dex_location_real(realpath(dex_location, nullptr));
97 if (dex_location_real != nullptr) {
98 dex_location_.assign(dex_location_real.get());
99 } else {
100 // If we can't get the realpath of the location there's not much point in trying to move on.
101 PLOG(ERROR) << "Could not get the realpath of dex_location " << dex_location;
102 return;
103 }
Richard Uhler740eec92015-10-15 15:12:23 -0700104
Richard Uhler66d874d2015-01-15 09:37:19 -0800105 if (load_executable_ && isa != kRuntimeISA) {
106 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
107 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
108 load_executable_ = false;
109 }
110
Richard Uhler743bf362016-04-19 15:39:37 -0700111 // Get the odex filename.
Richard Uhlerd684f522016-04-19 13:24:41 -0700112 std::string error_msg;
Richard Uhler743bf362016-04-19 15:39:37 -0700113 std::string odex_file_name;
114 if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700115 odex_.Reset(odex_file_name, vdex_fd, oat_fd);
Richard Uhler743bf362016-04-19 15:39:37 -0700116 } else {
Richard Uhlerd684f522016-04-19 13:24:41 -0700117 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
118 }
119
Richard Uhler743bf362016-04-19 15:39:37 -0700120 // Get the oat filename.
Calin Juravle357c66d2017-05-04 01:57:17 +0000121 std::string oat_file_name;
122 if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) {
123 oat_.Reset(oat_file_name);
Richard Uhlerd684f522016-04-19 13:24:41 -0700124 } else {
Calin Juravle357c66d2017-05-04 01:57:17 +0000125 LOG(WARNING) << "Failed to determine oat file name for dex location "
Calin Juravle9bfc6bb2017-05-04 00:13:50 +0000126 << dex_location_ << ": " << error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +0000127 }
128
129 // Check if the dex directory is writable.
130 // This will be needed in most uses of OatFileAssistant and so it's OK to
131 // compute it eagerly. (the only use which will not make use of it is
132 // OatFileAssistant::GetStatusDump())
133 size_t pos = dex_location_.rfind('/');
134 if (pos == std::string::npos) {
135 LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
136 } else {
137 std::string parent = dex_location_.substr(0, pos);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700138 if (access(parent.c_str(), W_OK) == 0 || oat_fd > 0) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000139 dex_parent_writable_ = true;
140 } else {
141 VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
Richard Uhlerd684f522016-04-19 13:24:41 -0700142 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800143 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800144}
145
146OatFileAssistant::~OatFileAssistant() {
147 // Clean up the lock file.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100148 if (flock_.get() != nullptr) {
149 unlink(flock_->GetPath().c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800150 }
151}
152
153bool OatFileAssistant::IsInBootClassPath() {
154 // Note: We check the current boot class path, regardless of the ISA
155 // specified by the user. This is okay, because the boot class path should
156 // be the same for all ISAs.
157 // TODO: Can we verify the boot class path is the same for all ISAs?
158 Runtime* runtime = Runtime::Current();
159 ClassLinker* class_linker = runtime->GetClassLinker();
160 const auto& boot_class_path = class_linker->GetBootClassPath();
161 for (size_t i = 0; i < boot_class_path.size(); i++) {
Richard Uhler740eec92015-10-15 15:12:23 -0700162 if (boot_class_path[i]->GetLocation() == dex_location_) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800163 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
164 return true;
165 }
166 }
167 return false;
168}
169
170bool OatFileAssistant::Lock(std::string* error_msg) {
171 CHECK(error_msg != nullptr);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100172 CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800173
Calin Juravle357c66d2017-05-04 01:57:17 +0000174 // Note the lock will only succeed for secondary dex files and in test
175 // environment.
176 //
177 // The lock *will fail* for all primary apks in a production environment.
178 // The app does not have permissions to create locks next to its dex location
179 // (be it system, data or vendor parition). We also cannot use the odex or
180 // oat location for the same reasoning.
181 //
182 // This is best effort and if it fails it's unlikely that we will be able
183 // to generate oat files anyway.
184 std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock";
Richard Uhler66d874d2015-01-15 09:37:19 -0800185
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100186 flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg);
187 if (flock_.get() == nullptr) {
Vladimir Marko66fdcbd2016-04-05 14:19:08 +0100188 unlink(lock_file_name.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800189 return false;
190 }
191 return true;
192}
193
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700194int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target,
195 bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700196 bool downgrade,
197 ClassLoaderContext* class_loader_context) {
Richard Uhler88bc6732016-11-14 14:38:03 +0000198 OatFileInfo& info = GetBestInfo();
Calin Juravle44e5efa2017-09-12 00:54:26 -0700199 DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target,
200 profile_changed,
201 downgrade,
202 class_loader_context);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000203 if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
204 return dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800205 }
Richard Uhler7225a8d2016-11-22 10:12:03 +0000206 return -dexopt_needed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800207}
208
Richard Uhlerf4b34872016-04-13 11:03:46 -0700209// Figure out the currently specified compile filter option in the runtime.
210// Returns true on success, false if the compiler filter is invalid, in which
211// case error_msg describes the problem.
212static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter,
213 std::string* error_msg) {
214 CHECK(filter != nullptr);
215 CHECK(error_msg != nullptr);
216
Calin Juravle357c66d2017-05-04 01:57:17 +0000217 *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700218 for (StringPiece option : Runtime::Current()->GetCompilerOptions()) {
219 if (option.starts_with("--compiler-filter=")) {
220 const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
221 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) {
222 *error_msg = std::string("Unknown --compiler-filter value: ")
223 + std::string(compiler_filter_string);
224 return false;
225 }
226 }
227 }
228 return true;
229}
230
Richard Uhler01be6812016-05-17 10:34:52 -0700231bool OatFileAssistant::IsUpToDate() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000232 return GetBestInfo().Status() == kOatUpToDate;
Richard Uhler01be6812016-05-17 10:34:52 -0700233}
234
Richard Uhler1e860612016-03-30 12:17:55 -0700235OatFileAssistant::ResultOfAttemptToUpdate
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700236OatFileAssistant::MakeUpToDate(bool profile_changed,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700237 ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700238 std::string* error_msg) {
Richard Uhlerf4b34872016-04-13 11:03:46 -0700239 CompilerFilter::Filter target;
240 if (!GetRuntimeCompilerFilterOption(&target, error_msg)) {
241 return kUpdateNotAttempted;
242 }
243
Richard Uhler88bc6732016-11-14 14:38:03 +0000244 OatFileInfo& info = GetBestInfo();
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700245 // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291.
246 // This is actually not trivial in the current logic as it will interact with the collision
247 // check:
248 // - currently, if the context does not match but we have no collisions we still accept the
249 // oat file.
250 // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make
251 // the oat code up to date the collision check becomes useless.
252 // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files
253 // loaded in other processes). So it boils down to how far do we want to complicate
254 // the logic in order to enable the use of oat files. Maybe its time to try simplify it.
Calin Juravle44e5efa2017-09-12 00:54:26 -0700255 switch (info.GetDexOptNeeded(
256 target, profile_changed, /*downgrade*/ false, class_loader_context)) {
Richard Uhler7225a8d2016-11-22 10:12:03 +0000257 case kNoDexOptNeeded:
258 return kUpdateSucceeded;
Richard Uhler88bc6732016-11-14 14:38:03 +0000259
Richard Uhler7225a8d2016-11-22 10:12:03 +0000260 // TODO: For now, don't bother with all the different ways we can call
261 // dex2oat to generate the oat file. Always generate the oat file as if it
262 // were kDex2OatFromScratch.
263 case kDex2OatFromScratch:
264 case kDex2OatForBootImage:
265 case kDex2OatForRelocation:
266 case kDex2OatForFilter:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700267 return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800268 }
269 UNREACHABLE();
270}
271
272std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler88bc6732016-11-14 14:38:03 +0000273 return GetBestInfo().ReleaseFileForUse();
Richard Uhler66d874d2015-01-15 09:37:19 -0800274}
275
Richard Uhler46cc64f2016-11-14 14:53:55 +0000276std::string OatFileAssistant::GetStatusDump() {
277 std::ostringstream status;
278 bool oat_file_exists = false;
279 bool odex_file_exists = false;
Richard Uhler03bc6592016-11-22 09:42:04 +0000280 if (oat_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000281 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000282 CHECK(oat_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000283
Richard Uhler46cc64f2016-11-14 14:53:55 +0000284 oat_file_exists = true;
Richard Uhler2f27abd2017-01-31 14:02:34 +0000285 status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
286 const OatFile* file = oat_.GetFile();
287 if (file == nullptr) {
288 // If the file is null even though the status is not kOatCannotOpen, it
289 // means we must have a vdex file with no corresponding oat file. In
290 // this case we cannot determine the compilation filter. Indicate that
291 // we have only the vdex file instead.
292 status << "vdex-only";
293 } else {
294 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
295 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000296 }
297
Richard Uhler03bc6592016-11-22 09:42:04 +0000298 if (odex_.Status() != kOatCannotOpen) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000299 // If we can open the file, Filename should not return null.
Richard Uhler03bc6592016-11-22 09:42:04 +0000300 CHECK(odex_.Filename() != nullptr);
Richard Uhler03bc6592016-11-22 09:42:04 +0000301
Richard Uhler46cc64f2016-11-14 14:53:55 +0000302 odex_file_exists = true;
303 if (oat_file_exists) {
304 status << "] ";
305 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000306 status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
307 const OatFile* file = odex_.GetFile();
308 if (file == nullptr) {
309 status << "vdex-only";
310 } else {
311 status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
312 }
Richard Uhler46cc64f2016-11-14 14:53:55 +0000313 }
314
315 if (!oat_file_exists && !odex_file_exists) {
316 status << "invalid[";
317 }
318
319 status << "]";
320 return status.str();
321}
322
Richard Uhler66d874d2015-01-15 09:37:19 -0800323std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700324 const OatFile &oat_file, const char *dex_location) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800325 std::vector<std::unique_ptr<const DexFile>> dex_files;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700326 if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
327 return dex_files;
328 } else {
329 return std::vector<std::unique_ptr<const DexFile>>();
330 }
331}
Richard Uhler66d874d2015-01-15 09:37:19 -0800332
Calin Juravle87e2cb62017-06-13 21:48:45 -0700333bool OatFileAssistant::LoadDexFiles(
334 const OatFile &oat_file,
335 const std::string& dex_location,
336 std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000337 // Load the main dex file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800338 std::string error_msg;
339 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
Calin Juravle87e2cb62017-06-13 21:48:45 -0700340 dex_location.c_str(), nullptr, &error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800341 if (oat_dex_file == nullptr) {
Richard Uhler9a37efc2016-08-05 16:32:55 -0700342 LOG(WARNING) << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700343 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800344 }
345
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700346 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800347 if (dex_file.get() == nullptr) {
348 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700349 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800350 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700351 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800352
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000353 // Load the rest of the multidex entries
Calin Juravle87e2cb62017-06-13 21:48:45 -0700354 for (size_t i = 1;; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700355 std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000356 oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700357 if (oat_dex_file == nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000358 // There are no more multidex entries to load.
Richard Uhler66d874d2015-01-15 09:37:19 -0800359 break;
360 }
361
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700362 dex_file = oat_dex_file->OpenDexFile(&error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800363 if (dex_file.get() == nullptr) {
364 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700365 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800366 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700367 out_dex_files->push_back(std::move(dex_file));
Richard Uhler66d874d2015-01-15 09:37:19 -0800368 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700369 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800370}
371
Richard Uhler9b994ea2015-06-24 08:44:19 -0700372bool OatFileAssistant::HasOriginalDexFiles() {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000373 // Ensure GetRequiredDexChecksums has been run so that
Richard Uhler9b994ea2015-06-24 08:44:19 -0700374 // has_original_dex_files_ is initialized. We don't care about the result of
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000375 // GetRequiredDexChecksums.
376 GetRequiredDexChecksums();
Richard Uhler9b994ea2015-06-24 08:44:19 -0700377 return has_original_dex_files_;
378}
379
Richard Uhler95abd042015-03-24 09:51:28 -0700380OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700381 return odex_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800382}
383
Richard Uhler95abd042015-03-24 09:51:28 -0700384OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler743bf362016-04-19 15:39:37 -0700385 return oat_.Status();
Richard Uhler66d874d2015-01-15 09:37:19 -0800386}
387
Richard Uhler2f27abd2017-01-31 14:02:34 +0000388bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000389 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
390 if (required_dex_checksums == nullptr) {
391 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
392 return true;
393 }
394
395 uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles();
396 if (required_dex_checksums->size() != number_of_dex_files) {
397 *error_msg = StringPrintf("expected %zu dex files but found %u",
398 required_dex_checksums->size(),
399 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000400 return false;
Andreas Gampef8cd8902017-01-18 16:05:01 -0800401 }
402
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000403 for (uint32_t i = 0; i < number_of_dex_files; i++) {
404 uint32_t expected_checksum = (*required_dex_checksums)[i];
405 uint32_t actual_checksum = file.GetLocationChecksum(i);
406 if (expected_checksum != actual_checksum) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700407 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000408 *error_msg = StringPrintf("Dex checksum does not match for dex: %s."
409 "Expected: %u, actual: %u",
410 dex.c_str(),
411 expected_checksum,
412 actual_checksum);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000413 return false;
414 }
415 }
416
Richard Uhler2f27abd2017-01-31 14:02:34 +0000417 return true;
418}
419
420bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000421 const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums();
422 if (required_dex_checksums == nullptr) {
423 LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
424 return true;
425 }
426
427 uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
428 if (required_dex_checksums->size() != number_of_dex_files) {
429 *error_msg = StringPrintf("expected %zu dex files but found %u",
430 required_dex_checksums->size(),
431 number_of_dex_files);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000432 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800433 }
434
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000435 for (uint32_t i = 0; i < number_of_dex_files; i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700436 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000437 uint32_t expected_checksum = (*required_dex_checksums)[i];
438 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr);
439 if (oat_dex_file == nullptr) {
440 *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
441 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800442 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000443 uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum();
444 if (expected_checksum != actual_checksum) {
445 VLOG(oat) << "Dex checksum does not match for dex: " << dex
446 << ". Expected: " << expected_checksum
447 << ", Actual: " << actual_checksum;
448 return false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 }
450 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000451 return true;
452}
453
454OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
455 // Verify the ART_USE_READ_BARRIER state.
456 // TODO: Don't fully reject files due to read barrier state. If they contain
457 // compiled code and are otherwise okay, we should return something like
458 // kOatRelocationOutOfDate. If they don't contain compiled code, the read
459 // barrier state doesn't matter.
460 const bool is_cc = file.GetOatHeader().IsConcurrentCopying();
461 constexpr bool kRuntimeIsCC = kUseReadBarrier;
462 if (is_cc != kRuntimeIsCC) {
463 return kOatCannotOpen;
464 }
465
466 // Verify the dex checksum.
467 std::string error_msg;
468 if (kIsVdexEnabled) {
469 VdexFile* vdex = file.GetVdexFile();
470 if (!DexChecksumUpToDate(*vdex, &error_msg)) {
471 LOG(ERROR) << error_msg;
472 return kOatDexOutOfDate;
473 }
474 } else {
475 if (!DexChecksumUpToDate(file, &error_msg)) {
476 LOG(ERROR) << error_msg;
477 return kOatDexOutOfDate;
478 }
479 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800480
Andreas Gampe29d38e72016-03-23 15:31:51 +0000481 CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
David Brazdilce4b0ba2016-01-28 15:05:49 +0000482
Richard Uhler66d874d2015-01-15 09:37:19 -0800483 // Verify the image checksum
Andreas Gampe29d38e72016-03-23 15:31:51 +0000484 if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
485 const ImageInfo* image_info = GetImageInfo();
486 if (image_info == nullptr) {
487 VLOG(oat) << "No image for oat image checksum to match against.";
Andreas Gampe29d38e72016-03-23 15:31:51 +0000488
Richard Uhler76f5cb62016-04-04 13:30:16 -0700489 if (HasOriginalDexFiles()) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000490 return kOatBootImageOutOfDate;
Richard Uhler76f5cb62016-04-04 13:30:16 -0700491 }
492
493 // If there is no original dex file to fall back to, grudgingly accept
494 // the oat file. This could technically lead to crashes, but there's no
495 // way we could find a better oat file to use for this dex location,
496 // and it's better than being stuck in a boot loop with no way out.
497 // The problem will hopefully resolve itself the next time the runtime
498 // starts up.
499 LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. "
500 << "Allow oat file use. This is potentially dangerous.";
Richard Uhler95e09672017-02-22 11:37:41 +0000501 } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000502 VLOG(oat) << "Oat image checksum does not match image checksum.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000503 return kOatBootImageOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000504 }
505 } else {
506 VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800507 }
508
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100509 if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000510 if (!file.IsPic()) {
511 const ImageInfo* image_info = GetImageInfo();
512 if (image_info == nullptr) {
513 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler03bc6592016-11-22 09:42:04 +0000514 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000515 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700516
Andreas Gampe29d38e72016-03-23 15:31:51 +0000517 // Verify the oat_data_begin recorded for the image in the oat file matches
518 // the actual oat_data_begin for boot.oat in the image.
519 const OatHeader& oat_header = file.GetOatHeader();
520 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
521 if (oat_data_begin != image_info->oat_data_begin) {
522 VLOG(oat) << file.GetLocation() <<
523 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
524 << " does not match actual image oat_data_begin ("
525 << image_info->oat_data_begin << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000526 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000527 }
Richard Uhlera62d2f02016-03-18 15:05:30 -0700528
Andreas Gampe29d38e72016-03-23 15:31:51 +0000529 // Verify the oat_patch_delta recorded for the image in the oat file matches
530 // the actual oat_patch_delta for the image.
531 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
532 if (oat_patch_delta != image_info->patch_delta) {
533 VLOG(oat) << file.GetLocation() <<
534 ": Oat file image patch delta (" << oat_patch_delta << ")"
535 << " does not match actual image patch delta ("
536 << image_info->patch_delta << ")";
Richard Uhler03bc6592016-11-22 09:42:04 +0000537 return kOatRelocationOutOfDate;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000538 }
539 } else {
540 // Oat files compiled in PIC mode do not require relocation.
541 VLOG(oat) << "Oat relocation test skipped for PIC oat file";
542 }
543 } else {
544 VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter;
Richard Uhler66d874d2015-01-15 09:37:19 -0800545 }
Richard Uhlere8109f72016-04-18 10:40:50 -0700546 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800547}
548
Calin Juravle357c66d2017-05-04 01:57:17 +0000549static bool DexLocationToOdexNames(const std::string& location,
550 InstructionSet isa,
551 std::string* odex_filename,
552 std::string* oat_dir,
553 std::string* isa_dir,
554 std::string* error_msg) {
555 CHECK(odex_filename != nullptr);
556 CHECK(error_msg != nullptr);
557
558 // The odex file name is formed by replacing the dex_location extension with
559 // .odex and inserting an oat/<isa> directory. For example:
560 // location = /foo/bar/baz.jar
561 // odex_location = /foo/bar/oat/<isa>/baz.odex
562
563 // Find the directory portion of the dex location and add the oat/<isa>
564 // directory.
565 size_t pos = location.rfind('/');
566 if (pos == std::string::npos) {
567 *error_msg = "Dex location " + location + " has no directory.";
568 return false;
569 }
570 std::string dir = location.substr(0, pos+1);
571 // Add the oat directory.
572 dir += "oat";
573 if (oat_dir != nullptr) {
574 *oat_dir = dir;
575 }
576 // Add the isa directory
577 dir += "/" + std::string(GetInstructionSetString(isa));
578 if (isa_dir != nullptr) {
579 *isa_dir = dir;
580 }
581
582 // Get the base part of the file without the extension.
583 std::string file = location.substr(pos+1);
584 pos = file.rfind('.');
585 if (pos == std::string::npos) {
586 *error_msg = "Dex location " + location + " has no extension.";
587 return false;
588 }
589 std::string base = file.substr(0, pos);
590
591 *odex_filename = dir + "/" + base + ".odex";
592 return true;
593}
594
595// Prepare a subcomponent of the odex directory.
596// (i.e. create and set the expected permissions on the path `dir`).
597static bool PrepareDirectory(const std::string& dir, std::string* error_msg) {
598 struct stat dir_stat;
599 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) {
600 // The directory exists. Check if it is indeed a directory.
601 if (!S_ISDIR(dir_stat.st_mode)) {
602 *error_msg = dir + " is not a dir";
603 return false;
604 } else {
605 // The dir is already on disk.
606 return true;
607 }
608 }
609
610 // Failed to stat. We need to create the directory.
611 if (errno != ENOENT) {
612 *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno);
613 return false;
614 }
615
616 mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH;
617 if (mkdir(dir.c_str(), mode) != 0) {
618 *error_msg = "Could not create dir " + dir + ":" + strerror(errno);
619 return false;
620 }
621 if (chmod(dir.c_str(), mode) != 0) {
622 *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno);
623 return false;
624 }
625 return true;
626}
627
628// Prepares the odex directory for the given dex location.
629static bool PrepareOdexDirectories(const std::string& dex_location,
630 const std::string& expected_odex_location,
631 InstructionSet isa,
632 std::string* error_msg) {
633 std::string actual_odex_location;
634 std::string oat_dir;
635 std::string isa_dir;
636 if (!DexLocationToOdexNames(
637 dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) {
638 return false;
639 }
640 DCHECK_EQ(expected_odex_location, actual_odex_location);
641
642 if (!PrepareDirectory(oat_dir, error_msg)) {
643 return false;
644 }
645 if (!PrepareDirectory(isa_dir, error_msg)) {
646 return false;
647 }
648 return true;
649}
650
Calin Juravled4215bb2017-09-20 11:54:21 -0700651class Dex2oatFileWrapper {
652 public:
653 explicit Dex2oatFileWrapper(File* file)
654 : file_(file),
655 unlink_file_at_destruction_(true) {
656 }
657
658 ~Dex2oatFileWrapper() {
659 if (unlink_file_at_destruction_ && (file_ != nullptr)) {
660 file_->Erase(/*unlink*/ true);
661 }
662 }
663
664 File* GetFile() { return file_.get(); }
665
666 void DisableUnlinkAtDestruction() {
667 unlink_file_at_destruction_ = false;
668 };
669
670 private:
671 std::unique_ptr<File> file_;
672 bool unlink_file_at_destruction_;
673};
674
Calin Juravle357c66d2017-05-04 01:57:17 +0000675OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks(
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700676 OatFileAssistant::OatFileInfo& info,
677 CompilerFilter::Filter filter,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700678 const ClassLoaderContext* class_loader_context,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700679 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800680 CHECK(error_msg != nullptr);
681
Richard Uhler8327cf72015-10-13 16:34:59 -0700682 Runtime* runtime = Runtime::Current();
683 if (!runtime->IsDex2OatEnabled()) {
684 *error_msg = "Generation of oat file for dex location " + dex_location_
685 + " not attempted because dex2oat is disabled.";
Richard Uhler1e860612016-03-30 12:17:55 -0700686 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700687 }
688
Calin Juravle357c66d2017-05-04 01:57:17 +0000689 if (info.Filename() == nullptr) {
Richard Uhler740eec92015-10-15 15:12:23 -0700690 *error_msg = "Generation of oat file for dex location " + dex_location_
Richard Uhler66d874d2015-01-15 09:37:19 -0800691 + " not attempted because the oat file name could not be determined.";
Richard Uhler1e860612016-03-30 12:17:55 -0700692 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800693 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000694 const std::string& oat_file_name = *info.Filename();
Calin Juravle367b9d82017-05-15 18:18:39 -0700695 const std::string& vdex_file_name = GetVdexFilename(oat_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800696
Richard Uhler66d874d2015-01-15 09:37:19 -0800697 // dex2oat ignores missing dex files and doesn't report an error.
698 // Check explicitly here so we can detect the error properly.
699 // TODO: Why does dex2oat behave that way?
Calin Juravle357c66d2017-05-04 01:57:17 +0000700 struct stat dex_path_stat;
701 if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) {
702 *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno);
Richard Uhler1e860612016-03-30 12:17:55 -0700703 return kUpdateNotAttempted;
Richard Uhler66d874d2015-01-15 09:37:19 -0800704 }
705
Calin Juravle357c66d2017-05-04 01:57:17 +0000706 // If this is the odex location, we need to create the odex file layout (../oat/isa/..)
707 if (!info.IsOatLocation()) {
708 if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) {
709 return kUpdateNotAttempted;
710 }
711 }
712
713 // Set the permissions for the oat and the vdex files.
714 // The user always gets read and write while the group and others propagate
715 // the reading access of the original dex file.
716 mode_t file_mode = S_IRUSR | S_IWUSR |
717 (dex_path_stat.st_mode & S_IRGRP) |
718 (dex_path_stat.st_mode & S_IROTH);
719
Calin Juravled4215bb2017-09-20 11:54:21 -0700720 Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str()));
721 File* vdex_file = vdex_file_wrapper.GetFile();
722 if (vdex_file == nullptr) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100723 *error_msg = "Generation of oat file " + oat_file_name
724 + " not attempted because the vdex file " + vdex_file_name
725 + " could not be opened.";
726 return kUpdateNotAttempted;
727 }
728
Calin Juravle357c66d2017-05-04 01:57:17 +0000729 if (fchmod(vdex_file->Fd(), file_mode) != 0) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100730 *error_msg = "Generation of oat file " + oat_file_name
731 + " not attempted because the vdex file " + vdex_file_name
732 + " could not be made world readable.";
733 return kUpdateNotAttempted;
734 }
735
Calin Juravled4215bb2017-09-20 11:54:21 -0700736 Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str()));
737 File* oat_file = oat_file_wrapper.GetFile();
738 if (oat_file == nullptr) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700739 *error_msg = "Generation of oat file " + oat_file_name
740 + " not attempted because the oat file could not be created.";
Richard Uhler1e860612016-03-30 12:17:55 -0700741 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700742 }
743
Calin Juravle357c66d2017-05-04 01:57:17 +0000744 if (fchmod(oat_file->Fd(), file_mode) != 0) {
Richard Uhler8327cf72015-10-13 16:34:59 -0700745 *error_msg = "Generation of oat file " + oat_file_name
746 + " not attempted because the oat file could not be made world readable.";
Richard Uhler1e860612016-03-30 12:17:55 -0700747 return kUpdateNotAttempted;
Richard Uhler8327cf72015-10-13 16:34:59 -0700748 }
749
750 std::vector<std::string> args;
751 args.push_back("--dex-file=" + dex_location_);
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000752 args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
Richard Uhler8327cf72015-10-13 16:34:59 -0700753 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
754 args.push_back("--oat-location=" + oat_file_name);
Calin Juravle07c6d722017-06-07 17:06:12 +0000755 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Calin Juravle44e5efa2017-09-12 00:54:26 -0700756 const std::string dex2oat_context = class_loader_context == nullptr
757 ? OatFile::kSpecialSharedLibrary
758 : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ "");
759 args.push_back("--class-loader-context=" + dex2oat_context);
Richard Uhler8327cf72015-10-13 16:34:59 -0700760
Richard Uhler66d874d2015-01-15 09:37:19 -0800761 if (!Dex2Oat(args, error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700762 return kUpdateFailed;
Richard Uhler8327cf72015-10-13 16:34:59 -0700763 }
764
David Brazdil7b49e6c2016-09-01 11:06:18 +0100765 if (vdex_file->FlushCloseOrErase() != 0) {
766 *error_msg = "Unable to close vdex file " + vdex_file_name;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100767 return kUpdateFailed;
768 }
769
Richard Uhler8327cf72015-10-13 16:34:59 -0700770 if (oat_file->FlushCloseOrErase() != 0) {
771 *error_msg = "Unable to close oat file " + oat_file_name;
Richard Uhler1e860612016-03-30 12:17:55 -0700772 return kUpdateFailed;
Richard Uhler66d874d2015-01-15 09:37:19 -0800773 }
774
Calin Juravle357c66d2017-05-04 01:57:17 +0000775 // Mark that the odex file has changed and we should try to reload.
776 info.Reset();
Calin Juravled4215bb2017-09-20 11:54:21 -0700777 // We have compiled successfully. Disable the auto-unlink.
778 vdex_file_wrapper.DisableUnlinkAtDestruction();
779 oat_file_wrapper.DisableUnlinkAtDestruction();
780
Richard Uhler1e860612016-03-30 12:17:55 -0700781 return kUpdateSucceeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800782}
783
784bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
785 std::string* error_msg) {
786 Runtime* runtime = Runtime::Current();
787 std::string image_location = ImageLocation();
788 if (image_location.empty()) {
789 *error_msg = "No image location found for Dex2Oat.";
790 return false;
791 }
792
793 std::vector<std::string> argv;
794 argv.push_back(runtime->GetCompilerExecutable());
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000795 if (runtime->IsJavaDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200796 argv.push_back("--debuggable");
797 }
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700798 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
Richard Uhler66d874d2015-01-15 09:37:19 -0800799
800 if (!runtime->IsVerificationEnabled()) {
801 argv.push_back("--compiler-filter=verify-none");
802 }
803
804 if (runtime->MustRelocateIfPossible()) {
805 argv.push_back("--runtime-arg");
806 argv.push_back("-Xrelocate");
807 } else {
808 argv.push_back("--runtime-arg");
809 argv.push_back("-Xnorelocate");
810 }
811
812 if (!kIsTargetBuild) {
813 argv.push_back("--host");
814 }
815
816 argv.push_back("--boot-image=" + image_location);
817
818 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
819 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
820
821 argv.insert(argv.end(), args.begin(), args.end());
822
Andreas Gampe9186ced2016-12-12 14:28:21 -0800823 std::string command_line(android::base::Join(argv, ' '));
Richard Uhler66d874d2015-01-15 09:37:19 -0800824 return Exec(argv, error_msg);
825}
826
Richard Uhlerb81881d2016-04-19 13:08:04 -0700827bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
828 InstructionSet isa,
829 std::string* odex_filename,
830 std::string* error_msg) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000831 return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800832}
833
Richard Uhlerb81881d2016-04-19 13:08:04 -0700834bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
835 InstructionSet isa,
836 std::string* oat_filename,
837 std::string* error_msg) {
838 CHECK(oat_filename != nullptr);
839 CHECK(error_msg != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800840
Richard Uhler55b58b62016-08-12 09:05:13 -0700841 std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa));
842 if (cache_dir.empty()) {
843 *error_msg = "Dalvik cache directory does not exist";
844 return false;
845 }
Richard Uhlerb81881d2016-04-19 13:08:04 -0700846
847 // TODO: The oat file assistant should be the definitive place for
848 // determining the oat file name from the dex location, not
849 // GetDalvikCacheFilename.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700850 return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800851}
852
Richard Uhler66d874d2015-01-15 09:37:19 -0800853std::string OatFileAssistant::ImageLocation() {
854 Runtime* runtime = Runtime::Current();
Andreas Gampe8994a042015-12-30 19:03:17 +0000855 const std::vector<gc::space::ImageSpace*>& image_spaces =
856 runtime->GetHeap()->GetBootImageSpaces();
857 if (image_spaces.empty()) {
858 return "";
859 }
860 return image_spaces[0]->GetImageLocation();
Richard Uhler66d874d2015-01-15 09:37:19 -0800861}
862
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000863const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() {
864 if (!required_dex_checksums_attempted_) {
865 required_dex_checksums_attempted_ = true;
866 required_dex_checksums_found_ = false;
867 cached_required_dex_checksums_.clear();
Richard Uhler66d874d2015-01-15 09:37:19 -0800868 std::string error_msg;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700869 if (DexFileLoader::GetMultiDexChecksums(dex_location_.c_str(),
870 &cached_required_dex_checksums_,
871 &error_msg)) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000872 required_dex_checksums_found_ = true;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700873 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800874 } else {
875 // This can happen if the original dex file has been stripped from the
876 // apk.
877 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700878 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800879
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000880 // Get the checksums from the odex if we can.
Richard Uhler743bf362016-04-19 15:39:37 -0700881 const OatFile* odex_file = odex_.GetFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800882 if (odex_file != nullptr) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000883 required_dex_checksums_found_ = true;
884 for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700885 std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000886 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr);
887 if (odex_dex_file == nullptr) {
888 required_dex_checksums_found_ = false;
889 break;
890 }
891 cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -0800892 }
893 }
894 }
895 }
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000896 return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800897}
898
Richard Uhler95e09672017-02-22 11:37:41 +0000899std::unique_ptr<OatFileAssistant::ImageInfo>
900OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) {
901 CHECK(error_msg != nullptr);
902
Richard Uhler95e09672017-02-22 11:37:41 +0000903 Runtime* runtime = Runtime::Current();
Richard Uhler8f104862017-02-22 12:34:01 +0000904 std::unique_ptr<ImageInfo> info(new ImageInfo());
905 info->location = runtime->GetImageLocation();
906
907 std::unique_ptr<ImageHeader> image_header(
908 gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg));
909 if (image_header == nullptr) {
Richard Uhler95e09672017-02-22 11:37:41 +0000910 return nullptr;
911 }
912
Richard Uhler8f104862017-02-22 12:34:01 +0000913 info->oat_checksum = image_header->GetOatChecksum();
914 info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
915 info->patch_delta = image_header->GetPatchDelta();
Richard Uhler95e09672017-02-22 11:37:41 +0000916 return info;
917}
918
Richard Uhler66d874d2015-01-15 09:37:19 -0800919const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
920 if (!image_info_load_attempted_) {
921 image_info_load_attempted_ = true;
Richard Uhler95e09672017-02-22 11:37:41 +0000922 std::string error_msg;
923 cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg);
924 if (cached_image_info_ == nullptr) {
925 LOG(WARNING) << "Unable to get runtime image info: " << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800926 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800927 }
Richard Uhler95e09672017-02-22 11:37:41 +0000928 return cached_image_info_.get();
Richard Uhler66d874d2015-01-15 09:37:19 -0800929}
930
Richard Uhler88bc6732016-11-14 14:38:03 +0000931OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
Calin Juravle357c66d2017-05-04 01:57:17 +0000932 // TODO(calin): Document the side effects of class loading when
933 // running dalvikvm command line.
934 if (dex_parent_writable_) {
935 // If the parent of the dex file is writable it means that we can
936 // create the odex file. In this case we unconditionally pick the odex
937 // as the best oat file. This corresponds to the regular use case when
938 // apps gets installed or when they load private, secondary dex file.
939 // For apps on the system partition the odex location will not be
940 // writable and thus the oat location might be more up to date.
941 return odex_;
942 }
943
944 // We cannot write to the odex location. This must be a system app.
945
946 // If the oat location is usable take it.
947 if (oat_.IsUseable()) {
948 return oat_;
949 }
950
951 // The oat file is not usable but the odex file might be up to date.
952 // This is an indication that we are dealing with an up to date prebuilt
953 // (that doesn't need relocation).
954 if (odex_.Status() == kOatUpToDate) {
955 return odex_;
956 }
957
958 // The oat file is not usable and the odex file is not up to date.
959 // However we have access to the original dex file which means we can make
960 // the oat location up to date.
961 if (HasOriginalDexFiles()) {
962 return oat_;
963 }
964
965 // We got into the worst situation here:
966 // - the oat location is not usable
967 // - the prebuild odex location is not up to date
968 // - and we don't have the original dex file anymore (stripped).
969 // Pick the odex if it exists, or the oat if not.
970 return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
Richard Uhler88bc6732016-11-14 14:38:03 +0000971}
972
Andreas Gampea463b6a2016-08-12 21:53:32 -0700973std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800974 DCHECK(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100975 std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800976 if (art_file.empty()) {
977 return nullptr;
978 }
979 std::string error_msg;
980 ScopedObjectAccess soa(Thread::Current());
Andreas Gampea463b6a2016-08-12 21:53:32 -0700981 std::unique_ptr<gc::space::ImageSpace> ret =
982 gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
Mathieu Chartiere778fc72016-01-25 20:11:28 -0800983 if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800984 LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
985 }
986 return ret;
987}
988
Richard Uhler88bc6732016-11-14 14:38:03 +0000989OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
990 bool is_oat_location)
991 : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location)
Richard Uhler743bf362016-04-19 15:39:37 -0700992{}
993
Richard Uhler88bc6732016-11-14 14:38:03 +0000994bool OatFileAssistant::OatFileInfo::IsOatLocation() {
995 return is_oat_location_;
996}
997
Richard Uhler743bf362016-04-19 15:39:37 -0700998const std::string* OatFileAssistant::OatFileInfo::Filename() {
999 return filename_provided_ ? &filename_ : nullptr;
1000}
1001
Richard Uhler03bc6592016-11-22 09:42:04 +00001002bool OatFileAssistant::OatFileInfo::IsUseable() {
1003 switch (Status()) {
1004 case kOatCannotOpen:
1005 case kOatDexOutOfDate:
1006 case kOatBootImageOutOfDate: return false;
1007
1008 case kOatRelocationOutOfDate:
1009 case kOatUpToDate: return true;
1010 }
1011 UNREACHABLE();
Richard Uhler743bf362016-04-19 15:39:37 -07001012}
1013
1014OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
1015 if (!status_attempted_) {
1016 status_attempted_ = true;
1017 const OatFile* file = GetFile();
1018 if (file == nullptr) {
Richard Uhler2f27abd2017-01-31 14:02:34 +00001019 // Check to see if there is a vdex file we can make use of.
1020 std::string error_msg;
Calin Juravle367b9d82017-05-15 18:18:39 -07001021 std::string vdex_filename = GetVdexFilename(filename_);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001022 std::unique_ptr<VdexFile> vdex;
1023 if (vdex_fd_ == -1) {
1024 vdex = VdexFile::Open(vdex_filename,
1025 false /*writeable*/,
1026 false /*low_4gb*/,
1027 false /*unquicken*/,
1028 &error_msg);
1029 } else {
1030 struct stat s;
1031 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1032 if (rc == -1) {
1033 PLOG(WARNING) << "Failed getting length of vdex file";
1034 } else {
1035 vdex = VdexFile::Open(vdex_fd_,
1036 s.st_size,
1037 vdex_filename,
1038 false /*writable*/,
1039 false /*low_4gb*/,
1040 false /* unquicken */,
1041 &error_msg);
1042 }
1043 }
Richard Uhler2f27abd2017-01-31 14:02:34 +00001044 if (vdex == nullptr) {
1045 status_ = kOatCannotOpen;
1046 VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg;
1047 } else {
1048 if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) {
1049 // The vdex file does not contain enough information to determine
1050 // whether it is up to date with respect to the boot image, so we
1051 // assume it is out of date.
1052 VLOG(oat) << error_msg;
1053 status_ = kOatBootImageOutOfDate;
1054 } else {
1055 status_ = kOatDexOutOfDate;
1056 }
1057 }
Richard Uhler743bf362016-04-19 15:39:37 -07001058 } else {
1059 status_ = oat_file_assistant_->GivenOatFileStatus(*file);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001060 VLOG(oat) << file->GetLocation() << " is " << status_
1061 << " with filter " << file->GetCompilerFilter();
Richard Uhler743bf362016-04-19 15:39:37 -07001062 }
1063 }
1064 return status_;
1065}
1066
Richard Uhler70a84262016-11-08 16:51:51 +00001067OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
Calin Juravle44e5efa2017-09-12 00:54:26 -07001068 CompilerFilter::Filter target,
1069 bool profile_changed,
1070 bool downgrade,
1071 ClassLoaderContext* context) {
1072
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001073 bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001074 bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade);
Calin Juravle44e5efa2017-09-12 00:54:26 -07001075 bool class_loader_context_okay = ClassLoaderContextIsOkay(context);
Richard Uhler70a84262016-11-08 16:51:51 +00001076
Calin Juravle44e5efa2017-09-12 00:54:26 -07001077 // Only check the filter and relocation if the class loader context is ok.
1078 // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone.
1079 if (class_loader_context_okay) {
1080 if (filter_okay && Status() == kOatUpToDate) {
1081 // The oat file is in good shape as is.
1082 return kNoDexOptNeeded;
1083 }
Richard Uhler70a84262016-11-08 16:51:51 +00001084
Calin Juravle44e5efa2017-09-12 00:54:26 -07001085 if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) {
1086 // If no compilation is desired, then it doesn't matter if the oat
1087 // file needs relocation. It's in good shape as is.
1088 return kNoDexOptNeeded;
1089 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001090
Calin Juravle44e5efa2017-09-12 00:54:26 -07001091 if (filter_okay && Status() == kOatRelocationOutOfDate) {
1092 return kDex2OatForRelocation;
1093 }
Richard Uhler7225a8d2016-11-22 10:12:03 +00001094
Calin Juravle44e5efa2017-09-12 00:54:26 -07001095 if (IsUseable()) {
1096 return kDex2OatForFilter;
1097 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001098
Calin Juravle44e5efa2017-09-12 00:54:26 -07001099 if (Status() == kOatBootImageOutOfDate) {
1100 return kDex2OatForBootImage;
1101 }
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +01001102 }
1103
1104 if (oat_file_assistant_->HasOriginalDexFiles()) {
1105 return kDex2OatFromScratch;
1106 } else {
1107 // Otherwise there is nothing we can do, even if we want to.
1108 return kNoDexOptNeeded;
1109 }
Richard Uhler70a84262016-11-08 16:51:51 +00001110}
1111
Richard Uhler743bf362016-04-19 15:39:37 -07001112const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1113 CHECK(!file_released_) << "GetFile called after oat file released.";
1114 if (!load_attempted_) {
1115 load_attempted_ = true;
1116 if (filename_provided_) {
1117 std::string error_msg;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001118 if (oat_fd_ != -1 && vdex_fd_ != -1) {
1119 file_.reset(OatFile::Open(vdex_fd_,
1120 oat_fd_,
1121 filename_.c_str(),
1122 nullptr,
1123 nullptr,
1124 oat_file_assistant_->load_executable_,
1125 false /* low_4gb */,
1126 oat_file_assistant_->dex_location_.c_str(),
1127 &error_msg));
1128 } else {
1129 file_.reset(OatFile::Open(filename_.c_str(),
1130 filename_.c_str(),
1131 nullptr,
1132 nullptr,
1133 oat_file_assistant_->load_executable_,
1134 false /* low_4gb */,
1135 oat_file_assistant_->dex_location_.c_str(),
1136 &error_msg));
1137 }
Richard Uhler743bf362016-04-19 15:39:37 -07001138 if (file_.get() == nullptr) {
1139 VLOG(oat) << "OatFileAssistant test for existing oat file "
1140 << filename_ << ": " << error_msg;
1141 }
1142 }
1143 }
1144 return file_.get();
1145}
1146
1147bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay(
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001148 CompilerFilter::Filter target, bool profile_changed, bool downgrade) {
Richard Uhler743bf362016-04-19 15:39:37 -07001149 const OatFile* file = GetFile();
1150 if (file == nullptr) {
1151 return false;
1152 }
1153
1154 CompilerFilter::Filter current = file->GetCompilerFilter();
1155 if (profile_changed && CompilerFilter::DependsOnProfile(current)) {
1156 VLOG(oat) << "Compiler filter not okay because Profile changed";
1157 return false;
1158 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -07001159 return downgrade ? !CompilerFilter::IsBetter(current, target) :
1160 CompilerFilter::IsAsGoodAs(current, target);
Richard Uhler743bf362016-04-19 15:39:37 -07001161}
1162
Calin Juravle44e5efa2017-09-12 00:54:26 -07001163bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) {
1164 if (context == nullptr) {
1165 VLOG(oat) << "ClassLoaderContext check ignored: null context";
1166 return true;
1167 }
1168
1169 const OatFile* file = GetFile();
1170 if (file == nullptr) {
Calin Juravle20c46442017-09-12 00:54:26 -07001171 // No oat file means we have nothing to verify.
1172 return true;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001173 }
1174
Calin Juravle20c46442017-09-12 00:54:26 -07001175 size_t dir_index = oat_file_assistant_->dex_location_.rfind('/');
Calin Juravle44e5efa2017-09-12 00:54:26 -07001176 std::string classpath_dir = (dir_index != std::string::npos)
Calin Juravle20c46442017-09-12 00:54:26 -07001177 ? oat_file_assistant_->dex_location_.substr(0, dir_index)
Calin Juravle44e5efa2017-09-12 00:54:26 -07001178 : "";
1179
1180 if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) {
1181 VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened";
1182 return false;
1183 }
1184
1185 bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext());
1186 if (!result) {
1187 VLOG(oat) << "ClassLoaderContext check failed. Context was "
1188 << file->GetClassLoaderContext()
1189 << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir);
1190 }
1191 return result;
1192}
1193
Richard Uhler743bf362016-04-19 15:39:37 -07001194bool OatFileAssistant::OatFileInfo::IsExecutable() {
1195 const OatFile* file = GetFile();
1196 return (file != nullptr && file->IsExecutable());
1197}
1198
Richard Uhler743bf362016-04-19 15:39:37 -07001199void OatFileAssistant::OatFileInfo::Reset() {
1200 load_attempted_ = false;
1201 file_.reset();
1202 status_attempted_ = false;
1203}
1204
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001205void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, int vdex_fd,
1206 int oat_fd) {
Richard Uhler743bf362016-04-19 15:39:37 -07001207 filename_provided_ = true;
1208 filename_ = filename;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001209 vdex_fd_ = vdex_fd;
1210 oat_fd_ = oat_fd;
Richard Uhler743bf362016-04-19 15:39:37 -07001211 Reset();
1212}
1213
1214std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1215 file_released_ = true;
1216 return std::move(file_);
1217}
1218
Richard Uhler70a84262016-11-08 16:51:51 +00001219std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
Richard Uhler3e580bc2016-11-08 16:23:07 +00001220 if (Status() == kOatUpToDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001221 return ReleaseFile();
1222 }
1223
1224 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
1225 << " attempting to fall back to interpreting oat file instead.";
1226
Richard Uhler03bc6592016-11-22 09:42:04 +00001227 if (Status() == kOatRelocationOutOfDate && !IsExecutable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001228 return ReleaseFile();
1229 }
1230
Richard Uhler03bc6592016-11-22 09:42:04 +00001231 if (Status() == kOatRelocationOutOfDate) {
Richard Uhler70a84262016-11-08 16:51:51 +00001232 // We are loading an oat file for runtime use that needs relocation.
1233 // Reload the file non-executable to ensure that we interpret out of the
1234 // dex code in the oat file rather than trying to execute the unrelocated
1235 // compiled code.
1236 oat_file_assistant_->load_executable_ = false;
1237 Reset();
Richard Uhler03bc6592016-11-22 09:42:04 +00001238 if (IsUseable()) {
Richard Uhler70a84262016-11-08 16:51:51 +00001239 CHECK(!IsExecutable());
1240 return ReleaseFile();
1241 }
1242 }
1243 return std::unique_ptr<OatFile>();
1244}
Richard Uhler66d874d2015-01-15 09:37:19 -08001245} // namespace art