Lines
97.4 %
Functions
100 %
Branches
//! The `block_end` seal: `unreachable` after an `end` that is not
//! end-reachable while the validator still is (plan 0014 slice 4.0c-1a).
use wasm_encoder::{BlockType, Catch};
use super::FunctionEmitter;
const EMPTY: BlockType = BlockType::Empty;
const END: u8 = 0x0b;
const UNREACHABLE: u8 = 0x00;
const NONE: Seal = Seal::None;
const AFTER: Seal = Seal::After;
/// Where `block_end` placed an `unreachable` around one `end`.
#[derive(Debug, PartialEq, Eq)]
enum Seal {
None,
After,
Before,
Both,
}
/// Closes every open frame, innermost first, reporting each `end`'s seals.
fn close_all(emit: &mut FunctionEmitter) -> Vec<Seal> {
let mut sealed = Vec::new();
while emit.block_depth() > 0 {
let start = emit.bytes_ref().len();
emit.block_end().expect("matching end");
sealed.push(match &emit.bytes_ref()[start..] {
[END] => Seal::None,
[END, UNREACHABLE] => Seal::After,
[UNREACHABLE, END] => Seal::Before,
[UNREACHABLE, END, UNREACHABLE] => Seal::Both,
other => panic!("block_end emitted {other:?}"),
});
sealed
#[test]
fn a_loop_left_only_by_its_exit_branch_is_sealed_between_the_ends() {
let mut emit = FunctionEmitter::new();
emit.block_start();
emit.loop_start();
emit.br_if(1);
emit.br(0);
assert_eq!(close_all(&mut emit), [AFTER, NONE]);
assert!(emit.is_live());
assert!(emit.is_reachable());
fn every_dead_frame_in_a_nest_is_sealed_and_a_targeted_one_is_not() {
emit.br_if(0);
emit.try_table(EMPTY, &[]);
emit.throw(0);
assert_eq!(close_all(&mut emit), [AFTER, AFTER, AFTER, NONE]);
fn a_try_table_whose_body_throws_is_sealed_after_its_end() {
assert_eq!(close_all(&mut emit), [AFTER]);
assert!(!emit.is_live());
assert!(!emit.is_reachable());
fn a_frame_closed_under_a_validator_dead_parent_is_not_sealed_after_end() {
emit.unreachable();
assert_eq!(close_all(&mut emit), [NONE, Seal::Both]);
fn a_targeted_end_is_not_sealed() {
let mut branched = FunctionEmitter::new();
branched.block_start();
branched.br(1);
assert_eq!(close_all(&mut branched), [AFTER, NONE]);
let mut caught = FunctionEmitter::new();
caught.block_start();
caught.try_table(EMPTY, &[Catch::All { label: 1 }]);
caught.throw(0);
assert_eq!(close_all(&mut caught), [AFTER, AFTER, NONE]);
fn an_if_without_else_is_not_sealed() {
emit.if_block(EMPTY);
assert_eq!(close_all(&mut emit), [NONE]);
fn an_if_whose_arms_both_diverge_is_sealed() {
emit.else_block();
emit.br(1);