Skip to content

Commit ecf5ebf

Browse files
kornelskiJamesC1305
authored andcommitted
fix: Ensure ConfigSpace has stable layout
Rust doesn't guarantee struct layout by default, so `repr(C)` or `repr(transparent)` are necessary for safety. Signed-off-by: Kornel <[email protected]>
1 parent 2c5776a commit ecf5ebf

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/utils/src/net/mac.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub const MAC_ADDR_LEN: u8 = 6;
2222

2323
/// Represents a MAC address
2424
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
25+
#[repr(transparent)]
2526
/// Representation of a MAC address.
2627
pub struct MacAddr {
2728
bytes: [u8; MAC_ADDR_LEN as usize],

src/vmm/src/devices/virtio/net/device.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ fn init_vnet_hdr(buf: &mut [u8]) {
9696
}
9797

9898
#[derive(Debug, Default, Clone, Copy)]
99+
#[repr(C)]
99100
pub struct ConfigSpace {
100101
pub guest_mac: MacAddr,
101102
}
102103

103-
// SAFETY: `ConfigSpace` contains only PODs.
104+
// SAFETY: `ConfigSpace` contains only PODs in `repr(C)` or `repr(transparent)`, without padding.
104105
unsafe impl ByteValued for ConfigSpace {}
105106

106107
/// VirtIO network device.

0 commit comments

Comments
 (0)