Skip to content

Commit

Permalink
Write -1 together with setting the global error value
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Sep 28, 2024
1 parent c4d5754 commit f162e51
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
dest: &MPlaceTy<'tcx>,
) -> InterpResult<'tcx> {
let err = self.eval_libc(err);
self.set_last_err_and_return_neg1(err, dest)
}

fn set_last_err_and_return_neg1(
&mut self,
err: Scalar,
dest: &MPlaceTy<'tcx>,
) -> InterpResult<'tcx> {
self.set_last_error(err)?;
self.eval_context_mut().write_int(-1, dest)
}
Expand Down
29 changes: 14 additions & 15 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {

// `stat` always follows symlinks.
let metadata = match FileMetadata::from_path(this, &path, true)? {
Some(metadata) => metadata,
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
Ok(metadata) => metadata,
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
};
let res = this.macos_stat_write_buf(metadata, buf_op)?;
this.write_int(res, dest)
Expand Down Expand Up @@ -706,8 +706,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}

let metadata = match FileMetadata::from_path(this, &path, false)? {
Some(metadata) => metadata,
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
Ok(metadata) => metadata,
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
};
let res = this.macos_stat_write_buf(metadata, buf_op)?;
this.write_int(res, dest)
Expand All @@ -734,8 +734,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}

let metadata = match FileMetadata::from_fd_num(this, fd)? {
Some(metadata) => metadata,
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
Ok(metadata) => metadata,
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
};
let res = this.macos_stat_write_buf(metadata, buf_op)?;
this.write_int(res, dest)
Expand Down Expand Up @@ -824,8 +824,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
FileMetadata::from_path(this, &path, follow_symlink)?
};
let metadata = match metadata {
Some(metadata) => metadata,
None => return this.write_int(-1, dest),
Ok(metadata) => metadata,
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
};

// The `mode` field specifies the type of the file and the permissions over the file for
Expand Down Expand Up @@ -1699,7 +1699,7 @@ impl FileMetadata {
ecx: &mut MiriInterpCx<'tcx>,
path: &Path,
follow_symlink: bool,
) -> InterpResult<'tcx, Option<FileMetadata>> {
) -> InterpResult<'tcx, Result<FileMetadata, Scalar>> {
let metadata =
if follow_symlink { std::fs::metadata(path) } else { std::fs::symlink_metadata(path) };

Expand All @@ -1709,9 +1709,9 @@ impl FileMetadata {
fn from_fd_num<'tcx>(
ecx: &mut MiriInterpCx<'tcx>,
fd_num: i32,
) -> InterpResult<'tcx, Option<FileMetadata>> {
) -> InterpResult<'tcx, Result<FileMetadata, Scalar>> {
let Some(fd) = ecx.machine.fds.get(fd_num) else {
return ecx.fd_not_found().map(|_: i32| None);
return Ok(Err(ecx.eval_libc("EBADF")));
};

let file = &fd
Expand All @@ -1731,12 +1731,11 @@ impl FileMetadata {
fn from_meta<'tcx>(
ecx: &mut MiriInterpCx<'tcx>,
metadata: Result<std::fs::Metadata, std::io::Error>,
) -> InterpResult<'tcx, Option<FileMetadata>> {
) -> InterpResult<'tcx, Result<FileMetadata, Scalar>> {
let metadata = match metadata {
Ok(metadata) => metadata,
Err(e) => {
ecx.set_last_error_from_io_error(e)?;
return Ok(None);
return Ok(Err(ecx.io_error_to_errnum(e)?));
}
};

Expand All @@ -1759,6 +1758,6 @@ impl FileMetadata {
let modified = extract_sec_and_nsec(metadata.modified())?;

// FIXME: Provide more fields using platform specific methods.
Ok(Some(FileMetadata { mode, size, created, accessed, modified }))
Ok(Ok(FileMetadata { mode, size, created, accessed, modified }))
}
}

0 comments on commit f162e51

Please sign in to comment.