From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id AE1133851C2E for ; Wed, 23 Sep 2020 12:19:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AE1133851C2E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mliska@suse.cz X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 06777AC4C; Wed, 23 Sep 2020 12:19:45 +0000 (UTC) From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Subject: [PATCH] Fix UBSAN errors in ipa-cp. To: gcc-patches@gcc.gnu.org Cc: Martin Jambor , Jan Hubicka Message-ID: <7115ce43-bf35-c6e4-46b6-56b386e5cb34@suse.cz> Date: Wed, 23 Sep 2020 14:19:07 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Wed, 23 Sep 2020 12:19:10 -0000 I see the following UBSAN errors: ./xgcc -B. /home/marxin/Programming/gcc/gcc/testsuite/g++.dg/ipa/pr96806.C -std=c++11 -O -fipa-cp -fipa-cp-clone --param=ipa-cp-max-recursive-depth=94 --param=logical-op-non-short-circuit=0 /home/marxin/Programming/gcc2/gcc/ipa-cp.c:3866:20: runtime error: signed integer overflow: 64 + 2147483584 cannot be represented in type 'int' /home/marxin/Programming/gcc2/gcc/ipa-cp.c:3843:16: runtime error: signed integer overflow: -2147483648 + -2147483648 cannot be represented in type 'int' /home/marxin/Programming/gcc2/gcc/ipa-cp.c:3864:20: runtime error: signed integer overflow: 1 + 2147483647 cannot be represented in type 'int' Patch can bootstrap on x86_64-linux-gnu and survives regression tests. Ready to be installed? Thanks, Martin gcc/ChangeLog: * ipa-cp.c (safe_add): Handle also very small negative values. (value_topo_info::propagate_effects): Use properly safe_add. --- gcc/ipa-cp.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/gcc/ipa-cp.c b/gcc/ipa-cp.c index b3e7d41ea10..e39ee28726d 100644 --- a/gcc/ipa-cp.c +++ b/gcc/ipa-cp.c @@ -3832,13 +3832,15 @@ propagate_constants_topo (class ipa_topo_info *topo) /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return - the bigger one if otherwise. */ + the bigger one if otherwise. Similarly for negative numbers. */ static int safe_add (int a, int b) { if (a > INT_MAX/2 || b > INT_MAX/2) return a > b ? a : b; + else if (a < -INT_MAX/2 || b < -INT_MAX/2) + return a > b ? b : a; else return a + b; } @@ -3861,9 +3863,10 @@ value_topo_info::propagate_effects () for (val = base; val; val = val->scc_next) { - time = safe_add (time, - val->local_time_benefit + val->prop_time_benefit); - size = safe_add (size, val->local_size_cost + val->prop_size_cost); + time = safe_add (time, val->local_time_benefit); + time = safe_add (time, val->prop_time_benefit); + size = safe_add (size, val->local_size_cost); + size = safe_add (size, val->prop_size_cost); } for (val = base; val; val = val->scc_next) -- 2.28.0