Skip to content

Commit 95af05c

Browse files
committed
feat(extractor): close remaining flow-lowering gaps — finally-before-return, do, switch
Closes the last deferred control-flow recall gaps in the --flow-locals lowering, so the flow detector now models every common construct (only goto/labeled statements and a few exotic forms still honestly bail). - finally-before-return: an `onReturn` continuation is threaded alongside `onThrow` (the finally-stack + return), so a `return` inside a try-with-finally runs the finally FIRST — a finally-disposed resource is released on the return path. Unlike `onThrow`, `onReturn` is never suppressed by catches (a return is never caught). The old "bail the whole method if a try-with-finally has a return" is removed; such methods are analysed, and an early return that skips a later dispose is now caught. - do-while: desugared `do { B } while(c)` -> `B; while(c){ B }` (the body runs 1+ times). Modelling it as a bare 0+-trip `while` would be unsound — a resource released only in the body but acquired outside would falsely leak on the phantom 0-trip path. - switch: lowered to a chain of opaque, mutually-exclusive `if(*)` branches, one per section, trailing `break` stripped (anything else — nested break, goto case, throw — bails). With NO default the LAST case becomes the tail rather than an empty no-match branch: that avoids a FALSE POSITIVE on an exhaustive switch (e.g. over an enum) where every case disposes; the cost is a sound recall gap (a non-exhaustive no-match leak is only missed when every case disposes). All no-false-positive-preserving. Validated on the core IR via a new flow_finally_switch fixture (silent: finally-before-return clean, switch-all-dispose; leak: early-return, switch-else) — tests 53/53 — and end-to-end in CI on new samples (earlyRet/doLeak/swLeak leak; other/doClean/swAll/tfRet silent). Existing samples keep their verdicts. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 801a52a commit 95af05c

7 files changed

Lines changed: 282 additions & 35 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ jobs:
287287
# ('qualLeak'); IsCatchAll matches only the canonical spellings (CodeRabbit review).
288288
echo "$out" | grep -qE "OWN001.*'qualLeak'" \
289289
|| { echo "FAIL: expected OWN001 on the qualified-typed-catch leak"; exit 1; }
290+
# remaining flow-lowering gaps closed: finally-before-return threading (an early
291+
# return that skips a later dispose leaks -> 'earlyRet'), `do` desugar (a body-local
292+
# never disposed leaks per iteration -> 'doLeak'), and `switch` lowering (a default
293+
# branch that does not dispose leaks -> 'swLeak').
294+
echo "$out" | grep -qE "OWN001.*'earlyRet'" \
295+
|| { echo "FAIL: expected OWN001 on the early-return-skips-dispose leak"; exit 1; }
296+
echo "$out" | grep -qE "OWN001.*'doLeak'" \
297+
|| { echo "FAIL: expected OWN001 on the undisposed local in a do-while loop"; exit 1; }
298+
echo "$out" | grep -qE "OWN001.*'swLeak'" \
299+
|| { echo "FAIL: expected OWN001 on the switch default-branch leak"; exit 1; }
290300
# dispose-optional (Task), disposed/escaping locals, a `for` loop whose
291301
# disposable is disposed after it (`looped`, balanced), a balanced
292302
# acquire+dispose in a loop (`whileClean`), a try/finally dispose (`tfClean`,
@@ -303,7 +313,10 @@ jobs:
303313
# `lamPrior`: a `new` inside a LAMBDA body is deferred (runs on invoke, not at the
304314
# declaration), so the lambda statement is not a throw point -> no phantom edge skips
305315
# its post-try dispose -> silent (Codex review: don't descend into lambda bodies).
306-
for ok in clean looped esc exemptTask whileClean asyncDisposed asyncDisposedCfg tfClean tfCatch tfRet tfNull cda daci cif ctorLater lamPrior; do
316+
# `other`: disposed by the finally, so threaded before the early return -> silent.
317+
# `doClean`: acquire+dispose balanced each `do` iteration. `swAll`: every `switch`
318+
# case disposes (no default) -> last case is the tail, no phantom no-match leak.
319+
for ok in clean looped esc exemptTask whileClean asyncDisposed asyncDisposedCfg tfClean tfCatch tfRet tfNull cda daci cif ctorLater lamPrior other doClean swAll; do
307320
if echo "$out" | grep -q "'$ok'"; then echo "FAIL: silent/exempt case '$ok' was reported"; exit 1; fi
308321
done
309322
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)"

