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 | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 19 | #include <sys/stat.h> |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 20 | |
| 21 | #include <memory> |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 22 | #include <optional> |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 23 | #include <sstream> |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 24 | #include <vector> |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 25 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 26 | #include "android-base/file.h" |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 27 | #include "android-base/logging.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 28 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 29 | #include "android-base/strings.h" |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 30 | #include "arch/instruction_set.h" |
| 31 | #include "base/array_ref.h" |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 32 | #include "base/compiler_filter.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 33 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 34 | #include "base/logging.h" // For VLOG. |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 35 | #include "base/macros.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 36 | #include "base/os.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 37 | #include "base/stl_util.h" |
Vladimir Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 38 | #include "base/string_view_cpp20.h" |
Mathieu Chartier | 4a17f8a | 2019-05-17 11:03:26 -0700 | [diff] [blame] | 39 | #include "base/systrace.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 40 | #include "base/utils.h" |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 41 | #include "base/zip_archive.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 42 | #include "class_linker.h" |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 43 | #include "class_loader_context.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 44 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 45 | #include "dex/dex_file_loader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 46 | #include "exec_utils.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 47 | #include "gc/heap.h" |
| 48 | #include "gc/space/image_space.h" |
| 49 | #include "image.h" |
| 50 | #include "oat.h" |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 51 | #include "oat_file_assistant_context.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 52 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 53 | #include "scoped_thread_state_change-inl.h" |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 54 | #include "vdex_file.h" |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 55 | #include "zlib.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 56 | |
| 57 | namespace art { |
| 58 | |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 59 | using ::android::base::ConsumePrefix; |
| 60 | using ::android::base::StringPrintf; |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 61 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 62 | static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@"; |
| 63 | static constexpr const char* kVdexExtension = ".vdex"; |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 64 | static constexpr const char* kDmExtension = ".dm"; |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 65 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 66 | std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) { |
| 67 | switch (status) { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 68 | case OatFileAssistant::kOatCannotOpen: |
| 69 | stream << "kOatCannotOpen"; |
| 70 | break; |
| 71 | case OatFileAssistant::kOatDexOutOfDate: |
| 72 | stream << "kOatDexOutOfDate"; |
| 73 | break; |
| 74 | case OatFileAssistant::kOatBootImageOutOfDate: |
| 75 | stream << "kOatBootImageOutOfDate"; |
| 76 | break; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 77 | case OatFileAssistant::kOatUpToDate: |
| 78 | stream << "kOatUpToDate"; |
| 79 | break; |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 80 | case OatFileAssistant::kOatContextOutOfDate: |
| 81 | stream << "kOaContextOutOfDate"; |
| 82 | break; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return stream; |
| 86 | } |
| 87 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 88 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 89 | const InstructionSet isa, |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 90 | ClassLoaderContext* context, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 91 | bool load_executable, |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 92 | bool only_load_trusted_executable, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 93 | OatFileAssistantContext* ofa_context) |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 94 | : OatFileAssistant(dex_location, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 95 | isa, |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 96 | context, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 97 | load_executable, |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 98 | only_load_trusted_executable, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 99 | ofa_context, |
| 100 | /*vdex_fd=*/-1, |
| 101 | /*oat_fd=*/-1, |
| 102 | /*zip_fd=*/-1) {} |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 103 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 104 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 105 | const InstructionSet isa, |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 106 | ClassLoaderContext* context, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 107 | bool load_executable, |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 108 | bool only_load_trusted_executable, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 109 | OatFileAssistantContext* ofa_context, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 110 | int vdex_fd, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 111 | int oat_fd, |
| 112 | int zip_fd) |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 113 | : context_(context), |
| 114 | isa_(isa), |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 115 | load_executable_(load_executable), |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 116 | only_load_trusted_executable_(only_load_trusted_executable), |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 117 | odex_(this, /*is_oat_location=*/false), |
| 118 | oat_(this, /*is_oat_location=*/true), |
| 119 | vdex_for_odex_(this, /*is_oat_location=*/false), |
| 120 | vdex_for_oat_(this, /*is_oat_location=*/true), |
| 121 | dm_for_odex_(this, /*is_oat_location=*/false), |
| 122 | dm_for_oat_(this, /*is_oat_location=*/true), |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 123 | zip_fd_(zip_fd) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 124 | CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location"; |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 125 | CHECK_IMPLIES(load_executable, context != nullptr) << "Loading executable without a context"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 126 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 127 | if (zip_fd < 0) { |
| 128 | CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd |
| 129 | << " oat_fd=" << oat_fd; |
| 130 | CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd |
| 131 | << " vdex_fd=" << vdex_fd;; |
Nicolas Geoffray | ee1c961 | 2021-03-02 13:43:20 +0000 | [diff] [blame] | 132 | CHECK(!UseFdToReadFiles()); |
| 133 | } else { |
| 134 | CHECK(UseFdToReadFiles()); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Calin Juravle | 099f8d6 | 2017-09-05 19:04:04 -0700 | [diff] [blame] | 137 | dex_location_.assign(dex_location); |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 138 | |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 139 | Runtime* runtime = Runtime::Current(); |
| 140 | |
| 141 | if (load_executable_ && runtime == nullptr) { |
| 142 | LOG(WARNING) << "OatFileAssistant: Load executable specified, " |
| 143 | << "but no active runtime is found. Will not attempt to load executable."; |
| 144 | load_executable_ = false; |
| 145 | } |
| 146 | |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 147 | if (load_executable_ && isa != kRuntimeISA) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 148 | LOG(WARNING) << "OatFileAssistant: Load executable specified, " |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 149 | << "but isa is not kRuntimeISA. Will not attempt to load executable."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 150 | load_executable_ = false; |
| 151 | } |
| 152 | |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 153 | if (ofa_context == nullptr) { |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 154 | CHECK(runtime != nullptr) << "runtime_options is not provided, and no active runtime is found."; |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 155 | ofa_context_ = std::make_unique<OatFileAssistantContext>(runtime); |
| 156 | } else { |
| 157 | ofa_context_ = ofa_context; |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (runtime == nullptr) { |
| 161 | // We need `MemMap` for mapping files. We don't have to initialize it when there is a runtime |
| 162 | // because the runtime initializes it. |
| 163 | MemMap::Init(); |
| 164 | } |
| 165 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 166 | // Get the odex filename. |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 167 | std::string error_msg; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 168 | std::string odex_file_name; |
| 169 | if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) { |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 170 | odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd); |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 171 | std::string vdex_file_name = GetVdexFilename(odex_file_name); |
Nicolas Geoffray | ee1c961 | 2021-03-02 13:43:20 +0000 | [diff] [blame] | 172 | // We dup FDs as the odex_ will claim ownership. |
| 173 | vdex_for_odex_.Reset(vdex_file_name, |
| 174 | UseFdToReadFiles(), |
| 175 | DupCloexec(zip_fd), |
| 176 | DupCloexec(vdex_fd), |
| 177 | DupCloexec(oat_fd)); |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 178 | |
| 179 | std::string dm_file_name = GetDmFilename(dex_location_); |
| 180 | dm_for_odex_.Reset(dm_file_name, |
| 181 | UseFdToReadFiles(), |
| 182 | DupCloexec(zip_fd), |
| 183 | DupCloexec(vdex_fd), |
| 184 | DupCloexec(oat_fd)); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 185 | } else { |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 186 | LOG(WARNING) << "Failed to determine odex file name: " << error_msg; |
| 187 | } |
| 188 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 189 | if (!UseFdToReadFiles()) { |
| 190 | // Get the oat filename. |
| 191 | std::string oat_file_name; |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 192 | if (DexLocationToOatFilename(dex_location_, |
| 193 | isa_, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 194 | GetRuntimeOptions().deny_art_apex_data_files, |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 195 | &oat_file_name, |
| 196 | &error_msg)) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 197 | oat_.Reset(oat_file_name, /*use_fd=*/ false); |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 198 | std::string vdex_file_name = GetVdexFilename(oat_file_name); |
| 199 | vdex_for_oat_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd); |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 200 | std::string dm_file_name = GetDmFilename(dex_location); |
| 201 | dm_for_oat_.Reset(dm_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 202 | } else { |
| 203 | LOG(WARNING) << "Failed to determine oat file name for dex location " |
| 204 | << dex_location_ << ": " << error_msg; |
| 205 | } |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Check if the dex directory is writable. |
| 209 | // This will be needed in most uses of OatFileAssistant and so it's OK to |
| 210 | // compute it eagerly. (the only use which will not make use of it is |
| 211 | // OatFileAssistant::GetStatusDump()) |
| 212 | size_t pos = dex_location_.rfind('/'); |
| 213 | if (pos == std::string::npos) { |
| 214 | LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 215 | } else if (!UseFdToReadFiles()) { |
| 216 | // We cannot test for parent access when using file descriptors. That's ok |
| 217 | // because in this case we will always pick the odex file anyway. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 218 | std::string parent = dex_location_.substr(0, pos); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 219 | if (access(parent.c_str(), W_OK) == 0) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 220 | dex_parent_writable_ = true; |
| 221 | } else { |
| 222 | VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno); |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 223 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 224 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 227 | std::unique_ptr<OatFileAssistant> OatFileAssistant::Create( |
| 228 | const std::string& filename, |
| 229 | const std::string& isa_str, |
Jiakai Zhang | b459341 | 2022-10-28 11:50:26 +0100 | [diff] [blame] | 230 | const std::optional<std::string>& context_str, |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 231 | bool load_executable, |
| 232 | bool only_load_trusted_executable, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 233 | OatFileAssistantContext* ofa_context, |
Jiakai Zhang | 865006d | 2022-07-01 21:09:10 +0100 | [diff] [blame] | 234 | /*out*/ std::unique_ptr<ClassLoaderContext>* context, |
| 235 | /*out*/ std::string* error_msg) { |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 236 | InstructionSet isa = GetInstructionSetFromString(isa_str.c_str()); |
| 237 | if (isa == InstructionSet::kNone) { |
| 238 | *error_msg = StringPrintf("Instruction set '%s' is invalid", isa_str.c_str()); |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
Jiakai Zhang | b459341 | 2022-10-28 11:50:26 +0100 | [diff] [blame] | 242 | std::unique_ptr<ClassLoaderContext> tmp_context = nullptr; |
| 243 | if (context_str.has_value()) { |
| 244 | tmp_context = ClassLoaderContext::Create(context_str->c_str()); |
| 245 | if (tmp_context == nullptr) { |
| 246 | *error_msg = StringPrintf("Class loader context '%s' is invalid", context_str->c_str()); |
| 247 | return nullptr; |
| 248 | } |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 249 | |
Jiakai Zhang | b459341 | 2022-10-28 11:50:26 +0100 | [diff] [blame] | 250 | if (!tmp_context->OpenDexFiles(android::base::Dirname(filename.c_str()), |
| 251 | /*context_fds=*/{}, |
| 252 | /*only_read_checksums=*/true)) { |
| 253 | *error_msg = |
| 254 | StringPrintf("Failed to load class loader context files for '%s' with context '%s'", |
| 255 | filename.c_str(), |
| 256 | context_str->c_str()); |
| 257 | return nullptr; |
| 258 | } |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | auto assistant = std::make_unique<OatFileAssistant>(filename.c_str(), |
| 262 | isa, |
Jiakai Zhang | 865006d | 2022-07-01 21:09:10 +0100 | [diff] [blame] | 263 | tmp_context.get(), |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 264 | load_executable, |
| 265 | only_load_trusted_executable, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 266 | ofa_context); |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 267 | |
Jiakai Zhang | 865006d | 2022-07-01 21:09:10 +0100 | [diff] [blame] | 268 | *context = std::move(tmp_context); |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 269 | return assistant; |
| 270 | } |
| 271 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 272 | bool OatFileAssistant::UseFdToReadFiles() { |
| 273 | return zip_fd_ >= 0; |
| 274 | } |
| 275 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 276 | bool OatFileAssistant::IsInBootClassPath() { |
| 277 | // Note: We check the current boot class path, regardless of the ISA |
| 278 | // specified by the user. This is okay, because the boot class path should |
| 279 | // be the same for all ISAs. |
| 280 | // TODO: Can we verify the boot class path is the same for all ISAs? |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 281 | for (const std::string& boot_class_path_location : |
| 282 | GetRuntimeOptions().boot_class_path_locations) { |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 283 | if (boot_class_path_location == dex_location_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 284 | VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path"; |
| 285 | return true; |
| 286 | } |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 291 | OatFileAssistant::DexOptTrigger OatFileAssistant::GetDexOptTrigger( |
Jiakai Zhang | b9617f1 | 2022-06-29 16:27:27 +0100 | [diff] [blame] | 292 | CompilerFilter::Filter target_compiler_filter, bool profile_changed, bool downgrade) { |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 293 | if (downgrade) { |
| 294 | // The caller's intention is to downgrade the compiler filter. We should only re-compile if the |
| 295 | // target compiler filter is worse than the current one. |
Jiakai Zhang | b9617f1 | 2022-06-29 16:27:27 +0100 | [diff] [blame] | 296 | return DexOptTrigger{.targetFilterIsWorse = true}; |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // This is the usual case. The caller's intention is to see if a better oat file can be generated. |
Jiakai Zhang | b9617f1 | 2022-06-29 16:27:27 +0100 | [diff] [blame] | 300 | DexOptTrigger dexopt_trigger{.targetFilterIsBetter = true, .primaryBootImageBecomesUsable = true}; |
| 301 | if (profile_changed && CompilerFilter::DependsOnProfile(target_compiler_filter)) { |
| 302 | // Since the profile has been changed, we should re-compile even if the compilation does not |
| 303 | // make the compiler filter better. |
| 304 | dexopt_trigger.targetFilterIsSame = true; |
| 305 | } |
| 306 | return dexopt_trigger; |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter, |
Calin Juravle | 0a5cad3 | 2020-02-14 20:29:26 +0000 | [diff] [blame] | 310 | bool profile_changed, |
| 311 | bool downgrade) { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 312 | OatFileInfo& info = GetBestInfo(); |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 313 | DexOptNeeded dexopt_needed = info.GetDexOptNeeded( |
| 314 | target_compiler_filter, GetDexOptTrigger(target_compiler_filter, profile_changed, downgrade)); |
Jiakai Zhang | b9617f1 | 2022-06-29 16:27:27 +0100 | [diff] [blame] | 315 | if (dexopt_needed != kNoDexOptNeeded && (&info == &dm_for_oat_ || &info == &dm_for_odex_)) { |
| 316 | // The usable vdex file is in the DM file. This information cannot be encoded in the integer. |
| 317 | // Return kDex2OatFromScratch so that neither the vdex in the "oat" location nor the vdex in the |
| 318 | // "odex" location will be picked by installd. |
| 319 | return kDex2OatFromScratch; |
| 320 | } |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 321 | if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) { |
| 322 | return dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 323 | } |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 324 | return -dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 327 | bool OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter, |
| 328 | DexOptTrigger dexopt_trigger, |
| 329 | /*out*/ DexOptStatus* dexopt_status) { |
| 330 | OatFileInfo& info = GetBestInfo(); |
| 331 | DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target_compiler_filter, dexopt_trigger); |
| 332 | if (info.IsUseable()) { |
| 333 | if (&info == &dm_for_oat_ || &info == &dm_for_odex_) { |
| 334 | dexopt_status->location_ = kLocationDm; |
| 335 | } else if (info.IsOatLocation()) { |
| 336 | dexopt_status->location_ = kLocationOat; |
| 337 | } else { |
| 338 | dexopt_status->location_ = kLocationOdex; |
| 339 | } |
| 340 | } else { |
| 341 | dexopt_status->location_ = kLocationNoneOrError; |
| 342 | } |
| 343 | return dexopt_needed != kNoDexOptNeeded; |
| 344 | } |
| 345 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 346 | bool OatFileAssistant::IsUpToDate() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 347 | return GetBestInfo().Status() == kOatUpToDate; |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 350 | std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 351 | return GetBestInfo().ReleaseFileForUse(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 354 | std::string OatFileAssistant::GetStatusDump() { |
| 355 | std::ostringstream status; |
| 356 | bool oat_file_exists = false; |
| 357 | bool odex_file_exists = false; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 358 | if (oat_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 359 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 360 | CHECK(oat_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 361 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 362 | oat_file_exists = true; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 363 | status << *oat_.Filename() << "[status=" << oat_.Status() << ", "; |
| 364 | const OatFile* file = oat_.GetFile(); |
| 365 | if (file == nullptr) { |
| 366 | // If the file is null even though the status is not kOatCannotOpen, it |
| 367 | // means we must have a vdex file with no corresponding oat file. In |
| 368 | // this case we cannot determine the compilation filter. Indicate that |
| 369 | // we have only the vdex file instead. |
| 370 | status << "vdex-only"; |
| 371 | } else { |
| 372 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 373 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 376 | if (odex_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 377 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 378 | CHECK(odex_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 379 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 380 | odex_file_exists = true; |
| 381 | if (oat_file_exists) { |
| 382 | status << "] "; |
| 383 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 384 | status << *odex_.Filename() << "[status=" << odex_.Status() << ", "; |
| 385 | const OatFile* file = odex_.GetFile(); |
| 386 | if (file == nullptr) { |
| 387 | status << "vdex-only"; |
| 388 | } else { |
| 389 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 390 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if (!oat_file_exists && !odex_file_exists) { |
| 394 | status << "invalid["; |
| 395 | } |
| 396 | |
| 397 | status << "]"; |
| 398 | return status.str(); |
| 399 | } |
| 400 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 401 | std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 402 | const OatFile &oat_file, const char *dex_location) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 403 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 404 | if (LoadDexFiles(oat_file, dex_location, &dex_files)) { |
| 405 | return dex_files; |
| 406 | } else { |
| 407 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 408 | } |
| 409 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 410 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 411 | bool OatFileAssistant::LoadDexFiles( |
| 412 | const OatFile &oat_file, |
| 413 | const std::string& dex_location, |
| 414 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 415 | // Load the main dex file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 416 | std::string error_msg; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 417 | const OatDexFile* oat_dex_file = oat_file.GetOatDexFile( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 418 | dex_location.c_str(), nullptr, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 419 | if (oat_dex_file == nullptr) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 420 | LOG(WARNING) << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 421 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 424 | 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] | 425 | if (dex_file.get() == nullptr) { |
| 426 | 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] | 427 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 428 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 429 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 430 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 431 | // Load the rest of the multidex entries |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 432 | for (size_t i = 1;; i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 433 | std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 434 | oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 435 | if (oat_dex_file == nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 436 | // There are no more multidex entries to load. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 440 | dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 441 | if (dex_file.get() == nullptr) { |
| 442 | 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] | 443 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 444 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 445 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 446 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 447 | return true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 450 | std::optional<bool> OatFileAssistant::HasDexFiles(std::string* error_msg) { |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 451 | ScopedTrace trace("HasDexFiles"); |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 452 | const std::vector<std::uint32_t>* checksums = GetRequiredDexChecksums(error_msg); |
| 453 | if (checksums == nullptr) { |
| 454 | return std::nullopt; |
| 455 | } |
| 456 | return !checksums->empty(); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 459 | OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 460 | return odex_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 463 | OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 464 | return oat_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Nicolas Geoffray | 6d2b6ba | 2022-12-05 11:41:43 +0000 | [diff] [blame] | 467 | bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) { |
| 468 | if (!file.ContainsDexCode()) { |
| 469 | // We've already checked during oat file creation that the dex files loaded |
| 470 | // from external files have the same checksums as the ones in the vdex file. |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 471 | return true; |
| 472 | } |
Nicolas Geoffray | 6d2b6ba | 2022-12-05 11:41:43 +0000 | [diff] [blame] | 473 | ScopedTrace trace("DexChecksumUpToDate"); |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 474 | const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(error_msg); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 475 | if (required_dex_checksums == nullptr) { |
Jiakai Zhang | 003d291 | 2022-11-24 17:27:07 +0000 | [diff] [blame] | 476 | return false; |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 477 | } |
| 478 | if (required_dex_checksums->empty()) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 479 | LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date."; |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount(); |
| 484 | if (required_dex_checksums->size() != number_of_dex_files) { |
| 485 | *error_msg = StringPrintf("expected %zu dex files but found %u", |
| 486 | required_dex_checksums->size(), |
| 487 | number_of_dex_files); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 488 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 491 | for (uint32_t i = 0; i < number_of_dex_files; i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 492 | std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 493 | uint32_t expected_checksum = (*required_dex_checksums)[i]; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 494 | const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 495 | if (oat_dex_file == nullptr) { |
| 496 | *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str()); |
| 497 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 498 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 499 | uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 500 | if (expected_checksum != actual_checksum) { |
| 501 | VLOG(oat) << "Dex checksum does not match for dex: " << dex |
| 502 | << ". Expected: " << expected_checksum |
| 503 | << ", Actual: " << actual_checksum; |
| 504 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 507 | return true; |
| 508 | } |
| 509 | |
| 510 | OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) { |
| 511 | // Verify the ART_USE_READ_BARRIER state. |
| 512 | // TODO: Don't fully reject files due to read barrier state. If they contain |
| 513 | // compiled code and are otherwise okay, we should return something like |
| 514 | // kOatRelocationOutOfDate. If they don't contain compiled code, the read |
| 515 | // barrier state doesn't matter. |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 516 | if (file.GetOatHeader().IsConcurrentCopying() != gUseReadBarrier) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 517 | return kOatCannotOpen; |
| 518 | } |
| 519 | |
| 520 | // Verify the dex checksum. |
| 521 | std::string error_msg; |
Nicolas Geoffray | 6d2b6ba | 2022-12-05 11:41:43 +0000 | [diff] [blame] | 522 | if (!DexChecksumUpToDate(file, &error_msg)) { |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 523 | LOG(ERROR) << error_msg; |
| 524 | return kOatDexOutOfDate; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 525 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 526 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 527 | CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter(); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 528 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 529 | // Verify the image checksum |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 530 | if (file.IsBackedByVdexOnly()) { |
| 531 | VLOG(oat) << "Image checksum test skipped for vdex file " << file.GetLocation(); |
| 532 | } else if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) { |
Vladimir Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 533 | if (!ValidateBootClassPathChecksums(file)) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 534 | VLOG(oat) << "Oat image checksum does not match image checksum."; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 535 | return kOatBootImageOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 536 | } |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 537 | if (!gc::space::ImageSpace::ValidateApexVersions( |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 538 | file, GetOatFileAssistantContext()->GetApexVersions(), &error_msg)) { |
Jiakai Zhang | 57a45be | 2022-02-07 16:12:57 +0000 | [diff] [blame] | 539 | VLOG(oat) << error_msg; |
Nicolas Geoffray | d3c45c2 | 2021-04-27 13:21:28 +0100 | [diff] [blame] | 540 | return kOatBootImageOutOfDate; |
| 541 | } |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 542 | } else { |
| 543 | VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 546 | if (only_load_trusted_executable_ && |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 547 | !LocationIsTrusted(file.GetLocation(), |
| 548 | !GetRuntimeOptions().deny_art_apex_data_files) && |
Nicolas Geoffray | 6d2b6ba | 2022-12-05 11:41:43 +0000 | [diff] [blame] | 549 | file.ContainsDexCode()) { |
| 550 | // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums. |
| 551 | GetRequiredDexChecksums(&error_msg); |
| 552 | // The constraint is only enforced if the zip has uncompressed dex code. |
| 553 | if (zip_file_only_contains_uncompressed_dex_) { |
| 554 | LOG(ERROR) << "Not loading " |
| 555 | << dex_location_ |
| 556 | << ": oat file has dex code, but APK has uncompressed dex code"; |
| 557 | return kOatDexOutOfDate; |
| 558 | } |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 561 | if (!ClassLoaderContextIsOkay(file)) { |
| 562 | return kOatContextOutOfDate; |
| 563 | } |
| 564 | |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 565 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 566 | } |
| 567 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 568 | bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers, |
| 569 | InstructionSet isa, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 570 | /* out */ std::string* dex_location, |
| 571 | /* out */ std::string* vdex_filename) { |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 572 | // Normally, OatFileAssistant should not assume that there is an active runtime. However, we |
| 573 | // reference the runtime here. This is okay because we are in a static function that is unrelated |
| 574 | // to other parts of OatFileAssistant. |
| 575 | DCHECK(Runtime::Current() != nullptr); |
| 576 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 577 | uint32_t checksum = adler32(0L, Z_NULL, 0); |
| 578 | for (const DexFile::Header* header : headers) { |
| 579 | checksum = adler32_combine(checksum, |
| 580 | header->checksum_, |
| 581 | header->file_size_ - DexFile::kNumNonChecksumBytes); |
| 582 | } |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 583 | |
| 584 | const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory(); |
| 585 | if (data_dir.empty() || Runtime::Current()->IsZygote()) { |
| 586 | *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum); |
| 587 | return false; |
| 588 | } |
| 589 | *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum); |
| 590 | |
| 591 | std::string odex_filename; |
| 592 | std::string error_msg; |
| 593 | if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) { |
| 594 | LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg; |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | *vdex_filename = GetVdexFilename(odex_filename); |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) { |
| 603 | DCHECK(basename.find('/') == std::string::npos); |
| 604 | // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension> |
| 605 | if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 || |
| 606 | !android::base::StartsWith(basename.c_str(), kAnonymousDexPrefix) || |
| 607 | !android::base::EndsWith(basename, kVdexExtension)) { |
| 608 | return false; |
| 609 | } |
| 610 | // Check that all characters between the prefix and extension are decimal digits. |
| 611 | for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) { |
| 612 | if (!std::isdigit(basename[i])) { |
| 613 | return false; |
| 614 | } |
| 615 | } |
| 616 | return true; |
| 617 | } |
| 618 | |
Orion Hodson | b6f8857 | 2021-02-10 13:59:18 +0000 | [diff] [blame] | 619 | bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location, |
| 620 | InstructionSet isa, |
| 621 | std::string* odex_filename, |
| 622 | std::string* error_msg) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 623 | CHECK(odex_filename != nullptr); |
| 624 | CHECK(error_msg != nullptr); |
| 625 | |
Jiakai Zhang | 7fa7086 | 2021-09-15 08:56:56 +0000 | [diff] [blame] | 626 | // For a DEX file on /apex, check if there is an odex file on /system. If so, and the file exists, |
| 627 | // use it. |
| 628 | if (LocationIsOnApex(location)) { |
| 629 | const std::string system_file = GetSystemOdexFilenameForApex(location, isa); |
| 630 | if (OS::FileExists(system_file.c_str(), /*check_file_type=*/true)) { |
| 631 | *odex_filename = system_file; |
| 632 | return true; |
| 633 | } else if (errno != ENOENT) { |
| 634 | PLOG(ERROR) << "Could not check odex file " << system_file; |
| 635 | } |
| 636 | } |
| 637 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 638 | // The odex file name is formed by replacing the dex_location extension with |
| 639 | // .odex and inserting an oat/<isa> directory. For example: |
| 640 | // location = /foo/bar/baz.jar |
| 641 | // odex_location = /foo/bar/oat/<isa>/baz.odex |
| 642 | |
| 643 | // Find the directory portion of the dex location and add the oat/<isa> |
| 644 | // directory. |
| 645 | size_t pos = location.rfind('/'); |
| 646 | if (pos == std::string::npos) { |
| 647 | *error_msg = "Dex location " + location + " has no directory."; |
| 648 | return false; |
| 649 | } |
| 650 | std::string dir = location.substr(0, pos+1); |
| 651 | // Add the oat directory. |
| 652 | dir += "oat"; |
Orion Hodson | b6f8857 | 2021-02-10 13:59:18 +0000 | [diff] [blame] | 653 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 654 | // Add the isa directory |
| 655 | dir += "/" + std::string(GetInstructionSetString(isa)); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 656 | |
| 657 | // Get the base part of the file without the extension. |
| 658 | std::string file = location.substr(pos+1); |
| 659 | pos = file.rfind('.'); |
| 660 | if (pos == std::string::npos) { |
| 661 | *error_msg = "Dex location " + location + " has no extension."; |
| 662 | return false; |
| 663 | } |
| 664 | std::string base = file.substr(0, pos); |
| 665 | |
| 666 | *odex_filename = dir + "/" + base + ".odex"; |
| 667 | return true; |
| 668 | } |
| 669 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 670 | bool OatFileAssistant::DexLocationToOatFilename(const std::string& location, |
| 671 | InstructionSet isa, |
| 672 | std::string* oat_filename, |
| 673 | std::string* error_msg) { |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 674 | DCHECK(Runtime::Current() != nullptr); |
| 675 | return DexLocationToOatFilename( |
| 676 | location, isa, Runtime::Current()->DenyArtApexDataFiles(), oat_filename, error_msg); |
| 677 | } |
| 678 | |
| 679 | bool OatFileAssistant::DexLocationToOatFilename(const std::string& location, |
| 680 | InstructionSet isa, |
| 681 | bool deny_art_apex_data_files, |
| 682 | std::string* oat_filename, |
| 683 | std::string* error_msg) { |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 684 | CHECK(oat_filename != nullptr); |
| 685 | CHECK(error_msg != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 686 | |
Orion Hodson | b6f8857 | 2021-02-10 13:59:18 +0000 | [diff] [blame] | 687 | // Check if `location` could have an oat file in the ART APEX data directory. If so, and the |
| 688 | // file exists, use it. |
Orion Hodson | e5276da | 2021-02-27 18:04:41 +0000 | [diff] [blame] | 689 | const std::string apex_data_file = GetApexDataOdexFilename(location, isa); |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 690 | if (!apex_data_file.empty() && !deny_art_apex_data_files) { |
Orion Hodson | e5276da | 2021-02-27 18:04:41 +0000 | [diff] [blame] | 691 | if (OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) { |
| 692 | *oat_filename = apex_data_file; |
| 693 | return true; |
| 694 | } else if (errno != ENOENT) { |
| 695 | PLOG(ERROR) << "Could not check odex file " << apex_data_file; |
| 696 | } |
Orion Hodson | b6f8857 | 2021-02-10 13:59:18 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Mathieu Chartier | 9e423af | 2018-05-14 10:08:29 -0700 | [diff] [blame] | 699 | // If ANDROID_DATA is not set, return false instead of aborting. |
| 700 | // This can occur for preopt when using a class loader context. |
Roland Levillain | 2e3cb54 | 2019-04-05 18:00:04 +0100 | [diff] [blame] | 701 | if (GetAndroidDataSafe(error_msg).empty()) { |
Mathieu Chartier | 9e423af | 2018-05-14 10:08:29 -0700 | [diff] [blame] | 702 | *error_msg = "GetAndroidDataSafe failed: " + *error_msg; |
| 703 | return false; |
| 704 | } |
| 705 | |
Orion Hodson | fa81f71 | 2021-01-13 12:27:21 +0000 | [diff] [blame] | 706 | std::string dalvik_cache; |
| 707 | bool have_android_data = false; |
| 708 | bool dalvik_cache_exists = false; |
| 709 | bool is_global_cache = false; |
| 710 | GetDalvikCache(GetInstructionSetString(isa), |
| 711 | /*create_if_absent=*/ true, |
| 712 | &dalvik_cache, |
| 713 | &have_android_data, |
| 714 | &dalvik_cache_exists, |
| 715 | &is_global_cache); |
| 716 | if (!dalvik_cache_exists) { |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 717 | *error_msg = "Dalvik cache directory does not exist"; |
| 718 | return false; |
| 719 | } |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 720 | |
| 721 | // TODO: The oat file assistant should be the definitive place for |
| 722 | // determining the oat file name from the dex location, not |
| 723 | // GetDalvikCacheFilename. |
Orion Hodson | fa81f71 | 2021-01-13 12:27:21 +0000 | [diff] [blame] | 724 | 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] | 725 | } |
| 726 | |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 727 | const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums(std::string* error_msg) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 728 | if (!required_dex_checksums_attempted_) { |
| 729 | required_dex_checksums_attempted_ = true; |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 730 | std::vector<uint32_t> checksums; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 731 | const ArtDexFileLoader dex_file_loader; |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 732 | std::vector<std::string> dex_locations_ignored; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 733 | if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(), |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 734 | &checksums, |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 735 | &dex_locations_ignored, |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 736 | &cached_required_dex_checksums_error_, |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 737 | zip_fd_, |
| 738 | &zip_file_only_contains_uncompressed_dex_)) { |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 739 | if (checksums.empty()) { |
| 740 | // The only valid case here is for APKs without dex files. |
| 741 | VLOG(oat) << "No dex file found in " << dex_location_; |
| 742 | } |
| 743 | |
| 744 | cached_required_dex_checksums_ = std::move(checksums); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 745 | } |
| 746 | } |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 747 | |
| 748 | if (cached_required_dex_checksums_.has_value()) { |
Jiakai Zhang | fc7236d | 2022-12-01 14:45:57 +0000 | [diff] [blame] | 749 | return &cached_required_dex_checksums_.value(); |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 750 | } else { |
| 751 | *error_msg = cached_required_dex_checksums_error_; |
| 752 | DCHECK(!error_msg->empty()); |
| 753 | return nullptr; |
| 754 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 757 | bool OatFileAssistant::ValidateBootClassPathChecksums(OatFileAssistantContext* ofa_context, |
| 758 | InstructionSet isa, |
| 759 | std::string_view oat_checksums, |
| 760 | std::string_view oat_boot_class_path, |
| 761 | /*out*/ std::string* error_msg) { |
| 762 | const std::vector<std::string>& bcp_locations = |
| 763 | ofa_context->GetRuntimeOptions().boot_class_path_locations; |
| 764 | |
| 765 | if (oat_checksums.empty() || oat_boot_class_path.empty()) { |
| 766 | *error_msg = oat_checksums.empty() ? "Empty checksums" : "Empty boot class path"; |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | size_t oat_bcp_size = gc::space::ImageSpace::CheckAndCountBCPComponents( |
| 771 | oat_boot_class_path, ArrayRef<const std::string>(bcp_locations), error_msg); |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 772 | if (oat_bcp_size == static_cast<size_t>(-1)) { |
| 773 | DCHECK(!error_msg->empty()); |
| 774 | return false; |
| 775 | } |
Theodore Dubois | b7a86cb | 2022-10-25 17:42:56 -0700 | [diff] [blame] | 776 | DCHECK_LE(oat_bcp_size, bcp_locations.size()); |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 777 | |
| 778 | size_t bcp_index = 0; |
| 779 | size_t boot_image_index = 0; |
| 780 | bool found_d = false; |
| 781 | |
| 782 | while (bcp_index < oat_bcp_size) { |
| 783 | static_assert(gc::space::ImageSpace::kImageChecksumPrefix == 'i', "Format prefix check"); |
| 784 | static_assert(gc::space::ImageSpace::kDexFileChecksumPrefix == 'd', "Format prefix check"); |
| 785 | if (StartsWith(oat_checksums, "i") && !found_d) { |
| 786 | const std::vector<OatFileAssistantContext::BootImageInfo>& boot_image_info_list = |
| 787 | ofa_context->GetBootImageInfoList(isa); |
| 788 | if (boot_image_index >= boot_image_info_list.size()) { |
| 789 | *error_msg = StringPrintf("Missing boot image for %s, remaining checksums: %s", |
| 790 | bcp_locations[bcp_index].c_str(), |
| 791 | std::string(oat_checksums).c_str()); |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | const OatFileAssistantContext::BootImageInfo& boot_image_info = |
| 796 | boot_image_info_list[boot_image_index]; |
| 797 | if (!ConsumePrefix(&oat_checksums, boot_image_info.checksum)) { |
| 798 | *error_msg = StringPrintf("Image checksum mismatch, expected %s to start with %s", |
| 799 | std::string(oat_checksums).c_str(), |
| 800 | boot_image_info.checksum.c_str()); |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | bcp_index += boot_image_info.component_count; |
| 805 | boot_image_index++; |
| 806 | } else if (StartsWith(oat_checksums, "d")) { |
| 807 | found_d = true; |
| 808 | const std::vector<std::string>* bcp_checksums = |
| 809 | ofa_context->GetBcpChecksums(bcp_index, error_msg); |
| 810 | if (bcp_checksums == nullptr) { |
| 811 | return false; |
| 812 | } |
| 813 | oat_checksums.remove_prefix(1u); |
| 814 | for (const std::string& checksum : *bcp_checksums) { |
| 815 | if (!ConsumePrefix(&oat_checksums, checksum)) { |
| 816 | *error_msg = StringPrintf( |
| 817 | "Dex checksum mismatch for bootclasspath file %s, expected %s to start with %s", |
| 818 | bcp_locations[bcp_index].c_str(), |
| 819 | std::string(oat_checksums).c_str(), |
| 820 | checksum.c_str()); |
| 821 | return false; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | bcp_index++; |
| 826 | } else { |
| 827 | *error_msg = StringPrintf("Unexpected checksums, expected %s to start with %s", |
| 828 | std::string(oat_checksums).c_str(), |
| 829 | found_d ? "'d'" : "'i' or 'd'"); |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | if (bcp_index < oat_bcp_size) { |
| 834 | if (!ConsumePrefix(&oat_checksums, ":")) { |
| 835 | if (oat_checksums.empty()) { |
| 836 | *error_msg = |
| 837 | StringPrintf("Checksum too short, missing %zu components", oat_bcp_size - bcp_index); |
| 838 | } else { |
| 839 | *error_msg = StringPrintf("Missing ':' separator at start of %s", |
| 840 | std::string(oat_checksums).c_str()); |
| 841 | } |
| 842 | return false; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | if (!oat_checksums.empty()) { |
| 848 | *error_msg = |
| 849 | StringPrintf("Checksum too long, unexpected tail: %s", std::string(oat_checksums).c_str()); |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | return true; |
| 854 | } |
| 855 | |
Vladimir Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 856 | bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) { |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 857 | // Get the checksums and the BCP from the oat file. |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 858 | const char* oat_boot_class_path_checksums = |
| 859 | oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey); |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 860 | const char* oat_boot_class_path = |
| 861 | oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey); |
| 862 | if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) { |
Vladimir Marko | f3d88a8 | 2018-12-21 16:38:47 +0000 | [diff] [blame] | 863 | return false; |
| 864 | } |
Vladimir Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 865 | |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 866 | std::string error_msg; |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 867 | bool result = ValidateBootClassPathChecksums(GetOatFileAssistantContext(), |
| 868 | isa_, |
| 869 | oat_boot_class_path_checksums, |
| 870 | oat_boot_class_path, |
| 871 | &error_msg); |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 872 | if (!result) { |
| 873 | VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation() |
| 874 | << " error: " << error_msg; |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 875 | return false; |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 876 | } |
| 877 | |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 878 | return true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 881 | bool OatFileAssistant::IsPrimaryBootImageUsable() { |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 882 | return !GetOatFileAssistantContext()->GetBootImageInfoList(isa_).empty(); |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 885 | OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() { |
Mathieu Chartier | 4a17f8a | 2019-05-17 11:03:26 -0700 | [diff] [blame] | 886 | ScopedTrace trace("GetBestInfo"); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 887 | // TODO(calin): Document the side effects of class loading when |
| 888 | // running dalvikvm command line. |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 889 | if (dex_parent_writable_ || UseFdToReadFiles()) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 890 | // If the parent of the dex file is writable it means that we can |
| 891 | // create the odex file. In this case we unconditionally pick the odex |
| 892 | // as the best oat file. This corresponds to the regular use case when |
| 893 | // apps gets installed or when they load private, secondary dex file. |
| 894 | // For apps on the system partition the odex location will not be |
| 895 | // writable and thus the oat location might be more up to date. |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 896 | |
| 897 | // If the odex is not useable, and we have a useable vdex, return the vdex |
| 898 | // instead. |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 899 | if (!odex_.IsUseable()) { |
| 900 | if (vdex_for_odex_.IsUseable()) { |
| 901 | return vdex_for_odex_; |
| 902 | } else if (dm_for_odex_.IsUseable()) { |
| 903 | return dm_for_odex_; |
| 904 | } |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 905 | } |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 906 | return odex_; |
| 907 | } |
| 908 | |
| 909 | // We cannot write to the odex location. This must be a system app. |
| 910 | |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 911 | // If the oat location is useable take it. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 912 | if (oat_.IsUseable()) { |
| 913 | return oat_; |
| 914 | } |
| 915 | |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 916 | // 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] | 917 | // This is an indication that we are dealing with an up to date prebuilt |
| 918 | // (that doesn't need relocation). |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 919 | if (odex_.IsUseable()) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 920 | return odex_; |
| 921 | } |
| 922 | |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 923 | // Look for a useable vdex file. |
| 924 | if (vdex_for_oat_.IsUseable()) { |
| 925 | return vdex_for_oat_; |
| 926 | } |
| 927 | if (vdex_for_odex_.IsUseable()) { |
| 928 | return vdex_for_odex_; |
| 929 | } |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 930 | if (dm_for_oat_.IsUseable()) { |
| 931 | return dm_for_oat_; |
| 932 | } |
| 933 | if (dm_for_odex_.IsUseable()) { |
| 934 | return dm_for_odex_; |
| 935 | } |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 936 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 937 | // We got into the worst situation here: |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 938 | // - the oat location is not useable |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 939 | // - the prebuild odex location is not up to date |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 940 | // - the vdex-only file is not useable |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 941 | // - and we don't have the original dex file anymore (stripped). |
| 942 | // Pick the odex if it exists, or the oat if not. |
| 943 | return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 946 | std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 947 | DCHECK(oat_file != nullptr); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 948 | std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art"); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 949 | if (art_file.empty()) { |
| 950 | return nullptr; |
| 951 | } |
| 952 | std::string error_msg; |
| 953 | ScopedObjectAccess soa(Thread::Current()); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 954 | std::unique_ptr<gc::space::ImageSpace> ret = |
| 955 | gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg); |
Mathieu Chartier | e778fc7 | 2016-01-25 20:11:28 -0800 | [diff] [blame] | 956 | 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] | 957 | LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg; |
| 958 | } |
| 959 | return ret; |
| 960 | } |
| 961 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 962 | OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant, |
| 963 | bool is_oat_location) |
| 964 | : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location) |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 965 | {} |
| 966 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 967 | bool OatFileAssistant::OatFileInfo::IsOatLocation() { |
| 968 | return is_oat_location_; |
| 969 | } |
| 970 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 971 | const std::string* OatFileAssistant::OatFileInfo::Filename() { |
| 972 | return filename_provided_ ? &filename_ : nullptr; |
| 973 | } |
| 974 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 975 | bool OatFileAssistant::OatFileInfo::IsUseable() { |
Mathieu Chartier | 4a17f8a | 2019-05-17 11:03:26 -0700 | [diff] [blame] | 976 | ScopedTrace trace("IsUseable"); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 977 | switch (Status()) { |
| 978 | case kOatCannotOpen: |
| 979 | case kOatDexOutOfDate: |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 980 | case kOatContextOutOfDate: |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 981 | case kOatBootImageOutOfDate: return false; |
| 982 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 983 | case kOatUpToDate: return true; |
| 984 | } |
| 985 | UNREACHABLE(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() { |
Mathieu Chartier | 4a17f8a | 2019-05-17 11:03:26 -0700 | [diff] [blame] | 989 | ScopedTrace trace("Status"); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 990 | if (!status_attempted_) { |
| 991 | status_attempted_ = true; |
| 992 | const OatFile* file = GetFile(); |
| 993 | if (file == nullptr) { |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 994 | status_ = kOatCannotOpen; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 995 | } else { |
| 996 | status_ = oat_file_assistant_->GivenOatFileStatus(*file); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 997 | VLOG(oat) << file->GetLocation() << " is " << status_ |
| 998 | << " with filter " << file->GetCompilerFilter(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | return status_; |
| 1002 | } |
| 1003 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1004 | OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded( |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1005 | CompilerFilter::Filter target_compiler_filter, const DexOptTrigger dexopt_trigger) { |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1006 | if (IsUseable()) { |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1007 | return ShouldRecompileForFilter(target_compiler_filter, dexopt_trigger) ? kDex2OatForFilter : |
| 1008 | kNoDexOptNeeded; |
| 1009 | } |
| 1010 | |
| 1011 | // In this case, the oat file is not usable. If the caller doesn't seek for a better compiler |
| 1012 | // filter (e.g., the caller wants to downgrade), then we should not recompile. |
| 1013 | if (!dexopt_trigger.targetFilterIsBetter) { |
| 1014 | return kNoDexOptNeeded; |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1015 | } |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1016 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1017 | if (Status() == kOatBootImageOutOfDate) { |
| 1018 | return kDex2OatForBootImage; |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 1021 | std::string error_msg; |
| 1022 | std::optional<bool> has_dex_files = oat_file_assistant_->HasDexFiles(&error_msg); |
| 1023 | if (has_dex_files.has_value()) { |
| 1024 | if (*has_dex_files) { |
| 1025 | return kDex2OatFromScratch; |
| 1026 | } else { |
| 1027 | // No dex file, so there is nothing we need to do. |
| 1028 | return kNoDexOptNeeded; |
| 1029 | } |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 1030 | } else { |
Jiakai Zhang | c372e79 | 2022-11-23 10:57:22 +0000 | [diff] [blame] | 1031 | // Unable to open the dex file, so there is nothing we can do. |
| 1032 | LOG(WARNING) << error_msg; |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 1033 | return kNoDexOptNeeded; |
| 1034 | } |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1037 | const OatFile* OatFileAssistant::OatFileInfo::GetFile() { |
| 1038 | CHECK(!file_released_) << "GetFile called after oat file released."; |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1039 | if (load_attempted_) { |
| 1040 | return file_.get(); |
| 1041 | } |
| 1042 | load_attempted_ = true; |
| 1043 | if (!filename_provided_) { |
| 1044 | return nullptr; |
| 1045 | } |
| 1046 | |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 1047 | if (LocationIsOnArtApexData(filename_) && |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 1048 | oat_file_assistant_->GetRuntimeOptions().deny_art_apex_data_files) { |
Orion Hodson | 971068d | 2021-06-30 21:17:53 +0100 | [diff] [blame] | 1049 | LOG(WARNING) << "OatFileAssistant rejected file " << filename_ |
| 1050 | << ": ART apexdata is untrusted."; |
| 1051 | return nullptr; |
| 1052 | } |
| 1053 | |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1054 | std::string error_msg; |
| 1055 | bool executable = oat_file_assistant_->load_executable_; |
| 1056 | if (android::base::EndsWith(filename_, kVdexExtension)) { |
| 1057 | executable = false; |
| 1058 | // Check to see if there is a vdex file we can make use of. |
| 1059 | std::unique_ptr<VdexFile> vdex; |
| 1060 | if (use_fd_) { |
| 1061 | if (vdex_fd_ >= 0) { |
| 1062 | struct stat s; |
| 1063 | int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s)); |
| 1064 | if (rc == -1) { |
| 1065 | error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno)); |
| 1066 | } else { |
| 1067 | vdex = VdexFile::Open(vdex_fd_, |
| 1068 | s.st_size, |
| 1069 | filename_, |
| 1070 | /*writable=*/ false, |
| 1071 | /*low_4gb=*/ false, |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1072 | &error_msg); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 1073 | } |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } else { |
| 1076 | vdex = VdexFile::Open(filename_, |
| 1077 | /*writable=*/ false, |
| 1078 | /*low_4gb=*/ false, |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1079 | &error_msg); |
| 1080 | } |
| 1081 | if (vdex == nullptr) { |
| 1082 | VLOG(oat) << "unable to open vdex file " << filename_ << ": " << error_msg; |
| 1083 | } else { |
| 1084 | file_.reset(OatFile::OpenFromVdex(zip_fd_, |
| 1085 | std::move(vdex), |
| 1086 | oat_file_assistant_->dex_location_, |
Nicolas Geoffray | 0f6af5e | 2023-01-30 14:29:11 +0000 | [diff] [blame] | 1087 | oat_file_assistant_->context_, |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1088 | &error_msg)); |
| 1089 | } |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 1090 | } else if (android::base::EndsWith(filename_, kDmExtension)) { |
| 1091 | executable = false; |
| 1092 | // Check to see if there is a vdex file we can make use of. |
| 1093 | std::unique_ptr<ZipArchive> dm_file(ZipArchive::Open(filename_.c_str(), &error_msg)); |
| 1094 | if (dm_file != nullptr) { |
| 1095 | std::unique_ptr<VdexFile> vdex(VdexFile::OpenFromDm(filename_, *dm_file)); |
| 1096 | if (vdex != nullptr) { |
| 1097 | file_.reset(OatFile::OpenFromVdex(zip_fd_, |
| 1098 | std::move(vdex), |
| 1099 | oat_file_assistant_->dex_location_, |
Nicolas Geoffray | 0f6af5e | 2023-01-30 14:29:11 +0000 | [diff] [blame] | 1100 | oat_file_assistant_->context_, |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 1101 | &error_msg)); |
| 1102 | } |
| 1103 | } |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1104 | } else { |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 1105 | if (executable && oat_file_assistant_->only_load_trusted_executable_) { |
Orion Hodson | 971068d | 2021-06-30 21:17:53 +0100 | [diff] [blame] | 1106 | executable = LocationIsTrusted(filename_, /*trust_art_apex_data_files=*/ true); |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1107 | } |
| 1108 | VLOG(oat) << "Loading " << filename_ << " with executable: " << executable; |
| 1109 | if (use_fd_) { |
| 1110 | if (oat_fd_ >= 0 && vdex_fd_ >= 0) { |
| 1111 | ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_, |
| 1112 | /*size=*/ 1u); |
| 1113 | file_.reset(OatFile::Open(zip_fd_, |
| 1114 | vdex_fd_, |
| 1115 | oat_fd_, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1116 | filename_.c_str(), |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 1117 | executable, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1118 | /*low_4gb=*/ false, |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1119 | dex_locations, |
Victor Hsieh | ce9b902 | 2021-07-21 10:44:06 -0700 | [diff] [blame] | 1120 | /*dex_fds=*/ ArrayRef<const int>(), |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1121 | /*reservation=*/ nullptr, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1122 | &error_msg)); |
| 1123 | } |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1124 | } else { |
| 1125 | file_.reset(OatFile::Open(/*zip_fd=*/ -1, |
| 1126 | filename_.c_str(), |
| 1127 | filename_.c_str(), |
| 1128 | executable, |
| 1129 | /*low_4gb=*/ false, |
| 1130 | oat_file_assistant_->dex_location_, |
| 1131 | &error_msg)); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1134 | if (file_.get() == nullptr) { |
| 1135 | VLOG(oat) << "OatFileAssistant test for existing oat file " |
| 1136 | << filename_ |
| 1137 | << ": " << error_msg; |
| 1138 | } else { |
| 1139 | VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable; |
| 1140 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1141 | return file_.get(); |
| 1142 | } |
| 1143 | |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1144 | bool OatFileAssistant::OatFileInfo::ShouldRecompileForFilter(CompilerFilter::Filter target, |
| 1145 | const DexOptTrigger dexopt_trigger) { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1146 | const OatFile* file = GetFile(); |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1147 | DCHECK(file != nullptr); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1148 | |
| 1149 | CompilerFilter::Filter current = file->GetCompilerFilter(); |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1150 | if (dexopt_trigger.targetFilterIsBetter && CompilerFilter::IsBetter(target, current)) { |
| 1151 | return true; |
| 1152 | } |
| 1153 | if (dexopt_trigger.targetFilterIsSame && current == target) { |
| 1154 | return true; |
| 1155 | } |
| 1156 | if (dexopt_trigger.targetFilterIsWorse && CompilerFilter::IsBetter(current, target)) { |
| 1157 | return true; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1158 | } |
Nicolas Geoffray | e7e026b | 2022-03-24 16:42:07 +0000 | [diff] [blame] | 1159 | |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1160 | if (dexopt_trigger.primaryBootImageBecomesUsable && |
| 1161 | CompilerFilter::DependsOnImageChecksum(current)) { |
Nicolas Geoffray | e7e026b | 2022-03-24 16:42:07 +0000 | [diff] [blame] | 1162 | // If the oat file has been compiled without an image, and the runtime is |
| 1163 | // now running with an image loaded from disk, return that we need to |
| 1164 | // re-compile. The recompilation will generate a better oat file, and with an app |
| 1165 | // image for profile guided compilation. |
| 1166 | const char* oat_boot_class_path_checksums = |
| 1167 | file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey); |
| 1168 | if (oat_boot_class_path_checksums != nullptr && |
| 1169 | !StartsWith(oat_boot_class_path_checksums, "i") && |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 1170 | oat_file_assistant_->IsPrimaryBootImageUsable()) { |
Nicolas Geoffray | e7e026b | 2022-03-24 16:42:07 +0000 | [diff] [blame] | 1171 | DCHECK(!file->GetOatHeader().RequiresImage()); |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1172 | return true; |
Nicolas Geoffray | e7e026b | 2022-03-24 16:42:07 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
Jiakai Zhang | dbfa799 | 2022-06-24 23:14:24 +0100 | [diff] [blame] | 1176 | return false; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1179 | bool OatFileAssistant::ClassLoaderContextIsOkay(const OatFile& oat_file) const { |
Jiakai Zhang | b459341 | 2022-10-28 11:50:26 +0100 | [diff] [blame] | 1180 | if (context_ == nullptr) { |
| 1181 | // The caller requests to skip the check. |
| 1182 | return true; |
| 1183 | } |
| 1184 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1185 | if (oat_file.IsBackedByVdexOnly()) { |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 1186 | // Only a vdex file, we don't depend on the class loader context. |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1190 | if (!CompilerFilter::IsVerificationEnabled(oat_file.GetCompilerFilter())) { |
Calin Juravle | af32242 | 2020-02-11 13:45:53 -0800 | [diff] [blame] | 1191 | // If verification is not enabled we don't need to verify the class loader context and we |
| 1192 | // assume it's ok. |
| 1193 | return true; |
| 1194 | } |
| 1195 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1196 | ClassLoaderContext::VerificationResult matches = context_->VerifyClassLoaderContextMatch( |
| 1197 | oat_file.GetClassLoaderContext(), |
| 1198 | /*verify_names=*/ true, |
| 1199 | /*verify_checksums=*/ true); |
| 1200 | if (matches == ClassLoaderContext::VerificationResult::kMismatch) { |
David Srbecky | 656fdcd | 2021-04-17 16:47:01 +0000 | [diff] [blame] | 1201 | VLOG(oat) << "ClassLoaderContext check failed. Context was " |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1202 | << oat_file.GetClassLoaderContext() |
| 1203 | << ". The expected context is " |
| 1204 | << context_->EncodeContextForOatFile(android::base::Dirname(dex_location_)); |
| 1205 | return false; |
David Srbecky | 656fdcd | 2021-04-17 16:47:01 +0000 | [diff] [blame] | 1206 | } |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1207 | return true; |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1210 | bool OatFileAssistant::OatFileInfo::IsExecutable() { |
| 1211 | const OatFile* file = GetFile(); |
| 1212 | return (file != nullptr && file->IsExecutable()); |
| 1213 | } |
| 1214 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1215 | void OatFileAssistant::OatFileInfo::Reset() { |
| 1216 | load_attempted_ = false; |
| 1217 | file_.reset(); |
| 1218 | status_attempted_ = false; |
| 1219 | } |
| 1220 | |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 1221 | void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, |
| 1222 | bool use_fd, |
| 1223 | int zip_fd, |
| 1224 | int vdex_fd, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1225 | int oat_fd) { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1226 | filename_provided_ = true; |
| 1227 | filename_ = filename; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 1228 | use_fd_ = use_fd; |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 1229 | zip_fd_ = zip_fd; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1230 | vdex_fd_ = vdex_fd; |
| 1231 | oat_fd_ = oat_fd; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1232 | Reset(); |
| 1233 | } |
| 1234 | |
| 1235 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() { |
| 1236 | file_released_ = true; |
| 1237 | return std::move(file_); |
| 1238 | } |
| 1239 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1240 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() { |
Mathieu Chartier | 4a17f8a | 2019-05-17 11:03:26 -0700 | [diff] [blame] | 1241 | ScopedTrace trace("ReleaseFileForUse"); |
Richard Uhler | 3e580bc | 2016-11-08 16:23:07 +0000 | [diff] [blame] | 1242 | if (Status() == kOatUpToDate) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1243 | return ReleaseFile(); |
| 1244 | } |
| 1245 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1246 | return std::unique_ptr<OatFile>(); |
| 1247 | } |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 1248 | |
| 1249 | // TODO(calin): we could provide a more refined status here |
| 1250 | // (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to |
| 1251 | // track more experiments but adds extra complexity. |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 1252 | void OatFileAssistant::GetOptimizationStatus(const std::string& filename, |
| 1253 | InstructionSet isa, |
| 1254 | std::string* out_compilation_filter, |
| 1255 | std::string* out_compilation_reason, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 1256 | OatFileAssistantContext* ofa_context) { |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1257 | // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load |
| 1258 | // non-executable and check the status manually. |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1259 | OatFileAssistant oat_file_assistant(filename.c_str(), |
| 1260 | isa, |
Jiakai Zhang | 78d86b5 | 2022-05-26 17:24:17 +0100 | [diff] [blame] | 1261 | /*context=*/nullptr, |
| 1262 | /*load_executable=*/false, |
| 1263 | /*only_load_trusted_executable=*/false, |
Jiakai Zhang | 7a5a95f | 2022-08-07 01:12:24 +0100 | [diff] [blame] | 1264 | ofa_context); |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 1265 | std::string out_odex_location; // unused |
| 1266 | std::string out_odex_status; // unused |
| 1267 | oat_file_assistant.GetOptimizationStatus( |
| 1268 | &out_odex_location, |
| 1269 | out_compilation_filter, |
| 1270 | out_compilation_reason, |
| 1271 | &out_odex_status); |
| 1272 | } |
| 1273 | |
| 1274 | void OatFileAssistant::GetOptimizationStatus( |
| 1275 | std::string* out_odex_location, |
| 1276 | std::string* out_compilation_filter, |
| 1277 | std::string* out_compilation_reason, |
| 1278 | std::string* out_odex_status) { |
| 1279 | OatFileInfo& oat_file_info = GetBestInfo(); |
| 1280 | const OatFile* oat_file = GetBestInfo().GetFile(); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (oat_file == nullptr) { |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 1283 | *out_odex_location = "error"; |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 1284 | *out_compilation_filter = "run-from-apk"; |
| 1285 | *out_compilation_reason = "unknown"; |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 1286 | // This mostly happens when we cannot open the oat file. |
| 1287 | // Note that it's different than kOatCannotOpen. |
| 1288 | // TODO: The design of getting the BestInfo is not ideal, |
| 1289 | // as it's not very clear what's the difference between |
| 1290 | // a nullptr and kOatcannotOpen. The logic should be revised |
| 1291 | // and improved. |
| 1292 | *out_odex_status = "io-error-no-oat"; |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1293 | return; |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 1294 | } |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1295 | |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 1296 | *out_odex_location = oat_file->GetLocation(); |
| 1297 | OatStatus status = oat_file_info.Status(); |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1298 | const char* reason = oat_file->GetCompilationReason(); |
| 1299 | *out_compilation_reason = reason == nullptr ? "unknown" : reason; |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 1300 | |
| 1301 | // If the oat file is invalid, the vdex file will be picked, so the status is `kOatUpToDate`. If |
| 1302 | // the vdex file is also invalid, then either `oat_file` is nullptr, or `status` is |
| 1303 | // `kOatDexOutOfDate`. |
| 1304 | DCHECK(status == kOatUpToDate || status == kOatDexOutOfDate); |
| 1305 | |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1306 | switch (status) { |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 1307 | case kOatUpToDate: |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1308 | *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter()); |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 1309 | *out_odex_status = "up-to-date"; |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1310 | return; |
| 1311 | |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 1312 | case kOatCannotOpen: |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1313 | case kOatBootImageOutOfDate: |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1314 | case kOatContextOutOfDate: |
Jiakai Zhang | 6c18fbd | 2022-06-24 19:01:45 +0100 | [diff] [blame] | 1315 | // These should never happen, but be robust. |
| 1316 | *out_compilation_filter = "unexpected"; |
| 1317 | *out_compilation_reason = "unexpected"; |
| 1318 | *out_odex_status = "unexpected"; |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 1319 | return; |
| 1320 | |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 1321 | case kOatDexOutOfDate: |
| 1322 | *out_compilation_filter = "run-from-apk-fallback"; |
| 1323 | *out_odex_status = "apk-more-recent"; |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1324 | return; |
Andreas Gampe | bd3e1ce | 2018-03-15 17:45:03 -0700 | [diff] [blame] | 1325 | } |
| 1326 | LOG(FATAL) << "Unreachable"; |
| 1327 | UNREACHABLE(); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1330 | } // namespace art |