Skip to content

Commit b02fe53

Browse files
authored
core/vm: move interpreter interruption check to jump instructions (ethereum#24026)
* core/vm: Remove interpreter loop interruption check * core/vm: Unit test for interpreter loop interruption * core/vm: Check for interpreter loop abort on every jump
1 parent 9331fe2 commit b02fe53

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

core/vm/instructions.go

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package vm
1818

1919
import (
20+
"sync/atomic"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/ethereum/go-ethereum/core/types"
2224
"github.com/ethereum/go-ethereum/params"
@@ -525,6 +527,9 @@ func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
525527
}
526528

527529
func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
530+
if atomic.LoadInt32(&interpreter.evm.abort) != 0 {
531+
return nil, errStopToken
532+
}
528533
pos := scope.Stack.pop()
529534
if !scope.Contract.validJumpdest(&pos) {
530535
return nil, ErrInvalidJump
@@ -534,6 +539,9 @@ func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
534539
}
535540

536541
func opJumpi(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
542+
if atomic.LoadInt32(&interpreter.evm.abort) != 0 {
543+
return nil, errStopToken
544+
}
537545
pos, cond := scope.Stack.pop(), scope.Stack.pop()
538546
if !cond.IsZero() {
539547
if !scope.Contract.validJumpdest(&pos) {

core/vm/interpreter.go

-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package vm
1818

1919
import (
2020
"hash"
21-
"sync/atomic"
2221

2322
"github.com/ethereum/go-ethereum/common"
2423
"github.com/ethereum/go-ethereum/common/math"
@@ -178,12 +177,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
178177
// explicit STOP, RETURN or SELFDESTRUCT is executed, an error occurred during
179178
// the execution of one of the operations or until the done flag is set by the
180179
// parent context.
181-
steps := 0
182180
for {
183-
steps++
184-
if steps%1000 == 0 && atomic.LoadInt32(&in.evm.abort) != 0 {
185-
break
186-
}
187181
if in.cfg.Debug {
188182
// Capture pre-execution values for tracing.
189183
logged, pcCopy, gasCopy = false, pc, contract.Gas

core/vm/interpreter_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package vm
18+
19+
import (
20+
"math/big"
21+
"testing"
22+
"time"
23+
24+
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/common/math"
26+
"github.com/ethereum/go-ethereum/core/rawdb"
27+
"github.com/ethereum/go-ethereum/core/state"
28+
"github.com/ethereum/go-ethereum/params"
29+
)
30+
31+
var loopInterruptTests = []string{
32+
// infinite loop using JUMP: push(2) jumpdest dup1 jump
33+
"60025b8056",
34+
// infinite loop using JUMPI: push(1) push(4) jumpdest dup2 dup2 jumpi
35+
"600160045b818157",
36+
}
37+
38+
func TestLoopInterrupt(t *testing.T) {
39+
address := common.BytesToAddress([]byte("contract"))
40+
vmctx := BlockContext{
41+
Transfer: func(StateDB, common.Address, common.Address, *big.Int) {},
42+
}
43+
44+
for i, tt := range loopInterruptTests {
45+
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
46+
statedb.CreateAccount(address)
47+
statedb.SetCode(address, common.Hex2Bytes(tt))
48+
statedb.Finalise(true)
49+
50+
evm := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{})
51+
52+
errChannel := make(chan error)
53+
timeout := make(chan bool)
54+
55+
go func(evm *EVM) {
56+
_, _, err := evm.Call(AccountRef(common.Address{}), address, nil, math.MaxUint64, new(big.Int))
57+
errChannel <- err
58+
}(evm)
59+
60+
go func() {
61+
<-time.After(time.Second)
62+
timeout <- true
63+
}()
64+
65+
evm.Cancel()
66+
67+
select {
68+
case <-timeout:
69+
t.Errorf("test %d timed out", i)
70+
case err := <-errChannel:
71+
if err != nil {
72+
t.Errorf("test %d failure: %v", i, err)
73+
}
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)