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