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