Skip to content

Commit 05ad3e3

Browse files
committed
fix(pool): teach metamorphic checker overspan + scan local-init full views (POOL005)
Two follow-ups after PR #71's first CI run: 1. Metamorphic selftest (the failing job): the alpha-rename / reorder transforms in scripts/metamorphic.py did not know the new `overspan` statement, so renaming `buf` left `overspan buf` referencing the vanished name (OWN025 -> OWN030, non-invariant). Teach _sub_stmt / _all_names / _touches that `overspan` reads its var, exactly like `use`/`release`. Selftest back to 8/8. 2. Codex review (Program.cs): a full view written as a local-declaration initializer (`var copy = buf.AsSpan().ToArray();`) was missed — LowerFlowStmt's LocalDeclarationStatementSyntax branch never calls EmitFlowExpr on a non-acquire initializer. Refactor the overspan scan into a shared EmitOverspans helper and call it from the local-decl branch too, so the over-copy initializer AND a view-local declaration (`Span<T> s = buf.AsSpan();`) are both caught. The corpus before.cs now exercises both spellings; after.cs bounds both. Spec / notes / P-007 updated for expression + initializer coverage (only the `Array.Clear(buf, 0, buf.Length)` spelling remains a follow-up). The corpus benchmark already validated recall 14 (before.cs -> OWN025) on the prior run. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 57bc1ba commit 05ad3e3

6 files changed

Lines changed: 59 additions & 34 deletions

File tree

‎corpus/real-world/arraypool-fullspan-overread/after.cs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
// AFTER (fixed). The view is BOUNDED to the logical length — `buf.AsSpan(0, n)` —
2-
// so only the `n` valid payload bytes are handed off; the oversized `[n, Length)`
3-
// tail is never read. The unbounded full view is gone, so the extractor emits no
4-
// `overspan` fact and the checker is silent.
1+
// AFTER (fixed). Every view is BOUNDED to the logical length — `buf.AsSpan(0, n)` —
2+
// so only the `n` valid payload bytes are read; the oversized `[n, Length)` tail is
3+
// never touched. No unbounded full view remains in either spelling (expression or
4+
// initializer), so the extractor emits no `overspan` fact and the checker is silent.
55
using System;
66
using System.Buffers;
77

88
static class PoolFullSpanOverread
99
{
10-
static void Frame(int n)
10+
static byte[] Frame(int n)
1111
{
1212
byte[] buf = ArrayPool<byte>.Shared.Rent(n);
1313
Fill(buf, n);
14-
Emit(buf.AsSpan(0, n)); // <-- bounded view: only the logical [0, n)
14+
Emit(buf.AsSpan(0, n)); // bounded view: only the logical [0, n)
15+
byte[] copy = buf.AsSpan(0, n).ToArray(); // bounded copy: only the logical [0, n)
1516
ArrayPool<byte>.Shared.Return(buf);
17+
return copy;
1618
}
1719

1820
static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; }

‎corpus/real-world/arraypool-fullspan-overread/before.cs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// BEFORE (buggy). Reduction of the ArrayPool "full-length view over-read" bug
22
// (P-007 POOL005). A rented array is OVERSIZED: `ArrayPool<T>.Shared.Rent(n)`
33
// returns an array of `Length >= n`, not exactly `n`. Here the code fills the
4-
// first `n` bytes, then hands off a FULL-length view — `buf.AsSpan()` with no
5-
// length — so the consumer reads the `n` valid bytes PLUS the stale `[n, Length)`
6-
// tail a *previous* renter left behind: a wrong-length read and an information
7-
// disclosure. The fix is a bounded view, `buf.AsSpan(0, n)` (see after.cs).
8-
// Representative of the pattern (pooled-buffer over-read/over-copy in tensor and
9-
// serialization code), not verbatim from one PR.
4+
// first `n` bytes, then takes a FULL-length view — `buf.AsSpan()` with no length —
5+
// so the `n` valid bytes are read together with the stale `[n, Length)` tail a
6+
// *previous* renter left behind: a wrong-length read and an information disclosure.
7+
// Both spellings are caught: the view used in an EXPRESSION (`Emit(buf.AsSpan())`)
8+
// and the view in a local-declaration INITIALIZER (`var copy = buf.AsSpan()...`).
9+
// The fix is a bounded view, `buf.AsSpan(0, n)` (see after.cs). Representative of
10+
// the pattern (pooled-buffer over-read/over-copy in tensor and serialization code),
11+
// not verbatim from one PR.
1012
//
1113
// Wrapped in a class so the extractor's per-class flow pass visits it; helpers
1214
// stubbed so the reduction is self-contained.
@@ -15,13 +17,15 @@
1517

