Skip to content

Commit b3bf5b1

Browse files
committed
feat(pool): POOL005 .Length spelling + MemoryPool tracking (POOL001/002/003)
Two pooled-buffer extractor additions, both riding machinery that already exists in the core — so there are NO core changes, only the Roslyn extractor + corpus + spec. POOL005 — the `.Length` spelling of the over-read/over-clear (sibling of #71's unbounded `buf.AsSpan()`). A pooled array is oversized (Rent(n) -> Length >= n), so passing the buffer's OWN `buf.Length` as the operative length reaches the whole array, past the rented n, into the stale [n, Length) tail: - FullViewOwner now also matches buf.AsSpan(0, buf.Length) / buf.AsMemory(0, buf.Length) and new Span<T>(buf, 0, buf.Length) (length arg is the receiver's own .Length); - a new ArrayClearOverOwner matches Array.Clear(buf, 0, buf.Length) (the over-clear). A real bound (buf.AsSpan(0, n), Array.Clear(buf, 0, n)) is not matched; the single-arg Array.Clear(buf) wipe is deliberately left alone. All emit the existing `overspan` fact -> OWN025. Corpus: arraypool-clear-past-length. MemoryPool — the OTHER pool was untracked entirely. MemoryPool<T>.Shared.Rent returns an IMemoryOwner<T> released by Dispose (no Return), so it is tracked as a regular Dispose-released local (IsMemoryPoolRent, mirroring IsPoolRent but NOT a poolBuffer: arg-passing is an escape, like any IDisposable). With that one acquire recognised, the existing flow covers POOL001 (never disposed -> OWN001), POOL002 (use after Dispose -> OWN002) and POOL003 (double Dispose -> OWN003). Corpus: memorypool-double-dispose (the MemoryPool twin of arraypool-double-return) -> OWN003. POOL003 for ArrayPool was already green (try/finally + aliased-receiver, both in the benchmark) — this adds its MemoryPool sibling. Benchmark recall floor 14 -> 16. Spec: BufferPolicies B9 (the .Length spelling), P-007 (status, scope, open Q#4 resolved). metamorphic.py already knows `overspan`; the new .own models reuse acquire/release. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent f206dbf commit b3bf5b1

14 files changed

Lines changed: 311 additions & 40 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,13 @@ jobs:
788788
# `buf.AsMemory()`) is a borrow lowered to a use of the OWNER (`ViewOwner`), so using it
789789
# after `Return(buf)` trips OWN002 — including RETURNING a `Memory<T>` view (which, unlike a
790790
# ref-struct `Span`, can ESCAPE the method), a dangling borrow handed to the caller. A
791-
# FULL-length view of a pooled buffer (`buf.AsSpan()` with no length bound) reaches past the
792-
# rented logical length into the oversized tail -> OWN025 (P-007 POOL005, the over-read; the
793-
# `arraypool-fullspan-overread` case). Remaining backlog: a FIELD-mediated cross-method
794-
# use-after-dispose (dispose in one method, use in another via shared state), a view stored in
791+
# view of a pooled buffer reaches past the rented length into the oversized tail -> OWN025
792+
# (P-007 POOL005, the over-read): the unbounded `buf.AsSpan()` AND the `.Length` spelling
793+
# (`buf.AsSpan(0, buf.Length)`, `Array.Clear(buf, 0, buf.Length)`) — the `arraypool-fullspan-
794+
# overread` / `arraypool-clear-past-length` cases. MemoryPool is now tracked too: a
795+
# `MemoryPool<T>.Rent` IMemoryOwner is released by Dispose, so its leak / double-dispose /
796+
# use-after-dispose ride the flow (POOL001/002/003 — the `memorypool-double-dispose` case ->
797+
# OWN003). Remaining backlog: a FIELD-mediated cross-method use-after-dispose, a view stored in
795798
# a FIELD, and an injected-source region-escape. A drop below the floor is a regression.
796-
run: python scripts/benchmark.py --min-recall 14
799+
run: python scripts/benchmark.py --min-recall 16
797800

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// AFTER (fixed). Every length/count argument is bound to the logical length `n`,
2+
// not the oversized `buf.Length`: `buf.AsSpan(0, n)` views only the valid payload
3+
// and `Array.Clear(buf, 0, n)` clears only it. The `[n, Length)` tail is never
4+
// touched, so the extractor emits no `overspan` fact and the checker is silent.
5+
using System;
6+
using System.Buffers;
7+
8+
static class PoolClearPastLength
9+
{
10+
static void Roundtrip(int n)
11+
{
12+
byte[] buf = ArrayPool<byte>.Shared.Rent(n);
13+
Fill(buf, n);
14+
Emit(buf.AsSpan(0, n)); // bounded by the rented length n
15+
Array.Clear(buf, 0, n); // clears only the logical [0, n)
16+
ArrayPool<byte>.Shared.Return(buf);
17+
}
18+
19+
static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; }
20+
static void Emit(ReadOnlySpan<byte> data) { }
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// BEFORE (buggy). POOL005, the `.Length` spelling of the over-read / over-clear.
2+
// A rented array is OVERSIZED: `ArrayPool<T>.Shared.Rent(n)` returns an array of
3+
// `Length >= n`. This code uses `buf.Length` — the oversized backing length — as
4+
// the operative length instead of the rented `n`: `buf.AsSpan(0, buf.Length)`
5+
// hands off the stale `[n, Length)` tail (a previous renter's bytes), and
6+
// `Array.Clear(buf, 0, buf.Length)` clears past `n` into that tail. The fix is to
7+
// bound by the logical length `n` (`buf.AsSpan(0, n)`, `Array.Clear(buf, 0, n)`,
8+
// see after.cs). Representative of the pattern, not verbatim from one PR.
9+
//
10+
// Wrapped in a class so the extractor's per-class flow pass visits it; helpers
11+
// stubbed so the reduction is self-contained.
12+
using System;
13+
using System.Buffers;
14+
15+
static class PoolClearPastLength
16+
{
17+
static void Roundtrip(int n)
18+
{
19+
byte[] buf = ArrayPool<byte>.Shared.Rent(n);
20+
Fill(buf, n); // valid payload is buf[0..n]
21+
Emit(buf.AsSpan(0, buf.Length)); // <-- BUG: length is buf.Length, not n -> stale tail
22+
Array.Clear(buf, 0, buf.Length); // <-- BUG: clears past n into the pooled tail
23+
ArrayPool<byte>.Shared.Return(buf);
24+
}
25+
26+
static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; }
27+
static void Emit(ReadOnlySpan<byte> data) { }
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// OwnLang model of the POOL005 `.Length` over-read / over-clear. `acquire` ==
2+
// ArrayPool.Rent (oversized: Length >= n), `release` == ArrayPool.Return.
3+
// `overspan buf` models using the buffer's OWN oversized `.Length` as the
4+
// operative length — `buf.AsSpan(0, buf.Length)` / `Array.Clear(buf, 0, buf.Length)`
5+
// — which reaches past the rented `n` into the stale tail. Bounding by `n` takes no
6+
// full view, so the fixed code has no `overspan` and is silent. -> OWN025. The
7+
// buffer is still returned, so there is no leak (OWN001).
8+
module Corpus
9+
resource Buffer {
10+
acquire rent
11+
release give
12+
kind "pooled buffer"
13+
}
14+
fn roundtrip(n: int) {
15+
let buf = acquire Buffer(n); // ArrayPool.Rent (Length >= n)
16+
overspan buf; // buf.AsSpan(0, buf.Length) / Array.Clear(buf, 0, buf.Length) -> OWN025
17+
release buf; // ArrayPool.Return
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN025
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ArrayPool over-read / over-clear — the `.Length` spelling (POOL005)
2+
3+
**Pattern:** a pooled array from `ArrayPool<T>.Shared.Rent(n)` is *oversized*
4+
(`Length >= n`). Code that uses `buf.Length` — the oversized backing length — as
5+
the operative length, rather than the rented `n`, reaches past the valid payload
6+
into the stale `[n, Length)` tail a previous renter left:
7+
8+
- `buf.AsSpan(0, buf.Length)` / `new Span<T>(buf, 0, buf.Length)` — an over-**read**
9+
(the consumer sees `n` valid bytes plus the stale tail: a wrong-length read and a
10+
potential information disclosure);
11+
- `Array.Clear(buf, 0, buf.Length)` — an over-**clear** (touches the pool's tail
12+
past the data that was actually written).
13+
14+
The fix is to bound by the logical length: `buf.AsSpan(0, n)`, `Array.Clear(buf, 0, n)`.
15+
16+
This is the `.Length` sibling of `arraypool-fullspan-overread` (which catches the
17+
unbounded `buf.AsSpan()`): same OWN025, a different way of writing the oversized
18+
length. The extractor recognises the `.Length` argument on the buffer's own view /
19+
`Array.Clear` (resolved BCL symbols) and emits an `overspan` fact.
20+
21+
**What the checker says:** **OWN025** `[resource: pooled buffer]` at each over-reach
22+
site. The buffer is still `Return`ed, so there is no OWN001 leak.
23+
24+
**Honesty / scope.** `case.own` is a faithful hand reduction (not C# ingested by the
25+
checker); `before.cs` / `after.cs` are representative of the bug and its fix. The
26+
single-arg `Array.Clear(buf)` (a deliberate whole-buffer wipe) is intentionally not
27+
flagged — only the explicit oversized `buf.Length` count is.
28+
29+
Reference: [P-007](../../../docs/proposals/P-007-arraypool-span.md); replay target
30+
AiDotNet.Tensors pooled-buffer over-clear/over-read.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// AFTER (fixed). A `using` declaration disposes the owner exactly once, at the end
2+
// of the scope, on every path — no second Dispose. The pooled memory is released
3+
// once and returned to the pool cleanly, so the checker is silent.
4+
using System;
5+
using System.Buffers;
6+
7+
static class MemoryPoolDoubleDispose
8+
{
9+
static void Run(int n)
10+
{
11+
using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(n);
12+
Work(owner.Memory);
13+
}
14+
15+
static void Work(Memory<byte> data) { }
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// BEFORE (buggy). POOL003 for the OTHER pool: a `MemoryPool<T>` hands back an
2+
// `IMemoryOwner<T>` released by `Dispose()` (there is no `Return`). Disposing the
3+
// same owner twice — here on the success path AND again in `finally` — is a double
4+
// release: the same memory can later be handed to two renters at once, exactly the
5+
// corruption dotnet/runtime#33767 describes for ArrayPool double-return. The fix is
6+
// to dispose exactly once (a `using` owner, see after.cs).
7+
//
8+
// Wrapped in a class so the extractor's per-class flow pass visits it; the helper
9+
// is stubbed so the reduction is self-contained.
10+
using System;
11+
using System.Buffers;
12+
13+
static class MemoryPoolDoubleDispose
14+
{
15+
static void Run(int n)
16+
{
17+
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(n);
18+
try
19+
{
20+
Work(owner.Memory);
21+
owner.Dispose(); // disposed here ...
22+
}
23+
finally
24+
{
25+
owner.Dispose(); // <-- ... and again here (double release)
26+
}
27+
}
28+
29+
static void Work(Memory<byte> data) { }
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// OwnLang model of the MemoryPool double-dispose. A MemoryPool<T> owner is
2+
// `acquire`d by Rent and `release`d by Dispose (there is no Return). Disposing it
3+
// twice is a double release -> OWN003 — the same verdict as an ArrayPool
4+
// double-return; the pool can then hand the same memory to two renters at once.
5+
module Corpus
6+
resource MemoryOwner {
7+
acquire Rent
8+
release Dispose
9+
}
10+
fn run(n: int) {
11+
let owner = acquire MemoryOwner(n); // MemoryPool.Rent
12+
release owner; // owner.Dispose()
13+
release owner; // owner.Dispose() again -> OWN003
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN003

0 commit comments

Comments
 (0)