Skip to content

Commit

Permalink
fix: Ensure ConfigSpace has stable layout
Browse files Browse the repository at this point in the history
Rust doesn't guarantee struct layout by default, so `repr(C)` or `repr(transparent)` are necessary for safety.

Signed-off-by: Kornel <kornel@cloudflare.com>
  • Loading branch information
kornelski committed Mar 20, 2024
1 parent f386cb2 commit 7701563
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/utils/src/net/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub const MAC_ADDR_LEN: u8 = 6;

/// Represents a MAC address
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(transparent)]
/// Representation of a MAC address.
pub struct MacAddr {
bytes: [u8; MAC_ADDR_LEN as usize],
Expand Down
3 changes: 2 additions & 1 deletion src/vmm/src/devices/virtio/net/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ fn init_vnet_hdr(buf: &mut [u8]) {
}

#[derive(Debug, Default, Clone, Copy)]
#[repr(C)]
pub struct ConfigSpace {
pub guest_mac: MacAddr,
}

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

/// VirtIO network device.
Expand Down

0 comments on commit 7701563

Please sign in to comment.