Skip to content

Commit bc8ea93

Browse files
committed
Auto merge of rust-lang#3524 - RalfJung:fd-write, r=RalfJung
file descriptors: make write take &mut self
2 parents 5c6f95c + b5482aa commit bc8ea93

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub trait FileDescriptor: std::fmt::Debug + Any {
2525
}
2626

2727
fn write<'tcx>(
28-
&self,
28+
&mut self,
2929
_communicate_allowed: bool,
3030
_bytes: &[u8],
3131
_tcx: TyCtxt<'tcx>,
@@ -103,13 +103,13 @@ impl FileDescriptor for io::Stdout {
103103
}
104104

105105
fn write<'tcx>(
106-
&self,
106+
&mut self,
107107
_communicate_allowed: bool,
108108
bytes: &[u8],
109109
_tcx: TyCtxt<'tcx>,
110110
) -> InterpResult<'tcx, io::Result<usize>> {
111111
// We allow writing to stderr even with isolation enabled.
112-
let result = Write::write(&mut { self }, bytes);
112+
let result = Write::write(self, bytes);
113113
// Stdout is buffered, flush to make sure it appears on the
114114
// screen. This is the write() syscall of the interpreted
115115
// program, we want it to correspond to a write() syscall on
@@ -135,7 +135,7 @@ impl FileDescriptor for io::Stderr {
135135
}
136136

137137
fn write<'tcx>(
138-
&self,
138+
&mut self,
139139
_communicate_allowed: bool,
140140
bytes: &[u8],
141141
_tcx: TyCtxt<'tcx>,
@@ -164,7 +164,7 @@ impl FileDescriptor for NullOutput {
164164
}
165165

166166
fn write<'tcx>(
167-
&self,
167+
&mut self,
168168
_communicate_allowed: bool,
169169
bytes: &[u8],
170170
_tcx: TyCtxt<'tcx>,
@@ -418,10 +418,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
418418
.min(u64::try_from(isize::MAX).unwrap());
419419
let communicate = this.machine.communicate();
420420

421-
if let Some(file_descriptor) = this.machine.fds.get(fd) {
422-
let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?;
421+
let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?.to_owned();
422+
if let Some(file_descriptor) = this.machine.fds.get_mut(fd) {
423423
let result = file_descriptor
424-
.write(communicate, bytes, *this.tcx)?
424+
.write(communicate, &bytes, *this.tcx)?
425425
.map(|c| i64::try_from(c).unwrap());
426426
this.try_unwrap_io_result(result)
427427
} else {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ impl FileDescriptor for FileHandle {
3939
}
4040

4141
fn write<'tcx>(
42-
&self,
42+
&mut self,
4343
communicate_allowed: bool,
4444
bytes: &[u8],
4545
_tcx: TyCtxt<'tcx>,
4646
) -> InterpResult<'tcx, io::Result<usize>> {
4747
assert!(communicate_allowed, "isolation should have prevented even opening a file");
48-
Ok((&mut &self.file).write(bytes))
48+
Ok(self.file.write(bytes))
4949
}
5050

5151
fn seek<'tcx>(

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Linux `eventfd` implementation.
22
//! Currently just a stub.
3-
use std::cell::Cell;
43
use std::io;
54

65
use rustc_middle::ty::TyCtxt;
@@ -20,7 +19,7 @@ use crate::*;
2019
struct Event {
2120
/// The object contains an unsigned 64-bit integer (uint64_t) counter that is maintained by the
2221
/// kernel. This counter is initialized with the value specified in the argument initval.
23-
val: Cell<u64>,
22+
val: u64,
2423
}
2524

2625
impl FileDescriptor for Event {
@@ -30,7 +29,7 @@ impl FileDescriptor for Event {
3029

3130
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
3231
// FIXME: this is wrong, the new and old FD should refer to the same event object!
33-
Ok(Box::new(Event { val: self.val.clone() }))
32+
Ok(Box::new(Event { val: self.val }))
3433
}
3534

3635
fn close<'tcx>(
@@ -53,12 +52,11 @@ impl FileDescriptor for Event {
5352
/// supplied buffer is less than 8 bytes, or if an attempt is
5453
/// made to write the value 0xffffffffffffffff.
5554
fn write<'tcx>(
56-
&self,
55+
&mut self,
5756
_communicate_allowed: bool,
5857
bytes: &[u8],
5958
tcx: TyCtxt<'tcx>,
6059
) -> InterpResult<'tcx, io::Result<usize>> {
61-
let v1 = self.val.get();
6260
let bytes: [u8; 8] = bytes.try_into().unwrap(); // FIXME fail gracefully when this has the wrong size
6361
// Convert from target endianness to host endianness.
6462
let num = match tcx.sess.target.endian {
@@ -67,9 +65,7 @@ impl FileDescriptor for Event {
6765
};
6866
// FIXME handle blocking when addition results in exceeding the max u64 value
6967
// or fail with EAGAIN if the file descriptor is nonblocking.
70-
let v2 = v1.checked_add(num).unwrap();
71-
self.val.set(v2);
72-
assert_eq!(8, bytes.len());
68+
self.val = self.val.checked_add(num).unwrap();
7369
Ok(Ok(8))
7470
}
7571
}
@@ -119,7 +115,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
119115
throw_unsup_format!("eventfd: EFD_SEMAPHORE is unsupported");
120116
}
121117

122-
let fd = this.machine.fds.insert_fd(Box::new(Event { val: Cell::new(val.into()) }));
118+
let fd = this.machine.fds.insert_fd(Box::new(Event { val: val.into() }));
123119
Ok(Scalar::from_i32(fd))
124120
}
125121
}

0 commit comments

Comments
 (0)