From nobody Mon May 6 05:33:23 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 1544441447318209.2407022791216; Mon, 10 Dec 2018 03:30:47 -0800 (PST) Received: from localhost ([::1]:60284 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWJls-00021M-AB for importer@patchew.org; Mon, 10 Dec 2018 06:30:44 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54727) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWJiQ-0007Om-TU for qemu-devel@nongnu.org; Mon, 10 Dec 2018 06:27:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWJiM-00066w-6g for qemu-devel@nongnu.org; Mon, 10 Dec 2018 06:27:10 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53332) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gWJiL-0005ya-U5; Mon, 10 Dec 2018 06:27:06 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gWJi7-0008CU-1J; Mon, 10 Dec 2018 11:26:51 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 10 Dec 2018 11:26:47 +0000 Message-Id: <20181210112649.11581-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181210112649.11581-1-peter.maydell@linaro.org> References: <20181210112649.11581-1-peter.maydell@linaro.org> 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 1/3] block/vpc: Don't take address of fields in packed structs 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: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Ben Warren , "Michael S. Tsirkin" , Stefan Weil , patches@linaro.org, Max Reitz , Igor Mammedov Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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. Avoid the bug by generating the UUID into a local variable which is definitely safely aligned and then copying it into place. Signed-off-by: Peter Maydell --- block/vpc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/block/vpc.c b/block/vpc.c index 80c5b2b197e..968d80ae461 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -979,6 +979,7 @@ static int coroutine_fn vpc_co_create(BlockdevCreateOpt= ions *opts, int64_t total_size; int disk_type; int ret =3D -EIO; + QemuUUID uuid; =20 assert(opts->driver =3D=3D BLOCKDEV_DRIVER_VPC); vpc_opts =3D &opts->u.vpc; @@ -1062,7 +1063,8 @@ static int coroutine_fn vpc_co_create(BlockdevCreateO= ptions *opts, =20 footer->type =3D cpu_to_be32(disk_type); =20 - qemu_uuid_generate(&footer->uuid); + qemu_uuid_generate(&uuid); + footer->uuid =3D uuid; =20 footer->checksum =3D cpu_to_be32(vpc_checksum(buf, HEADER_SIZE)); =20 --=20 2.19.2 From nobody Mon May 6 05:33:23 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1544441338827814.2953650677058; Mon, 10 Dec 2018 03:28:58 -0800 (PST) Received: from localhost ([::1]:60266 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWJjx-0000Sk-IH for importer@patchew.org; Mon, 10 Dec 2018 06:28:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54634) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWJiK-0007KN-4I for qemu-devel@nongnu.org; Mon, 10 Dec 2018 06:27:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWJiJ-00064c-44 for qemu-devel@nongnu.org; Mon, 10 Dec 2018 06:27:04 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53332) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gWJiI-0005ya-SE; Mon, 10 Dec 2018 06:27:03 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gWJi7-0008Ch-IE; Mon, 10 Dec 2018 11:26:51 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 10 Dec 2018 11:26:48 +0000 Message-Id: <20181210112649.11581-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181210112649.11581-1-peter.maydell@linaro.org> References: <20181210112649.11581-1-peter.maydell@linaro.org> 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 2/3] block/vdi: Don't take address of fields in packed structs 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: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Ben Warren , "Michael S. Tsirkin" , Stefan Weil , patches@linaro.org, Max Reitz , Igor Mammedov Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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. Instead of passing UUID related functions the address of a possibly unaligned QemuUUID struct, use local variables and then copy to/from the struct field as appropriate. Signed-off-by: Peter Maydell --- block/vdi.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index 2380daa583e..4cc726047c3 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -235,7 +235,8 @@ static void vdi_header_to_le(VdiHeader *header) =20 static void vdi_header_print(VdiHeader *header) { - char uuid[37]; + char uuidstr[37]; + QemuUUID uuid; logout("text %s", header->text); logout("signature 0x%08x\n", header->signature); logout("header size 0x%04x\n", header->header_size); @@ -254,14 +255,18 @@ static void vdi_header_print(VdiHeader *header) logout("block extra 0x%04x\n", header->block_extra); logout("blocks tot. 0x%04x\n", header->blocks_in_image); logout("blocks all. 0x%04x\n", header->blocks_allocated); - qemu_uuid_unparse(&header->uuid_image, uuid); - logout("uuid image %s\n", uuid); - qemu_uuid_unparse(&header->uuid_last_snap, uuid); - logout("uuid snap %s\n", uuid); - qemu_uuid_unparse(&header->uuid_link, uuid); - logout("uuid link %s\n", uuid); - qemu_uuid_unparse(&header->uuid_parent, uuid); - logout("uuid parent %s\n", uuid); + uuid =3D header->uuid_image; + qemu_uuid_unparse(&uuid, uuidstr); + logout("uuid image %s\n", uuidstr); + uuid =3D header->uuid_last_snap; + qemu_uuid_unparse(&uuid, uuidstr); + logout("uuid snap %s\n", uuidstr); + uuid =3D header->uuid_link; + qemu_uuid_unparse(&uuid, uuidstr); + logout("uuid link %s\n", uuidstr); + uuid =3D header->uuid_parent; + qemu_uuid_unparse(&uuid, uuidstr); + logout("uuid parent %s\n", uuidstr); } =20 static int coroutine_fn vdi_co_check(BlockDriverState *bs, BdrvCheckResult= *res, @@ -368,6 +373,7 @@ static int vdi_open(BlockDriverState *bs, QDict *option= s, int flags, size_t bmap_size; int ret; Error *local_err =3D NULL; + QemuUUID uuid_link, uuid_parent; =20 bs->file =3D bdrv_open_child(NULL, options, "file", bs, &child_file, false, errp); @@ -395,6 +401,9 @@ static int vdi_open(BlockDriverState *bs, QDict *option= s, int flags, goto fail; } =20 + uuid_link =3D header.uuid_link; + uuid_parent =3D header.uuid_parent; + if (header.disk_size % SECTOR_SIZE !=3D 0) { /* 'VBoxManage convertfromraw' can create images with odd disk siz= es. We accept them but round the disk size to the next multiple of @@ -444,11 +453,11 @@ static int vdi_open(BlockDriverState *bs, QDict *opti= ons, int flags, (uint64_t)header.blocks_in_image * header.block_size); ret =3D -ENOTSUP; goto fail; - } else if (!qemu_uuid_is_null(&header.uuid_link)) { + } else if (!qemu_uuid_is_null(&uuid_link)) { error_setg(errp, "unsupported VDI image (non-NULL link UUID)"); ret =3D -ENOTSUP; goto fail; - } else if (!qemu_uuid_is_null(&header.uuid_parent)) { + } else if (!qemu_uuid_is_null(&uuid_parent)) { error_setg(errp, "unsupported VDI image (non-NULL parent UUID)"); ret =3D -ENOTSUP; goto fail; @@ -733,6 +742,7 @@ static int coroutine_fn vdi_co_do_create(BlockdevCreate= Options *create_options, BlockDriverState *bs_file =3D NULL; BlockBackend *blk =3D NULL; uint32_t *bmap =3D NULL; + QemuUUID uuid; =20 assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_VDI); vdi_opts =3D &create_options->u.vdi; @@ -819,8 +829,10 @@ static int coroutine_fn vdi_co_do_create(BlockdevCreat= eOptions *create_options, if (image_type =3D=3D VDI_TYPE_STATIC) { header.blocks_allocated =3D blocks; } - qemu_uuid_generate(&header.uuid_image); - qemu_uuid_generate(&header.uuid_last_snap); + qemu_uuid_generate(&uuid); + header.uuid_image =3D uuid; + qemu_uuid_generate(&uuid); + header.uuid_last_snap =3D uuid; /* There is no need to set header.uuid_link or header.uuid_parent here= . */ if (VDI_DEBUG) { vdi_header_print(&header); --=20 2.19.2 From nobody Mon May 6 05:33:23 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1544441341219352.3895250879409; Mon, 10 Dec 2018 03:29:01 -0800 (PST) Received: from localhost ([::1]:60268 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWJjz-0000Uf-WB for importer@patchew.org; Mon, 10 Dec 2018 06:28:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54645) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWJiL-0007KS-4I for qemu-devel@nongnu.org; Mon, 10 Dec 2018 06:27:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWJiK-00065S-3U for qemu-devel@nongnu.org; Mon, 10 Dec 2018 06:27:05 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53332) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gWJiJ-0005ya-Qe; Mon, 10 Dec 2018 06:27:04 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gWJi8-0008D0-5Y; Mon, 10 Dec 2018 11:26:52 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 10 Dec 2018 11:26:49 +0000 Message-Id: <20181210112649.11581-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181210112649.11581-1-peter.maydell@linaro.org> References: <20181210112649.11581-1-peter.maydell@linaro.org> 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 3/3] uuid: Make qemu_uuid_bswap() take and return a QemuUUID 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: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Ben Warren , "Michael S. Tsirkin" , Stefan Weil , patches@linaro.org, Max Reitz , Igor Mammedov Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Currently qemu_uuid_bswap() takes a pointer to the QemuUUID to be byte-swapped. This means it can't be used when the UUID to be swapped is in a packed member of a struct. It's also out of line with the general bswap*() functions we provide in bswap.h, which take the value to be swapped and return it. Make qemu_uuid_bswap() take a QemuUUID and return the swapped version. This fixes some clang warnings about taking the address of a packed struct member in block/vdi.c. Signed-off-by: Peter Maydell Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Michael S. Tsirkin --- include/qemu/uuid.h | 2 +- block/vdi.c | 16 ++++++++-------- hw/acpi/vmgenid.c | 6 ++---- tests/vmgenid-test.c | 2 +- util/uuid.c | 10 +++++----- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/include/qemu/uuid.h b/include/qemu/uuid.h index 09489ce5c5e..037357d990b 100644 --- a/include/qemu/uuid.h +++ b/include/qemu/uuid.h @@ -56,6 +56,6 @@ char *qemu_uuid_unparse_strdup(const QemuUUID *uuid); =20 int qemu_uuid_parse(const char *str, QemuUUID *uuid); =20 -void qemu_uuid_bswap(QemuUUID *uuid); +QemuUUID qemu_uuid_bswap(QemuUUID uuid); =20 #endif diff --git a/block/vdi.c b/block/vdi.c index 4cc726047c3..0c34f6bae46 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -203,10 +203,10 @@ static void vdi_header_to_cpu(VdiHeader *header) header->block_extra =3D le32_to_cpu(header->block_extra); header->blocks_in_image =3D le32_to_cpu(header->blocks_in_image); header->blocks_allocated =3D le32_to_cpu(header->blocks_allocated); - qemu_uuid_bswap(&header->uuid_image); - qemu_uuid_bswap(&header->uuid_last_snap); - qemu_uuid_bswap(&header->uuid_link); - qemu_uuid_bswap(&header->uuid_parent); + header->uuid_image =3D qemu_uuid_bswap(header->uuid_image); + header->uuid_last_snap =3D qemu_uuid_bswap(header->uuid_last_snap); + header->uuid_link =3D qemu_uuid_bswap(header->uuid_link); + header->uuid_parent =3D qemu_uuid_bswap(header->uuid_parent); } =20 static void vdi_header_to_le(VdiHeader *header) @@ -227,10 +227,10 @@ static void vdi_header_to_le(VdiHeader *header) header->block_extra =3D cpu_to_le32(header->block_extra); header->blocks_in_image =3D cpu_to_le32(header->blocks_in_image); header->blocks_allocated =3D cpu_to_le32(header->blocks_allocated); - qemu_uuid_bswap(&header->uuid_image); - qemu_uuid_bswap(&header->uuid_last_snap); - qemu_uuid_bswap(&header->uuid_link); - qemu_uuid_bswap(&header->uuid_parent); + header->uuid_image =3D qemu_uuid_bswap(header->uuid_image); + header->uuid_last_snap =3D qemu_uuid_bswap(header->uuid_last_snap); + header->uuid_link =3D qemu_uuid_bswap(header->uuid_link); + header->uuid_parent =3D qemu_uuid_bswap(header->uuid_parent); } =20 static void vdi_header_print(VdiHeader *header) diff --git a/hw/acpi/vmgenid.c b/hw/acpi/vmgenid.c index d78b579a201..02717a8b0dc 100644 --- a/hw/acpi/vmgenid.c +++ b/hw/acpi/vmgenid.c @@ -30,8 +30,7 @@ void vmgenid_build_acpi(VmGenIdState *vms, GArray *table_= data, GArray *guid, * first, since that's what the guest expects */ g_array_set_size(guid, VMGENID_FW_CFG_SIZE - ARRAY_SIZE(guid_le.data)); - guid_le =3D vms->guid; - qemu_uuid_bswap(&guid_le); + guid_le =3D qemu_uuid_bswap(vms->guid); /* The GUID is written at a fixed offset into the fw_cfg file * in order to implement the "OVMF SDT Header probe suppressor" * see docs/specs/vmgenid.txt for more details @@ -149,8 +148,7 @@ static void vmgenid_update_guest(VmGenIdState *vms) * however, will expect the fields to be little-endian. * Perform a byte swap immediately before writing. */ - guid_le =3D vms->guid; - qemu_uuid_bswap(&guid_le); + guid_le =3D qemu_uuid_bswap(vms->guid); /* The GUID is written at a fixed offset into the fw_cfg file * in order to implement the "OVMF SDT Header probe suppressor" * see docs/specs/vmgenid.txt for more details. diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c index 0a6fb55f2eb..98db43f5a65 100644 --- a/tests/vmgenid-test.c +++ b/tests/vmgenid-test.c @@ -110,7 +110,7 @@ static void read_guid_from_memory(QemuUUID *guid) /* The GUID is in little-endian format in the guest, while QEMU * uses big-endian. Swap after reading. */ - qemu_uuid_bswap(guid); + *guid =3D qemu_uuid_bswap(*guid); } =20 static void read_guid_from_monitor(QemuUUID *guid) diff --git a/util/uuid.c b/util/uuid.c index ebf06c049ad..5787f0978c1 100644 --- a/util/uuid.c +++ b/util/uuid.c @@ -110,10 +110,10 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid) =20 /* Swap from UUID format endian (BE) to the opposite or vice versa. */ -void qemu_uuid_bswap(QemuUUID *uuid) +QemuUUID qemu_uuid_bswap(QemuUUID uuid) { - assert(QEMU_PTR_IS_ALIGNED(uuid, sizeof(uint32_t))); - bswap32s(&uuid->fields.time_low); - bswap16s(&uuid->fields.time_mid); - bswap16s(&uuid->fields.time_high_and_version); + bswap32s(&uuid.fields.time_low); + bswap16s(&uuid.fields.time_mid); + bswap16s(&uuid.fields.time_high_and_version); + return uuid; } --=20 2.19.2