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
19 #include <sys/stat.h>
20
21 #include "android-base/stringprintf.h"
22
23 #include "base/file_magic.h"
24 #include "base/file_utils.h"
25 #include "base/mem_map.h"
26 #include "base/mman.h" // For the PROT_* and MAP_* constants.
27 #include "base/stl_util.h"
28 #include "base/systrace.h"
29 #include "base/unix_file/fd_file.h"
30 #include "base/zip_archive.h"
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"
35
36 namespace art {
37
38 namespace {
39
40 class MemMapContainer : public DexFileContainer {
41 public:
MemMapContainer(MemMap && mem_map)42 explicit MemMapContainer(MemMap&& mem_map) : mem_map_(std::move(mem_map)) { }
~MemMapContainer()43 ~MemMapContainer() override { }
44
GetPermissions()45 int GetPermissions() override {
46 if (!mem_map_.IsValid()) {
47 return 0;
48 } else {
49 return mem_map_.GetProtect();
50 }
51 }
52
IsReadOnly()53 bool IsReadOnly() override {
54 return GetPermissions() == PROT_READ;
55 }
56
EnableWrite()57 bool EnableWrite() override {
58 CHECK(IsReadOnly());
59 if (!mem_map_.IsValid()) {
60 return false;
61 } else {
62 return mem_map_.Protect(PROT_READ | PROT_WRITE);
63 }
64 }
65
DisableWrite()66 bool DisableWrite() override {
67 CHECK(!IsReadOnly());
68 if (!mem_map_.IsValid()) {
69 return false;
70 } else {
71 return mem_map_.Protect(PROT_READ);
72 }
73 }
74
75 private:
76 MemMap mem_map_;
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
GetMultiDexChecksums(const char * filename,std::vector<uint32_t> * checksums,std::vector<std::string> * dex_locations,std::string * error_msg,int zip_fd,bool * zip_file_only_contains_uncompressed_dex) const87 bool ArtDexFileLoader::GetMultiDexChecksums(const char* filename,
88 std::vector<uint32_t>* checksums,
89 std::vector<std::string>* dex_locations,
90 std::string* error_msg,
91 int zip_fd,
92 bool* zip_file_only_contains_uncompressed_dex) const {
93 CHECK(checksums != nullptr);
94 uint32_t magic;
95
96 File fd;
97 if (zip_fd != -1) {
98 if (ReadMagicAndReset(zip_fd, &magic, error_msg)) {
99 fd = File(DupCloexec(zip_fd), /* check_usage= */ false);
100 }
101 } else {
102 fd = OpenAndReadMagic(filename, &magic, error_msg);
103 }
104 if (fd.Fd() == -1) {
105 DCHECK(!error_msg->empty());
106 return false;
107 }
108 if (IsZipMagic(magic)) {
109 std::unique_ptr<ZipArchive> zip_archive(
110 ZipArchive::OpenFromFd(fd.Release(), filename, error_msg));
111 if (zip_archive.get() == nullptr) {
112 *error_msg = StringPrintf("Failed to open zip archive '%s' (error msg: %s)", filename,
113 error_msg->c_str());
114 return false;
115 }
116
117 uint32_t idx = 0;
118 std::string zip_entry_name = GetMultiDexClassesDexName(idx);
119 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name.c_str(), error_msg));
120 if (zip_entry.get() == nullptr) {
121 *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", filename,
122 zip_entry_name.c_str(), error_msg->c_str());
123 return false;
124 }
125
126 if (zip_file_only_contains_uncompressed_dex != nullptr) {
127 // Start by assuming everything is uncompressed.
128 *zip_file_only_contains_uncompressed_dex = true;
129 }
130
131 do {
132 if (zip_file_only_contains_uncompressed_dex != nullptr) {
133 if (!(zip_entry->IsUncompressed() && zip_entry->IsAlignedTo(alignof(DexFile::Header)))) {
134 *zip_file_only_contains_uncompressed_dex = false;
135 }
136 }
137 checksums->push_back(zip_entry->GetCrc32());
138 dex_locations->push_back(GetMultiDexLocation(idx, filename));
139 zip_entry_name = GetMultiDexClassesDexName(++idx);
140 zip_entry.reset(zip_archive->Find(zip_entry_name.c_str(), error_msg));
141 } while (zip_entry.get() != nullptr);
142 return true;
143 }
144 if (IsMagicValid(magic)) {
145 std::unique_ptr<const DexFile> dex_file(OpenFile(fd.Release(),
146 filename,
147 /* verify= */ false,
148 /* verify_checksum= */ false,
149 /* mmap_shared= */ false,
150 error_msg));
151 if (dex_file == nullptr) {
152 return false;
153 }
154 checksums->push_back(dex_file->GetHeader().checksum_);
155 return true;
156 }
157 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
158 return false;
159 }
160
Open(const uint8_t * base,size_t size,const std::string & location,uint32_t location_checksum,const OatDexFile * oat_dex_file,bool verify,bool verify_checksum,std::string * error_msg,std::unique_ptr<DexFileContainer> container) const161 std::unique_ptr<const DexFile> ArtDexFileLoader::Open(
162 const uint8_t* base,
163 size_t size,
164 const std::string& location,
165 uint32_t location_checksum,
166 const OatDexFile* oat_dex_file,
167 bool verify,
168 bool verify_checksum,
169 std::string* error_msg,
170 std::unique_ptr<DexFileContainer> container) const {
171 ScopedTrace trace(std::string("Open dex file from RAM ") + location);
172 return OpenCommon(base,
173 size,
174 /*data_base=*/ nullptr,
175 /*data_size=*/ 0u,
176 location,
177 location_checksum,
178 oat_dex_file,
179 verify,
180 verify_checksum,
181 error_msg,
182 std::move(container),
183 /*verify_result=*/ nullptr);
184 }
185
Open(const std::string & location,uint32_t location_checksum,MemMap && map,bool verify,bool verify_checksum,std::string * error_msg) const186 std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const std::string& location,
187 uint32_t location_checksum,
188 MemMap&& map,
189 bool verify,
190 bool verify_checksum,
191 std::string* error_msg) const {
192 ScopedTrace trace(std::string("Open dex file from mapped-memory ") + location);
193 CHECK(map.IsValid());
194
195 size_t size = map.Size();
196 if (size < sizeof(DexFile::Header)) {
197 *error_msg = StringPrintf(
198 "DexFile: failed to open dex file '%s' that is too short to have a header",
199 location.c_str());
200 return nullptr;
201 }
202
203 uint8_t* begin = map.Begin();
204 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
205 size,
206 /*data_base=*/ nullptr,
207 /*data_size=*/ 0u,
208 location,
209 location_checksum,
210 kNoOatDexFile,
211 verify,
212 verify_checksum,
213 error_msg,
214 std::make_unique<MemMapContainer>(std::move(map)),
215 /*verify_result=*/ nullptr);
216 // Opening CompactDex is only supported from vdex files.
217 if (dex_file != nullptr && dex_file->IsCompactDexFile()) {
218 *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files",
219 location.c_str());
220 return nullptr;
221 }
222 return dex_file;
223 }
224
Open(const char * filename,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const225 bool ArtDexFileLoader::Open(const char* filename,
226 const std::string& location,
227 bool verify,
228 bool verify_checksum,
229 std::string* error_msg,
230 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
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 return OpenWithMagic(
238 magic, fd.Release(), location, verify, verify_checksum, error_msg, dex_files);
239 }
240
Open(int fd,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const241 bool ArtDexFileLoader::Open(int fd,
242 const std::string& location,
243 bool verify,
244 bool verify_checksum,
245 std::string* error_msg,
246 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
247 uint32_t magic;
248 if (!ReadMagicAndReset(fd, &magic, error_msg)) {
249 DCHECK(!error_msg->empty());
250 return false;
251 }
252 return OpenWithMagic(magic, fd, location, verify, verify_checksum, error_msg, dex_files);
253 }
254
Open(const char * filename,int fd,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const255 bool ArtDexFileLoader::Open(const char* filename,
256 int fd,
257 const std::string& location,
258 bool verify,
259 bool verify_checksum,
260 std::string* error_msg,
261 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
262 return fd == -1
263 ? Open(filename, location, verify, verify_checksum, error_msg, dex_files)
264 : Open(fd, location, verify, verify_checksum, error_msg, dex_files);
265 }
266
OpenWithMagic(uint32_t magic,int fd,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const267 bool ArtDexFileLoader::OpenWithMagic(uint32_t magic,
268 int fd,
269 const std::string& location,
270 bool verify,
271 bool verify_checksum,
272 std::string* error_msg,
273 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
274 ScopedTrace trace(std::string("Open dex file ") + std::string(location));
275 DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr";
276 if (IsZipMagic(magic)) {
277 return OpenZip(fd, location, verify, verify_checksum, error_msg, dex_files);
278 }
279 if (IsMagicValid(magic)) {
280 std::unique_ptr<const DexFile> dex_file(OpenFile(fd,
281 location,
282 verify,
283 verify_checksum,
284 /* mmap_shared= */ false,
285 error_msg));
286 if (dex_file.get() != nullptr) {
287 dex_files->push_back(std::move(dex_file));
288 return true;
289 } else {
290 return false;
291 }
292 }
293 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", location.c_str());
294 return false;
295 }
296
OpenDex(int fd,const std::string & location,bool verify,bool verify_checksum,bool mmap_shared,std::string * error_msg) const297 std::unique_ptr<const DexFile> ArtDexFileLoader::OpenDex(int fd,
298 const std::string& location,
299 bool verify,
300 bool verify_checksum,
301 bool mmap_shared,
302 std::string* error_msg) const {
303 ScopedTrace trace("Open dex file " + std::string(location));
304 return OpenFile(fd, location, verify, verify_checksum, mmap_shared, error_msg);
305 }
306
OpenZip(int fd,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const307 bool ArtDexFileLoader::OpenZip(int fd,
308 const std::string& location,
309 bool verify,
310 bool verify_checksum,
311 std::string* error_msg,
312 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
313 ScopedTrace trace("Dex file open Zip " + std::string(location));
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
OpenFile(int fd,const std::string & location,bool verify,bool verify_checksum,bool mmap_shared,std::string * error_msg) const324 std::unique_ptr<const DexFile> ArtDexFileLoader::OpenFile(int fd,
325 const std::string& location,
326 bool verify,
327 bool verify_checksum,
328 bool mmap_shared,
329 std::string* error_msg) const {
330 ScopedTrace trace(std::string("Open dex file ") + std::string(location));
331 CHECK(!location.empty());
332 MemMap map;
333 {
334 File delayed_close(fd, /* check_usage= */ false);
335 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;
347 map = MemMap::MapFile(length,
348 PROT_READ,
349 mmap_shared ? MAP_SHARED : MAP_PRIVATE,
350 fd,
351 0,
352 /*low_4gb=*/false,
353 location.c_str(),
354 error_msg);
355 if (!map.IsValid()) {
356 DCHECK(!error_msg->empty());
357 return nullptr;
358 }
359 }
360
361 const uint8_t* begin = map.Begin();
362 size_t size = map.Size();
363 if (size < sizeof(DexFile::Header)) {
364 *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
370 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(begin);
371
372 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
373 size,
374 /*data_base=*/ nullptr,
375 /*data_size=*/ 0u,
376 location,
377 dex_header->checksum_,
378 kNoOatDexFile,
379 verify,
380 verify_checksum,
381 error_msg,
382 std::make_unique<MemMapContainer>(std::move(map)),
383 /*verify_result=*/ nullptr);
384
385 // 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 }
391 return dex_file;
392 }
393
OpenOneDexFileFromZip(const ZipArchive & zip_archive,const char * entry_name,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,DexFileLoaderErrorCode * error_code) const394 std::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,
401 DexFileLoaderErrorCode* error_code) const {
402 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) {
406 *error_code = DexFileLoaderErrorCode::kEntryNotFound;
407 return nullptr;
408 }
409 if (zip_entry->GetUncompressedLength() == 0) {
410 *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str());
411 *error_code = DexFileLoaderErrorCode::kDexFileError;
412 return nullptr;
413 }
414
415 MemMap map;
416 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.
425 map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/error_msg);
426 if (!map.IsValid()) {
427 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
434 ScopedTrace map_extract_trace(StringPrintf("Mapped=%s Extracted=%s",
435 map.IsValid() ? "true" : "false",
436 map.IsValid() ? "false" : "true")); // this is redundant but much easier to read in traces.
437
438 if (!map.IsValid()) {
439 // Default path for compressed ZIP entries,
440 // and fallback for stored ZIP entries.
441 map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg);
442 }
443
444 if (!map.IsValid()) {
445 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
446 error_msg->c_str());
447 *error_code = DexFileLoaderErrorCode::kExtractToMemoryError;
448 return nullptr;
449 }
450 VerifyResult verify_result;
451 uint8_t* begin = map.Begin();
452 size_t size = map.Size();
453 std::unique_ptr<DexFile> dex_file = OpenCommon(begin,
454 size,
455 /*data_base=*/ nullptr,
456 /*data_size=*/ 0u,
457 location,
458 zip_entry->GetCrc32(),
459 kNoOatDexFile,
460 verify,
461 verify_checksum,
462 error_msg,
463 std::make_unique<MemMapContainer>(std::move(map)),
464 &verify_result);
465 if (dex_file != nullptr && dex_file->IsCompactDexFile()) {
466 *error_msg = StringPrintf("Opening CompactDex file '%s' is only supported from vdex files",
467 location.c_str());
468 return nullptr;
469 }
470 if (dex_file == nullptr) {
471 if (verify_result == VerifyResult::kVerifyNotAttempted) {
472 *error_code = DexFileLoaderErrorCode::kDexFileError;
473 } else {
474 *error_code = DexFileLoaderErrorCode::kVerifyError;
475 }
476 return nullptr;
477 }
478 if (!dex_file->DisableWrite()) {
479 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
480 *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError;
481 return nullptr;
482 }
483 CHECK(dex_file->IsReadOnly()) << location;
484 if (verify_result != VerifyResult::kVerifySucceeded) {
485 *error_code = DexFileLoaderErrorCode::kVerifyError;
486 return nullptr;
487 }
488 *error_code = DexFileLoaderErrorCode::kNoError;
489 return dex_file;
490 }
491
492 // Technically we do not have a limitation with respect to the number of dex files that can be in a
493 // multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols
494 // (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what
495 // seems an excessive number.
496 static constexpr size_t kWarnOnManyDexFilesThreshold = 100;
497
OpenAllDexFilesFromZip(const ZipArchive & zip_archive,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const498 bool ArtDexFileLoader::OpenAllDexFilesFromZip(
499 const ZipArchive& zip_archive,
500 const std::string& location,
501 bool verify,
502 bool verify_checksum,
503 std::string* error_msg,
504 std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
505 ScopedTrace trace("Dex file open from Zip " + std::string(location));
506 DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is nullptr";
507 DexFileLoaderErrorCode error_code;
508 std::unique_ptr<const DexFile> dex_file(OpenOneDexFileFromZip(zip_archive,
509 kClassesDex,
510 location,
511 verify,
512 verify_checksum,
513 error_msg,
514 &error_code));
515 if (dex_file.get() == nullptr) {
516 return false;
517 } else {
518 // Had at least classes.dex.
519 dex_files->push_back(std::move(dex_file));
520
521 // Now try some more.
522
523 // We could try to avoid std::string allocations by working on a char array directly. As we
524 // do not expect a lot of iterations, this seems too involved and brittle.
525
526 for (size_t i = 1; ; ++i) {
527 std::string name = GetMultiDexClassesDexName(i);
528 std::string fake_location = GetMultiDexLocation(i, location.c_str());
529 std::unique_ptr<const DexFile> next_dex_file(OpenOneDexFileFromZip(zip_archive,
530 name.c_str(),
531 fake_location,
532 verify,
533 verify_checksum,
534 error_msg,
535 &error_code));
536 if (next_dex_file.get() == nullptr) {
537 if (error_code != DexFileLoaderErrorCode::kEntryNotFound) {
538 LOG(WARNING) << "Zip open failed: " << *error_msg;
539 }
540 break;
541 } else {
542 dex_files->push_back(std::move(next_dex_file));
543 }
544
545 if (i == kWarnOnManyDexFilesThreshold) {
546 LOG(WARNING) << location << " has in excess of " << kWarnOnManyDexFilesThreshold
547 << " dex files. Please consider coalescing and shrinking the number to "
548 " avoid runtime overhead.";
549 }
550
551 if (i == std::numeric_limits<size_t>::max()) {
552 LOG(ERROR) << "Overflow in number of dex files!";
553 break;
554 }
555 }
556
557 return true;
558 }
559 }
560
OpenCommon(const uint8_t * base,size_t size,const uint8_t * data_base,size_t data_size,const std::string & location,uint32_t location_checksum,const OatDexFile * oat_dex_file,bool verify,bool verify_checksum,std::string * error_msg,std::unique_ptr<DexFileContainer> container,VerifyResult * verify_result)561 std::unique_ptr<DexFile> ArtDexFileLoader::OpenCommon(const uint8_t* base,
562 size_t size,
563 const uint8_t* data_base,
564 size_t data_size,
565 const std::string& location,
566 uint32_t location_checksum,
567 const OatDexFile* oat_dex_file,
568 bool verify,
569 bool verify_checksum,
570 std::string* error_msg,
571 std::unique_ptr<DexFileContainer> container,
572 VerifyResult* verify_result) {
573 return DexFileLoader::OpenCommon(base,
574 size,
575 data_base,
576 data_size,
577 location,
578 location_checksum,
579 oat_dex_file,
580 verify,
581 verify_checksum,
582 error_msg,
583 std::move(container),
584 verify_result);
585 }
586
587 } // namespace art
588