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, |
| 89 | std::string* error_msg, |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 90 | int zip_fd, |
| 91 | bool* zip_file_only_contains_uncompressed_dex) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 92 | CHECK(checksums != nullptr); |
| 93 | uint32_t magic; |
| 94 | |
| 95 | File fd; |
| 96 | if (zip_fd != -1) { |
| 97 | if (ReadMagicAndReset(zip_fd, &magic, error_msg)) { |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 98 | fd = File(DupCloexec(zip_fd), /* check_usage= */ false); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 99 | } |
| 100 | } else { |
| 101 | fd = OpenAndReadMagic(filename, &magic, error_msg); |
| 102 | } |
| 103 | if (fd.Fd() == -1) { |
| 104 | DCHECK(!error_msg->empty()); |
| 105 | return false; |
| 106 | } |
| 107 | if (IsZipMagic(magic)) { |
| 108 | std::unique_ptr<ZipArchive> zip_archive( |
| 109 | ZipArchive::OpenFromFd(fd.Release(), filename, error_msg)); |
| 110 | if (zip_archive.get() == nullptr) { |
| 111 | *error_msg = StringPrintf("Failed to open zip archive '%s' (error msg: %s)", filename, |
| 112 | error_msg->c_str()); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | uint32_t i = 0; |
| 117 | std::string zip_entry_name = GetMultiDexClassesDexName(i++); |
| 118 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name.c_str(), error_msg)); |
| 119 | if (zip_entry.get() == nullptr) { |
| 120 | *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", filename, |
| 121 | zip_entry_name.c_str(), error_msg->c_str()); |
| 122 | return false; |
| 123 | } |
| 124 | |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 125 | if (zip_file_only_contains_uncompressed_dex != nullptr) { |
| 126 | // Start by assuming everything is uncompressed. |
| 127 | *zip_file_only_contains_uncompressed_dex = true; |
| 128 | } |
| 129 | |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 130 | do { |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 131 | if (zip_file_only_contains_uncompressed_dex != nullptr) { |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 132 | if (!(zip_entry->IsUncompressed() && zip_entry->IsAlignedTo(alignof(DexFile::Header)))) { |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 133 | *zip_file_only_contains_uncompressed_dex = false; |
| 134 | } |
| 135 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 136 | checksums->push_back(zip_entry->GetCrc32()); |
| 137 | zip_entry_name = GetMultiDexClassesDexName(i++); |
| 138 | zip_entry.reset(zip_archive->Find(zip_entry_name.c_str(), error_msg)); |
| 139 | } while (zip_entry.get() != nullptr); |
| 140 | return true; |
| 141 | } |
| 142 | if (IsMagicValid(magic)) { |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 143 | std::unique_ptr<const DexFile> dex_file(OpenFile(fd.Release(), |
| 144 | filename, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 145 | /* verify= */ false, |
| 146 | /* verify_checksum= */ false, |
| 147 | /* mmap_shared= */ false, |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 148 | error_msg)); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 149 | if (dex_file == nullptr) { |
| 150 | return false; |
| 151 | } |
| 152 | checksums->push_back(dex_file->GetHeader().checksum_); |
| 153 | return true; |
| 154 | } |
| 155 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename); |
| 156 | return false; |
| 157 | } |
| 158 | |
Martin Stjernholm | 785c987 | 2018-11-28 00:25:18 +0000 | [diff] [blame] | 159 | std::unique_ptr<const DexFile> ArtDexFileLoader::Open( |
| 160 | const uint8_t* base, |
| 161 | size_t size, |
| 162 | const std::string& location, |
| 163 | uint32_t location_checksum, |
| 164 | const OatDexFile* oat_dex_file, |
| 165 | bool verify, |
| 166 | bool verify_checksum, |
| 167 | std::string* error_msg, |
| 168 | std::unique_ptr<DexFileContainer> container) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 169 | ScopedTrace trace(std::string("Open dex file from RAM ") + location); |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 170 | return OpenCommon(base, |
| 171 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 172 | /*data_base=*/ nullptr, |
| 173 | /*data_size=*/ 0u, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 174 | location, |
| 175 | location_checksum, |
| 176 | oat_dex_file, |
| 177 | verify, |
| 178 | verify_checksum, |
| 179 | error_msg, |
Martin Stjernholm | 785c987 | 2018-11-28 00:25:18 +0000 | [diff] [blame] | 180 | std::move(container), |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 181 | /*verify_result=*/ nullptr); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const std::string& location, |
| 185 | uint32_t location_checksum, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 186 | MemMap&& map, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 187 | bool verify, |
| 188 | bool verify_checksum, |
| 189 | std::string* error_msg) const { |
| 190 | ScopedTrace trace(std::string("Open dex file from mapped-memory ") + location); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 191 | CHECK(map.IsValid()); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 192 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 193 | size_t size = map.Size(); |
| 194 | if (size < sizeof(DexFile::Header)) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 195 | *error_msg = StringPrintf( |
| 196 | "DexFile: failed to open dex file '%s' that is too short to have a header", |
| 197 | location.c_str()); |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 201 | uint8_t* begin = map.Begin(); |
| 202 | std::unique_ptr<DexFile> dex_file = OpenCommon(begin, |
| 203 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 204 | /*data_base=*/ nullptr, |
| 205 | /*data_size=*/ 0u, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 206 | location, |
| 207 | location_checksum, |
| 208 | kNoOatDexFile, |
| 209 | verify, |
| 210 | verify_checksum, |
| 211 | error_msg, |
| 212 | std::make_unique<MemMapContainer>(std::move(map)), |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 213 | /*verify_result=*/ nullptr); |
Mathieu Chartier | 14e7bad | 2018-03-22 14:33:20 -0700 | [diff] [blame] | 214 | // Opening CompactDex is only supported from vdex files. |
| 215 | if (dex_file != nullptr && dex_file->IsCompactDexFile()) { |
| 216 | *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files", |
| 217 | location.c_str()); |
| 218 | return nullptr; |
| 219 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 220 | return dex_file; |
| 221 | } |
| 222 | |
| 223 | bool ArtDexFileLoader::Open(const char* filename, |
| 224 | const std::string& location, |
| 225 | bool verify, |
| 226 | bool verify_checksum, |
| 227 | std::string* error_msg, |
| 228 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 229 | ScopedTrace trace(std::string("Open dex file ") + std::string(location)); |
| 230 | DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr"; |
| 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 | } |
| 237 | if (IsZipMagic(magic)) { |
| 238 | return OpenZip(fd.Release(), location, verify, verify_checksum, error_msg, dex_files); |
| 239 | } |
| 240 | if (IsMagicValid(magic)) { |
| 241 | std::unique_ptr<const DexFile> dex_file(OpenFile(fd.Release(), |
| 242 | location, |
| 243 | verify, |
| 244 | verify_checksum, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 245 | /* mmap_shared= */ false, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 246 | error_msg)); |
| 247 | if (dex_file.get() != nullptr) { |
| 248 | dex_files->push_back(std::move(dex_file)); |
| 249 | return true; |
| 250 | } else { |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | std::unique_ptr<const DexFile> ArtDexFileLoader::OpenDex(int fd, |
| 259 | const std::string& location, |
| 260 | bool verify, |
| 261 | bool verify_checksum, |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 262 | bool mmap_shared, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 263 | std::string* error_msg) const { |
| 264 | ScopedTrace trace("Open dex file " + std::string(location)); |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 265 | return OpenFile(fd, location, verify, verify_checksum, mmap_shared, error_msg); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | bool ArtDexFileLoader::OpenZip(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("Dex file open Zip " + std::string(location)); |
| 275 | DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is nullptr"; |
| 276 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg)); |
| 277 | if (zip_archive.get() == nullptr) { |
| 278 | DCHECK(!error_msg->empty()); |
| 279 | return false; |
| 280 | } |
| 281 | return OpenAllDexFilesFromZip( |
| 282 | *zip_archive, location, verify, verify_checksum, error_msg, dex_files); |
| 283 | } |
| 284 | |
| 285 | std::unique_ptr<const DexFile> ArtDexFileLoader::OpenFile(int fd, |
| 286 | const std::string& location, |
| 287 | bool verify, |
| 288 | bool verify_checksum, |
David Brazdil | 2b9c35b | 2018-01-12 15:44:43 +0000 | [diff] [blame] | 289 | bool mmap_shared, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 290 | std::string* error_msg) const { |
| 291 | ScopedTrace trace(std::string("Open dex file ") + std::string(location)); |
| 292 | CHECK(!location.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 293 | MemMap map; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 294 | { |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 295 | File delayed_close(fd, /* check_usage= */ false); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 296 | struct stat sbuf; |
| 297 | memset(&sbuf, 0, sizeof(sbuf)); |
| 298 | if (fstat(fd, &sbuf) == -1) { |
| 299 | *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location.c_str(), |
| 300 | strerror(errno)); |
| 301 | return nullptr; |
| 302 | } |
| 303 | if (S_ISDIR(sbuf.st_mode)) { |
| 304 | *error_msg = StringPrintf("Attempt to mmap directory '%s'", location.c_str()); |
| 305 | return nullptr; |
| 306 | } |
| 307 | size_t length = sbuf.st_size; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 308 | map = MemMap::MapFile(length, |
| 309 | PROT_READ, |
| 310 | mmap_shared ? MAP_SHARED : MAP_PRIVATE, |
| 311 | fd, |
| 312 | 0, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 313 | /*low_4gb=*/false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 314 | location.c_str(), |
| 315 | error_msg); |
| 316 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 317 | DCHECK(!error_msg->empty()); |
| 318 | return nullptr; |
| 319 | } |
| 320 | } |
| 321 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 322 | const uint8_t* begin = map.Begin(); |
| 323 | size_t size = map.Size(); |
| 324 | if (size < sizeof(DexFile::Header)) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 325 | *error_msg = StringPrintf( |
| 326 | "DexFile: failed to open dex file '%s' that is too short to have a header", |
| 327 | location.c_str()); |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 331 | const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(begin); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 332 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 333 | std::unique_ptr<DexFile> dex_file = OpenCommon(begin, |
| 334 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 335 | /*data_base=*/ nullptr, |
| 336 | /*data_size=*/ 0u, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 337 | location, |
| 338 | dex_header->checksum_, |
| 339 | kNoOatDexFile, |
| 340 | verify, |
| 341 | verify_checksum, |
| 342 | error_msg, |
| 343 | std::make_unique<MemMapContainer>(std::move(map)), |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 344 | /*verify_result=*/ nullptr); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 345 | |
Mathieu Chartier | 14e7bad | 2018-03-22 14:33:20 -0700 | [diff] [blame] | 346 | // Opening CompactDex is only supported from vdex files. |
| 347 | if (dex_file != nullptr && dex_file->IsCompactDexFile()) { |
| 348 | *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files", |
| 349 | location.c_str()); |
| 350 | return nullptr; |
| 351 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 352 | return dex_file; |
| 353 | } |
| 354 | |
| 355 | std::unique_ptr<const DexFile> ArtDexFileLoader::OpenOneDexFileFromZip( |
| 356 | const ZipArchive& zip_archive, |
| 357 | const char* entry_name, |
| 358 | const std::string& location, |
| 359 | bool verify, |
| 360 | bool verify_checksum, |
| 361 | std::string* error_msg, |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 362 | DexFileLoaderErrorCode* error_code) const { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 363 | ScopedTrace trace("Dex file open from Zip Archive " + std::string(location)); |
| 364 | CHECK(!location.empty()); |
| 365 | std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg)); |
| 366 | if (zip_entry == nullptr) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 367 | *error_code = DexFileLoaderErrorCode::kEntryNotFound; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 368 | return nullptr; |
| 369 | } |
| 370 | if (zip_entry->GetUncompressedLength() == 0) { |
| 371 | *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str()); |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 372 | *error_code = DexFileLoaderErrorCode::kDexFileError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 373 | return nullptr; |
| 374 | } |
| 375 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 376 | MemMap map; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 377 | if (zip_entry->IsUncompressed()) { |
| 378 | if (!zip_entry->IsAlignedTo(alignof(DexFile::Header))) { |
| 379 | // Do not mmap unaligned ZIP entries because |
| 380 | // doing so would fail dex verification which requires 4 byte alignment. |
| 381 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 382 | << "please zipalign to " << alignof(DexFile::Header) << " bytes. " |
| 383 | << "Falling back to extracting file."; |
| 384 | } else { |
| 385 | // 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] | 386 | map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/error_msg); |
| 387 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 388 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 389 | << "is your ZIP file corrupted? Falling back to extraction."; |
| 390 | // Try again with Extraction which still has a chance of recovery. |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 395 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 396 | // Default path for compressed ZIP entries, |
| 397 | // and fallback for stored ZIP entries. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 398 | map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 401 | if (!map.IsValid()) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 402 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(), |
| 403 | error_msg->c_str()); |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 404 | *error_code = DexFileLoaderErrorCode::kExtractToMemoryError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 405 | return nullptr; |
| 406 | } |
| 407 | VerifyResult verify_result; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 408 | uint8_t* begin = map.Begin(); |
| 409 | size_t size = map.Size(); |
| 410 | std::unique_ptr<DexFile> dex_file = OpenCommon(begin, |
| 411 | size, |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 412 | /*data_base=*/ nullptr, |
| 413 | /*data_size=*/ 0u, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 414 | location, |
| 415 | zip_entry->GetCrc32(), |
| 416 | kNoOatDexFile, |
| 417 | verify, |
| 418 | verify_checksum, |
| 419 | error_msg, |
| 420 | std::make_unique<MemMapContainer>(std::move(map)), |
| 421 | &verify_result); |
Mathieu Chartier | 14e7bad | 2018-03-22 14:33:20 -0700 | [diff] [blame] | 422 | if (dex_file != nullptr && dex_file->IsCompactDexFile()) { |
| 423 | *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files", |
| 424 | location.c_str()); |
| 425 | return nullptr; |
| 426 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 427 | if (dex_file == nullptr) { |
| 428 | if (verify_result == VerifyResult::kVerifyNotAttempted) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 429 | *error_code = DexFileLoaderErrorCode::kDexFileError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 430 | } else { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 431 | *error_code = DexFileLoaderErrorCode::kVerifyError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 432 | } |
| 433 | return nullptr; |
| 434 | } |
| 435 | if (!dex_file->DisableWrite()) { |
| 436 | *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] | 437 | *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 438 | return nullptr; |
| 439 | } |
| 440 | CHECK(dex_file->IsReadOnly()) << location; |
| 441 | if (verify_result != VerifyResult::kVerifySucceeded) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 442 | *error_code = DexFileLoaderErrorCode::kVerifyError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 443 | return nullptr; |
| 444 | } |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 445 | *error_code = DexFileLoaderErrorCode::kNoError; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 446 | return dex_file; |
| 447 | } |
| 448 | |
| 449 | // Technically we do not have a limitation with respect to the number of dex files that can be in a |
| 450 | // multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols |
| 451 | // (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what |
| 452 | // seems an excessive number. |
| 453 | static constexpr size_t kWarnOnManyDexFilesThreshold = 100; |
| 454 | |
| 455 | bool ArtDexFileLoader::OpenAllDexFilesFromZip( |
| 456 | const ZipArchive& zip_archive, |
| 457 | const std::string& location, |
| 458 | bool verify, |
| 459 | bool verify_checksum, |
| 460 | std::string* error_msg, |
| 461 | std::vector<std::unique_ptr<const DexFile>>* dex_files) const { |
| 462 | ScopedTrace trace("Dex file open from Zip " + std::string(location)); |
| 463 | DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is nullptr"; |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 464 | DexFileLoaderErrorCode error_code; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 465 | std::unique_ptr<const DexFile> dex_file(OpenOneDexFileFromZip(zip_archive, |
| 466 | kClassesDex, |
| 467 | location, |
| 468 | verify, |
| 469 | verify_checksum, |
| 470 | error_msg, |
| 471 | &error_code)); |
| 472 | if (dex_file.get() == nullptr) { |
| 473 | return false; |
| 474 | } else { |
| 475 | // Had at least classes.dex. |
| 476 | dex_files->push_back(std::move(dex_file)); |
| 477 | |
| 478 | // Now try some more. |
| 479 | |
| 480 | // We could try to avoid std::string allocations by working on a char array directly. As we |
| 481 | // do not expect a lot of iterations, this seems too involved and brittle. |
| 482 | |
| 483 | for (size_t i = 1; ; ++i) { |
| 484 | std::string name = GetMultiDexClassesDexName(i); |
| 485 | std::string fake_location = GetMultiDexLocation(i, location.c_str()); |
| 486 | std::unique_ptr<const DexFile> next_dex_file(OpenOneDexFileFromZip(zip_archive, |
| 487 | name.c_str(), |
| 488 | fake_location, |
| 489 | verify, |
| 490 | verify_checksum, |
| 491 | error_msg, |
| 492 | &error_code)); |
| 493 | if (next_dex_file.get() == nullptr) { |
Dario Freni | e166fac | 2018-07-16 11:08:03 +0100 | [diff] [blame] | 494 | if (error_code != DexFileLoaderErrorCode::kEntryNotFound) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 495 | LOG(WARNING) << "Zip open failed: " << *error_msg; |
| 496 | } |
| 497 | break; |
| 498 | } else { |
| 499 | dex_files->push_back(std::move(next_dex_file)); |
| 500 | } |
| 501 | |
| 502 | if (i == kWarnOnManyDexFilesThreshold) { |
| 503 | LOG(WARNING) << location << " has in excess of " << kWarnOnManyDexFilesThreshold |
| 504 | << " dex files. Please consider coalescing and shrinking the number to " |
| 505 | " avoid runtime overhead."; |
| 506 | } |
| 507 | |
| 508 | if (i == std::numeric_limits<size_t>::max()) { |
| 509 | LOG(ERROR) << "Overflow in number of dex files!"; |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | return true; |
| 515 | } |
| 516 | } |
| 517 | |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 518 | std::unique_ptr<DexFile> ArtDexFileLoader::OpenCommon(const uint8_t* base, |
| 519 | size_t size, |
| 520 | const uint8_t* data_base, |
| 521 | size_t data_size, |
| 522 | const std::string& location, |
| 523 | uint32_t location_checksum, |
| 524 | const OatDexFile* oat_dex_file, |
| 525 | bool verify, |
| 526 | bool verify_checksum, |
| 527 | std::string* error_msg, |
| 528 | std::unique_ptr<DexFileContainer> container, |
| 529 | VerifyResult* verify_result) { |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame^] | 530 | return DexFileLoader::OpenCommon(base, |
| 531 | size, |
| 532 | data_base, |
| 533 | data_size, |
| 534 | location, |
| 535 | location_checksum, |
| 536 | oat_dex_file, |
| 537 | verify, |
| 538 | verify_checksum, |
| 539 | error_msg, |
| 540 | std::move(container), |
| 541 | verify_result); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 542 | } |
| 543 | |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 544 | } // namespace art |