blob: e919b36b9926e6245fd29bfdaf88b8bb1cd96a58 [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
19#include <fcntl.h>
20#ifdef __linux__
21#include <sys/sendfile.h>
22#else
23#include <sys/socket.h>
24#endif
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include <set>
30
31#include "base/logging.h"
Igor Murashkina315f5c2015-07-31 17:35:52 -070032#include "base/out.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "base/stringprintf.h"
34#include "class_linker.h"
35#include "gc/heap.h"
36#include "gc/space/image_space.h"
37#include "image.h"
38#include "oat.h"
39#include "os.h"
40#include "profiler.h"
41#include "runtime.h"
42#include "ScopedFd.h"
43#include "utils.h"
44
45namespace art {
46
47OatFileAssistant::OatFileAssistant(const char* dex_location,
48 const InstructionSet isa,
49 bool load_executable)
50 : OatFileAssistant(dex_location, nullptr, isa, load_executable, nullptr) { }
51
52OatFileAssistant::OatFileAssistant(const char* dex_location,
53 const char* oat_location,
54 const InstructionSet isa,
55 bool load_executable)
56 : OatFileAssistant(dex_location, oat_location, isa, load_executable, nullptr) { }
57
58OatFileAssistant::OatFileAssistant(const char* dex_location,
59 const InstructionSet isa,
60 bool load_executable,
61 const char* package_name)
62 : OatFileAssistant(dex_location, nullptr, isa, load_executable, package_name) { }
63
64OatFileAssistant::OatFileAssistant(const char* dex_location,
65 const char* oat_location,
66 const InstructionSet isa,
67 bool load_executable,
68 const char* package_name)
69 : dex_location_(dex_location), isa_(isa),
70 package_name_(package_name), load_executable_(load_executable) {
71 if (load_executable_ && isa != kRuntimeISA) {
72 LOG(WARNING) << "OatFileAssistant: Load executable specified, "
73 << "but isa is not kRuntimeISA. Will not attempt to load executable.";
74 load_executable_ = false;
75 }
76
77 // If the user gave a target oat location, save that as the cached oat
78 // location now so we won't try to construct the default location later.
79 if (oat_location != nullptr) {
80 cached_oat_file_name_ = std::string(oat_location);
81 cached_oat_file_name_attempted_ = true;
82 cached_oat_file_name_found_ = true;
83 }
84
85 // If there is no package name given, we will not be able to find any
86 // profiles associated with this dex location. Preemptively mark that to
87 // be the case, rather than trying to find and load the profiles later.
88 // Similarly, if profiling is disabled.
89 if (package_name == nullptr
90 || !Runtime::Current()->GetProfilerOptions().IsEnabled()) {
91 profile_load_attempted_ = true;
92 profile_load_succeeded_ = false;
93 old_profile_load_attempted_ = true;
94 old_profile_load_succeeded_ = false;
95 }
96}
97
98OatFileAssistant::~OatFileAssistant() {
99 // Clean up the lock file.
Richard Uhler581f4e92015-05-07 10:19:35 -0700100 if (flock_.HasFile()) {
101 TEMP_FAILURE_RETRY(unlink(flock_.GetFile()->GetPath().c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800102 }
103}
104
105bool OatFileAssistant::IsInBootClassPath() {
106 // Note: We check the current boot class path, regardless of the ISA
107 // specified by the user. This is okay, because the boot class path should
108 // be the same for all ISAs.
109 // TODO: Can we verify the boot class path is the same for all ISAs?
110 Runtime* runtime = Runtime::Current();
111 ClassLinker* class_linker = runtime->GetClassLinker();
112 const auto& boot_class_path = class_linker->GetBootClassPath();
113 for (size_t i = 0; i < boot_class_path.size(); i++) {
114 if (boot_class_path[i]->GetLocation() == std::string(dex_location_)) {
115 VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
116 return true;
117 }
118 }
119 return false;
120}
121
122bool OatFileAssistant::Lock(std::string* error_msg) {
123 CHECK(error_msg != nullptr);
Richard Uhler581f4e92015-05-07 10:19:35 -0700124 CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired";
Richard Uhler66d874d2015-01-15 09:37:19 -0800125
126 if (OatFileName() == nullptr) {
127 *error_msg = "Failed to determine lock file";
128 return false;
129 }
130 std::string lock_file_name = *OatFileName() + ".flock";
131
Richard Uhler581f4e92015-05-07 10:19:35 -0700132 if (!flock_.Init(lock_file_name.c_str(), error_msg)) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800133 TEMP_FAILURE_RETRY(unlink(lock_file_name.c_str()));
134 return false;
135 }
136 return true;
137}
138
Richard Uhler95abd042015-03-24 09:51:28 -0700139OatFileAssistant::DexOptNeeded OatFileAssistant::GetDexOptNeeded() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800140 // TODO: If the profiling code is ever restored, it's worth considering
141 // whether we should check to see if the profile is out of date here.
142
Richard Uhler95abd042015-03-24 09:51:28 -0700143 if (OatFileIsUpToDate() || OdexFileIsUpToDate()) {
144 return kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800145 }
Richard Uhler95abd042015-03-24 09:51:28 -0700146
147 if (OdexFileNeedsRelocation()) {
148 return kPatchOatNeeded;
149 }
150
151 if (OatFileNeedsRelocation()) {
152 return kSelfPatchOatNeeded;
153 }
154
Richard Uhler9b994ea2015-06-24 08:44:19 -0700155 return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800156}
157
158bool OatFileAssistant::MakeUpToDate(std::string* error_msg) {
Richard Uhler95abd042015-03-24 09:51:28 -0700159 switch (GetDexOptNeeded()) {
160 case kNoDexOptNeeded: return true;
161 case kDex2OatNeeded: return GenerateOatFile(error_msg);
162 case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
163 case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
Richard Uhler66d874d2015-01-15 09:37:19 -0800164 }
165 UNREACHABLE();
166}
167
168std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
Richard Uhler5f946da2015-07-17 12:28:32 -0700169 // The best oat files are, in descending order of bestness:
170 // 1. Properly relocated files. These may be opened executable.
171 // 2. Not out-of-date files that are already opened non-executable.
172 // 3. Not out-of-date files that we must reopen non-executable.
173
Richard Uhler66d874d2015-01-15 09:37:19 -0800174 if (OatFileIsUpToDate()) {
175 oat_file_released_ = true;
176 return std::move(cached_oat_file_);
177 }
178
179 if (OdexFileIsUpToDate()) {
180 oat_file_released_ = true;
181 return std::move(cached_odex_file_);
182 }
183
Richard Uhler5f946da2015-07-17 12:28:32 -0700184 VLOG(oat) << "Oat File Assistant: No relocated oat file found,"
185 << " attempting to fall back to interpreting oat file instead.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800186
Richard Uhler5f946da2015-07-17 12:28:32 -0700187 if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) {
188 oat_file_released_ = true;
189 return std::move(cached_oat_file_);
190 }
191
192 if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) {
193 oat_file_released_ = true;
194 return std::move(cached_odex_file_);
195 }
196
197 if (!OatFileIsOutOfDate()) {
198 load_executable_ = false;
199 ClearOatFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800200 if (!OatFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700201 CHECK(!OatFileIsExecutable());
202 oat_file_released_ = true;
203 return std::move(cached_oat_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800204 }
Richard Uhler5f946da2015-07-17 12:28:32 -0700205 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800206
Richard Uhler5f946da2015-07-17 12:28:32 -0700207 if (!OdexFileIsOutOfDate()) {
208 load_executable_ = false;
209 ClearOdexFileCache();
Richard Uhler66d874d2015-01-15 09:37:19 -0800210 if (!OdexFileIsOutOfDate()) {
Richard Uhler5f946da2015-07-17 12:28:32 -0700211 CHECK(!OdexFileIsExecutable());
212 oat_file_released_ = true;
213 return std::move(cached_odex_file_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800214 }
215 }
216
217 return std::unique_ptr<OatFile>();
218}
219
220std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
221 const OatFile& oat_file, const char* dex_location) {
222 std::vector<std::unique_ptr<const DexFile>> dex_files;
223
224 // Load the primary dex file.
225 std::string error_msg;
226 const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(
227 dex_location, nullptr, false);
228 if (oat_dex_file == nullptr) {
229 LOG(WARNING) << "Attempt to load out-of-date oat file "
230 << oat_file.GetLocation() << " for dex location " << dex_location;
231 return std::vector<std::unique_ptr<const DexFile>>();
232 }
233
Igor Murashkina315f5c2015-07-31 17:35:52 -0700234 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(outof(error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800235 if (dex_file.get() == nullptr) {
236 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
237 return std::vector<std::unique_ptr<const DexFile>>();
238 }
239 dex_files.push_back(std::move(dex_file));
240
241 // Load secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700242 for (size_t i = 1; ; i++) {
243 std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800244 oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700245 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800246 // There are no more secondary dex files to load.
247 break;
248 }
249
Igor Murashkina315f5c2015-07-31 17:35:52 -0700250 dex_file = oat_dex_file->OpenDexFile(outof(error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800251 if (dex_file.get() == nullptr) {
252 LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
253 return std::vector<std::unique_ptr<const DexFile>>();
254 }
255 dex_files.push_back(std::move(dex_file));
256 }
257 return dex_files;
258}
259
Richard Uhler9b994ea2015-06-24 08:44:19 -0700260bool OatFileAssistant::HasOriginalDexFiles() {
261 // Ensure GetRequiredDexChecksum has been run so that
262 // has_original_dex_files_ is initialized. We don't care about the result of
263 // GetRequiredDexChecksum.
264 GetRequiredDexChecksum();
265 return has_original_dex_files_;
266}
267
Richard Uhler66d874d2015-01-15 09:37:19 -0800268const std::string* OatFileAssistant::OdexFileName() {
269 if (!cached_odex_file_name_attempted_) {
270 CHECK(dex_location_ != nullptr) << "OatFileAssistant: null dex location";
271 cached_odex_file_name_attempted_ = true;
272
273 std::string error_msg;
274 cached_odex_file_name_found_ = DexFilenameToOdexFilename(
Igor Murashkina315f5c2015-07-31 17:35:52 -0700275 dex_location_, isa_, &cached_odex_file_name_, outof(error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 if (!cached_odex_file_name_found_) {
277 // If we can't figure out the odex file, we treat it as if the odex
278 // file was inaccessible.
279 LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
280 }
281 }
282 return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr;
283}
284
285bool OatFileAssistant::OdexFileExists() {
286 return GetOdexFile() != nullptr;
287}
288
Richard Uhler95abd042015-03-24 09:51:28 -0700289OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800290 if (OdexFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700291 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800292 }
293 if (OdexFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700294 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800295 }
Richard Uhler95abd042015-03-24 09:51:28 -0700296 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800297}
298
299bool OatFileAssistant::OdexFileIsOutOfDate() {
300 if (!odex_file_is_out_of_date_attempted_) {
301 odex_file_is_out_of_date_attempted_ = true;
302 const OatFile* odex_file = GetOdexFile();
303 if (odex_file == nullptr) {
304 cached_odex_file_is_out_of_date_ = true;
305 } else {
306 cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file);
307 }
308 }
309 return cached_odex_file_is_out_of_date_;
310}
311
312bool OatFileAssistant::OdexFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700313 return OdexFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800314}
315
316bool OatFileAssistant::OdexFileIsUpToDate() {
317 if (!odex_file_is_up_to_date_attempted_) {
318 odex_file_is_up_to_date_attempted_ = true;
319 const OatFile* odex_file = GetOdexFile();
320 if (odex_file == nullptr) {
321 cached_odex_file_is_up_to_date_ = false;
322 } else {
323 cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
324 }
325 }
326 return cached_odex_file_is_up_to_date_;
327}
328
329const std::string* OatFileAssistant::OatFileName() {
330 if (!cached_oat_file_name_attempted_) {
331 cached_oat_file_name_attempted_ = true;
332
333 // Compute the oat file name from the dex location.
334 CHECK(dex_location_ != nullptr) << "OatFileAssistant: null dex location";
335
336 // TODO: The oat file assistant should be the definitive place for
337 // determining the oat file name from the dex location, not
338 // GetDalvikCacheFilename.
339 std::string cache_dir = StringPrintf("%s%s",
340 DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
341 std::string error_msg;
342 cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700343 cache_dir.c_str(), &cached_oat_file_name_, outof(error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -0800344 if (!cached_oat_file_name_found_) {
345 // If we can't determine the oat file name, we treat the oat file as
346 // inaccessible.
347 LOG(WARNING) << "Failed to determine oat file name for dex location "
348 << dex_location_ << ": " << error_msg;
349 }
350 }
351 return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
352}
353
354bool OatFileAssistant::OatFileExists() {
355 return GetOatFile() != nullptr;
356}
357
Richard Uhler95abd042015-03-24 09:51:28 -0700358OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800359 if (OatFileIsOutOfDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700360 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800361 }
362 if (OatFileIsUpToDate()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700363 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800364 }
Richard Uhler95abd042015-03-24 09:51:28 -0700365 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800366}
367
368bool OatFileAssistant::OatFileIsOutOfDate() {
369 if (!oat_file_is_out_of_date_attempted_) {
370 oat_file_is_out_of_date_attempted_ = true;
371 const OatFile* oat_file = GetOatFile();
372 if (oat_file == nullptr) {
373 cached_oat_file_is_out_of_date_ = true;
374 } else {
375 cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file);
376 }
377 }
378 return cached_oat_file_is_out_of_date_;
379}
380
381bool OatFileAssistant::OatFileNeedsRelocation() {
Richard Uhler95abd042015-03-24 09:51:28 -0700382 return OatFileStatus() == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800383}
384
385bool OatFileAssistant::OatFileIsUpToDate() {
386 if (!oat_file_is_up_to_date_attempted_) {
387 oat_file_is_up_to_date_attempted_ = true;
388 const OatFile* oat_file = GetOatFile();
389 if (oat_file == nullptr) {
390 cached_oat_file_is_up_to_date_ = false;
391 } else {
392 cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
393 }
394 }
395 return cached_oat_file_is_up_to_date_;
396}
397
Richard Uhler95abd042015-03-24 09:51:28 -0700398OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800399 // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which
400 // is more work than we need to do. If performance becomes a concern, and
401 // this method is actually called, this should be fixed.
402 if (GivenOatFileIsOutOfDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700403 return kOatOutOfDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800404 }
405 if (GivenOatFileIsUpToDate(file)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700406 return kOatUpToDate;
Richard Uhler66d874d2015-01-15 09:37:19 -0800407 }
Richard Uhler95abd042015-03-24 09:51:28 -0700408 return kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800409}
410
411bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) {
412 // Verify the dex checksum.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700413 // Note: GetOatDexFile will return null if the dex checksum doesn't match
Richard Uhler66d874d2015-01-15 09:37:19 -0800414 // what we provide, which verifies the primary dex checksum for us.
415 const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum();
416 const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(
417 dex_location_, dex_checksum_pointer, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700418 if (oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800419 return true;
420 }
421
422 // Verify the dex checksums for any secondary multidex files
Andreas Gampe90e34042015-04-27 20:01:52 -0700423 for (size_t i = 1; ; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800424 std::string secondary_dex_location
Andreas Gampe90e34042015-04-27 20:01:52 -0700425 = DexFile::GetMultiDexLocation(i, dex_location_);
Richard Uhler66d874d2015-01-15 09:37:19 -0800426 const OatFile::OatDexFile* secondary_oat_dex_file
427 = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700428 if (secondary_oat_dex_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800429 // There are no more secondary dex files to check.
430 break;
431 }
432
433 std::string error_msg;
434 uint32_t expected_secondary_checksum = 0;
435 if (DexFile::GetChecksum(secondary_dex_location.c_str(),
Igor Murashkina315f5c2015-07-31 17:35:52 -0700436 &expected_secondary_checksum, outof(error_msg))) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800437 uint32_t actual_secondary_checksum
438 = secondary_oat_dex_file->GetDexFileLocationChecksum();
439 if (expected_secondary_checksum != actual_secondary_checksum) {
440 VLOG(oat) << "Dex checksum does not match for secondary dex: "
441 << secondary_dex_location
442 << ". Expected: " << expected_secondary_checksum
443 << ", Actual: " << actual_secondary_checksum;
Richard Uhler67ff7d12015-05-14 13:21:13 -0700444 return true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800445 }
446 } else {
447 // If we can't get the checksum for the secondary location, we assume
448 // the dex checksum is up to date for this and all other secondary dex
449 // files.
450 break;
451 }
452 }
453
454 // Verify the image checksum
455 const ImageInfo* image_info = GetImageInfo();
456 if (image_info == nullptr) {
457 VLOG(oat) << "No image for oat image checksum to match against.";
458 return true;
459 }
460
461 if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) {
462 VLOG(oat) << "Oat image checksum does not match image checksum.";
463 return true;
464 }
465
466 // The checksums are all good; the dex file is not out of date.
467 return false;
468}
469
470bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) {
Richard Uhler95abd042015-03-24 09:51:28 -0700471 return GivenOatFileStatus(file) == kOatNeedsRelocation;
Richard Uhler66d874d2015-01-15 09:37:19 -0800472}
473
474bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) {
475 if (GivenOatFileIsOutOfDate(file)) {
476 return false;
477 }
478
479 if (file.IsPic()) {
480 return true;
481 }
482
483 const ImageInfo* image_info = GetImageInfo();
484 if (image_info == nullptr) {
Richard Uhlerf7f798c2015-05-11 09:36:57 -0700485 VLOG(oat) << "No image to check oat relocation against.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 return false;
487 }
488
489 // Verify the oat_data_begin recorded for the image in the oat file matches
490 // the actual oat_data_begin for boot.oat in the image.
491 const OatHeader& oat_header = file.GetOatHeader();
492 uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin();
493 if (oat_data_begin != image_info->oat_data_begin) {
494 VLOG(oat) << file.GetLocation() <<
495 ": Oat file image oat_data_begin (" << oat_data_begin << ")"
496 << " does not match actual image oat_data_begin ("
497 << image_info->oat_data_begin << ")";
498 return false;
499 }
500
501 // Verify the oat_patch_delta recorded for the image in the oat file matches
502 // the actual oat_patch_delta for the image.
503 int32_t oat_patch_delta = oat_header.GetImagePatchDelta();
504 if (oat_patch_delta != image_info->patch_delta) {
505 VLOG(oat) << file.GetLocation() <<
506 ": Oat file image patch delta (" << oat_patch_delta << ")"
507 << " does not match actual image patch delta ("
508 << image_info->patch_delta << ")";
509 return false;
510 }
511 return true;
512}
513
514bool OatFileAssistant::ProfileExists() {
515 return GetProfile() != nullptr;
516}
517
518bool OatFileAssistant::OldProfileExists() {
519 return GetOldProfile() != nullptr;
520}
521
522// TODO: The IsProfileChangeSignificant implementation was copied from likely
523// bit-rotted code.
524bool OatFileAssistant::IsProfileChangeSignificant() {
525 ProfileFile* profile = GetProfile();
526 if (profile == nullptr) {
527 return false;
528 }
529
530 ProfileFile* old_profile = GetOldProfile();
531 if (old_profile == nullptr) {
532 return false;
533 }
534
535 // TODO: The following code to compare two profile files should live with
536 // the rest of the profiler code, not the oat file assistant code.
537
538 // A change in profile is considered significant if X% (change_thr property)
539 // of the top K% (compile_thr property) samples has changed.
540 const ProfilerOptions& options = Runtime::Current()->GetProfilerOptions();
541 const double top_k_threshold = options.GetTopKThreshold();
542 const double change_threshold = options.GetTopKChangeThreshold();
543 std::set<std::string> top_k, old_top_k;
544 profile->GetTopKSamples(top_k, top_k_threshold);
545 old_profile->GetTopKSamples(old_top_k, top_k_threshold);
546 std::set<std::string> diff;
547 std::set_difference(top_k.begin(), top_k.end(), old_top_k.begin(),
548 old_top_k.end(), std::inserter(diff, diff.end()));
549
550 // TODO: consider using the usedPercentage instead of the plain diff count.
551 double change_percent = 100.0 * static_cast<double>(diff.size())
552 / static_cast<double>(top_k.size());
553 std::set<std::string>::iterator end = diff.end();
554 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
555 VLOG(oat) << "Profile new in topK: " << *it;
556 }
557
558 if (change_percent > change_threshold) {
559 VLOG(oat) << "Oat File Assistant: Profile for " << dex_location_
560 << "has changed significantly: (top "
561 << top_k_threshold << "% samples changed in proportion of "
562 << change_percent << "%)";
563 return true;
564 }
565 return false;
566}
567
568// TODO: The CopyProfileFile implementation was copied from likely bit-rotted
569// code.
570void OatFileAssistant::CopyProfileFile() {
571 if (!ProfileExists()) {
572 return;
573 }
574
575 std::string profile_name = ProfileFileName();
576 std::string old_profile_name = OldProfileFileName();
577
578 ScopedFd src(open(old_profile_name.c_str(), O_RDONLY));
579 if (src.get() == -1) {
580 PLOG(WARNING) << "Failed to open profile file " << old_profile_name
581 << ". My uid:gid is " << getuid() << ":" << getgid();
582 return;
583 }
584
585 struct stat stat_src;
586 if (fstat(src.get(), &stat_src) == -1) {
587 PLOG(WARNING) << "Failed to get stats for profile file " << old_profile_name
588 << ". My uid:gid is " << getuid() << ":" << getgid();
589 return;
590 }
591
592 // Create the copy with rw------- (only accessible by system)
593 ScopedFd dst(open(profile_name.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0600));
594 if (dst.get() == -1) {
595 PLOG(WARNING) << "Failed to create/write prev profile file " << profile_name
596 << ". My uid:gid is " << getuid() << ":" << getgid();
597 return;
598 }
599
600#ifdef __linux__
601 if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
602#else
603 off_t len;
604 if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) {
605#endif
606 PLOG(WARNING) << "Failed to copy profile file " << old_profile_name
607 << " to " << profile_name << ". My uid:gid is " << getuid()
608 << ":" << getgid();
609 }
610}
611
Richard Uhler95abd042015-03-24 09:51:28 -0700612bool OatFileAssistant::RelocateOatFile(const std::string* input_file,
613 std::string* error_msg) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800614 CHECK(error_msg != nullptr);
615
Richard Uhler95abd042015-03-24 09:51:28 -0700616 if (input_file == nullptr) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800617 *error_msg = "Patching of oat file for dex location "
618 + std::string(dex_location_)
Richard Uhler95abd042015-03-24 09:51:28 -0700619 + " not attempted because the input file name could not be determined.";
Richard Uhler66d874d2015-01-15 09:37:19 -0800620 return false;
621 }
Richard Uhler95abd042015-03-24 09:51:28 -0700622 const std::string& input_file_name = *input_file;
Richard Uhler66d874d2015-01-15 09:37:19 -0800623
624 if (OatFileName() == nullptr) {
625 *error_msg = "Patching of oat file for dex location "
626 + std::string(dex_location_)
627 + " not attempted because the oat file name could not be determined.";
628 return false;
629 }
630 const std::string& oat_file_name = *OatFileName();
631
632 const ImageInfo* image_info = GetImageInfo();
633 Runtime* runtime = Runtime::Current();
634 if (image_info == nullptr) {
635 *error_msg = "Patching of oat file " + oat_file_name
636 + " not attempted because no image location was found.";
637 return false;
638 }
639
640 if (!runtime->IsDex2OatEnabled()) {
641 *error_msg = "Patching of oat file " + oat_file_name
642 + " not attempted because dex2oat is disabled";
643 return false;
644 }
645
646 std::vector<std::string> argv;
647 argv.push_back(runtime->GetPatchoatExecutable());
648 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_)));
Richard Uhler95abd042015-03-24 09:51:28 -0700649 argv.push_back("--input-oat-file=" + input_file_name);
Richard Uhler66d874d2015-01-15 09:37:19 -0800650 argv.push_back("--output-oat-file=" + oat_file_name);
651 argv.push_back("--patched-image-location=" + image_info->location);
652
653 std::string command_line(Join(argv, ' '));
654 if (!Exec(argv, error_msg)) {
655 // Manually delete the file. This ensures there is no garbage left over if
656 // the process unexpectedly died.
657 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
658 return false;
659 }
660
661 // Mark that the oat file has changed and we should try to reload.
662 ClearOatFileCache();
663 return true;
664}
665
666bool OatFileAssistant::GenerateOatFile(std::string* error_msg) {
667 CHECK(error_msg != nullptr);
668
669 if (OatFileName() == nullptr) {
670 *error_msg = "Generation of oat file for dex location "
671 + std::string(dex_location_)
672 + " not attempted because the oat file name could not be determined.";
673 return false;
674 }
675 const std::string& oat_file_name = *OatFileName();
676
677 Runtime* runtime = Runtime::Current();
678 if (!runtime->IsDex2OatEnabled()) {
679 *error_msg = "Generation of oat file " + oat_file_name
680 + " not attempted because dex2oat is disabled";
681 return false;
682 }
683
684 std::vector<std::string> args;
685 args.push_back("--dex-file=" + std::string(dex_location_));
686 args.push_back("--oat-file=" + oat_file_name);
687
688 // dex2oat ignores missing dex files and doesn't report an error.
689 // Check explicitly here so we can detect the error properly.
690 // TODO: Why does dex2oat behave that way?
691 if (!OS::FileExists(dex_location_)) {
692 *error_msg = "Dex location " + std::string(dex_location_) + " does not exists.";
693 return false;
694 }
695
696 if (!Dex2Oat(args, error_msg)) {
697 // Manually delete the file. This ensures there is no garbage left over if
698 // the process unexpectedly died.
699 TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str()));
700 return false;
701 }
702
703 // Mark that the oat file has changed and we should try to reload.
704 ClearOatFileCache();
705 return true;
706}
707
708bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args,
709 std::string* error_msg) {
710 Runtime* runtime = Runtime::Current();
711 std::string image_location = ImageLocation();
712 if (image_location.empty()) {
713 *error_msg = "No image location found for Dex2Oat.";
714 return false;
715 }
716
717 std::vector<std::string> argv;
718 argv.push_back(runtime->GetCompilerExecutable());
719 argv.push_back("--runtime-arg");
720 argv.push_back("-classpath");
721 argv.push_back("--runtime-arg");
722 argv.push_back(runtime->GetClassPathString());
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100723 if (runtime->IsDebuggable()) {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200724 argv.push_back("--debuggable");
725 }
Igor Murashkina315f5c2015-07-31 17:35:52 -0700726 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(outof(argv));
Richard Uhler66d874d2015-01-15 09:37:19 -0800727
728 if (!runtime->IsVerificationEnabled()) {
729 argv.push_back("--compiler-filter=verify-none");
730 }
731
732 if (runtime->MustRelocateIfPossible()) {
733 argv.push_back("--runtime-arg");
734 argv.push_back("-Xrelocate");
735 } else {
736 argv.push_back("--runtime-arg");
737 argv.push_back("-Xnorelocate");
738 }
739
740 if (!kIsTargetBuild) {
741 argv.push_back("--host");
742 }
743
744 argv.push_back("--boot-image=" + image_location);
745
746 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
747 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
748
749 argv.insert(argv.end(), args.begin(), args.end());
750
751 std::string command_line(Join(argv, ' '));
752 return Exec(argv, error_msg);
753}
754
755bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location,
756 InstructionSet isa, std::string* odex_filename, std::string* error_msg) {
757 CHECK(odex_filename != nullptr);
758 CHECK(error_msg != nullptr);
759
760 // The odex file name is formed by replacing the dex_location extension with
Richard Uhler63434112015-03-16 14:32:16 -0700761 // .odex and inserting an oat/<isa> directory. For example:
Richard Uhler66d874d2015-01-15 09:37:19 -0800762 // location = /foo/bar/baz.jar
Richard Uhler63434112015-03-16 14:32:16 -0700763 // odex_location = /foo/bar/oat/<isa>/baz.odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800764
Richard Uhler63434112015-03-16 14:32:16 -0700765 // Find the directory portion of the dex location and add the oat/<isa>
766 // directory.
Richard Uhler66d874d2015-01-15 09:37:19 -0800767 size_t pos = location.rfind('/');
768 if (pos == std::string::npos) {
769 *error_msg = "Dex location " + location + " has no directory.";
770 return false;
771 }
772 std::string dir = location.substr(0, pos+1);
Richard Uhler63434112015-03-16 14:32:16 -0700773 dir += "oat/" + std::string(GetInstructionSetString(isa));
Richard Uhler66d874d2015-01-15 09:37:19 -0800774
775 // Find the file portion of the dex location.
776 std::string file;
777 if (pos == std::string::npos) {
778 file = location;
779 } else {
780 file = location.substr(pos+1);
781 }
782
783 // Get the base part of the file without the extension.
784 pos = file.rfind('.');
785 if (pos == std::string::npos) {
786 *error_msg = "Dex location " + location + " has no extension.";
787 return false;
788 }
789 std::string base = file.substr(0, pos);
790
791 *odex_filename = dir + "/" + base + ".odex";
792 return true;
793}
794
795std::string OatFileAssistant::DalvikCacheDirectory() {
796 // Note: We don't cache this, because it will only be called once by
797 // OatFileName, and we don't care about the performance of the profiling
798 // code, which isn't used in practice.
799
800 // TODO: The work done in GetDalvikCache is overkill for what we need.
801 // Ideally a new API for getting the DalvikCacheDirectory the way we want
802 // (without existence testing, creation, or death) is provided with the rest
803 // of the GetDalvikCache family of functions. Until such an API is in place,
804 // we use GetDalvikCache to avoid duplicating the logic for determining the
805 // dalvik cache directory.
806 std::string result;
807 bool have_android_data;
808 bool dalvik_cache_exists;
809 bool is_global_cache;
810 GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache);
811 return result;
812}
813
814std::string OatFileAssistant::ProfileFileName() {
815 if (package_name_ != nullptr) {
816 return DalvikCacheDirectory() + std::string("profiles/") + package_name_;
817 }
818 return "";
819}
820
821std::string OatFileAssistant::OldProfileFileName() {
822 std::string profile_name = ProfileFileName();
823 if (profile_name.empty()) {
824 return "";
825 }
826 return profile_name + "@old";
827}
828
829std::string OatFileAssistant::ImageLocation() {
830 Runtime* runtime = Runtime::Current();
831 const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace();
832 if (image_space == nullptr) {
833 return "";
834 }
835 return image_space->GetImageLocation();
836}
837
838const uint32_t* OatFileAssistant::GetRequiredDexChecksum() {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700839 if (!required_dex_checksum_attempted_) {
840 required_dex_checksum_attempted_ = true;
841 required_dex_checksum_found_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800842 std::string error_msg;
843 CHECK(dex_location_ != nullptr) << "OatFileAssistant provided no dex location";
Richard Uhler9b994ea2015-06-24 08:44:19 -0700844 if (DexFile::GetChecksum(dex_location_, &cached_required_dex_checksum_, &error_msg)) {
845 required_dex_checksum_found_ = true;
846 has_original_dex_files_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800847 } else {
848 // This can happen if the original dex file has been stripped from the
849 // apk.
850 VLOG(oat) << "OatFileAssistant: " << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700851 has_original_dex_files_ = false;
Richard Uhler66d874d2015-01-15 09:37:19 -0800852
853 // Get the checksum from the odex if we can.
854 const OatFile* odex_file = GetOdexFile();
855 if (odex_file != nullptr) {
856 const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(
857 dex_location_, nullptr, false);
858 if (odex_dex_file != nullptr) {
Richard Uhler9b994ea2015-06-24 08:44:19 -0700859 cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum();
860 required_dex_checksum_found_ = true;
Richard Uhler66d874d2015-01-15 09:37:19 -0800861 }
862 }
863 }
864 }
Richard Uhler9b994ea2015-06-24 08:44:19 -0700865 return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr;
Richard Uhler66d874d2015-01-15 09:37:19 -0800866}
867
868const OatFile* OatFileAssistant::GetOdexFile() {
869 CHECK(!oat_file_released_) << "OdexFile called after oat file released.";
870 if (!odex_file_load_attempted_) {
871 odex_file_load_attempted_ = true;
872 if (OdexFileName() != nullptr) {
873 const std::string& odex_file_name = *OdexFileName();
874 std::string error_msg;
875 cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(),
876 odex_file_name.c_str(), nullptr, nullptr, load_executable_,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700877 dex_location_, outof(error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800878 if (cached_odex_file_.get() == nullptr) {
879 VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file "
880 << odex_file_name << ": " << error_msg;
881 }
882 }
883 }
884 return cached_odex_file_.get();
885}
886
Richard Uhler5f946da2015-07-17 12:28:32 -0700887bool OatFileAssistant::OdexFileIsExecutable() {
888 const OatFile* odex_file = GetOdexFile();
889 return (odex_file != nullptr && odex_file->IsExecutable());
890}
891
Richard Uhler66d874d2015-01-15 09:37:19 -0800892void OatFileAssistant::ClearOdexFileCache() {
893 odex_file_load_attempted_ = false;
894 cached_odex_file_.reset();
895 odex_file_is_out_of_date_attempted_ = false;
896 odex_file_is_up_to_date_attempted_ = false;
897}
898
899const OatFile* OatFileAssistant::GetOatFile() {
900 CHECK(!oat_file_released_) << "OatFile called after oat file released.";
901 if (!oat_file_load_attempted_) {
902 oat_file_load_attempted_ = true;
903 if (OatFileName() != nullptr) {
904 const std::string& oat_file_name = *OatFileName();
905 std::string error_msg;
906 cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(),
Richard Uhlere5fed032015-03-18 08:21:11 -0700907 oat_file_name.c_str(), nullptr, nullptr, load_executable_,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700908 dex_location_, outof(error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800909 if (cached_oat_file_.get() == nullptr) {
910 VLOG(oat) << "OatFileAssistant test for existing oat file "
911 << oat_file_name << ": " << error_msg;
912 }
913 }
914 }
915 return cached_oat_file_.get();
916}
917
Richard Uhler5f946da2015-07-17 12:28:32 -0700918bool OatFileAssistant::OatFileIsExecutable() {
919 const OatFile* oat_file = GetOatFile();
920 return (oat_file != nullptr && oat_file->IsExecutable());
921}
922
Richard Uhler66d874d2015-01-15 09:37:19 -0800923void OatFileAssistant::ClearOatFileCache() {
924 oat_file_load_attempted_ = false;
925 cached_oat_file_.reset();
926 oat_file_is_out_of_date_attempted_ = false;
927 oat_file_is_up_to_date_attempted_ = false;
928}
929
930const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() {
931 if (!image_info_load_attempted_) {
932 image_info_load_attempted_ = true;
933
934 Runtime* runtime = Runtime::Current();
935 const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace();
936 if (image_space != nullptr) {
937 cached_image_info_.location = image_space->GetImageLocation();
938
939 if (isa_ == kRuntimeISA) {
940 const ImageHeader& image_header = image_space->GetImageHeader();
941 cached_image_info_.oat_checksum = image_header.GetOatChecksum();
942 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin());
943 cached_image_info_.patch_delta = image_header.GetPatchDelta();
944 } else {
945 std::unique_ptr<ImageHeader> image_header(
946 gc::space::ImageSpace::ReadImageHeaderOrDie(
947 cached_image_info_.location.c_str(), isa_));
948 cached_image_info_.oat_checksum = image_header->GetOatChecksum();
949 cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin());
950 cached_image_info_.patch_delta = image_header->GetPatchDelta();
951 }
952 }
953 image_info_load_succeeded_ = (image_space != nullptr);
954 }
955 return image_info_load_succeeded_ ? &cached_image_info_ : nullptr;
956}
957
958ProfileFile* OatFileAssistant::GetProfile() {
959 if (!profile_load_attempted_) {
960 CHECK(package_name_ != nullptr)
961 << "pakage_name_ is nullptr: "
962 << "profile_load_attempted_ should have been true";
963 profile_load_attempted_ = true;
964 std::string profile_name = ProfileFileName();
965 if (!profile_name.empty()) {
966 profile_load_succeeded_ = cached_profile_.LoadFile(profile_name);
967 }
968 }
969 return profile_load_succeeded_ ? &cached_profile_ : nullptr;
970}
971
972ProfileFile* OatFileAssistant::GetOldProfile() {
973 if (!old_profile_load_attempted_) {
974 CHECK(package_name_ != nullptr)
975 << "pakage_name_ is nullptr: "
976 << "old_profile_load_attempted_ should have been true";
977 old_profile_load_attempted_ = true;
978 std::string old_profile_name = OldProfileFileName();
979 if (!old_profile_name.empty()) {
980 old_profile_load_succeeded_ = cached_old_profile_.LoadFile(old_profile_name);
981 }
982 }
983 return old_profile_load_succeeded_ ? &cached_old_profile_ : nullptr;
984}
985
986} // namespace art
987