reverie/tests/vdso.rs
facebook-github-bot 15d2f61411 Initial commit
fbshipit-source-id: c440d991296c92bdc5e109a11d269049e8840e94
2021-12-29 16:14:27 -08:00

88 lines
2.5 KiB
Rust

/*
* Copyright (c) Facebook, Inc. and its 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.
*/
use reverie::{
syscalls::{Errno, MemoryAccess, Syscall},
Error, Guest, Tool,
};
use serde::{Deserialize, 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> {
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)
}
otherwise => guest.tail_inject(otherwise).await,
}
}
}
#[cfg(all(not(sanitized), test))]
mod tests {
use super::*;
use reverie_ptrace::testing::check_fn;
use std::{mem::MaybeUninit, time};
#[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);
});
}
}