Skip to content

Commit

Permalink
do not use host 'raw_os_error' to compute target error code
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 26, 2024
1 parent 8b317d5 commit 0a9ea7e
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ impl FileDescription for FileHandle {
TRUE => Ok(()),
FALSE => {
let mut err = io::Error::last_os_error();
// This only runs on Windows hosts so we can use `raw_os_error`.
// We have to be careful not to forward that error code to target code.
let code: u32 = err.raw_os_error().unwrap().try_into().unwrap();
if matches!(code, ERROR_IO_PENDING | ERROR_LOCK_VIOLATION) {
if lock_nb {
Expand Down Expand Up @@ -337,15 +339,7 @@ trait EvalContextExtPrivate<'tcx>: crate::MiriInterpCxExt<'tcx> {
_ => interp_ok(this.eval_libc("DT_UNKNOWN").to_u8()?.into()),
}
}
Err(e) =>
match e.raw_os_error() {
Some(error) => interp_ok(error),
None =>
throw_unsup_format!(
"the error {} couldn't be converted to a return value",
e
),
},
Err(e) => this.io_error_to_errnum(e)?.to_i32(),
}
}
}
Expand Down Expand Up @@ -1137,7 +1131,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let open_dir = this.machine.dirs.streams.get_mut(&dirp).ok_or_else(|| {
err_unsup_format!("the DIR pointer passed to readdir_r did not come from opendir")
})?;
interp_ok(Scalar::from_i32(match open_dir.read_dir.next() {
interp_ok(match open_dir.read_dir.next() {
Some(Ok(dir_entry)) => {
// Write into entry, write pointer to result, return 0 on success.
// The name is written with write_os_str_to_c_str, while the rest of the
Expand Down Expand Up @@ -1215,25 +1209,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let result_place = this.deref_pointer(result_op)?;
this.write_scalar(this.read_scalar(entry_op)?, &result_place)?;

0
Scalar::from_i32(0)
}
None => {
// end of stream: return 0, assign *result=NULL
this.write_null(&this.deref_pointer(result_op)?)?;
0
Scalar::from_i32(0)
}
Some(Err(e)) =>
match e.raw_os_error() {
// return positive error number on error
Some(error) => error,
None => {
throw_unsup_format!(
"the error {} couldn't be converted to a return value",
e
)
}
},
}))
Some(Err(e)) => {
// return positive error number on error (do *not* set last error)
this.io_error_to_errnum(e)?
}
})
}

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

0 comments on commit 0a9ea7e

Please sign in to comment.