From e423460238fe1cfb8853faef3b855f1ce53b756c Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Thu, 1 Mar 2018 15:29:27 -0800 Subject: [PATCH] kvm: fix invocation of KVM_SET_SIGNAL_MASK ioctl Linux kernel expects size of sigset passed in through KVM_SET_SIGNAL_MASK ioctl to be exactly 8, but Rust's sigset size is 128 bytes, so we can not use sizeof to set up the size. Also let's add test set_signal_mask(). TEST=cargo test --features plugin; cargo test -p kvm; ./build_test BUG=chromium:800626 Change-Id: Ica757ad63d6754d5c8008ba1735982c7ca026f33 Signed-off-by: Dmitry Torokhov Reviewed-on: https://chromium-review.googlesource.com/944849 Reviewed-by: Zach Reizner --- kvm/src/lib.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/kvm/src/lib.rs b/kvm/src/lib.rs index 8037078073..2ab3a97484 100644 --- a/kvm/src/lib.rs +++ b/kvm/src/lib.rs @@ -967,7 +967,15 @@ impl Vcpu { // ensures no out-of-bounds errors below. &mut *(vec.as_ptr() as *mut kvm_signal_mask) }; - kvm_sigmask.len = size_of::() as u32; + + // Rust definition of sigset_t takes 128 bytes, but the kernel only + // expects 8-bytes structure, so we can't write + // kvm_sigmask.len = size_of::() as u32; + kvm_sigmask.len = 8; + // Ensure the length is not too big. + const _ASSERT: usize = size_of::() - 8 as usize; + + // Safe as we allocated exactly the needed space unsafe { std::ptr::copy(&sigset, kvm_sigmask.sigset.as_mut_ptr() as *mut sigset_t, 1); } @@ -1301,6 +1309,15 @@ mod tests { .unwrap(); } + #[test] + fn set_signal_mask() { + let kvm = Kvm::new().unwrap(); + let gm = GuestMemory::new(&vec![(GuestAddress(0), 0x10000)]).unwrap(); + let vm = Vm::new(&kvm, gm).unwrap(); + let vcpu = Vcpu::new(0, &kvm, &vm).unwrap(); + vcpu.set_signal_mask(&[sys_util::SIGRTMIN() + 0]).unwrap(); + } + #[test] fn vcpu_mmap_size() { let kvm = Kvm::new().unwrap();