Skip to content

Commit bb0e700

Browse files
committed
fix: do not double close file descriptors in unittests
With newer rust toolchains, rust will SIGABRT if an already closed file descriptor is closed in the the `Drop` implementation for `File`. This also applies to creating `File`s with invalid file descriptors. Thus fix tests that accidentally double-close fds, and remove those that explicitly construct `File`s with invalid file descriptors (they are redundant now anyway, because Rust would abort if this ever happened). Signed-off-by: Patrick Roy <[email protected]>
1 parent 0b117d3 commit bb0e700

File tree

12 files changed

+34
-45
lines changed

12 files changed

+34
-45
lines changed

src/vmm/src/arch/aarch64/gic/gicv2/regs/dist_regs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,8 @@ mod tests {
162162
format!("{:?}", res.unwrap_err()),
163163
"DeviceAttribute(Error(9), false, 1)"
164164
);
165+
166+
// dropping gic_fd would double close the gic fd, so leak it
167+
std::mem::forget(gic_fd);
165168
}
166169
}

src/vmm/src/arch/aarch64/gic/gicv2/regs/icc_regs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,8 @@ mod tests {
120120
format!("{:?}", res.unwrap_err()),
121121
"DeviceAttribute(Error(9), false, 2)"
122122
);
123+
124+
// dropping gic_fd would double close the gic fd, so leak it
125+
std::mem::forget(gic_fd);
123126
}
124127
}

src/vmm/src/arch/aarch64/gic/gicv3/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,8 @@ mod tests {
220220
format!("{:?}", res.unwrap_err()),
221221
"DeviceAttribute(Error(9), true, 4)"
222222
);
223+
224+
// dropping gic_fd would double close the gic fd, so leak it
225+
std::mem::forget(gic);
223226
}
224227
}

src/vmm/src/arch/aarch64/gic/gicv3/regs/dist_regs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ mod tests {
159159
format!("{:?}", res.unwrap_err()),
160160
"DeviceAttribute(Error(9), false, 1)"
161161
);
162+
163+
// dropping gic_fd would double close the gic fd, so leak it
164+
std::mem::forget(gic_fd);
162165
}
163166

164167
#[test]

src/vmm/src/arch/aarch64/gic/gicv3/regs/icc_regs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ mod tests {
206206
format!("{:?}", res.unwrap_err()),
207207
"DeviceAttribute(Error(9), false, 6)"
208208
);
209+
210+
// dropping gic_fd would double close the gic fd, so leak it
211+
std::mem::forget(gic_fd);
209212
}
210213

211214
#[test]

src/vmm/src/arch/aarch64/gic/gicv3/regs/redist_regs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,8 @@ mod tests {
120120
format!("{:?}", res.unwrap_err()),
121121
"DeviceAttribute(Error(9), false, 5)"
122122
);
123+
124+
// dropping gic_fd would double close the gic fd, so leak it
125+
std::mem::forget(gic_fd);
123126
}
124127
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,8 @@ mod tests {
299299

300300
let res = set_mpstate(&vcpu, kvm_mp_state::default());
301301
assert!(matches!(res, Err(VcpuError::SetMp(_))), "{:?}", res);
302+
303+
// dropping vcpu would double close the fd, so leak it
304+
std::mem::forget(vcpu);
302305
}
303306
}

src/vmm/src/devices/virtio/block/virtio/io/mod.rs

