Skip to content

Commit a7d8499

Browse files
authored
Merge pull request #293 from PhysShell/claude/own278-soundness-scope-uqvuiu
fix(extractor): #278 — a `-=` releases only in a proven, unguarded teardown context
2 parents 366bbf9 + 18000d0 commit a7d8499

43 files changed

Lines changed: 1568 additions & 10 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎corpus/real-world/screentogif-loaded-subscription/after.cs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Fix: subscribe with named handlers (so they have a `-=` handle) and detach them
2-
// in Window_Closing. The view-model no longer roots the window, and a repeated
3-
// Loaded no longer stacks duplicate handlers.
2+
// in Window_Closing, wired IN CODE (`Closing += Window_Closing`). The view-model
3+
// no longer roots the window, and a repeated Loaded no longer stacks duplicate
4+
// handlers.
5+
//
6+
// The code wiring is load-bearing (#278 follow-up): a `Window_Closing`-style NAME
7+
// alone proves nothing to the extractor (the XAML attach never reaches it, and a
8+
// bare name may be stale dead code), so the release is credited only because the
9+
// ctor provably attaches the handler to the window's own Closing event.
410
using System;
511
using System.Windows;
612

@@ -12,6 +18,7 @@ public VideoSource()
1218
{
1319
InitializeComponent();
1420
_viewModel = DataContext as VideoSourceViewModel;
21+
Closing += Window_Closing;
1522
}
1623

1724
private void Window_Loaded(object sender, RoutedEventArgs e)

