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 #ifndef ART_LIBDEXFILE_DEX_ART_DEX_FILE_LOADER_H_
18 #define ART_LIBDEXFILE_DEX_ART_DEX_FILE_LOADER_H_
19 
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "base/macros.h"
26 #include "dex/dex_file_loader.h"
27 
28 namespace art {
29 
30 class DexFile;
31 class DexFileContainer;
32 class MemMap;
33 class OatDexFile;
34 class ZipArchive;
35 
36 // Class that is used to open dex files and deal with corresponding multidex and location logic.
37 class ArtDexFileLoader : public DexFileLoader {
38  public:
~ArtDexFileLoader()39   virtual ~ArtDexFileLoader() { }
40 
41   // Returns the checksums of a file for comparison with GetLocationChecksum().
42   // For .dex files, this is the single header checksum.
43   // For zip files, this is the zip entry CRC32 checksum for classes.dex and
44   // each additional multidex entry classes2.dex, classes3.dex, etc.
45   // If a valid zip_fd is provided the file content will be read directly from
46   // the descriptor and `filename` will be used as alias for error logging. If
47   // zip_fd is -1, the method will try to open the `filename` and read the
48   // content from it.
49   // Return true if the checksums could be found, false otherwise.
50   bool GetMultiDexChecksums(const char* filename,
51                             std::vector<uint32_t>* checksums,
52                             std::string* error_msg,
53                             int zip_fd = -1,
54                             bool* only_contains_uncompressed_dex = nullptr) const override;
55 
56   // Opens .dex file, backed by existing memory
57   std::unique_ptr<const DexFile> Open(
58       const uint8_t* base,
59       size_t size,
60       const std::string& location,
61       uint32_t location_checksum,
62       const OatDexFile* oat_dex_file,
63       bool verify,
64       bool verify_checksum,
65       std::string* error_msg,
66       std::unique_ptr<DexFileContainer> container = nullptr) const override;
67 
68   // Opens .dex file that has been memory-mapped by the caller.
69   std::unique_ptr<const DexFile> Open(const std::string& location,
70                                       uint32_t location_checkum,
71                                       MemMap&& mem_map,
72                                       bool verify,
73                                       bool verify_checksum,
74                                       std::string* error_msg) const;
75 
76   // Opens all .dex files found in the file, guessing the container format based on file magic.
77   bool Open(const char* filename,
78             const std::string& location,
79             bool verify,
80             bool verify_checksum,
81             std::string* error_msg,
82             std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
83   bool Open(int fd,
84             const std::string& location,
85             bool verify,
86             bool verify_checksum,
87             std::string* error_msg,
88             std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
89 
90   // Open a single dex file from an fd. This function closes the fd.
91   std::unique_ptr<const DexFile> OpenDex(int fd,
92                                          const std::string& location,
93                                          bool verify,
94                                          bool verify_checksum,
95                                          bool mmap_shared,
96                                          std::string* error_msg) const;
97 
98   // Opens dex files from within a .jar, .zip, or .apk file
99   bool OpenZip(int fd,
100                const std::string& location,
101                bool verify,
102                bool verify_checksum,
103                std::string* error_msg,
104                std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
105 
106  private:
107   bool OpenWithMagic(uint32_t magic,
108                      int fd,
109                      const std::string& location,
110                      bool verify,
111                      bool verify_checksum,
112                      std::string* error_msg,
113                      std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
114 
115   std::unique_ptr<const DexFile> OpenFile(int fd,
116                                           const std::string& location,
117                                           bool verify,
118                                           bool verify_checksum,
119                                           bool mmap_shared,
120                                           std::string* error_msg) const;
121 
122   // Open all classesXXX.dex files from a zip archive.
123   bool OpenAllDexFilesFromZip(const ZipArchive& zip_archive,
124                               const std::string& location,
125                               bool verify,
126                               bool verify_checksum,
127                               std::string* error_msg,
128                               std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
129 
130   // Opens .dex file from the entry_name in a zip archive. error_code is undefined when non-null
131   // return.
132   std::unique_ptr<const DexFile> OpenOneDexFileFromZip(const ZipArchive& zip_archive,
133                                                        const char* entry_name,
134                                                        const std::string& location,
135                                                        bool verify,
136                                                        bool verify_checksum,
137                                                        std::string* error_msg,
138                                                        DexFileLoaderErrorCode* error_code) const;
139 
140   static std::unique_ptr<DexFile> OpenCommon(const uint8_t* base,
141                                              size_t size,
142                                              const uint8_t* data_base,
143                                              size_t data_size,
144                                              const std::string& location,
145                                              uint32_t location_checksum,
146                                              const OatDexFile* oat_dex_file,
147                                              bool verify,
148                                              bool verify_checksum,
149                                              std::string* error_msg,
150                                              std::unique_ptr<DexFileContainer> container,
151                                              VerifyResult* verify_result);
152 };
153 
154 }  // namespace art
155 
156 #endif  // ART_LIBDEXFILE_DEX_ART_DEX_FILE_LOADER_H_
157