‎docs/ROADMAP.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ architectural strictness, and the borrow-checker showcase):
9696
throw exit before each may-throw leaf in a `try` (including inside nested branches, with a
9797
constructor `new` as a throw point and typed/filtered catches handled). That closes the
9898
`dispose-not-called-on-throw` shape, which now lands in cross-tool **Agree** with
99-
CodeQL's dedicated query on the fixture. Deferred: `finally`-before-`return`,
100-
`switch`/`do`. See [docs/notes/real-world-mining.md](notes/real-world-mining.md).
99+
CodeQL's dedicated query on the fixture. `finally`-before-`return`, `do` and `switch` are
100+
lowered too, so the flow detector covers every common control-flow construct (only
101+
`goto`/labeled statements and a few exotic forms still bail). See
102+
[docs/notes/real-world-mining.md](notes/real-world-mining.md).
101103
2. **Resource core** — generalise WPF subscriptions + `IDisposable` into one
102104
acquire/release/owner/release-region model (P-004 ∪ P-005), so WPF is a
103105
*profile*, not a one-off.

‎docs/notes/real-world-mining.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,14 @@ Their leak findings are **nearly disjoint** (file overlap: **1**):
135135
with a Dispose *after* the try/catch (the caught path disposes the resource) still lowers
136136
sequentially, to avoid a false leak (PR #32 review). The three recall wins are pinned in CI
137137
(`nestedLeak`, `ctorPrior`, `typedLeak`) and on the core's IR (the `flow_nested_throw`
138-
fixture). Still deferred — both **sound recall gaps** (missed leaks, never false ones):
139-
`finally`-before-`return` threading (bailed today) and `switch`/`do`.
138+
fixture). The remaining control-flow gaps are now closed too: `finally`-before-`return`
139+
(the finally is threaded BEFORE the return instead of bailing the method), `do` (desugared
140+
to `B; while(c){B}` — the body runs 1+ times, so a plain 0+-trip `while` would falsely leak
141+
a body-released resource), and `switch` (opaque mutually-exclusive branches; with no
142+
`default` the last case is the tail rather than an empty no-match path, so an *exhaustive*
143+
switch is never falsely flagged — a non-exhaustive no-match leak is only missed when every
144+
case disposes, a sound recall gap). The flow detector now models every common control-flow
145+
construct; only `goto`/labeled statements and a few exotic forms still honestly bail.
140146
- **Agree — 1** (`HttpHelper.cs`).
141147

142148
So the SystemEvents and VideoSource findings are **differentiated — confirmed by the

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

Lines changed: 112 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,9 @@ cc.Filter is null
342342
or "global::System.Exception");
343343

