From patchwork Thu Aug 10 16:56:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Hubicka X-Patchwork-Id: 1819980 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=fbiZAx5L; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RMCl357zzz1yYC for ; Fri, 11 Aug 2023 02:57:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4C81D3858025 for ; Thu, 10 Aug 2023 16:57:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C81D3858025 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691686621; bh=jYpufd06KHGqaTKQ4b4ymvWNfgsXS23OYTMyOgNI3Ws=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fbiZAx5LMI+g/yV5DtlonhZnZ+b1uT/OMkSAXgTaZbflKNgp0hTa+prQKlUdK8j/B ReGMzZYZCtBr7LKKdDt7Q8bJFCtmB6x7l+UWo4OMZFMzR+JOnECLsTNyhyEwdKZV9a ezB/+7Jx1NG/kbBdc/+Wx0mvRbJU/YL5TcNou5dM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from nikam.ms.mff.cuni.cz (nikam.ms.mff.cuni.cz [195.113.20.16]) by sourceware.org (Postfix) with ESMTPS id 90E8D385781F for ; Thu, 10 Aug 2023 16:56:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 90E8D385781F Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id 414C0281310; Thu, 10 Aug 2023 18:56:39 +0200 (CEST) Date: Thu, 10 Aug 2023 18:56:39 +0200 To: gcc-patches@gcc.gnu.org Subject: Fix undefined behaviour in profile_count::differs_from_p Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, JMQ_SPF_NEUTRAL, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: Jan Hubicka via Gcc-patches From: Jan Hubicka Reply-To: Jan Hubicka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch avoid overflow in profile_count::differs_from_p and also makes it to return false from one of the values is undefined while other is defined. Bootstrapped/regtested x86_64-linux, comitted. gcc/ChangeLog: * profile-count.cc (profile_count::differs_from_p): Fix overflow and handling of undefined values. diff --git a/gcc/profile-count.cc b/gcc/profile-count.cc index e63c9432388..a14f379db8f 100644 --- a/gcc/profile-count.cc +++ b/gcc/profile-count.cc @@ -128,13 +128,14 @@ profile_count::differs_from_p (profile_count other) const { gcc_checking_assert (compatible_p (other)); if (!initialized_p () || !other.initialized_p ()) - return false; + return initialized_p () != other.initialized_p (); if ((uint64_t)m_val - (uint64_t)other.m_val < 100 || (uint64_t)other.m_val - (uint64_t)m_val < 100) return false; if (!other.m_val) return true; - int64_t ratio = (int64_t)m_val * 100 / other.m_val; + uint64_t ratio; + safe_scale_64bit (m_val, 100, other.m_val, &ratio); return ratio < 99 || ratio > 101; }