Skip to content

Fix potential out of bounds scope allocation #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/Ark/VM/ScopeView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ namespace Ark::internal
*
* @param id The symbol id of the variable
* @param val The value linked to the symbol
* @return bool true if the allocation succeeded, false otherwise
*/
void push_back(uint16_t id, Value&& val) noexcept;
bool push_back(uint16_t id, Value&& val) noexcept;

/**
* @brief Put a value in the scope
*
* @param id The symbol id of the variable
* @param val The value linked to the symbol
* @return bool true if the allocation succeeded, false otherwise
*/
void push_back(uint16_t id, const Value& val) noexcept;
bool push_back(uint16_t id, const Value& val) noexcept;

/**
* @brief Check if the scope maybe holds a specific symbol in memory
Expand Down
5 changes: 4 additions & 1 deletion include/Ark/VM/VM.inl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ inline void VM::store(const uint16_t id, const Value* val, internal::ExecutionCo
// avoid adding the pair (id, _) multiple times, with different values
Value* local = context.locals.back()[id];
if (local == nullptr) [[likely]]
context.locals.back().push_back(id, *val);
{
if (!context.locals.back().push_back(id, *val))
throw Error(fmt::format("Can not create variable: no more heap space (limit: {}). If you are using a recursive algorithm, try making use of tail call optimization to reduce the number of allocated scopes.", ScopeStackSize));
}
else
*local = *val;
}
Expand Down
12 changes: 10 additions & 2 deletions src/arkreactor/VM/ScopeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@ namespace Ark::internal
m_storage(storage), m_start(start), m_size(0), m_min_id(std::numeric_limits<uint16_t>::max()), m_max_id(0)
{}

void ScopeView::push_back(uint16_t id, Value&& val) noexcept
bool ScopeView::push_back(uint16_t id, Value&& val) noexcept
{
if (m_start + m_size >= ScopeStackSize) [[unlikely]]
return false;

if (id < m_min_id)
m_min_id = id;
if (id > m_max_id)
m_max_id = id;

m_storage[m_start + m_size] = std::make_pair(id, std::move(val));
++m_size;
return true;
}

void ScopeView::push_back(uint16_t id, const Value& val) noexcept
bool ScopeView::push_back(uint16_t id, const Value& val) noexcept
{
if (m_start + m_size >= ScopeStackSize) [[unlikely]]
return false;

if (id < m_min_id)
m_min_id = id;
if (id > m_max_id)
m_max_id = id;

m_storage[m_start + m_size] = std::make_pair(id, val);
++m_size;
return true;
}

bool ScopeView::maybeHas(const uint16_t id) const noexcept
Expand Down
Loading