-29
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ impl<T: Debug> FileEngine<T> {
187187
pub mod tests {
188188
#![allow(clippy::undocumented_unsafe_blocks)]
189189
use std::os::unix::ffi::OsStrExt;
190-
use std::os::unix::io::FromRawFd;
191190

192191
use vmm_sys_util::tempfile::TempFile;
193192

@@ -201,20 +200,6 @@ pub mod tests {
201200
// 2 pages of memory should be enough to test read/write ops and also dirty tracking.
202201
const MEM_LEN: usize = 8192;
203202

204-
macro_rules! assert_err {
205-
($expression:expr, $($pattern:tt)+) => {
206-
match $expression {
207-
Err(UserDataError {
208-
user_data: _,
209-
error: $($pattern)+,
210-
}) => (),
211-
ref err => {
212-
panic!("expected `{}` but got `{:?}`", stringify!($($pattern)+), err);
213-
}
214-
}
215-
};
216-
}
217-
218203
macro_rules! assert_sync_execution {
219204
($expression:expr, $count:expr) => {
220205
match $expression {
@@ -265,17 +250,7 @@ pub mod tests {
265250

266251
#[test]
267252
fn test_sync() {
268-
// Check invalid file
269253
let mem = create_mem();
270-
let file = unsafe { File::from_raw_fd(-2) };
271-
let mut engine = FileEngine::from_file(file, FileEngineType::Sync).unwrap();
272-
let res = engine.read(0, &mem, GuestAddress(0), 0, ());
273-
assert_err!(res, BlockIoError::Sync(sync_io::SyncIoError::Seek(_e)));
274-
let res = engine.write(0, &mem, GuestAddress(0), 0, ());
275-
assert_err!(res, BlockIoError::Sync(sync_io::SyncIoError::Seek(_e)));
276-
let res = engine.flush(());
277-
assert_err!(res, BlockIoError::Sync(sync_io::SyncIoError::SyncAll(_e)));
278-
279254
// Create backing file.
280255
let file = TempFile::new().unwrap().into_file();
281256
let mut engine = FileEngine::from_file(file, FileEngineType::Sync).unwrap();
@@ -342,10 +317,6 @@ pub mod tests {
342317

343318
#[test]
344319
fn test_async() {
345-
// Check invalid file
346-
let file = unsafe { File::from_raw_fd(-2) };
347-
FileEngine::<()>::from_file(file, FileEngineType::Async).unwrap_err();
348-
349320
// Create backing file.
350321
let file = TempFile::new().unwrap().into_file();
351322
let mut engine = FileEngine::<()>::from_file(file, FileEngineType::Async).unwrap();

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

+6
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,9 @@ pub mod tests {
18081808
assert_eq!(th.txq.used.idx.get(), 1);
18091809
assert!(&th.net().irq_trigger.has_pending_irq(IrqType::Vring));
18101810
th.txq.check_used_elem(0, 0, 0);
1811+
1812+
// dropping th would double close the tap fd, so leak it
1813+
std::mem::forget(th);
18111814
}
18121815

18131816
#[test]
@@ -2041,6 +2044,9 @@ pub mod tests {
20412044
1,
20422045
th.simulate_event(NetEvent::Tap)
20432046
);
2047+
2048+
// dropping th would double close the tap fd, so leak it
2049+
std::mem::forget(th);
20442050
}
20452051

20462052
#[test]

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

-13
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,6 @@ pub mod tests {
277277
let tap = Tap::open_named("").unwrap();
278278
tap.set_vnet_hdr_size(16).unwrap();
279279
tap.set_offload(0).unwrap();
280-
281-
let faulty_tap = Tap {
282-
tap_file: unsafe { File::from_raw_fd(-2) },
283-
if_name: [0x01; 16],
284-
};
285-
assert_eq!(
286-
faulty_tap.set_vnet_hdr_size(16).unwrap_err().to_string(),
287-
TapError::SetSizeOfVnetHdr(IoError::from_raw_os_error(9)).to_string()
288-
);
289-
assert_eq!(
290-
faulty_tap.set_offload(0).unwrap_err().to_string(),
291-
TapError::SetOffloadFlags(IoError::from_raw_os_error(9)).to_string()
292-
);
293280
}
294281

295282
#[test]

src/vmm/src/vstate/vcpu/aarch64.rs

+6
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ mod tests {
329329
err.err().unwrap().to_string(),
330330
"Error creating vcpu: Bad file descriptor (os error 9)".to_string()
331331
);
332+
333+
// dropping vm would double close the gic fd, so leak it
334+
std::mem::forget(vm);
332335
}
333336

334337
#[test]
@@ -361,6 +364,9 @@ mod tests {
361364
kvm_ioctls::Error::new(9)
362365
))
363366
);
367+
368+
// dropping vcpu would double close the gic fd, so leak it
369+
std::mem::forget(vcpu);
364370
}
365371

366372
#[test]

src/vmm/src/vstate/vcpu/x86_64.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,6 @@ impl Debug for VcpuState {
715715
mod tests {
716716
#![allow(clippy::undocumented_unsafe_blocks)]
717717

718-
use std::os::unix::io::AsRawFd;
719-
720718
use kvm_bindings::kvm_msr_entry;
721719
use kvm_ioctls::{Cap, Kvm};
722720

@@ -874,7 +872,7 @@ mod tests {
874872
// Restore the state into the existing vcpu.
875873
let result1 = vcpu.restore_state(&state);
876874
assert!(result1.is_ok(), "{}", result1.unwrap_err());
877-
unsafe { libc::close(vcpu.fd.as_raw_fd()) };
875+
drop(vcpu);
878876

879877
// Restore the state into a new vcpu.
880878
let (_vm, vcpu, _mem) = setup_vcpu(0x10000);

0 commit comments

Comments
 (0)