Skip to content

Commit c8ec00a

Browse files
committed
Remove the unnecessary io::Result<usize> from FileDescription::read/write as we already do the writing through helper
1 parent d55e95f commit c8ec00a

File tree

4 files changed

+43
-51
lines changed

4 files changed

+43
-51
lines changed

src/shims/unix/fd.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub trait FileDescription: std::fmt::Debug + Any {
3434
_ptr: Pointer,
3535
_dest: &MPlaceTy<'tcx>,
3636
_ecx: &mut MiriInterpCx<'tcx>,
37-
) -> InterpResult<'tcx, io::Result<usize>> {
37+
) -> InterpResult<'tcx> {
3838
throw_unsup_format!("cannot read from {}", self.name());
3939
}
4040

@@ -46,7 +46,7 @@ pub trait FileDescription: std::fmt::Debug + Any {
4646
_bytes: &[u8],
4747
_dest: &MPlaceTy<'tcx>,
4848
_ecx: &mut MiriInterpCx<'tcx>,
49-
) -> InterpResult<'tcx, io::Result<usize>> {
49+
) -> InterpResult<'tcx> {
5050
throw_unsup_format!("cannot write to {}", self.name());
5151
}
5252

@@ -60,7 +60,7 @@ pub trait FileDescription: std::fmt::Debug + Any {
6060
_ptr: Pointer,
6161
_dest: &MPlaceTy<'tcx>,
6262
_ecx: &mut MiriInterpCx<'tcx>,
63-
) -> InterpResult<'tcx, io::Result<usize>> {
63+
) -> InterpResult<'tcx> {
6464
throw_unsup_format!("cannot pread from {}", self.name());
6565
}
6666

@@ -73,7 +73,7 @@ pub trait FileDescription: std::fmt::Debug + Any {
7373
_offset: u64,
7474
_dest: &MPlaceTy<'tcx>,
7575
_ecx: &mut MiriInterpCx<'tcx>,
76-
) -> InterpResult<'tcx, io::Result<usize>> {
76+
) -> InterpResult<'tcx> {
7777
throw_unsup_format!("cannot pwrite to {}", self.name());
7878
}
7979

@@ -135,15 +135,14 @@ impl FileDescription for io::Stdin {
135135
ptr: Pointer,
136136
dest: &MPlaceTy<'tcx>,
137137
ecx: &mut MiriInterpCx<'tcx>,
138-
) -> InterpResult<'tcx, io::Result<usize>> {
138+
) -> InterpResult<'tcx> {
139139
if !communicate_allowed {
140140
// We want isolation mode to be deterministic, so we have to disallow all reads, even stdin.
141141
helpers::isolation_abort_error("`read` from stdin")?;
142142
}
143143
let result = Ok(Read::read(&mut { self }, bytes));
144144
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
145-
// TODO: remove the usize later
146-
Ok(Ok(0))
145+
Ok(())
147146
}
148147

149148
fn is_tty(&self, communicate_allowed: bool) -> bool {
@@ -163,7 +162,7 @@ impl FileDescription for io::Stdout {
163162
bytes: &[u8],
164163
dest: &MPlaceTy<'tcx>,
165164
ecx: &mut MiriInterpCx<'tcx>,
166-
) -> InterpResult<'tcx, io::Result<usize>> {
165+
) -> InterpResult<'tcx> {
167166
// We allow writing to stderr even with isolation enabled.
168167
let result = Ok(Write::write(&mut { self }, bytes));
169168
// Stdout is buffered, flush to make sure it appears on the
@@ -173,7 +172,7 @@ impl FileDescription for io::Stdout {
173172
// here.
174173
io::stdout().flush().unwrap();
175174
ecx.write_byte_helper(result, dest)?;
176-
Ok(Ok(0))
175+
Ok(())
177176
}
178177

