Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable fuzz tests and fix decoder bugs #597

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions dev/decoder/functor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ func (d *embeddedFieldPtrDecoder) FromDom(vp unsafe.Pointer, node internal.Node,
}

vp = unsafe.Pointer(uintptr(vp) + d.offset)
err := d.fieldDec.FromDom(vp, node, ctx)
if err != nil {
return err
}
return nil
return d.fieldDec.FromDom(vp, node, ctx)
}

type i8Decoder struct{}
Expand Down
4 changes: 2 additions & 2 deletions dev/decoder/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (d *structDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *cont
var gerr error
obj, err := node.AsObj()
if err != nil {
return nil
return err
}

next := obj.Children()
Expand All @@ -36,7 +36,7 @@ func (d *structDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *cont
val := internal.NewNode(internal.PtrOffset(next, 1))
next = val.Next()
if err != nil {
return nil
return err
}

idx := d.fieldMap.TryGet(key, i)
Expand Down
2 changes: 1 addition & 1 deletion dev/internal/rt_stubs_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func MakeSlice(oldPtr unsafe.Pointer, et *rt.GoType, newLen int) *rt.GoSlice {
return old
}

new := growslice(et, *old, newLen-old.Len)
new := growslice(et, *old, newLen)
// growslice does not zero out new[old.cap:new.len] since it assumes that
// the memory will be overwritten by an append() that called growslice.
// Since the caller of reflect_growslice is not append(),
Expand Down
4 changes: 2 additions & 2 deletions dev/internal/sonic_rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ func (node *Node) AsMapEface(ctx *Context, vp unsafe.Pointer) error {
func (node *Node) AsMapEfaceUseNumber(ctx *Context, vp unsafe.Pointer) error {
obj, err := node.AsObj()
if err != nil {
return nil
return err
}

size := obj.Len()
Expand Down Expand Up @@ -517,7 +517,7 @@ func (node *Node) AsMapEfaceUseNumber(ctx *Context, vp unsafe.Pointer) error {
func (node *Node) AsMapEfaceUseInt64(ctx *Context, vp unsafe.Pointer) error {
obj, err := node.AsObj()
if err != nil {
return nil
return err
}

size := obj.Len()
Expand Down
2 changes: 1 addition & 1 deletion dev/rs_wrapper/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dev/rs_wrapper/lib/darwin/libsonic_rs_aarch64-apple-darwin.a
Binary file not shown.
Binary file modified dev/rs_wrapper/lib/darwin/libsonic_rs_x86_64-apple-darwin.a
Binary file not shown.
Binary file modified dev/rs_wrapper/lib/linux/libsonic_rs_aarch64-unknown-linux-gnu.a
Binary file not shown.
Binary file modified dev/rs_wrapper/lib/linux/libsonic_rs_x86_64-unknown-linux-gnu.a
Binary file not shown.
11 changes: 7 additions & 4 deletions fuzz/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
testname := FuzzMain
corpusdir := ./testdata/fuzz/${testname}
go := go

fuzz:
fuzz:
${go} version
mkdir -p ${corpusdir}
rm -rf ./go-fuzz-corpus
git clone https://github.com/dvyukov/go-fuzz-corpus.git ./go-fuzz-corpus/
go install golang.org/x/tools/cmd/file2fuzz@latest
${go} install golang.org/x/tools/cmd/file2fuzz@latest
file2fuzz -o ${corpusdir} ./go-fuzz-corpus/json/corpus/* ./corpus/*

run:
SONIC_FUZZ_MEM_LIMIT=2 GOMAXPROCS=4 go test -fuzz=${testname} -v -fuzztime 10m

${go} version
SONIC_FUZZ_MEM_LIMIT=2 GOMAXPROCS=4 ${go} test -fuzz=${testname} -v -fuzztime 10m

clean:
rm -rf ./go-fuzz-corpus/
rm -rf ./testdata/
39 changes: 21 additions & 18 deletions fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.18
// +build go1.18

/*
Expand All @@ -19,21 +20,21 @@
package sonic_fuzz

import (
`encoding/json`
`testing`
_ `unicode/utf8`
`os`
`runtime`
`runtime/debug`
`time`
`io`
`log`
`strconv`

`github.com/bytedance/sonic`
`github.com/stretchr/testify/require`
`github.com/davecgh/go-spew/spew`
`github.com/bytedance/gopkg/util/gctuner`
"encoding/json"
"io"
"log"
"os"
"runtime"
"runtime/debug"
"strconv"
"testing"
"time"
"unicode/utf8"

"github.com/bytedance/gopkg/util/gctuner"
"github.com/bytedance/sonic"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/require"
)

func FuzzMain(f *testing.F) {
Expand All @@ -46,6 +47,7 @@ func FuzzMain(f *testing.F) {
// Used for debug falied fuzz corpus
func TestCorpus(t *testing.T) {
fuzzMain(t, []byte("[1\x00"))
// fuzzMain(t, []byte("3469446951536141862700000000000000000e-62"))
fuzzMain(t, []byte("\"\\uDE1D\\uDE1D\\uDEDD\\uDE1D\\uDE1D\\uDE1D\\uDE1D\\uDEDD\\uDE1D\""))
// fuzzMain(t, []byte(`{"":null}`))
}
Expand All @@ -57,8 +59,9 @@ func fuzzMain(t *testing.T, data []byte) {
fuzzHtmlEscape(t, data)
// fuzz ast get api, should not panic here.
fuzzAst(t, data)
// Only fuzz the validate json here.
if !json.Valid(data) {
// Rust std repr invalid utf8 chars is diff from Golang,
// so we skip invalid utf8 here.
if !json.Valid(data) || !utf8.Valid(data) {
return
}
fuzzStream(t, data)
Expand All @@ -82,7 +85,7 @@ func fuzzMain(t *testing.T, data []byte) {
if jerr != nil {
continue
}
require.Equal(t, sv, jv, dump(data, jv, jerr, sv, serr))
require.Equal(t, jv, sv, dump(string(data), jv, jerr, sv, serr))

v := jv
sout, serr := target.Marshal(v)
Expand Down
Loading