Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1 | /* |
| 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" |
| 32 | #include "base/stringprintf.h" |
| 33 | #include "class_linker.h" |
| 34 | #include "gc/heap.h" |
| 35 | #include "gc/space/image_space.h" |
| 36 | #include "image.h" |
| 37 | #include "oat.h" |
| 38 | #include "os.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 39 | #include "runtime.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 40 | #include "scoped_thread_state_change.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 41 | #include "ScopedFd.h" |
| 42 | #include "utils.h" |
| 43 | |
| 44 | namespace art { |
| 45 | |
| 46 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 47 | const InstructionSet isa, |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 48 | bool profile_changed, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 49 | bool load_executable) |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 50 | : OatFileAssistant(dex_location, nullptr, isa, profile_changed, load_executable) |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 51 | { } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 52 | |
| 53 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 54 | const char* oat_location, |
| 55 | const InstructionSet isa, |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 56 | bool profile_changed, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 57 | bool load_executable) |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 58 | : isa_(isa), profile_changed_(profile_changed), load_executable_(load_executable) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 59 | CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location"; |
| 60 | dex_location_.assign(dex_location); |
| 61 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 62 | if (load_executable_ && isa != kRuntimeISA) { |
| 63 | LOG(WARNING) << "OatFileAssistant: Load executable specified, " |
| 64 | << "but isa is not kRuntimeISA. Will not attempt to load executable."; |
| 65 | load_executable_ = false; |
| 66 | } |
| 67 | |
| 68 | // If the user gave a target oat location, save that as the cached oat |
| 69 | // location now so we won't try to construct the default location later. |
| 70 | if (oat_location != nullptr) { |
| 71 | cached_oat_file_name_ = std::string(oat_location); |
| 72 | cached_oat_file_name_attempted_ = true; |
| 73 | cached_oat_file_name_found_ = true; |
| 74 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | OatFileAssistant::~OatFileAssistant() { |
| 78 | // Clean up the lock file. |
Richard Uhler | 581f4e9 | 2015-05-07 10:19:35 -0700 | [diff] [blame] | 79 | if (flock_.HasFile()) { |
| 80 | TEMP_FAILURE_RETRY(unlink(flock_.GetFile()->GetPath().c_str())); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | bool OatFileAssistant::IsInBootClassPath() { |
| 85 | // Note: We check the current boot class path, regardless of the ISA |
| 86 | // specified by the user. This is okay, because the boot class path should |
| 87 | // be the same for all ISAs. |
| 88 | // TODO: Can we verify the boot class path is the same for all ISAs? |
| 89 | Runtime* runtime = Runtime::Current(); |
| 90 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 91 | const auto& boot_class_path = class_linker->GetBootClassPath(); |
| 92 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 93 | if (boot_class_path[i]->GetLocation() == dex_location_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 94 | VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path"; |
| 95 | return true; |
| 96 | } |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | bool OatFileAssistant::Lock(std::string* error_msg) { |
| 102 | CHECK(error_msg != nullptr); |
Richard Uhler | 581f4e9 | 2015-05-07 10:19:35 -0700 | [diff] [blame] | 103 | CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 104 | |
| 105 | if (OatFileName() == nullptr) { |
| 106 | *error_msg = "Failed to determine lock file"; |
| 107 | return false; |
| 108 | } |
| 109 | std::string lock_file_name = *OatFileName() + ".flock"; |
| 110 | |
Richard Uhler | 581f4e9 | 2015-05-07 10:19:35 -0700 | [diff] [blame] | 111 | if (!flock_.Init(lock_file_name.c_str(), error_msg)) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 112 | TEMP_FAILURE_RETRY(unlink(lock_file_name.c_str())); |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 118 | bool OatFileAssistant::OatFileCompilerFilterIsOkay(CompilerFilter::Filter target) { |
| 119 | const OatFile* oat_file = GetOatFile(); |
| 120 | if (oat_file != nullptr) { |
| 121 | CompilerFilter::Filter current = oat_file->GetCompilerFilter(); |
| 122 | return CompilerFilter::IsAsGoodAs(current, target); |
| 123 | } |
| 124 | return false; |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 125 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 126 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 127 | bool OatFileAssistant::OdexFileCompilerFilterIsOkay(CompilerFilter::Filter target) { |
| 128 | const OatFile* odex_file = GetOdexFile(); |
| 129 | if (odex_file != nullptr) { |
| 130 | CompilerFilter::Filter current = odex_file->GetCompilerFilter(); |
| 131 | return CompilerFilter::IsAsGoodAs(current, target); |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | OatFileAssistant::DexOptNeeded OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target) { |
| 137 | bool compilation_desired = CompilerFilter::IsCompilationEnabled(target); |
| 138 | |
| 139 | // See if the oat file is in good shape as is. |
| 140 | bool oat_okay = OatFileCompilerFilterIsOkay(target); |
| 141 | if (oat_okay) { |
| 142 | if (compilation_desired) { |
| 143 | if (OatFileIsUpToDate()) { |
| 144 | return kNoDexOptNeeded; |
| 145 | } |
| 146 | } else { |
| 147 | if (!OatFileIsOutOfDate()) { |
| 148 | return kNoDexOptNeeded; |
| 149 | } |
| 150 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 151 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 152 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 153 | // See if the odex file is in good shape as is. |
| 154 | bool odex_okay = OdexFileCompilerFilterIsOkay(target); |
| 155 | if (odex_okay) { |
| 156 | if (compilation_desired) { |
| 157 | if (OdexFileIsUpToDate()) { |
| 158 | return kNoDexOptNeeded; |
| 159 | } |
| 160 | } else { |
| 161 | if (!OdexFileIsOutOfDate()) { |
| 162 | return kNoDexOptNeeded; |
| 163 | } |
| 164 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 167 | // See if we can get an up-to-date file by running patchoat. |
| 168 | if (compilation_desired) { |
Richard Uhler | 1c4eb04 | 2016-03-29 13:27:41 -0700 | [diff] [blame^] | 169 | if (odex_okay && OdexFileNeedsRelocation() && OdexFileHasPatchInfo()) { |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 170 | return kPatchOatNeeded; |
| 171 | } |
| 172 | |
Richard Uhler | 1c4eb04 | 2016-03-29 13:27:41 -0700 | [diff] [blame^] | 173 | if (oat_okay && OatFileNeedsRelocation() && OatFileHasPatchInfo()) { |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 174 | return kSelfPatchOatNeeded; |
| 175 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 178 | // We can only run dex2oat if there are original dex files. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 179 | return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 182 | bool OatFileAssistant::MakeUpToDate(CompilerFilter::Filter target, std::string* error_msg) { |
| 183 | switch (GetDexOptNeeded(target)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 184 | case kNoDexOptNeeded: return true; |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 185 | case kDex2OatNeeded: return GenerateOatFile(target, error_msg); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 186 | case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg); |
| 187 | case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 188 | } |
| 189 | UNREACHABLE(); |
| 190 | } |
| 191 | |
| 192 | std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() { |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 193 | // The best oat files are, in descending order of bestness: |
| 194 | // 1. Properly relocated files. These may be opened executable. |
| 195 | // 2. Not out-of-date files that are already opened non-executable. |
| 196 | // 3. Not out-of-date files that we must reopen non-executable. |
| 197 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 198 | if (OatFileIsUpToDate()) { |
| 199 | oat_file_released_ = true; |
| 200 | return std::move(cached_oat_file_); |
| 201 | } |
| 202 | |
| 203 | if (OdexFileIsUpToDate()) { |
| 204 | oat_file_released_ = true; |
| 205 | return std::move(cached_odex_file_); |
| 206 | } |
| 207 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 208 | VLOG(oat) << "Oat File Assistant: No relocated oat file found," |
| 209 | << " attempting to fall back to interpreting oat file instead."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 210 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 211 | if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) { |
| 212 | oat_file_released_ = true; |
| 213 | return std::move(cached_oat_file_); |
| 214 | } |
| 215 | |
| 216 | if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) { |
| 217 | oat_file_released_ = true; |
| 218 | return std::move(cached_odex_file_); |
| 219 | } |
| 220 | |
| 221 | if (!OatFileIsOutOfDate()) { |
| 222 | load_executable_ = false; |
| 223 | ClearOatFileCache(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 224 | if (!OatFileIsOutOfDate()) { |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 225 | CHECK(!OatFileIsExecutable()); |
| 226 | oat_file_released_ = true; |
| 227 | return std::move(cached_oat_file_); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 228 | } |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 229 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 230 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 231 | if (!OdexFileIsOutOfDate()) { |
| 232 | load_executable_ = false; |
| 233 | ClearOdexFileCache(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 234 | if (!OdexFileIsOutOfDate()) { |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 235 | CHECK(!OdexFileIsExecutable()); |
| 236 | oat_file_released_ = true; |
| 237 | return std::move(cached_odex_file_); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | return std::unique_ptr<OatFile>(); |
| 242 | } |
| 243 | |
| 244 | std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles( |
| 245 | const OatFile& oat_file, const char* dex_location) { |
| 246 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 247 | |
| 248 | // Load the primary dex file. |
| 249 | std::string error_msg; |
| 250 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile( |
| 251 | dex_location, nullptr, false); |
| 252 | if (oat_dex_file == nullptr) { |
| 253 | LOG(WARNING) << "Attempt to load out-of-date oat file " |
| 254 | << oat_file.GetLocation() << " for dex location " << dex_location; |
| 255 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 256 | } |
| 257 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 258 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 259 | if (dex_file.get() == nullptr) { |
| 260 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
| 261 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 262 | } |
| 263 | dex_files.push_back(std::move(dex_file)); |
| 264 | |
| 265 | // Load secondary multidex files |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 266 | for (size_t i = 1; ; i++) { |
| 267 | std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 268 | oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 269 | if (oat_dex_file == nullptr) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 270 | // There are no more secondary dex files to load. |
| 271 | break; |
| 272 | } |
| 273 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 274 | dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 275 | if (dex_file.get() == nullptr) { |
| 276 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
| 277 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 278 | } |
| 279 | dex_files.push_back(std::move(dex_file)); |
| 280 | } |
| 281 | return dex_files; |
| 282 | } |
| 283 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 284 | bool OatFileAssistant::HasOriginalDexFiles() { |
| 285 | // Ensure GetRequiredDexChecksum has been run so that |
| 286 | // has_original_dex_files_ is initialized. We don't care about the result of |
| 287 | // GetRequiredDexChecksum. |
| 288 | GetRequiredDexChecksum(); |
| 289 | return has_original_dex_files_; |
| 290 | } |
| 291 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 292 | const std::string* OatFileAssistant::OdexFileName() { |
| 293 | if (!cached_odex_file_name_attempted_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 294 | cached_odex_file_name_attempted_ = true; |
| 295 | |
| 296 | std::string error_msg; |
| 297 | cached_odex_file_name_found_ = DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 298 | dex_location_, isa_, &cached_odex_file_name_, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 299 | if (!cached_odex_file_name_found_) { |
| 300 | // If we can't figure out the odex file, we treat it as if the odex |
| 301 | // file was inaccessible. |
| 302 | LOG(WARNING) << "Failed to determine odex file name: " << error_msg; |
| 303 | } |
| 304 | } |
| 305 | return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr; |
| 306 | } |
| 307 | |
| 308 | bool OatFileAssistant::OdexFileExists() { |
| 309 | return GetOdexFile() != nullptr; |
| 310 | } |
| 311 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 312 | OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 313 | if (OdexFileIsOutOfDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 314 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 315 | } |
| 316 | if (OdexFileIsUpToDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 317 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 318 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 319 | return kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | bool OatFileAssistant::OdexFileIsOutOfDate() { |
| 323 | if (!odex_file_is_out_of_date_attempted_) { |
| 324 | odex_file_is_out_of_date_attempted_ = true; |
| 325 | const OatFile* odex_file = GetOdexFile(); |
| 326 | if (odex_file == nullptr) { |
| 327 | cached_odex_file_is_out_of_date_ = true; |
| 328 | } else { |
| 329 | cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file); |
| 330 | } |
| 331 | } |
| 332 | return cached_odex_file_is_out_of_date_; |
| 333 | } |
| 334 | |
| 335 | bool OatFileAssistant::OdexFileNeedsRelocation() { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 336 | return OdexFileStatus() == kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | bool OatFileAssistant::OdexFileIsUpToDate() { |
| 340 | if (!odex_file_is_up_to_date_attempted_) { |
| 341 | odex_file_is_up_to_date_attempted_ = true; |
| 342 | const OatFile* odex_file = GetOdexFile(); |
| 343 | if (odex_file == nullptr) { |
| 344 | cached_odex_file_is_up_to_date_ = false; |
| 345 | } else { |
| 346 | cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file); |
| 347 | } |
| 348 | } |
| 349 | return cached_odex_file_is_up_to_date_; |
| 350 | } |
| 351 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 352 | std::string OatFileAssistant::ArtFileName(const OatFile* oat_file) const { |
| 353 | const std::string oat_file_location = oat_file->GetLocation(); |
| 354 | // Replace extension with .art |
| 355 | const size_t last_ext = oat_file_location.find_last_of('.'); |
| 356 | if (last_ext == std::string::npos) { |
| 357 | LOG(ERROR) << "No extension in oat file " << oat_file_location; |
| 358 | return std::string(); |
| 359 | } |
| 360 | return oat_file_location.substr(0, last_ext) + ".art"; |
| 361 | } |
| 362 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 363 | const std::string* OatFileAssistant::OatFileName() { |
| 364 | if (!cached_oat_file_name_attempted_) { |
| 365 | cached_oat_file_name_attempted_ = true; |
| 366 | |
| 367 | // Compute the oat file name from the dex location. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 368 | // TODO: The oat file assistant should be the definitive place for |
| 369 | // determining the oat file name from the dex location, not |
| 370 | // GetDalvikCacheFilename. |
| 371 | std::string cache_dir = StringPrintf("%s%s", |
| 372 | DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_)); |
| 373 | std::string error_msg; |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 374 | cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 375 | cache_dir.c_str(), &cached_oat_file_name_, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 376 | if (!cached_oat_file_name_found_) { |
| 377 | // If we can't determine the oat file name, we treat the oat file as |
| 378 | // inaccessible. |
| 379 | LOG(WARNING) << "Failed to determine oat file name for dex location " |
| 380 | << dex_location_ << ": " << error_msg; |
| 381 | } |
| 382 | } |
| 383 | return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr; |
| 384 | } |
| 385 | |
| 386 | bool OatFileAssistant::OatFileExists() { |
| 387 | return GetOatFile() != nullptr; |
| 388 | } |
| 389 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 390 | OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 391 | if (OatFileIsOutOfDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 392 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 393 | } |
| 394 | if (OatFileIsUpToDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 395 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 396 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 397 | return kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | bool OatFileAssistant::OatFileIsOutOfDate() { |
| 401 | if (!oat_file_is_out_of_date_attempted_) { |
| 402 | oat_file_is_out_of_date_attempted_ = true; |
| 403 | const OatFile* oat_file = GetOatFile(); |
| 404 | if (oat_file == nullptr) { |
| 405 | cached_oat_file_is_out_of_date_ = true; |
| 406 | } else { |
| 407 | cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file); |
| 408 | } |
| 409 | } |
| 410 | return cached_oat_file_is_out_of_date_; |
| 411 | } |
| 412 | |
| 413 | bool OatFileAssistant::OatFileNeedsRelocation() { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 414 | return OatFileStatus() == kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | bool OatFileAssistant::OatFileIsUpToDate() { |
| 418 | if (!oat_file_is_up_to_date_attempted_) { |
| 419 | oat_file_is_up_to_date_attempted_ = true; |
| 420 | const OatFile* oat_file = GetOatFile(); |
| 421 | if (oat_file == nullptr) { |
| 422 | cached_oat_file_is_up_to_date_ = false; |
| 423 | } else { |
| 424 | cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file); |
| 425 | } |
| 426 | } |
| 427 | return cached_oat_file_is_up_to_date_; |
| 428 | } |
| 429 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 430 | OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 431 | // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which |
| 432 | // is more work than we need to do. If performance becomes a concern, and |
| 433 | // this method is actually called, this should be fixed. |
| 434 | if (GivenOatFileIsOutOfDate(file)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 435 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 436 | } |
| 437 | if (GivenOatFileIsUpToDate(file)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 438 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 439 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 440 | return kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) { |
| 444 | // Verify the dex checksum. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 445 | // Note: GetOatDexFile will return null if the dex checksum doesn't match |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 446 | // what we provide, which verifies the primary dex checksum for us. |
| 447 | const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum(); |
| 448 | const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile( |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 449 | dex_location_.c_str(), dex_checksum_pointer, false); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 450 | if (oat_dex_file == nullptr) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 451 | return true; |
| 452 | } |
| 453 | |
| 454 | // Verify the dex checksums for any secondary multidex files |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 455 | for (size_t i = 1; ; i++) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 456 | std::string secondary_dex_location |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 457 | = DexFile::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 458 | const OatFile::OatDexFile* secondary_oat_dex_file |
| 459 | = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 460 | if (secondary_oat_dex_file == nullptr) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 461 | // There are no more secondary dex files to check. |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | std::string error_msg; |
| 466 | uint32_t expected_secondary_checksum = 0; |
| 467 | if (DexFile::GetChecksum(secondary_dex_location.c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 468 | &expected_secondary_checksum, &error_msg)) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 469 | uint32_t actual_secondary_checksum |
| 470 | = secondary_oat_dex_file->GetDexFileLocationChecksum(); |
| 471 | if (expected_secondary_checksum != actual_secondary_checksum) { |
| 472 | VLOG(oat) << "Dex checksum does not match for secondary dex: " |
| 473 | << secondary_dex_location |
| 474 | << ". Expected: " << expected_secondary_checksum |
| 475 | << ", Actual: " << actual_secondary_checksum; |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 476 | return true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 477 | } |
| 478 | } else { |
| 479 | // If we can't get the checksum for the secondary location, we assume |
| 480 | // the dex checksum is up to date for this and all other secondary dex |
| 481 | // files. |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 486 | CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter(); |
| 487 | VLOG(oat) << "Compiler filter for " << file.GetLocation() << " is " << current_compiler_filter; |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 488 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 489 | // Verify the image checksum |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 490 | if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) { |
| 491 | const ImageInfo* image_info = GetImageInfo(); |
| 492 | if (image_info == nullptr) { |
| 493 | VLOG(oat) << "No image for oat image checksum to match against."; |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) { |
| 498 | VLOG(oat) << "Oat image checksum does not match image checksum."; |
| 499 | return true; |
| 500 | } |
| 501 | } else { |
| 502 | VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 505 | // Verify the profile hasn't changed recently. |
| 506 | // TODO: Move this check to OatFileCompilerFilterIsOkay? Nothing bad should |
| 507 | // happen if we use an oat file compiled with an out-of-date profile. |
| 508 | if (CompilerFilter::DependsOnProfile(current_compiler_filter)) { |
| 509 | if (profile_changed_) { |
| 510 | VLOG(oat) << "The profile has changed recently."; |
| 511 | return true; |
| 512 | } |
| 513 | } else { |
| 514 | VLOG(oat) << "Profile check skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 517 | // Everything looks good; the dex file is not out of date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 518 | return false; |
| 519 | } |
| 520 | |
| 521 | bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 522 | return GivenOatFileStatus(file) == kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) { |
| 526 | if (GivenOatFileIsOutOfDate(file)) { |
| 527 | return false; |
| 528 | } |
| 529 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 530 | CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 531 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 532 | if (CompilerFilter::IsCompilationEnabled(current_compiler_filter)) { |
| 533 | if (!file.IsPic()) { |
| 534 | const ImageInfo* image_info = GetImageInfo(); |
| 535 | if (image_info == nullptr) { |
| 536 | VLOG(oat) << "No image to check oat relocation against."; |
| 537 | return false; |
| 538 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 539 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 540 | // Verify the oat_data_begin recorded for the image in the oat file matches |
| 541 | // the actual oat_data_begin for boot.oat in the image. |
| 542 | const OatHeader& oat_header = file.GetOatHeader(); |
| 543 | uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin(); |
| 544 | if (oat_data_begin != image_info->oat_data_begin) { |
| 545 | VLOG(oat) << file.GetLocation() << |
| 546 | ": Oat file image oat_data_begin (" << oat_data_begin << ")" |
| 547 | << " does not match actual image oat_data_begin (" |
| 548 | << image_info->oat_data_begin << ")"; |
| 549 | return false; |
| 550 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 551 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 552 | // Verify the oat_patch_delta recorded for the image in the oat file matches |
| 553 | // the actual oat_patch_delta for the image. |
| 554 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 555 | if (oat_patch_delta != image_info->patch_delta) { |
| 556 | VLOG(oat) << file.GetLocation() << |
| 557 | ": Oat file image patch delta (" << oat_patch_delta << ")" |
| 558 | << " does not match actual image patch delta (" |
| 559 | << image_info->patch_delta << ")"; |
| 560 | return false; |
| 561 | } |
| 562 | } else { |
| 563 | // Oat files compiled in PIC mode do not require relocation. |
| 564 | VLOG(oat) << "Oat relocation test skipped for PIC oat file"; |
| 565 | } |
| 566 | } else { |
| 567 | VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 568 | } |
| 569 | return true; |
| 570 | } |
| 571 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 572 | bool OatFileAssistant::RelocateOatFile(const std::string* input_file, |
| 573 | std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 574 | CHECK(error_msg != nullptr); |
| 575 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 576 | if (input_file == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 577 | *error_msg = "Patching of oat file for dex location " + dex_location_ |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 578 | + " not attempted because the input file name could not be determined."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 579 | return false; |
| 580 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 581 | const std::string& input_file_name = *input_file; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 582 | |
| 583 | if (OatFileName() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 584 | *error_msg = "Patching of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 585 | + " not attempted because the oat file name could not be determined."; |
| 586 | return false; |
| 587 | } |
| 588 | const std::string& oat_file_name = *OatFileName(); |
| 589 | |
| 590 | const ImageInfo* image_info = GetImageInfo(); |
| 591 | Runtime* runtime = Runtime::Current(); |
| 592 | if (image_info == nullptr) { |
| 593 | *error_msg = "Patching of oat file " + oat_file_name |
| 594 | + " not attempted because no image location was found."; |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | if (!runtime->IsDex2OatEnabled()) { |
| 599 | *error_msg = "Patching of oat file " + oat_file_name |
| 600 | + " not attempted because dex2oat is disabled"; |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | std::vector<std::string> argv; |
| 605 | argv.push_back(runtime->GetPatchoatExecutable()); |
| 606 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_))); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 607 | argv.push_back("--input-oat-file=" + input_file_name); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 608 | argv.push_back("--output-oat-file=" + oat_file_name); |
| 609 | argv.push_back("--patched-image-location=" + image_info->location); |
| 610 | |
| 611 | std::string command_line(Join(argv, ' ')); |
| 612 | if (!Exec(argv, error_msg)) { |
| 613 | // Manually delete the file. This ensures there is no garbage left over if |
| 614 | // the process unexpectedly died. |
| 615 | TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str())); |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | // Mark that the oat file has changed and we should try to reload. |
| 620 | ClearOatFileCache(); |
| 621 | return true; |
| 622 | } |
| 623 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 624 | bool OatFileAssistant::GenerateOatFile(CompilerFilter::Filter target, std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 625 | CHECK(error_msg != nullptr); |
| 626 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 627 | Runtime* runtime = Runtime::Current(); |
| 628 | if (!runtime->IsDex2OatEnabled()) { |
| 629 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
| 630 | + " not attempted because dex2oat is disabled."; |
| 631 | return false; |
| 632 | } |
| 633 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 634 | if (OatFileName() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 635 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 636 | + " not attempted because the oat file name could not be determined."; |
| 637 | return false; |
| 638 | } |
| 639 | const std::string& oat_file_name = *OatFileName(); |
| 640 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 641 | // dex2oat ignores missing dex files and doesn't report an error. |
| 642 | // Check explicitly here so we can detect the error properly. |
| 643 | // TODO: Why does dex2oat behave that way? |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 644 | if (!OS::FileExists(dex_location_.c_str())) { |
| 645 | *error_msg = "Dex location " + dex_location_ + " does not exists."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 646 | return false; |
| 647 | } |
| 648 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 649 | std::unique_ptr<File> oat_file; |
| 650 | oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str())); |
| 651 | if (oat_file.get() == nullptr) { |
| 652 | *error_msg = "Generation of oat file " + oat_file_name |
| 653 | + " not attempted because the oat file could not be created."; |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | if (fchmod(oat_file->Fd(), 0644) != 0) { |
| 658 | *error_msg = "Generation of oat file " + oat_file_name |
| 659 | + " not attempted because the oat file could not be made world readable."; |
| 660 | oat_file->Erase(); |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | std::vector<std::string> args; |
| 665 | args.push_back("--dex-file=" + dex_location_); |
| 666 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
| 667 | args.push_back("--oat-location=" + oat_file_name); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 668 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(target)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 669 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 670 | if (!Dex2Oat(args, error_msg)) { |
| 671 | // Manually delete the file. This ensures there is no garbage left over if |
| 672 | // the process unexpectedly died. |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 673 | oat_file->Erase(); |
| 674 | TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str())); |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | if (oat_file->FlushCloseOrErase() != 0) { |
| 679 | *error_msg = "Unable to close oat file " + oat_file_name; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 680 | TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str())); |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | // Mark that the oat file has changed and we should try to reload. |
| 685 | ClearOatFileCache(); |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args, |
| 690 | std::string* error_msg) { |
| 691 | Runtime* runtime = Runtime::Current(); |
| 692 | std::string image_location = ImageLocation(); |
| 693 | if (image_location.empty()) { |
| 694 | *error_msg = "No image location found for Dex2Oat."; |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | std::vector<std::string> argv; |
| 699 | argv.push_back(runtime->GetCompilerExecutable()); |
| 700 | argv.push_back("--runtime-arg"); |
| 701 | argv.push_back("-classpath"); |
| 702 | argv.push_back("--runtime-arg"); |
| 703 | argv.push_back(runtime->GetClassPathString()); |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 704 | if (runtime->IsDebuggable()) { |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 705 | argv.push_back("--debuggable"); |
| 706 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 707 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 708 | |
| 709 | if (!runtime->IsVerificationEnabled()) { |
| 710 | argv.push_back("--compiler-filter=verify-none"); |
| 711 | } |
| 712 | |
| 713 | if (runtime->MustRelocateIfPossible()) { |
| 714 | argv.push_back("--runtime-arg"); |
| 715 | argv.push_back("-Xrelocate"); |
| 716 | } else { |
| 717 | argv.push_back("--runtime-arg"); |
| 718 | argv.push_back("-Xnorelocate"); |
| 719 | } |
| 720 | |
| 721 | if (!kIsTargetBuild) { |
| 722 | argv.push_back("--host"); |
| 723 | } |
| 724 | |
| 725 | argv.push_back("--boot-image=" + image_location); |
| 726 | |
| 727 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 728 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 729 | |
| 730 | argv.insert(argv.end(), args.begin(), args.end()); |
| 731 | |
| 732 | std::string command_line(Join(argv, ' ')); |
| 733 | return Exec(argv, error_msg); |
| 734 | } |
| 735 | |
| 736 | bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location, |
| 737 | InstructionSet isa, std::string* odex_filename, std::string* error_msg) { |
| 738 | CHECK(odex_filename != nullptr); |
| 739 | CHECK(error_msg != nullptr); |
| 740 | |
| 741 | // The odex file name is formed by replacing the dex_location extension with |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 742 | // .odex and inserting an oat/<isa> directory. For example: |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 743 | // location = /foo/bar/baz.jar |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 744 | // odex_location = /foo/bar/oat/<isa>/baz.odex |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 745 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 746 | // Find the directory portion of the dex location and add the oat/<isa> |
| 747 | // directory. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 748 | size_t pos = location.rfind('/'); |
| 749 | if (pos == std::string::npos) { |
| 750 | *error_msg = "Dex location " + location + " has no directory."; |
| 751 | return false; |
| 752 | } |
| 753 | std::string dir = location.substr(0, pos+1); |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 754 | dir += "oat/" + std::string(GetInstructionSetString(isa)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 755 | |
| 756 | // Find the file portion of the dex location. |
| 757 | std::string file; |
| 758 | if (pos == std::string::npos) { |
| 759 | file = location; |
| 760 | } else { |
| 761 | file = location.substr(pos+1); |
| 762 | } |
| 763 | |
| 764 | // Get the base part of the file without the extension. |
| 765 | pos = file.rfind('.'); |
| 766 | if (pos == std::string::npos) { |
| 767 | *error_msg = "Dex location " + location + " has no extension."; |
| 768 | return false; |
| 769 | } |
| 770 | std::string base = file.substr(0, pos); |
| 771 | |
| 772 | *odex_filename = dir + "/" + base + ".odex"; |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | std::string OatFileAssistant::DalvikCacheDirectory() { |
| 777 | // Note: We don't cache this, because it will only be called once by |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 778 | // OatFileName. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 779 | |
| 780 | // TODO: The work done in GetDalvikCache is overkill for what we need. |
| 781 | // Ideally a new API for getting the DalvikCacheDirectory the way we want |
| 782 | // (without existence testing, creation, or death) is provided with the rest |
| 783 | // of the GetDalvikCache family of functions. Until such an API is in place, |
| 784 | // we use GetDalvikCache to avoid duplicating the logic for determining the |
| 785 | // dalvik cache directory. |
| 786 | std::string result; |
| 787 | bool have_android_data; |
| 788 | bool dalvik_cache_exists; |
| 789 | bool is_global_cache; |
| 790 | GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache); |
| 791 | return result; |
| 792 | } |
| 793 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 794 | std::string OatFileAssistant::ImageLocation() { |
| 795 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 796 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 797 | runtime->GetHeap()->GetBootImageSpaces(); |
| 798 | if (image_spaces.empty()) { |
| 799 | return ""; |
| 800 | } |
| 801 | return image_spaces[0]->GetImageLocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | const uint32_t* OatFileAssistant::GetRequiredDexChecksum() { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 805 | if (!required_dex_checksum_attempted_) { |
| 806 | required_dex_checksum_attempted_ = true; |
| 807 | required_dex_checksum_found_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 808 | std::string error_msg; |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 809 | if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 810 | required_dex_checksum_found_ = true; |
| 811 | has_original_dex_files_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 812 | } else { |
| 813 | // This can happen if the original dex file has been stripped from the |
| 814 | // apk. |
| 815 | VLOG(oat) << "OatFileAssistant: " << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 816 | has_original_dex_files_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 817 | |
| 818 | // Get the checksum from the odex if we can. |
| 819 | const OatFile* odex_file = GetOdexFile(); |
| 820 | if (odex_file != nullptr) { |
| 821 | const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile( |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 822 | dex_location_.c_str(), nullptr, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 823 | if (odex_dex_file != nullptr) { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 824 | cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum(); |
| 825 | required_dex_checksum_found_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 830 | return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | const OatFile* OatFileAssistant::GetOdexFile() { |
| 834 | CHECK(!oat_file_released_) << "OdexFile called after oat file released."; |
| 835 | if (!odex_file_load_attempted_) { |
| 836 | odex_file_load_attempted_ = true; |
| 837 | if (OdexFileName() != nullptr) { |
| 838 | const std::string& odex_file_name = *OdexFileName(); |
| 839 | std::string error_msg; |
| 840 | cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(), |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 841 | odex_file_name.c_str(), |
| 842 | nullptr, |
| 843 | nullptr, |
| 844 | load_executable_, |
| 845 | /*low_4gb*/false, |
| 846 | dex_location_.c_str(), |
| 847 | &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 848 | if (cached_odex_file_.get() == nullptr) { |
| 849 | VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file " |
| 850 | << odex_file_name << ": " << error_msg; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | return cached_odex_file_.get(); |
| 855 | } |
| 856 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 857 | bool OatFileAssistant::OdexFileIsExecutable() { |
| 858 | const OatFile* odex_file = GetOdexFile(); |
| 859 | return (odex_file != nullptr && odex_file->IsExecutable()); |
| 860 | } |
| 861 | |
Richard Uhler | 1c4eb04 | 2016-03-29 13:27:41 -0700 | [diff] [blame^] | 862 | bool OatFileAssistant::OdexFileHasPatchInfo() { |
| 863 | const OatFile* odex_file = GetOdexFile(); |
| 864 | return (odex_file != nullptr && odex_file->HasPatchInfo()); |
| 865 | } |
| 866 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 867 | void OatFileAssistant::ClearOdexFileCache() { |
| 868 | odex_file_load_attempted_ = false; |
| 869 | cached_odex_file_.reset(); |
| 870 | odex_file_is_out_of_date_attempted_ = false; |
| 871 | odex_file_is_up_to_date_attempted_ = false; |
| 872 | } |
| 873 | |
| 874 | const OatFile* OatFileAssistant::GetOatFile() { |
| 875 | CHECK(!oat_file_released_) << "OatFile called after oat file released."; |
| 876 | if (!oat_file_load_attempted_) { |
| 877 | oat_file_load_attempted_ = true; |
| 878 | if (OatFileName() != nullptr) { |
| 879 | const std::string& oat_file_name = *OatFileName(); |
| 880 | std::string error_msg; |
| 881 | cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(), |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 882 | oat_file_name.c_str(), |
| 883 | nullptr, |
| 884 | nullptr, |
| 885 | load_executable_, |
| 886 | /*low_4gb*/false, |
| 887 | dex_location_.c_str(), |
| 888 | &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 889 | if (cached_oat_file_.get() == nullptr) { |
| 890 | VLOG(oat) << "OatFileAssistant test for existing oat file " |
| 891 | << oat_file_name << ": " << error_msg; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | return cached_oat_file_.get(); |
| 896 | } |
| 897 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 898 | bool OatFileAssistant::OatFileIsExecutable() { |
| 899 | const OatFile* oat_file = GetOatFile(); |
| 900 | return (oat_file != nullptr && oat_file->IsExecutable()); |
| 901 | } |
| 902 | |
Richard Uhler | 1c4eb04 | 2016-03-29 13:27:41 -0700 | [diff] [blame^] | 903 | bool OatFileAssistant::OatFileHasPatchInfo() { |
| 904 | const OatFile* oat_file = GetOatFile(); |
| 905 | return (oat_file != nullptr && oat_file->HasPatchInfo()); |
| 906 | } |
| 907 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 908 | void OatFileAssistant::ClearOatFileCache() { |
| 909 | oat_file_load_attempted_ = false; |
| 910 | cached_oat_file_.reset(); |
| 911 | oat_file_is_out_of_date_attempted_ = false; |
| 912 | oat_file_is_up_to_date_attempted_ = false; |
| 913 | } |
| 914 | |
| 915 | const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() { |
| 916 | if (!image_info_load_attempted_) { |
| 917 | image_info_load_attempted_ = true; |
| 918 | |
| 919 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 920 | std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces(); |
| 921 | if (!image_spaces.empty()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 922 | cached_image_info_.location = image_spaces[0]->GetImageLocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 923 | |
| 924 | if (isa_ == kRuntimeISA) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 925 | const ImageHeader& image_header = image_spaces[0]->GetImageHeader(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 926 | cached_image_info_.oat_checksum = image_header.GetOatChecksum(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 927 | cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>( |
| 928 | image_header.GetOatDataBegin()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 929 | cached_image_info_.patch_delta = image_header.GetPatchDelta(); |
| 930 | } else { |
| 931 | std::unique_ptr<ImageHeader> image_header( |
| 932 | gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 933 | cached_image_info_.location.c_str(), isa_)); |
| 934 | cached_image_info_.oat_checksum = image_header->GetOatChecksum(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 935 | cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>( |
| 936 | image_header->GetOatDataBegin()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 937 | cached_image_info_.patch_delta = image_header->GetPatchDelta(); |
| 938 | } |
| 939 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 940 | image_info_load_succeeded_ = (!image_spaces.empty()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 941 | } |
| 942 | return image_info_load_succeeded_ ? &cached_image_info_ : nullptr; |
| 943 | } |
| 944 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 945 | gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) { |
| 946 | DCHECK(oat_file != nullptr); |
| 947 | std::string art_file = ArtFileName(oat_file); |
| 948 | if (art_file.empty()) { |
| 949 | return nullptr; |
| 950 | } |
| 951 | std::string error_msg; |
| 952 | ScopedObjectAccess soa(Thread::Current()); |
| 953 | gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), |
| 954 | oat_file, |
| 955 | &error_msg); |
Mathieu Chartier | e778fc7 | 2016-01-25 20:11:28 -0800 | [diff] [blame] | 956 | if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 957 | LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg; |
| 958 | } |
| 959 | return ret; |
| 960 | } |
| 961 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 962 | } // namespace art |
| 963 | |