Skip to content

Commit 14cefcb

Browse files
fuzz: improvements for exploring more code paths (#824)
* actions: select fuzz targets automatically So that we don't need to edit the actions yaml each time a new fuzz function is added. * fuzz: help fuzzers explore more paths 1. make charset.FromHTML explore BOM codepaths too 2. make json fuzz explore query paths too 3. make FuzzMimetype explore truncated and full inputs too (some functions have logic depending on truncation)
1 parent 5b11e3a commit 14cefcb

5 files changed

Lines changed: 30 additions & 22 deletions

File tree

‎.github/workflows/fuzz.yml‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,17 @@ jobs:
3232
key: fuzz-${{ runner.os }}-${{ github.run_id }}
3333
restore-keys: |
3434
fuzz-${{ runner.os }}-
35-
# 2m is chosen so that half of the 2000 free CI minutes offered by Github
36-
# will be used each month. The other half is reserved for tests in PRs.
37-
# TODO: Find a way to select all fuzz tests, without listing them individually.
38-
- run: go test -fuzztime=2m -fuzz=Mimetype ./
39-
- run: go test -fuzztime=2m -fuzz=Search ./internal/scan
40-
- run: go test -fuzztime=2m -fuzz=Match ./internal/scan
41-
- run: go test -fuzztime=2m -fuzz=Json ./internal/json
42-
- run: go test -fuzztime=2m -fuzz=Parser ./internal/csv
43-
- run: go test -fuzztime=2m -fuzz=Plain ./internal/charset
44-
- run: go test -fuzztime=2m -fuzz=XML ./internal/charset
45-
- run: go test -fuzztime=2m -fuzz=HTML ./internal/charset
46-
- run: go test -fuzztime=2m -fuzz=Meta ./internal/charset
47-
- run: go test -fuzztime=2m -fuzz=GetAnAttribute ./internal/markup
48-
- run: go test -fuzztime=2m -fuzz=GetAValue ./internal/markup
49-
- run: go test -fuzztime=2m -fuzz=ExtractFrame ./internal/mp3
50-
- run: go test -fuzztime=2m -fuzz=Detect ./internal/cdf
35+
# 2m per target is chosen so that roughly half of the 2000 free CI minutes
36+
# offered by Github will be used each month. The other half is reserved for
37+
# tests in PRs. Keep an eye on the total run time when adding fuzz targets.
38+
- name: Run all fuzz tests
39+
run: |
40+
for pkg in $(go list ./...); do
41+
for target in $(go test -list '^Fuzz' "$pkg" | grep '^Fuzz' || true); do
42+
echo "=== fuzzing $target in $pkg ==="
43+
go test -fuzztime=2m -fuzz="^${target}\$" "$pkg"
44+
done
45+
done
5146
5247
- name: Save fuzz corpus
5348
if: always()

‎internal/charset/charset_test.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,13 @@ func FuzzFromHTML(f *testing.F) {
9494
for _, tc := range fromHTMLTestCases {
9595
f.Add([]byte(tc.in))
9696
}
97+
// Seed BOMs so the FromBOM path gets explored.
98+
f.Add([]byte("\xef\xbb\xbf<meta charset=\"utf-8\">"))
99+
f.Add([]byte("\xfe\xff\x00a"))
100+
f.Add([]byte("\xff\xfea\x00"))
97101

98102
f.Fuzz(func(t *testing.T, d []byte) {
99-
fromHTML(d)
103+
FromHTML(d)
100104
})
101105
}
102106

‎internal/json/parser_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ func FuzzJson(f *testing.F) {
821821
for _, p := range positives {
822822
f.Add([]byte(p.json), true)
823823
}
824+
queryTypes := []string{QueryNone, QueryGeo, QueryHAR, QueryGLTF, QueryCDX}
824825
p := &parserState{}
825826
f.Fuzz(func(t *testing.T, data []byte, reset bool) {
826827
if reset {
@@ -831,5 +832,9 @@ func FuzzJson(f *testing.F) {
831832
p.consumeArray(data, nil, 1)
832833
p.consumeObject(data, nil, 1)
833834
p.consumeAny(data, nil, 1)
835+
// Exercise the query matching logic through the public entry point.
836+
for _, q := range queryTypes {
837+
Parse(q, data)
838+
}
834839
})
835840
}

‎internal/scan/bytes_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ func BenchmarkMatch(b *testing.B) {
525525
FullWord,
526526
} {
527527
b.Run(fmt.Sprintf("%d", f), func(b *testing.B) {
528+
b.ReportAllocs()
528529
for b.Loop() {
529530
Bytes(randData).Match(randData, f)
530531
}

‎mimetype_test.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"math"
87
"math/rand"
98
"mime"
109
"os"
@@ -880,16 +879,20 @@ func TestExtend(t *testing.T) {
880879
// search for panics.
881880
func FuzzMimetype(f *testing.F) {
882881
for _, tc := range testcases {
883-
if len(tc.data) < 100 && tc.bench == one {
884-
f.Add([]byte(tc.data))
882+
if tc.bench == one || tc.bench == all {
883+
f.Add([]byte(tc.data), true)
885884
}
886885
}
887886
// First node is root. Remove it because it matches any input.
888887
detectors := root.flatten()[1:]
889-
f.Fuzz(func(t *testing.T, data []byte) {
888+
f.Fuzz(func(t *testing.T, data []byte, truncate bool) {
890889
matched := false
890+
limit := uint32(0)
891+
if truncate {
892+
limit = uint32(len(data)) //nolint:gosec
893+
}
891894
for _, d := range detectors {
892-
if d.detector(data, math.MaxUint32) {
895+
if d.detector(data, limit) {
893896
matched = true
894897
}
895898
}

0 commit comments

Comments
 (0)