Skip to content

Commit 11e79a0

Browse files
committed
Move read_byte_helper_ev to eventfd.rs and fix minor bug caused by rebase
1 parent 93270c1 commit 11e79a0

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

src/shims/unix/fd.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
623623

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

682-
/// This function either writes to the user supplied buffer and to dest place, or return error.
683-
// TODO: this is only used for eventfd
684-
fn read_byte_helper_ev(
685-
&mut self,
686-
buf_place: &MPlaceTy<'tcx>,
687-
read_val: Option<u64>,
688-
result: io::Result<usize>,
689-
dest: &MPlaceTy<'tcx>,
690-
) -> InterpResult<'tcx> {
691-
let this = self.eval_context_mut();
692-
// `File::read` never returns a value larger than `count`, so this cannot fail.
693-
match result.map(|c| i64::try_from(c).unwrap()) {
694-
// try to pass this the write_ptr inside write
695-
// Pass the pointer inside the write function.
696-
Ok(read_bytes) => {
697-
// If reading to `bytes` did not fail, we write those bytes to the buffer.
698-
// Crucially, if fewer than `bytes.len()` bytes were read, only write
699-
// that much into the output buffer!
700-
// Write to the user supplied buffer.
701-
this.write_int(read_val.unwrap(), buf_place)?;
702-
// Write to the function return value place.
703-
this.write_int(read_bytes, dest)?;
704-
return Ok(());
705-
}
706-
Err(e) => {
707-
this.set_last_error_from_io_error(e)?;
708-
this.write_int(-1, dest)?;
709-
return Ok(());
710-
}
711-
}
712-
}
713-
714679
/// This function either writes the number of written bytes to dest place or return error.
715680
fn write_byte_helper(
716681
&mut self,

src/shims/unix/linux/eventfd.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl FileDescription for Event {
7272
// Check the size of slice, and return error only if the size of the slice < 8.
7373
if len < U64_ARRAY_SIZE.try_into().unwrap() {
7474
let result = Err(Error::from(ErrorKind::InvalidInput));
75-
ecx.read_byte_helper_ev(&buf_place, None, result, dest)?;
75+
read_byte_helper_ev(&buf_place, None, result, dest, ecx)?;
7676
return Ok(());
7777
}
7878

@@ -81,7 +81,7 @@ impl FileDescription for Event {
8181
if counter == 0 {
8282
if self.is_nonblock {
8383
let result = Err(Error::from(ErrorKind::WouldBlock));
84-
ecx.read_byte_helper_ev(&buf_place, None, result, dest)?;
84+
read_byte_helper_ev(&buf_place, None, result, dest, ecx)?;
8585
return Ok(());
8686
} else {
8787
//FIXME: blocking is not supported
@@ -91,7 +91,7 @@ impl FileDescription for Event {
9191
// Synchronize with all prior `write` calls to this FD.
9292
ecx.acquire_clock(&self.clock.borrow());
9393
let result = Ok(U64_ARRAY_SIZE);
94-
ecx.read_byte_helper_ev(&buf_place, Some(counter), result, dest)?;
94+
read_byte_helper_ev(&buf_place, Some(counter), result, dest, ecx)?;
9595
self.counter.set(0);
9696
// When any of the event happened, we check and update the status of all supported event
9797
// types for current file description.
@@ -230,3 +230,27 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
230230
Ok(Scalar::from_i32(fd_value))
231231
}
232232
}
233+
234+
/// This function either writes to the user supplied buffer and to dest place, or return error.
235+
fn read_byte_helper_ev<'tcx>(
236+
buf_place: &MPlaceTy<'tcx>,
237+
read_val: Option<u64>,
238+
result: io::Result<usize>,
239+
dest: &MPlaceTy<'tcx>,
240+
ecx: &mut MiriInterpCx<'tcx>,
241+
) -> InterpResult<'tcx> {
242+
match result.map(|c| i64::try_from(c).unwrap()) {
243+
Ok(read_bytes) => {
244+
// Write to the user supplied buffer.
245+
ecx.write_int(read_val.unwrap(), buf_place)?;
246+
// Write to the function return value place.
247+
ecx.write_int(read_bytes, dest)?;
248+
return Ok(());
249+
}
250+
Err(e) => {
251+
ecx.set_last_error_from_io_error(e)?;
252+
ecx.write_int(-1, dest)?;
253+
return Ok(());
254+
}
255+
}
256+
}

0 commit comments

Comments
 (0)