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_RUNTIME_DEX_ART_DEX_FILE_LOADER_H_
18 #define ART_RUNTIME_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(const uint8_t* base,
58                                       size_t size,
59                                       const std::string& location,
60                                       uint32_t location_checksum,
61                                       const OatDexFile* oat_dex_file,
62                                       bool verify,
63                                       bool verify_checksum,
64                                       std::string* error_msg) const OVERRIDE;
65 
66   // Opens .dex file that has been memory-mapped by the caller.
67   std::unique_ptr<const DexFile> Open(const std::string& location,
68                                       uint32_t location_checkum,
69                                       std::unique_ptr<MemMap> mem_map,
70                                       bool verify,
71                                       bool verify_checksum,
72                                       std::string* error_msg) const;
73 
74   // Opens all .dex files found in the file, guessing the container format based on file extension.
75   bool Open(const char* filename,
76             const std::string& location,
77             bool verify,
78             bool verify_checksum,
79             std::string* error_msg,
80             std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
81 
82   // Open a single dex file from an fd. This function closes the fd.
83   std::unique_ptr<const DexFile> OpenDex(int fd,
84                                          const std::string& location,
85                                          bool verify,
86                                          bool verify_checksum,
87                                          bool mmap_shared,
88                                          std::string* error_msg) const;
89 
90   // Opens dex files from within a .jar, .zip, or .apk file
91   bool OpenZip(int fd,
92                const std::string& location,
93                bool verify,
94                bool verify_checksum,
95                std::string* error_msg,
96                std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
97 
98  private:
99   std::unique_ptr<const DexFile> OpenFile(int fd,
100                                           const std::string& location,
101                                           bool verify,
102                                           bool verify_checksum,
103                                           bool mmap_shared,
104                                           std::string* error_msg) const;
105 
106   // Open all classesXXX.dex files from a zip archive.
107   bool OpenAllDexFilesFromZip(const ZipArchive& zip_archive,
108                               const std::string& location,
109                               bool verify,
110                               bool verify_checksum,
111                               std::string* error_msg,
112                               std::vector<std::unique_ptr<const DexFile>>* dex_files) const;
113 
114   // Opens .dex file from the entry_name in a zip archive. error_code is undefined when non-null
115   // return.
116   std::unique_ptr<const DexFile> OpenOneDexFileFromZip(const ZipArchive& zip_archive,
117                                                        const char* entry_name,
118                                                        const std::string& location,
119                                                        bool verify,
120                                                        bool verify_checksum,
121                                                        std::string* error_msg,
122                                                        ZipOpenErrorCode* error_code) const;
123 
124   static std::unique_ptr<DexFile> OpenCommon(const uint8_t* base,
125                                              size_t size,
126                                              const uint8_t* data_base,
127                                              size_t data_size,
128                                              const std::string& location,
129                                              uint32_t location_checksum,
130                                              const OatDexFile* oat_dex_file,
131                                              bool verify,
132                                              bool verify_checksum,
133                                              std::string* error_msg,
134                                              std::unique_ptr<DexFileContainer> container,
135                                              VerifyResult* verify_result);
136 };
137 
138 }  // namespace art
139 
140 #endif  // ART_RUNTIME_DEX_ART_DEX_FILE_LOADER_H_
141