From 6e891822c5b93bf69f195360b0e92f0f81a63864 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Wed, 28 Apr 2021 07:02:37 +0430 Subject: [PATCH] LibWasm: Implement parsing of the DataCount section With this, the parser should technically be able to parse all wasm modules. Any parse failure on correct modules should henceforth be labelled a bug :^) --- Userland/Libraries/LibWasm/Parser/Parser.cpp | 20 +++++++++++++++++-- .../Libraries/LibWasm/Printer/Printer.cpp | 2 ++ Userland/Libraries/LibWasm/Types.h | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWasm/Parser/Parser.cpp b/Userland/Libraries/LibWasm/Parser/Parser.cpp index e7532181ba..d588d02d1d 100644 --- a/Userland/Libraries/LibWasm/Parser/Parser.cpp +++ b/Userland/Libraries/LibWasm/Parser/Parser.cpp @@ -1066,8 +1066,16 @@ ParseResult DataSection::parse(InputStream& stream) ParseResult DataCountSection::parse([[maybe_unused]] InputStream& stream) { ScopeLogger logger("DataCountSection"); - // FIXME: Implement parsing optional values! - return with_eof_check(stream, ParseError::NotImplemented); + u32 value; + if (!LEB128::read_unsigned(stream, value)) { + if (stream.unreliable_eof()) { + // The section simply didn't contain anything. + return DataCountSection { {} }; + } + return ParseError::ExpectedSize; + } + + return DataCountSection { value }; } ParseResult Module::parse(InputStream& stream) @@ -1203,6 +1211,14 @@ ParseResult Module::parse(InputStream& stream) return section.error(); } } + case DataCountSection::section_id: { + if (auto section = DataCountSection::parse(section_stream); !section.is_error()) { + sections.append(section.release_value()); + continue; + } else { + return section.error(); + } + } default: return with_eof_check(stream, ParseError::InvalidIndex); } diff --git a/Userland/Libraries/LibWasm/Printer/Printer.cpp b/Userland/Libraries/LibWasm/Printer/Printer.cpp index 6c6d4c4128..fa6ac369c9 100644 --- a/Userland/Libraries/LibWasm/Printer/Printer.cpp +++ b/Userland/Libraries/LibWasm/Printer/Printer.cpp @@ -783,4 +783,6 @@ HashMap Wasm::Names::instruction_names { { Instructions::table_grow, "table.grow" }, { Instructions::table_size, "table.size" }, { Instructions::table_fill, "table.fill" }, + { Instructions::structured_else, "synthetic:else" }, + { Instructions::structured_end, "synthetic:end" }, }; diff --git a/Userland/Libraries/LibWasm/Types.h b/Userland/Libraries/LibWasm/Types.h index 8dd52e8ac2..2954a06638 100644 --- a/Userland/Libraries/LibWasm/Types.h +++ b/Userland/Libraries/LibWasm/Types.h @@ -945,7 +945,8 @@ public: StartSection, ElementSection, CodeSection, - DataSection>; + DataSection, + DataCountSection>; static constexpr Array wasm_magic { 0, 'a', 's', 'm' }; static constexpr Array wasm_version { 1, 0, 0, 0 };