-
Notifications
You must be signed in to change notification settings - Fork 0
feat(pool): POOL005 — full-length view of a pooled buffer over-reads its logical length → OWN025 #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(pool): POOL005 — full-length view of a pooled buffer over-reads its logical length → OWN025 #71
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // AFTER (fixed). Every view is BOUNDED to the logical length — `buf.AsSpan(0, n)` — | ||
| // so only the `n` valid payload bytes are read; the oversized `[n, Length)` tail is | ||
| // never touched. No unbounded full view remains in either spelling (expression or | ||
| // initializer), so the extractor emits no `overspan` fact and the checker is silent. | ||
| using System; | ||
| using System.Buffers; | ||
|
|
||
| static class PoolFullSpanOverread | ||
| { | ||
| static byte[] Frame(int n) | ||
| { | ||
| byte[] buf = ArrayPool<byte>.Shared.Rent(n); | ||
| Fill(buf, n); | ||
| Emit(buf.AsSpan(0, n)); // bounded view: only the logical [0, n) | ||
| byte[] copy = buf.AsSpan(0, n).ToArray(); // bounded copy: only the logical [0, n) | ||
| ArrayPool<byte>.Shared.Return(buf); | ||
| return copy; | ||
| } | ||
|
|
||
| static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; } | ||
| static void Emit(ReadOnlySpan<byte> data) { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // BEFORE (buggy). Reduction of the ArrayPool "full-length view over-read" bug | ||
| // (P-007 POOL005). A rented array is OVERSIZED: `ArrayPool<T>.Shared.Rent(n)` | ||
| // returns an array of `Length >= n`, not exactly `n`. Here the code fills the | ||
| // first `n` bytes, then takes a FULL-length view — `buf.AsSpan()` with no length — | ||
| // so the `n` valid bytes are read together with the stale `[n, Length)` tail a | ||
| // *previous* renter left behind: a wrong-length read and an information disclosure. | ||
| // Both spellings are caught: the view used in an EXPRESSION (`Emit(buf.AsSpan())`) | ||
| // and the view in a local-declaration INITIALIZER (`var copy = buf.AsSpan()...`). | ||
| // The fix is a bounded view, `buf.AsSpan(0, n)` (see after.cs). Representative of | ||
| // the pattern (pooled-buffer over-read/over-copy in tensor and serialization code), | ||
| // not verbatim from one PR. | ||
| // | ||
| // Wrapped in a class so the extractor's per-class flow pass visits it; helpers | ||
| // stubbed so the reduction is self-contained. | ||
| using System; | ||
| using System.Buffers; | ||
|
|
||
| static class PoolFullSpanOverread | ||
| { | ||
| static byte[] Frame(int n) | ||
| { | ||
| byte[] buf = ArrayPool<byte>.Shared.Rent(n); | ||
| Fill(buf, n); // valid payload is buf[0..n] | ||
| Emit(buf.AsSpan()); // <-- BUG (expression): the WHOLE oversized array, | ||
| // n payload bytes + the stale [n, Length) tail | ||
| byte[] copy = buf.AsSpan().ToArray(); // <-- BUG (initializer): the same over-read in a local | ||
| ArrayPool<byte>.Shared.Return(buf); | ||
| return copy; | ||
| } | ||
|
|
||
| static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; } | ||
| static void Emit(ReadOnlySpan<byte> data) { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // OwnLang model of the ArrayPool full-length view over-read. `acquire` == | ||
| // ArrayPool.Rent (the array is OVERSIZED: Length >= n), `release` == | ||
| // ArrayPool.Return. `overspan buf` models a FULL-length view `buf.AsSpan()` (no | ||
| // length bound) handed to a consumer — it reaches past the logical length n into | ||
| // the stale [n, Length) tail. The bounded form `buf.AsSpan(0, n)` takes no full | ||
| // view, so the fixed code has no `overspan` and is silent. The checker trips | ||
| // OWN025 (POOL005) at the view; the buffer is still correctly returned, so there | ||
| // is no leak (OWN001) and no use-after-return (OWN002). | ||
| module Corpus | ||
| resource Buffer { | ||
| acquire rent | ||
| release give | ||
| kind "pooled buffer" | ||
| } | ||
| fn frame(n: int) { | ||
| let buf = acquire Buffer(n); // ArrayPool.Rent (Length >= n) | ||
| overspan buf; // buf.AsSpan() — full-length view past n -> OWN025 | ||
| release buf; // ArrayPool.Return | ||
| } |
1 change: 1 addition & 0 deletions
1
corpus/real-world/arraypool-fullspan-overread/expected-diagnostics.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| OWN025 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # ArrayPool full-length view over-read (POOL005) | ||
|
|
||
| **Pattern:** a pooled array from `ArrayPool<T>.Shared.Rent(n)` is *oversized* — the | ||
| pool returns an array of `Length >= n`, not exactly `n`. Code that takes a | ||
| FULL-length view of it (`buf.AsSpan()` / `buf.AsMemory()` / `new Span<T>(buf)`, all | ||
| with **no length bound**) and reads, copies, or writes through that view processes | ||
| the `n` valid bytes **plus** the stale `[n, Length)` tail — bytes a *previous* | ||
| renter left behind. That is a correctness bug (wrong length) and an information | ||
| disclosure (a prior renter's data leaks out). The fix is a bounded view: | ||
| `buf.AsSpan(0, n)`. | ||
|
|
||
| This is P-007's **POOL005** ("clear/copy past the logical length") — the over-read / | ||
| over-copy vehicle is the unbounded view. It is distinct from **OWN024** (a | ||
| *sensitive* buffer not cleared on release): POOL005 is reading/copying *too much*; | ||
| OWN024 is clearing *too little*. | ||
|
|
||
| **What the checker says:** the OwnLang model trips **OWN025** | ||
| `[resource: pooled buffer]` at the view. The extractor recognises an unbounded | ||
| `AsSpan()`/`AsMemory()`/`new Span<T>(buf)` over a `Rent`ed local (by the resolved | ||
| `System.MemoryExtensions` / `System.Span<T>` BCL symbols, so a look-alike is not | ||
| mistaken for it) and emits an `overspan` fact; the core — and the hand `case.own` | ||
| reduction — raise OWN025. The buffer is still `Return`ed, so there is no OWN001 | ||
| leak and no OWN002 use-after-return; the only finding is the over-read itself. | ||
|
|
||
| **Honesty / scope.** `case.own` is a faithful hand reduction (not C# ingested by the | ||
| checker); `before.cs` / `after.cs` are representative of the bug and its fix, not a | ||
| verbatim PR diff. The extractor catches the unbounded view both as an *expression* | ||
| (`Emit(buf.AsSpan())`, `buf.AsSpan().CopyTo(...)`) and in a local-declaration | ||
| *initializer* — the over-copy `var copy = buf.AsSpan().ToArray();` and the | ||
| view-local `Span<byte> s = buf.AsSpan();` alike, since the full view lives in the | ||
| initializer either way. The remaining spelling is `Array.Clear(buf, 0, buf.Length)` | ||
| (a length argument of `buf.Length` rather than an unbounded view), a follow-up. | ||
|
|
||
| Reference: [P-007](../../../docs/proposals/P-007-arraypool-span.md); replay target | ||
| AiDotNet.Tensors pooled-buffer over-clear/over-read. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // POOL005 — a full-length Span view of a pooled buffer reaches past its logical | ||
| // length. `ArrayPool.Rent(n)` hands back an OVERSIZED array (Length >= n); a view | ||
| // with no length bound (`buf.AsSpan()`) spans the whole array, so reading or | ||
| // copying through it exposes the stale [n, Length) tail a previous renter left. | ||
| // Real C#: `Emit(buf.AsSpan())` where `Emit(buf.AsSpan(0, n))` was meant. | ||
| module Gallery | ||
| resource Buffer { | ||
| acquire rent | ||
| release give | ||
| kind "pooled buffer" | ||
| } | ||
| fn frame(n: int) { | ||
| let buf = acquire Buffer(n); | ||
| overspan buf; // full-length view -> OWN025 | ||
| release buf; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the over-copy is written as a local declaration initializer, e.g.
var copy = buf.AsSpan().ToArray();,LowerFlowStmttakes theLocalDeclarationStatementSyntaxbranch and never callsEmitFlowExpron non-acquire initializers, so this new descendant scan is never reached. That leaves a common POOL005 over-read silent even though the implementation comment calls out chains such as.ToArray(); please also scan declaration initializers before returning from that branch.Useful? React with 👍 / 👎.