jit_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. )
  24. const maxRun = 1000
  25. func TestSegmenting(t *testing.T) {
  26. prog := NewProgram([]byte{byte(PUSH1), 0x1, byte(PUSH1), 0x1, 0x0})
  27. err := CompileProgram(prog)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if instr, ok := prog.instructions[0].(pushSeg); ok {
  32. if len(instr.data) != 2 {
  33. t.Error("expected 2 element width pushSegment, got", len(instr.data))
  34. }
  35. } else {
  36. t.Errorf("expected instr[0] to be a pushSeg, got %T", prog.instructions[0])
  37. }
  38. prog = NewProgram([]byte{byte(PUSH1), 0x1, byte(PUSH1), 0x1, byte(JUMP)})
  39. err = CompileProgram(prog)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if _, ok := prog.instructions[1].(jumpSeg); ok {
  44. } else {
  45. t.Errorf("expected instr[1] to be jumpSeg, got %T", prog.instructions[1])
  46. }
  47. prog = NewProgram([]byte{byte(PUSH1), 0x1, byte(PUSH1), 0x1, byte(PUSH1), 0x1, byte(JUMP)})
  48. err = CompileProgram(prog)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if instr, ok := prog.instructions[0].(pushSeg); ok {
  53. if len(instr.data) != 2 {
  54. t.Error("expected 2 element width pushSegment, got", len(instr.data))
  55. }
  56. } else {
  57. t.Errorf("expected instr[0] to be a pushSeg, got %T", prog.instructions[0])
  58. }
  59. if _, ok := prog.instructions[2].(jumpSeg); ok {
  60. } else {
  61. t.Errorf("expected instr[1] to be jumpSeg, got %T", prog.instructions[1])
  62. }
  63. }
  64. func TestCompiling(t *testing.T) {
  65. prog := NewProgram([]byte{0x60, 0x10})
  66. err := CompileProgram(prog)
  67. if err != nil {
  68. t.Error("didn't expect compile error")
  69. }
  70. if len(prog.instructions) != 1 {
  71. t.Error("expected 1 compiled instruction, got", len(prog.instructions))
  72. }
  73. }
  74. func TestResetInput(t *testing.T) {
  75. var sender account
  76. env := NewEnv(false, true)
  77. contract := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
  78. contract.CodeAddr = &common.Address{}
  79. program := NewProgram([]byte{})
  80. RunProgram(program, env, contract, []byte{0xbe, 0xef})
  81. if contract.Input != nil {
  82. t.Errorf("expected input to be nil, got %x", contract.Input)
  83. }
  84. }
  85. func TestPcMappingToInstruction(t *testing.T) {
  86. program := NewProgram([]byte{byte(PUSH2), 0xbe, 0xef, byte(ADD)})
  87. CompileProgram(program)
  88. if program.mapping[3] != 1 {
  89. t.Error("expected mapping PC 4 to me instr no. 2, got", program.mapping[4])
  90. }
  91. }
  92. var benchmarks = map[string]vmBench{
  93. "pushes": vmBench{
  94. false, false, false,
  95. common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil,
  96. },
  97. }
  98. func BenchmarkPushes(b *testing.B) {
  99. runVmBench(benchmarks["pushes"], b)
  100. }
  101. type vmBench struct {
  102. precompile bool // compile prior to executing
  103. nojit bool // ignore jit (sets DisbaleJit = true
  104. forcejit bool // forces the jit, precompile is ignored
  105. code []byte
  106. input []byte
  107. }
  108. type account struct{}
  109. func (account) SubBalance(amount *big.Int) {}
  110. func (account) AddBalance(amount *big.Int) {}
  111. func (account) SetAddress(common.Address) {}
  112. func (account) Value() *big.Int { return nil }
  113. func (account) SetBalance(*big.Int) {}
  114. func (account) SetNonce(uint64) {}
  115. func (account) Balance() *big.Int { return nil }
  116. func (account) Address() common.Address { return common.Address{} }
  117. func (account) ReturnGas(*big.Int, *big.Int) {}
  118. func (account) SetCode([]byte) {}
  119. func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
  120. func runVmBench(test vmBench, b *testing.B) {
  121. var sender account
  122. if test.precompile && !test.forcejit {
  123. NewProgram(test.code)
  124. }
  125. env := NewEnv(test.nojit, test.forcejit)
  126. b.ResetTimer()
  127. for i := 0; i < b.N; i++ {
  128. context := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
  129. context.Code = test.code
  130. context.CodeAddr = &common.Address{}
  131. _, err := env.Vm().Run(context, test.input)
  132. if err != nil {
  133. b.Error(err)
  134. b.FailNow()
  135. }
  136. }
  137. }
  138. type Env struct {
  139. gasLimit *big.Int
  140. depth int
  141. evm *EVM
  142. }
  143. func NewEnv(noJit, forceJit bool) *Env {
  144. env := &Env{gasLimit: big.NewInt(10000), depth: 0}
  145. env.evm = New(env, Config{
  146. EnableJit: !noJit,
  147. ForceJit: forceJit,
  148. })
  149. return env
  150. }
  151. func (self *Env) RuleSet() RuleSet { return ruleSet{new(big.Int)} }
  152. func (self *Env) Vm() Vm { return self.evm }
  153. func (self *Env) Origin() common.Address { return common.Address{} }
  154. func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
  155. func (self *Env) AddStructLog(log StructLog) {
  156. }
  157. func (self *Env) StructLogs() []StructLog {
  158. return nil
  159. }
  160. //func (self *Env) PrevHash() []byte { return self.parent }
  161. func (self *Env) Coinbase() common.Address { return common.Address{} }
  162. func (self *Env) MakeSnapshot() Database { return nil }
  163. func (self *Env) SetSnapshot(Database) {}
  164. func (self *Env) Time() *big.Int { return big.NewInt(time.Now().Unix()) }
  165. func (self *Env) Difficulty() *big.Int { return big.NewInt(0) }
  166. func (self *Env) Db() Database { return nil }
  167. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  168. func (self *Env) VmType() Type { return StdVmTy }
  169. func (self *Env) GetHash(n uint64) common.Hash {
  170. return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
  171. }
  172. func (self *Env) AddLog(log *Log) {
  173. }
  174. func (self *Env) Depth() int { return self.depth }
  175. func (self *Env) SetDepth(i int) { self.depth = i }
  176. func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
  177. return true
  178. }
  179. func (self *Env) Transfer(from, to Account, amount *big.Int) {}
  180. func (self *Env) Call(caller ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  181. return nil, nil
  182. }
  183. func (self *Env) CallCode(caller ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  184. return nil, nil
  185. }
  186. func (self *Env) Create(caller ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  187. return nil, common.Address{}, nil
  188. }
  189. func (self *Env) DelegateCall(me ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  190. return nil, nil
  191. }