mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-28 18:40:29 +00:00
LibWasm: Decouple ModuleInstance from the AbstractMachine
This fixes a FIXME and will allow linking only select modules together, instead of linking every instantiated module into a big mess of exported entities :P
This commit is contained in:
parent
24b2a6c93a
commit
efb106069b
Notes:
sideshowbarker
2024-07-18 17:39:46 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/efb106069bb Pull-request: https://github.com/SerenityOS/serenity/pull/7239 Reviewed-by: https://github.com/linusg
4 changed files with 55 additions and 47 deletions
|
@ -31,18 +31,23 @@ class WebAssemblyModule final : public JS::Object {
|
||||||
JS_OBJECT(WebAssemblyModule, JS::Object);
|
JS_OBJECT(WebAssemblyModule, JS::Object);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// FIXME: This should only contain an instantiated module, not the entire abstract machine!
|
|
||||||
explicit WebAssemblyModule(JS::Object& prototype)
|
explicit WebAssemblyModule(JS::Object& prototype)
|
||||||
: JS::Object(prototype)
|
: JS::Object(prototype)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Wasm::AbstractMachine& machine() { return m_machine; }
|
||||||
|
Wasm::Module& module() { return *m_module; }
|
||||||
|
Wasm::ModuleInstance& module_instance() { return *m_module_instance; }
|
||||||
|
|
||||||
static WebAssemblyModule* create(JS::GlobalObject& global_object, Wasm::Module module)
|
static WebAssemblyModule* create(JS::GlobalObject& global_object, Wasm::Module module)
|
||||||
{
|
{
|
||||||
auto instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype());
|
auto instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype());
|
||||||
instance->m_module = move(module);
|
instance->m_module = move(module);
|
||||||
if (auto result = instance->m_machine.instantiate(*instance->m_module, {}); result.is_error())
|
if (auto result = machine().instantiate(*instance->m_module, {}); result.is_error())
|
||||||
global_object.vm().throw_exception<JS::TypeError>(global_object, result.release_error().error);
|
global_object.vm().throw_exception<JS::TypeError>(global_object, result.release_error().error);
|
||||||
|
else
|
||||||
|
instance->m_module_instance = result.release_value();
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
void initialize(JS::GlobalObject&) override;
|
void initialize(JS::GlobalObject&) override;
|
||||||
|
@ -53,10 +58,13 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_export);
|
JS_DECLARE_NATIVE_FUNCTION(get_export);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(wasm_invoke);
|
JS_DECLARE_NATIVE_FUNCTION(wasm_invoke);
|
||||||
|
|
||||||
Wasm::AbstractMachine m_machine;
|
static Wasm::AbstractMachine m_machine;
|
||||||
Optional<Wasm::Module> m_module;
|
Optional<Wasm::Module> m_module;
|
||||||
|
Optional<Wasm::ModuleInstance> m_module_instance;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Wasm::AbstractMachine WebAssemblyModule::m_machine;
|
||||||
|
|
||||||
TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
|
TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
|
||||||
{
|
{
|
||||||
auto object = vm.argument(0).to_object(global_object);
|
auto object = vm.argument(0).to_object(global_object);
|
||||||
|
@ -123,7 +131,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto instance = static_cast<WebAssemblyModule*>(object);
|
auto instance = static_cast<WebAssemblyModule*>(object);
|
||||||
for (auto& entry : instance->m_machine.module_instance().exports()) {
|
for (auto& entry : instance->module_instance().exports()) {
|
||||||
if (entry.name() == name) {
|
if (entry.name() == name) {
|
||||||
auto& value = entry.value();
|
auto& value = entry.value();
|
||||||
if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
|
if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
|
||||||
|
@ -151,7 +159,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
|
||||||
}
|
}
|
||||||
auto instance = static_cast<WebAssemblyModule*>(object);
|
auto instance = static_cast<WebAssemblyModule*>(object);
|
||||||
Wasm::FunctionAddress function_address { address };
|
Wasm::FunctionAddress function_address { address };
|
||||||
auto function_instance = instance->m_machine.store().get(function_address);
|
auto function_instance = WebAssemblyModule::machine().store().get(function_address);
|
||||||
if (!function_instance) {
|
if (!function_instance) {
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "Invalid function address");
|
vm.throw_exception<JS::TypeError>(global_object, "Invalid function address");
|
||||||
return {};
|
return {};
|
||||||
|
@ -194,7 +202,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = instance->m_machine.invoke(function_address, arguments);
|
auto result = WebAssemblyModule::machine().invoke(function_address, arguments);
|
||||||
if (result.is_trap()) {
|
if (result.is_trap()) {
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "Execution trapped");
|
vm.throw_exception<JS::TypeError>(global_object, "Execution trapped");
|
||||||
return {};
|
return {};
|
||||||
|
|
|
@ -85,10 +85,11 @@ GlobalInstance* Store::get(GlobalAddress address)
|
||||||
|
|
||||||
InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<ExternValue> externs)
|
InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<ExternValue> externs)
|
||||||
{
|
{
|
||||||
|
ModuleInstance main_module_instance;
|
||||||
Optional<InstantiationResult> instantiation_result;
|
Optional<InstantiationResult> instantiation_result;
|
||||||
|
|
||||||
module.for_each_section_of_type<TypeSection>([&](const TypeSection& section) {
|
module.for_each_section_of_type<TypeSection>([&](const TypeSection& section) {
|
||||||
m_module_instance.types() = section.types();
|
main_module_instance.types() = section.types();
|
||||||
});
|
});
|
||||||
|
|
||||||
// FIXME: Validate stuff
|
// FIXME: Validate stuff
|
||||||
|
@ -121,8 +122,8 @@ InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<Ex
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (auto result = allocate_all(module, externs, global_values); result.is_error()) {
|
if (auto result = allocate_all(module, main_module_instance, externs, global_values); result.has_value()) {
|
||||||
return result.error();
|
return result.release_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.for_each_section_of_type<ElementSection>([&](const ElementSection&) {
|
module.for_each_section_of_type<ElementSection>([&](const ElementSection&) {
|
||||||
|
@ -136,7 +137,7 @@ InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<Ex
|
||||||
segment.value().visit(
|
segment.value().visit(
|
||||||
[&](const DataSection::Data::Active& data) {
|
[&](const DataSection::Data::Active& data) {
|
||||||
auto frame = make<Frame>(
|
auto frame = make<Frame>(
|
||||||
m_module_instance,
|
main_module_instance,
|
||||||
Vector<Value> {},
|
Vector<Value> {},
|
||||||
data.offset,
|
data.offset,
|
||||||
1);
|
1);
|
||||||
|
@ -150,11 +151,11 @@ InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<Ex
|
||||||
[&](const ExternAddress&) { instantiation_result = InstantiationError { "Data segment offset returned an address" }; });
|
[&](const ExternAddress&) { instantiation_result = InstantiationError { "Data segment offset returned an address" }; });
|
||||||
if (instantiation_result.has_value() && instantiation_result->is_error())
|
if (instantiation_result.has_value() && instantiation_result->is_error())
|
||||||
return;
|
return;
|
||||||
if (m_module_instance.memories().size() <= data.index.value()) {
|
if (main_module_instance.memories().size() <= data.index.value()) {
|
||||||
instantiation_result = InstantiationError { String::formatted("Data segment referenced out-of-bounds memory ({}) of max {} entries", data.index.value(), m_module_instance.memories().size()) };
|
instantiation_result = InstantiationError { String::formatted("Data segment referenced out-of-bounds memory ({}) of max {} entries", data.index.value(), main_module_instance.memories().size()) };
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto address = m_module_instance.memories()[data.index.value()];
|
auto address = main_module_instance.memories()[data.index.value()];
|
||||||
if (auto instance = m_store.get(address)) {
|
if (auto instance = m_store.get(address)) {
|
||||||
if (instance->type().limits().max().value_or(data.init.size() + offset + 1) <= data.init.size() + offset) {
|
if (instance->type().limits().max().value_or(data.init.size() + offset + 1) <= data.init.size() + offset) {
|
||||||
instantiation_result = InstantiationError { String::formatted("Data segment attempted to write to out-of-bounds memory ({}) of max {} bytes", data.init.size() + offset, instance->type().limits().max().value()) };
|
instantiation_result = InstantiationError { String::formatted("Data segment attempted to write to out-of-bounds memory ({}) of max {} bytes", data.init.size() + offset, instance->type().limits().max().value()) };
|
||||||
|
@ -171,7 +172,7 @@ InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<Ex
|
||||||
});
|
});
|
||||||
|
|
||||||
module.for_each_section_of_type<StartSection>([&](const StartSection& section) {
|
module.for_each_section_of_type<StartSection>([&](const StartSection& section) {
|
||||||
auto& functions = m_module_instance.functions();
|
auto& functions = main_module_instance.functions();
|
||||||
auto index = section.function().index();
|
auto index = section.function().index();
|
||||||
if (functions.size() <= index.value()) {
|
if (functions.size() <= index.value()) {
|
||||||
instantiation_result = InstantiationError { String::formatted("Start section function referenced invalid index {} of max {} entries", index.value(), functions.size()) };
|
instantiation_result = InstantiationError { String::formatted("Start section function referenced invalid index {} of max {} entries", index.value(), functions.size()) };
|
||||||
|
@ -180,34 +181,34 @@ InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<Ex
|
||||||
invoke(functions[index.value()], {});
|
invoke(functions[index.value()], {});
|
||||||
});
|
});
|
||||||
|
|
||||||
return instantiation_result.value_or({});
|
return instantiation_result.value_or(move(main_module_instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
InstantiationResult AbstractMachine::allocate_all(const Module& module, Vector<ExternValue>& externs, Vector<Value>& global_values)
|
Optional<InstantiationError> AbstractMachine::allocate_all(const Module& module, ModuleInstance& module_instance, Vector<ExternValue>& externs, Vector<Value>& global_values)
|
||||||
{
|
{
|
||||||
Optional<InstantiationResult> result;
|
Optional<InstantiationError> result;
|
||||||
|
|
||||||
for (auto& entry : externs) {
|
for (auto& entry : externs) {
|
||||||
entry.visit(
|
entry.visit(
|
||||||
[&](const FunctionAddress& address) { m_module_instance.functions().append(address); },
|
[&](const FunctionAddress& address) { module_instance.functions().append(address); },
|
||||||
[&](const TableAddress& address) { m_module_instance.tables().append(address); },
|
[&](const TableAddress& address) { module_instance.tables().append(address); },
|
||||||
[&](const MemoryAddress& address) { m_module_instance.memories().append(address); },
|
[&](const MemoryAddress& address) { module_instance.memories().append(address); },
|
||||||
[&](const GlobalAddress& address) { m_module_instance.globals().append(address); });
|
[&](const GlobalAddress& address) { module_instance.globals().append(address); });
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: What if this fails?
|
// FIXME: What if this fails?
|
||||||
|
|
||||||
for (auto& func : module.functions()) {
|
for (auto& func : module.functions()) {
|
||||||
auto address = m_store.allocate(m_module_instance, func);
|
auto address = m_store.allocate(module_instance, func);
|
||||||
VERIFY(address.has_value());
|
VERIFY(address.has_value());
|
||||||
m_module_instance.functions().append(*address);
|
module_instance.functions().append(*address);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.for_each_section_of_type<TableSection>([&](const TableSection& section) {
|
module.for_each_section_of_type<TableSection>([&](const TableSection& section) {
|
||||||
for (auto& table : section.tables()) {
|
for (auto& table : section.tables()) {
|
||||||
auto table_address = m_store.allocate(table.type());
|
auto table_address = m_store.allocate(table.type());
|
||||||
VERIFY(table_address.has_value());
|
VERIFY(table_address.has_value());
|
||||||
m_module_instance.tables().append(*table_address);
|
module_instance.tables().append(*table_address);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -215,7 +216,7 @@ InstantiationResult AbstractMachine::allocate_all(const Module& module, Vector<E
|
||||||
for (auto& memory : section.memories()) {
|
for (auto& memory : section.memories()) {
|
||||||
auto memory_address = m_store.allocate(memory.type());
|
auto memory_address = m_store.allocate(memory.type());
|
||||||
VERIFY(memory_address.has_value());
|
VERIFY(memory_address.has_value());
|
||||||
m_module_instance.memories().append(*memory_address);
|
module_instance.memories().append(*memory_address);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -224,7 +225,7 @@ InstantiationResult AbstractMachine::allocate_all(const Module& module, Vector<E
|
||||||
for (auto& entry : section.entries()) {
|
for (auto& entry : section.entries()) {
|
||||||
auto address = m_store.allocate(entry.type(), global_values[index]);
|
auto address = m_store.allocate(entry.type(), global_values[index]);
|
||||||
VERIFY(address.has_value());
|
VERIFY(address.has_value());
|
||||||
m_module_instance.globals().append(*address);
|
module_instance.globals().append(*address);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -234,28 +235,28 @@ InstantiationResult AbstractMachine::allocate_all(const Module& module, Vector<E
|
||||||
Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress, Empty> address { Empty {} };
|
Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress, Empty> address { Empty {} };
|
||||||
entry.description().visit(
|
entry.description().visit(
|
||||||
[&](const FunctionIndex& index) {
|
[&](const FunctionIndex& index) {
|
||||||
if (m_module_instance.functions().size() > index.value())
|
if (module_instance.functions().size() > index.value())
|
||||||
address = FunctionAddress { m_module_instance.functions()[index.value()] };
|
address = FunctionAddress { module_instance.functions()[index.value()] };
|
||||||
else
|
else
|
||||||
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), m_module_instance.functions().size());
|
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.functions().size());
|
||||||
},
|
},
|
||||||
[&](const TableIndex& index) {
|
[&](const TableIndex& index) {
|
||||||
if (m_module_instance.tables().size() > index.value())
|
if (module_instance.tables().size() > index.value())
|
||||||
address = TableAddress { m_module_instance.tables()[index.value()] };
|
address = TableAddress { module_instance.tables()[index.value()] };
|
||||||
else
|
else
|
||||||
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), m_module_instance.tables().size());
|
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.tables().size());
|
||||||
},
|
},
|
||||||
[&](const MemoryIndex& index) {
|
[&](const MemoryIndex& index) {
|
||||||
if (m_module_instance.memories().size() > index.value())
|
if (module_instance.memories().size() > index.value())
|
||||||
address = MemoryAddress { m_module_instance.memories()[index.value()] };
|
address = MemoryAddress { module_instance.memories()[index.value()] };
|
||||||
else
|
else
|
||||||
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), m_module_instance.memories().size());
|
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.memories().size());
|
||||||
},
|
},
|
||||||
[&](const GlobalIndex& index) {
|
[&](const GlobalIndex& index) {
|
||||||
if (m_module_instance.globals().size() > index.value())
|
if (module_instance.globals().size() > index.value())
|
||||||
address = GlobalAddress { m_module_instance.globals()[index.value()] };
|
address = GlobalAddress { module_instance.globals()[index.value()] };
|
||||||
else
|
else
|
||||||
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), m_module_instance.globals().size());
|
dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.globals().size());
|
||||||
});
|
});
|
||||||
|
|
||||||
if (address.has<Empty>()) {
|
if (address.has<Empty>()) {
|
||||||
|
@ -263,14 +264,14 @@ InstantiationResult AbstractMachine::allocate_all(const Module& module, Vector<E
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_module_instance.exports().append(ExportInstance {
|
module_instance.exports().append(ExportInstance {
|
||||||
entry.name(),
|
entry.name(),
|
||||||
move(address).downcast<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>(),
|
move(address).downcast<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return result.value_or({});
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result AbstractMachine::invoke(FunctionAddress address, Vector<Value> arguments)
|
Result AbstractMachine::invoke(FunctionAddress address, Vector<Value> arguments)
|
||||||
|
|
|
@ -15,7 +15,6 @@ namespace Wasm {
|
||||||
struct InstantiationError {
|
struct InstantiationError {
|
||||||
String error { "Unknown error" };
|
String error { "Unknown error" };
|
||||||
};
|
};
|
||||||
using InstantiationResult = Result<void, InstantiationError>;
|
|
||||||
|
|
||||||
TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, FunctionAddress);
|
TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, FunctionAddress);
|
||||||
TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, ExternAddress);
|
TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, ExternAddress);
|
||||||
|
@ -419,6 +418,8 @@ private:
|
||||||
Vector<EntryType> m_data;
|
Vector<EntryType> m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using InstantiationResult = AK::Result<ModuleInstance, InstantiationError>;
|
||||||
|
|
||||||
class AbstractMachine {
|
class AbstractMachine {
|
||||||
public:
|
public:
|
||||||
explicit AbstractMachine() = default;
|
explicit AbstractMachine() = default;
|
||||||
|
@ -427,14 +428,11 @@ public:
|
||||||
InstantiationResult instantiate(const Module&, Vector<ExternValue>);
|
InstantiationResult instantiate(const Module&, Vector<ExternValue>);
|
||||||
Result invoke(FunctionAddress, Vector<Value>);
|
Result invoke(FunctionAddress, Vector<Value>);
|
||||||
|
|
||||||
auto& module_instance() const { return m_module_instance; }
|
|
||||||
auto& module_instance() { return m_module_instance; }
|
|
||||||
auto& store() const { return m_store; }
|
auto& store() const { return m_store; }
|
||||||
auto& store() { return m_store; }
|
auto& store() { return m_store; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
InstantiationResult allocate_all(const Module&, Vector<ExternValue>&, Vector<Value>& global_values);
|
Optional<InstantiationError> allocate_all(const Module&, ModuleInstance&, Vector<ExternValue>&, Vector<Value>& global_values);
|
||||||
ModuleInstance m_module_instance;
|
|
||||||
Store m_store;
|
Store m_store;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,7 @@ int main(int argc, char* argv[])
|
||||||
warnln("Module instantiation failed: {}", result.error().error);
|
warnln("Module instantiation failed: {}", result.error().error);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
auto module_instance = result.release_value();
|
||||||
|
|
||||||
auto stream = Core::OutputFileStream::standard_output();
|
auto stream = Core::OutputFileStream::standard_output();
|
||||||
auto print_func = [&](const auto& address) {
|
auto print_func = [&](const auto& address) {
|
||||||
|
@ -90,7 +91,7 @@ int main(int argc, char* argv[])
|
||||||
};
|
};
|
||||||
if (print) {
|
if (print) {
|
||||||
// Now, let's dump the functions!
|
// Now, let's dump the functions!
|
||||||
for (auto& address : machine.module_instance().functions()) {
|
for (auto& address : module_instance.functions()) {
|
||||||
print_func(address);
|
print_func(address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,7 +99,7 @@ int main(int argc, char* argv[])
|
||||||
if (!exported_function_to_execute.is_empty()) {
|
if (!exported_function_to_execute.is_empty()) {
|
||||||
Optional<Wasm::FunctionAddress> run_address;
|
Optional<Wasm::FunctionAddress> run_address;
|
||||||
Vector<Wasm::Value> values;
|
Vector<Wasm::Value> values;
|
||||||
for (auto& entry : machine.module_instance().exports()) {
|
for (auto& entry : module_instance.exports()) {
|
||||||
if (entry.name() == exported_function_to_execute) {
|
if (entry.name() == exported_function_to_execute) {
|
||||||
if (auto addr = entry.value().get_pointer<Wasm::FunctionAddress>())
|
if (auto addr = entry.value().get_pointer<Wasm::FunctionAddress>())
|
||||||
run_address = *addr;
|
run_address = *addr;
|
||||||
|
|
Loading…
Reference in a new issue