Skip to content

Commit

Permalink
Add explicit check for imported memory limits validity
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Mar 15, 2021
1 parent 06dffc2 commit 7b0609a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/fizzy/instantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void match_imported_memories(const std::vector<Memory>& module_imported_memories
if (imported_memories.empty())
throw instantiate_error{"module defines an imported memory but none was provided"};

const auto min = imported_memories[0].limits.min;
const auto& max = imported_memories[0].limits.max;
if (max.has_value() && min > *max)
throw instantiate_error{"provided imported memory min limit is above max limit"};

match_limits(imported_memories[0].limits, module_imported_memories[0].limits);

if (imported_memories[0].data == nullptr)
Expand All @@ -116,8 +121,6 @@ void match_imported_memories(const std::vector<Memory>& module_imported_memories
if (size % PageSize != 0)
throw instantiate_error{"provided imported memory size must be multiple of page size"};

const auto min = imported_memories[0].limits.min;
const auto& max = imported_memories[0].limits.max;
if (size < min * PageSize || (max.has_value() && size > *max * PageSize))
throw instantiate_error{"provided imported memory doesn't fit provided limits"};
}
Expand Down
4 changes: 4 additions & 0 deletions test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ TEST(instantiate, imported_memory_invalid)
EXPECT_THROW_MESSAGE(instantiate(*module, {}, {}, {{&memory, {1, std::nullopt}}}),
instantiate_error, "provided import's max is above import's max defined in module");

// Provided limits have max less than min
EXPECT_THROW_MESSAGE(instantiate(*module, {}, {}, {{&memory, {1, 0}}}), instantiate_error,
"provided imported memory min limit is above max limit");

// Null pointer
EXPECT_THROW_MESSAGE(instantiate(*module, {}, {}, {{nullptr, {1, 3}}}), instantiate_error,
"provided imported memory has a null pointer to data");
Expand Down

0 comments on commit 7b0609a

Please sign in to comment.