summaryrefslogtreecommitdiff
path: root/libdexfile/dex/dex_file_loader.cc
diff options
context:
space:
mode:
author David Sehr <sehr@google.com> 2018-06-28 16:59:00 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2018-06-28 16:59:00 +0000
commitd20a4d76c33cd6e609ad6b1b3cde09fdcbdde05c (patch)
treeccbf6e69ca17f4677186f4e148e1f9baf8bc7ae4 /libdexfile/dex/dex_file_loader.cc
parentb5271dd44a30f498689e503340d3c8d01bf31f07 (diff)
parentb095f022a9683a9123018c01e22595cf969fd88b (diff)
Merge "Refactor DexFile ownership"
Diffstat (limited to 'libdexfile/dex/dex_file_loader.cc')
-rw-r--r--libdexfile/dex/dex_file_loader.cc92
1 files changed, 45 insertions, 47 deletions
diff --git a/libdexfile/dex/dex_file_loader.cc b/libdexfile/dex/dex_file_loader.cc
index 457addf114..156f655150 100644
--- a/libdexfile/dex/dex_file_loader.cc
+++ b/libdexfile/dex/dex_file_loader.cc
@@ -54,8 +54,17 @@ class VectorContainer : public DexFileContainer {
return false;
}
+ uint8_t* Begin() OVERRIDE {
+ return vector_.data();
+ }
+
+ size_t Size() OVERRIDE {
+ return vector_.size();
+ }
+
private:
std::vector<uint8_t> vector_;
+
DISALLOW_COPY_AND_ASSIGN(VectorContainer);
};
@@ -224,17 +233,14 @@ std::unique_ptr<const DexFile> DexFileLoader::Open(const uint8_t* base,
bool verify,
bool verify_checksum,
std::string* error_msg) const {
- return OpenCommon(base,
- size,
- /*data_base*/ nullptr,
- /*data_size*/ 0,
+ return OpenCommon(std::make_unique<NonOwningDexFileContainer>(base, size),
+ std::make_unique<EmptyDexFileContainer>(),
location,
location_checksum,
oat_dex_file,
verify,
verify_checksum,
error_msg,
- /*container*/ nullptr,
/*verify_result*/ nullptr);
}
@@ -249,17 +255,14 @@ std::unique_ptr<const DexFile> DexFileLoader::OpenWithDataSection(
bool verify,
bool verify_checksum,
std::string* error_msg) const {
- return OpenCommon(base,
- size,
- data_base,
- data_size,
+ return OpenCommon(std::make_unique<NonOwningDexFileContainer>(base, size),
+ std::make_unique<NonOwningDexFileContainer>(data_base, data_size),
location,
location_checksum,
oat_dex_file,
verify,
verify_checksum,
error_msg,
- /*container*/ nullptr,
/*verify_result*/ nullptr);
}
@@ -307,49 +310,47 @@ bool DexFileLoader::OpenAll(
return false;
}
-std::unique_ptr<DexFile> DexFileLoader::OpenCommon(const uint8_t* base,
- size_t size,
- const uint8_t* data_base,
- size_t data_size,
+std::unique_ptr<DexFile> DexFileLoader::OpenCommon(std::unique_ptr<DexFileContainer> main_section,
+ std::unique_ptr<DexFileContainer> data_section,
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,
VerifyResult* verify_result) {
if (verify_result != nullptr) {
*verify_result = VerifyResult::kVerifyNotAttempted;
}
std::unique_ptr<DexFile> dex_file;
- if (size >= sizeof(StandardDexFile::Header) && StandardDexFile::IsMagicValid(base)) {
- if (data_size != 0) {
- CHECK_EQ(base, data_base) << "Unsupported for standard dex";
+ if (main_section->Size() >= sizeof(StandardDexFile::Header) &&
+ StandardDexFile::IsMagicValid(main_section->Begin())) {
+ if (data_section->Size() != 0) {
+ CHECK_EQ(main_section->Begin(), data_section->Begin()) << "Unsupported for standard dex";
}
- dex_file.reset(new StandardDexFile(base,
- size,
+ CHECK(main_section != nullptr);
+ CHECK(main_section->Begin() != nullptr);
+ dex_file.reset(new StandardDexFile(std::move(main_section),
location,
location_checksum,
- oat_dex_file,
- std::move(container)));
- } else if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(base)) {
- if (data_base == nullptr) {
+ oat_dex_file));
+ } else if (main_section->Size() >= sizeof(CompactDexFile::Header) &&
+ CompactDexFile::IsMagicValid(main_section->Begin())) {
+ if (data_section->Begin() == nullptr) {
// TODO: Is there a clean way to support both an explicit data section and reading the one
// from the header.
- CHECK_EQ(data_size, 0u);
- const CompactDexFile::Header* const header = CompactDexFile::Header::At(base);
- data_base = base + header->data_off_;
- data_size = header->data_size_;
+ CHECK_EQ(data_section->Size(), 0u);
+ const CompactDexFile::Header* const header =
+ CompactDexFile::Header::At(main_section->Begin());
+ data_section =
+ std::make_unique<NonOwningDexFileContainer>(main_section->Begin() + header->data_off_,
+ header->data_size_);
}
- dex_file.reset(new CompactDexFile(base,
- size,
- data_base,
- data_size,
+ dex_file.reset(new CompactDexFile(std::move(main_section),
+ std::move(data_section),
location,
location_checksum,
- oat_dex_file,
- std::move(container)));
+ oat_dex_file));
// Disable verification for CompactDex input.
verify = false;
} else {
@@ -409,19 +410,16 @@ std::unique_ptr<const DexFile> DexFileLoader::OpenOneDexFileFromZip(
return nullptr;
}
VerifyResult verify_result;
- std::unique_ptr<const DexFile> dex_file = OpenCommon(
- map.data(),
- map.size(),
- /*data_base*/ nullptr,
- /*data_size*/ 0u,
- location,
- zip_entry->GetCrc32(),
- /*oat_dex_file*/ nullptr,
- verify,
- verify_checksum,
- error_msg,
- std::make_unique<VectorContainer>(std::move(map)),
- &verify_result);
+ std::unique_ptr<const DexFile> dex_file =
+ OpenCommon(std::make_unique<VectorContainer>(std::move(map)),
+ std::make_unique<EmptyDexFileContainer>(),
+ location,
+ zip_entry->GetCrc32(),
+ /*oat_dex_file*/ nullptr,
+ verify,
+ verify_checksum,
+ error_msg,
+ &verify_result);
if (dex_file == nullptr) {
if (verify_result == VerifyResult::kVerifyNotAttempted) {
*error_code = ZipOpenErrorCode::kDexFileError;