Kagome
Polkadot Runtime Engine in C++17
memory_impl.cpp
Go to the documentation of this file.
1 
7 
9 
10 namespace kagome::runtime::wavm {
11 
12  MemoryImpl::MemoryImpl(WAVM::Runtime::Memory *memory,
13  std::unique_ptr<MemoryAllocator> &&allocator)
14  : allocator_{std::move(allocator)},
15  memory_{memory},
16  logger_{log::createLogger("WAVM Memory", "wavm")} {
17  BOOST_ASSERT(memory_);
18  BOOST_ASSERT(allocator_);
20  }
21 
22  MemoryImpl::MemoryImpl(WAVM::Runtime::Memory *memory, WasmSize heap_base)
23  : MemoryImpl{memory,
24  std::make_unique<MemoryAllocator>(
26  [this](auto size) { return resize(size); },
27  [this]() { return size(); }},
29  heap_base)} {}
30 
32  return allocator_->allocate(size);
33  }
34 
35  std::optional<WasmSize> MemoryImpl::deallocate(WasmPointer ptr) {
36  return allocator_->deallocate(ptr);
37  }
38 
39  int8_t MemoryImpl::load8s(WasmPointer addr) const {
40  return load<int8_t>(addr);
41  }
42  uint8_t MemoryImpl::load8u(WasmPointer addr) const {
43  return load<uint8_t>(addr);
44  }
45  int16_t MemoryImpl::load16s(WasmPointer addr) const {
46  return load<int16_t>(addr);
47  }
48  uint16_t MemoryImpl::load16u(WasmPointer addr) const {
49  return load<uint16_t>(addr);
50  }
51  int32_t MemoryImpl::load32s(WasmPointer addr) const {
52  return load<int32_t>(addr);
53  }
54  uint32_t MemoryImpl::load32u(WasmPointer addr) const {
55  return load<uint32_t>(addr);
56  }
57  int64_t MemoryImpl::load64s(WasmPointer addr) const {
58  return load<int64_t>(addr);
59  }
60  uint64_t MemoryImpl::load64u(WasmPointer addr) const {
61  return load<uint64_t>(addr);
62  }
63  std::array<uint8_t, 16> MemoryImpl::load128(WasmPointer addr) const {
64  auto byte_array = loadArray<uint8_t>(addr, 16);
65  std::array<uint8_t, 16> array;
66  std::copy_n(byte_array, 16, array.begin());
67  return array;
68  }
69 
71  kagome::runtime::WasmSize n) const {
72  common::Buffer res;
73  auto byte_array = loadArray<uint8_t>(addr, n);
74  return common::Buffer{byte_array, byte_array + n};
75  }
76 
78  kagome::runtime::WasmSize n) const {
79  std::string res;
80  res.reserve(n);
81  for (auto i = addr; i < addr + n; i++) {
82  res.push_back(load<uint8_t>(i));
83  }
84  SL_TRACE_FUNC_CALL(logger_, res, this, addr, n);
85  return res;
86  }
87 
88  void MemoryImpl::store8(WasmPointer addr, int8_t value) {
89  store<int8_t>(addr, value);
90  }
91  void MemoryImpl::store16(WasmPointer addr, int16_t value) {
92  store<int16_t>(addr, value);
93  }
94  void MemoryImpl::store32(WasmPointer addr, int32_t value) {
95  store<int32_t>(addr, value);
96  }
97  void MemoryImpl::store64(WasmPointer addr, int64_t value) {
98  store<int64_t>(addr, value);
99  }
101  const std::array<uint8_t, 16> &value) {
102  storeBuffer(addr, value);
103  }
105  gsl::span<const uint8_t> value) {
106  storeArray(addr, value);
107  }
108 
109  WasmSpan MemoryImpl::storeBuffer(gsl::span<const uint8_t> value) {
110  auto wasm_pointer = allocate(value.size());
111  if (wasm_pointer == 0) {
112  return 0;
113  }
114  storeBuffer(wasm_pointer, value);
115  auto res = PtrSize(wasm_pointer, value.size()).combine();
116  return res;
117  }
118 
119 } // namespace kagome::runtime::wavm
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
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_
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
std::array< uint8_t, 16 > load128(WasmPointer addr) const override
Definition: memory_impl.cpp:63
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
int64_t load64s(WasmPointer addr) const override
Definition: memory_impl.cpp:57
void resize(WasmSize new_size) override
constexpr size_t kInitialMemorySize
Definition: memory.hpp:20
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
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
int16_t load16s(WasmPointer addr) const override
Definition: memory_impl.cpp:45
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::unique_ptr< MemoryAllocator > allocator_
std::optional< WasmSize > deallocate(WasmPointer ptr) override
Definition: memory_impl.cpp:35
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