From 44ed951039168d38f9a64b0a7c1422f88d7a5cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 12 Oct 2020 13:32:53 +0200 Subject: [PATCH] tools: Add --input option to `evmc run` --- test/tools/CMakeLists.txt | 20 +++++++++++++++----- test/unittests/tool_commands_test.cpp | 19 +++++++++++++++++-- tools/commands/commands.hpp | 1 + tools/commands/run.cpp | 4 ++++ tools/evmc/main.cpp | 4 +++- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/test/tools/CMakeLists.txt b/test/tools/CMakeLists.txt index 18f008e6c..5f10e90e9 100644 --- a/test/tools/CMakeLists.txt +++ b/test/tools/CMakeLists.txt @@ -2,16 +2,26 @@ # Copyright 2019-2020 The EVMC Authors. # Licensed under the Apache License, Version 2.0. -set(prefix ${PROJECT_NAME}/evmc-run) +set(PREFIX ${PROJECT_NAME}/evmc-run) add_test( - NAME ${prefix}/example1 + NAME ${PREFIX}/example1 COMMAND evmc::tool run --vm $ 30600052596000f3 --gas 99 ) set_tests_properties( - ${prefix}/example1 PROPERTIES PASS_REGULAR_EXPRESSION + ${PREFIX}/example1 PROPERTIES PASS_REGULAR_EXPRESSION "Result: +success[\r\n]+Gas used: +6[\r\n]+Output: +0000000000000000000000000000000000000000000000000000000000000000[\r\n]" ) -get_property(tools_tests DIRECTORY PROPERTY TESTS) -set_tests_properties(${tools_tests} PROPERTIES ENVIRONMENT LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/tools-%p.profraw) + +add_test( + NAME ${PREFIX}/copy_input + COMMAND evmc::tool run --vm $ 600035600052596000f3 --input aabbccdd +) +set_tests_properties( + ${PREFIX}/copy_input PROPERTIES PASS_REGULAR_EXPRESSION + "Result: +success[\r\n]+Gas used: +7[\r\n]+Output: +aabbccdd00000000000000000000000000000000000000000000000000000000[\r\n]" +) + +get_property(TOOLS_TESTS DIRECTORY PROPERTY TESTS) +set_tests_properties(${TOOLS_TESTS} PROPERTIES ENVIRONMENT LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/tools-%p.profraw) diff --git a/test/unittests/tool_commands_test.cpp b/test/unittests/tool_commands_test.cpp index 26b3b08b9..3052edd54 100644 --- a/test/unittests/tool_commands_test.cpp +++ b/test/unittests/tool_commands_test.cpp @@ -31,7 +31,7 @@ TEST(tool_commands, run_empty_code) auto vm = evmc::VM{evmc_create_example_vm()}; std::ostringstream out; - const auto exit_code = cmd::run(vm, EVMC_FRONTIER, 1, "", out); + const auto exit_code = cmd::run(vm, EVMC_FRONTIER, 1, "", "", out); EXPECT_EQ(exit_code, 0); EXPECT_EQ(out.str(), out_pattern("Frontier", 1, "success", 0, "")); } @@ -41,9 +41,24 @@ TEST(tool_commands, run_return_my_address) auto vm = evmc::VM{evmc_create_example_vm()}; std::ostringstream out; - const auto exit_code = cmd::run(vm, EVMC_HOMESTEAD, 200, "30600052596000f3", out); + const auto exit_code = cmd::run(vm, EVMC_HOMESTEAD, 200, "30600052596000f3", "", out); EXPECT_EQ(exit_code, 0); EXPECT_EQ(out.str(), out_pattern("Homestead", 200, "success", 6, "0000000000000000000000000000000000000000000000000000000000000000")); } + +TEST(tool_commands, run_copy_input_to_output) +{ + // Yul: mstore(0, calldataload(0)) return(0, msize()) + auto vm = evmc::VM{evmc_create_example_vm()}; + std::ostringstream out; + + const auto exit_code = + cmd::run(vm, EVMC_TANGERINE_WHISTLE, 200, "600035600052596000f3", + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", out); + EXPECT_EQ(exit_code, 0); + EXPECT_EQ(out.str(), + out_pattern("Tangerine Whistle", 200, "success", 7, + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")); +} diff --git a/tools/commands/commands.hpp b/tools/commands/commands.hpp index 942f05e89..616d644d0 100644 --- a/tools/commands/commands.hpp +++ b/tools/commands/commands.hpp @@ -14,6 +14,7 @@ int run(evmc::VM& vm, evmc_revision rev, int64_t gas, const std::string& code_hex, + const std::string& input_hex, std::ostream& out); } } // namespace evmc diff --git a/tools/commands/run.cpp b/tools/commands/run.cpp index 83f44789e..ecc2ce583 100644 --- a/tools/commands/run.cpp +++ b/tools/commands/run.cpp @@ -15,13 +15,17 @@ int run(evmc::VM& vm, evmc_revision rev, int64_t gas, const std::string& code_hex, + const std::string& input_hex, std::ostream& out) { out << "Executing on " << rev << " with " << gas << " gas limit\n"; const auto code = from_hex(code_hex); + const auto input = from_hex(input_hex); evmc_message msg{}; msg.gas = gas; + msg.input_data = input.data(); + msg.input_size = input.size(); MockedHost host; const auto result = vm.execute(host, rev, msg, code.data(), code.size()); diff --git a/tools/evmc/main.cpp b/tools/evmc/main.cpp index 6dfecc479..f4da2c708 100644 --- a/tools/evmc/main.cpp +++ b/tools/evmc/main.cpp @@ -17,12 +17,14 @@ int main(int argc, const char** argv) std::string code_hex; int64_t gas = 1000000; auto rev = EVMC_ISTANBUL; + std::string input_hex; auto& run_cmd = *app.add_subcommand("run", "Execute EVM bytecode"); run_cmd.add_option("code", code_hex, "Hex-encoded bytecode")->required(); run_cmd.add_option("--vm", vm_config, "EVMC VM module")->required()->envname("EVMC_VM"); run_cmd.add_option("--gas", gas, "Execution gas limit", true)->check(CLI::Range(0, 1000000000)); run_cmd.add_option("--rev", rev, "EVM revision", true); + run_cmd.add_option("--input", input_hex, "Hex-encoded input bytes"); try { @@ -49,7 +51,7 @@ int main(int argc, const char** argv) return static_cast(ec); } std::cout << "Config: " << vm_config << "\n"; - return cmd::run(vm, rev, gas, code_hex, std::cout); + return cmd::run(vm, rev, gas, code_hex, input_hex, std::cout); } return 0;