2021-04-08 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-02-15 02:57:05 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-08 14:52:10 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
import
|
2024-06-21 07:44:10 +00:00
|
|
|
std/[sequtils, strutils], chronicles, eth/common, ./interpreter/op_codes, ./code_bytes
|
|
|
|
|
|
|
|
export code_bytes
|
|
|
|
|
|
|
|
type CodeStream* = object
|
|
|
|
code: CodeBytesRef
|
|
|
|
pc*: int
|
|
|
|
|
|
|
|
func init*(T: type CodeStream, code: CodeBytesRef): T =
|
|
|
|
T(code: code)
|
|
|
|
|
|
|
|
func init*(T: type CodeStream, code: sink seq[byte]): T =
|
|
|
|
T(code: CodeBytesRef.init(move(code)))
|
|
|
|
|
|
|
|
func init*(T: type CodeStream, code: openArray[byte]): T =
|
|
|
|
T(code: CodeBytesRef.init(code))
|
|
|
|
|
|
|
|
func init*(T: type CodeStream, code: openArray[char]): T =
|
|
|
|
T(code: CodeBytesRef.init(code))
|
|
|
|
|
|
|
|
template read*(c: var CodeStream, size: int): openArray[byte] =
|
2021-04-08 14:52:10 +00:00
|
|
|
if c.pc + size - 1 < c.bytes.len:
|
2024-06-18 00:55:35 +00:00
|
|
|
let pos = c.pc
|
2021-04-08 14:52:10 +00:00
|
|
|
c.pc += size
|
2024-06-21 07:44:10 +00:00
|
|
|
c.code.bytes.toOpenArray(pos, pos + size - 1)
|
2021-04-08 14:52:10 +00:00
|
|
|
else:
|
|
|
|
c.pc = c.bytes.len
|
2024-06-21 07:44:10 +00:00
|
|
|
c.code.bytes.toOpenArray(0, -1)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func readVmWord*(c: var CodeStream, n: static int): UInt256 =
|
2021-04-08 14:52:10 +00:00
|
|
|
## Reads `n` bytes from the code stream and pads
|
|
|
|
## the remaining bytes with zeros.
|
|
|
|
let result_bytes = cast[ptr array[32, byte]](addr result)
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
let last = min(c.pc + n, c.code.bytes.len)
|
2021-04-08 14:52:10 +00:00
|
|
|
let toWrite = last - c.pc
|
2024-06-21 07:44:10 +00:00
|
|
|
for i in 0 ..< toWrite:
|
|
|
|
result_bytes[i] = c.code.bytes[last - i - 1]
|
2021-04-08 14:52:10 +00:00
|
|
|
c.pc = last
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func len*(c: CodeStream): int =
|
|
|
|
len(c.code)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func next*(c: var CodeStream): Op =
|
|
|
|
if c.pc != c.code.len:
|
|
|
|
result = Op(c.code.bytes[c.pc])
|
2021-04-08 14:52:10 +00:00
|
|
|
inc c.pc
|
|
|
|
else:
|
2024-06-18 00:55:35 +00:00
|
|
|
result = Op.Stop
|
2021-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
iterator items*(c: var CodeStream): Op =
|
|
|
|
var nextOpcode = c.next()
|
2024-06-18 00:55:35 +00:00
|
|
|
while nextOpcode != Op.Stop:
|
2021-04-08 14:52:10 +00:00
|
|
|
yield nextOpcode
|
|
|
|
nextOpcode = c.next()
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func `[]`*(c: CodeStream, offset: int): Op =
|
|
|
|
Op(c.code.bytes[offset])
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func peek*(c: var CodeStream): Op =
|
|
|
|
if c.pc < c.code.bytes.len:
|
|
|
|
Op(c.code.bytes[c.pc])
|
2021-04-08 14:52:10 +00:00
|
|
|
else:
|
2024-06-18 00:55:35 +00:00
|
|
|
Op.Stop
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func updatePc*(c: var CodeStream, value: int) =
|
2021-04-08 14:52:10 +00:00
|
|
|
c.pc = min(value, len(c))
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func isValidOpcode*(c: CodeStream, position: int): bool =
|
|
|
|
c.code.isValidOpcode(position)
|
|
|
|
|
|
|
|
func bytes*(c: CodeStream): lent seq[byte] =
|
|
|
|
c.code.bytes()
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-18 00:55:35 +00:00
|
|
|
proc decompile*(original: CodeStream): seq[(int, Op, string)] =
|
2021-04-08 14:52:10 +00:00
|
|
|
# behave as https://etherscan.io/opcode-tool
|
2024-06-21 07:44:10 +00:00
|
|
|
var c = CodeStream.init(original.bytes)
|
2021-04-08 14:52:10 +00:00
|
|
|
while true:
|
|
|
|
var op = c.next
|
2022-04-08 04:54:11 +00:00
|
|
|
if op >= Push1 and op <= Push32:
|
2024-06-18 00:55:35 +00:00
|
|
|
result.add(
|
2024-06-21 07:44:10 +00:00
|
|
|
(
|
|
|
|
c.pc - 1,
|
|
|
|
op,
|
|
|
|
"0x" & c.read(op.int - 95).mapIt($(it.BiggestInt.toHex(2))).join(""),
|
|
|
|
)
|
|
|
|
)
|
2021-04-08 14:52:10 +00:00
|
|
|
elif op != Op.Stop:
|
|
|
|
result.add((c.pc - 1, op, ""))
|
|
|
|
else:
|
2022-04-08 04:54:11 +00:00
|
|
|
result.add((-1, Op.Stop, ""))
|
2021-04-08 14:52:10 +00:00
|
|
|
break
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
func atEnd*(c: CodeStream): bool =
|
|
|
|
c.pc >= c.code.bytes.len
|