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