analysis_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2017 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. "testing"
  19. "github.com/ethereum/go-ethereum/crypto"
  20. )
  21. func TestJumpDestAnalysis(t *testing.T) {
  22. tests := []struct {
  23. code []byte
  24. exp byte
  25. which int
  26. }{
  27. {[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0x40, 0},
  28. {[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0x50, 0},
  29. {[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, 0x7F, 0},
  30. {[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 1},
  31. {[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0x03, 0},
  32. {[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
  33. {[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x74, 0},
  34. {[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
  35. {[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x3F, 0},
  36. {[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xC0, 1},
  37. {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x7F, 0},
  38. {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xFF, 1},
  39. {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 2},
  40. {[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0x7f, 0},
  41. {[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0xA0, 1},
  42. {[]byte{byte(PUSH32)}, 0x7F, 0},
  43. {[]byte{byte(PUSH32)}, 0xFF, 1},
  44. {[]byte{byte(PUSH32)}, 0xFF, 2},
  45. }
  46. for i, test := range tests {
  47. ret := codeBitmap(test.code)
  48. if ret[test.which] != test.exp {
  49. t.Fatalf("test %d: expected %x, got %02x", i, test.exp, ret[test.which])
  50. }
  51. }
  52. }
  53. func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
  54. // 1.4 ms
  55. code := make([]byte, 1200000)
  56. bench.ResetTimer()
  57. for i := 0; i < bench.N; i++ {
  58. codeBitmap(code)
  59. }
  60. bench.StopTimer()
  61. }
  62. func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
  63. // 4 ms
  64. code := make([]byte, 1200000)
  65. bench.ResetTimer()
  66. for i := 0; i < bench.N; i++ {
  67. crypto.Keccak256Hash(code)
  68. }
  69. bench.StopTimer()
  70. }
  71. func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
  72. var op OpCode
  73. bencher := func(b *testing.B) {
  74. code := make([]byte, 32*b.N)
  75. for i := range code {
  76. code[i] = byte(op)
  77. }
  78. bits := make(bitvec, len(code)/8+1+4)
  79. b.ResetTimer()
  80. codeBitmapInternal(code, bits)
  81. }
  82. for op = PUSH1; op <= PUSH32; op++ {
  83. bench.Run(op.String(), bencher)
  84. }
  85. op = JUMPDEST
  86. bench.Run(op.String(), bencher)
  87. op = STOP
  88. bench.Run(op.String(), bencher)
  89. }