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