From 6820e0e175741aa5fff91a88aa50501611fd8d96 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sun, 24 Sep 2023 21:20:45 +0330 Subject: [PATCH] LibWasm: Make sure to place imported functions before the module's aafef1e92d8e750b8abd2100e5896df75505e2a1 broke this while trying to make the global import available in initialisation, this commit makes sure we place the module's own functions after all resolved imports. --- .../LibWasm/AbstractMachine/AbstractMachine.cpp | 11 ++++++++--- .../LibWasm/AbstractMachine/AbstractMachine.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp index f11b0277dc..fb00605619 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp @@ -162,11 +162,14 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector module_functions; + module_functions.ensure_capacity(module.functions().size()); + for (auto& func : module.functions()) { auto address = m_store.allocate(main_module_instance, func); VERIFY(address.has_value()); auxiliary_instance.functions().append(*address); - main_module_instance.functions().append(*address); + module_functions.append(*address); } BytecodeInterpreter interpreter(m_stack_info); @@ -193,7 +196,7 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector([&](ElementSection const& section) { @@ -393,7 +396,7 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector AbstractMachine::allocate_all_initial_phase(Module const& module, ModuleInstance& module_instance, Vector& externs, Vector& global_values) +Optional AbstractMachine::allocate_all_initial_phase(Module const& module, ModuleInstance& module_instance, Vector& externs, Vector& global_values, Vector& own_functions) { Optional result; @@ -405,6 +408,8 @@ Optional AbstractMachine::allocate_all_initial_phase(Module [&](GlobalAddress const& address) { module_instance.globals().append(address); }); } + module_instance.functions().extend(own_functions); + // FIXME: What if this fails? module.for_each_section_of_type([&](TableSection const& section) { diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index bc1166bd42..f6ac5f93ad 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -623,7 +623,7 @@ public: void enable_instruction_count_limit() { m_should_limit_instruction_count = true; } private: - Optional allocate_all_initial_phase(Module const&, ModuleInstance&, Vector&, Vector& global_values); + Optional allocate_all_initial_phase(Module const&, ModuleInstance&, Vector&, Vector& global_values, Vector& own_functions); Optional allocate_all_final_phase(Module const&, ModuleInstance&, Vector>& elements); Store m_store; StackInfo m_stack_info;