From patchwork Mon Aug 13 12:01:34 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 176919 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 980392C0087 for ; Mon, 13 Aug 2012 22:09:24 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1345464564; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Cc:Subject:Message-ID:Reply-To: MIME-Version:Content-Type:Content-Disposition:User-Agent: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=jCrTwF9BR1jYVfaX6ekZ Rpm5zdo=; b=YTXeglMCgGLnITpeo27PfHco0TBxBB1cw23bWzsYIR987Xvsd59o z/C4X44EOwKLeuHbe50lP4rWQkBbsjeNNajbyGQzxMQSjCYHRiRtha/R4ZQxzslv CsYZwYe/RcdL0adpu4oQkGOzq0yTI4By47egPYTrG9VBf3zAEcX6cS4= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Cc:Subject:Message-ID:Reply-To:MIME-Version:Content-Type:Content-Disposition:User-Agent:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=noEXMoJ5RUm/0lqB/7dEQZF6WTvsxgOY86NbWvoZjGyBpIg+sZDl8gOtZNHv/X W9UM+kZoZ6UQxb27BO0TDwCIQP6XKqBmZyvNyFGxVqNkXARhw1ff7/HcR0qhbgX0 gAHzCkEUPro+VGpVzzSKbPtSsJZGKtVcth5B/NYyYvvAg=; Received: (qmail 5358 invoked by alias); 13 Aug 2012 12:09:17 -0000 Received: (qmail 5349 invoked by uid 22791); 13 Aug 2012 12:09:16 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 13 Aug 2012 12:09:00 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q7DC8tVS030913 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 13 Aug 2012 08:08:55 -0400 Received: from zalov.redhat.com (vpn1-5-192.ams2.redhat.com [10.36.5.192]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q7DC8ru0002938 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 13 Aug 2012 08:08:54 -0400 Received: from zalov.cz (localhost [127.0.0.1]) by zalov.redhat.com (8.14.5/8.14.5) with ESMTP id q7DC1ZrP001630; Mon, 13 Aug 2012 14:01:36 +0200 Received: (from jakub@localhost) by zalov.cz (8.14.5/8.14.5/Submit) id q7DC1Y2X001629; Mon, 13 Aug 2012 14:01:34 +0200 Date: Mon, 13 Aug 2012 14:01:34 +0200 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Cc: John Regehr Subject: [PATCH] Fix some undefined behavior spots in gcc sources (PR c/53968) Message-ID: <20120813120134.GJ1999@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi! John Regehr discovered a couple of spots in GCC sources that invoke undefined behavior during bootstrap/regtest, the following patch fixes most of them. I couldn't reproduce the diagnostic.c failure and would like to leave the ipa hunk to Honza, I think the probability/frequency code often might go out of the expected limits and then invoke undefined signed overflow. The double_int_mask change is because e.g. with -E, ptr_mode is VOIDmode and so double_int_mask is called with 0 precision during compiler initialization. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2012-08-13 Jakub Jelinek PR c/53968 * tree.c (integer_pow2p): Avoid undefined signed overflows. * simplify-rtx.c (neg_const_int): Likewise. * expr.c (fixup_args_size_notes): Likewise. * stor-layout.c (set_min_and_max_values_for_integral_type): Likewise. * double-int.c (mul_double_wide_with_sign): Likewise. (double_int_mask): Likewise. * tree-ssa-loop-ivopts.c (get_address_cost): Likewise. Jakub --- gcc/tree.c.jj 2012-08-10 15:48:53.000000000 +0200 +++ gcc/tree.c 2012-08-13 10:25:46.610502929 +0200 @@ -1849,7 +1849,7 @@ int integer_pow2p (const_tree expr) { int prec; - HOST_WIDE_INT high, low; + unsigned HOST_WIDE_INT high, low; STRIP_NOPS (expr); --- gcc/simplify-rtx.c.jj 2012-08-10 15:49:20.000000000 +0200 +++ gcc/simplify-rtx.c 2012-08-13 09:51:43.628508537 +0200 @@ -66,7 +66,7 @@ static rtx simplify_binary_operation_1 ( static rtx neg_const_int (enum machine_mode mode, const_rtx i) { - return gen_int_mode (- INTVAL (i), mode); + return gen_int_mode (-(unsigned HOST_WIDE_INT) INTVAL (i), mode); } /* Test whether expression, X, is an immediate constant that represents --- gcc/expr.c.jj 2012-08-10 15:49:07.000000000 +0200 +++ gcc/expr.c 2012-08-13 10:40:01.182501639 +0200 @@ -3828,7 +3828,7 @@ fixup_args_size_notes (rtx prev, rtx las add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size)); #ifdef STACK_GROWS_DOWNWARD - this_delta = -this_delta; + this_delta = -(unsigned HOST_WIDE_INT) this_delta; #endif args_size -= this_delta; } --- gcc/stor-layout.c.jj 2012-08-10 15:49:20.000000000 +0200 +++ gcc/stor-layout.c 2012-08-13 10:14:14.388505253 +0200 @@ -2568,10 +2568,14 @@ set_min_and_max_values_for_integral_type = build_int_cst_wide (type, (precision - HOST_BITS_PER_WIDE_INT > 0 ? -1 - : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1), + : (HOST_WIDE_INT) + (((unsigned HOST_WIDE_INT) 1 + << (precision - 1)) - 1)), (precision - HOST_BITS_PER_WIDE_INT - 1 > 0 - ? (((HOST_WIDE_INT) 1 - << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1 + ? (HOST_WIDE_INT) + ((((unsigned HOST_WIDE_INT) 1 + << (precision - HOST_BITS_PER_WIDE_INT + - 1))) - 1) : 0)); } --- gcc/double-int.c.jj 2012-08-10 15:49:07.000000000 +0200 +++ gcc/double-int.c 2012-08-13 11:24:17.816495757 +0200 @@ -170,7 +170,7 @@ mul_double_wide_with_sign (unsigned HOST { k = i + j; /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */ - carry += arg1[i] * arg2[j]; + carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j]; /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */ carry += prod[k]; prod[k] = LOWPART (carry); @@ -625,7 +625,7 @@ double_int_mask (unsigned prec) else { mask.high = 0; - mask.low = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1; + mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0; } return mask; --- gcc/tree-ssa-loop-ivopts.c.jj 2012-08-10 15:49:07.000000000 +0200 +++ gcc/tree-ssa-loop-ivopts.c 2012-08-13 10:17:51.227504425 +0200 @@ -3173,7 +3173,7 @@ get_address_cost (bool symbol_present, b for (i = width; i >= 0; i--) { - off = -((HOST_WIDE_INT) 1 << i); + off = -((unsigned HOST_WIDE_INT) 1 << i); XEXP (addr, 1) = gen_int_mode (off, address_mode); if (memory_address_addr_space_p (mem_mode, addr, as)) break; @@ -3182,7 +3182,7 @@ get_address_cost (bool symbol_present, b for (i = width; i >= 0; i--) { - off = ((HOST_WIDE_INT) 1 << i) - 1; + off = ((unsigned HOST_WIDE_INT) 1 << i) - 1; XEXP (addr, 1) = gen_int_mode (off, address_mode); if (memory_address_addr_space_p (mem_mode, addr, as)) break;