• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     CHECK(IsReadOnly());
93     if (!mem_map_.IsValid()) {
94       return false;
95     } else {
96       return mem_map_.Protect(PROT_READ | PROT_WRITE);
97     }
98   }
99 
DisableWrite()100   bool DisableWrite() override {
101     CHECK(!IsReadOnly());
102     if (!mem_map_.IsValid()) {
103       return false;
104     } else {
105       return mem_map_.Protect(PROT_READ);
106     }
107   }
108 
Begin() const109   const uint8_t* Begin() const override { return mem_map_.Begin(); }
110 
End() const111   const uint8_t* End() const override { return mem_map_.End(); }
112 
IsFileMap() const113   bool IsFileMap() const override { return is_file_map_; }
114 
115  protected:
116   MemMap mem_map_;
117   bool is_file_map_;
118   DISALLOW_COPY_AND_ASSIGN(MemMapContainer);
119 };
120 
121 }  // namespace
122 
IsMagicValid(uint32_t magic)123 bool DexFileLoader::IsMagicValid(uint32_t magic) {
124   return IsMagicValid(reinterpret_cast<uint8_t*>(&magic));
125 }
126 
IsMagicValid(const uint8_t * magic)127 bool DexFileLoader::IsMagicValid(const uint8_t* magic) {
128   return StandardDexFile::IsMagicValid(magic) ||
129       CompactDexFile::IsMagicValid(magic);
130 }
131 
IsVersionAndMagicValid(const uint8_t * magic)132 bool DexFileLoader::IsVersionAndMagicValid(const uint8_t* magic) {
133   if (StandardDexFile::IsMagicValid(magic)) {
134     return StandardDexFile::IsVersionValid(magic);
135   }
136   if (CompactDexFile::IsMagicValid(magic)) {
137     return CompactDexFile::IsVersionValid(magic);
138   }
139   return false;
140 }
141 
IsMultiDexLocation(const char * location)142 bool DexFileLoader::IsMultiDexLocation(const char* location) {
143   return strrchr(location, kMultiDexSeparator) != nullptr;
144 }
145 
GetMultiDexClassesDexName(size_t index)146 std::string DexFileLoader::GetMultiDexClassesDexName(size_t index) {
147   return (index == 0) ? "classes.dex" : StringPrintf("classes%zu.dex", index + 1);
148 }
149 
GetMultiDexLocation(size_t index,const char * dex_location)150 std::string DexFileLoader::GetMultiDexLocation(size_t index, const char* dex_location) {
151   return (index == 0)
152       ? dex_location
153       : StringPrintf("%s%cclasses%zu.dex", dex_location, kMultiDexSeparator, index + 1);
154 }
155 
GetDexCanonicalLocation(const char * dex_location)156 std::string DexFileLoader::GetDexCanonicalLocation(const char* dex_location) {
157   CHECK_NE(dex_location, static_cast<const char*>(nullptr));
158   std::string base_location = GetBaseLocation(dex_location);
159   const char* suffix = dex_location + base_location.size();
160   DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator);
161 #ifdef _WIN32
162   // Warning: No symbolic link processing here.
163   PLOG(WARNING) << "realpath is unsupported on Windows.";
164 #else
165   // Warning: Bionic implementation of realpath() allocates > 12KB on the stack.
166   // Do not run this code on a small stack, e.g. in signal handler.
167   UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr));
168   if (path != nullptr && path.get() != base_location) {
169     return std::string(path.get()) + suffix;
170   }
171 #endif
172   if (suffix[0] == 0) {
173     return base_location;
174   } else {
175     return dex_location;
176   }
177 }
178 
179 // All of the implementations here should be independent of the runtime.
180 
DexFileLoader(const uint8_t * base,size_t size,const std::string & location)181 DexFileLoader::DexFileLoader(const uint8_t* base, size_t size, const std::string& location)
182     : DexFileLoader(std::make_shared<MemoryDexFileContainer>(base, base + size), location) {}
183 
DexFileLoader(std::vector<uint8_t> && memory,const std::string & location)184 DexFileLoader::DexFileLoader(std::vector<uint8_t>&& memory, const std::string& location)
185     : DexFileLoader(std::make_shared<VectorContainer>(std::move(memory)), location) {}
186 
DexFileLoader(MemMap && mem_map,const std::string & location)187 DexFileLoader::DexFileLoader(MemMap&& mem_map, const std::string& location)
188     : DexFileLoader(std::make_shared<MemMapContainer>(std::move(mem_map)), location) {}
189 
Open(uint32_t location_checksum,const OatDexFile * oat_dex_file,bool verify,bool verify_checksum,std::string * error_msg)190 std::unique_ptr<const DexFile> DexFileLoader::Open(uint32_t location_checksum,
191                                                    const OatDexFile* oat_dex_file,
192                                                    bool verify,
193                                                    bool verify_checksum,
194                                                    std::string* error_msg) {
195   DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_);
196 
197   uint32_t magic;
198   if (!InitAndReadMagic(&magic, error_msg) || !MapRootContainer(error_msg)) {
199     DCHECK(!error_msg->empty());
200     return {};
201   }
202   DCHECK(root_container_ != nullptr);
203   std::unique_ptr<const DexFile> dex_file = OpenCommon(root_container_,
204                                                        root_container_->Begin(),
205                                                        root_container_->Size(),
206                                                        location_,
207                                                        location_checksum,
208                                                        oat_dex_file,
209                                                        verify,
210                                                        verify_checksum,
211                                                        error_msg,
212                                                        nullptr);
213   return dex_file;
214 }
215 
InitAndReadMagic(uint32_t * magic,std::string * error_msg)216 bool DexFileLoader::InitAndReadMagic(uint32_t* magic, std::string* error_msg) {
217   if (root_container_ != nullptr) {
218     if (root_container_->Size() < sizeof(uint32_t)) {
219       *error_msg = StringPrintf("Unable to open '%s' : Size is too small", location_.c_str());
220       return false;
221     }
222     *magic = *reinterpret_cast<const uint32_t*>(root_container_->Begin());
223   } else {
224     // Open the file if we have not been given the file-descriptor directly before.
225     if (!file_.has_value()) {
226       CHECK(!filename_.empty());
227       file_.emplace(filename_, O_RDONLY, /* check_usage= */ false);
228       if (file_->Fd() == -1) {
229         *error_msg = StringPrintf("Unable to open '%s' : %s", filename_.c_str(), strerror(errno));
230         return false;
231       }
232     }
233     if (!ReadMagicAndReset(file_->Fd(), magic, error_msg)) {
234       return false;
235     }
236   }
237   return true;
238 }
239 
MapRootContainer(std::string * error_msg)240 bool DexFileLoader::MapRootContainer(std::string* error_msg) {
241   if (root_container_ != nullptr) {
242     return true;
243   }
244 
245   CHECK(MemMap::IsInitialized());
246   CHECK(file_.has_value());
247   struct stat sbuf;
248   memset(&sbuf, 0, sizeof(sbuf));
249   if (fstat(file_->Fd(), &sbuf) == -1) {
250     *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", filename_.c_str(), strerror(errno));
251     return false;
252   }
253   if (S_ISDIR(sbuf.st_mode)) {
254     *error_msg = StringPrintf("Attempt to mmap directory '%s'", filename_.c_str());
255     return false;
256   }
257   MemMap map = MemMap::MapFile(sbuf.st_size,
258                                PROT_READ,
259                                MAP_PRIVATE,
260                                file_->Fd(),
261                                0,
262                                /*low_4gb=*/false,
263                                filename_.c_str(),
264                                error_msg);
265   if (!map.IsValid()) {
266     DCHECK(!error_msg->empty());
267     return false;
268   }
269   root_container_ = std::make_shared<MemMapContainer>(std::move(map));
270   return true;
271 }
272 
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)273 bool DexFileLoader::Open(bool verify,
274                          bool verify_checksum,
275                          bool allow_no_dex_files,
276                          DexFileLoaderErrorCode* error_code,
277                          std::string* error_msg,
278                          std::vector<std::unique_ptr<const DexFile>>* dex_files) {
279   DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_);
280 
281   DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr";
282 
283   uint32_t magic;
284   if (!InitAndReadMagic(&magic, error_msg)) {
285     return false;
286   }
287 
288   if (IsZipMagic(magic)) {
289     std::unique_ptr<ZipArchive> zip_archive(
290         file_.has_value() ?
291             ZipArchive::OpenFromOwnedFd(file_->Fd(), location_.c_str(), error_msg) :
292             ZipArchive::OpenFromMemory(
293                 root_container_->Begin(), root_container_->Size(), location_.c_str(), error_msg));
294     if (zip_archive.get() == nullptr) {
295       DCHECK(!error_msg->empty());
296       return false;
297     }
298     for (size_t i = 0;; ++i) {
299       std::string name = GetMultiDexClassesDexName(i);
300       std::string multidex_location = GetMultiDexLocation(i, location_.c_str());
301       bool ok = OpenFromZipEntry(*zip_archive,
302                                  name.c_str(),
303                                  multidex_location,
304                                  verify,
305                                  verify_checksum,
306                                  error_code,
307                                  error_msg,
308                                  dex_files);
309       if (!ok) {
310         // We keep opening consecutive dex entries as long as we can (until entry is not found).
311         if (*error_code == DexFileLoaderErrorCode::kEntryNotFound) {
312           // Success if we loaded at least one entry, or if empty zip is explicitly allowed.
313           return i > 0 || allow_no_dex_files;
314         }
315         return false;
316       }
317       if (i == kWarnOnManyDexFilesThreshold) {
318         LOG(WARNING) << location_ << " has in excess of " << kWarnOnManyDexFilesThreshold
319                      << " dex files. Please consider coalescing and shrinking the number to "
320                         " avoid runtime overhead.";
321       }
322     }
323   }
324   if (IsMagicValid(magic)) {
325     if (!MapRootContainer(error_msg)) {
326       return false;
327     }
328     DCHECK(root_container_ != nullptr);
329     std::unique_ptr<const DexFile> dex_file =
330         OpenCommon(root_container_,
331                    root_container_->Begin(),
332                    root_container_->Size(),
333                    location_,
334                    /*location_checksum*/ {},  // Use default checksum from dex header.
335                    /*oat_dex_file=*/nullptr,
336                    verify,
337                    verify_checksum,
338                    error_msg,
339                    nullptr);
340     if (dex_file.get() != nullptr) {
341       dex_files->push_back(std::move(dex_file));
342       return true;
343     } else {
344       return false;
345     }
346   }
347   *error_msg = StringPrintf("Expected valid zip or dex file");
348   return false;
349 }
350 
OpenCommon(std::shared_ptr<DexFileContainer> container,const uint8_t * base,size_t 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)351 std::unique_ptr<DexFile> DexFileLoader::OpenCommon(std::shared_ptr<DexFileContainer> container,
352                                                    const uint8_t* base,
353                                                    size_t size,
354                                                    const std::string& location,
355                                                    std::optional<uint32_t> location_checksum,
356                                                    const OatDexFile* oat_dex_file,
357                                                    bool verify,
358                                                    bool verify_checksum,
359                                                    std::string* error_msg,
360                                                    DexFileLoaderErrorCode* error_code) {
361   if (container == nullptr) {
362     // We should never pass null here, but use reasonable default for app compat anyway.
363     container = std::make_shared<MemoryDexFileContainer>(base, size);
364   }
365   if (error_code != nullptr) {
366     *error_code = DexFileLoaderErrorCode::kDexFileError;
367   }
368   std::unique_ptr<DexFile> dex_file;
369   auto header = reinterpret_cast<const DexFile::Header*>(base);
370   if (size >= sizeof(StandardDexFile::Header) && StandardDexFile::IsMagicValid(base)) {
371     uint32_t checksum = location_checksum.value_or(header->checksum_);
372     dex_file.reset(new StandardDexFile(base, size, location, checksum, oat_dex_file, container));
373   } else if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(base)) {
374     uint32_t checksum = location_checksum.value_or(header->checksum_);
375     dex_file.reset(new CompactDexFile(base, size, location, checksum, oat_dex_file, container));
376   } else {
377     *error_msg = StringPrintf("Invalid or truncated dex file '%s'", location.c_str());
378   }
379   if (dex_file == nullptr) {
380     *error_msg =
381         StringPrintf("Failed to open dex file '%s': %s", location.c_str(), error_msg->c_str());
382     return nullptr;
383   }
384   if (!dex_file->Init(error_msg)) {
385     dex_file.reset();
386     return nullptr;
387   }
388   // NB: Dex verifier does not understand the compact dex format.
389   if (verify && !dex_file->IsCompactDexFile()) {
390     DEXFILE_SCOPED_TRACE(std::string("Verify dex file ") + location);
391     if (!dex::Verify(dex_file.get(), location.c_str(), verify_checksum, error_msg)) {
392       if (error_code != nullptr) {
393         *error_code = DexFileLoaderErrorCode::kVerifyError;
394       }
395       return nullptr;
396     }
397   }
398   if (error_code != nullptr) {
399     *error_code = DexFileLoaderErrorCode::kNoError;
400   }
401   return dex_file;
402 }
403 
OpenFromZipEntry(const ZipArchive & zip_archive,const char * entry_name,const std::string & location,bool verify,bool verify_checksum,DexFileLoaderErrorCode * error_code,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const404 bool DexFileLoader::OpenFromZipEntry(const ZipArchive& zip_archive,
405                                      const char* entry_name,
406                                      const std::string& location,
407                                      bool verify,
408                                      bool verify_checksum,
409                                      DexFileLoaderErrorCode* error_code,
410                                      std::string* error_msg,
411                                      std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
412   CHECK(!location.empty());
413   std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
414   if (zip_entry == nullptr) {
415     *error_code = DexFileLoaderErrorCode::kEntryNotFound;
416     return false;
417   }
418   if (zip_entry->GetUncompressedLength() == 0) {
419     *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str());
420     *error_code = DexFileLoaderErrorCode::kDexFileError;
421     return false;
422   }
423 
424   CHECK(MemMap::IsInitialized());
425   MemMap map;
426   bool is_file_map = false;
427   if (file_.has_value() && zip_entry->IsUncompressed()) {
428     if (!zip_entry->IsAlignedTo(alignof(DexFile::Header))) {
429       // Do not mmap unaligned ZIP entries because
430       // doing so would fail dex verification which requires 4 byte alignment.
431       LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
432                    << "please zipalign to " << alignof(DexFile::Header) << " bytes. "
433                    << "Falling back to extracting file.";
434     } else {
435       // Map uncompressed files within zip as file-backed to avoid a dirty copy.
436       map = zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/ error_msg);
437       if (!map.IsValid()) {
438         LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; "
439                      << "is your ZIP file corrupted? Falling back to extraction.";
440         // Try again with Extraction which still has a chance of recovery.
441       }
442       is_file_map = true;
443     }
444   }
445   if (!map.IsValid()) {
446     DEXFILE_SCOPED_TRACE(std::string("Extract dex file ") + location);
447 
448     // Default path for compressed ZIP entries,
449     // and fallback for stored ZIP entries.
450     map = zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg);
451   }
452   if (!map.IsValid()) {
453     *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
454                               error_msg->c_str());
455     *error_code = DexFileLoaderErrorCode::kExtractToMemoryError;
456     return false;
457   }
458   auto container = std::make_shared<MemMapContainer>(std::move(map), is_file_map);
459   container->SetIsZip();
460   if (!container->DisableWrite()) {
461     *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
462     *error_code = DexFileLoaderErrorCode::kMakeReadOnlyError;
463     return false;
464   }
465 
466   std::unique_ptr<const DexFile> dex_file = OpenCommon(container,
467                                                        container->Begin(),
468                                                        container->Size(),
469                                                        location,
470                                                        zip_entry->GetCrc32(),
471                                                        /*oat_dex_file=*/nullptr,
472                                                        verify,
473                                                        verify_checksum,
474                                                        error_msg,
475                                                        error_code);
476   if (dex_file == nullptr) {
477     return false;
478   }
479   CHECK(dex_file->IsReadOnly()) << location;
480   dex_files->push_back(std::move(dex_file));
481   return true;
482 }
483 
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) const484 std::unique_ptr<const DexFile> DexFileLoader::Open(
485     const uint8_t* base,
486     size_t size,
487     const std::string& location,
488     uint32_t location_checksum,
489     const OatDexFile* oat_dex_file,
490     bool verify,
491     bool verify_checksum,
492     std::string* error_msg,
493     std::unique_ptr<DexFileContainer> container) const {
494   return OpenCommon(base,
495                     size,
496                     /*data_base=*/nullptr,
497                     /*data_size=*/0,
498                     location,
499                     location_checksum,
500                     oat_dex_file,
501                     verify,
502                     verify_checksum,
503                     error_msg,
504                     std::move(container),
505                     /*verify_result=*/nullptr);
506 }
507 
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)508 std::unique_ptr<DexFile> DexFileLoader::OpenCommon(const uint8_t* base,
509                                                    size_t size,
510                                                    const uint8_t* data_base,
511                                                    size_t data_size,
512                                                    const std::string& location,
513                                                    uint32_t location_checksum,
514                                                    const OatDexFile* oat_dex_file,
515                                                    bool verify,
516                                                    bool verify_checksum,
517                                                    std::string* error_msg,
518                                                    std::unique_ptr<DexFileContainer> old_container,
519                                                    VerifyResult* verify_result) {
520   CHECK(data_base == base || data_base == nullptr);
521   CHECK(data_size == size || data_size == 0);
522   CHECK(verify_result == nullptr);
523 
524   // The provided container probably does implent the new API.
525   // We don't use it, but let's at least call its destructor.
526   struct NewContainer : public MemoryDexFileContainer {
527     using MemoryDexFileContainer::MemoryDexFileContainer;  // ctor.
528     std::unique_ptr<DexFileContainer> old_container_ = nullptr;
529   };
530   auto new_container = std::make_shared<NewContainer>(base, size);
531   new_container->old_container_ = std::move(old_container);
532 
533   return OpenCommon(std::move(new_container),
534                     base,
535                     size,
536                     location,
537                     location_checksum,
538                     oat_dex_file,
539                     verify,
540                     verify_checksum,
541                     error_msg,
542                     /*error_code=*/nullptr);
543 }
544 
545 }  // namespace art
546