[llvm-readobj] - Change how we create DynRegionInfo objects. NFCI.

Currently we have `checkDRI` and two `createDRIFrom` methods which
are used to create `DynRegionInfo` objects.

And we have an issue: constructions like:
`ObjF->getELFFile()->base() + P->p_offset`
that are used in `createDRIFrom` functions might overflow.

I had to revert `D85519` which triggered such UBSan failure.

This NFC, simplifies and generalizes how we create `DynRegionInfo` objects.
It will allow us to introduce more/better validation checks in a single place.
It also will allow to change `createDRI` to return `Expected<>` so
that we will be able to stop using the `reportError`, which
is used inside currently, and have a warning instead.

Differential revision: https://reviews.llvm.org/D86297
This commit is contained in:
Georgii Rymar 2020-08-20 18:40:52 +03:00
parent a0e92ffd0d
commit 80e9dd0878
1 changed files with 8 additions and 18 deletions

View File

@ -243,24 +243,14 @@ private:
TYPEDEF_ELF_TYPES(ELFT)
DynRegionInfo checkDRI(DynRegionInfo DRI) {
DynRegionInfo createDRI(uint64_t Offset, uint64_t Size, uint64_t EntSize) {
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
if (DRI.Addr < Obj->base() ||
reinterpret_cast<const uint8_t *>(DRI.Addr) + DRI.Size >
Obj->base() + Obj->getBufSize())
const uint8_t *Addr = Obj->base() + Offset;
if (Addr < Obj->base() || Addr + Size > Obj->base() + Obj->getBufSize())
reportError(errorCodeToError(llvm::object::object_error::parse_failed),
ObjF->getFileName());
return DRI;
}
DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) {
return checkDRI({ObjF->getELFFile()->base() + P->p_offset, P->p_filesz,
EntSize, ObjF->getFileName()});
}
DynRegionInfo createDRIFrom(const Elf_Shdr *S) {
return checkDRI({ObjF->getELFFile()->base() + S->sh_offset, S->sh_size,
S->sh_entsize, ObjF->getFileName()});
return {Addr, Size, EntSize, ObjF->getFileName()};
}
void printAttributes();
@ -1936,7 +1926,8 @@ void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
DynRegionInfo FromPhdr(ObjF->getFileName());
bool IsPhdrTableValid = false;
if (DynamicPhdr) {
FromPhdr = createDRIFrom(DynamicPhdr, sizeof(Elf_Dyn));
FromPhdr = createDRI(DynamicPhdr->p_offset, DynamicPhdr->p_filesz,
sizeof(Elf_Dyn));
FromPhdr.SizePrintName = "PT_DYNAMIC size";
FromPhdr.EntSizePrintName = "";
@ -1951,8 +1942,7 @@ void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
bool IsSecTableValid = false;
if (DynamicSec) {
FromSec =
checkDRI({ObjF->getELFFile()->base() + DynamicSec->sh_offset,
DynamicSec->sh_size, sizeof(Elf_Dyn), ObjF->getFileName()});
createDRI(DynamicSec->sh_offset, DynamicSec->sh_size, sizeof(Elf_Dyn));
FromSec.Context = describe(*DynamicSec);
FromSec.EntSizePrintName = "";
@ -2040,7 +2030,7 @@ ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> *ObjF,
DotDynsymSec = &Sec;
if (!DynSymRegion) {
DynSymRegion = createDRIFrom(&Sec);
DynSymRegion = createDRI(Sec.sh_offset, Sec.sh_size, Sec.sh_entsize);
DynSymRegion->Context = describe(Sec);
if (Expected<StringRef> E = Obj->getStringTableForSymtab(Sec))