Skip to content

Commit

Permalink
Make execution fail with exception when running out of code section
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jun 22, 2021
1 parent 24c79c9 commit 101eaf4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/evmone/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ AdvancedCodeAnalysis analyze(

// Make sure the last block is terminated.
// TODO: This is not needed if the last instruction is a terminating one.
analysis.instrs.emplace_back(op_tbl[OP_STOP].fn);
analysis.instrs.emplace_back(op_tbl[header.is_legacy_code() ? OP_STOP : OP_INVALID].fn);

// FIXME: assert(analysis.instrs.size() <= max_instrs_size);

Expand Down
15 changes: 10 additions & 5 deletions lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ CodeAnalysis analyze(const uint8_t* code, size_t code_size, const EOF1Header& he
// Using "raw" new operator instead of std::make_unique() to get uninitialized array.
std::unique_ptr<uint8_t[]> padded_code{new uint8_t[i + 1]}; // +1 for the final STOP.
std::copy_n(code, code_end, padded_code.get());
padded_code[i] = OP_STOP; // Set final STOP at the code end.
padded_code[i] =
header.is_legacy_code() ? OP_STOP : OP_INVALID; // Set final STOP/INVALID at the code end.

// TODO: Using fixed-size padding of 33, the padded code buffer and jumpdest bitmap can be
// created with single allocation.
Expand All @@ -48,13 +49,13 @@ CodeAnalysis analyze(const uint8_t* code, size_t code_size, const EOF1Header& he
namespace
{
const uint8_t* op_jump(ExecutionState& state, const CodeAnalysis::JumpdestMap& jumpdest_map,
const uint8_t* code, const EOF1Header& header) noexcept
const uint8_t* code) noexcept
{
const auto dst = state.stack.pop();
if (dst >= jumpdest_map.size() || !jumpdest_map[static_cast<size_t>(dst)])
{
state.status = EVMC_BAD_JUMP_DESTINATION;
return code + header.code_end(state.code.size());
return state.code.end();
}

return code + static_cast<size_t>(dst);
Expand Down Expand Up @@ -400,12 +401,16 @@ evmc_result execute(const VM& vm, ExecutionState& state, const EOF1Header& heade
}

case OP_JUMP:
pc = op_jump(state, analysis.jumpdest_map, analysis.padded_code.get(), header);
pc = op_jump(state, analysis.jumpdest_map, analysis.padded_code.get());
if (state.status == EVMC_BAD_JUMP_DESTINATION)
goto exit;
continue;
case OP_JUMPI:
if (state.stack[1] != 0)
{
pc = op_jump(state, analysis.jumpdest_map, analysis.padded_code.get(), header);
pc = op_jump(state, analysis.jumpdest_map, analysis.padded_code.get());
if (state.status == EVMC_BAD_JUMP_DESTINATION)
goto exit;
}
else
{
Expand Down
9 changes: 8 additions & 1 deletion test/unittests/evm_eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ TEST_P(evm, eof_execution_with_data_section)
{
rev = EVMC_SHANGHAI;
// data section contains ret(0, 1)
const auto code = eof_bytecode(mstore8(0, 1), ret(0, 1));
auto code = eof_bytecode(mstore8(0, 1), ret(0, 1));

execute(code);
EXPECT_STATUS(EVMC_INVALID_INSTRUCTION);
EXPECT_EQ(result.output_size, 0);

// data section contains ret(0, 1)
code = eof_bytecode(mstore8(0, 1) + OP_STOP, ret(0, 1));

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
Expand Down

0 comments on commit 101eaf4

Please sign in to comment.