344344
// Lower a method block to OwnIR flow nodes (acquire/use/release/if/return) for the
345-
// `tracked` local IDisposables. Returns null on any UNMODELLED statement
346-
// (loop/try/switch/...): the method is then honestly skipped, not guessed.
345+
// `tracked` local IDisposables. Returns null on any UNMODELLED statement (a `goto`, labeled
346+
// statement, local function, `lock`/`fixed`, …): the method is then honestly skipped, not
347+
// guessed. Loops, `try`, `do` and `switch` ARE modelled below.
347348
static List<object>? LowerFlowBody(BlockSyntax block, HashSet<string> tracked)
348349
{
349350
var nodes = new List<object>();
@@ -356,16 +357,19 @@ cc.Filter is null
356357
// `canEscape`: can a throw at the current position leave the METHOD (no enclosing
357358
// catch-all swallows it)? `onThrow`: the continuation a throw here runs to leave the
358359
// method (finally-stack + return), or null when no exception edge should be injected
359-
// (method level, or a region an enclosing catch-all swallows). Both default to the
360-
// method-body context: a throw escapes, but no edge is injected until a `try` sets one.
360+
// (method level, or a region an enclosing catch-all swallows). `onReturn`: the continuation
361+
// a `return` here runs FIRST — the enclosing `finally`(s), then the exit — so a resource a
362+
// finally disposes is released on the return path; null = a bare return (outside any try).
363+
// Defaults are the method-body context: throws escape, nothing is injected, returns are bare.
361364
static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, List<object> nodes,
362-
bool canEscape = true, List<object>? onThrow = null)
365+
bool canEscape = true, List<object>? onThrow = null,
366+
List<object>? onReturn = null)
363367
{
364368
switch (st)
365369
{
366370
case BlockSyntax b:
367371
foreach (var s2 in b.Statements)
368-
if (!LowerFlowStmt(s2, tracked, nodes, canEscape, onThrow))
372+
if (!LowerFlowStmt(s2, tracked, nodes, canEscape, onThrow, onReturn))
369373
return false;
370374
return true;
371375
case LocalDeclarationStatementSyntax ld:
@@ -384,22 +388,27 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, List<obje
384388
case IfStatementSyntax ifs:
385389
{
386390
var thenNodes = new List<object>();
387-
if (!LowerFlowStmt(ifs.Statement, tracked, thenNodes, canEscape, onThrow))
391+
if (!LowerFlowStmt(ifs.Statement, tracked, thenNodes, canEscape, onThrow, onReturn))
388392
return false;
389393
var elseNodes = new List<object>();
390-
if (ifs.Else is { } e && !LowerFlowStmt(e.Statement, tracked, elseNodes, canEscape, onThrow))
394+
if (ifs.Else is { } e && !LowerFlowStmt(e.Statement, tracked, elseNodes, canEscape, onThrow, onReturn))
391395
return false;
392396
nodes.Add(new { op = "if", line = LineOf(ifs), then = thenNodes, @else = elseNodes });
393397
return true;
394398
}
395399
case UsingStatementSyntax us:
396400
// using(...) {body}: the using local is auto-disposed (untracked); still
397401
// lower the body so a tracked plain local used inside is seen.
398-
return us.Statement is null || LowerFlowStmt(us.Statement, tracked, nodes, canEscape, onThrow);
402+
return us.Statement is null || LowerFlowStmt(us.Statement, tracked, nodes, canEscape, onThrow, onReturn);
399403
case ReturnStatementSyntax rs:
400-
// a tracked local never escapes (excluded), so a returned value is not a
401-
// tracked resource — model it as a bare return (a CFG exit edge).
402-
nodes.Add(new { op = "return", var = (string?)null, line = LineOf(rs) });
404+
// a tracked local never escapes (excluded), so a returned value is not a tracked
405+
// resource. A `return` first runs any enclosing `finally`(s) — threaded as
406+
// `onReturn`, so a resource the finally disposes is released on the return path —
407+
// then exits; outside a try it is a bare CFG exit edge.
408+
if (onReturn is not null)
409+
nodes.AddRange(onReturn);
410+
else
411+
nodes.Add(new { op = "return", var = (string?)null, line = LineOf(rs) });
403412
return true;
404413
case WhileStatementSyntax ws:
405414
{
@@ -409,7 +418,7 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, List<obje
409418
// double-release). The condition is opaque (we model control flow, not
410419
// values). If the body has an unmodelled statement, bail the method.
411420
var bodyNodes = new List<object>();
412-
if (ws.Statement is null || !LowerFlowStmt(ws.Statement, tracked, bodyNodes, canEscape, onThrow))
421+
if (ws.Statement is null || !LowerFlowStmt(ws.Statement, tracked, bodyNodes, canEscape, onThrow, onReturn))
413422
return false;
414423
nodes.Add(new { op = "while", line = LineOf(ws), body = bodyNodes });
415424
return true;
@@ -419,10 +428,10 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, List<obje
419428
// `foreach` runs its body 0+ times over an (opaque) collection — the same
420429
// ownership shape as `while`. The loop variable is never a `new`'d
421430
// candidate and the hidden enumerator is auto-disposed, so modelling the
422-
// body as a `while` is sound. (`do` stays unmodelled below — it runs 1+
423-
// times; `for` is handled next.)
431+
// body as a `while` is sound. (`for` and `do` are handled below; `do` runs 1+
432+
// times, so it is desugared rather than modelled as a bare 0+-trip `while`.)
424433
var bodyNodes = new List<object>();
425-
if (fes.Statement is null || !LowerFlowStmt(fes.Statement, tracked, bodyNodes, canEscape, onThrow))
434+
if (fes.Statement is null || !LowerFlowStmt(fes.Statement, tracked, bodyNodes, canEscape, onThrow, onReturn))
426435
return false;
427436
nodes.Add(new { op = "while", line = LineOf(fes), body = bodyNodes });
428437
return true;
@@ -434,9 +443,9 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, List<obje
434443
// model control flow, not values); the tracked locals are the ones the
435444
// BODY declares. A resource declared in the `for` *initializer* is not a
436445
// method-body local, so it is never a tracked candidate — no soundness
437-
// concern, just a separate (rare) recall gap. `do`/try/switch stay below.
446+
// concern, just a separate (rare) recall gap.
438447
var bodyNodes = new List<object>();
439-
if (fors.Statement is null || !LowerFlowStmt(fors.Statement, tracked, bodyNodes, canEscape, onThrow))
448+
if (fors.Statement is null || !LowerFlowStmt(fors.Statement, tracked, bodyNodes, canEscape, onThrow, onReturn))
440449
return false;
441450
nodes.Add(new { op = "while", line = LineOf(fors), body = bodyNodes });
442451
return true;
@@ -458,13 +467,6 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, List<obje
458467
foreach (var cc in trys.Catches)
459468
if (cc.Block.DescendantNodes().OfType<InvocationExpressionSyntax>().Any(IsDisposeShaped))
460469
return false;
461-
// A `return` in a try-WITH-finally isn't threaded through the exceptional exits
462-
// below (finally-before-return), so its finally release would be unreachable ->
463-
// bail to stay sound. The common `try { …; return; } finally { dispose }` is
464-
// safe anyway, so skipping it loses no real catch.
465-
if (trys.Finally is not null
466-
&& trys.Block.DescendantNodes().OfType<ReturnStatementSyntax>().Any())
467-
return false;
468470
var finallyNodes = new List<object>();
469471
if (trys.Finally is { } fin && !LowerFlowStmt(fin.Block, tracked, finallyNodes))
470472
return false;
@@ -493,15 +495,98 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, List<obje
493495
bodyOnThrow.AddRange(onThrow ?? new List<object>
494496
{ new { op = "return", var = (string?)null, line = LineOf(trys) } });
495497
}
498+
// A `return` inside the body runs THIS finally, then the enclosing return path
499+
// (its finallys), then exits — independent of catches (a `return` is never caught,
500+
// unlike a throw), so it is threaded even where the throw edges are suppressed. The
501+
// finally release thus runs before the return: `try { …; return; } finally { d }`
502+
// disposes on the return path instead of being bailed.
503+
var bodyOnReturn = new List<object>(finallyNodes);
504+
bodyOnReturn.AddRange(onReturn ?? new List<object>
505+
{ new { op = "return", var = (string?)null, line = LineOf(trys) } });
496506
foreach (var stmt in trys.Block.Statements)
497-
if (!LowerFlowStmt(stmt, tracked, nodes, bodyCanEscape, bodyOnThrow))
507+
if (!LowerFlowStmt(stmt, tracked, nodes, bodyCanEscape, bodyOnThrow, bodyOnReturn))
498508
return false;
499509
nodes.AddRange(finallyNodes); // normal completion runs the finally
500510
return true;
501511
}
512+
case DoStatementSyntax dos:
513+
{
514+
// do { B } while(c) ≡ B; while(c) { B } — the body runs 1+ times. Lower B once
515+
// unconditionally (the guaranteed first iteration), then a `while` of B (0+ more).
516+
// Modelling it as a plain `while` (0+ trips) would be UNSOUND: a resource released
517+
// only in the body but acquired before the loop would falsely leak on the phantom
518+
// 0-trip path. Bail (like the loops) if the body has an unmodelled statement.
519+
if (dos.Statement is null
520+
|| !LowerFlowStmt(dos.Statement, tracked, nodes, canEscape, onThrow, onReturn))
521+
return false;
522+
var bodyNodes = new List<object>();
523+
if (!LowerFlowStmt(dos.Statement, tracked, bodyNodes, canEscape, onThrow, onReturn))
524+
return false;
525+
nodes.Add(new { op = "while", line = LineOf(dos), body = bodyNodes });
526+
return true;
527+
}
528+
case SwitchStatementSyntax sw:
529+
{
530+
// switch(e) { (case L: | default:) section … } modelled as a chain of opaque,
531+
// mutually-exclusive branches: `if(*){ s1 } else { if(*){ s2 } else { … } }` — one
532+
// per section, value-opaque (we model control flow, not the matched value). A
533+
// trailing `break` ends a section (stripped); a section doing anything the model
534+
// can't place here (a nested `break`, `goto case`, `throw`) bails the method.
535+
List<object>? defaultNodes = null;
536+
var cases = new List<List<object>>();
537+
foreach (var section in sw.Sections)
538+
{
539+
var secNodes = new List<object>();
540+
if (!LowerSwitchSection(section, tracked, secNodes, canEscape, onThrow, onReturn))
541+
return false;
542+
if (section.Labels.Any(l => l is DefaultSwitchLabelSyntax))
543+
defaultNodes = secNodes;
544+
else
545+
cases.Add(secNodes);
546+
}
547+
// The chain's tail — the "no earlier case matched" branch. A `default` IS that
548+
// branch. With NO default we do NOT model an empty no-match path: that would falsely
549+
// flag a resource disposed in every case of an EXHAUSTIVE switch (e.g. over an enum)
550+
// as leaking on a path that cannot occur. Instead the LAST case becomes the tail
551+
// (assume some branch runs) — sound: a genuinely non-exhaustive no-match leak is only
552+
// missed when EVERY case disposes the resource (a recall gap, never a false positive).
553+
List<object> chain;
554+
if (defaultNodes is not null)
555+
chain = defaultNodes;
556+
else if (cases.Count == 0)
557+
return true; // empty switch -> no flow effect
558+
else
559+
{
560+
chain = cases[^1];
561+
cases.RemoveAt(cases.Count - 1);
562+
}
563+
for (int k = cases.Count - 1; k >= 0; k--)
564+
chain = new List<object>
565+
{ new { op = "if", line = LineOf(sw), then = cases[k], @else = chain } };
566+
nodes.AddRange(chain);
567+
return true;
568+
}
502569
default:
503-
return false; // unmodelled (do/switch/...) -> bail the method
570+
return false; // unmodelled (goto/labeled/throw/...) -> bail the method
571+
}
572+
}
573+
574+
// Lower one `switch` section's statements, treating a top-level `break` as the section
575+
// terminator (it just exits the switch, so it is stripped). Bails (false) on any statement the
576+
// flow model doesn't handle here — including a `break` nested inside an `if`/loop, which reaches
577+
// the unmodelled default and conservatively skips the whole method.
578+
static bool LowerSwitchSection(SwitchSectionSyntax section, HashSet<string> tracked,
579+
List<object> nodes, bool canEscape,
580+
List<object>? onThrow, List<object>? onReturn)
581+
{
582+
foreach (var stmt in section.Statements)
583+
{
584+
if (stmt is BreakStatementSyntax)
585+
break;
586+
if (!LowerFlowStmt(stmt, tracked, nodes, canEscape, onThrow, onReturn))
587+
return false;
504588
}
589+
return true;
505590
}
506591

507592
static void EmitFlowExpr(ExpressionSyntax expr, HashSet<string> tracked, List<object> nodes)

0 commit comments

Comments
 (0)