Skip to content

Commit 789255e

Browse files
PhysShellclaude
andauthored
feat(extractor): transitive consume contract — use-after-handoff through a forwarding consumer (#68)
The D4 frontier: cross-method use-after-dispose through a FORWARDING consumer. The inter-procedural consume-handoff (#55) caught a use-after-handoff when the callee disposes a by-value IDisposable parameter directly, but stopped one hop short — a parameter merely forwarded to another call was left ambiguous (ownir.py's `passed` branch: "awaiting a later transitive pass"). This is that pass. The extractor's consumer detection (ConsumesParam) is now transitive: a parameter is consumed if the body disposes it directly OR forwards it to another first-party consumer that consumes it — following Consume -> Inner -> Dispose through the chain (incl. directly-called local functions), cycle-guarded per parameter, binding each callee's body with its own tree's SemanticModel so cross-file chains resolve. So Consume(s) is still a call-site release and a use after it trips OWN002. The emitted facts match the direct handoff, so the core is unchanged. Conservative: a parameter handed to an unknown or borrowing callee is not consumed (no false release, no false OWN002). Deferred-body safety (Codex/CodeRabbit): the scan (ImmediateInvocations, shared with DisposesLocal) does NOT descend into nested lambda / local-function bodies — a dispose/forward stored in a callback runs deferred, not at the call boundary, so it is not a phantom handoff. Pinned by a silent control (DeferredHandoffNoFalsePositive) and by the new corpus case ownership-handoff-use-transitive (before -> OWN002, after silent), which lifts the benchmark recall floor 10 -> 11. ruff + mypy --strict clean, corpus 9/9, ownir 116/116; corpus benchmark green at recall 11. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 53950f8 commit 789255e

10 files changed

Lines changed: 250 additions & 30 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ jobs:
510510
# case disposes (no default) -> last case is the tail, no phantom no-match leak.
511511
# `ncf`: `ncf?.Dispose()` (null-conditional) in a threaded finally IS a release
512512
# (member-binding form), so it is disposed on the return path -> silent (Codex review).
513-
for ok in clean looped esc exemptTask whileClean asyncDisposed asyncDisposedCfg tfClean tfCatch tfRet tfNull cda daci cif ctorLater lamPrior other doClean swAll ncf captured shaClean stopped; do
513+
for ok in clean looped esc exemptTask whileClean asyncDisposed asyncDisposedCfg tfClean tfCatch tfRet tfNull cda daci cif ctorLater lamPrior other doClean swAll ncf captured shaClean stopped defer; do
514514
if echo "$out" | grep -q "'$ok'"; then echo "FAIL: silent/exempt case '$ok' was reported"; exit 1; fi
515515
done
516516
echo "OK: flow-sensitive OWN001/002/003 on real C# (path-sensitive, loops via while/foreach/for, try/finally sequential, never-vs-every-path wording, dispose-optional exempt, beyond flat)"
@@ -780,9 +780,12 @@ jobs:
780780
# (Rent/Return: OWN003/OWN002, pool resolved via the Roslyn SemanticModel so an
781781
# ALIASED receiver is caught), factory acquires (System.IO.File.Open*/Create*) are
782782
# recognised alongside `new`, and the inter-procedural CONSUME contract is modelled:
783-
# a first-party method owning a by-value IDisposable param is a handoff (a `call`
784-
# op), so a use after the handoff trips OWN002 (the cut is the signature, like
785-
# Rust's move). Remaining backlog: a cross-method use-after-dispose and an
786-
# injected-source region-escape. A drop below the floor is a regression.
787-
run: python scripts/benchmark.py --min-recall 10
783+
# a first-party method that owns a by-value IDisposable param — by disposing it
784+
# directly OR by forwarding it to another first-party consumer (the TRANSITIVE chain,
785+
# `ConsumesParam`) — is a handoff that releases the argument at the call site, so a use
786+
# after the handoff trips OWN002 (the cut is the signature, like Rust's move). Remaining
787+
# backlog: a FIELD-mediated cross-method use-after-dispose (dispose in one method, use in
788+
# another via shared state) and an injected-source region-escape. A drop below the floor
789+
# is a regression.
790+
run: python scripts/benchmark.py --min-recall 11
788791

‎corpus/real-world/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ exception-path (анализ не моделирует исключения), co
4343
|------|-----|------------------|
4444
| `arraypool-use-after-return` | OWN002 | rented-буфер вернули в пул, потом ещё читали slice |
4545
| `arraypool-double-return` | OWN003 | один и тот же массив вернули в ArrayPool дважды ([#33767](https://github.com/dotnet/runtime/issues/33767)) |
46+
| `ownership-handoff-use` | OWN002 | поток отдали потребителю (он его закрыл), потом ещё читали — use-after-handoff |
47+
| `ownership-handoff-use-transitive` | OWN002 | то же, но потребитель не закрывает сам, а **пробрасывает** владение дальше (transitive consume) |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.IO;
3+
4+
// FIX: read the stream BEFORE handing ownership to the consumer chain, so nothing touches it
5+
// after the handoff. Same transitive handoff (Consume -> Inner -> Dispose), correct order. The
6+
// handoff still discharges ownership (Inner closes it), so there is no leak and no
7+
// use-after-handoff -- the case must stay SILENT (this is the no-false-positive arm).
8+
static class HandoffUseTransitive
9+
{
10+
static void Inner(Stream s)
11+
{
12+
s.CopyTo(Stream.Null);
13+
s.Dispose();
14+
}
15+
16+
static void Consume(Stream sink)
17+
{
18+
Inner(sink);
19+
}
20+
21+
static long Run(string path)
22+
{
23+
var s = File.OpenRead(path);
24+
var len = s.Length; // read BEFORE the handoff
25+
Consume(s); // ownership moves to Consume -> Inner (disposed)
26+
return len; // no use after the handoff -> silent
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.IO;
3+
4+
// A TRANSITIVE inter-procedural use-after-handoff: the stream is handed to `Consume`, which
5+
// does NOT dispose it directly -- it FORWARDS it to `Inner`, which owns and closes it. So
6+
// `Consume` consumes its parameter *transitively* (one hop further down the chain), and the
7+
// caller's later read is a use-after-handoff. The consume signal travels through the
8+
// forwarding chain: the extractor follows `Consume -> Inner -> Dispose` and models `Consume(s)`
9+
// as a release of the argument at the call site, so a use after it trips OWN002. Unlike
10+
// `ownership-handoff-use` (the callee disposes the param itself), here the callee only forwards.
11+
static class HandoffUseTransitive
12+
{
13+
// Direct consumer: owns and closes the stream.
14+
static void Inner(Stream s)
15+
{
16+
s.CopyTo(Stream.Null);
17+
s.Dispose(); // Inner owns and closes it
18+
}
19+
20+
// Transitive consumer: it does NOT close `sink` itself -- it forwards ownership to Inner.
21+
// `sink` is still consumed (its obligation is discharged via Inner), so a caller must not
22+
// touch the argument after the handoff.
23+
static void Consume(Stream sink)
24+
{
25+
Inner(sink); // ownership forwarded to the real consumer
26+
}
27+
28+
// BUG: ownership moved into Consume (-> Inner, which disposed it), then the stream is read.
29+
static long Run(string path)
30+
{
31+
var s = File.OpenRead(path);
32+
Consume(s); // ownership moves to Consume -> Inner (disposed)
33+
return s.Length; // use-after-handoff (s is disposed) -> OWN002
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// OwnLang model of a TRANSITIVE inter-procedural use-after-handoff. `take` does not close the
2+
// stream itself -- it FORWARDS it to `inner`, which owns and closes it. So `take` consumes its
3+
// parameter TRANSITIVELY (the obligation travels one hop further), and `run` touching the
4+
// stream after `take(s)` is a use-after-handoff -> OWN002. The signature is still the cut: the
5+
// caller is checked against `take`'s (transitively inferred) contract, not its body -- no
6+
// whole-program analysis. (`consume` is an OwnLang keyword, so the reduction names the
7+
// consumers `take`/`inner`; the C# consumers are `Consume`/`Inner`.)
8+
module Corpus
9+
resource Stream {
10+
acquire open
11+
release close
12+
}
13+
// direct consumer: it OWNS the stream and closes it.
14+
fn inner(s: Stream) {
15+
use s;
16+
release s;
17+
}
18+
// transitive consumer: it forwards ownership to `inner` (does NOT close it itself), so it
19+
// consumes `s` one hop down the chain.
20+
fn take(s: Stream) {
21+
inner(s);
22+
}
23+
// BUG: the stream is used after ownership moved into `take` (-> inner, which closed it).
24+
fn run() {
25+
let s = acquire Stream();
26+
take(s); // ownership consumed transitively (take -> inner)
27+
use s; // <-- touched after handoff -> OWN002
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN002
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Inter-procedural use-after-handoff — the TRANSITIVE (forwarded) consumer
2+
3+
**Pattern:** the same use-after-handoff as `ownership-handoff-use`, but the consumer does **not**
4+
dispose the stream itself — it **forwards** it to another method that owns and closes it
5+
(`Consume(sink) -> Inner(sink) -> sink.Dispose()`). The caller hands ownership to `Consume`, then
6+
touches the stream again. The bug is the use **after** ownership moved; the handoff itself is
7+
correct (the stream is closed, just one hop further down).
8+
9+
**What the checker says:** using a resource after it was consumed (here, *transitively*) by a
10+
callee is the generic **OWN002** (use after release) — the same code as use-after-dispose.
11+
12+
**Why this case exists (the transitive consume-contract).** The consume contract was already
13+
modelled for a callee that disposes a by-value `IDisposable` parameter *directly*
14+
(`ownership-handoff-use`). But the inference deliberately **stopped at one hop**: a parameter
15+
merely *handed to another call* was "genuinely ambiguous without that callee's contract, so we do
16+
not infer it" (`ownlang/ownir.py`, the `passed` branch) — and the extractor's
17+
`ConsumeReleaseArgs` only recognised a *direct* disposer. So `Consume` (which forwards rather than
18+
disposes) was **not** seen as a consumer, the handoff was not a release, and the later `s.Length`
19+
was invisible — a **miss**.
20+
21+
Now the extractor's consumer detection (`ConsumesParam`) is **transitive**: a parameter is
22+
consumed if the body either disposes it directly **or** forwards it to another first-party
23+
consumer that consumes it — following `Consume -> Inner -> Dispose` through the chain, guarded
24+
against cycles. Inspecting each callee's own body keeps it inter-procedural without a cross-call
25+
signature table (and without a dangling-callee crash). Conservative: a parameter handed to an
26+
unknown or merely-borrowing callee is **not** treated as consumed (no false release, no false
27+
OWN002). This fixture is a **miss** before and a **catch** after — the ratchet row that lifts the
28+
use-after-handoff capability across a forwarding hop. The `.own` reduction already checks the
29+
transitive shape (the front-end's signature inference recurses); this brings it to real C#.
30+
31+
**Honesty / scope.** `case.own` is a faithful hand reduction of the C# pattern, not C# the `.own`
32+
checker ingested. `before.cs` / `after.cs` are representative of the bug and its fix, not a
33+
verbatim copy of one PR.

‎docs/proposals/P-016-deep-fact-extraction.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ a CFG-carrying bridge, and the existing core checks real code.
121121
slice; do it before the general case.
122122
- **B3 — Move / ownership transfer.** `return disposable`, or passing it to a callee
123123
that consumes it → move / escape (P-005 D5). Intraprocedural, driven by declared
124-
signatures, not whole-program tracing.
124+
signatures, not whole-program tracing. *Landed (consume handoff):* a call to a
125+
first-party consumer — a method that disposes a by-value `IDisposable` parameter
126+
**directly, or by forwarding it to another first-party consumer** (the transitive
127+
chain, `ConsumesParam`) — releases the argument at the call site, so a use after the
128+
handoff trips OWN002 (`corpus/real-world/ownership-handoff-use{,-transitive}`). The
129+
signal is each callee's own body, so it is inter-procedural without a signature table.
125130
- **B4 — Borrow / Span.** `Rent` → view → `Return`, `Span`/`ref` aliasing (P-007) —
126131
the borrow checker's crown jewel, hardest on C# (ref structs, Span lifetimes).
127132
- **B5 — Lifetime ordering.** WPF005 escape (`OWN014`) and DI captive (P-006,

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

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -860,50 +860,115 @@ static bool IsInNamespace(INamedTypeSymbol? t, params string[] parts)
860860
}
861861

862862
// The local names of arguments handed to a first-party CONSUMER at this call — a method
863-
// whose own body disposes the by-value IDisposable parameter the argument binds to. Such
864-
// an argument's ownership moves into the callee and is discharged there, so the handoff is
865-
// modelled as a RELEASE of the argument at the call site (the same shape as pool
866-
// `Return(buf)`); a later use is then a use-after-handoff (OWN002). Inspecting the callee's
867-
// OWN body means no cross-call signature table and no dangling-callee crash — a callee with
868-
// no body (interface / abstract / extern) or that does not dispose the param contributes
869-
// nothing, and the argument stays an ordinary escape. Arguments resolve to parameters by
870-
// NAME when `name:` is used, else by position; partial declarations are scanned for a body.
863+
// that takes ownership of the by-value IDisposable parameter the argument binds to and
864+
// discharges it: either by disposing it directly, or by forwarding it to another first-party
865+
// consumer (the transitive case, `ConsumesParam`). Such an argument's ownership moves into
866+
// the callee and is discharged there, so the handoff is modelled as a RELEASE of the argument
867+
// at the call site (the same shape as pool `Return(buf)`); a later use is then a
868+
// use-after-handoff (OWN002). Inspecting each callee's OWN body means no cross-call signature
869+
// table and no dangling-callee crash — a callee with no body (interface / abstract / extern)
870+
// or that does not consume the param contributes nothing, and the argument stays an ordinary
871+
// escape. Arguments resolve to parameters by NAME when `name:` is used, else by position.
871872
static List<string> ConsumeReleaseArgs(ExpressionSyntax e, SemanticModel model)
872873
{
873874
var consumed = new List<string>();
874875
if (e is not InvocationExpressionSyntax inv
875876
|| model.GetSymbolInfo(inv).Symbol is not IMethodSymbol sym)
876877
return consumed;
877-
SyntaxNode? body = null;
878-
foreach (var r in sym.DeclaringSyntaxReferences)
879-
if (r.GetSyntax() is BaseMethodDeclarationSyntax d
880-
&& ((SyntaxNode?)d.Body ?? d.ExpressionBody) is { } b)
881-
{
882-
body = b;
883-
break;
884-
}
885-
if (body is null)
886-
return consumed;
887878
var args = inv.ArgumentList.Arguments;
888879
for (int i = 0; i < args.Count; i++)
889880
{
890881
// map argument -> parameter: by name for `name: value`, else by position.
891882
var p = args[i].NameColon is { } nc
892883
? sym.Parameters.FirstOrDefault(q => q.Name == nc.Name.Identifier.Text)
893884
: (i < sym.Parameters.Length ? sym.Parameters[i] : null);
894-
if (p is { RefKind: RefKind.None } && ImplementsIDisposable(p.Type)
895-
&& DisposesLocal(body, p.Name)
896-
&& args[i].Expression is IdentifierNameSyntax aid)
885+
if (p is not null
886+
&& args[i].Expression is IdentifierNameSyntax aid
887+
&& ConsumesParam(sym, p, model,
888+
new HashSet<ISymbol>(SymbolEqualityComparer.Default)))
897889
consumed.Add(aid.Identifier.Text);
898890
}
899891
return consumed;
900892
}
901893

894+
// The body of a first-party method or LOCAL FUNCTION (block or expression-bodied), scanning
895+
// partial declarations; null for an interface/abstract/extern method (no body to inspect). A
896+
// directly-called local function runs synchronously, so a forwarding chain through one must be
897+
// followed too (CodeRabbit) — `LocalFunctionStatementSyntax` carries the same Body/ExpressionBody.
898+
static SyntaxNode? ConsumerBody(IMethodSymbol sym)
899+
{
900+
foreach (var r in sym.DeclaringSyntaxReferences)
901+
switch (r.GetSyntax())
902+
{
903+
case BaseMethodDeclarationSyntax d when ((SyntaxNode?)d.Body ?? d.ExpressionBody) is { } b:
904+
return b;
905+
case LocalFunctionStatementSyntax l when ((SyntaxNode?)l.Body ?? l.ExpressionBody) is { } lb:
906+
return lb;
907+
}
908+
return null;
909+
}
910+
911+
// The invocations that run IMMEDIATELY when `body` executes — excluding those inside a nested
912+
// lambda / local-function body, which run deferred (when the delegate is later invoked), not at
913+
// this method's call boundary. The same deferred-body rule the flow lowering already uses, so a
914+
// dispose/forward stored in a callback is not mistaken for an immediate discharge (Codex/CodeRabbit).
915+
static IEnumerable<InvocationExpressionSyntax> ImmediateInvocations(SyntaxNode body) =>
916+
body.DescendantNodes(n => n is not (AnonymousFunctionExpressionSyntax
917+
or LocalFunctionStatementSyntax))
918+
.OfType<InvocationExpressionSyntax>();
919+
920+
// Does `method` CONSUME (take ownership of and discharge) its by-value IDisposable
921+
// parameter `param`? Either (a) the body disposes it directly (`param.Dispose()`), or
922+
// (b) — the transitive step — the body hands it to ANOTHER first-party consumer that
923+
// consumes it (`Inner(param)` where `Inner` consumes its matching parameter). So a
924+
// forwarding chain `Consume(sink) => Inner(sink) => sink.Dispose()` is recognised, and a
925+
// caller's `Consume(s)` is still a handoff (a call-site release). Inspecting each callee's
926+
// own body keeps it inter-procedural without a signature table; `visited` (keyed on the
927+
// parameter) guards recursion on cyclic call graphs. Conservative: a param handed to an
928+
// unknown/borrowing callee yields false (no release, no false OWN002).
929+
static bool ConsumesParam(IMethodSymbol method, IParameterSymbol param,
930+
SemanticModel model, HashSet<ISymbol> visited)
931+
{
932+
if (param.RefKind != RefKind.None || !ImplementsIDisposable(param.Type))
933+
return false;
934+
if (!visited.Add(param.OriginalDefinition))
935+
return false; // cycle guard (per parameter)
936+
if (ConsumerBody(method) is not { } body)
937+
return false; // no body -> contributes nothing
938+
var name = param.Name;
939+
if (DisposesLocal(body, name)) // (a) disposes the parameter directly
940+
return true;
941+
// (b) transitive: the parameter is handed to another first-party consumer at an IMMEDIATE
942+
// call (not one deferred in a nested lambda/local function). The body may live in another
943+
// file, so bind its calls with that tree's OWN model (a SemanticModel only resolves nodes in
944+
// its own tree); the Compilation is shared across all parsed inputs.
945+
var bodyModel = model.Compilation.GetSemanticModel(body.SyntaxTree);
946+
foreach (var inv in ImmediateInvocations(body))
947+
{
948+
if (bodyModel.GetSymbolInfo(inv).Symbol is not IMethodSymbol callee)
949+
continue;
950+
var cargs = inv.ArgumentList.Arguments;
951+
for (int i = 0; i < cargs.Count; i++)
952+
{
953+
if (cargs[i].Expression is not IdentifierNameSyntax aid || aid.Identifier.Text != name)
954+
continue;
955+
var cp = cargs[i].NameColon is { } nc
956+
? callee.Parameters.FirstOrDefault(q => q.Name == nc.Name.Identifier.Text)
957+
: (i < callee.Parameters.Length ? callee.Parameters[i] : null);
958+
if (cp is not null && ConsumesParam(callee, cp, model, visited))
959+
return true;
960+
}
961+
}
962+
return false;
963+
}
964+
902965
// Does `body` dispose the local/parameter named `name` — a `name.Dispose()` / `.Close()` /
903-
// `.DisposeAsync()` call (the consume signal)?
966+
// `.DisposeAsync()` call (the consume signal)? Only IMMEDIATE calls count (`ImmediateInvocations`
967+
// excludes nested lambda / local-function bodies): a `name.Dispose()` inside a stored callback
968+
// runs deferred, not at this call site, so it is not a discharge here.
904969
static bool DisposesLocal(SyntaxNode body, string name)
905970
{
906-
foreach (var i in body.DescendantNodesAndSelf().OfType<InvocationExpressionSyntax>())
971+
foreach (var i in ImmediateInvocations(body))
907972
if (i.Expression is MemberAccessExpressionSyntax m
908973
&& m.Name.Identifier.Text is "Dispose" or "Close" or "DisposeAsync"
909974
&& m.Expression is IdentifierNameSyntax id && id.Identifier.Text == name)

0 commit comments

Comments
 (0)