1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +00:00

LibWasm: Tolerate modules with invalid sections and mark them as invalid

We should not crash, but rather just fail to verify them.
This commit is contained in:
Ali Mohammad Pur 2021-12-09 21:32:01 +03:30 committed by Ali Mohammad Pur
parent 2db27be2df
commit ed9e7f1ad0
2 changed files with 15 additions and 5 deletions

View file

@ -1363,16 +1363,22 @@ ParseResult<Module> Module::parse(InputStream& stream)
return Module { move(sections) }; return Module { move(sections) };
} }
void Module::populate_sections() bool Module::populate_sections()
{ {
auto is_ok = true;
FunctionSection const* function_section { nullptr }; FunctionSection const* function_section { nullptr };
for_each_section_of_type<FunctionSection>([&](FunctionSection const& section) { function_section = &section; }); for_each_section_of_type<FunctionSection>([&](FunctionSection const& section) { function_section = &section; });
for_each_section_of_type<CodeSection>([&](CodeSection const& section) { for_each_section_of_type<CodeSection>([&](CodeSection const& section) {
// FIXME: This should be considered invalid once validation is implemented. if (!function_section) {
if (!function_section) is_ok = false;
return; return;
}
size_t index = 0; size_t index = 0;
for (auto& entry : section.functions()) { for (auto& entry : section.functions()) {
if (function_section->types().size() <= index) {
is_ok = false;
return;
}
auto& type_index = function_section->types()[index]; auto& type_index = function_section->types()[index];
Vector<ValueType> locals; Vector<ValueType> locals;
for (auto& local : entry.func().locals()) { for (auto& local : entry.func().locals()) {
@ -1383,6 +1389,7 @@ void Module::populate_sections()
++index; ++index;
} }
}); });
return is_ok;
} }
String parse_error_to_string(ParseError error) String parse_error_to_string(ParseError error)

View file

@ -1003,7 +1003,10 @@ public:
explicit Module(Vector<AnySection> sections) explicit Module(Vector<AnySection> sections)
: m_sections(move(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; } auto& sections() const { return m_sections; }
@ -1044,7 +1047,7 @@ public:
static ParseResult<Module> parse(InputStream& stream); static ParseResult<Module> parse(InputStream& stream);
private: private:
void populate_sections(); bool populate_sections();
void set_validation_status(ValidationStatus status) { m_validation_status = status; } void set_validation_status(ValidationStatus status) { m_validation_status = status; }
Vector<AnySection> m_sections; Vector<AnySection> m_sections;