diff --git a/src/helpers.rs b/src/helpers.rs index b494e85075c4..134f556bf120 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -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>> { diff --git a/src/shims/env.rs b/src/shims/env.rs index 91acff40fe16..85ecd2b719f2 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -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>> { 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)?; @@ -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)?; @@ -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; @@ -320,11 +308,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx size_op: &OpTy<'tcx, Tag>, ) -> InterpResult<'tcx, Pointer>> { 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)?; @@ -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)?)?; @@ -469,11 +449,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx fn getpid(&mut self) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - let target_os = &this.tcx.sess.target.os; - assert!( - target_os == "linux" || target_os == "macos", - "`getpid` is only available for the UNIX target family" - ); + this.assert_target_os_is_unix("getpid"); this.check_no_isolation("`getpid`")?;