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-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)
|
2023-04-24 20:59:38 +00:00
|
|
|
result.bytes = @[]
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func len*(memory: EvmMemoryRef): int =
|
2023-04-24 20:59:38 +00:00
|
|
|
result = memory.bytes.len
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func extend*(memory: EvmMemoryRef; startPos: Natural; size: Natural) =
|
2021-04-08 14:52:10 +00:00
|
|
|
if size == 0:
|
|
|
|
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-07 08:24:32 +00:00
|
|
|
func read*(memory: EvmMemoryRef, startPos: Natural, size: Natural): seq[byte] =
|
2023-07-31 10:42:07 +00:00
|
|
|
result = newSeq[byte](size)
|
|
|
|
if size > 0:
|
|
|
|
copyMem(result[0].addr, memory.bytes[startPos].addr, size)
|
2023-04-24 20:59:38 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template read32Bytes*(memory: EvmMemoryRef, startPos): auto =
|
|
|
|
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))
|
2023-04-24 20:59:38 +00:00
|
|
|
for z, b in value:
|
|
|
|
memory.bytes[z + startPos] = b
|
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
|
|
|
|
elif dst < src:
|
|
|
|
for i in 0..<len:
|
|
|
|
memory.bytes[dst+i] = memory.bytes[src+i]
|
|
|
|
else: # src > dst
|
|
|
|
for i in countdown(len-1, 0):
|
|
|
|
memory.bytes[dst+i] = memory.bytes[src+i]
|
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
|
|
|
|
dataEndPos = dataPos.int64 + len
|
|
|
|
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
|
|
|
|
|
|
|
|
while di < dataEnd:
|
|
|
|
memory.bytes[mi] = data[di]
|
|
|
|
inc di
|
|
|
|
inc mi
|
|
|
|
|
|
|
|
# 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
|