This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Perform ubsan instrumentation for x >= 0 ? x : -x (take 2)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Richard Biener <rguenther at suse dot de>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Tue, 25 Mar 2014 22:34:32 +0100
- Subject: [PATCH] Perform ubsan instrumentation for x >= 0 ? x : -x (take 2)
- Authentication-results: sourceware.org; auth=none
- References: <20140325073412 dot GM1817 at tucnak dot redhat dot com> <alpine dot LSU dot 2 dot 11 dot 1403251007280 dot 26135 at zhemvz dot fhfr dot qr> <20140325094109 dot GO1817 at tucnak dot redhat dot com> <alpine dot LSU dot 2 dot 11 dot 1403251041200 dot 26135 at zhemvz dot fhfr dot qr>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
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.
--- 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