diff --git a/ffi/tests/hold.rs b/ffi/tests/hold.rs index 33b6e0788..daa3a65d8 100644 --- a/ffi/tests/hold.rs +++ b/ffi/tests/hold.rs @@ -3,12 +3,13 @@ //! below: a call holds the handle so no close frees it meanwhile, through a slot of the //! runtime's table once calls contend, and a close that would free it claims it closed //! first, then turns each hold it finds in the table into a count, so the last to let go -//! frees it. Every interleaving within a preemption bound: a hold reads the handle, which -//! a free writes, so loom fails the model on any free a hold could race. +//! frees it. Every interleaving within a preemption bound, [`Atom`] making it so: a hold +//! reads the handle, which a free writes, so loom fails the model on any free a hold could +//! race. A [`Flaw`] makes the port wrong in one place, and `flaws_are_caught` has each fail. use loom::cell::UnsafeCell; use loom::sync::Arc; -use loom::sync::atomic::Ordering::SeqCst; +use loom::sync::atomic::Ordering::{Relaxed, SeqCst}; use loom::sync::atomic::{AtomicUsize, fence}; use loom::thread; @@ -27,71 +28,202 @@ const REVOKED: usize = 1; /// clear for [`REVOKED`]. const KEY: usize = 8; +/// A sequentially consistent atomic that loom also explores every thread's access around. +/// +/// loom's DPOR keeps one last access per atomic, which a thread's own load overwrites, so +/// a load after another thread's never races the store or exchange that follows it, and +/// that exchange never runs after the other thread's load and before its next access: a +/// close never lands between a call's `load`, `compare_exchange`, and `fetch_sub` on the +/// count. Each access here first takes a relaxed read-modify-write on one `gate` all the +/// atomics share, which DPOR sees conflict with every other thread's, so it tries each +/// order of the accesses within the preemption bound. +struct Atom { + value: AtomicUsize, + gate: Arc, +} + +impl Atom { + fn new(value: usize, gate: &Arc) -> Self { + Self { + value: AtomicUsize::new(value), + gate: gate.clone(), + } + } + + fn step(&self) { + self.gate.fetch_add(0, Relaxed); + } + + fn load(&self) -> usize { + self.step(); + self.value.load(SeqCst) + } + + fn store(&self, value: usize) { + self.step(); + self.value.store(value, SeqCst); + } + + fn swap(&self, value: usize) -> usize { + self.step(); + self.value.swap(value, SeqCst) + } + + fn compare_exchange(&self, current: usize, new: usize) -> Result { + self.step(); + self.value.compare_exchange(current, new, SeqCst, SeqCst) + } + + fn fetch_add(&self, value: usize) -> usize { + self.step(); + self.value.fetch_add(value, SeqCst) + } + + fn fetch_sub(&self, value: usize) -> usize { + self.step(); + self.value.fetch_sub(value, SeqCst) + } + + fn fetch_or(&self, value: usize) -> usize { + self.step(); + self.value.fetch_or(value, SeqCst) + } +} + +/// A way the port can be made wrong, with the models that must each fail on it. +macro_rules! flaws { + ($($(#[$doc:meta])* $name:ident => [$($model:literal),+],)*) => { + #[derive(Clone, Copy, Debug, PartialEq)] + enum Flaw { + $($(#[$doc])* $name,)* + } + + impl Flaw { + const ALL: &[Flaw] = &[$(Flaw::$name),*]; + + /// The models that must fail with the port flawed so. + fn caught(self) -> &'static [&'static str] { + match self { + $(Flaw::$name => &[$($model),+],)* + } + } + } + }; +} + +flaws! { + /// A call on the count does not look for a close. + HoldIgnoresClosed => ["abandon_while_bias_taken"], + /// A call in the table does not look again for a close that claimed it meanwhile. + HoldSkipsRecheck => ["abandon_against_biased_calls", "close_against_biased_call"], + /// A call in the table publishes its slot without the fence ordering it before that look. + HoldSkipsFence => ["abandon_against_biased_calls", "close_against_biased_call"], + /// A close frees a handle a call on the count holds. + CloseIgnoresCount => ["close_against_call"], + /// A close frees a handle a call in the table holds. + CloseIgnoresSlots => ["close_against_biased_call"], + /// A close claims the handle without the fence ordering it before its look at the table. + CloseSkipsFence => ["close_against_biased_call"], + /// A move claims the handle without the fence ordering it before its look at the table. + AbandonSkipsFence => ["abandon_against_biased_calls"], + /// A move does not turn the holds in the table into counts. + AbandonSkipsSlots => ["abandon_against_biased_calls"], + /// A checked close's one-crossing drop claims a handle calls hold. + DropFreeSkipsClaim => ["checked_close_against_calls"], + /// The last hold is let go of while the owner's remains. + ReleaseFreesEarly => ["calls_alone"], +} + /// A handle as a binding holds it, beside a table with a slot per column for its row. struct Hold { - state: AtomicUsize, - slots: Vec, + state: Atom, + slots: Vec, /// What the handle points at: Rust's memory, which the free ends. raw: UnsafeCell, freed: AtomicUsize, + flaw: Option, } impl Hold { - fn new(columns: usize, biased: bool) -> Arc { + fn new(columns: usize, biased: bool, flaw: Option) -> Arc { + let gate = Arc::new(AtomicUsize::new(0)); Arc::new(Self { - state: AtomicUsize::new(HELD | if biased { BIAS } else { 0 }), - slots: (0..columns).map(|_| AtomicUsize::new(0)).collect(), + state: Atom::new(HELD | if biased { BIAS } else { 0 }, &gate), + slots: (0..columns).map(|_| Atom::new(0, &gate)).collect(), raw: UnsafeCell::new(7), freed: AtomicUsize::new(0), + flaw, }) } + /// Whether the port is made wrong this way. + fn flawed(&self, flaw: Flaw) -> bool { + self.flaw == Some(flaw) + } + + /// Order the access before it ahead of the one after: loom treats a sequentially + /// consistent access as acquire and release alone, so a fence stands for what Go's, + /// Kotlin's, and Swift's atomics order. + fn fence(&self, skipped: Flaw) { + if !self.flawed(skipped) { + fence(SeqCst); + } + } + /// Hold the handle from `column`: the slot it went to, if any, or `None` once closed. fn hold(&self, column: usize) -> Option> { // The fast path each binding inlines: the count, while calls neither contend for it // nor find it closed. - let n = self.state.load(SeqCst); - if n & (CLOSED | BIAS) == 0 && self.state.compare_exchange(n, n + HELD, SeqCst, SeqCst).is_ok() { + let n = self.state.load(); + let closed = if self.flawed(Flaw::HoldIgnoresClosed) { + 0 + } else { + CLOSED + }; + if n & (closed | BIAS) == 0 && self.state.compare_exchange(n, n + HELD).is_ok() { return Some(None); } - if self.state.load(SeqCst) & BIAS != 0 && self.slots[column].compare_exchange(0, KEY, SeqCst, SeqCst).is_ok() { - // Loom treats a sequentially consistent access as acquire and release alone, - // so a fence stands for what Go's, Kotlin's, and Swift's atomics order. - fence(SeqCst); - if self.state.load(SeqCst) & CLOSED == 0 { + if self.state.load() & BIAS != 0 && self.slots[column].compare_exchange(0, KEY).is_ok() { + self.fence(Flaw::HoldSkipsFence); + if self.flawed(Flaw::HoldSkipsRecheck) || self.state.load() & CLOSED == 0 { return Some(Some(column)); } // A close claimed it meanwhile, and may have counted this hold. - if self.slots[column].compare_exchange(KEY, 0, SeqCst, SeqCst).is_err() { - self.slots[column].store(0, SeqCst); + if self.slots[column].compare_exchange(KEY, 0).is_err() { + self.slots[column].store(0); self.release(None); } return None; } - let mut n = self.state.load(SeqCst); + let mut n = self.state.load(); loop { if n & CLOSED != 0 { return None; } - match self.state.compare_exchange(n, n + HELD, SeqCst, SeqCst) { + match self.state.compare_exchange(n, n + HELD) { Ok(_) => break, Err(now) => n = now, } } // The fast path failed on an open handle with no bias only as another call changed // the count meanwhile: later ones go to the table. - self.state.fetch_or(BIAS, SeqCst); + self.state.fetch_or(BIAS); Some(None) } /// Let go of a hold [`Self::hold`] took. fn release(&self, slot: Option) { if let Some(column) = slot - && self.slots[column].swap(0, SeqCst) == KEY + && self.slots[column].swap(0) == KEY { return; } - if self.state.fetch_sub(HELD, SeqCst) - HELD < HELD { + let left = if self.flawed(Flaw::ReleaseFreesEarly) { + 2 * HELD + } else { + HELD + }; + if self.state.fetch_sub(HELD) - HELD < left { self.free(); } } @@ -110,7 +242,7 @@ impl Hold { /// handle once no hold is left. fn abandon(&self) { let n = loop { - let n = self.state.load(SeqCst); + let n = self.state.load(); if n & CLOSING != 0 { thread::yield_now(); continue; @@ -118,17 +250,17 @@ impl Hold { if n & CLOSED != 0 { return; } - if self.state.compare_exchange(n, n | CLOSED, SeqCst, SeqCst).is_ok() { + if self.state.compare_exchange(n, n | CLOSED).is_ok() { break n; } }; - fence(SeqCst); - if n & BIAS != 0 { + self.fence(Flaw::AbandonSkipsFence); + if n & BIAS != 0 && !self.flawed(Flaw::AbandonSkipsSlots) { for slot in &self.slots { - if slot.load(SeqCst) == KEY { - self.state.fetch_add(HELD, SeqCst); - if slot.compare_exchange(KEY, KEY | REVOKED, SeqCst, SeqCst).is_err() { - self.state.fetch_sub(HELD, SeqCst); + if slot.load() == KEY { + self.state.fetch_add(HELD); + if slot.compare_exchange(KEY, KEY | REVOKED).is_err() { + self.state.fetch_sub(HELD); } } } @@ -140,7 +272,7 @@ impl Hold { /// closed, waiting out another close that is still deciding. fn close(&self) -> Closed { let n = loop { - let n = self.state.load(SeqCst); + let n = self.state.load(); if n & CLOSING != 0 { thread::yield_now(); continue; @@ -148,20 +280,20 @@ impl Hold { if n & CLOSED != 0 { return Closed::Already; } - if n & !BIAS != HELD { + if n & !BIAS != HELD && !self.flawed(Flaw::CloseIgnoresCount) { return Closed::Borrowed; } let claim = n | CLOSED | CLOSING; - if self.state.compare_exchange(n, claim, SeqCst, SeqCst).is_ok() { + if self.state.compare_exchange(n, claim).is_ok() { break n; } }; - fence(SeqCst); - if n & BIAS != 0 && self.slots.iter().any(|slot| slot.load(SeqCst) == KEY) { - self.state.fetch_sub(CLOSED | CLOSING, SeqCst); + self.fence(Flaw::CloseSkipsFence); + if n & BIAS != 0 && !self.flawed(Flaw::CloseIgnoresSlots) && self.slots.iter().any(|slot| slot.load() == KEY) { + self.state.fetch_sub(CLOSED | CLOSING); return Closed::Borrowed; } - self.state.store(CLOSED, SeqCst); + self.state.store(CLOSED); self.free(); Closed::Now } @@ -171,19 +303,19 @@ impl Hold { /// under a borrow, reopening it, and the handle freed. `None` while a hold or a close /// stands, when the binding holds it for a drop alone and then gives up the owner's hold. fn drop_free(&self, refused: bool) -> Option { - if self - .state - .compare_exchange(HELD, CLOSED | CLOSING, SeqCst, SeqCst) - .is_err() - { + let held = match self.flawed(Flaw::DropFreeSkipsClaim) { + true => self.state.load() & !CLOSED, + false => HELD, + }; + if self.state.compare_exchange(held, CLOSED | CLOSING).is_err() { return None; } if refused { - self.state.store(HELD, SeqCst); + self.state.store(HELD); return Some(false); } self.free(); - self.state.store(CLOSED, SeqCst); + self.state.store(CLOSED); Some(true) } @@ -206,14 +338,14 @@ impl Hold { /// No hold is left, and the handle was freed once, or not at all while still open. fn settled(&self, closed: bool) { - assert!(self.slots.iter().all(|slot| slot.load(SeqCst) == 0)); + assert!(self.slots.iter().all(|slot| slot.load() == 0)); match closed { true => { - assert_eq!(self.state.load(SeqCst) & !BIAS, CLOSED); + assert_eq!(self.state.load() & !BIAS, CLOSED); assert_eq!(self.freed.load(SeqCst), 1); } false => { - assert_eq!(self.state.load(SeqCst) & !BIAS, HELD); + assert_eq!(self.state.load() & !BIAS, HELD); assert_eq!(self.freed.load(SeqCst), 0); } } @@ -249,148 +381,199 @@ fn call(hold: &Hold, column: usize) -> bool { } } -/// Two calls in the table and a move giving up the owner's hold: the free races no read. -#[test] -fn abandon_against_biased_calls() { - model(|| { - let hold = Hold::new(2, true); - let calls: Vec<_> = (0..2) - .map(|column| { - let hold = hold.clone(); - thread::spawn(move || call(&hold, column)) - }) - .collect(); - hold.abandon(); - for c in calls { - c.join().unwrap(); +/// Each model, as a test of the port as it is and as a function of the flaw to make it fail. +macro_rules! models { + ($($(#[$doc:meta])* fn $name:ident($flaw:ident) $body:block)*) => { + mod models { + use super::*; + $(pub fn $name($flaw: Option) $body)* + + /// Every model's name. + pub const NAMES: &[&str] = &[$(stringify!($name)),*]; + + /// The model of that name. + pub fn run(name: &str, flaw: Option) { + match name { + $(stringify!($name) => $name(flaw),)* + _ => panic!("no model {name}"), + } + } } - hold.settled(true); - }); + $($(#[$doc])* #[test] fn $name() { models::$name(None) })* + }; } -/// Calls on the count bias the handle once they meet, while a move frees it. -#[test] -fn abandon_while_bias_taken() { - model(|| { - let hold = Hold::new(2, false); - let other = hold.clone(); - let calls = thread::spawn(move || { - call(&other, 1); - call(&other, 1) +models! { + /// Two calls in the table and a move giving up the owner's hold: the free races no read. + fn abandon_against_biased_calls(flaw) { + model(move || { + let hold = Hold::new(2, true, flaw); + let calls: Vec<_> = (0..2) + .map(|column| { + let hold = hold.clone(); + thread::spawn(move || call(&hold, column)) + }) + .collect(); + hold.abandon(); + for c in calls { + c.join().unwrap(); + } + hold.settled(true); }); - call(&hold, 0); - hold.abandon(); - calls.join().unwrap(); - hold.settled(true); - }); -} + } -/// Two calls sharing a column, one in the slot and one on the count, against a move. -#[test] -fn abandon_against_shared_column() { - model(|| { - let hold = Hold::new(1, true); - let other = hold.clone(); - let first = thread::spawn(move || call(&other, 0)); - let second = call(&hold, 0); - hold.abandon(); - first.join().unwrap(); - let _ = second; - hold.settled(true); - }); -} + /// Calls on the count bias the handle once they meet, while a move frees it. + fn abandon_while_bias_taken(flaw) { + model(move || { + let hold = Hold::new(2, false, flaw); + let other = hold.clone(); + let calls = thread::spawn(move || { + call(&other, 1); + call(&other, 1) + }); + call(&hold, 0); + hold.abandon(); + calls.join().unwrap(); + hold.settled(true); + }); + } -/// An unchecked close refused while a call holds the handle, in the table or the count, -/// and freeing it otherwise, which no read races. A call that published its slot as the -/// close claimed the handle is refused too, as the close is, which tells it from one that -/// holds only by waiting out the call. -#[test] -fn close_against_biased_call() { - model(|| { - let hold = Hold::new(2, true); - let other = hold.clone(); - let held = thread::spawn(move || call(&other, 1)); - let closed = hold.close(); - held.join().unwrap(); - if closed != Closed::Now { - assert_eq!(hold.close(), Closed::Now); - } - hold.settled(true); - }); -} + /// Two calls sharing a column, one in the slot and one on the count, against a move. + fn abandon_against_shared_column(flaw) { + model(move || { + let hold = Hold::new(1, true, flaw); + let other = hold.clone(); + let first = thread::spawn(move || call(&other, 0)); + let second = call(&hold, 0); + hold.abandon(); + first.join().unwrap(); + let _ = second; + hold.settled(true); + }); + } -/// Two unchecked closes against a call in the table: one that finds the handle closed -/// returns only once the other's close stands, never while it may yet be refused. -#[test] -fn closes_against_biased_call() { - model(|| { - let hold = Hold::new(2, true); - let (other, call_hold) = (hold.clone(), hold.clone()); - let held = thread::spawn(move || call(&call_hold, 1)); - let first = thread::spawn(move || other.close()); - let second = hold.close(); - let first = first.join().unwrap(); - held.join().unwrap(); - let closes = [first, second]; - if closes.contains(&Closed::Already) { - assert!(closes.contains(&Closed::Now)); - } - if !closes.contains(&Closed::Now) { - assert_eq!(hold.close(), Closed::Now); - } - hold.settled(true); - }); -} + /// An unchecked close refused while a call holds the handle, in the table or the count, + /// and freeing it otherwise, which no read races. A call that published its slot as the + /// close claimed the handle is refused too, as the close is, which tells it from one that + /// holds only by waiting out the call. + fn close_against_biased_call(flaw) { + model(move || { + let hold = Hold::new(2, true, flaw); + let other = hold.clone(); + let held = thread::spawn(move || call(&other, 1)); + let closed = hold.close(); + held.join().unwrap(); + if closed != Closed::Now { + assert_eq!(hold.close(), Closed::Now); + } + hold.settled(true); + }); + } -/// A checked close against calls on the count, which bias it as they meet, and a GC giving -/// up the owner's hold: a call starting during the one-crossing drop finds it closed, and -/// the handle is freed once, which no read races. -#[test] -fn checked_close_against_calls() { - model(|| { - let hold = Hold::new(2, false); - let (other, collector) = (hold.clone(), hold.clone()); - // A call already holding it, besides those starting: loom misses a close between a - // hold's load and its compare-and-swap, so the model holds one up front. - let running = hold.hold(1).unwrap(); - let calls = thread::spawn(move || { - assert_eq!(other.read(), 7); - other.release(running); - call(&other, 1); - call(&other, 1) + /// Two unchecked closes against a call in the table: one that finds the handle closed + /// returns only once the other's close stands, never while it may yet be refused. + fn closes_against_biased_call(flaw) { + model(move || { + let hold = Hold::new(2, true, flaw); + let (other, call_hold) = (hold.clone(), hold.clone()); + let held = thread::spawn(move || call(&call_hold, 1)); + let first = thread::spawn(move || other.close()); + let second = hold.close(); + let first = first.join().unwrap(); + held.join().unwrap(); + let closes = [first, second]; + if closes.contains(&Closed::Already) { + assert!(closes.contains(&Closed::Now)); + } + if !closes.contains(&Closed::Now) { + assert_eq!(hold.close(), Closed::Now); + } + hold.settled(true); }); - let collected = thread::spawn(move || collector.abandon()); - assert!(hold.checked_close(0, false)); - calls.join().unwrap(); - collected.join().unwrap(); - hold.settled(true); - }); -} + } -/// A checked close Rust refuses, against a call: it leaves the handle open, unfreed, and -/// usable, whichever path it took. -#[test] -fn checked_close_refused() { - model(|| { - let hold = Hold::new(2, false); - let other = hold.clone(); - let held = thread::spawn(move || call(&other, 1)); - assert!(!hold.checked_close(0, true)); - held.join().unwrap(); - assert!(call(&hold, 0)); - hold.settled(false); - }); + /// A checked close against calls on the count, which bias it as they meet, and a GC giving + /// up the owner's hold: a call starting during the one-crossing drop finds it closed, and + /// the handle is freed once, which no read races. + fn checked_close_against_calls(flaw) { + model(move || { + let hold = Hold::new(2, false, flaw); + let (other, collector) = (hold.clone(), hold.clone()); + let calls = thread::spawn(move || { + call(&other, 1); + call(&other, 1) + }); + let collected = thread::spawn(move || collector.abandon()); + assert!(hold.checked_close(0, false)); + calls.join().unwrap(); + collected.join().unwrap(); + hold.settled(true); + }); + } + + /// A checked close Rust refuses, against a call: it leaves the handle open, unfreed, and + /// usable, whichever path it took. + fn checked_close_refused(flaw) { + model(move || { + let hold = Hold::new(2, false, flaw); + let other = hold.clone(); + let held = thread::spawn(move || call(&other, 1)); + assert!(!hold.checked_close(0, true)); + held.join().unwrap(); + assert!(call(&hold, 0)); + hold.settled(false); + }); + } + + /// Calls that never meet a close leave the handle open and unfreed. + fn calls_alone(flaw) { + model(move || { + let hold = Hold::new(2, false, flaw); + let other = hold.clone(); + let a = thread::spawn(move || call(&other, 1)); + assert!(call(&hold, 0)); + assert!(a.join().unwrap()); + hold.settled(false); + }); + } + + /// An unchecked close against a call on the count: refused while it holds the handle, and + /// freeing it otherwise, which no read races. + fn close_against_call(flaw) { + model(move || { + let hold = Hold::new(1, false, flaw); + let other = hold.clone(); + let held = thread::spawn(move || call(&other, 0)); + let closed = hold.close(); + held.join().unwrap(); + if closed != Closed::Now { + assert_eq!(hold.close(), Closed::Now); + } + hold.settled(true); + }); + } } -/// Calls that never meet a close leave the handle open and unfreed. +/// The models do not pass vacuously: each fails on the flaws it is there to catch. #[test] -fn calls_alone() { - model(|| { - let hold = Hold::new(2, false); - let other = hold.clone(); - let a = thread::spawn(move || call(&other, 1)); - assert!(call(&hold, 0)); - assert!(a.join().unwrap()); - hold.settled(false); - }); +fn flaws_are_caught() { + // A name no model has would panic in `run`, which counts as caught. + for flaw in Flaw::ALL { + for name in flaw.caught() { + assert!(models::NAMES.contains(name), "{flaw:?} names no model {name}"); + } + } + let runs: Vec<_> = Flaw::ALL + .iter() + .flat_map(|&flaw| flaw.caught().iter().map(move |&name| (flaw, name))) + .map(|(flaw, name)| { + let run = std::thread::spawn(move || models::run(name, Some(flaw))); + (flaw, name, run) + }) + .collect(); + let missed: Vec<_> = runs + .into_iter() + .filter_map(|(flaw, name, run)| run.join().is_ok().then_some((flaw, name))) + .collect(); + assert!(missed.is_empty(), "no failure from {missed:?}"); } diff --git a/quest/m1/ffi-loom-hold.md b/quest/m1/ffi-loom-hold.md deleted file mode 100644 index f4a3138d0..000000000 --- a/quest/m1/ffi-loom-hold.md +++ /dev/null @@ -1,22 +0,0 @@ -# [S] Hold model's missed interleavings - -## Goal - -`ffi/tests/hold.rs`, the loom model of the hold counts Go, Kotlin, and Swift share, explores -a close or move landing between a call's hold and its release on the count, and fails on a -mutant that frees a handle a call holds there. - -## Plan - -Found in [#906](https://github.com/kixel-ai/rust2/pull/906), whose pick kept this quest: loom 0.7.3 never ran another -thread's load between a thread's `load` then `compare_exchange` and its later `fetch_sub`. -A probe, one thread doing `load`, `compare_exchange(n, n + 8)`, `fetch_sub(8)` against -another's `load`, passed `assert_eq!(n, 8)` in every execution, while the same probe without -the leading `load` failed it. So the model's calls on the count, whose fast path is that -`load` and `compare_exchange`, never meet a close mid-call, and a checked close that skipped -its `state == HELD` claim passed every model until #906 held one call up front. - -Find why (loom's DPOR with SeqCst, a preemption bound, or the probe), then make every model -reach those interleavings: a newer loom, an explicit scheduling point, or calls held up front -as #906's does. Check with mutants of `hold`, `close`, `abandon`, and `drop_free` that each -model fails.