Skip to content

Commit 36d6834

Browse files
authored
Merge pull request #306: fix(bridge+extractor): #294 tolerant-door contract + #305 teardown-predicate soundness
fix(bridge+extractor): #294 tolerant-door contract + #305 teardown-predicate soundness (audit → red → green)
2 parents ad8df31 + f6bf31c commit 36d6834

34 files changed

Lines changed: 1119 additions & 199 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// FIXED. The `-=` sits inside the `if (disposing)` branch — the canonical
2+
// IDisposable pattern the #293 disposing exception was built for: the branch
3+
// runs on every `Dispose()` call, so the release provably runs at teardown.
4+
//
5+
// own-check MUST treat this as released (silent): a POSITIVE use of the single
6+
// bool parameter of `Dispose(bool)` is the one canonical guard that does not
7+
// demote.
8+
using System;
9+
using System.ComponentModel;
10+
11+
public sealed class ReportView : IDisposable
12+
{
13+
private readonly INotifyPropertyChanged _properties; // injected, unknown lifetime
14+
15+
public ReportView(INotifyPropertyChanged properties)
16+
{
17+
_properties = properties;
18+
_properties.PropertyChanged += new PropertyChangedEventHandler(OnPropertiesChanged);
19+
}
20+
21+
public void Dispose()
22+
{
23+
Dispose(true);
24+
GC.SuppressFinalize(this);
25+
}
26+
27+
private void Dispose(bool disposing)
28+
{
29+
if (disposing)
30+
{
31+
_properties.PropertyChanged -= OnPropertiesChanged; // managed-dispose branch
32+
}
33+
}
34+
35+
~ReportView()
36+
{
37+
Dispose(false);
38+
}
39+
40+
private void OnPropertiesChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
41+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// BUGGY (the canonical Dispose(bool) pattern with the `-=` in the WRONG branch).
2+
//
3+
// The `-=` sits in the ELSE of `if (disposing)` — the finalizer-only branch.
4+
// The extractor's canonical-disposing exception checks only how the PARAMETER
5+
// IDENTIFIER is used in the condition (positive, not negated), never WHICH
6+
// branch the release site sits in — so this `-=` is credited as an unguarded
7+
// teardown release. But the else branch runs only via `~ReportView()` ->
8+
// `Dispose(false)`, and by the extractor's own finalizer doctrine (#278
9+
// follow-up 1) a live subscription keeps the subscriber REACHABLE through the
10+
// publisher's delegate — the finalizer never runs while the subscription is
11+
// live. The release is unreachable; the leak is silently swallowed.
12+
//
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.
15+
using System;
16+
using System.ComponentModel;
17+
18+
public sealed class ReportView : IDisposable
19+
{
20+
private readonly INotifyPropertyChanged _properties; // injected, unknown lifetime
21+
22+
public ReportView(INotifyPropertyChanged properties)
23+
{
24+
_properties = properties;
25+
_properties.PropertyChanged += new PropertyChangedEventHandler(OnPropertiesChanged);
26+
}
27+
28+
public void Dispose()
29+
{
30+
Dispose(true);
31+
GC.SuppressFinalize(this);
32+
}
33+
34+
private void Dispose(bool disposing)
35+
{
36+
if (disposing)
37+
{
38+
// managed cleanup of other state; the unsubscribe was misplaced below
39+
}
40+
else
41+
{
42+
_properties.PropertyChanged -= OnPropertiesChanged; // finalizer-only branch
43+
}
44+
}
45+
46+
~ReportView()
47+
{
48+
Dispose(false);
49+
}
50+
51+
private void OnPropertiesChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module WpfSubscriptionDisposingElseBranchRelease
2+
3+
// A subscription token: `+= handler` acquires the source<->listener edge; the
4+
// matching `-= handler` releases it. `kind` tags the resource so the generic
5+
// ownership finding carries a [resource: ...] note.
6+
resource Subscription {
7+
acquire Subscribe
8+
release Unsubscribe
9+
kind "subscription token"
10+
}
11+
12+
// Dispose(bool disposing) with the release in the ELSE branch: on the managed
13+
// path (disposing=true — the only path that ever runs while the object is
14+
// reachable) the token is never released. The core is branch-sensitive and
15+
// flags it; the C# extractor's canonical-disposing exception looks only at the
16+
// parameter's use in the condition, not at which branch holds the release, so
17+
// before.cs stays silent — an extractor gap, not a core gap.
18+
fn DisposeBool(properties: int, disposing: int) {
19+
let sub = acquire Subscription(properties);
20+
if (disposing) {
21+
return; // managed-dispose path exits WITHOUT releasing
22+
}
23+
release sub; // finalizer-only branch: unreachable while subscribed
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN001
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# The canonical `if (disposing)` exception credits a `-=` in the ELSE branch (soundness FN)
2+
3+
**Pattern.** The textbook `Dispose(bool)` pattern with the unsubscribe
4+
misplaced into the finalizer-only branch:
5+
6+
```csharp
7+
private void Dispose(bool disposing)
8+
{
9+
if (disposing) { /* ... */ }
10+
else
11+
{
12+
source.Changed -= OnChanged; // runs only via ~Finalizer -> Dispose(false)
13+
}
14+
}
15+
```
16+
17+
**The bug (Own.NET extractor).** `IsParamGuardedRelease` demotes any release
18+
under a parameter-dependent condition — EXCEPT the canonical positive
19+
`disposing` guard of `Dispose(bool)` (`IsCanonicalDisposingGuardUse`,
20+
`frontend/roslyn/OwnSharp.Extractor/Program.cs`). That exception classifies the
21+
**use of the parameter identifier in the condition** (not negated, not
22+
`== false`) and never asks **which branch the release site is in**. A site in
23+
the `else` of a positive `if (disposing)` sees the same positive condition, so
24+
it is credited — even though it executes only when `disposing == false`, i.e.
25+
on the finalizer path.
26+
27+
That contradicts the extractor's own finalizer doctrine (#278 follow-up 1): for
28+
a subscription leak the subscriber stays reachable through the publisher's
29+
delegate, so the finalizer never runs while the subscription is live — a `-=`
30+
on that path can never break the hold. A `-=` in a finalizer body is already a
31+
non-context; the same `-=` moved into the `Dispose(false)`-only branch is that
32+
exact code path wearing different syntax, and it is credited.
33+
34+
**Why the core is not the gap.** The `.own` reduction models the two branches
35+
directly: on the managed path (`disposing` truthy) the function returns without
36+
releasing — the branch-sensitive core reports OWN001.
37+
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).
44+
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`).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// FIXED. The flag is gone: Dispose() -> Cleanup() and the helper releases
2+
// unconditionally. The symbol-based teardown closure resolves the call and
3+
// credits exactly this helper (the same proven shape as
4+
// subscription-overload-conflated-cleanup/after.cs).
5+
//
6+
// own-check MUST treat this as released (silent): the `-=` is on the resolved
7+
// teardown path, matches the `+=`'s (receiver, handler) pair, and no parameter
8+
// of the enclosing method can skip it.
9+
using System;
10+
using System.ComponentModel;
11+
12+
public sealed class SessionDocument : IDisposable
13+
{
14+
private readonly INotifyPropertyChanged _properties; // injected, unknown lifetime
15+
16+
public SessionDocument(INotifyPropertyChanged properties)
17+
{
18+
_properties = properties;
19+
_properties.PropertyChanged += new PropertyChangedEventHandler(OnPropertiesChanged);
20+
}
21+
22+
public void Dispose() => Cleanup();
23+
24+
private void Cleanup()
25+
{
26+
_properties.PropertyChanged -= OnPropertiesChanged; // unconditional, on the teardown path
27+
}
28+
29+
private void OnPropertiesChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// BUGGY (the #278 parameter guard, rewritten as an early return — semantically
2+
// identical to `if (!keepAlive) { -= }`, syntactically invisible to the shipped
3+
// guard detector).
4+
//
5+
// Dispose() IS a teardown and it DOES call the helper — but it calls it with
6+
// keepAlive: true ("light teardown", the SectorTS UnregOnlyGoodys idiom), and
7+
// the helper's `-=` sits AFTER `if (keepAlive) return;`. The release provably
8+
// never runs on any path, yet own-check credits it:
9+
// * the symbol-based teardown closure adds Cleanup (Dispose calls it —
10+
// argument VALUES are not consulted), so `InTeardownContext` says yes;
11+
// * `IsParamGuardedRelease` inspects only ENCLOSING conditions of the `-=`
12+
// site — an early `return` is a preceding SIBLING statement, not an
13+
// ancestor, so the caller-controlled guard is invisible.
14+
// The enclosing-branch spelling of the exact same guard —
15+
// `if (!keepAlive) { ... -= ...; }` — IS demoted
16+
// (corpus/wpf/subscription-param-guarded-unregister). This case pins the
17+
// asymmetry between the two spellings of one guard.
18+
//
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.
21+
using System;
22+
using System.ComponentModel;
23+
24+
public sealed class SessionDocument : IDisposable
25+
{
26+
private readonly INotifyPropertyChanged _properties; // injected, unknown lifetime
27+
28+
public SessionDocument(INotifyPropertyChanged properties)
29+
{
30+
_properties = properties;
31+
_properties.PropertyChanged += new PropertyChangedEventHandler(OnPropertiesChanged);
32+
}
33+
34+
public void Dispose()
35+
{
36+
Cleanup(keepAlive: true); // "light" teardown: the production path
37+
}
38+
39+
public void Cleanup(bool keepAlive)
40+
{
41+
if (keepAlive)
42+
return; // every caller passes true
43+
_properties.PropertyChanged -= OnPropertiesChanged; // never reached
44+
}
45+
46+
private void OnPropertiesChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module WpfSubscriptionTeardownEarlyReturnGuard
2+
3+
// A subscription token: `+= handler` acquires the source<->listener edge; the
4+
// matching `-= handler` releases it. `kind` tags the resource so the generic
5+
// ownership finding carries a [resource: ...] note.
6+
resource Subscription {
7+
acquire Subscribe
8+
release Unsubscribe
9+
kind "subscription token"
10+
}
11+
12+
// Dispose calls Cleanup(keepAlive=true); inside the helper the release sits
13+
// AFTER `if (keepAlive) return;` — the early-return spelling of the #278 flag
14+
// guard. On the production path (keepAlive=true) the token is never released.
15+
// The core is branch-sensitive and flags it; the C# extractor's guard detector
16+
// inspects only ENCLOSING conditions of the `-=` site, so before.cs stays
17+
// silent — an extractor gap, not a core gap.
18+
fn CleanupKeepAlive(properties: int, keepAlive: int) {
19+
let sub = acquire Subscription(properties);
20+
if (keepAlive) {
21+
return; // production callers pass true
22+
}
23+
release sub; // the early-return-guarded `-=`
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN001
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# A parameter guard spelled as an early return defeats the #278 guard detector (soundness FN)
2+
3+
**Pattern.** The exact SectorTS `UnregOnlyGoodys` guard from
4+
`subscription-param-guarded-unregister`, rewritten from the enclosing-branch
5+
form into the early-return form:
6+
7+
```csharp
8+
// demoted today (enclosing branch): // credited today (early return):
9+
void Cleanup(bool keepAlive) void Cleanup(bool keepAlive)
10+
{ {
11+
if (!keepAlive) if (keepAlive)
12+
{ return;
13+
source.Changed -= OnChanged; source.Changed -= OnChanged;
14+
} }
15+
}
16+
```
17+
18+
The two spellings are semantically identical — the caller chooses whether the
19+
`-=` runs — but only the left one is demoted.
20+
21+
**The bug (Own.NET extractor).** `IsParamGuardedRelease`
22+
(`frontend/roslyn/OwnSharp.Extractor/Program.cs`, #278 rule 2) walks the
23+
**lexical ancestors** of the `-=` site and demotes when an enclosing
24+
`if`/`?:`/`switch`/`while`/`for` condition references a parameter. An early
25+
`return` guard is a preceding **sibling** statement, never an ancestor, so it
26+
is invisible. Meanwhile the symbol-based teardown closure
27+
(`TeardownContextMethods`) adds `Cleanup` because `Dispose()` calls it —
28+
argument **values** are not consulted, so `Cleanup(keepAlive: true)` credits
29+
the helper even though that call provably skips the release. Net: `before.cs`
30+
is silent, and the leak class #293 closed reopens one refactoring away.
31+
32+
**Why the core is not the gap.** The `.own` reduction models the guard as the
33+
branch it is, and the branch-sensitive core flags it (OWN001, "not released on
34+
every path"). This mirrors the Python bridge's own D7 fix
35+
(`docs/notes/interprocedural-tz.md`, INF-S3: an early `return` reachable before
36+
a forward makes the transfer conditional) — the same theorem, unfixed on the
37+
extractor side.
38+
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).
46+
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`.

0 commit comments

Comments
 (0)