Skip to content

Commit ea2718c

Browse files
committed
core/vm: improve JUMPDEST analysis
* JUMPDEST analysis is faster because less type conversions are performed. * The map of JUMPDEST locations is now created lazily at the first JUMP. * The result of the analysis is kept around for recursive invocations through CALL/CALLCODE. Fixes bnb-chain#1147
1 parent b4818a0 commit ea2718c

3 files changed

Lines changed: 44 additions & 25 deletions

File tree

‎core/vm/analysis.go‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,45 @@ package vm
33
import (
44
"math/big"
55

6-
"gopkg.in/fatih/set.v0"
6+
"github.com/ethereum/go-ethereum/common"
77
)
88

9-
type destinations struct {
10-
set *set.Set
11-
}
9+
var bigMaxUint64 = new(big.Int).SetUint64(^uint64(0))
1210

13-
func (d *destinations) Has(dest *big.Int) bool {
14-
return d.set.Has(string(dest.Bytes()))
15-
}
11+
// destinations stores one map per contract (keyed by hash of code).
12+
// The maps contain an entry for each location of a JUMPDEST
13+
// instruction.
14+
type destinations map[common.Hash]map[uint64]struct{}
1615

17-
func (d *destinations) Add(dest *big.Int) {
18-
d.set.Add(string(dest.Bytes()))
16+
// has checks whether code has a JUMPDEST at dest.
17+
func (d destinations) has(codehash common.Hash, code []byte, dest *big.Int) bool {
18+
// PC cannot go beyond len(code) and certainly can't be bigger than 64bits.
19+
// Don't bother checking for JUMPDEST in that case.
20+
if dest.Cmp(bigMaxUint64) > 0 {
21+
return false
22+
}
23+
m, analysed := d[codehash]
24+
if !analysed {
25+
m = jumpdests(code)
26+
d[codehash] = m
27+
}
28+
_, ok := m[dest.Uint64()]
29+
return ok
1930
}
2031

21-
func analyseJumpDests(code []byte) (dests *destinations) {
22-
dests = &destinations{set.New()}
23-
32+
// jumpdests creates a map that contains an entry for each
33+
// PC location that is a JUMPDEST instruction.
34+
func jumpdests(code []byte) map[uint64]struct{} {
35+
m := make(map[uint64]struct{})
2436
for pc := uint64(0); pc < uint64(len(code)); pc++ {
2537
var op OpCode = OpCode(code[pc])
2638
switch op {
2739
case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
2840
a := uint64(op) - uint64(PUSH1) + 1
29-
3041
pc += a
3142
case JUMPDEST:
32-
dests.Add(big.NewInt(int64(pc)))
43+
m[pc] = struct{}{}
3344
}
3445
}
35-
return
46+
return m
3647
}

‎core/vm/context.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type Context struct {
1616
caller ContextRef
1717
self ContextRef
1818

19+
jumpdests destinations // result of JUMPDEST analysis.
20+
1921
Code []byte
2022
CodeAddr *common.Address
2123

@@ -24,10 +26,17 @@ type Context struct {
2426
Args []byte
2527
}
2628

27-
// Create a new context for the given data items
29+
// Create a new context for the given data items.
2830
func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int) *Context {
2931
c := &Context{caller: caller, self: object, Args: nil}
3032

33+
if parent, ok := caller.(*Context); ok {
34+
// Reuse JUMPDEST analysis from parent context if available.
35+
c.jumpdests = parent.jumpdests
36+
} else {
37+
c.jumpdests = make(destinations)
38+
}
39+
3140
// Gas should be a pointer so it can safely be reduced through the run
3241
// This pointer will be off the state transition
3342
c.Gas = gas //new(big.Int).Set(gas)

‎core/vm/vm.go‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,16 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
7272
}
7373

7474
var (
75-
op OpCode
76-
77-
destinations = analyseJumpDests(context.Code)
78-
mem = NewMemory()
79-
stack = newStack()
80-
pc = new(big.Int)
81-
statedb = self.env.State()
75+
op OpCode
76+
codehash = crypto.Sha3Hash(code)
77+
mem = NewMemory()
78+
stack = newStack()
79+
pc = new(big.Int)
80+
statedb = self.env.State()
8281

8382
jump = func(from *big.Int, to *big.Int) error {
84-
nop := context.GetOp(to)
85-
if !destinations.Has(to) {
83+
if !context.jumpdests.has(codehash, code, to) {
84+
nop := context.GetOp(to)
8685
return fmt.Errorf("invalid jump destination (%v) %v", nop, to)
8786
}
8887

0 commit comments

Comments
 (0)