Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bc27fbb

Browse files
committed
Add assert_target_os_is_unix function
1 parent 58d00aa commit bc27fbb

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/helpers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
493493
)
494494
}
495495

496+
/// Helper function used inside the shims of foreign functions to assert that the target OS
497+
/// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
498+
/// if this is not the case.
499+
fn assert_target_os_is_unix(&self, name: &str) {
500+
assert!(
501+
target_os_is_unix(self.eval_context_ref().tcx.sess.target.os.as_ref()),
502+
"`{}` is only available for supported UNIX family targets",
503+
name,
504+
);
505+
}
506+
496507
/// Get last error variable as a place, lazily allocating thread-local storage for it if
497508
/// necessary.
498509
fn last_error_place(&mut self) -> InterpResult<'tcx, MPlaceTy<'tcx, Tag>> {

src/shims/env.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,7 @@ impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mi
114114
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
115115
fn getenv(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
116116
let this = self.eval_context_mut();
117-
let target_os = &this.tcx.sess.target.os;
118-
assert!(
119-
target_os == "linux" || target_os == "macos",
120-
"`getenv` is only available for the UNIX target family"
121-
);
117+
this.assert_target_os_is_unix("getenv");
122118

123119
let name_ptr = this.read_pointer(name_op)?;
124120
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
212208
value_op: &OpTy<'tcx, Tag>,
213209
) -> InterpResult<'tcx, i32> {
214210
let this = self.eval_context_mut();
215-
let target_os = &this.tcx.sess.target.os;
216-
assert!(
217-
target_os == "linux" || target_os == "macos",
218-
"`setenv` is only available for the UNIX target family"
219-
);
211+
this.assert_target_os_is_unix("setenv");
220212

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

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

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

329313
let buf = this.read_pointer(buf_op)?;
330314
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
379363

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

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

@@ -469,11 +449,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
469449

470450
fn getpid(&mut self) -> InterpResult<'tcx, i32> {
471451
let this = self.eval_context_mut();
472-
let target_os = &this.tcx.sess.target.os;
473-
assert!(
474-
target_os == "linux" || target_os == "macos",
475-
"`getpid` is only available for the UNIX target family"
476-
);
452+
this.assert_target_os_is_unix("getpid");
477453

478454
this.check_no_isolation("`getpid`")?;
479455

0 commit comments

Comments
 (0)