From nobody Mon Jan 06 23:46:35 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YRrRw0clwz5kR33; Mon, 06 Jan 2025 23:46:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YRrRv6q2Mz44YQ; Mon, 6 Jan 2025 23:46:35 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736207196; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4aXBUtrrRaCB5K2VL1LcXRGNdeocDSJ0jRGgTG6Kw6g=; b=NFu25rOuM/4y0kN/6wCed97/bT1wsGq7YTJdkqExYNQKEjDNy/xQvr/LyxhATkcS1yYug0 zsokSZwYCp7+KgFheOKrW8p0zAXeTufbocShUSev2S4+Tw8tTh0zhg7lHcda9ZqwMDFGG6 aShjJZbmnKnn8/wHxIZ2ploWjs7e+UWpXgYot6/DbQ0RvSrYAs5taykYeSMqGHUHlnbeSd bP8B+tCV+dQ9cHXSGojSldzgeRuDkaY0OhYgV47c9oxEXNu9v14iOTGJJrxpE0XfenrBF2 RDvDV7GeKgq8bRZKkkmG6XojfeAq88sRfDZlhA6OQtw3wAtSAZfA2Yln/aAzLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736207196; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4aXBUtrrRaCB5K2VL1LcXRGNdeocDSJ0jRGgTG6Kw6g=; b=Tx2nx42bqFEgf1aQyQPAxZX2Kgp1Gu5hkOG/PEFs/ksgN6ySEXBwopipR2GqcEblLApbm2 H3UASulDdgjMYQMqiL/mvFdlZHbju+txShXq2dWQbs4dL0QuM+GAs70vYMgftHJ3RaS4gw FSJznKfTnLFcKrBvbAIJbj7Zps6G+zKDKtaB+TCaUlvK88tWjqQJb1O+lkz1Oo1OnOqrMz OB2QhBvuavNbzSB9ehP50T9ue4XJl4XgbOovOF6mqL5jxLzFm/QQ9zfOnG8uNBAOW8DOEN N9J3Nk7KM7ZxsgmamvxZVYKjSsX9Jp5bOuKMYVsnmxjX0/pVKhkFM1CNHUK+dA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1736207196; a=rsa-sha256; cv=none; b=ipklQ/ZOeh0AawdvJi9mFBdGBmOYyrFlWmB5ZAHYm7FjRotdxU/ec1+zk/+ZYRD1+SjrD3 CIYJuVtCVlqFNwT4amOGKU3/cbb7qyAMolKW0RVTS2aAN0o+DNGs53hNrHt6awTp74ILAh mui16tPs5lS3+rW4I8l5T7Iy0OBtDq6l6gHymAKSkC/d46ua3rVkyrb0ou1rDMqxW54hPu PEFAX2K8zspFWcwH3l3t9/ffosr6eGmRWcn58MftBYe8DUZP2NYlWPo39ZzBzha0F5+GvO hZx2L0/eVCxaYut511OUK3XSmAS2NV+//IH1o5qNo9IQggabyD9tKxROtl86fQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4YRrRv5jrnzmCv; Mon, 06 Jan 2025 23:46:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 506NkZiI045172; Mon, 6 Jan 2025 23:46:35 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 506NkZXQ045169; Mon, 6 Jan 2025 23:46:35 GMT (envelope-from git) Date: Mon, 6 Jan 2025 23:46:35 GMT Message-Id: <202501062346.506NkZXQ045169@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 02ebbc781f08 - main - swab: Fix implementation to support overlapping copies List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 02ebbc781f082df9714e74775700d8c08bac7850 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=02ebbc781f082df9714e74775700d8c08bac7850 commit 02ebbc781f082df9714e74775700d8c08bac7850 Author: Warner Losh AuthorDate: 2025-01-06 23:44:21 +0000 Commit: Warner Losh CommitDate: 2025-01-06 23:46:05 +0000 swab: Fix implementation to support overlapping copies A number of image processing packages assume that swab() can handle to and from being the same. However, POSIX.1 states that overlapping buffers produces undefined results. Our old implementation would produce coherent results, but the recent change to the musl-inspired code does not. Since there's complaints in the forums for these image processing packages for musl and now FreeBSD, update the algorithm to just read a word at a time and bswap16 the results. All FreeBSD's architecutres support unaligned access in userland, and swab is not used in the kernel (g_part_apm has its own copy), so opt for even simpler code that's easier to understand. This makes the overlapping behavior match i386 again, since its assembler routine for swab handles overlapping correctly. PR: 283698 Sponsored by: Netflix Reviewed by: nwhitehorn Differential Revision: https://reviews.freebsd.org/D48259 --- lib/libc/string/swab.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/libc/string/swab.c b/lib/libc/string/swab.c index 2b044d68ca46..ed4436a49810 100644 --- a/lib/libc/string/swab.c +++ b/lib/libc/string/swab.c @@ -4,19 +4,22 @@ */ #include +#include void swab(const void * __restrict from, void * __restrict to, ssize_t len) { - const unsigned char *f = from; - unsigned char *t = to; + const uint16_t *f __aligned(1) = from; + uint16_t *t __aligned(1) = to; + /* + * POSIX says overlapping copy behavior is undefined, however many + * applications assume the old FreeBSD and current GNU libc behavior + * that will swap the bytes correctly when from == to. Reading both bytes + * and swapping them before writing them back accomplishes this. + */ while (len > 1) { - t[0] = f[1]; - t[1] = f[0]; - - f += 2; - t += 2; + *t++ = bswap16(*f++); len -= 2; } }