From ed9e7f1ad033404901a231fb5840225bef137581 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 9 Dec 2021 21:32:01 +0330 Subject: [PATCH] LibWasm: Tolerate modules with invalid sections and mark them as invalid We should not crash, but rather just fail to verify them. --- Userland/Libraries/LibWasm/Parser/Parser.cpp | 13 ++++++++++--- Userland/Libraries/LibWasm/Types.h | 7 +++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibWasm/Parser/Parser.cpp b/Userland/Libraries/LibWasm/Parser/Parser.cpp index d2ae4d4561..19558070c8 100644 --- a/Userland/Libraries/LibWasm/Parser/Parser.cpp +++ b/Userland/Libraries/LibWasm/Parser/Parser.cpp @@ -1363,16 +1363,22 @@ ParseResult Module::parse(InputStream& stream) return Module { move(sections) }; } -void Module::populate_sections() +bool Module::populate_sections() { + auto is_ok = true; FunctionSection const* function_section { nullptr }; for_each_section_of_type([&](FunctionSection const& section) { function_section = §ion; }); for_each_section_of_type([&](CodeSection const& section) { - // FIXME: This should be considered invalid once validation is implemented. - if (!function_section) + if (!function_section) { + is_ok = false; return; + } size_t index = 0; for (auto& entry : section.functions()) { + if (function_section->types().size() <= index) { + is_ok = false; + return; + } auto& type_index = function_section->types()[index]; Vector locals; for (auto& local : entry.func().locals()) { @@ -1383,6 +1389,7 @@ void Module::populate_sections() ++index; } }); + return is_ok; } String parse_error_to_string(ParseError error) diff --git a/Userland/Libraries/LibWasm/Types.h b/Userland/Libraries/LibWasm/Types.h index 1332333271..4677be889c 100644 --- a/Userland/Libraries/LibWasm/Types.h +++ b/Userland/Libraries/LibWasm/Types.h @@ -1003,7 +1003,10 @@ public: explicit Module(Vector sections) : m_sections(move(sections)) { - populate_sections(); + if (!populate_sections()) { + m_validation_status = ValidationStatus::Invalid; + m_validation_error = "Failed to populate module sections"sv; + } } auto& sections() const { return m_sections; } @@ -1044,7 +1047,7 @@ public: static ParseResult parse(InputStream& stream); private: - void populate_sections(); + bool populate_sections(); void set_validation_status(ValidationStatus status) { m_validation_status = status; } Vector m_sections;