jit_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2015 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. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/params"
  22. )
  23. const maxRun = 1000
  24. func TestSegmenting(t *testing.T) {
  25. prog := NewProgram([]byte{byte(PUSH1), 0x1, byte(PUSH1), 0x1, 0x0})
  26. err := CompileProgram(prog)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if instr, ok := prog.instructions[0].(pushSeg); ok {
  31. if len(instr.data) != 2 {
  32. t.Error("expected 2 element width pushSegment, got", len(instr.data))
  33. }
  34. } else {
  35. t.Errorf("expected instr[0] to be a pushSeg, got %T", prog.instructions[0])
  36. }
  37. prog = NewProgram([]byte{byte(PUSH1), 0x1, byte(PUSH1), 0x1, byte(JUMP)})
  38. err = CompileProgram(prog)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if _, ok := prog.instructions[1].(jumpSeg); ok {
  43. } else {
  44. t.Errorf("expected instr[1] to be jumpSeg, got %T", prog.instructions[1])
  45. }
  46. prog = NewProgram([]byte{byte(PUSH1), 0x1, byte(PUSH1), 0x1, byte(PUSH1), 0x1, byte(JUMP)})
  47. err = CompileProgram(prog)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. if instr, ok := prog.instructions[0].(pushSeg); ok {
  52. if len(instr.data) != 2 {
  53. t.Error("expected 2 element width pushSegment, got", len(instr.data))
  54. }
  55. } else {
  56. t.Errorf("expected instr[0] to be a pushSeg, got %T", prog.instructions[0])
  57. }
  58. if _, ok := prog.instructions[2].(jumpSeg); ok {
  59. } else {
  60. t.Errorf("expected instr[1] to be jumpSeg, got %T", prog.instructions[1])
  61. }
  62. }
  63. func TestCompiling(t *testing.T) {
  64. prog := NewProgram([]byte{0x60, 0x10})
  65. err := CompileProgram(prog)
  66. if err != nil {
  67. t.Error("didn't expect compile error")
  68. }
  69. if len(prog.instructions) != 1 {
  70. t.Error("expected 1 compiled instruction, got", len(prog.instructions))
  71. }
  72. }
  73. func TestResetInput(t *testing.T) {
  74. var sender account
  75. env := NewEnvironment(Context{}, nil, params.TestChainConfig, Config{})
  76. contract := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000))
  77. contract.CodeAddr = &common.Address{}
  78. program := NewProgram([]byte{})
  79. RunProgram(program, env, contract, []byte{0xbe, 0xef})
  80. if contract.Input != nil {
  81. t.Errorf("expected input to be nil, got %x", contract.Input)
  82. }
  83. }
  84. func TestPcMappingToInstruction(t *testing.T) {
  85. program := NewProgram([]byte{byte(PUSH2), 0xbe, 0xef, byte(ADD)})
  86. CompileProgram(program)
  87. if program.mapping[3] != 1 {
  88. t.Error("expected mapping PC 4 to me instr no. 2, got", program.mapping[4])
  89. }
  90. }
  91. var benchmarks = map[string]vmBench{
  92. "pushes": vmBench{
  93. false, false, false,
  94. common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil,
  95. },
  96. }
  97. func BenchmarkPushes(b *testing.B) {
  98. runVmBench(benchmarks["pushes"], b)
  99. }
  100. type vmBench struct {
  101. precompile bool // compile prior to executing
  102. nojit bool // ignore jit (sets DisbaleJit = true
  103. forcejit bool // forces the jit, precompile is ignored
  104. code []byte
  105. input []byte
  106. }
  107. type account struct{}
  108. func (account) SubBalance(amount *big.Int) {}
  109. func (account) AddBalance(amount *big.Int) {}
  110. func (account) SetAddress(common.Address) {}
  111. func (account) Value() *big.Int { return nil }
  112. func (account) SetBalance(*big.Int) {}
  113. func (account) SetNonce(uint64) {}
  114. func (account) Balance() *big.Int { return nil }
  115. func (account) Address() common.Address { return common.Address{} }
  116. func (account) ReturnGas(*big.Int) {}
  117. func (account) SetCode(common.Hash, []byte) {}
  118. func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
  119. func runVmBench(test vmBench, b *testing.B) {
  120. var sender account
  121. if test.precompile && !test.forcejit {
  122. NewProgram(test.code)
  123. }
  124. env := NewEnvironment(Context{}, nil, params.TestChainConfig, Config{EnableJit: !test.nojit, ForceJit: test.forcejit})
  125. b.ResetTimer()
  126. for i := 0; i < b.N; i++ {
  127. context := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000))
  128. context.Code = test.code
  129. context.CodeAddr = &common.Address{}
  130. _, err := env.EVM().Run(context, test.input)
  131. if err != nil {
  132. b.Error(err)
  133. b.FailNow()
  134. }
  135. }
  136. }