Skip to content

Commit fe6bc02

Browse files
committed
avoid using the address of an FD; now that we sort by that it may be user-visible
1 parent fc4748c commit fe6bc02

File tree

5 files changed

+25
-37
lines changed

5 files changed

+25
-37
lines changed

src/tools/miri/src/shims/files.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ impl<T: ?Sized> FileDescriptionRef<T> {
4848
pub fn id(&self) -> FdId {
4949
self.0.id
5050
}
51-
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()
56-
}
5751
}
5852

5953
/// Holds a weak reference to the actual file description.
@@ -76,11 +70,6 @@ impl<T: ?Sized> WeakFileDescriptionRef<T> {
7670
pub fn upgrade(&self) -> Option<FileDescriptionRef<T>> {
7771
self.0.upgrade().map(FileDescriptionRef)
7872
}
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-
}
8473
}
8574

8675
impl<T> VisitProvenance for WeakFileDescriptionRef<T> {
@@ -116,13 +105,12 @@ impl<T: FileDescription + 'static> FileDescriptionExt for T {
116105
communicate_allowed: bool,
117106
ecx: &mut MiriInterpCx<'tcx>,
118107
) -> InterpResult<'tcx, io::Result<()>> {
119-
let addr = self.addr();
120108
match Rc::into_inner(self.0) {
121109
Some(fd) => {
122110
// There might have been epolls interested in this FD. Remove that.
123111
ecx.machine.epoll_interests.remove_epolls(fd.id);
124112

125-
fd.inner.destroy(addr, communicate_allowed, ecx)
113+
fd.inner.destroy(fd.id, communicate_allowed, ecx)
126114
}
127115
None => {
128116
// Not the last reference.
@@ -200,7 +188,7 @@ pub trait FileDescription: std::fmt::Debug + FileDescriptionExt {
200188
/// `self_addr` is the address that this file description used to be stored at.
201189
fn destroy<'tcx>(
202190
self,
203-
_self_addr: usize,
191+
_self_id: FdId,
204192
_communicate_allowed: bool,
205193
_ecx: &mut MiriInterpCx<'tcx>,
206194
) -> InterpResult<'tcx, io::Result<()>>
@@ -379,7 +367,7 @@ impl FileDescription for FileHandle {
379367

380368
fn destroy<'tcx>(
381369
self,
382-
_self_addr: usize,
370+
_self_id: FdId,
383371
communicate_allowed: bool,
384372
_ecx: &mut MiriInterpCx<'tcx>,
385373
) -> InterpResult<'tcx, io::Result<()>> {

src/tools/miri/src/shims/unix/linux_like/epoll.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ impl FileDescription for Epoll {
142142

143143
fn destroy<'tcx>(
144144
mut self,
145-
self_addr: usize,
145+
self_id: FdId,
146146
_communicate_allowed: bool,
147147
ecx: &mut MiriInterpCx<'tcx>,
148148
) -> InterpResult<'tcx, io::Result<()>> {
149149
// If we were interested in some FDs, we can remove that now.
150150
let mut ids = self.interest_list.get_mut().keys().map(|(id, _num)| *id).collect::<Vec<_>>();
151151
ids.dedup(); // they come out of the map sorted
152152
for id in ids {
153-
ecx.machine.epoll_interests.remove(id, self_addr);
153+
ecx.machine.epoll_interests.remove(id, self_id);
154154
}
155155
interp_ok(Ok(()))
156156
}
@@ -164,37 +164,38 @@ impl UnixFileDescription for Epoll {}
164164

165165
/// The table of all EpollEventInterest.
166166
/// This tracks, for each file description, which epoll instances have an interest in events
167-
/// for this file description. The `Vec` is sorted by `addr`!
168-
pub struct EpollInterestTable(BTreeMap<FdId, Vec<WeakFileDescriptionRef<Epoll>>>);
167+
/// for this file description. The `FdId` is the ID of the epoll instance, so that we can recognize
168+
/// it later when it is slated for removal. The vector is sorted by that ID.
169+
pub struct EpollInterestTable(BTreeMap<FdId, Vec<(FdId, WeakFileDescriptionRef<Epoll>)>>);
169170

170171
impl EpollInterestTable {
171172
pub(crate) fn new() -> Self {
172173
EpollInterestTable(BTreeMap::new())
173174
}
174175

175-
fn insert(&mut self, id: FdId, epoll: WeakFileDescriptionRef<Epoll>) {
176+
fn insert(&mut self, id: FdId, epoll: &FileDescriptionRef<Epoll>) {
176177
let epolls = self.0.entry(id).or_default();
177178
let idx = epolls
178-
.binary_search_by_key(&epoll.addr(), |e| e.addr())
179+
.binary_search_by_key(&epoll.id(), |&(id, _)| id)
179180
.expect_err("trying to add an epoll that's already in the list");
180-
epolls.insert(idx, epoll);
181+
epolls.insert(idx, (epoll.id(), FileDescriptionRef::downgrade(epoll)));
181182
}
182183

183-
fn remove(&mut self, id: FdId, epoll_addr: usize) {
184+
fn remove(&mut self, id: FdId, epoll_id: FdId) {
184185
let epolls = self.0.entry(id).or_default();
185186
let idx = epolls
186-
.binary_search_by_key(&epoll_addr, |e| e.addr())
187+
.binary_search_by_key(&epoll_id, |&(id, _)| id)
187188
.expect("trying to remove an epoll that's not in the list");
188189
epolls.remove(idx);
189190
}
190191

191-
fn get_epolls(&self, id: FdId) -> Option<&Vec<WeakFileDescriptionRef<Epoll>>> {
192-
self.0.get(&id)
192+
fn get_epolls(&self, id: FdId) -> Option<impl Iterator<Item = &WeakFileDescriptionRef<Epoll>>> {
193+
self.0.get(&id).map(|epolls| epolls.iter().map(|(_id, epoll)| epoll))
193194
}
194195

195196
pub fn remove_epolls(&mut self, id: FdId) {
196197
if let Some(epolls) = self.0.remove(&id) {
197-
for epoll in epolls.iter().filter_map(|e| e.upgrade()) {
198+
for epoll in epolls.iter().filter_map(|(_id, epoll)| epoll.upgrade()) {
198199
// This is a still-live epoll with interest in this FD. Remove all
199200
// relevent interests.
200201
epoll
@@ -348,7 +349,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
348349
if interest_list.range(range_for_id(id)).next().is_none() {
349350
// This is the first time this FD got added to this epoll.
350351
// Remember that in the global list so we get notified about FD events.
351-
this.machine.epoll_interests.insert(id, FileDescriptionRef::downgrade(&epfd));
352+
this.machine.epoll_interests.insert(id, &epfd);
352353
}
353354
match interest_list.entry(epoll_key) {
354355
btree_map::Entry::Occupied(_) => {
@@ -388,7 +389,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
388389
// If this was the last interest in this FD, remove us from the global list
389390
// of who is interested in this FD.
390391
if interest_list.range(range_for_id(id)).next().is_none() {
391-
this.machine.epoll_interests.remove(id, epfd.addr());
392+
this.machine.epoll_interests.remove(id, epfd.id());
392393
}
393394

394395
// Remove related event instance from ready list.
@@ -541,7 +542,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
541542
return interp_ok(());
542543
};
543544
let epolls = epolls
544-
.iter()
545545
.map(|weak| {
546546
weak.upgrade()
547547
.expect("someone forgot to remove the garbage from `machine.epoll_interests`")

src/tools/miri/src/shims/unix/linux_like/eventfd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io;
44
use std::io::ErrorKind;
55

66
use crate::concurrency::VClock;
7-
use crate::shims::files::{FileDescription, FileDescriptionRef, WeakFileDescriptionRef};
7+
use crate::shims::files::{FdId, FileDescription, FileDescriptionRef, WeakFileDescriptionRef};
88
use crate::shims::unix::UnixFileDescription;
99
use crate::shims::unix::linux_like::epoll::{EpollReadyEvents, EvalContextExt as _};
1010
use crate::*;
@@ -39,7 +39,7 @@ impl FileDescription for EventFd {
3939

4040
fn destroy<'tcx>(
4141
self,
42-
_self_addr: usize,
42+
_self_id: FdId,
4343
_communicate_allowed: bool,
4444
_ecx: &mut MiriInterpCx<'tcx>,
4545
) -> InterpResult<'tcx, io::Result<()>> {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::io::ErrorKind;
99

1010
use crate::concurrency::VClock;
1111
use crate::shims::files::{
12-
EvalContextExt as _, FileDescription, FileDescriptionRef, WeakFileDescriptionRef,
12+
EvalContextExt as _, FdId, FileDescription, FileDescriptionRef, WeakFileDescriptionRef,
1313
};
1414
use crate::shims::unix::UnixFileDescription;
1515
use crate::shims::unix::linux_like::epoll::{EpollReadyEvents, EvalContextExt as _};
@@ -84,7 +84,7 @@ impl FileDescription for AnonSocket {
8484

8585
fn destroy<'tcx>(
8686
self,
87-
_self_addr: usize,
87+
_self_id: FdId,
8888
_communicate_allowed: bool,
8989
ecx: &mut MiriInterpCx<'tcx>,
9090
) -> InterpResult<'tcx, io::Result<()>> {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::SystemTime;
66

77
use bitflags::bitflags;
88

9-
use crate::shims::files::{FileDescription, FileHandle};
9+
use crate::shims::files::{FdId, FileDescription, FileHandle};
1010
use crate::shims::windows::handle::{EvalContextExt as _, Handle};
1111
use crate::*;
1212

@@ -26,7 +26,7 @@ impl FileDescription for DirHandle {
2626

2727
fn destroy<'tcx>(
2828
self,
29-
_self_addr: usize,
29+
_self_id: FdId,
3030
_communicate_allowed: bool,
3131
_ecx: &mut MiriInterpCx<'tcx>,
3232
) -> InterpResult<'tcx, io::Result<()>> {
@@ -53,7 +53,7 @@ impl FileDescription for MetadataHandle {
5353

5454
fn destroy<'tcx>(
5555
self,
56-
_self_addr: usize,
56+
_self_id: FdId,
5757
_communicate_allowed: bool,
5858
_ecx: &mut MiriInterpCx<'tcx>,
5959
) -> InterpResult<'tcx, io::Result<()>> {

0 commit comments

Comments
 (0)