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