Skip to content

Commit af83175

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
[Test code] Add try_from()/unwrap() for test code
Some places in the code don't allow us to re-type values to let the compiler verify that those conversions are safe, particularly with length values. Since these are for test code, I don't expect overflows to happen, so I've just inserted unwraps here. Signed-off-by: Jonathan Browne <[email protected]>
1 parent d6ea377 commit af83175

File tree

15 files changed

+79
-46
lines changed

15 files changed

+79
-46
lines changed

src/seccompiler/src/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ mod tests {
917917
assert_eq!(rc, 0);
918918
}
919919
let bpf_prog = sock_fprog {
920-
len: bpf_filter.len() as u16,
920+
len: u16::try_from(bpf_filter.len()).unwrap(),
921921
filter: bpf_filter.as_ptr(),
922922
};
923923
let bpf_prog_ptr = &bpf_prog as *const sock_fprog;

src/vmm/src/arch/x86_64/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ mod tests {
292292

293293
// Exercise the scenario where the field storing the length of the e820 entry table is
294294
// is bigger than the allocated memory.
295-
params.e820_entries = params.e820_table.len() as u8 + 1;
295+
params.e820_entries = u8::try_from(params.e820_table.len()).unwrap() + 1;
296296
assert!(add_e820_entry(
297297
&mut params,
298298
e820_map[0].addr,

src/vmm/src/devices/legacy/i8042.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ mod tests {
412412

413413
// Test buffer full.
414414
for i in 0..BUF_SIZE {
415-
i8042.push_byte(i as u8).unwrap();
415+
i8042.push_byte(i.try_into().unwrap()).unwrap();
416416
assert_eq!(i8042.buf_len(), i + 1);
417417
}
418418
assert_eq!(

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ pub(crate) mod tests {
853853
&infq,
854854
0,
855855
page_addr,
856-
SIZE_OF_U32 as u32,
856+
SIZE_OF_U32.try_into().unwrap(),
857857
VIRTQ_DESC_F_NEXT | VIRTQ_DESC_F_WRITE,
858858
);
859859

@@ -873,7 +873,7 @@ pub(crate) mod tests {
873873
&infq,
874874
1,
875875
page_addr,
876-
SIZE_OF_U32 as u32 + 1,
876+
u32::try_from(SIZE_OF_U32).unwrap() + 1,
877877
VIRTQ_DESC_F_NEXT,
878878
);
879879

@@ -908,7 +908,13 @@ pub(crate) mod tests {
908908
// to trigger the inflate event queue.
909909
{
910910
mem.write_obj::<u32>(0x1, GuestAddress(page_addr)).unwrap();
911-
set_request(&infq, 0, page_addr, SIZE_OF_U32 as u32, VIRTQ_DESC_F_NEXT);
911+
set_request(
912+
&infq,
913+
0,
914+
page_addr,
915+
SIZE_OF_U32.try_into().unwrap(),
916+
VIRTQ_DESC_F_NEXT,
917+
);
912918

913919
check_metric_after_block!(
914920
METRICS.balloon.event_fails,
@@ -929,7 +935,13 @@ pub(crate) mod tests {
929935
// Test the happy case.
930936
{
931937
mem.write_obj::<u32>(0x1, GuestAddress(page_addr)).unwrap();
932-
set_request(&infq, 0, page_addr, SIZE_OF_U32 as u32, VIRTQ_DESC_F_NEXT);
938+
set_request(
939+
&infq,
940+
0,
941+
page_addr,
942+
SIZE_OF_U32.try_into().unwrap(),
943+
VIRTQ_DESC_F_NEXT,
944+
);
933945

934946
check_metric_after_block!(
935947
METRICS.balloon.inflate_count,
@@ -957,7 +969,13 @@ pub(crate) mod tests {
957969

958970
// Error case: forgot to trigger deflate event queue.
959971
{
960-
set_request(&defq, 0, page_addr, SIZE_OF_U32 as u32, VIRTQ_DESC_F_NEXT);
972+
set_request(
973+
&defq,
974+
0,
975+
page_addr,
976+
SIZE_OF_U32.try_into().unwrap(),
977+
VIRTQ_DESC_F_NEXT,
978+
);
961979
check_metric_after_block!(
962980
METRICS.balloon.event_fails,
963981
1,
@@ -971,7 +989,13 @@ pub(crate) mod tests {
971989

972990
// Happy case.
973991
{
974-
set_request(&defq, 1, page_addr, SIZE_OF_U32 as u32, VIRTQ_DESC_F_NEXT);
992+
set_request(
993+
&defq,
994+
1,
995+
page_addr,
996+
SIZE_OF_U32.try_into().unwrap(),
997+
VIRTQ_DESC_F_NEXT,
998+
);
975999
check_metric_after_block!(
9761000
METRICS.balloon.deflate_count,
9771001
1,
@@ -993,7 +1017,13 @@ pub(crate) mod tests {
9931017

9941018
// Error case: forgot to trigger stats event queue.
9951019
{
996-
set_request(&statsq, 0, 0x1000, SIZE_OF_STAT as u32, VIRTQ_DESC_F_NEXT);
1020+
set_request(
1021+
&statsq,
1022+
0,
1023+
0x1000,
1024+
SIZE_OF_STAT.try_into().unwrap(),
1025+
VIRTQ_DESC_F_NEXT,
1026+
);
9971027
check_metric_after_block!(
9981028
METRICS.balloon.event_fails,
9991029
1,
@@ -1029,7 +1059,7 @@ pub(crate) mod tests {
10291059
&statsq,
10301060
0,
10311061
page_addr,
1032-
2 * SIZE_OF_STAT as u32,
1062+
2 * u32::try_from(SIZE_OF_STAT).unwrap(),
10331063
VIRTQ_DESC_F_NEXT,
10341064
);
10351065
check_metric_after_block!(METRICS.balloon.stats_updates_count, 1, {

src/vmm/src/devices/virtio/mmio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ pub(crate) mod tests {
755755
let mut buf = vec![0; 4];
756756
let queue_len = d.locked_device().queues().len();
757757
for q in 0..queue_len {
758-
d.queue_select = q as u32;
758+
d.queue_select = q.try_into().unwrap();
759759
write_le_u32(&mut buf[..], 16);
760760
d.bus_write(0x38, &buf[..]);
761761
write_le_u32(&mut buf[..], 1);
@@ -808,7 +808,7 @@ pub(crate) mod tests {
808808
let mut buf = vec![0; 4];
809809
let queues_count = d.locked_device().queues().len();
810810
for q in 0..queues_count {
811-
d.queue_select = q as u32;
811+
d.queue_select = q.try_into().unwrap();
812812
write_le_u32(&mut buf[..], 16);
813813
d.bus_write(0x38, &buf[..]);
814814
write_le_u32(&mut buf[..], 1);

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,8 @@ pub mod tests {
11501150
// Check that the frame wasn't deferred.
11511151
assert!(!th.net().rx_deferred_frame);
11521152
// Check that the frame has been written successfully to the valid Rx descriptor chain.
1153-
th.rxq.check_used_elem(3, 5, frame.len() as u32);
1153+
th.rxq
1154+
.check_used_elem(3, 5, frame.len().try_into().unwrap());
11541155
th.rxq.dtable[5].check_data(&frame);
11551156
}
11561157

@@ -1186,7 +1187,8 @@ pub mod tests {
11861187
assert_eq!(th.rxq.used.idx.get(), 1);
11871188
assert!(&th.net().irq_trigger.has_pending_irq(IrqType::Vring));
11881189
// Check that the frame has been written successfully to the Rx descriptor chain.
1189-
th.rxq.check_used_elem(0, 3, frame.len() as u32);
1190+
th.rxq
1191+
.check_used_elem(0, 3, frame.len().try_into().unwrap());
11901192
th.rxq.dtable[3].check_data(&frame[..100]);
11911193
th.rxq.dtable[5].check_data(&frame[100..150]);
11921194
th.rxq.dtable[11].check_data(&frame[150..]);
@@ -1225,11 +1227,13 @@ pub mod tests {
12251227
assert_eq!(th.rxq.used.idx.get(), 2);
12261228
assert!(&th.net().irq_trigger.has_pending_irq(IrqType::Vring));
12271229
// Check that the 1st frame was written successfully to the 1st Rx descriptor chain.
1228-
th.rxq.check_used_elem(0, 0, frame_1.len() as u32);
1230+
th.rxq
1231+
.check_used_elem(0, 0, frame_1.len().try_into().unwrap());
12291232
th.rxq.dtable[0].check_data(&frame_1);
12301233
th.rxq.dtable[1].check_data(&[0; 500]);
12311234
// Check that the 2nd frame was written successfully to the 2nd Rx descriptor chain.
1232-
th.rxq.check_used_elem(1, 2, frame_2.len() as u32);
1235+
th.rxq
1236+
.check_used_elem(1, 2, frame_2.len().try_into().unwrap());
12331237
th.rxq.dtable[2].check_data(&frame_2);
12341238
th.rxq.dtable[3].check_data(&[0; 500]);
12351239
}
@@ -1821,7 +1825,8 @@ pub mod tests {
18211825
assert!(&th.net().irq_trigger.has_pending_irq(IrqType::Vring));
18221826
// make sure the data queue advanced
18231827
assert_eq!(th.rxq.used.idx.get(), 1);
1824-
th.rxq.check_used_elem(0, 0, frame.len() as u32);
1828+
th.rxq
1829+
.check_used_elem(0, 0, frame.len().try_into().unwrap());
18251830
th.rxq.dtable[0].check_data(frame);
18261831
}
18271832
}
@@ -1930,7 +1935,8 @@ pub mod tests {
19301935
assert!(&th.net().irq_trigger.has_pending_irq(IrqType::Vring));
19311936
// make sure the data queue advanced
19321937
assert_eq!(th.rxq.used.idx.get(), 1);
1933-
th.rxq.check_used_elem(0, 0, frame.len() as u32);
1938+
th.rxq
1939+
.check_used_elem(0, 0, frame.len().try_into().unwrap());
19341940
th.rxq.dtable[0].check_data(frame);
19351941
}
19361942
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,11 @@ pub mod test {
516516
self.add_desc_chain(
517517
NetQueue::Rx,
518518
0,
519-
&[(0, expected_frame.len() as u32, VIRTQ_DESC_F_WRITE)],
519+
&[(
520+
0,
521+
u32::try_from(expected_frame.len()).unwrap(),
522+
VIRTQ_DESC_F_WRITE,
523+
)],
520524
);
521525
check_metric_after_block!(
522526
METRICS.net.rx_packets_count,
@@ -527,7 +531,7 @@ pub mod test {
527531
assert_eq!(self.rxq.used.idx.get(), used_idx + 1);
528532
assert!(&self.net().irq_trigger.has_pending_irq(IrqType::Vring));
529533
self.rxq
530-
.check_used_elem(used_idx, 0, expected_frame.len() as u32);
534+
.check_used_elem(used_idx, 0, expected_frame.len().try_into().unwrap());
531535
self.rxq.dtable[0].check_data(expected_frame);
532536
}
533537

src/vmm/src/devices/virtio/test_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ impl<'a> VirtQueue<'a> {
275275
}
276276

277277
pub fn size(&self) -> u16 {
278-
self.dtable.len() as u16
278+
// Safe to unwrap because the size is specified as a u16 when the table is first created.
279+
self.dtable.len().try_into().unwrap()
279280
}
280281

281282
pub fn dtable_start(&self) -> GuestAddress {

src/vmm/src/devices/virtio/vsock/csm/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ mod tests {
926926

927927
fn init_data_pkt(&mut self, mut data: &[u8]) -> &VsockPacket {
928928
assert!(data.len() <= self.pkt.buf_size());
929-
self.init_pkt(uapi::VSOCK_OP_RW, data.len() as u32);
929+
self.init_pkt(uapi::VSOCK_OP_RW, u32::try_from(data.len()).unwrap());
930930

931931
let len = data.len();
932932
self.pkt

src/vmm/src/devices/virtio/vsock/unix/muxer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ mod tests {
866866
) -> &mut VsockPacket {
867867
assert!(data.len() <= self.pkt.buf_size());
868868
self.init_pkt(local_port, peer_port, uapi::VSOCK_OP_RW)
869-
.set_len(data.len() as u32);
869+
.set_len(u32::try_from(data.len()).unwrap());
870870

871871
let data_len = data.len(); // store in tmp var to make borrow checker happy.
872872
self.pkt

0 commit comments

Comments
 (0)