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