Skip to content

Commit

Permalink
tools: Validate HEX arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Mar 22, 2021
1 parent bfe3cf1 commit 390ced7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 13 additions & 1 deletion test/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ add_evmc_tool_test(

add_evmc_tool_test(
copy_input
"--vm $<TARGET_FILE:evmc::example-vm> run 600035600052596000f3 --input aabbccdd"
"--vm $<TARGET_FILE:evmc::example-vm> run 600035600052596000f3 --input 0xaabbccdd"
"Result: +success[\r\n]+Gas used: +7[\r\n]+Output: +aabbccdd00000000000000000000000000000000000000000000000000000000[\r\n]"
)

Expand All @@ -39,5 +39,17 @@ add_evmc_tool_test(
"Result: +success[\r\n]+Gas used: +6[\r\n]+Output: +02[\r\n]"
)

add_evmc_tool_test(
invalid_hex_code
"--vm $<TARGET_FILE:evmc::example-vm> run 0x600"
"code: incomplete hex byte pair"
)

add_evmc_tool_test(
invalid_hex_input
"--vm $<TARGET_FILE:evmc::example-vm> run 0x --input aa0y"
"--input: invalid hex digit"
)

get_property(TOOLS_TESTS DIRECTORY PROPERTY TESTS)
set_tests_properties(${TOOLS_TESTS} PROPERTIES ENVIRONMENT LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/tools-%p.profraw)
24 changes: 22 additions & 2 deletions tools/evmc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,32 @@

#include "tools/commands/commands.hpp"
#include <CLI/CLI.hpp>
#include <evmc/hex.hpp>
#include <evmc/loader.h>

namespace
{
struct HexValidator : public CLI::Validator
{
HexValidator() : CLI::Validator{"HEX"}
{
name_ = "HEX";
func_ = [](const std::string& str) -> std::string {
const auto error_code = evmc::validate_hex(str);
if (error_code)
return error_code.message();
return {};
};
}
};
} // namespace

int main(int argc, const char** argv)
{
using namespace evmc;

static HexValidator Hex;

std::string vm_config;
std::string code_hex;
int64_t gas = 1000000;
Expand All @@ -23,10 +43,10 @@ int main(int argc, const char** argv)
*app.add_option("--vm", vm_config, "EVMC VM module")->envname("EVMC_VM");

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("code", code_hex, "Bytecode")->required()->check(Hex);
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");
run_cmd.add_option("--input", input_hex, "Input bytes")->check(Hex);
run_cmd.add_flag(
"--create", create,
"Create new contract out of the code and then execute this contract with the input");
Expand Down

0 comments on commit 390ced7

Please sign in to comment.