c.opt (flag_chkp_flexible_struct_trailing_arrays): Add new option.
authorAlexander Ivchenko <alexander.ivchenko@intel.com>
Tue, 27 Dec 2016 13:31:43 +0000 (13:31 +0000)
committerAlexander Ivchenko <aivchenk@gcc.gnu.org>
Tue, 27 Dec 2016 13:31:43 +0000 (13:31 +0000)
2016-12-27  Alexander Ivchenko  <alexander.ivchenko@intel.com>

* c-family/c.opt (flag_chkp_flexible_struct_trailing_arrays):
  Add new option.
  (fchkp-narrow-to-innermost-array): Fix typo.
* doc/cpp.texi (flag_chkp_flexible_struct_trailing_arrays): Ditto.
* tree-chkp.c (chkp_may_narrow_to_field ): Forbid
  narrowing when flag_chkp_flexible_struct_trailing_arrays is used
  and the field is the last array field in the structure.

2016-12-27  Alexander Ivchenko  <alexander.ivchenko@intel.com>

* gcc.target/i386/mpx/vla-trailing-1-lbv.c: New test.
* gcc.target/i386/mpx/vla-trailing-1-nov.c: Ditto.
* gcc.target/i386/mpx/vla-trailing-1-ubv.c: Ditto.

From-SVN: r243936

gcc/ChangeLog
gcc/c-family/c.opt
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-lbv.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-nov.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-ubv.c [new file with mode: 0644]
gcc/tree-chkp.c

index c15c1d3d8c0b52392ec0861a6db75d1a5e479365..25a3edb23e802fdaff9a0aa2358070ff8120176c 100644 (file)
@@ -1,3 +1,13 @@
+2016-12-27  Alexander Ivchenko  <alexander.ivchenko@intel.com>
+
+       * c-family/c.opt (flag_chkp_flexible_struct_trailing_arrays):
+       Add new option.
+       (fchkp-narrow-to-innermost-array): Fix typo.
+       * doc/cpp.texi (flag_chkp_flexible_struct_trailing_arrays): Ditto.
+       * tree-chkp.c (chkp_may_narrow_to_field ): Forbid
+       narrowing when flag_chkp_flexible_struct_trailing_arrays is used
+       and the field is the last array field in the structure.
+
 2016-12-27  Uros Bizjak  <ubizjak@gmail.com>
 
        * config/i386/i386.md (andqi_ext_1): Use general_operand
index a5333a35e4149e64e89ad03939ed25b6b37ec306..1d40d76f1b656c9d007497d5acd95cf3ef93ad90 100644 (file)
@@ -1207,7 +1207,13 @@ narrowing is on, field bounds are used.  Otherwise full object bounds are used.
 fchkp-narrow-to-innermost-array
 C ObjC C++ ObjC++ LTO RejectNegative Report Var(flag_chkp_narrow_to_innermost_arrray)
 Forces Pointer Bounds Checker to use bounds of the innermost arrays in case of
-nested static arryas access.  By default outermost array is used.
+nested static arrays access.  By default outermost array is used.
+
+fchkp-flexible-struct-trailing-arrays
+C ObjC C++ ObjC++ LTO Report Var(flag_chkp_flexible_struct_trailing_arrays)
+Forces Pointer Bounds Checker to treat all trailing arrays in structures as
+possibly flexible.  By default only arrays fields with zero length or that are
+marked with attribute bnd_variable_size are treated as flexible.
 
 fchkp-optimize
 C ObjC C++ ObjC++ LTO Report Var(flag_chkp_optimize) Init(-1)
index 9af6e849b7c569433d4cc7deaa4705ad40132eca..b27691458ffbbf74d6582c0952ed01eb187031cb 100644 (file)
@@ -447,7 +447,7 @@ Objective-C and Objective-C++ Dialects}.
 -fchkp-treat-zero-dynamic-size-as-infinite -fchkp-check-read @gol
 -fchkp-check-read -fchkp-check-write -fchkp-store-bounds @gol
 -fchkp-instrument-calls -fchkp-instrument-marked-only @gol
--fchkp-use-wrappers @gol
+-fchkp-use-wrappers -fchkp-flexible-struct-trailing-arrays@gol
 -fstack-protector -fstack-protector-all -fstack-protector-strong @gol
 -fstack-protector-explicit -fstack-check @gol
 -fstack-limit-register=@var{reg}  -fstack-limit-symbol=@var{sym} @gol
@@ -10954,6 +10954,13 @@ Forces Pointer Bounds Checker to use narrowed bounds for the address of the
 first field in the structure.  By default a pointer to the first field has
 the same bounds as a pointer to the whole structure.
 
+@item -fchkp-flexible-struct-trailing-arrays
+@opindex fchkp-flexible-struct-trailing-arrays
+@opindex fno-chkp-flexible-struct-trailing-arrays
+Forces Pointer Bounds Checker to treat all trailing arrays in structures as
+possibly flexible.  By default only array fields with zero length or that are
+marked with attribute bnd_variable_size are treated as flexible.
+
 @item -fchkp-narrow-to-innermost-array
 @opindex fchkp-narrow-to-innermost-array
 @opindex fno-chkp-narrow-to-innermost-array
index fc9233a21bb9a921e8d1f3ba2219b80d3fb9779d..d8eecd4bec287c8fe3c230287f88e8eb01df275a 100644 (file)
@@ -1,3 +1,9 @@
+2016-12-27  Alexander Ivchenko  <alexander.ivchenko@intel.com>
+
+       * gcc.target/i386/mpx/vla-trailing-1-lbv.c: New test.
+       * gcc.target/i386/mpx/vla-trailing-1-nov.c: Ditto.
+       * gcc.target/i386/mpx/vla-trailing-1-ubv.c: Ditto.
+
 2016-12-27  Uros Bizjak  <ubizjak@gmail.com>
 
        PR target/78904
diff --git a/gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-lbv.c b/gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-lbv.c
new file mode 100644 (file)
index 0000000..9739920
--- /dev/null
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+/* { dg-shouldfail "bounds violation" } */
+/* { dg-options "-fcheck-pointer-bounds -mmpx -fchkp-flexible-struct-trailing-arrays" } */
+
+
+#define SHOULDFAIL
+
+#include "mpx-check.h"
+
+struct S
+{
+  int a;
+  int p[10];
+};
+
+int rd (int *p, int i)
+{
+  int res = p[i];
+  printf ("%d\n", res);
+  return res;
+}
+
+int mpx_test (int argc, const char **argv)
+{
+  struct S *s = (struct S *)alloca (sizeof(struct S) + sizeof (int)*100);
+  rd (s->p, -2);
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-nov.c b/gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-nov.c
new file mode 100644 (file)
index 0000000..f5c8f95
--- /dev/null
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+/* { dg-options "-fcheck-pointer-bounds -mmpx -fchkp-flexible-struct-trailing-arrays" } */
+
+
+#include "mpx-check.h"
+
+struct S
+{
+  int a;
+  int p[10];
+};
+
+int rd (int *p, int i)
+{
+  int res = p[i];
+  printf ("%d\n", res);
+  return res;
+}
+
+int mpx_test (int argc, const char **argv)
+{
+  struct S *s = (struct S *)alloca (sizeof(struct S) + sizeof (int)*100);
+  rd (s->p, 0);
+  rd (s->p, 99);
+  s->p[0];
+  s->p[99];
+  
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-ubv.c b/gcc/testsuite/gcc.target/i386/mpx/vla-trailing-1-ubv.c
new file mode 100644 (file)
index 0000000..8385a5a
--- /dev/null
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+/* { dg-shouldfail "bounds violation" } */
+/* { dg-options "-fcheck-pointer-bounds -mmpx -fchkp-flexible-struct-trailing-arrays" } */
+
+
+#define SHOULDFAIL
+
+#include "mpx-check.h"
+
+struct S
+{
+  int a;
+  int p[10];
+};
+
+int rd (int *p, int i)
+{
+  int res = p[i];
+  printf ("%d\n", res);
+  return res;
+}
+
+int mpx_test (int argc, const char **argv)
+{
+  struct S *s = (struct S *)alloca (sizeof(struct S) + sizeof (int)*100);
+  rd (s->p, 110);
+
+  return 0;
+}
index 27696828716dd439154cec193ac4bb6a8ba610c6..6c7862ca04f269ce738bdcc30b5aaf3267e592f8 100644 (file)
@@ -3272,6 +3272,9 @@ chkp_may_narrow_to_field (tree field)
 {
   return DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
     && tree_to_uhwi (DECL_SIZE (field)) != 0
+    && !(flag_chkp_flexible_struct_trailing_arrays
+        && TREE_CODE(TREE_TYPE(field)) == ARRAY_TYPE
+        && !DECL_CHAIN (field))
     && (!DECL_FIELD_OFFSET (field)
        || TREE_CODE (DECL_FIELD_OFFSET (field)) == INTEGER_CST)
     && (!DECL_FIELD_BIT_OFFSET (field)