Skip to content

Commit e261223

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
Unwraps bounded by device spec limitations
In this commit, I changed several places that refer to memory addresses that we generate and that are bounded by limitations of the system to use explicit unwraps. Signed-off-by: Jonathan Browne <[email protected]>
1 parent af83175 commit e261223

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ pub const MMIO_MEM_SIZE: u64 = MEM_32BIT_GAP_SIZE;
6666
pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
6767
// It's safe to cast MMIO_MEM_START to usize because it fits in a u32 variable
6868
// (It points to an address in the 32 bit space).
69-
match size.checked_sub(MMIO_MEM_START as usize) {
69+
match size.checked_sub(usize::try_from(MMIO_MEM_START).unwrap()) {
7070
// case1: guest memory fits before the gap
7171
None | Some(0) => vec![(GuestAddress(0), size)],
7272
// case2: guest memory extends beyond the gap
7373
Some(remaining) => vec![
74-
(GuestAddress(0), MMIO_MEM_START as usize),
74+
(GuestAddress(0), usize::try_from(MMIO_MEM_START).unwrap()),
7575
(GuestAddress(FIRST_ADDR_PAST_32BITS), remaining),
7676
],
7777
}
@@ -133,12 +133,12 @@ pub fn configure_system(
133133
params.hdr.type_of_loader = KERNEL_LOADER_OTHER;
134134
params.hdr.boot_flag = KERNEL_BOOT_FLAG_MAGIC;
135135
params.hdr.header = KERNEL_HDR_MAGIC;
136-
params.hdr.cmd_line_ptr = cmdline_addr.raw_value() as u32;
137-
params.hdr.cmdline_size = cmdline_size as u32;
136+
params.hdr.cmd_line_ptr = u32::try_from(cmdline_addr.raw_value()).unwrap();
137+
params.hdr.cmdline_size = u32::try_from(cmdline_size).unwrap();
138138
params.hdr.kernel_alignment = KERNEL_MIN_ALIGNMENT_BYTES;
139139
if let Some(initrd_config) = initrd {
140-
params.hdr.ramdisk_image = initrd_config.address.raw_value() as u32;
141-
params.hdr.ramdisk_size = initrd_config.size as u32;
140+
params.hdr.ramdisk_image = u32::try_from(initrd_config.address.raw_value()).unwrap();
141+
params.hdr.ramdisk_size = u32::try_from(initrd_config.size).unwrap();
142142
}
143143

144144
add_e820_entry(&mut params, 0, EBDA_START, E820_RAM)?;

src/vmm/src/arch/x86_64/mptable.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn setup_mptable(mem: &GuestMemoryMmap, num_cpus: u8) -> Result<(), MptableE
140140
let size = mem::size_of::<mpspec::mpf_intel>() as u64;
141141
let mut mpf_intel = mpspec::mpf_intel {
142142
signature: SMP_MAGIC_IDENT,
143-
physptr: (base_mp.raw_value() + size) as u32,
143+
physptr: u32::try_from(base_mp.raw_value() + size).unwrap(),
144144
length: 1,
145145
specification: 4,
146146
..mpspec::mpf_intel::default()
@@ -263,7 +263,10 @@ pub fn setup_mptable(mem: &GuestMemoryMmap, num_cpus: u8) -> Result<(), MptableE
263263
signature: MPC_SIGNATURE,
264264
// it's safe to use unchecked_offset_from because
265265
// table_end > table_base
266-
length: table_end.unchecked_offset_from(table_base) as u16,
266+
length: table_end
267+
.unchecked_offset_from(table_base)
268+
.try_into()
269+
.unwrap(),
267270
spec: MPC_SPEC,
268271
oem: MPC_OEM,
269272
productid: MPC_PRODUCT_ID,

src/vmm/src/device_manager/mmio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl MMIODeviceManager {
150150
let io_addr = IoEventAddress::Mmio(
151151
device_info.addr + u64::from(crate::devices::virtio::NOTIFY_REG_OFFSET),
152152
);
153-
vm.register_ioevent(queue_evt, &io_addr, i as u32)
153+
vm.register_ioevent(queue_evt, &io_addr, u32::try_from(i).unwrap())
154154
.map_err(MmioError::RegisterIoEvent)?;
155155
}
156156
vm.register_irqfd(locked_device.interrupt_evt(), device_info.irqs[0])

src/vmm/src/vstate/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Vm {
263263
.enumerate()
264264
.try_for_each(|(index, region)| {
265265
let memory_region = kvm_userspace_memory_region {
266-
slot: index as u32,
266+
slot: u32::try_from(index).unwrap(),
267267
guest_phys_addr: region.start_addr().raw_value(),
268268
memory_size: region.len(),
269269
// It's safe to unwrap because the guest address is valid.

0 commit comments

Comments
 (0)