[BOLT][NFC] Handle "dynamic section sizes should match"

Address fuzzer crash on malformed input

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D121070
This commit is contained in:
Amir Ayupov
2022-03-08 09:17:41 -08:00
parent cfb9e474ae
commit 1e016c3bd5
2 changed files with 17 additions and 16 deletions

View File

@@ -260,9 +260,9 @@ private:
void disassemblePLTSectionX86(BinarySection &Section, uint64_t EntrySize);
/// ELF-specific part. TODO: refactor into new class.
#define ELF_FUNCTION(FUNC) \
template <typename ELFT> void FUNC(object::ELFObjectFile<ELFT> *Obj); \
void FUNC() { \
#define ELF_FUNCTION(TYPE, FUNC) \
template <typename ELFT> TYPE FUNC(object::ELFObjectFile<ELFT> *Obj); \
TYPE FUNC() { \
if (auto *ELF32LE = dyn_cast<object::ELF32LEObjectFile>(InputFile)) \
return FUNC(ELF32LE); \
if (auto *ELF64LE = dyn_cast<object::ELF64LEObjectFile>(InputFile)) \
@@ -277,25 +277,25 @@ private:
void patchELFPHDRTable();
/// Create section header table.
ELF_FUNCTION(patchELFSectionHeaderTable);
ELF_FUNCTION(void, patchELFSectionHeaderTable);
/// Create the regular symbol table and patch dyn symbol tables.
ELF_FUNCTION(patchELFSymTabs);
ELF_FUNCTION(void, patchELFSymTabs);
/// Read dynamic section/segment of ELF.
ELF_FUNCTION(readELFDynamic);
ELF_FUNCTION(Error, readELFDynamic);
/// Patch dynamic section/segment of ELF.
ELF_FUNCTION(patchELFDynamic);
ELF_FUNCTION(void, patchELFDynamic);
/// Patch .got
ELF_FUNCTION(patchELFGOT);
ELF_FUNCTION(void, patchELFGOT);
/// Patch allocatable relocation sections.
ELF_FUNCTION(patchELFAllocatableRelaSections);
ELF_FUNCTION(void, patchELFAllocatableRelaSections);
/// Finalize memory image of section header string table.
ELF_FUNCTION(finalizeSectionStringTable);
ELF_FUNCTION(void, finalizeSectionStringTable);
/// Return a name of the input file section in the output file.
template <typename ELFObjType, typename ELFShdrTy>

View File

@@ -1635,8 +1635,7 @@ Error RewriteInstance::readSpecialSections() {
parseSDTNotes();
// Read .dynamic/PT_DYNAMIC.
readELFDynamic();
return Error::success();
return readELFDynamic();
}
void RewriteInstance::adjustCommandLineOptions() {
@@ -5098,7 +5097,7 @@ void RewriteInstance::patchELFDynamic(ELFObjectFile<ELFT> *File) {
}
template <typename ELFT>
void RewriteInstance::readELFDynamic(ELFObjectFile<ELFT> *File) {
Error RewriteInstance::readELFDynamic(ELFObjectFile<ELFT> *File) {
const ELFFile<ELFT> &Obj = File->getELFFile();
using Elf_Phdr = typename ELFFile<ELFT>::Elf_Phdr;
@@ -5117,11 +5116,12 @@ void RewriteInstance::readELFDynamic(ELFObjectFile<ELFT> *File) {
outs() << "BOLT-INFO: static input executable detected\n";
// TODO: static PIE executable might have dynamic header
BC->IsStaticExecutable = true;
return;
return Error::success();
}
assert(DynamicPhdr->p_memsz == DynamicPhdr->p_filesz &&
"dynamic section sizes should match");
if (DynamicPhdr->p_memsz != DynamicPhdr->p_filesz)
return createStringError(errc::executable_format_error,
"dynamic section sizes should match");
// Go through all dynamic entries to locate entries of interest.
typename ELFT::DynRange DynamicEntries =
@@ -5165,6 +5165,7 @@ void RewriteInstance::readELFDynamic(ELFObjectFile<ELFT> *File) {
PLTRelocationsAddress.reset();
PLTRelocationsSize = 0;
}
return Error::success();
}
uint64_t RewriteInstance::getNewFunctionAddress(uint64_t OldAddress) {