From c09570a4ade01c628a15c87b385b39acc985b47a Mon Sep 17 00:00:00 2001 From: Harrm Date: Mon, 26 Aug 2024 17:44:51 +0300 Subject: [PATCH 1/2] Fix WASMEDGE_ID bug --- CMakeLists.txt | 5 +++++ cmake/Hunter/config.cmake | 4 ---- core/runtime/binaryen/module/module_factory_impl.cpp | 2 +- core/runtime/binaryen/module/module_factory_impl.hpp | 2 +- core/runtime/module_factory.hpp | 2 +- core/runtime/wasm_edge/module_factory_impl.cpp | 11 ++++++++--- core/runtime/wasm_edge/module_factory_impl.hpp | 2 +- core/runtime/wavm/module_factory_impl.cpp | 2 +- core/runtime/wavm/module_factory_impl.hpp | 2 +- test/mock/core/runtime/module_factory_mock.hpp | 2 +- 10 files changed, 20 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3bf78a616..890ca074ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,11 @@ set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/cmake/toolchain/cxx20.cmake" CACHE FILEPATH "Default toolchain") +# SHA1 hash of the WasmEdge repository archive. +# Required to separate cached runtimes +# compiled with different WasmEdge versions. +set(WASMEDGE_ID 58aea400de9179ad3e314c7e84fd4da345b8a643) + include("cmake/Hunter/init.cmake") add_compile_options(-gdwarf-4) diff --git a/cmake/Hunter/config.cmake b/cmake/Hunter/config.cmake index 36fd7f9b48..fb69acb6ea 100644 --- a/cmake/Hunter/config.cmake +++ b/cmake/Hunter/config.cmake @@ -57,10 +57,6 @@ if ("${WASM_COMPILER}" STREQUAL "WasmEdge") SPDLOG_FMT_EXTERNAL=ON ) - # hash or version, required later to separate WasmEdge - # binary cache directories from different versions - set(WASMEDGE_ID 58aea400de9179ad3e314c7e84fd4da345b8a643) - hunter_config( WasmEdge URL https://github.com/qdrvm/WasmEdge/archive/refs/heads/update/0.14.0.zip diff --git a/core/runtime/binaryen/module/module_factory_impl.cpp b/core/runtime/binaryen/module/module_factory_impl.cpp index 78bb129da9..d78e9b80ec 100644 --- a/core/runtime/binaryen/module/module_factory_impl.cpp +++ b/core/runtime/binaryen/module/module_factory_impl.cpp @@ -27,7 +27,7 @@ namespace kagome::runtime::binaryen { storage_{std::move(storage)}, hasher_(std::move(hasher)) {} - std::optional ModuleFactoryImpl::compilerType() const { + std::optional ModuleFactoryImpl::compilerType() const { return std::nullopt; } diff --git a/core/runtime/binaryen/module/module_factory_impl.hpp b/core/runtime/binaryen/module/module_factory_impl.hpp index 47c81bbda0..85bf85b1da 100644 --- a/core/runtime/binaryen/module/module_factory_impl.hpp +++ b/core/runtime/binaryen/module/module_factory_impl.hpp @@ -39,7 +39,7 @@ namespace kagome::runtime::binaryen { std::shared_ptr hasher); // ModuleFactory - std::optional compilerType() const override; + std::optional compilerType() const override; CompilationOutcome compile(std::filesystem::path path_compiled, BufferView code) const override; CompilationOutcome> loadCompiled( diff --git a/core/runtime/module_factory.hpp b/core/runtime/module_factory.hpp index e6849fc5f2..b1eeb64411 100644 --- a/core/runtime/module_factory.hpp +++ b/core/runtime/module_factory.hpp @@ -48,7 +48,7 @@ namespace kagome::runtime { * `std::nullopt` means `path_compiled` stores raw WASM code for * interpretation. */ - virtual std::optional compilerType() const = 0; + virtual std::optional compilerType() const = 0; /** * Compile `wasm` code to `path_compiled`. diff --git a/core/runtime/wasm_edge/module_factory_impl.cpp b/core/runtime/wasm_edge/module_factory_impl.cpp index d56e0327d9..256cbe56bc 100644 --- a/core/runtime/wasm_edge/module_factory_impl.cpp +++ b/core/runtime/wasm_edge/module_factory_impl.cpp @@ -24,10 +24,12 @@ #include "runtime/wasm_edge/memory_impl.hpp" #include "runtime/wasm_edge/register_host_api.hpp" #include "runtime/wasm_edge/wrappers.hpp" -#include "utils/mkdirs.hpp" #include "utils/read_file.hpp" #include "utils/write_file.hpp" +static_assert(std::string_view{WASMEDGE_ID}.size() == 40, + "WASMEDGE_ID should be set to WasmEdge repository SHA1 hash"); + namespace kagome::runtime::wasm_edge { enum class Error { INVALID_VALUE_TYPE = 1, @@ -371,11 +373,14 @@ namespace kagome::runtime::wasm_edge { BOOST_ASSERT(host_api_factory_); } - std::optional ModuleFactoryImpl::compilerType() const { + std::optional ModuleFactoryImpl::compilerType() const { if (config_.exec == ExecType::Interpreted) { return std::nullopt; } - return std::string("wasmedge_") + WASMEDGE_ID; + // version changes rarely, don't need the whole hash + static std::string versioned_str = + std::format("wasmedge_{}", std::string_view{WASMEDGE_ID}.substr(0, 12)); + return versioned_str; } CompilationOutcome ModuleFactoryImpl::compile( diff --git a/core/runtime/wasm_edge/module_factory_impl.hpp b/core/runtime/wasm_edge/module_factory_impl.hpp index 044e88fa25..83ad127203 100644 --- a/core/runtime/wasm_edge/module_factory_impl.hpp +++ b/core/runtime/wasm_edge/module_factory_impl.hpp @@ -54,7 +54,7 @@ namespace kagome::runtime::wasm_edge { std::shared_ptr core_factory, Config config); - std::optional compilerType() const override; + std::optional compilerType() const override; CompilationOutcome compile(std::filesystem::path path_compiled, BufferView code) const override; CompilationOutcome> loadCompiled( diff --git a/core/runtime/wavm/module_factory_impl.cpp b/core/runtime/wavm/module_factory_impl.cpp index 1488950899..756b8bf612 100644 --- a/core/runtime/wavm/module_factory_impl.cpp +++ b/core/runtime/wavm/module_factory_impl.cpp @@ -73,7 +73,7 @@ namespace kagome::runtime::wavm { }(); } - std::optional ModuleFactoryImpl::compilerType() const { + std::optional ModuleFactoryImpl::compilerType() const { return "wavm"; } diff --git a/core/runtime/wavm/module_factory_impl.hpp b/core/runtime/wavm/module_factory_impl.hpp index 0435953cfc..ea3c410885 100644 --- a/core/runtime/wavm/module_factory_impl.hpp +++ b/core/runtime/wavm/module_factory_impl.hpp @@ -50,7 +50,7 @@ namespace kagome::runtime::wavm { std::shared_ptr intrinsic_module, std::shared_ptr hasher); - std::optional compilerType() const override; + std::optional compilerType() const override; CompilationOutcome compile(std::filesystem::path path_compiled, BufferView code) const override; CompilationOutcome> loadCompiled( diff --git a/test/mock/core/runtime/module_factory_mock.hpp b/test/mock/core/runtime/module_factory_mock.hpp index 5eae958f4e..654935a7e9 100644 --- a/test/mock/core/runtime/module_factory_mock.hpp +++ b/test/mock/core/runtime/module_factory_mock.hpp @@ -16,7 +16,7 @@ namespace kagome::runtime { class ModuleFactoryMock final : public ModuleFactory { public: - MOCK_METHOD(std::optional, + MOCK_METHOD(std::optional, compilerType, (), (const, override)); From 7492c636fedf382d9e0e6dd4e6a6a15c3048640a Mon Sep 17 00:00:00 2001 From: Harrm Date: Mon, 26 Aug 2024 17:53:42 +0300 Subject: [PATCH 2/2] std::format -> fmt::format --- core/runtime/wasm_edge/module_factory_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/runtime/wasm_edge/module_factory_impl.cpp b/core/runtime/wasm_edge/module_factory_impl.cpp index 256cbe56bc..a0140dce16 100644 --- a/core/runtime/wasm_edge/module_factory_impl.cpp +++ b/core/runtime/wasm_edge/module_factory_impl.cpp @@ -379,7 +379,7 @@ namespace kagome::runtime::wasm_edge { } // version changes rarely, don't need the whole hash static std::string versioned_str = - std::format("wasmedge_{}", std::string_view{WASMEDGE_ID}.substr(0, 12)); + fmt::format("wasmedge_{}", std::string_view{WASMEDGE_ID}.substr(0, 12)); return versioned_str; }