Skip to content

Commit

Permalink
Add assert_target_os_is_unix function
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ncMN committed Jun 11, 2022
1 parent 3670c1a commit 615550f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
11 changes: 11 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
)
}

/// Helper function used inside the shims of foreign functions to assert that the target OS
/// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
/// if this is not the case.
fn assert_target_os_is_unix(&self, name: &str) {
assert!(
target_os_is_unix(self.eval_context_ref().tcx.sess.target.os.as_ref()),
"`{}` is only available for supported UNIX family targets",
name,
);
}

/// Get last error variable as a place, lazily allocating thread-local storage for it if
/// necessary.
fn last_error_place(&mut self) -> InterpResult<'tcx, MPlaceTy<'tcx, Tag>> {
Expand Down
30 changes: 5 additions & 25 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mi
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
fn getenv(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
let this = self.eval_context_mut();
let target_os = &this.tcx.sess.target.os;
assert!(
target_os == "linux" || target_os == "macos",
"`getenv` is only available for the UNIX target family"
);
this.assert_target_os_is_unix("getenv");

let name_ptr = this.read_pointer(name_op)?;
let name = this.read_os_str_from_c_str(name_ptr)?;
Expand Down Expand Up @@ -212,11 +208,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
value_op: &OpTy<'tcx, Tag>,
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();
let target_os = &this.tcx.sess.target.os;
assert!(
target_os == "linux" || target_os == "macos",
"`setenv` is only available for the UNIX target family"
);
this.assert_target_os_is_unix("setenv");

let name_ptr = this.read_pointer(name_op)?;
let value_ptr = this.read_pointer(value_op)?;
Expand Down Expand Up @@ -286,11 +278,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

fn unsetenv(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();
let target_os = &this.tcx.sess.target.os;
assert!(
target_os == "linux" || target_os == "macos",
"`unsetenv` is only available for the UNIX target family"
);
this.assert_target_os_is_unix("unsetenv");

let name_ptr = this.read_pointer(name_op)?;
let mut success = None;
Expand Down Expand Up @@ -320,11 +308,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
size_op: &OpTy<'tcx, Tag>,
) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
let this = self.eval_context_mut();
let target_os = &this.tcx.sess.target.os;
assert!(
target_os == "linux" || target_os == "macos",
"`getcwd` is only available for the UNIX target family"
);
this.assert_target_os_is_unix("getcwd");

let buf = this.read_pointer(buf_op)?;
let size = this.read_scalar(size_op)?.to_machine_usize(&*this.tcx)?;
Expand Down Expand Up @@ -379,11 +363,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

fn chdir(&mut self, path_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();
let target_os = &this.tcx.sess.target.os;
assert!(
target_os == "linux" || target_os == "macos",
"`chdir` is only available for the UNIX target family"
);
this.assert_target_os_is_unix("chdir");

let path = this.read_path_from_c_str(this.read_pointer(path_op)?)?;

Expand Down

0 comments on commit 615550f

Please sign in to comment.