Skip to content

Commit 58cbc18

Browse files
committed
Move read_byte_helper to ecx
1 parent 3914be9 commit 58cbc18

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/shims/unix/fd.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
581581
fd.pread(communicate, &mut bytes, offset, this)
582582
}
583583
};
584-
read_byte_helper(buf, bytes, result, dest, this)?;
584+
self.read_byte_helper(buf, bytes, result, dest)?;
585585
Ok(())
586586
}
587587

@@ -618,33 +618,35 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
618618
this.write_scalar(res, dest)?;
619619
Ok(())
620620
}
621-
}
622-
fn read_byte_helper<'tcx>(
623-
buf: Pointer,
624-
bytes: Vec<u8>,
625-
result: InterpResult<'tcx, io::Result<usize>>,
626-
dest: &MPlaceTy<'tcx>,
627-
ecx: &mut MiriInterpCx<'tcx>,
628-
) -> InterpResult<'tcx> {
629-
// `File::read` never returns a value larger than `count`, so this cannot fail.
630-
match result?.map(|c| i64::try_from(c).unwrap()) {
631-
// try to pass this the write_ptr inside write
632-
// Pass the pointer inside the write function.
633-
Ok(read_bytes) => {
634-
// If reading to `bytes` did not fail, we write those bytes to the buffer.
635-
// Crucially, if fewer than `bytes.len()` bytes were read, only write
636-
// that much into the output buffer!
637-
ecx.write_bytes_ptr(
638-
buf,
639-
bytes[..usize::try_from(read_bytes).unwrap()].iter().copied(),
640-
)?;
641-
ecx.write_scalar(Scalar::from_target_isize(read_bytes, ecx), dest)?;
642-
return Ok(());
643-
}
644-
Err(e) => {
645-
ecx.set_last_error_from_io_error(e)?;
646-
ecx.write_scalar(Scalar::from_target_isize(-1, ecx), dest)?;
647-
return Ok(());
621+
622+
fn read_byte_helper(
623+
&mut self,
624+
buf: Pointer,
625+
bytes: Vec<u8>,
626+
result: InterpResult<'tcx, io::Result<usize>>,
627+
dest: &MPlaceTy<'tcx>,
628+
) -> InterpResult<'tcx> {
629+
let this = self.eval_context_mut();
630+
// `File::read` never returns a value larger than `count`, so this cannot fail.
631+
match result?.map(|c| i64::try_from(c).unwrap()) {
632+
// try to pass this the write_ptr inside write
633+
// Pass the pointer inside the write function.
634+
Ok(read_bytes) => {
635+
// If reading to `bytes` did not fail, we write those bytes to the buffer.
636+
// Crucially, if fewer than `bytes.len()` bytes were read, only write
637+
// that much into the output buffer!
638+
this.write_bytes_ptr(
639+
buf,
640+
bytes[..usize::try_from(read_bytes).unwrap()].iter().copied(),
641+
)?;
642+
this.write_scalar(Scalar::from_target_isize(read_bytes, this), dest)?;
643+
return Ok(());
644+
}
645+
Err(e) => {
646+
this.set_last_error_from_io_error(e)?;
647+
this.write_scalar(Scalar::from_target_isize(-1, this), dest)?;
648+
return Ok(());
649+
}
648650
}
649651
}
650652
}

0 commit comments

Comments
 (0)