Message ID | 1D72918C-4D6A-4583-AF14-288757C04DD0@oracle.com |
---|---|
State | New |
Headers | show |
Series | Add a new option -fstrict-flex-array[=n] and attribute strict_flex_array(n) and use it in PR101836 | expand |
On Tue, 19 Jul 2022, Qing Zhao wrote: > From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 > From: Qing Zhao <qing.zhao@oracle.com> > Date: Mon, 18 Jul 2022 18:12:26 +0000 > Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size > [PR101836] > > Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array > of a structure is flexible array member in __builtin_object_size. > > gcc/ChangeLog: > > PR tree-optimization/101836 > * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p > and DECL_NOT_FLEXARRAY to determine a flexible array member reference. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/101836 > * gcc.dg/pr101836.c: New test. > * gcc.dg/pr101836_1.c: New test. > * gcc.dg/pr101836_2.c: New test. > * gcc.dg/pr101836_3.c: New test. > * gcc.dg/pr101836_4.c: New test. > * gcc.dg/pr101836_5.c: New test. > * gcc.dg/strict-flex-array-2.c: New test. > * gcc.dg/strict-flex-array-3.c: New test. > --- > gcc/testsuite/gcc.dg/pr101836.c | 60 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr101836_1.c | 60 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr101836_2.c | 60 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr101836_3.c | 60 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr101836_4.c | 60 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr101836_5.c | 60 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/strict-flex-array-2.c | 60 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/strict-flex-array-3.c | 60 ++++++++++++++++++++++ > gcc/tree-object-size.cc | 18 +++---- > 9 files changed, 489 insertions(+), 9 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/pr101836.c > create mode 100644 gcc/testsuite/gcc.dg/pr101836_1.c > create mode 100644 gcc/testsuite/gcc.dg/pr101836_2.c > create mode 100644 gcc/testsuite/gcc.dg/pr101836_3.c > create mode 100644 gcc/testsuite/gcc.dg/pr101836_4.c > create mode 100644 gcc/testsuite/gcc.dg/pr101836_5.c > create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c > create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c > > diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.c > new file mode 100644 > index 00000000000..e5b4e5160a4 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101836.c > @@ -0,0 +1,60 @@ > +/* -fstrict-flex-array is aliased with -ftrict-flex-array=3, which is the > + strictest, only [] is treated as flexible array. */ > +/* PR tree-optimization/101836 */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-array" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), 16); > + expect(__builtin_object_size(trailing_1->c, 1), 4); > + expect(__builtin_object_size(trailing_0->c, 1), 0); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr101836_1.c > new file mode 100644 > index 00000000000..30ea20427a5 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101836_1.c > @@ -0,0 +1,60 @@ > +/* -fstrict-flex-array=3 is the strictest, only [] is treated as > + flexible array. */ > +/* PR tree-optimization/101836 */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-array=3" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), 16); > + expect(__builtin_object_size(trailing_1->c, 1), 4); > + expect(__builtin_object_size(trailing_0->c, 1), 0); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr101836_2.c > new file mode 100644 > index 00000000000..ebbe88f433c > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101836_2.c > @@ -0,0 +1,60 @@ > +/* When -fstrict-flex-array=2, only [] and [0] are treated as flexiable > + arrays. */ > +/* PR tree-optimization/101836 */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-array=2" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), 16); > + expect(__builtin_object_size(trailing_1->c, 1), 4); > + expect(__builtin_object_size(trailing_0->c, 1), -1); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr101836_3.c > new file mode 100644 > index 00000000000..d4ba0afe5bc > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101836_3.c > @@ -0,0 +1,60 @@ > +/* When -fstrict-flex-array=1, [], [0], and [1] are treated as flexible > + arrays. */ > +/* PR tree-optimization/101836 */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-array=1" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), 16); > + expect(__builtin_object_size(trailing_1->c, 1), -1); > + expect(__builtin_object_size(trailing_0->c, 1), -1); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr101836_4.c > new file mode 100644 > index 00000000000..b10d3ce312d > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101836_4.c > @@ -0,0 +1,60 @@ > +/* when -fstrict-flex-array=0, all trailing arrays are treated as > + flexible arrays. */ > +/* PR tree-optimization/101836 */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-array=0" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), -1); > + expect(__builtin_object_size(trailing_1->c, 1), -1); > + expect(__builtin_object_size(trailing_0->c, 1), -1); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr101836_5.c > new file mode 100644 > index 00000000000..2f6b5f7ae1f > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101836_5.c > @@ -0,0 +1,60 @@ > +/* -fno-strict-flex-array is aliased to -fstrict-flex-array=0, > + all trailing arrays are treated as flexible array. */ > +/* PR tree-optimization/101836 */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fno-strict-flex-array" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), -1); > + expect(__builtin_object_size(trailing_1->c, 1), -1); > + expect(__builtin_object_size(trailing_0->c, 1), -1); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc.dg/strict-flex-array-2.c > new file mode 100644 > index 00000000000..326ddcfeda5 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c > @@ -0,0 +1,60 @@ > +/* test the combination of attribute strict_flex_array and option > + -fstrict-flex-array: when both attribute and option specified, > + attribute will have higher priority. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-array=3" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4] __attribute__ ((strict_flex_array (0))); > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1] __attribute__ ((strict_flex_array (1))); > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0] __attribute__ ((strict_flex_array (2))); > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), -1); > + expect(__builtin_object_size(trailing_1->c, 1), -1); > + expect(__builtin_object_size(trailing_0->c, 1), -1); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc.dg/strict-flex-array-3.c > new file mode 100644 > index 00000000000..990c5bb6223 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c > @@ -0,0 +1,60 @@ > +/* test the combination of attribute strict_flex_array and option > + -fstrict-flex-array: when both attribute and option specified, > + attribute will have higher priority. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-array=0" } */ > + > +#include <stdio.h> > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else \ > + { \ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + __builtin_abort (); \ > + } \ > +} while (0); > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4] __attribute__ ((strict_flex_array (1))); > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1] __attribute__ ((strict_flex_array (2))); > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0] __attribute__ ((strict_flex_array (3))); > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + expect(__builtin_object_size(normal->c, 1), 16); > + expect(__builtin_object_size(trailing_1->c, 1), 4); > + expect(__builtin_object_size(trailing_0->c, 1), 0); > + expect(__builtin_object_size(trailing_flex->c, 1), -1); > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc > index 4eb454a4a33..90710ecba72 100644 > --- a/gcc/tree-object-size.cc > +++ b/gcc/tree-object-size.cc > @@ -604,9 +604,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) > { > tree v = var; > - /* For &X->fld, compute object size only if fld isn't the last > - field, as struct { int i; char c[1]; } is often used instead > - of flexible array member. */ > + bool is_flexible_array_mem_ref = false; > + /* For &X->fld, compute object size if fld isn't a flexible array > + member. */ > while (v && v != pt_var) > switch (TREE_CODE (v)) > { > @@ -633,6 +633,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > v = NULL_TREE; > break; > } > + is_flexible_array_mem_ref > + = array_at_struct_end_p (v) > + && !DECL_NOT_FLEXARRAY (TREE_OPERAND (v, 1)); As said in the review for the first patch array_at_struct_end_p should check DECL_NOT_FLEXARRAY. OK with removing the extra check here. > while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) > if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > != UNION_TYPE > @@ -645,12 +648,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > == RECORD_TYPE) > { > - tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); > - for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) > - if (TREE_CODE (fld_chain) == FIELD_DECL) > - break; > - > - if (fld_chain) > + /* compute object size only if v is not a > + flexible array member. */ > + if (!is_flexible_array_mem_ref) > { > v = NULL_TREE; > break; >
> On Jul 28, 2022, at 3:28 AM, Richard Biener <rguenther@suse.de> wrote: > > On Tue, 19 Jul 2022, Qing Zhao wrote: > >> From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 >> From: Qing Zhao <qing.zhao@oracle.com> >> Date: Mon, 18 Jul 2022 18:12:26 +0000 >> Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size >> [PR101836] >> >> Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array >> of a structure is flexible array member in __builtin_object_size. >> >> gcc/ChangeLog: >> >> PR tree-optimization/101836 >> * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p >> and DECL_NOT_FLEXARRAY to determine a flexible array member reference. >> >> gcc/testsuite/ChangeLog: >> >> PR tree-optimization/101836 >> * gcc.dg/pr101836.c: New test. >> * gcc.dg/pr101836_1.c: New test. >> * gcc.dg/pr101836_2.c: New test. >> * gcc.dg/pr101836_3.c: New test. >> * gcc.dg/pr101836_4.c: New test. >> * gcc.dg/pr101836_5.c: New test. >> * gcc.dg/strict-flex-array-2.c: New test. >> * gcc.dg/strict-flex-array-3.c: New test. >> --- >> gcc/testsuite/gcc.dg/pr101836.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_1.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_2.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_3.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_4.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_5.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/strict-flex-array-2.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/strict-flex-array-3.c | 60 ++++++++++++++++++++++ >> gcc/tree-object-size.cc | 18 +++---- >> 9 files changed, 489 insertions(+), 9 deletions(-) >> create mode 100644 gcc/testsuite/gcc.dg/pr101836.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_1.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_2.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_3.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_4.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_5.c >> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c >> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c >> >> diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.c >> new file mode 100644 >> index 00000000000..e5b4e5160a4 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836.c >> @@ -0,0 +1,60 @@ >> +/* -fstrict-flex-array is aliased with -ftrict-flex-array=3, which is the >> + strictest, only [] is treated as flexible array. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-array" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), 0); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr101836_1.c >> new file mode 100644 >> index 00000000000..30ea20427a5 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_1.c >> @@ -0,0 +1,60 @@ >> +/* -fstrict-flex-array=3 is the strictest, only [] is treated as >> + flexible array. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-array=3" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), 0); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr101836_2.c >> new file mode 100644 >> index 00000000000..ebbe88f433c >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_2.c >> @@ -0,0 +1,60 @@ >> +/* When -fstrict-flex-array=2, only [] and [0] are treated as flexiable >> + arrays. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-array=2" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr101836_3.c >> new file mode 100644 >> index 00000000000..d4ba0afe5bc >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_3.c >> @@ -0,0 +1,60 @@ >> +/* When -fstrict-flex-array=1, [], [0], and [1] are treated as flexible >> + arrays. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-array=1" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr101836_4.c >> new file mode 100644 >> index 00000000000..b10d3ce312d >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_4.c >> @@ -0,0 +1,60 @@ >> +/* when -fstrict-flex-array=0, all trailing arrays are treated as >> + flexible arrays. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-array=0" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), -1); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr101836_5.c >> new file mode 100644 >> index 00000000000..2f6b5f7ae1f >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_5.c >> @@ -0,0 +1,60 @@ >> +/* -fno-strict-flex-array is aliased to -fstrict-flex-array=0, >> + all trailing arrays are treated as flexible array. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fno-strict-flex-array" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), -1); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc.dg/strict-flex-array-2.c >> new file mode 100644 >> index 00000000000..326ddcfeda5 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c >> @@ -0,0 +1,60 @@ >> +/* test the combination of attribute strict_flex_array and option >> + -fstrict-flex-array: when both attribute and option specified, >> + attribute will have higher priority. */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-array=3" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4] __attribute__ ((strict_flex_array (0))); >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1] __attribute__ ((strict_flex_array (1))); >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0] __attribute__ ((strict_flex_array (2))); >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), -1); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc.dg/strict-flex-array-3.c >> new file mode 100644 >> index 00000000000..990c5bb6223 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c >> @@ -0,0 +1,60 @@ >> +/* test the combination of attribute strict_flex_array and option >> + -fstrict-flex-array: when both attribute and option specified, >> + attribute will have higher priority. */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-array=0" } */ >> + >> +#include <stdio.h> >> + >> +#define expect(p, _v) do { \ >> + size_t v = _v; \ >> + if (p == v) \ >> + printf("ok: %s == %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4] __attribute__ ((strict_flex_array (1))); >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1] __attribute__ ((strict_flex_array (2))); >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0] __attribute__ ((strict_flex_array (3))); >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), 0); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc >> index 4eb454a4a33..90710ecba72 100644 >> --- a/gcc/tree-object-size.cc >> +++ b/gcc/tree-object-size.cc >> @@ -604,9 +604,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, >> else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) >> { >> tree v = var; >> - /* For &X->fld, compute object size only if fld isn't the last >> - field, as struct { int i; char c[1]; } is often used instead >> - of flexible array member. */ >> + bool is_flexible_array_mem_ref = false; >> + /* For &X->fld, compute object size if fld isn't a flexible array >> + member. */ >> while (v && v != pt_var) >> switch (TREE_CODE (v)) >> { >> @@ -633,6 +633,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, >> v = NULL_TREE; >> break; >> } >> + is_flexible_array_mem_ref >> + = array_at_struct_end_p (v) >> + && !DECL_NOT_FLEXARRAY (TREE_OPERAND (v, 1)); > > As said in the review for the first patch array_at_struct_end_p should > check DECL_NOT_FLEXARRAY. Okay. Then, should we change the name of “array_at_struct_end_p” to a more descriptive name, for example,”flexible_array_member_p”? Otherwise, It’s really confusing. > > OK with removing the extra check here. Okay. thanks. Qing > >> while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) >> if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) >> != UNION_TYPE >> @@ -645,12 +648,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, >> && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) >> == RECORD_TYPE) >> { >> - tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); >> - for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) >> - if (TREE_CODE (fld_chain) == FIELD_DECL) >> - break; >> - >> - if (fld_chain) >> + /* compute object size only if v is not a >> + flexible array member. */ >> + if (!is_flexible_array_mem_ref) >> { >> v = NULL_TREE; >> break; >> > > -- > Richard Biener <rguenther@suse.de> > SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, > Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; > HRB 36809 (AG Nuernberg)
On Fri, 29 Jul 2022, Qing Zhao wrote: > > > > On Jul 28, 2022, at 3:28 AM, Richard Biener <rguenther@suse.de> wrote: > > > > On Tue, 19 Jul 2022, Qing Zhao wrote: > > > >> From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 > >> From: Qing Zhao <qing.zhao@oracle.com> > >> Date: Mon, 18 Jul 2022 18:12:26 +0000 > >> Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size > >> [PR101836] > >> > >> Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array > >> of a structure is flexible array member in __builtin_object_size. > >> > >> gcc/ChangeLog: > >> > >> PR tree-optimization/101836 > >> * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p > >> and DECL_NOT_FLEXARRAY to determine a flexible array member reference. > >> > >> gcc/testsuite/ChangeLog: > >> > >> PR tree-optimization/101836 > >> * gcc.dg/pr101836.c: New test. > >> * gcc.dg/pr101836_1.c: New test. > >> * gcc.dg/pr101836_2.c: New test. > >> * gcc.dg/pr101836_3.c: New test. > >> * gcc.dg/pr101836_4.c: New test. > >> * gcc.dg/pr101836_5.c: New test. > >> * gcc.dg/strict-flex-array-2.c: New test. > >> * gcc.dg/strict-flex-array-3.c: New test. > >> --- > >> gcc/testsuite/gcc.dg/pr101836.c | 60 ++++++++++++++++++++++ > >> gcc/testsuite/gcc.dg/pr101836_1.c | 60 ++++++++++++++++++++++ > >> gcc/testsuite/gcc.dg/pr101836_2.c | 60 ++++++++++++++++++++++ > >> gcc/testsuite/gcc.dg/pr101836_3.c | 60 ++++++++++++++++++++++ > >> gcc/testsuite/gcc.dg/pr101836_4.c | 60 ++++++++++++++++++++++ > >> gcc/testsuite/gcc.dg/pr101836_5.c | 60 ++++++++++++++++++++++ > >> gcc/testsuite/gcc.dg/strict-flex-array-2.c | 60 ++++++++++++++++++++++ > >> gcc/testsuite/gcc.dg/strict-flex-array-3.c | 60 ++++++++++++++++++++++ > >> gcc/tree-object-size.cc | 18 +++---- > >> 9 files changed, 489 insertions(+), 9 deletions(-) > >> create mode 100644 gcc/testsuite/gcc.dg/pr101836.c > >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_1.c > >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_2.c > >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_3.c > >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_4.c > >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_5.c > >> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c > >> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c > >> > >> diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.c > >> new file mode 100644 > >> index 00000000000..e5b4e5160a4 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/pr101836.c > >> @@ -0,0 +1,60 @@ > >> +/* -fstrict-flex-array is aliased with -ftrict-flex-array=3, which is the > >> + strictest, only [] is treated as flexible array. */ > >> +/* PR tree-optimization/101836 */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fstrict-flex-array" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4]; > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1]; > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0]; > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), 16); > >> + expect(__builtin_object_size(trailing_1->c, 1), 4); > >> + expect(__builtin_object_size(trailing_0->c, 1), 0); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr101836_1.c > >> new file mode 100644 > >> index 00000000000..30ea20427a5 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/pr101836_1.c > >> @@ -0,0 +1,60 @@ > >> +/* -fstrict-flex-array=3 is the strictest, only [] is treated as > >> + flexible array. */ > >> +/* PR tree-optimization/101836 */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fstrict-flex-array=3" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4]; > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1]; > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0]; > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), 16); > >> + expect(__builtin_object_size(trailing_1->c, 1), 4); > >> + expect(__builtin_object_size(trailing_0->c, 1), 0); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr101836_2.c > >> new file mode 100644 > >> index 00000000000..ebbe88f433c > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/pr101836_2.c > >> @@ -0,0 +1,60 @@ > >> +/* When -fstrict-flex-array=2, only [] and [0] are treated as flexiable > >> + arrays. */ > >> +/* PR tree-optimization/101836 */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fstrict-flex-array=2" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4]; > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1]; > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0]; > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), 16); > >> + expect(__builtin_object_size(trailing_1->c, 1), 4); > >> + expect(__builtin_object_size(trailing_0->c, 1), -1); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr101836_3.c > >> new file mode 100644 > >> index 00000000000..d4ba0afe5bc > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/pr101836_3.c > >> @@ -0,0 +1,60 @@ > >> +/* When -fstrict-flex-array=1, [], [0], and [1] are treated as flexible > >> + arrays. */ > >> +/* PR tree-optimization/101836 */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fstrict-flex-array=1" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4]; > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1]; > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0]; > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), 16); > >> + expect(__builtin_object_size(trailing_1->c, 1), -1); > >> + expect(__builtin_object_size(trailing_0->c, 1), -1); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr101836_4.c > >> new file mode 100644 > >> index 00000000000..b10d3ce312d > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/pr101836_4.c > >> @@ -0,0 +1,60 @@ > >> +/* when -fstrict-flex-array=0, all trailing arrays are treated as > >> + flexible arrays. */ > >> +/* PR tree-optimization/101836 */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fstrict-flex-array=0" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4]; > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1]; > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0]; > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), -1); > >> + expect(__builtin_object_size(trailing_1->c, 1), -1); > >> + expect(__builtin_object_size(trailing_0->c, 1), -1); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr101836_5.c > >> new file mode 100644 > >> index 00000000000..2f6b5f7ae1f > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/pr101836_5.c > >> @@ -0,0 +1,60 @@ > >> +/* -fno-strict-flex-array is aliased to -fstrict-flex-array=0, > >> + all trailing arrays are treated as flexible array. */ > >> +/* PR tree-optimization/101836 */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fno-strict-flex-array" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4]; > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1]; > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0]; > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), -1); > >> + expect(__builtin_object_size(trailing_1->c, 1), -1); > >> + expect(__builtin_object_size(trailing_0->c, 1), -1); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc.dg/strict-flex-array-2.c > >> new file mode 100644 > >> index 00000000000..326ddcfeda5 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c > >> @@ -0,0 +1,60 @@ > >> +/* test the combination of attribute strict_flex_array and option > >> + -fstrict-flex-array: when both attribute and option specified, > >> + attribute will have higher priority. */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fstrict-flex-array=3" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4] __attribute__ ((strict_flex_array (0))); > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1] __attribute__ ((strict_flex_array (1))); > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0] __attribute__ ((strict_flex_array (2))); > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), -1); > >> + expect(__builtin_object_size(trailing_1->c, 1), -1); > >> + expect(__builtin_object_size(trailing_0->c, 1), -1); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc.dg/strict-flex-array-3.c > >> new file mode 100644 > >> index 00000000000..990c5bb6223 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c > >> @@ -0,0 +1,60 @@ > >> +/* test the combination of attribute strict_flex_array and option > >> + -fstrict-flex-array: when both attribute and option specified, > >> + attribute will have higher priority. */ > >> +/* { dg-do run } */ > >> +/* { dg-options "-O2 -fstrict-flex-array=0" } */ > >> + > >> +#include <stdio.h> > >> + > >> +#define expect(p, _v) do { \ > >> + size_t v = _v; \ > >> + if (p == v) \ > >> + printf("ok: %s == %zd\n", #p, p); \ > >> + else \ > >> + { \ > >> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > >> + __builtin_abort (); \ > >> + } \ > >> +} while (0); > >> + > >> +struct trailing_array_1 { > >> + int a; > >> + int b; > >> + int c[4] __attribute__ ((strict_flex_array (1))); > >> +}; > >> + > >> +struct trailing_array_2 { > >> + int a; > >> + int b; > >> + int c[1] __attribute__ ((strict_flex_array (2))); > >> +}; > >> + > >> +struct trailing_array_3 { > >> + int a; > >> + int b; > >> + int c[0] __attribute__ ((strict_flex_array (3))); > >> +}; > >> +struct trailing_array_4 { > >> + int a; > >> + int b; > >> + int c[]; > >> +}; > >> + > >> +void __attribute__((__noinline__)) stuff( > >> + struct trailing_array_1 *normal, > >> + struct trailing_array_2 *trailing_1, > >> + struct trailing_array_3 *trailing_0, > >> + struct trailing_array_4 *trailing_flex) > >> +{ > >> + expect(__builtin_object_size(normal->c, 1), 16); > >> + expect(__builtin_object_size(trailing_1->c, 1), 4); > >> + expect(__builtin_object_size(trailing_0->c, 1), 0); > >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); > >> +} > >> + > >> +int main(int argc, char *argv[]) > >> +{ > >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); > >> + > >> + return 0; > >> +} > >> diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc > >> index 4eb454a4a33..90710ecba72 100644 > >> --- a/gcc/tree-object-size.cc > >> +++ b/gcc/tree-object-size.cc > >> @@ -604,9 +604,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > >> else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) > >> { > >> tree v = var; > >> - /* For &X->fld, compute object size only if fld isn't the last > >> - field, as struct { int i; char c[1]; } is often used instead > >> - of flexible array member. */ > >> + bool is_flexible_array_mem_ref = false; > >> + /* For &X->fld, compute object size if fld isn't a flexible array > >> + member. */ > >> while (v && v != pt_var) > >> switch (TREE_CODE (v)) > >> { > >> @@ -633,6 +633,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > >> v = NULL_TREE; > >> break; > >> } > >> + is_flexible_array_mem_ref > >> + = array_at_struct_end_p (v) > >> + && !DECL_NOT_FLEXARRAY (TREE_OPERAND (v, 1)); > > > > As said in the review for the first patch array_at_struct_end_p should > > check DECL_NOT_FLEXARRAY. > Okay. > > Then, should we change the name of “array_at_struct_end_p” to a more > descriptive name, for example,”flexible_array_member_p”? Otherwise, It’s > really confusing. It has been that way since forever, so please don't (at least not as part of this series). > > > > > > OK with removing the extra check here. > > Okay. > > > thanks. > > Qing > > > >> while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) > >> if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > >> != UNION_TYPE > >> @@ -645,12 +648,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > >> && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > >> == RECORD_TYPE) > >> { > >> - tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); > >> - for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) > >> - if (TREE_CODE (fld_chain) == FIELD_DECL) > >> - break; > >> - > >> - if (fld_chain) > >> + /* compute object size only if v is not a > >> + flexible array member. */ > >> + if (!is_flexible_array_mem_ref) > >> { > >> v = NULL_TREE; > >> break; > >> > > > > -- > > Richard Biener <rguenther@suse.de> > > SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, > > Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; > > HRB 36809 (AG Nuernberg) > >
> On Aug 1, 2022, at 3:13 AM, Richard Biener <rguenther@suse.de> wrote: > > On Fri, 29 Jul 2022, Qing Zhao wrote: > >> >> >>> On Jul 28, 2022, at 3:28 AM, Richard Biener <rguenther@suse.de> wrote: >>> >>> On Tue, 19 Jul 2022, Qing Zhao wrote: >>> >>>> From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 >>>> From: Qing Zhao <qing.zhao@oracle.com> >>>> Date: Mon, 18 Jul 2022 18:12:26 +0000 >>>> Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size >>>> [PR101836] >>>> >>>> Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array >>>> of a structure is flexible array member in __builtin_object_size. >>>> >>>> gcc/ChangeLog: >>>> >>>> PR tree-optimization/101836 >>>> * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p >>>> and DECL_NOT_FLEXARRAY to determine a flexible array member reference. >>>> >>>> gcc/testsuite/ChangeLog: >>>> >>>> PR tree-optimization/101836 >>>> * gcc.dg/pr101836.c: New test. >>>> * gcc.dg/pr101836_1.c: New test. >>>> * gcc.dg/pr101836_2.c: New test. >>>> * gcc.dg/pr101836_3.c: New test. >>>> * gcc.dg/pr101836_4.c: New test. >>>> * gcc.dg/pr101836_5.c: New test. >>>> * gcc.dg/strict-flex-array-2.c: New test. >>>> * gcc.dg/strict-flex-array-3.c: New test. >>>> --- >>>> gcc/testsuite/gcc.dg/pr101836.c | 60 ++++++++++++++++++++++ >>>> gcc/testsuite/gcc.dg/pr101836_1.c | 60 ++++++++++++++++++++++ >>>> gcc/testsuite/gcc.dg/pr101836_2.c | 60 ++++++++++++++++++++++ >>>> gcc/testsuite/gcc.dg/pr101836_3.c | 60 ++++++++++++++++++++++ >>>> gcc/testsuite/gcc.dg/pr101836_4.c | 60 ++++++++++++++++++++++ >>>> gcc/testsuite/gcc.dg/pr101836_5.c | 60 ++++++++++++++++++++++ >>>> gcc/testsuite/gcc.dg/strict-flex-array-2.c | 60 ++++++++++++++++++++++ >>>> gcc/testsuite/gcc.dg/strict-flex-array-3.c | 60 ++++++++++++++++++++++ >>>> gcc/tree-object-size.cc | 18 +++---- >>>> 9 files changed, 489 insertions(+), 9 deletions(-) >>>> create mode 100644 gcc/testsuite/gcc.dg/pr101836.c >>>> create mode 100644 gcc/testsuite/gcc.dg/pr101836_1.c >>>> create mode 100644 gcc/testsuite/gcc.dg/pr101836_2.c >>>> create mode 100644 gcc/testsuite/gcc.dg/pr101836_3.c >>>> create mode 100644 gcc/testsuite/gcc.dg/pr101836_4.c >>>> create mode 100644 gcc/testsuite/gcc.dg/pr101836_5.c >>>> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c >>>> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c >>>> >>>> diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.c >>>> new file mode 100644 >>>> index 00000000000..e5b4e5160a4 >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/pr101836.c >>>> @@ -0,0 +1,60 @@ >>>> +/* -fstrict-flex-array is aliased with -ftrict-flex-array=3, which is the >>>> + strictest, only [] is treated as flexible array. */ >>>> +/* PR tree-optimization/101836 */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fstrict-flex-array" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4]; >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1]; >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0]; >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), 16); >>>> + expect(__builtin_object_size(trailing_1->c, 1), 4); >>>> + expect(__builtin_object_size(trailing_0->c, 1), 0); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr101836_1.c >>>> new file mode 100644 >>>> index 00000000000..30ea20427a5 >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/pr101836_1.c >>>> @@ -0,0 +1,60 @@ >>>> +/* -fstrict-flex-array=3 is the strictest, only [] is treated as >>>> + flexible array. */ >>>> +/* PR tree-optimization/101836 */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fstrict-flex-array=3" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4]; >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1]; >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0]; >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), 16); >>>> + expect(__builtin_object_size(trailing_1->c, 1), 4); >>>> + expect(__builtin_object_size(trailing_0->c, 1), 0); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr101836_2.c >>>> new file mode 100644 >>>> index 00000000000..ebbe88f433c >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/pr101836_2.c >>>> @@ -0,0 +1,60 @@ >>>> +/* When -fstrict-flex-array=2, only [] and [0] are treated as flexiable >>>> + arrays. */ >>>> +/* PR tree-optimization/101836 */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fstrict-flex-array=2" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4]; >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1]; >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0]; >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), 16); >>>> + expect(__builtin_object_size(trailing_1->c, 1), 4); >>>> + expect(__builtin_object_size(trailing_0->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr101836_3.c >>>> new file mode 100644 >>>> index 00000000000..d4ba0afe5bc >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/pr101836_3.c >>>> @@ -0,0 +1,60 @@ >>>> +/* When -fstrict-flex-array=1, [], [0], and [1] are treated as flexible >>>> + arrays. */ >>>> +/* PR tree-optimization/101836 */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fstrict-flex-array=1" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4]; >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1]; >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0]; >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), 16); >>>> + expect(__builtin_object_size(trailing_1->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_0->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr101836_4.c >>>> new file mode 100644 >>>> index 00000000000..b10d3ce312d >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/pr101836_4.c >>>> @@ -0,0 +1,60 @@ >>>> +/* when -fstrict-flex-array=0, all trailing arrays are treated as >>>> + flexible arrays. */ >>>> +/* PR tree-optimization/101836 */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fstrict-flex-array=0" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4]; >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1]; >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0]; >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_1->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_0->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr101836_5.c >>>> new file mode 100644 >>>> index 00000000000..2f6b5f7ae1f >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/pr101836_5.c >>>> @@ -0,0 +1,60 @@ >>>> +/* -fno-strict-flex-array is aliased to -fstrict-flex-array=0, >>>> + all trailing arrays are treated as flexible array. */ >>>> +/* PR tree-optimization/101836 */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fno-strict-flex-array" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4]; >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1]; >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0]; >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_1->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_0->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc.dg/strict-flex-array-2.c >>>> new file mode 100644 >>>> index 00000000000..326ddcfeda5 >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c >>>> @@ -0,0 +1,60 @@ >>>> +/* test the combination of attribute strict_flex_array and option >>>> + -fstrict-flex-array: when both attribute and option specified, >>>> + attribute will have higher priority. */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fstrict-flex-array=3" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4] __attribute__ ((strict_flex_array (0))); >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1] __attribute__ ((strict_flex_array (1))); >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0] __attribute__ ((strict_flex_array (2))); >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_1->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_0->c, 1), -1); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc.dg/strict-flex-array-3.c >>>> new file mode 100644 >>>> index 00000000000..990c5bb6223 >>>> --- /dev/null >>>> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c >>>> @@ -0,0 +1,60 @@ >>>> +/* test the combination of attribute strict_flex_array and option >>>> + -fstrict-flex-array: when both attribute and option specified, >>>> + attribute will have higher priority. */ >>>> +/* { dg-do run } */ >>>> +/* { dg-options "-O2 -fstrict-flex-array=0" } */ >>>> + >>>> +#include <stdio.h> >>>> + >>>> +#define expect(p, _v) do { \ >>>> + size_t v = _v; \ >>>> + if (p == v) \ >>>> + printf("ok: %s == %zd\n", #p, p); \ >>>> + else \ >>>> + { \ >>>> + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ >>>> + __builtin_abort (); \ >>>> + } \ >>>> +} while (0); >>>> + >>>> +struct trailing_array_1 { >>>> + int a; >>>> + int b; >>>> + int c[4] __attribute__ ((strict_flex_array (1))); >>>> +}; >>>> + >>>> +struct trailing_array_2 { >>>> + int a; >>>> + int b; >>>> + int c[1] __attribute__ ((strict_flex_array (2))); >>>> +}; >>>> + >>>> +struct trailing_array_3 { >>>> + int a; >>>> + int b; >>>> + int c[0] __attribute__ ((strict_flex_array (3))); >>>> +}; >>>> +struct trailing_array_4 { >>>> + int a; >>>> + int b; >>>> + int c[]; >>>> +}; >>>> + >>>> +void __attribute__((__noinline__)) stuff( >>>> + struct trailing_array_1 *normal, >>>> + struct trailing_array_2 *trailing_1, >>>> + struct trailing_array_3 *trailing_0, >>>> + struct trailing_array_4 *trailing_flex) >>>> +{ >>>> + expect(__builtin_object_size(normal->c, 1), 16); >>>> + expect(__builtin_object_size(trailing_1->c, 1), 4); >>>> + expect(__builtin_object_size(trailing_0->c, 1), 0); >>>> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >>>> +} >>>> + >>>> +int main(int argc, char *argv[]) >>>> +{ >>>> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); >>>> + >>>> + return 0; >>>> +} >>>> diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc >>>> index 4eb454a4a33..90710ecba72 100644 >>>> --- a/gcc/tree-object-size.cc >>>> +++ b/gcc/tree-object-size.cc >>>> @@ -604,9 +604,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, >>>> else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) >>>> { >>>> tree v = var; >>>> - /* For &X->fld, compute object size only if fld isn't the last >>>> - field, as struct { int i; char c[1]; } is often used instead >>>> - of flexible array member. */ >>>> + bool is_flexible_array_mem_ref = false; >>>> + /* For &X->fld, compute object size if fld isn't a flexible array >>>> + member. */ >>>> while (v && v != pt_var) >>>> switch (TREE_CODE (v)) >>>> { >>>> @@ -633,6 +633,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, >>>> v = NULL_TREE; >>>> break; >>>> } >>>> + is_flexible_array_mem_ref >>>> + = array_at_struct_end_p (v) >>>> + && !DECL_NOT_FLEXARRAY (TREE_OPERAND (v, 1)); >>> >>> As said in the review for the first patch array_at_struct_end_p should >>> check DECL_NOT_FLEXARRAY. >> Okay. >> >> Then, should we change the name of “array_at_struct_end_p” to a more >> descriptive name, for example,”flexible_array_member_p”? Otherwise, It’s >> really confusing. > > It has been that way since forever, so please don't (at least not as > part of this series). Okay, I will keep the name as it in this set of patches. (However, I do think that we should change the name of this routine, it’s really confusing…) Qing > >> >> >>> >>> OK with removing the extra check here. >> >> Okay. >> >> >> thanks. >> >> Qing >>> >>>> while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) >>>> if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) >>>> != UNION_TYPE >>>> @@ -645,12 +648,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, >>>> && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) >>>> == RECORD_TYPE) >>>> { >>>> - tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); >>>> - for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) >>>> - if (TREE_CODE (fld_chain) == FIELD_DECL) >>>> - break; >>>> - >>>> - if (fld_chain) >>>> + /* compute object size only if v is not a >>>> + flexible array member. */ >>>> + if (!is_flexible_array_mem_ref) >>>> { >>>> v = NULL_TREE; >>>> break; >>>> >>> >>> -- >>> Richard Biener <rguenther@suse.de> >>> SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, >>> Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; >>> HRB 36809 (AG Nuernberg) >> >> > > -- > Richard Biener <rguenther@suse.de> > SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, > Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; > HRB 36809 (AG Nuernberg)
On Tue, Jul 19, 2022 at 02:11:19PM +0000, Qing Zhao wrote: > From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 > From: Qing Zhao <qing.zhao@oracle.com> > Date: Mon, 18 Jul 2022 18:12:26 +0000 > Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size > [PR101836] > > Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array > of a structure is flexible array member in __builtin_object_size. FWIW, with these patches I've done builds of the Linux kernel using the ...=3 level, and things are looking correct on our end. (There are, as expected, many places in Linux that need to be fixed, and that work is on-going, guided by this option's results.) -Kees
Thanks a lot for your testing on Linux Kernel. Will work on the version 3 of this patch soon. Qing > On Aug 2, 2022, at 11:30 AM, Kees Cook <keescook@chromium.org> wrote: > > On Tue, Jul 19, 2022 at 02:11:19PM +0000, Qing Zhao wrote: >> From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 >> From: Qing Zhao <qing.zhao@oracle.com> >> Date: Mon, 18 Jul 2022 18:12:26 +0000 >> Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size >> [PR101836] >> >> Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array >> of a structure is flexible array member in __builtin_object_size. > > FWIW, with these patches I've done builds of the Linux kernel using > the ...=3 level, and things are looking correct on our end. (There are, > as expected, many places in Linux that need to be fixed, and that work > is on-going, guided by this option's results.) > > -Kees > > -- > Kees Cook
diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.c new file mode 100644 index 00000000000..e5b4e5160a4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-array is aliased with -ftrict-flex-array=3, which is the + strictest, only [] is treated as flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), 0); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr101836_1.c new file mode 100644 index 00000000000..30ea20427a5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_1.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-array=3 is the strictest, only [] is treated as + flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=3" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), 0); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr101836_2.c new file mode 100644 index 00000000000..ebbe88f433c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_2.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-array=2, only [] and [0] are treated as flexiable + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=2" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr101836_3.c new file mode 100644 index 00000000000..d4ba0afe5bc --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_3.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-array=1, [], [0], and [1] are treated as flexible + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=1" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr101836_4.c new file mode 100644 index 00000000000..b10d3ce312d --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_4.c @@ -0,0 +1,60 @@ +/* when -fstrict-flex-array=0, all trailing arrays are treated as + flexible arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=0" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), -1); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr101836_5.c new file mode 100644 index 00000000000..2f6b5f7ae1f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_5.c @@ -0,0 +1,60 @@ +/* -fno-strict-flex-array is aliased to -fstrict-flex-array=0, + all trailing arrays are treated as flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-strict-flex-array" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), -1); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc.dg/strict-flex-array-2.c new file mode 100644 index 00000000000..326ddcfeda5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c @@ -0,0 +1,60 @@ +/* test the combination of attribute strict_flex_array and option + -fstrict-flex-array: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=3" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4] __attribute__ ((strict_flex_array (0))); +}; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute__ ((strict_flex_array (1))); +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute__ ((strict_flex_array (2))); +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), -1); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc.dg/strict-flex-array-3.c new file mode 100644 index 00000000000..990c5bb6223 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c @@ -0,0 +1,60 @@ +/* test the combination of attribute strict_flex_array and option + -fstrict-flex-array: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=0" } */ + +#include <stdio.h> + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4] __attribute__ ((strict_flex_array (1))); +}; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute__ ((strict_flex_array (2))); +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute__ ((strict_flex_array (3))); +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), 0); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 4eb454a4a33..90710ecba72 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -604,9 +604,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) { tree v = var; - /* For &X->fld, compute object size only if fld isn't the last - field, as struct { int i; char c[1]; } is often used instead - of flexible array member. */ + bool is_flexible_array_mem_ref = false; + /* For &X->fld, compute object size if fld isn't a flexible array + member. */ while (v && v != pt_var) switch (TREE_CODE (v)) { @@ -633,6 +633,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, v = NULL_TREE; break; } + is_flexible_array_mem_ref + = array_at_struct_end_p (v) + && !DECL_NOT_FLEXARRAY (TREE_OPERAND (v, 1)); while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) != UNION_TYPE @@ -645,12 +648,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) == RECORD_TYPE) { - tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); - for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) - if (TREE_CODE (fld_chain) == FIELD_DECL) - break; - - if (fld_chain) + /* compute object size only if v is not a + flexible array member. */ + if (!is_flexible_array_mem_ref) { v = NULL_TREE; break;
From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 From: Qing Zhao <qing.zhao@oracle.com> Date: Mon, 18 Jul 2022 18:12:26 +0000 Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size [PR101836] Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array of a structure is flexible array member in __builtin_object_size. gcc/ChangeLog: PR tree-optimization/101836 * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p and DECL_NOT_FLEXARRAY to determine a flexible array member reference. gcc/testsuite/ChangeLog: PR tree-optimization/101836 * gcc.dg/pr101836.c: New test. * gcc.dg/pr101836_1.c: New test. * gcc.dg/pr101836_2.c: New test. * gcc.dg/pr101836_3.c: New test. * gcc.dg/pr101836_4.c: New test. * gcc.dg/pr101836_5.c: New test. * gcc.dg/strict-flex-array-2.c: New test. * gcc.dg/strict-flex-array-3.c: New test. --- gcc/testsuite/gcc.dg/pr101836.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_1.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_2.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_3.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_4.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_5.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/strict-flex-array-2.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/strict-flex-array-3.c | 60 ++++++++++++++++++++++ gcc/tree-object-size.cc | 18 +++---- 9 files changed, 489 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr101836.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_1.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_2.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_3.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_4.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_5.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c