mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.6 KiB
136 lines
4.6 KiB
7 years ago
|
// Copyright 2017 The go-ethereum Authors
|
||
|
// This file is part of the go-ethereum library.
|
||
|
//
|
||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
8 years ago
|
package vm
|
||
|
|
||
6 years ago
|
import (
|
||
3 years ago
|
"math/bits"
|
||
6 years ago
|
"testing"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/crypto"
|
||
|
)
|
||
8 years ago
|
|
||
|
func TestJumpDestAnalysis(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
code []byte
|
||
|
exp byte
|
||
|
which int
|
||
|
}{
|
||
3 years ago
|
{[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0b0000_0010, 0},
|
||
|
{[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0b0000_1010, 0},
|
||
|
{[]byte{0x00, byte(PUSH1), 0x00, byte(PUSH1), 0x00, byte(PUSH1), 0x00, byte(PUSH1)}, 0b0101_0100, 0},
|
||
|
{[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, bits.Reverse8(0x7F), 0},
|
||
|
{[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0001, 1},
|
||
|
{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0b1100_0000, 0},
|
||
|
{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0000, 1},
|
||
|
{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0010_1110, 0},
|
||
|
{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0000, 1},
|
||
|
{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1100, 0},
|
||
|
{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0011, 1},
|
||
|
{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1110, 0},
|
||
|
{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1111, 1},
|
||
|
{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0001, 2},
|
||
|
{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0b1111_1110, 0},
|
||
|
{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0b0000_0101, 1},
|
||
|
{[]byte{byte(PUSH32)}, 0b1111_1110, 0},
|
||
|
{[]byte{byte(PUSH32)}, 0b1111_1111, 1},
|
||
|
{[]byte{byte(PUSH32)}, 0b1111_1111, 2},
|
||
|
{[]byte{byte(PUSH32)}, 0b1111_1111, 3},
|
||
|
{[]byte{byte(PUSH32)}, 0b0000_0001, 4},
|
||
8 years ago
|
}
|
||
3 years ago
|
for i, test := range tests {
|
||
7 years ago
|
ret := codeBitmap(test.code)
|
||
8 years ago
|
if ret[test.which] != test.exp {
|
||
3 years ago
|
t.Fatalf("test %d: expected %x, got %02x", i, test.exp, ret[test.which])
|
||
8 years ago
|
}
|
||
|
}
|
||
6 years ago
|
}
|
||
8 years ago
|
|
||
3 years ago
|
const analysisCodeSize = 1200 * 1024
|
||
|
|
||
6 years ago
|
func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
|
||
|
// 1.4 ms
|
||
3 years ago
|
code := make([]byte, analysisCodeSize)
|
||
|
bench.SetBytes(analysisCodeSize)
|
||
6 years ago
|
bench.ResetTimer()
|
||
|
for i := 0; i < bench.N; i++ {
|
||
|
codeBitmap(code)
|
||
|
}
|
||
|
bench.StopTimer()
|
||
|
}
|
||
|
func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
|
||
|
// 4 ms
|
||
3 years ago
|
code := make([]byte, analysisCodeSize)
|
||
|
bench.SetBytes(analysisCodeSize)
|
||
6 years ago
|
bench.ResetTimer()
|
||
|
for i := 0; i < bench.N; i++ {
|
||
|
crypto.Keccak256Hash(code)
|
||
|
}
|
||
|
bench.StopTimer()
|
||
8 years ago
|
}
|
||
3 years ago
|
|
||
|
func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
|
||
|
var op OpCode
|
||
|
bencher := func(b *testing.B) {
|
||
3 years ago
|
code := make([]byte, analysisCodeSize)
|
||
|
b.SetBytes(analysisCodeSize)
|
||
3 years ago
|
for i := range code {
|
||
|
code[i] = byte(op)
|
||
|
}
|
||
|
bits := make(bitvec, len(code)/8+1+4)
|
||
|
b.ResetTimer()
|
||
3 years ago
|
for i := 0; i < b.N; i++ {
|
||
8 months ago
|
clear(bits)
|
||
3 years ago
|
codeBitmapInternal(code, bits)
|
||
|
}
|
||
3 years ago
|
}
|
||
|
for op = PUSH1; op <= PUSH32; op++ {
|
||
|
bench.Run(op.String(), bencher)
|
||
|
}
|
||
|
op = JUMPDEST
|
||
|
bench.Run(op.String(), bencher)
|
||
|
op = STOP
|
||
|
bench.Run(op.String(), bencher)
|
||
|
}
|
||
2 months ago
|
|
||
|
func BenchmarkJumpdestOpEOFAnalysis(bench *testing.B) {
|
||
|
var op OpCode
|
||
|
bencher := func(b *testing.B) {
|
||
|
code := make([]byte, analysisCodeSize)
|
||
|
b.SetBytes(analysisCodeSize)
|
||
|
for i := range code {
|
||
|
code[i] = byte(op)
|
||
|
}
|
||
|
bits := make(bitvec, len(code)/8+1+4)
|
||
|
b.ResetTimer()
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
clear(bits)
|
||
|
eofCodeBitmapInternal(code, bits)
|
||
|
}
|
||
|
}
|
||
|
for op = PUSH1; op <= PUSH32; op++ {
|
||
|
bench.Run(op.String(), bencher)
|
||
|
}
|
||
|
op = JUMPDEST
|
||
|
bench.Run(op.String(), bencher)
|
||
|
op = STOP
|
||
|
bench.Run(op.String(), bencher)
|
||
|
op = RJUMPV
|
||
|
bench.Run(op.String(), bencher)
|
||
|
op = EOFCREATE
|
||
|
bench.Run(op.String(), bencher)
|
||
|
}
|