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