Skip to content

Commit b803d22

Browse files
authored
feat(extractor): inter-procedural consume contract → use-after-handoff OWN002 (recall 8/10→9/11) (#55)
A use-after-handoff — a stream handed to a consumer that owns and disposes it, then touched again — was missed because the extractor treated every argument-pass as an escape (untracked). Now a call to a first-party CONSUMER (a method whose own body disposes a by-value IDisposable parameter) is modelled as a RELEASE of the matching argument at the call site (the pool-Return pattern, via ConsumeReleaseArgs + DisposesLocal); a use of the argument after the call is then OWN002. The escape exemption is gated on the same body-inspection (and the `Consume(s);` statement form), so an argument is exempted iff it is released. Inspecting the callee's own body means no cross-call signature table and no dangling-callee crash; arguments resolve to parameters by name or position, across partial declarations. New fixture ownership-handoff-use (a pure use-after-handoff) is a miss before and a catch (OWN002) after: recall 8/10 -> 9/11, 0 FP, CI floor --min-recall 9. ownership-handoff-consume now fires both arms (OWN001+OWN002).
1 parent c90a631 commit b803d22

9 files changed

Lines changed: 217 additions & 26 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -625,13 +625,13 @@ jobs:
625625
- name: Score the corpus on real C#
626626
# Precision is gated absolutely (every fix silent, zero false positives);
627627
# recall is pinned at the measured floor and ratchets up as the extractor
628-
# improves. Now 8/10 — pooled buffers ride the path-sensitive flow engine
629-
# (Rent = acquire, Return = release: OWN003/OWN002, pool resolved via the Roslyn
630-
# SemanticModel so an ALIASED receiver is caught), and ownership-transferring
631-
# factory acquires (System.IO.File.Open*/Create*) are now recognised alongside
632-
# `new`, so the leak arm of the interprocedural-handoff case fires OWN001.
633-
# Remaining backlog: the use-after-handoff (OWN002) arm of that case, a
634-
# cross-method use-after-dispose, and an injected-source region-escape. A drop
635-
# below the floor is a regression.
636-
run: python scripts/benchmark.py --min-recall 8
628+
# improves. Now 9/11 — pooled buffers ride the path-sensitive flow engine
629+
# (Rent/Return: OWN003/OWN002, pool resolved via the Roslyn SemanticModel so an
630+
# ALIASED receiver is caught), factory acquires (System.IO.File.Open*/Create*) are
631+
# recognised alongside `new`, and the inter-procedural CONSUME contract is modelled:
632+
# a first-party method owning a by-value IDisposable param is a handoff (a `call`
633+
# op), so a use after the handoff trips OWN002 (the cut is the signature, like
634+
# Rust's move). Remaining backlog: a cross-method use-after-dispose and an
635+
# injected-source region-escape. A drop below the floor is a regression.
636+
run: python scripts/benchmark.py --min-recall 9
637637

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.IO;
3+
4+
// FIX: read everything we need BEFORE handing ownership off, then never touch the stream.
5+
static class HandoffUse
6+
{
7+
public static void Consume(Stream sink)
8+
{
9+
sink.CopyTo(Stream.Null);
10+
sink.Dispose();
11+
}
12+
13+
static long Run(string path)
14+
{
15+
var s = File.OpenRead(path);
16+
long len = s.Length; // read first ...
17+
Consume(s); // ... then move ownership last
18+
return len;
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.IO;
3+
4+
// A pure inter-procedural use-after-handoff (no leak arm): a stream is handed to a
5+
// consumer that takes OWNERSHIP (reads it, then disposes it), and the caller then touches
6+
// the stream again. Unlike ownership-handoff-consume there is no leak arm -- the handoff
7+
// itself is correct, so the ONLY bug is the use AFTER ownership moved. Common shape:
8+
// serialize/compress into a stream, hand it to a sink that owns it, then accidentally read
9+
// it once more (an ObjectDisposedException at runtime).
10+
static class HandoffUse
11+
{
12+
// Consumer: takes ownership of `sink` and closes it. `sink` is `consume Stream`.
13+
public static void Consume(Stream sink)
14+
{
15+
sink.CopyTo(Stream.Null);
16+
sink.Dispose(); // Consume owns and closes it
17+
}
18+
19+
// BUG: ownership moved into Consume (which disposed it), then the stream is read. -> OWN002
20+
static long Run(string path)
21+
{
22+
var s = File.OpenRead(path);
23+
Consume(s); // ownership moves to Consume
24+
return s.Length; // use-after-handoff (s is disposed) -> OWN002
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// OwnLang model of a PURE inter-procedural use-after-handoff (no leak arm). `take` takes
2+
// the stream BY VALUE (a resource type => CONSUME): ownership moves in and it closes the
3+
// stream, so a caller must not touch it after the handoff. `run` hands off then reads ->
4+
// OWN002 (use after the resource was consumed by the callee). The fix reads BEFORE the
5+
// handoff. The signature is the cut -- the caller is checked against `take`'s contract,
6+
// not its body; no whole-program analysis. (`consume` is an OwnLang keyword, so the
7+
// reduction names the consumer `take`; the C# consumer is `Consume`.)
8+
module Corpus
9+
resource Stream {
10+
acquire open
11+
release close
12+
}
13+
fn take(s: Stream) {
14+
use s;
15+
release s;
16+
}
17+
fn run() {
18+
let s = acquire Stream();
19+
take(s); // ownership moves into the consumer
20+
use s; // <-- touched after handoff -> OWN002
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN002
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Inter-procedural use-after-handoff (pure)
2+
3+
**Pattern:** a stream is handed to a consumer that takes **ownership** (reads it, then
4+
`Dispose()`s it), and the caller then touches the stream again. Unlike
5+
`ownership-handoff-consume` there is **no leak arm** — the handoff itself is correct, so the
6+
*only* bug is the use **after** ownership moved. Common shape: serialize/compress into a
7+
stream, hand it to a sink that owns it, then accidentally read it once more (an
8+
`ObjectDisposedException` at runtime).
9+
10+
**What the checker says:** using a resource after it was consumed by a callee is the generic
11+
**OWN002** (use after release) — the same code `.own` produces for use-after-dispose.
12+
13+
**Why this case exists (the consume-contract proof).** The extractor used to treat any
14+
argument-passing as an *escape* (untracked), so a stream handed to `Consume(s)` simply
15+
vanished and the later `s.Length` was invisible — a **miss**. Now a call to a first-party
16+
**consumer** — a method whose own body disposes a by-value `IDisposable` parameter — is
17+
modelled as a **release of the argument at the call site**, the same shape as pool
18+
`Return(buf)` (the resource leaves the caller's hands right there). The use *after* that
19+
release then trips **OWN002**. The signal is the callee's own body, so it is inter-procedural
20+
without a cross-call signature table (and so without a dangling-callee crash). This fixture is
21+
a *miss* before and a *catch* after; `ownership-handoff-consume` is caught for its leak arm
22+
either way, so this is the row that makes the use-after-handoff capability a measurable ratchet.
23+
24+
**Honesty / scope.** `case.own` is a faithful hand reduction of the C# pattern, not C# the
25+
`.own` checker ingested. `before.cs` / `after.cs` are representative of the bug and its fix,
26+
not a verbatim copy of one PR.

‎docs/notes/corpus-benchmark.md‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ correct code), and **recall was 3/9** — the three caught are exactly the
2626
subscription/region class the extractor is strongest at (`zombie-viewmodel` →
2727
OWN001, two static-event escapes → OWN014).
2828

29-
## Ratchet → 8/10 (four ratchets)
29+
## Ratchet → 9/11 (five ratchets)
3030

3131
### → 4/9: a fixture was understating us
3232

@@ -89,14 +89,33 @@ handed back by some other API is never mistaken for an acquire). With it the lea
8989
8/10**. The blast radius is exactly one file: nothing else in the corpus or the samples opens
9090
a `File.*` stream, so no `after.cs` and no dog-food scan can newly cry wolf.
9191

92-
The remaining gaps are genuine **frontend extraction** islands. The *use-after-handoff*
93-
(`OWN002`) arm of `ownership-handoff-consume` — caught only as the leak today — needs the
94-
inter-procedural **consume** contract (a method that disposes a by-value parameter, checked at
95-
call sites like Rust's move; the cut is the *signature*, no whole-program points-to). A
96-
field/cross-method use-after-dispose needs cross-method field-state. And the injected-source
97-
region-escape (`viewmodel-escapes-to-app`) needs the source's lifetime *proven* — its DI
98-
registration — which the fixture does not even carry. The `.own` reductions catch all three;
99-
the C# extractor does not yet. Each is a real capability the floor will ratchet up to as it lands.
92+
### → 9/11: the inter-procedural consume contract
93+
94+
The last handoff gap was the *use-after-handoff* arm — a stream handed to a consumer that
95+
disposes it, then touched again (`OWN002`). The extractor treated every argument-pass as an
96+
*escape* (untracked), so the handed-off stream vanished and the later read was invisible. Now
97+
a call to a first-party **consumer** — a method whose own body disposes a by-value
98+
`IDisposable` parameter — is modelled as a **release of the argument at the call site**, the
99+
same shape as pool `Return(buf)` (the resource leaves the caller's hands right there). A use of
100+
the argument *after* that call is then a use-after-release, `OWN002`; the matching argument is
101+
exempted from the escape set so it stays tracked through the handoff. It is inter-procedural —
102+
the signal is the *callee's own body* — but there is **no cross-call signature table**, so a
103+
callee with no body to inspect (interface / abstract / extern) or that doesn't dispose the
104+
parameter yields nothing and the argument stays an ordinary escape. Crucially there is no
105+
dangling `call` op to a method the flow pass never lowered (the early call-op design crashed
106+
the bridge on exactly that — `UnitOfWorkFlowSample`'s consumer, whose body the pass skips), and
107+
the escape exemption is gated on the *same* body-inspection as the release, so an argument is
108+
exempted **iff** it is also released — never a tracked-but-unreleased local that would read as a
109+
false leak. A new fixture `ownership-handoff-use` (a *pure* use-after-handoff, no leak arm) is a
110+
miss before and a catch after, so **recall is now 9/11**; `ownership-handoff-consume` now fires
111+
both its arms (`OWN001`+`OWN002`). The verdicts were pinned locally (hand-built facts →
112+
`check_facts` → the exact `OWN001`/`OWN002` and silence on the fixes).
113+
114+
The remaining gaps are genuine **frontend extraction** islands: a field/cross-method
115+
use-after-dispose needs cross-method field-state, and the injected-source region-escape
116+
(`viewmodel-escapes-to-app`) needs the source's lifetime *proven* — its DI registration —
117+
which the fixture does not even carry. The `.own` reductions catch both; the C# extractor does
118+
not yet. Each is a real capability the floor will ratchet up to as it lands.
100119

101120
## Why catch/clean, not exact-code match
102121

‎docs/proposals/P-012-bug-corpus-mining.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(the bug is caught) and specificity (the fix is silent), gated in the
77
`corpus-benchmark` CI job. This is the measurement spine — the defensible number,
88
and the verifiable reward for any future learning loop. First measurement **3/9
9-
caught · 9/9 clean · 0 FP**, ratcheted to **8/10** over four steps: (1) a *fixture*
9+
caught · 9/9 clean · 0 FP**, ratcheted to **9/11** over five steps: (1) a *fixture*
1010
was understating us — `screentogif-loaded-subscription` referenced an undeclared VM
1111
type → `OWN050`, fixed by making it self-contained; (2) a real *capability* —
1212
pooled buffers are now routed through the path-sensitive flow engine (Rent =
@@ -17,10 +17,14 @@
1717
receiver (`var p = ArrayPool<int>.Shared; p.Return(buf)`) — a miss for the text
1818
heuristic — is caught (`arraypool-aliased-receiver`, +1 row); (4) ownership-transferring
1919
**factory acquires** (`System.IO.File.Open*`/`Create*`) are recognised alongside `new`,
20-
so the leak arm of the interprocedural-handoff case fires `OWN001`. Perfect precision
21-
throughout. The remaining gaps: the use-after-handoff (`OWN002`) arm of that case (needs
22-
the inter-procedural *consume* contract), a cross-method use-after-dispose, and an
23-
injected-source region-escape — the tracked recall backlog the floor ratchets up to.
20+
so the leak arm of the interprocedural-handoff case fires `OWN001`; (5) the inter-procedural
21+
**consume handoff** — a call to a first-party consumer (a method whose own body disposes a
22+
by-value `IDisposable` param) is modelled as a *release* of the argument at the call site (the
23+
pool-`Return` shape), so a use after the handoff trips `OWN002`; the signal is the callee's
24+
own body, so there is no cross-call signature table and no dangling-callee crash
25+
(`ownership-handoff-use`, +1 row). Perfect precision throughout. The remaining gaps: a
26+
cross-method use-after-dispose,
27+
and an injected-source region-escape — the tracked recall backlog the floor ratchets up to.
2428
Still ahead: those, GitHub mining at scale (stage 1) and the 50–100-repo prevalence
2529
scan (stage 2). See
2630
[docs/notes/corpus-benchmark.md](../notes/corpus-benchmark.md).

‎frontend/roslyn/OwnSharp.Extractor/Program.cs‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,20 @@ static void EmitFlowExpr(ExpressionSyntax expr, HashSet<string> tracked, Semanti
704704
nodes.Add(new { op = "release", var = pbuf, line = LineOf(expr) });
705705
return;
706706
}
707-
// any other reference to a tracked local -> use (once per local in this expr).
707+
// Foo(s) where Foo consumes (disposes) a by-value IDisposable parameter -> the handoff
708+
// RELEASES the matching argument(s) here (the inter-procedural consume contract,
709+
// modelled at the call site like pool Return). A later use of an argument is then a
710+
// use-after-handoff (OWN002). Do NOT return — other tracked arguments of the same call
711+
// (`Consume(s, t)`) still need their `use` below; a consumed arg is excluded from it.
712+
var consumed = ConsumeReleaseArgs(expr, model);
713+
foreach (var c in consumed)
714+
if (tracked.Contains(c))
715+
nodes.Add(new { op = "release", var = c, line = LineOf(expr) });
716+
// any other reference to a tracked local -> use (once per local; a consumed arg is a
717+
// release above, never also a use).
708718
var used = new SortedSet<string>(StringComparer.Ordinal);
709719
foreach (var idn in expr.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>())
710-
if (tracked.Contains(idn.Identifier.Text))
720+
if (tracked.Contains(idn.Identifier.Text) && !consumed.Contains(idn.Identifier.Text))
711721
used.Add(idn.Identifier.Text);
712722
foreach (var u in used)
713723
nodes.Add(new { op = "use", var = u, line = LineOf(expr) });
@@ -783,6 +793,58 @@ static bool IsOwningFactory(ExpressionSyntax? e, SemanticModel model)
783793
return ns is { Name: "System" } && ns.ContainingNamespace is { IsGlobalNamespace: true };
784794
}
785795

796+
// The local names of arguments handed to a first-party CONSUMER at this call — a method
797+
// whose own body disposes the by-value IDisposable parameter the argument binds to. Such
798+
// an argument's ownership moves into the callee and is discharged there, so the handoff is
799+
// modelled as a RELEASE of the argument at the call site (the same shape as pool
800+
// `Return(buf)`); a later use is then a use-after-handoff (OWN002). Inspecting the callee's
801+
// OWN body means no cross-call signature table and no dangling-callee crash — a callee with
802+
// no body (interface / abstract / extern) or that does not dispose the param contributes
803+
// nothing, and the argument stays an ordinary escape. Arguments resolve to parameters by
804+
// NAME when `name:` is used, else by position; partial declarations are scanned for a body.
805+
static List<string> ConsumeReleaseArgs(ExpressionSyntax e, SemanticModel model)
806+
{
807+
var consumed = new List<string>();
808+
if (e is not InvocationExpressionSyntax inv
809+
|| model.GetSymbolInfo(inv).Symbol is not IMethodSymbol sym)
810+
return consumed;
811+
SyntaxNode? body = null;
812+
foreach (var r in sym.DeclaringSyntaxReferences)
813+
if (r.GetSyntax() is BaseMethodDeclarationSyntax d
814+
&& ((SyntaxNode?)d.Body ?? d.ExpressionBody) is { } b)
815+
{
816+
body = b;
817+
break;
818+
}
819+
if (body is null)
820+
return consumed;
821+
var args = inv.ArgumentList.Arguments;
822+
for (int i = 0; i < args.Count; i++)
823+
{
824+
// map argument -> parameter: by name for `name: value`, else by position.
825+
var p = args[i].NameColon is { } nc
826+
? sym.Parameters.FirstOrDefault(q => q.Name == nc.Name.Identifier.Text)
827+
: (i < sym.Parameters.Length ? sym.Parameters[i] : null);
828+
if (p is { RefKind: RefKind.None } && ImplementsIDisposable(p.Type)
829+
&& DisposesLocal(body, p.Name)
830+
&& args[i].Expression is IdentifierNameSyntax aid)
831+
consumed.Add(aid.Identifier.Text);
832+
}
833+
return consumed;
834+
}
835+
836+
// Does `body` dispose the local/parameter named `name` — a `name.Dispose()` / `.Close()` /
837+
// `.DisposeAsync()` call (the consume signal)?
838+
static bool DisposesLocal(SyntaxNode body, string name)
839+
{
840+
foreach (var i in body.DescendantNodesAndSelf().OfType<InvocationExpressionSyntax>())
841+
if (i.Expression is MemberAccessExpressionSyntax m
842+
&& m.Name.Identifier.Text is "Dispose" or "Close" or "DisposeAsync"
843+
&& m.Expression is IdentifierNameSyntax id && id.Identifier.Text == name)
844+
return true;
845+
return false;
846+
}
847+
786848
// A field/local type treated as owned-disposable (syntax-only heuristic — no
787849
// semantic model): a curated set plus a few suffixes. Gated on the class `new`ing
788850
// the value, so injected/borrowed disposables are not flagged. Timer types are
@@ -1343,9 +1405,21 @@ or ImplicitObjectCreationExpressionSyntax } init
13431405
var nm = idn.Identifier.Text;
13441406
if (!candidates.Contains(nm))
13451407
continue;
1408+
// ... unless it is handed to a CONSUMER (a first-party method that
1409+
// disposes a by-value IDisposable param) as a bare `Consume(s);`
1410+
// statement: that is a handoff RELEASED at the call site, not an escape
1411+
// (else the use-after-handoff would be hidden). Tied to the statement
1412+
// form the flow pass lowers, so a local is never exempted without a
1413+
// matching release (a `var n = Consume(s)` initializer is NOT lowered
1414+
// here, so it stays an escape rather than a false leak).
1415+
bool consumedArg = idn.Parent is ArgumentSyntax
1416+
&& idn.Parent.Parent is ArgumentListSyntax
1417+
&& idn.Parent.Parent.Parent is InvocationExpressionSyntax cinv
1418+
&& cinv.Parent is ExpressionStatementSyntax
1419+
&& ConsumeReleaseArgs(cinv, model).Contains(nm);
13461420
if (idn.Parent is ReturnStatementSyntax
13471421
|| (idn.Parent is AssignmentExpressionSyntax asg && asg.Right == idn)
1348-
|| (idn.Parent is ArgumentSyntax && !poolBuffers.Contains(nm)))
1422+
|| (idn.Parent is ArgumentSyntax && !poolBuffers.Contains(nm) && !consumedArg))
13491423
escapedLocals.Add(nm);
13501424
}
13511425
var tracked = new HashSet<string>(candidates);

0 commit comments

Comments
 (0)