2015-07-06 17:54:22 -07:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 09:48:40 -07:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 17:54:22 -07:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 09:48:40 -07:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 17:54:22 -07:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 09:48:40 -07:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 17:54:22 -07:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 09:48:40 -07:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 17:54:22 -07:00
|
|
|
|
2014-11-04 09:17:38 -08:00
|
|
|
package vm
|
|
|
|
|
2015-03-28 12:30:16 -07:00
|
|
|
import (
|
|
|
|
"math/big"
|
2014-11-04 09:17:38 -08:00
|
|
|
|
2015-05-29 05:40:45 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-28 12:30:16 -07:00
|
|
|
)
|
|
|
|
|
2015-05-29 05:40:45 -07:00
|
|
|
var bigMaxUint64 = new(big.Int).SetUint64(^uint64(0))
|
2015-03-28 12:30:16 -07:00
|
|
|
|
2015-05-29 05:40:45 -07: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 12:30:16 -07:00
|
|
|
|
2015-05-29 05:40:45 -07: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 12:30:16 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 05:40:45 -07: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 01:53:49 -08:00
|
|
|
for pc := uint64(0); pc < uint64(len(code)); pc++ {
|
2014-11-04 09:17:38 -08: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 01:53:49 -08:00
|
|
|
a := uint64(op) - uint64(PUSH1) + 1
|
2014-11-04 09:17:38 -08:00
|
|
|
pc += a
|
2015-01-02 08:35:55 -08:00
|
|
|
case JUMPDEST:
|
2015-05-29 05:40:45 -07:00
|
|
|
m[pc] = struct{}{}
|
2014-11-04 09:17:38 -08:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 05:40:45 -07:00
|
|
|
return m
|
2014-11-04 09:17:38 -08:00
|
|
|
}
|