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

feat: fuzz gnovm/pkg/transpiler.Transpile #3457

Merged
merged 2 commits into from
Jan 9, 2025
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
80 changes: 80 additions & 0 deletions gnovm/pkg/transpiler/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package transpiler

import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)

func FuzzTranspiling(f *testing.F) {
if testing.Short() {
f.Skip("Running in -short mode")
}

// 1. Derive the seeds from our seedGnoFiles.
breakRoot := filepath.Join("gnolang", "gno")
pc, thisFile, _, _ := runtime.Caller(0)
index := strings.Index(thisFile, breakRoot)
_ = pc // to silence the pedantic golangci linter.
n2p5 marked this conversation as resolved.
Show resolved Hide resolved
rootPath := thisFile[:index+len(breakRoot)]
examplesDir := filepath.Join(rootPath, "examples")
ffs := os.DirFS(examplesDir)
fs.WalkDir(ffs, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
panic(err)
}
if !strings.HasSuffix(path, ".gno") {
return nil
}
file, err := ffs.Open(path)
if err != nil {
panic(err)
}
blob, err := io.ReadAll(file)
file.Close()
if err != nil {
panic(err)
}
f.Add(blob)
return nil
})

// 2. Run the fuzzers.
f.Fuzz(func(t *testing.T, gnoSourceCode []byte) {
// 3. Add timings to ensure that if transpiling takes a long time
// to run, that we report this as problematic.
doneCh := make(chan bool, 1)
readyCh := make(chan bool)
go func() {
defer func() {
r := recover()
if r == nil {
return
}

sr := fmt.Sprintf("%s", r)
if !strings.Contains(sr, "invalid line number ") {
panic(r)
}
}()
close(readyCh)
defer close(doneCh)
_, _ = Transpile(string(gnoSourceCode), "gno", "in.gno")
doneCh <- true
}()

<-readyCh

select {
case <-time.After(2 * time.Second):
t.Fatalf("took more than 2 seconds to transpile\n\n%s", gnoSourceCode)
case <-doneCh:
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("package\tA\nimport(\"\"//\"\n\"\"/***/)")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("package A\nimport(\"\"//\"\n\"\"/***/)")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("package A\ncon\x12\n\xec|b\x80\xddQst(\n/*\n\n\n\n\n\n\nka\n*/A)")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("//\"\npackage\tA\nimport(\"\"//\"\n\"\"/***/)")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("//0000\x170000000000:0\npackage A")
Loading