From 15effbaf2a4675df48f2c929b2b77581649f1b82 Mon Sep 17 00:00:00 2001 From: Diego <96022404+dzfrias@users.noreply.github.com> Date: Mon, 10 Jun 2024 16:10:28 -0700 Subject: [PATCH] LibWasm: Validate that function and code sections match (cherry picked from commit cda0d6d27746672058b26b0e61d0900dc73b1fb3) --- Userland/Libraries/LibWasm/Parser/Parser.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWasm/Parser/Parser.cpp b/Userland/Libraries/LibWasm/Parser/Parser.cpp index 4e8283157a..39efe2f57e 100644 --- a/Userland/Libraries/LibWasm/Parser/Parser.cpp +++ b/Userland/Libraries/LibWasm/Parser/Parser.cpp @@ -1483,12 +1483,17 @@ bool Module::populate_sections() { auto is_ok = true; FunctionSection const* function_section { nullptr }; + bool seen_code_section = false; for_each_section_of_type([&](FunctionSection const& section) { function_section = §ion; }); for_each_section_of_type([&](CodeSection const& section) { - if (!function_section) { + if (!function_section && section.functions().is_empty()) { + return; + } + if (!function_section || function_section->types().size() != section.functions().size()) { is_ok = false; return; } + seen_code_section = true; size_t index = 0; for (auto& entry : section.functions()) { if (function_section->types().size() <= index) { @@ -1505,6 +1510,8 @@ bool Module::populate_sections() ++index; } }); + if (!seen_code_section && function_section && !function_section->types().is_empty()) + return false; return is_ok; }