Skip to content

Commit ac7f7d7

Browse files
authored
Merge pull request #131 from PhysShell/claude/corpus-nethermind-arraypool-leak
corpus: real-world ArrayPool leak from Nethermind PatriciaTree (P-007/P-012 replay)
2 parents 0c71d8e + 03c0004 commit ac7f7d7

5 files changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// AFTER (fixed) — NethermindEth/nethermind#9322. The rented buffer is returned in
2+
// a `finally` block, so it goes back to the pool even when GetNew throws. No leak.
3+
using System;
4+
using System.Buffers;
5+
6+
namespace Corpus
7+
{
8+
internal sealed class PatriciaTree
9+
{
10+
public byte[] Get(ReadOnlySpan<byte> rawKey)
11+
{
12+
int nibblesCount = 2 * rawKey.Length;
13+
byte[] array = ArrayPool<byte>.Shared.Rent(nibblesCount);
14+
try
15+
{
16+
return GetNew(array); // may throw — buffer still returned below
17+
}
18+
finally
19+
{
20+
ArrayPool<byte>.Shared.Return(array); // returned on every path, throw included
21+
}
22+
}
23+
24+
private static byte[] GetNew(byte[] nibbles) => throw new TrieException();
25+
}
26+
27+
internal sealed class TrieException : Exception { }
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// BEFORE (buggy). Reduction of NethermindEth/nethermind#9322 — an ArrayPool
2+
// buffer leak in Nethermind.Trie/PatriciaTree.cs `Get` / `GetNodeByKey` (fixed in
3+
// Nethermind v1.35.0). For keys longer than the 64-byte stackalloc minimum the
4+
// methods Rent a byte[] from ArrayPool<byte>.Shared. The `Return` sat INSIDE the
5+
// try, after the may-throw `GetNew`, so a thrown TrieException skipped it and
6+
// leaked the rented buffer — gradual memory pressure under sustained RPC load with
7+
// long keys. The fix moves the `Return` into a `finally` block (see after.cs).
8+
//
9+
// The Return is inside the `try` (after the may-throw call) on purpose: that is the
10+
// real structure the PR fixed, and it is what the extractor's default throw-edge
11+
// model (a throw exit before each may-throw leaf in a try, `--flow-locals`) sees.
12+
// Reduced + helpers stubbed so the file is self-contained.
13+
using System;
14+
using System.Buffers;
15+
16+
namespace Corpus
17+
{
18+
internal sealed class PatriciaTree
19+
{
20+
public byte[] Get(ReadOnlySpan<byte> rawKey)
21+
{
22+
int nibblesCount = 2 * rawKey.Length;
23+
byte[] array = ArrayPool<byte>.Shared.Rent(nibblesCount); // Rent (long key)
24+
try
25+
{
26+
byte[] result = GetNew(array); // can throw TrieException
27+
ArrayPool<byte>.Shared.Return(array); // in the try -> skipped on throw -> LEAK
28+
return result;
29+
}
30+
catch (TrieException)
31+
{
32+
throw; // buffer NOT returned on this path
33+
}
34+
}
35+
36+
private static byte[] GetNew(byte[] nibbles) => throw new TrieException();
37+
}
38+
39+
internal sealed class TrieException : Exception { }
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// OwnLang model of NethermindEth/nethermind#9322 (fixed in Nethermind v1.35.0):
2+
// Nethermind.Trie/PatriciaTree.cs `Get` / `GetNodeByKey` Rent a byte[] from
3+
// ArrayPool for keys longer than the 64-byte stackalloc minimum, then call
4+
// `GetNew` (which can throw TrieException) and Return the buffer only on the
5+
// success path. When GetNew throws, the rented buffer leaks. The fix moves the
6+
// Return into a `finally` block.
7+
//
8+
// `acquire` == ArrayPool<byte>.Shared.Rent, `release` == .Return. The C# `throw`
9+
// is modeled as an early `return` arm that exits before the `release` — the path
10+
// that skips cleanup — so the core flags the un-released owned buffer as OWN001.
11+
module Corpus
12+
resource Buffer {
13+
acquire rent
14+
release give
15+
emit_type "byte[]"
16+
emit_acquire "ArrayPool<byte>.Shared.Rent({args})"
17+
emit_release "ArrayPool<byte>.Shared.Return({0})"
18+
}
19+
fn Get(keyLength: int, throws: int) {
20+
let array = acquire Buffer(keyLength); // ArrayPool.Rent (key > 64-byte stackalloc)
21+
if (throws) {
22+
return; // GetNew threw TrieException -> exits before Return
23+
}
24+
release array; // ArrayPool.Return -- only on the success path
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN001
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Nethermind PatriciaTree ArrayPool leak (leak on the exception path)
2+
3+
**Source.** [NethermindEth/nethermind#9322](https://github.com/NethermindEth/nethermind/pull/9322),
4+
fixed in Nethermind **v1.35.0**. File `src/Nethermind/Nethermind.Trie/PatriciaTree.cs`,
5+
methods `Get` / `GetNodeByKey`. (A replay target named in
6+
[P-007](../../../docs/proposals/P-007-arraypool-span.md): *Nethermind ArrayPool leaks*.)
7+
8+
**Pattern.** For keys longer than the 64-byte `stackalloc` minimum the methods
9+
`Rent` a `byte[]` from `ArrayPool<byte>.Shared`, then call `GetNew`, which can
10+
throw `TrieException`. The buffer was `Return`ed only on the success path (after
11+
the call), so a thrown `TrieException` skipped the `Return` and **leaked** the
12+
rented buffer — gradual memory pressure under sustained RPC load with long keys.
13+
The fix moves the `Return` into a `finally` block (`after.cs`).
14+
15+
**What the checker says.** The OwnLang reduction trips **OWN001** (owned resource
16+
not released on all paths): `acquire` is `ArrayPool.Rent`, `release` is `.Return`,
17+
and the `throw` path is modeled as an early `return` arm that exits before the
18+
`release`.
19+
20+
```text
21+
$ python -m ownlang check corpus/real-world/nethermind-patriciatree-arraypool-leak/case.own
22+
case.own:22: error: [OWN001] 'array' is owned but not released before return (leaks on at least one path)
23+
```
24+
25+
**Honesty / scope.** `case.own` is a *hand reduction* of the C# pattern, not direct
26+
extractor output. The uncaught C# `throw` on `GetNew`'s `TrieException` is modeled
27+
as an early `return` arm — a path that exits before cleanup, which is exactly what
28+
an uncaught throw is.
29+
30+
End to end, `before.cs` keeps the `Return` **inside the `try`** after the may-throw
31+
`GetNew` (the real structure the PR fixed): that is what the default throw-edge
32+
model sees — `scripts/benchmark.py` runs own-check with `--flow-locals`, which
33+
injects a throw exit before each may-throw leaf **in a `try`** (a body-level
34+
may-throw call *outside* a `try` would need `--body-throw-edges`, which the
35+
benchmark does not pass). `after.cs` returns the buffer in a `finally`, so it is
36+
returned on every path and the fix is silent. Both are scored by the
37+
`corpus-benchmark` CI job (SDK-backed; not runnable in this Python-only checkout —
38+
recall there is a tracked floor, specificity is absolute). `before.cs` / `after.cs`
39+
are reduced (helpers stubbed), not a verbatim copy of the PR diff.

0 commit comments

Comments
 (0)