Skip to content

Commit

Permalink
Fix binaryen memory initalization (#1704)
Browse files Browse the repository at this point in the history
* Fix binaryen memory initalization: remove size_ field
  • Loading branch information
Harrm authored Jul 19, 2023
1 parent fbb9df2 commit 80deb0f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
9 changes: 4 additions & 5 deletions core/runtime/binaryen/memory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ namespace kagome::runtime::binaryen {
MemoryImpl::MemoryImpl(RuntimeExternalInterface::InternalMemory *memory,
std::unique_ptr<MemoryAllocator> &&allocator)
: memory_{memory},
size_{kInitialMemorySize},
allocator_{std::move(allocator)},
logger_{log::createLogger("Binaryen Memory", "binaryen")} {
resize(size_);
resize(kInitialMemorySize);
}

MemoryImpl::MemoryImpl(RuntimeExternalInterface::InternalMemory *memory,
Expand All @@ -26,7 +25,7 @@ namespace kagome::runtime::binaryen {
std::make_unique<MemoryAllocator>(
MemoryAllocator::MemoryHandle{
[this](auto new_size) { return resize(new_size); },
[this]() { return size_; },
[this]() { return size(); },
[this](auto addr, uint32_t value) {
memory_->set<uint32_t>(addr, value);
},
Expand Down Expand Up @@ -80,13 +79,13 @@ namespace kagome::runtime::binaryen {

common::BufferView MemoryImpl::loadN(kagome::runtime::WasmPointer addr,
kagome::runtime::WasmSize n) const {
BOOST_ASSERT(size_ > addr and size_ - addr >= n);
BOOST_ASSERT(size() > addr and size() - addr >= n);
return common::BufferView{memory_->getBuffer<const uint8_t>(addr, n)};
}

std::string MemoryImpl::loadStr(kagome::runtime::WasmPointer addr,
kagome::runtime::WasmSize length) const {
BOOST_ASSERT(size_ > addr and size_ - addr >= length);
BOOST_ASSERT(size() > addr and size() - addr >= length);
std::string res;
res.reserve(length);
for (auto i = addr; i < addr + length; i++) {
Expand Down
7 changes: 3 additions & 4 deletions core/runtime/binaryen/memory_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,21 @@ namespace kagome::runtime::binaryen {
* We use this condition to avoid
* deallocated_ pointers fixup
*/
if (new_size >= size_) {
if (new_size >= size()) {
if (auto mod = new_size % kMemoryPageSize) {
new_size += kMemoryPageSize - mod;
}
size_ = new_size;
memory_->resize(new_size);
}
}

WasmSize size() const override {
return size_;
BOOST_ASSERT(memory_ != nullptr);
return memory_->getSize();
}

private:
RuntimeExternalInterface::InternalMemory *memory_;
WasmSize size_;
std::unique_ptr<MemoryAllocator> allocator_;

log::Logger logger_;
Expand Down

0 comments on commit 80deb0f

Please sign in to comment.