Skip to content

test intrinsic fallback bodies with Miri #140903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ci/docker/host-x86_64/x86_64-gnu-tools/checktools.sh
Original file line number Diff line number Diff line change
@@ -40,6 +40,14 @@ if [ -z "${PR_CI_JOB:-}" ]; then
else
python3 "$X_PY" test --stage 2 src/tools/miri src/tools/miri/cargo-miri
fi
# We re-run the test suite for a chance to find bugs in the intrinsic fallback bodies and in MIR
# optimizations. This can miss UB, so we only run the "pass" tests. We need to enable debug
# assertions as `-O` disables them but some tests rely on them. We also set a cfg flag so tests can
# adjust their expectations if needed. This can change the output of the tests so we ignore that,
# we only ensure that all assertions still pass.
MIRIFLAGS="-Zmiri-force-intrinsic-fallback --cfg force_intrinsic_fallback -O -Zmir-opt-level=4 -Cdebug-assertions=yes" \
MIRI_SKIP_UI_CHECKS=1 \
python3 "$X_PY" test --stage 2 src/tools/miri -- tests/{pass,panic}
# We natively run this script on x86_64-unknown-linux-gnu and x86_64-pc-windows-msvc.
# Also cover some other targets via cross-testing, in particular all tier 1 targets.
case $HOST_TARGET in
3 changes: 3 additions & 0 deletions src/tools/miri/README.md
Original file line number Diff line number Diff line change
@@ -393,6 +393,9 @@ to Miri failing to detect cases of undefined behavior in a program.
disables the randomization of the next thread to be picked, instead fixing a round-robin schedule.
Note however that other aspects of Miri's concurrency behavior are still randomize; use
`-Zmiri-deterministic-concurrency` to disable them all.
* `-Zmiri-force-intrinsic-fallback` forces the use of the "fallback" body for all intrinsics that
have one. This is useful to test the fallback bodies, but should not be used otherwise. It is
**unsound** since the fallback body might not be checking for all UB.
* `-Zmiri-native-lib=<path to a shared object file>` is an experimental flag for providing support
for calling native functions from inside the interpreter via FFI. The flag is supported only on
Unix systems. Functions not provided by that file are still executed via the usual Miri shims.
2 changes: 2 additions & 0 deletions src/tools/miri/src/bin/miri.rs
Original file line number Diff line number Diff line change
@@ -584,6 +584,8 @@ fn main() {
} else if arg == "-Zmiri-ignore-leaks" {
miri_config.ignore_leaks = true;
miri_config.collect_leak_backtraces = false;
} else if arg == "-Zmiri-force-intrinsic-fallback" {
miri_config.force_intrinsic_fallback = true;
} else if arg == "-Zmiri-strict-provenance" {
miri_config.provenance_mode = ProvenanceMode::Strict;
} else if arg == "-Zmiri-permissive-provenance" {
3 changes: 3 additions & 0 deletions src/tools/miri/src/eval.rs
Original file line number Diff line number Diff line change
@@ -165,6 +165,8 @@ pub struct MiriConfig {
pub address_reuse_cross_thread_rate: f64,
/// Round Robin scheduling with no preemption.
pub fixed_scheduling: bool,
/// Always prefer the intrinsic fallback body over the native Miri implementation.
pub force_intrinsic_fallback: bool,
}

impl Default for MiriConfig {
@@ -203,6 +205,7 @@ impl Default for MiriConfig {
address_reuse_rate: 0.5,
address_reuse_cross_thread_rate: 0.1,
fixed_scheduling: false,
force_intrinsic_fallback: false,
}
}
}
10 changes: 10 additions & 0 deletions src/tools/miri/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
@@ -28,6 +28,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
let this = self.eval_context_mut();

// Force use of fallback body, if available.
if this.machine.force_intrinsic_fallback
&& !this.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden
{
return interp_ok(Some(ty::Instance {
def: ty::InstanceKind::Item(instance.def_id()),
args: instance.args,
}));
}

// See if the core engine can handle this intrinsic.
if this.eval_intrinsic(instance, args, dest, ret)? {
return interp_ok(None);
5 changes: 5 additions & 0 deletions src/tools/miri/src/machine.rs
Original file line number Diff line number Diff line change
@@ -614,6 +614,9 @@ pub struct MiriMachine<'tcx> {

/// Cache for `mangle_internal_symbol`.
pub(crate) mangle_internal_symbol_cache: FxHashMap<&'static str, String>,

/// Always prefer the intrinsic fallback body over the native Miri implementation.
pub force_intrinsic_fallback: bool,
}

impl<'tcx> MiriMachine<'tcx> {
@@ -770,6 +773,7 @@ impl<'tcx> MiriMachine<'tcx> {
reject_in_isolation_warned: Default::default(),
int2ptr_warned: Default::default(),
mangle_internal_symbol_cache: Default::default(),
force_intrinsic_fallback: config.force_intrinsic_fallback,
}
}

@@ -946,6 +950,7 @@ impl VisitProvenance for MiriMachine<'_> {
reject_in_isolation_warned: _,
int2ptr_warned: _,
mangle_internal_symbol_cache: _,
force_intrinsic_fallback: _,
} = self;

threads.visit_provenance(visit);
26 changes: 15 additions & 11 deletions src/tools/miri/tests/pass/intrinsics/intrinsics.rs
Original file line number Diff line number Diff line change
@@ -33,20 +33,24 @@ fn main() {
assert_eq!(intrinsics::likely(false), false);
assert_eq!(intrinsics::unlikely(true), true);

let mut saw_true = false;
let mut saw_false = false;
// Skip this test when we use the fallback bodies, as that one is deterministic.
// (CI sets `--cfg force_intrinsic_fallback` together with `-Zmiri-force-intrinsic-fallback`.)
if !cfg!(force_intrinsic_fallback) {
let mut saw_true = false;
let mut saw_false = false;

for _ in 0..50 {
if intrinsics::is_val_statically_known(0) {
saw_true = true;
} else {
saw_false = true;
for _ in 0..50 {
if intrinsics::is_val_statically_known(0) {
saw_true = true;
} else {
saw_false = true;
}
}
assert!(
saw_true && saw_false,
"`is_val_statically_known` failed to return both true and false. Congrats, you won the lottery!"
);
}
assert!(
saw_true && saw_false,
"`is_val_statically_known` failed to return both true and false. Congrats, you won the lottery!"
);

intrinsics::forget(Bomb);