Kagome
Polkadot Runtime Engine in C++17
memory_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_RUNTIME_BINARYEN_WASM_MEMORY_IMPL_HPP
7 #define KAGOME_RUNTIME_BINARYEN_WASM_MEMORY_IMPL_HPP
8 
9 #include <binaryen/shell-interface.h>
10 
11 #include <array>
12 #include <cstring> // for std::memset in gcc
13 #include <memory>
14 #include <optional>
15 #include <unordered_map>
16 
17 #include <binaryen/wasm.h>
18 
19 #include "common/literals.hpp"
20 #include "log/logger.hpp"
21 #include "primitives/math.hpp"
22 #include "runtime/memory.hpp"
23 
24 namespace kagome::runtime {
25  class MemoryAllocator;
26 }
27 
28 namespace kagome::runtime::binaryen {
29 
37  class MemoryImpl final : public Memory {
38  public:
39  MemoryImpl(wasm::ShellExternalInterface::Memory *memory,
40  std::unique_ptr<MemoryAllocator> &&allocator);
41  MemoryImpl(wasm::ShellExternalInterface::Memory *memory,
42  WasmSize heap_base);
43  MemoryImpl(const MemoryImpl &copy) = delete;
44  MemoryImpl &operator=(const MemoryImpl &copy) = delete;
45  MemoryImpl(MemoryImpl &&move) = delete;
46  MemoryImpl &operator=(MemoryImpl &&move) = delete;
47  ~MemoryImpl() override = default;
48 
50  std::optional<WasmSize> deallocate(WasmPointer ptr) override;
51 
52  int8_t load8s(WasmPointer addr) const override;
53  uint8_t load8u(WasmPointer addr) const override;
54  int16_t load16s(WasmPointer addr) const override;
55  uint16_t load16u(WasmPointer addr) const override;
56  int32_t load32s(WasmPointer addr) const override;
57  uint32_t load32u(WasmPointer addr) const override;
58  int64_t load64s(WasmPointer addr) const override;
59  uint64_t load64u(WasmPointer addr) const override;
60  std::array<uint8_t, 16> load128(WasmPointer addr) const override;
62  kagome::runtime::WasmSize n) const override;
63  std::string loadStr(kagome::runtime::WasmPointer addr,
64  kagome::runtime::WasmSize length) const override;
65 
66  void store8(WasmPointer addr, int8_t value) override;
67  void store16(WasmPointer addr, int16_t value) override;
68  void store32(WasmPointer addr, int32_t value) override;
69  void store64(WasmPointer addr, int64_t value) override;
70  void store128(WasmPointer addr,
71  const std::array<uint8_t, 16> &value) override;
73  gsl::span<const uint8_t> value) override;
74 
75  WasmSpan storeBuffer(gsl::span<const uint8_t> value) override;
76 
77  void resize(WasmSize new_size) override {
82  if (new_size >= size_) {
83  size_ = new_size;
84  memory_->resize(new_size);
85  }
86  }
87 
88  WasmSize size() const override {
89  return size_;
90  }
91 
92  private:
93  wasm::ShellExternalInterface::Memory *memory_;
95  std::unique_ptr<MemoryAllocator> allocator_;
96 
98  };
99 } // namespace kagome::runtime::binaryen
100 
101 #endif // KAGOME_RUNTIME_BINARYEN_WASM_MEMORY_IMPL_HPP
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
uint32_t load32u(WasmPointer addr) const override
Definition: memory_impl.cpp:60
MemoryImpl & operator=(const MemoryImpl &copy)=delete
uint32_t WasmSize
Size type is uint32_t because we are working in 32 bit address space.
Definition: types.hpp:35
void store64(WasmPointer addr, int64_t value) override
uint8_t load8u(WasmPointer addr) const override
Definition: memory_impl.cpp:44
void storeBuffer(kagome::runtime::WasmPointer addr, gsl::span< const uint8_t > value) override
int8_t load8s(WasmPointer addr) const override
Definition: memory_impl.cpp:40
std::string loadStr(kagome::runtime::WasmPointer addr, kagome::runtime::WasmSize length) const override
Definition: memory_impl.cpp:88
void store128(WasmPointer addr, const std::array< uint8_t, 16 > &value) override
uint64_t load64u(WasmPointer addr) const override
Definition: memory_impl.cpp:68
void store8(WasmPointer addr, int8_t value) override
Definition: memory_impl.cpp:99
int64_t load64s(WasmPointer addr) const override
Definition: memory_impl.cpp:64
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
wasm::ShellExternalInterface::Memory * memory_
Definition: memory_impl.hpp:93
void resize(WasmSize new_size) override
Definition: memory_impl.hpp:77
uint64_t WasmSpan
combination of pointer and size, where less significant part represents wasm pointer, and most significant represents size
Definition: types.hpp:31
std::optional< WasmSize > deallocate(WasmPointer ptr) override
Definition: memory_impl.cpp:36
std::unique_ptr< MemoryAllocator > allocator_
Definition: memory_impl.hpp:95
uint32_t WasmPointer
type of wasm memory is 32 bit integer
Definition: types.hpp:26
WasmPointer allocate(WasmSize size) override
Definition: memory_impl.cpp:32
int16_t load16s(WasmPointer addr) const override
Definition: memory_impl.cpp:48
uint16_t load16u(WasmPointer addr) const override
Definition: memory_impl.cpp:52
MemoryImpl(wasm::ShellExternalInterface::Memory *memory, std::unique_ptr< MemoryAllocator > &&allocator)
Definition: memory_impl.cpp:13
common::Buffer loadN(kagome::runtime::WasmPointer addr, kagome::runtime::WasmSize n) const override
Definition: memory_impl.cpp:77
void store32(WasmPointer addr, int32_t value) override
void store16(WasmPointer addr, int16_t value) override
std::array< uint8_t, 16 > load128(WasmPointer addr) const override
Definition: memory_impl.cpp:72
WasmSize size() const override
Return the size of the memory.
Definition: memory_impl.hpp:88
int32_t load32s(WasmPointer addr) const override
Definition: memory_impl.cpp:56