1
//! The `block_end` seal: `unreachable` after an `end` that is not
2
//! end-reachable while the validator still is (plan 0014 slice 4.0c-1a).
3

            
4
use wasm_encoder::{BlockType, Catch};
5

            
6
use super::FunctionEmitter;
7

            
8
const EMPTY: BlockType = BlockType::Empty;
9
const END: u8 = 0x0b;
10
const UNREACHABLE: u8 = 0x00;
11

            
12
const NONE: Seal = Seal::None;
13
const AFTER: Seal = Seal::After;
14

            
15
/// Where `block_end` placed an `unreachable` around one `end`.
16
#[derive(Debug, PartialEq, Eq)]
17
enum Seal {
18
    None,
19
    After,
20
    Before,
21
    Both,
22
}
23

            
24
/// Closes every open frame, innermost first, reporting each `end`'s seals.
25
8
fn close_all(emit: &mut FunctionEmitter) -> Vec<Seal> {
26
8
    let mut sealed = Vec::new();
27
24
    while emit.block_depth() > 0 {
28
16
        let start = emit.bytes_ref().len();
29
16
        emit.block_end().expect("matching end");
30
16
        sealed.push(match &emit.bytes_ref()[start..] {
31
6
            [END] => Seal::None,
32
9
            [END, UNREACHABLE] => Seal::After,
33
            [UNREACHABLE, END] => Seal::Before,
34
1
            [UNREACHABLE, END, UNREACHABLE] => Seal::Both,
35
            other => panic!("block_end emitted {other:?}"),
36
        });
37
    }
38
8
    sealed
39
8
}
40

            
41
#[test]
42
1
fn a_loop_left_only_by_its_exit_branch_is_sealed_between_the_ends() {
43
1
    let mut emit = FunctionEmitter::new();
44
1
    emit.block_start();
45
1
    emit.loop_start();
46
1
    emit.br_if(1);
47
1
    emit.br(0);
48
1
    assert_eq!(close_all(&mut emit), [AFTER, NONE]);
49
1
    assert!(emit.is_live());
50
1
    assert!(emit.is_reachable());
51
1
}
52

            
53
#[test]
54
1
fn every_dead_frame_in_a_nest_is_sealed_and_a_targeted_one_is_not() {
55
1
    let mut emit = FunctionEmitter::new();
56
1
    emit.block_start();
57
1
    emit.br_if(0);
58
1
    emit.loop_start();
59
1
    emit.loop_start();
60
1
    emit.try_table(EMPTY, &[]);
61
1
    emit.throw(0);
62
1
    assert_eq!(close_all(&mut emit), [AFTER, AFTER, AFTER, NONE]);
63
1
}
64

            
65
#[test]
66
1
fn a_try_table_whose_body_throws_is_sealed_after_its_end() {
67
1
    let mut emit = FunctionEmitter::new();
68
1
    emit.try_table(EMPTY, &[]);
69
1
    emit.throw(0);
70
1
    assert_eq!(close_all(&mut emit), [AFTER]);
71
1
    assert!(!emit.is_live());
72
1
    assert!(!emit.is_reachable());
73
1
}
74

            
75
#[test]
76
1
fn a_frame_closed_under_a_validator_dead_parent_is_not_sealed_after_end() {
77
1
    let mut emit = FunctionEmitter::new();
78
1
    emit.block_start();
79
1
    emit.unreachable();
80
1
    emit.loop_start();
81
1
    emit.br(0);
82
1
    assert_eq!(close_all(&mut emit), [NONE, Seal::Both]);
83
1
}
84

            
85
#[test]
86
1
fn a_targeted_end_is_not_sealed() {
87
1
    let mut branched = FunctionEmitter::new();
88
1
    branched.block_start();
89
1
    branched.block_start();
90
1
    branched.br(1);
91
1
    assert_eq!(close_all(&mut branched), [AFTER, NONE]);
92

            
93
1
    let mut caught = FunctionEmitter::new();
94
1
    caught.block_start();
95
1
    caught.block_start();
96
1
    caught.try_table(EMPTY, &[Catch::All { label: 1 }]);
97
1
    caught.throw(0);
98
1
    assert_eq!(close_all(&mut caught), [AFTER, AFTER, NONE]);
99
1
}
100

            
101
#[test]
102
1
fn an_if_without_else_is_not_sealed() {
103
1
    let mut emit = FunctionEmitter::new();
104
1
    emit.if_block(EMPTY);
105
1
    emit.unreachable();
106
1
    assert_eq!(close_all(&mut emit), [NONE]);
107
1
    assert!(emit.is_reachable());
108
1
}
109

            
110
#[test]
111
1
fn an_if_whose_arms_both_diverge_is_sealed() {
112
1
    let mut emit = FunctionEmitter::new();
113
1
    emit.if_block(EMPTY);
114
1
    emit.unreachable();
115
1
    emit.else_block();
116
1
    emit.br(1);
117
1
    assert_eq!(close_all(&mut emit), [AFTER]);
118
1
}