Skip to content

Commit 50d51fb

Browse files
committed
Auto merge of rust-lang#3778 - RalfJung:fd, r=oli-obk
FD: refactor API names a little I feel like these two functions are different enough that their names should not indicate complete symmetry. I am not sure what the best names would be, though... `@oli-obk` any opinions?
2 parents f1e8c54 + 6f48edf commit 50d51fb

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

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

+17-17
Original file line numberDiff line numberDiff line change
@@ -240,25 +240,25 @@ impl FdTable {
240240
}
241241
pub(crate) fn init(mute_stdout_stderr: bool) -> FdTable {
242242
let mut fds = FdTable::new();
243-
fds.insert_fd(io::stdin());
243+
fds.insert_new(io::stdin());
244244
if mute_stdout_stderr {
245-
assert_eq!(fds.insert_fd(NullOutput), 1);
246-
assert_eq!(fds.insert_fd(NullOutput), 2);
245+
assert_eq!(fds.insert_new(NullOutput), 1);
246+
assert_eq!(fds.insert_new(NullOutput), 2);
247247
} else {
248-
assert_eq!(fds.insert_fd(io::stdout()), 1);
249-
assert_eq!(fds.insert_fd(io::stderr()), 2);
248+
assert_eq!(fds.insert_new(io::stdout()), 1);
249+
assert_eq!(fds.insert_new(io::stderr()), 2);
250250
}
251251
fds
252252
}
253253

254254
/// Insert a new file description to the FdTable.
255-
pub fn insert_fd(&mut self, fd: impl FileDescription) -> i32 {
255+
pub fn insert_new(&mut self, fd: impl FileDescription) -> i32 {
256256
let file_handle = FileDescriptionRef::new(fd);
257-
self.insert_fd_with_min_fd(file_handle, 0)
257+
self.insert_ref_with_min_fd(file_handle, 0)
258258
}
259259

260-
/// Insert a new FD that is at least `min_fd`.
261-
fn insert_fd_with_min_fd(&mut self, file_handle: FileDescriptionRef, min_fd: i32) -> i32 {
260+
/// Insert a file description, giving it a file descriptor that is at least `min_fd`.
261+
fn insert_ref_with_min_fd(&mut self, file_handle: FileDescriptionRef, min_fd: i32) -> i32 {
262262
// Find the lowest unused FD, starting from min_fd. If the first such unused FD is in
263263
// between used FDs, the find_map combinator will return it. If the first such unused FD
264264
// is after all other used FDs, the find_map combinator will return None, and we will use
@@ -294,7 +294,7 @@ impl FdTable {
294294
Some(fd.borrow_mut())
295295
}
296296

297-
pub fn dup(&self, fd: i32) -> Option<FileDescriptionRef> {
297+
pub fn get_ref(&self, fd: i32) -> Option<FileDescriptionRef> {
298298
let fd = self.fds.get(&fd)?;
299299
Some(fd.clone())
300300
}
@@ -313,16 +313,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
313313
fn dup(&mut self, old_fd: i32) -> InterpResult<'tcx, Scalar> {
314314
let this = self.eval_context_mut();
315315

316-
let Some(dup_fd) = this.machine.fds.dup(old_fd) else {
316+
let Some(dup_fd) = this.machine.fds.get_ref(old_fd) else {
317317
return Ok(Scalar::from_i32(this.fd_not_found()?));
318318
};
319-
Ok(Scalar::from_i32(this.machine.fds.insert_fd_with_min_fd(dup_fd, 0)))
319+
Ok(Scalar::from_i32(this.machine.fds.insert_ref_with_min_fd(dup_fd, 0)))
320320
}
321321

322322
fn dup2(&mut self, old_fd: i32, new_fd: i32) -> InterpResult<'tcx, Scalar> {
323323
let this = self.eval_context_mut();
324324

325-
let Some(dup_fd) = this.machine.fds.dup(old_fd) else {
325+
let Some(dup_fd) = this.machine.fds.get_ref(old_fd) else {
326326
return Ok(Scalar::from_i32(this.fd_not_found()?));
327327
};
328328
if new_fd != old_fd {
@@ -408,9 +408,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
408408
}
409409
let start = this.read_scalar(&args[2])?.to_i32()?;
410410

411-
match this.machine.fds.dup(fd) {
411+
match this.machine.fds.get_ref(fd) {
412412
Some(dup_fd) =>
413-
Ok(Scalar::from_i32(this.machine.fds.insert_fd_with_min_fd(dup_fd, start))),
413+
Ok(Scalar::from_i32(this.machine.fds.insert_ref_with_min_fd(dup_fd, start))),
414414
None => Ok(Scalar::from_i32(this.fd_not_found()?)),
415415
}
416416
} else if this.tcx.sess.target.os == "macos" && cmd == this.eval_libc_i32("F_FULLFSYNC") {
@@ -481,7 +481,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
481481
let communicate = this.machine.communicate();
482482

483483
// We temporarily dup the FD to be able to retain mutable access to `this`.
484-
let Some(fd) = this.machine.fds.dup(fd) else {
484+
let Some(fd) = this.machine.fds.get_ref(fd) else {
485485
trace!("read: FD not found");
486486
return Ok(Scalar::from_target_isize(this.fd_not_found()?, this));
487487
};
@@ -546,7 +546,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
546546

547547
let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?.to_owned();
548548
// We temporarily dup the FD to be able to retain mutable access to `this`.
549-
let Some(fd) = this.machine.fds.dup(fd) else {
549+
let Some(fd) = this.machine.fds.get_ref(fd) else {
550550
return Ok(Scalar::from_target_isize(this.fd_not_found()?, this));
551551
};
552552

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
545545

546546
let fd = options
547547
.open(path)
548-
.map(|file| this.machine.fds.insert_fd(FileHandle { file, writable }));
548+
.map(|file| this.machine.fds.insert_new(FileHandle { file, writable }));
549549

550550
Ok(Scalar::from_i32(this.try_unwrap_io_result(fd)?))
551551
}
@@ -1634,7 +1634,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
16341634

16351635
match file {
16361636
Ok(f) => {
1637-
let fd = this.machine.fds.insert_fd(FileHandle { file: f, writable: true });
1637+
let fd = this.machine.fds.insert_new(FileHandle { file: f, writable: true });
16381638
return Ok(Scalar::from_i32(fd));
16391639
}
16401640
Err(e) =>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
6464
);
6565
}
6666

67-
let fd = this.machine.fds.insert_fd(Epoll::default());
67+
let fd = this.machine.fds.insert_new(Epoll::default());
6868
Ok(Scalar::from_i32(fd))
6969
}
7070

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
178178
throw_unsup_format!("eventfd: encountered unknown unsupported flags {:#x}", flags);
179179
}
180180

181-
let fd = this.machine.fds.insert_fd(Event {
181+
let fd = this.machine.fds.insert_new(Event {
182182
counter: val.into(),
183183
is_nonblock,
184184
clock: VClock::default(),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
219219
};
220220

221221
let fds = &mut this.machine.fds;
222-
let sv0 = fds.insert_fd(socketpair_0);
223-
let sv1 = fds.insert_fd(socketpair_1);
222+
let sv0 = fds.insert_new(socketpair_0);
223+
let sv1 = fds.insert_new(socketpair_1);
224224
let sv0 = Scalar::from_int(sv0, sv.layout.size);
225225
let sv1 = Scalar::from_int(sv1, sv.layout.size);
226226

0 commit comments

Comments
 (0)