diff --git a/Libraries/LibWasm/Parser/Parser.cpp b/Libraries/LibWasm/Parser/Parser.cpp index 62c07d5b136..27fa47c7841 100644 --- a/Libraries/LibWasm/Parser/Parser.cpp +++ b/Libraries/LibWasm/Parser/Parser.cpp @@ -35,7 +35,7 @@ ParseError with_eof_check(Stream const& stream, ParseError error_if_not_eof) } template -static auto parse_vector(Stream& stream) +static auto parse_vector(ConstrainedStream& stream) { ScopeLogger logger; if constexpr (requires { T::parse(stream); }) { @@ -88,7 +88,7 @@ static auto parse_vector(Stream& stream) } } -static ParseResult parse_name(Stream& stream) +static ParseResult parse_name(ConstrainedStream& stream) { ScopeLogger logger; auto data = TRY(parse_vector(stream)); @@ -123,14 +123,14 @@ ParseResult ValueType::parse(Stream& stream) } } -ParseResult ResultType::parse(Stream& stream) +ParseResult ResultType::parse(ConstrainedStream& stream) { ScopeLogger logger("ResultType"sv); auto types = TRY(parse_vector(stream)); return ResultType { types }; } -ParseResult FunctionType::parse(Stream& stream) +ParseResult FunctionType::parse(ConstrainedStream& stream) { ScopeLogger logger("FunctionType"sv); auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag); @@ -146,7 +146,7 @@ ParseResult FunctionType::parse(Stream& stream) return FunctionType { parameters_result, results_result }; } -ParseResult Limits::parse(Stream& stream) +ParseResult Limits::parse(ConstrainedStream& stream) { ScopeLogger logger("Limits"sv); auto flag = TRY_READ(stream, u8, ParseError::ExpectedKindTag); @@ -170,14 +170,14 @@ ParseResult Limits::parse(Stream& stream) return Limits { static_cast(min), move(max) }; } -ParseResult MemoryType::parse(Stream& stream) +ParseResult MemoryType::parse(ConstrainedStream& stream) { ScopeLogger logger("MemoryType"sv); auto limits_result = TRY(Limits::parse(stream)); return MemoryType { limits_result }; } -ParseResult TableType::parse(Stream& stream) +ParseResult TableType::parse(ConstrainedStream& stream) { ScopeLogger logger("TableType"sv); auto type_result = TRY(ValueType::parse(stream)); @@ -187,7 +187,7 @@ ParseResult TableType::parse(Stream& stream) return TableType { type_result, limits_result }; } -ParseResult GlobalType::parse(Stream& stream) +ParseResult GlobalType::parse(ConstrainedStream& stream) { ScopeLogger logger("GlobalType"sv); auto type_result = TRY(ValueType::parse(stream)); @@ -199,7 +199,7 @@ ParseResult GlobalType::parse(Stream& stream) return GlobalType { type_result, mutable_ == 0x01 }; } -ParseResult BlockType::parse(Stream& stream) +ParseResult BlockType::parse(ConstrainedStream& stream) { ScopeLogger logger("BlockType"sv); auto kind = TRY_READ(stream, u8, ParseError::ExpectedKindTag); @@ -228,10 +228,12 @@ ParseResult BlockType::parse(Stream& stream) return BlockType { TypeIndex(index_value) }; } -ParseResult Instruction::parse(Stream& stream) +ParseResult Instruction::parse(ConstrainedStream& stream) { ScopeLogger logger("Instruction"sv); - auto byte = TRY_READ(stream, u8, ParseError::ExpectedKindTag); + u8 byte; + if (auto result = stream.read_some({ &byte, 1 }); result.is_error() || result.value().size() != 1) + return ParseError::ExpectedKindTag; OpCode opcode { byte }; @@ -858,38 +860,36 @@ ParseResult Instruction::parse(Stream& stream) return ParseError::UnknownInstruction; } -ParseResult CustomSection::parse(Stream& stream) +ParseResult CustomSection::parse(ConstrainedStream& stream) { ScopeLogger logger("CustomSection"sv); auto name = TRY(parse_name(stream)); - - ByteBuffer data_buffer; - if (data_buffer.try_resize(64).is_error()) + auto remaining = stream.remaining(); + auto maybe_data_buffer = ByteBuffer::create_uninitialized(remaining); + if (maybe_data_buffer.is_error()) return ParseError::OutOfMemory; + auto data_buffer = maybe_data_buffer.release_value(); - while (!stream.is_eof()) { - char buf[16]; - auto span_or_error = stream.read_some({ 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()) - return ParseError::HugeAllocationRequested; - } + 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; return CustomSection(name, move(data_buffer)); } -ParseResult TypeSection::parse(Stream& stream) +ParseResult TypeSection::parse(ConstrainedStream& stream) { ScopeLogger logger("TypeSection"sv); auto types = TRY(parse_vector(stream)); return TypeSection { types }; } -ParseResult ImportSection::Import::parse(Stream& stream) +ParseResult ImportSection::Import::parse(ConstrainedStream& stream) { ScopeLogger logger("Import"sv); auto module = TRY(parse_name(stream)); @@ -912,14 +912,14 @@ ParseResult ImportSection::Import::parse(Stream& stream) } } -ParseResult ImportSection::parse(Stream& stream) +ParseResult ImportSection::parse(ConstrainedStream& stream) { ScopeLogger logger("ImportSection"sv); auto imports = TRY(parse_vector(stream)); return ImportSection { imports }; } -ParseResult FunctionSection::parse(Stream& stream) +ParseResult FunctionSection::parse(ConstrainedStream& stream) { ScopeLogger logger("FunctionSection"sv); auto indices = TRY(parse_vector(stream)); @@ -932,35 +932,35 @@ ParseResult FunctionSection::parse(Stream& stream) return FunctionSection { move(typed_indices) }; } -ParseResult TableSection::Table::parse(Stream& stream) +ParseResult TableSection::Table::parse(ConstrainedStream& stream) { ScopeLogger logger("Table"sv); auto type = TRY(TableType::parse(stream)); return Table { type }; } -ParseResult TableSection::parse(Stream& stream) +ParseResult TableSection::parse(ConstrainedStream& stream) { ScopeLogger logger("TableSection"sv); auto tables = TRY(parse_vector(stream)); return TableSection { tables }; } -ParseResult MemorySection::Memory::parse(Stream& stream) +ParseResult MemorySection::Memory::parse(ConstrainedStream& stream) { ScopeLogger logger("Memory"sv); auto type = TRY(MemoryType::parse(stream)); return Memory { type }; } -ParseResult MemorySection::parse(Stream& stream) +ParseResult MemorySection::parse(ConstrainedStream& stream) { ScopeLogger logger("MemorySection"sv); auto memories = TRY(parse_vector(stream)); return MemorySection { memories }; } -ParseResult Expression::parse(Stream& stream, Optional size_hint) +ParseResult Expression::parse(ConstrainedStream& stream, Optional size_hint) { ScopeLogger logger("Expression"sv); @@ -1002,7 +1002,7 @@ ParseResult Expression::parse(Stream& stream, Optional size_ return Expression { move(instructions) }; } -ParseResult GlobalSection::Global::parse(Stream& stream) +ParseResult GlobalSection::Global::parse(ConstrainedStream& stream) { ScopeLogger logger("Global"sv); auto type = TRY(GlobalType::parse(stream)); @@ -1010,14 +1010,14 @@ ParseResult GlobalSection::Global::parse(Stream& stream) return Global { type, exprs }; } -ParseResult GlobalSection::parse(Stream& stream) +ParseResult GlobalSection::parse(ConstrainedStream& stream) { ScopeLogger logger("GlobalSection"sv); auto result = TRY(parse_vector(stream)); return GlobalSection { result }; } -ParseResult ExportSection::Export::parse(Stream& stream) +ParseResult ExportSection::Export::parse(ConstrainedStream& stream) { ScopeLogger logger("Export"sv); auto name = TRY(parse_name(stream)); @@ -1039,28 +1039,28 @@ ParseResult ExportSection::Export::parse(Stream& stream) } } -ParseResult ExportSection::parse(Stream& stream) +ParseResult ExportSection::parse(ConstrainedStream& stream) { ScopeLogger logger("ExportSection"sv); auto result = TRY(parse_vector(stream)); return ExportSection { result }; } -ParseResult StartSection::StartFunction::parse(Stream& stream) +ParseResult StartSection::StartFunction::parse(ConstrainedStream& stream) { ScopeLogger logger("StartFunction"sv); auto index = TRY(GenericIndexParser::parse(stream)); return StartFunction { index }; } -ParseResult StartSection::parse(Stream& stream) +ParseResult StartSection::parse(ConstrainedStream& stream) { ScopeLogger logger("StartSection"sv); auto result = TRY(StartFunction::parse(stream)); return StartSection { result }; } -ParseResult ElementSection::Element::parse(Stream& stream) +ParseResult ElementSection::Element::parse(ConstrainedStream& stream) { ScopeLogger logger("Element"sv); auto tag = TRY_READ(stream, LEB128, ParseError::ExpectedKindTag); @@ -1116,14 +1116,14 @@ ParseResult ElementSection::Element::parse(Stream& stre return Element { type, move(items), move(mode) }; } -ParseResult ElementSection::parse(Stream& stream) +ParseResult ElementSection::parse(ConstrainedStream& stream) { ScopeLogger logger("ElementSection"sv); auto result = TRY(parse_vector(stream)); return ElementSection { result }; } -ParseResult Locals::parse(Stream& stream) +ParseResult Locals::parse(ConstrainedStream& stream) { ScopeLogger logger("Locals"sv); auto count = TRY_READ(stream, LEB128, ParseError::InvalidSize); @@ -1136,7 +1136,7 @@ ParseResult Locals::parse(Stream& stream) return Locals { count, type }; } -ParseResult CodeSection::Func::parse(Stream& stream, size_t size_hint) +ParseResult CodeSection::Func::parse(ConstrainedStream& stream, size_t size_hint) { ScopeLogger logger("Func"sv); auto locals = TRY(parse_vector(stream)); @@ -1144,7 +1144,7 @@ ParseResult CodeSection::Func::parse(Stream& stream, size_t s return Func { move(locals), move(body) }; } -ParseResult CodeSection::Code::parse(Stream& stream) +ParseResult CodeSection::Code::parse(ConstrainedStream& stream) { ScopeLogger logger("Code"sv); auto size = TRY_READ(stream, LEB128, ParseError::InvalidSize); @@ -1156,14 +1156,14 @@ ParseResult CodeSection::Code::parse(Stream& stream) return Code { size, move(func) }; } -ParseResult CodeSection::parse(Stream& stream) +ParseResult CodeSection::parse(ConstrainedStream& stream) { ScopeLogger logger("CodeSection"sv); auto result = TRY(parse_vector(stream)); return CodeSection { move(result) }; } -ParseResult DataSection::Data::parse(Stream& stream) +ParseResult DataSection::Data::parse(ConstrainedStream& stream) { ScopeLogger logger("Data"sv); auto tag = TRY_READ(stream, LEB128, ParseError::ExpectedKindTag); @@ -1189,14 +1189,14 @@ ParseResult DataSection::Data::parse(Stream& stream) VERIFY_NOT_REACHED(); } -ParseResult DataSection::parse(Stream& stream) +ParseResult DataSection::parse(ConstrainedStream& stream) { ScopeLogger logger("DataSection"sv); auto data = TRY(parse_vector(stream)); return DataSection { data }; } -ParseResult DataCountSection::parse([[maybe_unused]] Stream& stream) +ParseResult DataCountSection::parse(ConstrainedStream& stream) { ScopeLogger logger("DataCountSection"sv); auto value_or_error = stream.read_value>(); diff --git a/Libraries/LibWasm/Types.h b/Libraries/LibWasm/Types.h index b82c333b959..d95275b256e 100644 --- a/Libraries/LibWasm/Types.h +++ b/Libraries/LibWasm/Types.h @@ -217,7 +217,7 @@ public: auto const& types() const { return m_types; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_types; @@ -235,7 +235,7 @@ public: auto& parameters() const { return m_parameters; } auto& results() const { return m_results; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_parameters; @@ -259,7 +259,7 @@ public: && (!other.max().has_value() || (m_max.has_value() && *m_max <= *other.max())); } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: u32 m_min { 0 }; @@ -276,7 +276,7 @@ public: auto& limits() const { return m_limits; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Limits m_limits; @@ -295,7 +295,7 @@ public: auto& limits() const { return m_limits; } auto& element_type() const { return m_element_type; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: ValueType m_element_type; @@ -314,7 +314,7 @@ public: auto& type() const { return m_type; } auto is_mutable() const { return m_is_mutable; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: ValueType m_type; @@ -360,7 +360,7 @@ public: return m_type_index; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Kind m_kind { Empty }; @@ -461,7 +461,7 @@ public: { } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); auto& opcode() const { return m_opcode; } auto& arguments() const { return m_arguments; } @@ -543,7 +543,7 @@ public: auto& name() const { return m_name; } auto& contents() const { return m_contents; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: ByteString m_name; @@ -561,7 +561,7 @@ public: auto& types() const { return m_types; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_types; @@ -583,7 +583,7 @@ public: auto& name() const { return m_name; } auto& description() const { return m_description; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: template @@ -608,7 +608,7 @@ public: auto& imports() const { return m_imports; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_imports; @@ -625,7 +625,7 @@ public: auto& types() const { return m_types; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_types; @@ -642,7 +642,7 @@ public: auto& type() const { return m_type; } - static ParseResult
parse(Stream& stream); + static ParseResult
parse(ConstrainedStream& stream); private: TableType m_type; @@ -658,7 +658,7 @@ public: auto& tables() const { return m_tables; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector
m_tables; @@ -675,7 +675,7 @@ public: auto& type() const { return m_type; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: MemoryType m_type; @@ -691,7 +691,7 @@ public: auto& memories() const { return m_memories; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_memories; @@ -706,7 +706,7 @@ public: auto& instructions() const { return m_instructions; } - static ParseResult parse(Stream& stream, Optional size_hint = {}); + static ParseResult parse(ConstrainedStream& stream, Optional size_hint = {}); private: Vector m_instructions; @@ -725,7 +725,7 @@ public: auto& type() const { return m_type; } auto& expression() const { return m_expression; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: GlobalType m_type; @@ -742,7 +742,7 @@ public: auto& entries() const { return m_entries; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_entries; @@ -764,7 +764,7 @@ public: auto& name() const { return m_name; } auto& description() const { return m_description; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: ByteString m_name; @@ -780,7 +780,7 @@ public: auto& entries() const { return m_entries; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_entries; @@ -797,7 +797,7 @@ public: auto& index() const { return m_index; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: FunctionIndex m_index; @@ -812,7 +812,7 @@ public: auto& function() const { return m_function; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Optional m_function; @@ -830,7 +830,7 @@ public: }; struct Element { - static ParseResult parse(Stream&); + static ParseResult parse(ConstrainedStream&); ValueType type; Vector init; @@ -846,7 +846,7 @@ public: auto& segments() const { return m_segments; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_segments; @@ -864,7 +864,7 @@ public: auto n() const { return m_n; } auto& type() const { return m_type; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: u32 m_n { 0 }; @@ -885,7 +885,7 @@ public: auto& locals() const { return m_locals; } auto& body() const { return m_body; } - static ParseResult parse(Stream& stream, size_t size_hint); + static ParseResult parse(ConstrainedStream& stream, size_t size_hint); private: Vector m_locals; @@ -902,7 +902,7 @@ public: auto size() const { return m_size; } auto& func() const { return m_func; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: u32 m_size { 0 }; @@ -918,7 +918,7 @@ public: auto& functions() const { return m_functions; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_functions; @@ -945,7 +945,7 @@ public: auto& value() const { return m_value; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Value m_value; @@ -960,7 +960,7 @@ public: auto& data() const { return m_data; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Vector m_data; @@ -977,7 +977,7 @@ public: auto& count() const { return m_count; } - static ParseResult parse(Stream& stream); + static ParseResult parse(ConstrainedStream& stream); private: Optional m_count;