[llvm] r319392 - Check alignment in getSectionContentsAsArray.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 16:44:22 PST 2017


Author: rafael
Date: Wed Nov 29 16:44:22 2017
New Revision: 319392

URL: http://llvm.org/viewvc/llvm-project?rev=319392&view=rev
Log:
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.

Added:
    llvm/trunk/test/Object/invalid-alignment.test
Modified:
    llvm/trunk/include/llvm/Object/ELF.h

Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=319392&r1=319391&r2=319392&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Wed Nov 29 16:44:22 2017
@@ -277,6 +277,9 @@ ELFFile<ELFT>::getSectionContentsAsArray
       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));
 }

Added: llvm/trunk/test/Object/invalid-alignment.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/invalid-alignment.test?rev=319392&view=auto
==============================================================================
--- llvm/trunk/test/Object/invalid-alignment.test (added)
+++ llvm/trunk/test/Object/invalid-alignment.test Wed Nov 29 16:44:22 2017
@@ -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




More information about the llvm-commits mailing list