From c65efdf59da7e165285a559a7e1a41e119be4659 Mon Sep 17 00:00:00 2001 From: rodiazet Date: Mon, 28 Nov 2022 11:31:05 +0100 Subject: [PATCH 1/2] test: Add tracer test for code containing 0 byte --- test/unittests/tracing_test.cpp | 41 ++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/test/unittests/tracing_test.cpp b/test/unittests/tracing_test.cpp index 03a4af6a4c..751de35b1f 100644 --- a/test/unittests/tracing_test.cpp +++ b/test/unittests/tracing_test.cpp @@ -44,15 +44,15 @@ class tracing : public Test { std::string m_name; std::ostringstream& m_trace; - const uint8_t* m_code = nullptr; + bytes_view m_code; void on_execution_start( evmc_revision /*rev*/, const evmc_message& /*msg*/, bytes_view code) noexcept override { - m_code = code.data(); + m_code = code; } - void on_execution_end(const evmc_result& /*result*/) noexcept override { m_code = nullptr; } + void on_execution_end(const evmc_result& /*result*/) noexcept override { m_code = {}; } void on_instruction_start(uint32_t pc, const intx::uint256* /*stack_top*/, int /*stack_height*/, const evmone::ExecutionState& /*state*/) noexcept override @@ -67,6 +67,28 @@ class tracing : public Test : m_name{std::move(name)}, m_trace{parent.trace_stream} {} }; + + class Inspector final : public evmone::Tracer + { + bytes m_last_code; + + void on_execution_start( + evmc_revision /*rev*/, const evmc_message& /*msg*/, bytes_view code) noexcept override + { + m_last_code = code; + } + + void on_execution_end(const evmc_result& /*result*/) noexcept override {} + + void on_instruction_start(uint32_t /*pc*/, const intx::uint256* /*stack_top*/, + int /*stack_height*/, const evmone::ExecutionState& /*state*/) noexcept override + {} + + public: + explicit Inspector() noexcept = default; + + [[nodiscard]] const bytes& get_last_code() const noexcept { return m_last_code; } + }; }; @@ -255,3 +277,16 @@ TEST_F(tracing, trace_undefined_instruction) {"error":"undefined instruction","gas":0,"gasUsed":1000000,"output":""} )"); } + +TEST_F(tracing, trace_code_containing_zero) +{ + auto tracer_ptr = std::make_unique(); + const auto& tracer = *tracer_ptr; + vm.add_tracer(std::move(tracer_ptr)); + + const auto code = bytecode{} + "602a6000556101c960015560068060166000396000f3600035600055"; + + trace(code); + + EXPECT_EQ(tracer.get_last_code().size(), code.size()); +} From ff67ad090179ba6d975b8e8bbcdac48c1170144e Mon Sep 17 00:00:00 2001 From: rodiazet Date: Mon, 28 Nov 2022 11:44:33 +0100 Subject: [PATCH 2/2] baseline: Fix code passing to notify_execution_start --- lib/evmone/baseline.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/evmone/baseline.cpp b/lib/evmone/baseline.cpp index 89f2d8e73e..eec045299a 100644 --- a/lib/evmone/baseline.cpp +++ b/lib/evmone/baseline.cpp @@ -327,7 +327,7 @@ evmc_result execute(const VM& vm, ExecutionState& state, const CodeAnalysis& ana auto* tracer = vm.get_tracer(); if (INTX_UNLIKELY(tracer != nullptr)) { - tracer->notify_execution_start(state.rev, *state.msg, code); + tracer->notify_execution_start(state.rev, *state.msg, state.original_code); dispatch(cost_table, state, code, tracer); } else