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.
|
|
|
|
*/
|
|
|
|
|
2022-06-30 21:56:20 +00:00
|
|
|
use reverie::syscalls::Errno;
|
|
|
|
use reverie::syscalls::MemoryAccess;
|
|
|
|
use reverie::syscalls::Syscall;
|
|
|
|
use reverie::syscalls::Timespec;
|
|
|
|
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 {
|
2022-11-18 20:36:51 +00:00
|
|
|
type GlobalState = ();
|
|
|
|
type ThreadState = ();
|
|
|
|
|
2021-12-30 00:14:23 +00:00
|
|
|
async fn handle_syscall_event<T: Guest<Self>>(
|
|
|
|
&self,
|
|
|
|
guest: &mut T,
|
|
|
|
syscall: Syscall,
|
|
|
|
) -> Result<i64, Error> {
|
|
|
|
match syscall {
|
|
|
|
Syscall::ClockGettime(_gettime) => Err(Errno::EINVAL.into()),
|
|
|
|
Syscall::Gettimeofday(gettimeofday) => {
|
|
|
|
let retval = guest.inject(syscall).await?;
|
|
|
|
if let Some(tod) = gettimeofday.tv() {
|
|
|
|
let mut tv = guest.memory().read_value(tod)?;
|
|
|
|
tv.tv_usec = (tv.tv_usec / 1000) * 1000 + 345;
|
|
|
|
guest.memory().write_value(tod, &tv)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(retval)
|
|
|
|
}
|
|
|
|
Syscall::Getcpu(getcpu) => {
|
|
|
|
if let Some(cpu) = getcpu.cpu() {
|
|
|
|
guest.memory().write_value(cpu, &0)?;
|
|
|
|
}
|
|
|
|
Ok(0)
|
|
|
|
}
|
2022-04-12 18:09:06 +00:00
|
|
|
Syscall::ClockGetres(clock_getres) => {
|
|
|
|
if let Some(ts) = clock_getres.res() {
|
|
|
|
guest.memory().write_value(
|
|
|
|
ts,
|
|
|
|
&Timespec {
|
|
|
|
tv_sec: 0,
|
|
|
|
tv_nsec: 42,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
2021-12-30 00:14:23 +00:00
|
|
|
otherwise => guest.tail_inject(otherwise).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(not(sanitized), test))]
|
|
|
|
mod tests {
|
2022-06-30 21:56:20 +00:00
|
|
|
use std::mem::MaybeUninit;
|
|
|
|
use std::time;
|
2021-12-30 00:14:23 +00:00
|
|
|
|
2022-08-06 15:21:44 +00:00
|
|
|
use reverie_ptrace::testing::check_fn;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2021-12-30 00:14:23 +00:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn run_guest_vdso_tod_test() {
|
|
|
|
check_fn::<LocalState, _>(|| {
|
|
|
|
// this calls clock_gettime.
|
|
|
|
let _now = time::Instant::now();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_guest_vdso_getcpu_test() {
|
|
|
|
check_fn::<LocalState, _>(|| {
|
|
|
|
// this calls getcpu via vdso.
|
|
|
|
let cpu = unsafe { libc::sched_getcpu() };
|
|
|
|
// NB: getcpu in vdso area always set cpu to 0.
|
|
|
|
// see symbol __vdso_getcpu.
|
|
|
|
assert_eq!(cpu, 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_guest_vdso_gettimeofday_test() {
|
|
|
|
check_fn::<LocalState, _>(|| {
|
|
|
|
let mut tod = MaybeUninit::zeroed();
|
|
|
|
let mut tz = MaybeUninit::zeroed();
|
|
|
|
assert_eq!(
|
|
|
|
unsafe { libc::gettimeofday(tod.as_mut_ptr(), tz.as_mut_ptr()) },
|
|
|
|
0
|
|
|
|
);
|
|
|
|
let tod = unsafe { tod.assume_init() };
|
|
|
|
assert_eq!(tod.tv_usec % 1000, 345);
|
|
|
|
});
|
|
|
|
}
|
2022-04-12 18:09:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_guest_vdso_clock_getres_test() {
|
|
|
|
check_fn::<LocalState, _>(|| {
|
|
|
|
let mut res = libc::timespec {
|
|
|
|
tv_sec: 1,
|
|
|
|
tv_nsec: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let ret = unsafe { libc::clock_getres(libc::CLOCK_MONOTONIC, &mut res as *mut _) };
|
|
|
|
|
|
|
|
assert_eq!(ret, 0);
|
|
|
|
|
|
|
|
assert_eq!(res.tv_sec, 0);
|
|
|
|
assert_eq!(res.tv_nsec, 42);
|
|
|
|
});
|
|
|
|
}
|
2021-12-30 00:14:23 +00:00
|
|
|
}
|