2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum 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.
|
|
|
|
//
|
|
|
|
// go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-11-04 19:17:38 +02:00
|
|
|
package vm
|
|
|
|
|
2015-03-28 21:30:16 +02:00
|
|
|
import (
|
|
|
|
"math/big"
|
2014-11-04 19:17:38 +02:00
|
|
|
|
2015-05-29 15:40:45 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-28 21:30:16 +02:00
|
|
|
)
|
|
|
|
|
2015-05-29 15:40:45 +03:00
|
|
|
var bigMaxUint64 = new(big.Int).SetUint64(^uint64(0))
|
2015-03-28 21:30:16 +02:00
|
|
|
|
2015-05-29 15:40:45 +03:00
|
|
|
// destinations stores one map per contract (keyed by hash of code).
|
|
|
|
// The maps contain an entry for each location of a JUMPDEST
|
|
|
|
// instruction.
|
|
|
|
type destinations map[common.Hash]map[uint64]struct{}
|
2015-03-28 21:30:16 +02:00
|
|
|
|
2015-05-29 15:40:45 +03:00
|
|
|
// has checks whether code has a JUMPDEST at dest.
|
|
|
|
func (d destinations) has(codehash common.Hash, code []byte, dest *big.Int) bool {
|
|
|
|
// PC cannot go beyond len(code) and certainly can't be bigger than 64bits.
|
|
|
|
// Don't bother checking for JUMPDEST in that case.
|
|
|
|
if dest.Cmp(bigMaxUint64) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m, analysed := d[codehash]
|
|
|
|
if !analysed {
|
|
|
|
m = jumpdests(code)
|
|
|
|
d[codehash] = m
|
|
|
|
}
|
|
|
|
_, ok := m[dest.Uint64()]
|
|
|
|
return ok
|
2015-03-28 21:30:16 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 15:40:45 +03:00
|
|
|
// jumpdests creates a map that contains an entry for each
|
|
|
|
// PC location that is a JUMPDEST instruction.
|
|
|
|
func jumpdests(code []byte) map[uint64]struct{} {
|
|
|
|
m := make(map[uint64]struct{})
|
2014-12-04 11:53:49 +02:00
|
|
|
for pc := uint64(0); pc < uint64(len(code)); pc++ {
|
2014-11-04 19:17:38 +02:00
|
|
|
var op OpCode = OpCode(code[pc])
|
|
|
|
switch op {
|
|
|
|
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:
|
2014-12-04 11:53:49 +02:00
|
|
|
a := uint64(op) - uint64(PUSH1) + 1
|
2014-11-04 19:17:38 +02:00
|
|
|
pc += a
|
2015-01-02 18:35:55 +02:00
|
|
|
case JUMPDEST:
|
2015-05-29 15:40:45 +03:00
|
|
|
m[pc] = struct{}{}
|
2014-11-04 19:17:38 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 15:40:45 +03:00
|
|
|
return m
|
2014-11-04 19:17:38 +02:00
|
|
|
}
|