From patchwork Tue Feb 13 08:07:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 200260 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp389709dyb; Tue, 13 Feb 2024 00:08:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFW9EZvXLg7OR86leROWMGDTKlPcfPY3M6km0mxYIS0Xuy3RGWhqHJjp35vjel3fhB4JLMS X-Received: by 2002:a9d:4b17:0:b0:6e2:e97a:4700 with SMTP id q23-20020a9d4b17000000b006e2e97a4700mr3901322otf.31.1707811726477; Tue, 13 Feb 2024 00:08:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707811726; cv=pass; d=google.com; s=arc-20160816; b=h37dOeDkXxGZi8dZTJVcn8Y+GSIuYbrZvbYZ4u+8sU9pD0/X5A8F/0N02G8AZp/hvb 8ZqDtElGeSqemxnfG57HmGG5rUS7sBa7xdu4uGsRoHjtWqTuT27vuBHpbEL7kwk3kIin RInqdpil2eHR/KUsvtXxJ5l3S/KKfA8AFBgcGbJj6KG7Z2+i0Hh91tvO4kd73SNLfw2K Mb8/D2GpSv95ZYd8i5Ol30XCIfyU4+6Q5gqVPrTDMpb1HVBjcx+mjbMPIN4OXroc8nGk xzEgihcp0nXNdIf7zvcPrZifL/4R1V+un7516k7pG+WSB7e02itfBiY+qjqcsFp1f3P5 w6ag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=0tqLisLUORZJr9e/5tHovgDSdimGqcywmjrUHc4jEsw=; fh=40/yrmqbmMUMdtv76nUnK+rwhiiNT2VwDqRXUBFBRNY=; b=I65ehh+AOg1YtQGmGUkY5LEQ/GkGe8PeR2oQO/QNZsDpYUThsUyK96DzGF1iIyHOe5 RBJppWcrbm8KWH0nRqOhIH6WDG9cYrkkhIJOFYgvTO9zaEwhQpokBb2O3eqmbC0FCt9a li2nk3e01G96yswslBUglZ0DYiWAC8ToHKsVM1ebYppKzT/3iFOtv6zgmfZN7LMIEngk 4mZCsMBxhqB+YOfY6gA+Po9EYwN33fZT5TkZTXxau3bcHeGpjqh3mMSefGJv65KbNbFm T5l+2NkhxfnShcf9zIQiunbrKh1er6Y2MnKIVYNL8I3viQp0SQxWi650afMrnPPn6Lz/ 65Yg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IkkpS4b9; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCVDrlYRm1bZU4I6Y/rfQCauPTd2eu2j4gYp2C5HXPbg3qviDTiwkPW9JxT9exmiR8/w+/+m3EILDNvTCC2AM7vIa4bi8A== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c12-20020a05620a0cec00b007871aa6b53esi1049731qkj.564.2024.02.13.00.08.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 00:08:46 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IkkpS4b9; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 26C2A3858404 for ; Tue, 13 Feb 2024 08:08:46 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 9DEF63858D20 for ; Tue, 13 Feb 2024 08:07:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9DEF63858D20 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 9DEF63858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707811677; cv=none; b=IQgJm2OEY+UB5DphMB9ir5ZVmJji5+RJuG9NytaISnm4hAOKS2dApuehFQnSBFDnFl9OSGN2e7uf6qE1gPNYQYQs/k6Ci8CzvXEt2FuHz4DFS+U/IZgxFdoYpgGAOxtIQ9ser0gNcw9rl0y+jdWG0bQaMSaQDu2Dzj77vmCyGr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707811677; c=relaxed/simple; bh=rEQ32e0qJ1XmIburvlYdtxnOBxIH+8VXhR5dqkWxtLY=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=WL7LEII9WBhEEJHRl3m1KWg8pVgcJ30naih8DlCKCf0xl3CjfoHhZj9qTg7hG3l1NadyYu5OuJgnLB3WM1bH3Xz3tI+rWuCDCKoOn9TwEuxYSsH8sE/yC2t6FkZ35ex13KBRlUPndQeezTLjOd4zTQQlofzBUNer9EOftMXNWOg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707811675; 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=0tqLisLUORZJr9e/5tHovgDSdimGqcywmjrUHc4jEsw=; b=IkkpS4b90Hagw0DElBmczAaMc5r4XCM6VrndVWHzQh5AiHdR0fpnnUHNOvXqtpunN9pWHa MnbD0KFSalSJGp/eiN9moXZxBoTQB3u+KoAWqzrh+OxReX19k/Jy6TWAchHUrYvbrKXmHo PW5Pv3GkBSFW3smlFoosThLP+Wr2OX4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-327-mf5luz52NzGyS3QLghqElQ-1; Tue, 13 Feb 2024 03:07:51 -0500 X-MC-Unique: mf5luz52NzGyS3QLghqElQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 9691B185A780; Tue, 13 Feb 2024 08:07:51 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5482EC03381; Tue, 13 Feb 2024 08:07:51 +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 41D87mDs1327546 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 13 Feb 2024 09:07:49 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41D87lJD1327545; Tue, 13 Feb 2024 09:07:47 +0100 Date: Tue, 13 Feb 2024 09:07:47 +0100 From: Jakub Jelinek To: Richard Biener , "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libgcc: Fix UB in FP_FROM_BITINT Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 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_H2, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790770388836125134 X-GMAIL-MSGID: 1790770388836125134 Hi! As I wrote earlier, I was seeing FAIL: gcc.dg/torture/bitint-24.c -O0 execution test FAIL: gcc.dg/torture/bitint-24.c -O2 execution test with the ia32 _BitInt enablement patch on i686-linux. I thought floatbitintxf.c was miscompiled with -O2 -march=i686 -mtune=generic, but it turned out to be UB in it. If a signed _BitInt to be converted to binary floating point has (after sign extension from possible partial limb to full limb) one or more most significant limbs equal to all ones and then in the limb below (the most significant non-~(UBILtype)0 limb) has the most significant limb cleared, like for 32-bit limbs 0x81582c05U, 0x0a8b01e4U, 0xc1b8b18fU, 0x2aac2a08U, -1U, -1U then bitint_reduce_prec can't reduce it to that 0x2aac2a08U limb, so msb is all ones and precision is negative (so it reduced precision from 161 to 192 bits down to 160 bits, in theory could go as low as 129 bits but that wouldn't change anything on the following behavior). But still iprec is negative, -160 here. For that case (i.e. where we are dealing with an negative input), the code was using 65 - __builtin_clzll (~msb) to compute how many relevant bits we have from the msb. Unfortunately that invokes UB for msb all ones. The right number of relevant bits in that case is 1 though (like for -2 it is 2 and -4 or -3 3 as already computed) - all we care about from that is that the most significant bit is set (i.e. the number is negative) and the bits below that should be supplied from the limbs below. So, the following patch fixes it by special casing it not to invoke UB. For msb 0 we already have a special case from before (but that is also different because msb 0 implies the whole number is 0 given the way bitint_reduce_prec works - even if we have limbs like ..., 0x80000000U, 0U the reduction can skip the most significant limb and msb then would be the one below it), so if iprec > 0, we already don't call __builtin_clzll on 0. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-13 Jakub Jelinek * soft-fp/bitint.h (FP_FROM_BITINT): If iprec < 0 and msb is all ones, just set n to 1 instead of using __builtin_clzll (~msb). Jakub --- libgcc/soft-fp/bitint.h.jj 2024-01-12 22:06:06.248661862 +0100 +++ libgcc/soft-fp/bitint.h 2024-02-12 18:56:42.947974875 +0100 @@ -276,7 +276,11 @@ bitint_negate (UBILtype *d, const UBILty } \ if (iprec < 0) \ { \ - n = sizeof (0ULL) * __CHAR_BIT__ + 1 - __builtin_clzll (~msb);\ + if (msb == (UBILtype) -1) \ + n = 1; \ + else \ + n = (sizeof (0ULL) * __CHAR_BIT__ + 1 \ + - __builtin_clzll (~msb)); \ if (BIL_TYPE_SIZE > DI##_BITS && n > DI##_BITS) \ { \ iv = msb >> (n - DI##_BITS); \