From patchwork Wed Apr 3 08:16:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1919106 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=V9QxBpf3; 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 4V8czd089Zz23tl for ; Wed, 3 Apr 2024 19:16:59 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C1D30384641E for ; Wed, 3 Apr 2024 08:16:57 +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 543803847718 for ; Wed, 3 Apr 2024 08:16:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 543803847718 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 543803847718 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=1712132195; cv=none; b=VanxY1lqTvuUCxVoCJbHHJYbuBrKHeVWntOGoOP56PWmJbqMJkz/22/jjinVJWz5Y4hd/Z5w3KAbPihisuXvFSUt5xcy5q5oKv02L3yf6DZhC0TBnjYILS1WopFs/h2W60hgolN7i3qwAhN5JKjXj6ylHqleCP3hi8SIJShl1BQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712132195; c=relaxed/simple; bh=mACRAU+R8zF/oY6Lc0PTsuu8YBoMNNJEdLwYff5BDx4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=DMVxbNhTLcHiJgmX0ykBdLY+VwJtThYSnxEwKC1VKb6lXJGoTog864sUkCB+1ZI55oXX2CWNiHBAfwwdZNbIKIJG+pchX5NEMsFW/bt3zP9lvKM9VdAItwIpLm7A0uQFQ/B6V4fnrYSgqe8jGS/kI6cLufNejP3/Z+72klK51Cw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712132193; 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:in-reply-to:in-reply-to: references:references; bh=skH2i44L2nL+bckCtq941g7iznNMruZAN+BiQRk/HCk=; b=V9QxBpf3/Cy0ozjYrzZw8ju67KH8nMvT0PIdpTJEY0KUbQCqLGq4jaVTxcCErTlVcM9v0L 5saj+t3d5M81WtdL269ZYf5vq9Ki7wvV3zN2QL5SBv1ye/SvzwEL/trdZnIW7EuPdqX3Ny U6bNK8ZXgjhfMRwnfLDG0jRy+P+Z2AE= 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-636-tFdGki_iPwmEPwfOB3a2vQ-1; Wed, 03 Apr 2024 04:16:31 -0400 X-MC-Unique: tFdGki_iPwmEPwfOB3a2vQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 752E685A5B6; Wed, 3 Apr 2024 08:16:31 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3B0501121313; Wed, 3 Apr 2024 08:16:31 +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 4338GPck1845292 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 3 Apr 2024 10:16:25 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4338GP3b1845290; Wed, 3 Apr 2024 10:16:25 +0200 Date: Wed, 3 Apr 2024 10:16:24 +0200 From: Jakub Jelinek To: Simon Chopin Cc: gcc-patches@gcc.gnu.org Subject: [committed] libquadmath: Don't assume the storage for __float128 arguments is aligned [PR114533] Message-ID: References: <20240312190352.3455451-1-simon.chopin@canonical.com> MIME-Version: 1.0 In-Reply-To: <20240312190352.3455451-1-simon.chopin@canonical.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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 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 On Tue, Mar 12, 2024 at 08:03:52PM +0100, Simon Chopin wrote: > On x86, this compiles into movdqa which segfaults on unaligned access. > > This kind of failure has been seen when running against glibc 2.39, > which incidentally changed the printf implementation to move away from > alloca() for this data to instead append it at the end of an existing > "scratch buffer", with arbitrary alignement, whereas alloca() was > probably more likely to be naturally aligned. > > Tested by adding the patch to the Ubuntu gcc-14 package in > https://launchpad.net/~schopin/+archive/ubuntu/libquadmath The formatting was incorrect and we need to also change it in another place. Here is what I've committed instead: With the register_printf_type/register_printf_modifier/register_printf_specifier APIs the C library is just told the size of the argument and is provided with a callback to fetch the argument from va_list using va_arg into C library provided memory. The C library isn't told what alignment requirement it has, but we were using direct load of a __float128 value from that memory which assumes __alignof (__float128) alignment. The following patch fixes that by using memcpy instead. I haven't been able to reproduce an actual crash, tried #include #include #include int main () { __float128 r; int prec = 20; int width = 46; char buf[128]; r = 2.0q; r = sqrtq (r); int n = quadmath_snprintf (buf, sizeof buf, "%+-#*.20Qe", width, r); if ((size_t) n < sizeof buf) printf ("%s\n", buf); /* Prints: +1.41421356237309504880e+00 */ quadmath_snprintf (buf, sizeof buf, "%Qa", r); if ((size_t) n < sizeof buf) printf ("%s\n", buf); /* Prints: 0x1.6a09e667f3bcc908b2fb1366ea96p+0 */ n = quadmath_snprintf (NULL, 0, "%+-#46.*Qe", prec, r); if (n > -1) { char *str = malloc (n + 1); if (str) { quadmath_snprintf (str, n + 1, "%+-#46.*Qe", prec, r); printf ("%s\n", str); /* Prints: +1.41421356237309504880e+00 */ } free (str); } printf ("%+-#*.20Qe\n", width, r); printf ("%Qa\n", r); printf ("%+-#46.*Qe\n", prec, r); printf ("%d %Qe %d %Qe %d %Qe\n", 1, r, 2, r, 3, r); return 0; } In any case, I think memcpy for loading from it is right. 2024-04-03 Simon Chopin Jakub Jelinek PR libquadmath/114533 * printf/printf_fp.c (__quadmath_printf_fp): Use memcpy to copy __float128 out of args. * printf/printf_fphex.c (__quadmath_printf_fphex): Likewise. Signed-off-by: Simon Chopin Jakub --- libquadmath/printf/printf_fp.c.jj 2020-01-12 11:54:39.787362505 +0100 +++ libquadmath/printf/printf_fp.c 2024-04-02 19:28:31.254670746 +0200 @@ -363,7 +363,7 @@ __quadmath_printf_fp (struct __quadmath_ /* Fetch the argument value. */ { - fpnum = **(const __float128 **) args[0]; + memcpy (&fpnum, *(const void *const *) args[0], sizeof (fpnum)); /* Check for special values: not a number or infinity. */ if (isnanq (fpnum)) --- libquadmath/printf/printf_fphex.c.jj 2020-01-12 11:54:39.787362505 +0100 +++ libquadmath/printf/printf_fphex.c 2024-04-02 19:29:03.968223151 +0200 @@ -163,7 +163,8 @@ __quadmath_printf_fphex (struct __quadma /* Fetch the argument value. */ { - fpnum.value = **(const __float128 **) args[0]; + memcpy (&fpnum.value, *(const void *const *) args[0], + sizeof (fpnum.value)); /* Check for special values: not a number or infinity. */ if (isnanq (fpnum.value))