Skip to content

Commit db83e2d

Browse files
committed
Move read_byte_helper to ecx
1 parent 3d0c9f2 commit db83e2d

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
@@ -582,7 +582,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
582582
fd.pread(communicate, &mut bytes, offset, this)
583583
}
584584
};
585-
read_byte_helper(buf, bytes, result, dest, this)?;
585+
self.read_byte_helper(buf, bytes, result, dest)?;
586586
Ok(())
587587
}
588588

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

0 commit comments

Comments
 (0)