From patchwork Thu Mar 7 08:09:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1909126 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=L5AVjwO5; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tr26T5xzdz1yX4 for ; Thu, 7 Mar 2024 19:10:24 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A90F83857BB2 for ; Thu, 7 Mar 2024 08:10:22 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 1910C3858D35 for ; Thu, 7 Mar 2024 08:10:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1910C3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1910C3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709799002; cv=none; b=ZioraPdvmAQwtxaNDV79MdVJgV46RKAY2TyklpUBYjGASMnPm2GaB/DtoqLFqn1LrghXjRWrXwFQ/Sh8ioLHOaIPa3AcIPNGVHmqjls+QZstC2OoOV0rOXgug++vo0/NquvLB53yaThJ09iZDHiPBu/oK9dDDnzObITNJnYto2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709799002; c=relaxed/simple; bh=4Se6GYeDmNbOrNZiFks3faCiAOggWtUFkH0z5EshUdM=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=qkaLpmt0FPkyaqrZYQtPstYvB+FCsHYytldNTAlm9eslzMZZJgC4tqfJHNhZWE9tK4y7vD+hD0sAYTimpBGVQdkHoF4JOr684YG6QzJh+m7BZzkAxiM2wYvQYNgFhAk0gY9Ylx9uNqxoj6zUneCxrOrI4MCitVKjfZSGGCIpQPo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709799000; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=7pG22fTxGIfm77vs0hj5/zqiPmdoSpTNdHNXED+fP5o=; b=L5AVjwO5213LpHHEVusES+/+a7uw8yrI1pgOp4dGOrMQBe1vhc3VadaLKw0aGFTi5YJDj7 /hk6f3XJUvi6yNw3FQSq6CEHBACH5xIa2T6ydHbf4s3C+PEFtVEYKV1xutgf7WWl7LKckK /y0RFVCEvQSojusr6RnjPM4Gpt8DfP0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-494-FBQoceJ7MwmRLvxLrmct8A-1; Thu, 07 Mar 2024 03:09:57 -0500 X-MC-Unique: FBQoceJ7MwmRLvxLrmct8A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DA8B729ABA11; Thu, 7 Mar 2024 08:09:56 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.226.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9EBCF40C6CB7; Thu, 7 Mar 2024 08:09:56 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 42789nFt2806124 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 7 Mar 2024 09:09:49 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 42789nQe2806123; Thu, 7 Mar 2024 09:09:49 +0100 Date: Thu, 7 Mar 2024 09:09:49 +0100 From: Jakub Jelinek To: Richard Biener , Jeff Law Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] expand: Fix UB in choose_mult_variant [PR105533] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! As documented in the function comment, choose_mult_variant attempts to compute costs of 3 different cases, val, -val and val - 1. The -val case is actually only done if val fits into host int, so there should be no overflow, but the val - 1 case is done unconditionally. val is shwi (but inside of synth_mult already uhwi), so when val is HOST_WIDE_INT_MIN, val - 1 invokes UB. The following patch fixes that by using val - HOST_WIDE_INT_1U, but I'm not really convinced it would DTRT for > 64-bit modes, so I've guarded it as well. Though, arch would need to have really strange costs that something that could be expressed as x << 63 would be better expressed as (x * 0x7fffffffffffffff) + 1 In the long term, I think we should just rewrite choose_mult_variant/synth_mult etc. to work on wide_int. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-07 Jakub Jelinek PR middle-end/105533 * expmed.cc (choose_mult_variant): Only try the val - 1 variant if val is not HOST_WIDE_INT_MIN or if mode has exactly HOST_BITS_PER_WIDE_INT precision. Avoid triggering UB while computing val - 1. * gcc.dg/pr105533.c: New test. Jakub --- gcc/expmed.cc.jj 2024-01-03 11:51:27.327789537 +0100 +++ gcc/expmed.cc 2024-03-06 16:01:27.898693433 +0100 @@ -3285,11 +3285,15 @@ choose_mult_variant (machine_mode mode, limit.latency = mult_cost - op_cost; } - synth_mult (&alg2, val - 1, &limit, mode); - alg2.cost.cost += op_cost; - alg2.cost.latency += op_cost; - if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost)) - *alg = alg2, *variant = add_variant; + if (val != HOST_WIDE_INT_MIN + || GET_MODE_UNIT_PRECISION (mode) == HOST_BITS_PER_WIDE_INT) + { + synth_mult (&alg2, val - HOST_WIDE_INT_1U, &limit, mode); + alg2.cost.cost += op_cost; + alg2.cost.latency += op_cost; + if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost)) + *alg = alg2, *variant = add_variant; + } return MULT_COST_LESS (&alg->cost, mult_cost); } --- gcc/testsuite/gcc.dg/pr105533.c.jj 2024-03-06 16:03:50.347757251 +0100 +++ gcc/testsuite/gcc.dg/pr105533.c 2024-03-06 16:03:26.226084751 +0100 @@ -0,0 +1,9 @@ +/* PR middle-end/105533 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +long long +foo (long long x, long long y) +{ + return ((x < 0) & (y != 0)) * (-__LONG_LONG_MAX__ - 1); +}