Kagome
Polkadot Runtime Engine in C++17
memory_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_RUNTIME_WAVM_IMPL_MEMORY_HPP
7 #define KAGOME_CORE_RUNTIME_WAVM_IMPL_MEMORY_HPP
8 
9 #include "runtime/memory.hpp"
10 
11 #include <gsl/span>
12 #include <optional>
13 
14 #include "common/buffer.hpp"
15 #include "common/literals.hpp"
16 #include "log/logger.hpp"
17 #include "primitives/math.hpp"
18 #include "runtime/types.hpp"
20 
21 namespace kagome::runtime {
22  class MemoryAllocator;
23 }
24 
25 namespace kagome::runtime::wavm {
26 
27  class MemoryImpl final : public kagome::runtime::Memory {
28  public:
29  MemoryImpl(WAVM::Runtime::Memory *memory,
30  std::unique_ptr<MemoryAllocator> &&allocator);
31  MemoryImpl(WAVM::Runtime::Memory *memory, WasmSize heap_base);
32  MemoryImpl(const MemoryImpl &copy) = delete;
33  MemoryImpl &operator=(const MemoryImpl &copy) = delete;
34  MemoryImpl(MemoryImpl &&move) = delete;
35  MemoryImpl &operator=(MemoryImpl &&move) = delete;
36 
38  std::optional<WasmSize> deallocate(WasmPointer ptr) override;
39 
40  template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
41  T load(WasmPointer addr) const {
42  auto res = WAVM::Runtime::memoryRef<T>(memory_, addr);
43  SL_TRACE_FUNC_CALL(logger_, res, this, addr);
44  return res;
45  }
46 
47  template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
48  T *loadArray(WasmPointer addr, size_t num) const {
49  auto res = WAVM::Runtime::memoryArrayPtr<T>(memory_, addr, num);
50  SL_TRACE_FUNC_CALL(logger_, gsl::span<T>(res, num), this, addr);
51  return res;
52  }
53 
54  int8_t load8s(WasmPointer addr) const override;
55  uint8_t load8u(WasmPointer addr) const override;
56  int16_t load16s(WasmPointer addr) const override;
57  uint16_t load16u(WasmPointer addr) const override;
58  int32_t load32s(WasmPointer addr) const override;
59  uint32_t load32u(WasmPointer addr) const override;
60  int64_t load64s(WasmPointer addr) const override;
61  uint64_t load64u(WasmPointer addr) const override;
62  std::array<uint8_t, 16> load128(WasmPointer addr) const override;
63 
64  common::Buffer loadN(WasmPointer addr, WasmSize n) const override;
65 
66  std::string loadStr(WasmPointer addr, WasmSize n) const override;
67 
68  template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
69  void store(WasmPointer addr, T value) {
70  SL_TRACE_VOID_FUNC_CALL(logger_, this, addr, value);
71  std::memcpy(
72  WAVM::Runtime::memoryArrayPtr<uint8_t>(memory_, addr, sizeof(value)),
73  &value,
74  sizeof(value));
75  }
76 
77  template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
78  void storeArray(WasmPointer addr, gsl::span<T> array) {
79  SL_TRACE_VOID_FUNC_CALL(logger_, this, addr, array);
80  std::memcpy(WAVM::Runtime::memoryArrayPtr<uint8_t>(
81  memory_, addr, sizeof(array.size_bytes())),
82  array.data(),
83  array.size_bytes());
84  }
85 
86  void store8(WasmPointer addr, int8_t value) override;
87  void store16(WasmPointer addr, int16_t value) override;
88  void store32(WasmPointer addr, int32_t value) override;
89  void store64(WasmPointer addr, int64_t value) override;
90  void store128(WasmPointer addr,
91  const std::array<uint8_t, 16> &value) override;
92  void storeBuffer(WasmPointer addr, gsl::span<const uint8_t> value) override;
93 
94  WasmSpan storeBuffer(gsl::span<const uint8_t> value) override;
95 
96  WasmSize size() const override {
97  return WAVM::Runtime::getMemoryNumPages(memory_) * kPageSize;
98  }
99 
100  void resize(WasmSize new_size) override {
104  if (new_size >= size()) {
105  auto new_page_number = (new_size / kPageSize) + 1;
106  WAVM::Runtime::growMemory(
107  memory_,
108  new_page_number - WAVM::Runtime::getMemoryNumPages(memory_));
109  }
110  }
111 
112  private:
113  void fill(PtrSize span, uint8_t value) {
114  auto native_ptr =
115  WAVM::Runtime::memoryArrayPtr<uint8_t>(memory_, span.ptr, span.size);
116  memset(native_ptr, value, span.size);
117  }
118 
119  constexpr static uint32_t kPageSize = 4096;
120  std::unique_ptr<MemoryAllocator> allocator_;
121  WAVM::Runtime::Memory *memory_;
123  };
124 
125 } // namespace kagome::runtime::wavm
126 
127 #endif // KAGOME_CORE_RUNTIME_WAVM_IMPL_MEMORY_HPP
void store16(WasmPointer addr, int16_t value) override
Definition: memory_impl.cpp:91
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
WasmSize size() const override
Return the size of the memory.
Definition: memory_impl.hpp:96
uint8_t load8u(WasmPointer addr) const override
Definition: memory_impl.cpp:42
static constexpr uint32_t kPageSize
int32_t load32s(WasmPointer addr) const override
Definition: memory_impl.cpp:51
uint32_t WasmSize
Size type is uint32_t because we are working in 32 bit address space.
Definition: types.hpp:35
WAVM::Runtime::Memory * memory_
WasmPointer ptr
address of buffer
Definition: ptr_size.hpp:40
void storeBuffer(WasmPointer addr, gsl::span< const uint8_t > value) override
int8_t load8s(WasmPointer addr) const override
Definition: memory_impl.cpp:39
uint16_t load16u(WasmPointer addr) const override
Definition: memory_impl.cpp:48
#define SL_TRACE_FUNC_CALL(logger, ret,...)
Definition: logger.hpp:142
void store64(WasmPointer addr, int64_t value) override
Definition: memory_impl.cpp:97
WasmPointer allocate(WasmSize size) override
Definition: memory_impl.cpp:31
void store(WasmPointer addr, T value)
Definition: memory_impl.hpp:69
std::array< uint8_t, 16 > load128(WasmPointer addr) const override
Definition: memory_impl.cpp:63
void fill(PtrSize span, uint8_t value)
void store32(WasmPointer addr, int32_t value) override
Definition: memory_impl.cpp:94
common::Buffer loadN(WasmPointer addr, WasmSize n) const override
Definition: memory_impl.cpp:70
uint32_t load32u(WasmPointer addr) const override
Definition: memory_impl.cpp:54
T load(WasmPointer addr) const
Definition: memory_impl.hpp:41
int64_t load64s(WasmPointer addr) const override
Definition: memory_impl.cpp:57
void resize(WasmSize new_size) override
void store128(WasmPointer addr, const std::array< uint8_t, 16 > &value) override
MemoryImpl(WAVM::Runtime::Memory *memory, std::unique_ptr< MemoryAllocator > &&allocator)
Definition: memory_impl.cpp:12
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
T * loadArray(WasmPointer addr, size_t num) const
Definition: memory_impl.hpp:48
uint64_t WasmSpan
combination of pointer and size, where less significant part represents wasm pointer, and most significant represents size
Definition: types.hpp:31
void storeArray(WasmPointer addr, gsl::span< T > array)
Definition: memory_impl.hpp:78
uint32_t WasmPointer
type of wasm memory is 32 bit integer
Definition: types.hpp:26
MemoryImpl & operator=(const MemoryImpl &copy)=delete
int16_t load16s(WasmPointer addr) const override
Definition: memory_impl.cpp:45
#define SL_TRACE_VOID_FUNC_CALL(logger,...)
Definition: logger.hpp:146
std::unique_ptr< MemoryAllocator > allocator_
std::optional< WasmSize > deallocate(WasmPointer ptr) override
Definition: memory_impl.cpp:35
WasmSize size
length of buffer
Definition: ptr_size.hpp:41
uint64_t load64u(WasmPointer addr) const override
Definition: memory_impl.cpp:60
std::string loadStr(WasmPointer addr, WasmSize n) const override
Definition: memory_impl.cpp:77
void store8(WasmPointer addr, int8_t value) override
Definition: memory_impl.cpp:88