Lines
98.08 %
Functions
100 %
Branches
//! A HANDLER-CASE whose body and every clause diverge has no fall-through
//! value: the module must validate and each diverging region be sealed with
//! `unreachable` (ADR-0026, plan 0014 S18).
use wasmparser::{BlockType, Operator, Parser, Payload};
use super::common::compile_and_validate;
const ALL_DIVERGING: &str = r#"(handler-case (error 'boom "m") (boom (e) (error 'bam "n")))"#;
const RETURN_FROM: &str =
"(block b (progn (handler-case (return-from b 1) (boom (e) (return-from b 2))) 5))";
const NESTED: &str = r#"(handler-case
(handler-case (error 'boom "m") (boom (e) (error 'bam "n")))
(bam (e) (error 'bim "o")))"#;
#[derive(Clone, Copy, PartialEq)]
enum Region {
ResultTryTable,
If,
Other,
}
#[derive(Default)]
struct Seals {
sealed_try_tables: usize,
unsealed_try_tables: usize,
sealed_raising_clauses: usize,
/// Typed try_tables are the HANDLER-CASE bodies (the nomi-eval boundary
/// wrapper is `Empty`), sealed right after their `end`; an `if` ending
/// `throw; unreachable` is a clause that re-raises (the host-guard `if`s end
/// in a `call`).
fn seals(wasm: &[u8]) -> Seals {
let mut found = Seals::default();
for payload in Parser::new(0).parse_all(wasm).filter_map(Result::ok) {
let Payload::CodeSectionEntry(body) = payload else {
continue;
};
let mut regions = Vec::new();
let mut prev = Vec::new();
let mut closed_try_table = false;
for op in body.get_operators_reader().expect("operators reader") {
let op = op.expect("operator");
if std::mem::take(&mut closed_try_table) {
match op {
Operator::Unreachable => found.sealed_try_tables += 1,
_ => found.unsealed_try_tables += 1,
match &op {
Operator::TryTable { try_table } if try_table.ty != BlockType::Empty => {
regions.push(Region::ResultTryTable);
Operator::If { .. } => regions.push(Region::If),
Operator::TryTable { .. } | Operator::Block { .. } | Operator::Loop { .. } => {
regions.push(Region::Other);
Operator::End => {
let sealed = matches!(prev.last(), Some(Operator::Unreachable));
let raised = matches!(prev.iter().rev().nth(1), Some(Operator::Throw { .. }))
|| matches!(prev.iter().rev().nth(2), Some(Operator::Throw { .. }));
match regions.pop() {
Some(Region::ResultTryTable) => closed_try_table = true,
Some(Region::If) if sealed && raised => found.sealed_raising_clauses += 1,
_ => {}
prev.push(op);
found
fn assert_sealed(src: &str, try_tables: usize, raising_clauses: usize) {
let found = seals(&compile_and_validate(src));
assert_eq!(found.unsealed_try_tables, 0, "{src}");
assert_eq!(found.sealed_try_tables, try_tables, "{src}");
assert_eq!(found.sealed_raising_clauses, raising_clauses, "{src}");
#[test]
fn all_diverging_validates_and_seals_body_and_clause() {
assert_sealed(ALL_DIVERGING, 1, 1);
fn all_diverging_inside_a_defun_validates_and_seals() {
assert_sealed(&format!("(defun f () {ALL_DIVERGING}) (f)"), 1, 1);
fn all_diverging_as_an_if_test_validates_and_seals() {
assert_sealed(&format!("(if {ALL_DIVERGING} 1 2)"), 1, 1);
// Not in tail position, so `return-from` is a real `br` out of the form.
fn return_from_body_and_clause_validates_and_seals() {
assert_sealed(RETURN_FROM, 1, 0);
// `form_diverges` does not classify the outer body as diverging; the frame
// does, so both try_tables are sealed after their `end`.
fn nested_all_diverging_validates_and_seals_both_forms() {
assert_sealed(NESTED, 2, 2);