Skip to content

Commit

Permalink
test: Use instr::traits[].name for instruction names
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Nov 29, 2022
1 parent 8a25cfd commit 8ed5814
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 36 deletions.
4 changes: 2 additions & 2 deletions test/fuzzer/fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t data_size) noe
auto ref_host = in.host; // Copy Host.
const auto& code = ref_host.accounts[in.msg.recipient].code;

if (print_input)
if (print_input != nullptr)
{
std::cout << "rev: " << int{in.rev} << "\n";
std::cout << "depth: " << int{in.msg.depth} << "\n";
std::cout << "code: " << hex(code) << "\n";
std::cout << "decoded: " << decode(code, in.rev) << "\n";
std::cout << "decoded: " << decode(code) << "\n";
std::cout << "input: " << hex({in.msg.input_data, in.msg.input_size}) << "\n";
std::cout << "account: " << hex(in.msg.recipient) << "\n";
std::cout << "caller: " << hex(in.msg.sender) << "\n";
Expand Down
20 changes: 6 additions & 14 deletions test/unittests/bytecode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,24 @@ TEST(bytecode, repeat)
EXPECT_EQ(0 * OP_STOP, "");
}

TEST(bytecode, to_name)
{
EXPECT_EQ(to_name(OP_SAR), "SAR");
EXPECT_EQ(to_name(OP_SAR, EVMC_HOMESTEAD), "UNDEFINED_INSTRUCTION:1d");
}

TEST(bytecode, decode)
{
const auto code = push(0x01e240) + OP_DUP1 + OP_GAS + "cc" + OP_REVERT;
EXPECT_EQ(decode(code, EVMC_FRONTIER),
"bytecode{} + OP_PUSH3 + \"01e240\" + OP_DUP1 + OP_GAS + \"cc\" + \"fd\"");
EXPECT_EQ(decode(code, EVMC_BYZANTIUM),
"bytecode{} + OP_PUSH3 + \"01e240\" + OP_DUP1 + OP_GAS + \"cc\" + OP_REVERT");
EXPECT_EQ(
decode(code), R"(bytecode{} + OP_PUSH3 + "01e240" + OP_DUP1 + OP_GAS + "cc" + OP_REVERT)");
}

TEST(bytecode, decode_push_trimmed_data)
{
const auto code1 = bytecode{} + OP_PUSH2 + "0000";
EXPECT_EQ(decode(code1, EVMC_FRONTIER), "bytecode{} + OP_PUSH2 + \"0000\"");
EXPECT_EQ(decode(code1), "bytecode{} + OP_PUSH2 + \"0000\"");

const auto code2 = bytecode{} + OP_PUSH2 + "00";
EXPECT_EQ(decode(code2, EVMC_FRONTIER), "bytecode{} + OP_PUSH2 + \"00\"");
EXPECT_EQ(decode(code2), "bytecode{} + OP_PUSH2 + \"00\"");

const auto code3 = bytecode{} + OP_PUSH2;
EXPECT_EQ(decode(code3, EVMC_FRONTIER), "bytecode{} + OP_PUSH2");
EXPECT_EQ(decode(code3), "bytecode{} + OP_PUSH2");

const auto code4 = bytecode{} + OP_PUSH2 + "";
EXPECT_EQ(decode(code4, EVMC_FRONTIER), "bytecode{} + OP_PUSH2");
EXPECT_EQ(decode(code4), "bytecode{} + OP_PUSH2");
}
3 changes: 2 additions & 1 deletion test/unittests/evm_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/// about accounts, without storage.

#include "evm_fixture.hpp"
#include <evmone/instructions_traits.hpp>

using namespace evmc::literals;
using evmone::test::evm;
Expand Down Expand Up @@ -193,7 +194,7 @@ TEST_P(evm, log_data_cost)
EXPECT_EQ(host.recorded_logs.size(), 0);
execute(cost - 1, code);
EXPECT_EQ(result.status_code, EVMC_OUT_OF_GAS);
EXPECT_EQ(host.recorded_logs.size(), 0) << to_name(op);
EXPECT_EQ(host.recorded_logs.size(), 0) << evmone::instr::traits[op].name;
host.recorded_logs.clear();
}
}
Expand Down
6 changes: 2 additions & 4 deletions test/unittests/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,10 @@ TEST_P(evm, undefined_instructions)
{
for (auto i = 0; i <= EVMC_MAX_REVISION; ++i)
{
auto r = evmc_revision(i);
auto names = evmc_get_instruction_names_table(r);

const auto r = evmc_revision(i);
for (uint8_t opcode = 0; opcode <= 0xfe; ++opcode)
{
if (names[opcode] != nullptr)
if (evmone::instr::gas_costs[r][opcode] != evmone::instr::undefined)
continue;

auto res = vm.execute(host, r, {}, &opcode, sizeof(opcode));
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/instructions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST(instructions, compare_with_evmc_instruction_tables)

const auto case_descr = [rev](size_t opcode) {
auto case_descr_str = std::ostringstream{};
case_descr_str << "opcode " << to_name(evmc_opcode(opcode), rev);
case_descr_str << "opcode " << instr::traits[opcode].name;
case_descr_str << " on revision " << rev;
return case_descr_str.str();
};
Expand Down
2 changes: 1 addition & 1 deletion test/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_library(testutils STATIC
)

target_link_libraries(testutils PRIVATE evmc::instructions)
target_include_directories(testutils PUBLIC ${PROJECT_SOURCE_DIR})
target_include_directories(testutils PUBLIC ${PROJECT_SOURCE_DIR} ${evmone_private_include_dir})

add_library(testutils-dump STATIC dump.cpp dump.hpp)
target_link_libraries(testutils-dump PRIVATE testutils evmone intx::intx)
Expand Down
16 changes: 3 additions & 13 deletions test/utils/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once

#include <evmc/evmc.hpp>
#include <evmc/instructions.h>
#include <evmone/instructions_traits.hpp>
#include <intx/intx.hpp>
#include <test/utils/utils.hpp>
#include <algorithm>
Expand Down Expand Up @@ -414,23 +414,13 @@ inline std::string hex(evmc_opcode opcode) noexcept
return hex(static_cast<uint8_t>(opcode));
}

inline std::string to_name(evmc_opcode opcode, evmc_revision rev = EVMC_MAX_REVISION) noexcept
{
const auto names = evmc_get_instruction_names_table(rev);
if (const auto name = names[opcode]; name)
return name;

return "UNDEFINED_INSTRUCTION:" + hex(opcode);
}

inline std::string decode(bytes_view bytecode, evmc_revision rev)
inline std::string decode(bytes_view bytecode)
{
auto s = std::string{"bytecode{}"};
const auto names = evmc_get_instruction_names_table(rev);
for (auto it = bytecode.begin(); it != bytecode.end(); ++it)
{
const auto opcode = *it;
if (const auto name = names[opcode]; name)
if (const auto name = evmone::instr::traits[opcode].name; name)
{
s += std::string{" + OP_"} + name;

Expand Down

0 comments on commit 8ed5814

Please sign in to comment.