Skip to content

Commit 207f382

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
[Test code] Apply bit masks to allow some wrapping
In these cases, values are intentionally truncated, either to split them into multiple values or to ensure that user-supplied data does not cause Firecracker to panic with a try_from/unwrap failure. Clippy will allow us to use "as" when we use bit masks to limit the size of an explicitly-sized value (u16, u32, u64). It doesn't work on usize values or on some more complicated expressions. Signed-off-by: Jonathan Browne <[email protected]>
1 parent 9bc019d commit 207f382

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

src/vmm/src/devices/bus.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ pub struct ConstantDevice;
9090
impl ConstantDevice {
9191
pub fn bus_read(&mut self, offset: u64, data: &mut [u8]) {
9292
for (i, v) in data.iter_mut().enumerate() {
93-
*v = (offset as u8) + (i as u8);
93+
*v = ((offset + i as u64) & 0xff) as u8;
9494
}
9595
}
9696

9797
fn bus_write(&mut self, offset: u64, data: &[u8]) {
9898
for (i, v) in data.iter().enumerate() {
99-
assert_eq!(*v, (offset as u8) + (i as u8))
99+
assert_eq!(*v, ((offset + i as u64) & 0xff) as u8)
100100
}
101101
}
102102
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,10 @@ pub(crate) mod tests {
750750
| (u64::from(*deflate_on_oom) << VIRTIO_BALLOON_F_DEFLATE_ON_OOM)
751751
| ((u64::from(*stats_interval)) << VIRTIO_BALLOON_F_STATS_VQ);
752752

753-
assert_eq!(balloon.avail_features_by_page(0), features as u32);
753+
assert_eq!(
754+
balloon.avail_features_by_page(0),
755+
(features & 0xFFFFFFFF) as u32
756+
);
754757
assert_eq!(balloon.avail_features_by_page(1), (features >> 32) as u32);
755758
for i in 2..10 {
756759
assert_eq!(balloon.avail_features_by_page(i), 0u32);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ mod tests {
685685
let cfg = disk_properties.virtio_block_config_space();
686686
assert_eq!(cfg.len(), BLOCK_CONFIG_SPACE_SIZE);
687687
for (i, byte) in cfg.iter().enumerate() {
688-
assert_eq!(*byte, (num_sectors >> (8 * i)) as u8);
688+
assert_eq!(*byte, ((num_sectors >> (8 * i)) & 0xff) as u8);
689689
}
690690
// Testing `backing_file.virtio_block_disk_image_id()` implies
691691
// duplicating that logic in tests, so skipping it.
@@ -707,7 +707,10 @@ mod tests {
707707

708708
let features: u64 = (1u64 << VIRTIO_F_VERSION_1) | (1u64 << VIRTIO_RING_F_EVENT_IDX);
709709

710-
assert_eq!(block.avail_features_by_page(0), features as u32);
710+
assert_eq!(
711+
block.avail_features_by_page(0),
712+
(features & 0xffffffff) as u32,
713+
);
711714
assert_eq!(block.avail_features_by_page(1), (features >> 32) as u32);
712715

713716
for i in 2..10 {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,10 @@ pub mod tests {
959959
| 1 << VIRTIO_F_VERSION_1
960960
| 1 << VIRTIO_RING_F_EVENT_IDX;
961961

962-
assert_eq!(net.avail_features_by_page(0), features as u32);
962+
assert_eq!(
963+
net.avail_features_by_page(0),
964+
(features & 0xFFFFFFFF) as u32,
965+
);
963966
assert_eq!(net.avail_features_by_page(1), (features >> 32) as u32);
964967
for i in 2..10 {
965968
assert_eq!(net.avail_features_by_page(i), 0u32);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,10 @@ mod tests {
391391

392392
let features = 1 << VIRTIO_F_VERSION_1;
393393

394-
assert_eq!(entropy_dev.avail_features_by_page(0), features as u32);
394+
assert_eq!(
395+
entropy_dev.avail_features_by_page(0),
396+
(features & 0xFFFFFFFF) as u32,
397+
);
395398
assert_eq!(
396399
entropy_dev.avail_features_by_page(1),
397400
(features >> 32) as u32

src/vmm/src/devices/virtio/vsock/packet.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,9 @@ mod tests {
707707
assert_eq!(pkt.buf_addr.unwrap().raw_value(), buf_desc.addr.get());
708708

709709
let zeros = vec![0_u8; pkt.buf_size()];
710-
let data: Vec<u8> = (0..pkt.buf_size()).map(|i| (i % 0x100) as u8).collect();
710+
let data: Vec<u8> = (0..pkt.buf_size())
711+
.map(|i| ((i as u64) & 0xff) as u8)
712+
.collect();
711713
for offset in 0..pkt.buf_size() {
712714
buf_desc.set_data(&zeros);
713715

0 commit comments

Comments
 (0)