blob: e1471f126a7272eee157ba1411194312d1a79f68 [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,
89 std::string* error_msg,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +000090 int zip_fd,
91 bool* zip_file_only_contains_uncompressed_dex) const {
David Sehr013fd802018-01-11 22:55:24 -080092 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 Gampe0de385f2018-10-11 11:11:13 -070098 fd = File(DupCloexec(zip_fd), /* check_usage= */ false);
David Sehr013fd802018-01-11 22:55:24 -080099 }
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 Geoffray66ff8a82018-02-28 13:27:55 +0000125 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 Sehr013fd802018-01-11 22:55:24 -0800130 do {
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000131 if (zip_file_only_contains_uncompressed_dex != nullptr) {
David Sehr79e26072018-04-06 17:58:50 -0700132 if (!(zip_entry->IsUncompressed() && zip_entry->IsAlignedTo(alignof(DexFile::Header)))) {
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000133 *zip_file_only_contains_uncompressed_dex = false;
134 }
135 }
David Sehr013fd802018-01-11 22:55:24 -0800136 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 Brazdil2b9c35b2018-01-12 15:44:43 +0000143 std::unique_ptr<const DexFile> dex_file(OpenFile(fd.Release(),
144 filename,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700145 /* verify= */ false,
146 /* verify_checksum= */ false,
147 /* mmap_shared= */ false,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000148 error_msg));
David Sehr013fd802018-01-11 22:55:24 -0800149 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 Stjernholm785c9872018-11-28 00:25:18 +0000159std::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 Sehr013fd802018-01-11 22:55:24 -0800169 ScopedTrace trace(std::string("Open dex file from RAM ") + location);
David Sehr0b426772018-07-03 23:03:42 +0000170 return OpenCommon(base,
171 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700172 /*data_base=*/ nullptr,
173 /*data_size=*/ 0u,
David Sehr013fd802018-01-11 22:55:24 -0800174 location,
175 location_checksum,
176 oat_dex_file,
177 verify,
178 verify_checksum,
179 error_msg,
Martin Stjernholm785c9872018-11-28 00:25:18 +0000180 std::move(container),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700181 /*verify_result=*/ nullptr);
David Sehr013fd802018-01-11 22:55:24 -0800182}
183
184std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const std::string& location,
185 uint32_t location_checksum,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100186 MemMap&& map,
David Sehr013fd802018-01-11 22:55:24 -0800187 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 Markoc34bebf2018-08-16 16:12:49 +0100191 CHECK(map.IsValid());
David Sehr013fd802018-01-11 22:55:24 -0800192
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100193 size_t size = map.Size();
194 if (size < sizeof(DexFile::Header)) {
David Sehr013fd802018-01-11 22:55:24 -0800195 *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 Markoc34bebf2018-08-16 16:12:49 +0100201 uint8_t* begin = map.Begin();
202 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
203 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700204 /*data_base=*/ nullptr,
205 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000206 location,
207 location_checksum,
208 kNoOatDexFile,
209 verify,
210 verify_checksum,
211 error_msg,
212 std::make_unique<MemMapContainer>(std::move(map)),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700213 /*verify_result=*/ nullptr);
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700214 // 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 Sehr013fd802018-01-11 22:55:24 -0800220 return dex_file;
221}
222
223bool 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 Gampe0de385f2018-10-11 11:11:13 -0700245 /* mmap_shared= */ false,
David Sehr013fd802018-01-11 22:55:24 -0800246 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
258std::unique_ptr<const DexFile> ArtDexFileLoader::OpenDex(int fd,
259 const std::string& location,
260 bool verify,
261 bool verify_checksum,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000262 bool mmap_shared,
David Sehr013fd802018-01-11 22:55:24 -0800263 std::string* error_msg) const {
264 ScopedTrace trace("Open dex file " + std::string(location));
David Brazdil2b9c35b2018-01-12 15:44:43 +0000265 return OpenFile(fd, location, verify, verify_checksum, mmap_shared, error_msg);
David Sehr013fd802018-01-11 22:55:24 -0800266}
267
268bool 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
285std::unique_ptr<const DexFile> ArtDexFileLoader::OpenFile(int fd,
286 const std::string& location,
287 bool verify,
288 bool verify_checksum,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000289 bool mmap_shared,
David Sehr013fd802018-01-11 22:55:24 -0800290 std::string* error_msg) const {
291 ScopedTrace trace(std::string("Open dex file ") + std::string(location));
292 CHECK(!location.empty());
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100293 MemMap map;
David Sehr013fd802018-01-11 22:55:24 -0800294 {
Andreas Gampe0de385f2018-10-11 11:11:13 -0700295 File delayed_close(fd, /* check_usage= */ false);
David Sehr013fd802018-01-11 22:55:24 -0800296 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 Markoc34bebf2018-08-16 16:12:49 +0100308 map = MemMap::MapFile(length,
309 PROT_READ,
310 mmap_shared ? MAP_SHARED : MAP_PRIVATE,
311 fd,
312 0,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700313 /*low_4gb=*/false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100314 location.c_str(),
315 error_msg);
316 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800317 DCHECK(!error_msg->empty());
318 return nullptr;
319 }
320 }
321
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100322 const uint8_t* begin = map.Begin();
323 size_t size = map.Size();
324 if (size < sizeof(DexFile::Header)) {
David Sehr013fd802018-01-11 22:55:24 -0800325 *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 Markoc34bebf2018-08-16 16:12:49 +0100331 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(begin);
David Sehr013fd802018-01-11 22:55:24 -0800332
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100333 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
334 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700335 /*data_base=*/ nullptr,
336 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000337 location,
338 dex_header->checksum_,
339 kNoOatDexFile,
340 verify,
341 verify_checksum,
342 error_msg,
343 std::make_unique<MemMapContainer>(std::move(map)),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700344 /*verify_result=*/ nullptr);
David Sehr013fd802018-01-11 22:55:24 -0800345
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700346 // 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 Sehr013fd802018-01-11 22:55:24 -0800352 return dex_file;
353}
354
355std::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 Frenie166fac2018-07-16 11:08:03 +0100362 DexFileLoaderErrorCode* error_code) const {
David Sehr013fd802018-01-11 22:55:24 -0800363 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 Frenie166fac2018-07-16 11:08:03 +0100367 *error_code = DexFileLoaderErrorCode::kEntryNotFound;
David Sehr013fd802018-01-11 22:55:24 -0800368 return nullptr;
369 }
370 if (zip_entry->GetUncompressedLength() == 0) {
371 *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100372 *error_code = DexFileLoaderErrorCode::kDexFileError;
David Sehr013fd802018-01-11 22:55:24 -0800373 return nullptr;
374 }
375
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100376 MemMap map;
David Sehr013fd802018-01-11 22:55:24 -0800377 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 Markoc34bebf2018-08-16 16:12:49 +0100386 map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/error_msg);
387 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800388 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 Markoc34bebf2018-08-16 16:12:49 +0100395 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800396 // Default path for compressed ZIP entries,
397 // and fallback for stored ZIP entries.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100398 map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg);
David Sehr013fd802018-01-11 22:55:24 -0800399 }
400
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100401 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800402 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
403 error_msg->c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100404 *error_code = DexFileLoaderErrorCode::kExtractToMemoryError;
David Sehr013fd802018-01-11 22:55:24 -0800405 return nullptr;
406 }
407 VerifyResult verify_result;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100408 uint8_t* begin = map.Begin();
409 size_t size = map.Size();
410 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
411 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700412 /*data_base=*/ nullptr,
413 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000414 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 Chartier14e7bad2018-03-22 14:33:20 -0700422 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 Sehr013fd802018-01-11 22:55:24 -0800427 if (dex_file == nullptr) {
428 if (verify_result == VerifyResult::kVerifyNotAttempted) {
Dario Frenie166fac2018-07-16 11:08:03 +0100429 *error_code = DexFileLoaderErrorCode::kDexFileError;
David Sehr013fd802018-01-11 22:55:24 -0800430 } else {
Dario Frenie166fac2018-07-16 11:08:03 +0100431 *error_code = DexFileLoaderErrorCode::kVerifyError;
David Sehr013fd802018-01-11 22:55:24 -0800432 }
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 Frenie166fac2018-07-16 11:08:03 +0100437 *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError;
David Sehr013fd802018-01-11 22:55:24 -0800438 return nullptr;
439 }
440 CHECK(dex_file->IsReadOnly()) << location;
441 if (verify_result != VerifyResult::kVerifySucceeded) {
Dario Frenie166fac2018-07-16 11:08:03 +0100442 *error_code = DexFileLoaderErrorCode::kVerifyError;
David Sehr013fd802018-01-11 22:55:24 -0800443 return nullptr;
444 }
Dario Frenie166fac2018-07-16 11:08:03 +0100445 *error_code = DexFileLoaderErrorCode::kNoError;
David Sehr013fd802018-01-11 22:55:24 -0800446 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.
453static constexpr size_t kWarnOnManyDexFilesThreshold = 100;
454
455bool 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 Frenie166fac2018-07-16 11:08:03 +0100464 DexFileLoaderErrorCode error_code;
David Sehr013fd802018-01-11 22:55:24 -0800465 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 Frenie166fac2018-07-16 11:08:03 +0100494 if (error_code != DexFileLoaderErrorCode::kEntryNotFound) {
David Sehr013fd802018-01-11 22:55:24 -0800495 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 Sehr0b426772018-07-03 23:03:42 +0000518std::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 Brazdila5c3a802019-03-08 14:59:41 +0000530 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 Brazdil8e1a7cb2018-03-27 08:14:25 +0000542}
543
David Sehr013fd802018-01-11 22:55:24 -0800544} // namespace art