crosvm/swap/tests/main.rs
Dennis Kempin b67eaca468 Use custom test harness for tests using fork
forking a process with multiple threads will lead to
undefined behavior. Unfortunately, newer versions of
libtest will spawn multiple threads even when running
a with --test-threads=1.

This change implements a custom, test harness using
libtest-mimic. It mimics the libtest CLI but enforces
a single threaded test process.

BUG=b:270167741
BUG=b:268496046
TEST=tools/run_tests --dut=host

Change-Id: Icef6a1b65bab7f5cd5021c01fbd94487fa0ca5fb
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4295157
Reviewed-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Frederick Mayle <fmayle@google.com>
2023-03-01 21:46:16 +00:00

106 lines
3.6 KiB
Rust

// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! Integration tests for vmm-swap feature
#[cfg(unix)]
mod common;
#[cfg(unix)]
mod test {
use super::common::*;
use base::pagesize;
use base::sys::wait_for_pid;
use base::AsRawDescriptor;
use base::FromRawDescriptor;
use base::IntoRawDescriptor;
use base::SafeDescriptor;
use base::Tube;
use swap::userfaultfd::register_regions;
use swap::userfaultfd::unregister_regions;
use swap::userfaultfd::Userfaultfd;
pub fn register_region_skip_obsolete_process() {
let shm = create_shared_memory("test", 3 * pagesize());
let uffd = create_uffd_for_test();
let base_addr = shm.base_addr();
let regions = [base_addr..(base_addr + 3 * pagesize())];
let (tube_main, tube_child) = Tube::pair().unwrap();
let pid = unsafe { libc::fork() };
if pid == 0 {
// child process
let uffd = create_uffd_for_test();
tube_child
.send(&unsafe { SafeDescriptor::from_raw_descriptor(uffd.as_raw_descriptor()) })
.unwrap();
std::process::exit(0);
}
let uffd_descriptor = tube_main
.recv::<SafeDescriptor>()
.unwrap()
.into_raw_descriptor();
wait_for_pid(pid, 0).unwrap();
let uffd_child = unsafe { Userfaultfd::from_raw_descriptor(uffd_descriptor) };
let result = unsafe { register_regions(&regions, &[uffd, uffd_child]) };
// no error from ENOMEM
assert_eq!(result.is_ok(), true);
}
pub fn unregister_region_skip_obsolete_process() {
let shm = create_shared_memory("test", 3 * pagesize());
let uffd = create_uffd_for_test();
let base_addr = shm.base_addr();
let regions = [base_addr..(base_addr + 3 * pagesize())];
let (tube_main, tube_child) = Tube::pair().unwrap();
let pid = unsafe { libc::fork() };
if pid == 0 {
// child process
let uffd = create_uffd_for_test();
tube_child
.send(&unsafe { SafeDescriptor::from_raw_descriptor(uffd.as_raw_descriptor()) })
.unwrap();
tube_child.recv::<u8>().unwrap();
std::process::exit(0);
}
let uffd_descriptor = tube_main
.recv::<SafeDescriptor>()
.unwrap()
.into_raw_descriptor();
let uffd_child = unsafe { Userfaultfd::from_raw_descriptor(uffd_descriptor) };
let uffds = [uffd, uffd_child];
unsafe { register_regions(&regions, &uffds) }.unwrap();
tube_main.send(&0_u8).unwrap();
// wait until the child process die and the uffd_child become obsolete.
wait_for_pid(pid, 0).unwrap();
let result = unregister_regions(&regions, &uffds);
// no error from ENOMEM
assert_eq!(result.is_ok(), true);
}
}
fn main() {
let args = libtest_mimic::Arguments {
// Force single-threaded execution to allow safe use of libc::fork in these tests.
test_threads: Some(1),
..libtest_mimic::Arguments::from_args()
};
let tests = vec![
#[cfg(unix)]
libtest_mimic::Trial::test("register_region_skip_obsolete_process", move || {
test::register_region_skip_obsolete_process();
Ok(())
}),
#[cfg(unix)]
libtest_mimic::Trial::test("unregister_region_skip_obsolete_process", move || {
test::unregister_region_skip_obsolete_process();
Ok(())
}),
];
libtest_mimic::run(&args, tests).exit();
}