Skip to content

Commit

Permalink
Merge pull request #474 from ethereum/advanced_fix_jumpi
Browse files Browse the repository at this point in the history
advanced: Fix JUMPI followed by stack underflow
  • Loading branch information
chfast authored Jun 14, 2022
2 parents aea66eb + 1a41241 commit ae7e826
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
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

0 comments on commit ae7e826

Please sign in to comment.