2021-04-08 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-03-21 11:24:32 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-08 14:52:10 +00:00
|
|
|
# Licensed under either of
|
2024-06-07 08:24:32 +00:00
|
|
|
# * 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.
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
# Type managing the EVM stack that comprises of 1024 256-bit words.
|
|
|
|
#
|
|
|
|
# The stack is a hot spot in EVM execution since it's used for practically every
|
|
|
|
# opcode. We use custom-allocated memory for several reasons, chiefly
|
|
|
|
# performance (at the time of writing, using a seq carried about 5% overhead on
|
|
|
|
# total EVM execution time):
|
|
|
|
#
|
|
|
|
# * no zeromem - the way the EVM uses the stack, it always writes full words
|
|
|
|
# meaning that whatever zeroing was done gets overwritten anyway - compilers
|
|
|
|
# are typically not smart enough to get rid of all of this
|
|
|
|
# * no reallocation - since we can allocate memory without zeroing, we can
|
|
|
|
# allocate the full stack length on creation and never grow / reallocate
|
|
|
|
# * less redundant range checking - we have to perform range checks manually and
|
|
|
|
# the compiler is not able to remove them consistently even though we range
|
|
|
|
# check manually
|
|
|
|
# * 32-byte alignment helps vector instruction optimization
|
|
|
|
#
|
|
|
|
# After calling `init`, the stack must be freed manually using `dispose`!
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
{.push raises: [].}
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
import
|
2024-11-30 09:07:10 +00:00
|
|
|
system/ansi_c,
|
|
|
|
stew/[assign2, ptrops],
|
2024-10-16 01:34:12 +00:00
|
|
|
stint,
|
|
|
|
eth/common/[base, addresses, hashes],
|
2024-11-30 09:07:10 +00:00
|
|
|
std/typetraits,
|
2024-06-07 08:24:32 +00:00
|
|
|
./evm_errors,
|
|
|
|
./interpreter/utils/utils_numeric
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
const evmStackSize = 1024
|
|
|
|
## https://ethereum.org/en/developers/docs/evm/#evm-instructions
|
|
|
|
|
2021-04-23 09:45:50 +00:00
|
|
|
type
|
2024-07-13 18:42:49 +00:00
|
|
|
EvmStack* = ref object
|
2024-11-30 09:07:10 +00:00
|
|
|
values: ptr EvmStackElement
|
|
|
|
memory: pointer
|
|
|
|
len*: int
|
|
|
|
|
|
|
|
EvmStackElement = object
|
|
|
|
data {.align: 32.}: UInt256
|
2021-04-23 09:45:50 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
EvmStackInts = uint64 | uint | int | GasInt
|
2021-04-23 09:45:50 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
static:
|
|
|
|
# A few sanity checks because we skip the GC / parts of the nim type system:
|
|
|
|
doAssert sizeof(UInt256) == 32, "no padding etc"
|
|
|
|
doAssert supportsCopyMem(EvmStackElement), "byte-based ops must work sanely"
|
2023-04-24 20:59:38 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
template `[]`*(s: EvmStack, i: int): EvmStackElement =
|
|
|
|
s.values.offset(i)[]
|
|
|
|
|
|
|
|
template `[]`*(s: EvmStack, i: BackwardsIndex): EvmStackElement =
|
|
|
|
s.values.offset(s.len - int(i))[]
|
|
|
|
|
|
|
|
template `[]=`*(s: EvmStack, i: int, v: EvmStackElement) =
|
|
|
|
assign(s[i], v)
|
|
|
|
|
|
|
|
template `[]=`*(s: EvmStack, i: BackwardsIndex, v: EvmStackElement) =
|
|
|
|
assign(s[i], v)
|
|
|
|
|
|
|
|
template toStackElem(v: EvmStackElement, elem: EvmStackElement) =
|
2024-06-07 08:24:32 +00:00
|
|
|
elem = v
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
template toStackElem(v: UInt256, elem: EvmStackElement) =
|
|
|
|
elem.data = v
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template toStackElem(v: EvmStackInts, elem: EvmStackElement) =
|
2024-11-30 09:07:10 +00:00
|
|
|
elem.data = v.u256
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
template toStackElem(v: Address, elem: EvmStackElement) =
|
2024-11-30 09:07:10 +00:00
|
|
|
elem.data.initFromBytesBE(v.data)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
template toStackElem(v: Hash32, elem: EvmStackElement) =
|
2024-11-30 09:07:10 +00:00
|
|
|
elem.data.initFromBytesBE(v.data)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template toStackElem(v: openArray[byte], elem: EvmStackElement) =
|
2024-11-30 09:07:10 +00:00
|
|
|
elem.data.initFromBytesBE(v)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template fromStackElem(elem: EvmStackElement, _: type UInt256): UInt256 =
|
2024-11-30 09:07:10 +00:00
|
|
|
elem.data
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
func fromStackElem(elem: EvmStackElement, _: type Address): Address =
|
2024-11-30 09:07:10 +00:00
|
|
|
elem.data.to(Bytes32).to(Address)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
template fromStackElem(elem: EvmStackElement, _: type Hash32): Hash32 =
|
2024-11-30 09:07:10 +00:00
|
|
|
Hash32(elem.data.toBytesBE())
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
template fromStackElem(elem: EvmStackElement, _: type Bytes32): Bytes32 =
|
|
|
|
elem.data.toBytesBE().to(Bytes32)
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func ensurePop(stack: EvmStack, expected: int): EvmResultVoid =
|
2024-11-30 09:07:10 +00:00
|
|
|
if stack.len < expected:
|
2024-06-07 08:24:32 +00:00
|
|
|
return err(stackErr(StackInsufficient))
|
|
|
|
ok()
|
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
func popAux(stack: EvmStack, T: type): EvmResult[T] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
2024-11-30 09:07:10 +00:00
|
|
|
stack.len -= 1
|
|
|
|
ok(fromStackElem(stack[stack.len], T))
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
func push*(stack: EvmStack,
|
|
|
|
value: EvmStackElement | EvmStackInts | UInt256 | Address | Hash32): EvmResultVoid =
|
|
|
|
let len = stack.len
|
|
|
|
if len > 1023:
|
|
|
|
return err(stackErr(StackFull))
|
|
|
|
toStackElem(value, stack[len])
|
|
|
|
stack.len = len + 1
|
|
|
|
ok()
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
func popInt*(stack: EvmStack): EvmResult[UInt256] =
|
2024-06-07 08:24:32 +00:00
|
|
|
popAux(stack, UInt256)
|
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
func popAddress*(stack: EvmStack): EvmResult[Address] =
|
2024-10-16 01:34:12 +00:00
|
|
|
popAux(stack, Address)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
proc init*(_: type EvmStack): EvmStack =
|
|
|
|
let memory = c_malloc(evmStackSize * sizeof(EvmStackElement) + 31)
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
EvmStack(
|
2024-11-30 09:07:10 +00:00
|
|
|
values: cast[ptr EvmStackElement](((cast[uint](memory) + 31) div 32) * 32) ,
|
|
|
|
memory: memory, # Need to free the same pointer that we got from malloc
|
|
|
|
len: 0,
|
2024-07-13 18:42:49 +00:00
|
|
|
)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
proc dispose*(stack: EvmStack) =
|
|
|
|
if stack[].memory != nil:
|
|
|
|
c_free(stack[].memory)
|
|
|
|
stack[].reset()
|
|
|
|
|
|
|
|
func swap*(stack: EvmStack, position: static int): EvmResultVoid =
|
|
|
|
## Swap the `top` and `top - position` items
|
|
|
|
let
|
|
|
|
idx = position + 1 # locals help compiler reason about overflows
|
|
|
|
len = stack.len
|
|
|
|
if stack.len >= idx:
|
|
|
|
let
|
|
|
|
l1 = len - 1
|
|
|
|
li = len - idx
|
|
|
|
let tmp {.noinit.} = stack[l1]
|
|
|
|
stack[l1] = stack[li]
|
|
|
|
stack[li] = tmp
|
2024-06-07 08:24:32 +00:00
|
|
|
ok()
|
2021-04-08 14:52:10 +00:00
|
|
|
else:
|
2024-06-07 08:24:32 +00:00
|
|
|
err(stackErr(StackInsufficient))
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
func dup*(stack: EvmStack, position: int): EvmResultVoid =
|
|
|
|
## Push copy of item at `top - position`
|
2021-04-08 14:52:10 +00:00
|
|
|
if position in 1 .. stack.len:
|
2024-11-30 09:07:10 +00:00
|
|
|
stack.push(stack[^position])
|
2021-04-08 14:52:10 +00:00
|
|
|
else:
|
2024-06-07 08:24:32 +00:00
|
|
|
err(stackErr(StackInsufficient))
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func peek*(stack: EvmStack): EvmResult[UInt256] =
|
2024-11-30 09:07:10 +00:00
|
|
|
? ensurePop(stack, 1)
|
|
|
|
ok(fromStackElem(stack[^1], UInt256))
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func peekSafeInt*(stack: EvmStack): EvmResult[int] =
|
2024-11-30 09:07:10 +00:00
|
|
|
? ensurePop(stack, 1)
|
|
|
|
ok(fromStackElem(stack[^1], UInt256).safeInt)
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func `[]`*(stack: EvmStack, i: BackwardsIndex, T: typedesc): EvmResult[T] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, int(i))
|
2024-11-30 09:07:10 +00:00
|
|
|
ok(fromStackElem(stack[i], T))
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func peekInt*(stack: EvmStack): EvmResult[UInt256] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
2024-11-30 09:07:10 +00:00
|
|
|
ok(fromStackElem(stack[^1], UInt256))
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
func peekAddress*(stack: EvmStack): EvmResult[Address] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
2024-11-30 09:07:10 +00:00
|
|
|
ok(fromStackElem(stack[^1], Address))
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func top*(stack: EvmStack,
|
2024-10-16 01:34:12 +00:00
|
|
|
value: EvmStackInts | UInt256 | Address | Hash32): EvmResultVoid =
|
2024-11-30 09:07:10 +00:00
|
|
|
? ensurePop(stack, 1)
|
|
|
|
toStackElem(value, stack[^1])
|
2024-06-07 08:24:32 +00:00
|
|
|
ok()
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
iterator items*(stack: EvmStack): UInt256 =
|
2024-11-30 09:07:10 +00:00
|
|
|
for i in 0..<stack.len:
|
|
|
|
yield stack[i].data
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
iterator pairs*(stack: EvmStack): (int, UInt256) =
|
2024-11-30 09:07:10 +00:00
|
|
|
for i in 0..<stack.len:
|
|
|
|
yield (i, stack[i].data)
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions with less safety
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template lsCheck*(stack: EvmStack, expected: int): EvmResultVoid =
|
|
|
|
ensurePop(stack, expected)
|
|
|
|
|
|
|
|
func lsTop*(stack: EvmStack,
|
2024-10-16 01:34:12 +00:00
|
|
|
value: EvmStackInts | UInt256 | Address | Hash32) =
|
2024-11-30 09:07:10 +00:00
|
|
|
toStackElem(value, stack[^1])
|
2024-07-18 11:59:53 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
func lsTop*(stack: EvmStack, value: openArray[byte]) =
|
|
|
|
toStackElem(value, stack[^1])
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
func lsPeekInt*(stack: EvmStack, i: BackwardsIndex): UInt256 =
|
2024-11-30 09:07:10 +00:00
|
|
|
fromStackElem(stack[i], UInt256)
|
2024-07-18 11:59:53 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
func lsPeekAddress*(stack: EvmStack, i: BackwardsIndex): Address =
|
2024-11-30 09:07:10 +00:00
|
|
|
fromStackElem(stack[i], Address)
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
func lsPeekMemRef*(stack: EvmStack, i: BackwardsIndex): int =
|
2024-11-30 09:07:10 +00:00
|
|
|
fromStackElem(stack[i], UInt256).cleanMemRef
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
func lsPeekSafeInt*(stack: EvmStack, i: BackwardsIndex): int =
|
2024-11-30 09:07:10 +00:00
|
|
|
fromStackElem(stack[i], UInt256).safeInt
|
2024-07-18 11:59:53 +00:00
|
|
|
|
2024-11-30 09:07:10 +00:00
|
|
|
func lsPeekTopic*(stack: EvmStack, i: BackwardsIndex): Bytes32 =
|
|
|
|
fromStackElem(stack[i], Bytes32)
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
func lsShrink*(stack: EvmStack, x: int) =
|
2024-11-30 09:07:10 +00:00
|
|
|
stack.len -= x
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
template binaryOp*(stack: EvmStack, binOp): EvmResultVoid =
|
2024-11-30 09:07:10 +00:00
|
|
|
let len = stack.len
|
|
|
|
if len >= 2:
|
|
|
|
let
|
|
|
|
l1 = len - 1
|
|
|
|
l2 = len - 2
|
|
|
|
stack[l2].data = binOp(stack[l1].data, stack[l2].data)
|
|
|
|
stack.len = l1
|
2024-07-18 11:59:53 +00:00
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template unaryOp*(stack: EvmStack, unOp): EvmResultVoid =
|
2024-11-30 09:07:10 +00:00
|
|
|
let len = stack.len
|
|
|
|
if len >= 1:
|
|
|
|
let l1 = len - 1
|
|
|
|
stack[l1].data = unOp(stack[l1].data)
|
2024-07-18 11:59:53 +00:00
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template binaryWithTop*(stack: EvmStack, binOp): EvmResultVoid =
|
2024-11-30 09:07:10 +00:00
|
|
|
let len = stack.len
|
|
|
|
if len >= 2:
|
|
|
|
let
|
|
|
|
l1 = len - 1
|
|
|
|
l2 = len - 2
|
|
|
|
binOp(stack[l2].data, stack[l1].data, stack[l2].data)
|
|
|
|
stack.len = l1
|
2024-07-18 11:59:53 +00:00
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template unaryWithTop*(stack: EvmStack, unOp): EvmResultVoid =
|
2024-11-30 09:07:10 +00:00
|
|
|
let len = stack.len
|
|
|
|
if len >= 1:
|
|
|
|
let l1 = len - 1
|
|
|
|
unOp(stack[l1], stack[l1].data, toStackElem)
|
2024-07-18 11:59:53 +00:00
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template unaryAddress*(stack: EvmStack, unOp): EvmResultVoid =
|
2024-11-30 09:07:10 +00:00
|
|
|
let len = stack.len
|
|
|
|
if len >= 1:
|
|
|
|
let l1 = len - 1
|
|
|
|
let address = fromStackElem(stack[l1], Address)
|
|
|
|
toStackElem(unOp(address), stack[l1])
|
2024-07-18 11:59:53 +00:00
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|