Skip to content

Commit

Permalink
implement shim for eventfd
Browse files Browse the repository at this point in the history
  • Loading branch information
DebugSteven committed Jul 18, 2022
1 parent 5f934a4 commit abaa446
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.epoll_create1(flag)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"eventfd" => {
let [initval, flag] = this.check_shim(abi, Abi::C {unwind: false }, link_name, args)?;
let result = this.eventfd(initval, flag)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"close" => {
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.close(fd)?;
Expand Down
30 changes: 28 additions & 2 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ use rustc_target::abi::{Align, Size};

use crate::*;
use epoll::Epoll;
use event::Event;
use shims::os_str::os_str_to_bytes;
use shims::time::system_time_to_duration;

mod epoll;
mod event;

#[derive(Debug)]
struct FileHandle {
Expand Down Expand Up @@ -762,9 +764,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

let flags = this.read_scalar(flags)?.to_i32()?;

let o_cloexec = this.eval_libc_i32("O_CLOEXEC")?;
let epoll_cloexec = this.eval_libc_i32("EPOLL_CLOEXEC")?;
// TODO actually do the thing
if flags == o_cloexec {
if flags == epoll_cloexec {
} else if flags != 0 {
let einval = this.eval_libc("EINVAL")?;
this.set_last_error(einval)?;
Expand All @@ -776,6 +778,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
Ok(fd)
}

fn eventfd(
&mut self,
_initval: &OpTy<'tcx, Tag>,
flags: &OpTy<'tcx, Tag>,
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

let flags = this.read_scalar(flags)?.to_i32()?;

let efd_cloexec = this.eval_libc_i32("EFD_CLOEXEC")?;
let efd_nonblock = this.eval_libc_i32("EFD_NONBLOCK")?;
let efd_semaphore = this.eval_libc_i32("EFD_SEMAPHORE")?;
// TODO actually do the thing
if flags & efd_cloexec != 0 {}
if flags & efd_nonblock != 0 {}
if flags & efd_semaphore != 0 {
throw_unsup_format!("EFD_SEMAPHORE is unsupported");
}

let fh = &mut this.machine.file_handler;
let fd = fh.insert_fd(Box::new(Event));
Ok(fd)
}

fn read(&mut self, fd: i32, buf: Pointer<Option<Tag>>, count: u64) -> InterpResult<'tcx, i64> {
let this = self.eval_context_mut();

Expand Down
4 changes: 4 additions & 0 deletions src/shims/unix/fs/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ impl FileDescriptor for Epoll {
fn dup<'tcx>(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
Ok(Box::new(Epoll))
}

fn as_unix_host_fd(&self) -> Option<i32> {
None
}
}

0 comments on commit abaa446

Please sign in to comment.