file descriptors: make write take &mut self

This commit is contained in:
Ralf Jung 2024-04-28 09:38:54 +02:00
parent 45d93945ad
commit b5482aad01
3 changed files with 15 additions and 19 deletions

View File

@ -25,7 +25,7 @@ fn read<'tcx>(
}
fn write<'tcx>(
&self,
&mut self,
_communicate_allowed: bool,
_bytes: &[u8],
_tcx: TyCtxt<'tcx>,
@ -103,13 +103,13 @@ fn name(&self) -> &'static str {
}
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 @@ fn name(&self) -> &'static str {
}
fn write<'tcx>(
&self,
&mut self,
_communicate_allowed: bool,
bytes: &[u8],
_tcx: TyCtxt<'tcx>,
@ -164,7 +164,7 @@ fn name(&self) -> &'static str {
}
fn write<'tcx>(
&self,
&mut self,
_communicate_allowed: bool,
bytes: &[u8],
_tcx: TyCtxt<'tcx>,
@ -418,10 +418,10 @@ fn write(
.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 {

View File

@ -39,13 +39,13 @@ fn read<'tcx>(
}
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>(

View File

@ -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 @@
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 @@ fn name(&self) -> &'static str {
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 @@ fn close<'tcx>(
/// 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 @@ fn write<'tcx>(
};
// 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 @@ fn eventfd(
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))
}
}