Skip to content

Commit 852446e

Browse files
committed
simplify epoll data structures: dont make the interests themselves into refcounted objects
1 parent 2f07932 commit 852446e

File tree

10 files changed

+191
-319
lines changed

10 files changed

+191
-319
lines changed

src/shims/files.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::*;
1414

1515
/// A unique id for file descriptions. While we could use the address, considering that
1616
/// is definitely unique, the address would expose interpreter internal state when used
17-
/// for sorting things. So instead we generate a unique id per file description is the name
17+
/// for sorting things. So instead we generate a unique id per file description which is the same
1818
/// for all `dup`licates and is never reused.
1919
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
2020
pub struct FdId(usize);
@@ -48,25 +48,11 @@ impl<T: ?Sized> FileDescriptionRef<T> {
4848
pub fn id(&self) -> FdId {
4949
self.0.id
5050
}
51-
}
52-
53-
impl<T: ?Sized> PartialEq for FileDescriptionRef<T> {
54-
fn eq(&self, other: &Self) -> bool {
55-
self.id() == other.id()
56-
}
57-
}
58-
59-
impl<T: ?Sized> Eq for FileDescriptionRef<T> {}
6051

61-
impl<T: ?Sized> PartialOrd for FileDescriptionRef<T> {
62-
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
63-
Some(self.cmp(other))
64-
}
65-
}
66-
67-
impl<T: ?Sized> Ord for FileDescriptionRef<T> {
68-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
69-
self.id().cmp(&other.id())
52+
/// Returns the raw address of this file description. Useful for equality comparisons.
53+
/// Use `id` instead if this can affect user-visible behavior!
54+
pub fn addr(&self) -> usize {
55+
Rc::as_ptr(&self.0).addr()
7056
}
7157
}
7258

@@ -90,6 +76,11 @@ impl<T: ?Sized> WeakFileDescriptionRef<T> {
9076
pub fn upgrade(&self) -> Option<FileDescriptionRef<T>> {
9177
self.0.upgrade().map(FileDescriptionRef)
9278
}
79+
80+
/// Returns the raw address of this file description. Useful for equality comparisons.
81+
pub fn addr(&self) -> usize {
82+
self.0.as_ptr().addr()
83+
}
9384
}
9485

9586
impl<T> VisitProvenance for WeakFileDescriptionRef<T> {
@@ -125,12 +116,13 @@ impl<T: FileDescription + 'static> FileDescriptionExt for T {
125116
communicate_allowed: bool,
126117
ecx: &mut MiriInterpCx<'tcx>,
127118
) -> InterpResult<'tcx, io::Result<()>> {
119+
let addr = self.addr();
128120
match Rc::into_inner(self.0) {
129121
Some(fd) => {
130-
// Remove entry from the global epoll_event_interest table.
131-
ecx.machine.epoll_interests.remove(fd.id);
122+
// There might have been epolls interested in this FD. Remove that.
123+
ecx.machine.epoll_interests.remove_epolls(fd.id);
132124

133-
fd.inner.close(communicate_allowed, ecx)
125+
fd.inner.destroy(addr, communicate_allowed, ecx)
134126
}
135127
None => {
136128
// Not the last reference.
@@ -203,9 +195,12 @@ pub trait FileDescription: std::fmt::Debug + FileDescriptionExt {
203195
throw_unsup_format!("cannot seek on {}", self.name());
204196
}
205197

206-
/// Close the file descriptor.
207-
fn close<'tcx>(
198+
/// Destroys the file description. Only called when the last duplicate file descriptor is closed.
199+
///
200+
/// `self_addr` is the address that this file description used to be stored at.
201+
fn destroy<'tcx>(
208202
self,
203+
_self_addr: usize,
209204
_communicate_allowed: bool,
210205
_ecx: &mut MiriInterpCx<'tcx>,
211206
) -> InterpResult<'tcx, io::Result<()>>
@@ -382,8 +377,9 @@ impl FileDescription for FileHandle {
382377
interp_ok((&mut &self.file).seek(offset))
383378
}
384379

385-
fn close<'tcx>(
380+
fn destroy<'tcx>(
386381
self,
382+
_self_addr: usize,
387383
communicate_allowed: bool,
388384
_ecx: &mut MiriInterpCx<'tcx>,
389385
) -> InterpResult<'tcx, io::Result<()>> {

0 commit comments

Comments
 (0)