Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow in analysis around gas cost of undefined instruction #93

Merged
merged 3 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/evmone/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ code_analysis analyze(
auto& instr = jumpdest ? analysis.instrs.back() : analysis.instrs.emplace_back(fns[c]);

auto metrics = instr_table[c];
block->gas_cost += metrics.gas_cost;
if (metrics.gas_cost > 0) // can be -1 for undefined instruction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know this instruction will be hit, shouldn't the cost be set to max instead (e.g. out of gas)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think then it would stop with EVMC_OUT_OF_GAS at the beginning of the block, instead of expected EVMC_UNDEFINED_INSTRUCTION

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should set it to 0 as this is real gas cost of executing it. Setting it to "max" is a nice hack, but I don't waste 64-bits in the table just for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block->gas_cost += metrics.gas_cost;
auto stack_req = metrics.num_stack_arguments - block->stack_diff;
block->stack_diff += (metrics.num_stack_returned_items - metrics.num_stack_arguments);
block->stack_req = std::max(block->stack_req, stack_req);
Expand Down
31 changes: 31 additions & 0 deletions test/unittests/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,37 @@ TEST_F(evm, undefined_instructions)
}
}

TEST_F(evm, undefined_instruction_analysis_overflow)
{
rev = EVMC_PETERSBURG;

auto undefined_opcode = evmc_opcode(0x0c);
auto code = bytecode{undefined_opcode};

execute(code);
EXPECT_EQ(result.status_code, EVMC_UNDEFINED_INSTRUCTION);
}

TEST_F(evm, undefined_instruction_block_cost_negative)
{
// For undefined instructions EVMC instruction tables have cost -1.
// If naively counted block costs can become negative.

const auto max_gas = std::numeric_limits<int64_t>::max();

const auto code1 = bytecode{} + "0f"; // Block cost -1.
execute(max_gas, code1);
EXPECT_STATUS(EVMC_UNDEFINED_INSTRUCTION);

const auto code2 = bytecode{} + OP_JUMPDEST + "c6" + "4b" + OP_STOP; // Block cost -1.
execute(max_gas, code2);
EXPECT_STATUS(EVMC_UNDEFINED_INSTRUCTION);

const auto code3 = bytecode{} + OP_ADDRESS + "2a" + "2b" + "2c" + "2d"; // Block cost -2.
execute(max_gas, code3);
EXPECT_STATUS(EVMC_UNDEFINED_INSTRUCTION);
}

TEST_F(evm, abort)
{
for (auto r = 0; r <= EVMC_MAX_REVISION; ++r)
Expand Down