Skip to content

Commit

Permalink
Merge pull request Fantom-foundation#28 from Fantom-foundation/HonzaD…
Browse files Browse the repository at this point in the history
…ajc/interpreter_tests

Interpreter tests
  • Loading branch information
jenikd authored Feb 16, 2023
2 parents 99baff2 + cebce26 commit 4d1ecae
Show file tree
Hide file tree
Showing 6 changed files with 1,007 additions and 9 deletions.
2 changes: 2 additions & 0 deletions core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/holiman/uint256"
)

//go:generate mockgen -source interface.go -destination mock_state.go -package vm StateDB

// StateDB is an EVM database for full state querying.
type StateDB interface {
CreateAccount(common.Address)
Expand Down
32 changes: 23 additions & 9 deletions core/vm/lfvm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ func step(c *context) {
func stepToEnd(c *context) {
steps(c, false)
}

func checkStackBoundry(c *context, op OpCode) error {
stackLen := c.stack.len()
if stackLen < staticStackBoundry[op].stackMin {
c.err = &vm.ErrStackUnderflow{}
c.status = ERROR
return c.err
}
if stackLen > int(params.StackLimit)-1 && stackLen > staticStackBoundry[op].stackMax {
c.err = &vm.ErrStackOverflow{}
c.status = ERROR
return c.err
}
return nil
}

func steps(c *context, one_step_only bool) {
// Idea: handle static gas price in static dispatch below (saves an array lookup)
static_gas_prices := getStaticGasPrices(c.isBerlin)
Expand All @@ -410,6 +426,11 @@ func steps(c *context, one_step_only bool) {
return
}

// Need to check Call stack boundry before using static gas
if op == CALL && checkStackBoundry(c, op) != nil {
return
}

// If the interpreter is operating in readonly mode, make sure no
// state-modifying operation is performed. The 3rd stack item
// for a call operation is the value. Transferring value from one
Expand All @@ -426,15 +447,8 @@ func steps(c *context, one_step_only bool) {
return
}

stackLen := c.stack.len()
if stackLen < staticStackBoundry[op].stackMin {
c.err = &vm.ErrStackUnderflow{}
c.status = ERROR
return
}
if stackLen > int(params.StackLimit)-1 && stackLen > staticStackBoundry[op].stackMax {
c.err = &vm.ErrStackOverflow{}
c.status = ERROR
// Check stack boundry for every instruction
if checkStackBoundry(c, op) != nil {
return
}

Expand Down
Loading

0 comments on commit 4d1ecae

Please sign in to comment.