From nobody Thu Nov 6 01:12:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539109111477857.3165386025715; Tue, 9 Oct 2018 11:18:31 -0700 (PDT) Received: from localhost ([::1]:53306 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9waU-0000K4-5m for importer@patchew.org; Tue, 09 Oct 2018 14:18:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35064) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9wYM-0007Qp-A3 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 14:16:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9wYL-0000uq-Fj for qemu-devel@nongnu.org; Tue, 09 Oct 2018 14:16:18 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:51736) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9wYL-0000u0-7F for qemu-devel@nongnu.org; Tue, 09 Oct 2018 14:16:17 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1g9wYJ-00057s-AF; Tue, 09 Oct 2018 19:16:15 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 19:16:12 +0100 Message-Id: <20181009181612.10633-1-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH] coccinelle: new inplace-byteswaps.cocci to remove inplace-byteswapping calls X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Richard Henderson , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new Coccinelle script which replaces uses of the inplace byteswapping functions *_to_cpus() and cpu_to_*s() with their not-in-place equivalents. This is useful for where the swapping is done on members of a packed struct -- taking the address of the member to pass it to an inplace function is undefined behaviour in C. Signed-off-by: Peter Maydell Reviewed-by: Eric Blake Reviewed-by: Richard Henderson --- Richard asked for a coccinelle script in the scripts/coccinelle directory, so here's a patch to add it. scripts/coccinelle/inplace-byteswaps.cocci | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 scripts/coccinelle/inplace-byteswaps.cocci diff --git a/scripts/coccinelle/inplace-byteswaps.cocci b/scripts/coccinell= e/inplace-byteswaps.cocci new file mode 100644 index 00000000000..a869a90cbfd --- /dev/null +++ b/scripts/coccinelle/inplace-byteswaps.cocci @@ -0,0 +1,65 @@ +// Replace uses of in-place byteswapping functions with calls to the +// equivalent not-in-place functions. This is necessary to avoid +// undefined behaviour if the expression being swapped is a field in a +// packed struct. + +@@ +expression E; +@@ +-be16_to_cpus(&E); ++E =3D be16_to_cpu(E); +@@ +expression E; +@@ +-be32_to_cpus(&E); ++E =3D be32_to_cpu(E); +@@ +expression E; +@@ +-be64_to_cpus(&E); ++E =3D be64_to_cpu(E); +@@ +expression E; +@@ +-cpu_to_be16s(&E); ++E =3D cpu_to_be16(E); +@@ +expression E; +@@ +-cpu_to_be32s(&E); ++E =3D cpu_to_be32(E); +@@ +expression E; +@@ +-cpu_to_be64s(&E); ++E =3D cpu_to_be64(E); +@@ +expression E; +@@ +-le16_to_cpus(&E); ++E =3D le16_to_cpu(E); +@@ +expression E; +@@ +-le32_to_cpus(&E); ++E =3D le32_to_cpu(E); +@@ +expression E; +@@ +-le64_to_cpus(&E); ++E =3D le64_to_cpu(E); +@@ +expression E; +@@ +-cpu_to_le16s(&E); ++E =3D cpu_to_le16(E); +@@ +expression E; +@@ +-cpu_to_le32s(&E); ++E =3D cpu_to_le32(E); +@@ +expression E; +@@ +-cpu_to_le64s(&E); ++E =3D cpu_to_le64(E); --=20 2.19.0