Skip to content

Commit e79a4f2

Browse files
committed
fix(extractor): green — #305 attacks A/B: early-return guards demote, canonical disposing is branch-aware
The green half of the adversarial audit (previous commit was the red): - IsParamGuardedByEarlyReturn (attack A): a `return` lexically preceding the release site inside the enclosing callable, guarded by a condition that references a parameter, demotes the release — the caller-controlled skip spelled as a sibling instead of an ancestor. The one exemption is the canonical INVERTED disposing exit (`if (!disposing) return;`), which guarantees the site runs on every Dispose() call. Nested lambdas / local functions are not scanned (their returns do not exit the frame); unguarded and field/local-guarded returns keep today's crediting. - SiteOnNegativeBranch (attack B): the canonical positive-`disposing` exemption now covers only a site on the POSITIVE side of the guard; a `-=` or `.Stop()` in the ELSE of `if (disposing)` (or the when-false arm of a ternary) demotes like any parameter guard — that branch is the finalizer path the predicate's own doctrine declares unreachable while the subscription/timer pins the owner. Both rules are folded into the shared IsParamGuardedRelease, so `-=` and timer `.Stop()` inherit them together (one predicate, one context model). IsCanonicalDisposingGuardUse is split into IsCanonicalDisposingParam + IsNegatedGuardUse so the enclosing-guard and early-return rules can invert the canonical polarity without duplicating the use-position analysis. Corpus: the two red subscription fixtures flip to caught; timer twins added (timer-stop-early-return-guard, timer-stop-disposing-else-branch). wpf corpus 28/28 on the .own side; cfg/diag parity regenerated (additive); Rust parity green. The canonical THEN-branch shapes (WinFormsDisposalSample, subscription-param-guarded-unregister after.cs family) keep their crediting — no sample carries the demoted shapes, so extractor goldens stay put; the CI corpus benchmark is the empirical arbiter for the C# side. Worst case of both rules is a kept warning, never a swallowed leak (#238). The general argument-value-aware answer remains #304; this is the bounded pre-cutover floor. Closes #305. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015de4MezSeUnZBoWq1fFU5M
1 parent 7addaad commit e79a4f2

18 files changed

Lines changed: 420 additions & 51 deletions

File tree

‎corpus/wpf/subscription-disposing-else-branch-release/before.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
// publisher's delegate — the finalizer never runs while the subscription is
1111
// live. The release is unreachable; the leak is silently swallowed.
1212
//
13-
// own-check MUST flag this OWN001 (today it stays silent — the FN this pins).
13+
// own-check MUST flag this OWN001. The shipped #293 exception credited it (the
14+
// use-position check never asked which branch holds the site) — the FN this pins.
1415
using System;
1516
using System.ComponentModel;
1617

‎corpus/wpf/subscription-disposing-else-branch-release/notes.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ exact code path wearing different syntax, and it is credited.
3535
directly: on the managed path (`disposing` truthy) the function returns without
3636
releasing — the branch-sensitive core reports OWN001.
3737

