From: Rafael Espindola Date: Thu, 30 Nov 2017 00:44:22 +0000 (+0000) Subject: Check alignment in getSectionContentsAsArray. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fb1028740138d634227e928a584c0fd0104474ea;p=llvm Check alignment in getSectionContentsAsArray. While the ArrayRef can technically have unaligned data, it would be extremely surprising if iterating over it caused undefined behavior when a reference to the underlying type was bound. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319392 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index c24b6310465..45c98233dec 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -277,6 +277,9 @@ ELFFile::getSectionContentsAsArray(const Elf_Shdr *Sec) const { Offset + Size > Buf.size()) return createError("invalid section offset"); + if (Offset % alignof(T)) + return createError("unaligned data"); + const T *Start = reinterpret_cast(base() + Offset); return makeArrayRef(Start, Size / sizeof(T)); } diff --git a/test/Object/invalid-alignment.test b/test/Object/invalid-alignment.test new file mode 100644 index 00000000000..872de8c28a1 --- /dev/null +++ b/test/Object/invalid-alignment.test @@ -0,0 +1,21 @@ +# RUN: yaml2obj %s -o %t.o +# RUN: not llvm-readobj -r %t.o 2>&1 | FileCheck %s + +# CHECK: Error reading file: unaligned data + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + Content: 42 + - Name: .rela.foo + Type: SHT_RELA + Info: .foo + Relocations: + - Offset: 0 + Type: R_X86_64_NONE