2021-04-26 05:18:13 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-10 12:37:52 -03:00
|
|
|
#include <AK/ConstrainedStream.h>
|
2023-01-02 17:07:18 -03:00
|
|
|
#include <AK/Debug.h>
|
2023-02-08 23:11:50 -03:00
|
|
|
#include <AK/Endian.h>
|
2025-11-07 07:29:20 -03:00
|
|
|
#include <AK/Enumerate.h>
|
2021-04-26 05:18:13 -03:00
|
|
|
#include <AK/LEB128.h>
|
2023-01-25 16:19:05 -03:00
|
|
|
#include <AK/MemoryStream.h>
|
2021-04-26 05:18:13 -03:00
|
|
|
#include <AK/ScopeLogger.h>
|
2023-06-12 06:34:22 -03:00
|
|
|
#include <AK/UFixedBigInt.h>
|
2021-04-26 05:18:13 -03:00
|
|
|
#include <LibWasm/Types.h>
|
|
|
|
|
|
|
|
|
|
namespace Wasm {
|
|
|
|
|
|
2024-07-27 21:21:19 -03:00
|
|
|
#define TRY_READ(stream, type, error) \
|
|
|
|
|
({ \
|
|
|
|
|
/* Ignore -Wshadow to allow nesting the macro. */ \
|
|
|
|
|
AK_IGNORE_DIAGNOSTIC("-Wshadow", \
|
|
|
|
|
auto&& _temporary_result = stream.read_value<type>()); \
|
|
|
|
|
static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
|
|
|
|
|
"Do not return a reference from a fallible expression"); \
|
|
|
|
|
if (_temporary_result.is_error()) [[unlikely]] \
|
|
|
|
|
return with_eof_check(stream, error); \
|
|
|
|
|
_temporary_result.release_value(); \
|
|
|
|
|
})
|
|
|
|
|
|
2023-02-09 21:00:18 -03:00
|
|
|
ParseError with_eof_check(Stream const& stream, ParseError error_if_not_eof)
|
2021-04-27 08:53:36 -03:00
|
|
|
{
|
2023-01-21 07:44:19 -03:00
|
|
|
if (stream.is_eof())
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseError::UnexpectedEof;
|
|
|
|
|
return error_if_not_eof;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 05:18:13 -03:00
|
|
|
template<typename T>
|
2025-06-06 09:11:24 -03:00
|
|
|
static auto parse_vector(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger;
|
2021-04-27 08:53:36 -03:00
|
|
|
if constexpr (requires { T::parse(stream); }) {
|
2024-03-11 18:57:59 -03:00
|
|
|
using ResultT = typename decltype(T::parse(stream))::ResultType;
|
2024-05-25 15:50:48 -03:00
|
|
|
auto count_or_error = stream.read_value<LEB128<u32>>();
|
2023-01-29 20:02:38 -03:00
|
|
|
if (count_or_error.is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<ResultT>> { with_eof_check(stream, ParseError::ExpectedSize) };
|
2023-01-29 20:02:38 -03:00
|
|
|
size_t count = count_or_error.release_value();
|
2021-04-27 08:53:36 -03:00
|
|
|
|
|
|
|
|
Vector<ResultT> entries;
|
2024-07-24 14:48:31 -03:00
|
|
|
entries.ensure_capacity(count);
|
2021-04-27 08:53:36 -03:00
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2021-04-26 05:18:13 -03:00
|
|
|
auto result = T::parse(stream);
|
|
|
|
|
if (result.is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<ResultT>> { result.error() };
|
2021-04-26 05:18:13 -03:00
|
|
|
entries.append(result.release_value());
|
|
|
|
|
}
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<ResultT>> { move(entries) };
|
|
|
|
|
} else {
|
2024-05-25 15:50:48 -03:00
|
|
|
auto count_or_error = stream.read_value<LEB128<u32>>();
|
2023-01-29 20:02:38 -03:00
|
|
|
if (count_or_error.is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
|
2023-01-29 20:02:38 -03:00
|
|
|
size_t count = count_or_error.release_value();
|
2021-04-27 08:53:36 -03:00
|
|
|
|
|
|
|
|
Vector<T> entries;
|
2024-07-24 14:48:31 -03:00
|
|
|
entries.ensure_capacity(count);
|
2021-04-27 08:53:36 -03:00
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2024-05-25 15:50:48 -03:00
|
|
|
if constexpr (IsSame<T, u32>) {
|
|
|
|
|
auto value_or_error = stream.read_value<LEB128<u32>>();
|
2023-01-29 20:02:38 -03:00
|
|
|
if (value_or_error.is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
|
2023-01-29 20:02:38 -03:00
|
|
|
size_t value = value_or_error.release_value();
|
2021-04-27 08:53:36 -03:00
|
|
|
entries.append(value);
|
|
|
|
|
} else if constexpr (IsSame<T, ssize_t>) {
|
2023-01-29 20:02:38 -03:00
|
|
|
auto value_or_error = stream.read_value<LEB128<ssize_t>>();
|
|
|
|
|
if (value_or_error.is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
|
2023-01-29 20:02:38 -03:00
|
|
|
ssize_t value = value_or_error.release_value();
|
2021-04-27 08:53:36 -03:00
|
|
|
entries.append(value);
|
|
|
|
|
} else if constexpr (IsSame<T, u8>) {
|
2021-09-02 05:38:12 -03:00
|
|
|
if (count > Constants::max_allowed_vector_size)
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<T>> { ParseError::HugeAllocationRequested };
|
|
|
|
|
entries.resize(count);
|
2023-03-01 11:27:35 -03:00
|
|
|
if (stream.read_until_filled({ entries.data(), entries.size() }).is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::InvalidInput) };
|
|
|
|
|
break; // Note: We read this all in one go!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ParseResult<Vector<T>> { move(entries) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
static ParseResult<ByteString> parse_name(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger;
|
2024-05-26 22:06:26 -03:00
|
|
|
auto data = TRY(parse_vector<u8>(stream));
|
2024-06-07 22:44:13 -03:00
|
|
|
auto string = ByteString::copy(data);
|
2025-06-26 20:52:09 -03:00
|
|
|
if (!Utf8View(string).validate(AllowLonelySurrogates::No))
|
2024-06-07 22:44:13 -03:00
|
|
|
return ParseError::InvalidUtf8;
|
|
|
|
|
return string;
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/values.html#integers
|
|
|
|
|
template<size_t N>
|
|
|
|
|
static ParseResult<i64> parse_signed_integer_of_size(Stream& stream)
|
|
|
|
|
{
|
|
|
|
|
static_assert(N <= 64);
|
|
|
|
|
constexpr size_t max_bytes = (N + 6) / 7;
|
|
|
|
|
i64 result = 0;
|
|
|
|
|
size_t shift = 0;
|
|
|
|
|
for (size_t i = 0; i < max_bytes; ++i) {
|
|
|
|
|
auto n = TRY_READ(stream, u8, ParseError::ExpectedSignedImmediate);
|
|
|
|
|
if (0 == (n & 0x80)) {
|
|
|
|
|
auto const remaining_bits = min(N - 7 * i, 7uz);
|
|
|
|
|
u8 const sign_bit = 1u << (remaining_bits - 1);
|
|
|
|
|
u8 const unused_mask = static_cast<u8>(0x7f & ~(static_cast<u8>(sign_bit << 1) - 1));
|
|
|
|
|
if ((n & sign_bit) ? ((n & unused_mask) != unused_mask) : ((n & unused_mask) != 0))
|
|
|
|
|
return with_eof_check(stream, ParseError::InvalidImmediate);
|
|
|
|
|
result |= static_cast<i64>(n & 0x7f) << shift;
|
|
|
|
|
shift += 7;
|
|
|
|
|
if (shift < 64 && (n & 0x40))
|
|
|
|
|
result |= ~static_cast<i64>(0) << shift;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
result |= static_cast<i64>(n & 0x7f) << shift;
|
|
|
|
|
shift += 7;
|
|
|
|
|
}
|
|
|
|
|
return with_eof_check(stream, ParseError::InvalidImmediate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#heap-types
|
|
|
|
|
static Optional<ValueType::Kind> abstract_heap_type_from_tag(u8 tag)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
|
|
|
|
switch (tag) {
|
2026-06-11 01:14:58 -03:00
|
|
|
case Constants::exn_reference_tag:
|
|
|
|
|
return ValueType::ExceptionReference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::array_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::ArrayReference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::struct_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::StructReference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::i31_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::I31Reference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::eq_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::EqReference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::any_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::AnyReference;
|
|
|
|
|
case Constants::extern_reference_tag:
|
|
|
|
|
return ValueType::ExternReference;
|
|
|
|
|
case Constants::function_reference_tag:
|
|
|
|
|
return ValueType::FunctionReference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::none_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::NoneReference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::noextern_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::NoExternReference;
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::nofunc_reference_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return ValueType::NoFunctionReference;
|
|
|
|
|
case Constants::noexn_reference_tag:
|
|
|
|
|
return ValueType::NoExceptionReference;
|
|
|
|
|
default:
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#heap-types
|
|
|
|
|
// NOTE: Nullable by default, caller must explicitly set nullability if needed.
|
|
|
|
|
static ParseResult<ValueType> parse_heap_type(Stream& stream)
|
|
|
|
|
{
|
|
|
|
|
auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
if (auto kind = abstract_heap_type_from_tag(tag); kind.has_value())
|
|
|
|
|
return ValueType(*kind);
|
|
|
|
|
|
|
|
|
|
ReconsumableStream new_stream { stream };
|
|
|
|
|
new_stream.unread({ &tag, 1 });
|
|
|
|
|
auto type_index = TRY(parse_signed_integer_of_size<33>(new_stream));
|
|
|
|
|
if (type_index < 0)
|
|
|
|
|
return with_eof_check(stream, ParseError::InvalidIndex);
|
|
|
|
|
if (type_index > NumericLimits<u32>::max())
|
|
|
|
|
return with_eof_check(stream, ParseError::InvalidIndex);
|
|
|
|
|
return ValueType(ValueType::TypeUseReference, TypeIndex(static_cast<u32>(type_index)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#reference-types
|
|
|
|
|
static ParseResult<ValueType> parse_reference_type(Stream& stream, u8 tag)
|
|
|
|
|
{
|
|
|
|
|
switch (tag) {
|
2025-10-14 12:21:33 -03:00
|
|
|
case Constants::nullable_reference_tag_tag:
|
2026-02-08 13:51:47 -03:00
|
|
|
case Constants::non_nullable_reference_tag_tag: {
|
2026-03-06 08:13:38 -03:00
|
|
|
bool nullable = tag == Constants::nullable_reference_tag_tag;
|
2026-06-11 01:14:58 -03:00
|
|
|
auto type = TRY(parse_heap_type(stream));
|
2026-03-06 08:12:51 -03:00
|
|
|
type.set_nullable(nullable);
|
|
|
|
|
return type;
|
2026-02-08 13:51:47 -03:00
|
|
|
}
|
2026-06-11 01:14:58 -03:00
|
|
|
default:
|
|
|
|
|
if (auto kind = abstract_heap_type_from_tag(tag); kind.has_value())
|
|
|
|
|
return ValueType(*kind);
|
|
|
|
|
return with_eof_check(stream, ParseError::InvalidType);
|
2026-02-08 13:51:47 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#value-types
|
2026-02-08 13:51:47 -03:00
|
|
|
ParseResult<ValueType> ValueType::parse(Stream& stream)
|
|
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ValueType"sv);
|
|
|
|
|
auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
|
case Constants::i32_tag:
|
|
|
|
|
return ValueType(I32);
|
|
|
|
|
case Constants::i64_tag:
|
|
|
|
|
return ValueType(I64);
|
|
|
|
|
case Constants::f32_tag:
|
|
|
|
|
return ValueType(F32);
|
|
|
|
|
case Constants::f64_tag:
|
|
|
|
|
return ValueType(F64);
|
|
|
|
|
case Constants::v128_tag:
|
|
|
|
|
return ValueType(V128);
|
2021-04-26 05:18:13 -03:00
|
|
|
default:
|
2026-02-08 13:51:47 -03:00
|
|
|
return parse_reference_type(stream, tag);
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#composite-types
|
|
|
|
|
static ParseResult<ValueType> parse_storage_type(Stream& stream)
|
|
|
|
|
{
|
|
|
|
|
auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
switch (tag) {
|
|
|
|
|
case Constants::i16_tag:
|
|
|
|
|
return ValueType(ValueType::I16);
|
|
|
|
|
case Constants::i8_tag:
|
|
|
|
|
return ValueType(ValueType::I8);
|
|
|
|
|
default: {
|
|
|
|
|
ReconsumableStream new_stream { stream };
|
|
|
|
|
new_stream.unread({ &tag, 1 });
|
|
|
|
|
return ValueType::parse(new_stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<ResultType> ResultType::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ResultType"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto types = TRY(parse_vector<ValueType>(stream));
|
|
|
|
|
return ResultType { types };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<FunctionType> FunctionType::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionType"sv);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-05-26 22:06:26 -03:00
|
|
|
auto parameters_result = TRY(parse_vector<ValueType>(stream));
|
|
|
|
|
auto results_result = TRY(parse_vector<ValueType>(stream));
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-05-26 22:06:26 -03:00
|
|
|
return FunctionType { parameters_result, results_result };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#composite-types
|
2026-02-02 13:44:20 -03:00
|
|
|
ParseResult<FieldType> FieldType::parse(ConstrainedStream& stream)
|
|
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("FieldType"sv);
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
auto type_ = TRY(parse_storage_type(stream));
|
2026-02-02 13:44:20 -03:00
|
|
|
|
|
|
|
|
auto mutable_ = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
if (mutable_ > 1)
|
|
|
|
|
return with_eof_check(stream, ParseError::InvalidTag);
|
|
|
|
|
|
|
|
|
|
return FieldType { mutable_ == 0x01, type_ };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseResult<StructType> StructType::parse(ConstrainedStream& stream)
|
|
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("StructType"sv);
|
|
|
|
|
|
|
|
|
|
auto fields = TRY(parse_vector<FieldType>(stream));
|
|
|
|
|
|
|
|
|
|
return StructType { fields };
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:09:18 -03:00
|
|
|
ParseResult<ArrayType> ArrayType::parse(ConstrainedStream& stream)
|
|
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ArrayType"sv);
|
|
|
|
|
|
|
|
|
|
auto type = TRY(FieldType::parse(stream));
|
|
|
|
|
|
|
|
|
|
return ArrayType { type };
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<Limits> Limits::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Limits"sv);
|
2024-07-27 21:21:19 -03:00
|
|
|
auto flag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
2023-01-21 07:44:19 -03:00
|
|
|
|
2025-09-23 00:52:12 -03:00
|
|
|
// Proposal 'memory64': flags 0/1 refer to 32-bit limits, flags 4/5 refer to 64-bit limits.
|
|
|
|
|
if (flag & ~0b00000101)
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::InvalidTag);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2025-09-23 00:52:12 -03:00
|
|
|
auto address_type = (flag & 0b00000100) ? AddressType::I64 : AddressType::I32;
|
|
|
|
|
|
2025-10-19 11:38:49 -03:00
|
|
|
auto min_or_error = stream.read_value<LEB128<u64>>();
|
2023-01-29 20:02:38 -03:00
|
|
|
if (min_or_error.is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::ExpectedSize);
|
2025-10-19 11:38:49 -03:00
|
|
|
u64 min = min_or_error.release_value();
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2025-09-23 00:52:12 -03:00
|
|
|
Optional<u64> max;
|
|
|
|
|
if (flag & 1) {
|
|
|
|
|
auto value_or_error = stream.read_value<LEB128<u64>>();
|
2023-01-29 20:02:38 -03:00
|
|
|
if (value_or_error.is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::ExpectedSize);
|
2023-01-29 20:02:38 -03:00
|
|
|
max = value_or_error.release_value();
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 11:38:49 -03:00
|
|
|
return Limits { address_type, min, move(max) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<MemoryType> MemoryType::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemoryType"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto limits_result = TRY(Limits::parse(stream));
|
|
|
|
|
return MemoryType { limits_result };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<TableType> TableType::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableType"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto type_result = TRY(ValueType::parse(stream));
|
|
|
|
|
if (!type_result.is_reference())
|
2024-07-27 21:21:19 -03:00
|
|
|
return ParseError::InvalidType;
|
2024-05-26 22:06:26 -03:00
|
|
|
auto limits_result = TRY(Limits::parse(stream));
|
|
|
|
|
return TableType { type_result, limits_result };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<GlobalType> GlobalType::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalType"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto type_result = TRY(ValueType::parse(stream));
|
2024-07-27 21:21:19 -03:00
|
|
|
auto mutable_ = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
2023-01-21 07:44:19 -03:00
|
|
|
|
2021-04-26 05:18:13 -03:00
|
|
|
if (mutable_ > 1)
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::InvalidTag);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-05-26 22:06:26 -03:00
|
|
|
return GlobalType { type_result, mutable_ == 0x01 };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-24 22:35:34 -03:00
|
|
|
ParseResult<TagType> TagType::parse(ConstrainedStream& stream)
|
|
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TagType"sv);
|
|
|
|
|
auto flags = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
if (flags != 0)
|
|
|
|
|
return ParseError::InvalidTag;
|
|
|
|
|
auto index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
return TagType { index, static_cast<TagType::Flags>(flags) };
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<BlockType> BlockType::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("BlockType"sv);
|
2024-07-27 21:21:19 -03:00
|
|
|
auto kind = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
2023-01-21 07:44:19 -03:00
|
|
|
|
2021-04-26 05:18:13 -03:00
|
|
|
if (kind == Constants::empty_block_tag)
|
|
|
|
|
return BlockType {};
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
switch (kind) {
|
|
|
|
|
case Constants::i32_tag:
|
|
|
|
|
case Constants::i64_tag:
|
|
|
|
|
case Constants::f32_tag:
|
|
|
|
|
case Constants::f64_tag:
|
|
|
|
|
case Constants::v128_tag:
|
|
|
|
|
case Constants::nullable_reference_tag_tag:
|
|
|
|
|
case Constants::non_nullable_reference_tag_tag: {
|
|
|
|
|
ReconsumableStream value_stream { stream };
|
|
|
|
|
value_stream.unread({ &kind, 1 });
|
|
|
|
|
return BlockType { TRY(ValueType::parse(value_stream)) };
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2021-05-01 12:26:45 -03:00
|
|
|
}
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
if (auto abstract_kind = abstract_heap_type_from_tag(kind); abstract_kind.has_value())
|
|
|
|
|
return BlockType { ValueType(*abstract_kind) };
|
|
|
|
|
|
|
|
|
|
ReconsumableStream index_stream { stream };
|
|
|
|
|
index_stream.unread({ &kind, 1 });
|
|
|
|
|
auto type_index = TRY(parse_signed_integer_of_size<33>(index_stream));
|
|
|
|
|
if (type_index < 0 || type_index > NumericLimits<u32>::max())
|
|
|
|
|
return with_eof_check(stream, ParseError::InvalidIndex);
|
|
|
|
|
return BlockType { TypeIndex(static_cast<u32>(type_index)) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-24 22:35:34 -03:00
|
|
|
ParseResult<Catch> Catch::parse(ConstrainedStream& stream)
|
|
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Catch"sv);
|
|
|
|
|
auto kind = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
switch (kind) {
|
|
|
|
|
case 0: {
|
|
|
|
|
// catch x l
|
|
|
|
|
auto tag_index = TRY(GenericIndexParser<TagIndex>::parse(stream));
|
|
|
|
|
auto label_index = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
|
|
|
|
return Catch { false, tag_index, label_index };
|
|
|
|
|
}
|
|
|
|
|
case 1: {
|
|
|
|
|
// catch_ref x l
|
|
|
|
|
auto tag_index = TRY(GenericIndexParser<TagIndex>::parse(stream));
|
|
|
|
|
auto label_index = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
|
|
|
|
return Catch { true, tag_index, label_index };
|
|
|
|
|
}
|
|
|
|
|
case 2: {
|
|
|
|
|
// catch_all l
|
|
|
|
|
auto label_index = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
2026-06-11 03:52:02 -03:00
|
|
|
return Catch { false, label_index };
|
2025-09-24 22:35:34 -03:00
|
|
|
}
|
|
|
|
|
case 3: {
|
|
|
|
|
// catch_all_ref l
|
|
|
|
|
auto label_index = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
2026-06-11 03:52:02 -03:00
|
|
|
return Catch { true, label_index };
|
2025-09-24 22:35:34 -03:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return ParseError::InvalidTag;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<Instruction> Instruction::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2024-07-24 14:48:31 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Instruction"sv);
|
2025-06-06 09:11:24 -03:00
|
|
|
u8 byte;
|
|
|
|
|
if (auto result = stream.read_some({ &byte, 1 }); result.is_error() || result.value().size() != 1)
|
|
|
|
|
return ParseError::ExpectedKindTag;
|
2022-11-22 14:38:59 -03:00
|
|
|
|
2024-07-24 14:48:31 -03:00
|
|
|
OpCode opcode { byte };
|
2022-11-22 14:38:59 -03:00
|
|
|
|
2024-07-24 14:48:31 -03:00
|
|
|
switch (opcode.value()) {
|
|
|
|
|
case Instructions::block.value():
|
|
|
|
|
case Instructions::loop.value():
|
|
|
|
|
case Instructions::if_.value(): {
|
|
|
|
|
auto block_type = TRY(BlockType::parse(stream));
|
|
|
|
|
return Instruction {
|
|
|
|
|
opcode, StructuredInstructionArgs { block_type, {}, {} }
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-09-24 22:35:34 -03:00
|
|
|
case Instructions::try_table.value(): {
|
|
|
|
|
// try_table block_type (catch*) (instruction*) end
|
|
|
|
|
auto block_type = TRY(BlockType::parse(stream));
|
|
|
|
|
auto catch_types = TRY(parse_vector<Catch>(stream));
|
|
|
|
|
return Instruction {
|
2026-05-14 05:35:51 -03:00
|
|
|
opcode, TryTableArgs { block_type, {}, catch_types.span() }
|
2025-09-24 22:35:34 -03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
case Instructions::throw_.value(): {
|
|
|
|
|
auto tag_index = TRY(GenericIndexParser<TagIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, tag_index };
|
|
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::br.value():
|
|
|
|
|
case Instructions::br_if.value(): {
|
|
|
|
|
// branches with a single label immediate
|
|
|
|
|
auto index = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
2026-01-23 08:36:20 -03:00
|
|
|
return Instruction { opcode, BranchArgs { index } };
|
2024-07-24 14:48:31 -03:00
|
|
|
}
|
2026-06-11 02:15:32 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions
|
|
|
|
|
// 0xD5 l:labelidx => br_on_null l
|
|
|
|
|
// 0xD6 l:labelidx => br_on_non_null l
|
|
|
|
|
case Instructions::br_on_null.value():
|
|
|
|
|
case Instructions::br_on_non_null.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, BranchArgs { index } };
|
|
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::br_table.value(): {
|
|
|
|
|
// br_table label* label
|
|
|
|
|
auto labels = TRY(parse_vector<GenericIndexParser<LabelIndex>>(stream));
|
|
|
|
|
auto default_label = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, TableBranchArgs { labels, default_label } };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::call.value(): {
|
|
|
|
|
// call function
|
|
|
|
|
auto function_index = TRY(GenericIndexParser<FunctionIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, function_index };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::call_indirect.value(): {
|
|
|
|
|
// call_indirect type table
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
auto table_index = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, IndirectCallArgs { type_index, table_index } };
|
|
|
|
|
}
|
2025-09-22 06:38:44 -03:00
|
|
|
case Instructions::return_call.value(): {
|
|
|
|
|
// return_call function
|
|
|
|
|
auto function_index = TRY(GenericIndexParser<FunctionIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, function_index };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::return_call_indirect.value(): {
|
|
|
|
|
// return_call_indirect type table
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
auto table_index = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, IndirectCallArgs { type_index, table_index } };
|
|
|
|
|
}
|
2026-03-06 08:13:38 -03:00
|
|
|
case Instructions::call_ref.value():
|
|
|
|
|
case Instructions::return_call_ref.value(): {
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, type_index };
|
|
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::i32_load.value():
|
|
|
|
|
case Instructions::i64_load.value():
|
|
|
|
|
case Instructions::f32_load.value():
|
|
|
|
|
case Instructions::f64_load.value():
|
|
|
|
|
case Instructions::i32_load8_s.value():
|
|
|
|
|
case Instructions::i32_load8_u.value():
|
|
|
|
|
case Instructions::i32_load16_s.value():
|
|
|
|
|
case Instructions::i32_load16_u.value():
|
|
|
|
|
case Instructions::i64_load8_s.value():
|
|
|
|
|
case Instructions::i64_load8_u.value():
|
|
|
|
|
case Instructions::i64_load16_s.value():
|
|
|
|
|
case Instructions::i64_load16_u.value():
|
|
|
|
|
case Instructions::i64_load32_s.value():
|
|
|
|
|
case Instructions::i64_load32_u.value():
|
|
|
|
|
case Instructions::i32_store.value():
|
|
|
|
|
case Instructions::i64_store.value():
|
|
|
|
|
case Instructions::f32_store.value():
|
|
|
|
|
case Instructions::f64_store.value():
|
|
|
|
|
case Instructions::i32_store8.value():
|
|
|
|
|
case Instructions::i32_store16.value():
|
|
|
|
|
case Instructions::i64_store8.value():
|
|
|
|
|
case Instructions::i64_store16.value():
|
|
|
|
|
case Instructions::i64_store32.value(): {
|
|
|
|
|
// op (align [multi-memory: memindex] offset)
|
2024-07-27 21:21:19 -03:00
|
|
|
u32 align = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
// Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment.
|
2024-07-27 21:21:19 -03:00
|
|
|
auto memory_index = 0;
|
2024-07-24 14:48:31 -03:00
|
|
|
if ((align & 0x40) != 0) {
|
|
|
|
|
align &= ~0x40;
|
2024-07-27 21:21:19 -03:00
|
|
|
memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2022-11-22 14:38:59 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 00:52:12 -03:00
|
|
|
// Proposal 'memory64': memarg offsets are u64 instead of u32.
|
|
|
|
|
auto offset = TRY_READ(stream, LEB128<u64>, ParseError::InvalidInput);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-07-27 21:21:19 -03:00
|
|
|
return Instruction { opcode, MemoryArgument { align, offset, MemoryIndex(memory_index) } };
|
2024-07-24 14:48:31 -03:00
|
|
|
}
|
|
|
|
|
case Instructions::local_get.value():
|
|
|
|
|
case Instructions::local_set.value():
|
|
|
|
|
case Instructions::local_tee.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<LocalIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, index };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::global_get.value():
|
|
|
|
|
case Instructions::global_set.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<GlobalIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, index };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::memory_size.value():
|
|
|
|
|
case Instructions::memory_grow.value(): {
|
|
|
|
|
// op [multi-memory: memindex]|0x00
|
2026-06-10 07:23:41 -03:00
|
|
|
u32 memory_index = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedKindTag);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
return Instruction { opcode, MemoryIndexArgument { MemoryIndex(memory_index) } };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::i32_const.value(): {
|
2024-07-27 21:21:19 -03:00
|
|
|
auto value = TRY_READ(stream, LEB128<i32>, ParseError::ExpectedSignedImmediate);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
return Instruction { opcode, value };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::i64_const.value(): {
|
|
|
|
|
// op literal
|
2024-07-27 21:21:19 -03:00
|
|
|
auto value = TRY_READ(stream, LEB128<i64>, ParseError::ExpectedSignedImmediate);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
return Instruction { opcode, value };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::f32_const.value(): {
|
|
|
|
|
// op literal
|
2024-07-27 21:21:19 -03:00
|
|
|
auto value = TRY_READ(stream, LittleEndian<u32>, ParseError::ExpectedFloatingImmediate);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
auto floating = bit_cast<float>(static_cast<u32>(value));
|
|
|
|
|
return Instruction { opcode, floating };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::f64_const.value(): {
|
|
|
|
|
// op literal
|
2024-07-27 21:21:19 -03:00
|
|
|
auto value = TRY_READ(stream, LittleEndian<u64>, ParseError::ExpectedFloatingImmediate);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
auto floating = bit_cast<double>(static_cast<u64>(value));
|
|
|
|
|
return Instruction { opcode, floating };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::table_get.value():
|
|
|
|
|
case Instructions::table_set.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, index };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::select_typed.value(): {
|
|
|
|
|
auto types = TRY(parse_vector<ValueType>(stream));
|
|
|
|
|
return Instruction { opcode, types };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::ref_null.value(): {
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#reference-instructions
|
|
|
|
|
auto type = TRY(parse_heap_type(stream));
|
2024-07-24 14:48:31 -03:00
|
|
|
return Instruction { opcode, type };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::ref_func.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<FunctionIndex>::parse(stream));
|
|
|
|
|
return Instruction { opcode, index };
|
|
|
|
|
}
|
2025-09-24 22:35:34 -03:00
|
|
|
case Instructions::throw_ref.value():
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::structured_end.value():
|
|
|
|
|
case Instructions::structured_else.value():
|
|
|
|
|
case Instructions::ref_is_null.value():
|
2026-06-11 02:15:32 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#reference-instructions
|
|
|
|
|
// 0xD3 => ref.eq
|
|
|
|
|
// 0xD4 => ref.as_non_null
|
|
|
|
|
case Instructions::ref_eq.value():
|
|
|
|
|
case Instructions::ref_as_non_null.value():
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::unreachable.value():
|
|
|
|
|
case Instructions::nop.value():
|
|
|
|
|
case Instructions::return_.value():
|
|
|
|
|
case Instructions::drop.value():
|
|
|
|
|
case Instructions::select.value():
|
|
|
|
|
case Instructions::i32_eqz.value():
|
|
|
|
|
case Instructions::i32_eq.value():
|
|
|
|
|
case Instructions::i32_ne.value():
|
|
|
|
|
case Instructions::i32_lts.value():
|
|
|
|
|
case Instructions::i32_ltu.value():
|
|
|
|
|
case Instructions::i32_gts.value():
|
|
|
|
|
case Instructions::i32_gtu.value():
|
|
|
|
|
case Instructions::i32_les.value():
|
|
|
|
|
case Instructions::i32_leu.value():
|
|
|
|
|
case Instructions::i32_ges.value():
|
|
|
|
|
case Instructions::i32_geu.value():
|
|
|
|
|
case Instructions::i64_eqz.value():
|
|
|
|
|
case Instructions::i64_eq.value():
|
|
|
|
|
case Instructions::i64_ne.value():
|
|
|
|
|
case Instructions::i64_lts.value():
|
|
|
|
|
case Instructions::i64_ltu.value():
|
|
|
|
|
case Instructions::i64_gts.value():
|
|
|
|
|
case Instructions::i64_gtu.value():
|
|
|
|
|
case Instructions::i64_les.value():
|
|
|
|
|
case Instructions::i64_leu.value():
|
|
|
|
|
case Instructions::i64_ges.value():
|
|
|
|
|
case Instructions::i64_geu.value():
|
|
|
|
|
case Instructions::f32_eq.value():
|
|
|
|
|
case Instructions::f32_ne.value():
|
|
|
|
|
case Instructions::f32_lt.value():
|
|
|
|
|
case Instructions::f32_gt.value():
|
|
|
|
|
case Instructions::f32_le.value():
|
|
|
|
|
case Instructions::f32_ge.value():
|
|
|
|
|
case Instructions::f64_eq.value():
|
|
|
|
|
case Instructions::f64_ne.value():
|
|
|
|
|
case Instructions::f64_lt.value():
|
|
|
|
|
case Instructions::f64_gt.value():
|
|
|
|
|
case Instructions::f64_le.value():
|
|
|
|
|
case Instructions::f64_ge.value():
|
|
|
|
|
case Instructions::i32_clz.value():
|
|
|
|
|
case Instructions::i32_ctz.value():
|
|
|
|
|
case Instructions::i32_popcnt.value():
|
|
|
|
|
case Instructions::i32_add.value():
|
|
|
|
|
case Instructions::i32_sub.value():
|
|
|
|
|
case Instructions::i32_mul.value():
|
|
|
|
|
case Instructions::i32_divs.value():
|
|
|
|
|
case Instructions::i32_divu.value():
|
|
|
|
|
case Instructions::i32_rems.value():
|
|
|
|
|
case Instructions::i32_remu.value():
|
|
|
|
|
case Instructions::i32_and.value():
|
|
|
|
|
case Instructions::i32_or.value():
|
|
|
|
|
case Instructions::i32_xor.value():
|
|
|
|
|
case Instructions::i32_shl.value():
|
|
|
|
|
case Instructions::i32_shrs.value():
|
|
|
|
|
case Instructions::i32_shru.value():
|
|
|
|
|
case Instructions::i32_rotl.value():
|
|
|
|
|
case Instructions::i32_rotr.value():
|
|
|
|
|
case Instructions::i64_clz.value():
|
|
|
|
|
case Instructions::i64_ctz.value():
|
|
|
|
|
case Instructions::i64_popcnt.value():
|
|
|
|
|
case Instructions::i64_add.value():
|
|
|
|
|
case Instructions::i64_sub.value():
|
|
|
|
|
case Instructions::i64_mul.value():
|
|
|
|
|
case Instructions::i64_divs.value():
|
|
|
|
|
case Instructions::i64_divu.value():
|
|
|
|
|
case Instructions::i64_rems.value():
|
|
|
|
|
case Instructions::i64_remu.value():
|
|
|
|
|
case Instructions::i64_and.value():
|
|
|
|
|
case Instructions::i64_or.value():
|
|
|
|
|
case Instructions::i64_xor.value():
|
|
|
|
|
case Instructions::i64_shl.value():
|
|
|
|
|
case Instructions::i64_shrs.value():
|
|
|
|
|
case Instructions::i64_shru.value():
|
|
|
|
|
case Instructions::i64_rotl.value():
|
|
|
|
|
case Instructions::i64_rotr.value():
|
|
|
|
|
case Instructions::f32_abs.value():
|
|
|
|
|
case Instructions::f32_neg.value():
|
|
|
|
|
case Instructions::f32_ceil.value():
|
|
|
|
|
case Instructions::f32_floor.value():
|
|
|
|
|
case Instructions::f32_trunc.value():
|
|
|
|
|
case Instructions::f32_nearest.value():
|
|
|
|
|
case Instructions::f32_sqrt.value():
|
|
|
|
|
case Instructions::f32_add.value():
|
|
|
|
|
case Instructions::f32_sub.value():
|
|
|
|
|
case Instructions::f32_mul.value():
|
|
|
|
|
case Instructions::f32_div.value():
|
|
|
|
|
case Instructions::f32_min.value():
|
|
|
|
|
case Instructions::f32_max.value():
|
|
|
|
|
case Instructions::f32_copysign.value():
|
|
|
|
|
case Instructions::f64_abs.value():
|
|
|
|
|
case Instructions::f64_neg.value():
|
|
|
|
|
case Instructions::f64_ceil.value():
|
|
|
|
|
case Instructions::f64_floor.value():
|
|
|
|
|
case Instructions::f64_trunc.value():
|
|
|
|
|
case Instructions::f64_nearest.value():
|
|
|
|
|
case Instructions::f64_sqrt.value():
|
|
|
|
|
case Instructions::f64_add.value():
|
|
|
|
|
case Instructions::f64_sub.value():
|
|
|
|
|
case Instructions::f64_mul.value():
|
|
|
|
|
case Instructions::f64_div.value():
|
|
|
|
|
case Instructions::f64_min.value():
|
|
|
|
|
case Instructions::f64_max.value():
|
|
|
|
|
case Instructions::f64_copysign.value():
|
|
|
|
|
case Instructions::i32_wrap_i64.value():
|
|
|
|
|
case Instructions::i32_trunc_sf32.value():
|
|
|
|
|
case Instructions::i32_trunc_uf32.value():
|
|
|
|
|
case Instructions::i32_trunc_sf64.value():
|
|
|
|
|
case Instructions::i32_trunc_uf64.value():
|
|
|
|
|
case Instructions::i64_extend_si32.value():
|
|
|
|
|
case Instructions::i64_extend_ui32.value():
|
|
|
|
|
case Instructions::i64_trunc_sf32.value():
|
|
|
|
|
case Instructions::i64_trunc_uf32.value():
|
|
|
|
|
case Instructions::i64_trunc_sf64.value():
|
|
|
|
|
case Instructions::i64_trunc_uf64.value():
|
|
|
|
|
case Instructions::f32_convert_si32.value():
|
|
|
|
|
case Instructions::f32_convert_ui32.value():
|
|
|
|
|
case Instructions::f32_convert_si64.value():
|
|
|
|
|
case Instructions::f32_convert_ui64.value():
|
|
|
|
|
case Instructions::f32_demote_f64.value():
|
|
|
|
|
case Instructions::f64_convert_si32.value():
|
|
|
|
|
case Instructions::f64_convert_ui32.value():
|
|
|
|
|
case Instructions::f64_convert_si64.value():
|
|
|
|
|
case Instructions::f64_convert_ui64.value():
|
|
|
|
|
case Instructions::f64_promote_f32.value():
|
|
|
|
|
case Instructions::i32_reinterpret_f32.value():
|
|
|
|
|
case Instructions::i64_reinterpret_f64.value():
|
|
|
|
|
case Instructions::f32_reinterpret_i32.value():
|
|
|
|
|
case Instructions::f64_reinterpret_i64.value():
|
|
|
|
|
case Instructions::i32_extend8_s.value():
|
|
|
|
|
case Instructions::i32_extend16_s.value():
|
|
|
|
|
case Instructions::i64_extend8_s.value():
|
|
|
|
|
case Instructions::i64_extend16_s.value():
|
|
|
|
|
case Instructions::i64_extend32_s.value():
|
|
|
|
|
return Instruction { opcode };
|
2026-06-11 02:15:32 -03:00
|
|
|
case 0xfb:
|
2024-07-24 14:48:31 -03:00
|
|
|
case 0xfc:
|
|
|
|
|
case 0xfd: {
|
|
|
|
|
// These are multibyte instructions.
|
2024-07-27 21:21:19 -03:00
|
|
|
auto selector = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2026-05-14 05:16:22 -03:00
|
|
|
if (selector > 0xffffff)
|
|
|
|
|
return ParseError::UnknownInstruction;
|
|
|
|
|
|
|
|
|
|
OpCode full_opcode = static_cast<u32>(opcode.value()) << 24 | selector;
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
switch (full_opcode.value()) {
|
|
|
|
|
case Instructions::i32_trunc_sat_f32_s.value():
|
|
|
|
|
case Instructions::i32_trunc_sat_f32_u.value():
|
|
|
|
|
case Instructions::i32_trunc_sat_f64_s.value():
|
|
|
|
|
case Instructions::i32_trunc_sat_f64_u.value():
|
|
|
|
|
case Instructions::i64_trunc_sat_f32_s.value():
|
|
|
|
|
case Instructions::i64_trunc_sat_f32_u.value():
|
|
|
|
|
case Instructions::i64_trunc_sat_f64_s.value():
|
|
|
|
|
case Instructions::i64_trunc_sat_f64_u.value():
|
|
|
|
|
return Instruction { full_opcode };
|
|
|
|
|
case Instructions::memory_init.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<DataIndex>::parse(stream));
|
|
|
|
|
|
2026-06-10 07:23:41 -03:00
|
|
|
// Proposal "multi-memory", literal 0x00 is replaced with a memory index (LEB-128).
|
|
|
|
|
u32 memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
|
|
|
|
return Instruction { full_opcode, MemoryInitArgs { index, MemoryIndex(memory_index) } };
|
2021-04-27 10:29:21 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::data_drop.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<DataIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, index };
|
2021-04-27 10:29:21 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::memory_copy.value(): {
|
2026-06-10 07:23:41 -03:00
|
|
|
// Proposal "multi-memory", literal 0x00 is replaced with two memory indices (LEB-128), destination and source, respectively.
|
2024-07-24 14:48:31 -03:00
|
|
|
MemoryIndex indices[] = { 0, 0 };
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 2; ++i) {
|
2026-06-10 07:23:41 -03:00
|
|
|
u32 memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2024-07-27 21:21:19 -03:00
|
|
|
indices[i] = memory_index;
|
2024-07-24 14:48:31 -03:00
|
|
|
}
|
|
|
|
|
return Instruction { full_opcode, MemoryCopyArgs { indices[1], indices[0] } };
|
2022-11-22 14:38:59 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::memory_fill.value(): {
|
2026-06-10 07:23:41 -03:00
|
|
|
// Proposal "multi-memory", literal 0x00 is replaced with a memory index (LEB-128).
|
|
|
|
|
u32 memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2024-07-24 14:48:31 -03:00
|
|
|
return Instruction { full_opcode, MemoryIndexArgument { MemoryIndex { memory_index } } };
|
2022-11-22 14:38:59 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::table_init.value(): {
|
|
|
|
|
auto element_index = TRY(GenericIndexParser<ElementIndex>::parse(stream));
|
2024-05-26 22:06:26 -03:00
|
|
|
auto table_index = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
2024-07-24 14:48:31 -03:00
|
|
|
return Instruction { full_opcode, TableElementArgs { element_index, table_index } };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::elem_drop.value(): {
|
|
|
|
|
auto element_index = TRY(GenericIndexParser<ElementIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, element_index };
|
2022-11-22 14:38:59 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::table_copy.value(): {
|
|
|
|
|
auto lhs = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
|
|
|
|
auto rhs = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, TableTableArgs { lhs, rhs } };
|
|
|
|
|
}
|
|
|
|
|
case Instructions::table_grow.value():
|
|
|
|
|
case Instructions::table_size.value():
|
|
|
|
|
case Instructions::table_fill.value(): {
|
|
|
|
|
auto index = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, index };
|
|
|
|
|
}
|
2026-06-11 02:15:32 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#aggregate-instructions
|
|
|
|
|
// instr ::= ...
|
|
|
|
|
// | 0xFB 0:u32 x:typeidx => struct.new x
|
|
|
|
|
// | 0xFB 1:u32 x:typeidx => struct.new_default x
|
|
|
|
|
case Instructions::struct_new.value():
|
|
|
|
|
case Instructions::struct_new_default.value(): {
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, type_index };
|
|
|
|
|
}
|
|
|
|
|
// | 0xFB 2:u32 x:typeidx i:fieldidx => struct.get x i
|
|
|
|
|
// | 0xFB 3:u32 x:typeidx i:fieldidx => struct.get_s x i
|
|
|
|
|
// | 0xFB 4:u32 x:typeidx i:fieldidx => struct.get_u x i
|
|
|
|
|
// | 0xFB 5:u32 x:typeidx i:fieldidx => struct.set x i
|
|
|
|
|
case Instructions::struct_get.value():
|
|
|
|
|
case Instructions::struct_get_s.value():
|
|
|
|
|
case Instructions::struct_get_u.value():
|
|
|
|
|
case Instructions::struct_set.value(): {
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
u32 field_index = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
|
|
|
|
|
return Instruction { full_opcode, StructFieldArgs { type_index, field_index } };
|
|
|
|
|
}
|
|
|
|
|
// | 0xFB 6:u32 x:typeidx => array.new x
|
|
|
|
|
// | 0xFB 7:u32 x:typeidx => array.new_default x
|
|
|
|
|
// | 0xFB 11:u32 x:typeidx => array.get x
|
|
|
|
|
// | 0xFB 12:u32 x:typeidx => array.get_s x
|
|
|
|
|
// | 0xFB 13:u32 x:typeidx => array.get_u x
|
|
|
|
|
// | 0xFB 14:u32 x:typeidx => array.set x
|
|
|
|
|
// | 0xFB 16:u32 x:typeidx => array.fill x
|
|
|
|
|
case Instructions::array_new.value():
|
|
|
|
|
case Instructions::array_new_default.value():
|
|
|
|
|
case Instructions::array_get.value():
|
|
|
|
|
case Instructions::array_get_s.value():
|
|
|
|
|
case Instructions::array_get_u.value():
|
|
|
|
|
case Instructions::array_set.value():
|
|
|
|
|
case Instructions::array_fill.value(): {
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, type_index };
|
|
|
|
|
}
|
|
|
|
|
// | 0xFB 8:u32 x:typeidx n:u32 => array.new_fixed x n
|
|
|
|
|
case Instructions::array_new_fixed.value(): {
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
u32 count = TRY_READ(stream, LEB128<u32>, ParseError::InvalidImmediate);
|
|
|
|
|
return Instruction { full_opcode, ArrayNewFixedArgs { type_index, count } };
|
|
|
|
|
}
|
|
|
|
|
// | 0xFB 9:u32 x:typeidx y:dataidx => array.new_data x y
|
|
|
|
|
// | 0xFB 18:u32 x:typeidx y:dataidx => array.init_data x y
|
|
|
|
|
case Instructions::array_new_data.value():
|
|
|
|
|
case Instructions::array_init_data.value(): {
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
auto data_index = TRY(GenericIndexParser<DataIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, ArrayDataArgs { type_index, data_index } };
|
|
|
|
|
}
|
|
|
|
|
// | 0xFB 10:u32 x:typeidx y:elemidx => array.new_elem x y
|
|
|
|
|
// | 0xFB 19:u32 x:typeidx y:elemidx => array.init_elem x y
|
|
|
|
|
case Instructions::array_new_elem.value():
|
|
|
|
|
case Instructions::array_init_elem.value(): {
|
|
|
|
|
auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
auto element_index = TRY(GenericIndexParser<ElementIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, ArrayElemArgs { type_index, element_index } };
|
|
|
|
|
}
|
|
|
|
|
// | 0xFB 17:u32 x_1:typeidx x_2:typeidx => array.copy x_1 x_2
|
|
|
|
|
case Instructions::array_copy.value(): {
|
|
|
|
|
auto destination_type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
auto source_type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
return Instruction { full_opcode, ArrayCopyArgs { destination_type_index, source_type_index } };
|
|
|
|
|
}
|
|
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#reference-instructions
|
|
|
|
|
// instr ::= ...
|
|
|
|
|
// | 0xFB 20:u32 ht:heaptype => ref.test (ref ht)
|
|
|
|
|
// | 0xFB 21:u32 ht:heaptype => ref.test (ref null ht)
|
|
|
|
|
// | 0xFB 22:u32 ht:heaptype => ref.cast (ref ht)
|
|
|
|
|
// | 0xFB 23:u32 ht:heaptype => ref.cast (ref null ht)
|
|
|
|
|
case Instructions::ref_test.value():
|
|
|
|
|
case Instructions::ref_test_null.value():
|
|
|
|
|
case Instructions::ref_cast.value():
|
|
|
|
|
case Instructions::ref_cast_null.value(): {
|
|
|
|
|
auto type = TRY(parse_heap_type(stream));
|
|
|
|
|
type.set_nullable(full_opcode == Instructions::ref_test_null || full_opcode == Instructions::ref_cast_null);
|
|
|
|
|
return Instruction { full_opcode, type };
|
|
|
|
|
}
|
|
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions
|
|
|
|
|
// instr ::= ...
|
|
|
|
|
// | 0xFB 24:u32 (null_1?, null_2?):castop l:labelidx ht_1:heaptype ht_2:heaptype
|
|
|
|
|
// => br_on_cast l (ref null_1? ht_1) (ref null_2? ht_2)
|
|
|
|
|
// | 0xFB 25:u32 (null_1?, null_2?):castop l:labelidx ht_1:heaptype ht_2:heaptype
|
|
|
|
|
// => br_on_cast_fail l (ref null_1? ht_1) (ref null_2? ht_2)
|
|
|
|
|
// castop ::= 0x00 => (ε, ε)
|
|
|
|
|
// | 0x01 => (null, ε)
|
|
|
|
|
// | 0x02 => (ε, null)
|
|
|
|
|
// | 0x03 => (null, null)
|
|
|
|
|
case Instructions::br_on_cast.value():
|
|
|
|
|
case Instructions::br_on_cast_fail.value(): {
|
|
|
|
|
auto cast_op = TRY_READ(stream, u8, ParseError::InvalidImmediate);
|
|
|
|
|
if (cast_op > 3)
|
|
|
|
|
return ParseError::InvalidImmediate;
|
|
|
|
|
auto label = TRY(GenericIndexParser<LabelIndex>::parse(stream));
|
|
|
|
|
auto source_type = TRY(parse_heap_type(stream));
|
|
|
|
|
source_type.set_nullable((cast_op & 0b01) != 0);
|
|
|
|
|
auto target_type = TRY(parse_heap_type(stream));
|
|
|
|
|
target_type.set_nullable((cast_op & 0b10) != 0);
|
|
|
|
|
return Instruction { full_opcode, BranchOnCastArgs { BranchArgs { label }, source_type, target_type } };
|
|
|
|
|
}
|
|
|
|
|
// https://webassembly.github.io/spec/core/binary/instructions.html#aggregate-instructions
|
|
|
|
|
// instr ::= ...
|
|
|
|
|
// | 0xFB 15:u32 => array.len
|
|
|
|
|
// | 0xFB 26:u32 => any.convert_extern
|
|
|
|
|
// | 0xFB 27:u32 => extern.convert_any
|
|
|
|
|
// | 0xFB 28:u32 => ref.i31
|
|
|
|
|
// | 0xFB 29:u32 => i31.get_s
|
|
|
|
|
// | 0xFB 30:u32 => i31.get_u
|
|
|
|
|
case Instructions::array_len.value():
|
|
|
|
|
case Instructions::any_convert_extern.value():
|
|
|
|
|
case Instructions::extern_convert_any.value():
|
|
|
|
|
case Instructions::ref_i31.value():
|
|
|
|
|
case Instructions::i31_get_s.value():
|
|
|
|
|
case Instructions::i31_get_u.value():
|
|
|
|
|
return Instruction { full_opcode };
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::v128_load.value():
|
|
|
|
|
case Instructions::v128_load8x8_s.value():
|
|
|
|
|
case Instructions::v128_load8x8_u.value():
|
|
|
|
|
case Instructions::v128_load16x4_s.value():
|
|
|
|
|
case Instructions::v128_load16x4_u.value():
|
|
|
|
|
case Instructions::v128_load32x2_s.value():
|
|
|
|
|
case Instructions::v128_load32x2_u.value():
|
|
|
|
|
case Instructions::v128_load8_splat.value():
|
|
|
|
|
case Instructions::v128_load16_splat.value():
|
|
|
|
|
case Instructions::v128_load32_splat.value():
|
|
|
|
|
case Instructions::v128_load64_splat.value():
|
|
|
|
|
case Instructions::v128_load32_zero.value():
|
|
|
|
|
case Instructions::v128_load64_zero.value():
|
|
|
|
|
case Instructions::v128_store.value(): {
|
|
|
|
|
// op (align [multi-memory memindex] offset)
|
2024-07-27 21:21:19 -03:00
|
|
|
u32 align = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
|
2021-04-27 10:29:21 -03:00
|
|
|
|
2023-10-22 17:49:28 -03:00
|
|
|
// Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment.
|
2024-07-27 21:21:19 -03:00
|
|
|
auto memory_index = 0;
|
2025-09-06 00:02:27 -03:00
|
|
|
if ((align & 0x40) != 0) {
|
|
|
|
|
align &= ~0x40;
|
2024-07-27 21:21:19 -03:00
|
|
|
memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2023-10-22 17:49:28 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 00:52:12 -03:00
|
|
|
// Proposal 'memory64': memarg offsets are u64 instead of u32.
|
|
|
|
|
auto offset = TRY_READ(stream, LEB128<u64>, ParseError::ExpectedIndex);
|
2021-04-27 10:29:21 -03:00
|
|
|
|
2024-07-27 21:21:19 -03:00
|
|
|
return Instruction { full_opcode, MemoryArgument { align, offset, MemoryIndex(memory_index) } };
|
2022-11-22 14:38:59 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::v128_load8_lane.value():
|
|
|
|
|
case Instructions::v128_load16_lane.value():
|
|
|
|
|
case Instructions::v128_load32_lane.value():
|
|
|
|
|
case Instructions::v128_load64_lane.value():
|
|
|
|
|
case Instructions::v128_store8_lane.value():
|
|
|
|
|
case Instructions::v128_store16_lane.value():
|
|
|
|
|
case Instructions::v128_store32_lane.value():
|
|
|
|
|
case Instructions::v128_store64_lane.value(): {
|
|
|
|
|
// op (align [multi-memory: memindex] offset) (index)
|
2024-07-27 21:21:19 -03:00
|
|
|
u32 align = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
|
2021-04-27 10:29:21 -03:00
|
|
|
|
2024-07-24 14:48:31 -03:00
|
|
|
// Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment.
|
2024-07-27 21:21:19 -03:00
|
|
|
auto memory_index = 0;
|
2025-09-06 00:02:27 -03:00
|
|
|
if ((align & 0x40) != 0) {
|
|
|
|
|
align &= ~0x40;
|
2024-07-27 21:21:19 -03:00
|
|
|
memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
|
2024-07-24 14:48:31 -03:00
|
|
|
}
|
2021-04-27 10:29:21 -03:00
|
|
|
|
2025-09-23 00:52:12 -03:00
|
|
|
// Proposal 'memory64': memarg offsets are u64 instead of u32.
|
|
|
|
|
auto offset = TRY_READ(stream, LEB128<u64>, ParseError::ExpectedIndex);
|
2024-07-27 21:21:19 -03:00
|
|
|
auto index = TRY_READ(stream, u8, ParseError::InvalidInput);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-07-27 21:21:19 -03:00
|
|
|
return Instruction { full_opcode, MemoryAndLaneArgument { { align, offset, MemoryIndex(memory_index) }, index } };
|
2022-11-22 14:38:59 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::v128_const.value(): {
|
|
|
|
|
// op (literal:16)
|
2024-07-27 21:21:19 -03:00
|
|
|
auto value = TRY_READ(stream, LittleEndian<u128>, ParseError::InvalidImmediate);
|
|
|
|
|
return Instruction { full_opcode, value };
|
2022-11-22 14:38:59 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::i8x16_shuffle.value(): {
|
|
|
|
|
// op 16x(lane)
|
|
|
|
|
u8 lanes[16];
|
|
|
|
|
for (size_t i = 0; i < 16; ++i) {
|
2024-07-27 21:21:19 -03:00
|
|
|
auto value = TRY_READ(stream, u8, ParseError::InvalidInput);
|
|
|
|
|
lanes[i] = value;
|
2021-04-27 10:29:21 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
return Instruction { full_opcode, ShuffleArgument(lanes) };
|
2021-04-27 10:29:21 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::i8x16_extract_lane_s.value():
|
|
|
|
|
case Instructions::i8x16_extract_lane_u.value():
|
|
|
|
|
case Instructions::i8x16_replace_lane.value():
|
|
|
|
|
case Instructions::i16x8_extract_lane_s.value():
|
|
|
|
|
case Instructions::i16x8_extract_lane_u.value():
|
|
|
|
|
case Instructions::i16x8_replace_lane.value():
|
|
|
|
|
case Instructions::i32x4_extract_lane.value():
|
|
|
|
|
case Instructions::i32x4_replace_lane.value():
|
|
|
|
|
case Instructions::i64x2_extract_lane.value():
|
|
|
|
|
case Instructions::i64x2_replace_lane.value():
|
|
|
|
|
case Instructions::f32x4_extract_lane.value():
|
|
|
|
|
case Instructions::f32x4_replace_lane.value():
|
|
|
|
|
case Instructions::f64x2_extract_lane.value():
|
|
|
|
|
case Instructions::f64x2_replace_lane.value(): {
|
|
|
|
|
// op (lane)
|
2024-07-27 21:21:19 -03:00
|
|
|
auto lane = TRY_READ(stream, u8, ParseError::InvalidInput);
|
2024-07-24 14:48:31 -03:00
|
|
|
return Instruction { full_opcode, LaneIndex { lane } };
|
2021-04-27 10:29:21 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
case Instructions::i8x16_swizzle.value():
|
|
|
|
|
case Instructions::i8x16_splat.value():
|
|
|
|
|
case Instructions::i16x8_splat.value():
|
|
|
|
|
case Instructions::i32x4_splat.value():
|
|
|
|
|
case Instructions::i64x2_splat.value():
|
|
|
|
|
case Instructions::f32x4_splat.value():
|
|
|
|
|
case Instructions::f64x2_splat.value():
|
|
|
|
|
case Instructions::i8x16_eq.value():
|
|
|
|
|
case Instructions::i8x16_ne.value():
|
|
|
|
|
case Instructions::i8x16_lt_s.value():
|
|
|
|
|
case Instructions::i8x16_lt_u.value():
|
|
|
|
|
case Instructions::i8x16_gt_s.value():
|
|
|
|
|
case Instructions::i8x16_gt_u.value():
|
|
|
|
|
case Instructions::i8x16_le_s.value():
|
|
|
|
|
case Instructions::i8x16_le_u.value():
|
|
|
|
|
case Instructions::i8x16_ge_s.value():
|
|
|
|
|
case Instructions::i8x16_ge_u.value():
|
|
|
|
|
case Instructions::i16x8_eq.value():
|
|
|
|
|
case Instructions::i16x8_ne.value():
|
|
|
|
|
case Instructions::i16x8_lt_s.value():
|
|
|
|
|
case Instructions::i16x8_lt_u.value():
|
|
|
|
|
case Instructions::i16x8_gt_s.value():
|
|
|
|
|
case Instructions::i16x8_gt_u.value():
|
|
|
|
|
case Instructions::i16x8_le_s.value():
|
|
|
|
|
case Instructions::i16x8_le_u.value():
|
|
|
|
|
case Instructions::i16x8_ge_s.value():
|
|
|
|
|
case Instructions::i16x8_ge_u.value():
|
|
|
|
|
case Instructions::i32x4_eq.value():
|
|
|
|
|
case Instructions::i32x4_ne.value():
|
|
|
|
|
case Instructions::i32x4_lt_s.value():
|
|
|
|
|
case Instructions::i32x4_lt_u.value():
|
|
|
|
|
case Instructions::i32x4_gt_s.value():
|
|
|
|
|
case Instructions::i32x4_gt_u.value():
|
|
|
|
|
case Instructions::i32x4_le_s.value():
|
|
|
|
|
case Instructions::i32x4_le_u.value():
|
|
|
|
|
case Instructions::i32x4_ge_s.value():
|
|
|
|
|
case Instructions::i32x4_ge_u.value():
|
|
|
|
|
case Instructions::f32x4_eq.value():
|
|
|
|
|
case Instructions::f32x4_ne.value():
|
|
|
|
|
case Instructions::f32x4_lt.value():
|
|
|
|
|
case Instructions::f32x4_gt.value():
|
|
|
|
|
case Instructions::f32x4_le.value():
|
|
|
|
|
case Instructions::f32x4_ge.value():
|
|
|
|
|
case Instructions::f64x2_eq.value():
|
|
|
|
|
case Instructions::f64x2_ne.value():
|
|
|
|
|
case Instructions::f64x2_lt.value():
|
|
|
|
|
case Instructions::f64x2_gt.value():
|
|
|
|
|
case Instructions::f64x2_le.value():
|
|
|
|
|
case Instructions::f64x2_ge.value():
|
|
|
|
|
case Instructions::v128_not.value():
|
|
|
|
|
case Instructions::v128_and.value():
|
|
|
|
|
case Instructions::v128_andnot.value():
|
|
|
|
|
case Instructions::v128_or.value():
|
|
|
|
|
case Instructions::v128_xor.value():
|
|
|
|
|
case Instructions::v128_bitselect.value():
|
|
|
|
|
case Instructions::v128_any_true.value():
|
|
|
|
|
case Instructions::f32x4_demote_f64x2_zero.value():
|
|
|
|
|
case Instructions::f64x2_promote_low_f32x4.value():
|
|
|
|
|
case Instructions::i8x16_abs.value():
|
|
|
|
|
case Instructions::i8x16_neg.value():
|
|
|
|
|
case Instructions::i8x16_popcnt.value():
|
|
|
|
|
case Instructions::i8x16_all_true.value():
|
|
|
|
|
case Instructions::i8x16_bitmask.value():
|
|
|
|
|
case Instructions::i8x16_narrow_i16x8_s.value():
|
|
|
|
|
case Instructions::i8x16_narrow_i16x8_u.value():
|
|
|
|
|
case Instructions::f32x4_ceil.value():
|
|
|
|
|
case Instructions::f32x4_floor.value():
|
|
|
|
|
case Instructions::f32x4_trunc.value():
|
|
|
|
|
case Instructions::f32x4_nearest.value():
|
|
|
|
|
case Instructions::i8x16_shl.value():
|
|
|
|
|
case Instructions::i8x16_shr_s.value():
|
|
|
|
|
case Instructions::i8x16_shr_u.value():
|
|
|
|
|
case Instructions::i8x16_add.value():
|
|
|
|
|
case Instructions::i8x16_add_sat_s.value():
|
|
|
|
|
case Instructions::i8x16_add_sat_u.value():
|
|
|
|
|
case Instructions::i8x16_sub.value():
|
|
|
|
|
case Instructions::i8x16_sub_sat_s.value():
|
|
|
|
|
case Instructions::i8x16_sub_sat_u.value():
|
|
|
|
|
case Instructions::f64x2_ceil.value():
|
|
|
|
|
case Instructions::f64x2_floor.value():
|
|
|
|
|
case Instructions::i8x16_min_s.value():
|
|
|
|
|
case Instructions::i8x16_min_u.value():
|
|
|
|
|
case Instructions::i8x16_max_s.value():
|
|
|
|
|
case Instructions::i8x16_max_u.value():
|
|
|
|
|
case Instructions::f64x2_trunc.value():
|
|
|
|
|
case Instructions::i8x16_avgr_u.value():
|
|
|
|
|
case Instructions::i16x8_extadd_pairwise_i8x16_s.value():
|
|
|
|
|
case Instructions::i16x8_extadd_pairwise_i8x16_u.value():
|
|
|
|
|
case Instructions::i32x4_extadd_pairwise_i16x8_s.value():
|
|
|
|
|
case Instructions::i32x4_extadd_pairwise_i16x8_u.value():
|
|
|
|
|
case Instructions::i16x8_abs.value():
|
|
|
|
|
case Instructions::i16x8_neg.value():
|
|
|
|
|
case Instructions::i16x8_q15mulr_sat_s.value():
|
|
|
|
|
case Instructions::i16x8_all_true.value():
|
|
|
|
|
case Instructions::i16x8_bitmask.value():
|
|
|
|
|
case Instructions::i16x8_narrow_i32x4_s.value():
|
|
|
|
|
case Instructions::i16x8_narrow_i32x4_u.value():
|
|
|
|
|
case Instructions::i16x8_extend_low_i8x16_s.value():
|
|
|
|
|
case Instructions::i16x8_extend_high_i8x16_s.value():
|
|
|
|
|
case Instructions::i16x8_extend_low_i8x16_u.value():
|
|
|
|
|
case Instructions::i16x8_extend_high_i8x16_u.value():
|
|
|
|
|
case Instructions::i16x8_shl.value():
|
|
|
|
|
case Instructions::i16x8_shr_s.value():
|
|
|
|
|
case Instructions::i16x8_shr_u.value():
|
|
|
|
|
case Instructions::i16x8_add.value():
|
|
|
|
|
case Instructions::i16x8_add_sat_s.value():
|
|
|
|
|
case Instructions::i16x8_add_sat_u.value():
|
|
|
|
|
case Instructions::i16x8_sub.value():
|
|
|
|
|
case Instructions::i16x8_sub_sat_s.value():
|
|
|
|
|
case Instructions::i16x8_sub_sat_u.value():
|
|
|
|
|
case Instructions::f64x2_nearest.value():
|
|
|
|
|
case Instructions::i16x8_mul.value():
|
|
|
|
|
case Instructions::i16x8_min_s.value():
|
|
|
|
|
case Instructions::i16x8_min_u.value():
|
|
|
|
|
case Instructions::i16x8_max_s.value():
|
|
|
|
|
case Instructions::i16x8_max_u.value():
|
|
|
|
|
case Instructions::i16x8_avgr_u.value():
|
|
|
|
|
case Instructions::i16x8_extmul_low_i8x16_s.value():
|
|
|
|
|
case Instructions::i16x8_extmul_high_i8x16_s.value():
|
|
|
|
|
case Instructions::i16x8_extmul_low_i8x16_u.value():
|
|
|
|
|
case Instructions::i16x8_extmul_high_i8x16_u.value():
|
|
|
|
|
case Instructions::i32x4_abs.value():
|
|
|
|
|
case Instructions::i32x4_neg.value():
|
|
|
|
|
case Instructions::i32x4_all_true.value():
|
|
|
|
|
case Instructions::i32x4_bitmask.value():
|
|
|
|
|
case Instructions::i32x4_extend_low_i16x8_s.value():
|
|
|
|
|
case Instructions::i32x4_extend_high_i16x8_s.value():
|
|
|
|
|
case Instructions::i32x4_extend_low_i16x8_u.value():
|
|
|
|
|
case Instructions::i32x4_extend_high_i16x8_u.value():
|
|
|
|
|
case Instructions::i32x4_shl.value():
|
|
|
|
|
case Instructions::i32x4_shr_s.value():
|
|
|
|
|
case Instructions::i32x4_shr_u.value():
|
|
|
|
|
case Instructions::i32x4_add.value():
|
|
|
|
|
case Instructions::i32x4_sub.value():
|
|
|
|
|
case Instructions::i32x4_mul.value():
|
|
|
|
|
case Instructions::i32x4_min_s.value():
|
|
|
|
|
case Instructions::i32x4_min_u.value():
|
|
|
|
|
case Instructions::i32x4_max_s.value():
|
|
|
|
|
case Instructions::i32x4_max_u.value():
|
|
|
|
|
case Instructions::i32x4_dot_i16x8_s.value():
|
|
|
|
|
case Instructions::i32x4_extmul_low_i16x8_s.value():
|
|
|
|
|
case Instructions::i32x4_extmul_high_i16x8_s.value():
|
|
|
|
|
case Instructions::i32x4_extmul_low_i16x8_u.value():
|
|
|
|
|
case Instructions::i32x4_extmul_high_i16x8_u.value():
|
|
|
|
|
case Instructions::i64x2_abs.value():
|
|
|
|
|
case Instructions::i64x2_neg.value():
|
|
|
|
|
case Instructions::i64x2_all_true.value():
|
|
|
|
|
case Instructions::i64x2_bitmask.value():
|
|
|
|
|
case Instructions::i64x2_extend_low_i32x4_s.value():
|
|
|
|
|
case Instructions::i64x2_extend_high_i32x4_s.value():
|
|
|
|
|
case Instructions::i64x2_extend_low_i32x4_u.value():
|
|
|
|
|
case Instructions::i64x2_extend_high_i32x4_u.value():
|
|
|
|
|
case Instructions::i64x2_shl.value():
|
|
|
|
|
case Instructions::i64x2_shr_s.value():
|
|
|
|
|
case Instructions::i64x2_shr_u.value():
|
|
|
|
|
case Instructions::i64x2_add.value():
|
|
|
|
|
case Instructions::i64x2_sub.value():
|
|
|
|
|
case Instructions::i64x2_mul.value():
|
|
|
|
|
case Instructions::i64x2_eq.value():
|
|
|
|
|
case Instructions::i64x2_ne.value():
|
|
|
|
|
case Instructions::i64x2_lt_s.value():
|
|
|
|
|
case Instructions::i64x2_gt_s.value():
|
|
|
|
|
case Instructions::i64x2_le_s.value():
|
|
|
|
|
case Instructions::i64x2_ge_s.value():
|
|
|
|
|
case Instructions::i64x2_extmul_low_i32x4_s.value():
|
|
|
|
|
case Instructions::i64x2_extmul_high_i32x4_s.value():
|
|
|
|
|
case Instructions::i64x2_extmul_low_i32x4_u.value():
|
|
|
|
|
case Instructions::i64x2_extmul_high_i32x4_u.value():
|
|
|
|
|
case Instructions::f32x4_abs.value():
|
|
|
|
|
case Instructions::f32x4_neg.value():
|
|
|
|
|
case Instructions::f32x4_sqrt.value():
|
|
|
|
|
case Instructions::f32x4_add.value():
|
|
|
|
|
case Instructions::f32x4_sub.value():
|
|
|
|
|
case Instructions::f32x4_mul.value():
|
|
|
|
|
case Instructions::f32x4_div.value():
|
|
|
|
|
case Instructions::f32x4_min.value():
|
|
|
|
|
case Instructions::f32x4_max.value():
|
|
|
|
|
case Instructions::f32x4_pmin.value():
|
|
|
|
|
case Instructions::f32x4_pmax.value():
|
|
|
|
|
case Instructions::f64x2_abs.value():
|
|
|
|
|
case Instructions::f64x2_neg.value():
|
|
|
|
|
case Instructions::f64x2_sqrt.value():
|
|
|
|
|
case Instructions::f64x2_add.value():
|
|
|
|
|
case Instructions::f64x2_sub.value():
|
|
|
|
|
case Instructions::f64x2_mul.value():
|
|
|
|
|
case Instructions::f64x2_div.value():
|
|
|
|
|
case Instructions::f64x2_min.value():
|
|
|
|
|
case Instructions::f64x2_max.value():
|
|
|
|
|
case Instructions::f64x2_pmin.value():
|
|
|
|
|
case Instructions::f64x2_pmax.value():
|
|
|
|
|
case Instructions::i32x4_trunc_sat_f32x4_s.value():
|
|
|
|
|
case Instructions::i32x4_trunc_sat_f32x4_u.value():
|
|
|
|
|
case Instructions::f32x4_convert_i32x4_s.value():
|
|
|
|
|
case Instructions::f32x4_convert_i32x4_u.value():
|
|
|
|
|
case Instructions::i32x4_trunc_sat_f64x2_s_zero.value():
|
|
|
|
|
case Instructions::i32x4_trunc_sat_f64x2_u_zero.value():
|
|
|
|
|
case Instructions::f64x2_convert_low_i32x4_s.value():
|
|
|
|
|
case Instructions::f64x2_convert_low_i32x4_u.value():
|
2025-09-22 23:37:20 -03:00
|
|
|
case Instructions::i8x16_relaxed_swizzle.value():
|
|
|
|
|
case Instructions::i32x4_relaxed_trunc_f32x4_s.value():
|
|
|
|
|
case Instructions::i32x4_relaxed_trunc_f32x4_u.value():
|
|
|
|
|
case Instructions::i32x4_relaxed_trunc_f64x2_s_zero.value():
|
|
|
|
|
case Instructions::i32x4_relaxed_trunc_f64x2_u_zero.value():
|
|
|
|
|
case Instructions::f32x4_relaxed_madd.value():
|
|
|
|
|
case Instructions::f32x4_relaxed_nmadd.value():
|
|
|
|
|
case Instructions::f64x2_relaxed_madd.value():
|
|
|
|
|
case Instructions::f64x2_relaxed_nmadd.value():
|
|
|
|
|
case Instructions::i8x16_relaxed_laneselect.value():
|
|
|
|
|
case Instructions::i16x8_relaxed_laneselect.value():
|
|
|
|
|
case Instructions::i32x4_relaxed_laneselect.value():
|
|
|
|
|
case Instructions::i64x2_relaxed_laneselect.value():
|
|
|
|
|
case Instructions::f32x4_relaxed_min.value():
|
|
|
|
|
case Instructions::f32x4_relaxed_max.value():
|
|
|
|
|
case Instructions::f64x2_relaxed_min.value():
|
|
|
|
|
case Instructions::f64x2_relaxed_max.value():
|
|
|
|
|
case Instructions::i16x8_relaxed_q15mulr_s.value():
|
|
|
|
|
case Instructions::i16x8_relaxed_dot_i8x16_i7x16_s.value():
|
|
|
|
|
case Instructions::i32x4_relaxed_dot_i8x16_i7x16_add_s.value():
|
2024-07-24 14:48:31 -03:00
|
|
|
// op
|
|
|
|
|
return Instruction { full_opcode };
|
|
|
|
|
default:
|
|
|
|
|
return ParseError::UnknownInstruction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ParseError::UnknownInstruction;
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<CustomSection> CustomSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("CustomSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto name = TRY(parse_name(stream));
|
2025-06-06 09:11:24 -03:00
|
|
|
auto remaining = stream.remaining();
|
|
|
|
|
auto maybe_data_buffer = ByteBuffer::create_uninitialized(remaining);
|
|
|
|
|
if (maybe_data_buffer.is_error())
|
2021-09-05 19:59:52 -03:00
|
|
|
return ParseError::OutOfMemory;
|
2025-06-06 09:11:24 -03:00
|
|
|
auto data_buffer = maybe_data_buffer.release_value();
|
2021-09-05 19:59:52 -03:00
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
size_t nread = 0;
|
|
|
|
|
do {
|
|
|
|
|
auto read = MUST(stream.read_some(data_buffer.bytes().slice(nread)));
|
|
|
|
|
nread += read.size();
|
|
|
|
|
} while (nread != remaining && !stream.is_eof());
|
|
|
|
|
|
|
|
|
|
if (nread != remaining)
|
|
|
|
|
return ParseError::UnexpectedEof;
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-05-26 22:06:26 -03:00
|
|
|
return CustomSection(name, move(data_buffer));
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#recursive-types
|
|
|
|
|
ParseResult<TypeSection::Type> TypeSection::Type::parse(ConstrainedStream& stream, Optional<u8> leading_tag)
|
2026-02-02 13:44:20 -03:00
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Type"sv);
|
2026-06-11 01:14:58 -03:00
|
|
|
u8 tag;
|
|
|
|
|
if (leading_tag.has_value())
|
|
|
|
|
tag = *leading_tag;
|
|
|
|
|
else
|
|
|
|
|
tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
|
|
|
|
|
bool is_final = true;
|
|
|
|
|
Vector<TypeIndex> supertypes;
|
|
|
|
|
if (tag == Constants::sub_type_tag || tag == Constants::sub_final_type_tag) {
|
|
|
|
|
is_final = tag == Constants::sub_final_type_tag;
|
|
|
|
|
supertypes = TRY(parse_vector<GenericIndexParser<TypeIndex>>(stream));
|
|
|
|
|
tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
}
|
2026-02-02 13:44:20 -03:00
|
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
|
case Constants::function_signature_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return Type { TRY(FunctionType::parse(stream)), move(supertypes), is_final };
|
2026-02-02 13:44:20 -03:00
|
|
|
case Constants::struct_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return Type { TRY(StructType::parse(stream)), move(supertypes), is_final };
|
2026-02-08 15:09:18 -03:00
|
|
|
case Constants::array_tag:
|
2026-06-11 01:14:58 -03:00
|
|
|
return Type { TRY(ArrayType::parse(stream)), move(supertypes), is_final };
|
2026-02-02 13:44:20 -03:00
|
|
|
default:
|
|
|
|
|
return ParseError::InvalidTag;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 01:14:58 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/modules.html#type-section
|
|
|
|
|
// https://webassembly.github.io/spec/core/binary/types.html#recursive-types
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<TypeSection> TypeSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TypeSection"sv);
|
2026-06-11 01:14:58 -03:00
|
|
|
|
|
|
|
|
auto rec_type_count_or_error = stream.read_value<LEB128<u32>>();
|
|
|
|
|
if (rec_type_count_or_error.is_error())
|
|
|
|
|
return with_eof_check(stream, ParseError::ExpectedSize);
|
|
|
|
|
size_t rec_type_count = rec_type_count_or_error.release_value();
|
|
|
|
|
|
|
|
|
|
Vector<Type> types;
|
|
|
|
|
types.ensure_capacity(rec_type_count);
|
|
|
|
|
for (size_t rec_type_index = 0; rec_type_index < rec_type_count; ++rec_type_index) {
|
|
|
|
|
auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
|
|
|
|
|
auto first_type_index = types.size();
|
|
|
|
|
if (tag == Constants::rec_group_tag) {
|
|
|
|
|
auto sub_type_count_or_error = stream.read_value<LEB128<u32>>();
|
|
|
|
|
if (sub_type_count_or_error.is_error())
|
|
|
|
|
return with_eof_check(stream, ParseError::ExpectedSize);
|
|
|
|
|
size_t sub_type_count = sub_type_count_or_error.release_value();
|
|
|
|
|
for (size_t i = 0; i < sub_type_count; ++i)
|
|
|
|
|
types.append(TRY(Type::parse(stream)));
|
|
|
|
|
} else {
|
|
|
|
|
types.append(TRY(Type::parse(stream, tag)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Type::RecGroupSpan const span {
|
|
|
|
|
static_cast<u32>(first_type_index),
|
|
|
|
|
static_cast<u32>(types.size() - first_type_index),
|
|
|
|
|
};
|
|
|
|
|
for (size_t i = first_type_index; i < types.size(); ++i)
|
|
|
|
|
types[i].set_rec_group(span);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 22:06:26 -03:00
|
|
|
return TypeSection { types };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<ImportSection::Import> ImportSection::Import::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Import"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto module = TRY(parse_name(stream));
|
|
|
|
|
auto name = TRY(parse_name(stream));
|
2024-07-27 21:21:19 -03:00
|
|
|
auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
2023-01-21 07:44:19 -03:00
|
|
|
|
2021-04-26 05:18:13 -03:00
|
|
|
switch (tag) {
|
|
|
|
|
case Constants::extern_function_tag: {
|
2024-05-26 22:06:26 -03:00
|
|
|
auto index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
|
|
|
|
|
return Import { module, name, index };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
case Constants::extern_table_tag:
|
|
|
|
|
return parse_with_type<TableType>(stream, module, name);
|
|
|
|
|
case Constants::extern_memory_tag:
|
|
|
|
|
return parse_with_type<MemoryType>(stream, module, name);
|
|
|
|
|
case Constants::extern_global_tag:
|
|
|
|
|
return parse_with_type<GlobalType>(stream, module, name);
|
2025-09-24 22:35:34 -03:00
|
|
|
case Constants::extern_tag_tag:
|
|
|
|
|
return parse_with_type<TagType>(stream, module, name);
|
2021-04-26 05:18:13 -03:00
|
|
|
default:
|
2024-07-27 21:21:19 -03:00
|
|
|
return ParseError::InvalidTag;
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<ImportSection> ImportSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ImportSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto imports = TRY(parse_vector<Import>(stream));
|
|
|
|
|
return ImportSection { imports };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<FunctionSection> FunctionSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto indices = TRY(parse_vector<u32>(stream));
|
2021-04-26 05:18:13 -03:00
|
|
|
|
|
|
|
|
Vector<TypeIndex> typed_indices;
|
2024-05-26 22:06:26 -03:00
|
|
|
typed_indices.ensure_capacity(indices.size());
|
|
|
|
|
for (auto entry : indices)
|
2021-04-26 05:18:13 -03:00
|
|
|
typed_indices.append(entry);
|
|
|
|
|
|
|
|
|
|
return FunctionSection { move(typed_indices) };
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 02:15:32 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/modules.html#table-section
|
|
|
|
|
// table ::= tt:tabletype => table tt (ref.null ht) (if tt = at lim (ref null? ht))
|
|
|
|
|
// | 0x40 0x00 tt:tabletype e:expr => table tt e
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<TableSection::Table> TableSection::Table::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Table"sv);
|
2026-06-11 02:15:32 -03:00
|
|
|
|
|
|
|
|
auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
if (tag == 0x40) {
|
|
|
|
|
auto reserved = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
|
|
|
|
if (reserved != 0x00)
|
|
|
|
|
return ParseError::InvalidTag;
|
|
|
|
|
auto type = TRY(TableType::parse(stream));
|
|
|
|
|
auto initializer = TRY(Expression::parse(stream));
|
|
|
|
|
return Table { type, move(initializer) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto element_type = TRY(parse_reference_type(stream, tag));
|
|
|
|
|
auto limits = TRY(Limits::parse(stream));
|
|
|
|
|
auto type = TableType { element_type, limits };
|
|
|
|
|
|
|
|
|
|
// Synthesize the implicit initializer (ref.null ht).
|
|
|
|
|
auto null_element = element_type;
|
|
|
|
|
null_element.set_nullable(true);
|
|
|
|
|
Vector<Instruction> instructions {
|
|
|
|
|
Instruction(Instructions::ref_null, null_element),
|
|
|
|
|
Instruction(Instructions::synthetic_end_expression),
|
|
|
|
|
};
|
|
|
|
|
return Table { type, Expression { move(instructions) } };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<TableSection> TableSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto tables = TRY(parse_vector<Table>(stream));
|
2026-06-11 02:15:32 -03:00
|
|
|
return TableSection { move(tables) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<MemorySection::Memory> MemorySection::Memory::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Memory"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto type = TRY(MemoryType::parse(stream));
|
|
|
|
|
return Memory { type };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<MemorySection> MemorySection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemorySection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto memories = TRY(parse_vector<Memory>(stream));
|
|
|
|
|
return MemorySection { memories };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<Expression> Expression::parse(ConstrainedStream& stream, Optional<size_t> size_hint)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Expression"sv);
|
2024-07-24 14:48:31 -03:00
|
|
|
|
2021-05-01 12:26:45 -03:00
|
|
|
InstructionPointer ip { 0 };
|
2024-07-24 14:48:31 -03:00
|
|
|
Vector<InstructionPointer> stack;
|
|
|
|
|
Vector<Instruction> instructions;
|
2024-07-26 15:31:30 -03:00
|
|
|
if (size_hint.has_value())
|
|
|
|
|
instructions.ensure_capacity(size_hint.release_value());
|
2024-07-24 14:48:31 -03:00
|
|
|
while (true) {
|
|
|
|
|
auto instruction = TRY(Instruction::parse(stream));
|
|
|
|
|
switch (instruction.opcode().value()) {
|
|
|
|
|
case Instructions::block.value():
|
|
|
|
|
case Instructions::loop.value():
|
|
|
|
|
case Instructions::if_.value():
|
2025-09-24 22:35:34 -03:00
|
|
|
case Instructions::try_table.value():
|
2024-07-24 14:48:31 -03:00
|
|
|
stack.append(ip);
|
|
|
|
|
break;
|
|
|
|
|
case Instructions::structured_end.value(): {
|
2025-08-22 12:11:25 -03:00
|
|
|
if (stack.is_empty()) {
|
|
|
|
|
instructions.empend(Instructions::synthetic_end_expression); // Synthetic noop to mark the end of the expression.
|
2024-07-24 14:48:31 -03:00
|
|
|
return Expression { move(instructions) };
|
2025-08-22 12:11:25 -03:00
|
|
|
}
|
2024-07-24 14:48:31 -03:00
|
|
|
// Patch the end_ip of the last structured instruction
|
2025-09-24 22:35:34 -03:00
|
|
|
auto entry = stack.take_last();
|
2026-01-08 10:57:32 -03:00
|
|
|
bool valid_type = instructions[entry.value()].arguments().visit(
|
2025-09-24 22:35:34 -03:00
|
|
|
[&](Instruction::StructuredInstructionArgs& args) {
|
2026-05-14 05:35:51 -03:00
|
|
|
args.end_ip = ip + (args.else_ip().has_value() ? 1 : 0);
|
2026-01-08 10:57:32 -03:00
|
|
|
return true;
|
2025-09-24 22:35:34 -03:00
|
|
|
},
|
|
|
|
|
[&](Instruction::TryTableArgs& args) {
|
2026-06-11 03:52:02 -03:00
|
|
|
args.end_ip = ip;
|
2026-01-08 10:57:32 -03:00
|
|
|
return true;
|
2025-09-24 22:35:34 -03:00
|
|
|
},
|
2026-01-08 10:57:32 -03:00
|
|
|
[](auto&) { return false; });
|
|
|
|
|
|
|
|
|
|
if (!valid_type)
|
|
|
|
|
return ParseError::InvalidType;
|
|
|
|
|
|
2024-07-24 14:48:31 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Instructions::structured_else.value(): {
|
|
|
|
|
if (stack.is_empty())
|
|
|
|
|
return ParseError::UnknownInstruction;
|
|
|
|
|
auto entry = stack.last();
|
2026-01-08 10:57:32 -03:00
|
|
|
auto* args = instructions[entry.value()].arguments().get_pointer<Instruction::StructuredInstructionArgs>();
|
|
|
|
|
if (!args)
|
|
|
|
|
return ParseError::InvalidType;
|
|
|
|
|
|
2026-05-14 05:35:51 -03:00
|
|
|
args->else_ip() = ip + 1;
|
2024-07-24 14:48:31 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
instructions.append(move(instruction));
|
|
|
|
|
++ip;
|
|
|
|
|
}
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2025-08-22 12:11:25 -03:00
|
|
|
instructions.empend(Instructions::synthetic_end_expression); // Synthetic noop to mark the end of the expression.
|
|
|
|
|
|
2024-07-24 14:48:31 -03:00
|
|
|
return Expression { move(instructions) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<GlobalSection::Global> GlobalSection::Global::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Global"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto type = TRY(GlobalType::parse(stream));
|
|
|
|
|
auto exprs = TRY(Expression::parse(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
return Global { type, move(exprs) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<GlobalSection> GlobalSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto result = TRY(parse_vector<Global>(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
return GlobalSection { move(result) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<ExportSection::Export> ExportSection::Export::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Export"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto name = TRY(parse_name(stream));
|
2024-07-27 21:21:19 -03:00
|
|
|
auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
|
2023-01-21 07:44:19 -03:00
|
|
|
|
2024-07-27 21:21:19 -03:00
|
|
|
auto index = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
|
case Constants::extern_function_tag:
|
2024-05-26 22:06:26 -03:00
|
|
|
return Export { name, ExportDesc { FunctionIndex { index } } };
|
2021-04-26 05:18:13 -03:00
|
|
|
case Constants::extern_table_tag:
|
2024-05-26 22:06:26 -03:00
|
|
|
return Export { name, ExportDesc { TableIndex { index } } };
|
2021-04-26 05:18:13 -03:00
|
|
|
case Constants::extern_memory_tag:
|
2024-05-26 22:06:26 -03:00
|
|
|
return Export { name, ExportDesc { MemoryIndex { index } } };
|
2021-04-26 05:18:13 -03:00
|
|
|
case Constants::extern_global_tag:
|
2024-05-26 22:06:26 -03:00
|
|
|
return Export { name, ExportDesc { GlobalIndex { index } } };
|
2025-09-24 22:35:34 -03:00
|
|
|
case Constants::extern_tag_tag:
|
|
|
|
|
return Export { name, ExportDesc { TagIndex { index } } };
|
2021-04-26 05:18:13 -03:00
|
|
|
default:
|
2024-07-27 21:21:19 -03:00
|
|
|
return ParseError::InvalidTag;
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<ExportSection> ExportSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ExportSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto result = TRY(parse_vector<Export>(stream));
|
|
|
|
|
return ExportSection { result };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<StartSection::StartFunction> StartSection::StartFunction::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartFunction"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto index = TRY(GenericIndexParser<FunctionIndex>::parse(stream));
|
|
|
|
|
return StartFunction { index };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<StartSection> StartSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto result = TRY(StartFunction::parse(stream));
|
|
|
|
|
return StartSection { result };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<ElementSection::Element> ElementSection::Element::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Element"sv);
|
2024-07-27 21:21:19 -03:00
|
|
|
auto tag = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedKindTag);
|
2023-01-21 07:44:19 -03:00
|
|
|
|
2024-05-27 11:16:59 -03:00
|
|
|
if (tag > 0x07)
|
|
|
|
|
return ParseError::InvalidTag;
|
|
|
|
|
|
|
|
|
|
auto has_passive = (tag & 0x01) != 0;
|
|
|
|
|
auto has_explicit_index = (tag & 0x02) != 0;
|
|
|
|
|
auto has_exprs = (tag & 0x04) != 0;
|
|
|
|
|
|
|
|
|
|
Variant<Active, Passive, Declarative> mode = Passive {};
|
|
|
|
|
if (has_passive) {
|
|
|
|
|
if (has_explicit_index) {
|
|
|
|
|
mode = Declarative {};
|
2021-05-01 15:19:17 -03:00
|
|
|
} else {
|
2024-05-27 11:16:59 -03:00
|
|
|
mode = Passive {};
|
2021-05-01 15:19:17 -03:00
|
|
|
}
|
2024-05-27 11:16:59 -03:00
|
|
|
} else {
|
|
|
|
|
TableIndex table_index = 0;
|
|
|
|
|
if (has_explicit_index)
|
|
|
|
|
table_index = TRY(GenericIndexParser<TableIndex>::parse(stream));
|
|
|
|
|
auto expression = TRY(Expression::parse(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
mode = Active { table_index, move(expression) };
|
2024-05-27 11:16:59 -03:00
|
|
|
}
|
|
|
|
|
|
2026-06-11 04:06:47 -03:00
|
|
|
// https://webassembly.github.io/spec/core/binary/modules.html#element-section
|
|
|
|
|
// elemkind ::= 0x00 => (ref func)
|
|
|
|
|
auto type = has_exprs ? ValueType(ValueType::FunctionReference) : ValueType(ValueType::FunctionReference, false);
|
2024-05-27 11:16:59 -03:00
|
|
|
if (has_passive || has_explicit_index) {
|
|
|
|
|
if (has_exprs) {
|
|
|
|
|
type = TRY(ValueType::parse(stream));
|
2021-05-01 15:19:17 -03:00
|
|
|
} else {
|
2024-07-27 21:21:19 -03:00
|
|
|
auto extern_ = TRY_READ(stream, u8, ParseError::InvalidType);
|
2024-05-27 11:16:59 -03:00
|
|
|
// Make sure that this is a function, as it's technically only the
|
|
|
|
|
// allowed one.
|
2024-07-27 21:21:19 -03:00
|
|
|
if (extern_ != 0x00) {
|
2024-05-27 11:16:59 -03:00
|
|
|
return ParseError::InvalidType;
|
|
|
|
|
}
|
2026-06-11 04:06:47 -03:00
|
|
|
type = ValueType(ValueType::FunctionReference, false);
|
2021-05-01 15:19:17 -03:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-27 11:16:59 -03:00
|
|
|
|
|
|
|
|
Vector<Expression> items;
|
|
|
|
|
if (!has_exprs) {
|
|
|
|
|
auto indices = TRY(parse_vector<GenericIndexParser<FunctionIndex>>(stream));
|
2024-06-16 10:42:00 -03:00
|
|
|
for (auto& index : indices) {
|
2025-08-22 12:11:25 -03:00
|
|
|
Vector<Instruction> instructions {
|
|
|
|
|
Instruction(Instructions::ref_func, index),
|
|
|
|
|
Instruction(Instructions::synthetic_end_expression),
|
|
|
|
|
};
|
2024-06-16 10:42:00 -03:00
|
|
|
items.empend(move(instructions));
|
|
|
|
|
}
|
2024-05-27 11:16:59 -03:00
|
|
|
} else {
|
|
|
|
|
items = TRY(parse_vector<Expression>(stream));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Element { type, move(items), move(mode) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<ElementSection> ElementSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ElementSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto result = TRY(parse_vector<Element>(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
return ElementSection { move(result) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<Locals> Locals::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Locals"sv);
|
2024-07-27 21:21:19 -03:00
|
|
|
auto count = TRY_READ(stream, LEB128<u32>, ParseError::InvalidSize);
|
2021-08-30 16:07:29 -03:00
|
|
|
|
|
|
|
|
if (count > Constants::max_allowed_function_locals_per_type)
|
2024-07-27 21:21:19 -03:00
|
|
|
return ParseError::HugeAllocationRequested;
|
2021-08-30 16:07:29 -03:00
|
|
|
|
2024-05-26 22:06:26 -03:00
|
|
|
auto type = TRY(ValueType::parse(stream));
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-07-27 21:21:19 -03:00
|
|
|
return Locals { count, type };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<CodeSection::Func> CodeSection::Func::parse(ConstrainedStream& stream, size_t size_hint)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Func"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto locals = TRY(parse_vector<Locals>(stream));
|
2024-07-26 15:31:30 -03:00
|
|
|
auto body = TRY(Expression::parse(stream, size_hint));
|
2024-07-26 18:01:03 -03:00
|
|
|
return Func { move(locals), move(body) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<CodeSection::Code> CodeSection::Code::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Code"sv);
|
2024-07-27 21:21:19 -03:00
|
|
|
auto size = TRY_READ(stream, LEB128<u32>, ParseError::InvalidSize);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2026-06-10 08:28:23 -03:00
|
|
|
// Constrain to the declared size so an invalid entry fails here instead of desyncing the remaining entries in the section.
|
|
|
|
|
auto code_stream = ConstrainedStream { MaybeOwned<Stream>(stream), size };
|
|
|
|
|
|
2025-04-09 14:02:30 -03:00
|
|
|
// Empirically, if there are `size` bytes to be read, then there's around
|
2024-07-26 15:31:30 -03:00
|
|
|
// `size / 2` instructions, so we pass that as our size hint.
|
2026-06-10 08:28:23 -03:00
|
|
|
auto func = TRY(Func::parse(code_stream, size / 2));
|
|
|
|
|
|
|
|
|
|
if (code_stream.remaining() != 0)
|
|
|
|
|
return ParseError::SectionSizeMismatch;
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-07-27 21:21:19 -03:00
|
|
|
return Code { size, move(func) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<CodeSection> CodeSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("CodeSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto result = TRY(parse_vector<Code>(stream));
|
2024-07-26 18:01:03 -03:00
|
|
|
return CodeSection { move(result) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<DataSection::Data> DataSection::Data::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Data"sv);
|
2024-07-27 21:21:19 -03:00
|
|
|
auto tag = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedKindTag);
|
2023-01-21 07:44:19 -03:00
|
|
|
|
2021-04-26 05:18:13 -03:00
|
|
|
if (tag > 0x02)
|
2024-07-27 21:21:19 -03:00
|
|
|
return ParseError::InvalidTag;
|
2021-04-26 05:18:13 -03:00
|
|
|
|
|
|
|
|
if (tag == 0x00) {
|
2024-05-26 22:06:26 -03:00
|
|
|
auto expr = TRY(Expression::parse(stream));
|
|
|
|
|
auto init = TRY(parse_vector<u8>(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
return Data { Active { move(init), { 0 }, move(expr) } };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
if (tag == 0x01) {
|
2024-05-26 22:06:26 -03:00
|
|
|
auto init = TRY(parse_vector<u8>(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
return Data { Passive { move(init) } };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
if (tag == 0x02) {
|
2024-05-26 22:06:26 -03:00
|
|
|
auto index = TRY(GenericIndexParser<MemoryIndex>::parse(stream));
|
|
|
|
|
auto expr = TRY(Expression::parse(stream));
|
|
|
|
|
auto init = TRY(parse_vector<u8>(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
return Data { Active { move(init), index, move(expr) } };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<DataSection> DataSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataSection"sv);
|
2024-05-26 22:06:26 -03:00
|
|
|
auto data = TRY(parse_vector<Data>(stream));
|
2026-05-14 05:25:54 -03:00
|
|
|
return DataSection { move(data) };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 09:11:24 -03:00
|
|
|
ParseResult<DataCountSection> DataCountSection::parse(ConstrainedStream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataCountSection"sv);
|
2023-01-29 20:02:38 -03:00
|
|
|
auto value_or_error = stream.read_value<LEB128<u32>>();
|
|
|
|
|
if (value_or_error.is_error()) {
|
2023-01-21 07:44:19 -03:00
|
|
|
if (stream.is_eof()) {
|
2021-04-27 23:32:37 -03:00
|
|
|
// The section simply didn't contain anything.
|
|
|
|
|
return DataCountSection { {} };
|
|
|
|
|
}
|
|
|
|
|
return ParseError::ExpectedSize;
|
|
|
|
|
}
|
2023-01-29 20:02:38 -03:00
|
|
|
u32 value = value_or_error.release_value();
|
2021-04-27 23:32:37 -03:00
|
|
|
|
|
|
|
|
return DataCountSection { value };
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-24 22:35:34 -03:00
|
|
|
ParseResult<TagSection> TagSection::parse(ConstrainedStream& stream)
|
|
|
|
|
{
|
|
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TagSection"sv);
|
|
|
|
|
// https://webassembly.github.io/exception-handling/core/binary/modules.html#binary-tagsec
|
2026-01-08 11:07:45 -03:00
|
|
|
auto tags = TRY(parse_vector<TagType>(stream));
|
2025-09-24 22:35:34 -03:00
|
|
|
return TagSection { move(tags) };
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-09 21:15:45 -03:00
|
|
|
ParseResult<SectionId> SectionId::parse(Stream& stream)
|
|
|
|
|
{
|
|
|
|
|
u8 id = TRY_READ(stream, u8, ParseError::ExpectedIndex);
|
|
|
|
|
switch (id) {
|
|
|
|
|
case 0x00:
|
|
|
|
|
return SectionId(SectionIdKind::Custom);
|
|
|
|
|
case 0x01:
|
|
|
|
|
return SectionId(SectionIdKind::Type);
|
|
|
|
|
case 0x02:
|
|
|
|
|
return SectionId(SectionIdKind::Import);
|
|
|
|
|
case 0x03:
|
|
|
|
|
return SectionId(SectionIdKind::Function);
|
|
|
|
|
case 0x04:
|
|
|
|
|
return SectionId(SectionIdKind::Table);
|
|
|
|
|
case 0x05:
|
|
|
|
|
return SectionId(SectionIdKind::Memory);
|
|
|
|
|
case 0x06:
|
|
|
|
|
return SectionId(SectionIdKind::Global);
|
|
|
|
|
case 0x07:
|
|
|
|
|
return SectionId(SectionIdKind::Export);
|
|
|
|
|
case 0x08:
|
|
|
|
|
return SectionId(SectionIdKind::Start);
|
|
|
|
|
case 0x09:
|
|
|
|
|
return SectionId(SectionIdKind::Element);
|
|
|
|
|
case 0x0a:
|
|
|
|
|
return SectionId(SectionIdKind::Code);
|
|
|
|
|
case 0x0b:
|
|
|
|
|
return SectionId(SectionIdKind::Data);
|
|
|
|
|
case 0x0c:
|
|
|
|
|
return SectionId(SectionIdKind::DataCount);
|
2025-09-24 22:35:34 -03:00
|
|
|
case 0x0d:
|
|
|
|
|
return SectionId(SectionIdKind::Tag);
|
2024-08-09 21:15:45 -03:00
|
|
|
default:
|
|
|
|
|
return ParseError::InvalidIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 20:13:37 -03:00
|
|
|
ParseResult<NonnullRefPtr<Module>> Module::parse(Stream& stream)
|
2021-04-26 05:18:13 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Module"sv);
|
2021-04-26 05:18:13 -03:00
|
|
|
u8 buf[4];
|
2023-03-01 11:27:35 -03:00
|
|
|
if (stream.read_until_filled({ buf, 4 }).is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::InvalidInput);
|
2021-04-26 05:18:13 -03:00
|
|
|
if (Bytes { buf, 4 } != wasm_magic.span())
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::InvalidModuleMagic);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2023-03-01 11:27:35 -03:00
|
|
|
if (stream.read_until_filled({ buf, 4 }).is_error())
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::InvalidInput);
|
2021-04-26 05:18:13 -03:00
|
|
|
if (Bytes { buf, 4 } != wasm_version.span())
|
2021-04-27 08:53:36 -03:00
|
|
|
return with_eof_check(stream, ParseError::InvalidModuleVersion);
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2024-08-09 21:15:45 -03:00
|
|
|
auto last_section_id = SectionId::SectionIdKind::Custom;
|
2026-06-10 08:29:08 -03:00
|
|
|
u32 seen_section_kinds = 0;
|
2024-08-21 20:13:37 -03:00
|
|
|
auto module_ptr = make_ref_counted<Module>();
|
|
|
|
|
auto& module = *module_ptr;
|
|
|
|
|
|
2024-07-08 23:04:48 -03:00
|
|
|
while (!stream.is_eof()) {
|
2024-08-09 21:15:45 -03:00
|
|
|
auto section_id = TRY(SectionId::parse(stream));
|
2024-07-27 21:21:19 -03:00
|
|
|
size_t section_size = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedSize);
|
2023-03-10 12:37:52 -03:00
|
|
|
auto section_stream = ConstrainedStream { MaybeOwned<Stream>(stream), section_size };
|
2021-04-26 05:18:13 -03:00
|
|
|
|
2026-06-10 08:29:08 -03:00
|
|
|
if (section_id.kind() != SectionId::SectionIdKind::Custom) {
|
|
|
|
|
auto kind_bit = 1u << to_underlying(section_id.kind());
|
|
|
|
|
if (seen_section_kinds & kind_bit)
|
|
|
|
|
return ParseError::DuplicateSection;
|
|
|
|
|
seen_section_kinds |= kind_bit;
|
|
|
|
|
}
|
2024-07-29 23:56:00 -03:00
|
|
|
|
2024-08-09 21:15:45 -03:00
|
|
|
switch (section_id.kind()) {
|
|
|
|
|
case SectionId::SectionIdKind::Custom:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.custom_sections().append(TRY(CustomSection::parse(section_stream)));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Type:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.type_section() = TRY(TypeSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Import:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.import_section() = TRY(ImportSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Function:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.function_section() = TRY(FunctionSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Table:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.table_section() = TRY(TableSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Memory:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.memory_section() = TRY(MemorySection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Global:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.global_section() = TRY(GlobalSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Export:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.export_section() = TRY(ExportSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Start:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.start_section() = TRY(StartSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Element:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.element_section() = TRY(ElementSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Code:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.code_section() = TRY(CodeSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::Data:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.data_section() = TRY(DataSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2024-08-09 21:15:45 -03:00
|
|
|
case SectionId::SectionIdKind::DataCount:
|
2024-07-29 23:56:00 -03:00
|
|
|
module.data_count_section() = TRY(DataCountSection::parse(section_stream));
|
2024-06-01 16:53:19 -03:00
|
|
|
break;
|
2025-09-24 22:35:34 -03:00
|
|
|
case SectionId::SectionIdKind::Tag:
|
|
|
|
|
module.tag_section() = TRY(TagSection::parse(section_stream));
|
|
|
|
|
break;
|
2021-04-26 05:18:13 -03:00
|
|
|
default:
|
2024-07-27 21:21:19 -03:00
|
|
|
return ParseError::InvalidIndex;
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
2025-09-24 22:35:34 -03:00
|
|
|
if (!section_id.can_appear_after(last_section_id))
|
|
|
|
|
return ParseError::SectionOutOfOrder;
|
2026-06-10 08:29:08 -03:00
|
|
|
// Custom sections don't participate in ordering.
|
|
|
|
|
if (section_id.kind() != SectionId::SectionIdKind::Custom)
|
|
|
|
|
last_section_id = section_id.kind();
|
2024-07-08 23:04:48 -03:00
|
|
|
if (section_stream.remaining() != 0)
|
2024-06-01 16:53:19 -03:00
|
|
|
return ParseError::SectionSizeMismatch;
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 07:29:20 -03:00
|
|
|
module_ptr->preprocess();
|
|
|
|
|
|
2024-08-21 20:13:37 -03:00
|
|
|
return module_ptr;
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 07:29:20 -03:00
|
|
|
void Module::preprocess()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString parse_error_to_byte_string(ParseError error)
|
2021-04-27 08:53:36 -03:00
|
|
|
{
|
|
|
|
|
switch (error) {
|
|
|
|
|
case ParseError::UnexpectedEof:
|
|
|
|
|
return "Unexpected end-of-file";
|
|
|
|
|
case ParseError::ExpectedIndex:
|
|
|
|
|
return "Expected a valid index value";
|
|
|
|
|
case ParseError::ExpectedKindTag:
|
|
|
|
|
return "Expected a valid kind tag";
|
|
|
|
|
case ParseError::ExpectedSize:
|
|
|
|
|
return "Expected a valid LEB128-encoded size";
|
|
|
|
|
case ParseError::ExpectedValueOrTerminator:
|
|
|
|
|
return "Expected either a terminator or a value";
|
|
|
|
|
case ParseError::InvalidIndex:
|
|
|
|
|
return "An index parsed was semantically invalid";
|
|
|
|
|
case ParseError::InvalidInput:
|
|
|
|
|
return "Input data contained invalid bytes";
|
|
|
|
|
case ParseError::InvalidModuleMagic:
|
|
|
|
|
return "Incorrect module magic (did not match \\0asm)";
|
|
|
|
|
case ParseError::InvalidModuleVersion:
|
|
|
|
|
return "Incorrect module version";
|
|
|
|
|
case ParseError::InvalidSize:
|
|
|
|
|
return "A parsed size did not make sense in context";
|
|
|
|
|
case ParseError::InvalidTag:
|
|
|
|
|
return "A parsed tag did not make sense in context";
|
|
|
|
|
case ParseError::InvalidType:
|
|
|
|
|
return "A parsed type did not make sense in context";
|
|
|
|
|
case ParseError::HugeAllocationRequested:
|
|
|
|
|
return "Parsing caused an attempt to allocate a very big chunk of memory, likely malformed data";
|
2021-09-05 19:59:52 -03:00
|
|
|
case ParseError::OutOfMemory:
|
|
|
|
|
return "The parser hit an OOM condition";
|
2021-04-27 10:29:21 -03:00
|
|
|
case ParseError::ExpectedFloatingImmediate:
|
|
|
|
|
return "Expected a floating point immediate";
|
|
|
|
|
case ParseError::ExpectedSignedImmediate:
|
|
|
|
|
return "Expected a signed integer immediate";
|
|
|
|
|
case ParseError::InvalidImmediate:
|
|
|
|
|
return "A parsed instruction immediate was invalid for the instruction it was used for";
|
2024-06-01 16:53:19 -03:00
|
|
|
case ParseError::SectionSizeMismatch:
|
|
|
|
|
return "A parsed section did not fulfill its expected size";
|
2024-06-07 22:44:13 -03:00
|
|
|
case ParseError::InvalidUtf8:
|
|
|
|
|
return "A parsed string was not valid UTF-8";
|
2021-04-27 10:29:21 -03:00
|
|
|
case ParseError::UnknownInstruction:
|
|
|
|
|
return "A parsed instruction was not known to this parser";
|
2024-07-29 23:56:00 -03:00
|
|
|
case ParseError::DuplicateSection:
|
|
|
|
|
return "Two sections of the same type were encountered";
|
2024-08-09 21:15:45 -03:00
|
|
|
case ParseError::SectionOutOfOrder:
|
|
|
|
|
return "A section encountered was not in the correct ordering";
|
2021-04-27 08:53:36 -03:00
|
|
|
}
|
|
|
|
|
return "Unknown error";
|
|
|
|
|
}
|
2025-05-13 08:06:33 -03:00
|
|
|
|
2021-04-26 05:18:13 -03:00
|
|
|
}
|