Go json coverage (#4689)

* Good fuzz target for golang coverage with modules

* Place target in right directory for go-json-iterator

So that coverage gets access to the right package
This commit is contained in:
Catena cyber 2020-11-22 20:05:32 +01:00 committed by GitHub
parent bb8b2fb354
commit 91a6a12dbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 13 deletions

View File

@ -38,7 +38,13 @@ if [[ $SANITIZER = *coverage* ]]; then
sed -i -e 's/TestFuzzCorpus/Test'$function'Corpus/' ./"${function,,}"_test.go
echo "#/bin/sh" > $OUT/$fuzzer
echo "cd $path" >> $OUT/$fuzzer
if [[ ${GO111MODULE:-} = on ]]; then
echo "export GO111MODULE=on" >> $OUT/$fuzzer
echo "cd ../pkg/mod/"`go list -m $basemod | sed 's/ /@/'` >> $OUT/$fuzzer
echo "cd ./"`echo $path | cut -d/ -f4-` >> $OUT/$fuzzer
else
echo "cd $path" >> $OUT/$fuzzer
fi
echo "go test -run Test${function}Corpus -v $tags -coverprofile \$1 " >> $OUT/$fuzzer
chmod +x $OUT/$fuzzer

View File

@ -17,7 +17,5 @@
FROM gcr.io/oss-fuzz-base/base-builder
RUN go get github.com/json-iterator/go
RUN mkdir fuzz
COPY fuzz_json.go fuzz
COPY fuzz_json.go $GOPATH/src/github.com/json-iterator/go/
COPY build.sh $SRC/
WORKDIR fuzz

View File

@ -16,5 +16,4 @@
################################################################################
compile_go_fuzzer . Fuzz fuzz_json
compile_go_fuzzer github.com/json-iterator/go Fuzz fuzz_json

View File

@ -2,12 +2,11 @@
// Use of this source code is governed by Apache 2 LICENSE.
// Modified from original file https://github.com/dvyukov/go-fuzz-corpus/blob/master/json/json.go
package jsonfuzz
package jsoniter
import (
"encoding/json"
"fmt"
jsoniter "github.com/json-iterator/go"
"reflect"
)
@ -21,7 +20,7 @@ func Fuzz(data []byte) int {
func() interface{} { return new(S) },
} {
v := ctor()
if jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, v) != nil {
if ConfigCompatibleWithStandardLibrary.Unmarshal(data, v) != nil {
continue
}
score = 1
@ -36,12 +35,12 @@ func Fuzz(data []byte) int {
panic("not equal")
}
data1, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(v)
data1, err := ConfigCompatibleWithStandardLibrary.Marshal(v)
if err != nil {
panic(err)
}
v1 := ctor()
if jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data1, v1) != nil {
if ConfigCompatibleWithStandardLibrary.Unmarshal(data1, v1) != nil {
continue
}
if !reflect.DeepEqual(v, v1) {
@ -85,9 +84,9 @@ type Marshaller struct {
}
func (m *Marshaller) MarshalJSON() ([]byte, error) {
return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(m.v)
return ConfigCompatibleWithStandardLibrary.Marshal(m.v)
}
func (m *Marshaller) UnmarshalJSON(data []byte) error {
return jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, &m.v)
return ConfigCompatibleWithStandardLibrary.Unmarshal(data, &m.v)
}