1618
static class PoolFullSpanOverread
1719
{
18-
static void Frame(int n)
20+
static byte[] Frame(int n)
1921
{
2022
byte[] buf = ArrayPool<byte>.Shared.Rent(n);
21-
Fill(buf, n); // valid payload is buf[0..n]
22-
Emit(buf.AsSpan()); // <-- BUG: the WHOLE oversized array, not buf.AsSpan(0, n):
23-
// n payload bytes + the stale [n, Length) tail
23+
Fill(buf, n); // valid payload is buf[0..n]
24+
Emit(buf.AsSpan()); // <-- BUG (expression): the WHOLE oversized array,
25+
// n payload bytes + the stale [n, Length) tail
26+
byte[] copy = buf.AsSpan().ToArray(); // <-- BUG (initializer): the same over-read in a local
2427
ArrayPool<byte>.Shared.Return(buf);
28+
return copy;
2529
}
2630

2731
static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; }

‎corpus/real-world/arraypool-fullspan-overread/notes.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ leak and no OWN002 use-after-return; the only finding is the over-read itself.
2424

2525
**Honesty / scope.** `case.own` is a faithful hand reduction (not C# ingested by the
2626
checker); `before.cs` / `after.cs` are representative of the bug and its fix, not a
27-
verbatim PR diff. This first slice catches the unbounded view used in an
28-
*expression* (`Emit(buf.AsSpan())`, `buf.AsSpan().CopyTo(...)`, `... .ToArray()`).
29-
A view stored in a local first (`var s = buf.AsSpan(); Use(s);`) and the
30-
`Array.Clear(buf, 0, buf.Length)` spelling are follow-ups.
27+
verbatim PR diff. The extractor catches the unbounded view both as an *expression*
28+
(`Emit(buf.AsSpan())`, `buf.AsSpan().CopyTo(...)`) and in a local-declaration
29+
*initializer* — the over-copy `var copy = buf.AsSpan().ToArray();` and the
30+
view-local `Span<byte> s = buf.AsSpan();` alike, since the full view lives in the
31+
initializer either way. The remaining spelling is `Array.Clear(buf, 0, buf.Length)`
32+
(a length argument of `buf.Length` rather than an unbounded view), a follow-up.
3133

