From 0dd218472bc7566f63ce577d0058bcc8e9471fdf Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sun, 19 Jan 2025 14:40:51 -0800 Subject: [PATCH] gnolang/gno: prototype Machine.RunContext --- gnovm/pkg/gnolang/gno_test.go | 27 +++++++++++++++++++ gnovm/pkg/gnolang/machine.go | 26 +++++++++++++++++- .../gnolang/testdata/corpra/parsefile/a.go | 9 ------- .../gnolang/testdata/corpra/parsefile/b.go | 16 ----------- .../testdata/corpra/parsefile/bug_3013.go | 22 --------------- .../corpra/parsefile/bug_3014_redefine.go | 10 ------- .../corpra/parsefile/bug_3014_redefine2.go | 21 --------------- .../corpra/parsefile/bug_3014_redefine3.go | 8 ------ .../corpra/parsefile/bug_3014_redefine4.go | 11 -------- .../corpra/parsefile/bug_3014_redefine5.go | 13 --------- .../corpra/parsefile/bug_3014_redefine6.go | 6 ----- .../4be24841138e3224 | 2 -- .../94196a9456e79dac | 2 -- 13 files changed, 52 insertions(+), 121 deletions(-) delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/a.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/b.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3013.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine2.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine3.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine4.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine5.go delete mode 100644 gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine6.go delete mode 100644 gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/4be24841138e3224 delete mode 100644 gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/94196a9456e79dac diff --git a/gnovm/pkg/gnolang/gno_test.go b/gnovm/pkg/gnolang/gno_test.go index 5a8c6faf315..67344814421 100644 --- a/gnovm/pkg/gnolang/gno_test.go +++ b/gnovm/pkg/gnolang/gno_test.go @@ -2,6 +2,7 @@ package gnolang import ( "bytes" + "context" "fmt" "io" "os" @@ -10,6 +11,7 @@ import ( "strings" "testing" "text/template" + "time" "unsafe" // "github.com/davecgh/go-spew/spew" @@ -291,6 +293,31 @@ func main() { m.RunMain() } +func TestRunMemoryAllocationBuster(t *testing.T) { + t.Parallel() + + m := NewMachine("test", nil) + c := `package test +func main() { + var x interface{} + for i := 0; i < 10000; i++ { + x = [1]interface{}{x} + } + for i := 0; i < 10000; i++ { + println(x) + } +} +` + n := MustParseFile("main.go", c) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + m.SetRunContext(ctx) + + m.RunFiles(n) + m.RunMain() + +} + func BenchmarkPreprocessForLoop(b *testing.B) { m := NewMachine("test", nil) c := `package test diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index 75d12ac5402..d667621f399 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -3,6 +3,7 @@ package gnolang // XXX rename file to machine.go. import ( + "context" "fmt" "io" "reflect" @@ -79,6 +80,13 @@ type Machine struct { // it is executed. It is reset to zero after the defer functions in the current // scope have finished executing. DeferPanicScope uint + + runCtx context.Context +} + +func (m *Machine) RunContext(ctx context.Context) { + m.runCtx = ctx + m.Run() } // NewMachine initializes a new gno virtual machine, acting as a shorthand @@ -98,6 +106,12 @@ func NewMachine(pkgPath string, store Store) *Machine { }) } +func (m *Machine) SetRunContext(ctx context.Context) { + if m != nil { + m.runCtx = ctx + } +} + // MachineOptions is used to pass options to [NewMachineWithOptions]. type MachineOptions struct { // Active package of the given machine; must be set before execution. @@ -810,7 +824,11 @@ func (m *Machine) RunStatement(s Stmt) { m.PushOp(OpHalt) m.PushStmt(s) m.PushOp(OpExec) - m.Run() + ctx := m.runCtx + if ctx == nil { + ctx = context.Background() + } + m.RunContext(ctx) } // Runs a declaration after preprocessing d. If d was already @@ -1168,6 +1186,12 @@ func (m *Machine) Run() { if m.Debugger.enabled { m.Debug() } + if m.runCtx != nil { + if err := m.runCtx.Err(); err != nil { + panic(err) + } + } + op := m.PopOp() if bm.OpsEnabled { // benchmark the operation. diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/a.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/a.go deleted file mode 100644 index ae05a655fd7..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/a.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import - _ "math/big" -) - -func main() { - println("Foo") -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/b.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/b.go deleted file mode 100644 index 07592ff47ed..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/b.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import "crypto/rand" - -func init() { -} - -func init() { -} - -func init() { -} - -func it() { - _ = rand.Read -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3013.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3013.go deleted file mode 100644 index f664f68f1b6..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3013.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import "testing" - -func TestDummy(t *testing.T) { - testTable := []struct { - name string - }{ - { - "one", - }, - { - "two", - }, - } - - for _, testCase := range testTable { - testCase := testCase - - println(testCase.name) - } -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine.go deleted file mode 100644 index 877b5fafc1d..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -var ss = []int{1, 2, 3} - -func main() { - for _, s := range ss { - s := s - println(s) - } -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine2.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine2.go deleted file mode 100644 index 450406a2202..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine2.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -var testTable = []struct { - name string -}{ - { - "one", - }, - { - "two", - }, -} - -func main() { - - for _, testCase := range testTable { - testCase := testCase - - println(testCase.name) - } -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine3.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine3.go deleted file mode 100644 index 74ed729fb28..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine3.go +++ /dev/null @@ -1,8 +0,0 @@ -package main - -func main() { - for i := 0; i < 3; i++ { - i := i - println(i) - } -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine4.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine4.go deleted file mode 100644 index db27dcdc6bf..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine4.go +++ /dev/null @@ -1,11 +0,0 @@ -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 -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine5.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine5.go deleted file mode 100644 index 97ec50f3330..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine5.go +++ /dev/null @@ -1,13 +0,0 @@ -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 -} diff --git a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine6.go b/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine6.go deleted file mode 100644 index 49b871be9c0..00000000000 --- a/gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine6.go +++ /dev/null @@ -1,6 +0,0 @@ -package main - -func main() { - a, b := 1, 2 - a, b := 3, 4 -} diff --git a/gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/4be24841138e3224 b/gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/4be24841138e3224 deleted file mode 100644 index 8894efa8d8e..00000000000 --- a/gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/4be24841138e3224 +++ /dev/null @@ -1,2 +0,0 @@ -go test fuzz v1 -string(".-700000000000000000000000000000000000000") diff --git a/gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/94196a9456e79dac b/gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/94196a9456e79dac deleted file mode 100644 index a317fbab107..00000000000 --- a/gnovm/pkg/gnolang/testdata/fuzz/FuzzConvertUntypedBigdecToFloat/94196a9456e79dac +++ /dev/null @@ -1,2 +0,0 @@ -go test fuzz v1 -string("200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")