[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 32/33] vhost-user: fix misaligned access to payload
From: |
Michael S. Tsirkin |
Subject: |
[Qemu-devel] [PULL 32/33] vhost-user: fix misaligned access to payload |
Date: |
Tue, 16 Jan 2018 06:48:27 +0200 |
We currently take a pointer to a misaligned field of a packed structure.
clang reports this as a build warning.
A fix is to keep payload in a separate structure, and access is it
from there using a vectored write.
Signed-off-by: Michael S. Tsirkin <address@hidden>
---
hw/virtio/vhost-user.c | 39 ++++++++++++++++++++++++---------------
1 file changed, 24 insertions(+), 15 deletions(-)
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
index 6ac3610..7930fd8 100644
--- a/hw/virtio/vhost-user.c
+++ b/hw/virtio/vhost-user.c
@@ -652,33 +652,34 @@ static void slave_read(void *opaque)
{
struct vhost_dev *dev = opaque;
struct vhost_user *u = dev->opaque;
- VhostUserMsg msg = { 0, };
+ VhostUserHeader hdr = { 0, };
+ VhostUserPayload payload = { 0, };
int size, ret = 0;
/* Read header */
- size = read(u->slave_fd, &msg, VHOST_USER_HDR_SIZE);
+ size = read(u->slave_fd, &hdr, VHOST_USER_HDR_SIZE);
if (size != VHOST_USER_HDR_SIZE) {
error_report("Failed to read from slave.");
goto err;
}
- if (msg.hdr.size > VHOST_USER_PAYLOAD_SIZE) {
+ if (hdr.size > VHOST_USER_PAYLOAD_SIZE) {
error_report("Failed to read msg header."
- " Size %d exceeds the maximum %zu.", msg.hdr.size,
+ " Size %d exceeds the maximum %zu.", hdr.size,
VHOST_USER_PAYLOAD_SIZE);
goto err;
}
/* Read payload */
- size = read(u->slave_fd, &msg.payload, msg.hdr.size);
- if (size != msg.hdr.size) {
+ size = read(u->slave_fd, &payload, hdr.size);
+ if (size != hdr.size) {
error_report("Failed to read payload from slave.");
goto err;
}
- switch (msg.hdr.request) {
+ switch (hdr.request) {
case VHOST_USER_SLAVE_IOTLB_MSG:
- ret = vhost_backend_handle_iotlb_msg(dev, &msg.payload.iotlb);
+ ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb);
break;
case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG :
ret = vhost_user_slave_handle_config_change(dev);
@@ -692,15 +693,23 @@ static void slave_read(void *opaque)
* REPLY_ACK feature handling. Other reply types has to be managed
* directly in their request handlers.
*/
- if (msg.hdr.flags & VHOST_USER_NEED_REPLY_MASK) {
- msg.hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
- msg.hdr.flags |= VHOST_USER_REPLY_MASK;
+ if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) {
+ struct iovec iovec[2];
- msg.payload.u64 = !!ret;
- msg.hdr.size = sizeof(msg.payload.u64);
- size = write(u->slave_fd, &msg, VHOST_USER_HDR_SIZE + msg.hdr.size);
- if (size != VHOST_USER_HDR_SIZE + msg.hdr.size) {
+ hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
+ hdr.flags |= VHOST_USER_REPLY_MASK;
+
+ payload.u64 = !!ret;
+ hdr.size = sizeof(payload.u64);
+
+ iovec[0].iov_base = &hdr;
+ iovec[0].iov_len = VHOST_USER_HDR_SIZE;
+ iovec[1].iov_base = &payload;
+ iovec[1].iov_len = hdr.size;
+
+ size = writev(u->slave_fd, iovec, ARRAY_SIZE(iovec));
+ if (size != VHOST_USER_HDR_SIZE + hdr.size) {
error_report("Failed to send msg reply to slave.");
goto err;
}
--
MST
- Re: [Qemu-devel] [PULL 22/33] vhost: Move log_dirty check, (continued)
- [Qemu-devel] [PULL 24/33] vhost: Merge sections added to temporary list, Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 23/33] vhost: Simplify ring verification checks, Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 26/33] x86_iommu: check if machine has PCI bus, Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 28/33] tests: acpi: init table descriptor in test_dst_table(), Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 29/33] tests: acpi: rename test_acpi_tables()/test_dst_table() to reflect its usage, Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 27/33] tests: acpi: move tested tables array allocation outside of test_acpi_dsdt_table(), Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 30/33] tests: acpi: add comments to fetch_rsdt_referenced_tables/data->tables usage, Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 25/33] x86_iommu: Move machine check to x86_iommu_realize(), Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 31/33] vhost-user: factor out msg head and payload, Michael S. Tsirkin, 2018/01/15
- [Qemu-devel] [PULL 32/33] vhost-user: fix misaligned access to payload,
Michael S. Tsirkin <=
- [Qemu-devel] [PULL 33/33] vhost: remove assertion to prevent crash, Michael S. Tsirkin, 2018/01/15
- Re: [Qemu-devel] [PULL 00/33] pc, pci, virtio: features, fixes, cleanups, Peter Maydell, 2018/01/16
- Re: [Qemu-devel] [PULL 00/33] pc, pci, virtio: features, fixes, cleanups, Peter Maydell, 2018/01/16
- Re: [Qemu-devel] [PULL 00/33] pc, pci, virtio: features, fixes, cleanups, Dr. David Alan Gilbert, 2018/01/16