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