Skip to content

Commit c966e8c

Browse files
authored
Rollup merge of rust-lang#150088 - folkertdev:riscv-pause-miri, r=RalfJung
miri: add `miri_spin_loop` to make `hint::spin_loop` work consistently fixes rust-lang#150050 Always use the same implementation when running miri (regardless of target, for consistency). This implementation yields the current thread to try find actual bugs. r? RalfJung
2 parents 1dbca0d + c7dd19b commit c966e8c

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

library/core/src/hint.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,15 @@ pub const unsafe fn assert_unchecked(cond: bool) {
269269
#[stable(feature = "renamed_spin_loop", since = "1.49.0")]
270270
pub fn spin_loop() {
271271
crate::cfg_select! {
272+
miri => {
273+
unsafe extern "Rust" {
274+
safe fn miri_spin_loop();
275+
}
276+
277+
// Miri does support some of the intrinsics that are called below, but to guarantee
278+
// consistent behavior across targets, this custom function is used.
279+
miri_spin_loop();
280+
}
272281
target_arch = "x86" => {
273282
// SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
274283
crate::arch::x86::_mm_pause()

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
435435
// Return value: 0 on success, otherwise the size it would have needed.
436436
this.write_int(if success { 0 } else { needed_size }, dest)?;
437437
}
438+
// Hint that a loop is spinning indefinitely.
439+
"miri_spin_loop" => {
440+
let [] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
441+
442+
// Try to run another thread to maximize the chance of finding actual bugs.
443+
this.yield_active_thread();
444+
}
438445
// Obtains the size of a Miri backtrace. See the README for details.
439446
"miri_backtrace_size" => {
440447
this.handle_miri_backtrace_size(abi, link_name, args, dest)?;

src/tools/miri/tests/utils/miri_extern.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,7 @@ extern "Rust" {
155155

156156
/// Blocks the current execution if the argument is false
157157
pub fn miri_genmc_assume(condition: bool);
158+
159+
/// Indicate to Miri that this thread is busy-waiting in a spin loop.
160+
pub fn miri_spin_loop();
158161
}

0 commit comments

Comments
 (0)