Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 34aec7c

Browse files
committed
make ecx.check_and_update_readiness a truly private helper function
1 parent 82c39ff commit 34aec7c

File tree

5 files changed

+31
-63
lines changed

5 files changed

+31
-63
lines changed

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ pub trait FileDescription: std::fmt::Debug + Any {
2828
/// Reads as much as possible into the given buffer, and returns the number of bytes read.
2929
fn read<'tcx>(
3030
&self,
31+
_self_ref: &FileDescriptionRef,
3132
_communicate_allowed: bool,
32-
_fd_id: FdId,
3333
_bytes: &mut [u8],
3434
_ecx: &mut MiriInterpCx<'tcx>,
3535
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -39,8 +39,8 @@ pub trait FileDescription: std::fmt::Debug + Any {
3939
/// Writes as much as possible from the given buffer, and returns the number of bytes written.
4040
fn write<'tcx>(
4141
&self,
42+
_self_ref: &FileDescriptionRef,
4243
_communicate_allowed: bool,
43-
_fd_id: FdId,
4444
_bytes: &[u8],
4545
_ecx: &mut MiriInterpCx<'tcx>,
4646
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -123,8 +123,8 @@ impl FileDescription for io::Stdin {
123123

124124
fn read<'tcx>(
125125
&self,
126+
_self_ref: &FileDescriptionRef,
126127
communicate_allowed: bool,
127-
_fd_id: FdId,
128128
bytes: &mut [u8],
129129
_ecx: &mut MiriInterpCx<'tcx>,
130130
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -147,8 +147,8 @@ impl FileDescription for io::Stdout {
147147

148148
fn write<'tcx>(
149149
&self,
150+
_self_ref: &FileDescriptionRef,
150151
_communicate_allowed: bool,
151-
_fd_id: FdId,
152152
bytes: &[u8],
153153
_ecx: &mut MiriInterpCx<'tcx>,
154154
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -176,8 +176,8 @@ impl FileDescription for io::Stderr {
176176

177177
fn write<'tcx>(
178178
&self,
179+
_self_ref: &FileDescriptionRef,
179180
_communicate_allowed: bool,
180-
_fd_id: FdId,
181181
bytes: &[u8],
182182
_ecx: &mut MiriInterpCx<'tcx>,
183183
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -202,8 +202,8 @@ impl FileDescription for NullOutput {
202202

203203
fn write<'tcx>(
204204
&self,
205+
_self_ref: &FileDescriptionRef,
205206
_communicate_allowed: bool,
206-
_fd_id: FdId,
207207
bytes: &[u8],
208208
_ecx: &mut MiriInterpCx<'tcx>,
209209
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -261,16 +261,6 @@ impl FileDescriptionRef {
261261
pub fn get_id(&self) -> FdId {
262262
self.0.id
263263
}
264-
265-
/// Function used to retrieve the readiness events of a file description and insert
266-
/// an `EpollEventInstance` into the ready list if the file description is ready.
267-
pub(crate) fn check_and_update_readiness<'tcx>(
268-
&self,
269-
ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
270-
) -> InterpResult<'tcx, ()> {
271-
use crate::shims::unix::linux::epoll::EvalContextExt;
272-
ecx.check_and_update_readiness(self.get_id(), || self.get_epoll_ready_events())
273-
}
274264
}
275265

276266
/// Holds a weak reference to the actual file description.
@@ -567,7 +557,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
567557
// `usize::MAX` because it is bounded by the host's `isize`.
568558
let mut bytes = vec![0; usize::try_from(count).unwrap()];
569559
let result = match offset {
570-
None => fd.read(communicate, fd.get_id(), &mut bytes, this),
560+
None => fd.read(&fd, communicate, &mut bytes, this),
571561
Some(offset) => {
572562
let Ok(offset) = u64::try_from(offset) else {
573563
let einval = this.eval_libc("EINVAL");
@@ -625,7 +615,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
625615
};
626616

627617
let result = match offset {
628-
None => fd.write(communicate, fd.get_id(), &bytes, this),
618+
None => fd.write(&fd, communicate, &bytes, this),
629619
Some(offset) => {
630620
let Ok(offset) = u64::try_from(offset) else {
631621
let einval = this.eval_libc("EINVAL");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::fx::FxHashMap;
1212
use rustc_target::abi::Size;
1313

1414
use crate::shims::os_str::bytes_to_os_str;
15-
use crate::shims::unix::fd::FdId;
15+
use crate::shims::unix::fd::FileDescriptionRef;
1616
use crate::shims::unix::*;
1717
use crate::*;
1818
use shims::time::system_time_to_duration;
@@ -32,8 +32,8 @@ impl FileDescription for FileHandle {
3232

3333
fn read<'tcx>(
3434
&self,
35+
_self_ref: &FileDescriptionRef,
3536
communicate_allowed: bool,
36-
_fd_id: FdId,
3737
bytes: &mut [u8],
3838
_ecx: &mut MiriInterpCx<'tcx>,
3939
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -43,8 +43,8 @@ impl FileDescription for FileHandle {
4343

4444
fn write<'tcx>(
4545
&self,
46+
_self_ref: &FileDescriptionRef,
4647
communicate_allowed: bool,
47-
_fd_id: FdId,
4848
bytes: &[u8],
4949
_ecx: &mut MiriInterpCx<'tcx>,
5050
) -> InterpResult<'tcx, io::Result<usize>> {

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
33
use std::io;
44
use std::rc::{Rc, Weak};
55

6-
use crate::shims::unix::fd::FdId;
6+
use crate::shims::unix::fd::{FdId, FileDescriptionRef};
77
use crate::shims::unix::*;
88
use crate::*;
99

@@ -309,7 +309,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
309309
}
310310

311311
// Readiness will be updated immediately when the epoll_event_interest is added or modified.
312-
file_descriptor.check_and_update_readiness(this)?;
312+
this.check_and_update_readiness(&file_descriptor)?;
313313

314314
return Ok(Scalar::from_i32(0));
315315
} else if op == epoll_ctl_del {
@@ -432,22 +432,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
432432
Ok(Scalar::from_i32(num_of_events))
433433
}
434434

435-
/// For a specific unique file descriptor id, get its ready events and update
435+
/// For a specific file description, get its ready events and update
436436
/// the corresponding ready list. This function is called whenever a file description
437-
/// is registered with epoll, or when read, write, or close operations are performed,
438-
/// regardless of any changes in readiness.
439-
///
440-
/// This is an internal helper function and is typically not meant to be used directly.
441-
/// In most cases, `FileDescriptionRef::check_and_update_readiness` should be preferred.
442-
fn check_and_update_readiness(
443-
&self,
444-
id: FdId,
445-
get_ready_events: impl FnOnce() -> InterpResult<'tcx, EpollReadyEvents>,
446-
) -> InterpResult<'tcx, ()> {
437+
/// is registered with epoll, or when its readiness *might* have changed.
438+
fn check_and_update_readiness(&self, fd_ref: &FileDescriptionRef) -> InterpResult<'tcx, ()> {
447439
let this = self.eval_context_ref();
440+
let id = fd_ref.get_id();
448441
// Get a list of EpollEventInterest that is associated to a specific file description.
449442
if let Some(epoll_interests) = this.machine.epoll_interests.get_epoll_interest(id) {
450-
let epoll_ready_events = get_ready_events()?;
443+
let epoll_ready_events = fd_ref.get_epoll_ready_events()?;
451444
// Get the bitmask of ready events.
452445
let ready_events = epoll_ready_events.get_event_bitmask(this);
453446

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use std::io;
44
use std::io::{Error, ErrorKind};
55
use std::mem;
66

7-
use fd::FdId;
87
use rustc_target::abi::Endian;
98

10-
use crate::shims::unix::linux::epoll::EpollReadyEvents;
9+
use crate::shims::unix::fd::FileDescriptionRef;
10+
use crate::shims::unix::linux::epoll::{EpollReadyEvents, EvalContextExt as _};
1111
use crate::shims::unix::*;
1212
use crate::{concurrency::VClock, *};
1313

@@ -60,8 +60,8 @@ impl FileDescription for Event {
6060
/// Read the counter in the buffer and return the counter if succeeded.
6161
fn read<'tcx>(
6262
&self,
63+
self_ref: &FileDescriptionRef,
6364
_communicate_allowed: bool,
64-
fd_id: FdId,
6565
bytes: &mut [u8],
6666
ecx: &mut MiriInterpCx<'tcx>,
6767
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -89,16 +89,8 @@ impl FileDescription for Event {
8989
self.counter.set(0);
9090
// When any of the event happened, we check and update the status of all supported event
9191
// types for current file description.
92+
ecx.check_and_update_readiness(self_ref)?;
9293

93-
// We have to use our own FdID in contrast to every other file descriptor out there, because
94-
// we are updating ourselves when writing and reading. Technically `Event` is like socketpair, but
95-
// it does not create two separate file descriptors. Thus we can't re-borrow ourselves via
96-
// `FileDescriptionRef::check_and_update_readiness` while already being mutably borrowed for read/write.
97-
crate::shims::unix::linux::epoll::EvalContextExt::check_and_update_readiness(
98-
ecx,
99-
fd_id,
100-
|| self.get_epoll_ready_events(),
101-
)?;
10294
return Ok(Ok(U64_ARRAY_SIZE));
10395
}
10496
}
@@ -117,8 +109,8 @@ impl FileDescription for Event {
117109
/// made to write the value 0xffffffffffffffff.
118110
fn write<'tcx>(
119111
&self,
112+
self_ref: &FileDescriptionRef,
120113
_communicate_allowed: bool,
121-
fd_id: FdId,
122114
bytes: &[u8],
123115
ecx: &mut MiriInterpCx<'tcx>,
124116
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -156,15 +148,8 @@ impl FileDescription for Event {
156148
};
157149
// When any of the event happened, we check and update the status of all supported event
158150
// types for current file description.
151+
ecx.check_and_update_readiness(self_ref)?;
159152

160-
// Just like read() above, we use this internal method to not get the second borrow of the
161-
// RefCell of this FileDescription. This is a special case, we should only use
162-
// FileDescriptionRef::check_and_update_readiness in normal case.
163-
crate::shims::unix::linux::epoll::EvalContextExt::check_and_update_readiness(
164-
ecx,
165-
fd_id,
166-
|| self.get_epoll_ready_events(),
167-
)?;
168153
Ok(Ok(U64_ARRAY_SIZE))
169154
}
170155
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::collections::VecDeque;
33
use std::io;
44
use std::io::{Error, ErrorKind, Read};
55

6-
use crate::shims::unix::fd::{FdId, WeakFileDescriptionRef};
7-
use crate::shims::unix::linux::epoll::EpollReadyEvents;
6+
use crate::shims::unix::fd::{FileDescriptionRef, WeakFileDescriptionRef};
7+
use crate::shims::unix::linux::epoll::{EpollReadyEvents, EvalContextExt as _};
88
use crate::shims::unix::*;
99
use crate::{concurrency::VClock, *};
1010

@@ -89,15 +89,15 @@ impl FileDescription for SocketPair {
8989
// Notify peer fd that closed has happened.
9090
// When any of the events happened, we check and update the status of all supported events
9191
// types of peer fd.
92-
peer_fd.check_and_update_readiness(ecx)?;
92+
ecx.check_and_update_readiness(&peer_fd)?;
9393
}
9494
Ok(Ok(()))
9595
}
9696

9797
fn read<'tcx>(
9898
&self,
99+
_self_ref: &FileDescriptionRef,
99100
_communicate_allowed: bool,
100-
_fd_id: FdId,
101101
bytes: &mut [u8],
102102
ecx: &mut MiriInterpCx<'tcx>,
103103
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -150,16 +150,16 @@ impl FileDescription for SocketPair {
150150
// a read is successful. This might result in our epoll emulation providing more
151151
// notifications than the real system.
152152
if let Some(peer_fd) = self.peer_fd().upgrade() {
153-
peer_fd.check_and_update_readiness(ecx)?;
153+
ecx.check_and_update_readiness(&peer_fd)?;
154154
}
155155

156156
return Ok(Ok(actual_read_size));
157157
}
158158

159159
fn write<'tcx>(
160160
&self,
161+
_self_ref: &FileDescriptionRef,
161162
_communicate_allowed: bool,
162-
_fd_id: FdId,
163163
bytes: &[u8],
164164
ecx: &mut MiriInterpCx<'tcx>,
165165
) -> InterpResult<'tcx, io::Result<usize>> {
@@ -200,7 +200,7 @@ impl FileDescription for SocketPair {
200200
drop(writebuf);
201201

202202
// Notification should be provided for peer fd as it became readable.
203-
peer_fd.check_and_update_readiness(ecx)?;
203+
ecx.check_and_update_readiness(&peer_fd)?;
204204

205205
return Ok(Ok(actual_write_size));
206206
}

0 commit comments

Comments
 (0)