From bc27fbb2f73b6f8d3f629b6954776a6572c2da19 Mon Sep 17 00:00:00 2001 From: infrandomness Date: Sat, 11 Jun 2022 17:54:23 +0200 Subject: [PATCH] Add `assert_target_os_is_unix` function --- src/helpers.rs | 11 +++++++++++ src/shims/env.rs | 36 ++++++------------------------------ 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index b494e85075c..134f556bf12 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -493,6 +493,17 @@ fn assert_target_os(&self, target_os: &str, name: &str) { ) } + /// 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 91acff40fe1..85ecd2b719f 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 @@ fn setenv( 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 @@ fn SetEnvironmentVariableW( 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 @@ fn getcwd( 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 @@ fn GetCurrentDirectoryW( 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 @@ fn update_environ(&mut self) -> InterpResult<'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`")?; -- 2.44.0