diff --git a/bench/deadlock/async-deadlock/Cargo.toml b/bench/deadlock/async-deadlock/Cargo.toml new file mode 100644 index 0000000..43ff0fa --- /dev/null +++ b/bench/deadlock/async-deadlock/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "async-deadlock" +version = "0.1.0" +edition = "2021" + +[workspace] + +[dependencies] +tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time"] } +parking_lot = { version = "0.12", features = ["send_guard"] } diff --git a/bench/deadlock/async-deadlock/src/main.rs b/bench/deadlock/async-deadlock/src/main.rs new file mode 100644 index 0000000..e07d144 --- /dev/null +++ b/bench/deadlock/async-deadlock/src/main.rs @@ -0,0 +1,25 @@ +use std::sync::Arc; + +use parking_lot::Mutex; + +async fn task1(a: Arc>, b: Arc>) { + let _ga = a.lock(); + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + let _gb = b.lock(); +} + +async fn task2(a: Arc>, b: Arc>) { + let _ga = a.lock(); + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + let _gb = b.lock(); +} + +#[tokio::main] +async fn main() { + let l1 = Arc::new(Mutex::new(0)); + let l2 = Arc::new(Mutex::new(0)); + let h1 = tokio::spawn(task1(l1.clone(), l2.clone())); + let h2 = tokio::spawn(task2(l2.clone(), l1.clone())); + let _ = h1.await; + let _ = h2.await; +} diff --git a/docs/async-modeling.md b/docs/async-modeling.md new file mode 100644 index 0000000..286fa64 --- /dev/null +++ b/docs/async-modeling.md @@ -0,0 +1,141 @@ +# Async modeling: current state and remaining gap + +How `async`/`tokio` code is (and is not) modeled, what landed on the `async` branch, +and the one blocker still separating the model from a real async deadlock. + +Code: `src/translate/callgraph.rs`, `src/translate/mir_to_pn/thread_control.rs`, +`src/concurrency/blocking.rs`, `src/memory/pta/builder.rs`. +Bench: `bench/deadlock/async-deadlock/`. + +## What is modeled + +| Piece | Mechanism | Status | +| ----- | --------- | ------ | +| `tokio::spawn` | `classify_thread_control` → `ThreadControlKind::AsyncSpawn` (regex `tokio…::spawn`); future resolved via `TyKind::Coroutine`/`CoroutineClosure` in `resolve_closure_def_id` | **done** | +| `JoinHandle.await` | type-based `classify_async_join_by_ty` on the awaited future (`JoinHandle` in the `poll`/`into_future` arg type); regex alone misses it because the callee is a `core::future` blanket impl | **done** | +| spawn/join wiring | `handle_spawn` emits an output arc to the future's `start`; `handle_join` links the spawned task's `end` into the `.await` join transition | **done** | +| guards held **across** `.await` | `BlockingCollector` + `LockGuardId.field` + `register_projected_guard`: a guard stored in the coroutine state (`(*_state as variant#N).k`) is registered with its field index and resolved receiver | **done** | + +The deadlock detector runs and reports; the async deadlock bench triggers spawn/join +arcs and lock acquire/release for both tasks. + +## Why `AsyncSpawn` / `AsyncJoin` classification needs the type check + +`.await` on a `JoinHandle` lowers to: + +``` +core::future::into_future::IntoFuture::into_future (blanket impl, def-id is in core) +core::future::future::Future::poll (blanket impl / Pin<&mut _>) +``` + +Neither def-path mentions `JoinHandle`, so a regex on `fn_path` cannot see it. +The join site is recognized by inspecting the awaited future's type +(`tokio::task::JoinHandle<…>` / `Pin<&'erased mut tokio::task::JoinHandle<…>>`), +see `classify_async_join_by_ty` in `callgraph.rs`. + +## The remaining blocker: coroutine upvar layout in the PTA + +> **Status (updated): fixed.** The env-binding fix below landed; the four guards +> now collapse into the two real locks with the correct AB–BA structure. + +### Symptom (before the fix) + +`bench/deadlock/async-deadlock` (two tasks, AB–BA lock order, guards held across +`.await`) should reduce to **one** deadlock (the two tasks deadlock on each other's +held locks). It currently reports several deadlocks because the lock grouping +splits the four guards into **four** separate `Mutex_0…Mutex_3` resources instead of +two real locks. + +``` +[lockgroup-detail] group0 ["{closure#0}::task1::__15(rx19fSome(1))"] = ParkingLotMutex +[lockgroup-detail] group1 ["{closure#0}::task1::__19(rx19fSome(0))"] = ParkingLotMutex +[lockgroup-detail] group2 ["{closure#0}::task2::__19(rx19fSome(0))"] = ParkingLotMutex +[lockgroup-detail] group3 ["{closure#0}::task2::__15(rx19fSome(1))"] = ParkingLotMutex +``` + +Expected: `task1.a` and `task2.b` are the *same* `Arc` (both l1), and +`task1.b` / `task2.a` are l2. Every cross-instance alias query returns `Unlikely`. + +### Root cause + +The `async fn` is split into a wrapper (`task1`) and a coroutine +(`task1::{closure#0}`). The wrapper constructs the coroutine: + +``` +_0 = {coroutine@…} { a: move _1, b: move _2 } // AggregateKind::Coroutine +``` + +`builder.rs` now routes `AggregateKind::Coroutine`/`CoroutineClosure` through the +same upvar binding as closures (`clo_heap.field_i ⊇ upvar_i`, `_0 → clo_heap`), so +the construction site is captured. But the coroutine **reads** its upvars through a +different shape than a closure: + +``` +_19 = copy (_1.0: &mut State) // _1 = Pin<&mut State>, _19 = state pointer +_4 = &(((*_19) as variant#3).0) // receiver = (*state).Downcast(variant#3).0 +``` + +Closures read `_1.field_i` directly; coroutines go `_1.0 → State → Downcast(variant) → field`. +The Phase-2 env binding (`_1.field_i ⊇ clo_heap.field_i`, `analysis.rs`) matches the +closure shape, so `_19` (the state pointer) ends up with no/incorrect points-to. +Consequently `collapsed_receiver_points_to` on the receiver `{coroutine, _19, field}` +is empty and `alias()` falls to `Unlikely`. + +### The fix + +In the env phase (`analysis.rs`), branch on `tcx.is_coroutine(def_id)`: + +- **closures**: `_1.field_i ⊇ heap.field_i` (unchanged); +- **coroutines**: bind the state pointer `_1.field_0 → state-heap` + (`Pin<&mut State>`, upvars read via `(*_1.0).variant#N.k`). + +After the fix the lock groups are: + +``` +[lockgroup-detail] group0 ["{closure#0}::task1::__15(rx19fSome(1))", "{closure#0}::task2::__19(rx19fSome(0))"] = ParkingLotMutex +[lockgroup-detail] group1 ["{closure#0}::task1::__19(rx19fSome(0))", "{closure#0}::task2::__15(rx19fSome(1))"] = ParkingLotMutex +``` + +`group0 = task1.b + task2.a` (both l2); `group1 = task1.a + task2.b` (both l1). +The net has `Mutex_0`/`Mutex_1` and the genuine AB–BA deadlock is reachable. + +## Remaining: coroutine suspend-return control flow + +> **Status (updated): fixed.** The suspend-return and double-join fixes below +> bring the bench to exactly **one** deadlock (the genuine AB–BA configuration). + +The aliasing fix was complete but the bug **count** was still inflated (≈16 vs 1). +The deadlock detector flags every terminal state without `main_end` +(`detect/deadlock.rs`). Three issues inflated the count; all now fixed: + +1. **Coroutine suspend-return** (`_0 = Poll::Pending; return` at `sleep().await`) + was treated as a completion, spuriously "finishing" the task while it still + held its lock (leaking it). `handle_return` now detects `Poll::Pending` returns + in coroutines and resumes to the continuation instead of the function end + (`terminator.rs`). +2. **Coroutine state dispatch**: `bb0`'s switch on the discriminant explored the + *resume* states unconditionally, letting `main` reach an `.await` poll without + its spawned task running. The dispatch now keeps only the entry state + (discriminant 0) — resumptions route through the suspend continuation. +3. **Double join**: `.await` lowers to both an `into_future` wrapper call and a + `poll` call; wiring *both* as `AsyncJoin` consumed the spawned task's end + token twice, blocking `main` at the second join. `classify_async_join_by_ty` + now only treats the `poll` call as the join. + +Result for `bench/deadlock/async-deadlock`: + +``` +Result : BUG FOUND (mode deadlock) +Bug count: 1 states=311, edges=754, reachable=311 +``` + +The single incident is the genuine AB–BA: both tasks blocked at `b.lock` on each +other's held `Mutex` while `main` waits at `h1.await`. + +## Decision / status + +- Guard recognition + spawn/join wiring: **done** and verified on the bench. +- Cross-instance `Arc` aliasing through coroutine state: **fixed** (coroutine + env binding), verified on the bench — 4 guards collapse to 2 real locks. +- Coroutine suspend/resume + async-join dedup: **done** — the bench reports exactly + the one genuine AB–BA deadlock. \ No newline at end of file diff --git a/docs/images/async-deadlock-petrinet-final.png b/docs/images/async-deadlock-petrinet-final.png new file mode 100644 index 0000000..32b76a2 Binary files /dev/null and b/docs/images/async-deadlock-petrinet-final.png differ diff --git a/docs/images/async-deadlock-petrinet-fixed.png b/docs/images/async-deadlock-petrinet-fixed.png new file mode 100644 index 0000000..e1c1106 Binary files /dev/null and b/docs/images/async-deadlock-petrinet-fixed.png differ diff --git a/docs/images/async-deadlock-petrinet-small.png b/docs/images/async-deadlock-petrinet-small.png new file mode 100644 index 0000000..943bf5c Binary files /dev/null and b/docs/images/async-deadlock-petrinet-small.png differ diff --git a/docs/images/async-deadlock-petrinet.png b/docs/images/async-deadlock-petrinet.png new file mode 100644 index 0000000..93c5bfa Binary files /dev/null and b/docs/images/async-deadlock-petrinet.png differ diff --git a/pn.toml.example b/pn.toml similarity index 100% rename from pn.toml.example rename to pn.toml diff --git a/src/concurrency/blocking.rs b/src/concurrency/blocking.rs index f7683ca..82b24aa 100644 --- a/src/concurrency/blocking.rs +++ b/src/concurrency/blocking.rs @@ -16,15 +16,35 @@ use crate::translate::callgraph::InstanceId; pub struct LockGuardId { pub instance_id: InstanceId, pub local: Local, + /// Field index when the guard is stored inside a coroutine/struct state + /// projection (e.g. `(*_state as variant#N).k`). `None` for a plain local. + pub field: Option, } impl LockGuardId { pub fn new(instance_id: InstanceId, local: Local) -> Self { - Self { instance_id, local } + Self { + instance_id, + local, + field: None, + } + } + + pub fn with_field(instance_id: InstanceId, local: Local, field: Option) -> Self { + Self { + instance_id, + local, + field, + } } pub fn get_alias_id(&self) -> AliasId { - AliasId::new(self.instance_id, self.local) + AliasId { + instance_id: self.instance_id, + local: self.local, + array_index: None, + field: self.field, + } } } @@ -317,10 +337,6 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { /// determined are simply left out — the consumer falls back to guard /// aliasing for those, preserving the previous (sound) behavior. fn resolve_lock_objects(&mut self) { - if self.lockguards.is_empty() { - return; - } - // A guard local can be defined in several places: a loop-carried guard // is assigned by the original `lock().unwrap()` *and* re-assigned from // `Condvar::wait(...)` every iteration. A single-def map would lose the @@ -372,9 +388,6 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { .. } = &term.kind { - if !destination.projection.is_empty() { - continue; - } let Some((def_id, substs)) = func.const_fn_def() else { continue; }; @@ -409,32 +422,48 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { receiver_local = r_local; receiver_field = receiver_field.or(r_field); } - def_source - .entry(destination.local) - .or_default() - .push(DefSource::LockAcquire { - local: receiver_local, - field: receiver_field, - }); - } else if ownership::is_wrapper_extract(def_id, self.tcx) - || ownership::is_arc_rc_deref(def_id, substs, self.tcx) - || self - .lockguards - .contains_key(&LockGuardId::new(self.instance_id, destination.local)) + if destination.projection.is_empty() { + def_source.entry(destination.local).or_default().push( + DefSource::LockAcquire { + local: receiver_local, + field: receiver_field, + }, + ); + } else { + // Guards held across a suspend point are stored in a + // coroutine state field (e.g. `(*_state as variant#N).k`), + // so the lock destination is a projection, not a plain + // local. Register the guard directly with its field index + // and receiver. + self.register_projected_guard( + destination, + receiver_local, + receiver_field, + term.source_info.span, + ); + } + } else if destination.projection.is_empty() + && (ownership::is_wrapper_extract(def_id, self.tcx) + || ownership::is_arc_rc_deref(def_id, substs, self.tcx) + || self.lockguards.contains_key(&LockGuardId::new( + self.instance_id, + destination.local, + ))) { // `Result::unwrap()`, `Arc/Rc::deref()`, and any other // guard-producing call (e.g. custom `HandyRwLock::rl/wl` // wrappers) do not change which lock object a guard // protects; they only re-express the receiver through a // temporary local, so forward arg0. - def_source - .entry(destination.local) - .or_default() - .push(DefSource::Forward { + def_source.entry(destination.local).or_default().push( + DefSource::Forward { local, projection: projection.to_vec(), - }); - } else if ownership::is_condvar_wait(def_id, self.tcx) { + }, + ); + } else if destination.projection.is_empty() + && ownership::is_condvar_wait(def_id, self.tcx) + { // `Condvar::wait(&self, guard)` releases the lock on // entry and re-acquires it on return, so the returned // guard protects the *same* mutex as the passed-in @@ -449,13 +478,12 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { _ => None, }) { - def_source - .entry(destination.local) - .or_default() - .push(DefSource::Forward { + def_source.entry(destination.local).or_default().push( + DefSource::Forward { local: g_local, projection: g_proj, - }); + }, + ); } } } @@ -463,16 +491,20 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { } } - let guard_locals: Vec = self.lockguards.keys().map(|g| g.local).collect(); - for guard_local in guard_locals { + let guard_ids: Vec = self.lockguards.keys().copied().collect(); + for guard_id in guard_ids { + // Projected guards (coroutine-state fields) already have their + // receiver recorded directly at the lock-acquire site. + if guard_id.field.is_some() { + continue; + } if let Some((local, field)) = Self::resolve_guard_receiver( &def_source, &self.lockguards, self.instance_id, - guard_local, + guard_id.local, &mut FxHashSet::default(), ) { - let guard_id = LockGuardId::new(self.instance_id, guard_local); let alias_id = AliasId { instance_id: self.instance_id, local, @@ -484,6 +516,49 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { } } + /// Register a lock guard whose destination is a projection (a field of a + /// coroutine/struct state such as `(*_state as variant#N).k`). The receiver + /// (the lock object) has already been resolved by the caller, so record the + /// guard → receiver mapping directly. + fn register_projected_guard( + &mut self, + destination: &rustc_middle::mir::Place<'tcx>, + receiver_local: Local, + receiver_field: Option, + span: Span, + ) { + let field = destination.projection.iter().find_map(|elem| { + if let rustc_middle::mir::ProjectionElem::Field(f, _) = elem { + Some(f.as_u32()) + } else { + None + } + }); + let dest_ty = destination.ty(self.body, self.tcx).ty; + let Some(lockguard_ty) = LockGuardTy::from_local_ty(dest_ty, self.tcx) else { + return; + }; + let guard_id = LockGuardId::with_field(self.instance_id, destination.local, field); + self.lockguards + .insert(guard_id, LockGuardInfo::new(lockguard_ty, span)); + self.lock_objects.insert( + guard_id, + AliasId { + instance_id: self.instance_id, + local: receiver_local, + array_index: None, + field: receiver_field, + }, + ); + log::debug!( + "[lock-acquire] projected guard local={:?} field={:?} -> receiver local={:?} field={:?}", + destination.local, + field, + receiver_local, + receiver_field + ); + } + /// Normalize a `lock()` receiver local back to its base lock object through /// `&((*obj).field)` borrows (single path, first definition). fn trace_to_receiver( @@ -538,7 +613,10 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { return Some((*base, *field)); } } - DefSource::Forward { local: src, projection } => { + DefSource::Forward { + local: src, + projection, + } => { if visiting.insert(*src) { if let Some((l, f)) = Self::resolve_guard_receiver( def_source, @@ -549,8 +627,7 @@ impl<'a, 'b, 'tcx> BlockingCollector<'a, 'b, 'tcx> { ) { let field = f.or_else(|| { projection.iter().find_map(|elem| { - if let rustc_middle::mir::ProjectionElem::Field(f, _) = elem - { + if let rustc_middle::mir::ProjectionElem::Field(f, _) = elem { Some(f.as_u32()) } else { None diff --git a/src/config.rs b/src/config.rs index b959140..8646240 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,6 +39,10 @@ pub struct PnConfig { pub scope_spawn: Vec, #[serde(default = "default_scope_join")] pub scope_join: Vec, + #[serde(default = "default_async_spawn")] + pub async_spawn: Vec, + #[serde(default = "default_async_join")] + pub async_join: Vec, #[serde(default = "default_condvar_notify")] pub condvar_notify: Vec, #[serde(default = "default_condvar_wait")] @@ -86,6 +90,8 @@ impl Default for PnConfig { thread_join: default_thread_join(), scope_spawn: default_scope_spawn(), scope_join: default_scope_join(), + async_spawn: default_async_spawn(), + async_join: default_async_join(), condvar_notify: default_condvar_notify(), condvar_wait: default_condvar_wait(), channel_send: default_channel_send(), @@ -166,6 +172,23 @@ fn default_scope_join() -> Vec { ] } +fn default_async_spawn() -> Vec { + vec![ + r"tokio[:a-zA-Z0-9_#\{\}]*::spawn".to_string(), + r"async_std[:a-zA-Z0-9_#\{\}]*::spawn".to_string(), + r"smol[:a-zA-Z0-9_#\{\}]*::spawn".to_string(), + ] +} + +fn default_async_join() -> Vec { + vec![ + // `.await` on a `JoinHandle` lowers to polling the handle future, so the + // join site surfaces as the handle's `Future::poll` in MIR. + r"tokio::task::JoinHandle[:a-zA-Z0-9_#\{\}]*::poll".to_string(), + r"async_std::task::JoinHandle[:a-zA-Z0-9_#\{\}]*::poll".to_string(), + ] +} + fn default_condvar_notify() -> Vec { vec![ // std::sync::Condvar::notify_one / notify_all (public API). diff --git a/src/memory/pointsto.rs b/src/memory/pointsto.rs index 6c9e570..2592318 100644 --- a/src/memory/pointsto.rs +++ b/src/memory/pointsto.rs @@ -112,7 +112,7 @@ impl std::convert::From for AliasId { instance_id: lockguard_id.instance_id, local: lockguard_id.local, array_index: None, - field: None, + field: lockguard_id.field, } } } diff --git a/src/memory/pta/analysis.rs b/src/memory/pta/analysis.rs index dcf4dd0..070dfdc 100644 --- a/src/memory/pta/analysis.rs +++ b/src/memory/pta/analysis.rs @@ -143,7 +143,24 @@ impl<'tcx> PointerAnalysis<'tcx> { if let Some(heaps) = heaps { let empty = self.arena.empty_path(); let env_node = self.arena.var_ctx(ctx.clone(), func, 1, empty); + let is_coroutine = self.tcx.is_coroutine(inst.def_id()); for heap in heaps { + if is_coroutine { + // Async-fn coroutine: `_1` is `Pin<&mut State>` and the + // captured upvars are read through the state pointer + // `_1.0` (deref → `(*_1.0).variant#N.k`), not through + // `_1.field_i` like a closure. Point the state pointer + // `_1.0` at the state heap where the upvars were stored + // by the coroutine aggregate. + let state_ptr_path = + self.arena.extend_path(empty, ProjElem::Field(0)); + let state_ptr = self + .arena + .var_ctx(ctx.clone(), func, 1, state_ptr_path); + self.constraints + .add(Constraint::AddressOf { dst: state_ptr, obj: heap }); + continue; + } self.constraints .add(Constraint::AddressOf { dst: env_node, obj: heap }); // Bind the base upvar slots (`_1.field_i`); the MAX probing diff --git a/src/memory/pta/builder.rs b/src/memory/pta/builder.rs index 4a51b70..b9f213a 100644 --- a/src/memory/pta/builder.rs +++ b/src/memory/pta/builder.rs @@ -12,15 +12,15 @@ extern crate rustc_hir; extern crate rustc_index; extern crate rustc_middle; -use rustc_hir::def_id::DefId; use rustc_data_structures::fx::FxHashSet; +use rustc_hir::def_id::DefId; use smallvec::SmallVec; +use rustc_middle::mir::interpret::Scalar; use rustc_middle::mir::{ - AggregateKind, Body, Const, ConstOperand, LocalKind, Operand, Place, PlaceElem, - ProjectionElem, Rvalue, StatementKind, TerminatorKind, + AggregateKind, Body, Const, ConstOperand, LocalKind, Operand, Place, PlaceElem, ProjectionElem, + Rvalue, StatementKind, TerminatorKind, }; -use rustc_middle::mir::interpret::Scalar; use rustc_middle::ty::{self, GenericArgsRef, Instance, TyCtxt, TypingEnv}; use rustc_span::Spanned; @@ -300,7 +300,8 @@ struct ConstraintBuilder<'a, 'tcx> { /// Closure DefId → environment field paths that were actually captured /// (base upvar slot and nested leaf paths of aggregate upvars). Threaded up /// to the driver so it can bind the closure body's env slots to the heap. - closure_env_paths: &'a mut rustc_data_structures::fx::FxHashMap>, + closure_env_paths: + &'a mut rustc_data_structures::fx::FxHashMap>, /// Call sites awaiting interprocedural resolution by the driver. pending: Vec>, /// Monotonic counter giving each call site a distinct fresh heap object. @@ -327,7 +328,8 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { p } - fn proj_kinds(proj: &[PlaceElem<'tcx>]) -> SmallVec<[ProjKind; 4]> { let mut out: SmallVec<[ProjKind; 4]> = SmallVec::new(); + fn proj_kinds(proj: &[PlaceElem<'tcx>]) -> SmallVec<[ProjKind; 4]> { + let mut out: SmallVec<[ProjKind; 4]> = SmallVec::new(); for e in proj { out.push(match e { ProjectionElem::Field(f, _) => ProjKind::Field(f.as_u32()), @@ -388,8 +390,10 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { let mut w = self.walk(); w.fresh() }; - self.constraints - .add(Constraint::AddressOf { dst: temp, obj: global }); + self.constraints.add(Constraint::AddressOf { + dst: temp, + obj: global, + }); Some(temp) } @@ -469,7 +473,9 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { if self.arena.path(p).is_empty() { continue; } - let dst = self.arena.var_ctx(self.ctx.clone(), self.func, lhs_local, p); + let dst = self + .arena + .var_ctx(self.ctx.clone(), self.func, lhs_local, p); let src_slot_path = if has_deref { p } else { @@ -479,7 +485,9 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { } combined }; - let src = self.arena.var_ctx(self.ctx.clone(), self.func, src_local, src_slot_path); + let src = self + .arena + .var_ctx(self.ctx.clone(), self.func, src_local, src_slot_path); self.constraints.add(Constraint::Copy { dst, src }); } } @@ -490,7 +498,22 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { match rvalue { Rvalue::Aggregate(box kind, fields) => match kind { AggregateKind::Closure(def_id, substs) => { - self.process_closure_aggregate(lhs_local, *def_id, substs, fields); + let upvar_tys: SmallVec<[ty::Ty<'tcx>; 8]> = + substs.as_closure().upvar_tys().into_iter().collect(); + self.process_closure_aggregate(lhs_local, *def_id, &upvar_tys, fields); + } + AggregateKind::Coroutine(def_id, substs) => { + let upvar_tys: SmallVec<[ty::Ty<'tcx>; 8]> = + substs.as_coroutine().upvar_tys().into_iter().collect(); + self.process_closure_aggregate(lhs_local, *def_id, &upvar_tys, fields); + } + AggregateKind::CoroutineClosure(def_id, substs) => { + let upvar_tys: SmallVec<[ty::Ty<'tcx>; 8]> = substs + .as_coroutine_closure() + .upvar_tys() + .into_iter() + .collect(); + self.process_closure_aggregate(lhs_local, *def_id, &upvar_tys, fields); } _ => { self.assign_aggregate(lhs_local, &lhs_proj, kind, fields); @@ -518,9 +541,7 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { } self.expand_aggregate_copy(&lhs_proj, lhs_local, op); } - Rvalue::Cast(_, op, _) - | Rvalue::Repeat(op, _) - | Rvalue::UnaryOp(_, op) => { + Rvalue::Cast(_, op, _) | Rvalue::Repeat(op, _) | Rvalue::UnaryOp(_, op) => { if let Some(v) = self.operand_value(op) { self.store_value(&lhs_proj, lhs_local, v); } @@ -564,15 +585,13 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { fn process_closure_aggregate( &mut self, dst_local: u32, - _def_id: DefId, - _substs: GenericArgsRef<'tcx>, + def_id: DefId, + upvar_tys: &[ty::Ty<'tcx>], fields: &rustc_index::IndexVec>, ) { - // 获取闭包的 upvar 类型信息 - let upvar_tys = _substs.as_closure().upvar_tys(); let empty = self.arena.empty_path(); - // 为闭包环境创建一个 heap + // 为闭包/协程环境创建一个 heap self.call_counter += 1; let clo_heap = self.arena.heap( AllocSite { @@ -583,13 +602,10 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { empty, ); - // Record the def-site environment heap so the closure body's env param - // (`_1`) can later be bound to it by the driver. A closure may be - // constructed at several sites; all recorded heaps are unioned. - self.closure_envs - .entry(_def_id) - .or_default() - .push(clo_heap); + // Record the def-site environment heap so the closure/coroutine body's + // env param (`_1`) can later be bound to it by the driver. A closure may + // be constructed at several sites; all recorded heaps are unioned. + self.closure_envs.entry(def_id).or_default().push(clo_heap); // 字段级 Copy: clo_heap.field_i ⊇ upvar_value_i for (i, op) in fields.iter_enumerated() { @@ -598,7 +614,7 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { }; let field_path = self.arena.extend_path(empty, ProjElem::Field(i.as_u32())); self.closure_env_paths - .entry(_def_id) + .entry(def_id) .or_default() .push(field_path); if let Some(dst) = self.arena.project(clo_heap, field_path) { @@ -626,13 +642,13 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { } let combined = self.extend_path_with(field_path, p); self.closure_env_paths - .entry(_def_id) + .entry(def_id) .or_default() .push(combined); if let Some(dst) = self.arena.project(clo_heap, combined) { - let src = self - .arena - .var_ctx(self.ctx.clone(), self.func, upvar_local, p); + let src = + self.arena + .var_ctx(self.ctx.clone(), self.func, upvar_local, p); self.constraints.add(Constraint::Copy { dst, src }); } } @@ -803,7 +819,7 @@ impl<'a, 'tcx> ConstraintBuilder<'a, 'tcx> { self.constraints.add(Constraint::AddressOf { dst: dest, obj: fresh_heap, - }); // The pointer's *internal* pointer fields hold the heap address. + }); // The pointer's *internal* pointer fields hold the heap address. // An inlined `Box::into_raw` reads them as `box.0.0` field // accesses, so point every field-slot prefix of the pointer's // type at the heap to make the raw pointer resolve to the shared diff --git a/src/translate/callgraph.rs b/src/translate/callgraph.rs index aa3e622..980b949 100644 --- a/src/translate/callgraph.rs +++ b/src/translate/callgraph.rs @@ -29,6 +29,8 @@ pub enum ThreadControlKind { ScopeSpawn, ScopeJoin, RayonJoin, + AsyncSpawn, + AsyncJoin, } #[derive(Copy, Clone, Debug)] @@ -57,7 +59,8 @@ impl CallSiteLocation { destination: Some(destination), kind: ThreadControlKind::Spawn - | ThreadControlKind::ScopeSpawn, + | ThreadControlKind::ScopeSpawn + | ThreadControlKind::AsyncSpawn, .. } => Some(*destination), _ => None, @@ -200,7 +203,9 @@ impl<'tcx> CallGraph<'tcx> { } if let CallSiteLocation::ThreadControl { - kind: ThreadControlKind::Spawn | ThreadControlKind::ScopeSpawn, + kind: ThreadControlKind::Spawn + | ThreadControlKind::ScopeSpawn + | ThreadControlKind::AsyncSpawn, destination: Some(alias_id), .. } = location @@ -403,11 +408,23 @@ impl<'a, 'tcx> Visitor<'tcx> for CallSiteCollector<'a, 'tcx> { if let ty::FnDef(def_id, substs) = *func_ty.kind() { let fn_path = self.tcx.def_path_str(def_id); - if let Some(control_kind) = - classify_thread_control(self.tcx, def_id, &fn_path, self.key_api_regex) - { + let awaited_ty = args + .first() + .and_then(|arg| arg.node.place()) + .map(|place| format!("{:?}", place.ty(self.body, self.tcx).ty)) + .unwrap_or_default(); + let control_kind = classify_thread_control( + self.tcx, + def_id, + &fn_path, + self.key_api_regex, + ) + .or_else(|| classify_async_join_by_ty(&fn_path, &awaited_ty)); + if let Some(control_kind) = control_kind { match control_kind { - ThreadControlKind::Spawn | ThreadControlKind::ScopeSpawn => { + ThreadControlKind::Spawn + | ThreadControlKind::ScopeSpawn + | ThreadControlKind::AsyncSpawn => { if self.handle_spawn_call( args.as_ref(), destination, @@ -423,7 +440,9 @@ impl<'a, 'tcx> Visitor<'tcx> for CallSiteCollector<'a, 'tcx> { return; } } - ThreadControlKind::Join | ThreadControlKind::ScopeJoin => { + ThreadControlKind::Join + | ThreadControlKind::ScopeJoin + | ThreadControlKind::AsyncJoin => { if let Some(callee) = self.resolve_instance(def_id, substs) { let alias_id = AliasId::from_place(self.caller_idx, destination.as_ref()); @@ -511,6 +530,14 @@ pub fn classify_thread_control( return Some(ThreadControlKind::ScopeJoin); } + if key_api_regex.async_spawn.is_match(fn_path) { + return Some(ThreadControlKind::AsyncSpawn); + } + + if key_api_regex.async_join.is_match(fn_path) { + return Some(ThreadControlKind::AsyncJoin); + } + if key_api_regex.thread_spawn.is_match(fn_path) { return Some(ThreadControlKind::Spawn); } @@ -522,4 +549,19 @@ pub fn classify_thread_control( None } +/// Detect an async join (`.await` on a `JoinHandle`) by inspecting the awaited +/// future's type. The poll / `into_future` callee is usually a blanket `impl` +/// (`core::future::IntoFuture::into_future`, `Pin<&mut _>::poll`), so its +/// def-path never mentions `JoinHandle` and the regex classifier misses it. +/// Only the `Future::poll` call is the actual join: `.await` lowers to both an +/// `into_future` wrapper call and a `poll` call, and wiring *both* to the spawned +/// task's end consumes that token twice, blocking `main` at the second join. +pub fn classify_async_join_by_ty(fn_path: &str, awaited_ty: &str) -> Option { + if fn_path.contains("::poll") && awaited_ty.contains("JoinHandle") { + Some(ThreadControlKind::AsyncJoin) + } else { + None + } +} + const RAYON_JOIN_PATTERNS: &[&str] = &["rayon_core::join", "rayon::join"]; diff --git a/src/translate/mir_to_pn/calls.rs b/src/translate/mir_to_pn/calls.rs index 98393e7..ccf1763 100644 --- a/src/translate/mir_to_pn/calls.rs +++ b/src/translate/mir_to_pn/calls.rs @@ -1,6 +1,7 @@ //! Calls: `handle_call` dispatch, `handle_lock_call`, `handle_normal_call`, `handle_atomic_call`, `handle_condvar_call`, `handle_channel_call`. use super::BodyToPetriNet; +use crate::translate::callgraph::InstanceId; use crate::{ concurrency::{ atomic::{AtomicApi, atomic_api_from_name}, @@ -10,7 +11,7 @@ use crate::{ util::has_pn_attribute, }; use rustc_hir::def_id::DefId; -use rustc_middle::mir::{BasicBlock, Operand}; +use rustc_middle::mir::{BasicBlock, Operand, Place, ProjectionElem}; use rustc_span::Spanned; use unipn::pt::TransitionType; use unipn::{PlaceId, TransitionId}; @@ -23,6 +24,23 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { .copied() } + /// Build the guard id for a lock-call destination place, preserving the + /// field index so guards stored in a coroutine/struct state projection + /// (e.g. `(*_state as variant#N).k`) are distinguished from plain locals. + pub(super) fn lockguard_id_for_dest( + instance_id: InstanceId, + destination: &Place<'tcx>, + ) -> LockGuardId { + let field = destination.projection.iter().find_map(|elem| { + if let ProjectionElem::Field(f, _) = elem { + Some(f.as_u32()) + } else { + None + } + }); + LockGuardId::with_field(instance_id, destination.local, field) + } + pub(super) fn handle_lock_call( &mut self, bb_idx: BasicBlock, @@ -50,7 +68,7 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { return None; } - let lockguard_id = LockGuardId::new(self.instance_id, destination.local); + let lockguard_id = Self::lockguard_id_for_dest(self.instance_id, destination); if let Some(guard) = self.lockguards.get(&lockguard_id) { let Some(lock_node) = self.lock_node_for_guard(lockguard_id) else { return None; diff --git a/src/translate/mir_to_pn/closure.rs b/src/translate/mir_to_pn/closure.rs index 46b63f8..0dedfa0 100644 --- a/src/translate/mir_to_pn/closure.rs +++ b/src/translate/mir_to_pn/closure.rs @@ -14,7 +14,11 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { let place_ty = place.ty(self.body, self.tcx).ty; match place_ty.kind() { rustc_middle::ty::TyKind::Closure(def_id, _) - | rustc_middle::ty::TyKind::FnDef(def_id, _) => Some(*def_id), + | rustc_middle::ty::TyKind::FnDef(def_id, _) + | rustc_middle::ty::TyKind::Coroutine(def_id, _) => Some(*def_id), + rustc_middle::ty::TyKind::CoroutineClosure(def_id, _) => { + Some(self.tcx.coroutine_for_closure(*def_id)) + } _ => None, } } @@ -24,7 +28,8 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { Const::Unevaluated(unevaluated, _) => Some(unevaluated.def), _ => { if let rustc_middle::ty::TyKind::Closure(def_id, _) - | rustc_middle::ty::TyKind::FnDef(def_id, _) = constant.ty().kind() + | rustc_middle::ty::TyKind::FnDef(def_id, _) + | rustc_middle::ty::TyKind::Coroutine(def_id, _) = constant.ty().kind() { Some(*def_id) } else { diff --git a/src/translate/mir_to_pn/drop_unsafe.rs b/src/translate/mir_to_pn/drop_unsafe.rs index 03ccf67..8b9d14d 100644 --- a/src/translate/mir_to_pn/drop_unsafe.rs +++ b/src/translate/mir_to_pn/drop_unsafe.rs @@ -4,13 +4,12 @@ use super::BodyToPetriNet; use crate::{ - concurrency::blocking::{LockGuardId, LockGuardTy}, - memory::pointsto::AliasId, + concurrency::blocking::LockGuardTy, memory::pointsto::AliasId, translate::mir_utils::rvalue_read_places, }; -use unipn::pt::{PtTransition, TransitionType, UnsafeOp}; use rustc_data_structures::fx::FxHashMap; use rustc_middle::mir::{BasicBlock, BasicBlockData, Rvalue}; +use unipn::pt::{PtTransition, TransitionType, UnsafeOp}; impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { pub(super) fn handle_drop( @@ -30,7 +29,7 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { .add_input_arc(self.bb_graph.last(*bb_idx), bb_end, 1); if !bb.is_cleanup { - let lockguard_id = LockGuardId::new(self.instance_id, place.local); + let lockguard_id = Self::lockguard_id_for_dest(self.instance_id, place); if self.lockguards.get(&lockguard_id).is_some() { let lock_alias = lockguard_id.get_alias_id(); diff --git a/src/translate/mir_to_pn/terminator.rs b/src/translate/mir_to_pn/terminator.rs index cf8a6c1..6f9106f 100644 --- a/src/translate/mir_to_pn/terminator.rs +++ b/src/translate/mir_to_pn/terminator.rs @@ -110,8 +110,19 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { targets: &SwitchTargets, name: &str, ) { + // In a coroutine body, `bb0` is the state-machine dispatch (switch on the + // discriminant). Its *resume* states are only valid after a previous + // suspend; exploring them from the initial poll lets e.g. `main` reach an + // `.await` poll without its spawned task ever running, producing bogus + // terminal states. The suspend fix already routes resumptions through the + // continuation, so here we only keep the entry state (discriminant 0). + let is_coroutine_dispatch = bb_idx.index() == 0 + && self.tcx.is_coroutine(self.instance.def_id()); let mut t_num = 1u8; for t in targets.all_targets() { + if is_coroutine_dispatch && t_num > 1 { + break; + } if self.exclude_bb.contains(&t.index()) { continue; } @@ -132,6 +143,19 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { } pub(super) fn handle_return(&mut self, bb_idx: BasicBlock, name: &str) { + // A coroutine's `Return` with `Poll::Pending` is a suspend point, not a + // completion: the task stays alive holding its locks and later resumes + // (the `Poll::Ready` return is the real completion). Connecting a suspend + // to the function end would spuriously "complete" the task and leak the + // locks it still holds, so instead we resume to the continuation block + // (the poll-switch's other target). + if self.tcx.is_coroutine(self.instance.def_id()) && self.block_sets_poll_pending(bb_idx) { + if let Some(cont) = self.find_suspend_continuation(bb_idx) { + self.handle_fallthrough(bb_idx, &cont, name, "suspend"); + } + return; + } + let return_node = self .functions_map() .get(&self.instance.def_id()) @@ -202,4 +226,38 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { TransitionType::Return(self.instance_id.index()) ); } + + /// Whether the return block sets `_0` to `Poll::Pending` (a coroutine + /// suspend) rather than `Poll::Ready` (completion). + fn block_sets_poll_pending(&self, bb_idx: BasicBlock) -> bool { + let bb = &self.body.basic_blocks[bb_idx]; + for stmt in &bb.statements { + if let rustc_middle::mir::StatementKind::Assign(box (place, _)) = &stmt.kind { + if place.local.index() == 0 { + return format!("{:?}", stmt.kind).contains("Pending"); + } + } + } + false + } + + /// Find the block a coroutine suspend resumes to: the poll switch that + /// leads into the suspend block has the Ready/continuation as another + /// target. + fn find_suspend_continuation(&self, suspend_bb: BasicBlock) -> Option { + for (_bb_idx, bb) in self.body.basic_blocks.iter_enumerated() { + if let Some(term) = &bb.terminator { + if let rustc_middle::mir::TerminatorKind::SwitchInt { targets, .. } = &term.kind { + if targets.all_targets().contains(&suspend_bb) { + for t in targets.all_targets() { + if *t != suspend_bb && !self.body.basic_blocks[*t].is_cleanup { + return Some(*t); + } + } + } + } + } + } + None + } } diff --git a/src/translate/mir_to_pn/thread_control.rs b/src/translate/mir_to_pn/thread_control.rs index 617a7c5..7c0dddf 100644 --- a/src/translate/mir_to_pn/thread_control.rs +++ b/src/translate/mir_to_pn/thread_control.rs @@ -3,7 +3,9 @@ use super::BodyToPetriNet; use crate::{ memory::pointsto::AliasId, - translate::callgraph::{ThreadControlKind, classify_thread_control}, + translate::callgraph::{ + ThreadControlKind, classify_async_join_by_ty, classify_thread_control, + }, }; use unipn::pt::{PtTransition, TransitionType}; use unipn::TransitionId; @@ -138,12 +140,19 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { bb_idx: &BasicBlock, span: &str, ) -> bool { - if let Some(kind) = classify_thread_control( + let awaited_ty = args + .first() + .and_then(|arg| arg.node.place()) + .map(|place| format!("{:?}", place.ty(self.body, self.tcx).ty)) + .unwrap_or_default(); + let kind = classify_thread_control( self.tcx, callee_def_id, callee_func_name, self.key_api_regex, - ) { + ) + .or_else(|| classify_async_join_by_ty(callee_func_name, &awaited_ty)); + if let Some(kind) = kind { match kind { ThreadControlKind::Spawn => { self.handle_spawn(callee_func_name, args, destination, target, *bb_idx, bb_end); @@ -165,6 +174,16 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { self.handle_rayon_join(callee_func_name, bb_idx, args, target, bb_end, span); return true; } + ThreadControlKind::AsyncSpawn => { + log::info!("[async] AsyncSpawn: {}", callee_func_name); + self.handle_spawn(callee_func_name, args, destination, target, *bb_idx, bb_end); + return true; + } + ThreadControlKind::AsyncJoin => { + log::info!("[async] AsyncJoin: {} (ty={})", callee_func_name, awaited_ty); + self.handle_join(callee_func_name, args, target, *bb_idx, bb_end); + return true; + } } } false @@ -278,6 +297,13 @@ impl<'translate, 'analysis, 'tcx> BodyToPetriNet<'translate, 'analysis, 'tcx> { closure_end, self.spawn_handle_end ); + } else { + let first_ty = args + .first() + .and_then(|a| a.node.place()) + .map(|p| format!("{:?}", p.ty(self.body, self.tcx).ty)) + .unwrap_or_default(); + log::warn!("[async] spawn: failed to resolve future from arg (ty={})", first_ty); } if let Some(transition) = self.net.transition_mut(bb_end) { diff --git a/src/translate/structure.rs b/src/translate/structure.rs index 2ba5c63..df72b82 100644 --- a/src/translate/structure.rs +++ b/src/translate/structure.rs @@ -129,6 +129,8 @@ pub struct KeyApiRegex { pub thread_join: Regex, pub scope_spwan: Regex, pub scope_join: Regex, + pub async_spawn: Regex, + pub async_join: Regex, pub condvar_notify: Regex, pub condvar_wait: Regex, @@ -155,6 +157,8 @@ impl KeyApiRegex { thread_join: make_regex(&config.thread_join), scope_spwan: make_regex(&config.scope_spawn), scope_join: make_regex(&config.scope_join), + async_spawn: make_regex(&config.async_spawn), + async_join: make_regex(&config.async_join), condvar_notify: make_regex(&config.condvar_notify), condvar_wait: make_regex(&config.condvar_wait), channel_send: make_regex(&config.channel_send),