Skip to content

Commit 9685dbe

Browse files
committed
sstable: mangle some more buffers in invariants builds
Mangle some more byte buffers when they become unused to help shake out memory safety bugs.
1 parent ab5eeb9 commit 9685dbe

File tree

8 files changed

+31
-9
lines changed

8 files changed

+31
-9
lines changed

internal/invariants/off.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ func (*Value[V]) Get() V {
5050
// Set the value; no-op in non-invariant builds.
5151
func (*Value[V]) Set(v V) {}
5252

53+
// MaybeMangle mangles a byte slice sometimes in invariant builds.
54+
func MaybeMangle(b []byte) {}
55+
56+
// Mangle mangles a byte slice in invariant builds.
57+
func Mangle(b []byte) {}
58+
5359
// BufMangler is a utility that can be used to test that the caller doesn't use
5460
type BufMangler struct{}
5561

internal/invariants/on.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ func (v *Value[V]) Get() V {
7272
return v.v
7373
}
7474

75+
// MaybeMangle mangles a byte slice sometimes in invariant builds.
76+
func MaybeMangle(b []byte) {
77+
if Sometimes(10) {
78+
Mangle(b)
79+
}
80+
}
81+
82+
// Mangle mangles a byte slice in invariant builds.
83+
func Mangle(b []byte) {
84+
for i := range b {
85+
b[i] = 0xCC
86+
}
87+
}
88+
7589
// BufMangler is a utility that can be used to test that the caller doesn't use
7690
type BufMangler struct {
7791
lastReturnedBuf []byte

sstable/block/buffer_pool.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/cockroachdb/errors"
99
"github.com/cockroachdb/pebble/internal/base"
1010
"github.com/cockroachdb/pebble/internal/cache"
11+
"github.com/cockroachdb/pebble/internal/invariants"
1112
)
1213

1314
// Alloc allocates a new Value for a block of length n (excluding the block
@@ -235,6 +236,7 @@ func (b *Buf) Release() {
235236
if b.p == nil {
236237
return
237238
}
239+
invariants.MaybeMangle(b.p.pool[b.i].b)
238240
// Clear the allocedBuffer's byte slice. This signals the allocated buffer
239241
// is no longer in use and a future call to BufferPool.Alloc may reuse this
240242
// buffer.

sstable/block/compression.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,7 @@ func (b *PhysicalBlock) WriteTo(w objstorage.Writable) (n int, err error) {
305305
// WriteTo is allowed to mangle the data. Mangle it ourselves some of the time
306306
// in invariant builds to catch callers that don't handle this.
307307
if invariants.Enabled && invariants.Sometimes(1) {
308-
for i := range b.data {
309-
b.data[i] = 0xFF
310-
}
308+
invariants.Mangle(b.data)
311309
}
312310
return len(b.data) + len(b.trailer), nil
313311
}
@@ -429,12 +427,7 @@ func (tb *TempBuffer) Release() {
429427
// maximum to the pool. This avoids holding on to occasional large buffers
430428
// necessary for e.g. singular large values.
431429
if tb.b != nil && len(tb.b) < tempBufferMaxReusedSize {
432-
if invariants.Sometimes(20) {
433-
// Mangle the buffer data.
434-
for i := range tb.b {
435-
tb.b[i] = 0xCC
436-
}
437-
}
430+
invariants.MaybeMangle(tb.b)
438431
tb.b = tb.b[:0]
439432
tempBufferPool.Put(tb)
440433
}

sstable/colblk/block.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ import (
141141
"github.com/cockroachdb/crlib/crbytes"
142142
"github.com/cockroachdb/errors"
143143
"github.com/cockroachdb/pebble/internal/binfmt"
144+
"github.com/cockroachdb/pebble/internal/invariants"
144145
"github.com/cockroachdb/pebble/internal/treeprinter"
145146
)
146147

@@ -209,6 +210,7 @@ type BlockEncoder struct {
209210

210211
// Reset resets an encoder for reuse.
211212
func (e *BlockEncoder) Reset() {
213+
invariants.MaybeMangle(e.buf)
212214
if cap(e.buf) > maxBlockRetainedSize {
213215
e.buf = nil
214216
}

sstable/colblk/prefix_bytes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ func (b *PrefixBytesBuilder) DataType(int) DataType { return DataTypePrefixBytes
721721
// size.
722722
func (b *PrefixBytesBuilder) Reset() {
723723
const maxRetainedData = 512 << 10 // 512 KB
724+
invariants.MaybeMangle(b.data)
724725
*b = PrefixBytesBuilder{
725726
bundleCalc: b.bundleCalc,
726727
data: b.data[:0],

sstable/colblk/raw_bytes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func (b *RawBytesBuilder) Init() {
153153
// Reset resets the builder to an empty state.
154154
func (b *RawBytesBuilder) Reset() {
155155
b.rows = 0
156+
invariants.MaybeMangle(b.data)
156157
b.data = b.data[:0]
157158
b.offsets.Reset()
158159
// Add an initial offset of zero to streamline the logic in RawBytes.At() to

sstable/runlength_bitmap.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package sstable
77
import (
88
"encoding/binary"
99
"iter"
10+
11+
"github.com/cockroachdb/pebble/internal/invariants"
1012
)
1113

1214
// BitmapRunLengthEncoder encodes a bitmap. It uses a run-length encoding for
@@ -42,6 +44,7 @@ type BitmapRunLengthEncoder struct {
4244

4345
// Init initializes or resets the encoder to its initial state.
4446
func (bb *BitmapRunLengthEncoder) Init() {
47+
invariants.MaybeMangle(bb.buf)
4548
bb.buf = bb.buf[:0]
4649
bb.allSetRunLength = 0
4750
bb.currByte = 0

0 commit comments

Comments
 (0)