summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWasm/Parser/Parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibWasm/Parser/Parser.cpp')
-rw-r--r--Userland/Libraries/LibWasm/Parser/Parser.cpp319
1 files changed, 167 insertions, 152 deletions
diff --git a/Userland/Libraries/LibWasm/Parser/Parser.cpp b/Userland/Libraries/LibWasm/Parser/Parser.cpp
index 093635b03a..45fd96390d 100644
--- a/Userland/Libraries/LibWasm/Parser/Parser.cpp
+++ b/Userland/Libraries/LibWasm/Parser/Parser.cpp
@@ -8,25 +8,27 @@
#include <AK/LEB128.h>
#include <AK/ScopeGuard.h>
#include <AK/ScopeLogger.h>
+#include <LibCore/MemoryStream.h>
#include <LibWasm/Types.h>
namespace Wasm {
-ParseError with_eof_check(InputStream const& stream, ParseError error_if_not_eof)
+ParseError with_eof_check(Core::Stream::Stream const& stream, ParseError error_if_not_eof)
{
- if (stream.unreliable_eof())
+ if (stream.is_eof())
return ParseError::UnexpectedEof;
return error_if_not_eof;
}
template<typename T>
-static auto parse_vector(InputStream& stream)
+static auto parse_vector(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger;
if constexpr (requires { T::parse(stream); }) {
using ResultT = typename decltype(T::parse(stream))::ValueType;
size_t count;
- if (!LEB128::read_unsigned(stream, count))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, count))
return ParseResult<Vector<ResultT>> { with_eof_check(stream, ParseError::ExpectedSize) };
Vector<ResultT> entries;
@@ -39,26 +41,29 @@ static auto parse_vector(InputStream& stream)
return ParseResult<Vector<ResultT>> { move(entries) };
} else {
size_t count;
- if (!LEB128::read_unsigned(stream, count))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, count))
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
Vector<T> entries;
for (size_t i = 0; i < count; ++i) {
if constexpr (IsSame<T, size_t>) {
size_t value;
- if (!LEB128::read_unsigned(stream, value))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, value))
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
entries.append(value);
} else if constexpr (IsSame<T, ssize_t>) {
ssize_t value;
- if (!LEB128::read_signed(stream, value))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_signed(wrapped_stream, value))
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
entries.append(value);
} else if constexpr (IsSame<T, u8>) {
if (count > Constants::max_allowed_vector_size)
return ParseResult<Vector<T>> { ParseError::HugeAllocationRequested };
entries.resize(count);
- if (!stream.read_or_error({ entries.data(), entries.size() }))
+ if (stream.read_entire_buffer({ entries.data(), entries.size() }).is_error())
return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::InvalidInput) };
break; // Note: We read this all in one go!
}
@@ -67,7 +72,7 @@ static auto parse_vector(InputStream& stream)
}
}
-static ParseResult<DeprecatedString> parse_name(InputStream& stream)
+static ParseResult<DeprecatedString> parse_name(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger;
auto data = parse_vector<u8>(stream);
@@ -83,24 +88,20 @@ struct ParseUntilAnyOfResult {
Vector<T> values;
};
template<typename T, u8... terminators, typename... Args>
-static ParseResult<ParseUntilAnyOfResult<T>> parse_until_any_of(InputStream& stream, Args&... args)
-requires(requires(InputStream& stream, Args... args) { T::parse(stream, args...); })
+static ParseResult<ParseUntilAnyOfResult<T>> parse_until_any_of(Core::Stream::Stream& stream, Args&... args)
+requires(requires(Core::Stream::Stream& stream, Args... args) { T::parse(stream, args...); })
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger;
ReconsumableStream new_stream { stream };
- ScopeGuard drain_errors {
- [&] {
- new_stream.handle_any_error();
- }
- };
ParseUntilAnyOfResult<T> result;
for (;;) {
- u8 byte;
- new_stream >> byte;
- if (new_stream.has_any_error())
+ auto byte_or_error = new_stream.read_value<u8>();
+ if (byte_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedValueOrTerminator);
+ auto byte = byte_or_error.release_value();
+
constexpr auto equals = [](auto&& a, auto&& b) { return a == b; };
if ((... || equals(byte, terminators))) {
@@ -117,13 +118,15 @@ requires(requires(InputStream& stream, Args... args) { T::parse(stream, args...)
}
}
-ParseResult<ValueType> ValueType::parse(InputStream& stream)
+ParseResult<ValueType> ValueType::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ValueType"sv);
- u8 tag;
- stream >> tag;
- if (stream.has_any_error())
+ auto tag_or_error = stream.read_value<u8>();
+ if (tag_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+
+ auto tag = tag_or_error.release_value();
+
switch (tag) {
case Constants::i32_tag:
return ValueType(I32);
@@ -142,7 +145,7 @@ ParseResult<ValueType> ValueType::parse(InputStream& stream)
}
}
-ParseResult<ResultType> ResultType::parse(InputStream& stream)
+ParseResult<ResultType> ResultType::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ResultType"sv);
auto types = parse_vector<ValueType>(stream);
@@ -151,14 +154,15 @@ ParseResult<ResultType> ResultType::parse(InputStream& stream)
return ResultType { types.release_value() };
}
-ParseResult<FunctionType> FunctionType::parse(InputStream& stream)
+ParseResult<FunctionType> FunctionType::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionType"sv);
- u8 tag;
- stream >> tag;
- if (stream.has_any_error())
+ auto tag_or_error = stream.read_value<u8>();
+ if (tag_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto tag = tag_or_error.release_value();
+
if (tag != Constants::function_signature_tag) {
dbgln("Expected 0x60, but found {:#x}", tag);
return with_eof_check(stream, ParseError::InvalidTag);
@@ -174,25 +178,28 @@ ParseResult<FunctionType> FunctionType::parse(InputStream& stream)
return FunctionType { parameters_result.release_value(), results_result.release_value() };
}
-ParseResult<Limits> Limits::parse(InputStream& stream)
+ParseResult<Limits> Limits::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Limits"sv);
- u8 flag;
- stream >> flag;
- if (stream.has_any_error())
+ auto flag_or_error = stream.read_value<u8>();
+ if (flag_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto flag = flag_or_error.release_value();
+
if (flag > 1)
return with_eof_check(stream, ParseError::InvalidTag);
size_t min;
- if (!LEB128::read_unsigned(stream, min))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, min))
return with_eof_check(stream, ParseError::ExpectedSize);
Optional<u32> max;
if (flag) {
size_t value;
- if (!LEB128::read_unsigned(stream, value))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, value))
return with_eof_check(stream, ParseError::ExpectedSize);
max = value;
}
@@ -200,7 +207,7 @@ ParseResult<Limits> Limits::parse(InputStream& stream)
return Limits { static_cast<u32>(min), move(max) };
}
-ParseResult<MemoryType> MemoryType::parse(InputStream& stream)
+ParseResult<MemoryType> MemoryType::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemoryType"sv);
auto limits_result = Limits::parse(stream);
@@ -209,7 +216,7 @@ ParseResult<MemoryType> MemoryType::parse(InputStream& stream)
return MemoryType { limits_result.release_value() };
}
-ParseResult<TableType> TableType::parse(InputStream& stream)
+ParseResult<TableType> TableType::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableType"sv);
auto type_result = ValueType::parse(stream);
@@ -223,50 +230,48 @@ ParseResult<TableType> TableType::parse(InputStream& stream)
return TableType { type_result.release_value(), limits_result.release_value() };
}
-ParseResult<GlobalType> GlobalType::parse(InputStream& stream)
+ParseResult<GlobalType> GlobalType::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalType"sv);
auto type_result = ValueType::parse(stream);
if (type_result.is_error())
return type_result.error();
- u8 mutable_;
- stream >> mutable_;
- if (stream.has_any_error())
+ auto mutable_or_error = stream.read_value<u8>();
+ if (mutable_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto mutable_ = mutable_or_error.release_value();
+
if (mutable_ > 1)
return with_eof_check(stream, ParseError::InvalidTag);
return GlobalType { type_result.release_value(), mutable_ == 0x01 };
}
-ParseResult<BlockType> BlockType::parse(InputStream& stream)
+ParseResult<BlockType> BlockType::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("BlockType"sv);
- u8 kind;
- stream >> kind;
- if (stream.has_any_error())
+ auto kind_or_error = stream.read_value<u8>();
+ if (kind_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+
+ auto kind = kind_or_error.release_value();
if (kind == Constants::empty_block_tag)
return BlockType {};
{
- InputMemoryStream value_stream { ReadonlyBytes { &kind, 1 } };
- if (auto value_type = ValueType::parse(value_stream); !value_type.is_error())
+ auto value_stream = Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { &kind, 1 }).release_value_but_fixme_should_propagate_errors();
+ if (auto value_type = ValueType::parse(*value_stream); !value_type.is_error())
return BlockType { value_type.release_value() };
}
ReconsumableStream new_stream { stream };
new_stream.unread({ &kind, 1 });
- ScopeGuard drain_errors {
- [&] {
- new_stream.handle_any_error();
- }
- };
ssize_t index_value;
- if (!LEB128::read_signed(new_stream, index_value))
+ Core::Stream::WrapInAKInputStream wrapped_new_stream { new_stream };
+ if (!LEB128::read_signed(wrapped_new_stream, index_value))
return with_eof_check(stream, ParseError::ExpectedIndex);
if (index_value < 0) {
@@ -277,7 +282,7 @@ ParseResult<BlockType> BlockType::parse(InputStream& stream)
return BlockType { TypeIndex(index_value) };
}
-ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, InstructionPointer& ip)
+ParseResult<Vector<Instruction>> Instruction::parse(Core::Stream::Stream& stream, InstructionPointer& ip)
{
struct NestedInstructionState {
Vector<Instruction> prior_instructions;
@@ -291,11 +296,12 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
do {
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Instruction"sv);
- u8 byte;
- stream >> byte;
- if (stream.has_any_error())
+ auto byte_or_error = stream.read_value<u8>();
+ if (byte_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto byte = byte_or_error.release_value();
+
if (!nested_instructions.is_empty()) {
auto& nested_structure = nested_instructions.last();
if (byte == 0x0b) {
@@ -408,13 +414,15 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
case Instructions::i64_store8.value():
case Instructions::i64_store16.value():
case Instructions::i64_store32.value(): {
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+
// op (align offset)
size_t align;
- if (!LEB128::read_unsigned(stream, align))
+ if (!LEB128::read_unsigned(wrapped_stream, align))
return with_eof_check(stream, ParseError::InvalidInput);
size_t offset;
- if (!LEB128::read_unsigned(stream, offset))
+ if (!LEB128::read_unsigned(wrapped_stream, offset))
return with_eof_check(stream, ParseError::InvalidInput);
resulting_instructions.append(Instruction { opcode, MemoryArgument { static_cast<u32>(align), static_cast<u32>(offset) } });
@@ -443,10 +451,11 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
case Instructions::memory_grow.value(): {
// op 0x0
// The zero is currently unused.
- u8 unused;
- stream >> unused;
- if (stream.has_any_error())
+ auto unused_or_error = stream.read_value<u8>();
+ if (unused_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+
+ auto unused = unused_or_error.release_value();
if (unused != 0x00) {
dbgln("Invalid tag in memory_grow {}", unused);
return with_eof_check(stream, ParseError::InvalidTag);
@@ -457,7 +466,8 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
}
case Instructions::i32_const.value(): {
i32 value;
- if (!LEB128::read_signed(stream, value))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_signed(wrapped_stream, value))
return with_eof_check(stream, ParseError::ExpectedSignedImmediate);
resulting_instructions.append(Instruction { opcode, value });
@@ -466,7 +476,8 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
case Instructions::i64_const.value(): {
// op literal
i64 value;
- if (!LEB128::read_signed(stream, value))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_signed(wrapped_stream, value))
return with_eof_check(stream, ParseError::ExpectedSignedImmediate);
resulting_instructions.append(Instruction { opcode, value });
@@ -475,8 +486,7 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
case Instructions::f32_const.value(): {
// op literal
LittleEndian<u32> value;
- stream >> value;
- if (stream.has_any_error())
+ if (stream.read_entire_buffer(value.bytes()).is_error())
return with_eof_check(stream, ParseError::ExpectedFloatingImmediate);
auto floating = bit_cast<float>(static_cast<u32>(value));
@@ -486,8 +496,7 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
case Instructions::f64_const.value(): {
// op literal
LittleEndian<u64> value;
- stream >> value;
- if (stream.has_any_error())
+ if (stream.read_entire_buffer(value.bytes()).is_error())
return with_eof_check(stream, ParseError::ExpectedFloatingImmediate);
auto floating = bit_cast<double>(static_cast<u64>(value));
@@ -668,7 +677,8 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
case 0xfc: {
// These are multibyte instructions.
u32 selector;
- if (!LEB128::read_unsigned(stream, selector))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, selector))
return with_eof_check(stream, ParseError::InvalidInput);
switch (selector) {
case Instructions::i32_trunc_sat_f32_s_second:
@@ -685,10 +695,11 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
auto index = GenericIndexParser<DataIndex>::parse(stream);
if (index.is_error())
return index.error();
- u8 unused;
- stream >> unused;
- if (stream.has_any_error())
+ auto unused_or_error = stream.read_value<u8>();
+ if (unused_or_error.is_error())
return with_eof_check(stream, ParseError::InvalidInput);
+
+ auto unused = unused_or_error.release_value();
if (unused != 0x00)
return ParseError::InvalidImmediate;
resulting_instructions.append(Instruction { OpCode { 0xfc00 | selector }, index.release_value() });
@@ -703,10 +714,11 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
}
case Instructions::memory_copy_second: {
for (size_t i = 0; i < 2; ++i) {
- u8 unused;
- stream >> unused;
- if (stream.has_any_error())
+ auto unused_or_error = stream.read_value<u8>();
+ if (unused_or_error.is_error())
return with_eof_check(stream, ParseError::InvalidInput);
+
+ auto unused = unused_or_error.release_value();
if (unused != 0x00)
return ParseError::InvalidImmediate;
}
@@ -714,10 +726,11 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
break;
}
case Instructions::memory_fill_second: {
- u8 unused;
- stream >> unused;
- if (stream.has_any_error())
+ auto unused_or_error = stream.read_value<u8>();
+ if (unused_or_error.is_error())
return with_eof_check(stream, ParseError::InvalidInput);
+
+ auto unused = unused_or_error.release_value();
if (unused != 0x00)
return ParseError::InvalidImmediate;
resulting_instructions.append(Instruction { OpCode { 0xfc00 | selector } });
@@ -769,7 +782,7 @@ ParseResult<Vector<Instruction>> Instruction::parse(InputStream& stream, Instruc
return resulting_instructions;
}
-ParseResult<CustomSection> CustomSection::parse(InputStream& stream)
+ParseResult<CustomSection> CustomSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("CustomSection"sv);
auto name = parse_name(stream);
@@ -780,9 +793,12 @@ ParseResult<CustomSection> CustomSection::parse(InputStream& stream)
if (data_buffer.try_resize(64).is_error())
return ParseError::OutOfMemory;
- while (!stream.has_any_error() && !stream.unreliable_eof()) {
+ while (!stream.is_eof()) {
char buf[16];
- auto size = stream.read({ buf, 16 });
+ auto span_or_error = stream.read({ buf, 16 });
+ if (span_or_error.is_error())
+ break;
+ auto size = span_or_error.release_value().size();
if (size == 0)
break;
if (data_buffer.try_append(buf, size).is_error())
@@ -792,7 +808,7 @@ ParseResult<CustomSection> CustomSection::parse(InputStream& stream)
return CustomSection(name.release_value(), move(data_buffer));
}
-ParseResult<TypeSection> TypeSection::parse(InputStream& stream)
+ParseResult<TypeSection> TypeSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TypeSection"sv);
auto types = parse_vector<FunctionType>(stream);
@@ -801,7 +817,7 @@ ParseResult<TypeSection> TypeSection::parse(InputStream& stream)
return TypeSection { types.release_value() };
}
-ParseResult<ImportSection::Import> ImportSection::Import::parse(InputStream& stream)
+ParseResult<ImportSection::Import> ImportSection::Import::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Import"sv);
auto module = parse_name(stream);
@@ -810,11 +826,12 @@ ParseResult<ImportSection::Import> ImportSection::Import::parse(InputStream& str
auto name = parse_name(stream);
if (name.is_error())
return name.error();
- u8 tag;
- stream >> tag;
- if (stream.has_any_error())
+ auto tag_or_error = stream.read_value<u8>();
+ if (tag_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto tag = tag_or_error.release_value();
+
switch (tag) {
case Constants::extern_function_tag: {
auto index = GenericIndexParser<TypeIndex>::parse(stream);
@@ -833,7 +850,7 @@ ParseResult<ImportSection::Import> ImportSection::Import::parse(InputStream& str
}
}
-ParseResult<ImportSection> ImportSection::parse(InputStream& stream)
+ParseResult<ImportSection> ImportSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ImportSection"sv);
auto imports = parse_vector<Import>(stream);
@@ -842,7 +859,7 @@ ParseResult<ImportSection> ImportSection::parse(InputStream& stream)
return ImportSection { imports.release_value() };
}
-ParseResult<FunctionSection> FunctionSection::parse(InputStream& stream)
+ParseResult<FunctionSection> FunctionSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionSection"sv);
auto indices = parse_vector<size_t>(stream);
@@ -857,7 +874,7 @@ ParseResult<FunctionSection> FunctionSection::parse(InputStream& stream)
return FunctionSection { move(typed_indices) };
}
-ParseResult<TableSection::Table> TableSection::Table::parse(InputStream& stream)
+ParseResult<TableSection::Table> TableSection::Table::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Table"sv);
auto type = TableType::parse(stream);
@@ -866,7 +883,7 @@ ParseResult<TableSection::Table> TableSection::Table::parse(InputStream& stream)
return Table { type.release_value() };
}
-ParseResult<TableSection> TableSection::parse(InputStream& stream)
+ParseResult<TableSection> TableSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableSection"sv);
auto tables = parse_vector<Table>(stream);
@@ -875,7 +892,7 @@ ParseResult<TableSection> TableSection::parse(InputStream& stream)
return TableSection { tables.release_value() };
}
-ParseResult<MemorySection::Memory> MemorySection::Memory::parse(InputStream& stream)
+ParseResult<MemorySection::Memory> MemorySection::Memory::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Memory"sv);
auto type = MemoryType::parse(stream);
@@ -884,7 +901,7 @@ ParseResult<MemorySection::Memory> MemorySection::Memory::parse(InputStream& str
return Memory { type.release_value() };
}
-ParseResult<MemorySection> MemorySection::parse(InputStream& stream)
+ParseResult<MemorySection> MemorySection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemorySection"sv);
auto memories = parse_vector<Memory>(stream);
@@ -893,7 +910,7 @@ ParseResult<MemorySection> MemorySection::parse(InputStream& stream)
return MemorySection { memories.release_value() };
}
-ParseResult<Expression> Expression::parse(InputStream& stream)
+ParseResult<Expression> Expression::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Expression"sv);
InstructionPointer ip { 0 };
@@ -904,7 +921,7 @@ ParseResult<Expression> Expression::parse(InputStream& stream)
return Expression { move(instructions.value().values) };
}
-ParseResult<GlobalSection::Global> GlobalSection::Global::parse(InputStream& stream)
+ParseResult<GlobalSection::Global> GlobalSection::Global::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Global"sv);
auto type = GlobalType::parse(stream);
@@ -916,7 +933,7 @@ ParseResult<GlobalSection::Global> GlobalSection::Global::parse(InputStream& str
return Global { type.release_value(), exprs.release_value() };
}
-ParseResult<GlobalSection> GlobalSection::parse(InputStream& stream)
+ParseResult<GlobalSection> GlobalSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalSection"sv);
auto result = parse_vector<Global>(stream);
@@ -925,19 +942,21 @@ ParseResult<GlobalSection> GlobalSection::parse(InputStream& stream)
return GlobalSection { result.release_value() };
}
-ParseResult<ExportSection::Export> ExportSection::Export::parse(InputStream& stream)
+ParseResult<ExportSection::Export> ExportSection::Export::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Export"sv);
auto name = parse_name(stream);
if (name.is_error())
return name.error();
- u8 tag;
- stream >> tag;
- if (stream.has_any_error())
+ auto tag_or_error = stream.read_value<u8>();
+ if (tag_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto tag = tag_or_error.release_value();
+
size_t index;
- if (!LEB128::read_unsigned(stream, index))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, index))
return with_eof_check(stream, ParseError::ExpectedIndex);
switch (tag) {
@@ -954,7 +973,7 @@ ParseResult<ExportSection::Export> ExportSection::Export::parse(InputStream& str
}
}
-ParseResult<ExportSection> ExportSection::parse(InputStream& stream)
+ParseResult<ExportSection> ExportSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ExportSection"sv);
auto result = parse_vector<Export>(stream);
@@ -963,7 +982,7 @@ ParseResult<ExportSection> ExportSection::parse(InputStream& stream)
return ExportSection { result.release_value() };
}
-ParseResult<StartSection::StartFunction> StartSection::StartFunction::parse(InputStream& stream)
+ParseResult<StartSection::StartFunction> StartSection::StartFunction::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartFunction"sv);
auto index = GenericIndexParser<FunctionIndex>::parse(stream);
@@ -972,7 +991,7 @@ ParseResult<StartSection::StartFunction> StartSection::StartFunction::parse(Inpu
return StartFunction { index.release_value() };
}
-ParseResult<StartSection> StartSection::parse(InputStream& stream)
+ParseResult<StartSection> StartSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartSection"sv);
auto result = StartFunction::parse(stream);
@@ -981,7 +1000,7 @@ ParseResult<StartSection> StartSection::parse(InputStream& stream)
return StartSection { result.release_value() };
}
-ParseResult<ElementSection::SegmentType0> ElementSection::SegmentType0::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType0> ElementSection::SegmentType0::parse(Core::Stream::Stream& stream)
{
auto expression = Expression::parse(stream);
if (expression.is_error())
@@ -993,12 +1012,13 @@ ParseResult<ElementSection::SegmentType0> ElementSection::SegmentType0::parse(In
return SegmentType0 { indices.release_value(), Active { 0, expression.release_value() } };
}
-ParseResult<ElementSection::SegmentType1> ElementSection::SegmentType1::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType1> ElementSection::SegmentType1::parse(Core::Stream::Stream& stream)
{
- u8 kind;
- stream >> kind;
- if (stream.has_any_error())
+ auto kind_or_error = stream.read_value<u8>();
+ if (kind_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+
+ auto kind = kind_or_error.release_value();
if (kind != 0)
return ParseError::InvalidTag;
auto indices = parse_vector<GenericIndexParser<FunctionIndex>>(stream);
@@ -1008,56 +1028,57 @@ ParseResult<ElementSection::SegmentType1> ElementSection::SegmentType1::parse(In
return SegmentType1 { indices.release_value() };
}
-ParseResult<ElementSection::SegmentType2> ElementSection::SegmentType2::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType2> ElementSection::SegmentType2::parse(Core::Stream::Stream& stream)
{
dbgln("Type 2");
(void)stream;
return ParseError::NotImplemented;
}
-ParseResult<ElementSection::SegmentType3> ElementSection::SegmentType3::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType3> ElementSection::SegmentType3::parse(Core::Stream::Stream& stream)
{
dbgln("Type 3");
(void)stream;
return ParseError::NotImplemented;
}
-ParseResult<ElementSection::SegmentType4> ElementSection::SegmentType4::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType4> ElementSection::SegmentType4::parse(Core::Stream::Stream& stream)
{
dbgln("Type 4");
(void)stream;
return ParseError::NotImplemented;
}
-ParseResult<ElementSection::SegmentType5> ElementSection::SegmentType5::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType5> ElementSection::SegmentType5::parse(Core::Stream::Stream& stream)
{
dbgln("Type 5");
(void)stream;
return ParseError::NotImplemented;
}
-ParseResult<ElementSection::SegmentType6> ElementSection::SegmentType6::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType6> ElementSection::SegmentType6::parse(Core::Stream::Stream& stream)
{
dbgln("Type 6");
(void)stream;
return ParseError::NotImplemented;
}
-ParseResult<ElementSection::SegmentType7> ElementSection::SegmentType7::parse(InputStream& stream)
+ParseResult<ElementSection::SegmentType7> ElementSection::SegmentType7::parse(Core::Stream::Stream& stream)
{
dbgln("Type 7");
(void)stream;
return ParseError::NotImplemented;
}
-ParseResult<ElementSection::Element> ElementSection::Element::parse(InputStream& stream)
+ParseResult<ElementSection::Element> ElementSection::Element::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Element"sv);
- u8 tag;
- stream >> tag;
- if (stream.has_any_error())
+ auto tag_or_error = stream.read_value<u8>();
+ if (tag_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto tag = tag_or_error.release_value();
+
switch (tag) {
case 0x00:
if (auto result = SegmentType0::parse(stream); result.is_error()) {
@@ -1118,7 +1139,7 @@ ParseResult<ElementSection::Element> ElementSection::Element::parse(InputStream&
}
}
-ParseResult<ElementSection> ElementSection::parse(InputStream& stream)
+ParseResult<ElementSection> ElementSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("ElementSection"sv);
auto result = parse_vector<Element>(stream);
@@ -1127,11 +1148,12 @@ ParseResult<ElementSection> ElementSection::parse(InputStream& stream)
return ElementSection { result.release_value() };
}
-ParseResult<Locals> Locals::parse(InputStream& stream)
+ParseResult<Locals> Locals::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Locals"sv);
size_t count;
- if (!LEB128::read_unsigned(stream, count))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, count))
return with_eof_check(stream, ParseError::InvalidSize);
if (count > Constants::max_allowed_function_locals_per_type)
@@ -1144,7 +1166,7 @@ ParseResult<Locals> Locals::parse(InputStream& stream)
return Locals { static_cast<u32>(count), type.release_value() };
}
-ParseResult<CodeSection::Func> CodeSection::Func::parse(InputStream& stream)
+ParseResult<CodeSection::Func> CodeSection::Func::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Func"sv);
auto locals = parse_vector<Locals>(stream);
@@ -1156,19 +1178,15 @@ ParseResult<CodeSection::Func> CodeSection::Func::parse(InputStream& stream)
return Func { locals.release_value(), body.release_value() };
}
-ParseResult<CodeSection::Code> CodeSection::Code::parse(InputStream& stream)
+ParseResult<CodeSection::Code> CodeSection::Code::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Code"sv);
size_t size;
- if (!LEB128::read_unsigned(stream, size))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, size))
return with_eof_check(stream, ParseError::InvalidSize);
auto constrained_stream = ConstrainedStream { stream, size };
- ScopeGuard drain_errors {
- [&] {
- constrained_stream.handle_any_error();
- }
- };
auto func = Func::parse(constrained_stream);
if (func.is_error())
@@ -1177,7 +1195,7 @@ ParseResult<CodeSection::Code> CodeSection::Code::parse(InputStream& stream)
return Code { static_cast<u32>(size), func.release_value() };
}
-ParseResult<CodeSection> CodeSection::parse(InputStream& stream)
+ParseResult<CodeSection> CodeSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("CodeSection"sv);
auto result = parse_vector<Code>(stream);
@@ -1186,14 +1204,15 @@ ParseResult<CodeSection> CodeSection::parse(InputStream& stream)
return CodeSection { result.release_value() };
}
-ParseResult<DataSection::Data> DataSection::Data::parse(InputStream& stream)
+ParseResult<DataSection::Data> DataSection::Data::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Data"sv);
- u8 tag;
- stream >> tag;
- if (stream.has_any_error())
+ auto tag_or_error = stream.read_value<u8>();
+ if (tag_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedKindTag);
+ auto tag = tag_or_error.release_value();
+
if (tag > 0x02)
return with_eof_check(stream, ParseError::InvalidTag);
@@ -1227,7 +1246,7 @@ ParseResult<DataSection::Data> DataSection::Data::parse(InputStream& stream)
VERIFY_NOT_REACHED();
}
-ParseResult<DataSection> DataSection::parse(InputStream& stream)
+ParseResult<DataSection> DataSection::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataSection"sv);
auto data = parse_vector<Data>(stream);
@@ -1237,12 +1256,13 @@ ParseResult<DataSection> DataSection::parse(InputStream& stream)
return DataSection { data.release_value() };
}
-ParseResult<DataCountSection> DataCountSection::parse([[maybe_unused]] InputStream& stream)
+ParseResult<DataCountSection> DataCountSection::parse([[maybe_unused]] Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataCountSection"sv);
u32 value;
- if (!LEB128::read_unsigned(stream, value)) {
- if (stream.unreliable_eof()) {
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, value)) {
+ if (stream.is_eof()) {
// The section simply didn't contain anything.
return DataCountSection { {} };
}
@@ -1252,41 +1272,36 @@ ParseResult<DataCountSection> DataCountSection::parse([[maybe_unused]] InputStre
return DataCountSection { value };
}
-ParseResult<Module> Module::parse(InputStream& stream)
+ParseResult<Module> Module::parse(Core::Stream::Stream& stream)
{
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Module"sv);
u8 buf[4];
- if (!stream.read_or_error({ buf, 4 }))
+ if (stream.read_entire_buffer({ buf, 4 }).is_error())
return with_eof_check(stream, ParseError::InvalidInput);
if (Bytes { buf, 4 } != wasm_magic.span())
return with_eof_check(stream, ParseError::InvalidModuleMagic);
- if (!stream.read_or_error({ buf, 4 }))
+ if (stream.read_entire_buffer({ buf, 4 }).is_error())
return with_eof_check(stream, ParseError::InvalidInput);
if (Bytes { buf, 4 } != wasm_version.span())
return with_eof_check(stream, ParseError::InvalidModuleVersion);
Vector<AnySection> sections;
for (;;) {
- u8 section_id;
- stream >> section_id;
- if (stream.unreliable_eof()) {
- stream.handle_any_error();
+ auto section_id_or_error = stream.read_value<u8>();
+ if (stream.is_eof())
break;
- }
- if (stream.has_any_error())
+ if (section_id_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedIndex);
+ auto section_id = section_id_or_error.release_value();
+
size_t section_size;
- if (!LEB128::read_unsigned(stream, section_size))
+ Core::Stream::WrapInAKInputStream wrapped_stream { stream };
+ if (!LEB128::read_unsigned(wrapped_stream, section_size))
return with_eof_check(stream, ParseError::ExpectedSize);
auto section_stream = ConstrainedStream { stream, section_size };
- ScopeGuard drain_errors {
- [&] {
- section_stream.handle_any_error();
- }
- };
switch (section_id) {
case CustomSection::section_id: