aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Johnstone <steven.james.johnstone@gmail.com>2022-03-10 13:38:00 +0000
committerDmitri Shuralyov <dmitshur@golang.org>2022-03-14 16:31:27 +0000
commitd69d093c77250a58c648756db42bbad8ad823770 (patch)
tree6b5c48cfcee0480352270056a7be489ff67155b0
parentc79ccd88ab65d4db6a5cf1875cf4d963b9e4fe0b (diff)
downloadgo-d69d093c77250a58c648756db42bbad8ad823770.tar.gz
go-d69d093c77250a58c648756db42bbad8ad823770.zip
[release-branch.go1.18] internal/fuzz: minimization should not reduce coverage
Minimization should result in a fuzz input which includes the same coverage bits as the original input. Updates #48326 Change-Id: I6c5f30058b57ccd1a096ad0e9452a4dfbb7d9aab Reviewed-on: https://go-review.googlesource.com/c/go/+/391454 Trust: Bryan Mills <bcmills@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org> Run-TryBot: Roland Shoemaker <roland@golang.org> Auto-Submit: Roland Shoemaker <roland@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> (cherry picked from commit 5003ed884a67ee26b4cedbe6f5b1c02bd5eb6630) Reviewed-on: https://go-review.googlesource.com/c/go/+/391798 Trust: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
-rw-r--r--src/cmd/go/testdata/script/test_fuzz_minimize_interesting.txt11
-rw-r--r--src/internal/fuzz/coverage.go11
-rw-r--r--src/internal/fuzz/worker.go3
3 files changed, 13 insertions, 12 deletions
diff --git a/src/cmd/go/testdata/script/test_fuzz_minimize_interesting.txt b/src/cmd/go/testdata/script/test_fuzz_minimize_interesting.txt
index 5d0de17f6b..a09e85b972 100644
--- a/src/cmd/go/testdata/script/test_fuzz_minimize_interesting.txt
+++ b/src/cmd/go/testdata/script/test_fuzz_minimize_interesting.txt
@@ -127,19 +127,8 @@ func FuzzMinCache(f *testing.F) {
if bytes.Equal(buf, seed) {
return
}
- if n := sum(buf); n < 0 {
- t.Error("sum cannot be negative")
- }
})
}
-
-func sum(buf []byte) int {
- n := 0
- for _, b := range buf {
- n += int(b)
- }
- return n
-}
-- check_testdata/check_testdata.go --
//go:build ignore
// +build ignore
diff --git a/src/internal/fuzz/coverage.go b/src/internal/fuzz/coverage.go
index 3dee73b81c..88f98a16b2 100644
--- a/src/internal/fuzz/coverage.go
+++ b/src/internal/fuzz/coverage.go
@@ -66,6 +66,17 @@ func countNewCoverageBits(base, snapshot []byte) int {
return n
}
+// isCoverageSubset returns true if all the base coverage bits are set in
+// snapshot
+func isCoverageSubset(base, snapshot []byte) bool {
+ for i, v := range base {
+ if v&snapshot[i] != v {
+ return false
+ }
+ }
+ return true
+}
+
// hasCoverageBit returns true if snapshot has at least one bit set that is
// also set in base.
func hasCoverageBit(base, snapshot []byte) bool {
diff --git a/src/internal/fuzz/worker.go b/src/internal/fuzz/worker.go
index 83d937ee6d..6e4c4e2d0f 100644
--- a/src/internal/fuzz/worker.go
+++ b/src/internal/fuzz/worker.go
@@ -894,7 +894,8 @@ func (ws *workerServer) minimizeInput(ctx context.Context, vals []any, mem *shar
}
return true
}
- if keepCoverage != nil && hasCoverageBit(keepCoverage, coverageSnapshot) {
+ // Minimization should preserve coverage bits.
+ if keepCoverage != nil && isCoverageSubset(keepCoverage, coverageSnapshot) {
return true
}
vals[args.Index] = prev