Skip to content

Commit

Permalink
test: fuzz gnovm/pkg/gnolang Machine.doOpEval for BasicLitExpr
Browse files Browse the repository at this point in the history
Fuzzing for BasicLitExpr and equivalence comparisons
for if those values parse in Go.

Updates #3087
  • Loading branch information
odeke-em committed Jan 9, 2025
1 parent 2438505 commit 373f360
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions gnovm/pkg/gnolang/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package gnolang

import (
"context"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

"github.com/cockroachdb/apd/v3"
)
Expand Down Expand Up @@ -87,3 +91,79 @@ func FuzzParseFile(f *testing.F) {
_, _ = ParseFile("a.go", goFileContents)
})
}

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

// 1. Add the seeds.
seeds := []*BasicLitExpr{
{Kind: INT, Value: "9223372036854775807"},
{Kind: INT, Value: "0"},
{Kind: INT, Value: "0777"},
{Kind: INT, Value: "0xDEADBEEF"},
{Kind: INT, Value: "0o123"},
{Kind: INT, Value: "0b111111111111111111111111111111111111111111111111111111111111111"},
{Kind: FLOAT, Value: "0.00001111"},
{Kind: FLOAT, Value: "9999.12"},
{Kind: STRING, Value: `"9999.12"`},
{Kind: STRING, Value: `"aaaaaaaaaaa "`},
{Kind: STRING, Value: `"🚨🌎"`},
}

for _, seed := range seeds {
blob, err := json.Marshal(seed)
if err != nil {
panic(err)
}
f.Add(blob)
}

// 2. Fuzz it.
f.Fuzz(func(t *testing.T, basicLitExprBlob []byte) {
expr := new(BasicLitExpr)
if err := json.Unmarshal(basicLitExprBlob, expr); err != nil {
return
}

defer func() {
r := recover()
if r == nil {
return
}

switch {
case strings.Contains(fmt.Sprintf("%s", r), "unexpected lit kind"):
return

default:
if !basicLitExprIsValidGoValue(t, expr) {
return
}
panic(r)
}
}()

m := NewMachine("test", nil)
m.PushExpr(expr)
m.doOpEval()
_ = m.PopValue()
})
}

func basicLitExprIsValidGoValue(t *testing.T, lit *BasicLitExpr) bool {
tempDir := t.TempDir()
file := filepath.Join(tempDir, "basic_lit_check.go")
var craftedGo = []byte(fmt.Sprintf(`package main
var _ = %s
func main() {}`, lit.Value))
if err := os.WriteFile(file, craftedGo, 0755); err != nil {
panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := exec.CommandContext(ctx, "go", "run", file).Run()
return err == nil
}

0 comments on commit 373f360

Please sign in to comment.