Skip to content

Commit

Permalink
Re-land "Migrate Binary::checkOffset from error_code to Error, NFC"
Browse files Browse the repository at this point in the history
This reverts commit 38f3ba5.

Fix the XCOFF error handling. Unlike std::error_code, Error must be
consumed or handled.
  • Loading branch information
rnk committed Jun 5, 2020
1 parent 1c44ace commit e03a135
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
8 changes: 4 additions & 4 deletions llvm/include/llvm/Object/Binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ class Binary {
return Triple::UnknownObjectFormat;
}

static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
const uint64_t Size) {
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
const uint64_t Size) {
if (Addr + Size < Addr || Addr + Size < Size ||
Addr + Size > uintptr_t(M.getBufferEnd()) ||
Addr < uintptr_t(M.getBufferStart())) {
return object_error::unexpected_eof;
return errorCodeToError(object_error::unexpected_eof);
}
return std::error_code();
return Error::success();
}
};

Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,10 +744,10 @@ ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
const Elf_Shdr *EShdr = getSection(Sec);
if (EShdr->sh_type == ELF::SHT_NOBITS)
return makeArrayRef((const uint8_t *)base(), 0);
if (std::error_code EC =
if (Error E =
checkOffset(getMemoryBufferRef(),
(uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
return errorCodeToError(EC);
return std::move(E);
return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
EShdr->sh_size);
}
Expand Down
18 changes: 10 additions & 8 deletions llvm/lib/Object/COFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
const void *Ptr,
const uint64_t Size = sizeof(T)) {
uintptr_t Addr = uintptr_t(Ptr);
if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
return EC;
if (Error E = Binary::checkOffset(M, Addr, Size))
return errorToErrorCode(std::move(E));
Obj = reinterpret_cast<const T *>(Addr);
return std::error_code();
}
Expand Down Expand Up @@ -374,9 +374,11 @@ getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
// relocations.
begin++;
}
if (Binary::checkOffset(M, uintptr_t(begin),
sizeof(coff_relocation) * NumRelocs))
if (auto E = Binary::checkOffset(M, uintptr_t(begin),
sizeof(coff_relocation) * NumRelocs)) {
consumeError(std::move(E));
return nullptr;
}
return begin;
}

Expand Down Expand Up @@ -555,8 +557,8 @@ std::error_code COFFObjectFile::initImportTablePtr() {
uintptr_t IntPtr = 0;
if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
return EC;
if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
return EC;
if (Error E = checkOffset(Data, IntPtr, DataEntry->Size))
return errorToErrorCode(std::move(E));
ImportDirectory = reinterpret_cast<
const coff_import_directory_table_entry *>(IntPtr);
return std::error_code();
Expand Down Expand Up @@ -1093,8 +1095,8 @@ Error COFFObjectFile::getSectionContents(const coff_section *Sec,
// data, as there's nothing that says that is not allowed.
uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
uint32_t SectionSize = getSectionSize(Sec);
if (checkOffset(Data, ConStart, SectionSize))
return make_error<BinaryError>();
if (Error E = checkOffset(Data, ConStart, SectionSize))
return E;
Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
return Error::success();
}
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/Object/XCOFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ template <typename T>
static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
const uint64_t Size = sizeof(T)) {
uintptr_t Addr = uintptr_t(Ptr);
if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
return errorCodeToError(EC);
if (Error E = Binary::checkOffset(M, Addr, Size))
return std::move(E);
return reinterpret_cast<const T *>(Addr);
}

Expand Down Expand Up @@ -668,9 +668,11 @@ Expected<XCOFFStringTable>
XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) {
// If there is a string table, then the buffer must contain at least 4 bytes
// for the string table's size. Not having a string table is not an error.
if (auto EC = Binary::checkOffset(
Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4))
if (Error E = Binary::checkOffset(
Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) {
consumeError(std::move(E));
return XCOFFStringTable{0, nullptr};
}

// Read the size out of the buffer.
uint32_t Size = support::endian::read32be(Obj->base() + Offset);
Expand Down

0 comments on commit e03a135

Please sign in to comment.