github.com/phillinzzz/newBsc@v1.1.6/core/vm/analysis_test.go (about)

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