From patchwork Wed Nov 30 19:02:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 701136 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tTVDM5jn9z9t0X for ; Thu, 1 Dec 2016 06:02:58 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="QzO7WCeg"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=UZjAeykwUXOD3xrxNPvfbf5cHQZvN EFzgEyw4WZioxVwefDMef0cvajbIIKmV1OxecoiqYVt1Kltjm1UeYfvDnLv/VVOQ YSxt+4FsxEAA1zVvSJnZPQLtA0HtW7E4MNXy/Zu+OBIAILGDX7nTRo0QFBPQ3bJg sgcNhA6ybDeJDU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=S+CQPeOs90dTb/YB30K2NBooLRE=; b=QzO 7WCegx+CnnK5AZECIlIPAAjx5/E/hzBuvugHupA7HUujkdAnbvJaycjDewbZB1+y hVHJIUK1d/66ZUVW5cFDBzNX7TGb0Vk5XVEPBHx3zB/BVVLnJ3WdeTCHhapSskwN 7dJp+2dNVUKK2Gu9g8K3psT6FrNU8IYP9LOFXK5E= Received: (qmail 110066 invoked by alias); 30 Nov 2016 19:02:49 -0000 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 Received: (qmail 110045 invoked by uid 89); 30 Nov 2016 19:02:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=20161130 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 30 Nov 2016 19:02:47 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DF7EECA610 for ; Wed, 30 Nov 2016 19:02:45 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-204-100.brq.redhat.com [10.40.204.100]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uAUJ2iQN022714 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 30 Nov 2016 14:02:45 -0500 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id uAUJ2gq5003054; Wed, 30 Nov 2016 20:02:43 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id uAUJ2gn1003053; Wed, 30 Nov 2016 20:02:42 +0100 Date: Wed, 30 Nov 2016 20:02:42 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix UB in dwarf2out.c (PR debug/78587) Message-ID: <20161130190242.GB3541@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-IsSubscribed: yes Hi! This patch fixes 3 spots with UB in dwarf2out.c, furthermore the first spot results in smaller/better debug info. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2016-11-30 Jakub Jelinek PR debug/78587 * dwarf2out.c (loc_descr_plus_const): For negative offset use uint_loc_descriptor instead of int_loc_descriptor and perform negation in unsigned HOST_WIDE_INT type. (scompare_loc_descriptor): Shift UINTVAL left instead of INTVAL. * gcc.dg/debug/pr78587.c: New test. Jakub --- gcc/dwarf2out.c.jj 2016-11-18 22:55:19.000000000 +0100 +++ gcc/dwarf2out.c 2016-11-30 15:16:39.402673343 +0100 @@ -1514,7 +1514,8 @@ loc_descr_plus_const (dw_loc_descr_ref * else { - loc->dw_loc_next = int_loc_descriptor (-offset); + loc->dw_loc_next + = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset); add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0)); } } @@ -13837,7 +13838,7 @@ scompare_loc_descriptor (enum dwarf_loca if (CONST_INT_P (XEXP (rtl, 1)) && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT && (size_of_int_loc_descriptor (shift) + 1 - + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift) + + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift) >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))) @@ -13852,7 +13853,7 @@ scompare_loc_descriptor (enum dwarf_loca add_loc_descr (&op0, int_loc_descriptor (shift)); add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0)); if (CONST_INT_P (XEXP (rtl, 1))) - op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift); + op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift); else { add_loc_descr (&op1, int_loc_descriptor (shift)); --- gcc/testsuite/gcc.dg/debug/pr78587.c.jj 2016-11-30 15:01:08.855153232 +0100 +++ gcc/testsuite/gcc.dg/debug/pr78587.c 2016-11-30 15:20:22.000000000 +0100 @@ -0,0 +1,23 @@ +/* PR debug/78587 */ +/* { dg-do compile } */ +/* { dg-additional-options "-w" } */ + +extern void bar (void); + +void +foo (long long x) +{ + x ^= 9223372036854775808ULL; + bar (); +} + +struct S { int w[4]; } a[1], b; + +void +baz () +{ + int e = (int) baz; + if (e <= -80) + e = 0; + b = a[e]; +}