179178
fn is_tty(&self, communicate_allowed: bool) -> bool {
@@ -193,12 +192,12 @@ impl FileDescription for io::Stderr {
193192
bytes: &[u8],
194193
dest: &MPlaceTy<'tcx>,
195194
ecx: &mut MiriInterpCx<'tcx>,
196-
) -> InterpResult<'tcx, io::Result<usize>> {
195+
) -> InterpResult<'tcx> {
197196
// We allow writing to stderr even with isolation enabled.
198197
// No need to flush, stderr is not buffered.
199198
let result = Ok(Write::write(&mut { self }, bytes));
200199
ecx.write_byte_helper(result, dest)?;
201-
Ok(Ok(0))
200+
Ok(())
202201
}
203202

204203
fn is_tty(&self, communicate_allowed: bool) -> bool {
@@ -222,11 +221,11 @@ impl FileDescription for NullOutput {
222221
bytes: &[u8],
223222
dest: &MPlaceTy<'tcx>,
224223
ecx: &mut MiriInterpCx<'tcx>,
225-
) -> InterpResult<'tcx, io::Result<usize>> {
224+
) -> InterpResult<'tcx> {
226225
// We just don't write anything, but report to the user that we did.
227226
let result = Ok(Ok(bytes.len()));
228227
ecx.write_byte_helper(result, dest)?;
229-
Ok(Ok(0))
228+
Ok(())
230229
}
231230
}
232231

@@ -585,8 +584,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
585584
// `usize::MAX` because it is bounded by the host's `isize`.
586585

587586
let mut bytes = vec![0; usize::try_from(count).unwrap()];
588-
// TODO: fix this later, handle this properly
589-
let _res = match offset {
587+
match offset {
590588
None => fd.read(&fd, communicate, &mut bytes, buf, dest, this)?,
591589
Some(offset) => {
592590
let Ok(offset) = u64::try_from(offset) else {
@@ -631,7 +629,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
631629
return Ok(());
632630
};
633631

634-
let _result = match offset {
632+
match offset {
635633
None => fd.write(&fd, communicate, &bytes, dest, this)?,
636634
Some(offset) => {
637635
let Ok(offset) = u64::try_from(offset) else {

src/shims/unix/fs.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ impl FileDescription for FileHandle {
3838
ptr: Pointer,
3939
dest: &MPlaceTy<'tcx>,
4040
ecx: &mut MiriInterpCx<'tcx>,
41-
) -> InterpResult<'tcx, io::Result<usize>> {
41+
) -> InterpResult<'tcx> {
4242
assert!(communicate_allowed, "isolation should have prevented even opening a file");
4343
let result = Ok((&mut &self.file).read(bytes));
4444
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
45-
// TODO: remove the usize later
46-
Ok(Ok(0))
45+
Ok(())
4746
}
4847

4948
fn write<'tcx>(
@@ -53,11 +52,11 @@ impl FileDescription for FileHandle {
5352
bytes: &[u8],
5453
dest: &MPlaceTy<'tcx>,
5554
ecx: &mut MiriInterpCx<'tcx>,
56-
) -> InterpResult<'tcx, io::Result<usize>> {
55+
) -> InterpResult<'tcx> {
5756
assert!(communicate_allowed, "isolation should have prevented even opening a file");
5857
let result = Ok((&mut &self.file).write(bytes));
5958
ecx.write_byte_helper(result, dest)?;
60-
Ok(Ok(0))
59+
Ok(())
6160
}
6261

6362
fn pread<'tcx>(
@@ -68,7 +67,7 @@ impl FileDescription for FileHandle {
6867
ptr: Pointer,
6968
dest: &MPlaceTy<'tcx>,
7069
ecx: &mut MiriInterpCx<'tcx>,
71-
) -> InterpResult<'tcx, io::Result<usize>> {
70+
) -> InterpResult<'tcx> {
7271
assert!(communicate_allowed, "isolation should have prevented even opening a file");
7372
// Emulates pread using seek + read + seek to restore cursor position.
7473
// Correctness of this emulation relies on sequential nature of Miri execution.
@@ -85,8 +84,7 @@ impl FileDescription for FileHandle {
8584
};
8685
let result = Ok(f());
8786
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
88-
// TODO: remove the usize later
89-
Ok(Ok(0))
87+
Ok(())
9088
}
9189

9290
fn pwrite<'tcx>(
@@ -96,7 +94,7 @@ impl FileDescription for FileHandle {
9694
offset: u64,
9795
dest: &MPlaceTy<'tcx>,
9896
ecx: &mut MiriInterpCx<'tcx>,
99-
) -> InterpResult<'tcx, io::Result<usize>> {
97+
) -> InterpResult<'tcx> {
10098
assert!(communicate_allowed, "isolation should have prevented even opening a file");
10199
// Emulates pwrite using seek + write + seek to restore cursor position.
102100
// Correctness of this emulation relies on sequential nature of Miri execution.
@@ -113,7 +111,7 @@ impl FileDescription for FileHandle {
113111
};
114112
let result = Ok(f());
115113
ecx.write_byte_helper(result, dest)?;
116-
Ok(Ok(0))
114+
Ok(())
117115
}
118116

