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