Commit 846f34f7 authored by Jeffrey Wilcke's avatar Jeffrey Wilcke

core/vm, tests: implemented semi-jit vm

* changed stack and removed stack ptr. Let go decide on slice reuse.
parent 698e98d9
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# with Go source code. If you know what GOPATH is then you probably # with Go source code. If you know what GOPATH is then you probably
# don't need to bother with make. # don't need to bother with make.
.PHONY: geth mist all test travis-test-with-coverage clean .PHONY: geth evm mist all test travis-test-with-coverage clean
GOBIN = build/bin GOBIN = build/bin
geth: geth:
...@@ -10,6 +10,10 @@ geth: ...@@ -10,6 +10,10 @@ geth:
@echo "Done building." @echo "Done building."
@echo "Run \"$(GOBIN)/geth\" to launch geth." @echo "Run \"$(GOBIN)/geth\" to launch geth."
evm:
build/env.sh $(GOROOT)/bin/go install -v $(shell build/ldflags.sh) ./cmd/evm
@echo "Done building."
@echo "Run \"$(GOBIN)/evm to start the evm."
mist: mist:
build/env.sh go install -v $(shell build/ldflags.sh) ./cmd/mist build/env.sh go install -v $(shell build/ldflags.sh) ./cmd/mist
@echo "Done building." @echo "Done building."
......
...@@ -32,6 +32,7 @@ import ( ...@@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger/glog"
) )
var ( var (
...@@ -40,6 +41,14 @@ var ( ...@@ -40,6 +41,14 @@ var (
Name: "debug", Name: "debug",
Usage: "output full trace logs", Usage: "output full trace logs",
} }
ForceJitFlag = cli.BoolFlag{
Name: "forcejit",
Usage: "forces jit compilation",
}
DisableJitFlag = cli.BoolFlag{
Name: "nojit",
Usage: "disabled jit compilation",
}
CodeFlag = cli.StringFlag{ CodeFlag = cli.StringFlag{
Name: "code", Name: "code",
Usage: "EVM code", Usage: "EVM code",
...@@ -77,6 +86,8 @@ func init() { ...@@ -77,6 +86,8 @@ func init() {
app = utils.NewApp("0.2", "the evm command line interface") app = utils.NewApp("0.2", "the evm command line interface")
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
DebugFlag, DebugFlag,
ForceJitFlag,
DisableJitFlag,
SysStatFlag, SysStatFlag,
CodeFlag, CodeFlag,
GasFlag, GasFlag,
...@@ -90,6 +101,10 @@ func init() { ...@@ -90,6 +101,10 @@ func init() {
func run(ctx *cli.Context) { func run(ctx *cli.Context) {
vm.Debug = ctx.GlobalBool(DebugFlag.Name) vm.Debug = ctx.GlobalBool(DebugFlag.Name)
vm.ForceJit = ctx.GlobalBool(ForceJitFlag.Name)
vm.DisableJit = ctx.GlobalBool(DisableJitFlag.Name)
glog.SetToStderr(true)
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
statedb := state.New(common.Hash{}, db) statedb := state.New(common.Hash{}, db)
...@@ -110,11 +125,6 @@ func run(ctx *cli.Context) { ...@@ -110,11 +125,6 @@ func run(ctx *cli.Context) {
) )
vmdone := time.Since(tstart) vmdone := time.Since(tstart)
if e != nil {
fmt.Println(e)
os.Exit(1)
}
if ctx.GlobalBool(DumpFlag.Name) { if ctx.GlobalBool(DumpFlag.Name) {
fmt.Println(string(statedb.Dump())) fmt.Println(string(statedb.Dump()))
} }
...@@ -133,7 +143,11 @@ num gc: %d ...@@ -133,7 +143,11 @@ num gc: %d
`, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC) `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
} }
fmt.Printf("OUT: 0x%x\n", ret) fmt.Printf("OUT: 0x%x", ret)
if e != nil {
fmt.Printf(" error: %v", e)
}
fmt.Println()
} }
func main() { func main() {
......
...@@ -307,6 +307,9 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso ...@@ -307,6 +307,9 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.ExecFlag, utils.ExecFlag,
utils.WhisperEnabledFlag, utils.WhisperEnabledFlag,
utils.VMDebugFlag, utils.VMDebugFlag,
utils.VMForceJitFlag,
utils.VMJitCacheFlag,
utils.VMEnableJitFlag,
utils.NetworkIdFlag, utils.NetworkIdFlag,
utils.RPCCORSDomainFlag, utils.RPCCORSDomainFlag,
utils.VerbosityFlag, utils.VerbosityFlag,
......
...@@ -27,6 +27,7 @@ import ( ...@@ -27,6 +27,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
...@@ -172,6 +173,25 @@ var ( ...@@ -172,6 +173,25 @@ var (
Value: "", Value: "",
} }
// vm flags
VMDebugFlag = cli.BoolFlag{
Name: "vmdebug",
Usage: "Virtual Machine debug output",
}
VMForceJitFlag = cli.BoolFlag{
Name: "forcejit",
Usage: "Force the JIT VM to take precedence",
}
VMJitCacheFlag = cli.IntFlag{
Name: "jitcache",
Usage: "Amount of cached JIT VM programs",
Value: 64,
}
VMEnableJitFlag = cli.BoolFlag{
Name: "jitvm",
Usage: "Enable the JIT VM",
}
// logging and debug settings // logging and debug settings
LogFileFlag = cli.StringFlag{ LogFileFlag = cli.StringFlag{
Name: "logfile", Name: "logfile",
...@@ -196,10 +216,6 @@ var ( ...@@ -196,10 +216,6 @@ var (
Usage: "The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the \".go\" suffix) or \"glob\" pattern and N is a log verbosity level.", Usage: "The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the \".go\" suffix) or \"glob\" pattern and N is a log verbosity level.",
Value: glog.GetVModule(), Value: glog.GetVModule(),
} }
VMDebugFlag = cli.BoolFlag{
Name: "vmdebug",
Usage: "Virtual Machine debug output",
}
BacktraceAtFlag = cli.GenericFlag{ BacktraceAtFlag = cli.GenericFlag{
Name: "backtrace_at", Name: "backtrace_at",
Usage: "If set to a file and line number (e.g., \"block.go:271\") holding a logging statement, a stack trace will be logged", Usage: "If set to a file and line number (e.g., \"block.go:271\") holding a logging statement, a stack trace will be logged",
...@@ -434,6 +450,13 @@ func SetupLogger(ctx *cli.Context) { ...@@ -434,6 +450,13 @@ func SetupLogger(ctx *cli.Context) {
glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name)) glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
} }
// SetupVM configured the VM package's global settings
func SetupVM(ctx *cli.Context) {
vm.DisableJit = !ctx.GlobalBool(VMEnableJitFlag.Name)
vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
}
// MakeChain creates a chain manager from set command line flags. // MakeChain creates a chain manager from set command line flags.
func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, extraDB common.Database) { func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, extraDB common.Database) {
datadir := ctx.GlobalString(DataDirFlag.Name) datadir := ctx.GlobalString(DataDirFlag.Name)
......
...@@ -84,8 +84,6 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block ...@@ -84,8 +84,6 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block
} }
func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) { func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
// If we are mining this block and validating we want to set the logs back to 0
cb := statedb.GetStateObject(coinbase.Address()) cb := statedb.GetStateObject(coinbase.Address())
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb) _, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
if err != nil { if err != nil {
......
...@@ -632,7 +632,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { ...@@ -632,7 +632,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
switch status { switch status {
case CanonStatTy: case CanonStatTy:
if glog.V(logger.Debug) { if glog.V(logger.Debug) {
glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) glog.Infof("[%v] inserted block #%d (%d TXs %v G %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), block.GasUsed(), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
} }
queue[i] = ChainEvent{block, block.Hash(), logs} queue[i] = ChainEvent{block, block.Hash(), logs}
queueEvent.canonicalCount++ queueEvent.canonicalCount++
......
...@@ -35,6 +35,7 @@ type Context struct { ...@@ -35,6 +35,7 @@ type Context struct {
jumpdests destinations // result of JUMPDEST analysis. jumpdests destinations // result of JUMPDEST analysis.
Code []byte Code []byte
Input []byte
CodeAddr *common.Address CodeAddr *common.Address
value, Gas, UsedGas, Price *big.Int value, Gas, UsedGas, Price *big.Int
......
...@@ -94,7 +94,7 @@ func ecrecoverFunc(in []byte) []byte { ...@@ -94,7 +94,7 @@ func ecrecoverFunc(in []byte) []byte {
v := byte(vbig.Uint64()) v := byte(vbig.Uint64())
if !crypto.ValidateSignatureValues(v, r, s) { if !crypto.ValidateSignatureValues(v, r, s) {
glog.V(logger.Error).Infof("EC RECOVER FAIL: v, r or s value invalid") glog.V(logger.Debug).Infof("EC RECOVER FAIL: v, r or s value invalid")
return nil return nil
} }
......
...@@ -54,8 +54,8 @@ func baseCheck(op OpCode, stack *stack, gas *big.Int) error { ...@@ -54,8 +54,8 @@ func baseCheck(op OpCode, stack *stack, gas *big.Int) error {
return err return err
} }
if r.stackPush > 0 && len(stack.data)-r.stackPop+r.stackPush > int(params.StackLimit.Int64())+1 { if r.stackPush > 0 && stack.len()-r.stackPop+r.stackPush > int(params.StackLimit.Int64()) {
return fmt.Errorf("stack limit reached %d (%d)", len(stack.data), params.StackLimit.Int64()) return fmt.Errorf("stack limit reached %d (%d)", stack.len(), params.StackLimit.Int64())
} }
gas.Add(gas, r.gas) gas.Add(gas, r.gas)
......
This diff is collapsed.
This diff is collapsed.
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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.
//
// The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package vm
import (
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
)
const maxRun = 1000
type vmBench struct {
precompile bool // compile prior to executing
nojit bool // ignore jit (sets DisbaleJit = true
forcejit bool // forces the jit, precompile is ignored
code []byte
input []byte
}
func runVmBench(test vmBench, b *testing.B) {
db, _ := ethdb.NewMemDatabase()
sender := state.NewStateObject(common.Address{}, db)
if test.precompile && !test.forcejit {
NewProgram(test.code)
}
env := NewEnv()
DisableJit = test.nojit
ForceJit = test.forcejit
b.ResetTimer()
for i := 0; i < b.N; i++ {
context := NewContext(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
context.Code = test.code
context.CodeAddr = &common.Address{}
_, err := New(env).Run(context, test.input)
if err != nil {
b.Error(err)
b.FailNow()
}
}
}
var benchmarks = map[string]vmBench{
"pushes": vmBench{
false, false, false,
common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil,
},
}
func BenchmarkPushes(b *testing.B) {
runVmBench(benchmarks["pushes"], b)
}
type Env struct {
gasLimit *big.Int
depth int
}
func NewEnv() *Env {
return &Env{big.NewInt(10000), 0}
}
func (self *Env) Origin() common.Address { return common.Address{} }
func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
func (self *Env) AddStructLog(log StructLog) {
}
func (self *Env) StructLogs() []StructLog {
return nil
}
//func (self *Env) PrevHash() []byte { return self.parent }
func (self *Env) Coinbase() common.Address { return common.Address{} }
func (self *Env) Time() uint64 { return uint64(time.Now().Unix()) }
func (self *Env) Difficulty() *big.Int { return big.NewInt(0) }
func (self *Env) State() *state.StateDB { return nil }
func (self *Env) GasLimit() *big.Int { return self.gasLimit }
func (self *Env) VmType() Type { return StdVmTy }
func (self *Env) GetHash(n uint64) common.Hash {
return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
}
func (self *Env) AddLog(log *state.Log) {
}
func (self *Env) Depth() int { return self.depth }
func (self *Env) SetDepth(i int) { self.depth = i }
func (self *Env) Transfer(from, to Account, amount *big.Int) error {
return nil
}
func (self *Env) Call(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
return nil, nil
}
func (self *Env) CallCode(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
return nil, nil
}
func (self *Env) Create(caller ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef) {
return nil, nil, nil
}
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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.
//
// The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package vm
var (
DisableJit bool // Disable the JIT VM
ForceJit bool // Force the JIT, skip byte VM
MaxProgSize int // Max cache size for JIT Programs
)
const defaultJitMaxCache int = 64
...@@ -27,32 +27,27 @@ func newstack() *stack { ...@@ -27,32 +27,27 @@ func newstack() *stack {
type stack struct { type stack struct {
data []*big.Int data []*big.Int
ptr int
} }
func (st *stack) Data() []*big.Int { func (st *stack) Data() []*big.Int {
return st.data[:st.ptr] return st.data
} }
func (st *stack) push(d *big.Int) { func (st *stack) push(d *big.Int) {
// NOTE push limit (1024) is checked in baseCheck // NOTE push limit (1024) is checked in baseCheck
stackItem := new(big.Int).Set(d) //stackItem := new(big.Int).Set(d)
if len(st.data) > st.ptr { //st.data = append(st.data, stackItem)
st.data[st.ptr] = stackItem st.data = append(st.data, d)
} else {
st.data = append(st.data, stackItem)
}
st.ptr++
} }
func (st *stack) pop() (ret *big.Int) { func (st *stack) pop() (ret *big.Int) {
st.ptr-- ret = st.data[len(st.data)-1]
ret = st.data[st.ptr] st.data = st.data[:len(st.data)-1]
return return
} }
func (st *stack) len() int { func (st *stack) len() int {
return st.ptr return len(st.data)
} }
func (st *stack) swap(n int) { func (st *stack) swap(n int) {
...@@ -60,7 +55,7 @@ func (st *stack) swap(n int) { ...@@ -60,7 +55,7 @@ func (st *stack) swap(n int) {
} }
func (st *stack) dup(n int) { func (st *stack) dup(n int) {
st.push(st.data[st.len()-n]) st.push(new(big.Int).Set(st.data[st.len()-n]))
} }
func (st *stack) peek() *big.Int { func (st *stack) peek() *big.Int {
......
...@@ -24,30 +24,19 @@ import ( ...@@ -24,30 +24,19 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
// Vm implements VirtualMachine // Vm implements VirtualMachine
type Vm struct { type Vm struct {
env Environment env Environment
err error
// For logging
debug bool
BreakPoints []int64
Stepping bool
Fn string
Recoverable bool
// Will be called before the vm returns
After func(*Context, error)
} }
// New returns a new Virtual Machine // New returns a new Virtual Machine
func New(env Environment) *Vm { func New(env Environment) *Vm {
return &Vm{env: env, debug: Debug, Recoverable: true} return &Vm{env: env}
} }
// Run loops and evaluates the contract's code with the given input data // Run loops and evaluates the contract's code with the given input data
...@@ -55,17 +44,67 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -55,17 +44,67 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
self.env.SetDepth(self.env.Depth() + 1) self.env.SetDepth(self.env.Depth() + 1)
defer self.env.SetDepth(self.env.Depth() - 1) defer self.env.SetDepth(self.env.Depth() - 1)
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
defer func() {
if err != nil {
// In case of a VM exception (known exceptions) all gas consumed (panics NOT included).
context.UseGas(context.Gas)
ret = context.Return(nil)
}
}()
if context.CodeAddr != nil {
if p := Precompiled[context.CodeAddr.Str()]; p != nil {
return self.RunPrecompiled(p, input, context)
}
}
var (
codehash = crypto.Sha3Hash(context.Code) // codehash is used when doing jump dest caching
program *Program
)
if !DisableJit {
// Fetch program status.
// * If ready run using JIT
// * If unknown, compile in a seperate goroutine
// * If forced wait for compilation and run once done
if status := GetProgramStatus(codehash); status == progReady {
return RunProgram(GetProgram(codehash), self.env, context, input)
} else if status == progUnknown {
if ForceJit {
// Create and compile program
program = NewProgram(context.Code)
perr := CompileProgram(program)
if perr == nil {
return RunProgram(program, self.env, context, input)
}
glog.V(logger.Info).Infoln("error compiling program", err)
} else {
// create and compile the program. Compilation
// is done in a seperate goroutine
program = NewProgram(context.Code)
go func() {
err := CompileProgram(program)
if err != nil {
glog.V(logger.Info).Infoln("error compiling program", err)
return
}
}()
}
}
}
var ( var (
caller = context.caller caller = context.caller
code = context.Code code = context.Code
value = context.value value = context.value
price = context.Price price = context.Price
op OpCode // current opcode op OpCode // current opcode
codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching mem = NewMemory() // bound memory
mem = NewMemory() // bound memory stack = newstack() // local stack
stack = newstack() // local stack statedb = self.env.State() // current state
statedb = self.env.State() // current state
// For optimisation reason we're using uint64 as the program counter. // For optimisation reason we're using uint64 as the program counter.
// It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Pratically much less so feasible. // It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Pratically much less so feasible.
pc = uint64(0) // program counter pc = uint64(0) // program counter
...@@ -89,32 +128,25 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -89,32 +128,25 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return. // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
defer func() { defer func() {
if self.After != nil {
self.After(context, err)
}
if err != nil { if err != nil {
self.log(pc, op, context.Gas, cost, mem, stack, context, err) self.log(pc, op, context.Gas, cost, mem, stack, context, err)
// In case of a VM exception (known exceptions) all gas consumed (panics NOT included).
context.UseGas(context.Gas)
ret = context.Return(nil)
} }
}() }()
if context.CodeAddr != nil {
if p := Precompiled[context.CodeAddr.Str()]; p != nil {
return self.RunPrecompiled(p, input, context)
}
}
// Don't bother with the execution if there's no code. // Don't bother with the execution if there's no code.
if len(code) == 0 { if len(code) == 0 {
return context.Return(nil), nil return context.Return(nil), nil
} }
for { for {
// Overhead of the atomic read might not be worth it
/* TODO this still causes a few issues in the tests
if program != nil && progStatus(atomic.LoadInt32(&program.status)) == progReady {
// move execution
glog.V(logger.Info).Infoln("Moved execution to JIT")
return runProgram(program, pc, mem, stack, self.env, context, input)
}
*/
// The base for all big integer arithmetic // The base for all big integer arithmetic
base := new(big.Int) base := new(big.Int)
...@@ -122,7 +154,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -122,7 +154,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
op = context.GetOp(pc) op = context.GetOp(pc)
// calculate the new memory size and gas price for the current executing opcode // calculate the new memory size and gas price for the current executing opcode
newMemSize, cost, err = self.calculateGasAndSize(context, caller, op, statedb, mem, stack) newMemSize, cost, err = calculateGasAndSize(self.env, context, caller, op, statedb, mem, stack)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -130,11 +162,9 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -130,11 +162,9 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
// Use the calculated gas. When insufficient gas is present, use all gas and return an // Use the calculated gas. When insufficient gas is present, use all gas and return an
// Out Of Gas error // Out Of Gas error
if !context.UseGas(cost) { if !context.UseGas(cost) {
return nil, OutOfGasError
context.UseGas(context.Gas)
return context.Return(nil), OutOfGasError
} }
// Resize the memory calculated previously // Resize the memory calculated previously
mem.Resize(newMemSize.Uint64()) mem.Resize(newMemSize.Uint64())
// Add a log message // Add a log message
...@@ -376,7 +406,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -376,7 +406,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
addr := common.BigToAddress(stack.pop()) addr := common.BigToAddress(stack.pop())
balance := statedb.GetBalance(addr) balance := statedb.GetBalance(addr)
stack.push(balance) stack.push(new(big.Int).Set(balance))
case ORIGIN: case ORIGIN:
origin := self.env.Origin() origin := self.env.Origin()
...@@ -388,7 +418,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -388,7 +418,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
stack.push(common.Bytes2Big(caller.Bytes())) stack.push(common.Bytes2Big(caller.Bytes()))
case CALLVALUE: case CALLVALUE:
stack.push(value) stack.push(new(big.Int).Set(value))
case CALLDATALOAD: case CALLDATALOAD:
data := getData(input, stack.pop(), common.Big32) data := getData(input, stack.pop(), common.Big32)
...@@ -441,7 +471,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -441,7 +471,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
mem.Set(mOff.Uint64(), l.Uint64(), codeCopy) mem.Set(mOff.Uint64(), l.Uint64(), codeCopy)
case GASPRICE: case GASPRICE:
stack.push(context.Price) stack.push(new(big.Int).Set(context.Price))
case BLOCKHASH: case BLOCKHASH:
num := stack.pop() num := stack.pop()
...@@ -471,11 +501,11 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -471,11 +501,11 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
case DIFFICULTY: case DIFFICULTY:
difficulty := self.env.Difficulty() difficulty := self.env.Difficulty()
stack.push(difficulty) stack.push(new(big.Int).Set(difficulty))
case GASLIMIT: case GASLIMIT:
stack.push(self.env.GasLimit()) stack.push(new(big.Int).Set(self.env.GasLimit()))
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: 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:
size := uint64(op - PUSH1 + 1) size := uint64(op - PUSH1 + 1)
...@@ -555,8 +585,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -555,8 +585,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
case MSIZE: case MSIZE:
stack.push(big.NewInt(int64(mem.Len()))) stack.push(big.NewInt(int64(mem.Len())))
case GAS: case GAS:
stack.push(context.Gas) stack.push(new(big.Int).Set(context.Gas))
case CREATE: case CREATE:
var ( var (
...@@ -652,7 +681,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { ...@@ -652,7 +681,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
// calculateGasAndSize calculates the required given the opcode and stack items calculates the new memorysize for // calculateGasAndSize calculates the required given the opcode and stack items calculates the new memorysize for
// the operation. This does not reduce gas or resizes the memory. // the operation. This does not reduce gas or resizes the memory.
func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) { func calculateGasAndSize(env Environment, context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) {
var ( var (
gas = new(big.Int) gas = new(big.Int)
newMemSize *big.Int = new(big.Int) newMemSize *big.Int = new(big.Int)
...@@ -759,7 +788,7 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo ...@@ -759,7 +788,7 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
gas.Add(gas, stack.data[stack.len()-1]) gas.Add(gas, stack.data[stack.len()-1])
if op == CALL { if op == CALL {
if self.env.State().GetStateObject(common.BigToAddress(stack.data[stack.len()-2])) == nil { if env.State().GetStateObject(common.BigToAddress(stack.data[stack.len()-2])) == nil {
gas.Add(gas, params.CallNewAccountGas) gas.Add(gas, params.CallNewAccountGas)
} }
} }
......
...@@ -20,8 +20,25 @@ import ( ...@@ -20,8 +20,25 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/ethereum/go-ethereum/core/vm"
) )
func init() {
if os.Getenv("JITVM") == "true" {
vm.ForceJit = true
} else {
vm.DisableJit = true
}
}
func BenchmarkStateCall1024(b *testing.B) {
fn := filepath.Join(stateTestDir, "stCallCreateCallCodeTest.json")
if err := BenchVmTest(fn, bconf{"Call1024BalanceTooLow", true, false}, b); err != nil {
b.Error(err)
}
}
func TestStateSystemOperations(t *testing.T) { func TestStateSystemOperations(t *testing.T) {
fn := filepath.Join(stateTestDir, "stSystemOperationsTest.json") fn := filepath.Join(stateTestDir, "stSystemOperationsTest.json")
if err := RunStateTest(fn, StateSkipTests); err != nil { if err := RunStateTest(fn, StateSkipTests); err != nil {
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"io" "io"
"math/big" "math/big"
"strconv" "strconv"
"testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
...@@ -60,6 +61,61 @@ func RunStateTest(p string, skipTests []string) error { ...@@ -60,6 +61,61 @@ func RunStateTest(p string, skipTests []string) error {
} }
func BenchStateTest(p string, conf bconf, b *testing.B) error {
tests := make(map[string]VmTest)
if err := readJsonFile(p, &tests); err != nil {
return err
}
test, ok := tests[conf.name]
if !ok {
return fmt.Errorf("test not found: %s", conf.name)
}
pNoJit := vm.DisableJit
vm.DisableJit = conf.nojit
pForceJit := vm.ForceJit
vm.ForceJit = conf.precomp
// XXX Yeah, yeah...
env := make(map[string]string)
env["currentCoinbase"] = test.Env.CurrentCoinbase
env["currentDifficulty"] = test.Env.CurrentDifficulty
env["currentGasLimit"] = test.Env.CurrentGasLimit
env["currentNumber"] = test.Env.CurrentNumber
env["previousHash"] = test.Env.PreviousHash
if n, ok := test.Env.CurrentTimestamp.(float64); ok {
env["currentTimestamp"] = strconv.Itoa(int(n))
} else {
env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchStateTest(test, env, b)
}
vm.DisableJit = pNoJit
vm.ForceJit = pForceJit
return nil
}
func benchStateTest(test VmTest, env map[string]string, b *testing.B) {
b.StopTimer()
db, _ := ethdb.NewMemDatabase()
statedb := state.New(common.Hash{}, db)
for addr, account := range test.Pre {
obj := StateObjectFromAccount(db, addr, account)
statedb.SetStateObject(obj)
for a, v := range account.Storage {
obj.SetState(common.HexToHash(a), common.HexToHash(v))
}
}
b.StartTimer()
RunState(statedb, env, test.Exec)
}
func runStateTests(tests map[string]VmTest, skipTests []string) error { func runStateTests(tests map[string]VmTest, skipTests []string) error {
skipTest := make(map[string]bool, len(skipTests)) skipTest := make(map[string]bool, len(skipTests))
for _, name := range skipTests { for _, name := range skipTests {
......
...@@ -21,6 +21,20 @@ import ( ...@@ -21,6 +21,20 @@ import (
"testing" "testing"
) )
func BenchmarkVmAckermann32Tests(b *testing.B) {
fn := filepath.Join(vmTestDir, "vmPerformanceTest.json")
if err := BenchVmTest(fn, bconf{"ackermann32", true, false}, b); err != nil {
b.Error(err)
}
}
func BenchmarkVmFibonacci16Tests(b *testing.B) {
fn := filepath.Join(vmTestDir, "vmPerformanceTest.json")
if err := BenchVmTest(fn, bconf{"fibonacci16", true, true}, b); err != nil {
b.Error(err)
}
}
// I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail. // I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
func TestVMArithmetic(t *testing.T) { func TestVMArithmetic(t *testing.T) {
fn := filepath.Join(vmTestDir, "vmArithmeticTest.json") fn := filepath.Join(vmTestDir, "vmArithmeticTest.json")
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"io" "io"
"math/big" "math/big"
"strconv" "strconv"
"testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
...@@ -48,8 +49,79 @@ func RunVmTestWithReader(r io.Reader, skipTests []string) error { ...@@ -48,8 +49,79 @@ func RunVmTestWithReader(r io.Reader, skipTests []string) error {
return nil return nil
} }
func RunVmTest(p string, skipTests []string) error { type bconf struct {
name string
precomp bool
nojit bool
}
func BenchVmTest(p string, conf bconf, b *testing.B) error {
tests := make(map[string]VmTest)
err := readJsonFile(p, &tests)
if err != nil {
return err
}
test, ok := tests[conf.name]
if !ok {
return fmt.Errorf("test not found: %s", conf.name)
}
pNoJit := vm.DisableJit
vm.DisableJit = conf.nojit
pForceJit := vm.ForceJit
vm.ForceJit = conf.precomp
env := make(map[string]string)
env["currentCoinbase"] = test.Env.CurrentCoinbase
env["currentDifficulty"] = test.Env.CurrentDifficulty
env["currentGasLimit"] = test.Env.CurrentGasLimit
env["currentNumber"] = test.Env.CurrentNumber
env["previousHash"] = test.Env.PreviousHash
if n, ok := test.Env.CurrentTimestamp.(float64); ok {
env["currentTimestamp"] = strconv.Itoa(int(n))
} else {
env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
}
/*
if conf.precomp {
program := vm.NewProgram(test.code)
err := vm.AttachProgram(program)
if err != nil {
return err
}
}
*/
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchVmTest(test, env, b)
}
vm.DisableJit = pNoJit
vm.ForceJit = pForceJit
return nil
}
func benchVmTest(test VmTest, env map[string]string, b *testing.B) {
b.StopTimer()
db, _ := ethdb.NewMemDatabase()
statedb := state.New(common.Hash{}, db)
for addr, account := range test.Pre {
obj := StateObjectFromAccount(db, addr, account)
statedb.SetStateObject(obj)
for a, v := range account.Storage {
obj.SetState(common.HexToHash(a), common.HexToHash(v))
}
}
b.StartTimer()
RunVm(statedb, env, test.Exec)
}
func RunVmTest(p string, skipTests []string) error {
tests := make(map[string]VmTest) tests := make(map[string]VmTest)
err := readJsonFile(p, &tests) err := readJsonFile(p, &tests)
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment