re PR sanitizer/81981 (-fsanitize=undefined makes a -Wmaybe-uninitialized warning...
authorJakub Jelinek <jakub@redhat.com>
Mon, 4 Sep 2017 08:11:44 +0000 (10:11 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 4 Sep 2017 08:11:44 +0000 (10:11 +0200)
PR sanitizer/81981
* gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR
and UBSAN_BOUNDS internal calls.  Clean up IFN_UBSAN_OBJECT_SIZE
handling.  Use replace_call_with_value with NULL instead of
gsi_replace, unlink_stmt_vdef and release_defs.

* gcc.dg/ubsan/pr81981.c: New test.

From-SVN: r251641

gcc/ChangeLog
gcc/gimple-fold.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/ubsan/pr81981.c [new file with mode: 0644]

index 4d1c041acaa2b3a0c995f03afaf08f35c13f78a4..50d5e26f96a9848aa0dc107fc204db80c41bfba7 100644 (file)
@@ -1,5 +1,11 @@
 2017-09-04  Jakub Jelinek  <jakub@redhat.com>
 
+       PR sanitizer/81981
+       * gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR
+       and UBSAN_BOUNDS internal calls.  Clean up IFN_UBSAN_OBJECT_SIZE
+       handling.  Use replace_call_with_value with NULL instead of
+       gsi_replace, unlink_stmt_vdef and release_defs.
+
        * gdbhooks.py (OptMachineModePrinter.to_string): Use 8 spaces
        instead of tab.
 
index 367b35c5c9ba6f70f89d0e4314a86f4a1cf84076..8366e4b50b8a42a677d3dd8b39746b3f2cf46bdd 100644 (file)
@@ -3936,18 +3936,43 @@ gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
                                        gimple_call_arg (stmt, 2));
          break;
        case IFN_UBSAN_OBJECT_SIZE:
-         if (integer_all_onesp (gimple_call_arg (stmt, 2))
-             || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
-                 && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST
-                 && tree_int_cst_le (gimple_call_arg (stmt, 1),
-                                     gimple_call_arg (stmt, 2))))
+         {
+           tree offset = gimple_call_arg (stmt, 1);
+           tree objsize = gimple_call_arg (stmt, 2);
+           if (integer_all_onesp (objsize)
+               || (TREE_CODE (offset) == INTEGER_CST
+                   && TREE_CODE (objsize) == INTEGER_CST
+                   && tree_int_cst_le (offset, objsize)))
+             {
+               replace_call_with_value (gsi, NULL_TREE);
+               return true;
+             }
+         }
+         break;
+       case IFN_UBSAN_PTR:
+         if (integer_zerop (gimple_call_arg (stmt, 1)))
            {
-             gsi_replace (gsi, gimple_build_nop (), false);
-             unlink_stmt_vdef (stmt);
-             release_defs (stmt);
+             replace_call_with_value (gsi, NULL_TREE);
              return true;
            }
          break;
+       case IFN_UBSAN_BOUNDS:
+         {
+           tree index = gimple_call_arg (stmt, 1);
+           tree bound = gimple_call_arg (stmt, 2);
+           if (TREE_CODE (index) == INTEGER_CST
+               && TREE_CODE (bound) == INTEGER_CST)
+             {
+               index = fold_convert (TREE_TYPE (bound), index);
+               if (TREE_CODE (index) == INTEGER_CST
+                   && tree_int_cst_le (index, bound))
+                 {
+                   replace_call_with_value (gsi, NULL_TREE);
+                   return true;
+                 }
+             }
+         }
+         break;
        case IFN_GOACC_DIM_SIZE:
        case IFN_GOACC_DIM_POS:
          result = fold_internal_goacc_dim (stmt);
index 38294c6e9f4510596357ddee4f6237bf821f9dbb..cd7ac1d84e7161d7d7bd0c88ec5d1949c388ef68 100644 (file)
@@ -1,5 +1,8 @@
 2017-09-04  Jakub Jelinek  <jakub@redhat.com>
 
+       PR sanitizer/81981
+       * gcc.dg/ubsan/pr81981.c: New test.
+
        PR tree-optimization/70043
        PR testsuite/82093
        * gfortran.dg/vect/pr70043.f90 (fn1): Start loop from 1 instead of 0.
diff --git a/gcc/testsuite/gcc.dg/ubsan/pr81981.c b/gcc/testsuite/gcc.dg/ubsan/pr81981.c
new file mode 100644 (file)
index 0000000..b2636d4
--- /dev/null
@@ -0,0 +1,21 @@
+/* PR sanitizer/81981 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wmaybe-uninitialized -fsanitize=undefined -ffat-lto-objects" } */
+
+int v;
+
+int
+foo (int i)
+{
+  int t[1], u[1];
+  int n = 0;
+
+  if (i)
+    {
+      t[n] = i;
+      u[0] = i;
+    }
+
+  v = u[0];            /* { dg-warning "may be used uninitialized in this function" } */
+  return t[0];         /* { dg-warning "may be used uninitialized in this function" } */
+}