Skip to content

Commit

Permalink
Ensure all packed structs are also repr(C)
Browse files Browse the repository at this point in the history
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:
<rust-lang/rust#125360>

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 <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
  • Loading branch information
danielverkamp authored and crosvm LUCI committed Aug 5, 2024
1 parent 34ed5bc commit 5d028ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion devices/src/pci/pci_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ mod tests {

use super::*;

#[repr(packed)]
#[repr(C, packed)]
#[derive(Clone, Copy, AsBytes)]
#[allow(dead_code)]
struct TestCap {
Expand Down
2 changes: 1 addition & 1 deletion devices/src/virtio/vsock/sys/windows/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions third_party/vmm_vhost/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -820,21 +820,21 @@ 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,
pub size: u64,
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,
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5d028ab

Please sign in to comment.