reverie/tests/signal.rs
Jason White 6813d9a7b2 rustfmt with imports_granularity=Item
Summary: This makes merge conflicts much easier to handle.

Reviewed By: johnhurt

Differential Revision: D37564000

fbshipit-source-id: a7f1a2711ffbdbb23c93e2f479f47d0368a2dad9
2022-06-30 14:56:20 -07:00

119 lines
3.5 KiB
Rust

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
// signal handling related tests.
use nix::sys::signal::Signal;
use reverie::syscalls::AddrMut;
use reverie::syscalls::ExitGroup;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::RtSigpending;
use reverie::syscalls::Syscall;
use reverie::syscalls::SyscallInfo;
use reverie::syscalls::Sysno;
use reverie::Error;
use reverie::Guest;
use reverie::Tool;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
struct LocalState;
#[reverie::tool]
impl Tool for LocalState {
async fn handle_syscall_event<T: Guest<Self>>(
&self,
guest: &mut T,
syscall: Syscall,
) -> Result<i64, Error> {
if syscall.number() == Sysno::exit_group {
let sigset_rptr = 0x7000_0100usize;
let sigset: AddrMut<libc::sigset_t> = AddrMut::from_raw(sigset_rptr as _).unwrap();
let exit_failure = ExitGroup::new().with_status(1);
let exit_success = syscall;
if guest
.inject(
RtSigpending::new()
.with_set(Some(sigset))
.with_sigsetsize(8usize),
)
.await
.is_ok()
{
let memory = guest.memory();
let pending: u64 = memory.read_value(sigset.cast())?;
if pending != 1u64 << (Signal::SIGVTALRM as i32 - 1) {
guest.tail_inject(exit_failure).await
} else {
guest.tail_inject(exit_success).await
}
} else {
guest.tail_inject(exit_success).await
}
} else {
guest.tail_inject(syscall).await
}
}
}
#[cfg(all(not(sanitized), test))]
mod tests {
use super::*;
use nix::sys::signal;
use reverie_ptrace::testing::check_fn;
use std::io;
use std::mem::MaybeUninit;
// kernel_sigset_t used by naked syscall
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct KernelSigset(u64);
impl From<&[Signal]> for KernelSigset {
fn from(signals: &[Signal]) -> Self {
let mut set: u64 = 0;
for &sig in signals {
set |= 1u64 << (sig as usize - 1);
}
KernelSigset(set)
}
}
#[allow(dead_code)]
unsafe fn block_signals(signals: &[Signal]) -> io::Result<KernelSigset> {
let set = KernelSigset::from(signals);
let mut oldset: MaybeUninit<u64> = MaybeUninit::uninit();
if libc::syscall(
libc::SYS_rt_sigprocmask,
libc::SIG_BLOCK,
&set as *const _,
oldset.as_mut_ptr(),
8,
) != 0
{
Err(io::Error::last_os_error())
} else {
Ok(KernelSigset(oldset.assume_init()))
}
}
#[test]
// The actual test is in `handle_syscall_event`. To test we can get
// pending signals from tracee, by injecting rt_sigpending.
fn can_get_pending_signals() {
check_fn::<LocalState, _>(|| {
assert!(unsafe { block_signals(&[Signal::SIGVTALRM]) }.is_ok());
assert!(signal::raise(Signal::SIGVTALRM).is_ok());
unsafe { libc::syscall(libc::SYS_exit_group, 0) };
});
}
}