Skip to content

Commit

Permalink
Remove gas_left from ExecutionState
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Apr 12, 2023
1 parent 9cc3522 commit c8c1694
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 33 deletions.
3 changes: 3 additions & 0 deletions lib/evmone/advanced_analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Stack
/// The execution state specialized for the Advanced interpreter.
struct AdvancedExecutionState : ExecutionState
{
int64_t gas_left = 0;
Stack stack;

/// The gas cost of the current block.
Expand All @@ -93,6 +94,7 @@ struct AdvancedExecutionState : ExecutionState
const evmc_host_interface& host_interface, evmc_host_context* host_ctx,
bytes_view _code) noexcept
: ExecutionState{message, revision, host_interface, host_ctx, _code},
gas_left{message.gas},
stack{stack_space.bottom()}
{}

Expand All @@ -109,6 +111,7 @@ struct AdvancedExecutionState : ExecutionState
bytes_view _code) noexcept
{
ExecutionState::reset(message, revision, host_interface, host_ctx, _code);
gas_left = message.gas;
stack.reset(stack_space.bottom());
analysis.advanced = nullptr; // For consistency with previous behavior.
current_block_cost = 0;
Expand Down
37 changes: 15 additions & 22 deletions lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,11 @@ template <Opcode Op>


template <bool TracingEnabled>
void dispatch(const CostTable& cost_table, ExecutionState& state, const uint8_t* code,
Tracer* tracer = nullptr) noexcept
int64_t dispatch(const CostTable& cost_table, ExecutionState& state, int64_t gas,
const uint8_t* code, Tracer* tracer = nullptr) noexcept
{
const auto stack_bottom = state.stack_space.bottom();

auto gas = state.gas_left;

// Code iterator and stack top pointer for interpreter loop.
Position position{code, stack_bottom};

Expand All @@ -255,8 +253,7 @@ void dispatch(const CostTable& cost_table, ExecutionState& state, const uint8_t*
if (const auto next = invoke<OPCODE>(cost_table, stack_bottom, position, gas, state); \
next.code_it == nullptr) \
{ \
state.gas_left = gas; \
return; \
return gas; \
} \
else \
{ \
Expand All @@ -271,16 +268,15 @@ void dispatch(const CostTable& cost_table, ExecutionState& state, const uint8_t*

default:
state.status = EVMC_UNDEFINED_INSTRUCTION;
state.gas_left = gas;
return;
return gas;
}
}
INTX_UNREACHABLE();
}

#if EVMONE_CGOTO_SUPPORTED
void dispatch_cgoto(
const CostTable& cost_table, ExecutionState& state, const uint8_t* code) noexcept
int64_t dispatch_cgoto(
const CostTable& cost_table, ExecutionState& state, int64_t gas, const uint8_t* code) noexcept
{
#pragma GCC diagnostic ignored "-Wpedantic"

Expand All @@ -300,17 +296,14 @@ void dispatch_cgoto(
// Code iterator and stack top pointer for interpreter loop.
Position position{code, stack_bottom};

auto gas = state.gas_left;

goto* cgoto_table[*position.code_it];

#define ON_OPCODE(OPCODE) \
TARGET_##OPCODE : ASM_COMMENT(OPCODE); \
if (const auto next = invoke<OPCODE>(cost_table, stack_bottom, position, gas, state); \
next.code_it == nullptr) \
{ \
state.gas_left = gas; \
return; \
return gas; \
} \
else \
{ \
Expand All @@ -325,12 +318,13 @@ void dispatch_cgoto(

TARGET_OP_UNDEFINED:
state.status = EVMC_UNDEFINED_INSTRUCTION;
state.gas_left = gas;
return gas;
}
#endif
} // namespace

evmc_result execute(const VM& vm, ExecutionState& state, const CodeAnalysis& analysis) noexcept
evmc_result execute(
const VM& vm, int64_t gas, ExecutionState& state, const CodeAnalysis& analysis) noexcept
{
state.analysis.baseline = &analysis; // Assign code analysis for instruction implementations.

Expand All @@ -342,20 +336,19 @@ evmc_result execute(const VM& vm, ExecutionState& state, const CodeAnalysis& ana
if (INTX_UNLIKELY(tracer != nullptr))
{
tracer->notify_execution_start(state.rev, *state.msg, analysis.executable_code);
dispatch<true>(cost_table, state, code.data(), tracer);
gas = dispatch<true>(cost_table, state, gas, code.data(), tracer);
}
else
{
#if EVMONE_CGOTO_SUPPORTED
if (vm.cgoto)
dispatch_cgoto(cost_table, state, code.data());
gas = dispatch_cgoto(cost_table, state, gas, code.data());
else
#endif
dispatch<false>(cost_table, state, code.data());
gas = dispatch<false>(cost_table, state, gas, code.data());
}

const auto gas_left =
(state.status == EVMC_SUCCESS || state.status == EVMC_REVERT) ? state.gas_left : 0;
const auto gas_left = (state.status == EVMC_SUCCESS || state.status == EVMC_REVERT) ? gas : 0;
const auto gas_refund = (state.status == EVMC_SUCCESS) ? state.gas_refund : 0;

assert(state.output_size != 0 || state.output_offset == 0);
Expand All @@ -375,6 +368,6 @@ evmc_result execute(evmc_vm* c_vm, const evmc_host_interface* host, evmc_host_co
const auto jumpdest_map = analyze(rev, {code, code_size});
auto state =
std::make_unique<ExecutionState>(*msg, rev, *host, ctx, bytes_view{code, code_size});
return execute(*vm, *state, jumpdest_map);
return execute(*vm, msg->gas, *state, jumpdest_map);
}
} // namespace evmone::baseline
2 changes: 1 addition & 1 deletion lib/evmone/baseline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ evmc_result execute(evmc_vm* vm, const evmc_host_interface* host, evmc_host_cont

/// Executes in Baseline interpreter on the given external and initialized state.
EVMC_EXPORT evmc_result execute(
const VM&, ExecutionState& state, const CodeAnalysis& analysis) noexcept;
const VM&, int64_t gas_limit, ExecutionState& state, const CodeAnalysis& analysis) noexcept;

} // namespace baseline
} // namespace evmone
8 changes: 1 addition & 7 deletions lib/evmone/execution_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class Memory
class ExecutionState
{
public:
int64_t gas_left = 0;
int64_t gas_refund = 0;
Memory memory;
const evmc_message* msg = nullptr;
Expand Down Expand Up @@ -166,19 +165,14 @@ class ExecutionState
ExecutionState(const evmc_message& message, evmc_revision revision,
const evmc_host_interface& host_interface, evmc_host_context* host_ctx,
bytes_view _code) noexcept
: gas_left{message.gas},
msg{&message},
host{host_interface, host_ctx},
rev{revision},
original_code{_code}
: msg{&message}, host{host_interface, host_ctx}, rev{revision}, original_code{_code}
{}

/// Resets the contents of the ExecutionState so that it could be reused.
void reset(const evmc_message& message, evmc_revision revision,
const evmc_host_interface& host_interface, evmc_host_context* host_ctx,
bytes_view _code) noexcept
{
gas_left = message.gas;
gas_refund = 0;
memory.clear();
msg = &message;
Expand Down
2 changes: 1 addition & 1 deletion test/bench/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ inline evmc::Result baseline_execute(evmc::VM& c_vm, ExecutionState& exec_state,
{
const auto& vm = *static_cast<evmone::VM*>(c_vm.get_raw_pointer());
exec_state.reset(msg, rev, host.get_interface(), host.to_context(), code);
return evmc::Result{baseline::execute(vm, exec_state, analysis)};
return evmc::Result{baseline::execute(vm, msg.gas, exec_state, analysis)};
}

inline evmc::Result evmc_execute(evmc::VM& vm, FakeExecutionState& /*exec_state*/,
Expand Down
2 changes: 0 additions & 2 deletions test/unittests/execution_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ TEST(execution_state, construct)
const evmone::ExecutionState st{
msg, EVMC_MAX_REVISION, host_interface, nullptr, {code, std::size(code)}};

EXPECT_EQ(st.gas_left, -1);
EXPECT_EQ(st.memory.size(), 0);
EXPECT_EQ(st.msg, &msg);
EXPECT_EQ(st.rev, EVMC_MAX_REVISION);
Expand All @@ -42,7 +41,6 @@ TEST(execution_state, default_construct)
{
const evmone::ExecutionState st;

EXPECT_EQ(st.gas_left, 0);
EXPECT_EQ(st.memory.size(), 0);
EXPECT_EQ(st.msg, nullptr);
EXPECT_EQ(st.rev, EVMC_FRONTIER);
Expand Down

0 comments on commit c8c1694

Please sign in to comment.