Skip to content

Commit

Permalink
file descriptors: make write take &mut self
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 28, 2024
1 parent f26bd28 commit c038718
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
16 changes: 8 additions & 8 deletions src/shims/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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
Expand All @@ -135,7 +135,7 @@ impl FileDescriptor for io::Stderr {
}

fn write<'tcx>(
&self,
&mut self,
_communicate_allowed: bool,
bytes: &[u8],
_tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -164,7 +164,7 @@ impl FileDescriptor for NullOutput {
}

fn write<'tcx>(
&self,
&mut self,
_communicate_allowed: bool,
bytes: &[u8],
_tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
14 changes: 5 additions & 9 deletions src/shims/unix/linux/eventfd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Linux `eventfd` implementation.
//! Currently just a stub.
use std::cell::Cell;
use std::io;

use rustc_middle::ty::TyCtxt;
Expand All @@ -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 {
Expand All @@ -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>(
Expand All @@ -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 {
Expand All @@ -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))
}
}
Expand Down Expand Up @@ -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))
}
}

0 comments on commit c038718

Please sign in to comment.