diff --git a/Meta/Lagom/Fuzzers/FuzzWasmParser.cpp b/Meta/Lagom/Fuzzers/FuzzWasmParser.cpp index d5c5acbe17..6ef9605e0f 100644 --- a/Meta/Lagom/Fuzzers/FuzzWasmParser.cpp +++ b/Meta/Lagom/Fuzzers/FuzzWasmParser.cpp @@ -4,8 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include -#include +#include #include #include #include @@ -13,8 +12,10 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { ReadonlyBytes bytes { data, size }; - InputMemoryStream stream { bytes }; - [[maybe_unused]] auto result = Wasm::Module::parse(stream); - stream.handle_any_error(); + auto stream_or_error = Core::Stream::FixedMemoryStream::construct(bytes); + if (stream_or_error.is_error()) + return 0; + auto stream = stream_or_error.release_value(); + [[maybe_unused]] auto result = Wasm::Module::parse(*stream); return 0; } diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index 1e3ebf988f..d7566c4853 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -105,19 +106,11 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) if (!is(object)) return vm.throw_completion("Expected a Uint8Array argument to parse_webassembly_module"); auto& array = static_cast(*object); - InputMemoryStream stream { array.data() }; - ScopeGuard handle_stream_error { - [&] { - stream.handle_any_error(); - } - }; - auto result = Wasm::Module::parse(stream); + auto stream = Core::Stream::FixedMemoryStream::construct(array.data()).release_value_but_fixme_should_propagate_errors(); + auto result = Wasm::Module::parse(*stream); if (result.is_error()) return vm.throw_completion(Wasm::parse_error_to_deprecated_string(result.error())); - if (stream.handle_any_error()) - return vm.throw_completion("Binary stream contained errors"); - HashMap imports; auto import_value = vm.argument(1); if (import_value.is_object()) { 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 #include #include +#include #include 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 -static auto parse_vector(InputStream& stream) +static auto parse_vector(Core::Stream::Stream& stream) { ScopeLogger 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> { with_eof_check(stream, ParseError::ExpectedSize) }; Vector entries; @@ -39,26 +41,29 @@ static auto parse_vector(InputStream& stream) return ParseResult> { 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> { with_eof_check(stream, ParseError::ExpectedSize) }; Vector entries; for (size_t i = 0; i < count; ++i) { if constexpr (IsSame) { size_t value; - if (!LEB128::read_unsigned(stream, value)) + Core::Stream::WrapInAKInputStream wrapped_stream { stream }; + if (!LEB128::read_unsigned(wrapped_stream, value)) return ParseResult> { with_eof_check(stream, ParseError::ExpectedSize) }; entries.append(value); } else if constexpr (IsSame) { ssize_t value; - if (!LEB128::read_signed(stream, value)) + Core::Stream::WrapInAKInputStream wrapped_stream { stream }; + if (!LEB128::read_signed(wrapped_stream, value)) return ParseResult> { with_eof_check(stream, ParseError::ExpectedSize) }; entries.append(value); } else if constexpr (IsSame) { if (count > Constants::max_allowed_vector_size) return ParseResult> { 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> { 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 parse_name(InputStream& stream) +static ParseResult parse_name(Core::Stream::Stream& stream) { ScopeLogger logger; auto data = parse_vector(stream); @@ -83,24 +88,20 @@ struct ParseUntilAnyOfResult { Vector values; }; template -static ParseResult> parse_until_any_of(InputStream& stream, Args&... args) -requires(requires(InputStream& stream, Args... args) { T::parse(stream, args...); }) +static ParseResult> parse_until_any_of(Core::Stream::Stream& stream, Args&... args) +requires(requires(Core::Stream::Stream& stream, Args... args) { T::parse(stream, args...); }) { ScopeLogger logger; ReconsumableStream new_stream { stream }; - ScopeGuard drain_errors { - [&] { - new_stream.handle_any_error(); - } - }; ParseUntilAnyOfResult result; for (;;) { - u8 byte; - new_stream >> byte; - if (new_stream.has_any_error()) + auto byte_or_error = new_stream.read_value(); + 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::parse(InputStream& stream) +ParseResult ValueType::parse(Core::Stream::Stream& stream) { ScopeLogger logger("ValueType"sv); - u8 tag; - stream >> tag; - if (stream.has_any_error()) + auto tag_or_error = stream.read_value(); + 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::parse(InputStream& stream) } } -ParseResult ResultType::parse(InputStream& stream) +ParseResult ResultType::parse(Core::Stream::Stream& stream) { ScopeLogger logger("ResultType"sv); auto types = parse_vector(stream); @@ -151,14 +154,15 @@ ParseResult ResultType::parse(InputStream& stream) return ResultType { types.release_value() }; } -ParseResult FunctionType::parse(InputStream& stream) +ParseResult FunctionType::parse(Core::Stream::Stream& stream) { ScopeLogger logger("FunctionType"sv); - u8 tag; - stream >> tag; - if (stream.has_any_error()) + auto tag_or_error = stream.read_value(); + 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::parse(InputStream& stream) return FunctionType { parameters_result.release_value(), results_result.release_value() }; } -ParseResult Limits::parse(InputStream& stream) +ParseResult Limits::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Limits"sv); - u8 flag; - stream >> flag; - if (stream.has_any_error()) + auto flag_or_error = stream.read_value(); + 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 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::parse(InputStream& stream) return Limits { static_cast(min), move(max) }; } -ParseResult MemoryType::parse(InputStream& stream) +ParseResult MemoryType::parse(Core::Stream::Stream& stream) { ScopeLogger logger("MemoryType"sv); auto limits_result = Limits::parse(stream); @@ -209,7 +216,7 @@ ParseResult MemoryType::parse(InputStream& stream) return MemoryType { limits_result.release_value() }; } -ParseResult TableType::parse(InputStream& stream) +ParseResult TableType::parse(Core::Stream::Stream& stream) { ScopeLogger logger("TableType"sv); auto type_result = ValueType::parse(stream); @@ -223,50 +230,48 @@ ParseResult TableType::parse(InputStream& stream) return TableType { type_result.release_value(), limits_result.release_value() }; } -ParseResult GlobalType::parse(InputStream& stream) +ParseResult GlobalType::parse(Core::Stream::Stream& stream) { ScopeLogger 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(); + 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::parse(InputStream& stream) +ParseResult BlockType::parse(Core::Stream::Stream& stream) { ScopeLogger logger("BlockType"sv); - u8 kind; - stream >> kind; - if (stream.has_any_error()) + auto kind_or_error = stream.read_value(); + 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::parse(InputStream& stream) return BlockType { TypeIndex(index_value) }; } -ParseResult> Instruction::parse(InputStream& stream, InstructionPointer& ip) +ParseResult> Instruction::parse(Core::Stream::Stream& stream, InstructionPointer& ip) { struct NestedInstructionState { Vector prior_instructions; @@ -291,11 +296,12 @@ ParseResult> Instruction::parse(InputStream& stream, Instruc do { ScopeLogger logger("Instruction"sv); - u8 byte; - stream >> byte; - if (stream.has_any_error()) + auto byte_or_error = stream.read_value(); + 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> 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(align), static_cast(offset) } }); @@ -443,10 +451,11 @@ ParseResult> 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(); + 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> 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> 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> Instruction::parse(InputStream& stream, Instruc case Instructions::f32_const.value(): { // op literal LittleEndian 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(static_cast(value)); @@ -486,8 +496,7 @@ ParseResult> Instruction::parse(InputStream& stream, Instruc case Instructions::f64_const.value(): { // op literal LittleEndian 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(static_cast(value)); @@ -668,7 +677,8 @@ ParseResult> 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> Instruction::parse(InputStream& stream, Instruc auto index = GenericIndexParser::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(); + 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> 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(); + 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> 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(); + 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> Instruction::parse(InputStream& stream, Instruc return resulting_instructions; } -ParseResult CustomSection::parse(InputStream& stream) +ParseResult CustomSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("CustomSection"sv); auto name = parse_name(stream); @@ -780,9 +793,12 @@ ParseResult 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::parse(InputStream& stream) return CustomSection(name.release_value(), move(data_buffer)); } -ParseResult TypeSection::parse(InputStream& stream) +ParseResult TypeSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("TypeSection"sv); auto types = parse_vector(stream); @@ -801,7 +817,7 @@ ParseResult TypeSection::parse(InputStream& stream) return TypeSection { types.release_value() }; } -ParseResult ImportSection::Import::parse(InputStream& stream) +ParseResult ImportSection::Import::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Import"sv); auto module = parse_name(stream); @@ -810,11 +826,12 @@ ParseResult 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(); + 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::parse(stream); @@ -833,7 +850,7 @@ ParseResult ImportSection::Import::parse(InputStream& str } } -ParseResult ImportSection::parse(InputStream& stream) +ParseResult ImportSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("ImportSection"sv); auto imports = parse_vector(stream); @@ -842,7 +859,7 @@ ParseResult ImportSection::parse(InputStream& stream) return ImportSection { imports.release_value() }; } -ParseResult FunctionSection::parse(InputStream& stream) +ParseResult FunctionSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("FunctionSection"sv); auto indices = parse_vector(stream); @@ -857,7 +874,7 @@ ParseResult FunctionSection::parse(InputStream& stream) return FunctionSection { move(typed_indices) }; } -ParseResult TableSection::Table::parse(InputStream& stream) +ParseResult TableSection::Table::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Table"sv); auto type = TableType::parse(stream); @@ -866,7 +883,7 @@ ParseResult TableSection::Table::parse(InputStream& stream) return Table { type.release_value() }; } -ParseResult TableSection::parse(InputStream& stream) +ParseResult TableSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("TableSection"sv); auto tables = parse_vector(stream); @@ -875,7 +892,7 @@ ParseResult TableSection::parse(InputStream& stream) return TableSection { tables.release_value() }; } -ParseResult MemorySection::Memory::parse(InputStream& stream) +ParseResult MemorySection::Memory::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Memory"sv); auto type = MemoryType::parse(stream); @@ -884,7 +901,7 @@ ParseResult MemorySection::Memory::parse(InputStream& str return Memory { type.release_value() }; } -ParseResult MemorySection::parse(InputStream& stream) +ParseResult MemorySection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("MemorySection"sv); auto memories = parse_vector(stream); @@ -893,7 +910,7 @@ ParseResult MemorySection::parse(InputStream& stream) return MemorySection { memories.release_value() }; } -ParseResult Expression::parse(InputStream& stream) +ParseResult Expression::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Expression"sv); InstructionPointer ip { 0 }; @@ -904,7 +921,7 @@ ParseResult Expression::parse(InputStream& stream) return Expression { move(instructions.value().values) }; } -ParseResult GlobalSection::Global::parse(InputStream& stream) +ParseResult GlobalSection::Global::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Global"sv); auto type = GlobalType::parse(stream); @@ -916,7 +933,7 @@ ParseResult GlobalSection::Global::parse(InputStream& str return Global { type.release_value(), exprs.release_value() }; } -ParseResult GlobalSection::parse(InputStream& stream) +ParseResult GlobalSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("GlobalSection"sv); auto result = parse_vector(stream); @@ -925,19 +942,21 @@ ParseResult GlobalSection::parse(InputStream& stream) return GlobalSection { result.release_value() }; } -ParseResult ExportSection::Export::parse(InputStream& stream) +ParseResult ExportSection::Export::parse(Core::Stream::Stream& stream) { ScopeLogger 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(); + 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::parse(InputStream& str } } -ParseResult ExportSection::parse(InputStream& stream) +ParseResult ExportSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("ExportSection"sv); auto result = parse_vector(stream); @@ -963,7 +982,7 @@ ParseResult ExportSection::parse(InputStream& stream) return ExportSection { result.release_value() }; } -ParseResult StartSection::StartFunction::parse(InputStream& stream) +ParseResult StartSection::StartFunction::parse(Core::Stream::Stream& stream) { ScopeLogger logger("StartFunction"sv); auto index = GenericIndexParser::parse(stream); @@ -972,7 +991,7 @@ ParseResult StartSection::StartFunction::parse(Inpu return StartFunction { index.release_value() }; } -ParseResult StartSection::parse(InputStream& stream) +ParseResult StartSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("StartSection"sv); auto result = StartFunction::parse(stream); @@ -981,7 +1000,7 @@ ParseResult StartSection::parse(InputStream& stream) return StartSection { result.release_value() }; } -ParseResult ElementSection::SegmentType0::parse(InputStream& stream) +ParseResult ElementSection::SegmentType0::parse(Core::Stream::Stream& stream) { auto expression = Expression::parse(stream); if (expression.is_error()) @@ -993,12 +1012,13 @@ ParseResult ElementSection::SegmentType0::parse(In return SegmentType0 { indices.release_value(), Active { 0, expression.release_value() } }; } -ParseResult ElementSection::SegmentType1::parse(InputStream& stream) +ParseResult ElementSection::SegmentType1::parse(Core::Stream::Stream& stream) { - u8 kind; - stream >> kind; - if (stream.has_any_error()) + auto kind_or_error = stream.read_value(); + 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>(stream); @@ -1008,56 +1028,57 @@ ParseResult ElementSection::SegmentType1::parse(In return SegmentType1 { indices.release_value() }; } -ParseResult ElementSection::SegmentType2::parse(InputStream& stream) +ParseResult ElementSection::SegmentType2::parse(Core::Stream::Stream& stream) { dbgln("Type 2"); (void)stream; return ParseError::NotImplemented; } -ParseResult ElementSection::SegmentType3::parse(InputStream& stream) +ParseResult ElementSection::SegmentType3::parse(Core::Stream::Stream& stream) { dbgln("Type 3"); (void)stream; return ParseError::NotImplemented; } -ParseResult ElementSection::SegmentType4::parse(InputStream& stream) +ParseResult ElementSection::SegmentType4::parse(Core::Stream::Stream& stream) { dbgln("Type 4"); (void)stream; return ParseError::NotImplemented; } -ParseResult ElementSection::SegmentType5::parse(InputStream& stream) +ParseResult ElementSection::SegmentType5::parse(Core::Stream::Stream& stream) { dbgln("Type 5"); (void)stream; return ParseError::NotImplemented; } -ParseResult ElementSection::SegmentType6::parse(InputStream& stream) +ParseResult ElementSection::SegmentType6::parse(Core::Stream::Stream& stream) { dbgln("Type 6"); (void)stream; return ParseError::NotImplemented; } -ParseResult ElementSection::SegmentType7::parse(InputStream& stream) +ParseResult ElementSection::SegmentType7::parse(Core::Stream::Stream& stream) { dbgln("Type 7"); (void)stream; return ParseError::NotImplemented; } -ParseResult ElementSection::Element::parse(InputStream& stream) +ParseResult ElementSection::Element::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Element"sv); - u8 tag; - stream >> tag; - if (stream.has_any_error()) + auto tag_or_error = stream.read_value(); + 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::parse(InputStream& } } -ParseResult ElementSection::parse(InputStream& stream) +ParseResult ElementSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("ElementSection"sv); auto result = parse_vector(stream); @@ -1127,11 +1148,12 @@ ParseResult ElementSection::parse(InputStream& stream) return ElementSection { result.release_value() }; } -ParseResult Locals::parse(InputStream& stream) +ParseResult Locals::parse(Core::Stream::Stream& stream) { ScopeLogger 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::parse(InputStream& stream) return Locals { static_cast(count), type.release_value() }; } -ParseResult CodeSection::Func::parse(InputStream& stream) +ParseResult CodeSection::Func::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Func"sv); auto locals = parse_vector(stream); @@ -1156,19 +1178,15 @@ ParseResult CodeSection::Func::parse(InputStream& stream) return Func { locals.release_value(), body.release_value() }; } -ParseResult CodeSection::Code::parse(InputStream& stream) +ParseResult CodeSection::Code::parse(Core::Stream::Stream& stream) { ScopeLogger 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::parse(InputStream& stream) return Code { static_cast(size), func.release_value() }; } -ParseResult CodeSection::parse(InputStream& stream) +ParseResult CodeSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("CodeSection"sv); auto result = parse_vector(stream); @@ -1186,14 +1204,15 @@ ParseResult CodeSection::parse(InputStream& stream) return CodeSection { result.release_value() }; } -ParseResult DataSection::Data::parse(InputStream& stream) +ParseResult DataSection::Data::parse(Core::Stream::Stream& stream) { ScopeLogger logger("Data"sv); - u8 tag; - stream >> tag; - if (stream.has_any_error()) + auto tag_or_error = stream.read_value(); + 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::parse(InputStream& stream) VERIFY_NOT_REACHED(); } -ParseResult DataSection::parse(InputStream& stream) +ParseResult DataSection::parse(Core::Stream::Stream& stream) { ScopeLogger logger("DataSection"sv); auto data = parse_vector(stream); @@ -1237,12 +1256,13 @@ ParseResult DataSection::parse(InputStream& stream) return DataSection { data.release_value() }; } -ParseResult DataCountSection::parse([[maybe_unused]] InputStream& stream) +ParseResult DataCountSection::parse([[maybe_unused]] Core::Stream::Stream& stream) { ScopeLogger 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::parse([[maybe_unused]] InputStre return DataCountSection { value }; } -ParseResult Module::parse(InputStream& stream) +ParseResult Module::parse(Core::Stream::Stream& stream) { ScopeLogger 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 sections; for (;;) { - u8 section_id; - stream >> section_id; - if (stream.unreliable_eof()) { - stream.handle_any_error(); + auto section_id_or_error = stream.read_value(); + 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: diff --git a/Userland/Libraries/LibWasm/Types.h b/Userland/Libraries/LibWasm/Types.h index 95a7b1526d..f221e84750 100644 --- a/Userland/Libraries/LibWasm/Types.h +++ b/Userland/Libraries/LibWasm/Types.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -58,22 +59,23 @@ AK_TYPEDEF_DISTINCT_ORDERED_ID(size_t, LabelIndex); AK_TYPEDEF_DISTINCT_ORDERED_ID(size_t, DataIndex); AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, InstructionPointer, Arithmetic, Comparison, Flags, Increment); -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); template struct GenericIndexParser { - static ParseResult parse(InputStream& stream) + static ParseResult parse(Core::Stream::Stream& stream) { 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::ExpectedIndex); return T { value }; } }; -class ReconsumableStream : public InputStream { +class ReconsumableStream : public Core::Stream::Stream { public: - explicit ReconsumableStream(InputStream& stream) + explicit ReconsumableStream(Core::Stream::Stream& stream) : m_stream(stream) { } @@ -81,8 +83,10 @@ public: void unread(ReadonlyBytes data) { m_buffer.append(data.data(), data.size()); } private: - size_t read(Bytes bytes) override + virtual ErrorOr read(Bytes bytes) override { + auto original_bytes = bytes; + size_t bytes_read_from_buffer = 0; if (!m_buffer.is_empty()) { auto read_size = min(bytes.size(), m_buffer.size()); @@ -93,20 +97,15 @@ private: bytes_read_from_buffer = read_size; } - return m_stream.read(bytes) + bytes_read_from_buffer; + return original_bytes.trim(TRY(m_stream.read(bytes)).size() + bytes_read_from_buffer); } - bool unreliable_eof() const override + + virtual bool is_eof() const override { - return m_buffer.is_empty() && m_stream.unreliable_eof(); + return m_buffer.is_empty() && m_stream.is_eof(); } - bool read_or_error(Bytes bytes) override - { - if (read(bytes)) - return true; - set_recoverable_error(); - return false; - } - bool discard_or_error(size_t count) override + + virtual ErrorOr discard(size_t count) override { size_t bytes_discarded_from_buffer = 0; if (!m_buffer.is_empty()) { @@ -116,49 +115,74 @@ private: bytes_discarded_from_buffer = read_size; } - return m_stream.discard_or_error(count - bytes_discarded_from_buffer); + return m_stream.discard(count - bytes_discarded_from_buffer); } - InputStream& m_stream; + virtual ErrorOr write(ReadonlyBytes) override + { + return Error::from_errno(EBADF); + } + + virtual bool is_open() const override + { + return m_stream.is_open(); + } + + virtual void close() override + { + m_stream.close(); + } + + Core::Stream::Stream& m_stream; Vector m_buffer; }; -class ConstrainedStream : public InputStream { +class ConstrainedStream : public Core::Stream::Stream { public: - explicit ConstrainedStream(InputStream& stream, size_t size) + explicit ConstrainedStream(Core::Stream::Stream& stream, size_t size) : m_stream(stream) , m_bytes_left(size) { } private: - size_t read(Bytes bytes) override + ErrorOr read(Bytes bytes) override { auto to_read = min(m_bytes_left, bytes.size()); - auto nread = m_stream.read(bytes.slice(0, to_read)); - m_bytes_left -= nread; - return nread; - } - bool unreliable_eof() const override - { - return m_bytes_left == 0 || m_stream.unreliable_eof(); - } - bool read_or_error(Bytes bytes) override - { - if (read(bytes)) - return true; - set_recoverable_error(); - return false; - } - bool discard_or_error(size_t count) override - { - auto to_discard = min(m_bytes_left, count); - if (m_stream.discard_or_error(to_discard)) - m_bytes_left -= to_discard; - return to_discard; + auto read_bytes = TRY(m_stream.read(bytes.slice(0, to_read))); + m_bytes_left -= read_bytes.size(); + return read_bytes; } - InputStream& m_stream; + bool is_eof() const override + { + return m_bytes_left == 0 || m_stream.is_eof(); + } + + ErrorOr discard(size_t count) override + { + if (count > m_bytes_left) + return Error::from_string_literal("Trying to discard more bytes than allowed"); + + return m_stream.discard(count); + } + + virtual ErrorOr write(ReadonlyBytes) override + { + return Error::from_errno(EBADF); + } + + virtual bool is_open() const override + { + return m_stream.is_open(); + } + + virtual void close() override + { + m_stream.close(); + } + + Core::Stream::Stream& m_stream; size_t m_bytes_left { 0 }; }; @@ -187,7 +211,7 @@ public: auto is_numeric() const { return !is_reference(); } auto kind() const { return m_kind; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); static DeprecatedString kind_name(Kind kind) { @@ -226,7 +250,7 @@ public: auto const& types() const { return m_types; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_types; @@ -244,7 +268,7 @@ public: auto& parameters() const { return m_parameters; } auto& results() const { return m_results; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_parameters; @@ -263,7 +287,7 @@ public: auto min() const { return m_min; } auto& max() const { return m_max; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: u32 m_min { 0 }; @@ -280,7 +304,7 @@ public: auto& limits() const { return m_limits; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Limits m_limits; @@ -299,7 +323,7 @@ public: auto& limits() const { return m_limits; } auto& element_type() const { return m_element_type; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: ValueType m_element_type; @@ -318,7 +342,7 @@ public: auto& type() const { return m_type; } auto is_mutable() const { return m_is_mutable; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: ValueType m_type; @@ -364,7 +388,7 @@ public: return m_type_index; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Kind m_kind { Empty }; @@ -428,7 +452,7 @@ public: { } - static ParseResult> parse(InputStream& stream, InstructionPointer& ip); + static ParseResult> parse(Core::Stream::Stream& stream, InstructionPointer& ip); auto& opcode() const { return m_opcode; } auto& arguments() const { return m_arguments; } @@ -475,7 +499,7 @@ public: auto& name() const { return m_name; } auto& contents() const { return m_contents; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: DeprecatedString m_name; @@ -493,7 +517,7 @@ public: auto& types() const { return m_types; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_types; @@ -515,7 +539,7 @@ public: auto& name() const { return m_name; } auto& description() const { return m_description; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: template @@ -542,7 +566,7 @@ public: auto& imports() const { return m_imports; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_imports; @@ -559,7 +583,7 @@ public: auto& types() const { return m_types; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_types; @@ -576,7 +600,7 @@ public: auto& type() const { return m_type; } - static ParseResult
parse(InputStream& stream); + static ParseResult
parse(Core::Stream::Stream& stream); private: TableType m_type; @@ -592,7 +616,7 @@ public: auto& tables() const { return m_tables; }; - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector
m_tables; @@ -609,7 +633,7 @@ public: auto& type() const { return m_type; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: MemoryType m_type; @@ -625,7 +649,7 @@ public: auto& memories() const { return m_memories; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_memories; @@ -640,7 +664,7 @@ public: auto& instructions() const { return m_instructions; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_instructions; @@ -659,7 +683,7 @@ public: auto& type() const { return m_type; } auto& expression() const { return m_expression; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: GlobalType m_type; @@ -676,7 +700,7 @@ public: auto& entries() const { return m_entries; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_entries; @@ -698,7 +722,7 @@ public: auto& name() const { return m_name; } auto& description() const { return m_description; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: DeprecatedString m_name; @@ -714,7 +738,7 @@ public: auto& entries() const { return m_entries; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_entries; @@ -731,7 +755,7 @@ public: auto& index() const { return m_index; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: FunctionIndex m_index; @@ -746,7 +770,7 @@ public: auto& function() const { return m_function; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: StartFunction m_function; @@ -765,43 +789,43 @@ public: struct SegmentType0 { // FIXME: Implement me! - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); Vector function_indices; Active mode; }; struct SegmentType1 { - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); Vector function_indices; }; struct SegmentType2 { // FIXME: Implement me! - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); }; struct SegmentType3 { // FIXME: Implement me! - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); }; struct SegmentType4 { // FIXME: Implement me! - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); }; struct SegmentType5 { // FIXME: Implement me! - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); }; struct SegmentType6 { // FIXME: Implement me! - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); }; struct SegmentType7 { // FIXME: Implement me! - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); }; struct Element { - static ParseResult parse(InputStream&); + static ParseResult parse(Core::Stream::Stream&); ValueType type; Vector init; @@ -817,7 +841,7 @@ public: auto& segments() const { return m_segments; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_segments; @@ -835,7 +859,7 @@ public: auto n() const { return m_n; } auto& type() const { return m_type; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: u32 m_n { 0 }; @@ -856,7 +880,7 @@ public: auto& locals() const { return m_locals; } auto& body() const { return m_body; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_locals; @@ -873,7 +897,7 @@ public: auto size() const { return m_size; } auto& func() const { return m_func; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: u32 m_size { 0 }; @@ -889,7 +913,7 @@ public: auto& functions() const { return m_functions; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_functions; @@ -916,7 +940,7 @@ public: auto& value() const { return m_value; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Value m_value; @@ -931,7 +955,7 @@ public: auto& data() const { return m_data; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Vector m_data; @@ -948,7 +972,7 @@ public: auto& count() const { return m_count; } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: Optional m_count; @@ -1043,7 +1067,7 @@ public: StringView validation_error() const { return *m_validation_error; } void set_validation_error(DeprecatedString error) { m_validation_error = move(error); } - static ParseResult parse(InputStream& stream); + static ParseResult parse(Core::Stream::Stream& stream); private: bool populate_sections(); diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp index 5d1592bce1..e7557025c1 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp @@ -12,6 +12,7 @@ #include "WebAssemblyTableObject.h" #include "WebAssemblyTablePrototype.h" #include +#include #include #include #include @@ -118,13 +119,8 @@ JS::ThrowCompletionOr parse_module(JS::VM& vm, JS::Object* buffer_object } else { return vm.throw_completion("Not a BufferSource"); } - InputMemoryStream stream { data }; - auto module_result = Wasm::Module::parse(stream); - ScopeGuard drain_errors { - [&] { - stream.handle_any_error(); - } - }; + auto stream = Core::Stream::FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors(); + auto module_result = Wasm::Module::parse(*stream); if (module_result.is_error()) { // FIXME: Throw CompileError instead. return vm.throw_completion(Wasm::parse_error_to_deprecated_string(module_result.error())); diff --git a/Userland/Utilities/wasm.cpp b/Userland/Utilities/wasm.cpp index 0d21b98bfc..66146bc45b 100644 --- a/Userland/Utilities/wasm.cpp +++ b/Userland/Utilities/wasm.cpp @@ -252,8 +252,8 @@ static Optional parse(StringView filename) return {}; } - InputMemoryStream stream { ReadonlyBytes { result.value()->data(), result.value()->size() } }; - auto parse_result = Wasm::Module::parse(stream); + auto stream = Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { result.value()->data(), result.value()->size() }).release_value_but_fixme_should_propagate_errors(); + auto parse_result = Wasm::Module::parse(*stream); if (parse_result.is_error()) { warnln("Something went wrong, either the file is invalid, or there's a bug with LibWasm!"); warnln("The parse error was {}", Wasm::parse_error_to_deprecated_string(parse_result.error()));