xref: /aosp_15_r20/art/libdexfile/dex/dex_file_loader.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 "dex_file_loader.h"
18 
19 #include <sys/stat.h>
20 
21 #include <memory>
22 #include <optional>
23 
24 #include "android-base/stringprintf.h"
25 #include "base/bit_utils.h"
26 #include "base/file_magic.h"
27 #include "base/mem_map.h"
28 #include "base/os.h"
29 #include "base/stl_util.h"
30 #include "base/systrace.h"
31 #include "base/unix_file/fd_file.h"
32 #include "base/zip_archive.h"
33 #include "compact_dex_file.h"
34 #include "dex_file.h"
35 #include "dex_file_verifier.h"
36 #include "standard_dex_file.h"
37 
38 namespace art {
39 
40 #if defined(STATIC_LIB)
41 #define DEXFILE_SCOPED_TRACE(name)
42 #else
43 #define DEXFILE_SCOPED_TRACE(name) ScopedTrace trace(name)
44 #endif
45 
46 namespace {
47 
48 // Technically we do not have a limitation with respect to the number of dex files that can be in a
49 // multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols
50 // (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what
51 // seems an excessive number.
52 static constexpr size_t kWarnOnManyDexFilesThreshold = 100;
53 
54 using android::base::StringPrintf;
55 
56 class VectorContainer : public DexFileContainer {
57  public:
VectorContainer(std::vector<uint8_t> && vector)58   explicit VectorContainer(std::vector<uint8_t>&& vector) : vector_(std::move(vector)) { }
~VectorContainer()59   ~VectorContainer() override { }
60 
IsReadOnly() const61   bool IsReadOnly() const override { return true; }
62 
EnableWrite()63   bool EnableWrite() override { return true; }
64 
DisableWrite()65   bool DisableWrite() override { return false; }
66 
Begin() const67   const uint8_t* Begin() const override { return vector_.data(); }
68 
End() const69   const uint8_t* End() const override { return vector_.data() + vector_.size(); }
70 
71  private:
72   std::vector<uint8_t> vector_;
73   DISALLOW_COPY_AND_ASSIGN(VectorContainer);
74 };
75 
76 class MemMapContainer : public DexFileContainer {
77  public:
MemMapContainer(MemMap && mem_map,bool is_file_map=false)78   explicit MemMapContainer(MemMap&& mem_map, bool is_file_map = false)
79       : mem_map_(std::move(mem_map)), is_file_map_(is_file_map) {}
80 
GetPermissions() const81   int GetPermissions() const {
82     if (!mem_map_.IsValid()) {
83       return 0;
84     } else {
85       return mem_map_.GetProtect();
86     }
87   }
88 
IsReadOnly() const89   bool IsReadOnly() const override { return GetPermissions() == PROT_READ; }
90 
EnableWrite()91   bool EnableWrite() override {
92     if (!IsReadOnly()) {
93       // We can already write to the container.
94       // This method may be called multiple times by tests if DexFiles share container.
95       return true;
96     }
97     if (!mem_map_.IsValid()) {
98       return false;
99     } else {
100       return mem_map_.Protect(PROT_READ | PROT_WRITE);
101     }
102   }
103 
DisableWrite()104   bool DisableWrite() override {
105     CHECK(!IsReadOnly());
106     if (!mem_map_.IsValid()) {
107       return false;
108     } else {
109       return mem_map_.Protect(PROT_READ);
110     }
111   }
112 
Begin() const113   const uint8_t* Begin() const override { return mem_map_.Begin(); }
114 
End() const115   const uint8_t* End() const override { return mem_map_.End(); }
116 
IsFileMap() const117   bool IsFileMap() const override { return is_file_map_; }
118 
119  protected:
120   MemMap mem_map_;
121   bool is_file_map_;
122   DISALLOW_COPY_AND_ASSIGN(MemMapContainer);
123 };
124 
125 }  // namespace
126 
127 const File DexFileLoader::kInvalidFile;
128 
IsMagicValid(uint32_t magic)129 bool DexFileLoader::IsMagicValid(uint32_t magic) {
130   return IsMagicValid(reinterpret_cast<uint8_t*>(&magic));
131 }
132 
IsMagicValid(const uint8_t * magic)133 bool DexFileLoader::IsMagicValid(const uint8_t* magic) {
134   return StandardDexFile::IsMagicValid(magic) ||
135       CompactDexFile::IsMagicValid(magic);
136 }
137 
IsVersionAndMagicValid(const uint8_t * magic)138 bool DexFileLoader::IsVersionAndMagicValid(const uint8_t* magic) {
139   if (StandardDexFile::IsMagicValid(magic)) {
140     return StandardDexFile::IsVersionValid(magic);
141   }
142   if (CompactDexFile::IsMagicValid(magic)) {
143     return CompactDexFile::IsVersionValid(magic);
144   }
145   return false;
146 }
147 
IsMultiDexLocation(std::string_view location)148 bool DexFileLoader::IsMultiDexLocation(std::string_view location) {
149   return location.find(kMultiDexSeparator) != std::string_view::npos;
150 }
151 
GetMultiDexClassesDexName(size_t index)152 std::string DexFileLoader::GetMultiDexClassesDexName(size_t index) {
153   return (index == 0) ? "classes.dex" : StringPrintf("classes%zu.dex", index + 1);
154 }
155 
GetMultiDexLocation(size_t index,const char * dex_location)156 std::string DexFileLoader::GetMultiDexLocation(size_t index, const char* dex_location) {
157   DCHECK(!IsMultiDexLocation(dex_location));
158   if (index == 0) {
159     return dex_location;
160   }
161   return StringPrintf("%s%cclasses%zu.dex", dex_location, kMultiDexSeparator, index + 1);
162 }
163 
GetMultiDexChecksums(std::vector<std::pair<std::string,uint32_t>> * checksums,std::string * error_msg,bool * only_contains_uncompressed_dex)164 bool DexFileLoader::GetMultiDexChecksums(
165     /*out*/ std::vector<std::pair<std::string, uint32_t>>* checksums,
166     /*out*/ std::string* error_msg,
167     /*out*/ bool* only_contains_uncompressed_dex) {
168   uint32_t magic;
169   if (!InitAndReadMagic(/*header_offset=*/0, &magic, error_msg)) {
170     return false;
171   }
172 
173   if (IsZipMagic(magic)) {
174     std::unique_ptr<ZipArchive> zip_archive(
175         file_->IsValid() ?
176             ZipArchive::OpenFromOwnedFd(file_->Fd(), location_.c_str(), error_msg) :
177             ZipArchive::OpenFromMemory(
178                 root_container_->Begin(), root_container_->Size(), location_.c_str(), error_msg));
179     if (zip_archive.get() == nullptr) {
180       DCHECK(!error_msg->empty());
181       return false;
182     }
183     if (only_contains_uncompressed_dex != nullptr) {
184       *only_contains_uncompressed_dex = true;
185     }
186     for (size_t i = 0;; ++i) {
187       std::string name = GetMultiDexClassesDexName(i);
188       std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(name.c_str(), error_msg));
189       if (zip_entry == nullptr) {
190         break;
191       }
192       if (only_contains_uncompressed_dex != nullptr) {
193         if (!(zip_entry->IsUncompressed() && zip_entry->IsAlignedTo(alignof(DexFile::Header)))) {
194           *only_contains_uncompressed_dex = false;
195         }
196       }
197       checksums->emplace_back(GetMultiDexLocation(i, location_.c_str()), zip_entry->GetCrc32());
198     }
199     return true;
200   }
201   if (!MapRootContainer(error_msg)) {
202     return false;
203   }
204   const uint8_t* begin = root_container_->Begin();
205   const uint8_t* end = root_container_->End();
206   size_t i = 0;
207   for (const uint8_t* ptr = begin; ptr < end;) {
208     const auto* header = reinterpret_cast<const DexFile::Header*>(ptr);
209     size_t size = dchecked_integral_cast<size_t>(end - ptr);
210     if (size < sizeof(*header) || !IsMagicValid(ptr)) {
211       *error_msg = StringPrintf("Invalid dex header: '%s'", filename_.c_str());
212       return false;
213     }
214     if (size < header->file_size_) {
215       *error_msg = StringPrintf("Truncated dex file: '%s'", filename_.c_str());
216       return false;
217     }
218     checksums->emplace_back(GetMultiDexLocation(i++, location_.c_str()), header->checksum_);
219     ptr += header->file_size_;
220   }
221   return true;
222 }
223 
GetMultiDexChecksum(std::optional<uint32_t> * checksum,std::string * error_msg,bool * only_contains_uncompressed_dex)224 bool DexFileLoader::GetMultiDexChecksum(std::optional<uint32_t>* checksum,
225                                         std::string* error_msg,
226                                         bool* only_contains_uncompressed_dex) {
227   CHECK(checksum != nullptr);
228   checksum->reset();  // Return nullopt for an empty zip archive.
229 
230   std::vector<std::pair<std::string, uint32_t>> checksums;
231   if (!GetMultiDexChecksums(&checksums, error_msg, only_contains_uncompressed_dex)) {
232     return false;
233   }
234   for (const auto& [location, current_checksum] : checksums) {
235     *checksum = checksum->value_or(kEmptyMultiDexChecksum) ^ current_checksum;
236   }
237   return true;
238 }
239 
GetDexCanonicalLocation(const char * dex_location)240 std::string DexFileLoader::GetDexCanonicalLocation(const char* dex_location) {
241   CHECK_NE(dex_location, static_cast<const char*>(nullptr));
242   std::string base_location = GetBaseLocation(dex_location);
243   const char* suffix = dex_location + base_location.size();
244   DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator);
245 #ifdef _WIN32
246   // Warning: No symbolic link processing here.
247   PLOG(WARNING) << "realpath is unsupported on Windows.";
248 #else
249   // Warning: Bionic implementation of realpath() allocates > 12KB on the stack.
250   // Do not run this code on a small stack, e.g. in signal handler.
251   UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr));
252   if (path != nullptr && path.get() != base_location) {
253     return std::string(path.get()) + suffix;
254   }
255 #endif
256   if (suffix[0] == 0) {
257     return base_location;
258   } else {
259     return dex_location;
260   }
261 }
262 
263 // All of the implementations here should be independent of the runtime.
264 
DexFileLoader(const uint8_t * base,size_t size,const std::string & location)265 DexFileLoader::DexFileLoader(const uint8_t* base, size_t size, const std::string& location)
266     : DexFileLoader(std::make_shared<MemoryDexFileContainer>(base, base + size), location) {}
267 
DexFileLoader(std::vector<uint8_t> && memory,const std::string & location)268 DexFileLoader::DexFileLoader(std::vector<uint8_t>&& memory, const std::string& location)
269     : DexFileLoader(std::make_shared<VectorContainer>(std::move(memory)), location) {}
270 
DexFileLoader(MemMap && mem_map,const std::string & location)271 DexFileLoader::DexFileLoader(MemMap&& mem_map, const std::string& location)
272     : DexFileLoader(std::make_shared<MemMapContainer>(std::move(mem_map)), location) {}
273 
OpenOne(size_t header_offset,uint32_t location_checksum,const OatDexFile * oat_dex_file,bool verify,bool verify_checksum,std::string * error_msg)274 std::unique_ptr<const DexFile> DexFileLoader::OpenOne(size_t header_offset,
275                                                       uint32_t location_checksum,
276                                                       const OatDexFile* oat_dex_file,
277                                                       bool verify,
278                                                       bool verify_checksum,
279                                                       std::string* error_msg) {
280   DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_);
281 
282   uint32_t magic;
283   if (!InitAndReadMagic(header_offset, &magic, error_msg) || !MapRootContainer(error_msg)) {
284     DCHECK(!error_msg->empty());
285     return {};
286   }
287   DCHECK(root_container_ != nullptr);
288   DCHECK_LE(header_offset, root_container_->Size());
289   std::unique_ptr<const DexFile> dex_file = OpenCommon(root_container_,
290                                                        root_container_->Begin() + header_offset,
291                                                        root_container_->Size() - header_offset,
292                                                        location_,
293                                                        location_checksum,
294                                                        oat_dex_file,
295                                                        verify,
296                                                        verify_checksum,
297                                                        error_msg,
298                                                        nullptr);
299   return dex_file;
300 }
301 
InitAndReadMagic(size_t header_offset,uint32_t * magic,std::string * error_msg)302 bool DexFileLoader::InitAndReadMagic(size_t header_offset,
303                                      uint32_t* magic,
304                                      std::string* error_msg) {
305   if (root_container_ != nullptr) {
306     if (root_container_->Size() < header_offset ||
307         root_container_->Size() - header_offset < sizeof(uint32_t)) {
308       *error_msg = StringPrintf("Unable to open '%s' : Size is too small", location_.c_str());
309       return false;
310     }
311     *magic = *reinterpret_cast<const uint32_t*>(root_container_->Begin() + header_offset);
312   } else {
313     // Open the file if we have not been given the file-descriptor directly before.
314     if (!file_->IsValid()) {
315       CHECK(!filename_.empty());
316       owned_file_ = File(filename_, O_RDONLY, /* check_usage= */ false);
317       if (!owned_file_->IsValid()) {
318         *error_msg = StringPrintf("Unable to open '%s' : %s", filename_.c_str(), strerror(errno));
319         return false;
320       }
321       file_ = &owned_file_.value();
322     }
323     CHECK_EQ(header_offset, 0u);  // We always expect to read from the start of physical file.
324     if (!ReadMagicAndReset(file_->Fd(), magic, error_msg)) {
325       return false;
326     }
327   }
328   return true;
329 }
330 
MapRootContainer(std::string * error_msg)331 bool DexFileLoader::MapRootContainer(std::string* error_msg) {
332   if (root_container_ != nullptr) {
333     return true;
334   }
335 
336   CHECK(MemMap::IsInitialized());
337   CHECK(file_->IsValid());
338   struct stat sbuf;
339   memset(&sbuf, 0, sizeof(sbuf));
340   if (fstat(file_->Fd(), &sbuf) == -1) {
341     *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", filename_.c_str(), strerror(errno));
342     return false;
343   }
344   if (S_ISDIR(sbuf.st_mode)) {
345     *error_msg = StringPrintf("Attempt to mmap directory '%s'", filename_.c_str());
346     return false;
347   }
348   MemMap map = MemMap::MapFile(sbuf.st_size,
349                                PROT_READ,
350                                MAP_PRIVATE,
351                                file_->Fd(),
352                                0,
353                                /*low_4gb=*/false,
354                                filename_.c_str(),
355                                error_msg);
356   if (!map.IsValid()) {
357     DCHECK(!error_msg->empty());
358     return false;
359   }
360   root_container_ = std::make_shared<MemMapContainer>(std::move(map), /*is_file_map=*/true);
361   return true;
362 }
363 
Open(bool verify,bool verify_checksum,bool allow_no_dex_files,DexFileLoaderErrorCode * error_code,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files)364 bool DexFileLoader::Open(bool verify,
365                          bool verify_checksum,
366                          bool allow_no_dex_files,
367                          DexFileLoaderErrorCode* error_code,
368                          std::string* error_msg,
369                          std::vector<std::unique_ptr<const DexFile>>* dex_files) {
370   DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_);
371 
372   DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr";
373 
374   uint32_t magic;
375   if (!InitAndReadMagic(/*header_offset=*/0, &magic, error_msg)) {
376     return false;
377   }
378 
379   if (IsZipMagic(magic)) {
380     std::unique_ptr<ZipArchive> zip_archive(
381         file_->IsValid() ?
382             ZipArchive::OpenFromOwnedFd(file_->Fd(), location_.c_str(), error_msg) :
383             ZipArchive::OpenFromMemory(
384                 root_container_->Begin(), root_container_->Size(), location_.c_str(), error_msg));
385     if (zip_archive.get() == nullptr) {
386       DCHECK(!error_msg->empty());
387       return false;
388     }
389     size_t multidex_count = 0;
390     for (size_t i = 0;; ++i) {
391       std::string name = GetMultiDexClassesDexName(i);
392       bool ok = OpenFromZipEntry(*zip_archive,
393                                  name.c_str(),
394                                  location_,
395                                  verify,
396                                  verify_checksum,
397                                  &multidex_count,
398                                  error_code,
399                                  error_msg,
400                                  dex_files);
401       if (!ok) {
402         // We keep opening consecutive dex entries as long as we can (until entry is not found).
403         if (*error_code == DexFileLoaderErrorCode::kEntryNotFound) {
404           // Success if we loaded at least one entry, or if empty zip is explicitly allowed.
405           return i > 0 || allow_no_dex_files;
406         }
407         return false;
408       }
409       if (i == kWarnOnManyDexFilesThreshold) {
410         LOG(WARNING) << location_ << " has in excess of " << kWarnOnManyDexFilesThreshold
411                      << " dex files. Please consider coalescing and shrinking the number to "
412                         " avoid runtime overhead.";
413       }
414     }
415   }
416   if (IsMagicValid(magic)) {
417     if (!MapRootContainer(error_msg)) {
418       return false;
419     }
420     DCHECK(root_container_ != nullptr);
421     size_t header_offset = 0;
422     for (size_t i = 0;; i++) {
423       std::string multidex_location = GetMultiDexLocation(i, location_.c_str());
424       std::unique_ptr<const DexFile> dex_file =
425           OpenCommon(root_container_,
426                      root_container_->Begin() + header_offset,
427                      root_container_->Size() - header_offset,
428                      multidex_location,
429                      /*location_checksum*/ {},  // Use default checksum from dex header.
430                      /*oat_dex_file=*/nullptr,
431                      verify,
432                      verify_checksum,
433                      error_msg,
434                      error_code);
435       if (dex_file == nullptr) {
436         return false;
437       }
438       dex_files->push_back(std::move(dex_file));
439       size_t file_size = dex_files->back()->GetHeader().file_size_;
440       CHECK_LE(file_size, root_container_->Size() - header_offset);
441       header_offset += file_size;
442       if (dex_files->back()->IsDexContainerLastEntry()) {
443         break;
444       }
445     }
446     return true;
447   }
448   *error_msg = StringPrintf("Expected valid zip or dex file");
449   return false;
450 }
451 
OpenCommon(std::shared_ptr<DexFileContainer> container,const uint8_t * base,size_t app_compat_size,const std::string & location,std::optional<uint32_t> location_checksum,const OatDexFile * oat_dex_file,bool verify,bool verify_checksum,std::string * error_msg,DexFileLoaderErrorCode * error_code)452 std::unique_ptr<DexFile> DexFileLoader::OpenCommon(std::shared_ptr<DexFileContainer> container,
453                                                    const uint8_t* base,
454                                                    size_t app_compat_size,
455                                                    const std::string& location,
456                                                    std::optional<uint32_t> location_checksum,
457                                                    const OatDexFile* oat_dex_file,
458                                                    bool verify,
459                                                    bool verify_checksum,
460                                                    std::string* error_msg,
461                                                    DexFileLoaderErrorCode* error_code) {
462   if (container == nullptr) {
463     // We should never pass null here, but use reasonable default for app compat anyway.
464     container = std::make_shared<MemoryDexFileContainer>(base, app_compat_size);
465   }
466   CHECK_GE(base, container->Begin());
467   CHECK_LE(base, container->End());
468   const size_t size = container->End() - base;
469   if (error_code != nullptr) {
470     *error_code = DexFileLoaderErrorCode::kDexFileError;
471   }
472   std::unique_ptr<DexFile> dex_file;
473   auto header = reinterpret_cast<const DexFile::Header*>(base);
474   if (size >= sizeof(StandardDexFile::Header) && StandardDexFile::IsMagicValid(base)) {
475     uint32_t checksum = location_checksum.value_or(header->checksum_);
476     dex_file.reset(new StandardDexFile(base, location, checksum, oat_dex_file, container));
477   } else if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(base)) {
478     uint32_t checksum = location_checksum.value_or(header->checksum_);
479     dex_file.reset(new CompactDexFile(base, location, checksum, oat_dex_file, container));
480   } else {
481     *error_msg = StringPrintf("Invalid or truncated dex file '%s'", location.c_str());
482   }
483   if (dex_file == nullptr) {
484     *error_msg =
485         StringPrintf("Failed to open dex file '%s': %s", location.c_str(), error_msg->c_str());
486     return nullptr;
487   }
488   if (!dex_file->Init(error_msg)) {
489     dex_file.reset();
490     return nullptr;
491   }
492   // NB: Dex verifier does not understand the compact dex format.
493   if (verify && !dex_file->IsCompactDexFile()) {
494     DEXFILE_SCOPED_TRACE(std::string("Verify dex file ") + location);
495     if (!dex::Verify(dex_file.get(), location.c_str(), verify_checksum, error_msg)) {
496       if (error_code != nullptr) {
497         *error_code = DexFileLoaderErrorCode::kVerifyError;
498       }
499       return nullptr;
500     }
501   }
502   if (error_code != nullptr) {
503     *error_code = DexFileLoaderErrorCode::kNoError;
504   }
505   return dex_file;
506 }
507 
OpenFromZipEntry(const ZipArchive & zip_archive,const char * entry_name,const std::string & location,bool verify,bool verify_checksum,size_t * multidex_count,DexFileLoaderErrorCode * error_code,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const508 bool DexFileLoader::OpenFromZipEntry(const ZipArchive& zip_archive,
509                                      const char* entry_name,
510                                      const std::string& location,
511                                      bool verify,
512                                      bool verify_checksum,
513                                      size_t* multidex_count,
514                                      DexFileLoaderErrorCode* error_code,
515                                      std::string* error_msg,
516                                      std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
517   CHECK(!location.empty());
518   std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
519   if (zip_entry == nullptr) {
520     *error_code = DexFileLoaderErrorCode::kEntryNotFound;
521     return false;
522   }
523   if (zip_entry->GetUncompressedLength() == 0) {
524     *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str());
525     *error_code = DexFileLoaderErrorCode::kDexFileError;
526     return false;
527   }
528 
529   CHECK(MemMap::IsInitialized());
530   MemMap map;
531   bool is_file_map = false;
532   if (file_->IsValid() && zip_entry->IsUncompressed()) {
533     if (!zip_entry->IsAlignedTo(alignof(DexFile::Header))) {
534       // Do not mmap unaligned ZIP entries because
535       // doing so would fail dex verification which requires 4 byte alignment.
536       LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
537                    << "please zipalign to " << alignof(DexFile::Header) << " bytes. "
538                    << "Falling back to extracting file.";
539     } else {
540       // Map uncompressed files within zip as file-backed to avoid a dirty copy.
541       map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/ error_msg);
542       if (!map.IsValid()) {
543         LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
544                      << "is your ZIP file corrupted? Falling back to extraction.";
545         // Try again with Extraction which still has a chance of recovery.
546       }
547       is_file_map = true;
548     }
549   }
550   if (!map.IsValid()) {
551     DEXFILE_SCOPED_TRACE(std::string("Extract dex file ") + location);
552 
553     // Default path for compressed ZIP entries,
554     // and fallback for stored ZIP entries.
555     map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg);
556   }
557   if (!map.IsValid()) {
558     *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
559                               error_msg->c_str());
560     *error_code = DexFileLoaderErrorCode::kExtractToMemoryError;
561     return false;
562   }
563   auto container = std::make_shared<MemMapContainer>(std::move(map), is_file_map);
564   container->SetIsZip();
565   if (!container->DisableWrite()) {
566     *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
567     *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError;
568     return false;
569   }
570 
571   size_t header_offset = 0;
572   for (size_t i = 0;; i++) {
573     std::string multidex_location = GetMultiDexLocation(*multidex_count, location.c_str());
574     ++(*multidex_count);
575     uint32_t multidex_checksum = zip_entry->GetCrc32() + i;
576     std::unique_ptr<const DexFile> dex_file = OpenCommon(container,
577                                                          container->Begin() + header_offset,
578                                                          container->Size() - header_offset,
579                                                          multidex_location,
580                                                          multidex_checksum,
581                                                          /*oat_dex_file=*/nullptr,
582                                                          verify,
583                                                          verify_checksum,
584                                                          error_msg,
585                                                          error_code);
586     if (dex_file == nullptr) {
587       return false;
588     }
589     if (dex_file->IsCompactDexFile()) {
590       *error_msg = StringPrintf("Can not open compact dex file from zip '%s'", location.c_str());
591       return false;
592     }
593     CHECK(dex_file->IsReadOnly()) << multidex_location;
594     dex_files->push_back(std::move(dex_file));
595     size_t file_size = dex_files->back()->GetHeader().file_size_;
596     CHECK_LE(file_size, container->Size() - header_offset);
597     header_offset += file_size;
598     if (dex_files->back()->IsDexContainerLastEntry()) {
599       break;
600     }
601   }
602   return true;
603 }
604 
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) const605 std::unique_ptr<const DexFile> DexFileLoader::Open(
606     const uint8_t* base,
607     size_t size,
608     const std::string& location,
609     uint32_t location_checksum,
610     const OatDexFile* oat_dex_file,
611     bool verify,
612     bool verify_checksum,
613     std::string* error_msg,
614     std::unique_ptr<DexFileContainer> container) const {
615   return OpenCommon(base,
616                     size,
617                     /*data_base=*/nullptr,
618                     /*data_size=*/0,
619                     location,
620                     location_checksum,
621                     oat_dex_file,
622                     verify,
623                     verify_checksum,
624                     error_msg,
625                     std::move(container),
626                     /*verify_result=*/nullptr);
627 }
628 
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> old_container,VerifyResult * verify_result)629 std::unique_ptr<DexFile> DexFileLoader::OpenCommon(const uint8_t* base,
630                                                    size_t size,
631                                                    const uint8_t* data_base,
632                                                    size_t data_size,
633                                                    const std::string& location,
634                                                    uint32_t location_checksum,
635                                                    const OatDexFile* oat_dex_file,
636                                                    bool verify,
637                                                    bool verify_checksum,
638                                                    std::string* error_msg,
639                                                    std::unique_ptr<DexFileContainer> old_container,
640                                                    VerifyResult* verify_result) {
641   CHECK(data_base == base || data_base == nullptr);
642   CHECK(data_size == size || data_size == 0);
643   CHECK(verify_result == nullptr);
644 
645   // The provided container probably does implent the new API.
646   // We don't use it, but let's at least call its destructor.
647   struct NewContainer : public MemoryDexFileContainer {
648     using MemoryDexFileContainer::MemoryDexFileContainer;  // ctor.
649     std::unique_ptr<DexFileContainer> old_container_ = nullptr;
650   };
651   auto new_container = std::make_shared<NewContainer>(base, size);
652   new_container->old_container_ = std::move(old_container);
653 
654   return OpenCommon(std::move(new_container),
655                     base,
656                     size,
657                     location,
658                     location_checksum,
659                     oat_dex_file,
660                     verify,
661                     verify_checksum,
662                     error_msg,
663                     /*error_code=*/nullptr);
664 }
665 
666 }  // namespace art
667