github.com/MetalBlockchain/subnet-evm@v0.4.9/core/vm/interpreter_test.go (about)

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