Skip to content

Commit c0c8ab8

Browse files
committed
Move error helper to helper.rs
1 parent 1151e0d commit c0c8ab8

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

src/helpers.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::cmp;
21
use std::collections::BTreeSet;
32
use std::iter;
43
use std::num::NonZero;
54
use std::sync::Mutex;
65
use std::time::Duration;
6+
use std::{cmp, io};
77

88
use rand::RngCore;
99

@@ -846,6 +846,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
846846
self.set_last_error(self.io_error_to_errnum(err)?)
847847
}
848848

849+
/// Sets the last OS error using a `std::io::ErrorKind` and writes -1 to dest place.
850+
fn set_io_error_and_return_neg_one(
851+
&mut self,
852+
err: io::Error,
853+
dest: &MPlaceTy<'tcx>,
854+
) -> InterpResult<'tcx> {
855+
self.set_last_error(self.io_error_to_errnum(err)?)?;
856+
self.write_int(-1, dest)?;
857+
Ok(())
858+
}
859+
849860
/// Helper function that consumes an `std::io::Result<T>` and returns an
850861
/// `InterpResult<'tcx,T>::Ok` instead. In case the result is an error, this function returns
851862
/// `Ok(-1)` and sets the last OS error accordingly.

src/shims/unix/fd.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl FileDescription for io::Stdin {
154154
match result {
155155
Ok(rw_bytes) =>
156156
ecx.return_read_write_success(Some(ptr), Some(&bytes), true, rw_bytes, dest),
157-
Err(e) => ecx.return_read_write_error(e, dest),
157+
Err(e) => ecx.set_io_error_and_return_neg_one(e, dest),
158158
}
159159
}
160160

@@ -188,7 +188,7 @@ impl FileDescription for io::Stdout {
188188
io::stdout().flush().unwrap();
189189
match result {
190190
Ok(rw_bytes) => ecx.return_read_write_success(None, None, false, rw_bytes, dest),
191-
Err(e) => ecx.return_read_write_error(e, dest),
191+
Err(e) => ecx.set_io_error_and_return_neg_one(e, dest),
192192
}
193193
}
194194

@@ -217,7 +217,7 @@ impl FileDescription for io::Stderr {
217217
let result = Write::write(&mut { self }, bytes);
218218
match result {
219219
Ok(rw_bytes) => ecx.return_read_write_success(None, None, false, rw_bytes, dest),
220-
Err(e) => ecx.return_read_write_error(e, dest),
220+
Err(e) => ecx.set_io_error_and_return_neg_one(e, dest),
221221
}
222222
}
223223

@@ -690,18 +690,4 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
690690
this.write_int(u64::try_from(actual_rw_size).unwrap(), dest)?;
691691
Ok(())
692692
}
693-
694-
/// Helper to implement `FileDescription::read/write`.
695-
/// This is only used when there is a read/write error.
696-
///`-1` is written to `dest` and the last libc error is set appropriately.
697-
fn return_read_write_error(
698-
&mut self,
699-
err: io::Error,
700-
dest: &MPlaceTy<'tcx>,
701-
) -> InterpResult<'tcx> {
702-
let this = self.eval_context_mut();
703-
this.set_last_error(this.io_error_to_errnum(err)?)?;
704-
this.write_int(-1, dest)?;
705-
Ok(())
706-
}
707693
}

src/shims/unix/fs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl FileDescription for FileHandle {
4545
match result {
4646
Ok(rw_bytes) =>
4747
ecx.return_read_write_success(Some(ptr), Some(&bytes), true, rw_bytes, dest),
48-
Err(e) => ecx.return_read_write_error(e, dest),
48+
Err(e) => ecx.set_io_error_and_return_neg_one(e, dest),
4949
}
5050
}
5151

@@ -63,7 +63,7 @@ impl FileDescription for FileHandle {
6363
let result = (&mut &self.file).write(bytes);
6464
match result {
6565
Ok(rw_bytes) => ecx.return_read_write_success(None, None, false, rw_bytes, dest),
66-
Err(e) => ecx.return_read_write_error(e, dest),
66+
Err(e) => ecx.set_io_error_and_return_neg_one(e, dest),
6767
}
6868
}
6969

