Skip to content

Commit

Permalink
Merge branch 'master' into all-use-fmt.Fprintf-to-avoid-raw-string-co…
Browse files Browse the repository at this point in the history
…ncatentation+io.StringWriter.WriteString
  • Loading branch information
n2p5 authored Jan 9, 2025
2 parents b4f6bbb + e5b9095 commit c58fbe1
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contribs/gnomd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/mattn/go-isatty v0.0.11 // indirect
github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
)
3 changes: 2 additions & 1 deletion contribs/gnomd/go.sum

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

31 changes: 31 additions & 0 deletions gnovm/pkg/gnolang/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gnolang

import (
"testing"
)

var sink any = nil

var pkgIDPaths = []string{
"encoding/json",
"math/bits",
"github.com/gnolang/gno/gnovm/pkg/gnolang",
"a",
" ",
"",
"github.com/gnolang/gno/gnovm/pkg/gnolang/vendor/pkg/github.com/gnolang/vendored",
}

func BenchmarkPkgIDFromPkgPath(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, path := range pkgIDPaths {
sink = PkgIDFromPkgPath(path)
}
}

if sink == nil {
b.Fatal("Benchmark did not run!")
}
sink = nil
}
89 changes: 89 additions & 0 deletions gnovm/pkg/gnolang/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package gnolang

import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/cockroachdb/apd/v3"
)

func FuzzConvertUntypedBigdecToFloat(f *testing.F) {
// 1. Firstly add seeds.
seeds := []string{
"-100000",
"100000",
"0",
}

check := new(apd.Decimal)
for _, seed := range seeds {
if check.UnmarshalText([]byte(seed)) == nil {
f.Add(seed)
}
}

f.Fuzz(func(t *testing.T, apdStr string) {
switch {
case strings.HasPrefix(apdStr, ".-"):
return
}

v := new(apd.Decimal)
if err := v.UnmarshalText([]byte(apdStr)); err != nil {
return
}
if _, err := v.Float64(); err != nil {
return
}

bd := BigdecValue{
V: v,
}
dst := new(TypedValue)
typ := Float64Type
ConvertUntypedBigdecTo(dst, bd, typ)
})
}

func FuzzParseFile(f *testing.F) {
// 1. Add the corpra.
parseFileDir := filepath.Join("testdata", "corpra", "parsefile")
paths, err := filepath.Glob(filepath.Join(parseFileDir, "*.go"))
if err != nil {
f.Fatal(err)
}

// Also load in files from gno/gnovm/tests/files
pc, curFile, _, _ := runtime.Caller(0)
curFileDir := filepath.Dir(curFile)
gnovmTestFilesDir, err := filepath.Abs(filepath.Join(curFileDir, "..", "..", "tests", "files"))
if err != nil {
_ = pc // To silence the arbitrary golangci linter.
f.Fatal(err)
}
globGnoTestFiles := filepath.Join(gnovmTestFilesDir, "*.gno")
gnoTestFiles, err := filepath.Glob(globGnoTestFiles)
if err != nil {
f.Fatal(err)
}
if len(gnoTestFiles) == 0 {
f.Fatalf("no files found from globbing %q", globGnoTestFiles)
}
paths = append(paths, gnoTestFiles...)

for _, path := range paths {
blob, err := os.ReadFile(path)
if err != nil {
f.Fatal(err)
}
f.Add(string(blob))
}

// 2. Now run the fuzzer.
f.Fuzz(func(t *testing.T, goFileContents string) {
_, _ = ParseFile("a.go", goFileContents)
})
}
20 changes: 19 additions & 1 deletion gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strings"
"sync"

bm "github.com/gnolang/gno/gnovm/pkg/benchops"
)
Expand Down Expand Up @@ -71,8 +72,25 @@ func (pid PkgID) Bytes() []byte {
return pid.Hashlet[:]
}

var (
pkgIDFromPkgPathCacheMu sync.Mutex // protects the shared cache.
// TODO: later on switch this to an LRU if needed to ensure
// fixed memory caps. For now though it isn't a problem:
// https://github.com/gnolang/gno/pull/3424#issuecomment-2564571785
pkgIDFromPkgPathCache = make(map[string]*PkgID, 100)
)

func PkgIDFromPkgPath(path string) PkgID {
return PkgID{HashBytes([]byte(path))}
pkgIDFromPkgPathCacheMu.Lock()
defer pkgIDFromPkgPathCacheMu.Unlock()

pkgID, ok := pkgIDFromPkgPathCache[path]
if !ok {
pkgID = new(PkgID)
*pkgID = PkgID{HashBytes([]byte(path))}
pkgIDFromPkgPathCache[path] = pkgID
}
return *pkgID
}

// Returns the ObjectID of the PackageValue associated with path.
Expand Down
9 changes: 9 additions & 0 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import
_ "math/big"
)

func main() {
println("Foo")
}
16 changes: 16 additions & 0 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "crypto/rand"

func init() {
}

func init() {
}

func init() {
}

func it() {
_ = rand.Read
}
22 changes: 22 additions & 0 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3013.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "testing"

func TestDummy(t *testing.T) {
testTable := []struct {
name string
}{
{
"one",
},
{
"two",
},
}

for _, testCase := range testTable {
testCase := testCase

println(testCase.name)
}
}
10 changes: 10 additions & 0 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main
var ss = []int{1, 2, 3}
func main() {
for _, s := range ss {
s := s
println(s)
 }
}
21 changes: 21 additions & 0 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main
var testTable = []struct {
name string
}{
 {
"one",
 },
 {
"two",
 },
}
func main() {
for _, testCase := range testTable {
testCase := testCase
println(testCase.name)
 }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
func main() {
for i := 0; i < 3; i++ {
i := i
println(i)
 }
}
11 changes: 11 additions & 0 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main
func main() {
a := 1
b := 3
println(a, b) // prints 1 3
// Re-declaration of 'a' is allowed because 'c' is a new variable
a, c := 2, 5
println(a, c) // prints 2 5
}
13 changes: 13 additions & 0 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main
func main() {
a := 1
println(a) // prints 1
if true {
a := 2 // valid: new scope inside the if statement
println(a) // prints 2
 }
println(a) // prints 1: outer variable is unchanged
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main
func main() {
a, b := 1, 2
a, b := 3, 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string(".-700000000000000000000000000000000000000")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
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.
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)")
Loading

0 comments on commit c58fbe1

Please sign in to comment.