Lines
95.24 %
Functions
100 %
Branches
//! Regression: an UNWIND-PROTECT enclosing a `catch-each` must not leak its
//! frame into the per-item lambda helper. Before the fix,
//! `take_local_pool` carved out a fresh local pool for the helper body but
//! left the enclosing function's `unwind_frames` stack in place; a
//! `return-from` inside the helper then computed a target depth against the
//! helper's own (small) emitter depth, which made the leaked outer frame's
//! (large) threshold look "crossed" — so the outer cleanup got spliced into
//! the helper's body too, in addition to its correct copy in the enclosing
//! function.
use super::common::compile_eval_and_validate;
use wasmparser::{FunctionBody, Operator, Parser, Payload};
const REPRO: &str = r#"(block a (block b (block c (block d
(unwind-protect
(catch-each (list 1 2) x (block q (unwind-protect (return-from q x) 0)))
(+ 0 777777))))))"#;
fn function_has_i32_const(body: &FunctionBody, value: i32) -> bool {
let Ok(reader) = body.get_operators_reader() else {
return false;
};
reader
.into_iter()
.any(|op| matches!(op, Ok(Operator::I32Const { value: v }) if v == value))
}
fn functions_containing_i32_const(wasm: &[u8], value: i32) -> usize {
Parser::new(0)
.parse_all(wasm)
.filter_map(Result::ok)
.filter(|payload| {
matches!(payload, Payload::CodeSectionEntry(body) if function_has_i32_const(body, value))
})
.count()
#[test]
fn outer_unwind_protect_cleanup_does_not_leak_into_catch_each_helper() {
let wasm = compile_eval_and_validate(REPRO);
assert_eq!(
functions_containing_i32_const(&wasm, 777_777),
1,
"the outer unwind-protect's cleanup constant must be emitted only in \
the enclosing function, not also in the catch-each per-item helper"
);