diff options
author | 2023-10-13 16:32:09 +0000 | |
---|---|---|
committer | 2023-10-16 12:40:31 +0000 | |
commit | 4b7aef13e87be3e35de747fb10845057f9ddb712 (patch) | |
tree | 5e8a2311d2055d7556d7db25b1363db88b5ebff2 | |
parent | a51744169fcc555158068183eae6e1a88483592a (diff) |
Revert^2 "Remove size argument from DexFile constructors."
This reverts commit 26b99c916496abf14a2fd87290720742328462fa.
Reason for revert: Reland
Change-Id: Ifc9418a5c76d9d4252c661efe7c13d45ae8810a2
-rw-r--r-- | compiler/optimizing/optimizing_unit_test.h | 1 | ||||
-rw-r--r-- | libartbase/base/common_art_test.h | 10 | ||||
-rw-r--r-- | libdexfile/dex/compact_dex_file.cc | 2 | ||||
-rw-r--r-- | libdexfile/dex/compact_dex_file.h | 1 | ||||
-rw-r--r-- | libdexfile/dex/dex_file.cc | 20 | ||||
-rw-r--r-- | libdexfile/dex/dex_file.h | 12 | ||||
-rw-r--r-- | libdexfile/dex/dex_file_loader.cc | 4 | ||||
-rw-r--r-- | libdexfile/dex/dex_file_verifier_test.cc | 2 | ||||
-rw-r--r-- | libdexfile/dex/standard_dex_file.h | 2 | ||||
-rw-r--r-- | runtime/class_linker_test.cc | 8 |
10 files changed, 28 insertions, 34 deletions
diff --git a/compiler/optimizing/optimizing_unit_test.h b/compiler/optimizing/optimizing_unit_test.h index 2e05c41f01..77e6420df8 100644 --- a/compiler/optimizing/optimizing_unit_test.h +++ b/compiler/optimizing/optimizing_unit_test.h @@ -244,7 +244,6 @@ 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 2a28011387..64e251e16c 100644 --- a/libartbase/base/common_art_test.h +++ b/libartbase/base/common_art_test.h @@ -183,9 +183,15 @@ 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); - 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 4fad26c140..9bc38f3d3f 100644 --- a/libdexfile/dex/compact_dex_file.cc +++ b/libdexfile/dex/compact_dex_file.cc @@ -82,13 +82,11 @@ 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 53af501a28..468ff49ecd 100644 --- a/libdexfile/dex/compact_dex_file.h +++ b/libdexfile/dex/compact_dex_file.h @@ -306,7 +306,6 @@ 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 55a9c08662..be1846a5cf 100644 --- a/libdexfile/dex/dex_file.cc +++ b/libdexfile/dex/dex_file.cc @@ -97,15 +97,13 @@ 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), - 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 @@ 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. @@ -191,11 +188,15 @@ bool DexFile::CheckMagicAndVersion(std::string* error_msg) const { 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 @@ ArrayRef<const uint8_t> DexFile::GetDataRange(const uint8_t* data, // 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 1b652c3e94..bda5bce2fc 100644 --- a/libdexfile/dex/dex_file.h +++ b/libdexfile/dex/dex_file.h @@ -768,13 +768,9 @@ class DexFile { 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 @@ 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, @@ -887,8 +882,7 @@ 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_; + size_t unused_size_ = 0; // Preserve layout for DRM (b/305203031). // Data memory range: Most dex offsets are relative to this memory range. // Standard dex: same as (begin_, size_). diff --git a/libdexfile/dex/dex_file_loader.cc b/libdexfile/dex/dex_file_loader.cc index e0d4c77eea..996204f365 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, 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 a2f2e93f2f..d67d9a938d 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, 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 52c76842c1..fe0223ac89 100644 --- a/libdexfile/dex/standard_dex_file.h +++ b/libdexfile/dex/standard_dex_file.h @@ -114,14 +114,12 @@ 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 cc43888505..605b1c4ef6 100644 --- a/runtime/class_linker_test.cc +++ b/runtime/class_linker_test.cc @@ -1546,12 +1546,8 @@ 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(), - 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, |