Skip to content

Commit 88f0925

Browse files
committed
implement minimal epoll_create1 shim
1 parent 97d115c commit 88f0925

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/shims/unix/foreign_items.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6262
let result = this.open(args)?;
6363
this.write_scalar(Scalar::from_i32(result), dest)?;
6464
}
65+
"epoll_create1" => {
66+
let [flag] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
67+
let result = this.epoll_create1(flag)?;
68+
this.write_scalar(Scalar::from_i32(result), dest)?;
69+
}
6570
"close" => {
6671
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
6772
let result = this.close(fd)?;

src/shims/unix/fs.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ use rustc_middle::ty::{self, layout::LayoutOf};
1414
use rustc_target::abi::{Align, Size};
1515

1616
use crate::*;
17+
use epoll::Epoll;
1718
use shims::os_str::os_str_to_bytes;
1819
use shims::time::system_time_to_duration;
1920

21+
mod epoll;
22+
2023
#[derive(Debug)]
2124
struct FileHandle {
2225
file: File,
@@ -725,6 +728,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
725728
}
726729
}
727730

731+
fn epoll_create1(&mut self, flags: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
732+
let this = self.eval_context_mut();
733+
734+
let flags = this.read_scalar(flags)?.to_i32()?;
735+
736+
let o_cloexec = this.eval_libc_i32("O_CLOEXEC")?;
737+
// TODO actually do the thing
738+
if flags == o_cloexec {
739+
} else if flags != 0 {
740+
let einval = this.eval_libc("EINVAL")?;
741+
this.set_last_error(einval)?;
742+
return Ok(-1);
743+
}
744+
745+
let fh = &mut this.machine.file_handler;
746+
let fd = fh.insert_fd(Box::new(Epoll));
747+
Ok(fd)
748+
}
749+
728750
fn read(&mut self, fd: i32, buf: Pointer<Option<Tag>>, count: u64) -> InterpResult<'tcx, i64> {
729751
let this = self.eval_context_mut();
730752

src/shims/unix/fs/epoll.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// use crate::shims::unix::fs::FileDescriptor;
2+
// use crate::shims::unix::fs::SeekFrom;
3+
use crate::*;
4+
5+
use super::FileDescriptor;
6+
use super::FileHandle;
7+
8+
use std::io;
9+
use std::io::SeekFrom;
10+
11+
#[derive(Debug)]
12+
pub struct Epoll;
13+
14+
impl FileDescriptor for Epoll {
15+
fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> {
16+
throw_unsup_format!("stderr and stdout cannot be used as FileHandle");
17+
}
18+
19+
fn read<'tcx>(
20+
&mut self,
21+
_communicate_allowed: bool,
22+
_bytes: &mut [u8],
23+
) -> InterpResult<'tcx, io::Result<usize>> {
24+
throw_unsup_format!("cannot read from stderr or stdout");
25+
}
26+
27+
fn write<'tcx>(
28+
&self,
29+
_communicate_allowed: bool,
30+
bytes: &[u8],
31+
) -> InterpResult<'tcx, io::Result<usize>> {
32+
// We just don't write anything, but report to the user that we did.
33+
Ok(Ok(bytes.len()))
34+
}
35+
36+
fn seek<'tcx>(
37+
&mut self,
38+
_communicate_allowed: bool,
39+
_offset: SeekFrom,
40+
) -> InterpResult<'tcx, io::Result<u64>> {
41+
throw_unsup_format!("cannot seek on stderr or stdout");
42+
}
43+
44+
fn close<'tcx>(
45+
self: Box<Self>,
46+
_communicate_allowed: bool,
47+
) -> InterpResult<'tcx, io::Result<i32>> {
48+
throw_unsup_format!("stderr and stdout cannot be closed");
49+
}
50+
51+
fn dup<'tcx>(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
52+
Ok(Box::new(Epoll))
53+
}
54+
}

0 commit comments

Comments
 (0)