Lines
100 %
Functions
Branches
//! The pre-`else` / pre-`end` seal: `unreachable` before a boundary the
//! frame reads dead, unless the last instruction already left the stack
//! polymorphic (plan 0014 slice 4.0c-3a).
use wasm_encoder::BlockType;
use super::FunctionEmitter;
const EMPTY: BlockType = BlockType::Empty;
const BLOCK: [u8; 2] = [0x02, 0x40];
const IF: [u8; 2] = [0x04, 0x40];
const ELSE: u8 = 0x05;
const END: u8 = 0x0b;
const UNREACHABLE: u8 = 0x00;
const THROW_0: [u8; 2] = [0x08, 0x00];
const CONST_1: [u8; 2] = [0x41, 0x01];
const DROP: u8 = 0x1a;
fn concat(parts: &[&[u8]]) -> Vec<u8> {
parts.concat()
}
#[test]
fn a_value_compiled_past_a_throw_is_sealed_before_end() {
let mut emit = FunctionEmitter::new();
emit.block_start();
emit.throw(0);
emit.i32_const(1);
emit.block_end().expect("matching end");
assert_eq!(
emit.bytes_ref(),
concat(&[&BLOCK, &THROW_0, &CONST_1, &[UNREACHABLE, END, UNREACHABLE]])
);
fn a_value_compiled_past_a_throw_is_sealed_before_else() {
emit.if_block(EMPTY);
emit.else_block();
concat(&[&IF, &THROW_0, &CONST_1, &[UNREACHABLE, ELSE, END]])
fn a_boundary_right_after_a_terminator_is_not_sealed_before() {
concat(&[&BLOCK, &THROW_0, &[END, UNREACHABLE]])
fn a_boundary_right_after_a_br_table_is_not_sealed_before() {
emit.br_table(&[0], 0);
concat(&[&BLOCK, &CONST_1, &[0x0e, 0x01, 0x00, 0x00, END]])
fn a_live_frame_is_not_sealed() {
emit.drop_value();
assert_eq!(emit.bytes_ref(), concat(&[&BLOCK, &CONST_1, &[DROP, END]]));
fn a_frame_opened_in_dead_code_is_sealed_before_its_end() {
emit.block_end().expect("inner end");
emit.block_end().expect("outer end");
concat(&[
&BLOCK,
&THROW_0,
&[UNREACHABLE, END, UNREACHABLE, END, UNREACHABLE]
])
fn a_splice_ending_at_a_terminator_is_not_sealed_before_end() {
let mut scratch = FunctionEmitter::new_seeded(1);
scratch.throw(0);
emit.splice(&scratch).expect("balanced scratch");
fn a_spliced_residue_is_sealed_before_end() {
scratch.i32_const(1);
fn an_empty_splice_keeps_the_terminator_polymorphic() {
emit.splice(&FunctionEmitter::new_seeded(1))
.expect("empty scratch");
fn finish_seals_a_residue_before_the_function_end() {
let body = emit.finish(&[]).expect("balanced").into_raw_body();
assert!(body.ends_with(&concat(&[&THROW_0, &CONST_1, &[UNREACHABLE, END]])));
fn finish_after_a_terminator_is_not_sealed() {
assert!(body.ends_with(&concat(&[&THROW_0, &[END]])));