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