Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions fuzz/bloom_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package fuzz_bloom

import (
"bytes"
"encoding/gob"
fuzz "github.com/AdaLogics/go-fuzz-headers"
"github.com/bits-and-blooms/bloom/v3"
"math"
"runtime/debug"
"testing"
)

func RoundTripGobEncodeDecode(t *testing.T, filter *bloom.BloomFilter) {
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
err := enc.Encode(filter)
if err != nil {
t.Error(err)
}
dec := gob.NewDecoder(&buffer)
var decoded_filter bloom.BloomFilter
err = dec.Decode(&decoded_filter)
if err != nil {
t.Error(err)
}
if !decoded_filter.Equal(filter) {
t.Errorf("Expected round trip encode/decode to result in identical bloom filters.")
}
}

type FuzzData struct {
ElementCount uint64
FalsePositives float64
Add [][]byte
AddString []string
TestLocations []uint64
NotAdded [][]byte
}

const (
maxElements = 10240
minElements = 2
maxFalsePositives = 0.99
minFalsePositives = 0.01
)

func FuzzBloom(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
f := fuzz.NewConsumer(data)
structuredData := FuzzData{}
err := f.GenerateStruct(&structuredData)
if err != nil {
return
}
defer func() {
if r := recover(); r != nil {
t.Log(string(debug.Stack()))
t.Fatalf("Failing input:\n %+v", structuredData)
}
}()

// Limit input to max size.
structuredData.ElementCount %= maxElements
if structuredData.ElementCount == 0 {
return
}

// Limit false positives.
structuredData.FalsePositives = math.Mod(structuredData.FalsePositives, maxFalsePositives)
if structuredData.FalsePositives < minFalsePositives || math.IsNaN(structuredData.FalsePositives) || math.IsInf(structuredData.FalsePositives, 0) {
return
}

filter := bloom.NewWithEstimates(uint(structuredData.ElementCount), structuredData.FalsePositives)
for _, to_add := range structuredData.Add {
_ = filter.Add(to_add)
}
for _, to_add := range structuredData.AddString {
_ = filter.AddString(to_add)
}
for _, to_test := range structuredData.Add {
if !filter.Test(to_test) {
t.Logf("Failed with structured input: %v", structuredData)
t.Errorf("%v was added but was not reported as present in the set.", to_test)
}
}
for _, to_test := range structuredData.AddString {
if !filter.TestString(to_test) {
t.Errorf("String '%s' was added but was not reported as present in the set.", to_test)
}
}

for _, to_test := range structuredData.NotAdded {
_ = filter.Test(to_test)
}
_ = filter.K()
_ = filter.ApproximatedSize()
_ = filter.Cap()
_ = filter.TestLocations(structuredData.TestLocations)
RoundTripGobEncodeDecode(t, filter)
})
}
4 changes: 4 additions & 0 deletions fuzz/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
# OSS-fuzz build script

compile_native_go_fuzzer $SRC/bloom/fuzz FuzzBloom fuzz_bloom
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/bits-and-blooms/bloom/v3
go 1.14

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
github.com/bits-and-blooms/bitset v1.3.1
github.com/twmb/murmur3 v1.1.6
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 h1:EKPd1INOIyr5hWOWhvpmQpY6tKjeG0hT1s3AMC/9fic=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1/go.mod h1:VzwV+t+dZ9j/H867F1M2ziD+yLHtB46oM35FxxMJ4d0=
github.com/bits-and-blooms/bitset v1.3.1 h1:y+qrlmq3XsWi+xZqSaueaE8ry8Y127iMxlMfqcK8p0g=
github.com/bits-and-blooms/bitset v1.3.1/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg=
github.com/twmb/murmur3 v1.1.6/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=