Skip to content

Commit 43540d4

Browse files
authored
Merge pull request ipsilon#536 from ethereum/fix-eof-tracing
Fix instruction tracing of EOF code
2 parents 8a5a70d + debc9a1 commit 43540d4

4 files changed

Lines changed: 32 additions & 16 deletions

File tree

lib/evmone/baseline.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ std::unique_ptr<uint8_t[]> pad_code(bytes_view code)
6464
CodeAnalysis analyze_legacy(bytes_view code)
6565
{
6666
// TODO: The padded code buffer and jumpdest bitmap can be created with single allocation.
67-
return {pad_code(code), analyze_jumpdests(code)};
67+
return {pad_code(code), code.size(), analyze_jumpdests(code)};
6868
}
6969

7070
CodeAnalysis analyze_eof1(bytes_view eof_container, const EOF1Header& header)
7171
{
7272
const auto executable_code = eof_container.substr(header.code_begin(), header.code_size);
73-
return {executable_code.data(), analyze_jumpdests(executable_code)};
73+
return {executable_code, analyze_jumpdests(executable_code)};
7474
}
7575
} // namespace
7676

@@ -326,17 +326,17 @@ evmc_result execute(const VM& vm, ExecutionState& state, const CodeAnalysis& ana
326326
auto* tracer = vm.get_tracer();
327327
if (INTX_UNLIKELY(tracer != nullptr))
328328
{
329-
tracer->notify_execution_start(state.rev, *state.msg, state.original_code);
330-
dispatch<true>(cost_table, state, code, tracer);
329+
tracer->notify_execution_start(state.rev, *state.msg, analysis.executable_code);
330+
dispatch<true>(cost_table, state, code.data(), tracer);
331331
}
332332
else
333333
{
334334
#if EVMONE_CGOTO_SUPPORTED
335335
if (vm.cgoto)
336-
dispatch_cgoto(cost_table, state, code);
336+
dispatch_cgoto(cost_table, state, code.data());
337337
else
338338
#endif
339-
dispatch<false>(cost_table, state, code);
339+
dispatch<false>(cost_table, state, code.data());
340340
}
341341

342342
const auto gas_left =

lib/evmone/baseline.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ class CodeAnalysis
2323
public:
2424
using JumpdestMap = std::vector<bool>;
2525

26-
const uint8_t* executable_code; ///< Pointer to the beginning of executable code section.
27-
JumpdestMap jumpdest_map; ///< Map of valid jump destinations.
26+
bytes_view executable_code; ///< Executable code section.
27+
JumpdestMap jumpdest_map; ///< Map of valid jump destinations.
2828

2929
private:
3030
/// Padded code for faster legacy code execution.
3131
/// If not nullptr the executable_code must point to it.
3232
std::unique_ptr<uint8_t[]> m_padded_code;
3333

3434
public:
35-
CodeAnalysis(std::unique_ptr<uint8_t[]> padded_code, JumpdestMap map)
36-
: executable_code{padded_code.get()},
35+
CodeAnalysis(std::unique_ptr<uint8_t[]> padded_code, size_t code_size, JumpdestMap map)
36+
: executable_code{padded_code.get(), code_size},
3737
jumpdest_map{std::move(map)},
3838
m_padded_code{std::move(padded_code)}
3939
{}
4040

41-
CodeAnalysis(const uint8_t* code, JumpdestMap map)
41+
CodeAnalysis(bytes_view code, JumpdestMap map)
4242
: executable_code{code}, jumpdest_map{std::move(map)}
4343
{}
4444
};

lib/evmone/instructions.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ inline code_iterator jump_impl(ExecutionState& state, const uint256& dst) noexce
684684
return nullptr;
685685
}
686686

687-
return state.analysis.baseline->executable_code + static_cast<size_t>(dst);
687+
return &state.analysis.baseline->executable_code[static_cast<size_t>(dst)];
688688
}
689689

690690
/// JUMP instruction implementation using baseline::CodeAnalysis.
@@ -703,7 +703,7 @@ inline code_iterator jumpi(StackTop stack, ExecutionState& state, code_iterator
703703

704704
inline code_iterator pc(StackTop stack, ExecutionState& state, code_iterator pos) noexcept
705705
{
706-
stack.push(static_cast<uint64_t>(pos - state.analysis.baseline->executable_code));
706+
stack.push(static_cast<uint64_t>(pos - state.analysis.baseline->executable_code.data()));
707707
return pos + 1;
708708
}
709709

test/unittests/tracing_test.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ class tracing : public Test
2828
vm{*static_cast<evmone::VM*>(m_baseline_vm.get_raw_pointer())}
2929
{}
3030

31-
std::string trace(bytes_view code, int32_t depth = 0, uint32_t flags = 0)
31+
std::string trace(
32+
bytes_view code, int32_t depth = 0, uint32_t flags = 0, evmc_revision rev = EVMC_BERLIN)
3233
{
3334
evmc::MockedHost host;
3435
evmc_message msg{};
35-
msg.flags = flags;
3636
msg.depth = depth;
37+
msg.flags = flags;
3738
msg.gas = 1000000;
38-
m_baseline_vm.execute(host, EVMC_BERLIN, msg, code.data(), code.size());
39+
m_baseline_vm.execute(host, rev, msg, code.data(), code.size());
3940
auto result = trace_stream.str();
4041
trace_stream.str({});
4142
return result;
@@ -290,3 +291,18 @@ TEST_F(tracing, trace_code_containing_zero)
290291

291292
EXPECT_EQ(tracer.get_last_code().size(), code.size());
292293
}
294+
295+
TEST_F(tracing, trace_eof)
296+
{
297+
vm.add_tracer(evmone::create_instruction_tracer(trace_stream));
298+
299+
trace_stream << '\n';
300+
EXPECT_EQ(trace(eof1_bytecode(add(2, 3) + OP_STOP), 0, 0, EVMC_SHANGHAI), R"(
301+
{"depth":0,"rev":"Shanghai","static":false}
302+
{"pc":0,"op":96,"opName":"PUSH1","gas":1000000,"stack":[],"memorySize":0}
303+
{"pc":2,"op":96,"opName":"PUSH1","gas":999997,"stack":["0x3"],"memorySize":0}
304+
{"pc":4,"op":1,"opName":"ADD","gas":999994,"stack":["0x3","0x2"],"memorySize":0}
305+
{"pc":5,"op":0,"opName":"STOP","gas":999991,"stack":["0x5"],"memorySize":0}
306+
{"error":null,"gas":999991,"gasUsed":9,"output":""}
307+
)");
308+
}

0 commit comments

Comments
 (0)