mirror of
https://github.com/facebookexperimental/reverie.git
synced 2025-02-02 17:51:25 +00:00
03cbd6044d
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
97 lines
3 KiB
Rust
97 lines
3 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.
|
|
*/
|
|
|
|
//! An example that tracks thread pedigree using local state
|
|
use clap::Parser;
|
|
use reverie::syscalls::Syscall;
|
|
use reverie::Error;
|
|
use reverie::Guest;
|
|
use reverie::Pid;
|
|
use reverie::Tool;
|
|
use reverie_util::pedigree::Pedigree;
|
|
use reverie_util::CommonToolArguments;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use tracing::debug;
|
|
use tracing::trace;
|
|
|
|
// TODO: Add handle pedigree forking, initialization, etc. to tool.
|
|
// This tool is NOT FUNCTIONAL in its current state.
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
|
struct PedigreeLocal(Pedigree);
|
|
|
|
#[reverie::tool]
|
|
impl Tool for PedigreeLocal {
|
|
type ThreadState = PedigreeLocal;
|
|
|
|
fn new(pid: Pid, _cfg: &()) -> Self {
|
|
debug!("[pedigree] initialize pedigree for pid {}", pid);
|
|
PedigreeLocal(Pedigree::new())
|
|
}
|
|
|
|
fn init_thread_state(
|
|
&self,
|
|
_tid: Pid,
|
|
parent: Option<(Pid, &Self::ThreadState)>,
|
|
) -> Self::ThreadState {
|
|
if let Some((_, state)) = parent {
|
|
let mut parent = state.clone();
|
|
let child = parent.0.fork_mut();
|
|
trace!("child pedigree: {:?}", child);
|
|
PedigreeLocal(child)
|
|
} else {
|
|
PedigreeLocal(Pedigree::new())
|
|
}
|
|
}
|
|
|
|
async fn handle_syscall_event<T: Guest<Self>>(
|
|
&self,
|
|
guest: &mut T,
|
|
syscall: Syscall,
|
|
) -> Result<i64, Error> {
|
|
match syscall {
|
|
Syscall::Fork(_) | Syscall::Vfork(_) | Syscall::Clone(_) => {
|
|
let retval = guest.inject(syscall).await?;
|
|
let pedigree = guest.thread_state_mut().0.fork_mut();
|
|
trace!(
|
|
"got new pedigree: {:?} => {:x?}",
|
|
pedigree,
|
|
nix::unistd::Pid::try_from(&pedigree)
|
|
);
|
|
Ok(retval)
|
|
}
|
|
Syscall::Getpid(_)
|
|
| Syscall::Getppid(_)
|
|
| Syscall::Gettid(_)
|
|
| Syscall::Getpgid(_)
|
|
| Syscall::Getpgrp(_) => {
|
|
let pid = guest.inject(syscall).await?;
|
|
let vpid = nix::unistd::Pid::try_from(&self.0).unwrap();
|
|
trace!("getpid returned {:?} vpid: {:?}", pid, vpid);
|
|
Ok(pid)
|
|
}
|
|
Syscall::Setpgid(_) => {
|
|
panic!("[pedigree] setpgid is not allowed.");
|
|
}
|
|
_ => guest.tail_inject(syscall).await,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Error> {
|
|
let args = CommonToolArguments::from_args();
|
|
let log_guard = args.init_tracing();
|
|
let tracer = reverie_ptrace::TracerBuilder::<PedigreeLocal>::new(args.into())
|
|
.spawn()
|
|
.await?;
|
|
let (status, _global_state) = tracer.wait().await?;
|
|
drop(log_guard); // Flush logs before exiting.
|
|
status.raise_or_exit()
|
|
}
|