From 2450eabed85558ff8dc08fe6421eef7ca1715d76 Mon Sep 17 00:00:00 2001 From: Jackson Owens Date: Tue, 26 Apr 2022 14:36:03 -0400 Subject: [PATCH] internal/metamorphic: overwrite unused bounds buffers After passing bounds to Pebble, overwrite the provided buffers with garbage. This helps ensure we never accidentally retain user-provided bounds buffers after a NewIter/SetOptions/SetBounds call. --- internal/metamorphic/ops.go | 70 ++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/internal/metamorphic/ops.go b/internal/metamorphic/ops.go index 1eda5721e3..4bf26cf443 100644 --- a/internal/metamorphic/ops.go +++ b/internal/metamorphic/ops.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "io" + "math/rand" "path/filepath" "strings" @@ -575,16 +576,24 @@ type newIterOp struct { func (o *newIterOp) run(t *test, h *history) { r := t.getReader(o.readerID) + var lower, upper []byte + if o.lower != nil { + lower = append(lower, o.lower...) + } + if o.upper != nil { + upper = append(upper, o.upper...) + } + opts := &pebble.IterOptions{ + LowerBound: lower, + UpperBound: upper, + KeyTypes: pebble.IterKeyType(o.keyTypes), + RangeKeyMasking: pebble.RangeKeyMasking{ + Suffix: o.rangeKeyMaskSuffix, + }, + } var i *pebble.Iterator for { - i = r.NewIter(&pebble.IterOptions{ - LowerBound: o.lower, - UpperBound: o.upper, - KeyTypes: pebble.IterKeyType(o.keyTypes), - RangeKeyMasking: pebble.RangeKeyMasking{ - Suffix: o.rangeKeyMaskSuffix, - }, - }) + i = r.NewIter(opts) if err := i.Error(); !errors.Is(err, errorfs.ErrInjected) { break } @@ -592,6 +601,12 @@ func (o *newIterOp) run(t *test, h *history) { _ = i.Close() } t.setIter(o.iterID, i) + + // Trash the bounds to ensure that Pebble doesn't rely on the stability of + // the user-provided bounds. + rand.Read(lower[:]) + rand.Read(upper[:]) + h.Recordf("%s // %v", o, i.Error()) } @@ -629,7 +644,20 @@ type iterSetBoundsOp struct { func (o *iterSetBoundsOp) run(t *test, h *history) { i := t.getIter(o.iterID) - i.SetBounds(o.lower, o.upper) + var lower, upper []byte + if o.lower != nil { + lower = append(lower, o.lower...) + } + if o.upper != nil { + upper = append(upper, o.upper...) + } + i.SetBounds(lower, upper) + + // Trash the bounds to ensure that Pebble doesn't rely on the stability of + // the user-provided bounds. + rand.Read(lower[:]) + rand.Read(upper[:]) + h.Recordf("%s // %v", o, i.Error()) } @@ -651,14 +679,30 @@ type iterSetOptionsOp struct { func (o *iterSetOptionsOp) run(t *test, h *history) { i := t.getIter(o.iterID) - i.SetOptions(&pebble.IterOptions{ - LowerBound: o.lower, - UpperBound: o.upper, + + var lower, upper []byte + if o.lower != nil { + lower = append(lower, o.lower...) + } + if o.upper != nil { + upper = append(upper, o.upper...) + } + opts := &pebble.IterOptions{ + LowerBound: lower, + UpperBound: upper, KeyTypes: pebble.IterKeyType(o.keyTypes), RangeKeyMasking: pebble.RangeKeyMasking{ Suffix: o.rangeKeyMaskSuffix, }, - }) + } + + i.SetOptions(opts) + + // Trash the bounds to ensure that Pebble doesn't rely on the stability of + // the user-provided bounds. + rand.Read(lower[:]) + rand.Read(upper[:]) + h.Recordf("%s // %v", o, i.Error()) }