diff mbox

Perform ubsan instrumentation for x >= 0 ? x : -x (take 2)

Message ID 20140325213432.GB1817@tucnak.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek March 25, 2014, 9:34 p.m. UTC
On Tue, Mar 25, 2014 at 10:43:02AM +0100, Richard Biener wrote:
> Yes, all transforms in fold-const would be invalid if the result doesn't
> behave in the same way wrt overflow.  Thus you really should instrument
> ABS_EXPR - you can treat it as A > 0 ? A : -A if that simplifies it.
> 
> I don't like the conditions that disable stuff based on sanitization.
> 
> Instrumenting ABS_EXPR shouldn't be too difficult.

Ok, here is a patch that does that.  Tested on x86_64-linux and i686-linux.
Ok for trunk?

2014-03-25  Jakub Jelinek  <jakub@redhat.com>

	PR sanitizer/60636
	* ubsan.c (instrument_si_overflow): Instrument ABS_EXPR.

	* c-c++-common/ubsan/pr60636.c: New test.



	Jakub

Comments

Richard Biener March 26, 2014, 8:59 a.m. UTC | #1
On Tue, 25 Mar 2014, Jakub Jelinek wrote:

> On Tue, Mar 25, 2014 at 10:43:02AM +0100, Richard Biener wrote:
> > Yes, all transforms in fold-const would be invalid if the result doesn't
> > behave in the same way wrt overflow.  Thus you really should instrument
> > ABS_EXPR - you can treat it as A > 0 ? A : -A if that simplifies it.
> > 
> > I don't like the conditions that disable stuff based on sanitization.
> > 
> > Instrumenting ABS_EXPR shouldn't be too difficult.
> 
> Ok, here is a patch that does that.  Tested on x86_64-linux and i686-linux.
> Ok for trunk?

Ok.

Thanks,
Richard.

> 2014-03-25  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR sanitizer/60636
> 	* ubsan.c (instrument_si_overflow): Instrument ABS_EXPR.
> 
> 	* c-c++-common/ubsan/pr60636.c: New test.
> 
> --- gcc/ubsan.c.jj	2014-03-19 14:44:23.000000000 +0100
> +++ gcc/ubsan.c	2014-03-25 13:00:35.052459078 +0100
> @@ -737,6 +737,21 @@ instrument_si_overflow (gimple_stmt_iter
>        gimple_call_set_lhs (g, lhs);
>        gsi_replace (&gsi, g, false);
>        break;
> +    case ABS_EXPR:
> +      /* Transform i = ABS_EXPR<u>;
> +	 into
> +	 _N = UBSAN_CHECK_SUB (0, u);
> +	 i = ABS_EXPR<_N>;  */
> +      a = build_int_cst (lhstype, 0);
> +      b = gimple_assign_rhs1 (stmt);
> +      g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b);
> +      a = make_ssa_name (lhstype, NULL);
> +      gimple_call_set_lhs (g, a);
> +      gimple_set_location (g, gimple_location (stmt));
> +      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
> +      gimple_assign_set_rhs1 (stmt, a);
> +      update_stmt (stmt);
> +      break;
>      default:
>        break;
>      }
> --- gcc/testsuite/c-c++-common/ubsan/pr60636.c.jj	2014-03-25 12:31:29.458629212 +0100
> +++ gcc/testsuite/c-c++-common/ubsan/pr60636.c	2014-03-25 12:31:29.458629212 +0100
> @@ -0,0 +1,15 @@
> +/* PR sanitizer/60636 */
> +/* { dg-do run } */
> +/* { dg-options "-fsanitize=undefined" } */
> +
> +volatile long long int a;
> +
> +int
> +main ()
> +{
> +  long long int u = -__LONG_LONG_MAX__ - 1;
> +  a = u > 0 ? u : -u;
> +  return 0;
> +}
> +
> +/* { dg-output "negation of -9223372036854775808 cannot be represented in type 'long long int'" } */
> 
> 
> 	Jakub
> 
>
diff mbox

Patch

--- gcc/ubsan.c.jj	2014-03-19 14:44:23.000000000 +0100
+++ gcc/ubsan.c	2014-03-25 13:00:35.052459078 +0100
@@ -737,6 +737,21 @@  instrument_si_overflow (gimple_stmt_iter
       gimple_call_set_lhs (g, lhs);
       gsi_replace (&gsi, g, false);
       break;
+    case ABS_EXPR:
+      /* Transform i = ABS_EXPR<u>;
+	 into
+	 _N = UBSAN_CHECK_SUB (0, u);
+	 i = ABS_EXPR<_N>;  */
+      a = build_int_cst (lhstype, 0);
+      b = gimple_assign_rhs1 (stmt);
+      g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b);
+      a = make_ssa_name (lhstype, NULL);
+      gimple_call_set_lhs (g, a);
+      gimple_set_location (g, gimple_location (stmt));
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+      gimple_assign_set_rhs1 (stmt, a);
+      update_stmt (stmt);
+      break;
     default:
       break;
     }
--- gcc/testsuite/c-c++-common/ubsan/pr60636.c.jj	2014-03-25 12:31:29.458629212 +0100
+++ gcc/testsuite/c-c++-common/ubsan/pr60636.c	2014-03-25 12:31:29.458629212 +0100
@@ -0,0 +1,15 @@ 
+/* PR sanitizer/60636 */
+/* { dg-do run } */
+/* { dg-options "-fsanitize=undefined" } */
+
+volatile long long int a;
+
+int
+main ()
+{
+  long long int u = -__LONG_LONG_MAX__ - 1;
+  a = u > 0 ? u : -u;
+  return 0;
+}
+
+/* { dg-output "negation of -9223372036854775808 cannot be represented in type 'long long int'" } */