Commit 645e477c authored by Brian Carlstrom's avatar Brian Carlstrom
Browse files

Ensure OpenDexFilesFromImage closes file to prevent file descriptor leak

Test: m -j32 test-art-host
Bug: 32095919
Change-Id: I44541edeb403a508e1b9794ee09fb1f55e3f42bf
parent 47d522b2
......@@ -94,7 +94,7 @@ TEST_F(ElfWriterTest, dlsym) {
/*low_4gb*/false,
&error_msg));
CHECK(ef.get() != nullptr) << error_msg;
CHECK(ef->Load(false, /*low_4gb*/false, &error_msg)) << error_msg;
CHECK(ef->Load(file.get(), false, /*low_4gb*/false, &error_msg)) << error_msg;
EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
......
......@@ -401,7 +401,7 @@ PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) {
return ERROR_OAT_FILE;
}
const std::string& file_path = oat_in->GetFile().GetPath();
const std::string& file_path = oat_in->GetFilePath();
const OatHeader* oat_header = GetOatHeader(oat_in);
if (oat_header == nullptr) {
......@@ -804,7 +804,7 @@ bool PatchOat::PatchOatHeader(ElfFileImpl* oat_file) {
}
OatHeader* oat_header = reinterpret_cast<OatHeader*>(oat_file->Begin() + rodata_sec->sh_offset);
if (!oat_header->IsValid()) {
LOG(ERROR) << "Elf file " << oat_file->GetFile().GetPath() << " has an invalid oat header";
LOG(ERROR) << "Elf file " << oat_file->GetFilePath() << " has an invalid oat header";
return false;
}
oat_header->RelocateOat(delta_);
......@@ -812,10 +812,11 @@ bool PatchOat::PatchOatHeader(ElfFileImpl* oat_file) {
}
bool PatchOat::PatchElf() {
if (oat_file_->Is64Bit())
if (oat_file_->Is64Bit()) {
return PatchElf<ElfFileImpl64>(oat_file_->GetImpl64());
else
} else {
return PatchElf<ElfFileImpl32>(oat_file_->GetImpl32());
}
}
template <typename ElfFileImpl>
......
This diff is collapsed.
......@@ -53,7 +53,7 @@ class ElfFile {
~ElfFile();
// Load segments into memory based on PT_LOAD program headers
bool Load(bool executable, bool low_4gb, std::string* error_msg);
bool Load(File* file, bool executable, bool low_4gb, std::string* error_msg);
const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
......@@ -65,7 +65,7 @@ class ElfFile {
// The end of the memory map address range for this ELF file.
uint8_t* End() const;
const File& GetFile() const;
const std::string& GetFilePath() const;
bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) const;
......
......@@ -61,8 +61,8 @@ class ElfFileImpl {
std::string* error_msg);
~ElfFileImpl();
const File& GetFile() const {
return *file_;
const std::string& GetFilePath() const {
return file_path_;
}
uint8_t* Begin() const {
......@@ -119,7 +119,7 @@ class ElfFileImpl {
// Load segments into memory based on PT_LOAD program headers.
// executable is true at run time, false at compile time.
bool Load(bool executable, bool low_4gb, std::string* error_msg);
bool Load(File* file, bool executable, bool low_4gb, std::string* error_msg);
bool Fixup(Elf_Addr base_address);
bool FixupDynamic(Elf_Addr base_address);
......@@ -132,14 +132,14 @@ class ElfFileImpl {
static void ApplyOatPatches(const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
uint8_t* to_patch, const uint8_t* to_patch_end);
bool Strip(std::string* error_msg);
bool Strip(File* file, std::string* error_msg);
private:
ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base);
bool Setup(int prot, int flags, bool low_4gb, std::string* error_msg);
bool Setup(File* file, int prot, int flags, bool low_4gb, std::string* error_msg);
bool SetMap(MemMap* map, std::string* error_msg);
bool SetMap(File* file, MemMap* map, std::string* error_msg);
uint8_t* GetProgramHeadersStart() const;
uint8_t* GetSectionHeadersStart() const;
......@@ -163,7 +163,7 @@ class ElfFileImpl {
const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const;
// Check that certain sections and their dependencies exist.
bool CheckSectionsExist(std::string* error_msg) const;
bool CheckSectionsExist(File* file, std::string* error_msg) const;
// Check that the link of the first section links to the second section.
bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const;
......@@ -191,7 +191,7 @@ class ElfFileImpl {
// Lookup a string by section type. Returns null for special 0 offset.
const char* GetString(Elf_Word section_type, Elf_Word) const;
const File* const file_;
const std::string file_path_;
const bool writable_;
const bool program_header_only_;
......
......@@ -898,7 +898,7 @@ bool ElfOatFile::ElfFileOpen(File* file,
DCHECK(!error_msg->empty());
return false;
}
bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
DCHECK(loaded || !error_msg->empty());
return loaded;
}
......
......@@ -851,7 +851,7 @@ static bool OpenDexFilesFromImage(const std::string& image_location,
return false;
}
std::string error_msg;
std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file.release(),
std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file.get(),
false,
false,
/*low_4gb*/false,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment