Skip to content

Commit 8fe7543

Browse files
committed
add helper function for target platform checks
1 parent c233c4a commit 8fe7543

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/helpers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
368368
}
369369
Ok(())
370370
}
371+
/// Helper function used inside the shims of foreign functions to check that the target
372+
/// platform is `platform`. It returns an error using the `name` of the foreign function if
373+
/// this is not the case.
374+
fn check_platform(&mut self, platform: &str, name: &str) -> InterpResult<'tcx> {
375+
if self.eval_context_mut().tcx.sess.target.target.target_os.to_lowercase() != platform {
376+
throw_unsup_format!(
377+
"`{}` is only available in the `{}` platform",
378+
name,
379+
platform,
380+
)
381+
}
382+
Ok(())
383+
}
371384

372385
/// Sets the last error variable.
373386
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {

src/shims/fs.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
347347
) -> InterpResult<'tcx, i32> {
348348
let this = self.eval_context_mut();
349349
this.check_no_isolation("stat")?;
350+
this.check_platform("macos", "stat")?;
350351
// `stat` always follows symlinks.
351352
this.stat_or_lstat(true, path_op, buf_op)
352353
}
@@ -359,6 +360,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
359360
) -> InterpResult<'tcx, i32> {
360361
let this = self.eval_context_mut();
361362
this.check_no_isolation("lstat")?;
363+
this.check_platform("macos", "lstat")?;
362364
this.stat_or_lstat(false, path_op, buf_op)
363365
}
364366

@@ -370,10 +372,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
370372
let this = self.eval_context_mut();
371373

372374
this.check_no_isolation("fstat")?;
373-
374-
if this.tcx.sess.target.target.target_os.to_lowercase() != "macos" {
375-
throw_unsup_format!("The `fstat` shim is only available for `macos` targets.")
376-
}
375+
this.check_platform("macos", "fstat")?;
377376

378377
let fd = this.read_scalar(fd_op)?.to_i32()?;
379378

@@ -392,10 +391,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
392391
) -> InterpResult<'tcx, i32> {
393392
let this = self.eval_context_mut();
394393

395-
if this.tcx.sess.target.target.target_os.to_lowercase() != "macos" {
396-
throw_unsup_format!("The `stat` and `lstat` shims are only available for `macos` targets.")
397-
}
398-
399394
let path_scalar = this.read_scalar(path_op)?.not_undef()?;
400395
let path: PathBuf = this.read_os_str_from_c_str(path_scalar)?.into();
401396

@@ -417,10 +412,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
417412
let this = self.eval_context_mut();
418413

419414
this.check_no_isolation("statx")?;
420-
421-
if this.tcx.sess.target.target.target_os.to_lowercase() != "linux" {
422-
throw_unsup_format!("The `statx` shim is only available for `linux` targets.")
423-
}
415+
this.check_platform("linux", "statx")?;
424416

425417
let statxbuf_scalar = this.read_scalar(statxbuf_op)?.not_undef()?;
426418
let pathname_scalar = this.read_scalar(pathname_op)?.not_undef()?;

0 commit comments

Comments
 (0)