Skip to content

Commit 79fcb1d

Browse files
authored
Merge pull request rust-lang#4160 from YohDeadfall/check-os-helper
Added a helper to dedup target OS checks
2 parents 54d0054 + 73997ec commit 79fcb1d

File tree

2 files changed

+28
-58
lines changed

2 files changed

+28
-58
lines changed

src/tools/miri/src/helpers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
678678
)
679679
}
680680

681+
/// Helper function used inside shims of foreign functions to check that the target OS
682+
/// is one of `target_oses`. It returns an error containing the `name` of the foreign function
683+
/// in a message if this is not the case.
684+
fn check_target_os(&self, target_oses: &[&str], name: Symbol) -> InterpResult<'tcx> {
685+
let target_os = self.eval_context_ref().tcx.sess.target.os.as_ref();
686+
if !target_oses.contains(&target_os) {
687+
throw_unsup_format!("`{name}` is not supported on {target_os}");
688+
}
689+
interp_ok(())
690+
}
691+
681692
/// Helper function used inside the shims of foreign functions to assert that the target OS
682693
/// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
683694
/// if this is not the case.

src/tools/miri/src/shims/unix/foreign_items.rs

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
354354
}
355355
"pipe2" => {
356356
// Currently this function does not exist on all Unixes, e.g. on macOS.
357-
if !matches!(&*this.tcx.sess.target.os, "linux" | "freebsd" | "solaris" | "illumos")
358-
{
359-
throw_unsup_format!("`pipe2` is not supported on {}", this.tcx.sess.target.os);
360-
}
357+
this.check_target_os(&["linux", "freebsd", "solaris", "illumos"], link_name)?;
361358
let [pipefd, flags] = this.check_shim(abi, Conv::C, link_name, args)?;
362359
let result = this.pipe2(pipefd, Some(flags))?;
363360
this.write_scalar(result, dest)?;
@@ -402,12 +399,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
402399

403400
"reallocarray" => {
404401
// Currently this function does not exist on all Unixes, e.g. on macOS.
405-
if !matches!(&*this.tcx.sess.target.os, "linux" | "freebsd" | "android") {
406-
throw_unsup_format!(
407-
"`reallocarray` is not supported on {}",
408-
this.tcx.sess.target.os
409-
);
410-
}
402+
this.check_target_os(&["linux", "freebsd", "android"], link_name)?;
411403
let [ptr, nmemb, size] = this.check_shim(abi, Conv::C, link_name, args)?;
412404
let ptr = this.read_pointer(ptr)?;
413405
let nmemb = this.read_target_usize(nmemb)?;
@@ -656,13 +648,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
656648
}
657649
"sched_getaffinity" => {
658650
// Currently this function does not exist on all Unixes, e.g. on macOS.
659-
if !matches!(&*this.tcx.sess.target.os, "linux" | "freebsd" | "android") {
660-
throw_unsup_format!(
661-
"`sched_getaffinity` is not supported on {}",
662-
this.tcx.sess.target.os
663-
);
664-
}
665-
651+
this.check_target_os(&["linux", "freebsd", "android"], link_name)?;
666652
let [pid, cpusetsize, mask] = this.check_shim(abi, Conv::C, link_name, args)?;
667653
let pid = this.read_scalar(pid)?.to_u32()?;
668654
let cpusetsize = this.read_target_usize(cpusetsize)?;
@@ -699,13 +685,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
699685
}
700686
"sched_setaffinity" => {
701687
// Currently this function does not exist on all Unixes, e.g. on macOS.
702-
if !matches!(&*this.tcx.sess.target.os, "linux" | "freebsd" | "android") {
703-
throw_unsup_format!(
704-
"`sched_setaffinity` is not supported on {}",
705-
this.tcx.sess.target.os
706-
);
707-
}
708-
688+
this.check_target_os(&["linux", "freebsd", "android"], link_name)?;
709689
let [pid, cpusetsize, mask] = this.check_shim(abi, Conv::C, link_name, args)?;
710690
let pid = this.read_scalar(pid)?.to_u32()?;
711691
let cpusetsize = this.read_target_usize(cpusetsize)?;
@@ -761,16 +741,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
761741
"getentropy" => {
762742
// This function is non-standard but exists with the same signature and behavior on
763743
// Linux, macOS, FreeBSD and Solaris/Illumos.
764-
if !matches!(
765-
&*this.tcx.sess.target.os,
766-
"linux" | "macos" | "freebsd" | "illumos" | "solaris" | "android"
767-
) {
768-
throw_unsup_format!(
769-
"`getentropy` is not supported on {}",
770-
this.tcx.sess.target.os
771-
);
772-
}
773-
744+
this.check_target_os(
745+
&["linux", "macos", "freebsd", "illumos", "solaris", "android"],
746+
link_name,
747+
)?;
774748
let [buf, bufsize] = this.check_shim(abi, Conv::C, link_name, args)?;
775749
let buf = this.read_pointer(buf)?;
776750
let bufsize = this.read_target_usize(bufsize)?;
@@ -797,15 +771,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
797771
"getrandom" => {
798772
// This function is non-standard but exists with the same signature and behavior on
799773
// Linux, FreeBSD and Solaris/Illumos.
800-
if !matches!(
801-
&*this.tcx.sess.target.os,
802-
"linux" | "freebsd" | "illumos" | "solaris" | "android"
803-
) {
804-
throw_unsup_format!(
805-
"`getrandom` is not supported on {}",
806-
this.tcx.sess.target.os
807-
);
808-
}
774+
this.check_target_os(
775+
&["linux", "freebsd", "illumos", "solaris", "android"],
776+
link_name,
777+
)?;
809778
let [ptr, len, flags] = this.check_shim(abi, Conv::C, link_name, args)?;
810779
let ptr = this.read_pointer(ptr)?;
811780
let len = this.read_target_usize(len)?;
@@ -817,12 +786,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
817786
"arc4random_buf" => {
818787
// This function is non-standard but exists with the same signature and
819788
// same behavior (eg never fails) on FreeBSD and Solaris/Illumos.
820-
if !matches!(&*this.tcx.sess.target.os, "freebsd" | "illumos" | "solaris") {
821-
throw_unsup_format!(
822-
"`arc4random_buf` is not supported on {}",
823-
this.tcx.sess.target.os
824-
);
825-
}
789+
this.check_target_os(&["freebsd", "illumos", "solaris"], link_name)?;
826790
let [ptr, len] = this.check_shim(abi, Conv::C, link_name, args)?;
827791
let ptr = this.read_pointer(ptr)?;
828792
let len = this.read_target_usize(len)?;
@@ -842,15 +806,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
842806
// For arm32 they did something custom, but similar enough that the same
843807
// `_Unwind_RaiseException` impl in miri should work:
844808
// https://github.com/ARM-software/abi-aa/blob/main/ehabi32/ehabi32.rst
845-
if !matches!(
846-
&*this.tcx.sess.target.os,
847-
"linux" | "freebsd" | "illumos" | "solaris" | "android" | "macos"
848-
) {
849-
throw_unsup_format!(
850-
"`_Unwind_RaiseException` is not supported on {}",
851-
this.tcx.sess.target.os
852-
);
853-
}
809+
this.check_target_os(
810+
&["linux", "freebsd", "illumos", "solaris", "android", "macos"],
811+
link_name,
812+
)?;
854813
// This function looks and behaves excatly like miri_start_unwind.
855814
let [payload] = this.check_shim(abi, Conv::C, link_name, args)?;
856815
this.handle_miri_start_unwind(payload)?;

0 commit comments

Comments
 (0)