Skip to content

Commit

Permalink
Move read_byte_helper_ev to eventfd.rs and fix minor bug caused by re…
Browse files Browse the repository at this point in the history
…base
  • Loading branch information
tiif committed Sep 11, 2024
1 parent 93270c1 commit 11e79a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
35 changes: 0 additions & 35 deletions src/shims/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {

let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?.to_owned();
// We temporarily dup the FD to be able to retain mutable access to `this`.
let Some(fd) = this.machine.fds.get(fd_num) else {
let res = this.fd_not_found()?;
this.write_scalar(Scalar::from_target_isize(res, this), dest)?;
let Some(fd) = this.machine.fds.get(fd_num) else {
let res: i32 = this.fd_not_found()?;
this.write_int(res, dest)?;
Expand Down Expand Up @@ -679,38 +676,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
}

/// This function either writes to the user supplied buffer and to dest place, or return error.
// TODO: this is only used for eventfd
fn read_byte_helper_ev(
&mut self,
buf_place: &MPlaceTy<'tcx>,
read_val: Option<u64>,
result: io::Result<usize>,
dest: &MPlaceTy<'tcx>,
) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
// `File::read` never returns a value larger than `count`, so this cannot fail.
match result.map(|c| i64::try_from(c).unwrap()) {
// try to pass this the write_ptr inside write
// Pass the pointer inside the write function.
Ok(read_bytes) => {
// If reading to `bytes` did not fail, we write those bytes to the buffer.
// Crucially, if fewer than `bytes.len()` bytes were read, only write
// that much into the output buffer!
// Write to the user supplied buffer.
this.write_int(read_val.unwrap(), buf_place)?;
// Write to the function return value place.
this.write_int(read_bytes, dest)?;
return Ok(());
}
Err(e) => {
this.set_last_error_from_io_error(e)?;
this.write_int(-1, dest)?;
return Ok(());
}
}
}

/// This function either writes the number of written bytes to dest place or return error.
fn write_byte_helper(
&mut self,
Expand Down
30 changes: 27 additions & 3 deletions src/shims/unix/linux/eventfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl FileDescription for Event {
// Check the size of slice, and return error only if the size of the slice < 8.
if len < U64_ARRAY_SIZE.try_into().unwrap() {
let result = Err(Error::from(ErrorKind::InvalidInput));
ecx.read_byte_helper_ev(&buf_place, None, result, dest)?;
read_byte_helper_ev(&buf_place, None, result, dest, ecx)?;
return Ok(());
}

Expand All @@ -81,7 +81,7 @@ impl FileDescription for Event {
if counter == 0 {
if self.is_nonblock {
let result = Err(Error::from(ErrorKind::WouldBlock));
ecx.read_byte_helper_ev(&buf_place, None, result, dest)?;
read_byte_helper_ev(&buf_place, None, result, dest, ecx)?;
return Ok(());
} else {
//FIXME: blocking is not supported
Expand All @@ -91,7 +91,7 @@ impl FileDescription for Event {
// Synchronize with all prior `write` calls to this FD.
ecx.acquire_clock(&self.clock.borrow());
let result = Ok(U64_ARRAY_SIZE);
ecx.read_byte_helper_ev(&buf_place, Some(counter), result, dest)?;
read_byte_helper_ev(&buf_place, Some(counter), result, dest, ecx)?;
self.counter.set(0);
// When any of the event happened, we check and update the status of all supported event
// types for current file description.
Expand Down Expand Up @@ -230,3 +230,27 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(Scalar::from_i32(fd_value))
}
}

/// This function either writes to the user supplied buffer and to dest place, or return error.
fn read_byte_helper_ev<'tcx>(
buf_place: &MPlaceTy<'tcx>,
read_val: Option<u64>,
result: io::Result<usize>,
dest: &MPlaceTy<'tcx>,
ecx: &mut MiriInterpCx<'tcx>,
) -> InterpResult<'tcx> {
match result.map(|c| i64::try_from(c).unwrap()) {
Ok(read_bytes) => {
// Write to the user supplied buffer.
ecx.write_int(read_val.unwrap(), buf_place)?;
// Write to the function return value place.
ecx.write_int(read_bytes, dest)?;
return Ok(());
}
Err(e) => {
ecx.set_last_error_from_io_error(e)?;
ecx.write_int(-1, dest)?;
return Ok(());
}
}
}

0 comments on commit 11e79a0

Please sign in to comment.