Skip to content

Commit

Permalink
Correct jumpdest analysis of EOF code in baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 31, 2021
1 parent fe4230b commit 911e8b3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@

namespace evmone::baseline
{
CodeAnalysis analyze(const uint8_t* code, size_t code_size)
CodeAnalysis analyze(const uint8_t* code, size_t code_size, const EOF1Header& header)
{
// To find if op is any PUSH opcode (OP_PUSH1 <= op <= OP_PUSH32)
// it can be noticed that OP_PUSH32 is INT8_MAX (0x7f) therefore
// static_cast<int8_t>(op) <= OP_PUSH32 is always true and can be skipped.
static_assert(OP_PUSH32 == std::numeric_limits<int8_t>::max());

// TODO optimize to store only bits for code section
CodeAnalysis::JumpdestMap map(code_size); // Allocate and init bitmap with zeros.
for (size_t i = 0; i < code_size; ++i)
for (auto i = header.code_begin(code); i < header.code_end(code, code_size); ++i)
{
const auto op = code[i];
const auto op = *i;
if (static_cast<int8_t>(op) >= OP_PUSH1) // If any PUSH opcode (see explanation above).
i += op - size_t{OP_PUSH1 - 1}; // Skip PUSH data.
else if (INTX_UNLIKELY(op == OP_JUMPDEST))
map[i] = true;
map[static_cast<size_t>(i - code)] = true;
}
return CodeAnalysis{std::move(map)};
}
Expand Down Expand Up @@ -788,7 +789,7 @@ evmc_result execute(evmc_vm* c_vm, const evmc_host_interface* host, evmc_host_co
EOF1Header eof1_header;
if (is_eof_code(code, code_size))
eof1_header = read_valid_eof1_header(code);
const auto jumpdest_map = analyze(code, code_size);
const auto jumpdest_map = analyze(code, code_size, eof1_header);
auto state = std::make_unique<ExecutionState>(*msg, rev, *host, ctx, code, code_size);
return execute(*vm, *state, eof1_header, jumpdest_map);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/baseline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct CodeAnalysis
};

/// Analyze the code to build the bitmap of valid JUMPDEST locations.
EVMC_EXPORT CodeAnalysis analyze(const uint8_t* code, size_t code_size);
EVMC_EXPORT CodeAnalysis analyze(const uint8_t* code, size_t code_size, const EOF1Header& header);

/// Executes in Baseline interpreter using EVMC-compatible parameters.
evmc_result execute(evmc_vm* vm, const evmc_host_interface* host, evmc_host_context* ctx,
Expand Down
3 changes: 2 additions & 1 deletion test/bench/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <evmc/mocked_host.hpp>
#include <evmone/analysis.hpp>
#include <evmone/baseline.hpp>
#include <evmone/eof.hpp>

namespace evmone::test
{
Expand Down Expand Up @@ -37,7 +38,7 @@ inline void baseline_analyze(benchmark::State& state, bytes_view code) noexcept
auto bytes_analysed = uint64_t{0};
for (auto _ : state)
{
auto r = evmone::baseline::analyze(code.data(), code.size());
auto r = evmone::baseline::analyze(code.data(), code.size(), {});
benchmark::DoNotOptimize(r);
bytes_analysed += code.size();
}
Expand Down

0 comments on commit 911e8b3

Please sign in to comment.