Skip to content

Commit 1a24495

Browse files
andreeaflorescujiangliu
authored andcommitted
update usage of FAM struct
FAM Struct now returns a result on new. Check the result before initializing the relevant structures. PartialEq is no longer implemented for FAM structs where the inner type does not implement PartialEq as well. As a consequence, we cannot test for equality between Cpuid values. Signed-off-by: Andreea Florescu <fandree@amazon.com>
1 parent e27b55d commit 1a24495

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/ioctls/system.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl Kvm {
255255
return Err(errno::Error::new(libc::ENOMEM));
256256
}
257257

258-
let mut cpuid = CpuId::new(num_entries);
258+
let mut cpuid = CpuId::new(num_entries).map_err(|_| errno::Error::new(libc::ENOMEM))?;
259259

260260
let ret = unsafe {
261261
// ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
@@ -344,7 +344,8 @@ impl Kvm {
344344
/// ```
345345
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
346346
pub fn get_msr_index_list(&self) -> Result<MsrList> {
347-
let mut msr_list = MsrList::new(KVM_MAX_MSR_ENTRIES);
347+
let mut msr_list =
348+
MsrList::new(KVM_MAX_MSR_ENTRIES).map_err(|_| errno::Error::new(libc::ENOMEM))?;
348349

349350
let ret = unsafe {
350351
// ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
@@ -656,8 +657,7 @@ mod tests {
656657
let kvm = unsafe { Kvm::from_raw_fd(rawfd) };
657658

658659
let cpuid_1 = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
659-
let cpuid_2 = CpuId::new(cpuid_1.as_fam_struct_ref().len());
660-
assert!(cpuid_1 != cpuid_2);
660+
let _ = CpuId::new(cpuid_1.as_fam_struct_ref().len()).unwrap();
661661
}
662662

663663
#[test]

src/ioctls/vcpu.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl VcpuFd {
381381
return Err(errno::Error::new(libc::ENOMEM));
382382
}
383383

384-
let mut cpuid = CpuId::new(num_entries);
384+
let mut cpuid = CpuId::new(num_entries).map_err(|_| errno::Error::new(libc::ENOMEM))?;
385385
let ret = unsafe {
386386
// Here we trust the kernel not to read past the end of the kvm_cpuid2 struct.
387387
ioctl_with_mut_ptr(self, KVM_GET_CPUID2(), cpuid.as_mut_fam_struct_ptr())
@@ -548,7 +548,7 @@ impl VcpuFd {
548548
/// index: 0x0000_0175,
549549
/// ..Default::default()
550550
/// },
551-
/// ]);
551+
/// ]).unwrap();
552552
/// let read = vcpu.get_msrs(&mut msrs).unwrap();
553553
/// assert_eq!(read, 2);
554554
/// ```
@@ -591,7 +591,7 @@ impl VcpuFd {
591591
/// index: 0x0000_0174,
592592
/// ..Default::default()
593593
/// },
594-
/// ]);
594+
/// ]).unwrap();
595595
/// let written = vcpu.set_msrs(&msrs).unwrap();
596596
/// assert_eq!(written, 1);
597597
/// ```
@@ -1035,7 +1035,7 @@ impl VcpuFd {
10351035
/// vm.get_preferred_target(&mut kvi).unwrap();
10361036
/// vcpu.vcpu_init(&kvi).expect("Cannot initialize vcpu");
10371037
///
1038-
/// let mut reg_list = RegList::new(500);
1038+
/// let mut reg_list = RegList::new(500).unwrap();
10391039
/// vcpu.get_reg_list(&mut reg_list).unwrap();
10401040
/// assert!(reg_list.as_fam_struct_ref().n > 0);
10411041
/// ```
@@ -1597,7 +1597,7 @@ mod tests {
15971597
..Default::default()
15981598
},
15991599
];
1600-
let msrs_wrapper = Msrs::from_entries(&msrs_to_set);
1600+
let msrs_wrapper = Msrs::from_entries(&msrs_to_set).unwrap();
16011601
vcpu.set_msrs(&msrs_wrapper).unwrap();
16021602

16031603
// Now test that GET_MSRS returns the same.
@@ -1611,7 +1611,8 @@ mod tests {
16111611
index: 0x0000_0175,
16121612
..Default::default()
16131613
},
1614-
]);
1614+
])
1615+
.unwrap();
16151616
let nmsrs = vcpu.get_msrs(&mut returned_kvm_msrs).unwrap();
16161617

16171618
// Verify the lengths match.
@@ -1990,13 +1991,16 @@ mod tests {
19901991
);
19911992
assert_eq!(
19921993
faulty_vcpu_fd
1993-
.get_msrs(&mut Msrs::new(1))
1994+
.get_msrs(&mut Msrs::new(1).unwrap())
19941995
.unwrap_err()
19951996
.errno(),
19961997
badf_errno
19971998
);
19981999
assert_eq!(
1999-
faulty_vcpu_fd.set_msrs(&Msrs::new(1)).unwrap_err().errno(),
2000+
faulty_vcpu_fd
2001+
.set_msrs(&Msrs::new(1).unwrap())
2002+
.unwrap_err()
2003+
.errno(),
20002004
badf_errno
20012005
);
20022006
assert_eq!(
@@ -2136,7 +2140,7 @@ mod tests {
21362140
let vm = kvm.create_vm().unwrap();
21372141
let vcpu = vm.create_vcpu(0).unwrap();
21382142

2139-
let mut reg_list = RegList::new(1);
2143+
let mut reg_list = RegList::new(1).unwrap();
21402144
// KVM_GET_REG_LIST demands that the vcpus be initalized, so we expect this to fail.
21412145
let err = vcpu.get_reg_list(&mut reg_list).unwrap_err();
21422146
assert!(err.errno() == libc::ENOEXEC);
@@ -2154,7 +2158,7 @@ mod tests {
21542158

21552159
// We make use of the number of registers returned to allocate memory and
21562160
// try one more time.
2157-
let mut reg_list = RegList::new(reg_list.as_mut_fam_struct().n as usize);
2161+
let mut reg_list = RegList::new(reg_list.as_mut_fam_struct().n as usize).unwrap();
21582162
assert!(vcpu.get_reg_list(&mut reg_list).is_ok());
21592163
}
21602164

0 commit comments

Comments
 (0)