|
| 1 | +# Pooled-buffer `Memory<T>` view that ESCAPES after return — the borrow checker, second bite |
| 2 | + |
| 3 | +**Pattern:** a rented `ArrayPool<T>` buffer is sliced into a `Memory<T>` local |
| 4 | +(`Memory<byte> view = buf.AsMemory(0, n)`) and **returned from inside a `try` whose `finally` |
| 5 | +returns the buffer to the pool** — *the* idiomatic ArrayPool cleanup. The `return view` is evaluated |
| 6 | +before the finally, but the caller receives the `Memory<byte>` only **after** the finally has |
| 7 | +recycled the array, so the borrow **escapes** its owner: the caller holds a view into an array the |
| 8 | +pool has already handed to someone else — a silent cross-tenant corruption (read/write of |
| 9 | +freed-and-reused memory). |
| 10 | + |
| 11 | +**What the checker says:** the view's escape (the `return`) is a use of the owner after it was |
| 12 | +released → the generic **OWN002** (use-after-release), surfaced at the escape site. |
| 13 | + |
| 14 | +**Why this case exists (P-007 POOL004 — view escape).** [`arraypool-span-view-after-return`](../arraypool-span-view-after-return) |
| 15 | +caught a `Span` view used after `Return` **inside** the method. But a `Span` is a **ref struct** — |
| 16 | +the C# compiler keeps it inside the method, so it cannot escape. A **`Memory<T>` is not a ref |
| 17 | +struct**: it *can* be returned or stored in a field, which is the genuinely dangerous escape the |
| 18 | +borrow checker must catch. The view recognition (`ViewOwner`) is now extended from `AsSpan` / |
| 19 | +`new Span<T>` to **`AsMemory` / `new Memory<T>`** (and the `ReadOnly*` forms), resolved via the same |
| 20 | +BCL symbols (`System.MemoryExtensions`, `System.Memory<T>`). Because a reference to the view lowers |
| 21 | +to a use of the owner — and `return view` is such a reference — the escape of a dangling view after |
| 22 | +`Return(buf)` trips OWN002. Before this slice the `Memory` view was unrecognised, so the dangling |
| 23 | +return looked balanced (acquire + release) — a **miss**. |
| 24 | + |
| 25 | +Crucially, the escaped view's use is modelled **after the `finally`**: a returned borrow is used by |
| 26 | +the caller *after* the method's cleanup, so the owner-use is re-emitted just before the method exits |
| 27 | +the `onReturn` (finally) chain. That is what catches the idiomatic |
| 28 | +`try { return view; } finally { Return(buf); }` — where the `return` is evaluated first but the |
| 29 | +caller only receives the view once the finally has recycled the buffer (a dangling escape the C# |
| 30 | +compiler does **not** prevent for `Memory<T>`). |
| 31 | + |
| 32 | +**Conservative (0 FP).** Only a view of a **tracked** owner that is used/returned **after** the |
| 33 | +owner's release fires. The fix (`after.cs`) copies out of the buffer (`AsSpan(..).ToArray()`) and |
| 34 | +returns the **copy**, so no view escapes — silent. A view used before the return, or a view of an |
| 35 | +untracked buffer, adds no finding; the borrow can never invent a release. |
| 36 | + |
| 37 | +**Honesty / scope.** `case.own` is a faithful hand reduction (the escaping view collapses to a use |
| 38 | +of the owner, exactly what the extractor emits), not C# the `.own` checker ingested. |
| 39 | +`before.cs`/`after.cs` are representative of the bug and its fix. This slice covers the **return** |
| 40 | +escape; storing the view in a **field** (object-level escape) and view **reassignment** are left for |
| 41 | +later rounds. |
0 commit comments