Skip to content

Commit 93270c1

Browse files
committed
Remove parameter byte from FileDescription::read/pread
1 parent f8fbe35 commit 93270c1

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

src/shims/unix/fd.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ pub trait FileDescription: std::fmt::Debug + Any {
3030
&self,
3131
_self_ref: &FileDescriptionRef,
3232
_communicate_allowed: bool,
33-
_bytes: &mut [u8],
34-
_len: usize,
3533
_ptr: Pointer,
34+
_len: u64,
3635
_dest: &MPlaceTy<'tcx>,
3736
_ecx: &mut MiriInterpCx<'tcx>,
3837
) -> InterpResult<'tcx> {
@@ -56,9 +55,9 @@ pub trait FileDescription: std::fmt::Debug + Any {
5655
fn pread<'tcx>(
5756
&self,
5857
_communicate_allowed: bool,
59-
_bytes: &mut [u8],
6058
_offset: u64,
6159
_ptr: Pointer,
60+
_len: u64,
6261
_dest: &MPlaceTy<'tcx>,
6362
_ecx: &mut MiriInterpCx<'tcx>,
6463
) -> InterpResult<'tcx> {
@@ -132,18 +131,18 @@ impl FileDescription for io::Stdin {
132131
&self,
133132
_self_ref: &FileDescriptionRef,
134133
communicate_allowed: bool,
135-
bytes: &mut [u8],
136-
_len: usize,
137134
ptr: Pointer,
135+
len: u64,
138136
dest: &MPlaceTy<'tcx>,
139137
ecx: &mut MiriInterpCx<'tcx>,
140138
) -> InterpResult<'tcx> {
139+
let mut bytes = vec![0; usize::try_from(len).unwrap()];
141140
if !communicate_allowed {
142141
// We want isolation mode to be deterministic, so we have to disallow all reads, even stdin.
143142
helpers::isolation_abort_error("`read` from stdin")?;
144143
}
145-
let result = Read::read(&mut { self }, bytes);
146-
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
144+
let result = Read::read(&mut { self }, &mut bytes);
145+
ecx.read_byte_helper(ptr, bytes, result, dest)?;
147146
Ok(())
148147
}
149148

@@ -585,18 +584,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
585584
// because it was a target's `usize`. Also we are sure that its smaller than
586585
// `usize::MAX` because it is bounded by the host's `isize`.
587586

588-
let len = usize::try_from(count).unwrap();
589-
let mut bytes = vec![0; len];
590587
match offset {
591-
None => fd.read(&fd, communicate, &mut bytes, len, buf, dest, this)?,
588+
None => fd.read(&fd, communicate, buf, count, dest, this)?,
592589
Some(offset) => {
593590
let Ok(offset) = u64::try_from(offset) else {
594591
let einval = this.eval_libc("EINVAL");
595592
this.set_last_error(einval)?;
596593
this.write_int(-1, dest)?;
597594
return Ok(());
598595
};
599-
fd.pread(communicate, &mut bytes, offset, buf, dest, this)?
596+
fd.pread(communicate, offset, buf, count, dest, this)?
600597
}
601598
};
602599
Ok(())
@@ -682,7 +679,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
682679
}
683680
}
684681

685-
686682
/// This function either writes to the user supplied buffer and to dest place, or return error.
687683
// TODO: this is only used for eventfd
688684
fn read_byte_helper_ev(

src/shims/unix/fs.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ impl FileDescription for FileHandle {
3434
&self,
3535
_self_ref: &FileDescriptionRef,
3636
communicate_allowed: bool,
37-
bytes: &mut [u8],
38-
_len: usize,
3937
ptr: Pointer,
38+
len: u64,
4039
dest: &MPlaceTy<'tcx>,
4140
ecx: &mut MiriInterpCx<'tcx>,
4241
) -> InterpResult<'tcx> {
4342
assert!(communicate_allowed, "isolation should have prevented even opening a file");
44-
let result = (&mut &self.file).read(bytes);
43+
let mut bytes = vec![0; usize::try_from(len).unwrap()];
44+
let result = (&mut &self.file).read(&mut bytes);
4545
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
4646
Ok(())
4747
}
@@ -63,21 +63,22 @@ impl FileDescription for FileHandle {
6363
fn pread<'tcx>(
6464
&self,
6565
communicate_allowed: bool,
66-
bytes: &mut [u8],
6766
offset: u64,
6867
ptr: Pointer,
68+
len: u64,
6969
dest: &MPlaceTy<'tcx>,
7070
ecx: &mut MiriInterpCx<'tcx>,
7171
) -> InterpResult<'tcx> {
7272
assert!(communicate_allowed, "isolation should have prevented even opening a file");
73+
let mut bytes = vec![0; usize::try_from(len).unwrap()];
7374
// Emulates pread using seek + read + seek to restore cursor position.
7475
// Correctness of this emulation relies on sequential nature of Miri execution.
7576
// The closure is used to emulate `try` block, since we "bubble" `io::Error` using `?`.
7677
let file = &mut &self.file;
7778
let mut f = || {
7879
let cursor_pos = file.stream_position()?;
7980
file.seek(SeekFrom::Start(offset))?;
80-
let res = file.read(bytes);
81+
let res = file.read(&mut bytes);
8182
// Attempt to restore cursor position even if the read has failed
8283
file.seek(SeekFrom::Start(cursor_pos))
8384
.expect("failed to restore file position, this shouldn't be possible");

src/shims/unix/linux/eventfd.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,15 @@ impl FileDescription for Event {
6262
&self,
6363
self_ref: &FileDescriptionRef,
6464
_communicate_allowed: bool,
65-
_bytes: &mut [u8],
66-
len: usize,
6765
ptr: Pointer,
66+
len: u64,
6867
dest: &MPlaceTy<'tcx>,
6968
ecx: &mut MiriInterpCx<'tcx>,
7069
) -> InterpResult<'tcx> {
7170
// eventfd read at the size of u64.
7271
let buf_place = ecx.ptr_to_mplace(ptr, ecx.machine.layouts.u64);
7372
// Check the size of slice, and return error only if the size of the slice < 8.
74-
if len < U64_ARRAY_SIZE {
73+
if len < U64_ARRAY_SIZE.try_into().unwrap() {
7574
let result = Err(Error::from(ErrorKind::InvalidInput));
7675
ecx.read_byte_helper_ev(&buf_place, None, result, dest)?;
7776
return Ok(());

src/shims/unix/unnamed_socket.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ impl FileDescription for AnonSocket {
126126
&self,
127127
_self_ref: &FileDescriptionRef,
128128
_communicate_allowed: bool,
129-
bytes: &mut [u8],
130-
_len: usize,
131129
ptr: Pointer,
130+
len: u64,
132131
dest: &MPlaceTy<'tcx>,
133132
ecx: &mut MiriInterpCx<'tcx>,
134133
) -> InterpResult<'tcx> {
135-
let request_byte_size = bytes.len();
134+
let request_byte_size = len;
135+
let mut bytes = vec![0; usize::try_from(len).unwrap()];
136136

137137
// Always succeed on read size 0.
138138
if request_byte_size == 0 {
@@ -179,7 +179,7 @@ impl FileDescription for AnonSocket {
179179

180180
// Do full read / partial read based on the space available.
181181
// Conveniently, `read` exists on `VecDeque` and has exactly the desired behavior.
182-
let actual_read_size = readbuf.buf.read(bytes).unwrap();
182+
let actual_read_size = readbuf.buf.read(&mut bytes).unwrap();
183183

184184
// Need to drop before others can access the readbuf again.
185185
drop(readbuf);

0 commit comments

Comments
 (0)