blob: 20a519bf99569ac908ac920b741dac08682987a7 [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
19#include <sys/mman.h> // For the PROT_* and MAP_* constants.
20#include <sys/stat.h>
21
22#include "android-base/stringprintf.h"
23
24#include "base/file_magic.h"
David Brazdil8e1a7cb2018-03-27 08:14:25 +000025#include "base/file_utils.h"
Vladimir Markoc34bebf2018-08-16 16:12:49 +010026#include "base/mem_map.h"
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
159std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const uint8_t* base,
160 size_t size,
161 const std::string& location,
162 uint32_t location_checksum,
163 const OatDexFile* oat_dex_file,
164 bool verify,
165 bool verify_checksum,
166 std::string* error_msg) const {
167 ScopedTrace trace(std::string("Open dex file from RAM ") + location);
David Sehr0b426772018-07-03 23:03:42 +0000168 return OpenCommon(base,
169 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700170 /*data_base=*/ nullptr,
171 /*data_size=*/ 0u,
David Sehr013fd802018-01-11 22:55:24 -0800172 location,
173 location_checksum,
174 oat_dex_file,
175 verify,
176 verify_checksum,
177 error_msg,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700178 /*container=*/ nullptr,
179 /*verify_result=*/ nullptr);
David Sehr013fd802018-01-11 22:55:24 -0800180}
181
182std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const std::string& location,
183 uint32_t location_checksum,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100184 MemMap&& map,
David Sehr013fd802018-01-11 22:55:24 -0800185 bool verify,
186 bool verify_checksum,
187 std::string* error_msg) const {
188 ScopedTrace trace(std::string("Open dex file from mapped-memory ") + location);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100189 CHECK(map.IsValid());
David Sehr013fd802018-01-11 22:55:24 -0800190
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100191 size_t size = map.Size();
192 if (size < sizeof(DexFile::Header)) {
David Sehr013fd802018-01-11 22:55:24 -0800193 *error_msg = StringPrintf(
194 "DexFile: failed to open dex file '%s' that is too short to have a header",
195 location.c_str());
196 return nullptr;
197 }
198
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100199 uint8_t* begin = map.Begin();
200 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
201 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700202 /*data_base=*/ nullptr,
203 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000204 location,
205 location_checksum,
206 kNoOatDexFile,
207 verify,
208 verify_checksum,
209 error_msg,
210 std::make_unique<MemMapContainer>(std::move(map)),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700211 /*verify_result=*/ nullptr);
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700212 // Opening CompactDex is only supported from vdex files.
213 if (dex_file != nullptr && dex_file->IsCompactDexFile()) {
214 *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files",
215 location.c_str());
216 return nullptr;
217 }
David Sehr013fd802018-01-11 22:55:24 -0800218 return dex_file;
219}
220
221bool ArtDexFileLoader::Open(const char* filename,
222 const std::string& location,
223 bool verify,
224 bool verify_checksum,
225 std::string* error_msg,
226 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
227 ScopedTrace trace(std::string("Open dex file ") + std::string(location));
228 DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr";
229 uint32_t magic;
230 File fd = OpenAndReadMagic(filename, &magic, error_msg);
231 if (fd.Fd() == -1) {
232 DCHECK(!error_msg->empty());
233 return false;
234 }
235 if (IsZipMagic(magic)) {
236 return OpenZip(fd.Release(), location, verify, verify_checksum, error_msg, dex_files);
237 }
238 if (IsMagicValid(magic)) {
239 std::unique_ptr<const DexFile> dex_file(OpenFile(fd.Release(),
240 location,
241 verify,
242 verify_checksum,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700243 /* mmap_shared= */ false,
David Sehr013fd802018-01-11 22:55:24 -0800244 error_msg));
245 if (dex_file.get() != nullptr) {
246 dex_files->push_back(std::move(dex_file));
247 return true;
248 } else {
249 return false;
250 }
251 }
252 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
253 return false;
254}
255
256std::unique_ptr<const DexFile> ArtDexFileLoader::OpenDex(int fd,
257 const std::string& location,
258 bool verify,
259 bool verify_checksum,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000260 bool mmap_shared,
David Sehr013fd802018-01-11 22:55:24 -0800261 std::string* error_msg) const {
262 ScopedTrace trace("Open dex file " + std::string(location));
David Brazdil2b9c35b2018-01-12 15:44:43 +0000263 return OpenFile(fd, location, verify, verify_checksum, mmap_shared, error_msg);
David Sehr013fd802018-01-11 22:55:24 -0800264}
265
266bool ArtDexFileLoader::OpenZip(int fd,
267 const std::string& location,
268 bool verify,
269 bool verify_checksum,
270 std::string* error_msg,
271 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
272 ScopedTrace trace("Dex file open Zip " + std::string(location));
273 DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is nullptr";
274 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
275 if (zip_archive.get() == nullptr) {
276 DCHECK(!error_msg->empty());
277 return false;
278 }
279 return OpenAllDexFilesFromZip(
280 *zip_archive, location, verify, verify_checksum, error_msg, dex_files);
281}
282
283std::unique_ptr<const DexFile> ArtDexFileLoader::OpenFile(int fd,
284 const std::string& location,
285 bool verify,
286 bool verify_checksum,
David Brazdil2b9c35b2018-01-12 15:44:43 +0000287 bool mmap_shared,
David Sehr013fd802018-01-11 22:55:24 -0800288 std::string* error_msg) const {
289 ScopedTrace trace(std::string("Open dex file ") + std::string(location));
290 CHECK(!location.empty());
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100291 MemMap map;
David Sehr013fd802018-01-11 22:55:24 -0800292 {
Andreas Gampe0de385f2018-10-11 11:11:13 -0700293 File delayed_close(fd, /* check_usage= */ false);
David Sehr013fd802018-01-11 22:55:24 -0800294 struct stat sbuf;
295 memset(&sbuf, 0, sizeof(sbuf));
296 if (fstat(fd, &sbuf) == -1) {
297 *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location.c_str(),
298 strerror(errno));
299 return nullptr;
300 }
301 if (S_ISDIR(sbuf.st_mode)) {
302 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location.c_str());
303 return nullptr;
304 }
305 size_t length = sbuf.st_size;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100306 map = MemMap::MapFile(length,
307 PROT_READ,
308 mmap_shared ? MAP_SHARED : MAP_PRIVATE,
309 fd,
310 0,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700311 /*low_4gb=*/false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100312 location.c_str(),
313 error_msg);
314 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800315 DCHECK(!error_msg->empty());
316 return nullptr;
317 }
318 }
319
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100320 const uint8_t* begin = map.Begin();
321 size_t size = map.Size();
322 if (size < sizeof(DexFile::Header)) {
David Sehr013fd802018-01-11 22:55:24 -0800323 *error_msg = StringPrintf(
324 "DexFile: failed to open dex file '%s' that is too short to have a header",
325 location.c_str());
326 return nullptr;
327 }
328
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100329 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(begin);
David Sehr013fd802018-01-11 22:55:24 -0800330
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100331 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
332 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700333 /*data_base=*/ nullptr,
334 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000335 location,
336 dex_header->checksum_,
337 kNoOatDexFile,
338 verify,
339 verify_checksum,
340 error_msg,
341 std::make_unique<MemMapContainer>(std::move(map)),
Andreas Gampe0de385f2018-10-11 11:11:13 -0700342 /*verify_result=*/ nullptr);
David Sehr013fd802018-01-11 22:55:24 -0800343
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700344 // Opening CompactDex is only supported from vdex files.
345 if (dex_file != nullptr && dex_file->IsCompactDexFile()) {
346 *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files",
347 location.c_str());
348 return nullptr;
349 }
David Sehr013fd802018-01-11 22:55:24 -0800350 return dex_file;
351}
352
353std::unique_ptr<const DexFile> ArtDexFileLoader::OpenOneDexFileFromZip(
354 const ZipArchive& zip_archive,
355 const char* entry_name,
356 const std::string& location,
357 bool verify,
358 bool verify_checksum,
359 std::string* error_msg,
Dario Frenie166fac2018-07-16 11:08:03 +0100360 DexFileLoaderErrorCode* error_code) const {
David Sehr013fd802018-01-11 22:55:24 -0800361 ScopedTrace trace("Dex file open from Zip Archive " + std::string(location));
362 CHECK(!location.empty());
363 std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
364 if (zip_entry == nullptr) {
Dario Frenie166fac2018-07-16 11:08:03 +0100365 *error_code = DexFileLoaderErrorCode::kEntryNotFound;
David Sehr013fd802018-01-11 22:55:24 -0800366 return nullptr;
367 }
368 if (zip_entry->GetUncompressedLength() == 0) {
369 *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100370 *error_code = DexFileLoaderErrorCode::kDexFileError;
David Sehr013fd802018-01-11 22:55:24 -0800371 return nullptr;
372 }
373
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100374 MemMap map;
David Sehr013fd802018-01-11 22:55:24 -0800375 if (zip_entry->IsUncompressed()) {
376 if (!zip_entry->IsAlignedTo(alignof(DexFile::Header))) {
377 // Do not mmap unaligned ZIP entries because
378 // doing so would fail dex verification which requires 4 byte alignment.
379 LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
380 << "please zipalign to " << alignof(DexFile::Header) << " bytes. "
381 << "Falling back to extracting file.";
382 } else {
383 // Map uncompressed files within zip as file-backed to avoid a dirty copy.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100384 map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/error_msg);
385 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800386 LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
387 << "is your ZIP file corrupted? Falling back to extraction.";
388 // Try again with Extraction which still has a chance of recovery.
389 }
390 }
391 }
392
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100393 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800394 // Default path for compressed ZIP entries,
395 // and fallback for stored ZIP entries.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100396 map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg);
David Sehr013fd802018-01-11 22:55:24 -0800397 }
398
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100399 if (!map.IsValid()) {
David Sehr013fd802018-01-11 22:55:24 -0800400 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
401 error_msg->c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100402 *error_code = DexFileLoaderErrorCode::kExtractToMemoryError;
David Sehr013fd802018-01-11 22:55:24 -0800403 return nullptr;
404 }
405 VerifyResult verify_result;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100406 uint8_t* begin = map.Begin();
407 size_t size = map.Size();
408 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
409 size,
Andreas Gampe0de385f2018-10-11 11:11:13 -0700410 /*data_base=*/ nullptr,
411 /*data_size=*/ 0u,
David Sehr0b426772018-07-03 23:03:42 +0000412 location,
413 zip_entry->GetCrc32(),
414 kNoOatDexFile,
415 verify,
416 verify_checksum,
417 error_msg,
418 std::make_unique<MemMapContainer>(std::move(map)),
419 &verify_result);
Mathieu Chartier14e7bad2018-03-22 14:33:20 -0700420 if (dex_file != nullptr && dex_file->IsCompactDexFile()) {
421 *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files",
422 location.c_str());
423 return nullptr;
424 }
David Sehr013fd802018-01-11 22:55:24 -0800425 if (dex_file == nullptr) {
426 if (verify_result == VerifyResult::kVerifyNotAttempted) {
Dario Frenie166fac2018-07-16 11:08:03 +0100427 *error_code = DexFileLoaderErrorCode::kDexFileError;
David Sehr013fd802018-01-11 22:55:24 -0800428 } else {
Dario Frenie166fac2018-07-16 11:08:03 +0100429 *error_code = DexFileLoaderErrorCode::kVerifyError;
David Sehr013fd802018-01-11 22:55:24 -0800430 }
431 return nullptr;
432 }
433 if (!dex_file->DisableWrite()) {
434 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100435 *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError;
David Sehr013fd802018-01-11 22:55:24 -0800436 return nullptr;
437 }
438 CHECK(dex_file->IsReadOnly()) << location;
439 if (verify_result != VerifyResult::kVerifySucceeded) {
Dario Frenie166fac2018-07-16 11:08:03 +0100440 *error_code = DexFileLoaderErrorCode::kVerifyError;
David Sehr013fd802018-01-11 22:55:24 -0800441 return nullptr;
442 }
Dario Frenie166fac2018-07-16 11:08:03 +0100443 *error_code = DexFileLoaderErrorCode::kNoError;
David Sehr013fd802018-01-11 22:55:24 -0800444 return dex_file;
445}
446
447// Technically we do not have a limitation with respect to the number of dex files that can be in a
448// multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols
449// (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what
450// seems an excessive number.
451static constexpr size_t kWarnOnManyDexFilesThreshold = 100;
452
453bool ArtDexFileLoader::OpenAllDexFilesFromZip(
454 const ZipArchive& zip_archive,
455 const std::string& location,
456 bool verify,
457 bool verify_checksum,
458 std::string* error_msg,
459 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
460 ScopedTrace trace("Dex file open from Zip " + std::string(location));
461 DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is nullptr";
Dario Frenie166fac2018-07-16 11:08:03 +0100462 DexFileLoaderErrorCode error_code;
David Sehr013fd802018-01-11 22:55:24 -0800463 std::unique_ptr<const DexFile> dex_file(OpenOneDexFileFromZip(zip_archive,
464 kClassesDex,
465 location,
466 verify,
467 verify_checksum,
468 error_msg,
469 &error_code));
470 if (dex_file.get() == nullptr) {
471 return false;
472 } else {
473 // Had at least classes.dex.
474 dex_files->push_back(std::move(dex_file));
475
476 // Now try some more.
477
478 // We could try to avoid std::string allocations by working on a char array directly. As we
479 // do not expect a lot of iterations, this seems too involved and brittle.
480
481 for (size_t i = 1; ; ++i) {
482 std::string name = GetMultiDexClassesDexName(i);
483 std::string fake_location = GetMultiDexLocation(i, location.c_str());
484 std::unique_ptr<const DexFile> next_dex_file(OpenOneDexFileFromZip(zip_archive,
485 name.c_str(),
486 fake_location,
487 verify,
488 verify_checksum,
489 error_msg,
490 &error_code));
491 if (next_dex_file.get() == nullptr) {
Dario Frenie166fac2018-07-16 11:08:03 +0100492 if (error_code != DexFileLoaderErrorCode::kEntryNotFound) {
David Sehr013fd802018-01-11 22:55:24 -0800493 LOG(WARNING) << "Zip open failed: " << *error_msg;
494 }
495 break;
496 } else {
497 dex_files->push_back(std::move(next_dex_file));
498 }
499
500 if (i == kWarnOnManyDexFilesThreshold) {
501 LOG(WARNING) << location << " has in excess of " << kWarnOnManyDexFilesThreshold
502 << " dex files. Please consider coalescing and shrinking the number to "
503 " avoid runtime overhead.";
504 }
505
506 if (i == std::numeric_limits<size_t>::max()) {
507 LOG(ERROR) << "Overflow in number of dex files!";
508 break;
509 }
510 }
511
512 return true;
513 }
514}
515
David Sehr0b426772018-07-03 23:03:42 +0000516std::unique_ptr<DexFile> ArtDexFileLoader::OpenCommon(const uint8_t* base,
517 size_t size,
518 const uint8_t* data_base,
519 size_t data_size,
520 const std::string& location,
521 uint32_t location_checksum,
522 const OatDexFile* oat_dex_file,
523 bool verify,
524 bool verify_checksum,
525 std::string* error_msg,
526 std::unique_ptr<DexFileContainer> container,
527 VerifyResult* verify_result) {
528 std::unique_ptr<DexFile> dex_file = DexFileLoader::OpenCommon(base,
529 size,
530 data_base,
531 data_size,
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000532 location,
533 location_checksum,
534 oat_dex_file,
535 verify,
536 verify_checksum,
537 error_msg,
David Sehr0b426772018-07-03 23:03:42 +0000538 std::move(container),
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000539 verify_result);
540
541 // Check if this dex file is located in the framework directory.
542 // If it is, set a flag on the dex file. This is used by hidden API
543 // policy decision logic.
Nicolas Geoffray0d0f3162018-05-10 12:55:40 +0100544 // Location can contain multidex suffix, so fetch its canonical version. Note
545 // that this will call `realpath`.
546 std::string path = DexFileLoader::GetDexCanonicalLocation(location.c_str());
547 if (dex_file != nullptr && LocationIsOnSystemFramework(path.c_str())) {
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000548 dex_file->SetIsPlatformDexFile();
549 }
550
551 return dex_file;
552}
553
David Sehr013fd802018-01-11 22:55:24 -0800554} // namespace art