Lines
100 %
Functions
Branches
use super::{FrameKind, ValidationFrame};
#[test]
fn unreachable_marks_the_current_frame() {
let mut frame = ValidationFrame::new();
assert!(frame.is_reachable());
frame.mark_unreachable();
assert!(!frame.is_reachable());
}
fn br_then_unreachable_then_end_leaves_the_enclosing_frame_reachable() {
// `(block (br 0)) <next>` — the block's OWN body goes unreachable
// after the `br`, but falling off the block's `end` must restore
// whatever reachability the ENCLOSING frame already had.
frame.push_ctrl(FrameKind::Block);
assert!(!frame.is_reachable(), "body is unreachable after br");
frame.pop_ctrl().expect("close the block");
assert!(
frame.is_reachable(),
"the enclosing frame's reachability must not inherit the closed \
frame's own unreachable flag"
);
fn end_without_incoming_branches_stays_reachable() {
// `(block <plain body>)` — no br ever targets it; falling through its
// `end` must leave the enclosing frame reachable, exactly as the
// "with incoming branches" case above does. Pinning both cases to
// the same outcome is the actual regression: a naive implementation
// that leaked the child's flag upward would pass one and fail the
// other.
fn throw_marks_unreachable_like_br() {
fn else_resets_reachability_for_the_alternate_arm() {
frame.push_ctrl(FrameKind::If);
frame.mark_unreachable(); // `then` arm ends with `br` / `unreachable`
frame.reset_for_else();
"the else arm is a fresh branch, not a continuation of `then`'s \
unreachable tail"
fn a_frame_entered_from_unreachable_code_still_starts_reachable() {
// Matches the wasm validation algorithm's `push_ctrl`: `unreachable` is
// always seeded `false` on entry, regardless of the enclosing frame's
// own state. Dead code nested inside other dead code is validated
// strictly against its own fresh, polymorphism-free base until
// something inside IT marks it unreachable in turn.
frame.push_ctrl(FrameKind::Block); // entered while already unreachable
"a newly entered frame starts reachable even from unreachable code"
fn pop_ctrl_never_pops_the_base_frame() {
let err = frame.pop_ctrl().expect_err("no open frame to pop");
assert!(matches!(err, crate::error::Error::Compile(_)));
fn a_branch_to_a_loop_does_not_mark_its_end() {
frame.push_ctrl(FrameKind::Loop);
frame.mark_targets([0, 1]);
assert!(!frame.nested[1].end_targeted, "a loop's label is its start");
assert!(frame.nested[0].end_targeted, "the block's label is its end");
fn every_kind_but_loop_is_marked_at_any_depth() {
for kind in [
FrameKind::Block,
FrameKind::If,
FrameKind::Loop,
FrameKind::TryTable,
] {
frame.push_ctrl(kind);
frame.mark_targets([0, 1, 1, 2, 3, 4, 5, 6, 7]);
let marked: Vec<bool> = frame.nested.iter().map(|f| f.end_targeted).collect();
assert_eq!(marked, [true, true, false, true, false, true, true, true]);