1
//! A HANDLER-CASE whose body and every clause diverge has no fall-through
2
//! value: the module must validate and each diverging region be sealed with
3
//! `unreachable` (ADR-0026, plan 0014 S18).
4

            
5
use wasmparser::{BlockType, Operator, Parser, Payload};
6

            
7
use super::common::compile_and_validate;
8

            
9
const ALL_DIVERGING: &str = r#"(handler-case (error 'boom "m") (boom (e) (error 'bam "n")))"#;
10
const RETURN_FROM: &str =
11
    "(block b (progn (handler-case (return-from b 1) (boom (e) (return-from b 2))) 5))";
12
const NESTED: &str = r#"(handler-case
13
  (handler-case (error 'boom "m") (boom (e) (error 'bam "n")))
14
  (bam (e) (error 'bim "o")))"#;
15

            
16
#[derive(Clone, Copy, PartialEq)]
17
enum Region {
18
    ResultTryTable,
19
    If,
20
    Other,
21
}
22

            
23
#[derive(Default)]
24
struct Seals {
25
    sealed_try_tables: usize,
26
    unsealed_try_tables: usize,
27
    sealed_raising_clauses: usize,
28
}
29

            
30
/// Typed try_tables are the HANDLER-CASE bodies (the nomi-eval boundary
31
/// wrapper is `Empty`), sealed right after their `end`; an `if` ending
32
/// `throw; unreachable` is a clause that re-raises (the host-guard `if`s end
33
/// in a `call`).
34
5
fn seals(wasm: &[u8]) -> Seals {
35
5
    let mut found = Seals::default();
36
5
    for payload in Parser::new(0).parse_all(wasm).filter_map(Result::ok) {
37
435
        let Payload::CodeSectionEntry(body) = payload else {
38
60
            continue;
39
        };
40
375
        let mut regions = Vec::new();
41
375
        let mut prev = Vec::new();
42
375
        let mut closed_try_table = false;
43
375
        for op in body.get_operators_reader().expect("operators reader") {
44
10683
            let op = op.expect("operator");
45
10683
            if std::mem::take(&mut closed_try_table) {
46
6
                match op {
47
6
                    Operator::Unreachable => found.sealed_try_tables += 1,
48
                    _ => found.unsealed_try_tables += 1,
49
                }
50
10677
            }
51
10683
            match &op {
52
11
                Operator::TryTable { try_table } if try_table.ty != BlockType::Empty => {
53
6
                    regions.push(Region::ResultTryTable);
54
                }
55
365
                Operator::If { .. } => regions.push(Region::If),
56
                Operator::TryTable { .. } | Operator::Block { .. } | Operator::Loop { .. } => {
57
83
                    regions.push(Region::Other);
58
                }
59
                Operator::End => {
60
829
                    let sealed = matches!(prev.last(), Some(Operator::Unreachable));
61
829
                    let raised = matches!(prev.iter().rev().nth(1), Some(Operator::Throw { .. }))
62
809
                        || matches!(prev.iter().rev().nth(2), Some(Operator::Throw { .. }));
63
829
                    match regions.pop() {
64
6
                        Some(Region::ResultTryTable) => closed_try_table = true,
65
365
                        Some(Region::If) if sealed && raised => found.sealed_raising_clauses += 1,
66
818
                        _ => {}
67
                    }
68
                }
69
9400
                _ => {}
70
            }
71
10683
            prev.push(op);
72
        }
73
    }
74
5
    found
75
5
}
76

            
77
5
fn assert_sealed(src: &str, try_tables: usize, raising_clauses: usize) {
78
5
    let found = seals(&compile_and_validate(src));
79
5
    assert_eq!(found.unsealed_try_tables, 0, "{src}");
80
5
    assert_eq!(found.sealed_try_tables, try_tables, "{src}");
81
5
    assert_eq!(found.sealed_raising_clauses, raising_clauses, "{src}");
82
5
}
83

            
84
#[test]
85
1
fn all_diverging_validates_and_seals_body_and_clause() {
86
1
    assert_sealed(ALL_DIVERGING, 1, 1);
87
1
}
88

            
89
#[test]
90
1
fn all_diverging_inside_a_defun_validates_and_seals() {
91
1
    assert_sealed(&format!("(defun f () {ALL_DIVERGING}) (f)"), 1, 1);
92
1
}
93

            
94
#[test]
95
1
fn all_diverging_as_an_if_test_validates_and_seals() {
96
1
    assert_sealed(&format!("(if {ALL_DIVERGING} 1 2)"), 1, 1);
97
1
}
98

            
99
// Not in tail position, so `return-from` is a real `br` out of the form.
100
#[test]
101
1
fn return_from_body_and_clause_validates_and_seals() {
102
1
    assert_sealed(RETURN_FROM, 1, 0);
103
1
}
104

            
105
// `form_diverges` does not classify the outer body as diverging; the frame
106
// does, so both try_tables are sealed after their `end`.
107
#[test]
108
1
fn nested_all_diverging_validates_and_seals_both_forms() {
109
1
    assert_sealed(NESTED, 2, 2);
110
1
}