interpreter_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. package vm
  17. import (
  18. "math/big"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. var loopInterruptTests = []string{
  28. // infinite loop using JUMP: push(2) jumpdest dup1 jump
  29. "60025b8056",
  30. // infinite loop using JUMPI: push(1) push(4) jumpdest dup2 dup2 jumpi
  31. "600160045b818157",
  32. }
  33. func TestLoopInterrupt(t *testing.T) {
  34. address := common.BytesToAddress([]byte("contract"))
  35. vmctx := BlockContext{
  36. Transfer: func(StateDB, common.Address, common.Address, *big.Int) {},
  37. }
  38. for i, tt := range loopInterruptTests {
  39. statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
  40. statedb.CreateAccount(address)
  41. statedb.SetCode(address, common.Hex2Bytes(tt))
  42. statedb.Finalise(true)
  43. evm := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{})
  44. errChannel := make(chan error)
  45. timeout := make(chan bool)
  46. go func(evm *EVM) {
  47. _, _, err := evm.Call(AccountRef(common.Address{}), address, nil, math.MaxUint64, new(big.Int))
  48. errChannel <- err
  49. }(evm)
  50. go func() {
  51. <-time.After(time.Second)
  52. timeout <- true
  53. }()
  54. evm.Cancel()
  55. select {
  56. case <-timeout:
  57. t.Errorf("test %d timed out", i)
  58. case err := <-errChannel:
  59. if err != nil {
  60. t.Errorf("test %d failure: %v", i, err)
  61. }
  62. }
  63. }
  64. }