blob: 4395474a0cb7a741ede44f4eaaefd178ff0f48dc [file] [log] [blame]
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001/*
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
David Srbecky052f5fb2023-02-13 12:42:12 +000019#include <sys/stat.h>
20
David Srbecky2bcd3a02023-01-25 18:57:39 +000021#include <memory>
David Srbecky1dfac992023-03-07 18:58:11 +000022#include <optional>
Mathieu Chartier79c87da2017-10-10 11:54:29 -070023
David Srbecky2bcd3a02023-01-25 18:57:39 +000024#include "android-base/stringprintf.h"
David Srbecky052f5fb2023-02-13 12:42:12 +000025#include "base/bit_utils.h"
26#include "base/file_magic.h"
27#include "base/mem_map.h"
28#include "base/os.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070029#include "base/stl_util.h"
David Srbecky052f5fb2023-02-13 12:42:12 +000030#include "base/systrace.h"
31#include "base/unix_file/fd_file.h"
32#include "base/zip_archive.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "compact_dex_file.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070034#include "dex_file.h"
35#include "dex_file_verifier.h"
Mathieu Chartier292567e2017-10-12 13:24:38 -070036#include "standard_dex_file.h"
David Sehr5a1f6292018-01-19 11:08:51 -080037
Mathieu Chartier79c87da2017-10-10 11:54:29 -070038namespace art {
39
Yabin Cuibdbd7072023-03-03 15:17:36 -080040#if defined(STATIC_LIB)
41#define DEXFILE_SCOPED_TRACE(name)
42#else
43#define DEXFILE_SCOPED_TRACE(name) ScopedTrace trace(name)
44#endif
45
David Sehr5a1f6292018-01-19 11:08:51 -080046namespace {
47
David Srbeckyf8580e52023-02-23 18:27:04 +000048// 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.
52static constexpr size_t kWarnOnManyDexFilesThreshold = 100;
53
David Srbecky052f5fb2023-02-13 12:42:12 +000054using android::base::StringPrintf;
55
David Sehr5a1f6292018-01-19 11:08:51 -080056class VectorContainer : public DexFileContainer {
57 public:
58 explicit VectorContainer(std::vector<uint8_t>&& vector) : vector_(std::move(vector)) { }
Roland Levillainf73caca2018-08-24 17:19:07 +010059 ~VectorContainer() override { }
David Sehr5a1f6292018-01-19 11:08:51 -080060
David Srbecky2bcd3a02023-01-25 18:57:39 +000061 bool IsReadOnly() const override { return true; }
David Sehr5a1f6292018-01-19 11:08:51 -080062
David Srbecky052f5fb2023-02-13 12:42:12 +000063 bool EnableWrite() override { return true; }
David Sehr5a1f6292018-01-19 11:08:51 -080064
David Srbecky052f5fb2023-02-13 12:42:12 +000065 bool DisableWrite() override { return false; }
David Sehr5a1f6292018-01-19 11:08:51 -080066
David Srbecky2bcd3a02023-01-25 18:57:39 +000067 const uint8_t* Begin() const override { return vector_.data(); }
68
69 const uint8_t* End() const override { return vector_.data() + vector_.size(); }
70
David Sehr5a1f6292018-01-19 11:08:51 -080071 private:
72 std::vector<uint8_t> vector_;
73 DISALLOW_COPY_AND_ASSIGN(VectorContainer);
74};
75
David Srbecky052f5fb2023-02-13 12:42:12 +000076class MemMapContainer : public DexFileContainer {
77 public:
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
81 int GetPermissions() const {
82 if (!mem_map_.IsValid()) {
83 return 0;
84 } else {
85 return mem_map_.GetProtect();
86 }
87 }
88
89 bool IsReadOnly() const override { return GetPermissions() == PROT_READ; }
90
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
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
109 const uint8_t* Begin() const override { return mem_map_.Begin(); }
110
111 const uint8_t* End() const override { return mem_map_.End(); }
112
113 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
David Sehr5a1f6292018-01-19 11:08:51 -0800121} // namespace
122
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700123bool DexFileLoader::IsMagicValid(uint32_t magic) {
124 return IsMagicValid(reinterpret_cast<uint8_t*>(&magic));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700125}
126
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700127bool DexFileLoader::IsMagicValid(const uint8_t* magic) {
128 return StandardDexFile::IsMagicValid(magic) ||
129 CompactDexFile::IsMagicValid(magic);
130}
131
132bool 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;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700140}
141
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700142bool DexFileLoader::IsMultiDexLocation(const char* location) {
143 return strrchr(location, kMultiDexSeparator) != nullptr;
144}
145
146std::string DexFileLoader::GetMultiDexClassesDexName(size_t index) {
147 return (index == 0) ? "classes.dex" : StringPrintf("classes%zu.dex", index + 1);
148}
149
150std::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
156std::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);
David Sehr10db8fe2018-07-18 11:01:20 -0700161#ifdef _WIN32
162 // Warning: No symbolic link processing here.
163 PLOG(WARNING) << "realpath is unsupported on Windows.";
164#else
Nicolas Geoffray0d0f3162018-05-10 12:55:40 +0100165 // 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.
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700167 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;
David Sehr10db8fe2018-07-18 11:01:20 -0700170 }
171#endif
172 if (suffix[0] == 0) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700173 return base_location;
174 } else {
175 return dex_location;
176 }
177}
178
David Sehr013fd802018-01-11 22:55:24 -0800179// All of the implementations here should be independent of the runtime.
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700180
David Srbecky052f5fb2023-02-13 12:42:12 +0000181DexFileLoader::DexFileLoader(const uint8_t* base, size_t size, const std::string& location)
David Srbecky0b9b3302023-02-24 19:03:51 +0000182 : DexFileLoader(std::make_shared<MemoryDexFileContainer>(base, base + size), location) {}
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700183
David Srbecky052f5fb2023-02-13 12:42:12 +0000184DexFileLoader::DexFileLoader(std::vector<uint8_t>&& memory, const std::string& location)
David Srbecky0b9b3302023-02-24 19:03:51 +0000185 : DexFileLoader(std::make_shared<VectorContainer>(std::move(memory)), location) {}
Vladimir Markof4d05ff2021-04-14 09:39:04 +0000186
David Srbecky052f5fb2023-02-13 12:42:12 +0000187DexFileLoader::DexFileLoader(MemMap&& mem_map, const std::string& location)
David Srbecky0b9b3302023-02-24 19:03:51 +0000188 : DexFileLoader(std::make_shared<MemMapContainer>(std::move(mem_map)), location) {}
David Srbecky052f5fb2023-02-13 12:42:12 +0000189
190std::unique_ptr<const DexFile> DexFileLoader::Open(uint32_t location_checksum,
David Srbecky2bcd3a02023-01-25 18:57:39 +0000191 const OatDexFile* oat_dex_file,
192 bool verify,
193 bool verify_checksum,
David Srbecky052f5fb2023-02-13 12:42:12 +0000194 std::string* error_msg) {
Yabin Cuibdbd7072023-03-03 15:17:36 -0800195 DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_);
David Srbecky052f5fb2023-02-13 12:42:12 +0000196
197 uint32_t magic;
198 if (!InitAndReadMagic(&magic, error_msg) || !MapRootContainer(error_msg)) {
199 DCHECK(!error_msg->empty());
200 return {};
201 }
David Srbecky1dfac992023-03-07 18:58:11 +0000202 DCHECK(root_container_ != nullptr);
David Srbecky0b9b3302023-02-24 19:03:51 +0000203 std::unique_ptr<const DexFile> dex_file = OpenCommon(root_container_,
David Srbecky1dfac992023-03-07 18:58:11 +0000204 root_container_->Begin(),
205 root_container_->Size(),
David Srbecky052f5fb2023-02-13 12:42:12 +0000206 location_,
207 location_checksum,
208 oat_dex_file,
209 verify,
210 verify_checksum,
211 error_msg,
212 nullptr);
213 return dex_file;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700214}
215
David Srbecky052f5fb2023-02-13 12:42:12 +0000216bool 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
240bool 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 }
David Srbecky0b9b3302023-02-24 19:03:51 +0000269 root_container_ = std::make_shared<MemMapContainer>(std::move(map));
David Srbecky052f5fb2023-02-13 12:42:12 +0000270 return true;
271}
272
273bool 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) {
Yabin Cuibdbd7072023-03-03 15:17:36 -0800279 DEXFILE_SCOPED_TRACE(std::string("Open dex file ") + location_);
David Srbecky052f5fb2023-02-13 12:42:12 +0000280
David Sehr5a1f6292018-01-19 11:08:51 -0800281 DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr";
David Srbecky052f5fb2023-02-13 12:42:12 +0000282
283 uint32_t magic;
284 if (!InitAndReadMagic(&magic, error_msg)) {
285 return false;
286 }
287
David Sehr5a1f6292018-01-19 11:08:51 -0800288 if (IsZipMagic(magic)) {
David Srbecky052f5fb2023-02-13 12:42:12 +0000289 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));
David Sehr5a1f6292018-01-19 11:08:51 -0800294 if (zip_archive.get() == nullptr) {
295 DCHECK(!error_msg->empty());
296 return false;
297 }
David Srbeckyf8580e52023-02-23 18:27:04 +0000298 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 }
David Sehr5a1f6292018-01-19 11:08:51 -0800323 }
324 if (IsMagicValid(magic)) {
David Srbecky052f5fb2023-02-13 12:42:12 +0000325 if (!MapRootContainer(error_msg)) {
326 return false;
327 }
David Srbecky1dfac992023-03-07 18:58:11 +0000328 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);
David Sehr5a1f6292018-01-19 11:08:51 -0800340 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");
David Sehr013fd802018-01-11 22:55:24 -0800348 return false;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700349}
350
David Srbecky1dfac992023-03-07 18:58:11 +0000351std::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 }
David Srbeckyf8580e52023-02-23 18:27:04 +0000365 if (error_code != nullptr) {
366 *error_code = DexFileLoaderErrorCode::kDexFileError;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700367 }
368 std::unique_ptr<DexFile> dex_file;
David Srbecky1dfac992023-03-07 18:58:11 +0000369 auto header = reinterpret_cast<const DexFile::Header*>(base);
David Sehr0b426772018-07-03 23:03:42 +0000370 if (size >= sizeof(StandardDexFile::Header) && StandardDexFile::IsMagicValid(base)) {
David Srbecky1dfac992023-03-07 18:58:11 +0000371 uint32_t checksum = location_checksum.value_or(header->checksum_);
372 dex_file.reset(new StandardDexFile(base, size, location, checksum, oat_dex_file, container));
David Sehr0b426772018-07-03 23:03:42 +0000373 } else if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(base)) {
David Srbecky1dfac992023-03-07 18:58:11 +0000374 uint32_t checksum = location_checksum.value_or(header->checksum_);
375 dex_file.reset(new CompactDexFile(base, size, location, checksum, oat_dex_file, container));
Mathieu Chartier700a9852018-02-06 18:27:38 -0800376 } else {
David Srbecky1dfac992023-03-07 18:58:11 +0000377 *error_msg = StringPrintf("Invalid or truncated dex file '%s'", location.c_str());
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700378 }
379 if (dex_file == nullptr) {
David Srbecky052f5fb2023-02-13 12:42:12 +0000380 *error_msg =
381 StringPrintf("Failed to open dex file '%s': %s", location.c_str(), error_msg->c_str());
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700382 return nullptr;
383 }
384 if (!dex_file->Init(error_msg)) {
385 dex_file.reset();
386 return nullptr;
387 }
David Srbecky771b44f2023-02-24 18:42:20 +0000388 // NB: Dex verifier does not understand the compact dex format.
389 if (verify && !dex_file->IsCompactDexFile()) {
Yabin Cuibdbd7072023-03-03 15:17:36 -0800390 DEXFILE_SCOPED_TRACE(std::string("Verify dex file ") + location);
David Srbecky14dc1bc2023-03-06 16:40:21 +0000391 if (!dex::Verify(dex_file.get(), location.c_str(), verify_checksum, error_msg)) {
David Srbeckyf8580e52023-02-23 18:27:04 +0000392 if (error_code != nullptr) {
393 *error_code = DexFileLoaderErrorCode::kVerifyError;
David Srbecky052f5fb2023-02-13 12:42:12 +0000394 }
395 return nullptr;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700396 }
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700397 }
David Srbeckyf8580e52023-02-23 18:27:04 +0000398 if (error_code != nullptr) {
399 *error_code = DexFileLoaderErrorCode::kNoError;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700400 }
401 return dex_file;
402}
403
David Srbeckyf8580e52023-02-23 18:27:04 +0000404bool 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 {
David Sehr5a1f6292018-01-19 11:08:51 -0800412 CHECK(!location.empty());
David Srbecky052f5fb2023-02-13 12:42:12 +0000413 std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
David Sehr5a1f6292018-01-19 11:08:51 -0800414 if (zip_entry == nullptr) {
Dario Frenie166fac2018-07-16 11:08:03 +0100415 *error_code = DexFileLoaderErrorCode::kEntryNotFound;
David Srbeckyf8580e52023-02-23 18:27:04 +0000416 return false;
David Sehr5a1f6292018-01-19 11:08:51 -0800417 }
418 if (zip_entry->GetUncompressedLength() == 0) {
419 *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100420 *error_code = DexFileLoaderErrorCode::kDexFileError;
David Srbeckyf8580e52023-02-23 18:27:04 +0000421 return false;
David Sehr5a1f6292018-01-19 11:08:51 -0800422 }
423
David Srbecky052f5fb2023-02-13 12:42:12 +0000424 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()) {
Yabin Cuibdbd7072023-03-03 15:17:36 -0800446 DEXFILE_SCOPED_TRACE(std::string("Extract dex file ") + location);
David Srbecky052f5fb2023-02-13 12:42:12 +0000447
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()) {
David Sehr5a1f6292018-01-19 11:08:51 -0800453 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
454 error_msg->c_str());
Dario Frenie166fac2018-07-16 11:08:03 +0100455 *error_code = DexFileLoaderErrorCode::kExtractToMemoryError;
David Srbeckyf8580e52023-02-23 18:27:04 +0000456 return false;
David Sehr5a1f6292018-01-19 11:08:51 -0800457 }
David Srbecky0b9b3302023-02-24 19:03:51 +0000458 auto container = std::make_shared<MemMapContainer>(std::move(map), is_file_map);
David Srbecky052f5fb2023-02-13 12:42:12 +0000459 container->SetIsZip();
David Srbeckyf8580e52023-02-23 18:27:04 +0000460 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 }
David Srbecky052f5fb2023-02-13 12:42:12 +0000465
David Srbecky1dfac992023-03-07 18:58:11 +0000466 std::unique_ptr<const DexFile> dex_file = OpenCommon(container,
467 container->Begin(),
468 container->Size(),
David Srbecky052f5fb2023-02-13 12:42:12 +0000469 location,
470 zip_entry->GetCrc32(),
471 /*oat_dex_file=*/nullptr,
472 verify,
473 verify_checksum,
474 error_msg,
David Srbeckyf8580e52023-02-23 18:27:04 +0000475 error_code);
476 if (dex_file == nullptr) {
477 return false;
David Srbecky052f5fb2023-02-13 12:42:12 +0000478 }
479 CHECK(dex_file->IsReadOnly()) << location;
David Srbeckyf8580e52023-02-23 18:27:04 +0000480 dex_files->push_back(std::move(dex_file));
481 return true;
David Sehr5a1f6292018-01-19 11:08:51 -0800482}
483
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700484} // namespace art