Skip to content

Commit 5d028ab

Browse files
danielverkampcrosvm LUCI
authored and
crosvm LUCI
committed
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: <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 <[email protected]> Reviewed-by: Frederick Mayle <[email protected]>
1 parent 34ed5bc commit 5d028ab

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

devices/src/pci/pci_configuration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ mod tests {
11001100

11011101
use super::*;
11021102

1103-
#[repr(packed)]
1103+
#[repr(C, packed)]
11041104
#[derive(Clone, Copy, AsBytes)]
11051105
#[allow(dead_code)]
11061106
struct TestCap {

devices/src/virtio/vsock/sys/windows/protocol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct virtio_vsock_config {
2020

2121
/// The message header for data packets sent on the tx/rx queues
2222
#[derive(Copy, Clone, Debug, Default, AsBytes, FromZeroes, FromBytes)]
23-
#[repr(packed)]
23+
#[repr(C, packed)]
2424
#[allow(non_camel_case_types)]
2525
pub struct virtio_vsock_hdr {
2626
pub src_cid: Le64,

third_party/vmm_vhost/src/message.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ bitflags! {
447447
}
448448

449449
/// A generic message to encapsulate a 64-bit value.
450-
#[repr(packed)]
450+
#[repr(C, packed)]
451451
#[derive(Default, Clone, Copy, AsBytes, FromZeroes, FromBytes)]
452452
pub struct VhostUserU64 {
453453
/// The encapsulated 64-bit common value.
@@ -623,7 +623,7 @@ impl VhostUserMsgValidator for VhostUserSingleMemoryRegion {
623623
}
624624

625625
/// Vring state descriptor.
626-
#[repr(packed)]
626+
#[repr(C, packed)]
627627
#[derive(Default, Clone, Copy, AsBytes, FromZeroes, FromBytes)]
628628
pub struct VhostUserVringState {
629629
/// Vring index.
@@ -735,7 +735,7 @@ bitflags! {
735735
}
736736

737737
/// Message to read/write device configuration space.
738-
#[repr(packed)]
738+
#[repr(C, packed)]
739739
#[derive(Default, Clone, Copy, AsBytes, FromZeroes, FromBytes)]
740740
pub struct VhostUserConfig {
741741
/// Offset of virtio device's configuration space.
@@ -820,21 +820,21 @@ impl VhostUserMsgValidator for VhostUserInflight {
820820

821821
/*
822822
* TODO: support dirty log, live migration and IOTLB operations.
823-
#[repr(packed)]
823+
#[repr(C, packed)]
824824
pub struct VhostUserVringArea {
825825
pub index: u32,
826826
pub flags: u32,
827827
pub size: u64,
828828
pub offset: u64,
829829
}
830830
831-
#[repr(packed)]
831+
#[repr(C, packed)]
832832
pub struct VhostUserLog {
833833
pub size: u64,
834834
pub offset: u64,
835835
}
836836
837-
#[repr(packed)]
837+
#[repr(C, packed)]
838838
pub struct VhostUserIotlb {
839839
pub iova: u64,
840840
pub size: u64,
@@ -1057,7 +1057,7 @@ impl VhostUserShmemUnmapMsg {
10571057
}
10581058

10591059
/// Inflight I/O descriptor state for split virtqueues
1060-
#[repr(packed)]
1060+
#[repr(C, packed)]
10611061
#[derive(Clone, Copy, Default)]
10621062
pub struct DescStateSplit {
10631063
/// Indicate whether this descriptor (only head) is inflight or not.
@@ -1078,7 +1078,7 @@ impl DescStateSplit {
10781078
}
10791079

10801080
/// Inflight I/O queue region for split virtqueues
1081-
#[repr(packed)]
1081+
#[repr(C, packed)]
10821082
pub struct QueueRegionSplit {
10831083
/// Features flags of this region
10841084
pub features: u64,
@@ -1109,7 +1109,7 @@ impl QueueRegionSplit {
11091109
}
11101110

11111111
/// Inflight I/O descriptor state for packed virtqueues
1112-
#[repr(packed)]
1112+
#[repr(C, packed)]
11131113
#[derive(Clone, Copy, Default)]
11141114
pub struct DescStatePacked {
11151115
/// Indicate whether this descriptor (only head) is inflight or not.
@@ -1142,7 +1142,7 @@ impl DescStatePacked {
11421142
}
11431143

11441144
/// Inflight I/O queue region for packed virtqueues
1145-
#[repr(packed)]
1145+
#[repr(C, packed)]
11461146
pub struct QueueRegionPacked {
11471147
/// Features flags of this region
11481148
pub features: u64,
@@ -1188,7 +1188,7 @@ impl QueueRegionPacked {
11881188
}
11891189

11901190
/// Virtio shared memory descriptor.
1191-
#[repr(packed)]
1191+
#[repr(C, packed)]
11921192
#[derive(Default, Copy, Clone, FromZeroes, FromBytes, AsBytes)]
11931193
pub struct VhostSharedMemoryRegion {
11941194
/// The shared memory region's shmid.

0 commit comments

Comments
 (0)