Skip to content

Commit 477b94a

Browse files
authored
Merge pull request pquerna#254 from bookmoons/master
Continuous fuzzing
2 parents dac163c + 2b878b2 commit 477b94a

File tree

9 files changed

+250
-1
lines changed

9 files changed

+250
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ tests/goser/ff/goser_ffjson.go
33
tests/types/ff/everything_ffjson.go
44
tests/number/ff/number_ffjson.go
55
tests/ff_ffjson.go
6+
fuzzing/

.travis.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
language: go
22

3+
services:
4+
- docker
5+
36
install:
47
- A=${PWD#*github.com/};A=${A%/ffjson};cd ../..;mv $A pquerna;cd pquerna/ffjson
58
- go get -d -v -t ./...
69

710
script: make clean && make lint && make test && make test
811

12+
jobs:
13+
include:
14+
- stage: Fuzz regression
15+
go: 1.12.x
16+
dist: bionic
17+
script:
18+
- cd tests/fuzz
19+
- ./fuzzit.sh local-regression
20+
- stage: Fuzz
21+
if: branch = master AND type IN (push)
22+
go: 1.12.x
23+
dist: bionic
24+
script:
25+
- cd tests/fuzz
26+
- ./fuzzit.sh fuzzing
27+
928
go:
1029
- "1.10.x"
1130
- "1.11.x"
1231

1332
env:
14-
- GO15VENDOREXPERIMENT=1
33+
GO15VENDOREXPERIMENT=1

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ffjson: faster JSON for Go
22

33
[![Build Status](https://travis-ci.org/pquerna/ffjson.svg?branch=master)](https://travis-ci.org/pquerna/ffjson)
4+
[![Fuzzit Status](https://app.fuzzit.dev/badge?org_id=pquerna)](https://app.fuzzit.dev/orgs/pquerna/dashboard)
45

56
`ffjson` generates static `MarshalJSON` and `UnmarshalJSON` functions for structures in Go. The generated functions reduce the reliance upon runtime reflection to do serialization and are generally 2 to 3 times faster. In cases where `ffjson` doesn't understand a Type involved, it falls back to `encoding/json`, meaning it is a safe drop in replacement. By using `ffjson` your JSON serialization just gets faster with no additional code changes.
67

tests/fuzz/fuzzit.sh

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
set -xe
3+
4+
# Validate arguments
5+
if [ "$#" -ne 1 ]; then
6+
echo "Usage: $0 <fuzz-type>"
7+
exit 1
8+
fi
9+
10+
# Configure
11+
NAME=ffjson
12+
TYPE=$1
13+
FUZZIT_VERSION=2.4.46
14+
GO_FUZZ_VERSION=1810d380ab9c2786af00db592f86d83063216ed0
15+
16+
# Setup
17+
(cd ../.. && make)
18+
rm -f target_ffjson.go
19+
ffjson target.go
20+
export GO111MODULE=on
21+
go get -u -v \
22+
github.com/dvyukov/go-fuzz/go-fuzz@$GO_FUZZ_VERSION \
23+
github.com/dvyukov/go-fuzz/go-fuzz-build@$GO_FUZZ_VERSION
24+
go mod vendor -v
25+
rm -rf gopath
26+
mkdir -p gopath/src
27+
mv vendor/* gopath/src
28+
rm -rf vendor
29+
export GOPATH=$PWD/gopath
30+
export GO111MODULE=off
31+
if [[ ! -f fuzzit || ! `./fuzzit --version` =~ $FUZZIT_VERSION$ ]]; then
32+
wget -q -O fuzzit https://github.com/fuzzitdev/fuzzit/releases/download/v$FUZZIT_VERSION/fuzzit_Linux_x86_64
33+
chmod a+x fuzzit
34+
fi
35+
./fuzzit --version
36+
37+
# Fuzz
38+
function fuzz {
39+
FUNC=Fuzz$1
40+
TARGET=$2
41+
go-fuzz-build -libfuzzer -func $FUNC -o fuzzer.a .
42+
clang -fsanitize=fuzzer fuzzer.a -o fuzzer
43+
./fuzzit create job --type $TYPE $NAME/$TARGET fuzzer
44+
}
45+
fuzz Generate generate
46+
fuzz Unmarshal unmarshal

tests/fuzz/generator_fuzz.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// +build gofuzz
2+
3+
package fuzz
4+
5+
import (
6+
"io/ioutil"
7+
"os"
8+
9+
_ "github.com/dvyukov/go-fuzz/go-fuzz-dep"
10+
"github.com/pquerna/ffjson/generator"
11+
)
12+
13+
// Fuzz tests code generation.
14+
func FuzzGenerate(fuzz []byte) int {
15+
err := os.MkdirAll("fuzzing", os.ModePerm)
16+
if err != nil {
17+
panic("could not make fuzzing dir")
18+
}
19+
err = ioutil.WriteFile("fuzzing/input.go", fuzz, 0644)
20+
if err != nil {
21+
panic("could not write input file")
22+
}
23+
err = generator.GenerateFiles(
24+
"go",
25+
"fuzzing/input.go",
26+
"fuzzing/output.go",
27+
"",
28+
true,
29+
true,
30+
)
31+
if err != nil {
32+
return 0
33+
}
34+
return 1
35+
}

tests/fuzz/go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module fuzz.test/fuzz
2+
3+
go 1.12
4+
5+
require (
6+
github.com/dvyukov/go-fuzz v0.0.0-20190828145000-1810d380ab9c // indirect
7+
github.com/elazarl/go-bindata-assetfs v1.0.0 // indirect
8+
github.com/stephens2424/writerset v1.0.2 // indirect
9+
golang.org/x/tools v0.0.0-20190911230505-6bfd74cf029c // indirect
10+
)

tests/fuzz/go.sum

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
github.com/Julusian/godocdown v0.0.0-20170816220326-6d19f8ff2df8/go.mod h1:INZr5t32rG59/5xeltqoCJoNY7e5x/3xoY9WSWVWg74=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/dvyukov/go-fuzz v0.0.0-20190828145000-1810d380ab9c h1:DFsz4uHHaXIkv6K/2JhR5ufgx/pquhSsBgB25b5Oj1k=
4+
github.com/dvyukov/go-fuzz v0.0.0-20190828145000-1810d380ab9c/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
5+
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
6+
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/pquerna/ffjson v0.0.0-20190813045741-dac163c6c0a9 h1:kyf9snWXHvQc+yxE9imhdI8YAm4oKeZISlaAR+x73zs=
9+
github.com/pquerna/ffjson v0.0.0-20190813045741-dac163c6c0a9/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
10+
github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C9WhFzY47SzYBIvzFqSvHIR6ROgDo4TtdTuRaOMjF/s=
11+
github.com/stephens2424/writerset v1.0.2 h1:znRLgU6g8RS5euYRcy004XeE4W+Tu44kALzy7ghPif8=
12+
github.com/stephens2424/writerset v1.0.2/go.mod h1:aS2JhsMn6eA7e82oNmW4rfsgAOp9COBTTl8mzkwADnc=
13+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
14+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
15+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
16+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
17+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
18+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
19+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
20+
golang.org/x/tools v0.0.0-20190911230505-6bfd74cf029c h1:ZgedNh8bIOBjyY5XEG0kR/41dSN9H+5jFZWuR/TgA1g=
21+
golang.org/x/tools v0.0.0-20190911230505-6bfd74cf029c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
22+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

tests/fuzz/target.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package fuzz
2+
3+
import (
4+
"time"
5+
)
6+
7+
// Data provides an unmarshaling target
8+
type Data struct {
9+
A uint8
10+
B uint16
11+
C uint32
12+
D uint64
13+
14+
E int8
15+
F int16
16+
G int32
17+
H int64
18+
19+
I float32
20+
J float64
21+
22+
M byte
23+
N rune
24+
25+
O int
26+
P uint
27+
Q string
28+
R bool
29+
S time.Time
30+
31+
Ap *uint8
32+
Bp *uint16
33+
Cp *uint32
34+
Dp *uint64
35+
36+
Ep *int8
37+
Fp *int16
38+
Gp *int32
39+
Hp *int64
40+
41+
IP *float32
42+
Jp *float64
43+
44+
Mp *byte
45+
Np *rune
46+
47+
Op *int
48+
Pp *uint
49+
Qp *string
50+
Rp *bool
51+
Sp *time.Time
52+
53+
Aa []uint8
54+
Ba []uint16
55+
Ca []uint32
56+
Da []uint64
57+
58+
Ea []int8
59+
Fa []int16
60+
Ga []int32
61+
Ha []int64
62+
63+
Ia []float32
64+
Ja []float64
65+
66+
Ma []byte
67+
Na []rune
68+
69+
Oa []int
70+
Pa []uint
71+
Qa []string
72+
Ra []bool
73+
74+
Aap []*uint8
75+
Bap []*uint16
76+
Cap []*uint32
77+
Dap []*uint64
78+
79+
Eap []*int8
80+
Fap []*int16
81+
Gap []*int32
82+
Hap []*int64
83+
84+
Iap []*float32
85+
Jap []*float64
86+
87+
Map []*byte
88+
Nap []*rune
89+
90+
Oap []*int
91+
Pap []*uint
92+
Qap []*string
93+
Rap []*bool
94+
}

tests/fuzz/target_fuzz.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build gofuzz
2+
3+
package fuzz
4+
5+
import (
6+
_ "github.com/dvyukov/go-fuzz/go-fuzz-dep"
7+
)
8+
9+
// FuzzUnmarshal tests unmarshaling.
10+
func FuzzUnmarshal(fuzz []byte) int {
11+
data := &Data{}
12+
err := data.UnmarshalJSON(fuzz)
13+
if err != nil {
14+
return 0
15+
}
16+
_, err = data.MarshalJSON()
17+
if err != nil {
18+
return 0
19+
}
20+
return 1
21+
}

0 commit comments

Comments
 (0)