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

New stack implementation #79

Merged
merged 9 commits into from
Jul 2, 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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ add_subdirectory(evmc)

cable_configure_compiler(NO_PEDANTIC NO_CONVERSION_WARNINGS)
if(CABLE_COMPILER_GNULIKE)
add_compile_options(-Wno-attributes) # Allow using unknown attributes.

# Prepend to CXX flags to allow overriding.
set(CMAKE_CXX_FLAGS "-fvisibility=hidden ${CMAKE_CXX_FLAGS}")
elseif(MSVC)
add_compile_options(/wd4324) # Disabled warning about alignment caused by alignas.
add_compile_options(/wd5030) # Allow using unknown attributes.
endif()

set(include_dir ${CMAKE_CURRENT_SOURCE_DIR}/include)
Expand Down
40 changes: 37 additions & 3 deletions lib/evmone/analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,49 @@ static_assert(continue_status == 0, "The 'continue' status is not 0");
/// 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),
/// so this type is big. Make sure it is allocated on heap.
struct evm_stack
{
/// The maximum number of stack items.
static constexpr auto limit = 1024;

/// The pointer to the top item, or below the stack bottom if stack is empty.
uint256* top_item;

/// The storage allocated for maximum possible number of items.
/// This is also the pointer to the bottom item.
/// Items are aligned to 256 bits for better packing in cache lines.
alignas(sizeof(uint256)) uint256 storage[limit];

/// Default constructor. Sets the top_item pointer to below the stack bottom.
[[clang::no_sanitize("bounds")]] evm_stack() noexcept : top_item{storage - 1} {}

/// The current number of items on the stack.
int size() noexcept { return static_cast<int>(top_item + 1 - storage); }

/// Returns the reference to the top item.
uint256& top() noexcept { return *top_item; }

/// Returns the reference to the stack item on given position from the stack top.
uint256& operator[](int index) noexcept { return *(top_item - index); }

/// Pushes an item on the stack. The stack limit is not checked.
void push(const uint256& item) noexcept { *++top_item = item; }

/// Returns an item popped from the top of the stack.
uint256 pop() noexcept { return *top_item--; }
};

struct execution_state
{
evmc_status_code status = EVMC_SUCCESS;
size_t pc = 0;
int64_t gas_left = 0;

std::vector<uint256> stack;
evm_stack stack;

std::vector<uint8_t> memory; // TODO: Use bytes.
int64_t memory_prev_cost = 0;
Expand All @@ -61,8 +97,6 @@ struct execution_state

evmc_revision rev = {};

uint256& item(size_t index) noexcept { return stack[stack.size() - index - 1]; }

/// Terminates the execution with the given status code.
void exit(evmc_status_code status_code) noexcept
{
Expand Down
36 changes: 19 additions & 17 deletions lib/evmone/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <evmc/helpers.hpp>
#include <evmc/instructions.h>

#include <memory>

namespace evmone
{
extern const exec_fn_table op_table[];
Expand All @@ -18,37 +20,37 @@ evmc_result execute(evmc_instance*, evmc_context* ctx, evmc_revision rev, const
{
auto analysis = analyze(op_table[rev], rev, code, code_size);

execution_state state;
state.analysis = &analysis;
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)
auto state = std::make_unique<execution_state>();
state->analysis = &analysis;
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)
{
auto& instr = analysis.instrs[state.pc];
auto& instr = analysis.instrs[state->pc];

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

instr.fn(state, instr.arg);
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 != stop_status ? state->status : continue_status;

if (result.status_code == EVMC_SUCCESS || result.status_code == EVMC_REVERT)
result.gas_left = state.gas_left;
result.gas_left = state->gas_left;

if (state.output_size > 0)
if (state->output_size > 0)
{
result.output_size = state.output_size;
result.output_size = state->output_size;
auto output_data = static_cast<uint8_t*>(std::malloc(result.output_size));
std::memcpy(output_data, &state.memory[state.output_offset], result.output_size);
std::memcpy(output_data, &state->memory[state->output_offset], result.output_size);
result.output_data = output_data;
result.release = [](const evmc_result* r) noexcept
{
Expand Down
Loading