forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core/vm: move interpreter interruption check to jump instructions (et…
…hereum#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
- Loading branch information
Showing
3 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2021 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package vm | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/math" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
var loopInterruptTests = []string{ | ||
// infinite loop using JUMP: push(2) jumpdest dup1 jump | ||
"60025b8056", | ||
// infinite loop using JUMPI: push(1) push(4) jumpdest dup2 dup2 jumpi | ||
"600160045b818157", | ||
} | ||
|
||
func TestLoopInterrupt(t *testing.T) { | ||
address := common.BytesToAddress([]byte("contract")) | ||
vmctx := BlockContext{ | ||
Transfer: func(StateDB, common.Address, common.Address, *big.Int) {}, | ||
} | ||
|
||
for i, tt := range loopInterruptTests { | ||
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) | ||
statedb.CreateAccount(address) | ||
statedb.SetCode(address, common.Hex2Bytes(tt)) | ||
statedb.Finalise(true) | ||
|
||
evm := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{}) | ||
|
||
errChannel := make(chan error) | ||
timeout := make(chan bool) | ||
|
||
go func(evm *EVM) { | ||
_, _, err := evm.Call(AccountRef(common.Address{}), address, nil, math.MaxUint64, new(big.Int)) | ||
errChannel <- err | ||
}(evm) | ||
|
||
go func() { | ||
<-time.After(time.Second) | ||
timeout <- true | ||
}() | ||
|
||
evm.Cancel() | ||
|
||
select { | ||
case <-timeout: | ||
t.Errorf("test %d timed out", i) | ||
case err := <-errChannel: | ||
if err != nil { | ||
t.Errorf("test %d failure: %v", i, err) | ||
} | ||
} | ||
} | ||
|
||
} |