2021-05-01 01:08:51 +04:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
#include <AK/ByteBuffer.h>
|
2021-05-17 12:29:44 +04:30
|
|
|
#include <AK/Function.h>
|
2021-05-10 17:26:17 +04:30
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/HashTable.h>
|
2024-11-21 01:48:16 +04:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2023-05-28 12:13:43 +02:00
|
|
|
#include <AK/StackInfo.h>
|
2023-06-12 13:04:22 +03:30
|
|
|
#include <AK/UFixedBigInt.h>
|
2021-05-01 01:08:51 +04:30
|
|
|
#include <LibWasm/Types.h>
|
|
|
|
|
|
|
|
namespace Wasm {
|
|
|
|
|
2021-05-17 12:29:44 +04:30
|
|
|
class Configuration;
|
2025-02-11 19:57:05 +05:00
|
|
|
class Result;
|
2021-05-21 21:10:44 +04:30
|
|
|
struct Interpreter;
|
2025-04-22 09:48:26 +02:00
|
|
|
struct Trap;
|
2021-05-17 12:29:44 +04:30
|
|
|
|
2021-05-10 17:26:17 +04:30
|
|
|
struct LinkError {
|
|
|
|
enum OtherErrors {
|
|
|
|
InvalidImportedModule,
|
|
|
|
};
|
2023-12-16 17:49:34 +03:30
|
|
|
Vector<ByteString> missing_imports;
|
2021-05-10 17:26:17 +04:30
|
|
|
Vector<OtherErrors> other_errors;
|
|
|
|
};
|
2021-05-01 01:08:51 +04:30
|
|
|
|
AK+Everywhere: Replace DistinctNumeric bool parameters with named ones
This means that rather than this:
```
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false,
false, true, FunctionAddress);
```
We now have this:
```
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic,
Comparison, Increment);
```
Which is a lot more readable. :^)
Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
2022-11-10 15:43:23 +00:00
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic, Comparison, Increment);
|
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ExternAddress, Arithmetic, Comparison, Increment);
|
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, TableAddress, Arithmetic, Comparison, Increment);
|
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, GlobalAddress, Arithmetic, Comparison, Increment);
|
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ElementAddress, Arithmetic, Comparison, Increment);
|
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, DataAddress, Arithmetic, Comparison, Increment);
|
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, MemoryAddress, Arithmetic, Comparison, Increment);
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
// FIXME: These should probably be made generic/virtual if/when we decide to do something more
|
|
|
|
// fancy than just a dumb interpreter.
|
2021-06-04 03:30:09 +04:30
|
|
|
class Reference {
|
|
|
|
public:
|
|
|
|
struct Null {
|
|
|
|
ValueType type;
|
|
|
|
};
|
|
|
|
struct Func {
|
|
|
|
FunctionAddress address;
|
2025-04-15 16:01:01 -06:00
|
|
|
RefPtr<Module const> source_module; // null if host function.
|
2021-06-04 03:30:09 +04:30
|
|
|
};
|
|
|
|
struct Extern {
|
|
|
|
ExternAddress address;
|
|
|
|
};
|
|
|
|
|
|
|
|
using RefType = Variant<Null, Func, Extern>;
|
|
|
|
explicit Reference(RefType ref)
|
|
|
|
: m_ref(move(ref))
|
|
|
|
{
|
|
|
|
}
|
2024-05-31 17:14:49 -07:00
|
|
|
explicit Reference()
|
|
|
|
: m_ref(Reference::Null { ValueType(ValueType::Kind::FunctionReference) })
|
|
|
|
{
|
|
|
|
}
|
2021-06-04 03:30:09 +04:30
|
|
|
|
|
|
|
auto& ref() const { return m_ref; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefType m_ref;
|
|
|
|
};
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
class Value {
|
|
|
|
public:
|
2024-08-17 15:40:21 -07:00
|
|
|
explicit Value(ValueType type)
|
2024-08-04 08:06:50 -07:00
|
|
|
: m_value(u128())
|
2021-05-25 01:38:02 +04:30
|
|
|
{
|
2024-08-17 15:40:21 -07:00
|
|
|
switch (type.kind()) {
|
|
|
|
case ValueType::I32:
|
|
|
|
case ValueType::I64:
|
|
|
|
case ValueType::F32:
|
|
|
|
case ValueType::F64:
|
|
|
|
case ValueType::V128:
|
|
|
|
break;
|
|
|
|
case ValueType::FunctionReference:
|
|
|
|
// ref.null funcref
|
|
|
|
m_value = u128(0, 2);
|
|
|
|
break;
|
|
|
|
case ValueType::ExternReference:
|
|
|
|
// ref.null externref
|
|
|
|
m_value = u128(0, 3);
|
|
|
|
break;
|
|
|
|
}
|
2021-05-25 01:38:02 +04:30
|
|
|
}
|
|
|
|
|
2024-08-04 08:06:50 -07:00
|
|
|
template<typename T>
|
|
|
|
requires(sizeof(T) == sizeof(u64)) explicit Value(T raw_value)
|
|
|
|
: m_value(u128(bit_cast<i64>(raw_value), 0))
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2024-08-04 08:06:50 -07:00
|
|
|
requires(sizeof(T) == sizeof(u32)) explicit Value(T raw_value)
|
|
|
|
: m_value(u128(static_cast<i64>(bit_cast<i32>(raw_value)), 0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
requires(sizeof(T) == sizeof(u8) && Signed<T>) explicit Value(T raw_value)
|
|
|
|
: m_value(u128(static_cast<i64>(bit_cast<i8>(raw_value)), 0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
requires(sizeof(T) == sizeof(u8) && Unsigned<T>) explicit Value(T raw_value)
|
|
|
|
: m_value(u128(static_cast<u64>(bit_cast<u8>(raw_value)), 0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
requires(sizeof(T) == sizeof(u16) && Signed<T>) explicit Value(T raw_value)
|
|
|
|
: m_value(u128(static_cast<i64>(bit_cast<i16>(raw_value)), 0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
requires(sizeof(T) == sizeof(u16) && Unsigned<T>) explicit Value(T raw_value)
|
|
|
|
: m_value(u128(static_cast<u64>(bit_cast<u16>(raw_value)), 0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit Value(Reference ref)
|
|
|
|
{
|
|
|
|
// Reference variant is encoded in the high storage of the u128:
|
|
|
|
// 0: funcref
|
|
|
|
// 1: externref
|
|
|
|
// 2: null funcref
|
|
|
|
// 3: null externref
|
|
|
|
ref.ref().visit(
|
2024-08-22 01:13:37 +02:00
|
|
|
[&](Reference::Func const& func) { m_value = u128(bit_cast<u64>(func.address), bit_cast<u64>(func.source_module.ptr())); },
|
2024-08-04 08:06:50 -07:00
|
|
|
[&](Reference::Extern const& func) { m_value = u128(bit_cast<u64>(func.address), 1); },
|
|
|
|
[&](Reference::Null const& null) { m_value = u128(0, null.type.kind() == ValueType::Kind::FunctionReference ? 2 : 3); });
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
2023-06-12 13:04:22 +03:30
|
|
|
template<SameAs<u128> T>
|
|
|
|
explicit Value(T raw_value)
|
|
|
|
: m_value(raw_value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:55:40 +04:30
|
|
|
ALWAYS_INLINE Value(Value const& value) = default;
|
|
|
|
ALWAYS_INLINE Value(Value&& value) = default;
|
|
|
|
ALWAYS_INLINE Value& operator=(Value&& value) = default;
|
|
|
|
ALWAYS_INLINE Value& operator=(Value const& value) = default;
|
2021-05-24 21:23:56 +04:30
|
|
|
|
2021-05-01 03:19:01 +04:30
|
|
|
template<typename T>
|
2024-08-04 08:06:50 -07:00
|
|
|
ALWAYS_INLINE T to() const
|
|
|
|
{
|
2024-08-16 16:07:17 +02:00
|
|
|
static_assert(IsOneOf<T, u128, u64, i64, f32, f64, Reference> || IsIntegral<T>, "Unsupported type for Value::to()");
|
|
|
|
|
2024-08-04 08:06:50 -07:00
|
|
|
if constexpr (IsSame<T, u128>) {
|
|
|
|
return m_value;
|
|
|
|
}
|
2024-08-16 16:07:17 +02:00
|
|
|
if constexpr (IsOneOf<T, u64, i64>) {
|
|
|
|
return bit_cast<T>(m_value.low());
|
2024-08-04 08:06:50 -07:00
|
|
|
}
|
2024-08-16 16:07:17 +02:00
|
|
|
if constexpr (IsIntegral<T> && sizeof(T) < 8) {
|
|
|
|
return bit_cast<T>(static_cast<MakeUnsigned<T>>(m_value.low() & NumericLimits<MakeUnsigned<T>>::max()));
|
2024-08-04 08:06:50 -07:00
|
|
|
}
|
|
|
|
if constexpr (IsSame<T, f32>) {
|
|
|
|
u32 low = m_value.low() & 0xFFFFFFFF;
|
|
|
|
return bit_cast<f32>(low);
|
|
|
|
}
|
|
|
|
if constexpr (IsSame<T, f64>) {
|
|
|
|
return bit_cast<f64>(m_value.low());
|
|
|
|
}
|
|
|
|
if constexpr (IsSame<T, Reference>) {
|
2024-08-22 01:13:37 +02:00
|
|
|
switch (m_value.high() & 3) {
|
2024-08-04 08:06:50 -07:00
|
|
|
case 0:
|
2024-08-22 01:13:37 +02:00
|
|
|
return Reference { Reference::Func { bit_cast<FunctionAddress>(m_value.low()), bit_cast<Wasm::Module*>(m_value.high()) } };
|
2024-08-04 08:06:50 -07:00
|
|
|
case 1:
|
2024-08-07 21:11:02 +02:00
|
|
|
return Reference { Reference::Extern { bit_cast<ExternAddress>(m_value.low()) } };
|
2024-08-04 08:06:50 -07:00
|
|
|
case 2:
|
2024-08-07 21:11:02 +02:00
|
|
|
return Reference { Reference::Null { ValueType(ValueType::Kind::FunctionReference) } };
|
2024-08-04 08:06:50 -07:00
|
|
|
case 3:
|
2024-08-07 21:11:02 +02:00
|
|
|
return Reference { Reference::Null { ValueType(ValueType::Kind::ExternReference) } };
|
2024-08-04 08:06:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-08-12 00:55:40 +04:30
|
|
|
}
|
2024-08-04 08:06:50 -07:00
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
auto& value() const { return m_value; }
|
|
|
|
|
|
|
|
private:
|
2024-08-04 08:06:50 -07:00
|
|
|
u128 m_value;
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
2025-06-06 14:13:28 +02:00
|
|
|
static_assert(IsTriviallyDestructible<Value>);
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
struct ExternallyManagedTrap {
|
|
|
|
Array<u8, 64> data;
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
template<typename T>
|
|
|
|
T const& unsafe_external_object_as() const
|
2023-02-25 11:15:11 +03:30
|
|
|
{
|
2025-04-22 09:48:26 +02:00
|
|
|
static_assert(sizeof(T) <= sizeof(data), "Object size is too large for ExternallyManagedTrap");
|
|
|
|
return *reinterpret_cast<T const*>(data.data());
|
2023-02-25 11:15:11 +03:30
|
|
|
}
|
2025-04-22 09:48:26 +02:00
|
|
|
};
|
2023-02-25 11:15:11 +03:30
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
struct Trap {
|
|
|
|
Variant<ByteString, ExternallyManagedTrap> data;
|
|
|
|
|
|
|
|
ByteString format() const
|
2023-02-25 11:15:11 +03:30
|
|
|
{
|
2025-04-22 09:48:26 +02:00
|
|
|
if (auto const* ptr = data.get_pointer<ByteString>())
|
|
|
|
return *ptr;
|
|
|
|
return "<Externally managed Trap Data>";
|
2023-02-25 11:15:11 +03:30
|
|
|
}
|
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
template<typename T>
|
|
|
|
static Trap from_external_object(T&& object)
|
2023-02-25 11:15:11 +03:30
|
|
|
{
|
2025-04-22 09:48:26 +02:00
|
|
|
static_assert(sizeof(T) <= sizeof(ExternallyManagedTrap::data), "Object size is too large for ExternallyManagedTrap");
|
|
|
|
static_assert(IsTriviallyCopyable<T>, "Object must be trivially copyable");
|
|
|
|
static_assert(IsTriviallyDestructible<T>, "Object must be trivially destructible");
|
|
|
|
|
|
|
|
ExternallyManagedTrap externally_managed_trap;
|
|
|
|
new (externally_managed_trap.data.data()) T(forward<T>(object));
|
|
|
|
return Trap { externally_managed_trap };
|
2023-02-25 11:15:11 +03:30
|
|
|
}
|
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
static Trap from_string(ByteString string)
|
|
|
|
{
|
|
|
|
return Trap { move(string) };
|
|
|
|
}
|
2023-02-25 11:15:11 +03:30
|
|
|
};
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
class Result {
|
|
|
|
public:
|
|
|
|
explicit Result(Vector<Value> values)
|
2021-07-01 17:03:17 +04:30
|
|
|
: m_result(move(values))
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:03:17 +04:30
|
|
|
Result(Trap trap)
|
|
|
|
: m_result(move(trap))
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:03:17 +04:30
|
|
|
auto is_trap() const { return m_result.has<Trap>(); }
|
|
|
|
auto& values() const { return m_result.get<Vector<Value>>(); }
|
|
|
|
auto& values() { return m_result.get<Vector<Value>>(); }
|
|
|
|
auto& trap() const { return m_result.get<Trap>(); }
|
|
|
|
auto& trap() { return m_result.get<Trap>(); }
|
2023-02-25 11:15:11 +03:30
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
private:
|
|
|
|
explicit Result(Variant<Vector<Value>, Trap>&& result)
|
|
|
|
: m_result(move(result))
|
2023-02-25 11:15:11 +03:30
|
|
|
{
|
|
|
|
}
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
Variant<Vector<Value>, Trap> m_result;
|
|
|
|
};
|
|
|
|
|
2025-07-25 14:28:29 +02:00
|
|
|
enum class InstantiationErrorSource : u8 {
|
|
|
|
Linking,
|
|
|
|
StartFunction,
|
|
|
|
};
|
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
struct InstantiationError {
|
|
|
|
ByteString error { "Unknown error" };
|
|
|
|
Optional<Trap> relevant_trap {};
|
2025-07-25 14:28:29 +02:00
|
|
|
InstantiationErrorSource source { InstantiationErrorSource::Linking };
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
using ExternValue = Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>;
|
|
|
|
|
|
|
|
class ExportInstance {
|
|
|
|
public:
|
2023-12-16 17:49:34 +03:30
|
|
|
explicit ExportInstance(ByteString name, ExternValue value)
|
2021-05-01 01:08:51 +04:30
|
|
|
: m_name(move(name))
|
|
|
|
, m_value(move(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& name() const { return m_name; }
|
|
|
|
auto& value() const { return m_value; }
|
|
|
|
|
|
|
|
private:
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_name;
|
2021-05-01 01:08:51 +04:30
|
|
|
ExternValue m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ModuleInstance {
|
|
|
|
public:
|
|
|
|
explicit ModuleInstance(
|
|
|
|
Vector<FunctionType> types, Vector<FunctionAddress> function_addresses, Vector<TableAddress> table_addresses,
|
2021-12-04 17:56:58 +03:30
|
|
|
Vector<MemoryAddress> memory_addresses, Vector<GlobalAddress> global_addresses, Vector<DataAddress> data_addresses,
|
|
|
|
Vector<ExportInstance> exports)
|
2021-05-01 01:08:51 +04:30
|
|
|
: m_types(move(types))
|
|
|
|
, m_functions(move(function_addresses))
|
|
|
|
, m_tables(move(table_addresses))
|
|
|
|
, m_memories(move(memory_addresses))
|
|
|
|
, m_globals(move(global_addresses))
|
2021-12-04 17:56:58 +03:30
|
|
|
, m_datas(move(data_addresses))
|
2021-05-01 01:08:51 +04:30
|
|
|
, m_exports(move(exports))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleInstance() = default;
|
|
|
|
|
|
|
|
auto& types() const { return m_types; }
|
|
|
|
auto& functions() const { return m_functions; }
|
|
|
|
auto& tables() const { return m_tables; }
|
|
|
|
auto& memories() const { return m_memories; }
|
|
|
|
auto& globals() const { return m_globals; }
|
2021-06-04 03:30:09 +04:30
|
|
|
auto& elements() const { return m_elements; }
|
2021-12-04 17:56:58 +03:30
|
|
|
auto& datas() const { return m_datas; }
|
2021-05-01 01:08:51 +04:30
|
|
|
auto& exports() const { return m_exports; }
|
|
|
|
|
|
|
|
auto& types() { return m_types; }
|
|
|
|
auto& functions() { return m_functions; }
|
|
|
|
auto& tables() { return m_tables; }
|
|
|
|
auto& memories() { return m_memories; }
|
|
|
|
auto& globals() { return m_globals; }
|
2021-06-04 03:30:09 +04:30
|
|
|
auto& elements() { return m_elements; }
|
2021-12-04 17:56:58 +03:30
|
|
|
auto& datas() { return m_datas; }
|
2021-05-01 01:08:51 +04:30
|
|
|
auto& exports() { return m_exports; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<FunctionType> m_types;
|
|
|
|
Vector<FunctionAddress> m_functions;
|
|
|
|
Vector<TableAddress> m_tables;
|
|
|
|
Vector<MemoryAddress> m_memories;
|
|
|
|
Vector<GlobalAddress> m_globals;
|
2021-06-04 03:30:09 +04:30
|
|
|
Vector<ElementAddress> m_elements;
|
2021-12-04 17:56:58 +03:30
|
|
|
Vector<DataAddress> m_datas;
|
2021-05-01 01:08:51 +04:30
|
|
|
Vector<ExportInstance> m_exports;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WasmFunction {
|
|
|
|
public:
|
2024-08-22 01:13:37 +02:00
|
|
|
explicit WasmFunction(FunctionType const& type, ModuleInstance const& instance, Module const& module, CodeSection::Code const& code)
|
2021-05-01 01:08:51 +04:30
|
|
|
: m_type(type)
|
2024-08-22 01:13:37 +02:00
|
|
|
, m_module(module.make_weak_ptr())
|
|
|
|
, m_module_instance(instance)
|
2021-05-01 01:08:51 +04:30
|
|
|
, m_code(code)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& type() const { return m_type; }
|
2024-08-22 01:13:37 +02:00
|
|
|
auto& module() const { return m_module_instance; }
|
2021-05-01 01:08:51 +04:30
|
|
|
auto& code() const { return m_code; }
|
2024-08-22 01:13:37 +02:00
|
|
|
RefPtr<Module const> module_ref() const { return m_module.strong_ref(); }
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
private:
|
2021-05-11 04:44:59 +04:30
|
|
|
FunctionType m_type;
|
2024-08-22 01:13:37 +02:00
|
|
|
WeakPtr<Module const> m_module;
|
|
|
|
ModuleInstance const& m_module_instance;
|
2024-07-27 16:44:36 -07:00
|
|
|
CodeSection::Code const& m_code;
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
class HostFunction {
|
|
|
|
public:
|
2024-07-08 18:09:00 -07:00
|
|
|
explicit HostFunction(AK::Function<Result(Configuration&, Vector<Value>&)> function, FunctionType const& type, ByteString name)
|
2021-05-17 12:29:44 +04:30
|
|
|
: m_function(move(function))
|
2021-05-01 01:08:51 +04:30
|
|
|
, m_type(type)
|
2024-07-08 18:09:00 -07:00
|
|
|
, m_name(move(name))
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:29:44 +04:30
|
|
|
auto& function() { return m_function; }
|
2021-05-01 01:08:51 +04:30
|
|
|
auto& type() const { return m_type; }
|
2024-07-08 18:09:00 -07:00
|
|
|
auto& name() const { return m_name; }
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
private:
|
2021-05-17 12:29:44 +04:30
|
|
|
AK::Function<Result(Configuration&, Vector<Value>&)> m_function;
|
2021-05-11 04:44:59 +04:30
|
|
|
FunctionType m_type;
|
2024-07-08 18:09:00 -07:00
|
|
|
ByteString m_name;
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
using FunctionInstance = Variant<WasmFunction, HostFunction>;
|
|
|
|
|
|
|
|
class TableInstance {
|
|
|
|
public:
|
2024-05-31 17:14:49 -07:00
|
|
|
explicit TableInstance(TableType const& type, Vector<Reference> elements)
|
2021-05-01 01:08:51 +04:30
|
|
|
: m_elements(move(elements))
|
|
|
|
, m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& elements() const { return m_elements; }
|
|
|
|
auto& elements() { return m_elements; }
|
|
|
|
auto& type() const { return m_type; }
|
|
|
|
|
2024-05-31 17:14:49 -07:00
|
|
|
bool grow(u32 size_to_grow, Reference const& fill_value)
|
2021-09-04 04:40:57 +04:30
|
|
|
{
|
|
|
|
if (size_to_grow == 0)
|
|
|
|
return true;
|
2024-05-31 17:14:49 -07:00
|
|
|
size_t new_size = m_elements.size() + size_to_grow;
|
2021-09-04 04:40:57 +04:30
|
|
|
if (auto max = m_type.limits().max(); max.has_value()) {
|
|
|
|
if (max.value() < new_size)
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-31 17:14:49 -07:00
|
|
|
if (new_size >= NumericLimits<u32>::max()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-04 04:40:57 +04:30
|
|
|
auto previous_size = m_elements.size();
|
2021-11-10 11:55:37 +01:00
|
|
|
if (m_elements.try_resize(new_size).is_error())
|
2021-09-04 04:40:57 +04:30
|
|
|
return false;
|
|
|
|
for (size_t i = previous_size; i < m_elements.size(); ++i)
|
|
|
|
m_elements[i] = fill_value;
|
2024-08-18 10:56:25 -07:00
|
|
|
|
|
|
|
m_type = TableType { m_type.element_type(), Limits(m_type.limits().min() + size_to_grow, m_type.limits().max()) };
|
|
|
|
|
2021-09-04 04:40:57 +04:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
private:
|
2024-05-31 17:14:49 -07:00
|
|
|
Vector<Reference> m_elements;
|
2024-03-30 19:41:27 +01:00
|
|
|
TableType m_type;
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
class MemoryInstance {
|
|
|
|
public:
|
2022-02-15 03:05:49 +03:30
|
|
|
static ErrorOr<MemoryInstance> create(MemoryType const& type)
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
2022-02-15 03:05:49 +03:30
|
|
|
MemoryInstance instance { type };
|
|
|
|
|
2024-07-10 15:05:20 -07:00
|
|
|
if (!instance.grow(type.limits().min() * Constants::page_size, GrowType::No))
|
2022-02-15 03:05:49 +03:30
|
|
|
return Error::from_string_literal("Failed to grow to requested size");
|
|
|
|
|
|
|
|
return { move(instance) };
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
auto& type() const { return m_type; }
|
|
|
|
auto size() const { return m_size; }
|
|
|
|
auto& data() const { return m_data; }
|
|
|
|
auto& data() { return m_data; }
|
|
|
|
|
2023-03-29 01:31:51 +03:30
|
|
|
enum class InhibitGrowCallback {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
2024-07-10 15:05:20 -07:00
|
|
|
enum class GrowType {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool grow(size_t size_to_grow, GrowType grow_type = GrowType::Yes, InhibitGrowCallback inhibit_callback = InhibitGrowCallback::No)
|
2021-05-03 22:45:47 +04:30
|
|
|
{
|
|
|
|
if (size_to_grow == 0)
|
|
|
|
return true;
|
2022-01-03 02:50:55 -07:00
|
|
|
u64 new_size = m_data.size() + size_to_grow;
|
2021-07-06 14:03:54 +04:30
|
|
|
// Can't grow past 2^16 pages.
|
|
|
|
if (new_size >= Constants::page_size * 65536)
|
|
|
|
return false;
|
2021-07-06 12:05:29 +04:30
|
|
|
if (auto max = m_type.limits().max(); max.has_value()) {
|
|
|
|
if (max.value() * Constants::page_size < new_size)
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-03 22:45:47 +04:30
|
|
|
auto previous_size = m_size;
|
2021-11-10 14:33:44 +01:00
|
|
|
if (m_data.try_resize(new_size).is_error())
|
2021-09-06 03:28:46 +04:30
|
|
|
return false;
|
2021-05-03 22:45:47 +04:30
|
|
|
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);
|
2023-03-29 01:31:51 +03:30
|
|
|
|
|
|
|
// NOTE: This exists because wasm-js-api wants to execute code after a successful grow,
|
|
|
|
// See [this issue](https://github.com/WebAssembly/spec/issues/1635) for more details.
|
|
|
|
if (inhibit_callback == InhibitGrowCallback::No && successful_grow_hook)
|
|
|
|
successful_grow_hook();
|
|
|
|
|
2024-07-10 15:05:20 -07:00
|
|
|
if (grow_type == GrowType::Yes) {
|
|
|
|
// Grow the memory's type. We do this when encountering a `memory.grow`.
|
|
|
|
//
|
|
|
|
// See relevant spec link:
|
|
|
|
// https://www.w3.org/TR/wasm-core-2/#growing-memories%E2%91%A0
|
|
|
|
m_type = MemoryType { Limits(m_type.limits().min() + size_to_grow / Constants::page_size, m_type.limits().max()) };
|
|
|
|
}
|
|
|
|
|
2021-05-03 22:45:47 +04:30
|
|
|
return true;
|
|
|
|
}
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2023-03-29 01:31:51 +03:30
|
|
|
Function<void()> successful_grow_hook;
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
private:
|
2022-02-15 03:05:49 +03:30
|
|
|
explicit MemoryInstance(MemoryType const& type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-30 19:41:27 +01:00
|
|
|
MemoryType m_type;
|
2021-05-01 01:08:51 +04:30
|
|
|
size_t m_size { 0 };
|
|
|
|
ByteBuffer m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GlobalInstance {
|
|
|
|
public:
|
2024-08-04 08:06:50 -07:00
|
|
|
explicit GlobalInstance(Value value, bool is_mutable, ValueType type)
|
2021-05-01 01:08:51 +04:30
|
|
|
: m_mutable(is_mutable)
|
2024-08-04 08:06:50 -07:00
|
|
|
, m_value(value)
|
|
|
|
, m_type(type)
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto is_mutable() const { return m_mutable; }
|
|
|
|
auto& value() const { return m_value; }
|
2024-08-04 08:06:50 -07:00
|
|
|
GlobalType type() const { return { m_type, is_mutable() }; }
|
2021-05-14 22:54:27 +04:30
|
|
|
void set_value(Value value)
|
|
|
|
{
|
|
|
|
VERIFY(is_mutable());
|
|
|
|
m_value = move(value);
|
|
|
|
}
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_mutable { false };
|
|
|
|
Value m_value;
|
2024-08-04 08:06:50 -07:00
|
|
|
ValueType m_type;
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
2021-12-04 17:56:58 +03:30
|
|
|
class DataInstance {
|
|
|
|
public:
|
|
|
|
explicit DataInstance(Vector<u8> data)
|
|
|
|
: m_data(move(data))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const { return m_data.size(); }
|
|
|
|
|
|
|
|
Vector<u8>& data() { return m_data; }
|
|
|
|
Vector<u8> const& data() const { return m_data; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<u8> m_data;
|
|
|
|
};
|
|
|
|
|
2021-06-04 03:30:09 +04:30
|
|
|
class ElementInstance {
|
|
|
|
public:
|
|
|
|
explicit ElementInstance(ValueType type, Vector<Reference> references)
|
|
|
|
: m_type(move(type))
|
|
|
|
, m_references(move(references))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& type() const { return m_type; }
|
|
|
|
auto& references() const { return m_references; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ValueType m_type;
|
|
|
|
Vector<Reference> m_references;
|
|
|
|
};
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
class Store {
|
|
|
|
public:
|
|
|
|
Store() = default;
|
|
|
|
|
2024-08-22 01:13:37 +02:00
|
|
|
Optional<FunctionAddress> allocate(ModuleInstance&, Module const&, CodeSection::Code const&, TypeIndex);
|
2021-05-17 12:29:44 +04:30
|
|
|
Optional<FunctionAddress> allocate(HostFunction&&);
|
2021-06-04 03:42:11 +04:30
|
|
|
Optional<TableAddress> allocate(TableType const&);
|
|
|
|
Optional<MemoryAddress> allocate(MemoryType const&);
|
2021-12-04 17:56:58 +03:30
|
|
|
Optional<DataAddress> allocate_data(Vector<u8>);
|
2021-06-04 03:42:11 +04:30
|
|
|
Optional<GlobalAddress> allocate(GlobalType const&, Value);
|
|
|
|
Optional<ElementAddress> allocate(ValueType const&, Vector<Reference>);
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2024-08-22 01:13:37 +02:00
|
|
|
Module const* get_module_for(FunctionAddress);
|
2021-05-01 01:08:51 +04:30
|
|
|
FunctionInstance* get(FunctionAddress);
|
|
|
|
TableInstance* get(TableAddress);
|
|
|
|
MemoryInstance* get(MemoryAddress);
|
|
|
|
GlobalInstance* get(GlobalAddress);
|
2021-12-04 17:56:58 +03:30
|
|
|
DataInstance* get(DataAddress);
|
2021-06-04 03:30:09 +04:30
|
|
|
ElementInstance* get(ElementAddress);
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<FunctionInstance> m_functions;
|
|
|
|
Vector<TableInstance> m_tables;
|
|
|
|
Vector<MemoryInstance> m_memories;
|
|
|
|
Vector<GlobalInstance> m_globals;
|
2021-06-04 03:30:09 +04:30
|
|
|
Vector<ElementInstance> m_elements;
|
2021-12-04 17:56:58 +03:30
|
|
|
Vector<DataInstance> m_datas;
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
class Label {
|
|
|
|
public:
|
2024-08-02 20:51:40 -07:00
|
|
|
explicit Label(size_t arity, InstructionPointer continuation, size_t stack_height)
|
2021-05-01 03:19:01 +04:30
|
|
|
: m_arity(arity)
|
2024-08-02 20:51:40 -07:00
|
|
|
, m_stack_height(stack_height)
|
2021-05-01 03:19:01 +04:30
|
|
|
, m_continuation(continuation)
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto continuation() const { return m_continuation; }
|
2021-05-01 03:19:01 +04:30
|
|
|
auto arity() const { return m_arity; }
|
2024-08-02 20:51:40 -07:00
|
|
|
auto stack_height() const { return m_stack_height; }
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
private:
|
2021-05-01 03:19:01 +04:30
|
|
|
size_t m_arity { 0 };
|
2024-08-02 20:51:40 -07:00
|
|
|
size_t m_stack_height { 0 };
|
2021-05-23 21:21:17 +04:30
|
|
|
InstructionPointer m_continuation { 0 };
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
class Frame {
|
|
|
|
public:
|
2021-06-04 03:42:11 +04:30
|
|
|
explicit Frame(ModuleInstance const& module, Vector<Value> locals, Expression const& expression, size_t arity)
|
2021-05-01 01:08:51 +04:30
|
|
|
: m_module(module)
|
|
|
|
, m_locals(move(locals))
|
|
|
|
, m_expression(expression)
|
|
|
|
, m_arity(arity)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& module() const { return m_module; }
|
|
|
|
auto& locals() const { return m_locals; }
|
2021-05-01 03:19:01 +04:30
|
|
|
auto& locals() { return m_locals; }
|
2021-05-01 01:08:51 +04:30
|
|
|
auto& expression() const { return m_expression; }
|
|
|
|
auto arity() const { return m_arity; }
|
2024-08-02 20:51:40 -07:00
|
|
|
auto label_index() const { return m_label_index; }
|
|
|
|
auto& label_index() { return m_label_index; }
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
private:
|
2021-06-04 03:42:11 +04:30
|
|
|
ModuleInstance const& m_module;
|
2021-05-01 01:08:51 +04:30
|
|
|
Vector<Value> m_locals;
|
2021-06-04 03:42:11 +04:30
|
|
|
Expression const& m_expression;
|
2021-05-01 01:08:51 +04:30
|
|
|
size_t m_arity { 0 };
|
2024-08-02 20:51:40 -07:00
|
|
|
size_t m_label_index { 0 };
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
2024-03-11 22:57:59 +01:00
|
|
|
using InstantiationResult = AK::ErrorOr<NonnullOwnPtr<ModuleInstance>, InstantiationError>;
|
2021-05-10 15:40:49 +04:30
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
struct HostVisitOps {
|
|
|
|
Function<void(ExternallyManagedTrap&)> visit_trap;
|
|
|
|
};
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
class AbstractMachine {
|
|
|
|
public:
|
|
|
|
explicit AbstractMachine() = default;
|
|
|
|
|
2021-11-01 01:36:35 +03:30
|
|
|
// Validate a module; permanently sets the module's validity status.
|
|
|
|
ErrorOr<void, ValidationError> validate(Module&);
|
2021-05-01 01:08:51 +04:30
|
|
|
// Load and instantiate a module, and link it into this interpreter.
|
2021-06-04 03:42:11 +04:30
|
|
|
InstantiationResult instantiate(Module const&, Vector<ExternValue>);
|
2021-05-01 01:08:51 +04:30
|
|
|
Result invoke(FunctionAddress, Vector<Value>);
|
2021-05-24 02:04:58 +04:30
|
|
|
Result invoke(Interpreter&, FunctionAddress, Vector<Value>);
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
auto& store() const { return m_store; }
|
|
|
|
auto& store() { return m_store; }
|
|
|
|
|
2021-07-15 00:00:45 +04:30
|
|
|
void enable_instruction_count_limit() { m_should_limit_instruction_count = true; }
|
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
void visit_external_resources(HostVisitOps const&);
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
private:
|
2025-04-22 09:48:26 +02:00
|
|
|
class InterpreterHandle {
|
|
|
|
public:
|
|
|
|
explicit InterpreterHandle(AbstractMachine& machine, Interpreter& interpreter)
|
|
|
|
: m_machine(machine)
|
|
|
|
, m_interpreter(interpreter)
|
|
|
|
{
|
|
|
|
m_machine.m_active_interpreters.set(&m_interpreter);
|
|
|
|
}
|
|
|
|
|
|
|
|
~InterpreterHandle()
|
|
|
|
{
|
|
|
|
m_machine.m_active_interpreters.remove(&m_interpreter);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AbstractMachine& m_machine;
|
|
|
|
Interpreter& m_interpreter;
|
|
|
|
};
|
|
|
|
|
|
|
|
[[nodiscard]] InterpreterHandle register_scoped(Interpreter& interpreter)
|
|
|
|
{
|
|
|
|
return InterpreterHandle(*this, interpreter);
|
|
|
|
}
|
|
|
|
|
2023-09-24 21:20:45 +03:30
|
|
|
Optional<InstantiationError> allocate_all_initial_phase(Module const&, ModuleInstance&, Vector<ExternValue>&, Vector<Value>& global_values, Vector<FunctionAddress>& own_functions);
|
2021-06-04 03:42:11 +04:30
|
|
|
Optional<InstantiationError> allocate_all_final_phase(Module const&, ModuleInstance&, Vector<Vector<Reference>>& elements);
|
2021-05-01 01:08:51 +04:30
|
|
|
Store m_store;
|
2023-05-28 12:13:43 +02:00
|
|
|
StackInfo m_stack_info;
|
2025-04-22 09:48:26 +02:00
|
|
|
HashTable<Interpreter*> m_active_interpreters;
|
2021-07-15 00:00:45 +04:30
|
|
|
bool m_should_limit_instruction_count { false };
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
2021-05-10 17:26:17 +04:30
|
|
|
class Linker {
|
|
|
|
public:
|
|
|
|
struct Name {
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString module;
|
|
|
|
ByteString name;
|
2021-05-10 17:26:17 +04:30
|
|
|
ImportSection::Import::ImportDesc type;
|
|
|
|
};
|
|
|
|
|
2021-06-04 03:42:11 +04:30
|
|
|
explicit Linker(Module const& module)
|
2021-05-10 17:26:17 +04:30
|
|
|
: m_module(module)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link a module, the import 'module name' is ignored with this.
|
2021-06-04 03:42:11 +04:30
|
|
|
void link(ModuleInstance const&);
|
2021-05-10 17:26:17 +04:30
|
|
|
|
|
|
|
// Link a bunch of qualified values, also matches 'module name'.
|
2021-06-04 03:42:11 +04:30
|
|
|
void link(HashMap<Name, ExternValue> const&);
|
2021-05-10 17:26:17 +04:30
|
|
|
|
2021-05-17 12:29:44 +04:30
|
|
|
auto& unresolved_imports()
|
|
|
|
{
|
|
|
|
populate();
|
|
|
|
return m_unresolved_imports;
|
|
|
|
}
|
|
|
|
|
2024-03-11 22:57:59 +01:00
|
|
|
AK::ErrorOr<Vector<ExternValue>, LinkError> finish();
|
2021-05-10 17:26:17 +04:30
|
|
|
|
|
|
|
private:
|
|
|
|
void populate();
|
|
|
|
|
2021-06-04 03:42:11 +04:30
|
|
|
Module const& m_module;
|
2021-05-10 17:26:17 +04:30
|
|
|
HashMap<Name, ExternValue> m_resolved_imports;
|
|
|
|
HashTable<Name> m_unresolved_imports;
|
|
|
|
Vector<Name> m_ordered_imports;
|
|
|
|
Optional<LinkError> m_error;
|
|
|
|
};
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
2021-05-10 17:26:17 +04:30
|
|
|
|
|
|
|
template<>
|
2023-11-08 20:29:12 +01:00
|
|
|
struct AK::Traits<Wasm::Linker::Name> : public AK::DefaultTraits<Wasm::Linker::Name> {
|
2021-05-10 17:26:17 +04:30
|
|
|
static constexpr bool is_trivial() { return false; }
|
2021-06-04 03:42:11 +04:30
|
|
|
static unsigned hash(Wasm::Linker::Name const& entry) { return pair_int_hash(entry.module.hash(), entry.name.hash()); }
|
|
|
|
static bool equals(Wasm::Linker::Name const& a, Wasm::Linker::Name const& b) { return a.name == b.name && a.module == b.module; }
|
2021-05-10 17:26:17 +04:30
|
|
|
};
|