blob: 971b99c588b5bba9005e0e307e1fd17fdbaab819 [file] [log] [blame]
David Sehr013fd802018-01-11 22:55:24 -08001/*
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 Sehr013fd802018-01-11 22:55:24 -080019#include <sys/stat.h>
20
21#include "android-base/stringprintf.h"
22
23#include "base/file_magic.h"
David Brazdil8e1a7cb2018-03-27 08:14:25 +000024#include "base/file_utils.h"
Vladimir Markoc34bebf2018-08-16 16:12:49 +010025#include "base/mem_map.h"
David Sehr10db8fe2018-07-18 11:01:20 -070026#include "base/mman.h" // For the PROT_* and MAP_* constants.
David Sehr013fd802018-01-11 22:55:24 -080027#include "base/stl_util.h"
28#include "base/systrace.h"
29#include "base/unix_file/fd_file.h"
David Sehr79e26072018-04-06 17:58:50 -070030#include "base/zip_archive.h"
David Sehr334b9d72018-02-12 18:27:56 -080031#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 Sehr013fd802018-01-11 22:55:24 -080035
36namespace art {
37
38namespace {
39
40class MemMapContainer : public DexFileContainer {
41 public:
Vladimir Markoc34bebf2018-08-16 16:12:49 +010042 explicit MemMapContainer(MemMap&& mem_map) : mem_map_(std::move(mem_map)) { }
Roland Levillainf73caca2018-08-24 17:19:07 +010043 ~MemMapContainer() override { }
David Sehr013fd802018-01-11 22:55:24 -080044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010045 int GetPermissions() override {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010046 if (!mem_map_.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -080047 return 0;
48 } else {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010049 return mem_map_.GetProtect();
David Sehr013fd802018-01-11 22:55:24 -080050 }
51 }
52
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010053 bool IsReadOnly() override {
David Sehr013fd802018-01-11 22:55:24 -080054 return GetPermissions() == PROT_READ;
55 }
56
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010057 bool EnableWrite() override {
David Sehr013fd802018-01-11 22:55:24 -080058 CHECK(IsReadOnly());
Vladimir Markoc34bebf2018-08-16 16:12:49 +010059 if (!mem_map_.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -080060 return false;
61 } else {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010062 return mem_map_.Protect(PROT_READ | PROT_WRITE);
David Sehr013fd802018-01-11 22:55:24 -080063 }
64 }
65
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010066 bool DisableWrite() override {
David Sehr013fd802018-01-11 22:55:24 -080067 CHECK(!IsReadOnly());
Vladimir Markoc34bebf2018-08-16 16:12:49 +010068 if (!mem_map_.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -080069 return false;
70 } else {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010071 return mem_map_.Protect(PROT_READ);
David Sehr013fd802018-01-11 22:55:24 -080072 }
73 }
74
75 private:
Vladimir Markoc34bebf2018-08-16 16:12:49 +010076 MemMap mem_map_;
David Sehr013fd802018-01-11 22:55:24 -080077 DISALLOW_COPY_AND_ASSIGN(MemMapContainer);
78};
79
80} // namespace
81
82using android::base::StringPrintf;
83
84static constexpr OatDexFile* kNoOatDexFile = nullptr;
85
86
87bool ArtDexFileLoader::GetMultiDexChecksums(const char* filename,
88 std::vector<uint32_t>* checksums,
Calin Juravle6e6f1b22020-12-15 19:13:19 -080089 std::vector<std::string>* dex_locations,
David Sehr013fd802018-01-11 22:55:24 -080090 std::string* error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +000091 int zip_fd,
92 bool* zip_file_only_contains_uncompressed_dex) const {
David Sehr013fd802018-01-11 22:55:24 -080093 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 Gampe0de385f2018-10-11 11:11:13 -070099 fd = File(DupCloexec(zip_fd), /* check_usage= */ false);
David Sehr013fd802018-01-11 22:55:24 -0800100 }
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 Juravle6e6f1b22020-12-15 19:13:19 -0800117 uint32_t idx = 0;
118 std::string zip_entry_name = GetMultiDexClassesDexName(idx);
David Sehr013fd802018-01-11 22:55:24 -0800119 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 Geoffray66ff8a82018-02-28 13:27:55 +0000126 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 Sehr013fd802018-01-11 22:55:24 -0800131 do {
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000132 if (zip_file_only_contains_uncompressed_dex != nullptr) {
David Sehr79e26072018-04-06 17:58:50 -0700133 if (!(zip_entry->IsUncompressed() && zip_entry->IsAlignedTo(alignof(DexFile::Header)))) {
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000134 *zip_file_only_contains_uncompressed_dex = false;
135 }
136 }
David Sehr013fd802018-01-11 22:55:24 -0800137 checksums->push_back(zip_entry->GetCrc32());
Calin Juravle6e6f1b22020-12-15 19:13:19 -0800138 dex_locations->push_back(GetMultiDexLocation(idx, filename));
139 zip_entry_name = GetMultiDexClassesDexName(++idx);
David Sehr013fd802018-01-11 22:55:24 -0800140 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 Brazdil2b9c35b2018-01-12 15:44:43 +0000145 std::unique_ptr<const DexFile> dex_file(OpenFile(fd.Release(),
146 filename,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700147 /* verify= */ false,
148 /* verify_checksum= */ false,
149 /* mmap_shared= */ false,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000150 error_msg));
David Sehr013fd802018-01-11 22:55:24 -0800151 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 Stjernholm785c9872018-11-28 00:25:18 +0000161std::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 Sehr013fd802018-01-11 22:55:24 -0800171 ScopedTrace trace(std::string("Open dex file from RAM ") + location);
David Sehr0b426772018-07-03 23:03:42 +0000172 return OpenCommon(base,
173 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700174 /*data_base=*/ nullptr,
175 /*data_size=*/ 0u,
David Sehr013fd802018-01-11 22:55:24 -0800176 location,
177 location_checksum,
178 oat_dex_file,
179 verify,
180 verify_checksum,
181 error_msg,
Martin Stjernholm785c9872018-11-28 00:25:18 +0000182 std::move(container),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700183 /*verify_result=*/ nullptr);
David Sehr013fd802018-01-11 22:55:24 -0800184}
185
186std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const std::string& location,
187 uint32_t location_checksum,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100188 MemMap&& map,
David Sehr013fd802018-01-11 22:55:24 -0800189 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 Markoc34bebf2018-08-16 16:12:49 +0100193 CHECK(map.IsValid());
David Sehr013fd802018-01-11 22:55:24 -0800194
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100195 size_t size = map.Size();
196 if (size < sizeof(DexFile::Header)) {
David Sehr013fd802018-01-11 22:55:24 -0800197 *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 Markoc34bebf2018-08-16 16:12:49 +0100203 uint8_t* begin = map.Begin();
204 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
205 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700206 /*data_base=*/ nullptr,
207 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000208 location,
209 location_checksum,
210 kNoOatDexFile,
211 verify,
212 verify_checksum,
213 error_msg,
214 std::make_unique<MemMapContainer>(std::move(map)),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700215 /*verify_result=*/ nullptr);
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700216 // 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 Sehr013fd802018-01-11 22:55:24 -0800222 return dex_file;
223}
224
225bool 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 Sehr013fd802018-01-11 22:55:24 -0800231 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 Brazdil89821862019-03-19 13:57:43 +0000237 return OpenWithMagic(
238 magic, fd.Release(), location, verify, verify_checksum, error_msg, dex_files);
239}
240
241bool 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 Juravle5ff23932020-12-11 18:26:14 -0800255bool 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 Brazdil89821862019-03-19 13:57:43 +0000267bool 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 Sehr013fd802018-01-11 22:55:24 -0800276 if (IsZipMagic(magic)) {
David Brazdil89821862019-03-19 13:57:43 +0000277 return OpenZip(fd, location, verify, verify_checksum, error_msg, dex_files);
David Sehr013fd802018-01-11 22:55:24 -0800278 }
279 if (IsMagicValid(magic)) {
David Brazdil89821862019-03-19 13:57:43 +0000280 std::unique_ptr<const DexFile> dex_file(OpenFile(fd,
David Sehr013fd802018-01-11 22:55:24 -0800281 location,
282 verify,
283 verify_checksum,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700284 /* mmap_shared= */ false,
David Sehr013fd802018-01-11 22:55:24 -0800285 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 Brazdil89821862019-03-19 13:57:43 +0000293 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", location.c_str());
David Sehr013fd802018-01-11 22:55:24 -0800294 return false;
295}
296
297std::unique_ptr<const DexFile> ArtDexFileLoader::OpenDex(int fd,
298 const std::string& location,
299 bool verify,
300 bool verify_checksum,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000301 bool mmap_shared,
David Sehr013fd802018-01-11 22:55:24 -0800302 std::string* error_msg) const {
303 ScopedTrace trace("Open dex file " + std::string(location));
David Brazdil2b9c35b2018-01-12 15:44:43 +0000304 return OpenFile(fd, location, verify, verify_checksum, mmap_shared, error_msg);
David Sehr013fd802018-01-11 22:55:24 -0800305}
306
307bool 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));
314 DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is nullptr";
315 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
316 if (zip_archive.get() == nullptr) {
317 DCHECK(!error_msg->empty());
318 return false;
319 }
320 return OpenAllDexFilesFromZip(
321 *zip_archive, location, verify, verify_checksum, error_msg, dex_files);
322}
323
324std::unique_ptr<const DexFile> ArtDexFileLoader::OpenFile(int fd,
325 const std::string& location,
326 bool verify,
327 bool verify_checksum,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000328 bool mmap_shared,
David Sehr013fd802018-01-11 22:55:24 -0800329 std::string* error_msg) const {
330 ScopedTrace trace(std::string("Open dex file ") + std::string(location));
331 CHECK(!location.empty());
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100332 MemMap map;
David Sehr013fd802018-01-11 22:55:24 -0800333 {
Andreas Gampe0de385f2018-10-11 11:11:13 -0700334 File delayed_close(fd, /* check_usage= */ false);
David Sehr013fd802018-01-11 22:55:24 -0800335 struct stat sbuf;
336 memset(&sbuf, 0, sizeof(sbuf));
337 if (fstat(fd, &sbuf) == -1) {
338 *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location.c_str(),
339 strerror(errno));
340 return nullptr;
341 }
342 if (S_ISDIR(sbuf.st_mode)) {
343 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location.c_str());
344 return nullptr;
345 }
346 size_t length = sbuf.st_size;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100347 map = MemMap::MapFile(length,
348 PROT_READ,
349 mmap_shared ? MAP_SHARED : MAP_PRIVATE,
350 fd,
351 0,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700352 /*low_4gb=*/false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100353 location.c_str(),
354 error_msg);
355 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800356 DCHECK(!error_msg->empty());
357 return nullptr;
358 }
359 }
360
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100361 const uint8_t* begin = map.Begin();
362 size_t size = map.Size();
363 if (size < sizeof(DexFile::Header)) {
David Sehr013fd802018-01-11 22:55:24 -0800364 *error_msg = StringPrintf(
365 "DexFile: failed to open dex file '%s' that is too short to have a header",
366 location.c_str());
367 return nullptr;
368 }
369
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100370 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(begin);
David Sehr013fd802018-01-11 22:55:24 -0800371
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100372 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
373 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700374 /*data_base=*/ nullptr,
375 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000376 location,
377 dex_header->checksum_,
378 kNoOatDexFile,
379 verify,
380 verify_checksum,
381 error_msg,
382 std::make_unique<MemMapContainer>(std::move(map)),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700383 /*verify_result=*/ nullptr);
David Sehr013fd802018-01-11 22:55:24 -0800384
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700385 // Opening CompactDex is only supported from vdex files.
386 if (dex_file != nullptr && dex_file->IsCompactDexFile()) {
387 *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files",
388 location.c_str());
389 return nullptr;
390 }
David Sehr013fd802018-01-11 22:55:24 -0800391 return dex_file;
392}
393
394std::unique_ptr<const DexFile> ArtDexFileLoader::OpenOneDexFileFromZip(
395 const ZipArchive& zip_archive,
396 const char* entry_name,
397 const std::string& location,
398 bool verify,
399 bool verify_checksum,
400 std::string* error_msg,
Dario Frenie166fac2018-07-16 11:08:03 +0100401 DexFileLoaderErrorCode* error_code) const {
David Sehr013fd802018-01-11 22:55:24 -0800402 ScopedTrace trace("Dex file open from Zip Archive " + std::string(location));
403 CHECK(!location.empty());
404 std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
405 if (zip_entry == nullptr) {
Dario Frenie166fac2018-07-16 11:08:03 +0100406 *error_code = DexFileLoaderErrorCode::kEntryNotFound;
David Sehr013fd802018-01-11 22:55:24 -0800407 return nullptr;
408 }
409 if (zip_entry->GetUncompressedLength() == 0) {
410 *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100411 *error_code = DexFileLoaderErrorCode::kDexFileError;
David Sehr013fd802018-01-11 22:55:24 -0800412 return nullptr;
413 }
414
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100415 MemMap map;
David Sehr013fd802018-01-11 22:55:24 -0800416 if (zip_entry->IsUncompressed()) {
417 if (!zip_entry->IsAlignedTo(alignof(DexFile::Header))) {
418 // Do not mmap unaligned ZIP entries because
419 // doing so would fail dex verification which requires 4 byte alignment.
420 LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
421 << "please zipalign to " << alignof(DexFile::Header) << " bytes. "
422 << "Falling back to extracting file.";
423 } else {
424 // Map uncompressed files within zip as file-backed to avoid a dirty copy.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100425 map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/error_msg);
426 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800427 LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
428 << "is your ZIP file corrupted? Falling back to extraction.";
429 // Try again with Extraction which still has a chance of recovery.
430 }
431 }
432 }
433
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100434 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800435 // Default path for compressed ZIP entries,
436 // and fallback for stored ZIP entries.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100437 map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg);
David Sehr013fd802018-01-11 22:55:24 -0800438 }
439
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100440 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800441 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
442 error_msg->c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100443 *error_code = DexFileLoaderErrorCode::kExtractToMemoryError;
David Sehr013fd802018-01-11 22:55:24 -0800444 return nullptr;
445 }
446 VerifyResult verify_result;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100447 uint8_t* begin = map.Begin();
448 size_t size = map.Size();
449 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
450 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700451 /*data_base=*/ nullptr,
452 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000453 location,
454 zip_entry->GetCrc32(),
455 kNoOatDexFile,
456 verify,
457 verify_checksum,
458 error_msg,
459 std::make_unique<MemMapContainer>(std::move(map)),
460 &verify_result);
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700461 if (dex_file != nullptr && dex_file->IsCompactDexFile()) {
462 *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files",
463 location.c_str());
464 return nullptr;
465 }
David Sehr013fd802018-01-11 22:55:24 -0800466 if (dex_file == nullptr) {
467 if (verify_result == VerifyResult::kVerifyNotAttempted) {
Dario Frenie166fac2018-07-16 11:08:03 +0100468 *error_code = DexFileLoaderErrorCode::kDexFileError;
David Sehr013fd802018-01-11 22:55:24 -0800469 } else {
Dario Frenie166fac2018-07-16 11:08:03 +0100470 *error_code = DexFileLoaderErrorCode::kVerifyError;
David Sehr013fd802018-01-11 22:55:24 -0800471 }
472 return nullptr;
473 }
474 if (!dex_file->DisableWrite()) {
475 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100476 *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError;
David Sehr013fd802018-01-11 22:55:24 -0800477 return nullptr;
478 }
479 CHECK(dex_file->IsReadOnly()) << location;
480 if (verify_result != VerifyResult::kVerifySucceeded) {
Dario Frenie166fac2018-07-16 11:08:03 +0100481 *error_code = DexFileLoaderErrorCode::kVerifyError;
David Sehr013fd802018-01-11 22:55:24 -0800482 return nullptr;
483 }
Dario Frenie166fac2018-07-16 11:08:03 +0100484 *error_code = DexFileLoaderErrorCode::kNoError;
David Sehr013fd802018-01-11 22:55:24 -0800485 return dex_file;
486}
487
488// Technically we do not have a limitation with respect to the number of dex files that can be in a
489// multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols
490// (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what
491// seems an excessive number.
492static constexpr size_t kWarnOnManyDexFilesThreshold = 100;
493
494bool ArtDexFileLoader::OpenAllDexFilesFromZip(
495 const ZipArchive& zip_archive,
496 const std::string& location,
497 bool verify,
498 bool verify_checksum,
499 std::string* error_msg,
500 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
501 ScopedTrace trace("Dex file open from Zip " + std::string(location));
502 DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is nullptr";
Dario Frenie166fac2018-07-16 11:08:03 +0100503 DexFileLoaderErrorCode error_code;
David Sehr013fd802018-01-11 22:55:24 -0800504 std::unique_ptr<const DexFile> dex_file(OpenOneDexFileFromZip(zip_archive,
505 kClassesDex,
506 location,
507 verify,
508 verify_checksum,
509 error_msg,
510 &error_code));
511 if (dex_file.get() == nullptr) {
512 return false;
513 } else {
514 // Had at least classes.dex.
515 dex_files->push_back(std::move(dex_file));
516
517 // Now try some more.
518
519 // We could try to avoid std::string allocations by working on a char array directly. As we
520 // do not expect a lot of iterations, this seems too involved and brittle.
521
522 for (size_t i = 1; ; ++i) {
523 std::string name = GetMultiDexClassesDexName(i);
524 std::string fake_location = GetMultiDexLocation(i, location.c_str());
525 std::unique_ptr<const DexFile> next_dex_file(OpenOneDexFileFromZip(zip_archive,
526 name.c_str(),
527 fake_location,
528 verify,
529 verify_checksum,
530 error_msg,
531 &error_code));
532 if (next_dex_file.get() == nullptr) {
Dario Frenie166fac2018-07-16 11:08:03 +0100533 if (error_code != DexFileLoaderErrorCode::kEntryNotFound) {
David Sehr013fd802018-01-11 22:55:24 -0800534 LOG(WARNING) << "Zip open failed: " << *error_msg;
535 }
536 break;
537 } else {
538 dex_files->push_back(std::move(next_dex_file));
539 }
540
541 if (i == kWarnOnManyDexFilesThreshold) {
542 LOG(WARNING) << location << " has in excess of " << kWarnOnManyDexFilesThreshold
543 << " dex files. Please consider coalescing and shrinking the number to "
544 " avoid runtime overhead.";
545 }
546
547 if (i == std::numeric_limits<size_t>::max()) {
548 LOG(ERROR) << "Overflow in number of dex files!";
549 break;
550 }
551 }
552
553 return true;
554 }
555}
556
David Sehr0b426772018-07-03 23:03:42 +0000557std::unique_ptr<DexFile> ArtDexFileLoader::OpenCommon(const uint8_t* base,
558 size_t size,
559 const uint8_t* data_base,
560 size_t data_size,
561 const std::string& location,
562 uint32_t location_checksum,
563 const OatDexFile* oat_dex_file,
564 bool verify,
565 bool verify_checksum,
566 std::string* error_msg,
567 std::unique_ptr<DexFileContainer> container,
568 VerifyResult* verify_result) {
David Brazdila5c3a802019-03-08 14:59:41 +0000569 return DexFileLoader::OpenCommon(base,
570 size,
571 data_base,
572 data_size,
573 location,
574 location_checksum,
575 oat_dex_file,
576 verify,
577 verify_checksum,
578 error_msg,
579 std::move(container),
580 verify_result);
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000581}
582
David Sehr013fd802018-01-11 22:55:24 -0800583} // namespace art