This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981, take 2)


On Fri, Sep 01, 2017 at 07:10:51PM +0200, Richard Biener wrote:
> OK, I thought we have one.  Can you add a helper for it please? 
> replace_with_nop or so?  I thought there's maybe replace_with_value which
> handles null lhs by replacing with nop.  (can't check, writing from phone)

Actually, you're right, replace_call_with_value does the right thing
when called on call without lhs (all these internal fns don't have lhs),
and NULL_TREE val ensures we'd ICE if that ever wasn't the case.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-09-01  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.

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

--- gcc/gimple-fold.c.jj	2017-09-01 09:26:37.054748039 +0200
+++ gcc/gimple-fold.c	2017-09-01 19:37:03.283795450 +0200
@@ -3936,18 +3936,43 @@ gimple_fold_call (gimple_stmt_iterator *
 					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);
--- gcc/testsuite/gcc.dg/ubsan/pr81981.c.jj	2017-09-01 19:35:37.555782465 +0200
+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c	2017-09-01 19:35:37.555782465 +0200
@@ -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" } */
+}


	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]