David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [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 "art_dex_file_loader.h" |
| 18 | |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 19 | #include <sys/stat.h> |
| 20 | |
| 21 | #include "android-base/stringprintf.h" |
| 22 | |
| 23 | #include "base/file_magic.h" |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 24 | #include "base/file_utils.h" |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 25 | #include "base/mem_map.h" |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 26 | #include "base/mman.h" // For the PROT_* and MAP_* constants. |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 27 | #include "base/stl_util.h" |
| 28 | #include "base/systrace.h" |
| 29 | #include "base/unix_file/fd_file.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 30 | #include "base/zip_archive.h" |
David Sehr | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 31 | #include "dex/compact_dex_file.h" |
| 32 | #include "dex/dex_file.h" |
| 33 | #include "dex/dex_file_verifier.h" |
| 34 | #include "dex/standard_dex_file.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | class MemMapContainer : public DexFileContainer { |
| 41 | public: |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 42 | explicit MemMapContainer(MemMap&& mem_map) : mem_map_(std::move(mem_map)) { } |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 43 | ~MemMapContainer() override { } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 44 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 45 | int GetPermissions() override { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 46 | if (!mem_map_.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 47 | return 0; |
| 48 | } else { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 49 | return mem_map_.GetProtect(); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 53 | bool IsReadOnly() override { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 54 | return GetPermissions() == PROT_READ; |
| 55 | } |
| 56 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 57 | bool EnableWrite() override { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 58 | CHECK(IsReadOnly()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 59 | if (!mem_map_.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 60 | return false; |
| 61 | } else { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 62 | return mem_map_.Protect(PROT_READ | PROT_WRITE); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 66 | bool DisableWrite() override { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 67 | CHECK(!IsReadOnly()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 68 | if (!mem_map_.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 69 | return false; |
| 70 | } else { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 71 | return mem_map_.Protect(PROT_READ); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 76 | MemMap mem_map_; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 77 | DISALLOW_COPY_AND_ASSIGN(MemMapContainer); |
| 78 | }; |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | using android::base::StringPrintf; |
| 83 | |
| 84 | static constexpr OatDexFile* kNoOatDexFile = nullptr; |
| 85 | |
| 86 | |
| 87 | bool ArtDexFileLoader::GetMultiDexChecksums(const char* filename, |
| 88 | std::vector<uint32_t>* checksums, |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 89 | std::vector<std::string>* dex_locations, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 90 | std::string* error_msg, |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 91 | int zip_fd, |
| 92 | bool* zip_file_only_contains_uncompressed_dex) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 93 | CHECK(checksums != nullptr); |
| 94 | uint32_t magic; |
| 95 | |
| 96 | File fd; |
| 97 | if (zip_fd != -1) { |
| 98 | if (ReadMagicAndReset(zip_fd, &magic, error_msg)) { |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 99 | fd = File(DupCloexec(zip_fd), /* check_usage= */ false); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 100 | } |
| 101 | } else { |
| 102 | fd = OpenAndReadMagic(filename, &magic, error_msg); |
| 103 | } |
| 104 | if (fd.Fd() == -1) { |
| 105 | DCHECK(!error_msg->empty()); |
| 106 | return false; |
| 107 | } |
| 108 | if (IsZipMagic(magic)) { |
| 109 | std::unique_ptr<ZipArchive> zip_archive( |
| 110 | ZipArchive::OpenFromFd(fd.Release(), filename, error_msg)); |
| 111 | if (zip_archive.get() == nullptr) { |
| 112 | *error_msg = StringPrintf("Failed to open zip archive '%s' (error msg: %s)", filename, |
| 113 | error_msg->c_str()); |
| 114 | return false; |
| 115 | } |
| 116 | |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 117 | uint32_t idx = 0; |
| 118 | std::string zip_entry_name = GetMultiDexClassesDexName(idx); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 119 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name.c_str(), error_msg)); |
| 120 | if (zip_entry.get() == nullptr) { |
| 121 | *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", filename, |
| 122 | zip_entry_name.c_str(), error_msg->c_str()); |
| 123 | return false; |
| 124 | } |
| 125 | |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 126 | if (zip_file_only_contains_uncompressed_dex != nullptr) { |
| 127 | // Start by assuming everything is uncompressed. |
| 128 | *zip_file_only_contains_uncompressed_dex = true; |
| 129 | } |
| 130 | |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 131 | do { |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 132 | if (zip_file_only_contains_uncompressed_dex != nullptr) { |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 133 | if (!(zip_entry->IsUncompressed() && zip_entry->IsAlignedTo(alignof(DexFile::Header)))) { |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 134 | *zip_file_only_contains_uncompressed_dex = false; |
| 135 | } |
| 136 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 137 | checksums->push_back(zip_entry->GetCrc32()); |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 138 | dex_locations->push_back(GetMultiDexLocation(idx, filename)); |
| 139 | zip_entry_name = GetMultiDexClassesDexName(++idx); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 140 | zip_entry.reset(zip_archive->Find(zip_entry_name.c_str(), error_msg)); |
| 141 | } while (zip_entry.get() != nullptr); |
| 142 | return true; |
| 143 | } |
| 144 | if (IsMagicValid(magic)) { |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 145 | std::unique_ptr<const DexFile> dex_file(OpenFile(fd.Release(), |
| 146 | filename, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 147 | /* verify= */ false, |
| 148 | /* verify_checksum= */ false, |
| 149 | /* mmap_shared= */ false, |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 150 | error_msg)); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 151 | if (dex_file == nullptr) { |
| 152 | return false; |
| 153 | } |
| 154 | checksums->push_back(dex_file->GetHeader().checksum_); |
| 155 | return true; |
| 156 | } |
| 157 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename); |
| 158 | return false; |
| 159 | } |
| 160 | |
Martin Stjernholm | 785c987 | 2018-11-28 00:25:18 +0000 | [diff] [blame] | 161 | std::unique_ptr<const DexFile> ArtDexFileLoader::Open( |
| 162 | const uint8_t* base, |
| 163 | size_t size, |
| 164 | const std::string& location, |
| 165 | uint32_t location_checksum, |
| 166 | const OatDexFile* oat_dex_file, |
| 167 | bool verify, |
| 168 | bool verify_checksum, |
| 169 | std::string* error_msg, |
| 170 | std::unique_ptr<DexFileContainer> container) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 171 | ScopedTrace trace(std::string("Open dex file from RAM ") + location); |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 172 | return OpenCommon(base, |
| 173 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 174 | /*data_base=*/ nullptr, |
| 175 | /*data_size=*/ 0u, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 176 | location, |
| 177 | location_checksum, |
| 178 | oat_dex_file, |
| 179 | verify, |
| 180 | verify_checksum, |
| 181 | error_msg, |
Martin Stjernholm | 785c987 | 2018-11-28 00:25:18 +0000 | [diff] [blame] | 182 | std::move(container), |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 183 | /*verify_result=*/ nullptr); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const std::string& location, |
| 187 | uint32_t location_checksum, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 188 | MemMap&& map, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 189 | bool verify, |
| 190 | bool verify_checksum, |
| 191 | std::string* error_msg) const { |
| 192 | ScopedTrace trace(std::string("Open dex file from mapped-memory ") + location); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 193 | CHECK(map.IsValid()); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 194 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 195 | size_t size = map.Size(); |
| 196 | if (size < sizeof(DexFile::Header)) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 197 | *error_msg = StringPrintf( |
| 198 | "DexFile: failed to open dex file '%s' that is too short to have a header", |
| 199 | location.c_str()); |
| 200 | return nullptr; |
| 201 | } |
| 202 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 203 | uint8_t* begin = map.Begin(); |
| 204 | std::unique_ptr<DexFile> dex_file = OpenCommon(begin, |
| 205 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 206 | /*data_base=*/ nullptr, |
| 207 | /*data_size=*/ 0u, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 208 | location, |
| 209 | location_checksum, |
| 210 | kNoOatDexFile, |
| 211 | verify, |
| 212 | verify_checksum, |
| 213 | error_msg, |
| 214 | std::make_unique<MemMapContainer>(std::move(map)), |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 215 | /*verify_result=*/ nullptr); |
Mathieu Chartier | 14e7bad | 2018-03-22 14:33:20 -0700 | [diff] [blame] | 216 | // Opening CompactDex is only supported from vdex files. |
| 217 | if (dex_file != nullptr && dex_file->IsCompactDexFile()) { |
| 218 | *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files", |
| 219 | location.c_str()); |
| 220 | return nullptr; |
| 221 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 222 | return dex_file; |
| 223 | } |
| 224 | |
| 225 | bool ArtDexFileLoader::Open(const char* filename, |
| 226 | const std::string& location, |
| 227 | bool verify, |
| 228 | bool verify_checksum, |
| 229 | std::string* error_msg, |
| 230 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 231 | uint32_t magic; |
| 232 | File fd = OpenAndReadMagic(filename, &magic, error_msg); |
| 233 | if (fd.Fd() == -1) { |
| 234 | DCHECK(!error_msg->empty()); |
| 235 | return false; |
| 236 | } |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 237 | return OpenWithMagic( |
| 238 | magic, fd.Release(), location, verify, verify_checksum, error_msg, dex_files); |
| 239 | } |
| 240 | |
| 241 | bool ArtDexFileLoader::Open(int fd, |
| 242 | const std::string& location, |
| 243 | bool verify, |
| 244 | bool verify_checksum, |
| 245 | std::string* error_msg, |
| 246 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 247 | uint32_t magic; |
| 248 | if (!ReadMagicAndReset(fd, &magic, error_msg)) { |
| 249 | DCHECK(!error_msg->empty()); |
| 250 | return false; |
| 251 | } |
| 252 | return OpenWithMagic(magic, fd, location, verify, verify_checksum, error_msg, dex_files); |
| 253 | } |
| 254 | |
Calin Juravle | 5ff2393 | 2020-12-11 18:26:14 -0800 | [diff] [blame] | 255 | bool ArtDexFileLoader::Open(const char* filename, |
| 256 | int fd, |
| 257 | const std::string& location, |
| 258 | bool verify, |
| 259 | bool verify_checksum, |
| 260 | std::string* error_msg, |
| 261 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 262 | return fd == -1 |
| 263 | ? Open(filename, location, verify, verify_checksum, error_msg, dex_files) |
| 264 | : Open(fd, location, verify, verify_checksum, error_msg, dex_files); |
| 265 | } |
| 266 | |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 267 | bool ArtDexFileLoader::OpenWithMagic(uint32_t magic, |
| 268 | int fd, |
| 269 | const std::string& location, |
| 270 | bool verify, |
| 271 | bool verify_checksum, |
| 272 | std::string* error_msg, |
| 273 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 274 | ScopedTrace trace(std::string("Open dex file ") + std::string(location)); |
| 275 | DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr"; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 276 | if (IsZipMagic(magic)) { |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 277 | return OpenZip(fd, location, verify, verify_checksum, error_msg, dex_files); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 278 | } |
| 279 | if (IsMagicValid(magic)) { |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 280 | std::unique_ptr<const DexFile> dex_file(OpenFile(fd, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 281 | location, |
| 282 | verify, |
| 283 | verify_checksum, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 284 | /* mmap_shared= */ false, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 285 | error_msg)); |
| 286 | if (dex_file.get() != nullptr) { |
| 287 | dex_files->push_back(std::move(dex_file)); |
| 288 | return true; |
| 289 | } else { |
| 290 | return false; |
| 291 | } |
| 292 | } |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 293 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", location.c_str()); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
| 297 | std::unique_ptr<const DexFile> ArtDexFileLoader::OpenDex(int fd, |
| 298 | const std::string& location, |
| 299 | bool verify, |
| 300 | bool verify_checksum, |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 301 | bool mmap_shared, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 302 | std::string* error_msg) const { |
| 303 | ScopedTrace trace("Open dex file " + std::string(location)); |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 304 | return OpenFile(fd, location, verify, verify_checksum, mmap_shared, error_msg); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | bool ArtDexFileLoader::OpenZip(int fd, |
| 308 | const std::string& location, |
| 309 | bool verify, |
| 310 | bool verify_checksum, |
| 311 | std::string* error_msg, |
| 312 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 313 | ScopedTrace trace("Dex file open Zip " + std::string(location)); |
Victor Hsieh | 9060ebe | 2021-06-17 12:57:12 -0700 | [diff] [blame] | 314 | return OpenZipInternal(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg), |
| 315 | location, |
| 316 | verify, |
| 317 | verify_checksum, |
| 318 | error_msg, |
| 319 | dex_files); |
| 320 | } |
| 321 | |
| 322 | bool ArtDexFileLoader::OpenZipFromOwnedFd( |
| 323 | int fd, |
| 324 | const std::string& location, |
| 325 | bool verify, |
| 326 | bool verify_checksum, |
| 327 | std::string* error_msg, |
| 328 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 329 | ScopedTrace trace("Dex file open Zip " + std::string(location) + " (owned fd)"); |
| 330 | return OpenZipInternal(ZipArchive::OpenFromOwnedFd(fd, location.c_str(), error_msg), |
| 331 | location, |
| 332 | verify, |
| 333 | verify_checksum, |
| 334 | error_msg, |
| 335 | dex_files); |
| 336 | } |
| 337 | |
| 338 | bool ArtDexFileLoader::OpenZipInternal( |
| 339 | ZipArchive* raw_zip_archive, |
| 340 | const std::string& location, |
| 341 | bool verify, |
| 342 | bool verify_checksum, |
| 343 | std::string* error_msg, |
| 344 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 345 | DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is nullptr"; |
Victor Hsieh | 9060ebe | 2021-06-17 12:57:12 -0700 | [diff] [blame] | 346 | std::unique_ptr<ZipArchive> zip_archive(raw_zip_archive); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 347 | if (zip_archive.get() == nullptr) { |
| 348 | DCHECK(!error_msg->empty()); |
| 349 | return false; |
| 350 | } |
| 351 | return OpenAllDexFilesFromZip( |
| 352 | *zip_archive, location, verify, verify_checksum, error_msg, dex_files); |
| 353 | } |
| 354 | |
| 355 | std::unique_ptr<const DexFile> ArtDexFileLoader::OpenFile(int fd, |
| 356 | const std::string& location, |
| 357 | bool verify, |
| 358 | bool verify_checksum, |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 359 | bool mmap_shared, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 360 | std::string* error_msg) const { |
| 361 | ScopedTrace trace(std::string("Open dex file ") + std::string(location)); |
| 362 | CHECK(!location.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 363 | MemMap map; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 364 | { |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 365 | File delayed_close(fd, /* check_usage= */ false); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 366 | struct stat sbuf; |
| 367 | memset(&sbuf, 0, sizeof(sbuf)); |
| 368 | if (fstat(fd, &sbuf) == -1) { |
| 369 | *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location.c_str(), |
| 370 | strerror(errno)); |
| 371 | return nullptr; |
| 372 | } |
| 373 | if (S_ISDIR(sbuf.st_mode)) { |
| 374 | *error_msg = StringPrintf("Attempt to mmap directory '%s'", location.c_str()); |
| 375 | return nullptr; |
| 376 | } |
| 377 | size_t length = sbuf.st_size; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 378 | map = MemMap::MapFile(length, |
| 379 | PROT_READ, |
| 380 | mmap_shared ? MAP_SHARED : MAP_PRIVATE, |
| 381 | fd, |
| 382 | 0, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 383 | /*low_4gb=*/false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 384 | location.c_str(), |
| 385 | error_msg); |
| 386 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 387 | DCHECK(!error_msg->empty()); |
| 388 | return nullptr; |
| 389 | } |
| 390 | } |
| 391 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 392 | const uint8_t* begin = map.Begin(); |
| 393 | size_t size = map.Size(); |
| 394 | if (size < sizeof(DexFile::Header)) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 395 | *error_msg = StringPrintf( |
| 396 | "DexFile: failed to open dex file '%s' that is too short to have a header", |
| 397 | location.c_str()); |
| 398 | return nullptr; |
| 399 | } |
| 400 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 401 | const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(begin); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 402 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 403 | std::unique_ptr<DexFile> dex_file = OpenCommon(begin, |
| 404 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 405 | /*data_base=*/ nullptr, |
| 406 | /*data_size=*/ 0u, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 407 | location, |
| 408 | dex_header->checksum_, |
| 409 | kNoOatDexFile, |
| 410 | verify, |
| 411 | verify_checksum, |
| 412 | error_msg, |
| 413 | std::make_unique<MemMapContainer>(std::move(map)), |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 414 | /*verify_result=*/ nullptr); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 415 | |
Mathieu Chartier | 14e7bad | 2018-03-22 14:33:20 -0700 | [diff] [blame] | 416 | // Opening CompactDex is only supported from vdex files. |
| 417 | if (dex_file != nullptr && dex_file->IsCompactDexFile()) { |
| 418 | *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files", |
| 419 | location.c_str()); |
| 420 | return nullptr; |
| 421 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 422 | return dex_file; |
| 423 | } |
| 424 | |
| 425 | std::unique_ptr<const DexFile> ArtDexFileLoader::OpenOneDexFileFromZip( |
| 426 | const ZipArchive& zip_archive, |
| 427 | const char* entry_name, |
| 428 | const std::string& location, |
| 429 | bool verify, |
| 430 | bool verify_checksum, |
| 431 | std::string* error_msg, |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 432 | DexFileLoaderErrorCode* error_code) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 433 | ScopedTrace trace("Dex file open from Zip Archive " + std::string(location)); |
| 434 | CHECK(!location.empty()); |
| 435 | std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg)); |
| 436 | if (zip_entry == nullptr) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 437 | *error_code = DexFileLoaderErrorCode::kEntryNotFound; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 438 | return nullptr; |
| 439 | } |
| 440 | if (zip_entry->GetUncompressedLength() == 0) { |
| 441 | *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str()); |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 442 | *error_code = DexFileLoaderErrorCode::kDexFileError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 443 | return nullptr; |
| 444 | } |
| 445 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 446 | MemMap map; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 447 | if (zip_entry->IsUncompressed()) { |
| 448 | if (!zip_entry->IsAlignedTo(alignof(DexFile::Header))) { |
| 449 | // Do not mmap unaligned ZIP entries because |
| 450 | // doing so would fail dex verification which requires 4 byte alignment. |
| 451 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 452 | << "please zipalign to " << alignof(DexFile::Header) << " bytes. " |
| 453 | << "Falling back to extracting file."; |
| 454 | } else { |
| 455 | // Map uncompressed files within zip as file-backed to avoid a dirty copy. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 456 | map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/error_msg); |
| 457 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 458 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 459 | << "is your ZIP file corrupted? Falling back to extraction."; |
| 460 | // Try again with Extraction which still has a chance of recovery. |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 465 | ScopedTrace map_extract_trace(StringPrintf("Mapped=%s Extracted=%s", |
| 466 | map.IsValid() ? "true" : "false", |
| 467 | map.IsValid() ? "false" : "true")); // this is redundant but much easier to read in traces. |
| 468 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 469 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 470 | // Default path for compressed ZIP entries, |
| 471 | // and fallback for stored ZIP entries. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 472 | map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 475 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 476 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(), |
| 477 | error_msg->c_str()); |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 478 | *error_code = DexFileLoaderErrorCode::kExtractToMemoryError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 479 | return nullptr; |
| 480 | } |
| 481 | VerifyResult verify_result; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 482 | uint8_t* begin = map.Begin(); |
| 483 | size_t size = map.Size(); |
| 484 | std::unique_ptr<DexFile> dex_file = OpenCommon(begin, |
| 485 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 486 | /*data_base=*/ nullptr, |
| 487 | /*data_size=*/ 0u, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 488 | location, |
| 489 | zip_entry->GetCrc32(), |
| 490 | kNoOatDexFile, |
| 491 | verify, |
| 492 | verify_checksum, |
| 493 | error_msg, |
| 494 | std::make_unique<MemMapContainer>(std::move(map)), |
| 495 | &verify_result); |
Mathieu Chartier | 14e7bad | 2018-03-22 14:33:20 -0700 | [diff] [blame] | 496 | if (dex_file != nullptr && dex_file->IsCompactDexFile()) { |
| 497 | *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files", |
| 498 | location.c_str()); |
| 499 | return nullptr; |
| 500 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 501 | if (dex_file == nullptr) { |
| 502 | if (verify_result == VerifyResult::kVerifyNotAttempted) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 503 | *error_code = DexFileLoaderErrorCode::kDexFileError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 504 | } else { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 505 | *error_code = DexFileLoaderErrorCode::kVerifyError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 506 | } |
| 507 | return nullptr; |
| 508 | } |
| 509 | if (!dex_file->DisableWrite()) { |
| 510 | *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str()); |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 511 | *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 512 | return nullptr; |
| 513 | } |
| 514 | CHECK(dex_file->IsReadOnly()) << location; |
| 515 | if (verify_result != VerifyResult::kVerifySucceeded) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 516 | *error_code = DexFileLoaderErrorCode::kVerifyError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 517 | return nullptr; |
| 518 | } |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 519 | *error_code = DexFileLoaderErrorCode::kNoError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 520 | return dex_file; |
| 521 | } |
| 522 | |
| 523 | // Technically we do not have a limitation with respect to the number of dex files that can be in a |
| 524 | // multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols |
| 525 | // (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what |
| 526 | // seems an excessive number. |
| 527 | static constexpr size_t kWarnOnManyDexFilesThreshold = 100; |
| 528 | |
| 529 | bool ArtDexFileLoader::OpenAllDexFilesFromZip( |
| 530 | const ZipArchive& zip_archive, |
| 531 | const std::string& location, |
| 532 | bool verify, |
| 533 | bool verify_checksum, |
| 534 | std::string* error_msg, |
| 535 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 536 | ScopedTrace trace("Dex file open from Zip " + std::string(location)); |
| 537 | DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is nullptr"; |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 538 | DexFileLoaderErrorCode error_code; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 539 | std::unique_ptr<const DexFile> dex_file(OpenOneDexFileFromZip(zip_archive, |
| 540 | kClassesDex, |
| 541 | location, |
| 542 | verify, |
| 543 | verify_checksum, |
| 544 | error_msg, |
| 545 | &error_code)); |
| 546 | if (dex_file.get() == nullptr) { |
| 547 | return false; |
| 548 | } else { |
| 549 | // Had at least classes.dex. |
| 550 | dex_files->push_back(std::move(dex_file)); |
| 551 | |
| 552 | // Now try some more. |
| 553 | |
| 554 | // We could try to avoid std::string allocations by working on a char array directly. As we |
| 555 | // do not expect a lot of iterations, this seems too involved and brittle. |
| 556 | |
| 557 | for (size_t i = 1; ; ++i) { |
| 558 | std::string name = GetMultiDexClassesDexName(i); |
| 559 | std::string fake_location = GetMultiDexLocation(i, location.c_str()); |
| 560 | std::unique_ptr<const DexFile> next_dex_file(OpenOneDexFileFromZip(zip_archive, |
| 561 | name.c_str(), |
| 562 | fake_location, |
| 563 | verify, |
| 564 | verify_checksum, |
| 565 | error_msg, |
| 566 | &error_code)); |
| 567 | if (next_dex_file.get() == nullptr) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 568 | if (error_code != DexFileLoaderErrorCode::kEntryNotFound) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 569 | LOG(WARNING) << "Zip open failed: " << *error_msg; |
| 570 | } |
| 571 | break; |
| 572 | } else { |
| 573 | dex_files->push_back(std::move(next_dex_file)); |
| 574 | } |
| 575 | |
| 576 | if (i == kWarnOnManyDexFilesThreshold) { |
| 577 | LOG(WARNING) << location << " has in excess of " << kWarnOnManyDexFilesThreshold |
| 578 | << " dex files. Please consider coalescing and shrinking the number to " |
| 579 | " avoid runtime overhead."; |
| 580 | } |
| 581 | |
| 582 | if (i == std::numeric_limits<size_t>::max()) { |
| 583 | LOG(ERROR) << "Overflow in number of dex files!"; |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | return true; |
| 589 | } |
| 590 | } |
| 591 | |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 592 | std::unique_ptr<DexFile> ArtDexFileLoader::OpenCommon(const uint8_t* base, |
| 593 | size_t size, |
| 594 | const uint8_t* data_base, |
| 595 | size_t data_size, |
| 596 | const std::string& location, |
| 597 | uint32_t location_checksum, |
| 598 | const OatDexFile* oat_dex_file, |
| 599 | bool verify, |
| 600 | bool verify_checksum, |
| 601 | std::string* error_msg, |
| 602 | std::unique_ptr<DexFileContainer> container, |
| 603 | VerifyResult* verify_result) { |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 604 | return DexFileLoader::OpenCommon(base, |
| 605 | size, |
| 606 | data_base, |
| 607 | data_size, |
| 608 | location, |
| 609 | location_checksum, |
| 610 | oat_dex_file, |
| 611 | verify, |
| 612 | verify_checksum, |
| 613 | error_msg, |
| 614 | std::move(container), |
| 615 | verify_result); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 616 | } |
| 617 | |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 618 | } // namespace art |