Skip to content

Commit 3df7142

Browse files
committed
core/vm: minor polishes, fix STATICCALL for precompiles
* Fix STATICCALL so it is able to call precompiles too * Fix write detection to use the correct value argument of CALL * Fix write protection to ignore the value in CALLCODE
1 parent 3d123bc commit 3df7142

2 files changed

Lines changed: 33 additions & 37 deletions

File tree

‎core/vm/evm.go‎

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,20 @@ func (evm *EVM) Cancel() {
123123
atomic.StoreInt32(&evm.abort, 1)
124124
}
125125

126-
// Call executes the contract associated with the addr with the given input as parameters. It also handles any
127-
// necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
128-
// case of an execution error or failed value transfer.
126+
// Call executes the contract associated with the addr with the given input as
127+
// parameters. It also handles any necessary value transfer required and takes
128+
// the necessary steps to create accounts and reverses the state in case of an
129+
// execution error or failed value transfer.
129130
func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
130131
if evm.vmConfig.NoRecursion && evm.depth > 0 {
131132
return nil, gas, nil
132133
}
133134

134-
// Depth check execution. Fail if we're trying to execute above the
135-
// limit.
135+
// Fail if we're trying to execute above the call depth limit
136136
if evm.depth > int(params.CallCreateDepth) {
137137
return nil, gas, ErrDepth
138138
}
139+
// Fail if we're trying to transfer more than the available balance
139140
if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
140141
return nil, gas, ErrInsufficientBalance
141142
}
@@ -173,21 +174,23 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
173174
return ret, contract.Gas, err
174175
}
175176

176-
// CallCode executes the contract associated with the addr with the given input as parameters. It also handles any
177-
// necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
178-
// case of an execution error or failed value transfer.
177+
// CallCode executes the contract associated with the addr with the given input
178+
// as parameters. It also handles any necessary value transfer required and takes
179+
// the necessary steps to create accounts and reverses the state in case of an
180+
// execution error or failed value transfer.
179181
//
180-
// CallCode differs from Call in the sense that it executes the given address' code with the caller as context.
182+
// CallCode differs from Call in the sense that it executes the given address'
183+
// code with the caller as context.
181184
func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
182185
if evm.vmConfig.NoRecursion && evm.depth > 0 {
183186
return nil, gas, nil
184187
}
185188

186-
// Depth check execution. Fail if we're trying to execute above the
187-
// limit.
189+
// Fail if we're trying to execute above the call depth limit
188190
if evm.depth > int(params.CallCreateDepth) {
189191
return nil, gas, ErrDepth
190192
}
193+
// Fail if we're trying to transfer more than the available balance
191194
if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
192195
return nil, gas, ErrInsufficientBalance
193196
}
@@ -211,18 +214,16 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
211214
return ret, contract.Gas, err
212215
}
213216

214-
// DelegateCall executes the contract associated with the addr with the given input as parameters.
215-
// It reverses the state in case of an execution error.
217+
// DelegateCall executes the contract associated with the addr with the given input
218+
// as parameters. It reverses the state in case of an execution error.
216219
//
217-
// DelegateCall differs from CallCode in the sense that it executes the given address' code with the caller as context
218-
// and the caller is set to the caller of the caller.
220+
// DelegateCall differs from CallCode in the sense that it executes the given address'
221+
// code with the caller as context and the caller is set to the caller of the caller.
219222
func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
220223
if evm.vmConfig.NoRecursion && evm.depth > 0 {
221224
return nil, gas, nil
222225
}
223-
224-
// Depth check execution. Fail if we're trying to execute above the
225-
// limit.
226+
// Fail if we're trying to execute above the call depth limit
226227
if evm.depth > int(params.CallCreateDepth) {
227228
return nil, gas, ErrDepth
228229
}
@@ -232,7 +233,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
232233
to = AccountRef(caller.Address())
233234
)
234235

235-
// Iinitialise a new contract and make initialise the delegate values
236+
// Initialise a new contract and make initialise the delegate values
236237
contract := NewContract(caller, to, nil, gas).AsDelegate()
237238
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
238239

@@ -245,18 +246,19 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
245246
return ret, contract.Gas, err
246247
}
247248

249+
// StaticCall executes the contract associated with the addr with the given input
250+
// as parameters while disallowing any modifications to the state during the call.
251+
// Opcodes that attempt to perform such modifications will result in exceptions
252+
// instead of performing the modifications.
248253
func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
249254
if evm.vmConfig.NoRecursion && evm.depth > 0 {
250255
return nil, gas, nil
251256
}
252-
253-
// Depth check execution. Fail if we're trying to execute above the
254-
// limit.
257+
// Fail if we're trying to execute above the call depth limit
255258
if evm.depth > int(params.CallCreateDepth) {
256259
return nil, gas, ErrDepth
257260
}
258-
259-
// make sure the readonly is only set if we aren't in readonly yet
261+
// Make sure the readonly is only set if we aren't in readonly yet
260262
// this makes also sure that the readonly flag isn't removed for
261263
// child calls.
262264
if !evm.interpreter.readonly {
@@ -268,23 +270,18 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
268270
to = AccountRef(addr)
269271
snapshot = evm.StateDB.Snapshot()
270272
)
271-
if !evm.StateDB.Exist(addr) {
272-
return nil, gas, nil
273-
}
274-
275-
// initialise a new contract and set the code that is to be used by the
276-
// EVM. The contract is a scoped evmironment for this execution context
273+
// Initialise a new contract and set the code that is to be used by the
274+
// EVM. The contract is a scoped environment for this execution context
277275
// only.
278276
contract := NewContract(caller, to, new(big.Int), gas)
279277
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
280278

281-
ret, err = evm.interpreter.Run(snapshot, contract, input)
282279
// When an error was returned by the EVM or when setting the creation code
283280
// above we revert to the snapshot and consume any gas remaining. Additionally
284-
// when we're in homestead this also counts for code storage gas errors.
281+
// when we're in Homestead this also counts for code storage gas errors.
282+
ret, err = run(evm, snapshot, contract, input)
285283
if err != nil {
286284
contract.UseGas(contract.Gas)
287-
288285
evm.StateDB.RevertToSnapshot(snapshot)
289286
}
290287
return ret, contract.Gas, err

‎core/vm/interpreter.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ func NewInterpreter(evm *EVM, cfg Config) *Interpreter {
8989
func (in *Interpreter) enforceRestrictions(op OpCode, operation operation, stack *Stack) error {
9090
if in.evm.chainRules.IsMetropolis {
9191
if in.readonly {
92-
// if the interpreter is operating in readonly mode, make sure no
93-
// state-modifying operation is performed. The 4th stack item
92+
// If the interpreter is operating in readonly mode, make sure no
93+
// state-modifying operation is performed. The 3rd stack item
9494
// for a call operation is the value. Transfering value from one
9595
// account to the others means the state is modified and should also
9696
// return with an error.
97-
if operation.writes ||
98-
((op == CALL || op == CALLCODE) && stack.Back(3).BitLen() > 0) {
97+
if operation.writes || (op == CALL && stack.Back(2).BitLen() > 0) {
9998
return errWriteProtection
10099
}
101100
}

0 commit comments

Comments
 (0)