consistent evm depth call check

This commit is contained in:
andri lim 2020-01-15 08:51:41 +07:00 committed by zah
parent 009aa35dfb
commit 89d1834d52
2 changed files with 15 additions and 13 deletions

View File

@ -144,11 +144,6 @@ proc postExecuteVM(c: Computation, opCode: static[Op]) {.gcsafe.} =
proc executeOpcodes*(c: Computation) {.gcsafe.}
proc applyMessage*(c: Computation, opCode: static[Op]) =
if c.msg.depth > MaxCallDepth:
c.setError(&"Stack depth limit reached depth={c.msg.depth}")
c.nextProc()
return
c.snapshot()
defer:
c.dispose()

View File

@ -528,10 +528,7 @@ proc canTransfer(c: Computation, memPos, memLen: int, value: Uint256, opCode: st
debug "Computation Failure", reason = "Insufficient funds available to transfer", required = c.msg.value, balance = senderBalance
return false
# unlike the other MaxCallDepth comparison,
# this one has not been entered child computation
# thats why it has `+ 1`
if c.msg.depth + 1 > MaxCallDepth:
if c.msg.depth >= MaxCallDepth:
debug "Computation Failure", reason = "Stack too deep", maximumDepth = MaxCallDepth, depth = c.msg.depth
return false
@ -732,6 +729,12 @@ template genCall(callName: untyped, opCode: Op): untyped =
if childGasFee >= 0:
c.gasMeter.consumeGas(childGasFee, reason = $opCode)
if c.msg.depth >= MaxCallDepth:
debug "Computation Failure", reason = "Stack too deep", maximumDepth = MaxCallDepth, depth = c.msg.depth
# return unused gas
c.gasMeter.returnGas(childGasLimit)
return
if childGasFee < 0 and childGasLimit <= 0:
raise newException(OutOfGas, "Gas not enough to perform calculation (" & callNameStr & ")")
@ -767,8 +770,16 @@ template genCall(callName: untyped, opCode: Op): untyped =
## CALLCODE, 0xf2, Message-call into this account with an alternative account's code.
## DELEGATECALL, 0xf4, Message-call into this account with an alternative account's code, but persisting the current values for sender and value.
## STATICCALL, 0xfa, Static message-call into an account.
when opCode == Call:
if emvcStatic == c.msg.flags and c.stack[^3, Uint256] > 0.u256:
raise newException(StaticContextError, "Cannot modify state while inside of a STATICCALL context")
var child = `callName Setup`(c, callName.astToStr)
if child.isNil:
push: 0
return
continuation(child):
addChildComputation(c, child)
@ -783,10 +794,6 @@ template genCall(callName: untyped, opCode: Op): untyped =
c.memOutPos,
child.output.toOpenArray(0, actualOutputSize - 1))
when opCode == Call:
if emvcStatic == c.msg.flags and child.msg.value > 0.u256:
raise newException(StaticContextError, "Cannot modify state while inside of a STATICCALL context")
child.applyMessage(opCode)
genCall(call, Call)