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