Skip to content

Commit

Permalink
implement minimal epoll_create1 shim
Browse files Browse the repository at this point in the history
  • Loading branch information
DebugSteven committed Jul 11, 2022
1 parent 97d115c commit 88f0925
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 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 @@ -62,6 +62,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.open(args)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"epoll_create1" => {
let [flag] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.epoll_create1(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
22 changes: 22 additions & 0 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ use rustc_middle::ty::{self, layout::LayoutOf};
use rustc_target::abi::{Align, Size};

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

mod epoll;

#[derive(Debug)]
struct FileHandle {
file: File,
Expand Down Expand Up @@ -725,6 +728,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}

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

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

let o_cloexec = this.eval_libc_i32("O_CLOEXEC")?;
// TODO actually do the thing
if flags == o_cloexec {
} else if flags != 0 {
let einval = this.eval_libc("EINVAL")?;
this.set_last_error(einval)?;
return Ok(-1);
}

let fh = &mut this.machine.file_handler;
let fd = fh.insert_fd(Box::new(Epoll));
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
54 changes: 54 additions & 0 deletions src/shims/unix/fs/epoll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// use crate::shims::unix::fs::FileDescriptor;
// use crate::shims::unix::fs::SeekFrom;
use crate::*;

use super::FileDescriptor;
use super::FileHandle;

use std::io;
use std::io::SeekFrom;

#[derive(Debug)]
pub struct Epoll;

impl FileDescriptor for Epoll {
fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> {
throw_unsup_format!("stderr and stdout cannot be used as FileHandle");
}

fn read<'tcx>(
&mut self,
_communicate_allowed: bool,
_bytes: &mut [u8],
) -> InterpResult<'tcx, io::Result<usize>> {
throw_unsup_format!("cannot read from stderr or stdout");
}

fn write<'tcx>(
&self,
_communicate_allowed: bool,
bytes: &[u8],
) -> InterpResult<'tcx, io::Result<usize>> {
// We just don't write anything, but report to the user that we did.
Ok(Ok(bytes.len()))
}

fn seek<'tcx>(
&mut self,
_communicate_allowed: bool,
_offset: SeekFrom,
) -> InterpResult<'tcx, io::Result<u64>> {
throw_unsup_format!("cannot seek on stderr or stdout");
}

fn close<'tcx>(
self: Box<Self>,
_communicate_allowed: bool,
) -> InterpResult<'tcx, io::Result<i32>> {
throw_unsup_format!("stderr and stdout cannot be closed");
}

fn dup<'tcx>(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
Ok(Box::new(Epoll))
}
}

0 comments on commit 88f0925

Please sign in to comment.