From 4dbdcaecb117d7e1fcaf0869f5d4602312552991 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 17 Dec 2014 23:58:52 +0100 Subject: Moved pre-compiled, moved depth check * Depth check has been moved to the execution * Pre compiled execution has been moved to the VM * PrecompiledAddress has been renamed to PrecompiledAccount --- vm/address.go | 12 ++++++------ vm/common.go | 2 +- vm/vm_debug.go | 24 +++++++++++++++++++++--- 3 files changed, 28 insertions(+), 10 deletions(-) (limited to 'vm') diff --git a/vm/address.go b/vm/address.go index be8921a3b..611979c94 100644 --- a/vm/address.go +++ b/vm/address.go @@ -11,25 +11,25 @@ type Address interface { Call(in []byte) []byte } -type PrecompiledAddress struct { +type PrecompiledAccount struct { Gas func(l int) *big.Int fn func(in []byte) []byte } -func (self PrecompiledAddress) Call(in []byte) []byte { +func (self PrecompiledAccount) Call(in []byte) []byte { return self.fn(in) } -var Precompiled = map[uint64]*PrecompiledAddress{ - 1: &PrecompiledAddress{func(l int) *big.Int { +var Precompiled = map[string]*PrecompiledAccount{ + string(ethutil.LeftPadBytes([]byte{1}, 20)): &PrecompiledAccount{func(l int) *big.Int { return GasEcrecover }, ecrecoverFunc}, - 2: &PrecompiledAddress{func(l int) *big.Int { + string(ethutil.LeftPadBytes([]byte{2}, 20)): &PrecompiledAccount{func(l int) *big.Int { n := big.NewInt(int64(l+31)/32 + 1) n.Mul(n, GasSha256) return n }, sha256Func}, - 3: &PrecompiledAddress{func(l int) *big.Int { + string(ethutil.LeftPadBytes([]byte{3}, 20)): &PrecompiledAccount{func(l int) *big.Int { n := big.NewInt(int64(l+31)/32 + 1) n.Mul(n, GasRipemd) return n diff --git a/vm/common.go b/vm/common.go index 592d44ccd..3d6d377ca 100644 --- a/vm/common.go +++ b/vm/common.go @@ -48,7 +48,7 @@ var ( S256 = ethutil.S256 ) -const MaxCallDepth = 1025 +const MaxCallDepth = 1024 func calcMemSize(off, l *big.Int) *big.Int { if l.Cmp(ethutil.Big0) == 0 { diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 8af1979b1..9da832a79 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -48,9 +48,8 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * }) closure := NewClosure(msg, caller, me, code, gas, price) - if self.env.Depth() == MaxCallDepth { - //closure.UseGas(gas) - return closure.Return(nil), DepthError{} + if p := Precompiled[string(me.Address())]; p != nil { + return self.RunPrecompiled(p, callData, closure) } if self.Recoverable { @@ -941,6 +940,25 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * } } +func (self *DebugVm) RunPrecompiled(p *PrecompiledAccount, callData []byte, closure *Closure) (ret []byte, err error) { + gas := p.Gas(len(callData)) + if closure.UseGas(gas) { + ret = p.Call(callData) + self.Printf("NATIVE_FUNC => %x", ret) + self.Endl() + + return closure.Return(ret), nil + } else { + self.Endl() + + tmp := new(big.Int).Set(closure.Gas) + + closure.UseGas(closure.Gas) + + return closure.Return(nil), OOG(gas, tmp) + } +} + func (self *DebugVm) Printf(format string, v ...interface{}) VirtualMachine { if self.logTy == LogTyPretty { self.logStr += fmt.Sprintf(format, v...) -- cgit v1.2.3 From 198cc69357a0f25ae486a041786e1239c6f5ab0f Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 21:58:26 +0100 Subject: Gas corrections and vm fixes --- vm/common.go | 2 +- vm/vm_debug.go | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'vm') diff --git a/vm/common.go b/vm/common.go index 3d6d377ca..529bbdeb1 100644 --- a/vm/common.go +++ b/vm/common.go @@ -37,7 +37,7 @@ var ( GasLog = big.NewInt(32) GasSha256 = big.NewInt(50) GasRipemd = big.NewInt(50) - GasEcrecover = big.NewInt(100) + GasEcrecover = big.NewInt(500) Pow256 = ethutil.BigPow(2, 256) diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 9da832a79..d78aff4ce 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -2,6 +2,7 @@ package vm import ( "fmt" + "math" "math/big" "github.com/ethereum/go-ethereum/crypto" @@ -112,7 +113,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * return closure.Return(nil), nil } - vmlogger.Debugf("(%d) %x gas: %v (d) %x\n", self.env.Depth(), closure.Address(), closure.Gas, callData) + vmlogger.Debugf("(%d) (%x) %x gas: %v (d) %x\n", self.env.Depth(), caller.Address()[:4], closure.Address(), closure.Gas, callData) for { prevStep = step @@ -185,11 +186,11 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * // 0 => non 0 mult = ethutil.Big3 } else if val.BigInt().Cmp(ethutil.Big0) != 0 && len(y.Bytes()) == 0 { - statedb.Refund(closure.caller.Address(), GasSStoreRefund, closure.Price) + statedb.Refund(caller.Address(), GasSStoreRefund) mult = ethutil.Big0 } else { - // non 0 => non 0 + // non 0 => non 0 (or 0 => 0) mult = ethutil.Big1 } gas.Set(new(big.Int).Mul(mult, GasSStore)) @@ -660,7 +661,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * cOff = 0 l = 0 } else if cOff+l > size { - l = 0 + l = uint64(math.Min(float64(cOff+l), float64(size))) } codeCopy := code[cOff : cOff+l] @@ -776,10 +777,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * val, loc := stack.Popn() statedb.SetState(closure.Address(), loc.Bytes(), val) - // Debug sessions are allowed to run without message - if closure.message != nil { - closure.message.AddStorageChange(loc.Bytes()) - } + closure.message.AddStorageChange(loc.Bytes()) self.Printf(" {0x%x : 0x%x}", loc.Bytes(), val.Bytes()) case JUMP: @@ -898,10 +896,12 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * return closure.Return(ret), nil case SUICIDE: - receiver := statedb.GetOrNewStateObject(stack.Pop().Bytes()) + balance := statedb.GetBalance(closure.Address()) + + self.Printf(" => (%x) %v", receiver.Address()[:4], balance) - receiver.AddAmount(statedb.GetBalance(closure.Address())) + receiver.AddAmount(balance) statedb.Delete(closure.Address()) fallthrough -- cgit v1.2.3 From 1e985f986569a68601b052c8949fc6f360e139d9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 23:59:40 +0100 Subject: Fixed casting error * big(bytes) == 0 when len(bytes) > 0 --- vm/vm_debug.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'vm') diff --git a/vm/vm_debug.go b/vm/vm_debug.go index d78aff4ce..708aada5b 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -181,11 +181,12 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * var mult *big.Int y, x := stack.Peekn() - val := closure.GetStorage(x) - if val.BigInt().Cmp(ethutil.Big0) == 0 && len(y.Bytes()) > 0 { + //val := closure.GetStorage(x) + val := statedb.GetState(closure.Address(), x.Bytes()) + if len(val) == 0 && len(y.Bytes()) > 0 { // 0 => non 0 mult = ethutil.Big3 - } else if val.BigInt().Cmp(ethutil.Big0) != 0 && len(y.Bytes()) == 0 { + } else if len(val) > 0 && len(y.Bytes()) == 0 { statedb.Refund(caller.Address(), GasSStoreRefund) mult = ethutil.Big0 -- cgit v1.2.3 From 59ef6e36931c980ba15babfb3680514635faebf6 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 19 Dec 2014 00:18:52 +0100 Subject: Cleaned up objects --- vm/closure.go | 35 +++-------------------------------- vm/vm_debug.go | 6 ++---- 2 files changed, 5 insertions(+), 36 deletions(-) (limited to 'vm') diff --git a/vm/closure.go b/vm/closure.go index bd5268f96..97b31ada0 100644 --- a/vm/closure.go +++ b/vm/closure.go @@ -3,7 +3,6 @@ package vm import ( "math/big" - "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" ) @@ -11,8 +10,6 @@ type ClosureRef interface { ReturnGas(*big.Int, *big.Int) Address() []byte SetCode([]byte) - GetStorage(*big.Int) *ethutil.Value - SetStorage(*big.Int, *ethutil.Value) } type Closure struct { @@ -41,10 +38,6 @@ func NewClosure(msg *state.Message, caller ClosureRef, object ClosureRef, code [ return c } -func (c *Closure) GetValue(x uint64) *ethutil.Value { - return c.GetRangeValue(x, 1) -} - func (c *Closure) GetOp(x uint64) OpCode { return OpCode(c.GetByte(x)) } @@ -65,30 +58,12 @@ func (c *Closure) GetBytes(x, y int) []byte { return c.Code[x : x+y] } -func (c *Closure) GetRangeValue(x, y uint64) *ethutil.Value { +func (c *Closure) GetRangeValue(x, y uint64) []byte { if x >= uint64(len(c.Code)) || y >= uint64(len(c.Code)) { - return ethutil.NewValue(0) - } - - partial := c.Code[x : x+y] - - return ethutil.NewValue(partial) -} - -/* - * State storage functions - */ -func (c *Closure) SetStorage(x *big.Int, val *ethutil.Value) { - c.object.SetStorage(x, val) -} - -func (c *Closure) GetStorage(x *big.Int) *ethutil.Value { - m := c.object.GetStorage(x) - if m == nil { - return ethutil.EmptyValue() + return nil } - return m + return c.Code[x : x+y] } func (c *Closure) Return(ret []byte) []byte { @@ -123,10 +98,6 @@ func (c *Closure) ReturnGas(gas, price *big.Int) { /* * Set / Get */ -func (c *Closure) Caller() ClosureRef { - return c.caller -} - func (c *Closure) Address() []byte { return c.object.Address() } diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 708aada5b..fd16a3895 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -181,7 +181,6 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * var mult *big.Int y, x := stack.Peekn() - //val := closure.GetStorage(x) val := statedb.GetState(closure.Address(), x.Bytes()) if len(val) == 0 && len(y.Bytes()) > 0 { // 0 => non 0 @@ -714,8 +713,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * //a := big.NewInt(int64(op) - int64(PUSH1) + 1) a := uint64(op - PUSH1 + 1) //pc.Add(pc, ethutil.Big1) - data := closure.GetRangeValue(pc+1, a) - val := ethutil.BigD(data.Bytes()) + val := ethutil.BigD(closure.GetRangeValue(pc+1, a)) // Push value to stack stack.Push(val) pc += a @@ -723,7 +721,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * step += int(op) - int(PUSH1) + 1 - self.Printf(" => 0x%x", data.Bytes()) + self.Printf(" => 0x%x", val.Bytes()) case POP: stack.Pop() case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: -- cgit v1.2.3 From 1508a23a6fe3cc50f718bfd6c62caae056534c09 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 20 Dec 2014 02:21:13 +0100 Subject: Minor updates on gas and removed/refactored old code. --- vm/environment.go | 5 +++++ vm/vm_debug.go | 41 ++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 19 deletions(-) (limited to 'vm') diff --git a/vm/environment.go b/vm/environment.go index d77fb1419..969bc5e43 100644 --- a/vm/environment.go +++ b/vm/environment.go @@ -2,6 +2,7 @@ package vm import ( "errors" + "fmt" "math/big" "github.com/ethereum/go-ethereum/ethutil" @@ -74,3 +75,7 @@ func (self *Log) Data() []byte { func (self *Log) RlpData() interface{} { return []interface{}{self.address, ethutil.ByteSliceToInterface(self.topics), self.data} } + +func (self *Log) String() string { + return fmt.Sprintf("[A=%x T=%x D=%x]", self.address, self.topics, self.data) +} diff --git a/vm/vm_debug.go b/vm/vm_debug.go index fd16a3895..aa3291e66 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -108,13 +108,13 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * } ) + vmlogger.Debugf("(%d) (%x) %x (code=%d) gas: %v (d) %x\n", self.env.Depth(), caller.Address()[:4], closure.Address(), len(code), closure.Gas, callData) + // Don't bother with the execution if there's no code. if len(code) == 0 { return closure.Return(nil), nil } - vmlogger.Debugf("(%d) (%x) %x gas: %v (d) %x\n", self.env.Depth(), caller.Address()[:4], closure.Address(), closure.Gas, callData) - for { prevStep = step // The base for all big integer arithmetic @@ -134,6 +134,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * addStepGasUsage(GasStep) var newMemSize *big.Int = ethutil.Big0 + var additionalGas *big.Int = new(big.Int) // Stack Check, memory resize & gas phase switch op { // Stack checks only @@ -213,22 +214,24 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2]) case SHA3: require(2) - gas.Set(GasSha) - newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2]) + additionalGas.Set(stack.data[stack.Len()-2]) case CALLDATACOPY: require(2) newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3]) + additionalGas.Set(stack.data[stack.Len()-3]) case CODECOPY: require(3) newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3]) + additionalGas.Set(stack.data[stack.Len()-3]) case EXTCODECOPY: require(4) newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-4]) + additionalGas.Set(stack.data[stack.Len()-4]) case CALL, CALLCODE: require(7) gas.Set(GasCall) @@ -245,20 +248,23 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-3]) } + switch op { + case CALLDATACOPY, CODECOPY, EXTCODECOPY: + additionalGas.Add(additionalGas, u256(31)) + additionalGas.Div(additionalGas, u256(32)) + addStepGasUsage(additionalGas) + case SHA3: + additionalGas.Add(additionalGas, u256(31)) + additionalGas.Div(additionalGas, u256(32)) + additionalGas.Mul(additionalGas, GasSha3Byte) + addStepGasUsage(additionalGas) + } + if newMemSize.Cmp(ethutil.Big0) > 0 { newMemSize.Add(newMemSize, u256(31)) newMemSize.Div(newMemSize, u256(32)) newMemSize.Mul(newMemSize, u256(32)) - switch op { - case CALLDATACOPY, CODECOPY, EXTCODECOPY: - addStepGasUsage(new(big.Int).Div(newMemSize, u256(32))) - case SHA3: - g := new(big.Int).Div(newMemSize, u256(32)) - g.Mul(g, GasSha3Byte) - addStepGasUsage(g) - } - if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 { memGasUsage := new(big.Int).Sub(newMemSize, u256(int64(mem.Len()))) memGasUsage.Mul(GasMemory, memGasUsage) @@ -643,9 +649,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * case CODECOPY, EXTCODECOPY: var code []byte if op == EXTCODECOPY { - addr := stack.Pop().Bytes() - - code = statedb.GetCode(addr) + code = statedb.GetCode(stack.Pop().Bytes()) } else { code = closure.Code } @@ -663,12 +667,11 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * } else if cOff+l > size { l = uint64(math.Min(float64(cOff+l), float64(size))) } - codeCopy := code[cOff : cOff+l] mem.Set(mOff, l, codeCopy) - self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, code[cOff:cOff+l]) + self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, codeCopy) case GASPRICE: stack.Push(closure.Price) @@ -891,7 +894,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * size, offset := stack.Popn() ret := mem.Get(offset.Int64(), size.Int64()) - self.Printf(" => (%d) 0x%x", len(ret), ret).Endl() + self.Printf(" => [%v, %v] (%d) 0x%x", offset, size, len(ret), ret).Endl() return closure.Return(ret), nil case SUICIDE: -- cgit v1.2.3 From 29c887ef2c39e91951e1ae14e7c4276334c434a4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 30 Dec 2014 16:16:02 +0100 Subject: Removed incorrect range check for push --- vm/closure.go | 17 +++++++---------- vm/vm_debug.go | 5 +++-- 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'vm') diff --git a/vm/closure.go b/vm/closure.go index 97b31ada0..df216f2ae 100644 --- a/vm/closure.go +++ b/vm/closure.go @@ -1,8 +1,10 @@ package vm import ( + "math" "math/big" + "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" ) @@ -51,19 +53,14 @@ func (c *Closure) GetByte(x uint64) byte { } func (c *Closure) GetBytes(x, y int) []byte { - if x >= len(c.Code) || y >= len(c.Code) { - return nil - } - - return c.Code[x : x+y] + return c.GetRangeValue(uint64(x), uint64(y)) } -func (c *Closure) GetRangeValue(x, y uint64) []byte { - if x >= uint64(len(c.Code)) || y >= uint64(len(c.Code)) { - return nil - } +func (c *Closure) GetRangeValue(x, size uint64) []byte { + x = uint64(math.Min(float64(x), float64(len(c.Code)))) + y := uint64(math.Min(float64(x+size), float64(len(c.Code)))) - return c.Code[x : x+y] + return ethutil.LeftPadBytes(c.Code[x:y], int(size)) } func (c *Closure) Return(ret []byte) []byte { diff --git a/vm/vm_debug.go b/vm/vm_debug.go index aa3291e66..2ee13c516 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -716,7 +716,8 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * //a := big.NewInt(int64(op) - int64(PUSH1) + 1) a := uint64(op - PUSH1 + 1) //pc.Add(pc, ethutil.Big1) - val := ethutil.BigD(closure.GetRangeValue(pc+1, a)) + byts := closure.GetRangeValue(pc+1, a) + val := ethutil.BigD(byts) // Push value to stack stack.Push(val) pc += a @@ -724,7 +725,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * step += int(op) - int(PUSH1) + 1 - self.Printf(" => 0x%x", val.Bytes()) + self.Printf(" => 0x%x", byts) case POP: stack.Pop() case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: -- cgit v1.2.3 From 2ebf33ac1cd41277a296176198cfde9085948c0c Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 30 Dec 2014 16:17:56 +0100 Subject: removed variable --- vm/vm_debug.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'vm') diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 2ee13c516..9a538c940 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -713,15 +713,11 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * // 0x50 range 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: - //a := big.NewInt(int64(op) - int64(PUSH1) + 1) a := uint64(op - PUSH1 + 1) - //pc.Add(pc, ethutil.Big1) byts := closure.GetRangeValue(pc+1, a) - val := ethutil.BigD(byts) // Push value to stack - stack.Push(val) + stack.Push(ethutil.BigD(byts)) pc += a - //pc.Add(pc, a.Sub(a, big.NewInt(1))) step += int(op) - int(PUSH1) + 1 -- cgit v1.2.3 From 138ab26b8c29db00022fb6afbca153f3c1928d00 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 30 Dec 2014 17:09:43 +0100 Subject: SIGNEXTEND missing from stack check --- vm/vm_debug.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'vm') diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 2ee13c516..a4e97ad48 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -140,7 +140,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * // Stack checks only case ISZERO, CALLDATALOAD, POP, JUMP, NOT: // 1 require(1) - case ADD, SUB, DIV, SDIV, MOD, SMOD, LT, GT, SLT, SGT, EQ, AND, OR, XOR, BYTE: // 2 + case ADD, SUB, DIV, SDIV, MOD, SMOD, LT, GT, SLT, SGT, EQ, AND, OR, XOR, BYTE, SIGNEXTEND: // 2 require(2) case ADDMOD, MULMOD: // 3 require(3) -- cgit v1.2.3 From 4b4e0821027cb5a82eaa04dcd89b1cad4a05af4e Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 31 Dec 2014 10:32:53 +0100 Subject: JUMPI never 'require' checked. --- vm/vm_debug.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'vm') diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 933fb7b12..1e1b85e6d 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -140,7 +140,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * // Stack checks only case ISZERO, CALLDATALOAD, POP, JUMP, NOT: // 1 require(1) - case ADD, SUB, DIV, SDIV, MOD, SMOD, LT, GT, SLT, SGT, EQ, AND, OR, XOR, BYTE, SIGNEXTEND: // 2 + case JUMPI, ADD, SUB, DIV, SDIV, MOD, SMOD, LT, GT, SLT, SGT, EQ, AND, OR, XOR, BYTE, SIGNEXTEND: // 2 require(2) case ADDMOD, MULMOD: // 3 require(3) -- cgit v1.2.3 From 4547a05a689e6a0f29dd2d90e840e84de7f564f4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 31 Dec 2014 11:12:40 +0100 Subject: Minor improvements * Moved gas and mem size to its own function --- vm/stack.go | 6 ++ vm/vm_debug.go | 325 +++++++++++++++++++++++++++++---------------------------- 2 files changed, 169 insertions(+), 162 deletions(-) (limited to 'vm') diff --git a/vm/stack.go b/vm/stack.go index 6091479cb..b9eaa10cd 100644 --- a/vm/stack.go +++ b/vm/stack.go @@ -91,6 +91,12 @@ func (st *Stack) Get(amount *big.Int) []*big.Int { return nil } +func (st *Stack) require(n int) { + if st.Len() < n { + panic(fmt.Sprintf("stack underflow (%d <=> %d)", st.Len(), n)) + } +} + func (st *Stack) Print() { fmt.Println("### stack ###") if len(st.data) > 0 { diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 1e1b85e6d..8829a9de0 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -49,15 +49,11 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * }) closure := NewClosure(msg, caller, me, code, gas, price) - if p := Precompiled[string(me.Address())]; p != nil { - return self.RunPrecompiled(p, callData, closure) - } - if self.Recoverable { // Recover from any require exception defer func() { if r := recover(); r != nil { - self.Endl() + self.Printf(" %v", r).Endl() closure.UseGas(closure.Gas) @@ -69,6 +65,10 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * }() } + if p := Precompiled[string(me.Address())]; p != nil { + return self.RunPrecompiled(p, callData, closure) + } + var ( op OpCode @@ -79,11 +79,6 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * step = 0 prevStep = 0 statedb = self.env.State() - require = func(m int) { - if stack.Len() < m { - panic(fmt.Sprintf("%04v (%v) stack err size = %d, required = %d", pc, op, stack.Len(), m)) - } - } jump = func(from uint64, to *big.Int) { p := to.Uint64() @@ -124,160 +119,11 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * // Get the memory location of pc op = closure.GetOp(pc) - gas := new(big.Int) - addStepGasUsage := func(amount *big.Int) { - if amount.Cmp(ethutil.Big0) >= 0 { - gas.Add(gas, amount) - } - } - - addStepGasUsage(GasStep) - - var newMemSize *big.Int = ethutil.Big0 - var additionalGas *big.Int = new(big.Int) - // Stack Check, memory resize & gas phase - switch op { - // Stack checks only - case ISZERO, CALLDATALOAD, POP, JUMP, NOT: // 1 - require(1) - case JUMPI, ADD, SUB, DIV, SDIV, MOD, SMOD, LT, GT, SLT, SGT, EQ, AND, OR, XOR, BYTE, SIGNEXTEND: // 2 - require(2) - case ADDMOD, MULMOD: // 3 - require(3) - case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: - n := int(op - SWAP1 + 2) - require(n) - case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: - n := int(op - DUP1 + 1) - require(n) - case LOG0, LOG1, LOG2, LOG3, LOG4: - n := int(op - LOG0) - require(n + 2) - - gas.Set(GasLog) - addStepGasUsage(new(big.Int).Mul(big.NewInt(int64(n)), GasLog)) - - mSize, mStart := stack.Peekn() - addStepGasUsage(mSize) - - newMemSize = calcMemSize(mStart, mSize) - case EXP: - require(2) - - gas.Set(big.NewInt(int64(len(stack.data[stack.Len()-2].Bytes()) + 1))) - // Gas only - case STOP: - gas.Set(ethutil.Big0) - case SUICIDE: - require(1) - - gas.Set(ethutil.Big0) - case SLOAD: - require(1) - - gas.Set(GasSLoad) - // Memory resize & Gas - case SSTORE: - require(2) - - var mult *big.Int - y, x := stack.Peekn() - val := statedb.GetState(closure.Address(), x.Bytes()) - if len(val) == 0 && len(y.Bytes()) > 0 { - // 0 => non 0 - mult = ethutil.Big3 - } else if len(val) > 0 && len(y.Bytes()) == 0 { - statedb.Refund(caller.Address(), GasSStoreRefund) - - mult = ethutil.Big0 - } else { - // non 0 => non 0 (or 0 => 0) - mult = ethutil.Big1 - } - gas.Set(new(big.Int).Mul(mult, GasSStore)) - case BALANCE: - require(1) - gas.Set(GasBalance) - case MSTORE: - require(2) - newMemSize = calcMemSize(stack.Peek(), u256(32)) - case MLOAD: - require(1) - - newMemSize = calcMemSize(stack.Peek(), u256(32)) - case MSTORE8: - require(2) - newMemSize = calcMemSize(stack.Peek(), u256(1)) - case RETURN: - require(2) - - newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2]) - case SHA3: - require(2) - gas.Set(GasSha) - newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2]) - additionalGas.Set(stack.data[stack.Len()-2]) - case CALLDATACOPY: - require(2) - - newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3]) - additionalGas.Set(stack.data[stack.Len()-3]) - case CODECOPY: - require(3) - - newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3]) - additionalGas.Set(stack.data[stack.Len()-3]) - case EXTCODECOPY: - require(4) - - newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-4]) - additionalGas.Set(stack.data[stack.Len()-4]) - case CALL, CALLCODE: - require(7) - gas.Set(GasCall) - addStepGasUsage(stack.data[stack.Len()-1]) - - x := calcMemSize(stack.data[stack.Len()-6], stack.data[stack.Len()-7]) - y := calcMemSize(stack.data[stack.Len()-4], stack.data[stack.Len()-5]) - - newMemSize = ethutil.BigMax(x, y) - case CREATE: - require(3) - gas.Set(GasCreate) - - newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-3]) - } - - switch op { - case CALLDATACOPY, CODECOPY, EXTCODECOPY: - additionalGas.Add(additionalGas, u256(31)) - additionalGas.Div(additionalGas, u256(32)) - addStepGasUsage(additionalGas) - case SHA3: - additionalGas.Add(additionalGas, u256(31)) - additionalGas.Div(additionalGas, u256(32)) - additionalGas.Mul(additionalGas, GasSha3Byte) - addStepGasUsage(additionalGas) - } - - if newMemSize.Cmp(ethutil.Big0) > 0 { - newMemSize.Add(newMemSize, u256(31)) - newMemSize.Div(newMemSize, u256(32)) - newMemSize.Mul(newMemSize, u256(32)) - - if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 { - memGasUsage := new(big.Int).Sub(newMemSize, u256(int64(mem.Len()))) - memGasUsage.Mul(GasMemory, memGasUsage) - memGasUsage.Div(memGasUsage, u256(32)) - - addStepGasUsage(memGasUsage) + self.Printf("(pc) %-3d -o- %-14s (m) %-4d (s) %-4d ", pc, op.String(), mem.Len(), stack.Len()) - } - - } + newMemSize, gas := self.calculateGasAndSize(closure, caller, op, statedb, mem, stack) - self.Printf("(pc) %-3d -o- %-14s", pc, op.String()) - self.Printf(" (m) %-4d (s) %-4d (g) %-3v (%v)", mem.Len(), stack.Len(), gas, closure.Gas) + self.Printf("(g) %-3v (%v)", gas, closure.Gas) if !closure.UseGas(gas) { self.Endl() @@ -939,6 +785,161 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * } } +func (self *DebugVm) calculateGasAndSize(closure *Closure, caller ClosureRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *Stack) (*big.Int, *big.Int) { + gas := new(big.Int) + addStepGasUsage := func(amount *big.Int) { + if amount.Cmp(ethutil.Big0) >= 0 { + gas.Add(gas, amount) + } + } + + addStepGasUsage(GasStep) + + var newMemSize *big.Int = ethutil.Big0 + var additionalGas *big.Int = new(big.Int) + // Stack Check, memory resize & gas phase + switch op { + // Stack checks only + case ISZERO, CALLDATALOAD, POP, JUMP, NOT: // 1 + stack.require(1) + case JUMPI, ADD, SUB, DIV, SDIV, MOD, SMOD, LT, GT, SLT, SGT, EQ, AND, OR, XOR, BYTE, SIGNEXTEND: // 2 + stack.require(2) + case ADDMOD, MULMOD: // 3 + stack.require(3) + case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: + n := int(op - SWAP1 + 2) + stack.require(n) + case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: + n := int(op - DUP1 + 1) + stack.require(n) + case LOG0, LOG1, LOG2, LOG3, LOG4: + n := int(op - LOG0) + stack.require(n + 2) + + gas.Set(GasLog) + addStepGasUsage(new(big.Int).Mul(big.NewInt(int64(n)), GasLog)) + + mSize, mStart := stack.Peekn() + addStepGasUsage(mSize) + + newMemSize = calcMemSize(mStart, mSize) + case EXP: + stack.require(2) + + gas.Set(big.NewInt(int64(len(stack.data[stack.Len()-2].Bytes()) + 1))) + // Gas only + case STOP: + gas.Set(ethutil.Big0) + case SUICIDE: + stack.require(1) + + gas.Set(ethutil.Big0) + case SLOAD: + stack.require(1) + + gas.Set(GasSLoad) + // Memory resize & Gas + case SSTORE: + stack.require(2) + + var mult *big.Int + y, x := stack.Peekn() + val := statedb.GetState(closure.Address(), x.Bytes()) + if len(val) == 0 && len(y.Bytes()) > 0 { + // 0 => non 0 + mult = ethutil.Big3 + } else if len(val) > 0 && len(y.Bytes()) == 0 { + statedb.Refund(caller.Address(), GasSStoreRefund) + + mult = ethutil.Big0 + } else { + // non 0 => non 0 (or 0 => 0) + mult = ethutil.Big1 + } + gas.Set(new(big.Int).Mul(mult, GasSStore)) + case BALANCE: + stack.require(1) + gas.Set(GasBalance) + case MSTORE: + stack.require(2) + newMemSize = calcMemSize(stack.Peek(), u256(32)) + case MLOAD: + stack.require(1) + + newMemSize = calcMemSize(stack.Peek(), u256(32)) + case MSTORE8: + stack.require(2) + newMemSize = calcMemSize(stack.Peek(), u256(1)) + case RETURN: + stack.require(2) + + newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2]) + case SHA3: + stack.require(2) + gas.Set(GasSha) + newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2]) + additionalGas.Set(stack.data[stack.Len()-2]) + case CALLDATACOPY: + stack.require(2) + + newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3]) + additionalGas.Set(stack.data[stack.Len()-3]) + case CODECOPY: + stack.require(3) + + newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3]) + additionalGas.Set(stack.data[stack.Len()-3]) + case EXTCODECOPY: + stack.require(4) + + newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-4]) + additionalGas.Set(stack.data[stack.Len()-4]) + case CALL, CALLCODE: + stack.require(7) + gas.Set(GasCall) + addStepGasUsage(stack.data[stack.Len()-1]) + + x := calcMemSize(stack.data[stack.Len()-6], stack.data[stack.Len()-7]) + y := calcMemSize(stack.data[stack.Len()-4], stack.data[stack.Len()-5]) + + newMemSize = ethutil.BigMax(x, y) + case CREATE: + stack.require(3) + gas.Set(GasCreate) + + newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-3]) + } + + switch op { + case CALLDATACOPY, CODECOPY, EXTCODECOPY: + additionalGas.Add(additionalGas, u256(31)) + additionalGas.Div(additionalGas, u256(32)) + addStepGasUsage(additionalGas) + case SHA3: + additionalGas.Add(additionalGas, u256(31)) + additionalGas.Div(additionalGas, u256(32)) + additionalGas.Mul(additionalGas, GasSha3Byte) + addStepGasUsage(additionalGas) + } + + if newMemSize.Cmp(ethutil.Big0) > 0 { + newMemSize.Add(newMemSize, u256(31)) + newMemSize.Div(newMemSize, u256(32)) + newMemSize.Mul(newMemSize, u256(32)) + + if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 { + memGasUsage := new(big.Int).Sub(newMemSize, u256(int64(mem.Len()))) + memGasUsage.Mul(GasMemory, memGasUsage) + memGasUsage.Div(memGasUsage, u256(32)) + + addStepGasUsage(memGasUsage) + } + + } + + return newMemSize, gas +} + func (self *DebugVm) RunPrecompiled(p *PrecompiledAccount, callData []byte, closure *Closure) (ret []byte, err error) { gas := p.Gas(len(callData)) if closure.UseGas(gas) { -- cgit v1.2.3