-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fuzz gnovm/pkg/transpiler.Transpile (#3457)
Adds a fuzzer for Transpile, which found bugs: * #3425 in which this following Go program crashed the transpiler ```go package A import(""//" ""/***/) ``` * #3426 which generated an input that revealed the fact that Gno deviates from Go by allowing unused variables yet Go's standard is strict on unused variables like this program ```go package main func main() { const c1 = 1 < 8 main() 1 } ``` which we shouldn't allow * partially #3428 which revealed the discrepancy in Gno that the overflow detection is still lacking as the following program is invalid Go but Gno allowed it to run ```go package main func main() { const c1 = int8(1) << 8 println(c1) } ``` because 1<<8 (256) is higher than the range of int8 for which the maximum is `(1<<7) - 1 aka 127` Updates #3087 Co-authored-by: Nathan Toups <[email protected]>
- Loading branch information
Showing
6 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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: | ||
} | ||
}) | ||
} |
2 changes: 2 additions & 0 deletions
2
gnovm/pkg/transpiler/testdata/fuzz/FuzzTranspiling/26ec2c0a22e242fc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("package\tA\nimport(\"\"//\"\n\"\"/***/)") |
2 changes: 2 additions & 0 deletions
2
gnovm/pkg/transpiler/testdata/fuzz/FuzzTranspiling/54f7287f473abfa5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("package A\nimport(\"\"//\"\n\"\"/***/)") |
2 changes: 2 additions & 0 deletions
2
gnovm/pkg/transpiler/testdata/fuzz/FuzzTranspiling/703a1cacabb84c6d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") |
2 changes: 2 additions & 0 deletions
2
gnovm/pkg/transpiler/testdata/fuzz/FuzzTranspiling/bcd60839c81ca478
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("//\"\npackage\tA\nimport(\"\"//\"\n\"\"/***/)") |
2 changes: 2 additions & 0 deletions
2
gnovm/pkg/transpiler/testdata/fuzz/FuzzTranspiling/bf4f7515b25bf71b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("//0000\x170000000000:0\npackage A") |