1
//! The pre-`else` / pre-`end` seal: `unreachable` before a boundary the
2
//! frame reads dead, unless the last instruction already left the stack
3
//! polymorphic (plan 0014 slice 4.0c-3a).
4

            
5
use wasm_encoder::BlockType;
6

            
7
use super::FunctionEmitter;
8

            
9
const EMPTY: BlockType = BlockType::Empty;
10
const BLOCK: [u8; 2] = [0x02, 0x40];
11
const IF: [u8; 2] = [0x04, 0x40];
12
const ELSE: u8 = 0x05;
13
const END: u8 = 0x0b;
14
const UNREACHABLE: u8 = 0x00;
15
const THROW_0: [u8; 2] = [0x08, 0x00];
16
const CONST_1: [u8; 2] = [0x41, 0x01];
17
const DROP: u8 = 0x1a;
18

            
19
11
fn concat(parts: &[&[u8]]) -> Vec<u8> {
20
11
    parts.concat()
21
11
}
22

            
23
#[test]
24
1
fn a_value_compiled_past_a_throw_is_sealed_before_end() {
25
1
    let mut emit = FunctionEmitter::new();
26
1
    emit.block_start();
27
1
    emit.throw(0);
28
1
    emit.i32_const(1);
29
1
    emit.block_end().expect("matching end");
30
1
    assert_eq!(
31
1
        emit.bytes_ref(),
32
1
        concat(&[&BLOCK, &THROW_0, &CONST_1, &[UNREACHABLE, END, UNREACHABLE]])
33
    );
34
1
}
35

            
36
#[test]
37
1
fn a_value_compiled_past_a_throw_is_sealed_before_else() {
38
1
    let mut emit = FunctionEmitter::new();
39
1
    emit.if_block(EMPTY);
40
1
    emit.throw(0);
41
1
    emit.i32_const(1);
42
1
    emit.else_block();
43
1
    emit.block_end().expect("matching end");
44
1
    assert_eq!(
45
1
        emit.bytes_ref(),
46
1
        concat(&[&IF, &THROW_0, &CONST_1, &[UNREACHABLE, ELSE, END]])
47
    );
48
1
}
49

            
50
#[test]
51
1
fn a_boundary_right_after_a_terminator_is_not_sealed_before() {
52
1
    let mut emit = FunctionEmitter::new();
53
1
    emit.block_start();
54
1
    emit.throw(0);
55
1
    emit.block_end().expect("matching end");
56
1
    assert_eq!(
57
1
        emit.bytes_ref(),
58
1
        concat(&[&BLOCK, &THROW_0, &[END, UNREACHABLE]])
59
    );
60
1
}
61

            
62
#[test]
63
1
fn a_boundary_right_after_a_br_table_is_not_sealed_before() {
64
1
    let mut emit = FunctionEmitter::new();
65
1
    emit.block_start();
66
1
    emit.i32_const(1);
67
1
    emit.br_table(&[0], 0);
68
1
    emit.block_end().expect("matching end");
69
1
    assert_eq!(
70
1
        emit.bytes_ref(),
71
1
        concat(&[&BLOCK, &CONST_1, &[0x0e, 0x01, 0x00, 0x00, END]])
72
    );
73
1
}
74

            
75
#[test]
76
1
fn a_live_frame_is_not_sealed() {
77
1
    let mut emit = FunctionEmitter::new();
78
1
    emit.block_start();
79
1
    emit.i32_const(1);
80
1
    emit.drop_value();
81
1
    emit.block_end().expect("matching end");
82
1
    assert_eq!(emit.bytes_ref(), concat(&[&BLOCK, &CONST_1, &[DROP, END]]));
83
1
}
84

            
85
#[test]
86
1
fn a_frame_opened_in_dead_code_is_sealed_before_its_end() {
87
1
    let mut emit = FunctionEmitter::new();
88
1
    emit.block_start();
89
1
    emit.throw(0);
90
1
    emit.block_start();
91
1
    emit.block_end().expect("inner end");
92
1
    emit.block_end().expect("outer end");
93
1
    assert_eq!(
94
1
        emit.bytes_ref(),
95
1
        concat(&[
96
1
            &BLOCK,
97
1
            &THROW_0,
98
1
            &BLOCK,
99
1
            &[UNREACHABLE, END, UNREACHABLE, END, UNREACHABLE]
100
1
        ])
101
    );
102
1
}
103

            
104
#[test]
105
1
fn a_splice_ending_at_a_terminator_is_not_sealed_before_end() {
106
1
    let mut emit = FunctionEmitter::new();
107
1
    emit.block_start();
108
1
    let mut scratch = FunctionEmitter::new_seeded(1);
109
1
    scratch.throw(0);
110
1
    emit.splice(&scratch).expect("balanced scratch");
111
1
    emit.block_end().expect("matching end");
112
1
    assert_eq!(
113
1
        emit.bytes_ref(),
114
1
        concat(&[&BLOCK, &THROW_0, &[END, UNREACHABLE]])
115
    );
116
1
}
117

            
118
#[test]
119
1
fn a_spliced_residue_is_sealed_before_end() {
120
1
    let mut emit = FunctionEmitter::new();
121
1
    emit.block_start();
122
1
    let mut scratch = FunctionEmitter::new_seeded(1);
123
1
    scratch.throw(0);
124
1
    scratch.i32_const(1);
125
1
    emit.splice(&scratch).expect("balanced scratch");
126
1
    emit.block_end().expect("matching end");
127
1
    assert_eq!(
128
1
        emit.bytes_ref(),
129
1
        concat(&[&BLOCK, &THROW_0, &CONST_1, &[UNREACHABLE, END, UNREACHABLE]])
130
    );
131
1
}
132

            
133
#[test]
134
1
fn an_empty_splice_keeps_the_terminator_polymorphic() {
135
1
    let mut emit = FunctionEmitter::new();
136
1
    emit.block_start();
137
1
    emit.throw(0);
138
1
    emit.splice(&FunctionEmitter::new_seeded(1))
139
1
        .expect("empty scratch");
140
1
    emit.block_end().expect("matching end");
141
1
    assert_eq!(
142
1
        emit.bytes_ref(),
143
1
        concat(&[&BLOCK, &THROW_0, &[END, UNREACHABLE]])
144
    );
145
1
}
146

            
147
#[test]
148
1
fn finish_seals_a_residue_before_the_function_end() {
149
1
    let mut emit = FunctionEmitter::new();
150
1
    emit.throw(0);
151
1
    emit.i32_const(1);
152
1
    let body = emit.finish(&[]).expect("balanced").into_raw_body();
153
1
    assert!(body.ends_with(&concat(&[&THROW_0, &CONST_1, &[UNREACHABLE, END]])));
154
1
}
155

            
156
#[test]
157
1
fn finish_after_a_terminator_is_not_sealed() {
158
1
    let mut emit = FunctionEmitter::new();
159
1
    emit.throw(0);
160
1
    let body = emit.finish(&[]).expect("balanced").into_raw_body();
161
1
    assert!(body.ends_with(&concat(&[&THROW_0, &[END]])));
162
1
}