Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: handle writing to dest within the foreign item handler #3779

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,24 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.write_scalar(scalar, &errno_place)
}

fn set_libc_err_and_return_neg1(
&mut self,
err: &str,
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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these new functions are superseded by the helper that @tiif added and that I extended. You should be able to use set_last_error_and_return now (after a rebase).


/// Gets the last error variable.
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar> {
let this = self.eval_context_mut();
Expand Down Expand Up @@ -846,6 +864,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
self.set_last_error(self.io_error_to_errnum(err)?)
}

/// Helper function that consumes an `std::io::Result<i32>` and writes the `Ok` integer
/// to the destination. In case the result is an error, this function writes
/// `-1` and sets the last OS error accordingly.
fn write_io_result(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to the new io_error.rs file, and put it right below try_unwrap_io_result (since it extends that helper).

&mut self,
result: std::io::Result<i32>,
dest: &MPlaceTy<'tcx>,
) -> InterpResult<'tcx> {
let res = self.try_unwrap_io_result(result)?;
self.eval_context_mut().write_int(res, dest)
}

/// Helper function that consumes an `std::io::Result<T>` and returns an
/// `InterpResult<'tcx,T>::Ok` instead. In case the result is an error, this function returns
/// `Ok(-1)` and sets the last OS error accordingly.
Expand Down
5 changes: 2 additions & 3 deletions src/shims/unix/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(Scalar::from_u32(this.get_pid()))
}

fn linux_gettid(&mut self) -> InterpResult<'tcx, Scalar> {
fn linux_gettid(&mut self, dest: &MPlaceTy<'tcx>) -> InterpResult<'tcx> {
let this = self.eval_context_ref();
this.assert_target_os("linux", "gettid");

let index = this.machine.threads.active_thread().to_u32();

// Compute a TID for this thread, ensuring that the main thread has PID == TID.
let tid = this.get_pid().strict_add(index);

Ok(Scalar::from_u32(tid))
self.eval_context_mut().write_int(tid, dest)
}
}
3 changes: 1 addition & 2 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
"mmap" => {
let [addr, length, prot, flags, fd, offset] = this.check_shim(abi, Abi::C {unwind: false}, link_name, args)?;
let offset = this.read_scalar(offset)?.to_int(this.libc_ty_layout("off_t").size)?;
let ptr = this.mmap(addr, length, prot, flags, fd, offset)?;
this.write_scalar(ptr, dest)?;
this.mmap(addr, length, prot, flags, fd, offset, dest)?;
}
"munmap" => {
let [addr, length] = this.check_shim(abi, Abi::C {unwind: false}, link_name, args)?;
Expand Down
12 changes: 4 additions & 8 deletions src/shims/unix/freebsd/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,21 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
"stat" | "stat@FBSD_1.0" => {
let [path, buf] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.macos_fbsd_stat(path, buf)?;
this.write_scalar(result, dest)?;
this.macos_fbsd_stat(path, buf, dest)?;
}
"lstat" | "lstat@FBSD_1.0" => {
let [path, buf] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.macos_fbsd_lstat(path, buf)?;
this.write_scalar(result, dest)?;
this.macos_fbsd_lstat(path, buf, dest)?;
}
"fstat" | "fstat@FBSD_1.0" => {
let [fd, buf] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.macos_fbsd_fstat(fd, buf)?;
this.write_scalar(result, dest)?;
this.macos_fbsd_fstat(fd, buf, dest)?;
}
"readdir_r" | "readdir_r@FBSD_1.0" => {
let [dirp, entry, result] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.macos_fbsd_readdir_r(dirp, entry, result)?;
this.write_scalar(result, dest)?;
this.macos_fbsd_readdir_r(dirp, entry, result, dest)?;
}

// Miscellaneous
Expand Down
Loading