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( |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 280 | dex_location, nullptr, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 281 | if (oat_dex_file == nullptr) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 282 | LOG(WARNING) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 283 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 284 | } |
| 285 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 286 | 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] | 287 | if (dex_file.get() == nullptr) { |
| 288 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
| 289 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 290 | } |
| 291 | dex_files.push_back(std::move(dex_file)); |
| 292 | |
| 293 | // Load secondary multidex files |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 294 | for (size_t i = 1; ; i++) { |
| 295 | std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 296 | oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 297 | if (oat_dex_file == nullptr) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 298 | // There are no more secondary dex files to load. |
| 299 | break; |
| 300 | } |
| 301 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 302 | dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 303 | if (dex_file.get() == nullptr) { |
| 304 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
| 305 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 306 | } |
| 307 | dex_files.push_back(std::move(dex_file)); |
| 308 | } |
| 309 | return dex_files; |
| 310 | } |
| 311 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 312 | bool OatFileAssistant::HasOriginalDexFiles() { |
| 313 | // Ensure GetRequiredDexChecksum has been run so that |
| 314 | // has_original_dex_files_ is initialized. We don't care about the result of |
| 315 | // GetRequiredDexChecksum. |
| 316 | GetRequiredDexChecksum(); |
| 317 | return has_original_dex_files_; |
| 318 | } |
| 319 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 320 | const std::string* OatFileAssistant::OdexFileName() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 321 | return odex_.Filename(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | bool OatFileAssistant::OdexFileExists() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 325 | return odex_.Exists(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 328 | OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 329 | return odex_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | bool OatFileAssistant::OdexFileIsOutOfDate() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 333 | return odex_.IsOutOfDate(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | bool OatFileAssistant::OdexFileNeedsRelocation() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 337 | return odex_.NeedsRelocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | bool OatFileAssistant::OdexFileIsUpToDate() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 341 | return odex_.IsUpToDate(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 344 | CompilerFilter::Filter OatFileAssistant::OdexFileCompilerFilter() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 345 | return odex_.CompilerFilter(); |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 346 | } |
Richard Uhler | 4c7f193 | 2016-04-15 15:51:21 -0700 | [diff] [blame] | 347 | |
| 348 | static std::string ArtFileName(const OatFile* oat_file) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 349 | const std::string oat_file_location = oat_file->GetLocation(); |
| 350 | // Replace extension with .art |
| 351 | const size_t last_ext = oat_file_location.find_last_of('.'); |
| 352 | if (last_ext == std::string::npos) { |
| 353 | LOG(ERROR) << "No extension in oat file " << oat_file_location; |
| 354 | return std::string(); |
| 355 | } |
| 356 | return oat_file_location.substr(0, last_ext) + ".art"; |
| 357 | } |
| 358 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 359 | const std::string* OatFileAssistant::OatFileName() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 360 | return oat_.Filename(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | bool OatFileAssistant::OatFileExists() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 364 | return oat_.Exists(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 367 | OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 368 | return oat_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | bool OatFileAssistant::OatFileIsOutOfDate() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 372 | return oat_.IsOutOfDate(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | bool OatFileAssistant::OatFileNeedsRelocation() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 376 | return oat_.NeedsRelocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | bool OatFileAssistant::OatFileIsUpToDate() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 380 | return oat_.IsUpToDate(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 383 | CompilerFilter::Filter OatFileAssistant::OatFileCompilerFilter() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 384 | return oat_.CompilerFilter(); |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 387 | OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 388 | // Verify the dex checksum. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 389 | // Note: GetOatDexFile will return null if the dex checksum doesn't match |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 390 | // what we provide, which verifies the primary dex checksum for us. |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 391 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 392 | const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum(); |
| 393 | const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile( |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 394 | dex_location_.c_str(), dex_checksum_pointer, &error_msg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 395 | if (oat_dex_file == nullptr) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 396 | VLOG(oat) << error_msg; |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 397 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Verify the dex checksums for any secondary multidex files |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 401 | for (size_t i = 1; ; i++) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 402 | std::string secondary_dex_location = 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 |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 404 | = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr); |
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 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 410 | uint32_t expected_secondary_checksum = 0; |
| 411 | if (DexFile::GetChecksum(secondary_dex_location.c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 412 | &expected_secondary_checksum, &error_msg)) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 413 | uint32_t actual_secondary_checksum |
| 414 | = secondary_oat_dex_file->GetDexFileLocationChecksum(); |
| 415 | if (expected_secondary_checksum != actual_secondary_checksum) { |
| 416 | VLOG(oat) << "Dex checksum does not match for secondary dex: " |
| 417 | << secondary_dex_location |
| 418 | << ". Expected: " << expected_secondary_checksum |
| 419 | << ", Actual: " << actual_secondary_checksum; |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 420 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 421 | } |
| 422 | } else { |
| 423 | // If we can't get the checksum for the secondary location, we assume |
| 424 | // the dex checksum is up to date for this and all other secondary dex |
| 425 | // files. |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 430 | CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter(); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 431 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 432 | // Verify the image checksum |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 433 | if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) { |
| 434 | const ImageInfo* image_info = GetImageInfo(); |
| 435 | if (image_info == nullptr) { |
| 436 | VLOG(oat) << "No image for oat image checksum to match against."; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 437 | |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 438 | if (HasOriginalDexFiles()) { |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 439 | return kOatOutOfDate; |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // If there is no original dex file to fall back to, grudgingly accept |
| 443 | // the oat file. This could technically lead to crashes, but there's no |
| 444 | // way we could find a better oat file to use for this dex location, |
| 445 | // and it's better than being stuck in a boot loop with no way out. |
| 446 | // The problem will hopefully resolve itself the next time the runtime |
| 447 | // starts up. |
| 448 | LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. " |
| 449 | << "Allow oat file use. This is potentially dangerous."; |
| 450 | } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() |
| 451 | != GetCombinedImageChecksum()) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 452 | VLOG(oat) << "Oat image checksum does not match image checksum."; |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 453 | return kOatOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 454 | } |
| 455 | } else { |
| 456 | VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Vladimir Marko | f6d1e0f | 2016-05-23 15:32:42 +0100 | [diff] [blame] | 459 | if (CompilerFilter::IsBytecodeCompilationEnabled(current_compiler_filter)) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 460 | if (!file.IsPic()) { |
| 461 | const ImageInfo* image_info = GetImageInfo(); |
| 462 | if (image_info == nullptr) { |
| 463 | VLOG(oat) << "No image to check oat relocation against."; |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 464 | return kOatNeedsRelocation; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 465 | } |
Richard Uhler | a62d2f0 | 2016-03-18 15:05:30 -0700 | [diff] [blame] | 466 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 467 | // Verify the oat_data_begin recorded for the image in the oat file matches |
| 468 | // the actual oat_data_begin for boot.oat in the image. |
| 469 | const OatHeader& oat_header = file.GetOatHeader(); |
| 470 | uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin(); |
| 471 | if (oat_data_begin != image_info->oat_data_begin) { |
| 472 | VLOG(oat) << file.GetLocation() << |
| 473 | ": Oat file image oat_data_begin (" << oat_data_begin << ")" |
| 474 | << " does not match actual image oat_data_begin (" |
| 475 | << image_info->oat_data_begin << ")"; |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 476 | return kOatNeedsRelocation; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 477 | } |
Richard Uhler | a62d2f0 | 2016-03-18 15:05:30 -0700 | [diff] [blame] | 478 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 479 | // Verify the oat_patch_delta recorded for the image in the oat file matches |
| 480 | // the actual oat_patch_delta for the image. |
| 481 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 482 | if (oat_patch_delta != image_info->patch_delta) { |
| 483 | VLOG(oat) << file.GetLocation() << |
| 484 | ": Oat file image patch delta (" << oat_patch_delta << ")" |
| 485 | << " does not match actual image patch delta (" |
| 486 | << image_info->patch_delta << ")"; |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 487 | return kOatNeedsRelocation; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 488 | } |
| 489 | } else { |
| 490 | // Oat files compiled in PIC mode do not require relocation. |
| 491 | VLOG(oat) << "Oat relocation test skipped for PIC oat file"; |
| 492 | } |
| 493 | } else { |
| 494 | VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 495 | } |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 496 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 499 | OatFileAssistant::ResultOfAttemptToUpdate |
| 500 | OatFileAssistant::RelocateOatFile(const std::string* input_file, std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 501 | CHECK(error_msg != nullptr); |
| 502 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 503 | if (input_file == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 504 | *error_msg = "Patching of oat file for dex location " + dex_location_ |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 505 | + " not attempted because the input file name could not be determined."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 506 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 507 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 508 | const std::string& input_file_name = *input_file; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 509 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 510 | if (oat_.Filename() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 511 | *error_msg = "Patching of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 512 | + " not attempted because the oat file name could not be determined."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 513 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 514 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 515 | const std::string& oat_file_name = *oat_.Filename(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 516 | |
| 517 | const ImageInfo* image_info = GetImageInfo(); |
| 518 | Runtime* runtime = Runtime::Current(); |
| 519 | if (image_info == nullptr) { |
| 520 | *error_msg = "Patching of oat file " + oat_file_name |
| 521 | + " not attempted because no image location was found."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 522 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (!runtime->IsDex2OatEnabled()) { |
| 526 | *error_msg = "Patching of oat file " + oat_file_name |
| 527 | + " not attempted because dex2oat is disabled"; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 528 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | std::vector<std::string> argv; |
| 532 | argv.push_back(runtime->GetPatchoatExecutable()); |
| 533 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_))); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 534 | argv.push_back("--input-oat-file=" + input_file_name); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 535 | argv.push_back("--output-oat-file=" + oat_file_name); |
| 536 | argv.push_back("--patched-image-location=" + image_info->location); |
| 537 | |
| 538 | std::string command_line(Join(argv, ' ')); |
| 539 | if (!Exec(argv, error_msg)) { |
| 540 | // Manually delete the file. This ensures there is no garbage left over if |
| 541 | // the process unexpectedly died. |
Vladimir Marko | 66fdcbd | 2016-04-05 14:19:08 +0100 | [diff] [blame] | 542 | unlink(oat_file_name.c_str()); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 543 | return kUpdateFailed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // 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] | 547 | oat_.Reset(); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 548 | return kUpdateSucceeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 551 | OatFileAssistant::ResultOfAttemptToUpdate |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 552 | OatFileAssistant::GenerateOatFile(std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 553 | CHECK(error_msg != nullptr); |
| 554 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 555 | Runtime* runtime = Runtime::Current(); |
| 556 | if (!runtime->IsDex2OatEnabled()) { |
| 557 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
| 558 | + " not attempted because dex2oat is disabled."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 559 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 562 | if (oat_.Filename() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 563 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 564 | + " not attempted because the oat file name could not be determined."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 565 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 566 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 567 | const std::string& oat_file_name = *oat_.Filename(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 568 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 569 | // dex2oat ignores missing dex files and doesn't report an error. |
| 570 | // Check explicitly here so we can detect the error properly. |
| 571 | // TODO: Why does dex2oat behave that way? |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 572 | if (!OS::FileExists(dex_location_.c_str())) { |
| 573 | *error_msg = "Dex location " + dex_location_ + " does not exists."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 574 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 577 | std::unique_ptr<File> oat_file; |
| 578 | oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str())); |
| 579 | if (oat_file.get() == nullptr) { |
| 580 | *error_msg = "Generation of oat file " + oat_file_name |
| 581 | + " not attempted because the oat file could not be created."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 582 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | if (fchmod(oat_file->Fd(), 0644) != 0) { |
| 586 | *error_msg = "Generation of oat file " + oat_file_name |
| 587 | + " not attempted because the oat file could not be made world readable."; |
| 588 | oat_file->Erase(); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 589 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | std::vector<std::string> args; |
| 593 | args.push_back("--dex-file=" + dex_location_); |
| 594 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
| 595 | args.push_back("--oat-location=" + oat_file_name); |
| 596 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 597 | if (!Dex2Oat(args, error_msg)) { |
| 598 | // Manually delete the file. This ensures there is no garbage left over if |
| 599 | // the process unexpectedly died. |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 600 | oat_file->Erase(); |
Vladimir Marko | 66fdcbd | 2016-04-05 14:19:08 +0100 | [diff] [blame] | 601 | unlink(oat_file_name.c_str()); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 602 | return kUpdateFailed; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | if (oat_file->FlushCloseOrErase() != 0) { |
| 606 | *error_msg = "Unable to close oat file " + oat_file_name; |
Vladimir Marko | 66fdcbd | 2016-04-05 14:19:08 +0100 | [diff] [blame] | 607 | unlink(oat_file_name.c_str()); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 608 | return kUpdateFailed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // 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] | 612 | oat_.Reset(); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 613 | return kUpdateSucceeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args, |
| 617 | std::string* error_msg) { |
| 618 | Runtime* runtime = Runtime::Current(); |
| 619 | std::string image_location = ImageLocation(); |
| 620 | if (image_location.empty()) { |
| 621 | *error_msg = "No image location found for Dex2Oat."; |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | std::vector<std::string> argv; |
| 626 | argv.push_back(runtime->GetCompilerExecutable()); |
| 627 | argv.push_back("--runtime-arg"); |
| 628 | argv.push_back("-classpath"); |
| 629 | argv.push_back("--runtime-arg"); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 630 | std::string class_path = runtime->GetClassPathString(); |
| 631 | if (class_path == "") { |
| 632 | class_path = OatFile::kSpecialSharedLibrary; |
| 633 | } |
| 634 | argv.push_back(class_path); |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 635 | if (runtime->IsDebuggable()) { |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 636 | argv.push_back("--debuggable"); |
| 637 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 638 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 639 | |
| 640 | if (!runtime->IsVerificationEnabled()) { |
| 641 | argv.push_back("--compiler-filter=verify-none"); |
| 642 | } |
| 643 | |
| 644 | if (runtime->MustRelocateIfPossible()) { |
| 645 | argv.push_back("--runtime-arg"); |
| 646 | argv.push_back("-Xrelocate"); |
| 647 | } else { |
| 648 | argv.push_back("--runtime-arg"); |
| 649 | argv.push_back("-Xnorelocate"); |
| 650 | } |
| 651 | |
| 652 | if (!kIsTargetBuild) { |
| 653 | argv.push_back("--host"); |
| 654 | } |
| 655 | |
| 656 | argv.push_back("--boot-image=" + image_location); |
| 657 | |
| 658 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 659 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 660 | |
| 661 | argv.insert(argv.end(), args.begin(), args.end()); |
| 662 | |
| 663 | std::string command_line(Join(argv, ' ')); |
| 664 | return Exec(argv, error_msg); |
| 665 | } |
| 666 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 667 | bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location, |
| 668 | InstructionSet isa, |
| 669 | std::string* odex_filename, |
| 670 | std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 671 | CHECK(odex_filename != nullptr); |
| 672 | CHECK(error_msg != nullptr); |
| 673 | |
| 674 | // 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] | 675 | // .odex and inserting an oat/<isa> directory. For example: |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 676 | // location = /foo/bar/baz.jar |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 677 | // odex_location = /foo/bar/oat/<isa>/baz.odex |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 678 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 679 | // Find the directory portion of the dex location and add the oat/<isa> |
| 680 | // directory. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 681 | size_t pos = location.rfind('/'); |
| 682 | if (pos == std::string::npos) { |
| 683 | *error_msg = "Dex location " + location + " has no directory."; |
| 684 | return false; |
| 685 | } |
| 686 | std::string dir = location.substr(0, pos+1); |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 687 | dir += "oat/" + std::string(GetInstructionSetString(isa)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 688 | |
| 689 | // Find the file portion of the dex location. |
| 690 | std::string file; |
| 691 | if (pos == std::string::npos) { |
| 692 | file = location; |
| 693 | } else { |
| 694 | file = location.substr(pos+1); |
| 695 | } |
| 696 | |
| 697 | // Get the base part of the file without the extension. |
| 698 | pos = file.rfind('.'); |
| 699 | if (pos == std::string::npos) { |
| 700 | *error_msg = "Dex location " + location + " has no extension."; |
| 701 | return false; |
| 702 | } |
| 703 | std::string base = file.substr(0, pos); |
| 704 | |
| 705 | *odex_filename = dir + "/" + base + ".odex"; |
| 706 | return true; |
| 707 | } |
| 708 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 709 | bool OatFileAssistant::DexLocationToOatFilename(const std::string& location, |
| 710 | InstructionSet isa, |
| 711 | std::string* oat_filename, |
| 712 | std::string* error_msg) { |
| 713 | CHECK(oat_filename != nullptr); |
| 714 | CHECK(error_msg != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 715 | |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame^] | 716 | std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa)); |
| 717 | if (cache_dir.empty()) { |
| 718 | *error_msg = "Dalvik cache directory does not exist"; |
| 719 | return false; |
| 720 | } |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 721 | |
| 722 | // TODO: The oat file assistant should be the definitive place for |
| 723 | // determining the oat file name from the dex location, not |
| 724 | // GetDalvikCacheFilename. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 725 | 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] | 726 | } |
| 727 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 728 | std::string OatFileAssistant::ImageLocation() { |
| 729 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 730 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 731 | runtime->GetHeap()->GetBootImageSpaces(); |
| 732 | if (image_spaces.empty()) { |
| 733 | return ""; |
| 734 | } |
| 735 | return image_spaces[0]->GetImageLocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | const uint32_t* OatFileAssistant::GetRequiredDexChecksum() { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 739 | if (!required_dex_checksum_attempted_) { |
| 740 | required_dex_checksum_attempted_ = true; |
| 741 | required_dex_checksum_found_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 742 | std::string error_msg; |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 743 | 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] | 744 | required_dex_checksum_found_ = true; |
| 745 | has_original_dex_files_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 746 | } else { |
| 747 | // This can happen if the original dex file has been stripped from the |
| 748 | // apk. |
| 749 | VLOG(oat) << "OatFileAssistant: " << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 750 | has_original_dex_files_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 751 | |
| 752 | // Get the checksum from the odex if we can. |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 753 | const OatFile* odex_file = odex_.GetFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 754 | if (odex_file != nullptr) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 755 | const OatFile::OatDexFile* odex_dex_file |
| 756 | = odex_file->GetOatDexFile(dex_location_.c_str(), nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 757 | if (odex_dex_file != nullptr) { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 758 | cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum(); |
| 759 | required_dex_checksum_found_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 764 | return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 767 | const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() { |
| 768 | if (!image_info_load_attempted_) { |
| 769 | image_info_load_attempted_ = true; |
| 770 | |
| 771 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 772 | std::vector<gc::space::ImageSpace*> image_spaces = runtime->GetHeap()->GetBootImageSpaces(); |
| 773 | if (!image_spaces.empty()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 774 | cached_image_info_.location = image_spaces[0]->GetImageLocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 775 | |
| 776 | if (isa_ == kRuntimeISA) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 777 | const ImageHeader& image_header = image_spaces[0]->GetImageHeader(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 778 | cached_image_info_.oat_checksum = image_header.GetOatChecksum(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 779 | cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>( |
| 780 | image_header.GetOatDataBegin()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 781 | cached_image_info_.patch_delta = image_header.GetPatchDelta(); |
| 782 | } else { |
| 783 | std::unique_ptr<ImageHeader> image_header( |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 784 | gc::space::ImageSpace::ReadImageHeaderOrDie(cached_image_info_.location.c_str(), isa_)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 785 | cached_image_info_.oat_checksum = image_header->GetOatChecksum(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 786 | cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>( |
| 787 | image_header->GetOatDataBegin()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 788 | cached_image_info_.patch_delta = image_header->GetPatchDelta(); |
| 789 | } |
| 790 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 791 | image_info_load_succeeded_ = (!image_spaces.empty()); |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 792 | |
Jeff Hao | fd336c3 | 2016-04-07 19:46:31 -0700 | [diff] [blame] | 793 | combined_image_checksum_ = CalculateCombinedImageChecksum(isa_); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 794 | } |
| 795 | return image_info_load_succeeded_ ? &cached_image_info_ : nullptr; |
| 796 | } |
| 797 | |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 798 | // TODO: Use something better than xor. |
Jeff Hao | fd336c3 | 2016-04-07 19:46:31 -0700 | [diff] [blame] | 799 | uint32_t OatFileAssistant::CalculateCombinedImageChecksum(InstructionSet isa) { |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 800 | uint32_t checksum = 0; |
| 801 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 802 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Jeff Hao | fd336c3 | 2016-04-07 19:46:31 -0700 | [diff] [blame] | 803 | if (isa == kRuntimeISA) { |
| 804 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 805 | checksum ^= image_space->GetImageHeader().GetOatChecksum(); |
| 806 | } |
| 807 | } else { |
| 808 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 809 | std::string location = image_space->GetImageLocation(); |
| 810 | std::unique_ptr<ImageHeader> image_header( |
| 811 | gc::space::ImageSpace::ReadImageHeaderOrDie(location.c_str(), isa)); |
| 812 | checksum ^= image_header->GetOatChecksum(); |
| 813 | } |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 814 | } |
| 815 | return checksum; |
| 816 | } |
| 817 | |
| 818 | uint32_t OatFileAssistant::GetCombinedImageChecksum() { |
| 819 | if (!image_info_load_attempted_) { |
| 820 | GetImageInfo(); |
| 821 | } |
| 822 | return combined_image_checksum_; |
| 823 | } |
| 824 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 825 | gc::space::ImageSpace* OatFileAssistant::OpenImageSpace(const OatFile* oat_file) { |
| 826 | DCHECK(oat_file != nullptr); |
| 827 | std::string art_file = ArtFileName(oat_file); |
| 828 | if (art_file.empty()) { |
| 829 | return nullptr; |
| 830 | } |
| 831 | std::string error_msg; |
| 832 | ScopedObjectAccess soa(Thread::Current()); |
| 833 | gc::space::ImageSpace* ret = gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), |
| 834 | oat_file, |
| 835 | &error_msg); |
Mathieu Chartier | e778fc7 | 2016-01-25 20:11:28 -0800 | [diff] [blame] | 836 | 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] | 837 | LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg; |
| 838 | } |
| 839 | return ret; |
| 840 | } |
| 841 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 842 | OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant) |
| 843 | : oat_file_assistant_(oat_file_assistant) |
| 844 | {} |
| 845 | |
| 846 | const std::string* OatFileAssistant::OatFileInfo::Filename() { |
| 847 | return filename_provided_ ? &filename_ : nullptr; |
| 848 | } |
| 849 | |
| 850 | bool OatFileAssistant::OatFileInfo::Exists() { |
| 851 | return GetFile() != nullptr; |
| 852 | } |
| 853 | |
| 854 | OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() { |
| 855 | if (!status_attempted_) { |
| 856 | status_attempted_ = true; |
| 857 | const OatFile* file = GetFile(); |
| 858 | if (file == nullptr) { |
| 859 | status_ = kOatOutOfDate; |
| 860 | } else { |
| 861 | status_ = oat_file_assistant_->GivenOatFileStatus(*file); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 862 | VLOG(oat) << file->GetLocation() << " is " << status_ |
| 863 | << " with filter " << file->GetCompilerFilter(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | return status_; |
| 867 | } |
| 868 | |
| 869 | bool OatFileAssistant::OatFileInfo::IsOutOfDate() { |
| 870 | return Status() == kOatOutOfDate; |
| 871 | } |
| 872 | |
| 873 | bool OatFileAssistant::OatFileInfo::NeedsRelocation() { |
| 874 | return Status() == kOatNeedsRelocation; |
| 875 | } |
| 876 | |
| 877 | bool OatFileAssistant::OatFileInfo::IsUpToDate() { |
| 878 | return Status() == kOatUpToDate; |
| 879 | } |
| 880 | |
| 881 | CompilerFilter::Filter OatFileAssistant::OatFileInfo::CompilerFilter() { |
| 882 | const OatFile* file = GetFile(); |
| 883 | CHECK(file != nullptr); |
| 884 | return file->GetCompilerFilter(); |
| 885 | } |
| 886 | |
| 887 | const OatFile* OatFileAssistant::OatFileInfo::GetFile() { |
| 888 | CHECK(!file_released_) << "GetFile called after oat file released."; |
| 889 | if (!load_attempted_) { |
| 890 | load_attempted_ = true; |
| 891 | if (filename_provided_) { |
| 892 | std::string error_msg; |
| 893 | file_.reset(OatFile::Open(filename_.c_str(), |
| 894 | filename_.c_str(), |
| 895 | nullptr, |
| 896 | nullptr, |
| 897 | oat_file_assistant_->load_executable_, |
| 898 | /*low_4gb*/false, |
| 899 | oat_file_assistant_->dex_location_.c_str(), |
| 900 | &error_msg)); |
| 901 | if (file_.get() == nullptr) { |
| 902 | VLOG(oat) << "OatFileAssistant test for existing oat file " |
| 903 | << filename_ << ": " << error_msg; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | return file_.get(); |
| 908 | } |
| 909 | |
| 910 | bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay( |
| 911 | CompilerFilter::Filter target, bool profile_changed) { |
| 912 | const OatFile* file = GetFile(); |
| 913 | if (file == nullptr) { |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | CompilerFilter::Filter current = file->GetCompilerFilter(); |
| 918 | if (profile_changed && CompilerFilter::DependsOnProfile(current)) { |
| 919 | VLOG(oat) << "Compiler filter not okay because Profile changed"; |
| 920 | return false; |
| 921 | } |
| 922 | return CompilerFilter::IsAsGoodAs(current, target); |
| 923 | } |
| 924 | |
| 925 | bool OatFileAssistant::OatFileInfo::IsExecutable() { |
| 926 | const OatFile* file = GetFile(); |
| 927 | return (file != nullptr && file->IsExecutable()); |
| 928 | } |
| 929 | |
| 930 | bool OatFileAssistant::OatFileInfo::HasPatchInfo() { |
| 931 | const OatFile* file = GetFile(); |
| 932 | return (file != nullptr && file->HasPatchInfo()); |
| 933 | } |
| 934 | |
| 935 | void OatFileAssistant::OatFileInfo::Reset() { |
| 936 | load_attempted_ = false; |
| 937 | file_.reset(); |
| 938 | status_attempted_ = false; |
| 939 | } |
| 940 | |
| 941 | void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) { |
| 942 | filename_provided_ = true; |
| 943 | filename_ = filename; |
| 944 | Reset(); |
| 945 | } |
| 946 | |
| 947 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() { |
| 948 | file_released_ = true; |
| 949 | return std::move(file_); |
| 950 | } |
| 951 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 952 | } // namespace art |
| 953 | |