]> granicus.if.org Git - llvm/commitdiff
Check alignment in getSectionContentsAsArray.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 30 Nov 2017 00:44:22 +0000 (00:44 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 30 Nov 2017 00:44:22 +0000 (00:44 +0000)
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

include/llvm/Object/ELF.h
test/Object/invalid-alignment.test [new file with mode: 0644]

index c24b6310465e4d5ce1bf02d40470d1cc8787c071..45c98233dec0f7b244ed47ddcc3dca28f2204bd9 100644 (file)
@@ -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/test/Object/invalid-alignment.test b/test/Object/invalid-alignment.test
new file mode 100644 (file)
index 0000000..872de8c
--- /dev/null
@@ -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