re PR sanitizer/68418 (ubsan complains about left shifts even with -fwrapv)

gcc:
	PR sanitizer/68418
	* c-family/c-ubsan.c (ubsan_instrument_shift): Disable
	sanitization of left shifts for wrapping signed types as well.

gcc/testsuite:
	PR sanitizer/68418
	* gcc.dg/ubsan/c99-wrapv-shift-1.c,
	gcc.dg/ubsan/c99-wrapv-shift-2.c: New testcases.

From-SVN: r231582
This commit is contained in:
Paolo Bonzini 2015-12-12 08:29:27 +00:00 committed by Paolo Bonzini
parent aa31006f5d
commit b300764434
5 changed files with 39 additions and 6 deletions

View File

@ -1,3 +1,9 @@
2015-12-12 Paolo Bonzini <bonzini@gnu.org>
PR sanitizer/68418
* c-family/c-ubsan.c (ubsan_instrument_shift): Disable
sanitization of left shifts for wrapping signed types as well.
2015-12-11 Eric Botcazou <ebotcazou@adacore.com>
PR middle-end/68215

View File

@ -124,12 +124,17 @@ ubsan_instrument_shift (location_t loc, enum tree_code code,
t = fold_convert_loc (loc, op1_utype, op1);
t = fold_build2 (GT_EXPR, boolean_type_node, t, uprecm1);
/* If this is not a signed operation, don't perform overflow checks.
Also punt on bit-fields. */
if (!INTEGRAL_TYPE_P (type0)
|| TYPE_OVERFLOW_WRAPS (type0)
|| GET_MODE_BITSIZE (TYPE_MODE (type0)) != TYPE_PRECISION (type0))
;
/* For signed x << y, in C99/C11, the following:
(unsigned) x >> (uprecm1 - y)
if non-zero, is undefined. */
if (code == LSHIFT_EXPR
&& !TYPE_UNSIGNED (type0)
&& flag_isoc99)
else if (code == LSHIFT_EXPR && flag_isoc99 && cxx_dialect < cxx11)
{
tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
fold_convert (op1_utype, unshare_expr (op1)));
@ -142,9 +147,7 @@ ubsan_instrument_shift (location_t loc, enum tree_code code,
/* For signed x << y, in C++11 and later, the following:
x < 0 || ((unsigned) x >> (uprecm1 - y))
if > 1, is undefined. */
if (code == LSHIFT_EXPR
&& !TYPE_UNSIGNED (type0)
&& (cxx_dialect >= cxx11))
else if (code == LSHIFT_EXPR && cxx_dialect >= cxx11)
{
tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
fold_convert (op1_utype, unshare_expr (op1)));

View File

@ -1,3 +1,9 @@
2015-12-12 Paolo Bonzini <bonzini@gnu.org>
PR sanitizer/68418
* gcc.dg/ubsan/c99-wrapv-shift-1.c,
gcc.dg/ubsan/c99-wrapv-shift-2.c: New testcases.
2015-12-11 Jeff Law <law@redhat.com>
PR tree-optimization/68844

View File

@ -0,0 +1,9 @@
/* { dg-do run } */
/* { dg-options "-fsanitize=shift -fwrapv -w -std=c99" } */
int
main (void)
{
int a = -42;
a << 1;
}

View File

@ -0,0 +1,9 @@
/* { dg-do run } */
/* { dg-options "-fsanitize=shift -fwrapv -w -std=c99" } */
int
main (void)
{
int a = 1;
a <<= 31;
}