Skip to content

Commit b7294a4

Browse files
committed
a bit of FileDescriptor trait cleanup
1 parent 3f88f4c commit b7294a4

File tree

6 files changed

+35
-31
lines changed

6 files changed

+35
-31
lines changed

src/tools/miri/src/helpers.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod convert;
22

3+
use std::any::Any;
34
use std::cmp;
45
use std::iter;
56
use std::num::NonZeroUsize;
@@ -23,7 +24,23 @@ use rand::RngCore;
2324

2425
use crate::*;
2526

26-
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
27+
/// A trait to work around not having trait object upcasting:
28+
/// Add `AsAny` as supertrait and your trait objects can be turned into `&dyn Any` on which you can
29+
/// then call `downcast`.
30+
pub trait AsAny: Any {
31+
fn as_any(&self) -> &dyn Any;
32+
fn as_any_mut(&mut self) -> &mut dyn Any;
33+
}
34+
impl<T: Any> AsAny for T {
35+
#[inline(always)]
36+
fn as_any(&self) -> &dyn Any {
37+
self
38+
}
39+
#[inline(always)]
40+
fn as_any_mut(&mut self) -> &mut dyn Any {
41+
self
42+
}
43+
}
2744

2845
// This mapping should match `decode_error_kind` in
2946
// <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>)
119136
}
120137
}
121138

139+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
122140
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
123141
/// Checks if the given crate/module exists.
124142
fn have_module(&self, path: &[&str]) -> bool {

src/tools/miri/src/shims/unix/fs.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,20 @@ use crate::shims::os_str::bytes_to_os_str;
1717
use crate::*;
1818
use shims::os_str::os_str_to_bytes;
1919
use shims::time::system_time_to_duration;
20-
use shims::unix::linux::fd::epoll::Epoll;
2120

2221
#[derive(Debug)]
2322
pub struct FileHandle {
2423
file: File,
2524
writable: bool,
2625
}
2726

28-
pub trait FileDescriptor: std::fmt::Debug {
27+
pub trait FileDescriptor: std::fmt::Debug + helpers::AsAny {
2928
fn name(&self) -> &'static str;
3029

3130
fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> {
3231
throw_unsup_format!("{} cannot be used as FileHandle", self.name());
3332
}
3433

35-
fn as_epoll_handle<'tcx>(&mut self) -> InterpResult<'tcx, &mut Epoll> {
36-
throw_unsup_format!("not an epoll file descriptor");
37-
}
38-
3934
fn read<'tcx>(
4035
&mut self,
4136
_communicate_allowed: bool,
@@ -69,7 +64,9 @@ pub trait FileDescriptor: std::fmt::Debug {
6964

7065
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>>;
7166

72-
fn is_tty(&self) -> bool;
67+
fn is_tty(&self) -> bool {
68+
false
69+
}
7370

7471
#[cfg(unix)]
7572
fn as_unix_host_fd(&self) -> Option<i32> {
@@ -271,10 +268,6 @@ impl FileDescriptor for NullOutput {
271268
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
272269
Ok(Box::new(NullOutput))
273270
}
274-
275-
fn is_tty(&self) -> bool {
276-
false
277-
}
278271
}
279272

280273
#[derive(Debug)]

src/tools/miri/src/shims/unix/linux/fd.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
8080
let event = EpollEvent { events, data };
8181

8282
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
83-
let epfd = epfd.as_epoll_handle()?;
83+
let epfd = epfd
84+
.as_any_mut()
85+
.downcast_mut::<Epoll>()
86+
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;
8487

8588
epfd.file_descriptors.insert(fd, event);
8689
Ok(Scalar::from_i32(0))
@@ -89,7 +92,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
8992
}
9093
} else if op == epoll_ctl_del {
9194
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
92-
let epfd = epfd.as_epoll_handle()?;
95+
let epfd = epfd
96+
.as_any_mut()
97+
.downcast_mut::<Epoll>()
98+
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;
9399

94100
epfd.file_descriptors.remove(&fd);
95101
Ok(Scalar::from_i32(0))
@@ -148,7 +154,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
148154

149155
let numevents = 0;
150156
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
151-
let _epfd = epfd.as_epoll_handle()?;
157+
let _epfd = epfd
158+
.as_any_mut()
159+
.downcast_mut::<Epoll>()
160+
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_wait`"))?;
152161

153162
// FIXME return number of events ready when scheme for marking events ready exists
154163
Ok(Scalar::from_i32(numevents))

src/tools/miri/src/shims/unix/linux/fd/epoll.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,10 @@ impl FileDescriptor for Epoll {
3232
"epoll"
3333
}
3434

35-
fn as_epoll_handle<'tcx>(&mut self) -> InterpResult<'tcx, &mut Epoll> {
36-
Ok(self)
37-
}
38-
3935
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
4036
Ok(Box::new(self.clone()))
4137
}
4238

43-
fn is_tty(&self) -> bool {
44-
false
45-
}
46-
4739
fn close<'tcx>(
4840
self: Box<Self>,
4941
_communicate_allowed: bool,

src/tools/miri/src/shims/unix/linux/fd/event.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ impl FileDescriptor for Event {
2828
Ok(Box::new(Event { val: self.val.clone() }))
2929
}
3030

31-
fn is_tty(&self) -> bool {
32-
false
33-
}
34-
3531
fn close<'tcx>(
3632
self: Box<Self>,
3733
_communicate_allowed: bool,

src/tools/miri/src/shims/unix/linux/fd/socketpair.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ impl FileDescriptor for SocketPair {
1919
Ok(Box::new(SocketPair))
2020
}
2121

22-
fn is_tty(&self) -> bool {
23-
false
24-
}
25-
2622
fn close<'tcx>(
2723
self: Box<Self>,
2824
_communicate_allowed: bool,

0 commit comments

Comments
 (0)