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

fix: hardcode max vm cycles in keeper #1807

Merged
merged 7 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 19 additions & 1 deletion gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type MachineOptions struct {
Alloc *Allocator // or see MaxAllocBytes.
MaxAllocBytes int64 // or 0 for no limit.
MaxCycles int64 // or 0 for no limit.
IsTest bool
deelawn marked this conversation as resolved.
Show resolved Hide resolved
}

// the machine constructor gets spammed
Expand All @@ -99,6 +100,10 @@ func NewMachineWithOptions(opts MachineOptions) *Machine {
checkTypes := opts.CheckTypes
readOnly := opts.ReadOnly
maxCycles := opts.MaxCycles
if !opts.IsTest && maxCycles == 0 {
// Never allow an infinite number of cycles.
maxCycles = 10_000_000
deelawn marked this conversation as resolved.
Show resolved Hide resolved
}
output := opts.Output
if output == nil {
output = os.Stdout
Expand Down Expand Up @@ -654,6 +659,13 @@ func (m *Machine) RunFunc(fn Name) {
func (m *Machine) RunMain() {
defer func() {
if r := recover(); r != nil {
// This is likely due to an infinite loop or recursion.
// Don't tryp to print out the machine state as it may
deelawn marked this conversation as resolved.
Show resolved Hide resolved
// take a very long time or never finish.
if v, ok := r.(cpuCyclePanic); ok {
panic(v.String())
}

fmt.Printf("Machine.RunMain() panic: %v\n%s\n",
r, m.String())
panic(r)
Expand Down Expand Up @@ -949,10 +961,16 @@ const (
//----------------------------------------
// "CPU" steps.

type cpuCyclePanic struct{}

func (p cpuCyclePanic) String() string {
return "CPU cycle overrun"
}
deelawn marked this conversation as resolved.
Show resolved Hide resolved

func (m *Machine) incrCPU(cycles int64) {
m.Cycles += cycles
if m.MaxCycles != 0 && m.Cycles > m.MaxCycles {
panic("CPU cycle overrun")
panic(cpuCyclePanic{})
}
}

Expand Down
1 change: 1 addition & 0 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAll
Store: store,
Context: ctx,
MaxAllocBytes: maxAlloc,
IsTest: true,
})
return m
}
Expand Down
12 changes: 12 additions & 0 deletions gnovm/tests/files/cycles.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {
recurse()
}

func recurse() int {
return recurse()
}

// Error:
// CPU cycle overrun
1 change: 1 addition & 0 deletions gnovm/tests/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestMachineTestMemPackage(t *testing.T) {
Output: os.Stdout,
Store: store,
Context: nil,
IsTest: true,
})
memPkg := gno.ReadMemPackage(tt.path, "test")

Expand Down
Loading