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

Rework main loop iteration #107

Merged
merged 2 commits into from
Jul 24, 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
21 changes: 4 additions & 17 deletions lib/evmone/analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ using bytes32 = std::array<uint8_t, 32>;

using bytes = std::basic_string<uint8_t>;

/// The status code value indicating that the execution should continue.
/// The 0 value is used.
constexpr auto continue_status = EVMC_SUCCESS;
static_assert(continue_status == 0, "The 'continue' status is not 0");

/// The status code value indicating that the execution should be stopped.
/// The internal error (-1) is used. We could use any other negative value,
/// but using one of the constants defined by evmc_status_code avoids
/// warnings in Undefined Behavior Sanitizer.
/// The EVMC_INTERNAL_ERROR MUST NOT be used in evmone for any other case.
constexpr auto stop_status = EVMC_INTERNAL_ERROR;

/// The stack for 256-bit EVM words.
///
/// This implementation reserves memory inplace for all possible stack items (1024),
Expand Down Expand Up @@ -68,10 +56,12 @@ struct evm_stack
uint256 pop() noexcept { return *top_item--; }
};

struct instr_info;

struct execution_state
{
const instr_info* next_instr{nullptr};
evmc_status_code status = EVMC_SUCCESS;
size_t pc = 0;
int64_t gas_left = 0;

evm_stack stack;
Expand Down Expand Up @@ -100,11 +90,8 @@ struct execution_state
/// Terminates the execution with the given status code.
void exit(evmc_status_code status_code) noexcept
{
// If the status_code matches the "continue" status, replace it with the "stop" status.
// That will be revert after the execution loop terminates.
if (status_code == continue_status)
status_code = stop_status;
status = status_code;
next_instr = nullptr;
}
};

Expand Down
12 changes: 6 additions & 6 deletions lib/evmone/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ evmc_result execute(evmc_instance*, evmc_context* ctx, evmc_revision rev, const

auto state = std::make_unique<execution_state>();
state->analysis = &analysis;
state->next_instr = &state->analysis->instrs[0];
state->msg = msg;
state->code = code;
state->code_size = code_size;
state->host = evmc::HostContext{ctx};
state->gas_left = msg->gas;
state->rev = rev;
while (state->status == continue_status)
while (state->next_instr)
{
auto& instr = analysis.instrs[state->pc];
const auto& instr = *state->next_instr;

// Advance the PC not to allow jump opcodes to overwrite it.
++state->pc;
// Advance next_instr to allow jump opcodes to overwrite it.
++state->next_instr;

instr.fn(*state, instr.arg);
}

evmc_result result{};

// Assign status code, revert the "stop" status back to "continue" status - see .exit().
result.status_code = state->status != stop_status ? state->status : continue_status;
result.status_code = state->status;

if (result.status_code == EVMC_SUCCESS || result.status_code == EVMC_REVERT)
result.gas_left = state->gas_left;
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void op_jump(execution_state& state, instr_argument) noexcept
(pc = state.analysis->find_jumpdest(static_cast<int>(dst))) < 0)
return state.exit(EVMC_BAD_JUMP_DESTINATION);

state.pc = static_cast<size_t>(pc);
state.next_instr = &state.analysis->instrs[static_cast<size_t>(pc)];
}

void op_jumpi(execution_state& state, instr_argument arg) noexcept
Expand Down