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