Kagome
Polkadot Runtime Engine in C++17
memory_impl.cpp
Go to the documentation of this file.
1 
7 
9 #include "runtime/ptr_size.hpp"
10 
11 namespace kagome::runtime::binaryen {
12 
13  MemoryImpl::MemoryImpl(wasm::ShellExternalInterface::Memory *memory,
14  std::unique_ptr<MemoryAllocator> &&allocator)
15  : memory_{memory},
17  allocator_{std::move(allocator)},
18  logger_{log::createLogger("Binaryen Memory", "binaryen")} {
19  resize(size_);
20  }
21 
22  MemoryImpl::MemoryImpl(wasm::ShellExternalInterface::Memory *memory,
23  WasmSize heap_base)
24  : MemoryImpl{memory,
25  std::make_unique<MemoryAllocator>(
27  [this](auto new_size) { return resize(new_size); },
28  [this]() { return size_; }},
30  heap_base)} {}
31 
33  return allocator_->allocate(size);
34  }
35 
36  std::optional<WasmSize> MemoryImpl::deallocate(WasmPointer ptr) {
37  return allocator_->deallocate(ptr);
38  }
39 
40  int8_t MemoryImpl::load8s(WasmPointer addr) const {
41  BOOST_ASSERT(allocator_->checkAddress<int8_t>(addr));
42  return memory_->get<int8_t>(addr);
43  }
44  uint8_t MemoryImpl::load8u(WasmPointer addr) const {
45  BOOST_ASSERT(allocator_->checkAddress<uint8_t>(addr));
46  return memory_->get<uint8_t>(addr);
47  }
48  int16_t MemoryImpl::load16s(WasmPointer addr) const {
49  BOOST_ASSERT(allocator_->checkAddress<int16_t>(addr));
50  return memory_->get<int16_t>(addr);
51  }
52  uint16_t MemoryImpl::load16u(WasmPointer addr) const {
53  BOOST_ASSERT(allocator_->checkAddress<uint16_t>(addr));
54  return memory_->get<uint16_t>(addr);
55  }
56  int32_t MemoryImpl::load32s(WasmPointer addr) const {
57  BOOST_ASSERT(allocator_->checkAddress<int32_t>(addr));
58  return memory_->get<int32_t>(addr);
59  }
60  uint32_t MemoryImpl::load32u(WasmPointer addr) const {
61  BOOST_ASSERT(allocator_->checkAddress<uint32_t>(addr));
62  return memory_->get<uint32_t>(addr);
63  }
64  int64_t MemoryImpl::load64s(WasmPointer addr) const {
65  BOOST_ASSERT(allocator_->checkAddress<int64_t>(addr));
66  return memory_->get<int64_t>(addr);
67  }
68  uint64_t MemoryImpl::load64u(WasmPointer addr) const {
69  BOOST_ASSERT(allocator_->checkAddress<uint64_t>(addr));
70  return memory_->get<uint64_t>(addr);
71  }
72  std::array<uint8_t, 16> MemoryImpl::load128(WasmPointer addr) const {
73  BOOST_ASSERT((allocator_->checkAddress<std::array<uint8_t, 16>>(addr)));
74  return memory_->get<std::array<uint8_t, 16>>(addr);
75  }
76 
78  kagome::runtime::WasmSize n) const {
79  BOOST_ASSERT(size_ > addr and size_ - addr >= n);
80  common::Buffer res;
81  res.reserve(n);
82  for (auto i = addr; i < addr + n; i++) {
83  res.putUint8(memory_->get<uint8_t>(i));
84  }
85  return res;
86  }
87 
89  kagome::runtime::WasmSize length) const {
90  BOOST_ASSERT(size_ > addr and size_ - addr >= length);
91  std::string res;
92  res.reserve(length);
93  for (auto i = addr; i < addr + length; i++) {
94  res.push_back(static_cast<char>(memory_->get<uint8_t>(i)));
95  }
96  return res;
97  }
98 
99  void MemoryImpl::store8(WasmPointer addr, int8_t value) {
100  BOOST_ASSERT((allocator_->checkAddress<int8_t>(addr)));
101  memory_->set<int8_t>(addr, value);
102  }
103 
104  void MemoryImpl::store16(WasmPointer addr, int16_t value) {
105  BOOST_ASSERT((allocator_->checkAddress<int16_t>(addr)));
106  memory_->set<int16_t>(addr, value);
107  }
108 
109  void MemoryImpl::store32(WasmPointer addr, int32_t value) {
110  BOOST_ASSERT((allocator_->checkAddress<int32_t>(addr)));
111  memory_->set<int32_t>(addr, value);
112  }
113 
114  void MemoryImpl::store64(WasmPointer addr, int64_t value) {
115  BOOST_ASSERT((allocator_->checkAddress<int64_t>(addr)));
116  memory_->set<int64_t>(addr, value);
117  }
118 
120  const std::array<uint8_t, 16> &value) {
121  BOOST_ASSERT((allocator_->checkAddress<std::array<uint8_t, 16>>(addr)));
122  memory_->set<std::array<uint8_t, 16>>(addr, value);
123  }
124 
126  gsl::span<const uint8_t> value) {
127  const auto size = static_cast<size_t>(value.size());
128  BOOST_ASSERT((allocator_->checkAddress(addr, size)));
129  for (size_t i = addr, j = 0; i < addr + size; i++, j++) {
130  memory_->set(i, value[j]);
131  }
132  }
133 
134  WasmSpan MemoryImpl::storeBuffer(gsl::span<const uint8_t> value) {
135  const auto size = static_cast<size_t>(value.size());
136  BOOST_ASSERT(std::numeric_limits<WasmSize>::max() > size);
137  auto wasm_pointer = allocate(size);
138  if (wasm_pointer == 0) {
139  return 0;
140  }
141  storeBuffer(wasm_pointer, value);
142  return PtrSize(wasm_pointer, value.size()).combine();
143  }
144 
145 } // namespace kagome::runtime::binaryen
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
uint32_t load32u(WasmPointer addr) const override
Definition: memory_impl.cpp:60
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
constexpr size_t kInitialMemorySize
Definition: memory.hpp:20
wasm::ShellExternalInterface::Memory * memory_
Definition: memory_impl.hpp:93
void resize(WasmSize new_size) override
Definition: memory_impl.hpp:77
SLBuffer & reserve(size_t size)
Definition: buffer.hpp:61
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
SLBuffer & putUint8(uint8_t n)
Put a 8-bit {.
Definition: buffer.hpp:79
uint16_t load16u(WasmPointer addr) const override
Definition: memory_impl.cpp:52
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
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