From 57e011dc3fa82b0b04c5dc752290c65e3ba37f4c Mon Sep 17 00:00:00 2001 From: jakub Date: Wed, 26 Mar 2014 09:18:26 +0000 Subject: [PATCH] PR sanitizer/60636 * ubsan.c (instrument_si_overflow): Instrument ABS_EXPR. * c-c++-common/ubsan/pr60636.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@208841 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 3 +++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/c-c++-common/ubsan/pr60636.c | 15 +++++++++++++++ gcc/ubsan.c | 15 +++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/ubsan/pr60636.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 43c221429b015..7faec434e0559 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,8 @@ 2014-03-26 Jakub Jelinek + PR sanitizer/60636 + * ubsan.c (instrument_si_overflow): Instrument ABS_EXPR. + * tree-vrp.c (simplify_internal_call_using_ranges): If only one range is range_int_cst_p, but not both, at least optimize addition/subtraction of 0 and multiplication by 0 or 1. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0cb408ca1a7e3..cdc8e9aa3da02 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-03-26 Jakub Jelinek + + PR sanitizer/60636 + * c-c++-common/ubsan/pr60636.c: New test. + 2014-03-26 Andreas Schwab * g++.dg/torture/pr60315.C: Remove duplication. diff --git a/gcc/testsuite/c-c++-common/ubsan/pr60636.c b/gcc/testsuite/c-c++-common/ubsan/pr60636.c new file mode 100644 index 0000000000000..4164341375529 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/pr60636.c @@ -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'" } */ diff --git a/gcc/ubsan.c b/gcc/ubsan.c index 22470dafeb2d1..8a8c289aa2363 100644 --- a/gcc/ubsan.c +++ b/gcc/ubsan.c @@ -737,6 +737,21 @@ instrument_si_overflow (gimple_stmt_iterator gsi) gimple_call_set_lhs (g, lhs); gsi_replace (&gsi, g, false); break; + case ABS_EXPR: + /* Transform i = ABS_EXPR; + 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; }