diff --git a/projects/go-json-iterator/Dockerfile b/projects/go-json-iterator/Dockerfile new file mode 100644 index 000000000..eef259a0a --- /dev/null +++ b/projects/go-json-iterator/Dockerfile @@ -0,0 +1,24 @@ +# Copyright 2019 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +################################################################################ + +FROM gcr.io/oss-fuzz-base/base-builder +MAINTAINER taowen@gmail.com +RUN go get github.com/json-iterator/go + +RUN mkdir fuzz +COPY fuzz_json.go fuzz/ +COPY build.sh $SRC/ +WORKDIR fuzz diff --git a/projects/go-json-iterator/build.sh b/projects/go-json-iterator/build.sh new file mode 100755 index 000000000..c8edbc320 --- /dev/null +++ b/projects/go-json-iterator/build.sh @@ -0,0 +1,31 @@ +#!/bin/bash -eu +# Copyright 2019 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +################################################################################ + +# build target function +function compile_fuzzer { + path=$1 + function=$2 + fuzzer=$3 + + # Instrument all Go files relevant to this fuzzer + go-fuzz-build -libfuzzer -func $function -o $fuzzer.a $path + + # Instrumented, compiled Go ($fuzzer.a) + fuzzing engine = fuzzer binary + $CXX $CXXFLAGS $LIB_FUZZING_ENGINE $fuzzer.a -lpthread -o $OUT/$fuzzer +} + +compile_fuzzer . Fuzz fuzz_json diff --git a/projects/go-json-iterator/fuzz_json.go b/projects/go-json-iterator/fuzz_json.go new file mode 100644 index 000000000..c057a63bf --- /dev/null +++ b/projects/go-json-iterator/fuzz_json.go @@ -0,0 +1,93 @@ +// Copyright 2015 go-fuzz project authors. All rights reserved. +// 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 + +import ( + "encoding/json" + "fmt" + jsoniter "github.com/json-iterator/go" + "reflect" +) + +func Fuzz(data []byte) int { + score := 0 + for _, ctor := range []func() interface{}{ + //func() interface{} { return nil }, + func() interface{} { return new([]interface{}) }, + func() interface{} { m := map[string]string{}; return &m }, + func() interface{} { m := map[string]interface{}{}; return &m }, + func() interface{} { return new(S) }, + } { + v := ctor() + if jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, v) != nil { + continue + } + score = 1 + vj := ctor() + err := json.Unmarshal(data, vj) + if err != nil { + panic(err) + } + if !reflect.DeepEqual(v, vj) { + fmt.Printf("v0: %#v\n", v) + fmt.Printf("v1: %#v\n", vj) + panic("not equal") + } + + data1, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(v) + if err != nil { + panic(err) + } + v1 := ctor() + if jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data1, v1) != nil { + continue + } + if !reflect.DeepEqual(v, v1) { + fmt.Printf("v0: %#v\n", v) + fmt.Printf("v1: %#v\n", v1) + panic("not equal") + } + } + return score +} + +type S struct { + A int `json:",omitempty"` + B string `json:"B1,omitempty"` + C float64 + D bool + E uint8 + F []byte + G interface{} + H map[string]interface{} + I map[string]string + J []interface{} + K []string + L S1 + M *S1 + N *int + O **int + // P json.RawMessage + Q Marshaller + R int `json:"-"` + S int `json:",string"` +} + +type S1 struct { + A int + B string +} + +type Marshaller struct { + v string +} + +func (m *Marshaller) MarshalJSON() ([]byte, error) { + return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(m.v) +} + +func (m *Marshaller) UnmarshalJSON(data []byte) error { + return jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, &m.v) +} diff --git a/projects/go-json-iterator/project.yaml b/projects/go-json-iterator/project.yaml new file mode 100644 index 000000000..2fc93ba65 --- /dev/null +++ b/projects/go-json-iterator/project.yaml @@ -0,0 +1,8 @@ +homepage: "https://jsoniter.com" +primary_contact: "taowen@gmail.com" +auto_ccs : "p.antoine@catenacyber.fr" + +fuzzing_engines: +- libfuzzer +sanitizers: +- address