2021-12-30 00:14:23 +00:00
|
|
|
/*
|
2022-06-02 18:51:06 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2021-12-30 00:14:23 +00:00
|
|
|
* 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;
|
2022-06-30 21:56:20 +00:00
|
|
|
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;
|
2021-12-30 00:14:23 +00:00
|
|
|
|
2022-11-03 19:53:11 +00:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2021-12-30 00:14:23 +00:00
|
|
|
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 {
|
2022-06-30 21:56:20 +00:00
|
|
|
use std::io;
|
|
|
|
use std::mem::MaybeUninit;
|
2021-12-30 00:14:23 +00:00
|
|
|
|
2022-08-06 15:21:44 +00:00
|
|
|
use nix::sys::signal;
|
|
|
|
use reverie_ptrace::testing::check_fn;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2021-12-30 00:14:23 +00:00
|
|
|
// 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());
|
|
|
|
|
2022-03-30 03:22:00 +00:00
|
|
|
unsafe { libc::syscall(libc::SYS_exit_group, 0) };
|
2021-12-30 00:14:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|