Remove size argument from DexFile constructors.
We know the DEX size from the DEX header, and we know the range of
valid underlying memory from DexFileContainer, so it is redundant.
Test: ./art/test.py -b --host 001
Change-Id: I0bd0fe25797ead4df864d97a2bfa8608f75bd694
diff --git a/compiler/optimizing/optimizing_unit_test.h b/compiler/optimizing/optimizing_unit_test.h
index 2e05c41..77e6420 100644
--- a/compiler/optimizing/optimizing_unit_test.h
+++ b/compiler/optimizing/optimizing_unit_test.h
@@ -244,7 +244,6 @@
auto container =
std::make_shared<MemoryDexFileContainer>(dex_data, sizeof(StandardDexFile::Header));
dex_files_.emplace_back(new StandardDexFile(dex_data,
- sizeof(StandardDexFile::Header),
"no_location",
/*location_checksum*/ 0,
/*oat_dex_file*/ nullptr,
diff --git a/libartbase/base/common_art_test.h b/libartbase/base/common_art_test.h
index 2a28011..64e251e 100644
--- a/libartbase/base/common_art_test.h
+++ b/libartbase/base/common_art_test.h
@@ -183,9 +183,15 @@
const std::unique_ptr<const DexFile>& dex = dex_files[0];
CHECK(dex->EnableWrite()) << "Failed to enable write";
DexFile* dex_file = const_cast<DexFile*>(dex.get());
+ size_t original_size = dex_file->Size();
mutator(dex_file);
- const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
- if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
+ // NB: mutation might have changed the DEX size in the header.
+ std::vector<uint8_t> copy(dex_file->Begin(), dex_file->Begin() + original_size);
+ copy.resize(dex_file->Size()); // Shrink/expand to new size.
+ uint32_t checksum = DexFile::CalculateChecksum(copy.data(), copy.size());
+ CHECK_GE(copy.size(), sizeof(DexFile::Header));
+ reinterpret_cast<DexFile::Header*>(copy.data())->checksum_ = checksum;
+ if (!output_dex->WriteFully(copy.data(), copy.size())) {
return false;
}
if (output_dex->Flush() != 0) {
diff --git a/libdexfile/dex/compact_dex_file.cc b/libdexfile/dex/compact_dex_file.cc
index 4fad26c..9bc38f3 100644
--- a/libdexfile/dex/compact_dex_file.cc
+++ b/libdexfile/dex/compact_dex_file.cc
@@ -82,13 +82,11 @@
}
CompactDexFile::CompactDexFile(const uint8_t* base,
- size_t size,
const std::string& location,
uint32_t location_checksum,
const OatDexFile* oat_dex_file,
std::shared_ptr<DexFileContainer> container)
: DexFile(base,
- size,
location,
location_checksum,
oat_dex_file,
diff --git a/libdexfile/dex/compact_dex_file.h b/libdexfile/dex/compact_dex_file.h
index 53af501..468ff49 100644
--- a/libdexfile/dex/compact_dex_file.h
+++ b/libdexfile/dex/compact_dex_file.h
@@ -306,7 +306,6 @@
private:
CompactDexFile(const uint8_t* base,
- size_t size,
const std::string& location,
uint32_t location_checksum,
const OatDexFile* oat_dex_file,
diff --git a/libdexfile/dex/dex_file.cc b/libdexfile/dex/dex_file.cc
index 55a9c08..be1846a 100644
--- a/libdexfile/dex/dex_file.cc
+++ b/libdexfile/dex/dex_file.cc
@@ -97,15 +97,13 @@
}
DexFile::DexFile(const uint8_t* base,
- size_t size,
const std::string& location,
uint32_t location_checksum,
const OatDexFile* oat_dex_file,
std::shared_ptr<DexFileContainer> container,
bool is_compact_dex)
: begin_(base),
- size_(size),
- data_(GetDataRange(base, size, container.get())),
+ data_(GetDataRange(base, container.get())),
location_(location),
location_checksum_(location_checksum),
header_(reinterpret_cast<const Header*>(base)),
@@ -125,7 +123,6 @@
is_compact_dex_(is_compact_dex),
hiddenapi_domain_(hiddenapi::Domain::kApplication) {
CHECK(begin_ != nullptr) << GetLocation();
- CHECK_GT(size_, 0U) << GetLocation();
// Check base (=header) alignment.
// Must be 4-byte aligned to avoid undefined behavior when accessing
// any of the sections via a pointer.
@@ -191,11 +188,15 @@
return true;
}
-ArrayRef<const uint8_t> DexFile::GetDataRange(const uint8_t* data,
- size_t size,
- DexFileContainer* container) {
+ArrayRef<const uint8_t> DexFile::GetDataRange(const uint8_t* data, DexFileContainer* container) {
CHECK(container != nullptr);
- if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(data)) {
+ CHECK_GE(data, container->Begin());
+ CHECK_LE(data, container->End());
+ size_t size = container->End() - data;
+ if (size >= sizeof(StandardDexFile::Header) && StandardDexFile::IsMagicValid(data)) {
+ auto header = reinterpret_cast<const DexFile::Header*>(data);
+ size = std::min<size_t>(size, header->file_size_);
+ } else if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(data)) {
auto header = reinterpret_cast<const CompactDexFile::Header*>(data);
// TODO: Remove. This is a hack. See comment of the Data method.
ArrayRef<const uint8_t> separate_data = container->Data();
@@ -205,6 +206,9 @@
// Shared compact dex data is located at the end after all dex files.
data += header->data_off_;
size = header->data_size_;
+ } else {
+ // Invalid dex file header.
+ // Some tests create dex files using just zeroed memory.
}
return {data, size};
}
diff --git a/libdexfile/dex/dex_file.h b/libdexfile/dex/dex_file.h
index 1b652c3..36d490c 100644
--- a/libdexfile/dex/dex_file.h
+++ b/libdexfile/dex/dex_file.h
@@ -768,13 +768,9 @@
return begin_;
}
- size_t Size() const {
- return size_;
- }
+ size_t Size() const { return header_->file_size_; }
- static ArrayRef<const uint8_t> GetDataRange(const uint8_t* data,
- size_t size,
- DexFileContainer* container);
+ static ArrayRef<const uint8_t> GetDataRange(const uint8_t* data, DexFileContainer* container);
const uint8_t* DataBegin() const { return data_.data(); }
@@ -867,7 +863,6 @@
static constexpr uint32_t kDefaultMethodsVersion = 37;
DexFile(const uint8_t* base,
- size_t size,
const std::string& location,
uint32_t location_checksum,
const OatDexFile* oat_dex_file,
@@ -887,9 +882,6 @@
// The base address of the memory mapping.
const uint8_t* const begin_;
- // The size of the underlying memory allocation in bytes.
- const size_t size_;
-
// Data memory range: Most dex offsets are relative to this memory range.
// Standard dex: same as (begin_, size_).
// Compact: shared data which is located after all non-shared data.
diff --git a/libdexfile/dex/dex_file_loader.cc b/libdexfile/dex/dex_file_loader.cc
index e0d4c77..996204f 100644
--- a/libdexfile/dex/dex_file_loader.cc
+++ b/libdexfile/dex/dex_file_loader.cc
@@ -435,10 +435,10 @@
auto header = reinterpret_cast<const DexFile::Header*>(base);
if (size >= sizeof(StandardDexFile::Header) && StandardDexFile::IsMagicValid(base)) {
uint32_t checksum = location_checksum.value_or(header->checksum_);
- dex_file.reset(new StandardDexFile(base, size, location, checksum, oat_dex_file, container));
+ dex_file.reset(new StandardDexFile(base, location, checksum, oat_dex_file, container));
} else if (size >= sizeof(CompactDexFile::Header) && CompactDexFile::IsMagicValid(base)) {
uint32_t checksum = location_checksum.value_or(header->checksum_);
- dex_file.reset(new CompactDexFile(base, size, location, checksum, oat_dex_file, container));
+ dex_file.reset(new CompactDexFile(base, location, checksum, oat_dex_file, container));
} else {
*error_msg = StringPrintf("Invalid or truncated dex file '%s'", location.c_str());
}
diff --git a/libdexfile/dex/dex_file_verifier_test.cc b/libdexfile/dex/dex_file_verifier_test.cc
index a2f2e93..d67d9a9 100644
--- a/libdexfile/dex/dex_file_verifier_test.cc
+++ b/libdexfile/dex/dex_file_verifier_test.cc
@@ -60,7 +60,7 @@
protected:
DexFile* GetDexFile(const uint8_t* dex_bytes, size_t length) {
auto container = std::make_shared<MemoryDexFileContainer>(dex_bytes, length);
- return new StandardDexFile(dex_bytes, length, "tmp", 0, nullptr, std::move(container));
+ return new StandardDexFile(dex_bytes, "tmp", 0, nullptr, std::move(container));
}
void VerifyModification(const char* dex_file_base64_content,
diff --git a/libdexfile/dex/standard_dex_file.h b/libdexfile/dex/standard_dex_file.h
index 52c7684..fe0223a 100644
--- a/libdexfile/dex/standard_dex_file.h
+++ b/libdexfile/dex/standard_dex_file.h
@@ -114,14 +114,12 @@
private:
StandardDexFile(const uint8_t* base,
- size_t size,
const std::string& location,
uint32_t location_checksum,
const OatDexFile* oat_dex_file,
// Shared since several dex files may be stored in the same logical container.
std::shared_ptr<DexFileContainer> container)
: DexFile(base,
- size,
location,
location_checksum,
oat_dex_file,
diff --git a/runtime/class_linker_test.cc b/runtime/class_linker_test.cc
index cc43888..605b1c4 100644
--- a/runtime/class_linker_test.cc
+++ b/runtime/class_linker_test.cc
@@ -1546,12 +1546,8 @@
auto container =
std::make_shared<MemoryDexFileContainer>(old_dex_file->Begin(), old_dex_file->Size());
- std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
- old_dex_file->Size(),
- location->ToModifiedUtf8(),
- 0u,
- nullptr,
- std::move(container)));
+ std::unique_ptr<DexFile> dex_file(new StandardDexFile(
+ old_dex_file->Begin(), location->ToModifiedUtf8(), 0u, nullptr, std::move(container)));
// Make a copy of the dex cache with changed name.
dex_cache.Assign(class_linker->AllocAndInitializeDexCache(Thread::Current(),
*dex_file,