119117
fn seek<'tcx>(

src/shims/unix/linux/eventfd.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,21 @@ impl FileDescription for Event {
6666
ptr: Pointer,
6767
dest: &MPlaceTy<'tcx>,
6868
ecx: &mut MiriInterpCx<'tcx>,
69-
) -> InterpResult<'tcx, io::Result<usize>> {
69+
) -> InterpResult<'tcx> {
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 {
7272
let result = Ok(Err(Error::from(ErrorKind::InvalidInput)));
7373
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
7474
// TODO: remove the usize later
75-
return Ok(Ok(0));
75+
return Ok(());
7676
};
7777
// Block when counter == 0.
7878
let counter = self.counter.get();
7979
if counter == 0 {
8080
if self.is_nonblock {
8181
let result = Ok(Err(Error::from(ErrorKind::WouldBlock)));
8282
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
83-
// TODO: remove the usize later
84-
return Ok(Ok(0));
83+
return Ok(());
8584
} else {
8685
//FIXME: blocking is not supported
8786
throw_unsup_format!("eventfd: blocking is unsupported");
@@ -101,8 +100,7 @@ impl FileDescription for Event {
101100

102101
let result = Ok(Ok(U64_ARRAY_SIZE));
103102
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
104-
// TODO: remove the usize later
105-
return Ok(Ok(0));
103+
return Ok(());
106104
}
107105
}
108106

@@ -125,12 +123,12 @@ impl FileDescription for Event {
125123
bytes: &[u8],
126124
dest: &MPlaceTy<'tcx>,
127125
ecx: &mut MiriInterpCx<'tcx>,
128-
) -> InterpResult<'tcx, io::Result<usize>> {
126+
) -> InterpResult<'tcx> {
129127
// Check the size of slice, and return error only if the size of the slice < 8.
130128
let Some(bytes) = bytes.first_chunk::<U64_ARRAY_SIZE>() else {
131129
let result = Ok(Err(Error::from(ErrorKind::InvalidInput)));
132130
ecx.write_byte_helper(result, dest)?;
133-
return Ok(Ok(0));
131+
return Ok(());
134132
};
135133
// Convert from bytes to int according to host endianness.
136134
let num = match ecx.tcx.sess.target.endian {
@@ -141,7 +139,7 @@ impl FileDescription for Event {
141139
if num == u64::MAX {
142140
let result = Ok(Err(Error::from(ErrorKind::InvalidInput)));
143141
ecx.write_byte_helper(result, dest)?;
144-
return Ok(Ok(0));
142+
return Ok(());
145143
}
146144
// If the addition does not let the counter to exceed the maximum value, update the counter.
147145
// Else, block.
@@ -157,7 +155,7 @@ impl FileDescription for Event {
157155
if self.is_nonblock {
158156
let result = Ok(Err(Error::from(ErrorKind::WouldBlock)));
159157
ecx.write_byte_helper(result, dest)?;
160-
return Ok(Ok(0));
158+
return Ok(());
161159
} else {
162160
//FIXME: blocking is not supported
163161
throw_unsup_format!("eventfd: blocking is unsupported");
@@ -170,7 +168,7 @@ impl FileDescription for Event {
170168

171169
let result = Ok(Ok(U64_ARRAY_SIZE));
172170
ecx.write_byte_helper(result, dest)?;
173-
Ok(Ok(0))
171+
Ok(())
174172
}
175173
}
176174

src/shims/unix/unnamed_socket.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,14 @@ impl FileDescription for AnonSocket {
130130
ptr: Pointer,
131131
dest: &MPlaceTy<'tcx>,
132132
ecx: &mut MiriInterpCx<'tcx>,
133-
) -> InterpResult<'tcx, io::Result<usize>> {
133+
) -> InterpResult<'tcx> {
134134
let request_byte_size = bytes.len();
135135

136136
// Always succeed on read size 0.
137137
if request_byte_size == 0 {
138138
let result = Ok(Ok(0));
139139
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
140-
// TODO: remove the usize later
141-
return Ok(Ok(0));
140+
return Ok(());
142141
}
143142

144143
let Some(readbuf) = &self.readbuf else {
@@ -153,8 +152,7 @@ impl FileDescription for AnonSocket {
153152
// 0 bytes successfully read indicates end-of-file.
154153
let result = Ok(Ok(0));
155154
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
156-
// TODO: remove the usize later
157-
return Ok(Ok(0));
155+
return Ok(());
158156
} else {
159157
if self.is_nonblock {
160158
// Non-blocking socketpair with writer and empty buffer.
@@ -164,8 +162,7 @@ impl FileDescription for AnonSocket {
164162
// Since there is no ErrorKind for EAGAIN, WouldBlock is used.
165163
let result = Ok(Err(Error::from(ErrorKind::WouldBlock)));
166164
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
167-
// TODO: remove the usize later
168-
return Ok(Ok(0));
165+
return Ok(());
169166
} else {
170167
// Blocking socketpair with writer and empty buffer.
171168
// FIXME: blocking is currently not supported
@@ -199,8 +196,7 @@ impl FileDescription for AnonSocket {
199196

200197
let result = Ok(Ok(actual_read_size));
201198
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
202-
// TODO: remove the usize later
203-
return Ok(Ok(0));
199+
return Ok(());
204200
}
205201

206202
fn write<'tcx>(
@@ -210,14 +206,14 @@ impl FileDescription for AnonSocket {
210206
bytes: &[u8],
211207
dest: &MPlaceTy<'tcx>,
212208
ecx: &mut MiriInterpCx<'tcx>,
213-
) -> InterpResult<'tcx, io::Result<usize>> {
209+
) -> InterpResult<'tcx> {
214210
let write_size = bytes.len();
215211
// Always succeed on write size 0.
216212
// ("If count is zero and fd refers to a file other than a regular file, the results are not specified.")
217213
if write_size == 0 {
218214
let result = Ok(Ok(0));
219215
ecx.write_byte_helper(result, dest)?;
220-
return Ok(Ok(0));
216+
return Ok(());
221217
}
222218

223219
// We are writing to our peer's readbuf.
@@ -226,7 +222,7 @@ impl FileDescription for AnonSocket {
226222
// closed.
227223
let result = Ok(Err(Error::from(ErrorKind::BrokenPipe)));
228224
ecx.write_byte_helper(result, dest)?;
229-
return Ok(Ok(0));
225+
return Ok(());
230226
};
231227

232228
let Some(writebuf) = &peer_fd.downcast::<AnonSocket>().unwrap().readbuf else {
@@ -240,7 +236,9 @@ impl FileDescription for AnonSocket {
240236
if available_space == 0 {
241237
if self.is_nonblock {
242238
// Non-blocking socketpair with a full buffer.
243-
return Ok(Err(Error::from(ErrorKind::WouldBlock)));
239+
let result = Ok(Err(Error::from(ErrorKind::WouldBlock)));
240+
ecx.write_byte_helper(result, dest)?;
241+
return Ok(());
244242
} else {
245243
// Blocking socketpair with a full buffer.
246244
throw_unsup_format!("socketpair write: blocking isn't supported yet");
@@ -263,7 +261,7 @@ impl FileDescription for AnonSocket {
263261

264262
let result = Ok(Ok(actual_write_size));
265263
ecx.write_byte_helper(result, dest)?;
266-
return Ok(Ok(0));
264+
return Ok(());
267265
}
268266
}
269267

0 commit comments

Comments
 (0)