Skip to content

Commit 609fdbd

Browse files
committed
feat(pool): MemoryPool using-owned view escape — using owner; return owner.Memory → OWN002 (POOL004)
The follow-up Codex flagged on #73. `using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(n); return owner.Memory;` is sugar for `try { return owner.Memory; } finally { owner.Dispose(); }` — the implicit scope-exit dispose hands the caller a Memory backed by a buffer already returned to the pool: a dangling borrow. Previously silent because the flow-locals candidate pass skips `using` locals (auto-disposed, non-leak), so the owner never entered `tracked`. Fix (extractor only, reuses #70's onReturn / ReturnedViewOwners machinery): a new LowerFlowStatements desugars a TRACKED `using IMemoryOwner = MemoryPool.Rent(...)` declaration into `acquire; try { rest } finally { release }` — the using-dispose is threaded onto the rest's returns/throws, so a returned view of the owner is read after the release -> OWN002. Routed from LowerFlowBody, the BlockSyntax case, and the try-body. With no such `using` declaration, LowerFlowStatements lowers each statement exactly as the old loop, so every existing shape (incl. #73's `using owner; Consume(owner.Memory.Span)` after.cs) is unchanged. The candidate pass now tracks `using` MemoryPool owners (gated to IsMemoryPoolRent). Corpus: memorypool-using-view-escape (before: `using owner; return owner.Memory` -> OWN002; after: return the IMemoryOwner, ownership transferred -> silent) — the MemoryPool twin of arraypool-memory-view-escape (#70). Benchmark recall floor 17 -> 18. P-007 status updated. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 377c182 commit 609fdbd

8 files changed

