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 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 26 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 27 | #include "base/logging.h" // For VLOG. |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 28 | #include "base/macros.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 29 | #include "base/os.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 30 | #include "base/stl_util.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 31 | #include "base/utils.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 32 | #include "class_linker.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 33 | #include "compiler_filter.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 34 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 35 | #include "dex/dex_file_loader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 36 | #include "exec_utils.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 37 | #include "gc/heap.h" |
| 38 | #include "gc/space/image_space.h" |
| 39 | #include "image.h" |
| 40 | #include "oat.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 41 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 42 | #include "scoped_thread_state_change-inl.h" |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 43 | #include "vdex_file.h" |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 44 | #include "class_loader_context.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 45 | |
| 46 | namespace art { |
| 47 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 48 | using android::base::StringPrintf; |
| 49 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 50 | std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) { |
| 51 | switch (status) { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 52 | case OatFileAssistant::kOatCannotOpen: |
| 53 | stream << "kOatCannotOpen"; |
| 54 | break; |
| 55 | case OatFileAssistant::kOatDexOutOfDate: |
| 56 | stream << "kOatDexOutOfDate"; |
| 57 | break; |
| 58 | case OatFileAssistant::kOatBootImageOutOfDate: |
| 59 | stream << "kOatBootImageOutOfDate"; |
| 60 | break; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 61 | case OatFileAssistant::kOatUpToDate: |
| 62 | stream << "kOatUpToDate"; |
| 63 | break; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 64 | default: |
| 65 | UNREACHABLE(); |
| 66 | } |
| 67 | |
| 68 | return stream; |
| 69 | } |
| 70 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 71 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 72 | const InstructionSet isa, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 73 | bool load_executable, |
| 74 | bool only_load_system_executable) |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 75 | : OatFileAssistant(dex_location, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 76 | isa, |
| 77 | load_executable, |
| 78 | only_load_system_executable, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 79 | /*vdex_fd=*/ -1, |
| 80 | /*oat_fd=*/ -1, |
| 81 | /*zip_fd=*/ -1) {} |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 82 | |
| 83 | |
| 84 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 85 | const InstructionSet isa, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 86 | bool load_executable, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 87 | bool only_load_system_executable, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 88 | int vdex_fd, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 89 | int oat_fd, |
| 90 | int zip_fd) |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 91 | : isa_(isa), |
| 92 | load_executable_(load_executable), |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 93 | only_load_system_executable_(only_load_system_executable), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 94 | odex_(this, /*is_oat_location=*/ false), |
| 95 | oat_(this, /*is_oat_location=*/ true), |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 96 | zip_fd_(zip_fd) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 97 | CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 98 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 99 | if (zip_fd < 0) { |
| 100 | CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd |
| 101 | << " oat_fd=" << oat_fd; |
| 102 | CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd |
| 103 | << " vdex_fd=" << vdex_fd;; |
| 104 | } |
| 105 | |
Calin Juravle | 099f8d6 | 2017-09-05 19:04:04 -0700 | [diff] [blame] | 106 | dex_location_.assign(dex_location); |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 107 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 108 | if (load_executable_ && isa != kRuntimeISA) { |
| 109 | LOG(WARNING) << "OatFileAssistant: Load executable specified, " |
| 110 | << "but isa is not kRuntimeISA. Will not attempt to load executable."; |
| 111 | load_executable_ = false; |
| 112 | } |
| 113 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 114 | // Get the odex filename. |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 115 | std::string error_msg; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 116 | std::string odex_file_name; |
| 117 | if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) { |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 118 | odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 119 | } else { |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 120 | LOG(WARNING) << "Failed to determine odex file name: " << error_msg; |
| 121 | } |
| 122 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 123 | if (!UseFdToReadFiles()) { |
| 124 | // Get the oat filename. |
| 125 | std::string oat_file_name; |
| 126 | if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 127 | oat_.Reset(oat_file_name, /*use_fd=*/ false); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 128 | } else { |
| 129 | LOG(WARNING) << "Failed to determine oat file name for dex location " |
| 130 | << dex_location_ << ": " << error_msg; |
| 131 | } |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Check if the dex directory is writable. |
| 135 | // This will be needed in most uses of OatFileAssistant and so it's OK to |
| 136 | // compute it eagerly. (the only use which will not make use of it is |
| 137 | // OatFileAssistant::GetStatusDump()) |
| 138 | size_t pos = dex_location_.rfind('/'); |
| 139 | if (pos == std::string::npos) { |
| 140 | LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 141 | } else if (!UseFdToReadFiles()) { |
| 142 | // We cannot test for parent access when using file descriptors. That's ok |
| 143 | // because in this case we will always pick the odex file anyway. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 144 | std::string parent = dex_location_.substr(0, pos); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 145 | if (access(parent.c_str(), W_OK) == 0) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 146 | dex_parent_writable_ = true; |
| 147 | } else { |
| 148 | VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno); |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 149 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 150 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | OatFileAssistant::~OatFileAssistant() { |
| 154 | // Clean up the lock file. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 155 | if (flock_.get() != nullptr) { |
| 156 | unlink(flock_->GetPath().c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 160 | bool OatFileAssistant::UseFdToReadFiles() { |
| 161 | return zip_fd_ >= 0; |
| 162 | } |
| 163 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 164 | bool OatFileAssistant::IsInBootClassPath() { |
| 165 | // Note: We check the current boot class path, regardless of the ISA |
| 166 | // specified by the user. This is okay, because the boot class path should |
| 167 | // be the same for all ISAs. |
| 168 | // TODO: Can we verify the boot class path is the same for all ISAs? |
| 169 | Runtime* runtime = Runtime::Current(); |
| 170 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 171 | const auto& boot_class_path = class_linker->GetBootClassPath(); |
| 172 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 173 | if (boot_class_path[i]->GetLocation() == dex_location_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 174 | VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path"; |
| 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 181 | int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, |
| 182 | bool profile_changed, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 183 | bool downgrade, |
| 184 | ClassLoaderContext* class_loader_context) { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 185 | OatFileInfo& info = GetBestInfo(); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 186 | DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, |
| 187 | profile_changed, |
| 188 | downgrade, |
| 189 | class_loader_context); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 190 | if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) { |
| 191 | return dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 192 | } |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 193 | return -dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 196 | bool OatFileAssistant::IsUpToDate() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 197 | return GetBestInfo().Status() == kOatUpToDate; |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 200 | std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 201 | return GetBestInfo().ReleaseFileForUse(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 204 | std::string OatFileAssistant::GetStatusDump() { |
| 205 | std::ostringstream status; |
| 206 | bool oat_file_exists = false; |
| 207 | bool odex_file_exists = false; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 208 | if (oat_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 209 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 210 | CHECK(oat_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 211 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 212 | oat_file_exists = true; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 213 | status << *oat_.Filename() << "[status=" << oat_.Status() << ", "; |
| 214 | const OatFile* file = oat_.GetFile(); |
| 215 | if (file == nullptr) { |
| 216 | // If the file is null even though the status is not kOatCannotOpen, it |
| 217 | // means we must have a vdex file with no corresponding oat file. In |
| 218 | // this case we cannot determine the compilation filter. Indicate that |
| 219 | // we have only the vdex file instead. |
| 220 | status << "vdex-only"; |
| 221 | } else { |
| 222 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 223 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 226 | if (odex_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 227 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 228 | CHECK(odex_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 229 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 230 | odex_file_exists = true; |
| 231 | if (oat_file_exists) { |
| 232 | status << "] "; |
| 233 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 234 | status << *odex_.Filename() << "[status=" << odex_.Status() << ", "; |
| 235 | const OatFile* file = odex_.GetFile(); |
| 236 | if (file == nullptr) { |
| 237 | status << "vdex-only"; |
| 238 | } else { |
| 239 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 240 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (!oat_file_exists && !odex_file_exists) { |
| 244 | status << "invalid["; |
| 245 | } |
| 246 | |
| 247 | status << "]"; |
| 248 | return status.str(); |
| 249 | } |
| 250 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 251 | std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 252 | const OatFile &oat_file, const char *dex_location) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 253 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 254 | if (LoadDexFiles(oat_file, dex_location, &dex_files)) { |
| 255 | return dex_files; |
| 256 | } else { |
| 257 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 258 | } |
| 259 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 260 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 261 | bool OatFileAssistant::LoadDexFiles( |
| 262 | const OatFile &oat_file, |
| 263 | const std::string& dex_location, |
| 264 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 265 | // Load the main dex file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 266 | std::string error_msg; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 267 | const OatDexFile* oat_dex_file = oat_file.GetOatDexFile( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 268 | dex_location.c_str(), nullptr, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 269 | if (oat_dex_file == nullptr) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 270 | LOG(WARNING) << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 271 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 274 | 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] | 275 | if (dex_file.get() == nullptr) { |
| 276 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 277 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 278 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 279 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 280 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 281 | // Load the rest of the multidex entries |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 282 | for (size_t i = 1;; i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 283 | std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 284 | oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 285 | if (oat_dex_file == nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 286 | // There are no more multidex entries to load. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 287 | break; |
| 288 | } |
| 289 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 290 | dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 291 | if (dex_file.get() == nullptr) { |
| 292 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 293 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 294 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 295 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 296 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 297 | return true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 300 | bool OatFileAssistant::HasOriginalDexFiles() { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 301 | // Ensure GetRequiredDexChecksums has been run so that |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 302 | // 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] | 303 | // GetRequiredDexChecksums. |
| 304 | GetRequiredDexChecksums(); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 305 | return has_original_dex_files_; |
| 306 | } |
| 307 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 308 | OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 309 | return odex_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 312 | OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 313 | return oat_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 316 | bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 317 | const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(); |
| 318 | if (required_dex_checksums == nullptr) { |
| 319 | LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date."; |
| 320 | return true; |
| 321 | } |
| 322 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 323 | uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles(); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 324 | if (required_dex_checksums->size() != number_of_dex_files) { |
| 325 | *error_msg = StringPrintf("expected %zu dex files but found %u", |
| 326 | required_dex_checksums->size(), |
| 327 | number_of_dex_files); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 328 | return false; |
Andreas Gampe | f8cd890 | 2017-01-18 16:05:01 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 331 | for (uint32_t i = 0; i < number_of_dex_files; i++) { |
| 332 | uint32_t expected_checksum = (*required_dex_checksums)[i]; |
| 333 | uint32_t actual_checksum = file.GetLocationChecksum(i); |
| 334 | if (expected_checksum != actual_checksum) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 335 | std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 336 | *error_msg = StringPrintf("Dex checksum does not match for dex: %s." |
| 337 | "Expected: %u, actual: %u", |
| 338 | dex.c_str(), |
| 339 | expected_checksum, |
| 340 | actual_checksum); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | } |
| 344 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
| 348 | bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 349 | const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(); |
| 350 | if (required_dex_checksums == nullptr) { |
| 351 | LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date."; |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount(); |
| 356 | if (required_dex_checksums->size() != number_of_dex_files) { |
| 357 | *error_msg = StringPrintf("expected %zu dex files but found %u", |
| 358 | required_dex_checksums->size(), |
| 359 | number_of_dex_files); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 360 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 363 | for (uint32_t i = 0; i < number_of_dex_files; i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 364 | std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 365 | uint32_t expected_checksum = (*required_dex_checksums)[i]; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 366 | const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 367 | if (oat_dex_file == nullptr) { |
| 368 | *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str()); |
| 369 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 370 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 371 | uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 372 | if (expected_checksum != actual_checksum) { |
| 373 | VLOG(oat) << "Dex checksum does not match for dex: " << dex |
| 374 | << ". Expected: " << expected_checksum |
| 375 | << ", Actual: " << actual_checksum; |
| 376 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | |
| 382 | OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) { |
| 383 | // Verify the ART_USE_READ_BARRIER state. |
| 384 | // TODO: Don't fully reject files due to read barrier state. If they contain |
| 385 | // compiled code and are otherwise okay, we should return something like |
| 386 | // kOatRelocationOutOfDate. If they don't contain compiled code, the read |
| 387 | // barrier state doesn't matter. |
| 388 | const bool is_cc = file.GetOatHeader().IsConcurrentCopying(); |
| 389 | constexpr bool kRuntimeIsCC = kUseReadBarrier; |
| 390 | if (is_cc != kRuntimeIsCC) { |
| 391 | return kOatCannotOpen; |
| 392 | } |
| 393 | |
| 394 | // Verify the dex checksum. |
| 395 | std::string error_msg; |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 396 | VdexFile* vdex = file.GetVdexFile(); |
| 397 | if (!DexChecksumUpToDate(*vdex, &error_msg)) { |
| 398 | LOG(ERROR) << error_msg; |
| 399 | return kOatDexOutOfDate; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 400 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 401 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 402 | CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter(); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 403 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 404 | // Verify the image checksum |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 405 | if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) { |
| 406 | const ImageInfo* image_info = GetImageInfo(); |
| 407 | if (image_info == nullptr) { |
| 408 | VLOG(oat) << "No image for oat image checksum to match against."; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 409 | |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 410 | if (HasOriginalDexFiles()) { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 411 | return kOatBootImageOutOfDate; |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // If there is no original dex file to fall back to, grudgingly accept |
| 415 | // the oat file. This could technically lead to crashes, but there's no |
| 416 | // way we could find a better oat file to use for this dex location, |
| 417 | // and it's better than being stuck in a boot loop with no way out. |
| 418 | // The problem will hopefully resolve itself the next time the runtime |
| 419 | // starts up. |
| 420 | LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. " |
| 421 | << "Allow oat file use. This is potentially dangerous."; |
Andreas Gampe | fb83d76 | 2018-12-13 23:30:25 +0000 | [diff] [blame] | 422 | } else if (file.GetOatHeader().GetBootImageChecksum() != image_info->boot_image_checksum) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 423 | VLOG(oat) << "Oat image checksum does not match image checksum."; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 424 | return kOatBootImageOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 425 | } |
| 426 | } else { |
| 427 | VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 430 | // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums. |
| 431 | DCHECK(required_dex_checksums_attempted_); |
| 432 | if (only_load_system_executable_ && |
| 433 | !LocationIsOnSystem(file.GetLocation().c_str()) && |
| 434 | file.ContainsDexCode() && |
| 435 | zip_file_only_contains_uncompressed_dex_) { |
| 436 | LOG(ERROR) << "Not loading " |
| 437 | << dex_location_ |
| 438 | << ": oat file has dex code, but APK has uncompressed dex code"; |
| 439 | return kOatDexOutOfDate; |
| 440 | } |
| 441 | |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 442 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 445 | static bool DexLocationToOdexNames(const std::string& location, |
| 446 | InstructionSet isa, |
| 447 | std::string* odex_filename, |
| 448 | std::string* oat_dir, |
| 449 | std::string* isa_dir, |
| 450 | std::string* error_msg) { |
| 451 | CHECK(odex_filename != nullptr); |
| 452 | CHECK(error_msg != nullptr); |
| 453 | |
| 454 | // The odex file name is formed by replacing the dex_location extension with |
| 455 | // .odex and inserting an oat/<isa> directory. For example: |
| 456 | // location = /foo/bar/baz.jar |
| 457 | // odex_location = /foo/bar/oat/<isa>/baz.odex |
| 458 | |
| 459 | // Find the directory portion of the dex location and add the oat/<isa> |
| 460 | // directory. |
| 461 | size_t pos = location.rfind('/'); |
| 462 | if (pos == std::string::npos) { |
| 463 | *error_msg = "Dex location " + location + " has no directory."; |
| 464 | return false; |
| 465 | } |
| 466 | std::string dir = location.substr(0, pos+1); |
| 467 | // Add the oat directory. |
| 468 | dir += "oat"; |
| 469 | if (oat_dir != nullptr) { |
| 470 | *oat_dir = dir; |
| 471 | } |
| 472 | // Add the isa directory |
| 473 | dir += "/" + std::string(GetInstructionSetString(isa)); |
| 474 | if (isa_dir != nullptr) { |
| 475 | *isa_dir = dir; |
| 476 | } |
| 477 | |
| 478 | // Get the base part of the file without the extension. |
| 479 | std::string file = location.substr(pos+1); |
| 480 | pos = file.rfind('.'); |
| 481 | if (pos == std::string::npos) { |
| 482 | *error_msg = "Dex location " + location + " has no extension."; |
| 483 | return false; |
| 484 | } |
| 485 | std::string base = file.substr(0, pos); |
| 486 | |
| 487 | *odex_filename = dir + "/" + base + ".odex"; |
| 488 | return true; |
| 489 | } |
| 490 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 491 | bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location, |
| 492 | InstructionSet isa, |
| 493 | std::string* odex_filename, |
| 494 | std::string* error_msg) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 495 | return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 498 | bool OatFileAssistant::DexLocationToOatFilename(const std::string& location, |
| 499 | InstructionSet isa, |
| 500 | std::string* oat_filename, |
| 501 | std::string* error_msg) { |
| 502 | CHECK(oat_filename != nullptr); |
| 503 | CHECK(error_msg != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 504 | |
Mathieu Chartier | 9e423af | 2018-05-14 10:08:29 -0700 | [diff] [blame] | 505 | // If ANDROID_DATA is not set, return false instead of aborting. |
| 506 | // This can occur for preopt when using a class loader context. |
| 507 | if (GetAndroidDataSafe(error_msg) == nullptr) { |
| 508 | *error_msg = "GetAndroidDataSafe failed: " + *error_msg; |
| 509 | return false; |
| 510 | } |
| 511 | |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 512 | std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa)); |
| 513 | if (cache_dir.empty()) { |
| 514 | *error_msg = "Dalvik cache directory does not exist"; |
| 515 | return false; |
| 516 | } |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 517 | |
| 518 | // TODO: The oat file assistant should be the definitive place for |
| 519 | // determining the oat file name from the dex location, not |
| 520 | // GetDalvikCacheFilename. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 521 | 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] | 522 | } |
| 523 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 524 | const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() { |
| 525 | if (!required_dex_checksums_attempted_) { |
| 526 | required_dex_checksums_attempted_ = true; |
| 527 | required_dex_checksums_found_ = false; |
| 528 | cached_required_dex_checksums_.clear(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 529 | std::string error_msg; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 530 | const ArtDexFileLoader dex_file_loader; |
| 531 | if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(), |
| 532 | &cached_required_dex_checksums_, |
| 533 | &error_msg, |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 534 | zip_fd_, |
| 535 | &zip_file_only_contains_uncompressed_dex_)) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 536 | required_dex_checksums_found_ = true; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 537 | has_original_dex_files_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 538 | } else { |
| 539 | // This can happen if the original dex file has been stripped from the |
| 540 | // apk. |
| 541 | VLOG(oat) << "OatFileAssistant: " << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 542 | has_original_dex_files_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 543 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 544 | // Get the checksums from the odex if we can. |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 545 | const OatFile* odex_file = odex_.GetFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 546 | if (odex_file != nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 547 | required_dex_checksums_found_ = true; |
| 548 | for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 549 | std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str()); |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 550 | const OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 551 | if (odex_dex_file == nullptr) { |
| 552 | required_dex_checksums_found_ = false; |
| 553 | break; |
| 554 | } |
| 555 | cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 560 | return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 563 | std::unique_ptr<OatFileAssistant::ImageInfo> |
| 564 | OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) { |
| 565 | CHECK(error_msg != nullptr); |
| 566 | |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 567 | Runtime* runtime = Runtime::Current(); |
Richard Uhler | 8f10486 | 2017-02-22 12:34:01 +0000 | [diff] [blame] | 568 | std::unique_ptr<ImageInfo> info(new ImageInfo()); |
| 569 | info->location = runtime->GetImageLocation(); |
Andreas Gampe | fb83d76 | 2018-12-13 23:30:25 +0000 | [diff] [blame] | 570 | |
| 571 | std::unique_ptr<ImageHeader> image_header( |
| 572 | gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg)); |
| 573 | if (image_header == nullptr) { |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 574 | return nullptr; |
| 575 | } |
Andreas Gampe | fb83d76 | 2018-12-13 23:30:25 +0000 | [diff] [blame] | 576 | |
| 577 | info->boot_image_checksum = image_header->GetImageChecksum(); |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 578 | return info; |
| 579 | } |
| 580 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 581 | const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() { |
| 582 | if (!image_info_load_attempted_) { |
| 583 | image_info_load_attempted_ = true; |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 584 | std::string error_msg; |
| 585 | cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg); |
| 586 | if (cached_image_info_ == nullptr) { |
| 587 | LOG(WARNING) << "Unable to get runtime image info: " << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 588 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 589 | } |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 590 | return cached_image_info_.get(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 593 | OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 594 | // TODO(calin): Document the side effects of class loading when |
| 595 | // running dalvikvm command line. |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 596 | if (dex_parent_writable_ || UseFdToReadFiles()) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 597 | // If the parent of the dex file is writable it means that we can |
| 598 | // create the odex file. In this case we unconditionally pick the odex |
| 599 | // as the best oat file. This corresponds to the regular use case when |
| 600 | // apps gets installed or when they load private, secondary dex file. |
| 601 | // For apps on the system partition the odex location will not be |
| 602 | // writable and thus the oat location might be more up to date. |
| 603 | return odex_; |
| 604 | } |
| 605 | |
| 606 | // We cannot write to the odex location. This must be a system app. |
| 607 | |
| 608 | // If the oat location is usable take it. |
| 609 | if (oat_.IsUseable()) { |
| 610 | return oat_; |
| 611 | } |
| 612 | |
| 613 | // The oat file is not usable but the odex file might be up to date. |
| 614 | // This is an indication that we are dealing with an up to date prebuilt |
| 615 | // (that doesn't need relocation). |
| 616 | if (odex_.Status() == kOatUpToDate) { |
| 617 | return odex_; |
| 618 | } |
| 619 | |
| 620 | // The oat file is not usable and the odex file is not up to date. |
| 621 | // However we have access to the original dex file which means we can make |
| 622 | // the oat location up to date. |
| 623 | if (HasOriginalDexFiles()) { |
| 624 | return oat_; |
| 625 | } |
| 626 | |
| 627 | // We got into the worst situation here: |
| 628 | // - the oat location is not usable |
| 629 | // - the prebuild odex location is not up to date |
| 630 | // - and we don't have the original dex file anymore (stripped). |
| 631 | // Pick the odex if it exists, or the oat if not. |
| 632 | return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 635 | std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 636 | DCHECK(oat_file != nullptr); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 637 | std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art"); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 638 | if (art_file.empty()) { |
| 639 | return nullptr; |
| 640 | } |
| 641 | std::string error_msg; |
| 642 | ScopedObjectAccess soa(Thread::Current()); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 643 | std::unique_ptr<gc::space::ImageSpace> ret = |
| 644 | gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg); |
Mathieu Chartier | e778fc7 | 2016-01-25 20:11:28 -0800 | [diff] [blame] | 645 | 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] | 646 | LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg; |
| 647 | } |
| 648 | return ret; |
| 649 | } |
| 650 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 651 | OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant, |
| 652 | bool is_oat_location) |
| 653 | : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location) |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 654 | {} |
| 655 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 656 | bool OatFileAssistant::OatFileInfo::IsOatLocation() { |
| 657 | return is_oat_location_; |
| 658 | } |
| 659 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 660 | const std::string* OatFileAssistant::OatFileInfo::Filename() { |
| 661 | return filename_provided_ ? &filename_ : nullptr; |
| 662 | } |
| 663 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 664 | bool OatFileAssistant::OatFileInfo::IsUseable() { |
| 665 | switch (Status()) { |
| 666 | case kOatCannotOpen: |
| 667 | case kOatDexOutOfDate: |
| 668 | case kOatBootImageOutOfDate: return false; |
| 669 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 670 | case kOatUpToDate: return true; |
| 671 | } |
| 672 | UNREACHABLE(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() { |
| 676 | if (!status_attempted_) { |
| 677 | status_attempted_ = true; |
| 678 | const OatFile* file = GetFile(); |
| 679 | if (file == nullptr) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 680 | // Check to see if there is a vdex file we can make use of. |
| 681 | std::string error_msg; |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 682 | std::string vdex_filename = GetVdexFilename(filename_); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 683 | std::unique_ptr<VdexFile> vdex; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 684 | if (use_fd_) { |
| 685 | if (vdex_fd_ >= 0) { |
| 686 | struct stat s; |
| 687 | int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s)); |
| 688 | if (rc == -1) { |
| 689 | error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno)); |
| 690 | } else { |
| 691 | vdex = VdexFile::Open(vdex_fd_, |
| 692 | s.st_size, |
| 693 | vdex_filename, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 694 | /*writable=*/ false, |
| 695 | /*low_4gb=*/ false, |
| 696 | /*unquicken=*/ false, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 697 | &error_msg); |
| 698 | } |
| 699 | } |
| 700 | } else { |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 701 | vdex = VdexFile::Open(vdex_filename, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 702 | /*writable=*/ false, |
| 703 | /*low_4gb=*/ false, |
| 704 | /*unquicken=*/ false, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 705 | &error_msg); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 706 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 707 | if (vdex == nullptr) { |
| 708 | status_ = kOatCannotOpen; |
| 709 | VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg; |
| 710 | } else { |
| 711 | if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) { |
| 712 | // The vdex file does not contain enough information to determine |
| 713 | // whether it is up to date with respect to the boot image, so we |
| 714 | // assume it is out of date. |
| 715 | VLOG(oat) << error_msg; |
| 716 | status_ = kOatBootImageOutOfDate; |
| 717 | } else { |
| 718 | status_ = kOatDexOutOfDate; |
| 719 | } |
| 720 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 721 | } else { |
| 722 | status_ = oat_file_assistant_->GivenOatFileStatus(*file); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 723 | VLOG(oat) << file->GetLocation() << " is " << status_ |
| 724 | << " with filter " << file->GetCompilerFilter(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | return status_; |
| 728 | } |
| 729 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 730 | OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded( |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 731 | CompilerFilter::Filter target, |
| 732 | bool profile_changed, |
| 733 | bool downgrade, |
| 734 | ClassLoaderContext* context) { |
| 735 | |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 736 | bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 737 | bool class_loader_context_okay = ClassLoaderContextIsOkay(context); |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 738 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 739 | // Only check the filter and relocation if the class loader context is ok. |
| 740 | // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone. |
| 741 | if (class_loader_context_okay) { |
| 742 | if (filter_okay && Status() == kOatUpToDate) { |
| 743 | // The oat file is in good shape as is. |
| 744 | return kNoDexOptNeeded; |
| 745 | } |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 746 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 747 | if (IsUseable()) { |
| 748 | return kDex2OatForFilter; |
| 749 | } |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 750 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 751 | if (Status() == kOatBootImageOutOfDate) { |
| 752 | return kDex2OatForBootImage; |
| 753 | } |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if (oat_file_assistant_->HasOriginalDexFiles()) { |
| 757 | return kDex2OatFromScratch; |
| 758 | } else { |
| 759 | // Otherwise there is nothing we can do, even if we want to. |
| 760 | return kNoDexOptNeeded; |
| 761 | } |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 764 | const OatFile* OatFileAssistant::OatFileInfo::GetFile() { |
| 765 | CHECK(!file_released_) << "GetFile called after oat file released."; |
| 766 | if (!load_attempted_) { |
| 767 | load_attempted_ = true; |
| 768 | if (filename_provided_) { |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 769 | bool executable = oat_file_assistant_->load_executable_; |
| 770 | if (executable && oat_file_assistant_->only_load_system_executable_) { |
| 771 | executable = LocationIsOnSystem(filename_.c_str()); |
| 772 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 773 | std::string error_msg; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 774 | if (use_fd_) { |
| 775 | if (oat_fd_ >= 0 && vdex_fd_ >= 0) { |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 776 | file_.reset(OatFile::Open(zip_fd_, |
| 777 | vdex_fd_, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 778 | oat_fd_, |
| 779 | filename_.c_str(), |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 780 | executable, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 781 | /*low_4gb=*/ false, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 782 | oat_file_assistant_->dex_location_.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 783 | /*reservation=*/ nullptr, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 784 | &error_msg)); |
| 785 | } |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 786 | } else { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 787 | file_.reset(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 788 | filename_.c_str(), |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 789 | filename_.c_str(), |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 790 | executable, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 791 | /*low_4gb=*/ false, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 792 | oat_file_assistant_->dex_location_.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 793 | /*reservation=*/ nullptr, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 794 | &error_msg)); |
| 795 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 796 | if (file_.get() == nullptr) { |
| 797 | VLOG(oat) << "OatFileAssistant test for existing oat file " |
| 798 | << filename_ << ": " << error_msg; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | return file_.get(); |
| 803 | } |
| 804 | |
| 805 | bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay( |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 806 | CompilerFilter::Filter target, bool profile_changed, bool downgrade) { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 807 | const OatFile* file = GetFile(); |
| 808 | if (file == nullptr) { |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | CompilerFilter::Filter current = file->GetCompilerFilter(); |
| 813 | if (profile_changed && CompilerFilter::DependsOnProfile(current)) { |
| 814 | VLOG(oat) << "Compiler filter not okay because Profile changed"; |
| 815 | return false; |
| 816 | } |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 817 | return downgrade ? !CompilerFilter::IsBetter(current, target) : |
| 818 | CompilerFilter::IsAsGoodAs(current, target); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 821 | bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) { |
| 822 | if (context == nullptr) { |
| 823 | VLOG(oat) << "ClassLoaderContext check ignored: null context"; |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | const OatFile* file = GetFile(); |
| 828 | if (file == nullptr) { |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 829 | // No oat file means we have nothing to verify. |
| 830 | return true; |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 833 | size_t dir_index = oat_file_assistant_->dex_location_.rfind('/'); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 834 | std::string classpath_dir = (dir_index != std::string::npos) |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 835 | ? oat_file_assistant_->dex_location_.substr(0, dir_index) |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 836 | : ""; |
| 837 | |
| 838 | if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) { |
| 839 | VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened"; |
| 840 | return false; |
| 841 | } |
| 842 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 843 | |
| 844 | const bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()) != |
| 845 | ClassLoaderContext::VerificationResult::kMismatch; |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 846 | if (!result) { |
| 847 | VLOG(oat) << "ClassLoaderContext check failed. Context was " |
| 848 | << file->GetClassLoaderContext() |
| 849 | << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir); |
| 850 | } |
| 851 | return result; |
| 852 | } |
| 853 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 854 | bool OatFileAssistant::OatFileInfo::IsExecutable() { |
| 855 | const OatFile* file = GetFile(); |
| 856 | return (file != nullptr && file->IsExecutable()); |
| 857 | } |
| 858 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 859 | void OatFileAssistant::OatFileInfo::Reset() { |
| 860 | load_attempted_ = false; |
| 861 | file_.reset(); |
| 862 | status_attempted_ = false; |
| 863 | } |
| 864 | |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 865 | void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, |
| 866 | bool use_fd, |
| 867 | int zip_fd, |
| 868 | int vdex_fd, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 869 | int oat_fd) { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 870 | filename_provided_ = true; |
| 871 | filename_ = filename; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 872 | use_fd_ = use_fd; |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 873 | zip_fd_ = zip_fd; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 874 | vdex_fd_ = vdex_fd; |
| 875 | oat_fd_ = oat_fd; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 876 | Reset(); |
| 877 | } |
| 878 | |
| 879 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() { |
| 880 | file_released_ = true; |
| 881 | return std::move(file_); |
| 882 | } |
| 883 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 884 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() { |
Richard Uhler | 3e580bc | 2016-11-08 16:23:07 +0000 | [diff] [blame] | 885 | if (Status() == kOatUpToDate) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 886 | return ReleaseFile(); |
| 887 | } |
| 888 | |
| 889 | VLOG(oat) << "Oat File Assistant: No relocated oat file found," |
| 890 | << " attempting to fall back to interpreting oat file instead."; |
| 891 | |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 892 | switch (Status()) { |
| 893 | case kOatBootImageOutOfDate: |
Andreas Gampe | 8f81de5 | 2018-03-06 08:39:21 -0800 | [diff] [blame] | 894 | // OutOfDate may be either a mismatched image, or a missing image. |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 895 | if (oat_file_assistant_->HasOriginalDexFiles()) { |
Andreas Gampe | 8f81de5 | 2018-03-06 08:39:21 -0800 | [diff] [blame] | 896 | // If there are original dex files, it is better to use them (to avoid a potential |
| 897 | // quickening mismatch because the boot image changed). |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 898 | break; |
| 899 | } |
Andreas Gampe | 8f81de5 | 2018-03-06 08:39:21 -0800 | [diff] [blame] | 900 | // If we do not accept the oat file, we may not have access to dex bytecode at all. Grudgingly |
| 901 | // go forward. |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 902 | FALLTHROUGH_INTENDED; |
| 903 | |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 904 | case kOatUpToDate: |
| 905 | case kOatCannotOpen: |
| 906 | case kOatDexOutOfDate: |
| 907 | break; |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 908 | } |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 909 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 910 | return std::unique_ptr<OatFile>(); |
| 911 | } |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 912 | |
| 913 | // TODO(calin): we could provide a more refined status here |
| 914 | // (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to |
| 915 | // track more experiments but adds extra complexity. |
| 916 | void OatFileAssistant::GetOptimizationStatus( |
| 917 | const std::string& filename, |
| 918 | InstructionSet isa, |
| 919 | std::string* out_compilation_filter, |
| 920 | std::string* out_compilation_reason) { |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 921 | // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load |
| 922 | // non-executable and check the status manually. |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 923 | OatFileAssistant oat_file_assistant(filename.c_str(), isa, /*load_executable=*/ false); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 924 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 925 | |
| 926 | if (oat_file == nullptr) { |
| 927 | *out_compilation_filter = "run-from-apk"; |
| 928 | *out_compilation_reason = "unknown"; |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 929 | return; |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 930 | } |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 931 | |
| 932 | OatStatus status = oat_file_assistant.GivenOatFileStatus(*oat_file); |
| 933 | const char* reason = oat_file->GetCompilationReason(); |
| 934 | *out_compilation_reason = reason == nullptr ? "unknown" : reason; |
| 935 | switch (status) { |
| 936 | case OatStatus::kOatUpToDate: |
| 937 | *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter()); |
| 938 | return; |
| 939 | |
| 940 | case kOatCannotOpen: // This should never happen, but be robust. |
| 941 | *out_compilation_filter = "error"; |
| 942 | *out_compilation_reason = "error"; |
| 943 | return; |
| 944 | |
| 945 | // kOatBootImageOutOfDate - The oat file is up to date with respect to the |
| 946 | // dex file, but is out of date with respect to the boot image. |
| 947 | case kOatBootImageOutOfDate: |
| 948 | FALLTHROUGH_INTENDED; |
| 949 | case kOatDexOutOfDate: |
| 950 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 951 | *out_compilation_filter = "run-from-apk-fallback"; |
| 952 | } else { |
| 953 | *out_compilation_filter = "run-from-vdex-fallback"; |
| 954 | } |
| 955 | return; |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 956 | } |
| 957 | LOG(FATAL) << "Unreachable"; |
| 958 | UNREACHABLE(); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 961 | } // namespace art |