diff --git a/nimbus/vm/evmc_host.nim b/nimbus/vm/evmc_host.nim index 72c7d6f91..6686dbd22 100644 --- a/nimbus/vm/evmc_host.nim +++ b/nimbus/vm/evmc_host.nim @@ -116,25 +116,20 @@ proc hostEmitLogImpl(ctx: Computation, address: EthAddress, for i in 0 ..< topicsCount: log.topics[i] = topics[i].bytes - if dataSize > 0: - log.data = newSeq[byte](dataSize) - copyMem(log.data[0].addr, data, dataSize) - + log.data = @makeOpenArray(data, dataSize) log.address = address ctx.addLogEntry(log) template createImpl(c: Computation, m: nimbus_message, res: nimbus_result) = # TODO: use evmc_message to evoid copy - var childMsg = Message( + let childMsg = Message( kind: CallKind(m.kind), depth: m.depth, gas: m.gas, sender: m.sender, - value: Uint256.fromEvmc(m.value) + value: Uint256.fromEvmc(m.value), + data: @makeOpenArray(m.inputData, m.inputSize.int) ) - if m.input_size.int > 0: - childMsg.data = newSeq[byte](m.input_size.int) - copyMem(childMsg.data[0].addr, m.input_data, m.input_size.int) let child = newComputation(c.vmState, childMsg, Uint256.fromEvmc(m.create2_salt)) child.execCreate() @@ -149,13 +144,14 @@ template createImpl(c: Computation, m: nimbus_message, res: nimbus_result) = 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 = hostReleaseResultImpl template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) = - var childMsg = Message( + let childMsg = Message( kind: CallKind(m.kind), depth: m.depth, gas: m.gas, @@ -163,13 +159,10 @@ template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) = codeAddress: m.destination, contractAddress: if m.kind == EVMC_CALL: m.destination else: c.msg.contractAddress, value: Uint256.fromEvmc(m.value), + data: @makeOpenArray(m.inputData, m.inputSize.int) flags: MsgFlags(m.flags) ) - if m.input_size.int > 0: - childMsg.data = newSeq[byte](m.input_size.int) - copyMem(childMsg.data[0].addr, m.input_data, m.input_size.int) - let child = newComputation(c.vmState, childMsg) child.execCall() @@ -183,6 +176,7 @@ template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) = 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) diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index b1c9bf42f..1efd15a2a 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -7,7 +7,7 @@ import strformat, times, sets, stew/ranges, sequtils, options, - chronicles, stint, nimcrypto, stew/ranges/typedranges, eth/common, + chronicles, stint, nimcrypto, stew/ranges/[typedranges, ptr_arith], eth/common, ./utils/[macros_procs_opcodes, utils_numeric], ./gas_meter, ./gas_costs, ./opcode_values, ./vm_forks, ../memory, ../message, ../stack, ../code_stream, ../computation, @@ -606,10 +606,7 @@ template genCreate(callName: untyped, opCode: Op): untyped = ) var res = c.host.call(msg) - if res.output_size.int > 0: - c.returnData = newSeq[byte](res.output_size.int) - copyMem(c.returnData[0].addr, res.output_data, c.returnData.len) - + c.returnData = @makeOpenArray(res.outputData, res.outputSize.int) c.gasMeter.returnGas(res.gas_left) if res.status_code == EVMC_SUCCESS: @@ -775,9 +772,7 @@ template genCall(callName: untyped, opCode: Op): untyped = ) var res = c.host.call(msg) - if res.output_size.int > 0: - c.returnData = newSeq[byte](res.output_size.int) - copyMem(c.returnData[0].addr, res.output_data, c.returnData.len) + c.returnData = @makeOpenArray(res.outputData, res.outputSize.int) let actualOutputSize = min(memOutLen, c.returnData.len) if actualOutputSize > 0: diff --git a/nimbus/vm/precompiles.nim b/nimbus/vm/precompiles.nim index 9c4951006..3fe08b642 100644 --- a/nimbus/vm/precompiles.nim +++ b/nimbus/vm/precompiles.nim @@ -245,10 +245,8 @@ proc bn256ecAdd*(computation: Computation, fork: Fork = FkByzantium) = input: array[128, byte] output: array[64, byte] # Padding data - let msglen = len(computation.msg.data) - let tocopy = if msglen < 128: msglen else: 128 - if tocopy > 0: - copyMem(addr input[0], addr computation.msg.data[0], tocopy) + let len = min(computation.msg.data.len, 128) - 1 + input[0..len] = computation.msg.data[0..len] var p1 = G1.getPoint(input.toOpenArray(0, 63)) var p2 = G1.getPoint(input.toOpenArray(64, 127)) var apo = (p1 + p2).toAffine() @@ -267,11 +265,8 @@ proc bn256ecMul*(computation: Computation, fork: Fork = FkByzantium) = output: array[64, byte] # Padding data - let msglen = len(computation.msg.data) - let tocopy = if msglen < 96: msglen else: 96 - if tocopy > 0: - copyMem(addr input[0], addr computation.msg.data[0], tocopy) - + let len = min(computation.msg.data.len, 96) - 1 + input[0..len] = computation.msg.data[0..len] var p1 = G1.getPoint(input.toOpenArray(0, 63)) var fr = getFR(input.toOpenArray(64, 95)) var apo = (p1 * fr).toAffine()