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