From 3fae6528c03543151773caae1ae6101df70bf901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 25 Apr 2017 13:36:24 +0200 Subject: [PATCH 01/10] VM: Add return data buffer --- libevm/VM.h | 3 +++ libevm/VMCalls.cpp | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/libevm/VM.h b/libevm/VM.h index 3adb958d06d..73fc6e7d8ec 100755 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -104,6 +104,9 @@ class VM: public VMFace // space for code bytes m_code; + /// Space for return data. + bytes m_returnData; + // space for data stack, grows towards smaller addresses from the end u256 m_stack[1024]; u256 *m_stackEnd = &m_stack[1024]; diff --git a/libevm/VMCalls.cpp b/libevm/VMCalls.cpp index fb7b8238df3..0322e4c28d1 100755 --- a/libevm/VMCalls.cpp +++ b/libevm/VMCalls.cpp @@ -120,6 +120,9 @@ void VM::caseCreate() uint64_t initOff = (uint64_t)m_SP[1]; uint64_t initSize = (uint64_t)m_SP[2]; + // Clear the return data buffer. This will not free the memory. + m_returnData.clear(); + if (m_ext->balance(m_ext->myAddress) >= endowment && m_ext->depth < 1024) { *m_io_gas_p = m_io_gas; @@ -139,12 +142,20 @@ void VM::caseCreate() void VM::caseCall() { m_bounce = &VM::interpretCases; + + // TODO: Please check if that does not actually increases the stack size. + // That was the case before. unique_ptr callParams(new CallParameters()); + + // Clear the return data buffer. This will not free the memory. + m_returnData.clear(); + bytesRef output; if (caseCallSetup(callParams.get(), output)) { std::pair callResult = m_ext->call(*callParams); callResult.second.copyTo(output); + m_returnData.assign(callResult.second.begin(), callResult.second.end()); m_SPP[0] = callResult.first ? 1 : 0; } From ec797dd0c7a4e59b6050167760002472b640232b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 5 May 2017 11:48:06 +0200 Subject: [PATCH 02/10] Extend EVM Schedule with "return data" flag --- libevmcore/EVMSchedule.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libevmcore/EVMSchedule.h b/libevmcore/EVMSchedule.h index 308cea4dbc3..50669a2acc0 100644 --- a/libevmcore/EVMSchedule.h +++ b/libevmcore/EVMSchedule.h @@ -39,6 +39,7 @@ struct EVMSchedule bool eip150Mode = false; bool eip158Mode = false; bool haveRevert = false; + bool haveReturnData = false; std::array tierStepGas; unsigned expGas = 10; unsigned expByteGas = 10; @@ -109,6 +110,7 @@ static const EVMSchedule MetropolisSchedule = [] { EVMSchedule schedule = EIP158Schedule; schedule.haveRevert = true; + schedule.haveReturnData = true; return schedule; }(); From ce59f0c44e7d4c49b937d4fbf4996dd0ef285a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 5 May 2017 12:20:50 +0200 Subject: [PATCH 03/10] EVM: remove disassembly feature --- libevmcore/Instruction.cpp | 167 ------------------------------------- libevmcore/Instruction.h | 3 - 2 files changed, 170 deletions(-) diff --git a/libevmcore/Instruction.cpp b/libevmcore/Instruction.cpp index 6cc24b916c1..25c466bc187 100755 --- a/libevmcore/Instruction.cpp +++ b/libevmcore/Instruction.cpp @@ -29,156 +29,6 @@ using namespace std; using namespace dev; using namespace dev::eth; -const std::map dev::eth::c_instructions = -{ - { "STOP", Instruction::STOP }, - { "ADD", Instruction::ADD }, - { "SUB", Instruction::SUB }, - { "MUL", Instruction::MUL }, - { "DIV", Instruction::DIV }, - { "SDIV", Instruction::SDIV }, - { "MOD", Instruction::MOD }, - { "SMOD", Instruction::SMOD }, - { "EXP", Instruction::EXP }, - { "BNOT", Instruction::NOT }, - { "LT", Instruction::LT }, - { "GT", Instruction::GT }, - { "SLT", Instruction::SLT }, - { "SGT", Instruction::SGT }, - { "EQ", Instruction::EQ }, - { "NOT", Instruction::ISZERO }, - { "AND", Instruction::AND }, - { "OR", Instruction::OR }, - { "XOR", Instruction::XOR }, - { "BYTE", Instruction::BYTE }, - { "ADDMOD", Instruction::ADDMOD }, - { "MULMOD", Instruction::MULMOD }, - { "SIGNEXTEND", Instruction::SIGNEXTEND }, - { "SHA3", Instruction::SHA3 }, - { "ADDRESS", Instruction::ADDRESS }, - { "BALANCE", Instruction::BALANCE }, - { "ORIGIN", Instruction::ORIGIN }, - { "CALLER", Instruction::CALLER }, - { "CALLVALUE", Instruction::CALLVALUE }, - { "CALLDATALOAD", Instruction::CALLDATALOAD }, - { "CALLDATASIZE", Instruction::CALLDATASIZE }, - { "CALLDATACOPY", Instruction::CALLDATACOPY }, - { "CODESIZE", Instruction::CODESIZE }, - { "CODECOPY", Instruction::CODECOPY }, - { "GASPRICE", Instruction::GASPRICE }, - { "EXTCODESIZE", Instruction::EXTCODESIZE }, - { "EXTCODECOPY", Instruction::EXTCODECOPY }, - { "BLOCKHASH", Instruction::BLOCKHASH }, - { "COINBASE", Instruction::COINBASE }, - { "TIMESTAMP", Instruction::TIMESTAMP }, - { "NUMBER", Instruction::NUMBER }, - { "DIFFICULTY", Instruction::DIFFICULTY }, - { "GASLIMIT", Instruction::GASLIMIT }, - { "JUMPTO", Instruction::JUMPTO }, - { "JUMPIF", Instruction::JUMPTO }, - { "JUMPSUB", Instruction::JUMPSUB }, - { "JUMPV", Instruction::JUMPV }, - { "JUMPSUBV", Instruction::JUMPSUBV }, - { "RETURNSUB", Instruction::RETURNSUB }, - { "POP", Instruction::POP }, - { "MLOAD", Instruction::MLOAD }, - { "MSTORE", Instruction::MSTORE }, - { "MSTORE8", Instruction::MSTORE8 }, - { "SLOAD", Instruction::SLOAD }, - { "SSTORE", Instruction::SSTORE }, - { "JUMP", Instruction::JUMP }, - { "JUMPI", Instruction::JUMPI }, - { "PC", Instruction::PC }, - { "MSIZE", Instruction::MSIZE }, - { "GAS", Instruction::GAS }, - { "BEGIN", Instruction::JUMPDEST }, - { "JUMPDEST", Instruction::JUMPDEST }, - { "BEGINDATA", Instruction::BEGINDATA }, - { "BEGINSUB", Instruction::BEGINSUB }, - { "PUSH1", Instruction::PUSH1 }, - { "PUSH2", Instruction::PUSH2 }, - { "PUSH3", Instruction::PUSH3 }, - { "PUSH4", Instruction::PUSH4 }, - { "PUSH5", Instruction::PUSH5 }, - { "PUSH6", Instruction::PUSH6 }, - { "PUSH7", Instruction::PUSH7 }, - { "PUSH8", Instruction::PUSH8 }, - { "PUSH9", Instruction::PUSH9 }, - { "PUSH10", Instruction::PUSH10 }, - { "PUSH11", Instruction::PUSH11 }, - { "PUSH12", Instruction::PUSH12 }, - { "PUSH13", Instruction::PUSH13 }, - { "PUSH14", Instruction::PUSH14 }, - { "PUSH15", Instruction::PUSH15 }, - { "PUSH16", Instruction::PUSH16 }, - { "PUSH17", Instruction::PUSH17 }, - { "PUSH18", Instruction::PUSH18 }, - { "PUSH19", Instruction::PUSH19 }, - { "PUSH20", Instruction::PUSH20 }, - { "PUSH21", Instruction::PUSH21 }, - { "PUSH22", Instruction::PUSH22 }, - { "PUSH23", Instruction::PUSH23 }, - { "PUSH24", Instruction::PUSH24 }, - { "PUSH25", Instruction::PUSH25 }, - { "PUSH26", Instruction::PUSH26 }, - { "PUSH27", Instruction::PUSH27 }, - { "PUSH28", Instruction::PUSH28 }, - { "PUSH29", Instruction::PUSH29 }, - { "PUSH30", Instruction::PUSH30 }, - { "PUSH31", Instruction::PUSH31 }, - { "PUSH32", Instruction::PUSH32 }, - { "DUP1", Instruction::DUP1 }, - { "DUP2", Instruction::DUP2 }, - { "DUP3", Instruction::DUP3 }, - { "DUP4", Instruction::DUP4 }, - { "DUP5", Instruction::DUP5 }, - { "DUP6", Instruction::DUP6 }, - { "DUP7", Instruction::DUP7 }, - { "DUP8", Instruction::DUP8 }, - { "DUP9", Instruction::DUP9 }, - { "DUP10", Instruction::DUP10 }, - { "DUP11", Instruction::DUP11 }, - { "DUP12", Instruction::DUP12 }, - { "DUP13", Instruction::DUP13 }, - { "DUP14", Instruction::DUP14 }, - { "DUP15", Instruction::DUP15 }, - { "DUP16", Instruction::DUP16 }, - { "SWAP1", Instruction::SWAP1 }, - { "SWAP2", Instruction::SWAP2 }, - { "SWAP3", Instruction::SWAP3 }, - { "SWAP4", Instruction::SWAP4 }, - { "SWAP5", Instruction::SWAP5 }, - { "SWAP6", Instruction::SWAP6 }, - { "SWAP7", Instruction::SWAP7 }, - { "SWAP8", Instruction::SWAP8 }, - { "SWAP9", Instruction::SWAP9 }, - { "SWAP10", Instruction::SWAP10 }, - { "SWAP11", Instruction::SWAP11 }, - { "SWAP12", Instruction::SWAP12 }, - { "SWAP13", Instruction::SWAP13 }, - { "SWAP14", Instruction::SWAP14 }, - { "SWAP15", Instruction::SWAP15 }, - { "SWAP16", Instruction::SWAP16 }, - { "LOG0", Instruction::LOG0 }, - { "LOG1", Instruction::LOG1 }, - { "LOG2", Instruction::LOG2 }, - { "LOG3", Instruction::LOG3 }, - { "LOG4", Instruction::LOG4 }, - { "CREATE", Instruction::CREATE }, - { "CALL", Instruction::CALL }, - { "CALLCODE", Instruction::CALLCODE }, - { "RETURN", Instruction::RETURN }, - { "DELEGATECALL", Instruction::DELEGATECALL }, - { "REVERT", Instruction::REVERT }, - { "INVALID", Instruction::INVALID }, - { "SUICIDE", Instruction::SUICIDE }, - - // these are generated by the interpreter - should never be in user code - { "PUSHC", Instruction::PUSHC }, - { "JUMPC", Instruction::JUMPC }, - { "JUMPCI", Instruction::JUMPCI } -}; - static const std::map c_instructionInfo = { // Add, Args, Ret, SideEffects, GasPriceTier { Instruction::STOP, { "STOP", 0, 0, 0, true, Tier::Zero } }, @@ -350,23 +200,6 @@ void dev::eth::eachInstruction( } } -string dev::eth::disassemble(bytes const& _mem) -{ - stringstream ret; - eachInstruction(_mem, [&](Instruction _instr, u256 const& _data) { - if (!isValidInstruction(_instr)) - ret << "0x" << hex << int(_instr) << " "; - else - { - InstructionInfo info = instructionInfo(_instr); - ret << info.name << " "; - if (info.additional) - ret << "0x" << hex << _data << " "; - } - }); - return ret.str(); -} - InstructionInfo dev::eth::instructionInfo(Instruction _inst) { auto it = c_instructionInfo.find(_inst); diff --git a/libevmcore/Instruction.h b/libevmcore/Instruction.h index 7ddfc52a46e..81edf164c34 100755 --- a/libevmcore/Instruction.h +++ b/libevmcore/Instruction.h @@ -274,9 +274,6 @@ extern const std::map c_instructions; /// Iterate through EVM code and call a function on each instruction. void eachInstruction(bytes const& _mem, std::function const& _onInstruction); -/// Convert from EVM code to simple EVM assembly language. -std::string disassemble(bytes const& _mem); - } } From f07808e6f97cfc644bf622fc5b3b0bd6bd14a000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 5 May 2017 12:21:21 +0200 Subject: [PATCH 04/10] Introduce RETURNDATA* opcodes Introduce RETURNDATASIZE and RETURNDATACOPY opcodes, they do nothing useful at the moment. --- libevm/VM.cpp | 12 ++++++++++++ libevmcore/Instruction.cpp | 2 ++ libevmcore/Instruction.h | 3 +++ 3 files changed, 17 insertions(+) diff --git a/libevm/VM.cpp b/libevm/VM.cpp index f8396c22dd9..a5ed880fc78 100755 --- a/libevm/VM.cpp +++ b/libevm/VM.cpp @@ -685,6 +685,12 @@ void VM::interpretCases() } NEXT + CASE(RETURNDATASIZE) + { + throwBadInstruction(); + } + NEXT + CASE(CODESIZE) { ON_OP(); @@ -715,6 +721,12 @@ void VM::interpretCases() } NEXT + CASE(RETURNDATACOPY) + { + throwBadInstruction(); + } + NEXT + CASE(CODECOPY) { m_copyMemSize = toInt63(m_SP[2]); diff --git a/libevmcore/Instruction.cpp b/libevmcore/Instruction.cpp index 25c466bc187..f33cd31002a 100755 --- a/libevmcore/Instruction.cpp +++ b/libevmcore/Instruction.cpp @@ -54,6 +54,8 @@ static const std::map c_instructionInfo = { Instruction::ADDMOD, { "ADDMOD", 0, 3, 1, false, Tier::Mid } }, { Instruction::MULMOD, { "MULMOD", 0, 3, 1, false, Tier::Mid } }, { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1, false, Tier::Low } }, + { Instruction::RETURNDATASIZE,{"RETURNDATASIZE", 0, 0, 1, false, Tier::Low } }, + { Instruction::RETURNDATACOPY,{"RETURNDATACOPY", 0, 3, 0, false, Tier::Low } }, { Instruction::SHA3, { "SHA3", 0, 2, 1, false, Tier::Special } }, { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false, Tier::Base } }, { Instruction::BALANCE, { "BALANCE", 0, 1, 1, false, Tier::Special } }, diff --git a/libevmcore/Instruction.h b/libevmcore/Instruction.h index 81edf164c34..049a85df459 100755 --- a/libevmcore/Instruction.h +++ b/libevmcore/Instruction.h @@ -47,6 +47,9 @@ enum class Instruction: uint8_t EXP, ///< exponential operation SIGNEXTEND, ///< extend length of signed integer + RETURNDATASIZE = 0x0d, ///< size of data returned from previous call + RETURNDATACOPY = 0x0e, ///< copy data returned from previous call to memory + LT = 0x10, ///< less-than comparision GT, ///< greater-than comparision SLT, ///< signed less-than comparision From 798322dd40f11dc07dcd79d6b03b576c2930d237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 5 May 2017 12:24:36 +0200 Subject: [PATCH 05/10] Implement RETURNDATASIZE --- libevm/VM.cpp | 8 +++++++- libevm/VMOpt.cpp | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/libevm/VM.cpp b/libevm/VM.cpp index a5ed880fc78..336827ee130 100755 --- a/libevm/VM.cpp +++ b/libevm/VM.cpp @@ -687,7 +687,13 @@ void VM::interpretCases() CASE(RETURNDATASIZE) { - throwBadInstruction(); + if (!m_schedule->haveReturnData) + throwBadInstruction(); + + ON_OP(); + updateIOGas(); + + m_SPP[0] = m_returnData.size(); } NEXT diff --git a/libevm/VMOpt.cpp b/libevm/VMOpt.cpp index 0c090d39fbd..398a9908c9a 100755 --- a/libevm/VMOpt.cpp +++ b/libevm/VMOpt.cpp @@ -35,6 +35,7 @@ void VM::reportStackUse() std::array VM::c_metrics; void VM::initMetrics() { + // FIXME: This is not thread safe. static bool done=false; if (!done) { From d30ddfb4c5fe1480b8f42ba38881cca3698c1858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 5 May 2017 12:28:03 +0200 Subject: [PATCH 06/10] Implement RETURNDATACOPY --- libevm/VM.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libevm/VM.cpp b/libevm/VM.cpp index 336827ee130..df3982f057a 100755 --- a/libevm/VM.cpp +++ b/libevm/VM.cpp @@ -729,7 +729,15 @@ void VM::interpretCases() CASE(RETURNDATACOPY) { - throwBadInstruction(); + if (!m_schedule->haveReturnData) + throwBadInstruction(); + + m_copyMemSize = toInt63(m_SP[2]); + updateMem(memNeed(m_SP[0], m_SP[2])); + ON_OP(); + updateIOGas(); + + copyDataToMemory(&m_returnData, m_SP); } NEXT From 3ee15d0201a098343941d0656aa15a3fb5852c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 9 May 2017 13:41:22 +0200 Subject: [PATCH 07/10] Fix RETURNDATACOPY and RETURNDATASIZE prices --- libevmcore/Instruction.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libevmcore/Instruction.cpp b/libevmcore/Instruction.cpp index f33cd31002a..f3c169112a1 100755 --- a/libevmcore/Instruction.cpp +++ b/libevmcore/Instruction.cpp @@ -54,8 +54,8 @@ static const std::map c_instructionInfo = { Instruction::ADDMOD, { "ADDMOD", 0, 3, 1, false, Tier::Mid } }, { Instruction::MULMOD, { "MULMOD", 0, 3, 1, false, Tier::Mid } }, { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1, false, Tier::Low } }, - { Instruction::RETURNDATASIZE,{"RETURNDATASIZE", 0, 0, 1, false, Tier::Low } }, - { Instruction::RETURNDATACOPY,{"RETURNDATACOPY", 0, 3, 0, false, Tier::Low } }, + { Instruction::RETURNDATASIZE,{"RETURNDATASIZE", 0, 0, 1, false, Tier::Base } }, + { Instruction::RETURNDATACOPY,{"RETURNDATACOPY", 0, 3, 0, true, Tier::VeryLow } }, { Instruction::SHA3, { "SHA3", 0, 2, 1, false, Tier::Special } }, { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false, Tier::Base } }, { Instruction::BALANCE, { "BALANCE", 0, 1, 1, false, Tier::Special } }, From 05e438e5aaafb1cd1457f4110ef6eee3a1da88de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 10 May 2017 17:50:24 +0200 Subject: [PATCH 08/10] Refactor code handling return buffer --- libevm/VMCalls.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libevm/VMCalls.cpp b/libevm/VMCalls.cpp index 0322e4c28d1..16157343e07 100755 --- a/libevm/VMCalls.cpp +++ b/libevm/VMCalls.cpp @@ -153,11 +153,20 @@ void VM::caseCall() bytesRef output; if (caseCallSetup(callParams.get(), output)) { - std::pair callResult = m_ext->call(*callParams); - callResult.second.copyTo(output); - m_returnData.assign(callResult.second.begin(), callResult.second.end()); - - m_SPP[0] = callResult.first ? 1 : 0; + bool success = false; + owning_bytes_ref outputRef; + std::tie(success, outputRef) = m_ext->call(*callParams); + outputRef.copyTo(output); + + // Here we have 2 options: + // 1. Keep the whole returned memory buffer (owning_bytes_ref): + // higher memory footprint, no memory copy. + // 2. Copy only the return data from the returned memory buffer: + // minimal memory footprint, additional memory copy. + // Option 2 used: + m_returnData = outputRef.toBytes(); + + m_SPP[0] = success ? 1 : 0; } else m_SPP[0] = 0; From 861a9eaba4f97c63f8079082107a778ed8bb7ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 16 May 2017 12:25:05 +0200 Subject: [PATCH 09/10] Add support for REVERT in CREATE --- libethereum/ExtVM.cpp | 4 ++-- libethereum/ExtVM.h | 2 +- libevm/ExtVMFace.h | 2 +- libevm/JitVM.cpp | 7 ++++++- libevm/VM.h | 2 +- libevm/VMCalls.cpp | 6 +++++- test/tools/jsontests/vm.cpp | 4 ++-- test/tools/jsontests/vm.h | 2 +- 8 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libethereum/ExtVM.cpp b/libethereum/ExtVM.cpp index df10f350c98..c17520f3ffa 100644 --- a/libethereum/ExtVM.cpp +++ b/libethereum/ExtVM.cpp @@ -115,7 +115,7 @@ void ExtVM::setStore(u256 _n, u256 _v) m_s.setStorage(myAddress, _n, _v); } -h160 ExtVM::create(u256 _endowment, u256& io_gas, bytesConstRef _code, OnOpFunc const& _onOp) +std::pair ExtVM::create(u256 _endowment, u256& io_gas, bytesConstRef _code, OnOpFunc const& _onOp) { Executive e{m_s, envInfo(), m_sealEngine, depth + 1}; if (!e.create(myAddress, _endowment, gasPrice, io_gas, _code, origin)) @@ -124,7 +124,7 @@ h160 ExtVM::create(u256 _endowment, u256& io_gas, bytesConstRef _code, OnOpFunc e.accrueSubState(sub); } io_gas = e.gas(); - return e.newAddress(); + return {e.newAddress(), e.takeOutput()}; } void ExtVM::suicide(Address _a) diff --git a/libethereum/ExtVM.h b/libethereum/ExtVM.h index dadcf453607..a46978ed335 100644 --- a/libethereum/ExtVM.h +++ b/libethereum/ExtVM.h @@ -65,7 +65,7 @@ class ExtVM: public ExtVMFace virtual size_t codeSizeAt(Address _a) override final; /// Create a new contract. - virtual h160 create(u256 _endowment, u256& io_gas, bytesConstRef _code, OnOpFunc const& _onOp = {}) override final; + virtual std::pair create(u256 _endowment, u256& io_gas, bytesConstRef _code, OnOpFunc const& _onOp = {}) override final; /// Create a new message call. Leave _myAddressOverride as the default to use the present address as caller. /// @returns success flag and output data, if any. diff --git a/libevm/ExtVMFace.h b/libevm/ExtVMFace.h index 57c06467997..1d1a8e8337c 100644 --- a/libevm/ExtVMFace.h +++ b/libevm/ExtVMFace.h @@ -274,7 +274,7 @@ class ExtVMFace virtual void suicide(Address) { sub.suicides.insert(myAddress); } /// Create a new (contract) account. - virtual h160 create(u256, u256&, bytesConstRef, OnOpFunc const&) { return h160(); } + virtual std::pair create(u256, u256&, bytesConstRef, OnOpFunc const&) = 0; /// Make a new message call. /// @returns success flag and output data, if any. diff --git a/libevm/JitVM.cpp b/libevm/JitVM.cpp index c9b6d317912..4b523519385 100644 --- a/libevm/JitVM.cpp +++ b/libevm/JitVM.cpp @@ -149,12 +149,17 @@ int64_t call( u256 gas = _msg->gas; // ExtVM::create takes the sender address from .myAddress. assert(fromEvmC(_msg->sender) == env.myAddress); - auto addr = env.create(value, gas, input, {}); + + // TODO: EVMJIT does not support RETURNDATA at the moment, so + // the output is ignored here. + h160 addr; + std::tie(addr, std::ignore) = env.create(value, gas, input, {}); auto gasLeft = static_cast(gas); if (addr) std::copy(addr.begin(), addr.end(), _outputData); else gasLeft |= EVM_CALL_FAILURE; + return gasLeft; } diff --git a/libevm/VM.h b/libevm/VM.h index 73fc6e7d8ec..1a917324807 100755 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -104,7 +104,7 @@ class VM: public VMFace // space for code bytes m_code; - /// Space for return data. + /// RETURNDATA buffer for memory returned from direct subcalls. bytes m_returnData; // space for data stack, grows towards smaller addresses from the end diff --git a/libevm/VMCalls.cpp b/libevm/VMCalls.cpp index 16157343e07..51e1d8b7aea 100755 --- a/libevm/VMCalls.cpp +++ b/libevm/VMCalls.cpp @@ -130,7 +130,11 @@ void VM::caseCreate() if (!m_schedule->staticCallDepthLimit()) createGas -= createGas / 64; u256 gas = createGas; - m_SPP[0] = (u160)m_ext->create(endowment, gas, bytesConstRef(m_mem.data() + initOff, initSize), m_onOp); + h160 addr; + owning_bytes_ref output; + std::tie(addr, output) = m_ext->create(endowment, gas, bytesConstRef(m_mem.data() + initOff, initSize), m_onOp); + m_SPP[0] = (u160)addr; + m_returnData = output.toBytes(); *m_io_gas_p -= (createGas - gas); m_io_gas = uint64_t(*m_io_gas_p); } diff --git a/test/tools/jsontests/vm.cpp b/test/tools/jsontests/vm.cpp index 2702d8db57e..e6f7ebf435f 100644 --- a/test/tools/jsontests/vm.cpp +++ b/test/tools/jsontests/vm.cpp @@ -37,13 +37,13 @@ FakeExtVM::FakeExtVM(EnvInfo const& _envInfo, unsigned _depth): /// TODO: XXX: ExtVMFace(_envInfo, Address(), Address(), Address(), 0, 1, bytesConstRef(), bytes(), EmptySHA3, _depth) {} -h160 FakeExtVM::create(u256 _endowment, u256& io_gas, bytesConstRef _init, OnOpFunc const&) +std::pair FakeExtVM::create(u256 _endowment, u256& io_gas, bytesConstRef _init, OnOpFunc const&) { Address na = right160(sha3(rlpList(myAddress, get<1>(addresses[myAddress])))); Transaction t(_endowment, gasPrice, io_gas, _init.toBytes()); callcreates.push_back(t); - return na; + return {na, eth::owning_bytes_ref{}}; } std::pair FakeExtVM::call(CallParameters& _p) diff --git a/test/tools/jsontests/vm.h b/test/tools/jsontests/vm.h index f1a2a60112f..73b3f866fc3 100644 --- a/test/tools/jsontests/vm.h +++ b/test/tools/jsontests/vm.h @@ -54,7 +54,7 @@ class FakeExtVM: public eth::ExtVMFace virtual void suicide(Address _a) override { std::get<0>(addresses[_a]) += std::get<0>(addresses[myAddress]); addresses.erase(myAddress); } virtual bytes const& codeAt(Address _a) override { return std::get<3>(addresses[_a]); } virtual size_t codeSizeAt(Address _a) override { return std::get<3>(addresses[_a]).size(); } - virtual h160 create(u256 _endowment, u256& io_gas, bytesConstRef _init, eth::OnOpFunc const&) override; + virtual std::pair create(u256 _endowment, u256& io_gas, bytesConstRef _init, eth::OnOpFunc const&) override; virtual std::pair call(eth::CallParameters&) override; void setTransaction(Address _caller, u256 _value, u256 _gasPrice, bytes const& _data); void setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, std::map const& _storage, bytes const& _code); From e18d8399d95a756c36b84a40b24667bcd131de59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 18 May 2017 12:19:59 +0200 Subject: [PATCH 10/10] Change RETURNDATA opcode numbers --- libevmcore/Instruction.cpp | 4 ++-- libevmcore/Instruction.h | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/libevmcore/Instruction.cpp b/libevmcore/Instruction.cpp index f3c169112a1..354a75e38d4 100755 --- a/libevmcore/Instruction.cpp +++ b/libevmcore/Instruction.cpp @@ -54,8 +54,6 @@ static const std::map c_instructionInfo = { Instruction::ADDMOD, { "ADDMOD", 0, 3, 1, false, Tier::Mid } }, { Instruction::MULMOD, { "MULMOD", 0, 3, 1, false, Tier::Mid } }, { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1, false, Tier::Low } }, - { Instruction::RETURNDATASIZE,{"RETURNDATASIZE", 0, 0, 1, false, Tier::Base } }, - { Instruction::RETURNDATACOPY,{"RETURNDATACOPY", 0, 3, 0, true, Tier::VeryLow } }, { Instruction::SHA3, { "SHA3", 0, 2, 1, false, Tier::Special } }, { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false, Tier::Base } }, { Instruction::BALANCE, { "BALANCE", 0, 1, 1, false, Tier::Special } }, @@ -70,6 +68,8 @@ static const std::map c_instructionInfo = { Instruction::GASPRICE, { "GASPRICE", 0, 0, 1, false, Tier::Base } }, { Instruction::EXTCODESIZE, { "EXTCODESIZE", 0, 1, 1, false, Tier::Special } }, { Instruction::EXTCODECOPY, { "EXTCODECOPY", 0, 4, 0, true, Tier::Special } }, + { Instruction::RETURNDATASIZE,{"RETURNDATASIZE", 0, 0, 1, false, Tier::Base } }, + { Instruction::RETURNDATACOPY,{"RETURNDATACOPY", 0, 3, 0, true, Tier::VeryLow } }, { Instruction::BLOCKHASH, { "BLOCKHASH", 0, 1, 1, false, Tier::Ext } }, { Instruction::COINBASE, { "COINBASE", 0, 0, 1, false, Tier::Base } }, { Instruction::TIMESTAMP, { "TIMESTAMP", 0, 0, 1, false, Tier::Base } }, diff --git a/libevmcore/Instruction.h b/libevmcore/Instruction.h index 049a85df459..90a1925eafc 100755 --- a/libevmcore/Instruction.h +++ b/libevmcore/Instruction.h @@ -47,9 +47,6 @@ enum class Instruction: uint8_t EXP, ///< exponential operation SIGNEXTEND, ///< extend length of signed integer - RETURNDATASIZE = 0x0d, ///< size of data returned from previous call - RETURNDATACOPY = 0x0e, ///< copy data returned from previous call to memory - LT = 0x10, ///< less-than comparision GT, ///< greater-than comparision SLT, ///< signed less-than comparision @@ -77,6 +74,8 @@ enum class Instruction: uint8_t GASPRICE, ///< get price of gas in current environment EXTCODESIZE, ///< get external code size (from another contract) EXTCODECOPY, ///< copy external code (from another contract) + RETURNDATASIZE = 0x3d, ///< size of data returned from previous call + RETURNDATACOPY = 0x3e, ///< copy data returned from previous call to memory BLOCKHASH = 0x40, ///< get hash of most recent complete block COINBASE, ///< get the block's coinbase address