a bit of FileDescriptor trait cleanup

This commit is contained in:
Ralf Jung 2023-02-24 14:36:14 +01:00
parent 3f88f4ce9b
commit b7294a4305
6 changed files with 35 additions and 31 deletions

View file

@ -1,5 +1,6 @@
pub mod convert;
use std::any::Any;
use std::cmp;
use std::iter;
use std::num::NonZeroUsize;
@ -23,7 +24,23 @@ use rand::RngCore;
use crate::*;
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
/// A trait to work around not having trait object upcasting:
/// Add `AsAny` as supertrait and your trait objects can be turned into `&dyn Any` on which you can
/// then call `downcast`.
pub trait AsAny: Any {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
impl<T: Any> AsAny for T {
#[inline(always)]
fn as_any(&self) -> &dyn Any {
self
}
#[inline(always)]
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
// This mapping should match `decode_error_kind` in
// <https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/mod.rs>.
@ -119,6 +136,7 @@ fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>)
}
}
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
/// Checks if the given crate/module exists.
fn have_module(&self, path: &[&str]) -> bool {

View file

@ -17,7 +17,6 @@ use crate::shims::os_str::bytes_to_os_str;
use crate::*;
use shims::os_str::os_str_to_bytes;
use shims::time::system_time_to_duration;
use shims::unix::linux::fd::epoll::Epoll;
#[derive(Debug)]
pub struct FileHandle {
@ -25,17 +24,13 @@ pub struct FileHandle {
writable: bool,
}
pub trait FileDescriptor: std::fmt::Debug {
pub trait FileDescriptor: std::fmt::Debug + helpers::AsAny {
fn name(&self) -> &'static str;
fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> {
throw_unsup_format!("{} cannot be used as FileHandle", self.name());
}
fn as_epoll_handle<'tcx>(&mut self) -> InterpResult<'tcx, &mut Epoll> {
throw_unsup_format!("not an epoll file descriptor");
}
fn read<'tcx>(
&mut self,
_communicate_allowed: bool,
@ -69,7 +64,9 @@ pub trait FileDescriptor: std::fmt::Debug {
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>>;
fn is_tty(&self) -> bool;
fn is_tty(&self) -> bool {
false
}
#[cfg(unix)]
fn as_unix_host_fd(&self) -> Option<i32> {
@ -271,10 +268,6 @@ impl FileDescriptor for NullOutput {
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
Ok(Box::new(NullOutput))
}
fn is_tty(&self) -> bool {
false
}
}
#[derive(Debug)]

View file

@ -80,7 +80,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let event = EpollEvent { events, data };
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
let epfd = epfd.as_epoll_handle()?;
let epfd = epfd
.as_any_mut()
.downcast_mut::<Epoll>()
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;
epfd.file_descriptors.insert(fd, event);
Ok(Scalar::from_i32(0))
@ -89,7 +92,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
}
} else if op == epoll_ctl_del {
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
let epfd = epfd.as_epoll_handle()?;
let epfd = epfd
.as_any_mut()
.downcast_mut::<Epoll>()
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;
epfd.file_descriptors.remove(&fd);
Ok(Scalar::from_i32(0))
@ -148,7 +154,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let numevents = 0;
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
let _epfd = epfd.as_epoll_handle()?;
let _epfd = epfd
.as_any_mut()
.downcast_mut::<Epoll>()
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_wait`"))?;
// FIXME return number of events ready when scheme for marking events ready exists
Ok(Scalar::from_i32(numevents))

View file

@ -32,18 +32,10 @@ impl FileDescriptor for Epoll {
"epoll"
}
fn as_epoll_handle<'tcx>(&mut self) -> InterpResult<'tcx, &mut Epoll> {
Ok(self)
}
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
Ok(Box::new(self.clone()))
}
fn is_tty(&self) -> bool {
false
}
fn close<'tcx>(
self: Box<Self>,
_communicate_allowed: bool,

View file

@ -28,10 +28,6 @@ impl FileDescriptor for Event {
Ok(Box::new(Event { val: self.val.clone() }))
}
fn is_tty(&self) -> bool {
false
}
fn close<'tcx>(
self: Box<Self>,
_communicate_allowed: bool,

View file

@ -19,10 +19,6 @@ impl FileDescriptor for SocketPair {
Ok(Box::new(SocketPair))
}
fn is_tty(&self) -> bool {
false
}
fn close<'tcx>(
self: Box<Self>,
_communicate_allowed: bool,