Skip to content

Commit 805f35a

Browse files
committed
Use read_byte_pointer in every FileDescription::read
1 parent 1c0004e commit 805f35a

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

src/shims/unix/fd.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,18 @@ impl FileDescription for io::Stdin {
128128
_self_ref: &FileDescriptionRef,
129129
communicate_allowed: bool,
130130
bytes: &mut [u8],
131-
_ptr: Pointer,
132-
_dest: &MPlaceTy<'tcx>,
133-
_ecx: &mut MiriInterpCx<'tcx>,
131+
ptr: Pointer,
132+
dest: &MPlaceTy<'tcx>,
133+
ecx: &mut MiriInterpCx<'tcx>,
134134
) -> InterpResult<'tcx, io::Result<usize>> {
135135
if !communicate_allowed {
136136
// We want isolation mode to be deterministic, so we have to disallow all reads, even stdin.
137137
helpers::isolation_abort_error("`read` from stdin")?;
138138
}
139-
Ok(Read::read(&mut { self }, bytes))
139+
let result = Ok(Read::read(&mut { self }, bytes));
140+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
141+
// TODO: remove the usize later
142+
Ok(Ok(0))
140143
}
141144

142145
fn is_tty(&self, communicate_allowed: bool) -> bool {
@@ -572,19 +575,21 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
572575
// `usize::MAX` because it is bounded by the host's `isize`.
573576

574577
let mut bytes = vec![0; usize::try_from(count).unwrap()];
575-
let result = match offset {
576-
None => fd.read(&fd, communicate, &mut bytes, buf, dest, this),
578+
// TODO: fix this later, handle this properly
579+
let _res = match offset {
580+
None => fd.read(&fd, communicate, &mut bytes, buf, dest, this)?,
577581
Some(offset) => {
578582
let Ok(offset) = u64::try_from(offset) else {
579583
let einval = this.eval_libc("EINVAL");
580584
this.set_last_error(einval)?;
581585
this.write_scalar(Scalar::from_target_isize(-1, this), dest)?;
582586
return Ok(());
583587
};
584-
fd.pread(communicate, &mut bytes, offset, this)
588+
let result = fd.pread(communicate, &mut bytes, offset, this);
589+
self.read_byte_helper(buf, bytes, result, dest)?;
590+
return Ok(());
585591
}
586592
};
587-
self.read_byte_helper(buf, bytes, result, dest)?;
588593
Ok(())
589594
}
590595

src/shims/unix/fs.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ impl FileDescription for FileHandle {
3535
_self_ref: &FileDescriptionRef,
3636
communicate_allowed: bool,
3737
bytes: &mut [u8],
38-
_ptr: Pointer,
39-
_dest: &MPlaceTy<'tcx>,
40-
_ecx: &mut MiriInterpCx<'tcx>,
38+
ptr: Pointer,
39+
dest: &MPlaceTy<'tcx>,
40+
ecx: &mut MiriInterpCx<'tcx>,
4141
) -> InterpResult<'tcx, io::Result<usize>> {
4242
assert!(communicate_allowed, "isolation should have prevented even opening a file");
43-
Ok((&mut &self.file).read(bytes))
43+
let result = Ok((&mut &self.file).read(bytes));
44+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
45+
// TODO: remove the usize later
46+
Ok(Ok(0))
4447
}
4548

4649
fn write<'tcx>(

src/shims/unix/linux/eventfd.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,25 @@ impl FileDescription for Event {
6363
self_ref: &FileDescriptionRef,
6464
_communicate_allowed: bool,
6565
bytes: &mut [u8],
66-
_ptr: Pointer,
67-
_dest: &MPlaceTy<'tcx>,
66+
ptr: Pointer,
67+
dest: &MPlaceTy<'tcx>,
6868
ecx: &mut MiriInterpCx<'tcx>,
6969
) -> InterpResult<'tcx, io::Result<usize>> {
7070
// Check the size of slice, and return error only if the size of the slice < 8.
7171
let Some(bytes) = bytes.first_chunk_mut::<U64_ARRAY_SIZE>() else {
72-
return Ok(Err(Error::from(ErrorKind::InvalidInput)));
72+
let result = Ok(Err(Error::from(ErrorKind::InvalidInput)));
73+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
74+
// TODO: remove the usize later
75+
return Ok(Ok(0));
7376
};
7477
// Block when counter == 0.
7578
let counter = self.counter.get();
7679
if counter == 0 {
7780
if self.is_nonblock {
78-
return Ok(Err(Error::from(ErrorKind::WouldBlock)));
81+
let result = Ok(Err(Error::from(ErrorKind::WouldBlock)));
82+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
83+
// TODO: remove the usize later
84+
return Ok(Ok(0));
7985
} else {
8086
//FIXME: blocking is not supported
8187
throw_unsup_format!("eventfd: blocking is unsupported");
@@ -93,7 +99,10 @@ impl FileDescription for Event {
9399
// types for current file description.
94100
ecx.check_and_update_readiness(self_ref)?;
95101

96-
return Ok(Ok(U64_ARRAY_SIZE));
102+
let result = Ok(Ok(U64_ARRAY_SIZE));
103+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
104+
// TODO: remove the usize later
105+
return Ok(Ok(0));
97106
}
98107
}
99108

src/shims/unix/unnamed_socket.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,17 @@ impl FileDescription for AnonSocket {
127127
_self_ref: &FileDescriptionRef,
128128
_communicate_allowed: bool,
129129
bytes: &mut [u8],
130-
_ptr: Pointer,
131-
_dest: &MPlaceTy<'tcx>,
130+
ptr: Pointer,
131+
dest: &MPlaceTy<'tcx>,
132132
ecx: &mut MiriInterpCx<'tcx>,
133133
) -> InterpResult<'tcx, io::Result<usize>> {
134134
let request_byte_size = bytes.len();
135135

136136
// Always succeed on read size 0.
137137
if request_byte_size == 0 {
138+
let result = Ok(Ok(0));
139+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
140+
// TODO: remove the usize later
138141
return Ok(Ok(0));
139142
}
140143

@@ -148,6 +151,9 @@ impl FileDescription for AnonSocket {
148151
if self.peer_fd().upgrade().is_none() {
149152
// Socketpair with no peer and empty buffer.
150153
// 0 bytes successfully read indicates end-of-file.
154+
let result = Ok(Ok(0));
155+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
156+
// TODO: remove the usize later
151157
return Ok(Ok(0));
152158
} else {
153159
if self.is_nonblock {
@@ -156,7 +162,10 @@ impl FileDescription for AnonSocket {
156162
// EAGAIN or EWOULDBLOCK can be returned for socket,
157163
// POSIX.1-2001 allows either error to be returned for this case.
158164
// Since there is no ErrorKind for EAGAIN, WouldBlock is used.
159-
return Ok(Err(Error::from(ErrorKind::WouldBlock)));
165+
let result = Ok(Err(Error::from(ErrorKind::WouldBlock)));
166+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
167+
// TODO: remove the usize later
168+
return Ok(Ok(0));
160169
} else {
161170
// Blocking socketpair with writer and empty buffer.
162171
// FIXME: blocking is currently not supported
@@ -188,7 +197,10 @@ impl FileDescription for AnonSocket {
188197
ecx.check_and_update_readiness(&peer_fd)?;
189198
}
190199

191-
return Ok(Ok(actual_read_size));
200+
let result = Ok(Ok(actual_read_size));
201+
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
202+
// TODO: remove the usize later
203+
return Ok(Ok(0));
192204
}
193205

194206
fn write<'tcx>(

0 commit comments

Comments
 (0)