Skip to content

Commit 377c182

Browse files
authored
Merge pull request #73 from PhysShell/claude/memorypool-view-borrow
feat(pool): MemoryPool view borrow — owner.Memory[.Span] used after Dispose → OWN002 (POOL002)
2 parents 6eeb89e + c000057 commit 377c182

8 files changed

Lines changed: 122 additions & 10 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,11 @@ jobs:
792792
# (P-007 POOL005, the over-read): the unbounded `buf.AsSpan()` AND the `.Length` view spelling
793793
# (`buf.AsSpan(0, buf.Length)`) — the `arraypool-fullspan-overread` / `arraypool-length-
794794
# overread` cases (a write/wipe like `Array.Clear(buf, 0, buf.Length)` is not flagged).
795-
# MemoryPool is now tracked too: a
796-
# `MemoryPool<T>.Rent` IMemoryOwner is released by Dispose, so its leak / double-dispose /
797-
# use-after-dispose ride the flow (POOL001/002/003 — the `memorypool-double-dispose` case ->
798-
# OWN003). Remaining backlog: a FIELD-mediated cross-method use-after-dispose, a view stored in
795+
# MemoryPool is tracked too: a `MemoryPool<T>.Rent` IMemoryOwner is released by Dispose, so its
796+
# leak / double-dispose ride the flow (POOL001/003 — `memorypool-double-dispose` -> OWN003), and
797+
# its `owner.Memory` / `owner.Memory.Span` view is a borrow lowered to a use of the OWNER
798+
# (`ViewOwner`), so reading it after Dispose trips OWN002 (POOL002 — `memorypool-view-after-
799+
# dispose`). Remaining backlog: a FIELD-mediated cross-method use-after-dispose, a view stored in
799800
# a FIELD, and an injected-source region-escape. A drop below the floor is a regression.
800-
run: python scripts/benchmark.py --min-recall 16
801+
run: python scripts/benchmark.py --min-recall 17
801802

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// AFTER (fixed). A `using` declaration owns the `IMemoryOwner` lifetime: the buffer is
2+
// returned to the pool exactly once, at the end of the scope, AFTER the view has been
3+
// read. The `owner.Memory.Span` borrow is consumed while the owner is still alive, so it
4+
// is never read past the buffer's return. The checker is silent.
5+
using System;
6+
using System.Buffers;
7+
8+
static class MemoryPoolViewAfterDispose
9+
{
10+
static void Run(int n)
11+
{
12+
using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(n);
13+
Consume(owner.Memory.Span); // read while the owner is still alive
14+
}
15+
16+
static void Consume(ReadOnlySpan<byte> data) { }
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// BEFORE (buggy). POOL002 for MemoryPool: an `IMemoryOwner<T>` from `MemoryPool<T>`
2+
// exposes its pooled buffer as a `Memory<T>` via `owner.Memory`. That Memory (and the
3+
// `Span` taken from it) is a BORROW of the owner — it is only valid while the owner is
4+
// alive. Reading the view AFTER `owner.Dispose()` (which returns the memory to the pool)
5+
// reads memory that may already have been handed to another renter: a use-after-free.
6+
// The fix is to read the view BEFORE disposing — or let a `using` own the lifetime
7+
// (see after.cs). The MemoryPool twin of `arraypool-span-view-after-return`.
8+
//
9+
// Wrapped in a class so the extractor's per-class flow pass visits it; the helper is
10+
// stubbed so the reduction is self-contained.
11+
using System;
12+
using System.Buffers;
13+
14+
static class MemoryPoolViewAfterDispose
15+
{
16+
static void Run(int n)
17+
{
18+
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(n);
19+
Memory<byte> view = owner.Memory; // a borrow of the owner's pooled buffer
20+
owner.Dispose(); // returns the memory to the pool ...
21+
Consume(view.Span); // <-- BUG: ... but the view is read AFTER (use-after-free)
22+
}
23+
24+
static void Consume(ReadOnlySpan<byte> data) { }
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// OwnLang model of the MemoryPool view-after-dispose. `acquire` == MemoryPool.Rent,
2+
// `release` == owner.Dispose(). A `Memory<T>` view (`owner.Memory`, and the `Span` from
3+
// it) is a BORROW of the owner — the extractor lowers a use of the view to a use of the
4+
// OWNER, so reading it after Dispose is the generic use-after-release (OWN002), the same
5+
// code reading the owner directly would give. The borrow is resolved to its owner in the
6+
// extractor; the core sees a plain use-after-release.
7+
module Corpus
8+
resource MemoryOwner {
9+
acquire Rent
10+
release Dispose
11+
}
12+
fn run(n: int) {
13+
let owner = acquire MemoryOwner(n); // MemoryPool.Rent
14+
release owner; // owner.Dispose() <-- too early
15+
use owner; // read the Memory view AFTER Dispose -> OWN002
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN002
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# MemoryPool view used after dispose (POOL002, the Dispose-released pool)
2+
3+
**Pattern:** `MemoryPool<T>.Shared.Rent(n)` returns an `IMemoryOwner<T>` whose pooled
4+
buffer is exposed as a `Memory<T>` via `owner.Memory` (and a `Span<T>` via
5+
`owner.Memory.Span`). That view is a **borrow** of the owner — valid only while the
6+
owner is alive. Reading it after `owner.Dispose()` (which returns the memory to the
7+
pool) reads memory that may already belong to another renter: a use-after-free. The
8+
fix is to read the view *before* disposing, or to let a `using` own the lifetime.
9+
10+
This is the MemoryPool twin of `arraypool-span-view-after-return`: there a `Span`
11+
view of a `Rent`ed array is used after `Return`; here a `Memory`/`Span` view of an
12+
`IMemoryOwner` is used after `Dispose`. Both lower the view to a use of the **owner**
13+
(`ViewOwner` in the extractor), so the core sees a plain use-after-release.
14+
15+
**What it adds:** the extractor now recognises `owner.Memory` / `owner.Memory.Span`
16+
(resolved via the `System.Buffers.IMemoryOwner<T>.Memory` property) as a borrow of the
17+
owner — completing the MemoryPool story begun in #72 (which tracked the owner's
18+
acquire / release lifecycle: POOL001 leak, POOL003 double-dispose). With the view
19+
recognised, a view-local read after an explicit `owner.Dispose()` is
20+
**POOL002 → OWN002**. (The *returned*-`Memory` dangle from the idiomatic `using`
21+
owner — `using owner; return owner.Memory;`, where the implicit scope-exit dispose
22+
hands a stale view to the caller — is a follow-up: `using` locals are skipped as
23+
non-leak candidates, so that escape needs the scope-exit dispose modelled as a
24+
release on the return path, like the ArrayPool try/finally `Memory` escape.)
25+
26+
**What the checker says:** the OwnLang model and the real `before.cs` both trip
27+
**OWN002** (use after release). The `using` fix in `after.cs` reads the view while the
28+
owner is alive and is silent.
29+
30+
**Honesty / scope.** `case.own` is a faithful hand reduction (not C# ingested by the
31+
checker); `before.cs` / `after.cs` are representative of the bug and its fix.
32+
33+
Reference: [P-007](../../../docs/proposals/P-007-arraypool-span.md).

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
read-capable VIEW is the over-read. **POOL003 (double-return → OWN003) is built** for ArrayPool
2020
(try/finally + aliased-receiver, corpus `arraypool-double-return` / `arraypool-aliased-receiver`),
2121
and **MemoryPool is now tracked** — a `MemoryPool<T>.Rent` `IMemoryOwner` is released by Dispose,
22-
so its leak / double-dispose / use-after-dispose ride the same flow as POOL001/002/003 (corpus
23-
`memorypool-double-dispose` → OWN003). A POOL005 view stored in a FIELD, and the
24-
`IMemoryOwner.Memory.Span` view borrow, are next
22+
so its leak / double-dispose ride the same flow as POOL001/003 (corpus `memorypool-double-dispose`
23+
→ OWN003), and its `owner.Memory` / `owner.Memory.Span` is a borrow lowered to a use of the OWNER
24+
(`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
2526
- **Depends on:** `spec/OwnCore.md` (OWN001 leak, OWN002 use-after-release,
2627
OWN003 double-release, OWN008 release-while-borrowed), the buffer/borrow model
2728
in `spec/`, [P-001](P-001-csharp-extractor.md). See

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,9 @@ e is InvocationExpressionSyntax i
900900
&& IsMemoryPoolType(sym.ContainingType);
901901

902902
// The owner buffer a Span/ReadOnlySpan/Memory/ReadOnlyMemory VIEW expression borrows from:
903-
// `owner.AsSpan(...)` / `owner.AsMemory(...)`, or `new Span<T>(owner, …)` / `new Memory<T>(owner)`
904-
// (and the ReadOnly* forms), where the source is a local identifier. Returns the owner local name,
903+
// `owner.AsSpan(...)` / `owner.AsMemory(...)`, `new Span<T>(owner, …)` / `new Memory<T>(owner)`
904+
// (and the ReadOnly* forms), or `owner.Memory` / `owner.Memory.Span` of a `System.Buffers.
905+
// IMemoryOwner<T>` (a MemoryPool rental), where the source is a local identifier. Returns the owner local name,
905906
// else null. The BORROW is recognised by the RESOLVED BCL symbols — `System.MemoryExtensions`
906907
// `AsSpan`/`AsMemory` (which alias the receiver array) and the `System.Span<T>` / `ReadOnlySpan<T>`
907908
// / `Memory<T>` / `ReadOnlyMemory<T>` constructor (which wraps the array argument) — NOT by name, so
@@ -926,6 +927,23 @@ e is InvocationExpressionSyntax i
926927
{ ContainingType: { Name: "Span" or "ReadOnlySpan" or "Memory" or "ReadOnlyMemory" } sct }
927928
&& IsInNamespace(sct, "System"))
928929
return arg.Identifier.Text;
930+
// owner.Memory — a Memory<T> view of a System.Buffers.IMemoryOwner<T> (e.g. a MemoryPool rental).
931+
// Like array.AsMemory(), the Memory CAN escape (return / field); a use of the view after the
932+
// owner's Dispose is a use of the owner after release (OWN002), and a returned Memory after
933+
// Dispose is a dangling borrow. Recognised by the resolved `IMemoryOwner<T>.Memory` property.
934+
if (e is MemberAccessExpressionSyntax mem
935+
&& mem.Name.Identifier.Text == "Memory"
936+
&& mem.Expression is IdentifierNameSyntax mo
937+
&& model.GetSymbolInfo(mem).Symbol is IPropertySymbol { ContainingType: { Name: "IMemoryOwner" } ict }
938+
&& IsInNamespace(ict, "System", "Buffers"))
939+
return mo.Identifier.Text;
940+
// owner.Memory.Span — the Span<T> of that Memory view (a ref-struct borrow that cannot escape).
941+
if (e is MemberAccessExpressionSyntax { Name.Identifier.Text: "Span" } spanAcc
942+
&& spanAcc.Expression is MemberAccessExpressionSyntax
943+
{ Name.Identifier.Text: "Memory", Expression: IdentifierNameSyntax mo2 } innerMem
944+
&& model.GetSymbolInfo(innerMem).Symbol is IPropertySymbol { ContainingType: { Name: "IMemoryOwner" } ict2 }
945+
&& IsInNamespace(ict2, "System", "Buffers"))
946+
return mo2.Identifier.Text;
929947
return null;
930948
}
931949

0 commit comments

Comments
 (0)