From 8668851cb1c517fd09f0119e09eea3340fe574fa Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Mon, 3 Jul 2023 12:25:18 +0330 Subject: [PATCH] LibWasm: Do not allow self-referencial globals This is written in a somewhat roundabout way in the spec, so the initial implementation did not implement it correctly. --- .../LibWasm/AbstractMachine/Validator.cpp | 15 ++++++++++++++- .../Libraries/LibWasm/AbstractMachine/Validator.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp b/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp index 557668cfb6..8a317c64e6 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -56,7 +57,10 @@ ErrorOr Validator::validate(Module& module) }, [this](TableType const& type) { m_context.tables.append(type); }, [this](MemoryType const& type) { m_context.memories.append(type); }, - [this](GlobalType const& type) { m_context.globals.append(type); }); + [this](GlobalType const& type) { + m_globals_without_internal_globals.append(type); + m_context.globals.append(type); + }); } }); @@ -93,6 +97,7 @@ ErrorOr Validator::validate(Module& module) for (auto& memory : section.memories()) m_context.memories.unchecked_append(memory.type()); }); + module.for_each_section_of_type([this](GlobalSection const& section) { m_context.globals.ensure_capacity(m_context.globals.size() + section.entries().size()); for (auto& global : section.entries()) @@ -213,12 +218,20 @@ ErrorOr Validator::validate(ElementSection const& section return Errors::invalid("active element initializer type"sv, ValueType(ValueType::I32), expression_result.result_types); return {}; })); + + for (auto& expression : segment.init) { + auto result = TRY(validate(expression, { segment.type })); + if (!result.is_constant) + return Errors::invalid("element initializer"sv); + } } return {}; } ErrorOr Validator::validate(GlobalSection const& section) { + TemporaryChange omit_internal_globals { m_context.globals, m_globals_without_internal_globals }; + for (auto& entry : section.entries()) { auto& type = entry.type(); TRY(validate(type)); diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h index 809ef2cd9b..fd9c1fca8f 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h @@ -334,6 +334,7 @@ private: Vector m_entered_scopes; Vector m_block_details; Vector m_entered_blocks; + Vector m_globals_without_internal_globals; }; }