From nobody Thu May 2 10:39:25 2024 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 1537892725173744.5077569241228; Tue, 25 Sep 2018 09:25:25 -0700 (PDT) Received: from localhost ([::1]:54243 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g4q9L-0007NK-FU for importer@patchew.org; Tue, 25 Sep 2018 12:25:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43624) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g4q3f-0002JU-Mj for qemu-devel@nongnu.org; Tue, 25 Sep 2018 12:19:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g4q3e-0006jV-Ft for qemu-devel@nongnu.org; Tue, 25 Sep 2018 12:19:31 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:48622) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g4q3e-0006gA-3I for qemu-devel@nongnu.org; Tue, 25 Sep 2018 12:19:30 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1g4q3a-0001Hy-Lo; Tue, 25 Sep 2018 17:19:26 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 25 Sep 2018 17:19:24 +0100 Message-Id: <20180925161924.7832-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] migration/ram.c: Avoid taking address of fields in packed MultiFDInit_t struct 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: Juan Quintela , "Dr. David Alan Gilbert" , 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" Taking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this: migration/ram.c:651:19: warning: taking address of packed member 'magic' of= class or structure 'MultiFDInit_t' may result in an unaligned pointer valu= e [-Waddress-of-packed-member] migration/ram.c:652:19: warning: taking address of packed member 'version' = of class or structure 'MultiFDInit_t' may result in an unaligned pointer va= lue [-Waddress-of-packed-member] migration/ram.c:737:19: warning: taking address of packed member 'magic' of= class or structure 'MultiFDPacket_t' may result in an unaligned pointer va= lue [-Waddress-of-packed-member] migration/ram.c:745:19: warning: taking address of packed member 'version' = of class or structure 'MultiFDPacket_t' may result in an unaligned pointer = value [-Waddress-of-packed-member] migration/ram.c:755:19: warning: taking address of packed member 'size' of = class or structure 'MultiFDPacket_t' may result in an unaligned pointer val= ue [-Waddress-of-packed-member] Avoid the bug by not using the "modify in place" byteswapping functions. Signed-off-by: Peter Maydell Reviewed-by: Marc-Andr=C3=A9 Lureau --- Now I have a machine with the version of clang that emits these warnings I'm starting to care a little more about them :-) --- migration/ram.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index f6fd8e5e096..4fc3bc36816 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -648,8 +648,8 @@ static int multifd_recv_initial_packet(QIOChannel *c, E= rror **errp) return -1; } =20 - be32_to_cpus(&msg.magic); - be32_to_cpus(&msg.version); + msg.magic =3D be32_to_cpu(msg.magic); + msg.version =3D be32_to_cpu(msg.version); =20 if (msg.magic !=3D MULTIFD_MAGIC) { error_setg(errp, "multifd: received packet magic %x " @@ -734,7 +734,7 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams= *p, Error **errp) RAMBlock *block; int i; =20 - be32_to_cpus(&packet->magic); + packet->magic =3D be32_to_cpu(packet->magic); if (packet->magic !=3D MULTIFD_MAGIC) { error_setg(errp, "multifd: received packet " "magic %x and expected magic %x", @@ -742,7 +742,7 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams= *p, Error **errp) return -1; } =20 - be32_to_cpus(&packet->version); + packet->version =3D be32_to_cpu(packet->version); if (packet->version !=3D MULTIFD_VERSION) { error_setg(errp, "multifd: received packet " "version %d and expected version %d", @@ -752,7 +752,7 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams= *p, Error **errp) =20 p->flags =3D be32_to_cpu(packet->flags); =20 - be32_to_cpus(&packet->size); + packet->size =3D be32_to_cpu(packet->size); if (packet->size > migrate_multifd_page_count()) { error_setg(errp, "multifd: received packet " "with size %d and expected maximum size %d", --=20 2.19.0