wire: derive arena chunk class without cursor state
What changed, and why it matters
This commit refactors a memory arena used when decoding Bitcoin transactions and blocks. It removes a chunk-size cursor and instead calculates the right chunk size on every allocation. It also adds a guard so negative allocation sizes are rejected instead of causing a panic. The change is primarily a robustness and maintainability improvement; it does not appear to fix an actively exploitable vulnerability, but it removes a class of potential panic/corruption bugs related to stale cursor state and negative sizes.
Reviewers should confirm that the new `grow()` loop cannot select a class smaller than `n` and that `max(n, len(a.cur)+1)` correctly prevents oscillation or fragmentation. Fuzz/property tests already cover round-trips and randomized allocations; consider adding a test for an arena reused across many borrow/release cycles to ensure no class-cursor regression. No urgent security deployment is indicated by the diff alone.
Security signals we found
Defensive check added: negative allocation sizes now return an error instead of panicking on slice bounds.
Stateful cursor removed, reducing risk of incorrect chunk-size escalation after rewind/release/reuse.
putScriptChunk comment updated to explain that a length mismatch would be internal corruption and could cause a future slice-bounds panic.
No direct diff evidence of an exploitable memory-safety bug being fixed; changes are framed as robustness/correctness.
Evidence from the diff
The patch changes scriptArena in btcd’s wire package so that grow() derives the next chunk size class from max(n, len(a.cur)+1) rather than from a stored class field. This eliminates txScriptChunkClass/blockScriptChunkClass constants and the startClass parameter to borrowScriptArena(). It also removes cursor repositioning in rewindSlow() and cursor reset in release(). Additionally, alloc() now rejects n < 0 with errScriptArenaFull rather than allowing a slice-bounds panic. Tests are updated to call borrowScriptArena() without arguments and a new test verifies negative-size rejection.
Changed components
wire/arena.gowire/msgtx.gowire/msgblock.gowire/arena_test.gowire/arena_rapid_test.gowire/bench_test.gowire/decode_budget_bench_test.goInspect captured patch +82 / −81
### wire/arena.go
@@ -47,17 +47,6 @@ const (
// passing the arena capacity check can be satisfied by one chunk;
// the compile-time assertion below enforces the coupling.
maxScriptChunkSize = 1 << 22
-
- // txScriptChunkClass is the chunk size class used as the starting
- // point when decoding standalone transactions. See
- // scriptChunkClasses.
- txScriptChunkClass = 0
-
- // blockScriptChunkClass is the chunk size class used as the starting
- // point when decoding full blocks. The arena is rewound between each
- // transaction, so blocks have the same initial requirement as standalone
- // transactions and grow only when an individual transaction requires it.
- blockScriptChunkClass = txScriptChunkClass
)
// scriptChunkClasses defines the ladder of chunk sizes that back script
@@ -150,9 +139,13 @@ var scriptChunkPools = func() [len(scriptChunkClasses)]*chunkClassPool {
return pools
}()
-// putScriptChunk returns a chunk to the pool for its size class. Chunks are
-// only ever created by the class pools, so their lengths always match a class
-// size exactly; anything else indicates internal corruption.
+// putScriptChunk returns a chunk to the pool for its size class. Chunks
+// are only ever created by the class pools, so their lengths always match a
+// class size exactly. A chunk whose size matches no class can only mean
+// internal corruption, and reusing it would hand some future allocation a
+// buffer too small for the capacity it was granted, so a slice bounds check
+// would panic deep inside a decode. Failing loudly here instead pinpoints
+// the corruption at the source.
func putScriptChunk(chunk *[]byte) {
for i, size := range scriptChunkClasses {
if len(*chunk) == size {
@@ -193,10 +186,6 @@ type scriptArena struct {
// decode limit previously enforced by the fixed slab size.
used int
- // class is the size class index the next chunk will be drawn from
- // (at minimum) when the current chunk is exhausted.
- class int
-
// dead marks an arena that has been released. A dead arena rejects all
// allocations and ignores rewinds until the arena is borrowed again.
// Arenas have a single lexical owner and references must not be retained
@@ -212,19 +201,15 @@ var scriptArenaPool = sync.Pool{
},
}
-// borrowScriptArena returns an arena that will serve its first allocation
-// from a chunk of the given starting size class. The startClass must be a
-// valid index into scriptChunkClasses; all callers pass one of the
-// *ScriptChunkClass constants. Chunks are borrowed lazily, so an arena
-// that never allocates never touches the chunk pools. The returned arena
-// must be handed back via release.
+// borrowScriptArena returns an arena ready to stage script data. Chunks
+// are borrowed lazily, so an arena that never allocates never touches the
+// chunk pools. The returned arena must be handed back via release.
//
// Forgetting to call release is safe in the memory sense: the chunks are
// ordinary garbage collected slices, so an abandoned arena is simply
// reclaimed by the GC and only the pooling benefit is lost.
-func borrowScriptArena(startClass int) *scriptArena {
+func borrowScriptArena() *scriptArena {
a := scriptArenaPool.Get().(*scriptArena)
- a.class = startClass
a.used = 0
a.dead = false
return a
@@ -243,9 +228,12 @@ func (a *scriptArena) remaining() int {
// path fills every allocation with io.ReadFull).
//
// alloc fails with errScriptArenaFull once the total bytes handed out since
-// the last rewind would exceed scriptArenaMaxAlloc.
+// the last rewind would exceed scriptArenaMaxAlloc, and for any negative
+// size. The decode path never passes a negative size, but rejecting one
+// here keeps alloc total on any input rather than panicking on the slice
+// bounds below, which the remaining-budget check alone does not catch.
func (a *scriptArena) alloc(n int) ([]byte, error) {
- if n > a.remaining() {
+ if n < 0 || n > a.remaining() {
return nil, errScriptArenaFull
}
if n > len(a.cur)-a.off {
@@ -275,24 +263,22 @@ func (a *scriptArena) allocSlow(n int) ([]byte, error) {
}
// grow borrows a new chunk large enough to hold n bytes and makes it the
-// current chunk. The new chunk comes from the next size class in the ladder
-// so repeated growth converges to the largest class in a constant number of
-// steps. Any request is satisfiable because callers bound n by the arena
-// capacity, which equals the largest class size.
+// current chunk. The chunk comes from the smallest size class that
+// holds both n and one more byte than the current chunk, so consecutive
+// growths walk up the ladder and converge to the largest class in a
+// constant number of steps without any per-arena class bookkeeping. Any
+// request is satisfiable because callers bound n by the arena capacity,
+// which equals the largest class size.
func (a *scriptArena) grow(n int) {
- class := a.class
- if class >= len(scriptChunkClasses) {
- class = len(scriptChunkClasses) - 1
- }
- for class < len(scriptChunkClasses)-1 && scriptChunkClasses[class] < n {
- class++
+ sizeClass := 0
+ for scriptChunkClasses[sizeClass] < max(n, len(a.cur)+1) {
+ sizeClass++
}
- chunk := scriptChunkPools[class].get()
+ chunk := scriptChunkPools[sizeClass].get()
a.chunks = append(a.chunks, chunk)
a.cur = *chunk
a.off = 0
- a.class = class + 1
}
// rewind resets the arena so previously allocated memory is reused from the
@@ -347,15 +333,8 @@ func (a *scriptArena) rewindSlow() {
a.chunks = append(a.chunks[:0], keep)
a.cur = *keep
-
- // The next grow should pick up where the retained chunk's class
- // leaves off.
- for i, size := range scriptChunkClasses {
- if size == len(*keep) {
- a.class = i + 1
- break
- }
- }
+ // Nothing else to do: grow derives the next class from the retained
+ // chunk's size, so there is no class cursor to reposition.
}
// release returns every chunk to its class pool and recycles the arena
@@ -377,7 +356,6 @@ func (a *scriptArena) release() {
a.chunks = a.chunks[:0]
a.cur = nil
a.off = 0
- a.class = 0
// Poison the capacity so any alloc through a stale reference fails
// with errScriptArenaFull rather than succeeding against recycled
### wire/arena_rapid_test.go
@@ -117,7 +117,7 @@ func TestScriptArenaPropertyTxRoundTrip(t *testing.T) {
var wireBuf bytes.Buffer
require.NoError(rt, tx.Serialize(&wireBuf))
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
buf := binarySerializer.Borrow()
@@ -181,11 +181,7 @@ func TestScriptArenaPropertyBlockRoundTrip(t *testing.T) {
// accounting and that live allocations never alias one another.
func TestScriptArenaPropertyAllocator(t *testing.T) {
rapid.Check(t, func(rt *rapid.T) {
- startClass := rapid.IntRange(
- 0, len(scriptChunkClasses)-1,
- ).Draw(rt, "startClass")
-
- ar := borrowScriptArena(startClass)
+ ar := borrowScriptArena()
defer ar.release()
type allocation struct {
@@ -245,8 +241,8 @@ func TestScriptArenaPropertyAllocator(t *testing.T) {
// TestReadTxOutOwnedScript ensures the script returned by the exported
// ReadTxOut has an exact-sized backing allocation owned by the output.
func TestReadTxOutOwnedScript(t *testing.T) {
- class := txScriptChunkClass
- size := scriptChunkClasses[class]
+ const sizeClass = 0
+ size := scriptChunkClasses[sizeClass]
testPool := &chunkClassPool{
fixed: make(chan *[]byte, 1),
pool: sync.Pool{
@@ -257,10 +253,10 @@ func TestReadTxOutOwnedScript(t *testing.T) {
},
}
- origPool := scriptChunkPools[class]
- scriptChunkPools[class] = testPool
+ origPool := scriptChunkPools[sizeClass]
+ scriptChunkPools[sizeClass] = testPool
t.Cleanup(func() {
- scriptChunkPools[class] = origPool
+ scriptChunkPools[sizeClass] = origPool
})
orig := blockOne.Transactions[0].TxOut[0]
@@ -286,7 +282,7 @@ func TestReadTxOutOwnedScript(t *testing.T) {
// ownership interval: double release is a no-op, and a released arena refuses
// to allocate even after a rewind.
func TestScriptArenaReleaseSafety(t *testing.T) {
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
_, err := ar.alloc(128)
require.NoError(t, err)
@@ -306,7 +302,7 @@ func TestScriptArenaReleaseSafety(t *testing.T) {
ar.release()
// A fresh borrow resets the poisoned state.
- fresh := borrowScriptArena(txScriptChunkClass)
+ fresh := borrowScriptArena()
defer fresh.release()
s, err := fresh.alloc(32)
require.NoError(t, err)
### wire/arena_test.go
@@ -18,7 +18,7 @@ import (
// capacity clamped to length, and no overlap between consecutive
// allocations.
func TestScriptArenaAlloc(t *testing.T) {
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
sizes := []int{0, 1, 25, 512, 1000, 33}
@@ -51,7 +51,7 @@ func TestScriptArenaAlloc(t *testing.T) {
// ladder as demand grows and that a single allocation larger than the next
// class skips ahead to a class that fits it.
func TestScriptArenaGrowth(t *testing.T) {
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
// The first allocation draws the starting class.
@@ -77,10 +77,27 @@ func TestScriptArenaGrowth(t *testing.T) {
)
}
+// TestScriptArenaAllocNegativeSize ensures a negative allocation size is
+// rejected with errScriptArenaFull. The decode path never passes one, but
+// a caller that did would otherwise panic on slice bounds since the
+// capacity check only compares against the remaining budget.
+func TestScriptArenaAllocNegativeSize(t *testing.T) {
+ ar := borrowScriptArena()
+ defer ar.release()
+
+ for _, size := range []int{-1, -58, -(8 << 20)} {
+ _, err := ar.alloc(size)
+ require.ErrorIs(t, err, errScriptArenaFull, "size %d", size)
+ }
+
+ // Rejected allocations must not consume any of the budget.
+ require.Equal(t, scriptArenaMaxAlloc, ar.remaining())
+}
+
// TestScriptArenaCapacity ensures the arena enforces the per-transaction
// staging limit that the fixed slab previously provided.
func TestScriptArenaCapacity(t *testing.T) {
- ar := borrowScriptArena(blockScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
// Fill the arena right up to its capacity.
@@ -107,7 +124,7 @@ func TestScriptArenaCapacity(t *testing.T) {
// TestScriptArenaRewind ensures rewinding recycles the memory of the largest
// chunk in place and returns the smaller warm-up chunks to their pools.
func TestScriptArenaRewind(t *testing.T) {
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
// Grow through two classes.
@@ -125,7 +142,10 @@ func TestScriptArenaRewind(t *testing.T) {
require.Len(t, ar.chunks, 1)
s, err := ar.alloc(8)
require.NoError(t, err)
- require.Equal(t, &largest[0], &s[0])
+
+ // The allocation must be served from the retained chunk itself, not
+ // from a copy that merely holds equal bytes.
+ require.Same(t, &largest[0], &s[0])
}
// TestBlockArenaTransactionReuse ensures block staging depends on the largest
@@ -182,9 +202,11 @@ func TestBlockArenaTransactionReuse(t *testing.T) {
require.NoError(t, decoded.Serialize(&roundTrip))
require.Equal(t, encoded.Bytes(), roundTrip.Bytes())
+ // The whole block must be staged by a single smallest-class
+ // chunk: one borrow serves every transaction.
for i, count := range allocated {
want := 0
- if i == txScriptChunkClass {
+ if i == 0 {
want = 1
}
require.Equal(t, want, count, "class %d", i)
@@ -284,8 +306,7 @@ func TestScriptArenaRandomizedAllocations(t *testing.T) {
rng := rand.New(rand.NewSource(42))
for round := 0; round < 50; round++ {
- startClass := rng.Intn(len(scriptChunkClasses))
- ar := borrowScriptArena(startClass)
+ ar := borrowScriptArena()
for rewinds := 0; rewinds < 4; rewinds++ {
var live [][]byte
### wire/bench_test.go
@@ -445,7 +445,7 @@ func BenchmarkReadTxOut(b *testing.B) {
func BenchmarkReadTxOutBuf(b *testing.B) {
b.ReportAllocs()
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
buffer := binarySerializer.Borrow()
buf := []byte{
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
@@ -502,7 +502,7 @@ func BenchmarkWriteTxOutBuf(b *testing.B) {
func BenchmarkReadTxIn(b *testing.B) {
b.ReportAllocs()
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
buffer := binarySerializer.Borrow()
buf := []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
### wire/decode_budget_bench_test.go
@@ -148,13 +148,17 @@ func BenchmarkTruncatedDecodeBudget(b *testing.B) {
})
b.Run("transaction_inputs", func(b *testing.B) {
+ // The 4-byte transaction version is read by MsgTx.BtcDecode before
+ // the input count varint.
payload := benchmarkCountPayload(
b, make([]byte, 4), maxTxInPerMessage,
)
- // Prewarm the script arena so the comparison isolates the count-sized
- // transaction input allocation changed by this patch.
- ar := borrowScriptArena(txScriptChunkClass)
+ // Prewarm the script arena so the borrow and release pair populates
+ // the chunk pool, and the benchmark measures only the count-sized
+ // transaction input allocation made while decoding rather than the
+ // one-time chunk creation.
+ ar := borrowScriptArena()
ar.release()
b.ReportAllocs()
@@ -171,11 +175,13 @@ func BenchmarkTruncatedDecodeBudget(b *testing.B) {
})
b.Run("streaming_transaction_inputs", func(b *testing.B) {
+ // The 4-byte transaction version is read by MsgTx.BtcDecode before
+ // the input count varint.
payload := benchmarkCountPayload(
b, make([]byte, 4), maxTxInPerMessage,
)
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
ar.release()
b.ReportAllocs()
### wire/msgblock.go
@@ -107,7 +107,7 @@ func (msg *MsgBlock) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) er
// transaction's btcDecode rewinds it, which is safe because the
// scripts are copied into their final exactly-sized buffer before the
// next transaction is decoded.
- ar := borrowScriptArena(blockScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
msg.Transactions = make([]*MsgTx, 0, txCount)
@@ -188,7 +188,7 @@ func (msg *MsgBlock) DeserializeTxLoc(r *bytes.Buffer) ([]TxLoc, error) {
return nil, err
}
- ar := borrowScriptArena(blockScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
// Deserialize each transaction while keeping track of its location
### wire/msgtx.go
@@ -418,7 +418,7 @@ func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error
buf := binarySerializer.Borrow()
defer binarySerializer.Return(buf)
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
err := msg.btcDecode(r, pver, enc, buf, ar)
@@ -1127,7 +1127,7 @@ func ReadTxOut(r io.Reader, pver uint32, version int32, to *TxOut) error {
buf := binarySerializer.Borrow()
defer binarySerializer.Return(buf)
- ar := borrowScriptArena(txScriptChunkClass)
+ ar := borrowScriptArena()
defer ar.release()
err := readTxOutBuf(r, pver, version, to, buf, ar)Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.