2021-04-08 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-06-07 08: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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
2021-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
import
|
2024-06-18 00:55:35 +00:00
|
|
|
stew/assign2,
|
2024-06-07 08:24:32 +00:00
|
|
|
./evm_errors,
|
2023-04-24 20:59:38 +00:00
|
|
|
./interpreter/utils/utils_numeric
|
2023-04-22 14:17:37 +00:00
|
|
|
|
|
|
|
type
|
2024-06-07 08:24:32 +00:00
|
|
|
EvmMemoryRef* = ref object
|
2023-04-24 20:59:38 +00:00
|
|
|
bytes*: seq[byte]
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func new*(_: type EvmMemoryRef): EvmMemoryRef =
|
2021-04-08 14:52:10 +00:00
|
|
|
new(result)
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func len*(memory: EvmMemoryRef): int =
|
2024-06-18 00:55:35 +00:00
|
|
|
memory.bytes.len
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-18 00:55:35 +00:00
|
|
|
func extend*(memory: EvmMemoryRef; startPos, size: int) =
|
|
|
|
if size <= 0:
|
2021-04-08 14:52:10 +00:00
|
|
|
return
|
2023-05-30 16:15:04 +00:00
|
|
|
let newSize = ceil32(startPos + size)
|
2021-04-08 14:52:10 +00:00
|
|
|
if newSize <= len(memory):
|
|
|
|
return
|
2023-05-30 16:15:04 +00:00
|
|
|
memory.bytes.setLen(newSize)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func new*(_: type EvmMemoryRef, size: Natural): EvmMemoryRef =
|
|
|
|
result = EvmMemoryRef.new()
|
2021-04-08 14:52:10 +00:00
|
|
|
result.extend(0, size)
|
|
|
|
|
2024-06-18 00:55:35 +00:00
|
|
|
template read*(memory: EvmMemoryRef, startPos, size: int): openArray[byte] =
|
|
|
|
memory.bytes.toOpenArray(startPos, startPos + size - 1)
|
2023-04-24 20:59:38 +00:00
|
|
|
|
2024-06-18 00:55:35 +00:00
|
|
|
template read32Bytes*(memory: EvmMemoryRef, startPos: int): openArray[byte] =
|
2024-06-07 08:24:32 +00:00
|
|
|
memory.bytes.toOpenArray(startPos, startPos + 31)
|
|
|
|
|
2022-09-26 04:56:54 +00:00
|
|
|
when defined(evmc_enabled):
|
2024-06-07 08:24:32 +00:00
|
|
|
func readPtr*(memory: EvmMemoryRef, startPos: Natural): ptr byte =
|
2023-04-24 20:59:38 +00:00
|
|
|
if memory.bytes.len == 0 or startPos >= memory.bytes.len: return
|
|
|
|
result = memory.bytes[startPos].addr
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func write*(memory: EvmMemoryRef, startPos: Natural, value: openArray[byte]): EvmResultVoid =
|
2023-04-24 20:59:38 +00:00
|
|
|
let size = value.len
|
|
|
|
if size == 0:
|
|
|
|
return
|
2024-06-07 08:24:32 +00:00
|
|
|
if startPos + size > memory.len:
|
|
|
|
return err(memErr(MemoryFull))
|
2024-06-18 00:55:35 +00:00
|
|
|
|
|
|
|
assign(memory.bytes.toOpenArray(startPos, int(startPos + size) - 1), value)
|
2024-06-07 08:24:32 +00:00
|
|
|
ok()
|
2023-06-26 09:58:59 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func write*(memory: EvmMemoryRef, startPos: Natural, value: byte): EvmResultVoid =
|
|
|
|
if startPos + 1 > memory.len:
|
|
|
|
return err(memErr(MemoryFull))
|
2023-08-28 11:04:22 +00:00
|
|
|
memory.bytes[startPos] = value
|
2024-06-07 08:24:32 +00:00
|
|
|
ok()
|
2023-08-28 11:04:22 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func copy*(memory: EvmMemoryRef, dst, src, len: Natural) =
|
2023-06-26 09:58:59 +00:00
|
|
|
if len <= 0: return
|
|
|
|
memory.extend(max(dst, src), len)
|
|
|
|
if dst == src:
|
|
|
|
return
|
2024-06-18 00:55:35 +00:00
|
|
|
assign(
|
|
|
|
memory.bytes.toOpenArray(dst, dst + len - 1),
|
|
|
|
memory.bytes.toOpenArray(src, src + len - 1))
|
2023-08-28 11:04:22 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func writePadded*(memory: EvmMemoryRef, data: openArray[byte],
|
2023-08-28 11:04:22 +00:00
|
|
|
memPos, dataPos, len: Natural) =
|
|
|
|
|
|
|
|
memory.extend(memPos, len)
|
|
|
|
let
|
2024-06-18 00:55:35 +00:00
|
|
|
dataEndPos = dataPos + len
|
2023-08-28 11:04:22 +00:00
|
|
|
dataStart = min(dataPos, data.len)
|
|
|
|
dataEnd = min(data.len, dataEndPos)
|
|
|
|
dataLen = dataEnd - dataStart
|
|
|
|
padStart = min(memPos + dataLen, memory.len)
|
|
|
|
numPad = min(memory.len - padStart, len - dataLen)
|
|
|
|
padEnd = padStart + numPad
|
|
|
|
|
|
|
|
var
|
|
|
|
di = dataStart
|
|
|
|
mi = memPos
|
|
|
|
|
2024-06-18 00:55:35 +00:00
|
|
|
assign(
|
|
|
|
memory.bytes.toOpenArray(mi, mi + dataLen - 1),
|
|
|
|
data.toOpenArray(di, di + dataLen - 1))
|
|
|
|
mi += dataLen
|
2023-08-28 11:04:22 +00:00
|
|
|
|
|
|
|
# although memory.extend already pad new block of memory
|
|
|
|
# with zeros, it can be rewrite by some opcode
|
|
|
|
# so we need to clean the garbage if current op supply us with
|
|
|
|
# `data` shorter than `len`
|
|
|
|
while mi < padEnd:
|
|
|
|
memory.bytes[mi] = 0.byte
|
|
|
|
inc mi
|