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