‎corpus/real-world/screentogif-loaded-subscription/notes.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ produces the core's domain-neutral **OWN001** (the severity tiering lives in the
3838
extractor, above the core). As with the rest of the corpus, `case.own` is a hand
3939
reduction of the C# pattern, not verbatim extractor output; `before.cs` / `after.cs`
4040
are representative of the leak and its fix.
41+
42+
**#278 follow-up.** `after.cs` wires `Closing += Window_Closing` in the ctor. The
43+
real ScreenToGif attaches the handler in XAML, which the extractor never sees — and
44+
since #278's follow-up a `Window_Closing`-style *name* alone is NOT a teardown
45+
context (a bare name may be stale dead code; the name-suffix exemption was a
46+
silent-FN hole). The corpus case therefore carries the wiring in code, which is the
47+
honest, provable form of the same fix; the name-only shape is pinned as a BAD case
48+
in `corpus/wpf/subscription-xaml-name-only-release`. A future XAML-aware slice can
49+
credit the XAML attach with actual evidence.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// FIXED. Exactly ONE `Window_Closing` remains — the delegate-compatible one —
2+
// and it holds the `-=`. With the lifecycle event still unresolved, the name
3+
// is now UNAMBIGUOUS (a single same-named method in the immediate class), so
4+
// the fallback may credit it: whichever overload the delegate would pick, it
5+
// is this one.
6+
//
7+
// own-check MUST treat the subscription as released (silent); the unresolved
8+
// `Closing +=` itself stays the usual OWN050 advisory.
9+
using System;
10+
using System.ComponentModel;
11+
12+
public partial class OrdersWindow : Window
13+
{
14+
private readonly INotifyPropertyChanged _orders; // injected, unknown lifetime
15+
16+
public OrdersWindow(INotifyPropertyChanged orders)
17+
{
18+
_orders = orders;
19+
_orders.PropertyChanged += OnOrdersChanged;
20+
Closing += Window_Closing;
21+
}
22+
23+
private void Window_Closing(object sender, CancelEventArgs e)
24+
{
25+
_orders.PropertyChanged -= OnOrdersChanged; // the one and only candidate
26+
}
27+
28+
private void OnOrdersChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// BUGGY (#278 follow-up 2 — the unresolved-overload fallback; hand-reduced
2+
// into case.own).
3+
//
4+
// The class wires `Closing += Window_Closing` on an UNRESOLVED lifecycle event
5+
// (the WPF `Window` base never resolves on a Linux runner without the
6+
// reference pack) and declares TWO `Window_Closing` overloads. The runtime
7+
// delegate attaches exactly ONE of them — chosen by the event's delegate
8+
// signature, which is precisely the information the extractor is missing. The
9+
// delegate-compatible overload detaches nothing; the `-=` sits in the OTHER,
10+
// never-attached overload.
11+
//
12+
// own-check MUST flag the subscription OWN001: an ambiguous name (2+ same-named
13+
// methods) may not ground the teardown, else a `-=` in the wrong overload
14+
// silently swallows the leak. (`Window` is deliberately not defined in-file —
15+
// the unresolved `Closing +=` itself surfaces as the usual OWN050 advisory.)
16+
using System;
17+
using System.ComponentModel;
18+
19+
public partial class OrdersWindow : Window
20+
{
21+
private readonly INotifyPropertyChanged _orders; // injected, unknown lifetime
22+
23+
public OrdersWindow(INotifyPropertyChanged orders)
24+
{
25+
_orders = orders;
26+
_orders.PropertyChanged += OnOrdersChanged;
27+
Closing += Window_Closing;
28+
}
29+
30+
private void Window_Closing(object sender, CancelEventArgs e)
31+
{
32+
// the delegate-compatible overload: detaches nothing
33+
}
34+
35+
private void Window_Closing(object sender, EventArgs e)
36+
{
37+
_orders.PropertyChanged -= OnOrdersChanged; // never attached at runtime
38+
}
39+
40+
private void OnOrdersChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module WpfSubscriptionAmbiguousOverloadWiring
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+
// The ctor subscribes; the wired teardown handler name is AMBIGUOUS (two
13+
// overloads, the event unresolved), and the `-=` sits in the overload the
14+
// delegate never attaches. The executed teardown path performs no release
15+
// => OWN001.
16+
fn OrdersWindow(orders: int) {
17+
let sub = acquire Subscription(orders);
18+
// the attached Window_Closing overload detaches nothing; the `-=` lives
19+
// in the never-attached sibling overload (before.cs)
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN001
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Subscription whose `-=` sits in the never-attached overload of an ambiguously-named wired handler (soundness FN)
2+
3+
**Pattern (#278 follow-up 2 — the unresolved-overload fallback blocker).** The
4+
class wires `Closing += Window_Closing` where the lifecycle event does NOT
5+
resolve (WPF `Window` base on a Linux runner without the reference pack), and
6+
declares TWO `Window_Closing` overloads. A method group syntactically denotes
7+
its whole overload set, but the runtime delegate attaches exactly ONE member —
8+
selected by the event's delegate signature, which is precisely the information
9+
the extractor is missing. Here the delegate-compatible overload
10+
(`(object, CancelEventArgs)`) detaches nothing, and the `-=` sits in the other,
11+
never-attached overload.
12+
13+
**The bug.** The previous slice's unresolved-event fallback added EVERY
14+
same-named own method to the teardown set, so the `-=` in the never-attached
15+
overload was silently credited as a release — the unresolved twin of the
16+
invocation-overload conflation pinned by
17+
`subscription-overload-conflated-cleanup`.
18+
19+
**The fix.** When the handler binds no definite symbol, the name grounds a
20+
teardown ONLY if it is unambiguous — exactly one `IMethodSymbol` with that name
21+
in the immediate class. Zero or 2+ matches credit nothing and keep the honest
22+
warning. The symbol-resolved path is unchanged: when the event resolves, the
23+
delegate's exact target is credited even among overloads (pinned in the smoke
24+
matrix; `subscription-xaml-name-only-release/after.cs` keeps the resolved
25+
single-handler control). The prior fallback's `CandidateSymbols` crediting is
26+
gone with it — candidates of a failed method-group binding are the same
27+
ambiguous overload set by another name.
28+
29+
**`before.cs`** → OWN001 (ambiguous, `-=` unproven). **`after.cs`** — the
30+
positive control: same unresolved event, exactly one `Window_Closing`, `-=`
31+
inside it → silent (whichever overload the delegate would pick, it is that
32+
one). Both keep the usual OWN050 advisory for the unresolved `Closing +=`
33+
itself.
34+
35+
**What the checker says (`.own` reduction).** The scope acquires the token and
36+
the executed teardown path performs no release => **OWN001** with the
37+
subscription-token resource tag.
38+
39+
**Regression guard.** `scripts/benchmark.py`: `before.cs` must be **caught**,
40+
`after.cs` must be **silent**.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// FIXED. The release moved to Dispose — a deterministic teardown the OWNER
2+
// calls, which does not depend on the object first becoming unreachable. (A
3+
// finalizer may still exist for unmanaged state; it just cannot be the
4+
// subscription's release path.)
5+
//
6+
// own-check MUST treat this as released (silent).
7+
using System;
8+
using System.ComponentModel;
9+
10+
public sealed class FinalizerDetachDocument : IDisposable
11+
{
12+
private readonly INotifyPropertyChanged _properties; // injected, unknown lifetime
13+
14+
public FinalizerDetachDocument(INotifyPropertyChanged properties)
15+
{
16+
_properties = properties;
17+
_properties.PropertyChanged += OnPropertiesChanged;
18+
}
19+
20+
public void Dispose()
21+
{
22+
_properties.PropertyChanged -= OnPropertiesChanged; // deterministic teardown
23+
}
24+
25+
private void OnPropertiesChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// BUGGY (#278 follow-up, blocker 1; hand-reduced into case.own).
2+
//
3+
// The ctor subscribes to an injected publisher; the only matching `-=` sits in
4+
// the FINALIZER. That release can never run while it matters: the publisher's
5+
// delegate holds a strong reference to this object, so as long as the
6+
// subscription is live the subscriber is REACHABLE and the GC never finalizes
7+
// it. The `-=` exists precisely on the one path that the leak itself blocks.
8+
// (For a static/process-lived publisher the finalizer is simply never reached
9+
// for the life of the process — same argument, absolute.)
10+
//
11+
// own-check MUST flag this OWN001. Crediting the finalizer was a silent
12+
// false-negative path in the first #278 slice.
13+
using System.ComponentModel;
14+
15+
public sealed class FinalizerDetachDocument
16+
{
17+
private readonly INotifyPropertyChanged _properties; // injected, unknown lifetime
18+
19+
public FinalizerDetachDocument(INotifyPropertyChanged properties)
20+
{
21+
_properties = properties;
22+
_properties.PropertyChanged += OnPropertiesChanged;
23+
}
24+
25+
~FinalizerDetachDocument()
26+
{
27+
// unreachable while subscribed: the delegate keeps `this` alive
28+
_properties.PropertyChanged -= OnPropertiesChanged;
29+
}
30+
31+
private void OnPropertiesChanged(object sender, PropertyChangedEventArgs e) { /* ... */ }
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module WpfSubscriptionFinalizerRelease
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+
// The ctor subscribes; the only `-=` lives in the finalizer. A finalizer runs
13+
// only after the object becomes unreachable — but the live subscription is
14+
// exactly what keeps it reachable, so the release path is blocked by the leak
15+
// it is supposed to fix. Modelled as the ctor scope with no release => OWN001.
16+
fn FinalizerDetachDocument(properties: int) {
17+
let sub = acquire Subscription(properties);
18+
// the finalizer `-=` is not modelled as a release: it cannot run while
19+
// the subscription holds `this` reachable (before.cs)
20+
}

0 commit comments

Comments
 (0)