Skip to content

Commit

Permalink
huff0: Improve fuzz base set
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Dec 6, 2023
1 parent bb3065b commit 59b3d4d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
21 changes: 17 additions & 4 deletions huff0/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func FuzzCompress(f *testing.F) {
fuzz.AddFromZip(f, "testdata/fse_compress.zip", fuzz.TypeRaw, false)
fuzz.AddFromZip(f, "testdata/regression.zip", fuzz.TypeRaw, testing.Short())
f.Fuzz(func(t *testing.T, buf0 []byte) {
//use of Compress1X
var s Scratch
Expand Down Expand Up @@ -76,15 +77,27 @@ func FuzzCompress(f *testing.F) {

func FuzzDecompress1x(f *testing.F) {
fuzz.AddFromZip(f, "testdata/huff0_decompress1x.zip", fuzz.TypeRaw, false)
var s Scratch
addCompressed := func(b []byte) {
s.Reuse = ReusePolicyNone
if b2, _, err := Compress1X(b, &s); err == nil {
f.Add(b2)
}
s.Reuse = ReusePolicyNone
if b2, _, err := Compress4X(b, &s); err == nil {
f.Add(b2)
}
}
fuzz.ReturnFromZip(f, "testdata/regression.zip", fuzz.TypeRaw, addCompressed)
fuzz.ReturnFromZip(f, "testdata/fse_compress.zip", fuzz.TypeRaw, addCompressed)

f.Fuzz(func(t *testing.T, buf0 []byte) {
var s Scratch
_, remain, err := ReadTable(buf0, &s)
if err != nil {
return
}
out, err := s.Decompress1X(remain)
if err != nil || out == nil {
return
}
s.Decompress1X(remain)
s.Decompress4X(remain, len(buf0))
})
}
58 changes: 58 additions & 0 deletions internal/fuzz/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,64 @@ func AddFromZip(f *testing.F, filename string, t InputType, short bool) {
}
}

// ReturnFromZip will read the supplied zip and add all as corpus for f.
// Byte slices only.
func ReturnFromZip(tb testing.TB, filename string, t InputType, fn func([]byte)) {
file, err := os.Open(filename)
if err != nil {
tb.Fatal(err)
}
fi, err := file.Stat()
if err != nil {
tb.Fatal(err)
}
zr, err := zip.NewReader(file, fi.Size())
if err != nil {
tb.Fatal(err)
}
for _, file := range zr.File {
rc, err := file.Open()
if err != nil {
tb.Fatal(err)
}

b, err := io.ReadAll(rc)
if err != nil {
tb.Fatal(err)
}
rc.Close()
t := t
if t == TypeOSSFuzz {
t = TypeRaw // Fallback
if len(b) >= 4 {
sz := binary.BigEndian.Uint32(b)
if sz <= uint32(len(b))-4 {
fn(b[4 : 4+sz])
continue
}
}
}

if bytes.HasPrefix(b, []byte("go test fuzz")) {
t = TypeGoFuzz
} else {
t = TypeRaw
}

if t == TypeRaw {
fn(b)
continue
}
vals, err := unmarshalCorpusFile(b)
if err != nil {
tb.Fatal(err)
}
for _, v := range vals {
fn(v)
}
}
}

// unmarshalCorpusFile decodes corpus bytes into their respective values.
func unmarshalCorpusFile(b []byte) ([][]byte, error) {
if len(b) == 0 {
Expand Down

0 comments on commit 59b3d4d

Please sign in to comment.