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