From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 22D9B3857C55; Thu, 9 Mar 2023 13:29:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22D9B3857C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678368588; bh=KmFOmSAPzpKlpJoHYFkbosr4IYBO95f1a6COTk686mg=; h=From:To:Subject:Date:From; b=yNZziAOixlGc0chwiWfdTvD/mbkxb1ZISIbgM6A4S34aQQNw4KiwiwAWZ4nJThKpO oguXF62/CzvEToa6wVCkbDiITfLgggneXBoxofs3MPqFvGWQtH9xGE9xQUCoyxWEmu lLsBNUANDjqT5b8LD+OsgeG1p8/wm4497gICRqCg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6550] middle-end/108995 - avoid folding when sanitizing overflow X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/master X-Git-Oldrev: bad177e848787258070415dbe002b2c6fba1c511 X-Git-Newrev: ace65db9215882b95e2ead1bb0dc8c54c2ea69be Message-Id: <20230309132948.22D9B3857C55@sourceware.org> Date: Thu, 9 Mar 2023 13:29:48 +0000 (GMT) List-Id: https://gcc.gnu.org/g:ace65db9215882b95e2ead1bb0dc8c54c2ea69be commit r13-6550-gace65db9215882b95e2ead1bb0dc8c54c2ea69be Author: Richard Biener Date: Wed Mar 8 09:06:44 2023 +0100 middle-end/108995 - avoid folding when sanitizing overflow The following plugs one place in extract_muldiv where it should avoid folding when sanitizing overflow. PR middle-end/108995 * fold-const.cc (extract_muldiv_1): Avoid folding (CST * b) / CST2 when sanitizing overflow and we rely on overflow being undefined. * gcc.dg/ubsan/pr108995.c: New testcase. Diff: --- gcc/fold-const.cc | 7 +++---- gcc/testsuite/gcc.dg/ubsan/pr108995.c | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 99882ef820a..02a24c5fe65 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -7093,6 +7093,7 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type, If we have an unsigned type, we cannot do this since it will change the result if the original computation overflowed. */ if (TYPE_OVERFLOW_UNDEFINED (ctype) + && !TYPE_OVERFLOW_SANITIZED (ctype) && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR) || (tcode == MULT_EXPR && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR @@ -7102,8 +7103,7 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type, if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c), TYPE_SIGN (type))) { - if (TYPE_OVERFLOW_UNDEFINED (ctype)) - *strict_overflow_p = true; + *strict_overflow_p = true; return fold_build2 (tcode, ctype, fold_convert (ctype, op0), fold_convert (ctype, const_binop (TRUNC_DIV_EXPR, @@ -7112,8 +7112,7 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type, else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1), TYPE_SIGN (type))) { - if (TYPE_OVERFLOW_UNDEFINED (ctype)) - *strict_overflow_p = true; + *strict_overflow_p = true; return fold_build2 (code, ctype, fold_convert (ctype, op0), fold_convert (ctype, const_binop (TRUNC_DIV_EXPR, diff --git a/gcc/testsuite/gcc.dg/ubsan/pr108995.c b/gcc/testsuite/gcc.dg/ubsan/pr108995.c new file mode 100644 index 00000000000..166825b2ef8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr108995.c @@ -0,0 +1,18 @@ +/* { dg-do run { target int32 } } */ +/* { dg-shouldfail "ubsan" } */ +/* With optimization we constant fold and diagnose the overflow and do + not sanitize anything. */ +/* { dg-skip-if "" { *-*-* } { "*" } { ! "-O0" } } */ +/* { dg-options "-fsanitize=undefined -fno-sanitize-recover=undefined" } */ + +int a; +const int b = 44514; +int *c = &a; + +int main () +{ + *c = 65526 * b / 6; + return 0; +} + +/* { dg-output "signed integer overflow: 44514 \\* 65526 cannot be represented in type 'int'" } */