From 95b9821f26978d674d39a179b638207138823c16 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Mon, 3 May 2021 22:45:47 +0430 Subject: [PATCH] LibWasm: Implement memory.grow, memory.size and drop These allow a very basic memory-using program to work. --- .../LibWasm/AbstractMachine/AbstractMachine.h | 15 ++++++++++- .../LibWasm/AbstractMachine/Interpreter.cpp | 26 ++++++++++++++++--- Userland/Libraries/LibWasm/Constants.h | 2 ++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index dc52dc98daa..4f6169c2fa8 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -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; diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp b/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp index 1dbf4c9db3d..518f896d17b 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp @@ -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((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>()->to(); + VERIFY(new_pages.has_value()); + if (instance->grow(new_pages.value() * Constants::page_size)) + configuration.stack().push(make((i32)old_pages)); + else + configuration.stack().push(make((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. diff --git a/Userland/Libraries/LibWasm/Constants.h b/Userland/Libraries/LibWasm/Constants.h index 9d5f346fc6b..7ae73b782af 100644 --- a/Userland/Libraries/LibWasm/Constants.h +++ b/Userland/Libraries/LibWasm/Constants.h @@ -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; + }