From: Jakub Jelinek Date: Tue, 13 Feb 2018 20:23:58 +0000 (+0100) Subject: re PR c/82210 (Having _Alignas in a struct with VLAs causes writing to one array... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=201d17c0d835112bdea5408e6eff4e250f3c238a;p=gcc.git re PR c/82210 (Having _Alignas in a struct with VLAs causes writing to one array to overwrite another) 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: r257635 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f60fb1d2b86..c58d419556b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,10 @@ 2018-02-13 Jakub Jelinek + 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. + PR middle-end/84309 * match.pd (pow(C,x) -> exp(log(C)*x)): Use exp2s and log2s instead of exps and logs in the use_exp2 case. diff --git a/gcc/stor-layout.c b/gcc/stor-layout.c index 8c415ebb6ac..5fdf81a9a25 100644 --- a/gcc/stor-layout.c +++ b/gcc/stor-layout.c @@ -1622,6 +1622,30 @@ 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 + = least_bit_hwi (TREE_INT_CST_LOW (TYPE_SIZE (type))); + 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)) { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index cd5ab819fea..19c0832f2cd 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2018-02-13 Jakub Jelinek + PR c/82210 + * gcc.c-torture/execute/pr82210.c: New test. + PR middle-end/84309 * gcc.dg/pr84309-2.c: New test. diff --git a/gcc/testsuite/gcc.c-torture/execute/pr82210.c b/gcc/testsuite/gcc.c-torture/execute/pr82210.c new file mode 100644 index 00000000000..48fb715570a --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr82210.c @@ -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; +}