srctree

Robin Linden parent 86912372 32145798
wasm: Fix local.set w/ missing arg incorrectly being valid

inlinesplit
wasm/validation.cpp added: 21, removed: 3, total 18
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2024 David Zero <zero-one@zer0-one.net>
// SPDX-FileCopyrightText: 2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -399,7 +400,9 @@ tl::expected<void, ValidationError> validate_function(std::uint32_t func_idx,
return tl::unexpected{ValidationError::LocalUndefined};
}
 
v.pop_val_expect(func_code.locals[ls->idx].type);
if (auto pop_res = v.pop_val_expect(func_code.locals[ls->idx].type); !pop_res.has_value()) {
return tl::unexpected{pop_res.error()};
}
} else if (LocalTee const *lt = std::get_if<LocalTee>(&inst)) {
if (lt->idx >= func_code.locals.size()) {
return tl::unexpected{ValidationError::LocalUndefined};
 
wasm/validation_test.cpp added: 21, removed: 3, total 18
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2024 David Zero <zero-one@zer0-one.net>
// SPDX-FileCopyrightText: 2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -182,5 +183,19 @@ int main() {
a.expect(validate(m).has_value());
});
 
s.add_test("Function: localset & localget, valid", [=](etest::IActions &a) mutable {
m.code_section->entries[0].code = {I32Const{42}, LocalSet{.idx = 0}, LocalGet{.idx = 0}};
m.code_section->entries[0].locals = {{.count = 1, .type = ValueType::Int32}};
 
a.expect(validate(m).has_value());
});
 
s.add_test("Function: localset & localget, missing arg", [=](etest::IActions &a) mutable {
m.code_section->entries[0].code = {LocalSet{.idx = 0}, LocalGet{.idx = 0}};
m.code_section->entries[0].locals = {{.count = 1, .type = ValueType::Int32}};
 
a.expect_eq(validate(m), tl::unexpected{ValidationError::ValueStackUnderflow});
});
 
return s.run();
}