Skip to content

Commit 122d2db

Browse files
committed
Merge pull request bnb-chain#1150 from fjl/fix-jumpdest
core/vm: improve JUMPDEST analysis
2 parents 0cd7236 + c9ed9d2 commit 122d2db

4 files changed

Lines changed: 173 additions & 30 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: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,22 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
7171
}
7272
}
7373

74-
var (
75-
op OpCode
74+
// Don't bother with the execution if there's no code.
75+
if len(code) == 0 {
76+
return context.Return(nil), nil
77+
}
7678

77-
destinations = analyseJumpDests(context.Code)
78-
mem = NewMemory()
79-
stack = newStack()
80-
pc = new(big.Int)
81-
statedb = self.env.State()
79+
var (
80+
op OpCode
81+
codehash = crypto.Sha3Hash(code)
82+
mem = NewMemory()
83+
stack = newStack()
84+
pc = new(big.Int)
85+
statedb = self.env.State()
8286

8387
jump = func(from *big.Int, to *big.Int) error {
84-
nop := context.GetOp(to)
85-
if !destinations.Has(to) {
88+
if !context.jumpdests.has(codehash, code, to) {
89+
nop := context.GetOp(to)
8690
return fmt.Errorf("invalid jump destination (%v) %v", nop, to)
8791
}
8892

@@ -95,11 +99,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
9599
}
96100
)
97101

98-
// Don't bother with the execution if there's no code.
99-
if len(code) == 0 {
100-
return context.Return(nil), nil
101-
}
102-
103102
for {
104103
// The base for all big integer arithmetic
105104
base := new(big.Int)

‎tests/files/StateTests/stSpecialTest.json‎

Lines changed: 124 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)