Skip to content

Commit 3f763c7

Browse files
committed
do not use host 'raw_os_error' to compute target error code
1 parent 72664f7 commit 3f763c7

File tree

1 file changed

+11
-24
lines changed
  • src/tools/miri/src/shims/unix

1 file changed

+11
-24
lines changed

src/tools/miri/src/shims/unix/fs.rs

+11-24
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ impl FileDescription for FileHandle {
229229
TRUE => Ok(()),
230230
FALSE => {
231231
let mut err = io::Error::last_os_error();
232+
// This only runs on Windows hosts so we can use `raw_os_error`.
233+
// We have to be careful not to forward that error code to target code.
232234
let code: u32 = err.raw_os_error().unwrap().try_into().unwrap();
233235
if matches!(code, ERROR_IO_PENDING | ERROR_LOCK_VIOLATION) {
234236
if lock_nb {
@@ -337,15 +339,7 @@ trait EvalContextExtPrivate<'tcx>: crate::MiriInterpCxExt<'tcx> {
337339
_ => interp_ok(this.eval_libc("DT_UNKNOWN").to_u8()?.into()),
338340
}
339341
}
340-
Err(e) =>
341-
match e.raw_os_error() {
342-
Some(error) => interp_ok(error),
343-
None =>
344-
throw_unsup_format!(
345-
"the error {} couldn't be converted to a return value",
346-
e
347-
),
348-
},
342+
Err(e) => this.io_error_to_errnum(e)?.to_i32(),
349343
}
350344
}
351345
}
@@ -1137,7 +1131,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11371131
let open_dir = this.machine.dirs.streams.get_mut(&dirp).ok_or_else(|| {
11381132
err_unsup_format!("the DIR pointer passed to readdir_r did not come from opendir")
11391133
})?;
1140-
interp_ok(Scalar::from_i32(match open_dir.read_dir.next() {
1134+
interp_ok(match open_dir.read_dir.next() {
11411135
Some(Ok(dir_entry)) => {
11421136
// Write into entry, write pointer to result, return 0 on success.
11431137
// The name is written with write_os_str_to_c_str, while the rest of the
@@ -1215,25 +1209,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12151209
let result_place = this.deref_pointer(result_op)?;
12161210
this.write_scalar(this.read_scalar(entry_op)?, &result_place)?;
12171211

1218-
0
1212+
Scalar::from_i32(0)
12191213
}
12201214
None => {
12211215
// end of stream: return 0, assign *result=NULL
12221216
this.write_null(&this.deref_pointer(result_op)?)?;
1223-
0
1217+
Scalar::from_i32(0)
12241218
}
1225-
Some(Err(e)) =>
1226-
match e.raw_os_error() {
1227-
// return positive error number on error
1228-
Some(error) => error,
1229-
None => {
1230-
throw_unsup_format!(
1231-
"the error {} couldn't be converted to a return value",
1232-
e
1233-
)
1234-
}
1235-
},
1236-
}))
1219+
Some(Err(e)) => {
1220+
// return positive error number on error (do *not* set last error)
1221+
this.io_error_to_errnum(e)?
1222+
}
1223+
})
12371224
}
12381225

12391226
fn closedir(&mut self, dirp_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {

0 commit comments

Comments
 (0)