reverie/tests/signal.rs
Vladimir Makaev 03cbd6044d update hermetic_infra/** files with correct license header - 1/x
Summary:
Followed guide here https://www.internalfb.com/intern/wiki/Linting/License_Lint/ to add fbcode/hermetic_infra/** code to license linter. As we have parts of our code shipped as Open Source it's important to get this automated

This diff is updating existing file's licenses to not get conflict after lint rule enablement

Reviewed By: jasonwhite

Differential Revision: D40674080

fbshipit-source-id: da6ecac036f8964619cf7912058f3a911558e7b1
2022-10-26 12:18:14 -07:00

120 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 std::io;
use std::mem::MaybeUninit;
use nix::sys::signal;
use reverie_ptrace::testing::check_fn;
use super::*;
// 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) };
});
}
}