LibWasm: Implement memory.grow, memory.size and drop

These allow a very basic memory-using program to work.
This commit is contained in:
Ali Mohammad Pur 2021-05-03 22:45:47 +04:30 committed by Andreas Kling
parent 3402381d7a
commit 95b9821f26
Notes: sideshowbarker 2024-07-18 17:54:19 +09:00
3 changed files with 39 additions and 4 deletions

View file

@ -297,7 +297,20 @@ public:
auto& data() const { return m_data; }
auto& data() { return m_data; }
void grow(size_t new_size) { m_data.grow(new_size); }
bool grow(size_t size_to_grow)
{
if (size_to_grow == 0)
return true;
auto new_size = m_data.size() + size_to_grow;
if (m_type.limits().max().value_or(new_size) < new_size)
return false;
auto previous_size = m_size;
m_data.grow(new_size);
m_size = new_size;
// The spec requires that we zero out everything on grow
__builtin_memset(m_data.offset_pointer(previous_size), 0, size_to_grow);
return true;
}
private:
const MemoryType& m_type;

View file

@ -462,15 +462,35 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
global->set_value(move(value));
return;
}
case Instructions::memory_size.value():
case Instructions::memory_grow.value():
case Instructions::memory_size.value(): {
auto address = configuration.frame()->module().memories()[0];
auto instance = configuration.store().get(address);
auto pages = instance->size() / Constants::page_size;
dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
configuration.stack().push(make<Value>((i32)pages));
return;
}
case Instructions::memory_grow.value(): {
auto address = configuration.frame()->module().memories()[0];
auto instance = configuration.store().get(address);
i32 old_pages = instance->size() / Constants::page_size;
auto new_pages = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>();
VERIFY(new_pages.has_value());
if (instance->grow(new_pages.value() * Constants::page_size))
configuration.stack().push(make<Value>((i32)old_pages));
else
configuration.stack().push(make<Value>((i32)-1));
return;
}
case Instructions::table_get.value():
case Instructions::table_set.value():
case Instructions::ref_null.value():
case Instructions::ref_func.value():
case Instructions::ref_is_null.value():
case Instructions::drop.value():
goto unimplemented;
case Instructions::drop.value():
configuration.stack().pop();
return;
case Instructions::select.value():
case Instructions::select_typed.value(): {
// Note: The type seems to only be used for validation.

View file

@ -34,4 +34,6 @@ static constexpr auto extern_table_tag = 0x01;
static constexpr auto extern_memory_tag = 0x02;
static constexpr auto extern_global_tag = 0x03;
static constexpr auto page_size = 64 * KiB;
}