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