summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2017-11-30 00:44:22 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2017-11-30 00:44:22 +0000
commita4e418f71342da5ad67e211557971a40b13db9e8 (patch)
treef87745ba482c8040442f86e4802cb82f12dc148c /llvm
parent80fbb85555b51fbb8c500ec13756490400191865 (diff)
downloadbcm5719-llvm-a4e418f71342da5ad67e211557971a40b13db9e8.tar.gz
bcm5719-llvm-a4e418f71342da5ad67e211557971a40b13db9e8.zip
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. llvm-svn: 319392
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/Object/ELF.h3
-rw-r--r--llvm/test/Object/invalid-alignment.test21
2 files changed, 24 insertions, 0 deletions
diff --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h
index c24b6310465..45c98233dec 100644
--- a/llvm/include/llvm/Object/ELF.h
+++ b/llvm/include/llvm/Object/ELF.h
@@ -277,6 +277,9 @@ ELFFile<ELFT>::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<const T *>(base() + Offset);
return makeArrayRef(Start, Size / sizeof(T));
}
diff --git a/llvm/test/Object/invalid-alignment.test b/llvm/test/Object/invalid-alignment.test
new file mode 100644
index 00000000000..872de8c28a1
--- /dev/null
+++ b/llvm/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
OpenPOWER on IntegriCloud