From 5d028abd1c63285904c1df758356997631dba850 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Fri, 2 Aug 2024 15:20:37 -0700 Subject: [PATCH] Ensure all packed structs are also repr(C) Using #[repr(packed)] alone does not guarantee that the struct fields will stay in the specified order, and as of a change in Rust 1.80, the compiler will actually reorder such structs in practice in some cases: Add "C" to all structs that were previously #[repr(packed)] alone, since these are all trying to match an externally-defined layout where order matters. None of these would get reordered in practice today, even with the Rust 1.80 change, but this ensures they will always stay consistent. Change-Id: I397fd0bd531a34e0f1726afb830bcd7fcc6a2f05 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5758933 Commit-Queue: Daniel Verkamp Reviewed-by: Frederick Mayle --- devices/src/pci/pci_configuration.rs | 2 +- .../src/virtio/vsock/sys/windows/protocol.rs | 2 +- third_party/vmm_vhost/src/message.rs | 22 +++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/devices/src/pci/pci_configuration.rs b/devices/src/pci/pci_configuration.rs index 411aafb72d8..4e8e0e9652c 100644 --- a/devices/src/pci/pci_configuration.rs +++ b/devices/src/pci/pci_configuration.rs @@ -1100,7 +1100,7 @@ mod tests { use super::*; - #[repr(packed)] + #[repr(C, packed)] #[derive(Clone, Copy, AsBytes)] #[allow(dead_code)] struct TestCap { diff --git a/devices/src/virtio/vsock/sys/windows/protocol.rs b/devices/src/virtio/vsock/sys/windows/protocol.rs index a495b97cf8f..e7d73b150ff 100644 --- a/devices/src/virtio/vsock/sys/windows/protocol.rs +++ b/devices/src/virtio/vsock/sys/windows/protocol.rs @@ -20,7 +20,7 @@ pub struct virtio_vsock_config { /// The message header for data packets sent on the tx/rx queues #[derive(Copy, Clone, Debug, Default, AsBytes, FromZeroes, FromBytes)] -#[repr(packed)] +#[repr(C, packed)] #[allow(non_camel_case_types)] pub struct virtio_vsock_hdr { pub src_cid: Le64, diff --git a/third_party/vmm_vhost/src/message.rs b/third_party/vmm_vhost/src/message.rs index 6da9fde63e9..ea09850643f 100644 --- a/third_party/vmm_vhost/src/message.rs +++ b/third_party/vmm_vhost/src/message.rs @@ -447,7 +447,7 @@ bitflags! { } /// A generic message to encapsulate a 64-bit value. -#[repr(packed)] +#[repr(C, packed)] #[derive(Default, Clone, Copy, AsBytes, FromZeroes, FromBytes)] pub struct VhostUserU64 { /// The encapsulated 64-bit common value. @@ -623,7 +623,7 @@ impl VhostUserMsgValidator for VhostUserSingleMemoryRegion { } /// Vring state descriptor. -#[repr(packed)] +#[repr(C, packed)] #[derive(Default, Clone, Copy, AsBytes, FromZeroes, FromBytes)] pub struct VhostUserVringState { /// Vring index. @@ -735,7 +735,7 @@ bitflags! { } /// Message to read/write device configuration space. -#[repr(packed)] +#[repr(C, packed)] #[derive(Default, Clone, Copy, AsBytes, FromZeroes, FromBytes)] pub struct VhostUserConfig { /// Offset of virtio device's configuration space. @@ -820,7 +820,7 @@ impl VhostUserMsgValidator for VhostUserInflight { /* * TODO: support dirty log, live migration and IOTLB operations. -#[repr(packed)] +#[repr(C, packed)] pub struct VhostUserVringArea { pub index: u32, pub flags: u32, @@ -828,13 +828,13 @@ pub struct VhostUserVringArea { pub offset: u64, } -#[repr(packed)] +#[repr(C, packed)] pub struct VhostUserLog { pub size: u64, pub offset: u64, } -#[repr(packed)] +#[repr(C, packed)] pub struct VhostUserIotlb { pub iova: u64, pub size: u64, @@ -1057,7 +1057,7 @@ impl VhostUserShmemUnmapMsg { } /// Inflight I/O descriptor state for split virtqueues -#[repr(packed)] +#[repr(C, packed)] #[derive(Clone, Copy, Default)] pub struct DescStateSplit { /// Indicate whether this descriptor (only head) is inflight or not. @@ -1078,7 +1078,7 @@ impl DescStateSplit { } /// Inflight I/O queue region for split virtqueues -#[repr(packed)] +#[repr(C, packed)] pub struct QueueRegionSplit { /// Features flags of this region pub features: u64, @@ -1109,7 +1109,7 @@ impl QueueRegionSplit { } /// Inflight I/O descriptor state for packed virtqueues -#[repr(packed)] +#[repr(C, packed)] #[derive(Clone, Copy, Default)] pub struct DescStatePacked { /// Indicate whether this descriptor (only head) is inflight or not. @@ -1142,7 +1142,7 @@ impl DescStatePacked { } /// Inflight I/O queue region for packed virtqueues -#[repr(packed)] +#[repr(C, packed)] pub struct QueueRegionPacked { /// Features flags of this region pub features: u64, @@ -1188,7 +1188,7 @@ impl QueueRegionPacked { } /// Virtio shared memory descriptor. -#[repr(packed)] +#[repr(C, packed)] #[derive(Default, Copy, Clone, FromZeroes, FromBytes, AsBytes)] pub struct VhostSharedMemoryRegion { /// The shared memory region's shmid.