1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:07:35 +00:00

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.
This commit is contained in:
Ali Mohammad Pur 2023-07-03 12:25:18 +03:30 committed by Ali Mohammad Pur
parent 4f0f1c7c72
commit 8668851cb1
2 changed files with 15 additions and 1 deletions

View file

@ -7,6 +7,7 @@
#include <AK/HashTable.h> #include <AK/HashTable.h>
#include <AK/Result.h> #include <AK/Result.h>
#include <AK/SourceLocation.h> #include <AK/SourceLocation.h>
#include <AK/TemporaryChange.h>
#include <AK/Try.h> #include <AK/Try.h>
#include <LibWasm/AbstractMachine/Validator.h> #include <LibWasm/AbstractMachine/Validator.h>
#include <LibWasm/Printer/Printer.h> #include <LibWasm/Printer/Printer.h>
@ -56,7 +57,10 @@ ErrorOr<void, ValidationError> Validator::validate(Module& module)
}, },
[this](TableType const& type) { m_context.tables.append(type); }, [this](TableType const& type) { m_context.tables.append(type); },
[this](MemoryType const& type) { m_context.memories.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<void, ValidationError> Validator::validate(Module& module)
for (auto& memory : section.memories()) for (auto& memory : section.memories())
m_context.memories.unchecked_append(memory.type()); m_context.memories.unchecked_append(memory.type());
}); });
module.for_each_section_of_type<GlobalSection>([this](GlobalSection const& section) { module.for_each_section_of_type<GlobalSection>([this](GlobalSection const& section) {
m_context.globals.ensure_capacity(m_context.globals.size() + section.entries().size()); m_context.globals.ensure_capacity(m_context.globals.size() + section.entries().size());
for (auto& global : section.entries()) for (auto& global : section.entries())
@ -213,12 +218,20 @@ ErrorOr<void, ValidationError> Validator::validate(ElementSection const& section
return Errors::invalid("active element initializer type"sv, ValueType(ValueType::I32), expression_result.result_types); return Errors::invalid("active element initializer type"sv, ValueType(ValueType::I32), expression_result.result_types);
return {}; return {};
})); }));
for (auto& expression : segment.init) {
auto result = TRY(validate(expression, { segment.type }));
if (!result.is_constant)
return Errors::invalid("element initializer"sv);
}
} }
return {}; return {};
} }
ErrorOr<void, ValidationError> Validator::validate(GlobalSection const& section) ErrorOr<void, ValidationError> Validator::validate(GlobalSection const& section)
{ {
TemporaryChange omit_internal_globals { m_context.globals, m_globals_without_internal_globals };
for (auto& entry : section.entries()) { for (auto& entry : section.entries()) {
auto& type = entry.type(); auto& type = entry.type();
TRY(validate(type)); TRY(validate(type));

View file

@ -334,6 +334,7 @@ private:
Vector<ChildScopeKind> m_entered_scopes; Vector<ChildScopeKind> m_entered_scopes;
Vector<BlockDetails> m_block_details; Vector<BlockDetails> m_block_details;
Vector<FunctionType> m_entered_blocks; Vector<FunctionType> m_entered_blocks;
Vector<GlobalType> m_globals_without_internal_globals;
}; };
} }