@@ -95,7 +95,7 @@ impl FileDescription for FileHandle {
9595
match result {
9696
Ok(rw_bytes) =>
9797
ecx.return_read_write_success(Some(ptr), Some(&bytes), true, rw_bytes, dest),
98-
Err(e) => ecx.return_read_write_error(e, dest),
98+
Err(e) => ecx.set_io_error_and_return_neg_one(e, dest),
9999
}
100100
}
101101

@@ -126,7 +126,7 @@ impl FileDescription for FileHandle {
126126
let result = f();
127127
match result {
128128
Ok(rw_bytes) => ecx.return_read_write_success(None, None, false, rw_bytes, dest),
129-
Err(e) => ecx.return_read_write_error(e, dest),
129+
Err(e) => ecx.set_io_error_and_return_neg_one(e, dest),
130130
}
131131
}
132132

src/shims/unix/linux/eventfd.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl FileDescription for Event {
127127
let ty = ecx.machine.layouts.u64;
128128
// Check the size of slice, and return error only if the size of the slice < 8.
129129
if len < ty.layout.size.bytes_usize() {
130-
return ecx.return_read_write_error(Error::from(ErrorKind::InvalidInput), dest);
130+
return ecx.set_io_error_and_return_neg_one(Error::from(ErrorKind::InvalidInput), dest);
131131
}
132132

133133
// Read the user supplied value from the pointer.
@@ -136,7 +136,7 @@ impl FileDescription for Event {
136136

137137
// u64::MAX as input is invalid because the maximum value of counter is u64::MAX - 1.
138138
if num == u64::MAX {
139-
return ecx.return_read_write_error(Error::from(ErrorKind::InvalidInput), dest);
139+
return ecx.set_io_error_and_return_neg_one(Error::from(ErrorKind::InvalidInput), dest);
140140
}
141141
// If the addition does not let the counter to exceed the maximum value, update the counter.
142142
// Else, block.
@@ -150,7 +150,8 @@ impl FileDescription for Event {
150150
}
151151
None | Some(u64::MAX) =>
152152
if self.is_nonblock {
153-
return ecx.return_read_write_error(Error::from(ErrorKind::WouldBlock), dest);
153+
return ecx
154+
.set_io_error_and_return_neg_one(Error::from(ErrorKind::WouldBlock), dest);
154155
} else {
155156
throw_unsup_format!("eventfd: blocking is unsupported");
156157
},

src/shims/unix/unnamed_socket.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ impl FileDescription for AnonSocket {
158158
// EAGAIN or EWOULDBLOCK can be returned for socket,
159159
// POSIX.1-2001 allows either error to be returned for this case.
160160
// Since there is no ErrorKind for EAGAIN, WouldBlock is used.
161-
return ecx.return_read_write_error(Error::from(ErrorKind::WouldBlock), dest);
161+
return ecx
162+
.set_io_error_and_return_neg_one(Error::from(ErrorKind::WouldBlock), dest);
162163
} else {
163164
// Blocking socketpair with writer and empty buffer.
164165
// FIXME: blocking is currently not supported
@@ -212,7 +213,7 @@ impl FileDescription for AnonSocket {
212213
let Some(peer_fd) = self.peer_fd().upgrade() else {
213214
// If the upgrade from Weak to Rc fails, it indicates that all read ends have been
214215
// closed.
215-
return ecx.return_read_write_error(Error::from(ErrorKind::BrokenPipe), dest);
216+
return ecx.set_io_error_and_return_neg_one(Error::from(ErrorKind::BrokenPipe), dest);
216217
};
217218

218219
let Some(writebuf) = &peer_fd.downcast::<AnonSocket>().unwrap().readbuf else {
@@ -226,7 +227,8 @@ impl FileDescription for AnonSocket {
226227
if available_space == 0 {
227228
if self.is_nonblock {
228229
// Non-blocking socketpair with a full buffer.
229-
return ecx.return_read_write_error(Error::from(ErrorKind::WouldBlock), dest);
230+
return ecx
231+
.set_io_error_and_return_neg_one(Error::from(ErrorKind::WouldBlock), dest);
230232
} else {
231233
// Blocking socketpair with a full buffer.
232234
throw_unsup_format!("socketpair write: blocking isn't supported yet");

0 commit comments

Comments
 (0)