about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-28 08:00:44 +0000
committerbors <bors@rust-lang.org>2024-04-28 08:00:44 +0000
commitbc8ea9324c6fbcb4705b6cfef5fccde95a39db3e (patch)
treebef62edc161048c95240f6b7effd9d98f6dc8a6b /src
parent5c6f95ca30d62cac626f3a1ea2674a425e8bf293 (diff)
parentb5482aad01e1bc48a5eacd1e1244470bfb2ba09f (diff)
downloadrust-bc8ea9324c6fbcb4705b6cfef5fccde95a39db3e.tar.gz
rust-bc8ea9324c6fbcb4705b6cfef5fccde95a39db3e.zip
Auto merge of #3524 - RalfJung:fd-write, r=RalfJung
file descriptors: make write take &mut self
Diffstat (limited to 'src')
-rw-r--r--src/tools/miri/src/shims/unix/fd.rs16
-rw-r--r--src/tools/miri/src/shims/unix/fs.rs4
-rw-r--r--src/tools/miri/src/shims/unix/linux/eventfd.rs14
3 files changed, 15 insertions, 19 deletions
diff --git a/src/tools/miri/src/shims/unix/fd.rs b/src/tools/miri/src/shims/unix/fd.rs
index a5fe38b902d..18a41f6c667 100644
--- a/src/tools/miri/src/shims/unix/fd.rs
+++ b/src/tools/miri/src/shims/unix/fd.rs
@@ -25,7 +25,7 @@ pub trait FileDescriptor: std::fmt::Debug + Any {
     }
 
     fn write<'tcx>(
-        &self,
+        &mut self,
         _communicate_allowed: bool,
         _bytes: &[u8],
         _tcx: TyCtxt<'tcx>,
@@ -103,13 +103,13 @@ impl FileDescriptor for io::Stdout {
     }
 
     fn write<'tcx>(
-        &self,
+        &mut self,
         _communicate_allowed: bool,
         bytes: &[u8],
         _tcx: TyCtxt<'tcx>,
     ) -> InterpResult<'tcx, io::Result<usize>> {
         // We allow writing to stderr even with isolation enabled.
-        let result = Write::write(&mut { self }, bytes);
+        let result = Write::write(self, bytes);
         // Stdout is buffered, flush to make sure it appears on the
         // screen.  This is the write() syscall of the interpreted
         // program, we want it to correspond to a write() syscall on
@@ -135,7 +135,7 @@ impl FileDescriptor for io::Stderr {
     }
 
     fn write<'tcx>(
-        &self,
+        &mut self,
         _communicate_allowed: bool,
         bytes: &[u8],
         _tcx: TyCtxt<'tcx>,
@@ -164,7 +164,7 @@ impl FileDescriptor for NullOutput {
     }
 
     fn write<'tcx>(
-        &self,
+        &mut self,
         _communicate_allowed: bool,
         bytes: &[u8],
         _tcx: TyCtxt<'tcx>,
@@ -418,10 +418,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
             .min(u64::try_from(isize::MAX).unwrap());
         let communicate = this.machine.communicate();
 
-        if let Some(file_descriptor) = this.machine.fds.get(fd) {
-            let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?;
+        let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?.to_owned();
+        if let Some(file_descriptor) = this.machine.fds.get_mut(fd) {
             let result = file_descriptor
-                .write(communicate, bytes, *this.tcx)?
+                .write(communicate, &bytes, *this.tcx)?
                 .map(|c| i64::try_from(c).unwrap());
             this.try_unwrap_io_result(result)
         } else {
diff --git a/src/tools/miri/src/shims/unix/fs.rs b/src/tools/miri/src/shims/unix/fs.rs
index ebf9f43c19e..0bf0e3d52c3 100644
--- a/src/tools/miri/src/shims/unix/fs.rs
+++ b/src/tools/miri/src/shims/unix/fs.rs
@@ -39,13 +39,13 @@ impl FileDescriptor for FileHandle {
     }
 
     fn write<'tcx>(
-        &self,
+        &mut self,
         communicate_allowed: bool,
         bytes: &[u8],
         _tcx: TyCtxt<'tcx>,
     ) -> InterpResult<'tcx, io::Result<usize>> {
         assert!(communicate_allowed, "isolation should have prevented even opening a file");
-        Ok((&mut &self.file).write(bytes))
+        Ok(self.file.write(bytes))
     }
 
     fn seek<'tcx>(
diff --git a/src/tools/miri/src/shims/unix/linux/eventfd.rs b/src/tools/miri/src/shims/unix/linux/eventfd.rs
index 0f28b69ac4a..452527017fe 100644
--- a/src/tools/miri/src/shims/unix/linux/eventfd.rs
+++ b/src/tools/miri/src/shims/unix/linux/eventfd.rs
@@ -1,6 +1,5 @@
 //! Linux `eventfd` implementation.
 //! Currently just a stub.
-use std::cell::Cell;
 use std::io;
 
 use rustc_middle::ty::TyCtxt;
@@ -20,7 +19,7 @@ use crate::*;
 struct Event {
     /// The object contains an unsigned 64-bit integer (uint64_t) counter that is maintained by the
     /// kernel. This counter is initialized with the value specified in the argument initval.
-    val: Cell<u64>,
+    val: u64,
 }
 
 impl FileDescriptor for Event {
@@ -30,7 +29,7 @@ impl FileDescriptor for Event {
 
     fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
         // FIXME: this is wrong, the new and old FD should refer to the same event object!
-        Ok(Box::new(Event { val: self.val.clone() }))
+        Ok(Box::new(Event { val: self.val }))
     }
 
     fn close<'tcx>(
@@ -53,12 +52,11 @@ impl FileDescriptor for Event {
     /// supplied buffer is less than 8 bytes, or if an attempt is
     /// made to write the value 0xffffffffffffffff.
     fn write<'tcx>(
-        &self,
+        &mut self,
         _communicate_allowed: bool,
         bytes: &[u8],
         tcx: TyCtxt<'tcx>,
     ) -> InterpResult<'tcx, io::Result<usize>> {
-        let v1 = self.val.get();
         let bytes: [u8; 8] = bytes.try_into().unwrap(); // FIXME fail gracefully when this has the wrong size
         // Convert from target endianness to host endianness.
         let num = match tcx.sess.target.endian {
@@ -67,9 +65,7 @@ impl FileDescriptor for Event {
         };
         // FIXME handle blocking when addition results in exceeding the max u64 value
         // or fail with EAGAIN if the file descriptor is nonblocking.
-        let v2 = v1.checked_add(num).unwrap();
-        self.val.set(v2);
-        assert_eq!(8, bytes.len());
+        self.val = self.val.checked_add(num).unwrap();
         Ok(Ok(8))
     }
 }
@@ -119,7 +115,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
             throw_unsup_format!("eventfd: EFD_SEMAPHORE is unsupported");
         }
 
-        let fd = this.machine.fds.insert_fd(Box::new(Event { val: Cell::new(val.into()) }));
+        let fd = this.machine.fds.insert_fd(Box::new(Event { val: val.into() }));
         Ok(Scalar::from_i32(fd))
     }
 }