From patchwork Wed Mar 8 09:38:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 66126 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp232718wrd; Wed, 8 Mar 2023 01:39:31 -0800 (PST) X-Google-Smtp-Source: AK7set/Nw3d/gPdTBgR2n/9OFvsBwdjTl9yLcViu9xeXJr0CLQ/YTISGYXm0w1BO55GwOR4YSq2x X-Received: by 2002:a17:907:9493:b0:87e:9441:4c6c with SMTP id dm19-20020a170907949300b0087e94414c6cmr23895987ejc.49.1678268371050; Wed, 08 Mar 2023 01:39:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678268371; cv=none; d=google.com; s=arc-20160816; b=f0XVkfaEbH+T75jq6uyGGMYhg9GLyU/6E9i2CF+hSvd3BEu32lBFGKTZYXxpzJ9oWq jYHZqU8hJKBeW2WVEYP1mhewHIpPy6V7OVVIRyUsflrUFOyFgfMdzZatzprDN7PcKr5f sxFGB04JrubTyBcqY/9TFx58++4xHbcHoa94Upbek1HBR7zSYwCSQnFiPafZkCPDN8tN 31EWqVViEMWmyldE8QIVCDuyQHxUo+cYVqQUoWMLJ4rKqS2qc2+deKpZ407IGXKrTeN1 7YPJlVTyuwcofV4E5xhpKfxXzY4riFRs6AspvXEpLttfKZQt71jeEzUZ6iwTLZSvMvGL qKlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:cc:to:date:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=M9m/vbi4YmYGCqZ/JfvJj6O0IOt+Ftm4O7S77o/a8PU=; b=MNwH9W4edWRevsDPVQsCVFimxQtO6s/f8j5K91iVB9cxC7KBInvP/V4/qhS39wpdOa COifAujRRpHF6ejBKRPNg2VoCkBnwlHSkXKVk7G7zFbY5kltqg4kHz0Klir3rc4YSdkD Qw3W1bTxIiZcgf2qBw7Ht2hsimIkqI3JdmYnVeTXneu0bJFKPNhxfQkKW0A8dRKuN1pI CU6n+IapxsY/rvuIid2bCL6VeMD6rdoS24DQZNE8wyucn9ONeme09tEojEEHTA/LAl3i tZNJjIJaSDf9Q/0DwBELpOuJEcrzk0KOrRztf7krMRcEZqqovYKvGgF6xsALOPzlB3Rd EQeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=P5vGRT8n; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a13-20020a1709062b0d00b008b315ed8bd6si2826156ejg.787.2023.03.08.01.39.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Mar 2023 01:39:31 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=P5vGRT8n; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 98D8C3858C62 for ; Wed, 8 Mar 2023 09:39:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 98D8C3858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678268369; bh=M9m/vbi4YmYGCqZ/JfvJj6O0IOt+Ftm4O7S77o/a8PU=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=P5vGRT8n3oVgpwnx89mu5sZ5PbpipT0S+Q1u9Ii6zm+/1Ovrz8VNtst35pdFerJsK CxxcZaTBPi6BmiuwZmE4y0/yaS/IKQSn+yHqliCxX5IKwReOiYRv2AOCMx+0pSlQLZ 0A7eIEqSgHYG1g2rHXzMwBpJ/raGlVJBb/4kdE6Y= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 94E923858C54 for ; Wed, 8 Mar 2023 09:38:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 94E923858C54 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id B392121A01; Wed, 8 Mar 2023 09:38:43 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 8936B2C141; Wed, 8 Mar 2023 09:38:43 +0000 (UTC) Date: Wed, 8 Mar 2023 09:38:43 +0000 (UTC) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek Subject: [PATCH] middle-end/108995 - avoid folding when sanitizing overflow User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20230308093929.98D8C3858C62@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759791935400826026?= X-GMAIL-MSGID: =?utf-8?q?1759791935400826026?= The following plugs one place in extract_muldiv where it should avoid folding when sanitizing overflow. I'm unsure about the testcase, I didn't find any that tests for a runtime sanitizer error ... Bootstrapped and tested on x86_64-unknown-linux-gnu. OK? 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. --- gcc/fold-const.cc | 7 +++---- gcc/testsuite/gcc.dg/ubsan/pr108995.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/ubsan/pr108995.c 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..79a178c6751 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr108995.c @@ -0,0 +1,15 @@ +/* { dg-do run { xfail *-*-* } } */ +/* 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; +}