Skip to content

Commit

Permalink
Check configurable memory limit in memory.grow
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Sep 2, 2020
1 parent 84a2e27 commit 8b3c2b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
15 changes: 8 additions & 7 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,11 @@ std::unique_ptr<Instance> instantiate(Module module,

auto [memory, memory_limits] =
allocate_memory(module.memorysec, imported_memories, memory_pages_limit);
if (memory_limits.max.has_value())
{
assert(*memory_limits.max <= memory_pages_limit);
memory_pages_limit = *memory_limits.max;
}

// Before starting to fill memory and table,
// check that data and element segments are within bounds.
Expand Down Expand Up @@ -746,8 +751,8 @@ std::unique_ptr<Instance> instantiate(Module module,
// We need to create instance before filling table,
// because table functions will capture the pointer to instance.
auto instance = std::make_unique<Instance>(std::move(module), std::move(memory), memory_limits,
std::move(table), table_limits, std::move(globals), std::move(imported_functions),
std::move(imported_globals));
memory_pages_limit, std::move(table), table_limits, std::move(globals),
std::move(imported_functions), std::move(imported_globals));

// Fill the table based on elements segment
for (size_t i = 0; i < instance->module.elementsec.size(); ++i)
Expand Down Expand Up @@ -1230,11 +1235,7 @@ ExecutionResult execute(
uint32_t ret = static_cast<uint32_t>(cur_pages);
try
{
// TODO use memory_pages_limit passed to instantiate
const size_t memory_max_pages =
(instance.memory_limits.max.has_value() ? *instance.memory_limits.max :
DefaultMemoryPagesLimit);
if (new_pages > memory_max_pages)
if (new_pages > instance.memory_pages_limit)
throw std::bad_alloc();
memory->resize(new_pages * PageSize);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ struct Instance
// For these cases unique_ptr would either have a normal deleter or noop deleter respectively
bytes_ptr memory = {nullptr, [](bytes*) {}};
Limits memory_limits;
// Hard limit for memory growth in pages, checked when memory is defined as unbounded in module
unsigned memory_pages_limit = 0;
// Table is either allocated and owned by the instance or imported and owned externally.
// For these cases unique_ptr would either have a normal deleter or noop deleter respectively.
table_ptr table = {nullptr, [](table_elements*) {}};
Expand All @@ -82,13 +84,14 @@ struct Instance
std::vector<ExternalFunction> imported_functions;
std::vector<ExternalGlobal> imported_globals;

Instance(Module _module, bytes_ptr _memory, Limits _memory_limits, table_ptr _table,
Limits _table_limits, std::vector<Value> _globals,
Instance(Module _module, bytes_ptr _memory, Limits _memory_limits, unsigned _memory_pages_limit,
table_ptr _table, Limits _table_limits, std::vector<Value> _globals,
std::vector<ExternalFunction> _imported_functions,
std::vector<ExternalGlobal> _imported_globals)
: module(std::move(_module)),
memory(std::move(_memory)),
memory_limits(_memory_limits),
memory_pages_limit(_memory_pages_limit),
table(std::move(_table)),
table_limits(_table_limits),
globals(std::move(_globals)),
Expand Down

0 comments on commit 8b3c2b8

Please sign in to comment.