2017-11-14 00:04:53 +03:00
|
|
|
// 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/>.
|
|
|
|
|
2017-06-03 20:01:22 +03:00
|
|
|
package vm
|
|
|
|
|
2018-10-04 18:15:37 +03:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
2017-06-03 20:01:22 +03:00
|
|
|
|
|
|
|
func TestJumpDestAnalysis(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
code []byte
|
|
|
|
exp byte
|
|
|
|
which int
|
|
|
|
}{
|
|
|
|
{[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0x40, 0},
|
|
|
|
{[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0x50, 0},
|
|
|
|
{[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, 0x7F, 0},
|
|
|
|
{[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 1},
|
|
|
|
{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0x03, 0},
|
|
|
|
{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
|
|
|
|
{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x74, 0},
|
|
|
|
{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
|
|
|
|
{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x3F, 0},
|
|
|
|
{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xC0, 1},
|
|
|
|
{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x7F, 0},
|
|
|
|
{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xFF, 1},
|
|
|
|
{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 2},
|
|
|
|
{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0x7f, 0},
|
|
|
|
{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0xA0, 1},
|
|
|
|
{[]byte{byte(PUSH32)}, 0x7F, 0},
|
|
|
|
{[]byte{byte(PUSH32)}, 0xFF, 1},
|
|
|
|
{[]byte{byte(PUSH32)}, 0xFF, 2},
|
|
|
|
}
|
2021-08-24 10:00:10 +03:00
|
|
|
for i, test := range tests {
|
2017-09-08 13:47:44 +03:00
|
|
|
ret := codeBitmap(test.code)
|
2017-06-03 20:01:22 +03:00
|
|
|
if ret[test.which] != test.exp {
|
2021-08-24 10:00:10 +03:00
|
|
|
t.Fatalf("test %d: expected %x, got %02x", i, test.exp, ret[test.which])
|
2017-06-03 20:01:22 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 18:15:37 +03:00
|
|
|
}
|
2017-06-03 20:01:22 +03:00
|
|
|
|
2021-08-30 15:13:06 +03:00
|
|
|
const analysisCodeSize = 1200 * 1024
|
|
|
|
|
2018-10-04 18:15:37 +03:00
|
|
|
func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
|
|
|
|
// 1.4 ms
|
2021-08-30 15:13:06 +03:00
|
|
|
code := make([]byte, analysisCodeSize)
|
|
|
|
bench.SetBytes(analysisCodeSize)
|
2018-10-04 18:15:37 +03:00
|
|
|
bench.ResetTimer()
|
|
|
|
for i := 0; i < bench.N; i++ {
|
|
|
|
codeBitmap(code)
|
|
|
|
}
|
|
|
|
bench.StopTimer()
|
|
|
|
}
|
|
|
|
func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
|
|
|
|
// 4 ms
|
2021-08-30 15:13:06 +03:00
|
|
|
code := make([]byte, analysisCodeSize)
|
|
|
|
bench.SetBytes(analysisCodeSize)
|
2018-10-04 18:15:37 +03:00
|
|
|
bench.ResetTimer()
|
|
|
|
for i := 0; i < bench.N; i++ {
|
|
|
|
crypto.Keccak256Hash(code)
|
|
|
|
}
|
|
|
|
bench.StopTimer()
|
2017-06-03 20:01:22 +03:00
|
|
|
}
|
2021-08-24 10:00:10 +03:00
|
|
|
|
|
|
|
func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
|
|
|
|
var op OpCode
|
|
|
|
bencher := func(b *testing.B) {
|
2021-08-30 15:13:06 +03:00
|
|
|
code := make([]byte, analysisCodeSize)
|
|
|
|
b.SetBytes(analysisCodeSize)
|
2021-08-24 10:00:10 +03:00
|
|
|
for i := range code {
|
|
|
|
code[i] = byte(op)
|
|
|
|
}
|
|
|
|
bits := make(bitvec, len(code)/8+1+4)
|
|
|
|
b.ResetTimer()
|
2021-08-30 15:13:06 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for j := range bits {
|
|
|
|
bits[j] = 0
|
|
|
|
}
|
|
|
|
codeBitmapInternal(code, bits)
|
|
|
|
}
|
2021-08-24 10:00:10 +03:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|