mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 04:09:13 +00:00
LibWasm+LibWeb+test-wasm: Refcount Wasm::Module for function references
Prior to funcref, a partial chunk of an invalid module was never needed, but funcref allows a partially instantiated module to modify imported tables with references to its own functions, which means we need to keep the second module alive while that function reference is present within the imported table. This was tested by the spectests, but very rarely caught as our GC does not behave particularly predictably, making it so the offending module remains in memory just long enough to let the tests pass. This commit makes it so all function references keep their respective modules alive.
This commit is contained in:
parent
5606ce412e
commit
a60ecea16a
Notes:
github-actions[bot]
2024-08-22 07:37:30 +00:00
Author: https://github.com/alimpfard
Commit: a60ecea16a
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1159
9 changed files with 54 additions and 37 deletions
|
@ -14,14 +14,14 @@
|
|||
|
||||
namespace Wasm {
|
||||
|
||||
Optional<FunctionAddress> Store::allocate(ModuleInstance& module, CodeSection::Code const& code, TypeIndex type_index)
|
||||
Optional<FunctionAddress> Store::allocate(ModuleInstance& instance, Module const& module, CodeSection::Code const& code, TypeIndex type_index)
|
||||
{
|
||||
FunctionAddress address { m_functions.size() };
|
||||
if (type_index.value() > module.types().size())
|
||||
if (type_index.value() > instance.types().size())
|
||||
return {};
|
||||
|
||||
auto& type = module.types()[type_index.value()];
|
||||
m_functions.empend(WasmFunction { type, module, code });
|
||||
auto& type = instance.types()[type_index.value()];
|
||||
m_functions.empend(WasmFunction { type, instance, module, code });
|
||||
return address;
|
||||
}
|
||||
|
||||
|
@ -84,6 +84,14 @@ FunctionInstance* Store::get(FunctionAddress address)
|
|||
return &m_functions[value];
|
||||
}
|
||||
|
||||
Module const* Store::get_module_for(Wasm::FunctionAddress address)
|
||||
{
|
||||
auto* function = get(address);
|
||||
if (!function || function->has<HostFunction>())
|
||||
return nullptr;
|
||||
return function->get<WasmFunction>().module_ref().ptr();
|
||||
}
|
||||
|
||||
TableInstance* Store::get(TableAddress address)
|
||||
{
|
||||
auto value = address.value();
|
||||
|
@ -223,7 +231,7 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<Ex
|
|||
size_t i = 0;
|
||||
for (auto& code : module.code_section().functions()) {
|
||||
auto type_index = module.function_section().types()[i];
|
||||
auto address = m_store.allocate(main_module_instance, code, type_index);
|
||||
auto address = m_store.allocate(main_module_instance, module, code, type_index);
|
||||
VERIFY(address.has_value());
|
||||
auxiliary_instance.functions().append(*address);
|
||||
module_functions.append(*address);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue