From f162e510b9d2564542626d132107f912da83bb2f Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 1 Aug 2024 11:43:24 +0000 Subject: [PATCH] Write `-1` together with setting the global error value --- src/helpers.rs | 8 ++++++++ src/shims/unix/fs.rs | 29 ++++++++++++++--------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 9b26469964..b483e1017c 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -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) } diff --git a/src/shims/unix/fs.rs b/src/shims/unix/fs.rs index f2b16b7ca0..abc14ef195 100644 --- a/src/shims/unix/fs.rs +++ b/src/shims/unix/fs.rs @@ -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) @@ -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) @@ -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) @@ -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 @@ -1699,7 +1699,7 @@ impl FileMetadata { ecx: &mut MiriInterpCx<'tcx>, path: &Path, follow_symlink: bool, - ) -> InterpResult<'tcx, Option> { + ) -> InterpResult<'tcx, Result> { let metadata = if follow_symlink { std::fs::metadata(path) } else { std::fs::symlink_metadata(path) }; @@ -1709,9 +1709,9 @@ impl FileMetadata { fn from_fd_num<'tcx>( ecx: &mut MiriInterpCx<'tcx>, fd_num: i32, - ) -> InterpResult<'tcx, Option> { + ) -> InterpResult<'tcx, Result> { 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 @@ -1731,12 +1731,11 @@ impl FileMetadata { fn from_meta<'tcx>( ecx: &mut MiriInterpCx<'tcx>, metadata: Result, - ) -> InterpResult<'tcx, Option> { + ) -> InterpResult<'tcx, Result> { 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)?)); } }; @@ -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 })) } }