Skip to content

Commit c90a631

Browse files
authored
feat(extractor): recognise File.Open*/Create* factory acquires → handoff leak arm (recall 7/10→8/10) (#54)
The flow/candidate passes only ever treated `new X()` as acquiring an owned disposable, so a stream opened by a factory (`var s = File.OpenRead(path)`) was invisible and the leak arm of ownership-handoff-consume scored a miss. New IsOwningFactory recognises ownership-transferring factory calls off the resolved symbol against a curated System.IO.File set (Open*/Create*/*Text), wired into the same two acquire points as `new`/pool. The existing escape/using/dispose logic then applies unchanged. Effect: Leak fires OWN001 (the `using var` fix stays clean), flipping the case to caught -- recall 7/10 -> 8/10, specificity 10/10, 0 FP, CI floor --min-recall 8. Curated + symbol-resolved = precision over recall; blast radius is exactly one file. The use-after-handoff (OWN002) arm stays future (needs the inter-procedural consume contract).
1 parent 9ede9b0 commit c90a631

5 files changed

Lines changed: 82 additions & 24 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +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 7/10 — pooled buffers ride the path-sensitive flow engine
629-
# (Rent = acquire, Return = release: OWN003/OWN002), and pool recognition is
630-
# now resolved through the Roslyn SemanticModel, so a double-return reached via
631-
# an ALIASED pool receiver (`var p = ArrayPool<int>.Shared; p.Return(buf)`) — a
632-
# miss for the old text heuristic — is caught. Remaining backlog: interprocedural
633-
# handoff, cross-method use-after-dispose, a region-escape shape. A drop below
634-
# the floor is a regression.
635-
run: python scripts/benchmark.py --min-recall 7
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
636637

‎corpus/real-world/ownership-handoff-consume/notes.md‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ points-to is involved: the signature `consume Stream` is the cut point, exactly
4444
as Rust's borrow checker is modular against function signatures.
4545

4646
As with every corpus case, `case.own` is a faithful hand reduction of the C#
47-
pattern in `before.cs` / `after.cs`, **not** C# the checker ingested — OwnLang
48-
has no C# front-end. The corpus shows the ownership *logic* maps onto real bugs,
49-
not that the tool scanned real C#.
47+
pattern in `before.cs` / `after.cs`, **not** C# the `.own` checker ingested —
48+
OwnLang has no C# front-end. The corpus shows the ownership *logic* maps onto
49+
real bugs, not that the `.own` tool scanned real C#.
50+
51+
**Extractor status (real C#, the `corpus-benchmark` path).** The benchmark now
52+
catches the **leak arm** (`Leak` → `OWN001`) end-to-end: `IsOwningFactory`
53+
recognises `File.OpenRead` as an *owned acquire* (a factory, not `new`), so the
54+
un-disposed stream is flagged exactly as a `new`'d one would be, and the
55+
`using var` fix stays silent. The **use-after-handoff arm** (`Run` → `OWN002`) is
56+
still extractor-future — it needs the inter-procedural *consume* contract:
57+
recognise that `Archive` disposes its by-value parameter, then model `Archive(s)`
58+
as a *release* of `s` so the later `s.Length` is a use-after-release. The `.own`
59+
reduction proves both arms today; the C# front-end catches the leak.

‎docs/notes/corpus-benchmark.md‎

Lines changed: 23 additions & 6 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 → 7/10 (three ratchets)
29+
## Ratchet → 8/10 (four ratchets)
3030

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

@@ -75,11 +75,28 @@ fixture — `arraypool-aliased-receiver`, a double-return reached through `var p
7575
heuristic's failure mode was recall-leaning (a missed alias is a missed catch, never a false alarm),
7676
so the upgrade only adds — precision stays absolute. **Recall is now 7/10.**
7777

78-
The remaining three misses are genuine **frontend extraction gaps** — the interprocedural
79-
ownership-handoff (`OWN001`+`OWN002`), a field/cross-method use-after-dispose, and a
80-
region-escape shape — the `.own` reductions all catch them, the C# extractor does not yet.
81-
That is the itemized recall backlog; each is a real capability the floor will ratchet up to
82-
as it lands.
78+
### → 8/10: factory acquires, not just `new`
79+
80+
The extractor only ever treated `new X()` as *acquiring* an owned disposable — so a stream
81+
opened by a **factory**, `var s = File.OpenRead(path)`, was invisible, and the leak arm of
82+
`ownership-handoff-consume` (a stream neither disposed nor handed off → a real `OWN001`)
83+
scored a miss. `File.Open*` / `Create*` hand back a fresh `FileStream` the caller owns
84+
exactly as if it had `new`'d one, so a local bound to one is an acquire. `IsOwningFactory`
85+
recognises them off the resolved **symbol** against a curated `System.IO.File` set (precision
86+
over recall — the set grows only where ownership is certain, so a borrowed/cached disposable
87+
handed back by some other API is never mistaken for an acquire). With it the leak arm fires
88+
`OWN001` (the fix's `using var` stays silent), so the case flips to caught — **recall is now
89+
8/10**. The blast radius is exactly one file: nothing else in the corpus or the samples opens
90+
a `File.*` stream, so no `after.cs` and no dog-food scan can newly cry wolf.
91+
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.
83100

84101
## Why catch/clean, not exact-code match
85102

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

Lines changed: 9 additions & 6 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 **7/10** over three steps: (1) a *fixture*
9+
caught · 9/9 clean · 0 FP**, ratcheted to **8/10** over four 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 =
@@ -15,11 +15,14 @@
1515
recognition moved off the receiver-text heuristic onto the **Roslyn SemanticModel**
1616
(binding `System.Buffers.ArrayPool<T>`), so a double-return through an *aliased* pool
1717
receiver (`var p = ArrayPool<int>.Shared; p.Return(buf)`) — a miss for the text
18-
heuristic — is caught (`arraypool-aliased-receiver`, +1 row). Perfect precision
19-
throughout. The remaining 3 misses are genuine frontend extraction gaps
20-
(interprocedural handoff, a cross-method use-after-dispose, a region-escape shape)
21-
— the tracked recall backlog the floor ratchets up to. Still ahead: those, GitHub
22-
mining at scale (stage 1) and the 50–100-repo prevalence scan (stage 2). See
18+
heuristic — is caught (`arraypool-aliased-receiver`, +1 row); (4) ownership-transferring
19+
**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.
24+
Still ahead: those, GitHub mining at scale (stage 1) and the 50–100-repo prevalence
25+
scan (stage 2). See
2326
[docs/notes/corpus-benchmark.md](../notes/corpus-benchmark.md).
2427
- **Depends on:** P-001 (C# → OwnIR extractor — the scanner that does stage 2);
2528
the existing `corpus/` layout (`before.cs`, `after.cs`,

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, SemanticM
442442
if (tracked.Contains(v.Identifier.Text)
443443
&& (v.Initializer?.Value is ObjectCreationExpressionSyntax
444444
or ImplicitObjectCreationExpressionSyntax
445-
|| IsPoolRent(v.Initializer?.Value, model))) // ArrayPool<T> Rent
445+
|| IsPoolRent(v.Initializer?.Value, model) // ArrayPool<T> Rent
446+
|| IsOwningFactory(v.Initializer?.Value, model))) // File.Open*/Create* factory
446447
nodes.Add(new { op = "acquire", var = v.Identifier.Text, line = LineOf(v) });
447448
return true;
448449
case ExpressionStatementSyntax es:
@@ -758,6 +759,30 @@ e is InvocationExpressionSyntax i
758759
&& i.ArgumentList.Arguments[0].Expression is IdentifierNameSyntax buf
759760
? buf.Identifier.Text : null;
760761

762+
// A factory call that CREATES and hands back a fresh owned IDisposable the caller must
763+
// release — recognised via the resolved symbol (curated, the same spirit as
764+
// IsDisposableType is for `new`). System.IO.File.Open*/Create*/*Text return a NEW
765+
// FileStream / StreamReader / StreamWriter that the caller owns exactly as if it had
766+
// `new`'d one, so a local bound to one is an acquire. Curated + symbol-resolved, so a
767+
// borrowed/cached disposable handed back by some other API is never mistaken for an
768+
// owned acquire (precision over recall — the set grows only as ownership is certain).
769+
static bool IsOwningFactory(ExpressionSyntax? e, SemanticModel model)
770+
{
771+
if (e is not InvocationExpressionSyntax i
772+
|| model.GetSymbolInfo(i).Symbol is not IMethodSymbol sym
773+
|| sym.Name is not ("OpenRead" or "OpenWrite" or "Open" or "Create"
774+
or "OpenText" or "CreateText" or "AppendText"))
775+
return false;
776+
INamedTypeSymbol? ct = sym.ContainingType;
777+
if (ct is null || ct.Name != "File")
778+
return false;
779+
INamespaceSymbol? ns = ct.ContainingNamespace; // System.IO.File -> IO
780+
if (ns is null || ns.Name != "IO")
781+
return false;
782+
ns = ns.ContainingNamespace; // IO -> System
783+
return ns is { Name: "System" } && ns.ContainingNamespace is { IsGlobalNamespace: true };
784+
}
785+
761786
// A field/local type treated as owned-disposable (syntax-only heuristic — no
762787
// semantic model): a curated set plus a few suffixes. Gated on the class `new`ing
763788
// the value, so injected/borrowed disposables are not flagged. Timer types are
@@ -1301,6 +1326,8 @@ or ImplicitObjectCreationExpressionSyntax } init
13011326
candidates.Add(v.Identifier.Text);
13021327
poolBuffers.Add(v.Identifier.Text);
13031328
}
1329+
else if (IsOwningFactory(v.Initializer?.Value, model)) // File.Open*/Create* factory
1330+
candidates.Add(v.Identifier.Text);
13041331
}
13051332
if (candidates.Count == 0)
13061333
continue;

0 commit comments

Comments
 (0)