Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 4f80a5f

Browse files
committed
Make the ELFFile constructor private.
With this all clients have to use the new create method which returns an Expected. Fixes a crash on invalid input. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315376 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2e19222 commit 4f80a5f

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

include/llvm/Object/ELF.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class ELFFile {
8383
private:
8484
StringRef Buf;
8585

86+
ELFFile(StringRef Object);
87+
8688
public:
8789
const Elf_Ehdr *getHeader() const {
8890
return reinterpret_cast<const Elf_Ehdr *>(base());
@@ -112,7 +114,7 @@ class ELFFile {
112114
Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel *Rel,
113115
const Elf_Shdr *SymTab) const;
114116

115-
ELFFile(StringRef Object);
117+
static Expected<ELFFile> create(StringRef Object);
116118

117119
bool isMipsELF64() const {
118120
return getHeader()->e_machine == ELF::EM_MIPS &&
@@ -345,9 +347,13 @@ ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections) const {
345347
return getStringTable(&Sections[Index]);
346348
}
347349

350+
template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
351+
348352
template <class ELFT>
349-
ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {
350-
assert(sizeof(Elf_Ehdr) <= Buf.size() && "Invalid buffer");
353+
Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
354+
if (sizeof(Elf_Ehdr) > Object.size())
355+
return createError("Invalid buffer");
356+
return ELFFile(Object);
351357
}
352358

353359
template <class ELFT>

include/llvm/Object/ELFObjectFile.h

+14-9
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
211211
using Elf_Dyn = typename ELFFile<ELFT>::Elf_Dyn;
212212

213213
private:
214-
ELFObjectFile(MemoryBufferRef Object, const Elf_Shdr *DotDynSymSec,
215-
const Elf_Shdr *DotSymtabSec, ArrayRef<Elf_Word> ShndxTable);
214+
ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
215+
const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
216+
ArrayRef<Elf_Word> ShndxTable);
216217

217218
protected:
218219
ELFFile<ELFT> EF;
@@ -851,7 +852,10 @@ ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
851852
template <class ELFT>
852853
Expected<ELFObjectFile<ELFT>>
853854
ELFObjectFile<ELFT>::create(MemoryBufferRef Object) {
854-
ELFFile<ELFT> EF(Object.getBuffer());
855+
auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
856+
if (Error E = EFOrErr.takeError())
857+
return std::move(E);
858+
auto EF = std::move(*EFOrErr);
855859

856860
auto SectionsOrErr = EF.sections();
857861
if (!SectionsOrErr)
@@ -883,24 +887,25 @@ ELFObjectFile<ELFT>::create(MemoryBufferRef Object) {
883887
}
884888
}
885889
}
886-
return ELFObjectFile<ELFT>(Object, DotDynSymSec, DotSymtabSec, ShndxTable);
890+
return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
891+
ShndxTable);
887892
}
888893

889894
template <class ELFT>
890-
ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object,
895+
ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
891896
const Elf_Shdr *DotDynSymSec,
892897
const Elf_Shdr *DotSymtabSec,
893898
ArrayRef<Elf_Word> ShndxTable)
894899
: ELFObjectFileBase(
895900
getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
896901
Object),
897-
EF(Data.getBuffer()), DotDynSymSec(DotDynSymSec),
898-
DotSymtabSec(DotSymtabSec), ShndxTable(ShndxTable) {}
902+
EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
903+
ShndxTable(ShndxTable) {}
899904

900905
template <class ELFT>
901906
ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
902-
: ELFObjectFile(Other.Data, Other.DotDynSymSec, Other.DotSymtabSec,
903-
Other.ShndxTable) {}
907+
: ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
908+
Other.DotSymtabSec, Other.ShndxTable) {}
904909

905910
template <class ELFT>
906911
basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const {

test/Object/Inputs/invalid-buffer.elf

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ELF

test/Object/invalid.test

+3
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ INVALID-SECTION-NUM: section table goes past the end of file
8181

8282
RUN: not llvm-readobj -r %p/Inputs/invalid-rel-sym.elf 2>&1 | FileCheck --check-prefix=INVALID-REL-SYM %s
8383
INVALID-REL-SYM: invalid section offset
84+
85+
RUN: not llvm-readobj -r %p/Inputs/invalid-buffer.elf 2>&1 | FileCheck --check-prefix=INVALID-BUFFER %s
86+
INVALID-BUFFER: Invalid buffer

0 commit comments

Comments
 (0)