Skip to content

Commit

Permalink
Fix WAVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrm committed Jul 3, 2024
1 parent e494530 commit ac264b3
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 200 deletions.
3 changes: 2 additions & 1 deletion core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ namespace {
injector.template create<sptr<storage::trie::TrieSerializer>>(),
injector.template create<sptr<runtime::wavm::IntrinsicModule>>(),
module_cache_opt,
injector.template create<sptr<crypto::Hasher>>());
injector.template create<sptr<crypto::Hasher>>(),
std::make_shared<runtime::WasmInstrumenter>());
}),
di::bind<runtime::wavm::IntrinsicResolver>.template to<runtime::wavm::IntrinsicResolverImpl>(),
#endif
Expand Down
3 changes: 2 additions & 1 deletion core/parachain/pvf/kagome_pvf_worker_injector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ namespace kagome::parachain {
injector.template create<sptr<storage::trie::TrieSerializer>>(),
injector.template create<sptr<runtime::wavm::IntrinsicModule>>(),
module_cache,
injector.template create<sptr<crypto::Hasher>>());
injector.template create<sptr<crypto::Hasher>>(),
std::make_shared<runtime::WasmInstrumenter>());
}),
bind_by_lambda<runtime::ModuleFactory>([](const auto &injector) {
return injector
Expand Down
10 changes: 7 additions & 3 deletions core/runtime/wavm/instance_environment_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ namespace kagome::runtime::wavm {
std::shared_ptr<storage::trie::TrieStorage> storage,
std::shared_ptr<storage::trie::TrieSerializer> serializer,
std::shared_ptr<host_api::HostApiFactory> host_api_factory,
std::shared_ptr<const ModuleFactory> module_factory)
std::shared_ptr<const ModuleFactory> module_factory,
std::shared_ptr<const WasmInstrumenter> instrumenter)
: storage_{std::move(storage)},
serializer_{std::move(serializer)},
host_api_factory_{std::move(host_api_factory)},
module_factory_{module_factory} {
module_factory_{module_factory},
instrumenter_{std::move(instrumenter)} {
BOOST_ASSERT(module_factory_ != nullptr);
BOOST_ASSERT(instrumenter_ != nullptr);
}

InstanceEnvironment InstanceEnvironmentFactory::make(
Expand All @@ -35,7 +38,8 @@ namespace kagome::runtime::wavm {
std::shared_ptr<IntrinsicModuleInstance> intrinsic_instance) const {
auto new_storage_provider =
std::make_shared<TrieStorageProviderImpl>(storage_, serializer_);
auto core_factory = std::make_shared<CoreApiFactoryImpl>(module_factory_);
auto core_factory =
std::make_shared<CoreApiFactoryImpl>(module_factory_, instrumenter_);

std::shared_ptr<MemoryProvider> memory_provider;
switch (memory_origin) {
Expand Down
6 changes: 5 additions & 1 deletion core/runtime/wavm/instance_environment_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include "runtime/instance_environment.hpp"
#include "runtime/wabt/instrument.hpp"

namespace kagome::host_api {
class HostApiFactory;
Expand All @@ -27,6 +28,7 @@ namespace WAVM::Runtime {

namespace kagome::runtime {
class ModuleFactory;
class WasmInstrumenter;
} // namespace kagome::runtime

namespace kagome::runtime::wavm {
Expand All @@ -39,7 +41,8 @@ namespace kagome::runtime::wavm {
std::shared_ptr<storage::trie::TrieStorage> storage,
std::shared_ptr<storage::trie::TrieSerializer> serializer,
std::shared_ptr<host_api::HostApiFactory> host_api_factory,
std::shared_ptr<const ModuleFactory> module_factory);
std::shared_ptr<const ModuleFactory> module_factory,
std::shared_ptr<const WasmInstrumenter> instrumenter);

enum class MemoryOrigin { EXTERNAL, INTERNAL };
[[nodiscard]] InstanceEnvironment make(
Expand All @@ -52,6 +55,7 @@ namespace kagome::runtime::wavm {
std::shared_ptr<storage::trie::TrieSerializer> serializer_;
std::shared_ptr<host_api::HostApiFactory> host_api_factory_;
std::shared_ptr<const ModuleFactory> module_factory_;
std::shared_ptr<const WasmInstrumenter> instrumenter_;
};

} // namespace kagome::runtime::wavm
15 changes: 11 additions & 4 deletions core/runtime/wavm/module_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ namespace kagome::runtime::wavm {
std::shared_ptr<storage::trie::TrieSerializer> serializer,
std::shared_ptr<IntrinsicModule> intrinsic_module,
std::optional<std::shared_ptr<ModuleCache>> module_cache,
std::shared_ptr<crypto::Hasher> hasher)
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<const WasmInstrumenter> instrumenter)
: compartment_{std::move(compartment)},
module_params_{std::move(module_params)},
host_api_factory_{host_api_factory},
storage_{storage},
serializer_{serializer},
intrinsic_module_{std::move(intrinsic_module)},
hasher_(std::move(hasher)) {
hasher_(std::move(hasher)),
instrumenter_{std::move(instrumenter)} {
BOOST_ASSERT(compartment_ != nullptr);
BOOST_ASSERT(module_params_ != nullptr);
BOOST_ASSERT(host_api_factory_ != nullptr);
BOOST_ASSERT(intrinsic_module_ != nullptr);
BOOST_ASSERT(hasher_ != nullptr);
BOOST_ASSERT(instrumenter_ != nullptr);

if (module_cache.has_value()) {
WAVM::Runtime::setGlobalObjectCache(std::move(module_cache.value()));
Expand All @@ -43,8 +46,12 @@ namespace kagome::runtime::wavm {

CompilationOutcome<std::shared_ptr<Module>> ModuleFactoryImpl::make(
common::BufferView code) const {
auto env_factory = std::make_shared<InstanceEnvironmentFactory>(
storage_, serializer_, host_api_factory_, shared_from_this());
auto env_factory =
std::make_shared<InstanceEnvironmentFactory>(storage_,
serializer_,
host_api_factory_,
shared_from_this(),
instrumenter_);
OUTCOME_TRY(module,
ModuleImpl::compileFrom(compartment_,
*module_params_,
Expand Down
9 changes: 8 additions & 1 deletion core/runtime/wavm/module_factory_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "runtime/module_factory.hpp"

#include "outcome/outcome.hpp"
#include "runtime/wabt/instrument.hpp"

namespace kagome::application {
class AppConfiguration;
Expand All @@ -27,6 +28,10 @@ namespace kagome::storage::trie {
class TrieSerializer;
} // namespace kagome::storage::trie

namespace kagome::runtime {
class WasmInstrumenter;
}

namespace kagome::runtime::wavm {

class CompartmentWrapper;
Expand All @@ -47,7 +52,8 @@ namespace kagome::runtime::wavm {
std::shared_ptr<storage::trie::TrieSerializer> serializer,
std::shared_ptr<IntrinsicModule> intrinsic_module,
std::optional<std::shared_ptr<ModuleCache>> module_cache,
std::shared_ptr<crypto::Hasher> hasher);
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<const WasmInstrumenter> instrumenter);

CompilationOutcome<std::shared_ptr<Module>> make(
common::BufferView code) const override;
Expand All @@ -60,6 +66,7 @@ namespace kagome::runtime::wavm {
std::shared_ptr<storage::trie::TrieSerializer> serializer_;
std::shared_ptr<IntrinsicModule> intrinsic_module_;
std::shared_ptr<crypto::Hasher> hasher_;
std::shared_ptr<const WasmInstrumenter> instrumenter_;
};

} // namespace kagome::runtime::wavm
17 changes: 0 additions & 17 deletions test/core/runtime/wavm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,3 @@ target_link_libraries(core_integration_test
filesystem
logger_for_tests
)

addtest(wavm_module_init_test
wavm_module_init_test.cpp
)
target_link_libraries(wavm_module_init_test
runtime_wavm
basic_code_provider
blockchain
hasher
core_api
host_api_factory
offchain_persistent_storage
offchain_worker_pool
runtime_properties_cache
logger_for_tests
module_repository
)
2 changes: 1 addition & 1 deletion test/core/runtime/wavm/wasm_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class WavmMemoryHeapTest : public ::testing::Test {
memory_ = std::make_unique<Memory>(handle, std::move(allocator));
}

static const uint32_t memory_size_ = 20 * runtime::kMemoryPageSize;
static const uint32_t memory_size_ = 20 * kagome::runtime::kMemoryPageSize;
static const uint32_t memory_page_limit_ = 512_MB;

std::unique_ptr<Memory> memory_;
Expand Down
169 changes: 0 additions & 169 deletions test/core/runtime/wavm/wavm_module_init_test.cpp

This file was deleted.

11 changes: 9 additions & 2 deletions test/core/runtime/wavm/wavm_runtime_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

#pragma once

#include <memory>
#include "core/runtime/runtime_test_base.hpp"

#include "crypto/hasher/hasher_impl.hpp"
#include "mock/core/application/app_configuration_mock.hpp"
#include "mock/core/runtime/instrument_wasm.hpp"
#include "mock/core/storage/trie/trie_storage_mock.hpp"
#include "runtime/common/core_api_factory_impl.hpp"
#include "runtime/module.hpp"
Expand Down Expand Up @@ -50,11 +52,16 @@ class WavmRuntimeTest : public RuntimeTestBase {
serializer_,
intrinsic_module,
std::nullopt,
hasher_);
hasher_,
std::make_shared<runtime::NoopWasmInstrumenter>());

auto instance_env_factory =
std::make_shared<kagome::runtime::wavm::InstanceEnvironmentFactory>(
trie_storage_, serializer_, host_api_factory_, module_factory);
trie_storage_,
serializer_,
host_api_factory_,
module_factory,
std::make_shared<runtime::NoopWasmInstrumenter>());

return module_factory;
}
Expand Down

0 comments on commit ac264b3

Please sign in to comment.