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