Lines changed: 154 additions & 12 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,5 +798,5 @@ jobs:
798798
# (`ViewOwner`), so reading it after Dispose trips OWN002 (POOL002 — `memorypool-view-after-
799799
# dispose`). Remaining backlog: a FIELD-mediated cross-method use-after-dispose, a view stored in
800800
# a FIELD, and an injected-source region-escape. A drop below the floor is a regression.
801-
run: python scripts/benchmark.py --min-recall 17
801+
run: python scripts/benchmark.py --min-recall 18
802802

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// AFTER (fixed). Ownership is TRANSFERRED to the caller: the method returns the
2+
// `IMemoryOwner<T>` itself (no `using`), so nothing is disposed here. The caller owns the
3+
// owner's lifetime (`using var owner = Borrow(n); … owner.Memory …`) and the pooled buffer
4+
// stays alive as long as the returned view is used. No dangling borrow, so the extractor
5+
// untracks the escaped owner and the checker is silent.
6+
using System;
7+
using System.Buffers;
8+
9+
static class MemoryPoolUsingViewEscape
10+
{
11+
static IMemoryOwner<byte> Borrow(int n)
12+
{
13+
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(n);
14+
return owner; // transfer ownership to the caller — the caller disposes it
15+
}
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// BEFORE (buggy). POOL004 for MemoryPool, the idiomatic-`using` dangle. An
2+
// `IMemoryOwner<T>` from `MemoryPool<T>` is held with a `using` declaration — so it is
3+
// `Dispose()`d at scope exit — but the method RETURNS `owner.Memory`, a borrow of the
4+
// owner's pooled buffer. The implicit dispose runs as the method returns, so the caller
5+
// receives a `Memory<T>` backed by memory already handed back to the pool: a dangling
6+
// borrow / use-after-free. `using owner = …; return owner.Memory;` is exactly
7+
// `try { return owner.Memory; } finally { owner.Dispose(); }` — the MemoryPool twin of
8+
// the ArrayPool try/finally `Memory` escape. The fix is to TRANSFER ownership: return the
9+
// `IMemoryOwner` itself (no `using`) and let the caller own its lifetime (see after.cs).
10+
//
11+
// Wrapped in a class so the extractor's per-class flow pass visits it.
12+
using System;
13+
using System.Buffers;
14+
15+
static class MemoryPoolUsingViewEscape
16+
{
17+
static Memory<byte> Borrow(int n)
18+
{
19+
using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(n);
20+
return owner.Memory; // <-- BUG: the `using` disposes owner as we return, so the
21+
// caller gets a view of buffer already returned to the pool
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// OwnLang model of the MemoryPool `using`-view escape. `acquire` == MemoryPool.Rent,
2+
// `release` == the implicit `using` scope-exit Dispose. The method returns `owner.Memory`
3+
// — a borrow of the owner — but the `using` disposes the owner FIRST (the extractor
4+
// desugars `using owner = …; return owner.Memory;` to `acquire; release; use`), so the
5+
// caller reads a view of memory already returned to the pool: a use-after-release lowered
6+
// to OWN002. (The using-dispose is threaded before the return's view-use, mirroring the
7+
// ArrayPool try/finally Memory escape.)
8+
module Corpus
9+
resource MemoryOwner {
10+
acquire Rent
11+
release Dispose
12+
}
13+
fn lease(n: int) {
14+
let owner = acquire MemoryOwner(n); // using MemoryPool.Rent
15+
release owner; // implicit `using` Dispose at scope exit
16+
use owner; // return owner.Memory — read by the caller AFTER Dispose -> OWN002
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN002
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# MemoryPool `using`-owned view escape (POOL004, the idiomatic dangle)
2+
3+
**Pattern:** an `IMemoryOwner<T>` from `MemoryPool<T>` is held with a `using`
4+
declaration (so it is `Dispose()`d at scope exit), but the method **returns a view of
5+
it** — `using owner = …; return owner.Memory;`. The implicit dispose runs as the method
6+
returns, so the caller receives a `Memory<T>` backed by a buffer already returned to the
7+
pool: a dangling borrow / use-after-free. It is exactly
8+
`try { return owner.Memory; } finally { owner.Dispose(); }` — the MemoryPool twin of the
9+
ArrayPool try/finally `Memory` escape (`arraypool-memory-view-escape`, #70). The fix is to
10+
**transfer ownership**: return the `IMemoryOwner` itself (no `using`) so the caller owns
11+
its lifetime.
12+
13+
**Why it was missed before (Codex review on #73):** `using` locals are skipped by the
14+
flow-locals candidate pass — they are auto-disposed, so normally not leak candidates — so
15+
the owner never entered `tracked` and the returned view was not mapped to it. This slice
16+
**desugars a tracked `using IMemoryOwner = MemoryPool.Rent(…)` declaration** into
17+
`acquire; try { rest } finally { release }`: the implicit scope-exit dispose is threaded
18+
onto the rest's returns (and throws), so the returned view's caller-use lands *after* the
19+
release and trips OWN002 — reusing the same `onReturn` / `ReturnedViewOwners` machinery
20+
that catches the ArrayPool try/finally form. Methods with no such `using` declaration are
21+
lowered exactly as before.
22+
23+
**What the checker says:** the OwnLang model and the real `before.cs` both trip
24+
**OWN002**. The ownership-transfer fix in `after.cs` returns the owner (no `using`), so the
25+
escaped owner is untracked and the checker is silent.
26+
27+
**Honesty / scope.** `case.own` is a faithful hand reduction (not C# ingested by the
28+
checker); `before.cs` / `after.cs` are representative of the bug and its fix.
29+
30+
Reference: [P-007](../../../docs/proposals/P-007-arraypool-span.md); the ArrayPool twin is
31+
`arraypool-memory-view-escape` (#70).

‎docs/proposals/P-007-arraypool-span.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
so its leak / double-dispose ride the same flow as POOL001/003 (corpus `memorypool-double-dispose`
2323
→ OWN003), and its `owner.Memory` / `owner.Memory.Span` is a borrow lowered to a use of the OWNER
2424
(`ViewOwner`), so reading the view after `Dispose` trips **POOL002 → OWN002** (corpus
25-
`memorypool-view-after-dispose`). A POOL005 view stored in a FIELD is next
25+
`memorypool-view-after-dispose`). The idiomatic `using owner = MemoryPool.Rent(…); return
26+
owner.Memory;` — which dangles after the implicit scope-exit dispose — is caught too:
27+
the flow desugars a tracked `using IMemoryOwner` declaration to `acquire; try { rest }
28+
finally { release }`, so the returned view is read after the release (**POOL004 → OWN002**;
29+
corpus `memorypool-using-view-escape`). A POOL005 view stored in a FIELD is next
2630
- **Depends on:** `spec/OwnCore.md` (OWN001 leak, OWN002 use-after-release,
2731
OWN003 double-release, OWN008 release-while-borrowed), the buffer/borrow model
2832
in `spec/`, [P-001](P-001-csharp-extractor.md). See

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

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,58 @@ cc.Filter is null
422422
static List<object>? LowerFlowBody(BlockSyntax block, HashSet<string> tracked, SemanticModel model)
423423
{
424424
var nodes = new List<object>();
425-
foreach (var st in block.Statements)
426-
if (!LowerFlowStmt(st, tracked, model, nodes))
427-
return null;
425+
if (!LowerFlowStatements(block.Statements, 0, tracked, model, nodes,
426+
canEscape: true, onThrow: null, onReturn: null))
427+
return null;
428428
return nodes;
429429
}
430430

431+
// Lower a statement LIST, desugaring a tracked `using IMemoryOwner owner = MemoryPool.Rent(...)`
432+
// declaration into `acquire; try { rest } finally { release }` — the implicit scope-exit Dispose is
433+
// threaded onto the rest's returns/throws (exactly like a `finally`), so a RETURNED view of the owner
434+
// (`using owner = …; return owner.Memory;`) is a dangling borrow read by the caller after the dispose
435+
// -> OWN002 (Codex review on #73). With no such declaration this is identical to lowering each
436+
// statement in turn, so non-pooled `using` locals and every existing shape are unaffected.
437+
static bool LowerFlowStatements(IReadOnlyList<StatementSyntax> stmts, int start,
438+
HashSet<string> tracked, SemanticModel model, List<object> nodes,
439+
bool canEscape, List<object>? onThrow, List<object>? onReturn)
440+
{
441+
for (var i = start; i < stmts.Count; i++)
442+
{
443+
var st = stmts[i];
444+
if (st is LocalDeclarationStatementSyntax usingDecl
445+
&& usingDecl.UsingKeyword != default
446+
&& usingDecl.Declaration.Variables.Count == 1
447+
&& tracked.Contains(usingDecl.Declaration.Variables[0].Identifier.Text)
448+
&& IsMemoryPoolRent(usingDecl.Declaration.Variables[0].Initializer?.Value, model))
449+
{
450+
var uv = usingDecl.Declaration.Variables[0];
451+
var owner = uv.Identifier.Text;
452+
var exit = new List<object> { new { op = "return", var = (string?)null, line = LineOf(usingDecl) } };
453+
InjectThrowEdge(usingDecl, nodes, onThrow); // a throw DURING Rent() runs the OUTER path (owner not yet acquired)
454+
nodes.Add(new { op = "acquire", var = owner, line = LineOf(uv) });
455+
var release = new { op = "release", var = owner, line = LineOf(uv) };
456+
// The rest of THIS block is the try-body; the implicit using-dispose is its finally — run
457+
// before a return (so a returned view is read after release) and on normal completion.
458+
var restOnReturn = new List<object> { release };
459+
restOnReturn.AddRange(onReturn ?? exit);
460+
List<object>? restOnThrow = null;
461+
if (canEscape)
462+
{
463+
restOnThrow = new List<object> { release };
464+
restOnThrow.AddRange(onThrow ?? exit);
465+
}
466+
if (!LowerFlowStatements(stmts, i + 1, tracked, model, nodes, canEscape, restOnThrow, restOnReturn))
467+
return false;
468+
nodes.Add(release); // normal completion (no return) disposes at scope exit too
469+
return true;
470+
}
471+
if (!LowerFlowStmt(st, tracked, model, nodes, canEscape, onThrow, onReturn))
472+
return false;
473+
}
474+
return true;
475+
}
476+
431477
// `canEscape`: can a throw at the current position leave the METHOD (no enclosing
432478
// catch-all swallows it)? `onThrow`: the continuation a throw here runs to leave the
433479
// method (finally-stack + return), or null when no exception edge should be injected
@@ -442,10 +488,7 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, SemanticM
442488
switch (st)
443489
{
444490
case BlockSyntax b:
445-
foreach (var s2 in b.Statements)
446-
if (!LowerFlowStmt(s2, tracked, model, nodes, canEscape, onThrow, onReturn))
447-
return false;
448-
return true;
491+
return LowerFlowStatements(b.Statements, 0, tracked, model, nodes, canEscape, onThrow, onReturn);
449492
case LocalDeclarationStatementSyntax ld:
450493
InjectThrowEdge(ld, nodes, onThrow);
451494
if (ld.UsingKeyword == default)
@@ -608,9 +651,8 @@ or ImplicitObjectCreationExpressionSyntax
608651
var bodyOnReturn = new List<object>(finallyNodes);
609652
bodyOnReturn.AddRange(onReturn ?? new List<object>
610653
{ new { op = "return", var = (string?)null, line = LineOf(trys) } });
611-
foreach (var stmt in trys.Block.Statements)
612-
if (!LowerFlowStmt(stmt, tracked, model, nodes, bodyCanEscape, bodyOnThrow, bodyOnReturn))
613-
return false;
654+
if (!LowerFlowStatements(trys.Block.Statements, 0, tracked, model, nodes, bodyCanEscape, bodyOnThrow, bodyOnReturn))
655+
return false;
614656
nodes.AddRange(finallyNodes); // normal completion runs the finally
615657
return true;
616658
}
@@ -1941,7 +1983,15 @@ or ImplicitObjectCreationExpressionSyntax
19411983
foreach (var ld in mbody.DescendantNodes().OfType<LocalDeclarationStatementSyntax>())
19421984
{
19431985
if (ld.UsingKeyword != default)
1986+
{
1987+
// A `using` local is auto-disposed (not a leak candidate). But a `using` MemoryPool
1988+
// owner whose Memory VIEW is returned dangles after the implicit scope-exit dispose —
1989+
// track it so the flow's using-desugaring catches that escape. Others stay skipped.
1990+
foreach (var v in ld.Declaration.Variables)
1991+
if (IsMemoryPoolRent(v.Initializer?.Value, model))
1992+
candidates.Add(v.Identifier.Text);
19441993
continue;
1994+
}
19451995
foreach (var v in ld.Declaration.Variables)
19461996
if (v.Initializer is { Value: ObjectCreationExpressionSyntax
19471997
or ImplicitObjectCreationExpressionSyntax } init

0 commit comments

Comments
 (0)