Transaction: Just enough support to work with nested calls

Proper nested call functionality is being skipped in this iteration of new EVMC
host code to keep it simpler, to allow testing and architecture to be built
around the less complicated non-nested cases first.

Instead, nested calls use the old `Computation` path, and bypass any
third-party EVM that may be loaded.  The results are the same, and mixing
different EVMs in this way is actually permitted in the EVMC specification.

This approach also means third-party EVMs we test don't need to support
precompiles and we don't need to specially handle those cases.
(E.g. "evmone" doesn't support precompiles, just EVM1 opcodes).

(These before/after scope actions are approximately copy-pasted from
`nimbus/vm/evmc_host.nim`, making their detailed behaviour "obviously correct".
Of course they are subject to tests as well.  The small stack property of
a3c8a5c3 "EVMC: Small stacks when using EVMC, closes #575 (segfaults)" is
carefully retained.)

Signed-off-by: Jamie Lokier <jamie@shareable.org>
This commit is contained in:
Jamie Lokier 2021-05-24 09:53:53 +01:00
parent 97b4aa5619
commit 6d4205b0b0
No known key found for this signature in database
GPG Key ID: CBC25C68435C30A2
2 changed files with 137 additions and 3 deletions

View File

@ -0,0 +1,135 @@
# Nimbus - Services available to EVM code that is run for a transaction
#
# Copyright (c) 2019-2021 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
#{.push raises: [Defect].}
import
sets, stint, chronicles, stew/ranges/ptr_arith,
eth/common/eth_types,
".."/[vm_types, vm_computation],
./host_types
proc evmcResultRelease(res: var EvmcResult) {.cdecl, gcsafe.} =
dealloc(res.output_data)
proc beforeExecCreateEvmcNested(host: TransactionHost,
m: EvmcMessage): Computation {.inline.} =
# TODO: use evmc_message to avoid copy
let childMsg = Message(
kind: CallKind(m.kind),
depth: m.depth,
gas: m.gas,
sender: m.sender.fromEvmc,
value: m.value.fromEvmc,
data: @(makeOpenArray(m.inputData, m.inputSize.int))
)
# TODO: Salt should maybe change endianness when called via EVMC glue.
return newComputation(host.vmState, childMsg, m.create2_salt.fromEvmc)
proc afterExecCreateEvmcNested(host: TransactionHost, child: Computation,
res: var EvmcResult) {.inline.} =
if not child.shouldBurnGas:
res.gas_left = child.gasMeter.gasRemaining
if child.isSuccess:
host.computation.merge(child)
res.status_code = EVMC_SUCCESS
res.create_address = child.msg.contractAddress.toEvmc
else:
res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT
if child.output.len > 0:
# TODO: can we move the ownership of seq to raw pointer?
res.output_size = child.output.len.uint
res.output_data = cast[ptr byte](alloc(child.output.len))
copyMem(res.output_data, child.output[0].addr, child.output.len)
res.release = evmcResultRelease
proc beforeExecCallEvmcNested(host: TransactionHost,
m: EvmcMessage): Computation {.inline.} =
let childMsg = Message(
kind: CallKind(m.kind),
depth: m.depth,
gas: m.gas,
sender: m.sender.fromEvmc,
codeAddress: m.destination.fromEvmc,
contractAddress: if m.kind == EVMC_CALL:
m.destination.fromEvmc
else:
host.computation.msg.contractAddress,
value: m.value.fromEvmc,
data: @(makeOpenArray(m.inputData, m.inputSize.int)),
flags: if m.isStatic: emvcStatic else: emvcNoFlags,
)
return newComputation(host.vmState, childMsg)
proc afterExecCallEvmcNested(host: TransactionHost, child: Computation,
res: var EvmcResult) {.inline.} =
if not child.shouldBurnGas:
res.gas_left = child.gasMeter.gasRemaining
if child.isSuccess:
host.computation.merge(child)
res.status_code = EVMC_SUCCESS
else:
res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT
if child.output.len > 0:
# TODO: can we move the ownership of seq to raw pointer?
res.output_size = child.output.len.uint
res.output_data = cast[ptr byte](alloc(child.output.len))
copyMem(res.output_data, child.output[0].addr, child.output.len)
res.release = evmcResultRelease
# The next three functions are designed so `callEvmcNested` uses very small C
# stack usage for each level of nested EVM calls.
#
# To keep the C stack usage small when there are deeply nested EVM calls,
# `callEvmcNested` must use as little stack as possible, going from the EVM
# which calls it to the nested EVM which it calls.
#
# First, `callEvmcNested` itself is `template` so it is inlined to the caller
# at Nim level, not C level. Only at Nim level is inlining guaranteed across
# `import`. This saves a C stack frame, which matters because some C compilers
# reserve space for 1-3 copies of the large `EvmcResult` return value.
#
# Second, the complicated parts of preparation and return are done in
# out-of-line functions `beforeExecEvmcNested` and `afterExecEvmcNested`. They
# are annotated with `{.noinline.}` to make sure they are out-of-line. The
# annotation ensures they don't contribute to the stack frame of
# `callEvmcNested`, because otherwise the compiler can optimistically inline.
# (Even across modules when using `-flto`).
#
# The functions `beforeExecEvmcNested` and `afterExecEvmcNested` can use as
# much stack as they like.
proc beforeExecEvmcNested(host: TransactionHost, msg: EvmcMessage): Computation
# This function must be declared with `{.noinline.}` to make sure it doesn't
# contribute to the stack frame of `callEvmcNested` below.
{.noinline.} =
if msg.kind == EVMC_CREATE or msg.kind == EVMC_CREATE2:
return beforeExecCreateEvmcNested(host, msg)
else:
return beforeExecCallEvmcNested(host, msg)
proc afterExecEvmcNested(host: TransactionHost, child: Computation,
kind: EvmcCallKind): EvmcResult
# This function must be declared with `{.noinline.}` to make sure it doesn't
# contribute to the stack frame of `callEvmcNested` below.
{.noinline.} =
if kind == EVMC_CREATE or kind == EVMC_CREATE2:
afterExecCreateEvmcNested(host, child, result)
else:
afterExecCallEvmcNested(host, child, result)
template callEvmcNested*(host: TransactionHost, msg: EvmcMessage): EvmcResult =
# This function must be declared `template` to ensure it is inlined at Nim
# level to its caller across `import`. C level `{.inline.}` won't do this.
# Note that template parameters `host` and `msg` are multiple-evaluated.
let child = beforeExecEvmcNested(host, msg)
child.execCallOrCreate()
afterExecEvmcNested(host, child, msg.kind)

View File

@ -12,7 +12,7 @@ import
sets, times, stint, chronicles,
eth/common/eth_types, ../db/accounts_cache, ../forks,
".."/[vm_types, vm_state, vm_computation, vm_internals],
./host_types, ./host_trace
./host_types, ./host_trace, ./host_call_nested
proc setupTxContext(host: TransactionHost) =
# Conversion issues:
@ -221,8 +221,7 @@ proc selfDestruct(host: TransactionHost, address, beneficiary: HostAddress) {.sh
#host.selfDestructs.incl(address)
proc call(host: TransactionHost, msg: EvmcMessage): EvmcResult {.show.} =
echo "**** Nested call not implemented ****"
return EvmcResult(status_code: EVMC_REJECTED)
return host.callEvmcNested(msg)
proc getTxContext(host: TransactionHost): EvmcTxContext {.show.} =
if not host.cachedTxContext: