Kagome
Polkadot Runtime Engine in C++17
module_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <memory>
9 
10 #include <binaryen/wasm-binary.h>
11 #include <binaryen/wasm-interpreter.h>
12 
13 #include "common/mp_utils.hpp"
19 
20 using namespace kagome::common::literals;
21 
24  switch (e) {
25  case Error::EMPTY_STATE_CODE:
26  return "Provided state code is empty, calling a function is impossible";
27  case Error::INVALID_STATE_CODE:
28  return "Invalid state code, calling a function is impossible";
29  }
30  return "Unknown error";
31 }
32 
33 namespace kagome::runtime::binaryen {
34 
35  ModuleImpl::ModuleImpl(
36  std::unique_ptr<wasm::Module> &&module,
37  std::shared_ptr<const InstanceEnvironmentFactory> env_factory,
38  const common::Hash256 &code_hash)
39  : env_factory_{std::move(env_factory)},
40  module_{std::move(module)},
41  code_hash_(code_hash) {
42  BOOST_ASSERT(module_ != nullptr);
43  BOOST_ASSERT(env_factory_ != nullptr);
44  }
45 
46  outcome::result<std::unique_ptr<ModuleImpl>> ModuleImpl::createFromCode(
47  const std::vector<uint8_t> &code,
48  std::shared_ptr<const InstanceEnvironmentFactory> env_factory,
49  const common::Hash256 &code_hash) {
50  auto log = log::createLogger("wasm_module", "binaryen");
51  // that nolint suppresses false positive in a library function
52  // NOLINTNEXTLINE(clang-analyzer-core.NonNullParamChecker)
53  if (code.empty()) {
55  }
56 
57  auto module = std::make_unique<wasm::Module>();
58  {
59  wasm::WasmBinaryBuilder parser(
60  *module,
61  reinterpret_cast<std::vector<char> const &>(code), // NOLINT
62  false);
63 
64  try {
65  parser.read();
66  } catch (wasm::ParseException &e) {
67  std::ostringstream msg;
68  e.dump(msg);
69  log->error(msg.str());
71  }
72  }
73 
74  module->memory.initial = kDefaultHeappages;
75 
76  std::unique_ptr<ModuleImpl> wasm_module_impl(
77  new ModuleImpl(std::move(module), std::move(env_factory), code_hash));
78  return wasm_module_impl;
79  }
80 
81  outcome::result<std::shared_ptr<ModuleInstance>> ModuleImpl::instantiate()
82  const {
83  auto env = env_factory_->make();
84  return std::make_shared<ModuleInstanceImpl>(
85  std::move(env.env), module_, env.rei, code_hash_);
86  }
87 
88 } // namespace kagome::runtime::binaryen
std::shared_ptr< wasm::Module > module_
Definition: module_impl.hpp:60
ModuleImpl(ModuleImpl &&)=default
OUTCOME_CPP_DEFINE_CATEGORY(kagome::runtime::binaryen, ModuleImpl::Error, e)
Definition: module_impl.cpp:22
outcome::result< std::shared_ptr< ModuleInstance > > instantiate() const override
Definition: module_impl.cpp:81
std::shared_ptr< const InstanceEnvironmentFactory > env_factory_
Definition: module_impl.hpp:59
static constexpr auto kDefaultHeappages
Definition: module_impl.hpp:35
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
static outcome::result< std::unique_ptr< ModuleImpl > > createFromCode(const std::vector< uint8_t > &code, std::shared_ptr< const InstanceEnvironmentFactory > env_factory_, const common::Hash256 &code_hash)
Definition: module_impl.cpp:46