Skip to content

Commit aa4f8d0

Browse files
committed
Revert "[Object/ELF.h] - Improve testing of the fields in ELFFile<ELFT>::sections()."
This reverts commit r366796 because it was causing ubsan buildbot failures. llvm-svn: 366815
1 parent 24f3e10 commit aa4f8d0

File tree

4 files changed

+5
-67
lines changed

4 files changed

+5
-67
lines changed

llvm/include/llvm/Object/ELF.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -513,22 +513,15 @@ Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
513513
NumSections = First->sh_size;
514514

515515
if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
516-
return createError("invalid number of sections specified in the NULL "
517-
"section's sh_size field (" +
518-
Twine(NumSections) + ")");
516+
// TODO: this error is untested.
517+
return createError("section table goes past the end of file");
519518

520519
const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
521-
if (SectionTableOffset + SectionTableSize < SectionTableOffset)
522-
return createError(
523-
"invalid section header table offset (e_shoff = 0x" +
524-
Twine::utohexstr(SectionTableOffset) +
525-
") or invalid number of sections specified in the first section "
526-
"header's sh_size field (0x" +
527-
Twine::utohexstr(NumSections) + ")");
528520

529521
// Section table goes past end of file!
530522
if (SectionTableOffset + SectionTableSize > FileSize)
531523
return createError("section table goes past the end of file");
524+
532525
return makeArrayRef(First, NumSections);
533526
}
534527

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct FileHeader {
7777
llvm::yaml::Hex64 Entry;
7878

7979
Optional<llvm::yaml::Hex16> SHEntSize;
80-
Optional<llvm::yaml::Hex64> SHOffset;
80+
Optional<llvm::yaml::Hex16> SHOffset;
8181
Optional<llvm::yaml::Hex16> SHNum;
8282
Optional<llvm::yaml::Hex16> SHStrNdx;
8383
};

llvm/test/Object/invalid.test

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -552,58 +552,3 @@ FileHeader:
552552
Sections:
553553
- Name: .foo
554554
Type: SHT_PROGBITS
555-
556-
## We report an error if the number of sections stored in sh_size
557-
## is greater than UINT64_MAX / sizeof(Elf_Shdr) == 288230376151711743.
558-
## Here we check that do not crash on a border value.
559-
560-
# RUN: yaml2obj --docnum=26 %s -o %t26
561-
# RUN: not llvm-readobj -h %t26 2>&1 | FileCheck -DFILE=%t26 --check-prefix=INVALID-SEC-NUM1 %s
562-
563-
# INVALID-SEC-NUM1: error: '[[FILE]]': invalid section header table offset (e_shoff = 0x40) or invalid number of sections specified in the first section header's sh_size field (0x3ffffffffffffff)
564-
565-
--- !ELF
566-
FileHeader:
567-
Class: ELFCLASS64
568-
Data: ELFDATA2LSB
569-
Type: ET_REL
570-
Machine: EM_X86_64
571-
SHNum: 0x0
572-
Sections:
573-
- Type: SHT_NULL
574-
Size: 288230376151711743
575-
576-
## See above, but now we test the UINT64_MAX / sizeof(Elf_Shdr) value.
577-
## The error is slightly different in this case.
578-
579-
# RUN: yaml2obj --docnum=27 %s -o %t27
580-
# RUN: not llvm-readobj -h %t27 2>&1 | FileCheck -DFILE=%t27 --check-prefix=INVALID-SEC-NUM2 %s
581-
582-
# INVALID-SEC-NUM2: error: '[[FILE]]': invalid number of sections specified in the NULL section's sh_size field (288230376151711744)
583-
584-
--- !ELF
585-
FileHeader:
586-
Class: ELFCLASS64
587-
Data: ELFDATA2LSB
588-
Type: ET_REL
589-
Machine: EM_X86_64
590-
SHNum: 0x0
591-
Sections:
592-
- Type: SHT_NULL
593-
Size: 288230376151711744
594-
595-
## Check the case when SHOffset is too large, but SHNum is not. SHOffset + SHNum overflows the uint64 type.
596-
597-
# RUN: yaml2obj --docnum=28 %s -o %t28
598-
# RUN: not llvm-readobj -h %t28 2>&1 | FileCheck -DFILE=%t28 --check-prefix=INVALID-SEC-NUM3 %s
599-
600-
# INVALID-SEC-NUM3: error: '[[FILE]]': invalid section header table offset (e_shoff = 0xffffffffffffffff) or invalid number of sections specified in the first section header's sh_size field (0x1)
601-
602-
--- !ELF
603-
FileHeader:
604-
Class: ELFCLASS64
605-
Data: ELFDATA2LSB
606-
Type: ET_REL
607-
Machine: EM_X86_64
608-
SHOffset: 0xffffffffffffffff
609-
SHNum: 0x1

llvm/tools/yaml2obj/yaml2elf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
244244
// Immediately following the ELF header and program headers.
245245
Header.e_shoff =
246246
Doc.Header.SHOffset
247-
? (typename ELFT::uint)(*Doc.Header.SHOffset)
247+
? (uint16_t)*Doc.Header.SHOffset
248248
: sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
249249
Header.e_shnum =
250250
Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : SN2I.size() + 1;

0 commit comments

Comments
 (0)