From patchwork Tue Mar 25 21:34:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 333721 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 48503140092 for ; Wed, 26 Mar 2014 08:34:45 +1100 (EST) 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:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=O1M0Mh/lEzPEtxj/G 4LlTmJJY36/VfFp/9FY2ZfngK8TMKHwje0wl4Q05102aoHhnNvrp9Hpin2dcufEj XCwCxoHKqW8gYMwuMrmCucByoe1zzSWIUu8KxVWE3/B6peJru0gH5IeU9lk12PIb xc6NZXa2T62hg4R84Mwxub7efI= 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:references:mime-version :content-type:in-reply-to; s=default; bh=ff1F9OXSUAfNXWNJcEEELAC EHY8=; b=tlqn56IcvQ6+kC39f98i+zChPK6+3i0FvtqcmSOG/kUYZ7G+jaN0qnK /v0M/ofa55rOgPstv149Ug7vcz5f4UnE/aDfzVjAnMOgGrIeUYFlueBoFh3KhvcW qmVr4NhJ0g+7qVXhkKoki6g7gepM2rBIuLgTv/W/B9MG1XvCFI6g= Received: (qmail 17045 invoked by alias); 25 Mar 2014 21:34:39 -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 17032 invoked by uid 89); 25 Mar 2014 21:34:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.8 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 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; Tue, 25 Mar 2014 21:34:37 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2PLYZj6001963 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 25 Mar 2014 17:34:36 -0400 Received: from tucnak.zalov.cz (ovpn-116-51.ams2.redhat.com [10.36.116.51]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s2PLYYkq032311 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 25 Mar 2014 17:34:35 -0400 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.14.8/8.14.7) with ESMTP id s2PLYWn3025083; Tue, 25 Mar 2014 22:34:33 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.14.8/8.14.8/Submit) id s2PLYWtg025082; Tue, 25 Mar 2014 22:34:32 +0100 Date: Tue, 25 Mar 2014 22:34:32 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Perform ubsan instrumentation for x >= 0 ? x : -x (take 2) Message-ID: <20140325213432.GB1817@tucnak.redhat.com> Reply-To: Jakub Jelinek References: <20140325073412.GM1817@tucnak.redhat.com> <20140325094109.GO1817@tucnak.redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes On Tue, Mar 25, 2014 at 10:43:02AM +0100, Richard Biener wrote: > Yes, all transforms in fold-const would be invalid if the result doesn't > behave in the same way wrt overflow. Thus you really should instrument > ABS_EXPR - you can treat it as A > 0 ? A : -A if that simplifies it. > > I don't like the conditions that disable stuff based on sanitization. > > Instrumenting ABS_EXPR shouldn't be too difficult. Ok, here is a patch that does that. Tested on x86_64-linux and i686-linux. Ok for trunk? 2014-03-25 Jakub Jelinek PR sanitizer/60636 * ubsan.c (instrument_si_overflow): Instrument ABS_EXPR. * c-c++-common/ubsan/pr60636.c: New test. Jakub --- gcc/ubsan.c.jj 2014-03-19 14:44:23.000000000 +0100 +++ gcc/ubsan.c 2014-03-25 13:00:35.052459078 +0100 @@ -737,6 +737,21 @@ instrument_si_overflow (gimple_stmt_iter gimple_call_set_lhs (g, lhs); gsi_replace (&gsi, g, false); break; + case ABS_EXPR: + /* Transform i = ABS_EXPR; + into + _N = UBSAN_CHECK_SUB (0, u); + i = ABS_EXPR<_N>; */ + a = build_int_cst (lhstype, 0); + b = gimple_assign_rhs1 (stmt); + g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b); + a = make_ssa_name (lhstype, NULL); + gimple_call_set_lhs (g, a); + gimple_set_location (g, gimple_location (stmt)); + gsi_insert_before (&gsi, g, GSI_SAME_STMT); + gimple_assign_set_rhs1 (stmt, a); + update_stmt (stmt); + break; default: break; } --- gcc/testsuite/c-c++-common/ubsan/pr60636.c.jj 2014-03-25 12:31:29.458629212 +0100 +++ gcc/testsuite/c-c++-common/ubsan/pr60636.c 2014-03-25 12:31:29.458629212 +0100 @@ -0,0 +1,15 @@ +/* PR sanitizer/60636 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=undefined" } */ + +volatile long long int a; + +int +main () +{ + long long int u = -__LONG_LONG_MAX__ - 1; + a = u > 0 ? u : -u; + return 0; +} + +/* { dg-output "negation of -9223372036854775808 cannot be represented in type 'long long int'" } */