1
//! Regression: an UNWIND-PROTECT enclosing a `catch-each` must not leak its
2
//! frame into the per-item lambda helper. Before the fix,
3
//! `take_local_pool` carved out a fresh local pool for the helper body but
4
//! left the enclosing function's `unwind_frames` stack in place; a
5
//! `return-from` inside the helper then computed a target depth against the
6
//! helper's own (small) emitter depth, which made the leaked outer frame's
7
//! (large) threshold look "crossed" — so the outer cleanup got spliced into
8
//! the helper's body too, in addition to its correct copy in the enclosing
9
//! function.
10

            
11
use super::common::compile_eval_and_validate;
12
use wasmparser::{FunctionBody, Operator, Parser, Payload};
13

            
14
const REPRO: &str = r#"(block a (block b (block c (block d
15
    (unwind-protect
16
      (catch-each (list 1 2) x (block q (unwind-protect (return-from q x) 0)))
17
      (+ 0 777777))))))"#;
18

            
19
75
fn function_has_i32_const(body: &FunctionBody, value: i32) -> bool {
20
75
    let Ok(reader) = body.get_operators_reader() else {
21
        return false;
22
    };
23
75
    reader
24
75
        .into_iter()
25
2025
        .any(|op| matches!(op, Ok(Operator::I32Const { value: v }) if v == value))
26
75
}
27

            
28
1
fn functions_containing_i32_const(wasm: &[u8], value: i32) -> usize {
29
1
    Parser::new(0)
30
1
        .parse_all(wasm)
31
1
        .filter_map(Result::ok)
32
88
        .filter(|payload| {
33
88
            matches!(payload, Payload::CodeSectionEntry(body) if function_has_i32_const(body, value))
34
88
        })
35
        .count()
36
1
}
37

            
38
#[test]
39
1
fn outer_unwind_protect_cleanup_does_not_leak_into_catch_each_helper() {
40
1
    let wasm = compile_eval_and_validate(REPRO);
41
1
    assert_eq!(
42
1
        functions_containing_i32_const(&wasm, 777_777),
43
1
        1,
44
        "the outer unwind-protect's cleanup constant must be emitted only in \
45
         the enclosing function, not also in the catch-each per-item helper"
46
    );
47
1
}