Skip to content

Commit 6a8347f

Browse files
committed
fix(aarch64): drop fds in unit tests before asserts
If test need to drop fd, it needs to do that before assert checks. Otherwise if assert fails, the IO safety error will occur instead of the assertion error. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent e34ba7a commit 6a8347f

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/vmm/src/arch/aarch64/vcpu.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,16 @@ mod tests {
532532
unsafe { libc::close(vm.fd().as_raw_fd()) };
533533

534534
let err = KvmVcpu::new(0, &vm);
535+
536+
// dropping vm would double close the gic fd, so leak it
537+
// do the drop before assertion. Otherwise if assert fails,
538+
// we get IO runtime error instead of assert error.
539+
std::mem::forget(vm);
540+
535541
assert_eq!(
536542
err.err().unwrap().to_string(),
537543
"Error creating vcpu: Bad file descriptor (os error 9)".to_string()
538544
);
539-
540-
// dropping vm would double close the gic fd, so leak it
541-
std::mem::forget(vm);
542545
}
543546

544547
#[test]
@@ -574,6 +577,12 @@ mod tests {
574577
&vcpu_config,
575578
&optional_capabilities,
576579
);
580+
581+
// dropping vcpu would double close the gic fd, so leak it
582+
// do the drop before assertion. Otherwise if assert fails,
583+
// we get IO runtime error instead of assert error.
584+
std::mem::forget(vcpu);
585+
577586
assert_eq!(
578587
err.unwrap_err(),
579588
KvmVcpuError::ConfigureRegisters(VcpuArchError::SetOneReg(
@@ -582,9 +591,6 @@ mod tests {
582591
kvm_ioctls::Error::new(9)
583592
))
584593
);
585-
586-
// dropping vcpu would double close the gic fd, so leak it
587-
std::mem::forget(vcpu);
588594
}
589595

590596
#[test]
@@ -780,9 +786,12 @@ mod tests {
780786
assert!(matches!(res, Err(VcpuArchError::GetMp(_))), "{:?}", res);
781787

782788
let res = vcpu.set_mpstate(kvm_mp_state::default());
783-
assert!(matches!(res, Err(VcpuArchError::SetMp(_))), "{:?}", res);
784789

785790
// dropping vcpu would double close the fd, so leak it
791+
// do the drop before assertion. Otherwise if assert fails,
792+
// we get IO runtime error instead of assert error.
786793
std::mem::forget(vcpu);
794+
795+
assert!(matches!(res, Err(VcpuArchError::SetMp(_))), "{:?}", res);
787796
}
788797
}

0 commit comments

Comments
 (0)