3234
Reference: [P-007](../../../docs/proposals/P-007-arraypool-span.md); replay target
3335
AiDotNet.Tensors pooled-buffer over-clear/over-read.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
The borrow checker on real C#. **POOL005 (full-length view over-read → OWN025) first slice
1010
built** — an unbounded `buf.AsSpan()` / `buf.AsMemory()` / `new Span<T>(buf)` over a `Rent`ed
1111
local reaches past the logical length `n` into the oversized `[n, Length)` tail; the extractor
12-
emits an `overspan` fact and the core raises OWN025 `[resource: pooled buffer]` at the view
13-
(corpus `arraypool-fullspan-overread`). POOL003 (double-return — flow already catches OWN003), a
14-
view stored in a FIELD, and the view-LOCAL / `Array.Clear(buf, 0, buf.Length)` spellings of
15-
POOL005 next
12+
emits an `overspan` fact and the core raises OWN025 `[resource: pooled buffer]` at the view —
13+
both when the view is used in an EXPRESSION (`Emit(buf.AsSpan())`) and when it is a local-decl
14+
INITIALIZER (`var copy = buf.AsSpan().ToArray();`, `Span<T> s = buf.AsSpan();`) (corpus
15+
`arraypool-fullspan-overread`). POOL003 (double-return — flow already catches OWN003), a view
16+
stored in a FIELD, and the `Array.Clear(buf, 0, buf.Length)` spelling of POOL005 next
1617
- **Depends on:** `spec/OwnCore.md` (OWN001 leak, OWN002 use-after-release,
1718
OWN003 double-release, OWN008 release-while-borrowed), the buffer/borrow model
1819
in `spec/`, [P-001](P-001-csharp-extractor.md). See
@@ -44,7 +45,7 @@ The corpus already pins two real cases (`corpus/real-world/arraypool-double-retu
4445
| **POOL002** view after return | a `Span`/`Memory` view used after the owner is `Return`ed | `OWN002` |
4546
| **POOL003** double return | `Return` reachable twice for the same buffer | `OWN003` |
4647
| **POOL004** view escapes | a borrowed `Span` returned/stored beyond the owner's lifetime | `OWN004`/`OWN008` |
47-
| **POOL005** clear/copy past length | a full-length view (`buf.AsSpan()`, no bound) reads/copies beyond the logical length of the rented region | `OWN025` `[resource: pooled buffer]` ✅ (view in an expression; view-local / `Array.Clear` next) |
48+
| **POOL005** clear/copy past length | a full-length view (`buf.AsSpan()`, no bound) reads/copies beyond the logical length of the rented region | `OWN025` `[resource: pooled buffer]` ✅ (view in an expression or initializer; `Array.Clear(buf, 0, buf.Length)` next) |
4849

4950
Resource mapping:
5051

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,19 @@ static bool LowerFlowStmt(StatementSyntax st, HashSet<string> tracked, SemanticM
450450
InjectThrowEdge(ld, nodes, onThrow);
451451
if (ld.UsingKeyword == default)
452452
foreach (var v in ld.Declaration.Variables)
453+
{
453454
if (tracked.Contains(v.Identifier.Text)
454455
&& (v.Initializer?.Value is ObjectCreationExpressionSyntax
455456
or ImplicitObjectCreationExpressionSyntax
456457
|| IsPoolRent(v.Initializer?.Value, model) // ArrayPool<T> Rent
457458
|| IsOwningFactory(v.Initializer?.Value, model))) // File / crypto Create* factory
458459
nodes.Add(new { op = "acquire", var = v.Identifier.Text, line = LineOf(v) });
460+
// POOL005: a full-length view in the initializer — `var copy = buf.AsSpan().ToArray();`
461+
// — over-reads the pooled tail just as `Emit(buf.AsSpan());` does. EmitFlowExpr is not
462+
// called on a non-acquire initializer, so scan it here for the overspan (Codex review).
463+
if (v.Initializer?.Value is { } vinit)
464+
EmitOverspans(vinit, tracked, model, nodes);
465+
}
459466
return true;
460467
case ExpressionStatementSyntax es:
461468
InjectThrowEdge(es, nodes, onThrow);
@@ -796,13 +803,19 @@ static void EmitFlowExpr(ExpressionSyntax expr, HashSet<string> tracked, Semanti
796803
}
797804
foreach (var u in used)
798805
nodes.Add(new { op = "use", var = u, line = LineOf(expr) });
799-
// POOL005: a FULL-LENGTH view of a tracked pooled buffer (no length bound) reaches past the
800-
// logical length it was Rent'd for — the oversized [n, Length) tail. Emit one `overspan` op per
801-
// owner so the core raises OWN025 (a property of the view-creation site, alongside the plain
802-
// `use` above). Walks the whole expression so a view nested in a call (`Sink.Write(buf.AsSpan())`)
803-
// or a chain (`buf.AsSpan().ToArray()`) is found; a BOUNDED view (`buf.AsSpan(0, n)`) is not
804-
// matched (FullViewOwner returns null). Among `tracked` locals only pooled buffers are arrays the
805-
// BCL AsSpan/Span symbols bind to, so this never fires on a tracked IDisposable / factory result.
806+
// POOL005: a full-length view of a pooled buffer anywhere in this expression -> overspan/OWN025.
807+
EmitOverspans(expr, tracked, model, nodes);
808+
}
809+
810+
// POOL005: emit one `overspan` op per tracked pooled buffer that `expr` takes a FULL-LENGTH view of
811+
// (no length bound) — the core raises OWN025. Walks the whole expression so a view nested in a call
812+
// (`Sink.Write(buf.AsSpan())`), a chain (`buf.AsSpan().ToArray()`), or a local-declaration
813+
// initializer (`var copy = buf.AsSpan().ToArray();`) is found; a BOUNDED view (`buf.AsSpan(0, n)`)
814+
// is not matched (FullViewOwner returns null). Among `tracked` locals only pooled buffers are the
815+
// arrays the BCL AsSpan/Span symbols bind to, so this never fires on a tracked IDisposable / factory
816+
// result. Shared by the expression-statement and local-declaration lowering paths.
817+
static void EmitOverspans(ExpressionSyntax expr, HashSet<string> tracked, SemanticModel model, List<object> nodes)
818+
{
806819
var overspanned = new SortedSet<string>(StringComparer.Ordinal);
807820
foreach (var node in expr.DescendantNodesAndSelf().OfType<ExpressionSyntax>())
808821
if (FullViewOwner(node, model) is { } owner && tracked.Contains(owner))

‎scripts/metamorphic.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
Let,
5151
Module,
5252
Move,
53+
Overspan,
5354
Release,
5455
Return,
5556
Subscribe,
@@ -131,6 +132,8 @@ def _sub_stmt(s: Stmt, old: str, new: str) -> Stmt:
131132
return replace(s, var=new) if s.var == old else s
132133
if isinstance(s, Use):
133134
return replace(s, var=new) if s.var == old else s
135+
if isinstance(s, Overspan):
136+
return replace(s, var=new) if s.var == old else s
134137
if isinstance(s, Call):
135138
return replace(s, args=[_sub_expr(a, old, new) for a in s.args])
136139
if isinstance(s, BorrowBlock):
@@ -185,7 +188,7 @@ def walk(body: list[Stmt]) -> None:
185188
if isinstance(s, Let):
186189
names.add(s.name)
187190
names.update(_expr_names(s.rhs))
188-
elif isinstance(s, (Release, Use)):
191+
elif isinstance(s, (Release, Use, Overspan)):
189192
names.add(s.var)
190193
elif isinstance(s, Call):
191194
for a in s.args:
@@ -243,7 +246,7 @@ def _touches(s: Stmt) -> set[str] | None:
243246
simple, reorderable statement (control flow / borrow blocks are excluded)."""
244247
if isinstance(s, Let):
245248
return {s.name} | _expr_names(s.rhs)
246-
if isinstance(s, (Release, Use)):
249+
if isinstance(s, (Release, Use, Overspan)):
247250
return {s.var}
248251
if isinstance(s, Call):
249252
return set().union(set(), *(_expr_names(a) for a in s.args))

0 commit comments

Comments
 (0)