]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR c/82210 (Having _Alignas in a struct with VLAs causes writing to...
authorJakub Jelinek <jakub@redhat.com>
Mon, 25 Jun 2018 17:21:47 +0000 (19:21 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 25 Jun 2018 17:21:47 +0000 (19:21 +0200)
Backported from mainline
2018-02-13  Jakub Jelinek  <jakub@redhat.com>

PR c/82210
* stor-layout.c (place_field): For variable length fields, adjust
offset_align afterwards not just based on the field's alignment,
but also on the size.

* gcc.c-torture/execute/pr82210.c: New test.

From-SVN: r262063

gcc/ChangeLog
gcc/stor-layout.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/execute/pr82210.c [new file with mode: 0644]

index 1c0d9b840732568cf592ae0d74c67163a8a7d613..88eabb9420ba4a96911bf4de65830283da5886a1 100644 (file)
@@ -1,6 +1,13 @@
 2018-06-25  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2018-02-13  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/82210
+       * stor-layout.c (place_field): For variable length fields, adjust
+       offset_align afterwards not just based on the field's alignment,
+       but also on the size.
+
        2018-02-10  Jakub Jelinek  <jakub@redhat.com>
 
        PR sanitizer/83987
index dd40ca00af8eb29a9f7fb891c8197cdfbbfd3bbd..2aa93258a185d4f20440b888c57053d87140c815 100644 (file)
@@ -1519,6 +1519,31 @@ place_field (record_layout_info rli, tree field)
        = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
       rli->bitpos = bitsize_zero_node;
       rli->offset_align = MIN (rli->offset_align, desired_align);
+
+      if (!multiple_of_p (bitsizetype, DECL_SIZE (field),
+                         bitsize_int (rli->offset_align)))
+       {
+         tree type = strip_array_types (TREE_TYPE (field));
+         /* The above adjusts offset_align just based on the start of the
+            field.  The field might not have a size that is a multiple of
+            that offset_align though.  If the field is an array of fixed
+            sized elements, assume there can be any multiple of those
+            sizes.  If it is a variable length aggregate or array of
+            variable length aggregates, assume worst that the end is
+            just BITS_PER_UNIT aligned.  */
+         if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
+           {
+             if (TREE_INT_CST_LOW (TYPE_SIZE (type)))
+               {
+                 unsigned HOST_WIDE_INT sz
+                   = TREE_INT_CST_LOW (TYPE_SIZE (type));
+                 sz = sz & -sz;
+                 rli->offset_align = MIN (rli->offset_align, sz);
+               }
+           }
+         else
+           rli->offset_align = MIN (rli->offset_align, BITS_PER_UNIT);
+       }
     }
   else if (targetm.ms_bitfield_layout_p (rli->t))
     {
index 4e92dc30240d6c827ec60e3c560836d24f4aa3f1..029e88d43fed2e04665b78919710a357b053063d 100644 (file)
@@ -1,6 +1,11 @@
 2018-06-25  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2018-02-13  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/82210
+       * gcc.c-torture/execute/pr82210.c: New test.
+
        2018-02-12  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/84341
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr82210.c b/gcc/testsuite/gcc.c-torture/execute/pr82210.c
new file mode 100644 (file)
index 0000000..48fb715
--- /dev/null
@@ -0,0 +1,26 @@
+/* PR c/82210 */
+
+void
+foo (int size)
+{
+  int i;
+  struct S {
+    __attribute__((aligned (16))) struct T { short c; } a[size];
+    int b[size];
+  } s;
+
+  for (i = 0; i < size; i++)
+    s.a[i].c = 0x1234;
+  for (i = 0; i < size; i++)
+    s.b[i] = 0;
+  for (i = 0; i < size; i++)
+    if (s.a[i].c != 0x1234 || s.b[i] != 0)
+      __builtin_abort ();
+}
+
+int
+main ()
+{
+  foo (15);
+  return 0;
+}