38-
**Fix direction (bounded, lexical — matches the #293 style).** The canonical
39-
exception must be branch-aware: a positive `disposing` condition exempts only
40-
sites within its THEN branch. A site in the `else` of the canonical guard is
41-
the finalizer branch and must demote (or be treated as a finalizer non-context
42-
outright). See `docs/notes/teardown-predicate-adversarial-audit.md` (attack B).
38+
**The fix (#305).** The canonical exception is branch-aware now
39+
(`SiteOnNegativeBranch`): a positive `disposing` condition exempts only sites
40+
within its THEN branch; a site in the `else` (or the when-false arm of a
41+
ternary) demotes like any parameter guard. Timer twin:
42+
`timer-stop-disposing-else-branch` (shared predicate). See
43+
`docs/notes/teardown-predicate-adversarial-audit.md` (attack B).
4344

44-
**CI impact.** `before.cs` is a known-miss (recall floor is an absolute count);
45-
`after.cs` is the canonical positive-branch pattern the #293 exception was
46-
built to credit (exercised by `frontend/roslyn/samples/WinFormsDisposalSample.cs`).
45+
**CI impact.** `before.cs` flips to caught with the fix; `after.cs` is the
46+
canonical positive-branch pattern the #293 exception was built to credit
47+
(exercised by `frontend/roslyn/samples/WinFormsDisposalSample.cs`).

‎corpus/wpf/subscription-teardown-early-return-guard/before.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
// (corpus/wpf/subscription-param-guarded-unregister). This case pins the
1717
// asymmetry between the two spellings of one guard.
1818
//
19-
// own-check MUST flag this OWN001 (today it stays silent — the FN this pins).
19+
// own-check MUST flag this OWN001. The shipped #293 predicate credited it (an
20+
// ancestors-only guard walk cannot see a sibling `return`) — the FN this pins.
2021
using System;
2122
using System.ComponentModel;
2223

‎corpus/wpf/subscription-teardown-early-return-guard/notes.md‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ every path"). This mirrors the Python bridge's own D7 fix
3636
a forward makes the transfer conditional) — the same theorem, unfixed on the
3737
extractor side.
3838

39-
**Fix direction (bounded, lexical — matches the #293 style).** Demote a `-=`
40-
when a `return` lexically precedes it inside the enclosing callable and that
41-
`return` is guarded by a condition referencing a parameter — EXCEPT the
42-
canonical negated-disposing early exit (`if (!disposing) return;`), which
43-
guarantees the site *does* run on every `Dispose()` call and must stay
44-
credited. See `docs/notes/teardown-predicate-adversarial-audit.md` (attack A).
39+
**The fix (#305).** `IsParamGuardedByEarlyReturn`, folded into
40+
`IsParamGuardedRelease`: a `return` lexically preceding the site inside the
41+
enclosing callable, guarded by a parameter-referencing condition, demotes —
42+
EXCEPT the canonical negated-disposing early exit (`if (!disposing) return;`),
43+
which guarantees the site *does* run on every `Dispose()` call and stays
44+
credited. Timer twin: `timer-stop-early-return-guard` (shared predicate). See
45+
`docs/notes/teardown-predicate-adversarial-audit.md` (attack A).
4546

46-
**CI impact.** `before.cs` is a known-miss (recall floor is an absolute count —
47-
`caught` does not move); `after.cs` reuses the proven-silent
48-
helper-from-Dispose shape of `subscription-overload-conflated-cleanup`.
47+
**CI impact.** `before.cs` flips to caught with the fix; `after.cs` reuses the
48+
proven-silent helper-from-Dispose shape of
49+
`subscription-overload-conflated-cleanup`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// FIXED. The Stop() sits inside the `if (disposing)` branch — the canonical
2+
// IDisposable pattern: the branch runs on every `Dispose()` call, so the
3+
// release provably runs at teardown.
4+
using System;
5+
6+
public sealed class FeedTicker : IDisposable
7+
{
8+
private readonly DispatcherTimer _timer = new DispatcherTimer();
9+
10+
public FeedTicker()
11+
{
12+
_timer.Tick += OnTick;
13+
_timer.Start();
14+
}
15+
16+
public void Dispose()
17+
{
18+
Dispose(true);
19+
GC.SuppressFinalize(this);
20+
}
21+
22+
private void Dispose(bool disposing)
23+
{
24+
if (disposing)
25+
{
26+
_timer.Stop(); // managed-dispose branch
27+
}
28+
}
29+
30+
~FeedTicker()
31+
{
32+
Dispose(false);
33+
}
34+
35+
private void OnTick(object sender, EventArgs e) { /* ... */ }
36+
}
37+
38+
// In-file stand-in for System.Windows.Threading.DispatcherTimer (WPF is not on
39+
// the corpus reference set; same shape as samples/SampleTypes.cs). NOT
40+
// IDisposable — Stop() IS the release, which is exactly the WPF002 pattern.
41+
public sealed class DispatcherTimer
42+
{
43+
public event EventHandler Tick;
44+
public void Start() { }
45+
public void Stop() { }
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// BUGGY (WPF002 twin of subscription-disposing-else-branch-release: the
2+
// canonical Dispose(bool) pattern with the Stop() in the WRONG branch).
3+
//
4+
// The Stop() sits in the ELSE of `if (disposing)` — it runs only via
5+
// `~FeedTicker() -> Dispose(false)`, and the finalizer never runs while the
6+
// running timer keeps the owner alive. The canonical-disposing exemption
7+
// classifies only the parameter's use in the condition (positive), never which
8+
// branch holds the release site, so the Stop() is credited and the leak is
9+
// silently swallowed.
10+
//
11+
// own-check MUST flag this OWN001 [resource: timer].
12+
using System;
13+
14+
public sealed class FeedTicker : IDisposable
15+
{
16+
private readonly DispatcherTimer _timer = new DispatcherTimer();
17+
18+
public FeedTicker()
19+
{
20+
_timer.Tick += OnTick;
21+
_timer.Start();
22+
}
23+
24+
public void Dispose()
25+
{
26+
Dispose(true);
27+
GC.SuppressFinalize(this);
28+
}
29+
30+
private void Dispose(bool disposing)
31+
{
32+
if (disposing)
33+
{
34+
// managed cleanup of other state; the Stop() was misplaced below
35+
}
36+
else
37+
{
38+
_timer.Stop(); // finalizer-only branch
39+
}
40+
}
41+
42+
~FeedTicker()
43+
{
44+
Dispose(false);
45+
}
46+
47+
private void OnTick(object sender, EventArgs e) { /* ... */ }
48+
}
49+
50+
// In-file stand-in for System.Windows.Threading.DispatcherTimer (WPF is not on
51+
// the corpus reference set; same shape as samples/SampleTypes.cs). NOT
52+
// IDisposable — Stop() IS the release, which is exactly the WPF002 pattern.
53+
public sealed class DispatcherTimer
54+
{
55+
public event EventHandler Tick;
56+
public void Start() { }
57+
public void Stop() { }
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module WpfTimerStopDisposingElseBranch
2+
3+
// A running timer: `Elapsed += handler` + `Start()` acquires the timer<->owner
4+
// edge; the matching `Stop()` releases it. `kind` tags the resource so the
5+
// generic ownership finding carries a [resource: timer] note.
6+
resource Timer {
7+
acquire Start
8+
release Stop
9+
kind "timer"
10+
}
11+
12+
// Dispose(bool disposing) with the Stop() in the ELSE branch: on the managed
13+
// path (disposing=true — the only path that runs while the owner is reachable)
14+
// the timer is never stopped => OWN001.
15+
fn DisposeBool(interval: int, disposing: int) {
16+
let t = acquire Timer(interval);
17+
if (disposing) {
18+
return; // managed-dispose path exits WITHOUT stopping
19+
}
20+
release t; // finalizer-only branch: unreachable while running
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN001
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# WPF002 twin: a Stop() in the ELSE of the canonical `if (disposing)` (soundness FN, #305 attack B)
2+
3+
The timer twin of `subscription-disposing-else-branch-release` — `.Stop()`
4+
rides the same teardown/guard predicate as the `-=`, so the else-branch hole
5+
applied verbatim: a Stop() that executes only via `~Finalizer ->
6+
Dispose(false)` was credited as a teardown release, while the finalizer itself
7+
is a declared non-context.
8+
9+
Fixed together with the subscription case: the canonical positive-`disposing`
10+
exemption now covers only a site on the POSITIVE side of the guard
11+
(`SiteOnNegativeBranch`) — a release in the `else` demotes like any parameter
12+
guard. The `if (disposing)` THEN-branch pattern (`after.cs`,
13+
`timer-stop-param-guarded`-family shapes) stays credited unchanged. Full
14+
attack record: `docs/notes/teardown-predicate-adversarial-audit.md`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// FIXED. The flag is gone: Dispose() -> Shutdown() and the helper stops the
2+
// timer unconditionally on the resolved teardown path.
3+
using System;
4+
5+
public sealed class FeedTicker : IDisposable
6+
{
7+
private readonly DispatcherTimer _timer = new DispatcherTimer();
8+
9+
public FeedTicker()
10+
{
11+
_timer.Tick += OnTick;
12+
_timer.Start();
13+
}
14+
15+
public void Dispose()
16+
{
17+
Shutdown();
18+
}
19+
20+
private void Shutdown()
21+
{
22+
_timer.Stop(); // unconditional, on the teardown path
23+
}
24+
25+
private void OnTick(object sender, EventArgs e) { /* ... */ }
26+
}
27+
28+
// In-file stand-in for System.Windows.Threading.DispatcherTimer (WPF is not on
29+
// the corpus reference set; same shape as samples/SampleTypes.cs). NOT
30+
// IDisposable — Stop() IS the release, which is exactly the WPF002 pattern.
31+
public sealed class DispatcherTimer
32+
{
33+
public event EventHandler Tick;
34+
public void Start() { }
35+
public void Stop() { }
36+
}

0 commit comments

Comments
 (0)