Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "dex_file_loader.h" |
| 18 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 19 | #include <sys/stat.h> |
| 20 | |
David Srbecky | 2bcd3a0 | 2023-01-25 18:57:39 +0000 | [diff] [blame] | 21 | #include <memory> |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 22 | #include <optional> |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 23 | |
David Srbecky | 2bcd3a0 | 2023-01-25 18:57:39 +0000 | [diff] [blame] | 24 | #include "android-base/stringprintf.h" |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
| 26 | #include "base/file_magic.h" |
| 27 | #include "base/mem_map.h" |
| 28 | #include "base/os.h" |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 29 | #include "base/stl_util.h" |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 30 | #include "base/systrace.h" |
| 31 | #include "base/unix_file/fd_file.h" |
| 32 | #include "base/zip_archive.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 33 | #include "compact_dex_file.h" |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 34 | #include "dex_file.h" |
| 35 | #include "dex_file_verifier.h" |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 36 | #include "standard_dex_file.h" |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 37 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 38 | namespace art { |
| 39 | |
Yabin Cui | bdbd707 | 2023-03-03 15:17:36 -0800 | [diff] [blame] | 40 | #if defined(STATIC_LIB) |
| 41 | #define DEXFILE_SCOPED_TRACE(name) |
| 42 | #else |
| 43 | #define DEXFILE_SCOPED_TRACE(name) ScopedTrace trace(name) |
| 44 | #endif |
| 45 | |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 46 | namespace { |
| 47 | |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 48 | // Technically we do not have a limitation with respect to the number of dex files that can be in a |
| 49 | // multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols |
| 50 | // (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what |
| 51 | // seems an excessive number. |
| 52 | static constexpr size_t kWarnOnManyDexFilesThreshold = 100; |
| 53 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 54 | using android::base::StringPrintf; |
| 55 | |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 56 | class VectorContainer : public DexFileContainer { |
| 57 | public: |
| 58 | explicit VectorContainer(std::vector<uint8_t>&& vector) : vector_(std::move(vector)) { } |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 59 | ~VectorContainer() override { } |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 60 | |
David Srbecky | 2bcd3a0 | 2023-01-25 18:57:39 +0000 | [diff] [blame] | 61 | bool IsReadOnly() const override { return true; } |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 62 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 63 | bool EnableWrite() override { return true; } |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 64 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 65 | bool DisableWrite() override { return false; } |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 66 | |
David Srbecky | 2bcd3a0 | 2023-01-25 18:57:39 +0000 | [diff] [blame] | 67 | const uint8_t* Begin() const override { return vector_.data(); } |
| 68 | |
| 69 | const uint8_t* End() const override { return vector_.data() + vector_.size(); } |
| 70 | |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 71 | private: |
| 72 | std::vector<uint8_t> vector_; |
| 73 | DISALLOW_COPY_AND_ASSIGN(VectorContainer); |
| 74 | }; |
| 75 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 76 | class MemMapContainer : public DexFileContainer { |
| 77 | public: |
| 78 | explicit MemMapContainer(MemMap&& mem_map, bool is_file_map = false) |
| 79 | : mem_map_(std::move(mem_map)), is_file_map_(is_file_map) {} |
| 80 | |
| 81 | int GetPermissions() const { |
| 82 | if (!mem_map_.IsValid()) { |
| 83 | return 0; |
| 84 | } else { |
| 85 | return mem_map_.GetProtect(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | bool IsReadOnly() const override { return GetPermissions() == PROT_READ; } |
| 90 | |
| 91 | bool EnableWrite() override { |
| 92 | CHECK(IsReadOnly()); |
| 93 | if (!mem_map_.IsValid()) { |
| 94 | return false; |
| 95 | } else { |
| 96 | return mem_map_.Protect(PROT_READ | PROT_WRITE); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | bool DisableWrite() override { |
| 101 | CHECK(!IsReadOnly()); |
| 102 | if (!mem_map_.IsValid()) { |
| 103 | return false; |
| 104 | } else { |
| 105 | return mem_map_.Protect(PROT_READ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | const uint8_t* Begin() const override { return mem_map_.Begin(); } |
| 110 | |
| 111 | const uint8_t* End() const override { return mem_map_.End(); } |
| 112 | |
| 113 | bool IsFileMap() const override { return is_file_map_; } |
| 114 | |
| 115 | protected: |
| 116 | MemMap mem_map_; |
| 117 | bool is_file_map_; |
| 118 | DISALLOW_COPY_AND_ASSIGN(MemMapContainer); |
| 119 | }; |
| 120 | |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 121 | } // namespace |
| 122 | |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 123 | bool DexFileLoader::IsMagicValid(uint32_t magic) { |
| 124 | return IsMagicValid(reinterpret_cast<uint8_t*>(&magic)); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 127 | bool DexFileLoader::IsMagicValid(const uint8_t* magic) { |
| 128 | return StandardDexFile::IsMagicValid(magic) || |
| 129 | CompactDexFile::IsMagicValid(magic); |
| 130 | } |
| 131 | |
| 132 | bool DexFileLoader::IsVersionAndMagicValid(const uint8_t* magic) { |
| 133 | if (StandardDexFile::IsMagicValid(magic)) { |
| 134 | return StandardDexFile::IsVersionValid(magic); |
| 135 | } |
| 136 | if (CompactDexFile::IsMagicValid(magic)) { |
| 137 | return CompactDexFile::IsVersionValid(magic); |
| 138 | } |
| 139 | return false; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 142 | bool DexFileLoader::IsMultiDexLocation(const char* location) { |
| 143 | return strrchr(location, kMultiDexSeparator) != nullptr; |
| 144 | } |
| 145 | |
| 146 | std::string DexFileLoader::GetMultiDexClassesDexName(size_t index) { |
| 147 | return (index == 0) ? "classes.dex" : StringPrintf("classes%zu.dex", index + 1); |
| 148 | } |
| 149 | |
| 150 | std::string DexFileLoader::GetMultiDexLocation(size_t index, const char* dex_location) { |
| 151 | return (index == 0) |
| 152 | ? dex_location |
| 153 | : StringPrintf("%s%cclasses%zu.dex", dex_location, kMultiDexSeparator, index + 1); |
| 154 | } |
| 155 | |
| 156 | std::string DexFileLoader::GetDexCanonicalLocation(const char* dex_location) { |
| 157 | CHECK_NE(dex_location, static_cast<const char*>(nullptr)); |
| 158 | std::string base_location = GetBaseLocation(dex_location); |
| 159 | const char* suffix = dex_location + base_location.size(); |
| 160 | DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator); |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 161 | #ifdef _WIN32 |
| 162 | // Warning: No symbolic link processing here. |
| 163 | PLOG(WARNING) << "realpath is unsupported on Windows."; |
| 164 | #else |
Nicolas Geoffray | 0d0f316 | 2018-05-10 12:55:40 +0100 | [diff] [blame] | 165 | // Warning: Bionic implementation of realpath() allocates > 12KB on the stack. |
| 166 | // Do not run this code on a small stack, e.g. in signal handler. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 167 | UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr)); |
| 168 | if (path != nullptr && path.get() != base_location) { |
| 169 | return std::string(path.get()) + suffix; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 170 | } |
| 171 | #endif |
| 172 | if (suffix[0] == 0) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 173 | return base_location; |
| 174 | } else { |
| 175 | return dex_location; |
| 176 | } |
| 177 | } |
| 178 | |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 179 | // All of the implementations here should be independent of the runtime. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 180 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 181 | DexFileLoader::DexFileLoader(const uint8_t* base, size_t size, const std::string& location) |
David Srbecky | 0b9b330 | 2023-02-24 19:03:51 +0000 | [diff] [blame] | 182 | : DexFileLoader(std::make_shared<MemoryDexFileContainer>(base, base + size), location) {} |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 183 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 184 | DexFileLoader::DexFileLoader(std::vector<uint8_t>&& memory, const std::string& location) |
David Srbecky | 0b9b330 | 2023-02-24 19:03:51 +0000 | [diff] [blame] | 185 | : DexFileLoader(std::make_shared<VectorContainer>(std::move(memory)), location) {} |
Vladimir Marko | f4d05ff | 2021-04-14 09:39:04 +0000 | [diff] [blame] | 186 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 187 | DexFileLoader::DexFileLoader(MemMap&& mem_map, const std::string& location) |
David Srbecky | 0b9b330 | 2023-02-24 19:03:51 +0000 | [diff] [blame] | 188 | : DexFileLoader(std::make_shared<MemMapContainer>(std::move(mem_map)), location) {} |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 189 | |
| 190 | std::unique_ptr<const DexFile> DexFileLoader::Open(uint32_t location_checksum, |
David Srbecky | 2bcd3a0 | 2023-01-25 18:57:39 +0000 | [diff] [blame] | 191 | const OatDexFile* oat_dex_file, |
| 192 | bool verify, |
| 193 | bool verify_checksum, |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 194 | std::string* error_msg) { |
Yabin Cui | bdbd707 | 2023-03-03 15:17:36 -0800 | [diff] [blame] | 195 | DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_); |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 196 | |
| 197 | uint32_t magic; |
| 198 | if (!InitAndReadMagic(&magic, error_msg) || !MapRootContainer(error_msg)) { |
| 199 | DCHECK(!error_msg->empty()); |
| 200 | return {}; |
| 201 | } |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 202 | DCHECK(root_container_ != nullptr); |
David Srbecky | 0b9b330 | 2023-02-24 19:03:51 +0000 | [diff] [blame] | 203 | std::unique_ptr<const DexFile> dex_file = OpenCommon(root_container_, |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 204 | root_container_->Begin(), |
| 205 | root_container_->Size(), |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 206 | location_, |
| 207 | location_checksum, |
| 208 | oat_dex_file, |
| 209 | verify, |
| 210 | verify_checksum, |
| 211 | error_msg, |
| 212 | nullptr); |
| 213 | return dex_file; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 214 | } |
| 215 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 216 | bool DexFileLoader::InitAndReadMagic(uint32_t* magic, std::string* error_msg) { |
| 217 | if (root_container_ != nullptr) { |
| 218 | if (root_container_->Size() < sizeof(uint32_t)) { |
| 219 | *error_msg = StringPrintf("Unable to open '%s' : Size is too small", location_.c_str()); |
| 220 | return false; |
| 221 | } |
| 222 | *magic = *reinterpret_cast<const uint32_t*>(root_container_->Begin()); |
| 223 | } else { |
| 224 | // Open the file if we have not been given the file-descriptor directly before. |
| 225 | if (!file_.has_value()) { |
| 226 | CHECK(!filename_.empty()); |
| 227 | file_.emplace(filename_, O_RDONLY, /* check_usage= */ false); |
| 228 | if (file_->Fd() == -1) { |
| 229 | *error_msg = StringPrintf("Unable to open '%s' : %s", filename_.c_str(), strerror(errno)); |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 | if (!ReadMagicAndReset(file_->Fd(), magic, error_msg)) { |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | bool DexFileLoader::MapRootContainer(std::string* error_msg) { |
| 241 | if (root_container_ != nullptr) { |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | CHECK(MemMap::IsInitialized()); |
| 246 | CHECK(file_.has_value()); |
| 247 | struct stat sbuf; |
| 248 | memset(&sbuf, 0, sizeof(sbuf)); |
| 249 | if (fstat(file_->Fd(), &sbuf) == -1) { |
| 250 | *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", filename_.c_str(), strerror(errno)); |
| 251 | return false; |
| 252 | } |
| 253 | if (S_ISDIR(sbuf.st_mode)) { |
| 254 | *error_msg = StringPrintf("Attempt to mmap directory '%s'", filename_.c_str()); |
| 255 | return false; |
| 256 | } |
| 257 | MemMap map = MemMap::MapFile(sbuf.st_size, |
| 258 | PROT_READ, |
| 259 | MAP_PRIVATE, |
| 260 | file_->Fd(), |
| 261 | 0, |
| 262 | /*low_4gb=*/false, |
| 263 | filename_.c_str(), |
| 264 | error_msg); |
| 265 | if (!map.IsValid()) { |
| 266 | DCHECK(!error_msg->empty()); |
| 267 | return false; |
| 268 | } |
David Srbecky | 0b9b330 | 2023-02-24 19:03:51 +0000 | [diff] [blame] | 269 | root_container_ = std::make_shared<MemMapContainer>(std::move(map)); |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 270 | return true; |
| 271 | } |
| 272 | |
| 273 | bool DexFileLoader::Open(bool verify, |
| 274 | bool verify_checksum, |
| 275 | bool allow_no_dex_files, |
| 276 | DexFileLoaderErrorCode* error_code, |
| 277 | std::string* error_msg, |
| 278 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Yabin Cui | bdbd707 | 2023-03-03 15:17:36 -0800 | [diff] [blame] | 279 | DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_); |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 280 | |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 281 | DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr"; |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 282 | |
| 283 | uint32_t magic; |
| 284 | if (!InitAndReadMagic(&magic, error_msg)) { |
| 285 | return false; |
| 286 | } |
| 287 | |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 288 | if (IsZipMagic(magic)) { |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 289 | std::unique_ptr<ZipArchive> zip_archive( |
| 290 | file_.has_value() ? |
| 291 | ZipArchive::OpenFromOwnedFd(file_->Fd(), location_.c_str(), error_msg) : |
| 292 | ZipArchive::OpenFromMemory( |
| 293 | root_container_->Begin(), root_container_->Size(), location_.c_str(), error_msg)); |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 294 | if (zip_archive.get() == nullptr) { |
| 295 | DCHECK(!error_msg->empty()); |
| 296 | return false; |
| 297 | } |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 298 | for (size_t i = 0;; ++i) { |
| 299 | std::string name = GetMultiDexClassesDexName(i); |
| 300 | std::string multidex_location = GetMultiDexLocation(i, location_.c_str()); |
| 301 | bool ok = OpenFromZipEntry(*zip_archive, |
| 302 | name.c_str(), |
| 303 | multidex_location, |
| 304 | verify, |
| 305 | verify_checksum, |
| 306 | error_code, |
| 307 | error_msg, |
| 308 | dex_files); |
| 309 | if (!ok) { |
| 310 | // We keep opening consecutive dex entries as long as we can (until entry is not found). |
| 311 | if (*error_code == DexFileLoaderErrorCode::kEntryNotFound) { |
| 312 | // Success if we loaded at least one entry, or if empty zip is explicitly allowed. |
| 313 | return i > 0 || allow_no_dex_files; |
| 314 | } |
| 315 | return false; |
| 316 | } |
| 317 | if (i == kWarnOnManyDexFilesThreshold) { |
| 318 | LOG(WARNING) << location_ << " has in excess of " << kWarnOnManyDexFilesThreshold |
| 319 | << " dex files. Please consider coalescing and shrinking the number to " |
| 320 | " avoid runtime overhead."; |
| 321 | } |
| 322 | } |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 323 | } |
| 324 | if (IsMagicValid(magic)) { |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 325 | if (!MapRootContainer(error_msg)) { |
| 326 | return false; |
| 327 | } |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 328 | DCHECK(root_container_ != nullptr); |
| 329 | std::unique_ptr<const DexFile> dex_file = |
| 330 | OpenCommon(root_container_, |
| 331 | root_container_->Begin(), |
| 332 | root_container_->Size(), |
| 333 | location_, |
| 334 | /*location_checksum*/ {}, // Use default checksum from dex header. |
| 335 | /*oat_dex_file=*/nullptr, |
| 336 | verify, |
| 337 | verify_checksum, |
| 338 | error_msg, |
| 339 | nullptr); |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 340 | if (dex_file.get() != nullptr) { |
| 341 | dex_files->push_back(std::move(dex_file)); |
| 342 | return true; |
| 343 | } else { |
| 344 | return false; |
| 345 | } |
| 346 | } |
| 347 | *error_msg = StringPrintf("Expected valid zip or dex file"); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 348 | return false; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 349 | } |
| 350 | |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 351 | std::unique_ptr<DexFile> DexFileLoader::OpenCommon(std::shared_ptr<DexFileContainer> container, |
| 352 | const uint8_t* base, |
| 353 | size_t size, |
| 354 | const std::string& location, |
| 355 | std::optional<uint32_t> location_checksum, |
| 356 | const OatDexFile* oat_dex_file, |
| 357 | bool verify, |
| 358 | bool verify_checksum, |
| 359 | std::string* error_msg, |
| 360 | DexFileLoaderErrorCode* error_code) { |
| 361 | if (container == nullptr) { |
| 362 | // We should never pass null here, but use reasonable default for app compat anyway. |
| 363 | container = std::make_shared<MemoryDexFileContainer>(base, size); |
| 364 | } |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 365 | if (error_code != nullptr) { |
| 366 | *error_code = DexFileLoaderErrorCode::kDexFileError; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 367 | } |
| 368 | std::unique_ptr<DexFile> dex_file; |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 369 | auto header = reinterpret_cast<const DexFile::Header*>(base); |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 370 | if (size >= sizeof(StandardDexFile::Header) && StandardDexFile::IsMagicValid(base)) { |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 371 | uint32_t checksum = location_checksum.value_or(header->checksum_); |
| 372 | dex_file.reset(new StandardDexFile(base, size, location, checksum, oat_dex_file, container)); |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 373 | } else if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(base)) { |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 374 | uint32_t checksum = location_checksum.value_or(header->checksum_); |
| 375 | dex_file.reset(new CompactDexFile(base, size, location, checksum, oat_dex_file, container)); |
Mathieu Chartier | 700a985 | 2018-02-06 18:27:38 -0800 | [diff] [blame] | 376 | } else { |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 377 | *error_msg = StringPrintf("Invalid or truncated dex file '%s'", location.c_str()); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 378 | } |
| 379 | if (dex_file == nullptr) { |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 380 | *error_msg = |
| 381 | StringPrintf("Failed to open dex file '%s': %s", location.c_str(), error_msg->c_str()); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 382 | return nullptr; |
| 383 | } |
| 384 | if (!dex_file->Init(error_msg)) { |
| 385 | dex_file.reset(); |
| 386 | return nullptr; |
| 387 | } |
David Srbecky | 771b44f | 2023-02-24 18:42:20 +0000 | [diff] [blame] | 388 | // NB: Dex verifier does not understand the compact dex format. |
| 389 | if (verify && !dex_file->IsCompactDexFile()) { |
Yabin Cui | bdbd707 | 2023-03-03 15:17:36 -0800 | [diff] [blame] | 390 | DEXFILE_SCOPED_TRACE(std::string("Verify dex file ") + location); |
David Srbecky | 14dc1bc | 2023-03-06 16:40:21 +0000 | [diff] [blame] | 391 | if (!dex::Verify(dex_file.get(), location.c_str(), verify_checksum, error_msg)) { |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 392 | if (error_code != nullptr) { |
| 393 | *error_code = DexFileLoaderErrorCode::kVerifyError; |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 394 | } |
| 395 | return nullptr; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 396 | } |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 397 | } |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 398 | if (error_code != nullptr) { |
| 399 | *error_code = DexFileLoaderErrorCode::kNoError; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 400 | } |
| 401 | return dex_file; |
| 402 | } |
| 403 | |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 404 | bool DexFileLoader::OpenFromZipEntry(const ZipArchive& zip_archive, |
| 405 | const char* entry_name, |
| 406 | const std::string& location, |
| 407 | bool verify, |
| 408 | bool verify_checksum, |
| 409 | DexFileLoaderErrorCode* error_code, |
| 410 | std::string* error_msg, |
| 411 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 412 | CHECK(!location.empty()); |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 413 | std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg)); |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 414 | if (zip_entry == nullptr) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 415 | *error_code = DexFileLoaderErrorCode::kEntryNotFound; |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 416 | return false; |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 417 | } |
| 418 | if (zip_entry->GetUncompressedLength() == 0) { |
| 419 | *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str()); |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 420 | *error_code = DexFileLoaderErrorCode::kDexFileError; |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 421 | return false; |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 422 | } |
| 423 | |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 424 | CHECK(MemMap::IsInitialized()); |
| 425 | MemMap map; |
| 426 | bool is_file_map = false; |
| 427 | if (file_.has_value() && zip_entry->IsUncompressed()) { |
| 428 | if (!zip_entry->IsAlignedTo(alignof(DexFile::Header))) { |
| 429 | // Do not mmap unaligned ZIP entries because |
| 430 | // doing so would fail dex verification which requires 4 byte alignment. |
| 431 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 432 | << "please zipalign to " << alignof(DexFile::Header) << " bytes. " |
| 433 | << "Falling back to extracting file."; |
| 434 | } else { |
| 435 | // Map uncompressed files within zip as file-backed to avoid a dirty copy. |
| 436 | map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/ error_msg); |
| 437 | if (!map.IsValid()) { |
| 438 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 439 | << "is your ZIP file corrupted? Falling back to extraction."; |
| 440 | // Try again with Extraction which still has a chance of recovery. |
| 441 | } |
| 442 | is_file_map = true; |
| 443 | } |
| 444 | } |
| 445 | if (!map.IsValid()) { |
Yabin Cui | bdbd707 | 2023-03-03 15:17:36 -0800 | [diff] [blame] | 446 | DEXFILE_SCOPED_TRACE(std::string("Extract dex file ") + location); |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 447 | |
| 448 | // Default path for compressed ZIP entries, |
| 449 | // and fallback for stored ZIP entries. |
| 450 | map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg); |
| 451 | } |
| 452 | if (!map.IsValid()) { |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 453 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(), |
| 454 | error_msg->c_str()); |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 455 | *error_code = DexFileLoaderErrorCode::kExtractToMemoryError; |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 456 | return false; |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 457 | } |
David Srbecky | 0b9b330 | 2023-02-24 19:03:51 +0000 | [diff] [blame] | 458 | auto container = std::make_shared<MemMapContainer>(std::move(map), is_file_map); |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 459 | container->SetIsZip(); |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 460 | if (!container->DisableWrite()) { |
| 461 | *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str()); |
| 462 | *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError; |
| 463 | return false; |
| 464 | } |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 465 | |
David Srbecky | 1dfac99 | 2023-03-07 18:58:11 +0000 | [diff] [blame] | 466 | std::unique_ptr<const DexFile> dex_file = OpenCommon(container, |
| 467 | container->Begin(), |
| 468 | container->Size(), |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 469 | location, |
| 470 | zip_entry->GetCrc32(), |
| 471 | /*oat_dex_file=*/nullptr, |
| 472 | verify, |
| 473 | verify_checksum, |
| 474 | error_msg, |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 475 | error_code); |
| 476 | if (dex_file == nullptr) { |
| 477 | return false; |
David Srbecky | 052f5fb | 2023-02-13 12:42:12 +0000 | [diff] [blame] | 478 | } |
| 479 | CHECK(dex_file->IsReadOnly()) << location; |
David Srbecky | f8580e5 | 2023-02-23 18:27:04 +0000 | [diff] [blame] | 480 | dex_files->push_back(std::move(dex_file)); |
| 481 | return true; |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 484 | } // namespace art |