Skip to content

Commit 1d99573

Browse files
authored
core/vm: faster code analysis (ethereum#23381)
* core/vm: more detailed benchmark for jumpdest analysis * core/vm: make jd analysis benchmark alloc free * core/vm: improve jumpdest analysis * core/vm: improve worst-case * core/vm: further improvements in analysis * core/vm: improve jumpdest analysis >PUSH15 * core/vm: make jd analysis ref by value * core/vm: fix misspell * core/vm: improve set8 and set16 a bit * core/vm: reduce amount of code * core/vm: optimize byte copying
1 parent f38abc5 commit 1d99573

File tree

4 files changed

+105
-19
lines changed

4 files changed

+105
-19
lines changed

core/vm/analysis.go

+78-16
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,49 @@
1616

1717
package vm
1818

19+
const (
20+
set2BitsMask = uint16(0b1100_0000_0000_0000)
21+
set3BitsMask = uint16(0b1110_0000_0000_0000)
22+
set4BitsMask = uint16(0b1111_0000_0000_0000)
23+
set5BitsMask = uint16(0b1111_1000_0000_0000)
24+
set6BitsMask = uint16(0b1111_1100_0000_0000)
25+
set7BitsMask = uint16(0b1111_1110_0000_0000)
26+
)
27+
1928
// bitvec is a bit vector which maps bytes in a program.
2029
// An unset bit means the byte is an opcode, a set bit means
2130
// it's data (i.e. argument of PUSHxx).
2231
type bitvec []byte
2332

24-
func (bits *bitvec) set(pos uint64) {
25-
(*bits)[pos/8] |= 0x80 >> (pos % 8)
33+
var lookup = [8]byte{
34+
0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1,
35+
}
36+
37+
func (bits bitvec) set1(pos uint64) {
38+
bits[pos/8] |= lookup[pos%8]
39+
}
40+
41+
func (bits bitvec) setN(flag uint16, pos uint64) {
42+
a := flag >> (pos % 8)
43+
bits[pos/8] |= byte(a >> 8)
44+
if b := byte(a); b != 0 {
45+
// If the bit-setting affects the neighbouring byte, we can assign - no need to OR it,
46+
// since it's the first write to that byte
47+
bits[pos/8+1] = b
48+
}
49+
}
50+
51+
func (bits bitvec) set8(pos uint64) {
52+
a := byte(0xFF >> (pos % 8))
53+
bits[pos/8] |= a
54+
bits[pos/8+1] = ^a
2655
}
27-
func (bits *bitvec) set8(pos uint64) {
28-
(*bits)[pos/8] |= 0xFF >> (pos % 8)
29-
(*bits)[pos/8+1] |= ^(0xFF >> (pos % 8))
56+
57+
func (bits bitvec) set16(pos uint64) {
58+
a := byte(0xFF >> (pos % 8))
59+
bits[pos/8] |= a
60+
bits[pos/8+1] = 0xFF
61+
bits[pos/8+2] = ^a
3062
}
3163

3264
// codeSegment checks if the position is in a code segment.
@@ -40,22 +72,52 @@ func codeBitmap(code []byte) bitvec {
4072
// ends with a PUSH32, the algorithm will push zeroes onto the
4173
// bitvector outside the bounds of the actual code.
4274
bits := make(bitvec, len(code)/8+1+4)
75+
return codeBitmapInternal(code, bits)
76+
}
77+
78+
// codeBitmapInternal is the internal implementation of codeBitmap.
79+
// It exists for the purpose of being able to run benchmark tests
80+
// without dynamic allocations affecting the results.
81+
func codeBitmapInternal(code, bits bitvec) bitvec {
4382
for pc := uint64(0); pc < uint64(len(code)); {
4483
op := OpCode(code[pc])
45-
46-
if op >= PUSH1 && op <= PUSH32 {
47-
numbits := op - PUSH1 + 1
48-
pc++
84+
pc++
85+
if op < PUSH1 || op > PUSH32 {
86+
continue
87+
}
88+
numbits := op - PUSH1 + 1
89+
if numbits >= 8 {
90+
for ; numbits >= 16; numbits -= 16 {
91+
bits.set16(pc)
92+
pc += 16
93+
}
4994
for ; numbits >= 8; numbits -= 8 {
50-
bits.set8(pc) // 8
95+
bits.set8(pc)
5196
pc += 8
5297
}
53-
for ; numbits > 0; numbits-- {
54-
bits.set(pc)
55-
pc++
56-
}
57-
} else {
58-
pc++
98+
}
99+
switch numbits {
100+
case 1:
101+
bits.set1(pc)
102+
pc += 1
103+
case 2:
104+
bits.setN(set2BitsMask, pc)
105+
pc += 2
106+
case 3:
107+
bits.setN(set3BitsMask, pc)
108+
pc += 3
109+
case 4:
110+
bits.setN(set4BitsMask, pc)
111+
pc += 4
112+
case 5:
113+
bits.setN(set5BitsMask, pc)
114+
pc += 5
115+
case 6:
116+
bits.setN(set6BitsMask, pc)
117+
pc += 6
118+
case 7:
119+
bits.setN(set7BitsMask, pc)
120+
pc += 7
59121
}
60122
}
61123
return bits

core/vm/analysis_test.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func TestJumpDestAnalysis(t *testing.T) {
4747
{[]byte{byte(PUSH32)}, 0xFF, 1},
4848
{[]byte{byte(PUSH32)}, 0xFF, 2},
4949
}
50-
for _, test := range tests {
50+
for i, test := range tests {
5151
ret := codeBitmap(test.code)
5252
if ret[test.which] != test.exp {
53-
t.Fatalf("expected %x, got %02x", test.exp, ret[test.which])
53+
t.Fatalf("test %d: expected %x, got %02x", i, test.exp, ret[test.which])
5454
}
5555
}
5656
}
@@ -73,3 +73,23 @@ func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
7373
}
7474
bench.StopTimer()
7575
}
76+
77+
func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
78+
var op OpCode
79+
bencher := func(b *testing.B) {
80+
code := make([]byte, 32*b.N)
81+
for i := range code {
82+
code[i] = byte(op)
83+
}
84+
bits := make(bitvec, len(code)/8+1+4)
85+
b.ResetTimer()
86+
codeBitmapInternal(code, bits)
87+
}
88+
for op = PUSH1; op <= PUSH32; op++ {
89+
bench.Run(op.String(), bencher)
90+
}
91+
op = JUMPDEST
92+
bench.Run(op.String(), bencher)
93+
op = STOP
94+
bench.Run(op.String(), bencher)
95+
}

core/vm/instructions.go

+4
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
669669
}
670670
stack.push(&temp)
671671
if err == nil || err == ErrExecutionReverted {
672+
ret = common.CopyBytes(ret)
672673
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
673674
}
674675
scope.Contract.Gas += returnGas
@@ -703,6 +704,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
703704
}
704705
stack.push(&temp)
705706
if err == nil || err == ErrExecutionReverted {
707+
ret = common.CopyBytes(ret)
706708
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
707709
}
708710
scope.Contract.Gas += returnGas
@@ -730,6 +732,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
730732
}
731733
stack.push(&temp)
732734
if err == nil || err == ErrExecutionReverted {
735+
ret = common.CopyBytes(ret)
733736
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
734737
}
735738
scope.Contract.Gas += returnGas
@@ -757,6 +760,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
757760
}
758761
stack.push(&temp)
759762
if err == nil || err == ErrExecutionReverted {
763+
ret = common.CopyBytes(ret)
760764
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
761765
}
762766
scope.Contract.Gas += returnGas

core/vm/interpreter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
262262
// if the operation clears the return data (e.g. it has returning data)
263263
// set the last return to the result of the operation.
264264
if operation.returns {
265-
in.returnData = common.CopyBytes(res)
265+
in.returnData = res
266266
}
267267

268268
switch {

0 commit comments

Comments
 (0)