From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) by sourceware.org (Postfix) with ESMTPS id F02C83858289 for ; Mon, 10 Oct 2022 21:49:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F02C83858289 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=canonical.com Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) (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 smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 9C80840008 for ; Mon, 10 Oct 2022 21:49:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1665438544; bh=QdQrGJFMqBKvbhd3DnecgYLaOJiKrfoyzeEOmehlmP8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Sesi5EvAOfsEDh+4ZOLFTW1QkZDfU7vf5j0tBLu+dy0+FIFGo800i+GeD5dACUT88 vWsU6eML+hOmy/hHaZJW6MfoM/ToJNLYuRQLaosUkjI0YzV8koHXDYwpK02xZGinn8 FYaBqelHQBMf4uJ0YZyqojb/fX7COcPcuo7gyttKL6EaX4dVyxVEWWYJrssuWy+P/6 2c1ZRJs+lagghxB7vCJ4WECDMxDCBdM+jCwlFzYRaS2vYfZ+AVHRKexkGhHdPhKdkz r2oGOFQKLpfJ4Xn0sweKUpM2WHP7vqjKzUQ+adeL10uUOsZd93LWsY1YsQt0zYVEya ALXwCZlqAZkcA== Received: by mail-lf1-f69.google.com with SMTP id x20-20020ac25dd4000000b004a2c484368aso1367522lfq.16 for ; Mon, 10 Oct 2022 14:49:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QdQrGJFMqBKvbhd3DnecgYLaOJiKrfoyzeEOmehlmP8=; b=eFeoHNJVYEXTY7FXhzzVilQ73imf5606vE+p2gzVqn27HgZUeliwf0Z3DEnxqaC2Yv AEOlPjxwZ8x23FYP8+ll+n8kGCLjenx0pXoefljAoDb8F0fEBRxWIJ1U9zeGkOta+mzX CorJelZwSSuUMYf740HlJvkQnP5IxE+4NSxMsanz8mYjzd0n+hCTH0DnLfoVPZ8k32Wb 72kfJ4QWE74sWIqcHsSHDdAjTpCdgb2gupefPNvGoNNhh1J0ruhsAmjYz+WJX0hqR1p6 6UpNsw/F8wpvQ1tSMaRA3gnZXLVjgjt5qhi/UGYxTVMUbcuQrlJuuOiKlS2aJGEScxRt 7lvw== X-Gm-Message-State: ACrzQf1Wnpi3rOFKMbKiKSO+F9T+UqVOCtqg7yTXMQqs+QZsmvd2ZTyv A2l0j3MI5YcJTpHT5anRs3hNtcVRfv7CdzNbQXXJSVMNwiRo7+P5Tx1YTlotZ/PLxGX7Luahcqj bxshhJy0aC6M0/kxts1Sz/Q1SP3W0+B0jf3vBin24nHHPiUgfrxsSjg== X-Received: by 2002:a05:6512:3ca3:b0:4a2:6e07:3f90 with SMTP id h35-20020a0565123ca300b004a26e073f90mr7099070lfv.407.1665438543749; Mon, 10 Oct 2022 14:49:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4ZBBU7Ll6kpkZ8imtUwVBARVZA4WM/YOiDIY3TiWpTqFrr9CCCRatLw21v/aOTVTWIfqhKmWz70/LUdFbEybQ= X-Received: by 2002:a05:6512:3ca3:b0:4a2:6e07:3f90 with SMTP id h35-20020a0565123ca300b004a26e073f90mr7099064lfv.407.1665438543547; Mon, 10 Oct 2022 14:49:03 -0700 (PDT) MIME-Version: 1.0 References: <20221010060050.3741173-1-aurelien@aurel32.net> In-Reply-To: <20221010060050.3741173-1-aurelien@aurel32.net> From: Michael Hudson-Doyle Date: Tue, 11 Oct 2022 10:48:51 +1300 Message-ID: Subject: Re: [PATCH v3] Avoid undefined behaviour in ibm128 implementation of llroundl (BZ #29488) To: Aurelien Jarno Cc: libc-alpha@sourceware.org Content-Type: multipart/alternative; boundary="000000000000c8a83205eab520c8" X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,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 List-Id: --000000000000c8a83205eab520c8 Content-Type: text/plain; charset="UTF-8" On Mon, 10 Oct 2022 at 19:01, Aurelien Jarno wrote: > Detecting an overflow edge case depended on signed overflow of a long > long. Replace the additions and the overflow checks by > __builtin_add_overflow(). > This certainly looks better than my fix, thanks! Cheers, mwh > sysdeps/ieee754/ldbl-128ibm/s_llroundl.c | 21 +++++++++------------ > 1 file changed, 9 insertions(+), 12 deletions(-) > > This patch is based on the original patch from Michael Hudson-Doyle, > using __builtin_add_overflow() as suggested by Florian Weimer > > It passes all tests on ppc64el with gcc 12 with both -O2 and -O3. > > diff --git a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c > b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c > index d85154e73a..d8c0de1faf 100644 > --- a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c > +++ b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c > @@ -66,38 +66,35 @@ __llroundl (long double x) > /* Peg at max/min values, assuming that the above conversions do so. > Strictly speaking, we can return anything for values that > overflow, > but this is more useful. */ > - res = hi + lo; > - > - /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */ > - if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0))) > + if (__glibc_unlikely (__builtin_add_overflow (hi, lo, &res))) > goto overflow; > > xh -= lo; > ldbl_canonicalize (&xh, &xl); > > - hi = res; > if (xh > 0.5) > { > - res += 1; > + if (__glibc_unlikely (__builtin_add_overflow (res, 1, &res))) > + goto overflow; > } > else if (xh == 0.5) > { > if (xl > 0.0 || (xl == 0.0 && res >= 0)) > - res += 1; > + if (__glibc_unlikely (__builtin_add_overflow (res, 1, &res))) > + goto overflow; > } > else if (-xh > 0.5) > { > - res -= 1; > + if (__glibc_unlikely (__builtin_add_overflow (res, -1, &res))) > + goto overflow; > } > else if (-xh == 0.5) > { > if (xl < 0.0 || (xl == 0.0 && res <= 0)) > - res -= 1; > + if (__glibc_unlikely (__builtin_add_overflow (res, -1, &res))) > + goto overflow; > } > > - if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0))) > - goto overflow; > - > return res; > } > else > -- > 2.35.1 > > --000000000000c8a83205eab520c8--