github.com/MetalBlockchain/subnet-evm@v0.4.9/core/vm/analysis_test.go (about) 1 // (c) 2019-2020, 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 2017 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/bits" 31 "testing" 32 33 "github.com/ethereum/go-ethereum/crypto" 34 ) 35 36 func TestJumpDestAnalysis(t *testing.T) { 37 tests := []struct { 38 code []byte 39 exp byte 40 which int 41 }{ 42 {[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0b0000_0010, 0}, 43 {[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0b0000_1010, 0}, 44 {[]byte{0x00, byte(PUSH1), 0x00, byte(PUSH1), 0x00, byte(PUSH1), 0x00, byte(PUSH1)}, 0b0101_0100, 0}, 45 {[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, bits.Reverse8(0x7F), 0}, 46 {[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0001, 1}, 47 {[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0b1100_0000, 0}, 48 {[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0000, 1}, 49 {[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0010_1110, 0}, 50 {[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0000, 1}, 51 {[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1100, 0}, 52 {[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0011, 1}, 53 {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1110, 0}, 54 {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1111, 1}, 55 {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0001, 2}, 56 {[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0b1111_1110, 0}, 57 {[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0b0000_0101, 1}, 58 {[]byte{byte(PUSH32)}, 0b1111_1110, 0}, 59 {[]byte{byte(PUSH32)}, 0b1111_1111, 1}, 60 {[]byte{byte(PUSH32)}, 0b1111_1111, 2}, 61 {[]byte{byte(PUSH32)}, 0b1111_1111, 3}, 62 {[]byte{byte(PUSH32)}, 0b0000_0001, 4}, 63 } 64 for i, test := range tests { 65 ret := codeBitmap(test.code) 66 if ret[test.which] != test.exp { 67 t.Fatalf("test %d: expected %x, got %02x", i, test.exp, ret[test.which]) 68 } 69 } 70 } 71 72 const analysisCodeSize = 1200 * 1024 73 74 func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) { 75 // 1.4 ms 76 code := make([]byte, analysisCodeSize) 77 bench.SetBytes(analysisCodeSize) 78 bench.ResetTimer() 79 for i := 0; i < bench.N; i++ { 80 codeBitmap(code) 81 } 82 bench.StopTimer() 83 } 84 func BenchmarkJumpdestHashing_1200k(bench *testing.B) { 85 // 4 ms 86 code := make([]byte, analysisCodeSize) 87 bench.SetBytes(analysisCodeSize) 88 bench.ResetTimer() 89 for i := 0; i < bench.N; i++ { 90 crypto.Keccak256Hash(code) 91 } 92 bench.StopTimer() 93 } 94 95 func BenchmarkJumpdestOpAnalysis(bench *testing.B) { 96 var op OpCode 97 bencher := func(b *testing.B) { 98 code := make([]byte, analysisCodeSize) 99 b.SetBytes(analysisCodeSize) 100 for i := range code { 101 code[i] = byte(op) 102 } 103 bits := make(bitvec, len(code)/8+1+4) 104 b.ResetTimer() 105 for i := 0; i < b.N; i++ { 106 for j := range bits { 107 bits[j] = 0 108 } 109 codeBitmapInternal(code, bits) 110 } 111 } 112 for op = PUSH1; op <= PUSH32; op++ { 113 bench.Run(op.String(), bencher) 114 } 115 op = JUMPDEST 116 bench.Run(op.String(), bencher) 117 op = STOP 118 bench.Run(op.String(), bencher) 119 }