summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Nicolas Geoffray <ngeoffray@google.com> 2023-10-13 14:52:55 +0000
committer Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2023-10-13 16:17:40 +0000
commit26b99c916496abf14a2fd87290720742328462fa (patch)
treebc4192e6e63861bcf86037c025416addb4c94d2b
parentc79c1912043294f088ec838c12d6d3f2d5b64aa0 (diff)
Revert "Remove size argument from DexFile constructors."
This reverts commit e49ab4d6d4f8226db09803458250f2b9f41abf43. Bug: 305203031 Reason for revert: b/305203031 Change-Id: I7109fcd07b691141f5a67a096688def3b2d6e57e
-rw-r--r--compiler/optimizing/optimizing_unit_test.h1
-rw-r--r--libartbase/base/common_art_test.h10
-rw-r--r--libdexfile/dex/compact_dex_file.cc2
-rw-r--r--libdexfile/dex/compact_dex_file.h1
-rw-r--r--libdexfile/dex/dex_file.cc20
-rw-r--r--libdexfile/dex/dex_file.h12
-rw-r--r--libdexfile/dex/dex_file_loader.cc4
-rw-r--r--libdexfile/dex/dex_file_verifier_test.cc2
-rw-r--r--libdexfile/dex/standard_dex_file.h2
-rw-r--r--runtime/class_linker_test.cc8
10 files changed, 35 insertions, 27 deletions
diff --git a/compiler/optimizing/optimizing_unit_test.h b/compiler/optimizing/optimizing_unit_test.h
index 77e6420df8..2e05c41f01 100644
--- a/compiler/optimizing/optimizing_unit_test.h
+++ b/compiler/optimizing/optimizing_unit_test.h
@@ -244,6 +244,7 @@ class OptimizingUnitTestHelper {
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 64e251e16c..2a28011387 100644
--- a/libartbase/base/common_art_test.h
+++ b/libartbase/base/common_art_test.h
@@ -183,15 +183,9 @@ class CommonArtTestImpl {
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);
- // 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())) {
+ const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
+ if (!output_dex->WriteFully(dex->Begin(), dex->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 9bc38f3d3f..4fad26c140 100644
--- a/libdexfile/dex/compact_dex_file.cc
+++ b/libdexfile/dex/compact_dex_file.cc
@@ -82,11 +82,13 @@ uint32_t CompactDexFile::CalculateChecksum() const {
}
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 468ff49ecd..53af501a28 100644
--- a/libdexfile/dex/compact_dex_file.h
+++ b/libdexfile/dex/compact_dex_file.h
@@ -306,6 +306,7 @@ class CompactDexFile : public DexFile {
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 be1846a5cf..55a9c08662 100644
--- a/libdexfile/dex/dex_file.cc
+++ b/libdexfile/dex/dex_file.cc
@@ -97,13 +97,15 @@ bool DexFile::DisableWrite() const {
}
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),
- data_(GetDataRange(base, container.get())),
+ size_(size),
+ data_(GetDataRange(base, size, container.get())),
location_(location),
location_checksum_(location_checksum),
header_(reinterpret_cast<const Header*>(base)),
@@ -123,6 +125,7 @@ DexFile::DexFile(const uint8_t* base,
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.
@@ -188,15 +191,11 @@ bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
return true;
}
-ArrayRef<const uint8_t> DexFile::GetDataRange(const uint8_t* data, DexFileContainer* container) {
+ArrayRef<const uint8_t> DexFile::GetDataRange(const uint8_t* data,
+ size_t size,
+ DexFileContainer* container) {
CHECK(container != nullptr);
- 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)) {
+ 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();
@@ -206,9 +205,6 @@ ArrayRef<const uint8_t> DexFile::GetDataRange(const uint8_t* data, DexFileContai
// 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 36d490c0c4..1b652c3e94 100644
--- a/libdexfile/dex/dex_file.h
+++ b/libdexfile/dex/dex_file.h
@@ -768,9 +768,13 @@ class DexFile {
return begin_;
}
- size_t Size() const { return header_->file_size_; }
+ size_t Size() const {
+ return size_;
+ }
- static ArrayRef<const uint8_t> GetDataRange(const uint8_t* data, DexFileContainer* container);
+ static ArrayRef<const uint8_t> GetDataRange(const uint8_t* data,
+ size_t size,
+ DexFileContainer* container);
const uint8_t* DataBegin() const { return data_.data(); }
@@ -863,6 +867,7 @@ class DexFile {
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,
@@ -882,6 +887,9 @@ class DexFile {
// 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 996204f365..e0d4c77eea 100644
--- a/libdexfile/dex/dex_file_loader.cc
+++ b/libdexfile/dex/dex_file_loader.cc
@@ -435,10 +435,10 @@ std::unique_ptr<DexFile> DexFileLoader::OpenCommon(std::shared_ptr<DexFileContai
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, location, checksum, oat_dex_file, container));
+ dex_file.reset(new StandardDexFile(base, size, 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, location, checksum, oat_dex_file, container));
+ dex_file.reset(new CompactDexFile(base, size, 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 d67d9a938d..a2f2e93f2f 100644
--- a/libdexfile/dex/dex_file_verifier_test.cc
+++ b/libdexfile/dex/dex_file_verifier_test.cc
@@ -60,7 +60,7 @@ class DexFileVerifierTest : public testing::Test {
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, "tmp", 0, nullptr, std::move(container));
+ return new StandardDexFile(dex_bytes, length, "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 fe0223ac89..52c76842c1 100644
--- a/libdexfile/dex/standard_dex_file.h
+++ b/libdexfile/dex/standard_dex_file.h
@@ -114,12 +114,14 @@ class StandardDexFile : public DexFile {
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 605b1c4ef6..cc43888505 100644
--- a/runtime/class_linker_test.cc
+++ b/runtime/class_linker_test.cc
@@ -1546,8 +1546,12 @@ TEST_F(ClassLinkerTest, RegisterDexFileName) {
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(), location->ToModifiedUtf8(), 0u, nullptr, std::move(container)));
+ std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
+ old_dex_file->Size(),
+ 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,