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

advanced: Fix JUMPI followed by stack underflow #474

Merged
merged 1 commit into from
Jun 14, 2022
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
16 changes: 7 additions & 9 deletions lib/evmone/advanced_instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,16 @@ const Instruction* op_jump(const Instruction*, AdvancedExecutionState& state) no
const Instruction* op_jumpi(const Instruction* instr, AdvancedExecutionState& state) noexcept
{
if (state.stack[1] != 0)
instr = op_jump(instr, state);
{
instr = op_jump(instr, state); // target
state.stack.pop(); // condition
}
else
{
state.stack.pop();

instr = opx_beginblock(instr, state);
state.stack.pop(); // target
state.stack.pop(); // condition
instr = opx_beginblock(instr, state); // follow-by block
}

// OPT: The pc must be the BEGINBLOCK (even in fallback case),
// so we can execute it straight away.

state.stack.pop();
return instr;
}

Expand Down
6 changes: 6 additions & 0 deletions test/unittests/evm_control_flow_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ TEST_P(evm, jumpi_jumpdest)
EXPECT_GAS_USED(EVMC_SUCCESS, 20);
}

TEST_P(evm, jumpi_followed_by_stack_underflow)
{
execute(push(0) + OP_DUP1 + OP_JUMPI + OP_POP);
EXPECT_STATUS(EVMC_STACK_UNDERFLOW);
}

TEST_P(evm, pc_sum)
{
const auto code = 4 * OP_PC + 3 * OP_ADD + ret_top();
Expand Down