From f158ca3602d60757e4b1386a6338ac694ba4de21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 10 May 2021 09:54:02 +0200 Subject: [PATCH 1/2] Rename evmone.cpp -> vm.cpp Rename evmone.cpp -> vm.cpp in anticipation for creating VM class. --- lib/evmone/CMakeLists.txt | 4 ++-- lib/evmone/{evmone.cpp => vm.cpp} | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) rename lib/evmone/{evmone.cpp => vm.cpp} (93%) diff --git a/lib/evmone/CMakeLists.txt b/lib/evmone/CMakeLists.txt index 8e86589f73..1cb45226cd 100644 --- a/lib/evmone/CMakeLists.txt +++ b/lib/evmone/CMakeLists.txt @@ -13,7 +13,6 @@ add_library(evmone analysis.hpp baseline.cpp baseline.hpp - evmone.cpp execution.cpp execution.hpp instruction_traits.hpp @@ -22,6 +21,7 @@ add_library(evmone instructions_calls.cpp limits.hpp opcodes_helpers.h + vm.cpp ) target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE evmc::instructions ethash::keccak) target_include_directories(evmone PUBLIC @@ -42,6 +42,6 @@ if(NOT SANITIZE) target_link_options(evmone PRIVATE $<$:LINKER:--no-undefined>) endif() -set_source_files_properties(evmone.cpp PROPERTIES COMPILE_DEFINITIONS PROJECT_VERSION="${PROJECT_VERSION}") +set_source_files_properties(vm.cpp PROPERTIES COMPILE_DEFINITIONS PROJECT_VERSION="${PROJECT_VERSION}") add_standalone_library(evmone) diff --git a/lib/evmone/evmone.cpp b/lib/evmone/vm.cpp similarity index 93% rename from lib/evmone/evmone.cpp rename to lib/evmone/vm.cpp index 3bdb92104c..fc2fabaa64 100644 --- a/lib/evmone/evmone.cpp +++ b/lib/evmone/vm.cpp @@ -1,10 +1,9 @@ // evmone: Fast Ethereum Virtual Machine implementation -// Copyright 2018-2020 The evmone Authors. +// Copyright 2018 The evmone Authors. // SPDX-License-Identifier: Apache-2.0 /// @file /// EVMC instance and entry point of evmone is defined here. -/// The file name matches the evmone.h public header. #include "baseline.hpp" #include "execution.hpp" From 2a71ef72065dffbb503662538fb6380a0dc75add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 13 Jan 2021 14:38:32 +0100 Subject: [PATCH 2/2] Add evmone::VM class This adds evmone::VM class where we can store additional data (e.g. configuration options) --- lib/evmone/baseline.cpp | 8 +++++--- lib/evmone/baseline.hpp | 12 +++++++++--- lib/evmone/vm.cpp | 26 +++++++++++++++++--------- lib/evmone/vm.hpp | 16 ++++++++++++++++ 4 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 lib/evmone/vm.hpp diff --git a/lib/evmone/baseline.cpp b/lib/evmone/baseline.cpp index 57954e0235..b1a1337e7c 100644 --- a/lib/evmone/baseline.cpp +++ b/lib/evmone/baseline.cpp @@ -5,6 +5,7 @@ #include "baseline.hpp" #include "execution_state.hpp" #include "instructions.hpp" +#include "vm.hpp" #include #include @@ -96,15 +97,16 @@ inline evmc_status_code check_requirements(const char* const* instruction_names, } } // namespace -evmc_result execute(evmc_vm* /*vm*/, const evmc_host_interface* host, evmc_host_context* ctx, +evmc_result execute(evmc_vm* c_vm, const evmc_host_interface* host, evmc_host_context* ctx, evmc_revision rev, const evmc_message* msg, const uint8_t* code, size_t code_size) noexcept { + auto vm = static_cast(c_vm); const auto jumpdest_map = analyze(code, code_size); auto state = std::make_unique(*msg, rev, *host, ctx, code, code_size); - return execute(*state, jumpdest_map); + return execute(*vm, *state, jumpdest_map); } -evmc_result execute(ExecutionState& state, const CodeAnalysis& analysis) noexcept +evmc_result execute(const VM& /*vm*/, ExecutionState& state, const CodeAnalysis& analysis) noexcept { const auto rev = state.rev; const auto code = state.code.data(); diff --git a/lib/evmone/baseline.hpp b/lib/evmone/baseline.hpp index 7df4982dc8..fabc867860 100644 --- a/lib/evmone/baseline.hpp +++ b/lib/evmone/baseline.hpp @@ -8,7 +8,11 @@ #include #include -namespace evmone::baseline +namespace evmone +{ +class VM; + +namespace baseline { struct CodeAnalysis { @@ -25,5 +29,7 @@ evmc_result execute(evmc_vm* vm, const evmc_host_interface* host, evmc_host_cont evmc_revision rev, const evmc_message* msg, const uint8_t* code, size_t code_size) noexcept; /// Executes in Baseline interpreter on the given external and initialized state. -evmc_result execute(ExecutionState& state, const CodeAnalysis& analysis) noexcept; -} // namespace evmone::baseline +evmc_result execute(const VM&, ExecutionState& state, const CodeAnalysis& analysis) noexcept; + +} // namespace baseline +} // namespace evmone diff --git a/lib/evmone/vm.cpp b/lib/evmone/vm.cpp index fc2fabaa64..870b93b397 100644 --- a/lib/evmone/vm.cpp +++ b/lib/evmone/vm.cpp @@ -3,8 +3,9 @@ // SPDX-License-Identifier: Apache-2.0 /// @file -/// EVMC instance and entry point of evmone is defined here. +/// EVMC instance (class VM) and entry point of evmone is defined here. +#include "vm.hpp" #include "baseline.hpp" #include "execution.hpp" #include @@ -15,8 +16,8 @@ namespace { void destroy(evmc_vm* vm) noexcept { - // TODO: Mark function with [[gnu:nonnull]] or add CHECK(). - delete vm; + assert(vm != nullptr); + delete static_cast(vm); } constexpr evmc_capabilities_flagset get_capabilities(evmc_vm* /*vm*/) noexcept @@ -42,13 +43,12 @@ evmc_set_option_result set_option(evmc_vm* vm, char const* name, char const* val } return EVMC_SET_OPTION_INVALID_NAME; } + } // namespace -} // namespace evmone -extern "C" { -EVMC_EXPORT evmc_vm* evmc_create_evmone() noexcept -{ - return new evmc_vm{ + +inline constexpr VM::VM() noexcept + : evmc_vm{ EVMC_ABI_VERSION, "evmone", PROJECT_VERSION, @@ -56,6 +56,14 @@ EVMC_EXPORT evmc_vm* evmc_create_evmone() noexcept evmone::execute, evmone::get_capabilities, evmone::set_option, - }; + } +{} + +} // namespace evmone + +extern "C" { +EVMC_EXPORT evmc_vm* evmc_create_evmone() noexcept +{ + return new evmone::VM{}; } } diff --git a/lib/evmone/vm.hpp b/lib/evmone/vm.hpp new file mode 100644 index 0000000000..2028ce55b0 --- /dev/null +++ b/lib/evmone/vm.hpp @@ -0,0 +1,16 @@ +// evmone: Fast Ethereum Virtual Machine implementation +// Copyright 2021 The evmone Authors. +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include + +namespace evmone +{ +/// The evmone EVMC instance. +class VM : public evmc_vm +{ +public: + inline constexpr VM() noexcept; +}; +} // namespace evmone