From patchwork Fri Jul 6 13:11:17 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulrich Weigand X-Patchwork-Id: 169472 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 AE5F82C007F for ; Fri, 6 Jul 2012 23:11:41 +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=1342185102; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Received:Message-Id:Received:Subject:To:Date: From:MIME-Version:Content-Type:Content-Transfer-Encoding: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=W4cbzBRe/jWnLINqsiJi oM0SoMg=; b=x6A0BWiSZc9pLCNLsFfI1/Gug4bK0D8RD7myziYBaPGqgeSOrQod P+qTHiinr3ZyWLbrkvhJXDpP2QTRkjsp2+S8kfh0ftmy9Lckii8cxyq/s2O73HdJ u26aIjBIf+FYAG9dqv+gPf7aOIFhv6K4xp5Jdmmunl4poyrbXcB3YSo= 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:Received:Message-Id:Received:Subject:To:Date:From:MIME-Version:Content-Type:Content-Transfer-Encoding:x-cbid:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=DRoGq5zEoWfESZMNV24izLVFhWuT9teU+vtOeCtlTcOl/ZeYVvPJaMvWBYZm5O Jw+JWnpzpYK26oUf0UhErMRF5Z/GoBjS7buk9o9U3ndIppmgMOX8cP1NgS1JjOEV uorixKbwJKS1VdDiEO0JPgI+dVE3iLWOpku12ocMcdi7Q=; Received: (qmail 6639 invoked by alias); 6 Jul 2012 13:11:37 -0000 Received: (qmail 6522 invoked by uid 22791); 6 Jul 2012 13:11:37 -0000 X-SWARE-Spam-Status: No, hits=-3.3 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, MSGID_FROM_MTA_HEADER, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from e06smtp10.uk.ibm.com (HELO e06smtp10.uk.ibm.com) (195.75.94.106) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 06 Jul 2012 13:11:22 +0000 Received: from /spool/local by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 6 Jul 2012 14:11:21 +0100 Received: from d06nrmr1707.portsmouth.uk.ibm.com (9.149.39.225) by e06smtp10.uk.ibm.com (192.168.101.140) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 6 Jul 2012 14:11:20 +0100 Received: from d06av02.portsmouth.uk.ibm.com (d06av02.portsmouth.uk.ibm.com [9.149.37.228]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q66DBJfR2678878 for ; Fri, 6 Jul 2012 14:11:19 +0100 Received: from d06av02.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av02.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q66DBJF6028482 for ; Fri, 6 Jul 2012 07:11:19 -0600 Received: from tuxmaker.boeblingen.de.ibm.com (tuxmaker.boeblingen.de.ibm.com [9.152.85.9]) by d06av02.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with SMTP id q66DBHwE028422 for ; Fri, 6 Jul 2012 07:11:17 -0600 Message-Id: <201207061311.q66DBHwE028422@d06av02.portsmouth.uk.ibm.com> Received: by tuxmaker.boeblingen.de.ibm.com (sSMTP sendmail emulation); Fri, 06 Jul 2012 15:11:17 +0200 Subject: [PATCH] Fix undefined behaviour in combine.c:force_to_mode To: gcc-patches@gcc.gnu.org Date: Fri, 6 Jul 2012 15:11:17 +0200 (CEST) From: "Ulrich Weigand" MIME-Version: 1.0 x-cbid: 12070613-4966-0000-0000-000002DC6347 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 Hello, in testing a patch on arm-linux-gnueabihf, I ran into a bootstrap comparison failure that turned out to be caused by a pre-existing bug in common code, where combine.c code exposed undefined behaviour. The problem is this code in force_to_mode when simplifying a LSHIFTRT: /* Here we can only do something if the shift count is a constant, this shift constant is valid for the host, and we can do arithmetic in OP_MODE. */ if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT && HWI_COMPUTABLE_MODE_P (op_mode)) { rtx inner = XEXP (x, 0); unsigned HOST_WIDE_INT inner_mask; /* Select the mask of the bits we need for the shift operand. */ inner_mask = mask << INTVAL (XEXP (x, 1)); If the source code being compiled contains a shift by a negative constant shift amount, INTVAL (XEXP (x, 1)) at this point can be negative. (Note that this does not necessarily mean that the original program has undefined behaviour, since that shift could be in a place that is never actually executed.) If this is the case, then the last line above will cause undefined behaviour of GCC itself. Note that most other places in force_to_mode optimizing shifts already check for non-negative shift amounts; but in this place the check is missing. The following patch adds the check here as well, fixing the undefined behaviour (and subsequent bootstrap comparison failure) in my test. Tested on arm-linux-gnueabihf. OK for mainline? Bye, Ulrich ChangeLog: * combine.c (force_to_mode) [LSHIFTRT]: Avoid undefined behaviour due to negative shift amount. === modified file 'gcc/combine.c' --- gcc/combine.c 2012-02-22 12:22:43 +0000 +++ gcc/combine.c 2012-07-03 19:46:18 +0000 @@ -8432,6 +8432,7 @@ in OP_MODE. */ if (CONST_INT_P (XEXP (x, 1)) + && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT && HWI_COMPUTABLE_MODE_P (op_mode)) {